Assign a Public IP Address to an Azure Virtual Machine
Introduction
A public IP address enables you to communicate with your Azure virtual machine (VM) over the internet. This document guides you through the process of assigning a public IP address to an existing or new VM using the Azure portal and Azure CLI.
Note: Public IP addresses incur costs. Review Azure pricing for details.
Prerequisites
- An Azure account with an active subscription.
- An existing Azure Virtual Machine (optional, if assigning to a new VM).
Method 1: Using the Azure Portal
Assign to an Existing VM
- Navigate to the Azure portal: portal.azure.com
- In the portal, search for and select Virtual machines.
- Click on the VM you want to assign a public IP address to.
- In the VM's Overview page, under Networking, click IP configurations.
- Click on the existing IP configuration (usually named
basic
or similar). - Under Public IP address, select Create new.
- Provide a Name for the public IP address resource.
- Choose the SKU (Standard is recommended for production workloads).
- Select the Assignment method (Static is recommended if you need a consistent IP).
- Click OK to save the changes.
Visual representation of the IP configuration screen.
Assign to a New VM
- When creating a new VM in the Azure portal, navigate to the Networking tab during the creation process.
- Under Public IP, select Create new.
- Configure the public IP address as described in the previous section (Name, SKU, Assignment).
- Complete the VM creation process.
Method 2: Using Azure CLI
First, ensure you have the Azure CLI installed and are logged in:
az login
Create a Public IP Address Resource
Create a new public IP address resource. Replace MyResourceGroup
and myPublicIP
with your desired names.
az network public-ip create \
--resource-group MyResourceGroup \
--name myPublicIP \
--sku Standard \
--allocation-method Static \
--location eastus
Note: For dynamic allocation, use
--allocation-method Dynamic
. The --location
must match your VM's location.
Associate the Public IP with a VM Network Interface (NIC)
First, get the name of your VM's NIC. Replace MyResourceGroup
and myVM
.
az vm show \
--resource-group MyResourceGroup \
--name myVM \
--query networkProfile.networkInterfaces[0].id \
--output tsv
Once you have the NIC ID, associate the public IP. Replace MyResourceGroup
, myNic
, and myPublicIP
.
az network nic ip-config update \
--resource-group MyResourceGroup \
--nic-name myNic \
--name ipconfig1 \
--public-ip-address myPublicIP
Important: The
--nic-name
and --name ipconfig1
might vary. You can list NICs and their IP configurations using az network nic list
and az network nic show
.
Verifying the Public IP Address
After assignment, you can verify the public IP address:
- Azure Portal: Go to your VM's Overview page. The public IP address will be listed.
- Azure CLI:
az vm show \ --resource-group MyResourceGroup \ --name myVM \ --show-details \ --query publicIps \ --output tsv