Documentation Index
Fetch the complete documentation index at: https://resources.devweekends.com/llms.txt
Use this file to discover all available pages before exploring further.
Module 10: NAT & PAT Deep Dive
NAT (Network Address Translation) is one of the most critical networking concepts. It’s the reason your home network with private IPs can access the internet, and it’s fundamental to understanding cloud networking, firewalls, and security. The best analogy for NAT is a company receptionist handling phone calls. Employees (private IPs) all make outgoing calls, but the outside world only sees the company’s main phone number (public IP). The receptionist (NAT device) keeps a log: “Extension 204 is talking to Client X on line 3.” When Client X calls back, the receptionist checks the log and routes the call to extension 204. Without the receptionist, outsiders would have no idea how to reach a specific extension — and that is exactly the point.Estimated Time: 3-4 hours
Difficulty: Intermediate
Prerequisites: Module 9 (IP Addressing Deep Dive)
Difficulty: Intermediate
Prerequisites: Module 9 (IP Addressing Deep Dive)
10.1 The Problem NAT Solves
The IPv4 Exhaustion Crisis
Before NAT (Theoretical)
With NAT (Reality)
10.2 Types of NAT
1. Static NAT (One-to-One)
Maps one private IP to one public IP permanently.2. Dynamic NAT (Many-to-Many)
Maps private IPs to a pool of public IPs dynamically.3. PAT / NAT Overload (Many-to-One) — Most Common
Port Address Translation maps many private IPs to ONE public IP using port numbers. This is the type of NAT that makes the modern internet possible — it is what runs on your home router right now.10.3 How PAT Works Step-by-Step
Scenario: Your laptop (192.168.1.10) visits google.com
NAT Translation (Outbound)
Router receives packet, creates NAT table entry:
Rewrites packet:
| Private IP:Port | Public IP:Port | Destination |
|---|---|---|
| 192.168.1.10:54321 | 203.0.113.50:10001 | 142.250.190.46:443 |
NAT Translation (Inbound)
Router looks up NAT table:
- Dest Port 10001 → maps to 192.168.1.10:54321
NAT Table Example
When multiple devices are active:| Private IP:Port | Public IP:Port | Destination | Protocol | Timeout |
|---|---|---|---|---|
| 192.168.1.10:54321 | 203.0.113.50:10001 | 142.250.190.46:443 | TCP | 3600s |
| 192.168.1.10:54322 | 203.0.113.50:10002 | 151.101.1.69:443 | TCP | 3600s |
| 192.168.1.11:60000 | 203.0.113.50:10003 | 142.250.190.46:443 | TCP | 3600s |
| 192.168.1.12:49152 | 203.0.113.50:10004 | 13.107.42.14:443 | TCP | 3600s |
10.4 NAT Terminology
NAT Address Types
| Term | Meaning |
|---|---|
| Inside Local | Private IP of internal host (your device’s IP) |
| Inside Global | Public IP representing internal host (router’s public IP) |
| Outside Local | How external host appears to internal network |
| Outside Global | Actual public IP of external host |
10.5 NAT Gateway in Cloud (AWS Example)
In cloud environments like AWS, NAT Gateways serve a specific purpose:The Problem
The Solution: NAT Gateway
- Private instance sends packet to internet (e.g., apt-get update)
- Route table sends 0.0.0.0/0 traffic to NAT Gateway
- NAT Gateway translates private IP to its Elastic IP
- Response comes back to NAT Gateway
- NAT Gateway translates back to private IP
10.6 Port Forwarding
NAT blocks inbound connections by default. Port forwarding creates explicit mappings.Use Case: Hosting a Minecraft Server at Home
Common Port Forwarding Scenarios
| Service | External Port | Internal Target |
|---|---|---|
| Web Server | 80, 443 | 192.168.1.10:80 |
| SSH | 22 | 192.168.1.10:22 |
| Minecraft | 25565 | 192.168.1.100:25565 |
| Plex | 32400 | 192.168.1.50:32400 |
10.7 NAT Traversal Problems
NAT breaks the end-to-end principle of the internet. This causes issues for:1. Peer-to-Peer Applications
Solutions
STUN
Session Traversal Utilities for NAT
- Discovers your public IP and port
- Works for ~80% of NAT types
TURN
Traversal Using Relays around NAT
- Relay server forwards all traffic
- Works for all NAT types
- Higher latency, more bandwidth cost
ICE
Interactive Connectivity Establishment
- Tries STUN first, falls back to TURN
- Used by WebRTC
UPnP
Universal Plug and Play
- Apps automatically configure port forwarding
- Security risk if enabled
10.8 Double NAT (Carrier-Grade NAT)
Some ISPs use NAT themselves due to IPv4 exhaustion:Problems with Double NAT
- Port forwarding doesn’t work (you don’t control ISP’s NAT)
- Slower due to double translation
- Some games/VoIP may have issues
- You might share public IP with hundreds of other customers
How to Check for CGNAT
10.9 NAT vs Firewall
People often confuse NAT with firewalls. They’re different:| Aspect | NAT | Firewall |
|---|---|---|
| Purpose | Address translation | Security filtering |
| Function | Rewrites IP/port | Allows/blocks traffic |
| Default Inbound | Drops (no mapping) | Configurable rules |
| Security | Side effect, not purpose | Primary purpose |
10.10 Summary: NAT at a Glance
Next Module
Module 11: Routing Deep Dive
Master how routers make decisions, understand BGP, OSPF, and how the internet’s routing actually works.
Interview Deep-Dive
Explain how PAT allows thousands of devices to share a single public IP. What are the limits?
Explain how PAT allows thousands of devices to share a single public IP. What are the limits?
Is NAT a security feature? What is the difference between NAT and a firewall?
Is NAT a security feature? What is the difference between NAT and a firewall?
Strong Answer:
- NAT’s purpose is address translation, not security. A firewall’s purpose is policy-based traffic filtering. They overlap because PAT inherently drops unsolicited inbound connections — with no matching NAT table entry, incoming packets are discarded.
- However, calling NAT a security feature is misleading. NAT has no policy framework, no logging, no granular control. A firewall lets you define rules like “allow port 443 from 10.0.0.0/8, deny from everywhere else.” NAT’s implicit blocking is all-or-nothing. Port forwarding rules punch holes that NAT cannot control granularly.
- Critically, if you rely on NAT for security and migrate to IPv6 (where NAT is unnecessary), you lose that implicit protection with no firewall rules in place.
- Best practice: always deploy a proper firewall regardless of NAT. In AWS, security groups are your firewall, NAT Gateway handles translation. Separate services, separate responsibilities.
Your AWS bill shows high NAT Gateway data charges. How do you investigate and reduce cost?
Your AWS bill shows high NAT Gateway data charges. How do you investigate and reduce cost?
Strong Answer:
- Enable VPC Flow Logs on private subnets to identify which instances generate the most outbound NAT traffic. Aggregate by destination to find top traffic targets.
- The most common culprit: instances accessing S3 through NAT instead of a free VPC Gateway Endpoint. Adding S3 and DynamoDB gateway endpoints can cut NAT bills by 60-80%. Traffic to other AWS services (SQS, SNS, ECR, CloudWatch) can go through Interface VPC Endpoints, also bypassing NAT.
- Check for chatty monitoring agents sending metrics to external SaaS. Consider aggregating locally or using AWS-native monitoring. Check for health checks against external endpoints that could be internal.
- NAT Gateway costs roughly 450/month in processing alone. Gateway endpoints are free for S3 and DynamoDB.