Create a virtual machine with a static public IP address using the Azure CLI
You can create a virtual machine with a static public IP address. A public IP address enables you to communicate to a virtual machine from the internet. Assign a static public IP address, rather than a dynamic address, to ensure that the address never changes. Learn more about static public IP addresses. To change a public IP address assigned to an existing virtual machine from dynamic to static, or to work with private IP addresses, see Add, change, or remove IP addresses. Public IP addresses have a nominal charge, and there is a limit to the number of public IP addresses that you can use per subscription.
Create a virtual machine
You can complete the following steps from your local computer or by using the Azure Cloud Shell. To use your local computer, ensure you have the Azure CLI installed. To use the Azure Cloud Shell, select Try It in the top right corner of any command box that follows. The Cloud Shell signs you into Azure.
- If using the Cloud Shell, skip to step 2. Open a command session and sign into Azure with
Create a resource group with the az group create command. The following example creates a resource group in the East US Azure region:
az group create --name myResourceGroup --location eastus
Create a virtual machine with the az vm create command. The
--public-ip-address-allocation=staticoption assigns a static public IP address to the virtual machine. The following example creates an Ubuntu virtual machine with a static, basic SKU public IP address named myPublicIpAddress:
az vm create \ --resource-group myResourceGroup \ --name myVM \ --image UbuntuLTS \ --admin-username azureuser \ --generate-ssh-keys \ --public-ip-address myPublicIpAddress \ --public-ip-address-allocation static
If the public IP address must be a standard SKU, add
--public-ip-sku Standardto the previous command. Learn more about Public IP address SKUs. If the virtual machine will be added to the back-end pool of a public Azure Load Balancer, the SKU of the virtual machine's public IP address must match the SKU of the load balancer's public IP address. For details, see Azure Load Balancer.
View the public IP address assigned and confirm that it was created as a static, basic SKU address, with az network public-ip show:
az network public-ip show \ --resource-group myResourceGroup \ --name myPublicIpAddress \ --query [ipAddress,publicIpAllocationMethod,sku] \ --output table
Azure assigned a public IP address from addresses used in the region you created the virtual machine in. You can download the list of ranges (prefixes) for the Azure Public, US government, China, and Germany clouds.
Do not modify the IP address settings within the virtual machine's operating system. The operating system is unaware of Azure public IP addresses. Though you can add private IP address settings to the operating system, we recommend not doing so unless necessary, and not until after reading Add a private IP address to an operating system.
Clean up resources
When no longer needed, you can use az group delete to remove the resource group and all of the resources it contains:
az group delete --name myResourceGroup --yes