When provisioning an Azure virtual machine connected to an Azure network, the machine receives an IP address within the network address space field. Unfortunately, the IP provided is dynamic and provided via an Azure DHCP platform.But, we need to keep in mind some aspects:
- If you restart your VM, the IP will remain the same
- If you shutdown your VM from the operating system, the machine will be stopped. And when you start back your machine, the VM will will remain the same too.
- If you shutdown your VM from the Azure portal or via an Azure powershell command or API, the machine will be stopped. When you start back your VM, the machine may receive a new IP address
- If an Azure host or hardware failure occurs, the Azure controller will try to start the machine on a healthy host. The VM may receive a new IP address
The question is why ?
The answer can simply be resumed on two sentences:
- The IP address is dynamic, so don’t be surprised that one day, you figure-out that the IP has changed 🙂
- There are two types of VM shutdown :
- Shutdown without de-provisioning: When you shutdown your VM from the OS itself, the VM is shuttled down but Azure will keep it resources ready (IP, VIP…). The bad news is that Azure will keep billing this VM even if it’s really stopped and dos not use compute resources.
- Shutdown with de-provisioning : If you shutdown your VM for the Azure portal, Powershell command or using Azure API, the machine will be stopped and de-provisoned (De-allocated). All its non-static resources will be lost, certainly i’m taling about IP and VIP
Look her for more information about this interesting topic : HERE
To avoid loosing your IP configuration, make your life easier by fixing the IP addresses of your VM.
Until this time, fixing a VM IP address can only be done using powershell. I created this simple Powershell script that can be used to fix your IP address.
- Download and install the Azure Powershell modules HERE
- Copy the script to a local location (Example : C:\Scripts)
- Run an elevated Powershell window
- change the powershell script execution policy to unrestricted (Set-ExecutionPolicy Unrestricted)
- You should to provide the following information
- Browse the folder where the script resides and run it using the following syntax
- .\SetAzureVMStaticIP.ps1 -SubscriptionName ‘AAAA’ -VMName ‘BBBB’ -ServiceName ‘CCCC’ -VNETName ‘DDDD’ -IP ‘EEEE’
.\SetAzureVMStaticIP.ps1 -SubscriptionName ‘BuildWindows SAMIR FARHAT’ -VMName ‘Renault4L01’ -ServiceName ‘Renault4L01’ -VNETName ‘VNET01’ -IP ‘126.96.36.199’
- This script will only work on VMs with one single NIC. I will soon post another script that support fixing IP addresses for VMs with multiple NICs
- Azure will restart your machine to apply this modification, so be aware of it