Hello all,

By default, each Virtual Machine in Azure has a private IP address. That IP address comes from Virtual Network embedded DHCP and, by default, is configured as Dynamic. In some scenarios, private dynamic IP address is not appropriate and you need to configure private IP address to be static.

In on-premise infrastructure,  you will (most probably) just select one of available IPs and configure in machine directly. In Azure that is not the best idea, because if you change IP address in machine directly, Azure network will not be aware of that IP address. That can disrupt your access to virtual machine over virtual machine public IP.

If you need to configure private IP address as static, you need to change the IP configuration on NIC level, on Azure, and set Static in IP configuration. That is the same as the Reservation in DHCP and although private IP address will be received from Azure Network DHCP, it will always be the same. If you still need “real” static private IP, you need to configure it directly in machine, but if you have previously configured static private IP in NIC IP configuration, access to the virtual machine will be available. Of course, you need to configure same IP address in NIC IP configuration, as well as in virtual machine.

Changing NIC IP configuration using Azure Portal is pretty straight forward. Once you select desired NIC,  you just need to open IP configuration from blade.

Then you need to see what configuration need to be changed, and change per your needs.

Of course, life is easier if you want to use PowerShell 🙂

# Define variables
$RGName       = "ResourceGroup"
$NICName      = "EU-IIS-1-nic"

# Set private IP address to static
$NetworkInterface = Get-AzNetworkInterface -Name $NICName -ResourceGroupName $RGName
$NetworkInterface.IpConfigurations[0].PrivateIpAllocationMethod = "Static"
$NetworkInterface | Set-AzNetworkInterface

Cheers!

Please follow and like us:

One thought on “How to set static IP address for Virtual Machines”

  1. Extended to function with some error handling ~
    Function Find-EmptyString {
    [CmdletBinding()]
    param (
    [Parameter(Mandatory=$true)]
    [AllowNull()]
    [Alias(“Variable”)]
    $VariableName,
    [Parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string]$Message,
    [Parameter(Mandatory=$false)]
    [ValidateNotNullOrEmpty()]
    [string]$Action = “Break”
    )
    process {
    if ([string]::IsNullOrWhiteSpace(“$VariableName”)) {
    $Addition = ” Stopping.”
    $FinalMessage = $Message + $Addition
    if ($Action -eq “Break”) {
    Write-Warning -Message “$FinalMessage”
    Break
    }
    else {
    Write-Warning -Message “$FinalMessage”
    Continue
    }
    }
    }
    }
    Function Set-AzVmPrivateStaticIp {
    [CmdletBinding()]
    param (
    # Name of the VM
    [Parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string]$Vm,
    # Name of the VM’s NIC
    [Parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string]$NicName

    )
    process {
    $ConfirmVm = (Get-AzResource -ResourceType “Microsoft.Compute/virtualMachines” -Name $Vm -ErrorAction SilentlyContinue)
    Find-EmptyString -Variable $ConfirmVm -Message “Cannot find instance of the VM with the name $VM.”
    $ConfirmNic = (Get-AzNetworkInterface -Name $NicName -ResourceGroupName $ConfirmVM.ResourceGroupName -ErrorAction SilentlyContinue)
    Find-EmptyString -Variable $ConfirmNic -Message “Cannot find instance of the NIC with the name $NicName.”
    try {
    $ConfirmNic.IpConfigurations[0].PrivateIpAllocationMethod = “Static”
    $ConfirmNic | Set-AzNetworkInterface
    }
    catch {
    Write-Error “$_” -ErrorAction Stop
    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.