Home Microsoft AzureAzure NAT Gateway: Secure Outbound Access for Linux VMs

Azure NAT Gateway: Secure Outbound Access for Linux VMs

by Knowledge Base Linux
Azure NAT Gateway

Virtual machines in a private subnet still need outbound internet access for package updates, API calls, and license checks, but giving each VM a public IP address expands its attack surface. Azure NAT Gateway solves this cleanly: it provides managed, outbound-only internet access for a whole subnet through one or more static public IPs, with no inbound connections allowed. This guide shows how to set up Azure NAT Gateway for Linux VMs, from subnet design to verification.

You will create a NAT gateway with a static public IP, associate it with a subnet, deploy a VM without a public IP, and prove that outbound traffic works while inbound traffic does not. The examples use the Azure CLI, which makes the setup repeatable for additional subnets.

Azure NAT Gateway illustration

Prerequisites for Azure NAT Gateway

You need an Azure subscription, the Azure CLI authenticated, and a resource group with a virtual network. You should also know which subnets need outbound access, because NAT Gateway is associated at the subnet level: every VM in an associated subnet shares the gateway, and VMs in other subnets are unaffected.

One design rule matters from the start: a subnet cannot use both NAT Gateway and a per-VM public IP for outbound traffic in a simple way, because NAT Gateway takes precedence for outbound flows. Plan your subnets so that private VMs use NAT Gateway and any VM that genuinely needs a public IP sits in a separate subnet.

Step 1: Create a Public IP and NAT Gateway

Create a static Standard SKU public IP address first, then the NAT gateway that uses it. Standard SKU is required; Basic SKU public IPs do not work with NAT Gateway. Use a consistent naming scheme so the gateway is easy to find later:

az network public-ip create \
  --resource-group rg-linux \
  --name pip-nat-01 \
  --sku Standard \
  --allocation-method Static \
  --zone 1 2 3
az network nat gateway create \
  --resource-group rg-linux \
  --name natgw-01 \
  --public-ip-addresses pip-nat-01 \
  --idle-timeout 10 \
  --location eastus

The idle timeout controls how long an idle TCP connection stays open through the gateway. Ten minutes is a reasonable default; long-lived connections such as database sessions may need a higher value, while short API calls can use a lower one.

Step 2: Associate the NAT Gateway with a Subnet

Associate the gateway with the subnet that hosts your private VMs. This single step changes outbound routing for the whole subnet, so do it during a maintenance window if the subnet already hosts production workloads:

az network vnet subnet update \
  --resource-group rg-linux \
  --vnet-name vnet-linux \
  --name snet-private \
  --nat-gateway natgw-01
az network vnet subnet show \
  --resource-group rg-linux \
  --vnet-name vnet-linux \
  --name snet-private \
  --query natGateway.id

Confirm the association in the output before testing. If the subnet already has a user-defined route table, check that it does not override the NAT Gateway path: a route to a firewall appliance or virtual appliance takes precedence and can silently bypass the gateway.

Step 3: Deploy a Linux VM Without a Public IP

Create a Linux VM in the private subnet without a public IP address. Without NAT Gateway, this VM would have no outbound internet at all; with the gateway, it can reach the internet but cannot be reached from it:

az vm create \
  --resource-group rg-linux \
  --name vm-private-01 \
  --image Ubuntu2404 \
  --vnet-name vnet-linux \
  --subnet snet-private \
  --public-ip-address "" \
  --admin-username azureuser \
  --generate-ssh-keys

Connect to the VM through Azure Bastion or a jump host in a public subnet, since there is no public IP to SSH to directly. Then test outbound access from inside the VM:

curl -sI https://archive.ubuntu.com/
sudo apt update
curl -s https://ifconfig.me/

The last command should return the NAT Gateway public IP, not the VM’s private address. That confirms outbound traffic is leaving through the gateway. Package updates working from a VM with no public IP is the whole point of this architecture.

Step 4: Verify Inbound Traffic Is Blocked

Outbound-only is a security property, so verify it rather than assuming it. From an external machine, try to reach the VM’s private address, which should fail, and confirm that no public IP is associated with the VM’s network interface:

az vm show --resource-group rg-linux --name vm-private-01 \
  --query "publicIpAddress" --output tsv
az network nic show --resource-group rg-linux --name vm-private-01VMNic \
  --query "ipConfigurations[0].publicIpAddress"

Both should be empty. Also review the network security group for the subnet: NAT Gateway does not replace NSG rules, so keep inbound rules restrictive and allow only the management paths you actually use, such as Bastion traffic.

Troubleshooting Azure NAT Gateway

If outbound traffic fails after association, check the basics in order: the subnet association, the public IP SKU, and the NSG outbound rules. A Basic SKU public IP or a missing association are the most common setup errors, and both are visible in the Azure portal or CLI output.

If connections drop intermittently, you may be exhausting SNAT ports. Each public IP provides 64,512 SNAT ports, shared across all VMs in the subnet. Add a second public IP to the gateway, reduce the idle timeout so ports recycle faster, or split busy subnets so each has its own gateway. Monitor SNAT port usage in Azure Monitor before adding capacity blindly.

If DNS resolution fails but IP connectivity works, the problem is not NAT Gateway: check the virtual network DNS settings and any custom DNS servers. NAT Gateway forwards traffic as-is, so a DNS failure points at DNS configuration, not at the gateway.

Conclusion

Azure NAT Gateway gives private Linux VMs safe outbound internet access through a single, stable public IP, without exposing them to inbound traffic. Associate it at the subnet level, verify both directions of traffic, and monitor SNAT port usage as the subnet grows. For related Azure networking, see our guides on Azure Bastion for Linux VMs and private endpoints for Blob Storage. The official Azure NAT Gateway documentation covers zone redundancy and multiple public IPs in detail.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.