Jun 172019

ADM

How to change computer's IP address from command line

  • June 17 2019
  • ADM

 

 

How to change computer's IP address from command line - windows

 

In Windows, to change the IP address from Control Panel is quite easy, but when the IP needs to be changed often then it goes back on the command line because is easy to be saved as a .bat or .cmd file and can be executed as many times as needed.

The command line tool used to change the IP is netsh which allows you to configure just about any aspect of your network connections in Windows. In order to use the tool the command line needs to be started as Administrator:

  • in Windows 10 or 8.1, right-click the Start menu (or press Windows+X on your keyboard) and choose "Command Prompt (Admin)."
  • In previous versions of Windows, search Start for "command prompt" and then right-click the result and choose "Run as Administrator."

Additionally, to this, you might also need to change the DNS settings.

Step 1 - View Your Network Information

To change the IP address first you need to find the full name of the network for the interface you want to do the change. For this, type the following command:

netsh interface ipv4 show config

The result of the command will display all the network interfaces available on the computer.

How to change computer's IP address from command line - IPAddress001

In my case, I am looking for the "Local Area Connection". My current IP address is 192.168.66.129.

Step2 - Change IP Address, Subnet Mask, and Default Gateway

The syntax to change the IP is:

netsh interface ipv4 set address name="THE INTERFACE NAME" static IP_ADDRESS SUBNET_MASK GATEWAY

For my case:

netsh interface ipv4 set address name="Local Area Connection" static 192.168.66.100 255.255.255.0 192.168.66.2

Note: to still have internet connectivity I will use the same Default Gateway.

If you need to change the IP to automatic run the following command:

netsh interface ip set address "Local Area Connection" dhcp

Step 3 - Check the Configuration

To check the configuration we check again the settings by running the show config command:

netsh interface ipv4 show config

How to change computer's IP address from command line - IPAddress002

You can notice the change of the IP address for the "Local Area Connection" interface.

Step 4 - Add the commands in a .bat file(optional)

The .bat file will contain the command to change the IP followed by the show config command to see the changes directly on the console. For the.bat file to be successfully executed needs also to be under Administrator rights.

netsh interface ipv4 set address name="Local Area Connection" static 192.168.66.100 255.255.255.0 192.168.66.2
ping -n 3 127.0.0.1 >nul
netsh interface ipv4 show config

Note: the ping command is just to wait 3 seconds until the show config command is called. without it, you might not have all the info. Running again the show config command will display the info correctly.

References