Changing an IP address of Azure virtual machines isn’t a complicated task. Simply locate the VM in Azure Portal and change the IP of it. A little bit more complicated is the scenario when we would like to switch IP addresses between two existing virtual machines. This scenario may be suitable many times like VM maintenance, licenses management, restore from VM image, etc.
One option we have is to use nic-ipconfig CLI. But I will show you another option which works without scripting. We will exchange NIC (Network Interfaces) between VMs and this will also switch IP addresses associated with them.
We will play with two virtual machines: int-worker and int-worker2. Our goal is to take the current IP address of the int-worker VM and assign it to int-worker2 and the opposite.
1. Check our VMs status first
Both VMs are running now.
Go to Networking and check settings for both VMs.
We can see that:
– int-worker VM is attached to NIC dev-worker399 with IP address starting 52.166.
– int-worker2 VM is attached to NIC dev-worker2167 with IP address starting 40.91.
2. We must stop both VMs before we can detach/attach NIC from it. VMs must have status Stopped (deallocated).
3. Because we can’t have VM without at least one NIC attached to it we must create another new NIC as spare. Search for “Network interface” in Azure Portal and add a new one. It must be in the same resource group and virtual network as our VMs.
We have to call this NIC “dummy-nic” that we can search for it easily and is clear to everyone it was created just for maintenance purposes.
4. Go to the Networking configuration section of the int-worker VM and attach the newly created dummy-nic to it.
5. Because the VM has 2 NICs assigned now, one of them can be detached. Detach NIC dev-worker399.
6. Go to the same setting screen of int-worker2 VM and attach the dev-worker399 NIC to it. It should appear in the selector because the NIC is currently not assigned to any VM and can be used for attaching it to a new one.
7. Next, you detach the original NIC from int-worker2 VM and continue the same path to assign it to int-worker VM and detach dummy-nic from it.
8. Check the new IP address of VMs. As you can see int-worker2 VM has the IP address assigned which was originally assigned to int-worker VM.