Changing IP address using the command line

There is no doubt that all of you, my readers, can set a static IP address on any Windows machine. It’s so easy task. However, I’ve got a trick question – can you quickly change the IP address? What’s more, can you perform this operation on multiple computers in a short time?

Yes, you can do that manually. However, doing this manually we can make a mistake. You can become bored or simply type a wrong number. Great way to make a havoc.

If you try to do this manually, you will need to access every single machine, open a Network and sharing center applet, locate your network card, open its properties, find the IP v4 settings and change them according to your needs. Then close the dialog. This will take a few minutes.

Now you need to repeat this process on every Windows host that you have. Let’s assume that you have 5, 10 or 20 servers. Or that you need to change the IP address on your laptop to the address related to each customer’s network.

clip_image002

A long, long time ago in a faraway land, I worked on an exciting project of the enterprise WAN network. There was 20 or 21 Windows DC servers, one per every site. I made the configuration scripts that in a split second place correct configuration for P address, DNS server(s), DNS replication, WINS servers and replication, and so on.

I recently revived this mighty trick, while working on the one of my latest projects. I needed to setup the static IP address on my laptop while working with the customer. Then I went back in the office and I needed to change the IP address back to DHCP. Furthermore, I found that I need to change addresses on some 8 servers to avoid address conflicts. With the command line, this was finished in less than 10 minutes, including logging to remote servers.

 

Using the NetSh command

Microsoft introduced the NetSh command (shorten from NETwork SHell) with Windows 2000. This command is actually a complete shell for the network settings. All the settings you can see in the GUI are here. In addition, you can find some additional settings only through this shell.

Many users avoid using it due its complexity for learning and plethora of the options. I personally found it easy to use. Once when you picked the basics and learned to navigate through its subcommands, you will unleash the full power of the networking. You can learn a lot about this command from this Microsoft TechNet page.

In addition, you can learn a lot about this command if you set everything in the GUI and then export these settings in the netsh configuration script from the command line (I will use c:\temp folder in this example):

netsh dump > c:\temp\netshcfg.txt

Open the elevated command prompt. We need to change just the IP address on the NIC. In this example we don’t need default gateway nor DNS servers. However, we can set even these parameters.

We need to assign the IP address 192.168.38.100 with the network mask 255.255.255.192 to the NIC. We can do that with the command netsh interface ip. This is the same as IPv4.

Please, keep in mind that we need to change the existing IP address settings, not to add the new one.

What’s the difference? When we changing an existing IP settings, we replacing the current values with the new values. In case that we want to add a new value, we will keep an existing settings and add these new aside. This is useful when we need to associate more than one IP address with the same NIC.

 

Setting new IP address

Before we can execute the command to set the new values, we need to know the name of the interface. The command is

netsh interface ip show interface

We will see the list of all available interfaces, similar to this one:

Idx     Met         MTU          State                Name
---  ----------  ----------  ------------  ---------------------------
 20          25        1500  disconnected  Wi-Fi
 26           1        1500  disconnected  Ethernet
  6          25        1500  disconnected  Local Area Connection* 2
  1          75  4294967295  connected     Loopback Pseudo-Interface 1
 24          25        1500  connected     Ethernet 2
 29          25        1500  connected     Contoso VN
 10          25        1500  connected     FabrikaM VN
  3          25        1500  connected     Virtual ISP #1
 18          25        1500  connected     Virtual ISP #2

In this case, my network card is the second in the list, named Ethernet. Knowing this and the IP address, I will execute the following command

netsh interface ip set address "Ethernet" static 192.168.38.100 255.255.255.192

That’s it. The IP address is set. The command static indicates that we want the static IP address. Then we need to specify the value of the IP address and network mask. We can use the keywords address= and mask=, but this is not necessary.

We can check it with the command ipconfig and we will see these new parameters:

Ethernet adapter Ethernet:

Connection-specific DNS Suffix . :

IPv4 Address. . . . . . . . . . . : 192.168.38.100

Subnet Mask . . . . . . . . . . . : 255.255.255.192

Default Gateway . . . . . . . . . :

 

Reverting to DHCP mode

When we finish our work, we can switch the NIC back to the DHCP mode. The command is similar to previous:

netsh interface ip set address "Ethernet" dhcp

This command will delete the static IP address and it will initiate a search for the IP address using the DHCP mechanism.

 

But I need also default gateway!

We can set this value too. Just add its value in the end of the first command.

netsh interface ip set address "Ethernet" static 192.168.38.100 255.255.255.192 192.168.38.65

The latest parameter will be recognized as the IP address of the default gateway.

 

If you want it permanent

The address set in this way will last until the next reboot, except that we mark it permanent. To do so, you need to add the parameter store=persistent:

netsh interface ip set address "Ethernet" static 192.168.38.100 255.255.255.192 192.168.38.65 store=persistent

 

And the DNS servers in the end

In case that we need to add the addresses of the DNS servers, we can use similar commands. We can assign the parameters for the DNS servers in two ways – dynamically, using a DHCP service, or statically.

For the DHCP mode we need to execute the command:

netsh interface ip set dnsservers "Ethernet" dhcp

The difference is in the subcommand dnsservers instead address. We can also use the abbreviated form – dns. In addition, we have less parameters to setup here.

For the static DNS settings we need to type a few more parameters:

netsh interface ip set dnsservers "Ethernet" static 192.168.35.10

When we need to register the name of the host with the DNS server, we can add the parameter register=primary. This will initiate the DNS registration with the DNS server that holds the DNS zone corresponding to the primary DNS suffix of our machine. You can always add this parameter when specifying the static parameters.

 

Let the NETSH be with you!

Now you can use this command to configure the static IP address on every Windows machine. Even more, this command can be useful during the troubleshooting process. You can export the current configuration and examine it carefully. Even more, you can reset the configuration to defaults.

Explore additional options and possibilities. You will find an exciting new world of the networking on Windows.

Stay tuned.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s