|
| 1 | +--- |
| 2 | +title: Create an Azure virtual machine with a dual-stack network - PowerShell |
| 3 | +titleSuffix: Azure Virtual Network |
| 4 | +description: In this article, learn how to use PowerShell to create a virtual machine with a dual-stack virtual network in Azure. |
| 5 | +author: asudbring |
| 6 | +ms.author: allensu |
| 7 | +ms.service: virtual-network |
| 8 | +ms.subservice: ip-services |
| 9 | +ms.topic: how-to |
| 10 | +ms.date: 08/15/2022 |
| 11 | +ms.custom: template-how-to |
| 12 | +--- |
| 13 | + |
| 14 | +# Create an Azure Virtual Machine with a dual-stack network using PowerShell |
| 15 | + |
| 16 | +In this article, you'll create a virtual machine in Azure with PowerShell. The virtual machine is created along with the dual-stack network as part of the procedures. When completed, the virtual machine supports IPv4 and IPv6 communication. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 21 | +- Azure PowerShell installed locally or Azure Cloud Shell. |
| 22 | +- Sign in to Azure PowerShell and ensure you've selected the subscription with which you want to use this feature. For more information, see [Sign in with Azure PowerShell](/powershell/azure/authenticate-azureps). |
| 23 | +- 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 necessary. |
| 24 | + |
| 25 | +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](/powershell/azure/install-Az-ps). If you're running PowerShell locally, you also need to run `Connect-AzAccount` to create a connection with Azure. |
| 26 | + |
| 27 | +## Create a resource group |
| 28 | + |
| 29 | +An Azure resource group is a logical container into which Azure resources are deployed and managed. |
| 30 | + |
| 31 | +Create a resource group with [New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup) named **myResourceGroup** in the **eastus2** location. |
| 32 | + |
| 33 | +```azurepowershell-interactive |
| 34 | +$rg =@{ |
| 35 | + Name = 'myResourceGroup' |
| 36 | + Location = 'eastus2' |
| 37 | +} |
| 38 | +New-AzResourceGroup @rg |
| 39 | +``` |
| 40 | + |
| 41 | +## Create a virtual network |
| 42 | + |
| 43 | +In this section, you'll create a dual-stack virtual network for the virtual machine. |
| 44 | + |
| 45 | +Use [New-AzVirtualNetwork](/powershell/module/az.network/new-azvirtualnetwork) and [New-AzVirtualNetworkSubnetConfig](/powershell/module/az.network/new-azvirtualnetworksubnetconfig) to create a virtual network. |
| 46 | + |
| 47 | +```azurepowershell-interactive |
| 48 | +## Create backend subnet config ## |
| 49 | +$subnet = @{ |
| 50 | + Name = 'myBackendSubnet' |
| 51 | + AddressPrefix = '10.0.0.0/24','2404:f800:8000:122::/64' |
| 52 | +} |
| 53 | +$subnetConfig = New-AzVirtualNetworkSubnetConfig @subnet |
| 54 | +
|
| 55 | +## Create the virtual network ## |
| 56 | +$net = @{ |
| 57 | + Name = 'myVNet' |
| 58 | + ResourceGroupName = 'myResourceGroup' |
| 59 | + Location = 'eastus2' |
| 60 | + AddressPrefix = '10.0.0.0/16','2404:f800:8000:122::/63' |
| 61 | + Subnet = $subnetConfig |
| 62 | +} |
| 63 | +New-AzVirtualNetwork @net |
| 64 | +
|
| 65 | +``` |
| 66 | + |
| 67 | +## Create public IP addresses |
| 68 | + |
| 69 | +You'll create two public IP addresses in this section, IPv4 and IPv6. |
| 70 | + |
| 71 | +Use [New-AzPublicIpAddress](/powershell/module/az.network/new-azpublicipaddress) to create the public IP addresses. |
| 72 | + |
| 73 | +```azurepowershell-interactive |
| 74 | +$ip4 = @{ |
| 75 | + Name = 'myPublicIP-IPv4' |
| 76 | + ResourceGroupName = 'myResourceGroup' |
| 77 | + Location = 'eastus2' |
| 78 | + Sku = 'Standard' |
| 79 | + AllocationMethod = 'Static' |
| 80 | + IpAddressVersion = 'IPv4' |
| 81 | + Zone = 1,2,3 |
| 82 | +} |
| 83 | +New-AzPublicIpAddress @ip4 |
| 84 | +
|
| 85 | +$ip6 = @{ |
| 86 | + Name = 'myPublicIP-IPv6' |
| 87 | + ResourceGroupName = 'myResourceGroup' |
| 88 | + Location = 'eastus2' |
| 89 | + Sku = 'Standard' |
| 90 | + AllocationMethod = 'Static' |
| 91 | + IpAddressVersion = 'IPv6' |
| 92 | + Zone = 1,2,3 |
| 93 | +} |
| 94 | +New-AzPublicIpAddress @ip6 |
| 95 | +``` |
| 96 | +## Create a network security group |
| 97 | + |
| 98 | +In this section, you'll create a network security group for the virtual machine and virtual network. |
| 99 | + |
| 100 | +Use [New-AzNetworkSecurityGroup](/powershell/module/az.network/new-aznetworksecuritygroup) and [New-AzNetworkSecurityRuleConfig](/powershell/module/az.network/new-aznetworksecurityruleconfig) to create the network security group and rules. |
| 101 | + |
| 102 | +```azurepowershell-interactive |
| 103 | +## Create rule for network security group and place in variable. ## |
| 104 | +$nsgrule1 = @{ |
| 105 | + Name = 'myNSGRuleSSH' |
| 106 | + Description = 'Allow SSH' |
| 107 | + Protocol = '*' |
| 108 | + SourcePortRange = '*' |
| 109 | + DestinationPortRange = '22' |
| 110 | + SourceAddressPrefix = 'Internet' |
| 111 | + DestinationAddressPrefix = '*' |
| 112 | + Access = 'Allow' |
| 113 | + Priority = '200' |
| 114 | + Direction = 'Inbound' |
| 115 | +} |
| 116 | +$rule1 = New-AzNetworkSecurityRuleConfig @nsgrule1 |
| 117 | +
|
| 118 | +$nsgrule2 = @{ |
| 119 | + Name = 'myNSGRuleAllOUT' |
| 120 | + Description = 'Allow All out' |
| 121 | + Protocol = '*' |
| 122 | + SourcePortRange = '*' |
| 123 | + DestinationPortRange = '*' |
| 124 | + SourceAddressPrefix = 'Internet' |
| 125 | + DestinationAddressPrefix = '*' |
| 126 | + Access = 'Allow' |
| 127 | + Priority = '201' |
| 128 | + Direction = 'Outbound' |
| 129 | +} |
| 130 | +$rule2 = New-AzNetworkSecurityRuleConfig @nsgrule2 |
| 131 | +
|
| 132 | +## Create network security group ## |
| 133 | +$nsg = @{ |
| 134 | + Name = 'myNSG' |
| 135 | + ResourceGroupName = 'myResourceGroup' |
| 136 | + Location = 'eastus2' |
| 137 | + SecurityRules = $rule1,$rule2 |
| 138 | +} |
| 139 | +New-AzNetworkSecurityGroup @nsg |
| 140 | +``` |
| 141 | + |
| 142 | +## Create virtual machine |
| 143 | + |
| 144 | +In this section, you'll create the virtual machine and its supporting resources. |
| 145 | + |
| 146 | +### Create network interface |
| 147 | + |
| 148 | +You'll use [New-AzNetworkInterface](/powershell/module/az.network/new-aznetworkinterface) and [New-AzNetworkInterfaceIpConfig](/powershell/module/az.network/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. |
| 149 | + |
| 150 | +```azurepowershell-interactive |
| 151 | +## Place the virtual network into a variable. ## |
| 152 | +$net = @{ |
| 153 | + Name = 'myVNet' |
| 154 | + ResourceGroupName = 'myResourceGroup' |
| 155 | +} |
| 156 | +$vnet = Get-AzVirtualNetwork @net |
| 157 | +
|
| 158 | +## Place the network security group into a variable. ## |
| 159 | +$ns = @{ |
| 160 | + Name = 'myNSG' |
| 161 | + ResourceGroupName = 'myResourceGroup' |
| 162 | +} |
| 163 | +$nsg = Get-AzNetworkSecurityGroup @ns |
| 164 | +
|
| 165 | +## Place the IPv4 public IP address into a variable. ## |
| 166 | +$pub4 = @{ |
| 167 | + Name = 'myPublicIP-IPv4' |
| 168 | + ResourceGroupName = 'myResourceGroup' |
| 169 | +} |
| 170 | +$pubIPv4 = Get-AzPublicIPAddress @pub4 |
| 171 | +
|
| 172 | +## Place the IPv6 public IP address into a variable. ## |
| 173 | +$pub6 = @{ |
| 174 | + Name = 'myPublicIP-IPv6' |
| 175 | + ResourceGroupName = 'myResourceGroup' |
| 176 | +} |
| 177 | +$pubIPv6 = Get-AzPublicIPAddress @pub6 |
| 178 | +
|
| 179 | +## Create IPv4 configuration for NIC. ## |
| 180 | +$IP4c = @{ |
| 181 | + Name = 'ipconfig-ipv4' |
| 182 | + Subnet = $vnet.Subnets[0] |
| 183 | + PrivateIpAddressVersion = 'IPv4' |
| 184 | + PublicIPAddress = $pubIPv4 |
| 185 | +} |
| 186 | +$IPv4Config = New-AzNetworkInterfaceIpConfig @IP4c |
| 187 | +
|
| 188 | +## Create IPv6 configuration for NIC. ## |
| 189 | +$IP6c = @{ |
| 190 | + Name = 'ipconfig-ipv6' |
| 191 | + Subnet = $vnet.Subnets[0] |
| 192 | + PrivateIpAddressVersion = 'IPv6' |
| 193 | + PublicIPAddress = $pubIPv6 |
| 194 | +} |
| 195 | +$IPv6Config = New-AzNetworkInterfaceIpConfig @IP6c |
| 196 | +
|
| 197 | +## Command to create network interface for VM ## |
| 198 | +$nic = @{ |
| 199 | + Name = 'myNIC1' |
| 200 | + ResourceGroupName = 'myResourceGroup' |
| 201 | + Location = 'eastus2' |
| 202 | + NetworkSecurityGroup = $nsg |
| 203 | + IpConfiguration = $IPv4Config,$IPv6Config |
| 204 | +} |
| 205 | +New-AzNetworkInterface @nic |
| 206 | +``` |
| 207 | + |
| 208 | +### Create virtual machine |
| 209 | + |
| 210 | +Use the following commands to create the virtual machine: |
| 211 | + |
| 212 | +* [New-AzVM](/powershell/module/az.compute/new-azvm) |
| 213 | + |
| 214 | +* [New-AzVMConfig](/powershell/module/az.compute/new-azvmconfig) |
| 215 | + |
| 216 | +* [Set-AzVMOperatingSystem](/powershell/module/az.compute/set-azvmoperatingsystem) |
| 217 | + |
| 218 | +* [Set-AzVMSourceImage](/powershell/module/az.compute/set-azvmsourceimage) |
| 219 | + |
| 220 | +* [Add-AzVMNetworkInterface](/powershell/module/az.compute/add-azvmnetworkinterface) |
| 221 | + |
| 222 | +```azurepowershell-interactive |
| 223 | +$cred = Get-Credential |
| 224 | +
|
| 225 | +## Place network interface into a variable. ## |
| 226 | +$nic = @{ |
| 227 | + Name = 'myNIC1' |
| 228 | + ResourceGroupName = 'myResourceGroup' |
| 229 | +} |
| 230 | +$nicVM = Get-AzNetworkInterface @nic |
| 231 | +
|
| 232 | +## Create a virtual machine configuration for VMs ## |
| 233 | +$vmsz = @{ |
| 234 | + VMName = 'myVM' |
| 235 | + VMSize = 'Standard_DS1_v2' |
| 236 | +} |
| 237 | +$vmos = @{ |
| 238 | + ComputerName = 'myVM' |
| 239 | + Credential = $cred |
| 240 | +} |
| 241 | +$vmimage = @{ |
| 242 | + PublisherName = 'Debian' |
| 243 | + Offer = 'debian-11' |
| 244 | + Skus = '11' |
| 245 | + Version = 'latest' |
| 246 | +} |
| 247 | +$vmConfig = New-AzVMConfig @vmsz ` |
| 248 | + | Set-AzVMOperatingSystem @vmos -Linux ` |
| 249 | + | Set-AzVMSourceImage @vmimage ` |
| 250 | + | Add-AzVMNetworkInterface -Id $nicVM.Id |
| 251 | +
|
| 252 | +## Create the virtual machine for VMs ## |
| 253 | +$vm = @{ |
| 254 | + ResourceGroupName = 'myResourceGroup' |
| 255 | + Location = 'eastus2' |
| 256 | + VM = $vmConfig |
| 257 | + SshKeyName = 'mySSHKey' |
| 258 | + } |
| 259 | +New-AzVM @vm -GenerateSshKey |
| 260 | +``` |
| 261 | + |
| 262 | +## Test SSH connection |
| 263 | + |
| 264 | +Use [Get-AzPublicIpAddress](/powershell/module/az.network/get-azpublicipaddress) to display the IP addresses of the virtual machine. |
| 265 | + |
| 266 | +```azurepowershell-interactive |
| 267 | +$ip4 = @{ |
| 268 | + ResourceGroupName = 'myResourceGroup' |
| 269 | + Name = 'myPublicIP-IPv4' |
| 270 | +} |
| 271 | +Get-AzPublicIPAddress @ip4 | select IpAddress |
| 272 | +``` |
| 273 | + |
| 274 | +```azurepowershell-interactive |
| 275 | +PS /home/user> Get-AzPublicIPAddress @ip4 | select IpAddress |
| 276 | +
|
| 277 | +IpAddress |
| 278 | +--------- |
| 279 | +20.72.115.187 |
| 280 | +``` |
| 281 | + |
| 282 | +```azurepowershell-interactive |
| 283 | +$ip6 = @{ |
| 284 | + ResourceGroupName = 'myResourceGroup' |
| 285 | + Name = 'myPublicIP-IPv6' |
| 286 | +} |
| 287 | +Get-AzPublicIPAddress @ip6 | select IpAddress |
| 288 | +``` |
| 289 | + |
| 290 | +```azurepowershell-interactive |
| 291 | +PS /home/user> Get-AzPublicIPAddress @ip6 | select IpAddress |
| 292 | +
|
| 293 | +IpAddress |
| 294 | +--------- |
| 295 | +2603:1030:403:3::1ca |
| 296 | +``` |
| 297 | + |
| 298 | +Open an SSH connection to the virtual machine by using the following command. Replace the IP address with the IP address of your virtual machine. |
| 299 | + |
| 300 | +```azurepowershell-interactive |
| 301 | + |
| 302 | +``` |
| 303 | + |
| 304 | +## Clean up resources |
| 305 | + |
| 306 | +When no longer needed, use the [Remove-AzResourceGroup](/powershell/module/az.resources/remove-azresourcegroup) command to remove the resource group, virtual machine, and all related resources. |
| 307 | + |
| 308 | +```azurepowershell-interactive |
| 309 | +Remove-AzResourceGroup -Name 'myResourceGroup' |
| 310 | +``` |
| 311 | + |
| 312 | +## Next steps |
| 313 | + |
| 314 | +In this article, you learned how to create an Azure Virtual machine with a dual-stack network. |
| 315 | + |
| 316 | +For more information about IPv6 and IP addresses in Azure, see: |
| 317 | + |
| 318 | +- [Overview of IPv6 for Azure Virtual Network.](ipv6-overview.md) |
| 319 | + |
| 320 | +- [What is Azure Virtual Network IP Services?](ip-services-overview.md) |
| 321 | + |
| 322 | + |
0 commit comments