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
Copy file name to clipboardExpand all lines: articles/virtual-network/tutorial-filter-network-traffic-cli.md
+92-62Lines changed: 92 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,37 +31,37 @@ A network security group contains security rules. Security rules specify a sourc
31
31
32
32
### Create application security groups
33
33
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 *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:
35
35
36
36
```azurecli-interactive
37
37
az group create \
38
-
--name myResourceGroup \
39
-
--location eastus
38
+
--name test-rg \
39
+
--location westus2
40
40
```
41
41
42
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.
43
43
44
44
```azurecli-interactive
45
45
az network asg create \
46
-
--resource-group myResourceGroup \
47
-
--name myAsgWebServers \
48
-
--location eastus
46
+
--resource-group test-rg \
47
+
--name asg-web-servers \
48
+
--location westus2
49
49
50
50
az network asg create \
51
-
--resource-group myResourceGroup \
52
-
--name myAsgMgmtServers \
53
-
--location eastus
51
+
--resource-group test-rg \
52
+
--name asg-mgmt-servers \
53
+
--location westus2
54
54
```
55
55
56
56
### Create a network security group
57
57
58
-
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*:
59
59
60
60
```azurecli-interactive
61
61
# Create a network security group
62
62
az network nsg create \
63
-
--resource-group myResourceGroup \
64
-
--name myNsg
63
+
--resource-group test-rg \
64
+
--name nsg-1
65
65
```
66
66
67
67
### Create security rules
@@ -70,136 +70,163 @@ Create a security rule with [az network nsg rule create](/cli/azure/network/nsg/
70
70
71
71
```azurecli-interactive
72
72
az network nsg rule create \
73
-
--resource-group myResourceGroup \
74
-
--nsg-name myNsg \
73
+
--resource-group test-rg \
74
+
--nsg-name nsg-1 \
75
75
--name Allow-Web-All \
76
76
--access Allow \
77
77
--protocol Tcp \
78
78
--direction Inbound \
79
79
--priority 100 \
80
80
--source-address-prefix Internet \
81
81
--source-port-range "*" \
82
-
--destination-asgs "myAsgWebServers" \
82
+
--destination-asgs "asg-web-servers" \
83
83
--destination-port-range 80 443
84
84
```
85
85
86
86
The following example creates a rule that allows traffic inbound from the Internet to the *myMgmtServers* application security group over port 22:
87
87
88
88
```azurecli-interactive
89
89
az network nsg rule create \
90
-
--resource-group myResourceGroup \
91
-
--nsg-name myNsg \
90
+
--resource-group test-rg \
91
+
--nsg-name nsg-1 \
92
92
--name Allow-SSH-All \
93
93
--access Allow \
94
94
--protocol Tcp \
95
95
--direction Inbound \
96
96
--priority 110 \
97
97
--source-address-prefix Internet \
98
98
--source-port-range "*" \
99
-
--destination-asgs "myAsgMgmtServers" \
99
+
--destination-asgs "asg-mgmt-servers" \
100
100
--destination-port-range 22
101
101
```
102
102
103
-
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, SSH (port 22) is exposed to the internet for the *asg-mgmt-servers* 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.
104
104
105
105
## Create a virtual network
106
106
107
-
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*:
108
108
109
109
```azurecli-interactive
110
110
az network vnet create \
111
-
--name myVirtualNetwork \
112
-
--resource-group myResourceGroup \
111
+
--name vnet-1 \
112
+
--resource-group test-rg \
113
113
--address-prefixes 10.0.0.0/16
114
114
```
115
115
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 *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:
117
117
118
118
```azurecli-interactive
119
119
az network vnet subnet create \
120
-
--vnet-name myVirtualNetwork \
121
-
--resource-group myResourceGroup \
122
-
--name mySubnet \
120
+
--vnet-name vnet-1 \
121
+
--resource-group test-rg \
122
+
--name subnet-1 \
123
123
--address-prefix 10.0.0.0/24 \
124
-
--network-security-group myNsg
124
+
--network-security-group nsg-1
125
125
```
126
126
127
127
## Create virtual machines
128
128
129
129
Create two VMs in the virtual network so you can validate traffic filtering in a later step.
130
130
131
-
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.
131
+
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 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.
132
132
133
-
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.
133
+
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 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.
134
134
135
135
```azurecli-interactive
136
-
adminPassword="<replace-with-your-password>"
137
-
138
136
az vm create \
139
-
--resource-group myResourceGroup \
140
-
--name myVmWeb \
137
+
--resource-group test-rg \
138
+
--name vm-web \
141
139
--image Ubuntu2204 \
142
-
--vnet-name myVirtualNetwork \
143
-
--subnet mySubnet \
140
+
--vnet-name vnet-1 \
141
+
--subnet subnet-1 \
144
142
--nsg "" \
145
-
--asgs myAsgWebServers \
143
+
--asgs asg-web-servers \
146
144
--admin-username azureuser \
147
-
--admin-password $adminPassword
145
+
--generate-ssh-keys
148
146
```
149
147
150
148
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:
163
+
Create a VM to serve as a management server:
166
164
167
165
```azurecli-interactive
168
166
az vm create \
169
-
--resource-group myResourceGroup \
170
-
--name myVmMgmt \
167
+
--resource-group test-rg \
168
+
--name vm-mgmt \
171
169
--image Ubuntu2204 \
172
-
--vnet-name myVirtualNetwork \
173
-
--subnet mySubnet \
170
+
--vnet-name vnet-1 \
171
+
--subnet subnet-1 \
174
172
--nsg "" \
175
-
--asgs myAsgMgmtServers \
173
+
--asgs asg-mgmt-servers \
176
174
--admin-username azureuser \
177
-
--admin-password $adminPassword
175
+
--generate-ssh-keys
178
176
```
179
177
180
-
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.
178
+
The VM takes a few minutes to create. Don't continue with the next step until Azure finishes creating the VM.
179
+
180
+
## Enable Microsoft Entra ID sign in for the virtual machines
181
+
182
+
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.
183
+
184
+
```bash
185
+
az vm extension set \
186
+
--publisher Microsoft.Azure.ActiveDirectory \
187
+
--name AADSSHLoginForLinux \
188
+
--resource-group test-rg \
189
+
--vm-name vm-web
190
+
```
191
+
192
+
```bash
193
+
az vm extension set \
194
+
--publisher Microsoft.Azure.ActiveDirectory \
195
+
--name AADSSHLoginForLinux \
196
+
--resource-group test-rg \
197
+
--vm-name vm-mgmt
198
+
```
181
199
182
200
## Test traffic filters
183
201
184
-
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 login 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 login to the VMs.
185
203
186
-
```bash
187
-
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
+
207
+
### Store IP address of VM in order to SSH
208
+
209
+
Run the following command to store the IP address of the VM as an environment variable:
210
+
211
+
```bash
212
+
export IP_ADDRESS=$(az vm show --show-details --resource-group test-rg --name vm-mgmt --query publicIps --output tsv)
188
213
```
189
214
190
-
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.
219
+
The connection succeeds, because port 22 is allowed inbound from the Internet to the *asg-mgmt-servers* application security group that the network interface attached to the *vm-mgmt* VM is in.
193
220
194
-
Use the following command to SSH to the *myVmWeb* VM from the *myVmMgmt* VM:
221
+
Use the following command to SSH to the *vm-web* VM from the *vm-mgmt* VM:
195
222
196
223
```bash
197
-
ssh azureuser@myVmWeb
224
+
ssh -o StrictHostKeyChecking=no azureuser@vm-web
198
225
```
199
226
200
-
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.
227
+
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.
201
228
202
-
Use the following commands to install the nginx web server on the *myVmWeb* VM:
229
+
Use the following commands to install the nginx web server on the *vm-web* VM:
203
230
204
231
```bash
205
232
# Update package source
@@ -209,20 +236,23 @@ sudo apt-get -y update
209
236
sudo apt-get -y install nginx
210
237
```
211
238
212
-
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:
239
+
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:
213
240
214
241
```bash
215
-
curl myVmWeb
242
+
curl vm-web
216
243
```
217
244
218
-
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.
245
+
Logout 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 port 80 is allowed inbound from the Internet to the *asg-web-servers* application security group that the network interface attached to the *vm-web* VM is in.
219
246
220
247
## Clean up resources
221
248
222
249
When no longer needed, use [az group delete](/cli/azure/group) to remove the resource group and all of the resources it contains.
0 commit comments