|
| 1 | +--- |
| 2 | +title: Create a Linux VM in Azure with multiple NICs |
| 3 | +description: Learn how to create a Linux VM with multiple NICs attached to it using the Azure CLI or Resource Manager templates. |
| 4 | +author: mattmcinnes |
| 5 | +ms.service: azure-virtual-machines |
| 6 | +ms.subservice: networking |
| 7 | +ms.topic: how-to |
| 8 | +ms.custom: devx-track-azurecli, linux-related-content, innovation-engine |
| 9 | +ms.date: 04/06/2023 |
| 10 | +ms.author: mattmcinnes |
| 11 | +ms.reviewer: cynthn |
| 12 | +--- |
| 13 | + |
| 14 | +# How to create a Linux virtual machine in Azure with multiple network interface cards |
| 15 | + |
| 16 | +**Applies to:** :heavy_check_mark: Linux VMs :heavy_check_mark: Flexible scale sets |
| 17 | + |
| 18 | +This article details how to create a VM with multiple NICs with the Azure CLI. |
| 19 | + |
| 20 | +## Create supporting resources |
| 21 | +Install the latest [Azure CLI](/cli/azure/install-az-cli2) and log in to an Azure account using [az login](/cli/azure/reference-index). |
| 22 | + |
| 23 | +In the following examples, replace example parameter names with your own values. Example parameter names included *myResourceGroup*, *mystorageaccount*, and *myVM*. |
| 24 | + |
| 25 | +First, create a resource group with [az group create](/cli/azure/group). The following example creates a resource group named *myResourceGroup* in the *eastus* location. In these examples, we declare environment variables as they are used and add a random suffix to unique resource names. |
| 26 | + |
| 27 | +```azurecli |
| 28 | +export RANDOM_SUFFIX=$(openssl rand -hex 3) |
| 29 | +export MY_RESOURCE_GROUP_NAME="myResourceGroup$RANDOM_SUFFIX" |
| 30 | +export REGION="WestUS2" |
| 31 | +az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION |
| 32 | +``` |
| 33 | +<!-- expected_similarity=0.3 --> |
| 34 | +```JSON |
| 35 | +{ |
| 36 | + "id": "/subscriptions/xxxxx/resourceGroups/myResourceGroupxxx", |
| 37 | + "location": "WestUS2", |
| 38 | + "managedBy": null, |
| 39 | + "name": "myResourceGroupxxx", |
| 40 | + "properties": { |
| 41 | + "provisioningState": "Succeeded" |
| 42 | + }, |
| 43 | + "tags": null, |
| 44 | + "type": "Microsoft.Resources/resourceGroups" |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Create the virtual network with [az network vnet create](/cli/azure/network/vnet). The following example creates a virtual network named *myVnet* and subnet named *mySubnetFrontEnd*: |
| 49 | + |
| 50 | +```azurecli |
| 51 | +export VNET_NAME="myVnet" |
| 52 | +export FRONTEND_SUBNET="mySubnetFrontEnd" |
| 53 | +az network vnet create \ |
| 54 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 55 | + --name $VNET_NAME \ |
| 56 | + --address-prefix 10.0.0.0/16 \ |
| 57 | + --subnet-name $FRONTEND_SUBNET \ |
| 58 | + --subnet-prefix 10.0.1.0/24 |
| 59 | +``` |
| 60 | + |
| 61 | +Create a subnet for the back-end traffic with [az network vnet subnet create](/cli/azure/network/vnet/subnet). The following example creates a subnet named *mySubnetBackEnd*: |
| 62 | + |
| 63 | +```azurecli |
| 64 | +export BACKEND_SUBNET="mySubnetBackEnd" |
| 65 | +az network vnet subnet create \ |
| 66 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 67 | + --vnet-name $VNET_NAME \ |
| 68 | + --name $BACKEND_SUBNET \ |
| 69 | + --address-prefix 10.0.2.0/24 |
| 70 | +``` |
| 71 | + |
| 72 | +Create a network security group with [az network nsg create](/cli/azure/network/nsg). The following example creates a network security group named *myNetworkSecurityGroup*: |
| 73 | + |
| 74 | +```azurecli |
| 75 | +export NSG_NAME="myNetworkSecurityGroup" |
| 76 | +az network nsg create \ |
| 77 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 78 | + --name $NSG_NAME |
| 79 | +``` |
| 80 | + |
| 81 | +## Create and configure multiple NICs |
| 82 | +Create two NICs with [az network nic create](/cli/azure/network/nic). The following example creates two NICs, named *myNic1* and *myNic2*, connected to the network security group, with one NIC connecting to each subnet: |
| 83 | + |
| 84 | +```azurecli |
| 85 | +export NIC1="myNic1" |
| 86 | +export NIC2="myNic2" |
| 87 | +az network nic create \ |
| 88 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 89 | + --name $NIC1 \ |
| 90 | + --vnet-name $VNET_NAME \ |
| 91 | + --subnet $FRONTEND_SUBNET \ |
| 92 | + --network-security-group $NSG_NAME |
| 93 | +az network nic create \ |
| 94 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 95 | + --name $NIC2 \ |
| 96 | + --vnet-name $VNET_NAME \ |
| 97 | + --subnet $BACKEND_SUBNET \ |
| 98 | + --network-security-group $NSG_NAME |
| 99 | +``` |
| 100 | + |
| 101 | +## Create a VM and attach the NICs |
| 102 | +When you create the VM, specify the NICs you created with --nics. You also need to take care when you select the VM size. There are limits for the total number of NICs that you can add to a VM. Read more about [Linux VM sizes](../sizes.md). |
| 103 | + |
| 104 | +Create a VM with [az vm create](/cli/azure/vm). The following example creates a VM named *myVM*: |
| 105 | + |
| 106 | +```azurecli |
| 107 | +export VM_NAME="myVM" |
| 108 | +az vm create \ |
| 109 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 110 | + --name $VM_NAME \ |
| 111 | + --image Ubuntu2204 \ |
| 112 | + --size Standard_DS3_v2 \ |
| 113 | + --admin-username azureuser \ |
| 114 | + --generate-ssh-keys \ |
| 115 | + --nics $NIC1 $NIC2 |
| 116 | +``` |
| 117 | + |
| 118 | +Add routing tables to the guest OS by completing the steps in [Configure the guest OS for multiple NICs](#configure-guest-os-for-multiple-nics). |
| 119 | + |
| 120 | +## Add a NIC to a VM |
| 121 | +The previous steps created a VM with multiple NICs. You can also add NICs to an existing VM with the Azure CLI. Different [VM sizes](../sizes.md) support a varying number of NICs, so size your VM accordingly. If needed, you can [resize a VM](../resize-vm.md). |
| 122 | + |
| 123 | +Create another NIC with [az network nic create](/cli/azure/network/nic). The following example creates a NIC named *myNic3* connected to the back-end subnet and network security group created in the previous steps: |
| 124 | + |
| 125 | +```azurecli |
| 126 | +export NIC3="myNic3" |
| 127 | +az network nic create \ |
| 128 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 129 | + --name $NIC3 \ |
| 130 | + --vnet-name $VNET_NAME \ |
| 131 | + --subnet $BACKEND_SUBNET \ |
| 132 | + --network-security-group $NSG_NAME |
| 133 | +``` |
| 134 | + |
| 135 | +To add a NIC to an existing VM, first deallocate the VM with [az vm deallocate](/cli/azure/vm). The following example deallocates the VM named *myVM*: |
| 136 | + |
| 137 | +```azurecli |
| 138 | +az vm deallocate --resource-group $MY_RESOURCE_GROUP_NAME --name $VM_NAME |
| 139 | +``` |
| 140 | + |
| 141 | +Add the NIC with [az vm nic add](/cli/azure/vm/nic). The following example adds *myNic3* to *myVM*: |
| 142 | + |
| 143 | +```azurecli |
| 144 | +az vm nic add \ |
| 145 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 146 | + --vm-name $VM_NAME \ |
| 147 | + --nics $NIC3 |
| 148 | +``` |
| 149 | + |
| 150 | +Start the VM with [az vm start](/cli/azure/vm): |
| 151 | + |
| 152 | +```azurecli |
| 153 | +az vm start --resource-group $MY_RESOURCE_GROUP_NAME --name $VM_NAME |
| 154 | +``` |
| 155 | + |
| 156 | +Add routing tables to the guest OS by completing the steps in [Configure the guest OS for multiple NICs](#configure-guest-os-for-multiple-nics). |
| 157 | + |
| 158 | +## Remove a NIC from a VM |
| 159 | +To remove a NIC from an existing VM, first deallocate the VM with [az vm deallocate](/cli/azure/vm). The following example deallocates the VM named *myVM*: |
| 160 | + |
| 161 | +```azurecli |
| 162 | +az vm deallocate --resource-group $MY_RESOURCE_GROUP_NAME --name $VM_NAME |
| 163 | +``` |
| 164 | + |
| 165 | +Remove the NIC with [az vm nic remove](/cli/azure/vm/nic). The following example removes *myNic3* from *myVM*: |
| 166 | + |
| 167 | +```azurecli |
| 168 | +az vm nic remove \ |
| 169 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 170 | + --vm-name $VM_NAME \ |
| 171 | + --nics $NIC3 |
| 172 | +``` |
| 173 | + |
| 174 | +Start the VM with [az vm start](/cli/azure/vm): |
| 175 | + |
| 176 | +```azurecli |
| 177 | +az vm start --resource-group $MY_RESOURCE_GROUP_NAME --name $VM_NAME |
| 178 | +``` |
| 179 | + |
| 180 | +## Create multiple NICs using Resource Manager templates |
| 181 | +Azure Resource Manager templates use declarative JSON files to define your environment. You can read an [overview of Azure Resource Manager](/azure/azure-resource-manager/management/overview). Resource Manager templates provide a way to create multiple instances of a resource during deployment, such as creating multiple NICs. You use *copy* to specify the number of instances to create: |
| 182 | + |
| 183 | +```json |
| 184 | +"copy": { |
| 185 | + "name": "multiplenics" |
| 186 | + "count": "[parameters('count')]" |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +Read more about [creating multiple instances using *copy*](/azure/azure-resource-manager/templates/copy-resources). |
| 191 | + |
| 192 | +You can also use a copyIndex() to then append a number to a resource name, which allows you to create myNic1, myNic2, etc. The following shows an example of appending the index value: |
| 193 | + |
| 194 | +```json |
| 195 | +"name": "[concat('myNic', copyIndex())]", |
| 196 | +``` |
| 197 | + |
| 198 | +You can read a complete example of [creating multiple NICs using Resource Manager templates](/azure/virtual-network/template-samples). |
| 199 | + |
| 200 | +Add routing tables to the guest OS by completing the steps in [Configure the guest OS for multiple NICs](#configure-guest-os-for-multiple-nics). |
| 201 | + |
| 202 | +## Configure guest OS for multiple NICs |
| 203 | + |
| 204 | +The previous steps created a virtual network and subnet, attached NICs, then created a VM. A public IP address and network security group rules that allow SSH traffic were not created. To configure the guest OS for multiple NICs, you need to allow remote connections and run commands locally on the VM. |
| 205 | + |
| 206 | +To allow SSH traffic, create a network security group rule with [az network nsg rule create](/cli/azure/network/nsg/rule#az-network-nsg-rule-create) as follows: |
| 207 | + |
| 208 | +```azurecli |
| 209 | +az network nsg rule create \ |
| 210 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 211 | + --nsg-name $NSG_NAME \ |
| 212 | + --name allow_ssh \ |
| 213 | + --priority 101 \ |
| 214 | + --destination-port-ranges 22 |
| 215 | +``` |
| 216 | + |
| 217 | +Create a public IP address with [az network public-ip create](/cli/azure/network/public-ip#az-network-public-ip-create) and assign it to the first NIC with [az network nic ip-config update](/cli/azure/network/nic/ip-config#az-network-nic-ip-config-update): |
| 218 | + |
| 219 | +```azurecli |
| 220 | +export PUBLIC_IP_NAME="myPublicIP" |
| 221 | +az network public-ip create --resource-group $MY_RESOURCE_GROUP_NAME --name $PUBLIC_IP_NAME |
| 222 | +
|
| 223 | +az network nic ip-config update \ |
| 224 | + --resource-group $MY_RESOURCE_GROUP_NAME \ |
| 225 | + --nic-name $NIC1 \ |
| 226 | + --name ipconfig1 \ |
| 227 | + --public-ip $PUBLIC_IP_NAME |
| 228 | +``` |
| 229 | + |
| 230 | +To view the public IP address of the VM, use [az vm show](/cli/azure/vm#az-vm-show) as follows: |
| 231 | + |
| 232 | +```azurecli |
| 233 | +az vm show --resource-group $MY_RESOURCE_GROUP_NAME --name $VM_NAME -d --query publicIps -o tsv |
| 234 | +``` |
| 235 | +<!-- expected_similarity=0.3 --> |
| 236 | +```TEXT |
| 237 | +x.x.x.x |
| 238 | +``` |
| 239 | + |
| 240 | +Now SSH to the public IP address of your VM. The default username provided in a previous step was *azureuser*. Provide your own username and public IP address: |
| 241 | + |
| 242 | +```bash |
| 243 | +export IP_ADDRESS=$(az vm show --resource-group $MY_RESOURCE_GROUP_NAME --name $VM_NAME -d --query publicIps -o tsv) |
| 244 | +ssh -o StrictHostKeyChecking=no azureuser@$IP_ADDRESS |
| 245 | +``` |
| 246 | +To send to or from a secondary network interface, you have to manually add persistent routes to the operating system for each secondary network interface. In this article, *eth1* is the secondary interface. Instructions for adding persistent routes to the operating system vary by distro. See documentation for your distro for instructions. |
| 247 | + |
| 248 | +When adding the route to the operating system, the gateway address is the first address of the subnet the network interface is in. For example, if the subnet has been assigned the range 10.0.2.0/24, the gateway you specify for the route is 10.0.2.1 or if the subnet has been assigned the range 10.0.2.128/25, the gateway you specify for the route is 10.0.2.129. You can define a specific network for the route's destination, or specify a destination of 0.0.0.0, if you want all traffic for the interface to go through the specified gateway. The gateway for each subnet is managed by the virtual network. |
| 249 | + |
| 250 | +Once you've added the route for a secondary interface, verify that the route is in your route table with `route -n`. The following example output is for the route table that has the two network interfaces added to the VM in this article: |
| 251 | + |
| 252 | +```output |
| 253 | +Kernel IP routing table |
| 254 | +Destination Gateway Genmask Flags Metric Ref Use Iface |
| 255 | +0.0.0.0 10.0.1.1 0.0.0.0 UG 0 0 0 eth0 |
| 256 | +0.0.0.0 10.0.2.1 0.0.0.0 UG 0 0 0 eth1 |
| 257 | +10.0.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 |
| 258 | +10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 |
| 259 | +168.63.129.16 10.0.1.1 255.255.255.255 UGH 0 0 0 eth0 |
| 260 | +169.254.169.254 10.0.1.1 255.255.255.255 UGH 0 0 0 eth0 |
| 261 | +``` |
| 262 | + |
| 263 | +Confirm that the route you added persists across reboots by checking your route table again after a reboot. To test connectivity, you can enter the following command, for example, where *eth1* is the name of a secondary network interface: `ping bing.com -c 4 -I eth1` |
| 264 | + |
| 265 | +## Next steps |
| 266 | +Review [Linux VM sizes](../sizes.md) when trying to creating a VM with multiple NICs. Pay attention to the maximum number of NICs each VM size supports. |
| 267 | + |
| 268 | +To further secure your VMs, use just in time VM access. This feature opens network security group rules for SSH traffic when needed, and for a defined period of time. For more information, see [Manage virtual machine access using just in time](/azure/security-center/security-center-just-in-time). |
0 commit comments