In this article, you create a virtual machine in Azure with the Azure portal. The virtual machine is created along with the dual-stack network as part of the procedures. You choose from the Azure portal, Azure CLI, or Azure PowerShell to complete the steps in this article. When completed, the virtual machine supports IPv4 and IPv6 communication.
Prerequisites
You can use the local Azure CLI.
- This tutorial requires version 2.0.28 or later of the Azure CLI.
- An Azure account with an active subscription. Create an account.
- Azure PowerShell installed locally.
- Sign in to Azure PowerShell and select the subscription you want to use. For more information, see Sign in with Azure PowerShell.
- Ensure your Az. Network module is 4.3.0 or later. To verify the installed module, use the command Get-InstalledModule -Name "Az.Network". If the module requires an update, use the command Update-Module -Name "Az. Network".
If you choose to install and use PowerShell locally, this article requires the Azure PowerShell module version 5.4.1 or later. Run Get-Module -ListAvailable Az to find the installed version. If you need to upgrade, see Install Azure PowerShell module. If you're running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.
Create a resource group and virtual network
In this section, you create a resource group and dual-stack virtual network for the virtual machine in the Azure portal.
Create a resource group
Sign-in to the Azure portal.
In the portal, search for and select Resource groups.
Select + Create.
On the Basics tab, enter or select the following values:
| Setting |
Value |
| Subscription |
Select your subscription. |
| Resource group |
Enter myResourceGroup. |
| Region |
Select (Asia) China North 3. |
Select Review + create, and then select Create.
Create a virtual network
In the search box at the top of the portal, enter Virtual network. Select Virtual networks in the search results.
Select + Create.
In the Basics tab of Create virtual network, enter or select the following information.
| Setting |
Value |
| Project details |
|
| Subscription |
Select your subscription. |
| Resource group |
Select myResourceGroup. |
| Instance details |
|
| Name |
Enter myVNet. |
| Region |
Select China North 3. |
Select the IP Addresses tab, or Next>Next.
Leave the default IPv4 address space of 10.0.0.0/16. If the default is absent or different, enter an IPv4 address space of 10.0.0.0/16.
Select the default subnet.
On the Edit subnet page, enter myBackendSubnet in Subnet name and select Save.
Select Add IPv6 address space from the dropdown menu.
In IPv6 address space, edit the default address space and change its value to 2404:f800:8000:122::/63.
To add an IPv6 subnet, select + Add a subnet and enter or select the following information:
| Setting |
Value |
| Subnet |
|
| Subnet name |
Enter myBackendSubnet. |
| Address range |
Leave default of 2404:f800:8000:122::. |
| Size |
Leave the default of /64. |
Select Add.
Select the Review + create.
Select Create.
In this section, you create a resource group dual-stack virtual network for the virtual machine with Azure CLI.
Create a resource group with az group create named myResourceGroup in the eastus2 location.
az group create \
--name myResourceGroup \
--location eastus2
Use az network vnet create to create a virtual network.
az network vnet create \
--resource-group myResourceGroup \
--location eastus2 \
--name myVNet \
--address-prefixes 10.0.0.0/16 2404:f800:8000:122::/63 \
--subnet-name myBackendSubnet \
--subnet-prefixes 10.0.0.0/24 2404:f800:8000:122::/64
In this section, you create a dual-stack virtual network for the virtual machine with Azure PowerShell.
Create a resource group with New-AzResourceGroup named myResourceGroup in the eastus2 location.
$rg =@{
Name = 'myResourceGroup'
Location = 'eastus2'
}
New-AzResourceGroup @rg
Use New-AzVirtualNetwork and New-AzVirtualNetworkSubnetConfig to create a virtual network.
## Create backend subnet config ##
$subnet = @{
Name = 'myBackendSubnet'
AddressPrefix = '10.0.0.0/24','2404:f800:8000:122::/64'
}
$subnetConfig = New-AzVirtualNetworkSubnetConfig @subnet
## Create the virtual network ##
$net = @{
Name = 'myVNet'
ResourceGroupName = 'myResourceGroup'
Location = 'eastus2'
AddressPrefix = '10.0.0.0/16','2404:f800:8000:122::/63'
Subnet = $subnetConfig
}
New-AzVirtualNetwork @net
Create public IP addresses
You create two public IP addresses in this section, IPv4 and IPv6 in the Azure portal.
Create IPv4 public IP address
In the search box at the top of the portal, enter Public IP address. Select Public IP addresses in the search results.
Select + Create.
Enter or select the following information in Create public IP address.
| Setting |
Value |
| Project details |
|
| Subscription |
Select your subscription. |
| Resource group |
Select myResourceGroup. |
| Location |
Select China North 3. |
| Availability zone |
Select Zone redundant. |
| Instance details |
|
| Name |
Enter myPublicIP-IPv4. |
| IP version |
Select IPv4. |
| SKU |
Leave the default of Standard. |
| Tier |
Leave the default of Regional. |
| IP address assignment |
|
| Routing preference |
Leave the default of Microsoft network. |
| Idle timeout (minutes) |
Leave the default of 4. |
| DNS name label |
Enter myPublicIP-IPv4. |
Select Review + create then Create.
Create IPv6 public IP address
In the search box at the top of the portal, enter Public IP address. Select Public IP addresses in the search results.
Select + Create.
Enter or select the following information in Create public IP address.
| Setting |
Value |
| Project details |
|
| Subscription |
Select your subscription. |
| Resource group |
Select myResourceGroup. |
| Location |
Select China North 3. |
| Availability zone |
Select Zone redundant. |
| Instance details |
|
| Name |
Enter myPublicIP-IPv6. |
| IP version |
Select IPv6. |
| SKU |
Leave the default of Standard. |
| Tier |
Leave the default of Regional. |
| IP address assignment |
|
| DNS name label |
Enter myPublicIP-IPv6. |
Select Review + create then Create.
You create two public IP addresses in this section, IPv4 and IPv6 with Azure CLI.
Use az network public-ip create to create the public IP addresses.
az network public-ip create \
--resource-group myResourceGroup \
--name myPublicIP-Ipv4 \
--sku Standard \
--version IPv4 \
--zone 1 2 3
az network public-ip create \
--resource-group myResourceGroup \
--name myPublicIP-Ipv6 \
--sku Standard \
--version IPv6 \
--zone 1 2 3
You create two public IP addresses in this section, IPv4 and IPv6.
Use New-AzPublicIpAddress to create the public IP addresses.
$ip4 = @{
Name = 'myPublicIP-IPv4'
ResourceGroupName = 'myResourceGroup'
Location = 'chinanorth3'
Sku = 'Standard'
AllocationMethod = 'Static'
IpAddressVersion = 'IPv4'
Zone = 1,2,3
}
New-AzPublicIpAddress @ip4
$ip6 = @{
Name = 'myPublicIP-IPv6'
ResourceGroupName = 'myResourceGroup'
Location = 'chinanorth3'
Sku = 'Standard'
AllocationMethod = 'Static'
IpAddressVersion = 'IPv6'
Zone = 1,2,3
}
New-AzPublicIpAddress @ip6
Create virtual machine
In this section, you create the virtual machine and its supporting resources.
Create virtual machine
In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.
Select + Create then Azure virtual machine.
In the Basics tab, enter or select the following information.
| Setting |
Value |
| Project details |
|
| Subscription |
Select your subscription. |
| Resource group |
Select myResourceGroup. |
| Instance details |
|
| Virtual machine name |
Enter myVM. |
| Region |
Select China North 3. |
| Availability options |
Select No infrastructure redundancy required. |
| Security type |
Select Standard. |
| Image |
Select Ubuntu Server 20.04 LTS - Gen2. |
| Size |
Select the default size. |
| Administrator account |
|
| Authentication type |
Select SSH public key. |
| Username |
Enter a username. |
| SSH public key source |
Select Generate new key pair. |
| Key pair name |
Enter mySSHKey. |
| Inbound port rules |
|
| Public inbound ports |
Select None. |
Select the Networking tab, or Next: Disks then Next: Networking.
Enter or select the following information in the Networking tab.
| Setting |
Value |
| Network interface |
|
| Virtual network |
Select myVNet. |
| Subnet |
Select myBackendSubnet (10.1.0.0/24,2404:f800:8000:122:/64). |
| Public IP |
Select myPublicIP-IPv4. |
| NIC network security group |
Select Advanced. |
| Configure network security group |
Select Create new. Enter myNSG in Name. Select OK. |
Select Review + create.
Select Create.
Generate new key pair appears. Select Download private key and create resource.
The private key downloads to your local computer. Copy the private key to a directory on your computer. In the following example, it's ~/.ssh.
A network interface is automatically created and attached to the chosen virtual network during creation. In this section, you add the IPv6 configuration to the existing network interface.
In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.
Select myVM or your existing virtual machine name.
Select Networking in Settings.
The name of your default network interface will be myvmxx, with xx a random number. In this example, it's myvm281. Select myvm281 next to Network Interface:.
In the properties of the network interface, select IP configurations in Settings.
In IP configurations, select + Add.
In Add IP configuration, enter or select the following information.
| Setting |
Value |
| Name |
Enter Ipv6config. |
| IP version |
Select IPv6. |
| Private IP address settings |
|
| Allocation |
Leave the default of Dynamic. |
| Public IP address |
Select Associate. |
| Public IP address |
Select myPublicIP-IPv6. |
Select OK.
In this section, you create the virtual machine and its supporting resources.
Create a network security group
Create a network security group with az network nsg create. The default rules in the network security group deny all inbound access from the internet.
az network nsg create \
--resource-group myResourceGroup \
--name myNSG
Create network interface
You use az network nic create to create the network interface for the virtual machine. The public IP addresses and the NSG created previously are associated with the NIC. The network interface is attached to the virtual network you created previously.
az network nic create \
--resource-group myResourceGroup \
--name myNIC1 \
--vnet-name myVNet \
--subnet myBackEndSubnet \
--network-security-group myNSG \
--public-ip-address myPublicIP-IPv4
Create IPv6 IP configuration
Use az network nic ip-config create to create the IPv6 configuration for the NIC.
az network nic ip-config create \
--resource-group myResourceGroup \
--name myIPv6config \
--nic-name myNIC1 \
--private-ip-address-version IPv6 \
--vnet-name myVNet \
--subnet myBackendSubnet \
--public-ip-address myPublicIP-IPv6
Create virtual machine
Use az vm create to create the virtual machine.
az vm create \
--resource-group myResourceGroup \
--name myVM \
--nics myNIC1 \
--image Ubuntu2204 \
--admin-username azureuser \
--authentication-type ssh \
--generate-ssh-keys
In this section, you create the virtual machine and its supporting resources.
Create a network security group
Create a network security group with New-AzNetworkSecurityGroup. The default rules in the network security group deny all inbound access from the internet.
$nsg = @{
Name = 'myNSG'
ResourceGroupName = 'myResourceGroup'
Location = 'eastus2'
}
New-AzNetworkSecurityGroup @nsg
Create network interface
You use New-AzNetworkInterface and New-AzNetworkInterfaceIpConfig to create the network interface for the virtual machine. The public IP addresses and the NSG created previously are associated with the NIC. The network interface is attached to the virtual network you created previously.
## Place the virtual network into a variable. ##
$net = @{
Name = 'myVNet'
ResourceGroupName = 'myResourceGroup'
}
$vnet = Get-AzVirtualNetwork @net
## Place the network security group into a variable. ##
$ns = @{
Name = 'myNSG'
ResourceGroupName = 'myResourceGroup'
}
$nsg = Get-AzNetworkSecurityGroup @ns
## Place the IPv4 public IP address into a variable. ##
$pub4 = @{
Name = 'myPublicIP-IPv4'
ResourceGroupName = 'myResourceGroup'
}
$pubIPv4 = Get-AzPublicIPAddress @pub4
## Place the IPv6 public IP address into a variable. ##
$pub6 = @{
Name = 'myPublicIP-IPv6'
ResourceGroupName = 'myResourceGroup'
}
$pubIPv6 = Get-AzPublicIPAddress @pub6
## Create IPv4 configuration for NIC. ##
$IP4c = @{
Name = 'ipconfig-ipv4'
Subnet = $vnet.Subnets[0]
PrivateIpAddressVersion = 'IPv4'
PublicIPAddress = $pubIPv4
}
$IPv4Config = New-AzNetworkInterfaceIpConfig @IP4c
## Create IPv6 configuration for NIC. ##
$IP6c = @{
Name = 'ipconfig-ipv6'
Subnet = $vnet.Subnets[0]
PrivateIpAddressVersion = 'IPv6'
PublicIPAddress = $pubIPv6
}
$IPv6Config = New-AzNetworkInterfaceIpConfig @IP6c
## Command to create network interface for VM ##
$nic = @{
Name = 'myNIC1'
ResourceGroupName = 'myResourceGroup'
Location = 'eastus2'
NetworkSecurityGroup = $nsg
IpConfiguration = $IPv4Config,$IPv6Config
}
New-AzNetworkInterface @nic
Create virtual machine
Use the following commands to create the virtual machine:
$cred = Get-Credential
## Place network interface into a variable. ##
$nic = @{
Name = 'myNIC1'
ResourceGroupName = 'myResourceGroup'
}
$nicVM = Get-AzNetworkInterface @nic
## Create a virtual machine configuration for VMs ##
$vmsz = @{
VMName = 'myVM'
VMSize = 'Standard_DS1_v2'
}
$vmos = @{
ComputerName = 'myVM'
Credential = $cred
}
$vmimage = @{
PublisherName = 'Debian'
Offer = 'debian-11'
Skus = '11'
Version = 'latest'
}
$vmConfig = New-AzVMConfig @vmsz `
| Set-AzVMOperatingSystem @vmos -Linux `
| Set-AzVMSourceImage @vmimage `
| Add-AzVMNetworkInterface -Id $nicVM.Id
## Create the virtual machine for VMs ##
$vm = @{
ResourceGroupName = 'myResourceGroup'
Location = 'eastus2'
VM = $vmConfig
SshKeyName = 'mySSHKey'
}
New-AzVM @vm -GenerateSshKey
Install nginx
In this section, you create a network security group rule to allow HTTP traffic and install nginx on the virtual machine using the Run Command feature.
Create an inbound security rule
In the search box at the top of the portal, enter Network security group. Select Network security groups in the search results.
Select myNSG.
Select Inbound security rules in Settings.
Select + Add.
Enter or select the following information:
| Setting |
Value |
| Source |
Leave the default of Any. |
| Source port ranges |
Leave the default of *. |
| Destination |
Leave the default of Any. |
| Service |
Select HTTP. |
| Action |
Leave the default of Allow. |
| Priority |
Enter 100. |
| Name |
Enter myNSGRuleHTTP. |
Select Add.
Install nginx with Run Command
In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.
Select myVM.
In Operations, select Run command.
Select RunShellScript.
Enter the following commands:
sudo apt-get update
sudo apt-get install -y nginx
Select Run.
Wait for the command to complete. The output pane displays Enable succeeded when complete.
Create an inbound security rule
Use az network nsg rule create to create a network security group rule to allow HTTP traffic.
az network nsg rule create \
--resource-group myResourceGroup \
--nsg-name myNSG \
--name myNSGRuleHTTP \
--protocol '*' \
--direction inbound \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range 80 \
--access allow \
--priority 100
Install nginx
Use az vm run-command invoke to install nginx on the virtual machine.
az vm run-command invoke \
--resource-group myResourceGroup \
--name myVM \
--command-id RunShellScript \
--scripts "sudo apt-get update && sudo apt-get install -y nginx"
Create an inbound security rule
Use Get-AzNetworkSecurityGroup and Add-AzNetworkSecurityRuleConfig to create a network security group rule to allow HTTP traffic.
## Place the network security group into a variable. ##
$nsg = @{
Name = 'myNSG'
ResourceGroupName = 'myResourceGroup'
}
$nsgObj = Get-AzNetworkSecurityGroup @nsg
## Create the network security group rule. ##
$nsgRule = @{
Name = 'myNSGRuleHTTP'
Protocol = '*'
Direction = 'Inbound'
Priority = 100
SourceAddressPrefix = '*'
SourcePortRange = '*'
DestinationAddressPrefix = '*'
DestinationPortRange = 80
Access = 'Allow'
}
$nsgObj | Add-AzNetworkSecurityRuleConfig @nsgRule | Set-AzNetworkSecurityGroup
Install nginx
Use Invoke-AzVMRunCommand to install nginx on the virtual machine.
$vm = @{
ResourceGroupName = 'myResourceGroup'
Name = 'myVM'
CommandId = 'RunShellScript'
ScriptString = 'sudo apt-get update && sudo apt-get install -y nginx'
}
Invoke-AzVMRunCommand @vm
Test dual-stack connectivity
Verify nginx is running by connecting to the public IPv4 address of the virtual machine.
In the search box at the top of the portal, enter Public IP address. Select Public IP addresses in the search results.
Select myPublicIP-IPv4.
Note the public IPv4 address in the Overview under IP address. In this example, it's 203.0.113.77.
Open your web browser and browse to http://203.0.113.77. Replace the IP address with the public IPv4 address of your virtual machine.
The default nginx welcome page is displayed, confirming that the web server is running and accessible over IPv4.
Use az network public-ip show to display the IP addresses of the virtual machine.
az network public-ip show \
--resource-group myResourceGroup \
--name myPublicIP-IPv4 \
--query ipAddress \
--output tsv
Open your web browser and browse to the IPv4 address displayed. The default nginx welcome page confirms that the web server is running and accessible.
Use the following command to display the IPv6 address:
az network public-ip show \
--resource-group myResourceGroup \
--name myPublicIP-IPv6 \
--query ipAddress \
--output tsv
Use Get-AzPublicIpAddress to display the IP addresses of the virtual machine.
$ip4 = @{
ResourceGroupName = 'myResourceGroup'
Name = 'myPublicIP-IPv4'
}
Get-AzPublicIPAddress @ip4 | select IpAddress
Open your web browser and browse to the IPv4 address displayed. The default nginx welcome page confirms that the web server is running and accessible.
Use the following command to display the IPv6 address:
$ip6 = @{
ResourceGroupName = 'myResourceGroup'
Name = 'myPublicIP-IPv6'
}
Get-AzPublicIPAddress @ip6 | select IpAddress
Clean up resources
When your finished with the resources created in this article, delete the resource group and all of the resources it contains:
In the search box at the top of the portal, enter myResourceGroup. Select myResourceGroup in the search results in Resource groups.
Select Delete resource group.
Enter myResourceGroup for TYPE THE RESOURCE GROUP NAME and select Delete.
When no longer needed, use the az group delete command to remove the resource group, virtual machine, and all related resources.
az group delete \
--name myResourceGroup
When no longer needed, use the Remove-AzResourceGroup command to remove the resource group, virtual machine, and all related resources.
Remove-AzResourceGroup -Name 'myResourceGroup'
Next steps
In this article, you learned how to create an Azure Virtual machine with a dual-stack network.
For more information about IPv6 and IP addresses in Azure, see: