Azure port blocking is usually done with a Network Security Group (NSG). Create a deny rule for the required inbound or outbound port, assign the NSG to the relevant subnet or virtual machine network interface, and make sure any higher-priority allow rule does not override it. Azure evaluates NSG rules by priority, with lower numbers evaluated first.
Block a port in the Azure portal
- Open Network security groups in the Azure portal.
- Select an existing NSG or create a new one.
- Open Inbound security rules or Outbound security rules , depending on the traffic direction.
- Select + Add.
- Configure the rule:
- Source: Any, or a specific IP range
- Source port ranges:
* - Destination: Any, or a selected service or IP
- Destination port ranges: Enter the port, such as
22,3389, or8080 - Protocol: TCP, UDP, or Any
- Action: Deny
- Priority: Choose a number lower than conflicting allow rules
- Name: For example,
Deny-SSH-Inbound
- Select Add , then confirm that the NSG is associated with the VM’s network interface or subnet.
An NSG can block public access while still allowing private traffic. For
example, you could deny port 3389 from the Internet but create a higher-
priority allow rule for trusted administration IP addresses or a permitted
virtual network. Microsoft also documents using Azure Virtual Network Manager
security configurations to apply blocking rules across selected network
groups.
Example: block public SSH
To block SSH from the Internet, create an inbound rule with:
- Source: Internet
- Destination port:
22 - Protocol: TCP
- Action: Deny
- Priority:
100
If administrators still need SSH access, add an allow rule for their fixed
public IP range with a lower priority number, such as 50. Keep the deny rule
below that allow rule, because Azure processes the lower priority number
first.
Important checks
- Review NSGs attached to both the subnet and VM network interface.
- Check for existing allow rules with higher precedence.
- For outbound blocking, configure an outbound rule rather than an inbound rule.
- Test from the intended source network after deployment.
- Use Azure Policy if you need to prevent users from creating inbound rules for selected high-risk ports across a subscription.
