Block outbound SSH with an NSG rule
- Block SSH (22) from vm-web to vm-db
- Test a port from vm-web with nc
Security wants vm-web (in subnet-web) stopped from opening SSH sessions (port 22) to anything in subnet-db (10.1.3.0/24). The app on vm-web still needs SQL on vm-db (10.1.3.4) at port 1433. nsg-web is already on subnet-web, with no rules yet. Add an outbound rule to it, then log in to vm-web and test both ports.
Add an outbound deny for SSH to subnet-db.
az network nsg rule create -g rg-lab --nsg-name nsg-web --name Deny-SSH-To-DB --priority 200 --direction Outbound --access Deny --protocol Tcp --destination-address-prefixes 10.1.3.0/24 --destination-port-ranges 22What's this?
Adds or changes one rule in a network security group (NSG). Rules are checked from the lowest priority number up, and the first match decides.
-gthe resource group the resources live in--nsg-namethe network security group the rule is in--namethe name of the thing to look at or change--prioritywhere the rule sits in the order. Lower numbers are checked first--directionInbound for traffic coming in to the VM, Outbound for traffic leaving it--accessAllow or Deny--protocolTCP or UDP--destination-port-rangeswhich ports the rule matches
--direction Outboundfilter traffic leaving subnet-web--destination-address-prefixes 10.1.3.0/24only traffic headed to subnet-db
Connect to vm-web. ssh opens a shell on the VM, so the next commands run from vm-web, not from Cloud Shell.
ssh vm-webWhat's this?
Opens a shell on vm-web, so the next commands run on that VM. In this lab it's a shortcut. In real Azure you'd connect through something like Azure Bastion, or SSH to an address you can reach.
Test SSH to vm-db. nc tries to open a TCP connection and tells you if the port answers. It should fail now.
nc -zv 10.1.3.4 22What's this?
netcat, a standard Linux tool. Tries to open a TCP connection and reports whether it worked.
-zonly check whether the port answers. Send no data-vprint the result in words10.1.3.4where to connect22the port to try
-zonly check the port, send no data-vprint whether it worked10.1.3.4 22the IP and port to try
Test SQL to vm-db. It should still work. Type exit when done to go back to Cloud Shell.
nc -zv 10.1.3.4 1433What's this?
netcat, a standard Linux tool. Tries to open a TCP connection and reports whether it worked.
-zonly check whether the port answers. Send no data-vprint the result in words10.1.3.4where to connect1433the port to try
Answer the question below.
Sources on Microsoft Learn (2)
?
- Resource group, the outer dashed frame
- Virtual network, with its address space top right
- Subnet, with range and usable IPs (5 reserved)
- Network security group. A small shield on a VM's NIC means the NIC has one too
- Route table on the subnet
- VM Appliance Private endpoint
- Peering Connected Initiated or access off
- Goal working Goal broken, X marks where it stops
- Private DNS zone, dotted lines go to linked VNets
- Orange pulse: what your last command changed. Drag to pan, Ctrl and scroll to zoom.
vm-web → vm-db:22should stay blocked: open, but it should be blockedvm-web → vm-db:1433should work: working