Change port on windows with cmd command line

As a topology element within the network and between its devices and applications, ports are assigned specific numbers. Usually, a number should be allocated to each service and/or application to indicate where data will be sent and from where it will be received. In a standard setup of any Windows OS, it is sufficient to mention the numbers of HTTP (port 80) or HTTPS (port 443) services, as such ports are already registered within the system. There are circumstances, however, when these ports should be modified. You may wish to eliminate ports that may potentially conflict, avoid using commonly used ports for security reasons, or employ non-standard ports for specific software. Knowing the procedure improves the likelihood of smooth procedures without interference on services that exist.

Steps to Change a Port Using CMD

Accessing Command Prompt with Administrator Rights

To modify port settings, you must open the Command Prompt as an administrator. This ensures you have the necessary permissions to make changes to system settings.

Identifying the Service Associated with the Port

Before changing a port, identify which service or application is using the current port. Use the command:

netstat -ano | findstr :<port_number>

Replace <port_number> with the specific port to locate its process ID (PID). Then, use the following command to find the process name:

tasklist | findstr <PID>

Stopping the Service Temporarily

Stop the service using the port to avoid conflicts during the modification process. This can be done with:

net stop <service_name>

Changing the Port Configuration

Edit the port settings using the following steps:

  1. Navigate to the service configuration file or registry settings using CMD.
  2. Modify the relevant entries to replace the old port with the new one. For instance, to modify the HTTP service, update its configuration file or registry key.

Configuring Firewall Rules for the New Port

Add a new firewall rule to allow traffic on the updated port:

netsh advfirewall firewall add rule name="New Port Rule" protocol=TCP dir=in localport=<new_port> action=allow

Restarting the Service

After updating the port and firewall rules, restart the service:

net start <service_name>

Testing the New Configuration

Confirm that the new port is working by checking the service status or using the following command:

telnet <ip_address> <new_port>

Commands for Port Modification

Viewing Current Port Usage

To check which ports are currently in use and identify associated processes, use:

netstat -ano

For specific ports, filter the output with:

netstat -ano | findstr :<port_number>

Stopping Services Using a Port

If a service is occupying the port you want to modify, stop it using:

net stop <service_name>

Replace <service_name> with the actual service name.

Changing Port for a Service

To update the port for a specific service, use commands depending on its configuration. For example:

  • HTTP Service: Modify the port in the registry:
    reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" /v <new_port> /t REG_SZ /d <value>
  • SQL Server: Use the SQL configuration tool via CMD or PowerShell.

Adding a Firewall Rule for a New Port

To allow traffic through a newly configured port:

netsh advfirewall firewall add rule name="Allow New Port" protocol=TCP dir=in localport=<new_port> action=allow

Removing an Existing Port Rule

To delete a rule for an old or unwanted port:

netsh advfirewall firewall delete rule name="Old Port Rule"

Verifying Configuration

Test if the new port is open and operational with:

telnet <ip_address> <new_port>

If telnet is unavailable, you can install it using:

dism /online /Enable-Feature /FeatureName:TelnetClient

Preparing to Change Ports

Before making any changes to ports in Windows, it’s essential to prepare your system to avoid errors and disruptions. First, identify the current usage of ports on your system. You can do this by running the netstat -ano command, which provides a list of all active ports and their associated processes. Take note of the ports you plan to modify and verify that they are not being used by critical services.

Next, ensure you have administrative privileges on your system, as modifying ports typically requires elevated permissions. It’s also recommended to create a system restore point or backup your registry settings before making any changes. This precaution allows you to revert to a stable configuration in case of an error.

Finally, check your firewall and network settings to ensure that the desired port is not blocked. This step will prevent connectivity issues once the port has been updated. With these preparations complete, you can proceed confidently to modify the required port settings.

Troubleshooting Common Issues

Resolving “Port Already in Use” Error

If a port is already in use and conflicts with your desired configuration, identify the process occupying the port:

netstat -ano | findstr :<port_number>

Note the PID and terminate the associated process:

taskkill /PID <PID> /F

Ensure no critical system services are stopped during this process.

Addressing Firewall Restrictions

If the port change is not effective, the issue may be related to firewall settings. Verify and add rules for the updated port:

netsh advfirewall firewall add rule name="Allow New Port" protocol=TCP dir=in localport=<new_port> action=allow

Fixing Service Restart Errors

If a service fails to restart after a port modification, check its configuration file or registry settings. Ensure the new port is correctly specified and does not conflict with existing configurations.

Checking Application Compatibility

Sometimes, third-party applications do not respond to port changes. Double-check their configuration files or documentation for proper port modification steps.

Validating Network Accessibility

Use telnet to ensure the new port is accessible:

telnet <ip_address> <new_port>

If telnet fails, check your router or network firewall for additional restrictions.

Undoing Changes in Case of Errors

If the changes cause system instability, revert to the original port by re-editing the configuration or registry:

reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" /v <original_port> /t REG_SZ /d <value>

Conclusion

Changing ports in Windows through the command line is a powerful way to manage system configurations. By understanding the steps involved and preparing thoroughly, you can avoid common pitfalls and ensure seamless implementation.

Begin by identifying the ports you want to modify and checking their current usage. Take appropriate measures to stop any processes or services using the port before proceeding with the changes. Utilize commands like netsh to manage firewall rules and regedit for registry-based configurations.

Troubleshooting potential issues, such as “port already in use” errors or application compatibility problems, is also an essential part of the process. With careful attention to each step and the right troubleshooting techniques, you can confidently reconfigure ports to meet your system’s needs.

Share:

More Posts

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments