Network address translation
Network Address Translation (NAT) is a method used by routers or firewalls to modify network address information in the IP header of packets while they are in transit. Its primary function is to allow multiple devices within a local network (typically using private IP addresses) to share a single public IP address for communicating with external networks, such as the Internet.
NAT was widely adopted as a temporary solution to mitigate the depletion of available IPv4 addresses by allowing many devices behind a single public IP, although it also provides a layer of security by hiding the internal network structure.
Contents
Overview and Purpose
In the early days of the internet, it was assumed that every device connected directly to the internet would have its own unique, globally routable public IP address. With the explosive growth of the internet and connected devices, it became clear that the 4.3 billion addresses available in IPv4 would not be sufficient.
NAT was introduced as a solution. It allows organizations and homes to use blocks of private IP addresses internally (e.g., `192.168.x.x`, `10.x.x.x`), which are not routable on the public internet. A NAT device (usually a router or firewall) sits at the boundary between the private network and the public internet.
When a device on the private network sends an outgoing packet to the internet, the NAT device intercepts it and translates the packet's source IP address (the private IP) to the NAT device's own public IP address. When the response packet comes back from the internet, the NAT device recognizes which internal device the packet is intended for and translates the destination IP address back to the private IP before forwarding it into the internal network.
Besides IPv4 conservation, NAT also provides a degree of security by making it difficult for external entities to initiate connections directly to internal devices, as their private IP addresses are not visible on the public internet.
How it Works
NAT maintains a mapping table to keep track of active connections and perform translations.
When an outgoing packet from a private network device reaches the NAT router: 1. The router notes the packet's original source IP (private) and source port. 2. It replaces the source IP with the router's own public IP. 3. It replaces the source port with a unique public port number from a pool of available ports managed by the router. 4. It stores the mapping (original private IP + port <-> public IP + translated public port) in its NAT table. 5. The packet is then forwarded to the internet with the translated source information.
When an incoming packet from the internet arrives at the NAT router for a specific public IP and port: 1. The router checks its NAT table for a mapping that matches the destination public IP and port. 2. If a match is found, it replaces the destination IP address with the corresponding original private IP from the table. 3. It replaces the destination port with the corresponding original private port. 4. The packet is then forwarded to the appropriate device on the private network.
Types of NAT
NAT comes in several variations:
- Source NAT (SNAT) or Masquerading
- Modifies the source address of packets as they leave the private network. This is the most common type used to allow multiple internal devices to share a single public IP for outgoing connections. Masquerading is a specific form of SNAT typically used when the public IP address of the router is dynamic, as it automatically uses the current public IP.
- Destination NAT (DNAT) or Port Forwarding
- Modifies the destination address of packets as they arrive at the router from an external network. This is used to direct incoming connections targeted at a specific public IP and port to a particular device and port on the internal network. For example, forwarding incoming traffic on public port 80 to an internal web server on its private IP and port 80.
- Static NAT
- A one-to-one mapping between a private IP address and a public IP address. Less common than SNAT/Masquerading as it doesn't conserve public IPv4 addresses but can be useful for making an internal server always accessible via a dedicated public IP.
NAT vs. Public IPs for Every Device
Historically, the alternative to NAT was giving every device a unique public IP address.
- Advantages of NAT
- - **IPv4 Conservation:** Allows millions of devices to connect to the internet using a limited number of public IPs.
- - **Security:** Hides the internal network structure from the public internet, providing a basic layer of privacy and making direct attacks on internal devices more difficult (unless ports are forwarded).
- Disadvantages of NAT
- - **Breaks End-to-End Connectivity:** Devices on the internet cannot directly initiate connections to internal devices without specific NAT configurations (like port forwarding).
- - **Complicates Peer-to-Peer:** Can make setting up peer-to-peer applications, online gaming, and some communication protocols challenging.
- - **Troubleshooting:** Can sometimes make diagnosing network problems more complex.
- - **IPv6 Irrelevance:** With the vast address space of IPv6, NAT for address conservation is largely unnecessary in IPv6 networks.
Setting up NAT (Masquerading/SNAT) on Debian Linux Server (CLI Tutorial)
This tutorial explains how to configure a Debian Linux server to act as a NAT gateway (specifically using Masquerading, a type of SNAT) for an internal network. This allows devices on the internal network to access the internet using the server's public IP address.
Scenario:
- Your Linux server has at least two network interfaces.
- One interface (e.g., `eth0`, `ens3`) is connected to the public internet and has a public IP address.
- Another interface (e.g., `eth1`, `ens4`) is connected to your internal private network and has a private IP address (e.g., `192.168.1.1`).
- Devices on your internal network are configured to use the server's internal IP (`192.168.1.1`) as their default gateway.
Prerequisites:
- A Debian-based Linux server (e.g., Debian 10+, Ubuntu 18.04+).
- Command-line access (SSH).
- sudo privileges.
- At least two configured network interfaces.
- IP addresses assigned to both interfaces.
- Basic understanding of network interfaces and IP addressing.
Steps:
- Enable IP Forwarding:
The Linux kernel must be configured to forward packets between network interfaces. Edit the system control configuration file:
sudo nano /etc/sysctl.conf
Find the line `#net.ipv4.ip_forward=1` and uncomment it by removing the `#` at the beginning: text
net.ipv4.ip_forward=1
Save and close the file. Apply the changes immediately:
sudo sysctl -p
You can verify it's enabled:
cat /proc/sys/net/ipv4/ip_forward
should output `1`
- Install nftables (if not already installed):
`nftables` is the modern packet filtering framework in Linux, replacing `iptables` as the default in recent distributions.
sudo apt update sudo apt install nftables -y
- Add the Masquerade Rule using nftables:
This rule tells the kernel to perform NAT (specifically masquerading) for outgoing traffic leaving the specified interface. Replace `your_outbound_interface` with the name of your server's interface connected to the public internet (e.g., `eth0`, `ens3`, `peth0`). You can find interface names using `ip a`.
sudo nft add table ip nat sudo nft add chain ip nat POSTROUTING { type nat hook postrouting priority 100 ; } sudo nft add rule ip nat POSTROUTING oifname "your_outbound_interface" masquerade
- `add table ip nat`: Creates a new NAT table for IPv4.
- `add chain ip nat POSTROUTING ...`: Creates a chain named `POSTROUTING` within the `nat` table, hooked into the kernel's post-routing stage (after routing decision is made, just before packet leaves). `type nat` and `hook postrouting priority 100` are standard for source NAT.
- `add rule ip nat POSTROUTING oifname "your_outbound_interface" masquerade`: Adds a rule to the `POSTROUTING` chain. `oifname` matches packets based on the outgoing interface name. `masquerade` is the action to perform SNAT, automatically using the IP of the outgoing interface.
- List the nftables ruleset:
Check that your rules have been added.
sudo nft list ruleset
You should see the `nat` table and the `POSTROUTING` chain with your masquerade rule.
- Make the nftables rules persistent:
The rules added via `nft add` are temporary and will be lost on reboot. You need to save them to the configuration file and enable the nftables service. Save the current ruleset:
sudo nft list ruleset > /etc/nftables.conf
Enable and start the nftables service to load the rules on boot:
sudo systemctl enable nftables sudo systemctl start nftables
- (Optional) Using iptables for Masquerading:
If you are using the older `iptables` command or the `iptables-nft` compatibility layer, the equivalent commands for masquerading are:
sudo iptables -t nat -A POSTROUTING -o your_outbound_interface -j MASQUERADE sudo iptables-save | sudo tee /etc/iptables/rules.v4 # Save rules (location varies)
You would then typically need to ensure a service loads these saved iptables rules on boot.
After completing these steps, devices on your internal network (configured to use the Linux server as their gateway) should be able to access the internet via NAT.
Destination NAT (DNAT / Port Forwarding): Setting up DNAT (Port Forwarding) involves adding rules to the `prerouting` chain in the `nat` table to redirect incoming traffic. For example, to forward incoming TCP traffic on public port 80 to an internal web server at `192.168.1.100` on port 80:
- Using `nftables`: `sudo nft add rule ip nat PREROUTING tcp dport 80 counter dnat to 192.168.1.100:80` (Requires a `PREROUTING` chain setup similar to `POSTROUTING`).
- Using `iptables`: `sudo iptables -t nat -A PREROUTING -i your_inbound_interface -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:80`
Advantages of NAT
- **IPv4 Address Conservation:** Allows many devices to share a single public IP.
- **Security:** Hides the internal network's IP structure, making direct attacks on internal IPs difficult.
- **Flexibility:** Simplifies network renumbering within the private space.
Disadvantages of NAT
- **Breaks End-to-End Principle:** Prevents external devices from initiating connections directly to internal devices without specific port forwarding rules.
- **Complexity for P2P/Incoming Services:** Can complicate the setup of applications requiring direct incoming connections.
- **Overhead:** Introduces processing overhead on the NAT device (usually negligible on modern hardware).
- **Troubleshooting:** Can sometimes mask network issues.
See also
- IP address
- IPv4
- IPv6
- Router
- Firewall (computing)
- iptables
- nftables
- Private network
- Public IP address
- Port forwarding