If using the CLI locally in Bash, sign in to Azure with az login
.
Use the az network nic ip-config update command to associate a public IP address to an IP configuration. The following example associates an existing public IP address named myPublicIP to an IP configuration named ipconfig1. This IP configuration belongs to an existing network interface named myVMNic in a resource group named myResourceGroup.
az network nic ip-config update \
--name ipconfig1 \
--nic-name myVMNic \
--resource-group myResourceGroup \
--public-ip-address myPublicIP
If you don't have an existing public IP address, use the az network public-ip create command to create one. For example, the following command creates a public IP address named myPublicIP in a resource group named myResourceGroup.
az network public-ip create --name myPublicIP --resource-group myResourceGroup
Note
This command creates a public IP address with default values for several settings that you may want to customize. For more information about public IP address settings, see Create a public IP address. An IP address is assigned from the pool of public IP addresses reserved for an Azure region. For a list of the address pools used in each region, see Azure IP ranges and service tags. If you need the address to be assigned from a specific prefix, use a Public IP address prefix.
If you don't know the names of the network interfaces attached to your VM, use the az vm nic list command to view them. For example, the following command lists the network interfaces attached to a VM named myVM in a resource group named myResourceGroup:
az vm nic list --vm-name myVM --resource-group myResourceGroup
The output includes one or more lines that are similar to the following example, where myVMNic is the name of the network interface:
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myVMNic",
If you don't know the names of the IP configurations for a network interface, use the az network nic ip-config list command to retrieve them. For example, the following command lists the names of the IP configurations for a network interface named myVMNic in a resource group named myResourceGroup:
az network nic ip-config list --nic-name myVMNic --resource-group myResourceGroup --out table
View the public IP address assigned to the IP configuration with the az vm list-ip-addresses command. The following example shows the IP addresses assigned to an existing VM named myVM in a resource group named myResourceGroup.
az vm list-ip-addresses --name myVM --resource-group myResourceGroup --out table
Note
An IP address is assigned from the pool of public IP addresses reserved for an Azure region. For a list of the address pools used in each region, see Azure IP ranges and service tags. If you need the address to be assigned from a specific prefix, use a Public IP address prefix.
Open the necessary ports in your security groups by adjusting the security rules in the network security groups. For information, see Allow network traffic to the VM.
If using PowerShell locally, sign in to Azure with Connect-AzAccount -Environment AzureChinaCloud
.
Use the Get-AzVirtualNetwork command to retrieve the virtual networks in a resource group. Then, use the Get-AzVirtualNetworkSubnetConfig command to retrieve the subnet configurations in a virtual network.
Use the Get-AzNetworkInterface command to retrieve the network interfaces in a resource group. Then, use the Get-AzPublicIpAddress command to retrieve the existing public IP addresses in a resource group.
Use the Set-AzNetworkInterfaceIpConfig command to associate the public IP address to the IP configuration. Then, use the Set-AzNetworkInterface command to write the new IP configuration to the network interface.
The following example associates an existing public IP address named myPublicIP to an IP configuration named ipconfig1. This IP configuration belongs to an existing network interface named myVMNic that exists in a subnet named mySubnet in a virtual network named myVNet. All resources are in a resource group named myResourceGroup.
$vnet = Get-AzVirtualNetwork -Name myVNet -ResourceGroupName myResourceGroup
$subnet = Get-AzVirtualNetworkSubnetConfig -Name mySubnet -VirtualNetwork $vnet
$nic = Get-AzNetworkInterface -Name myVMNic -ResourceGroupName myResourceGroup
$pip = Get-AzPublicIpAddress -Name myPublicIP -ResourceGroupName myResourceGroup
$nic | Set-AzNetworkInterfaceIpConfig -Name ipconfig1 -PublicIPAddress $pip -Subnet $subnet
$nic | Set-AzNetworkInterface
If you don't have an existing public IP address, use the New-AzPublicIpAddress command to create one. For example, the following command creates a dynamic public IP address named myPublicIP in a resource group named myResourceGroup in the chinaeast region.
New-AzPublicIpAddress -Name myPublicIP -ResourceGroupName myResourceGroup -AllocationMethod Dynamic -Location chinaeast
Note
This command creates a public IP address with default values for several settings that you may want to customize. For more information about public IP address settings, see Create a public IP address. An IP address is assigned from the pool of public IP addresses reserved for an Azure region. For a list of the address pools used in each region, see Azure IP ranges and service tags. If you need the address to be assigned from a specific prefix, use a Public IP address prefix.
If you want to know the names and properties of the network interfaces attached to your VM, use the Get-AzVM command to view them. For example, the following command lists the network interfaces attached to a VM named myVM in a resource group named myResourceGroup:
$vm = Get-AzVM -name myVM -ResourceGroupName myResourceGroup
$vm.NetworkProfile
The output includes one or more lines that are similar to the example that follows. In the example output, myVMNic is the name of the network interface.
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myVMNic",
To retrieve the names of the virtual networks or subnets that a network interface is in, use the Get-AzNetworkInterface command. For example, the following command gets the virtual network and subnet information for a network interface named myVMNic in a resource group named myResourceGroup:
$nic = Get-AzNetworkInterface -Name myVMNic -ResourceGroupName myResourceGroup
$ipConfigs = $nic.IpConfigurations
$ipConfigs.Subnet | Select Id
The output includes one or more lines that are similar to the example that follows. In the example output, myVNet is the name of the virtual network and mySubnet is the name of the subnet.
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVNet/subnets/mySubnet",
To retrieve the names of the IP configurations for a network interface, use the Get-AzNetworkInterface command. For example, the following command lists the names of the IP configurations for a network interface named myVMNic in a resource group named myResourceGroup:
$nic = Get-AzNetworkInterface -Name myVMNic -ResourceGroupName myResourceGroup
$nic.IPConfigurations
The output includes one or more lines that are similar to the example that follows. In the example output, ipconfig1 is the name of an IP configuration.
Id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myVMNic/ipConfigurations/ipconfig1
View the public IP address assigned to the IP configuration with the Get-AzPublicIpAddress command. The following example retrieves the addresses assigned to a public IP address named myPublicIP in a resource group named myResourceGroup.
Get-AzPublicIpAddress -Name myPublicIP -ResourceGroupName myResourceGroup | Select IpAddress
For a list of the public IP addresses assigned to an IP configuration, run the following commands:
$nic = Get-AzNetworkInterface -Name myVMNic -ResourceGroupName myResourceGroup
$nic.IPConfigurations
$address = $nic.IPConfigurations.PublicIpAddress
$address | Select Id
The output includes one or more lines that are similar to the example that follows. In the example output, myPublicIP is the name of the public IP address assigned to the IP configuration.
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Network/publicIPAddresses/myPublicIP"
Note
An IP address is assigned from the pool of public IP addresses reserved for an Azure region. For a list of the address pools used in each region, see Azure IP ranges and service tags. If you need the address to be assigned from a specific prefix, use a Public IP address prefix.
Open the necessary ports in your security groups by adjusting the security rules in the network security groups. For information, see Allow network traffic to the VM.
Before you can connect to a public IP address from the internet, you must open the necessary ports in your security groups. These ports must be open in any network security group that you might have associated to the network interface, the subnet of the network interface, or both. Although security groups filter traffic to the private IP address of the network interface, after inbound internet traffic arrives at the public IP address, Azure translates the public address to the private IP address. Therefore, if a network security group prevents the traffic flow, the communication with the public IP address fails.
In this article, you learned how to associate a public IP address to a VM using the Azure portal, Azure CLI, or Azure PowerShell.