You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: In this article, you learn how to filter network traffic to a subnet, with a network security group, using the Azure CLI.
4
-
services: virtual-network
5
4
author: asudbring
6
-
7
5
ms.service: azure-virtual-network
8
-
ms.devlang: azurecli
9
6
ms.topic: how-to
10
-
ms.tgt_pltfrm: virtual-network
11
-
ms.date: 03/30/2018
7
+
ms.date: 08/09/2024
12
8
ms.author: allensu
13
9
ms.custom: devx-track-azurecli
14
10
# Customer intent: I want to filter network traffic to virtual machines that perform similar functions, such as web servers.
@@ -35,175 +31,201 @@ A network security group contains security rules. Security rules specify a sourc
35
31
36
32
### Create application security groups
37
33
38
-
First create a resource group for all the resources created in this article with [az group create](/cli/azure/group). The following example creates a resource group in the *eastus* location:
34
+
First create a resource group for all the resources created in this article with [az group create](/cli/azure/group). The following example creates a resource group in the *westus2* location:
39
35
40
36
```azurecli-interactive
41
37
az group create \
42
-
--name myResourceGroup \
43
-
--location eastus
38
+
--name test-rg \
39
+
--location westus2
44
40
```
45
41
46
42
Create an application security group with [az network asg create](/cli/azure/network/asg). An application security group enables you to group servers with similar port filtering requirements. The following example creates two application security groups.
47
43
48
44
```azurecli-interactive
49
45
az network asg create \
50
-
--resource-group myResourceGroup \
51
-
--name myAsgWebServers \
52
-
--location eastus
46
+
--resource-group test-rg \
47
+
--name asg-web-servers \
48
+
--location westus2
53
49
54
50
az network asg create \
55
-
--resource-group myResourceGroup \
56
-
--name myAsgMgmtServers \
57
-
--location eastus
51
+
--resource-group test-rg \
52
+
--name asg-mgmt-servers \
53
+
--location westus2
58
54
```
59
55
60
56
### Create a network security group
61
57
62
-
Create a network security group with [az network nsg create](/cli/azure/network/nsg). The following example creates a network security group named *myNsg*:
58
+
Create a network security group with [az network nsg create](/cli/azure/network/nsg). The following example creates a network security group named *nsg-1*:
63
59
64
60
```azurecli-interactive
65
61
# Create a network security group
66
62
az network nsg create \
67
-
--resource-group myResourceGroup \
68
-
--name myNsg
63
+
--resource-group test-rg \
64
+
--name nsg-1
69
65
```
70
66
71
67
### Create security rules
72
68
73
-
Create a security rule with [az network nsg rule create](/cli/azure/network/nsg/rule). The following example creates a rule that allows traffic inbound from the internet to the *myWebServers* application security group over ports 80 and 443:
69
+
Create a security rule with [az network nsg rule create](/cli/azure/network/nsg/rule). The following example creates a rule that allows traffic inbound from the internet to the *asg-web-servers* application security group over ports 80 and 443:
74
70
75
71
```azurecli-interactive
76
72
az network nsg rule create \
77
-
--resource-group myResourceGroup \
78
-
--nsg-name myNsg \
73
+
--resource-group test-rg \
74
+
--nsg-name nsg-1 \
79
75
--name Allow-Web-All \
80
76
--access Allow \
81
77
--protocol Tcp \
82
78
--direction Inbound \
83
79
--priority 100 \
84
80
--source-address-prefix Internet \
85
81
--source-port-range "*" \
86
-
--destination-asgs "myAsgWebServers" \
82
+
--destination-asgs "asg-web-servers" \
87
83
--destination-port-range 80 443
88
84
```
89
85
90
-
The following example creates a rule that allows traffic inbound from the Internet to the *myMgmtServers* application security group over port 22:
86
+
The following example creates a rule that allows traffic inbound from the Internet to the *asg-mgmt-servers* application security group over port 22:
91
87
92
88
```azurecli-interactive
93
89
az network nsg rule create \
94
-
--resource-group myResourceGroup \
95
-
--nsg-name myNsg \
90
+
--resource-group test-rg \
91
+
--nsg-name nsg-1 \
96
92
--name Allow-SSH-All \
97
93
--access Allow \
98
94
--protocol Tcp \
99
95
--direction Inbound \
100
96
--priority 110 \
101
97
--source-address-prefix Internet \
102
98
--source-port-range "*" \
103
-
--destination-asgs "myAsgMgmtServers" \
99
+
--destination-asgs "asg-mgmt-servers" \
104
100
--destination-port-range 22
105
101
```
106
102
107
-
In this article, SSH (port 22) is exposed to the internet for the *myAsgMgmtServers* VM. For production environments, instead of exposing port 22 to the internet, it's recommended that you connect to Azure resources that you want to manage using a [VPN](../vpn-gateway/vpn-gateway-about-vpngateways.md?toc=%2fazure%2fvirtual-network%2ftoc.json) or [private](../expressroute/expressroute-introduction.md?toc=%2fazure%2fvirtual-network%2ftoc.json) network connection.
103
+
In this article, the *asg-mgmt-servers* asg exposes SSH (port 22) to the internet. For production environments, use a [VPN](../vpn-gateway/vpn-gateway-about-vpngateways.md?toc=%2fazure%2fvirtual-network%2ftoc.json) or [private](../expressroute/expressroute-introduction.md?toc=%2fazure%2fvirtual-network%2ftoc.json) network connection to manage Azure resources instead of exposing port 22 to the internet.
108
104
109
105
## Create a virtual network
110
106
111
-
Create a virtual network with [az network vnet create](/cli/azure/network/vnet). The following example creates a virtual named *myVirtualNetwork*:
107
+
Create a virtual network with [az network vnet create](/cli/azure/network/vnet). The following example creates a virtual named *vnet-1*:
112
108
113
109
```azurecli-interactive
114
110
az network vnet create \
115
-
--name myVirtualNetwork \
116
-
--resource-group myResourceGroup \
111
+
--name vnet-1 \
112
+
--resource-group test-rg \
117
113
--address-prefixes 10.0.0.0/16
118
114
```
119
115
120
-
Add a subnet to a virtual network with [az network vnet subnet create](/cli/azure/network/vnet/subnet). The following example adds a subnet named *mySubnet* to the virtual network and associates the *myNsg* network security group to it:
116
+
Add a subnet to a virtual network with [az network vnet subnet create](/cli/azure/network/vnet/subnet). The following example adds a subnet named *subnet-1* to the virtual network and associates the *nsg-1* network security group to it:
121
117
122
118
```azurecli-interactive
123
119
az network vnet subnet create \
124
-
--vnet-name myVirtualNetwork \
125
-
--resource-group myResourceGroup \
126
-
--name mySubnet \
120
+
--vnet-name vnet-1 \
121
+
--resource-group test-rg \
122
+
--name subnet-1 \
127
123
--address-prefix 10.0.0.0/24 \
128
-
--network-security-group myNsg
124
+
--network-security-group nsg-1
129
125
```
130
126
131
127
## Create virtual machines
132
128
133
129
Create two VMs in the virtual network so you can validate traffic filtering in a later step.
134
130
135
-
Create a VM with [az vm create](/cli/azure/vm). The following example creates a VM that will serve as a web server. The `--asgs myAsgWebServers` option causes Azure to make the network interface it creates for the VM a member of the *myAsgWebServers* application security group.
136
-
137
-
The `--nsg ""` option is specified to prevent Azure from creating a default network security group for the network interface Azure creates when it creates the VM. To streamline this article, a password is used. Keys are typically used in production deployments. If you use keys, you must also configure SSH agent forwarding for the remaining steps. For more information, see the documentation for your SSH client. Replace `<replace-with-your-password>` in the following command with a password of your choosing.
131
+
Create a VM with [az vm create](/cli/azure/vm). The following example creates a VM that serves as a web server. The `--asgs asg-web-servers` option causes Azure to make the network interface it creates for the VM a member of the *asg-web-servers* application security group. The `--nsg ""` option is specified to prevent Azure from creating a default network security group for the network interface Azure creates when it creates the VM. The command prompts you to create a password for the VM. SSH keys aren't used in this example to facilitate the later steps in this article. In a production environment, use SSH keys for security.
138
132
139
133
```azurecli-interactive
140
-
adminPassword="<replace-with-your-password>"
141
-
142
134
az vm create \
143
-
--resource-group myResourceGroup \
144
-
--name myVmWeb \
135
+
--resource-group test-rg \
136
+
--name vm-web \
145
137
--image Ubuntu2204 \
146
-
--vnet-name myVirtualNetwork \
147
-
--subnet mySubnet \
138
+
--vnet-name vnet-1 \
139
+
--subnet subnet-1 \
148
140
--nsg "" \
149
-
--asgs myAsgWebServers \
141
+
--asgs asg-web-servers \
150
142
--admin-username azureuser \
151
-
--admin-password $adminPassword
143
+
--authentication-type password \
144
+
--assign-identity
152
145
```
153
146
154
147
The VM takes a few minutes to create. After the VM is created, output similar to the following example is returned:
Take note of the **publicIpAddress**. This address is used to access the VM from the internet in a later step. Create a VM to serve as a management server:
162
+
Create a VM with [az vm create](/cli/azure/vm). The following example creates a VM that serves as a management server. The `--asgs asg-mgmt-servers` option causes Azure to make the network interface it creates for the VM a member of the *asg-mgmt-servers* application security group.
163
+
164
+
The following example creates a VM and adds a user account. The `--generate-ssh-keys` parameter causes the CLI to look for an available ssh key in `~/.ssh`. If one is found, that key is used. If not, one is generated and stored in `~/.ssh`. Finally, we deploy the latest `Ubuntu 22.04` image.
170
165
171
166
```azurecli-interactive
172
167
az vm create \
173
-
--resource-group myResourceGroup \
174
-
--name myVmMgmt \
168
+
--resource-group test-rg \
169
+
--name vm-mgmt \
175
170
--image Ubuntu2204 \
176
-
--vnet-name myVirtualNetwork \
177
-
--subnet mySubnet \
171
+
--vnet-name vnet-1 \
172
+
--subnet subnet-1 \
178
173
--nsg "" \
179
-
--asgs myAsgMgmtServers \
174
+
--asgs asg-mgmt-servers \
180
175
--admin-username azureuser \
181
-
--admin-password $adminPassword
176
+
--generate-ssh-keys \
177
+
--assign-identity
182
178
```
183
179
184
-
The VM takes a few minutes to create. After the VM is created, note the **publicIpAddress** in the returned output. This address is used to access the VM in the next step. Don't continue with the next step until Azure finishes creating the VM.
180
+
The VM takes a few minutes to create. Don't continue with the next step until Azure finishes creating the VM.
181
+
182
+
## Enable Microsoft Entra ID sign in for the virtual machines
183
+
184
+
The following code example the extension to enable a Microsoft Entra ID sign-in for a Linux VM. VM extensions are small applications that provide post-deployment configuration and automation tasks on Azure virtual machines.
185
+
186
+
```bash
187
+
az vm extension set \
188
+
--publisher Microsoft.Azure.ActiveDirectory \
189
+
--name AADSSHLoginForLinux \
190
+
--resource-group test-rg \
191
+
--vm-name vm-web
192
+
193
+
az vm extension set \
194
+
--publisher Microsoft.Azure.ActiveDirectory \
195
+
--name AADSSHLoginForLinux \
196
+
--resource-group test-rg \
197
+
--vm-name vm-mgmt
198
+
```
185
199
186
200
## Test traffic filters
187
201
188
-
Use the command that follows to create an SSH session with the *myVmMgmt* VM. Replace *\<publicIpAddress>* with the public IP address of your VM. In the example above, the IP address is *13.90.242.231*.
202
+
Using an SSH client of your choice, connect to the VMs created previously. For example, the following command can be used from a command line interface such as [Windows Subsystem for Linux](/windows/wsl/install)to create an SSH session with the *vm-mgmt* VM. In the previous steps, we enabled Microsoft Entra ID sign-in for the VMs. You can sign-in to the virtual machines using your Microsoft Entra ID credentials or you can use the SSH key that you used to create the VMs. In the following example, we use the SSH key to sign in to management VM and then sign in to the web VM from the management VM with a password.
189
203
190
-
```bash
191
-
ssh azureuser@<publicIpAddress>
204
+
For more information about how to SSH to a Linux VM and sign in with Microsoft Entra ID, see [Sign in to a Linux virtual machine in Azure by using Microsoft Entra ID and OpenSSH](/entra/identity/devices/howto-vm-sign-in-azure-ad-linux).
205
+
206
+
### Store IP address of VM in order to SSH
207
+
208
+
Run the following command to store the IP address of the VM as an environment variable:
209
+
210
+
```bash
211
+
export IP_ADDRESS=$(az vm show --show-details --resource-group test-rg --name vm-mgmt --query publicIps --output tsv)
192
212
```
193
213
194
-
When prompted for a password, enter the password you entered in [Create VMs](#create-virtual-machines).
The connection succeeds, because port 22 is allowed inbound from the Internet to the *myAsgMgmtServers* application security group that the network interface attached to the *myVmMgmt* VM is in.
218
+
The connection succeeds because the network interface attached to the *vm-mgmt* VM is in the *asg-mgmt-servers* application security group, which allows port 22 inbound from the Internet.
197
219
198
-
Use the following command to SSH to the *myVmWeb* VM from the *myVmMgmt* VM:
220
+
Use the following command to SSH to the *vm-web* VM from the *vm-mgmt* VM:
199
221
200
222
```bash
201
-
ssh azureuser@myVmWeb
223
+
ssh -o StrictHostKeyChecking=no azureuser@vm-web
202
224
```
203
225
204
-
The connection succeeds because a default security rule within each network security group allows traffic over all ports between all IP addresses within a virtual network. You can't SSH to the *myVmWeb* VM from the Internet because the security rule for the *myAsgWebServers* doesn't allow port 22 inbound from the Internet.
226
+
The connection succeeds because a default security rule within each network security group allows traffic over all ports between all IP addresses within a virtual network. You can't SSH to the *vm-web* VM from the Internet because the security rule for the *asg-web-servers* doesn't allow port 22 inbound from the Internet.
205
227
206
-
Use the following commands to install the nginx web server on the *myVmWeb* VM:
228
+
Use the following commands to install the nginx web server on the *vm-web* VM:
207
229
208
230
```bash
209
231
# Update package source
@@ -213,24 +235,27 @@ sudo apt-get -y update
213
235
sudo apt-get -y install nginx
214
236
```
215
237
216
-
The *myVmWeb* VM is allowed outbound to the Internet to retrieve nginx because a default security rule allows all outbound traffic to the Internet. Exit the *myVmWeb* SSH session, which leaves you at the `username@myVmMgmt:~$` prompt of the *myVmMgmt* VM. To retrieve the nginx welcome screen from the *myVmWeb* VM, enter the following command:
238
+
The *vm-web* VM is allowed outbound to the Internet to retrieve nginx because a default security rule allows all outbound traffic to the Internet. Exit the *vm-web* SSH session, which leaves you at the `username@vm-mgmt:~$` prompt of the *vm-mgmt* VM. To retrieve the nginx welcome screen from the *vm-web* VM, enter the following command:
217
239
218
240
```bash
219
-
curl myVmWeb
241
+
curl vm-web
220
242
```
221
243
222
-
Logout of the *myVmMgmt* VM. To confirm that you can access the *myVmWeb* web server from outside of Azure, enter `curl <publicIpAddress>` from your own computer. The connection succeeds, because port 80 is allowed inbound from the Internet to the *myAsgWebServers* application security group that the network interface attached to the *myVmWeb* VM is in.
244
+
Sign out of the *vm-mgmt* VM. To confirm that you can access the *vm-web* web server from outside of Azure, enter `curl <publicIpAddress>` from your own computer. The connection succeeds because the *asg-web-servers* application security group, which the network interface attached to the *vm-web* VM is in, allows port 80 inbound from the Internet.
223
245
224
246
## Clean up resources
225
247
226
248
When no longer needed, use [az group delete](/cli/azure/group) to remove the resource group and all of the resources it contains.
227
249
228
250
```azurecli-interactive
229
-
az group delete --name myResourceGroup --yes
251
+
az group delete \
252
+
--name test-rg \
253
+
--yes \
254
+
--no-wait
230
255
```
231
256
232
257
## Next steps
233
258
234
259
In this article, you created a network security group and associated it to a virtual network subnet. To learn more about network security groups, see [Network security group overview](./network-security-groups-overview.md) and [Manage a network security group](manage-network-security-group.md).
235
260
236
-
Azure routes traffic between subnets by default. You may instead, choose to route traffic between subnets through a VM, serving as a firewall, for example. To learn how, see [Create a route table](tutorial-create-route-table-cli.md).
261
+
Azure routes traffic between subnets by default. You can instead, choose to route traffic between subnets through a VM, serving as a firewall, for example. To learn how, see [Create a route table](tutorial-create-route-table-cli.md).
0 commit comments