Hello! In our last lesson, we explored how dynamic routing protocols like OSPF and BGP allow networks to automatically share reachability information. You learned that BGP is the protocol that connects different Autonomous Systems, forming the backbone of the internet.
This raises a critical question: Your home or office network likely uses private IP addresses (like 192.168.x.x), which BGP routers on the internet are configured to ignore. So, how can dozens of devices on your private network all access public websites using a single public IP address from your ISP?
This lesson will answer that exact question. We will cover Network Address Translation (NAT). You will learn to explain the function of NAT and configure basic Source NAT (masquerading) on a Linux router to provide internet access. This is a fundamental skill for any network or systems administrator and a concept you'll need to articulate clearly in technical interviews.
1. The "Why": Solving the IP Address Puzzle
The internet was built on IPv4, which provides about 4.3 billion unique addresses. With the explosion of internet-connected devices, this supply was exhausted long ago. Network Address Translation (NAT) was developed as a clever solution to this problem.
NAT allows an entire private network of computers, each with its own private IP address, to share a single public IP address to communicate with the internet.
To start, let's get a clear, high-level overview of this concept.
NAT Explained - Network Address Translation
This animated video from PowerCert gives a concise explanation of why NAT is necessary and the difference between public and private IP addresses.
Watch the video from the beginning to 03:30. Focus on understanding the core problem (IPv4 exhaustion) and how NAT acts as a translator between the private internal network and the public internet.
As the video explained, the router sits at the edge of your private network and performs this translation. When a device inside your network wants to connect to a website, the router swaps the device's private source IP address for its own public IP address before sending the request to the internet.

2. The "How": Source NAT and Connection Tracking
The specific type of NAT we've been describing is called Source NAT (SNAT), because it modifies the source IP address of the outgoing packets.
But this creates a new puzzle: if multiple internal devices are browsing the web simultaneously, their traffic all appears to come from the same public IP address. When a web server sends a reply back to the router, how does the router know which internal device to forward it to?
The answer is connection tracking. The router maintains a table that tracks every outgoing connection. It doesn't just change the IP address; it also changes the source port number. This combination of the public IP and a unique port number for each connection allows it to direct return traffic to the correct internal host.
Let's watch a detailed visualization of this process.
Mastering Source Network Address Translation (SNAT) in Nftables
This video from LinuxCloudHacks provides an excellent breakdown of the SNAT mechanism, focusing on the problem of return traffic and how connection tracking solves it.
Watch from 00:17 to 06:40. Pay close attention to the explanation of why a public server can't reply directly to a private IP. Then, observe how the firewall (router) replaces the source IP and uses its connection tracking table to manage the return traffic. The concepts are universal, even though the video mentions nftables specifically.
3. The "Practice": Configuring a Linux NAT Gateway
Now, let's put this theory into practice. Your goal is to turn a Linux machine with two network interfaces into a NAT gateway (or router). This is a common task in data center and cloud environments.
We will use the following scenario as our guide:
There are two main steps to configure this on a Linux system.
Step 1: Enable IP Forwarding
By default, a Linux system will not forward packets between its network interfaces. We must explicitly enable this functionality to make it act as a router.
You can do this temporarily with the following command:
sudo echo 1 > /proc/sys/net/ipv4/ip_forward
To make this change permanent across reboots, you need to edit the /etc/sysctl.conf file. Find the line #net.ipv4.ip_forward=1 and uncomment it by removing the #. Then apply the change with sudo sysctl -p.
Step 2: Configure the NAT Rule
For decades, the standard tool for this in Linux has been iptables. A more modern tool, nftables, is now the default in many distributions, but iptables is still incredibly widespread. You should be familiar with both.
We will use a special, dynamic form of SNAT called masquerading. This is ideal because it automatically uses the IP address of the outgoing interface (eth0 in our diagram), even if that IP is assigned dynamically via DHCP.
Using iptables (The Classic Method)
Using Masquerading with Iptables for Network Address Translation ...
This GeeksForGeeks article provides a straightforward guide to setting up masquerading with iptables. We will follow the key steps.
Review 'Step 3: Add Masquerading Rule...' and 'Step 4: Specify Masquerading for a Specific Interface'. Also, look at the explanation of the command flags at the end of the article. This will give you the exact commands and their meaning.
To implement the solution for our diagram, the iptables command is:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Let's break this down:
-t nat: Specifies that we are working with thenattable, which is used for network address translation.-A POSTROUTING: Appends this rule to thePOSTROUTINGchain. This chain processes packets right before they are sent out of a network interface. This is the correct place for SNAT, as routing decisions have already been made.-o eth0: This is the crucial part. It makes the rule match only for packets going out of theeth0interface (our public interface).-j MASQUERADE: This is the target action. It tellsiptablesto perform source NAT, replacing the source IP with the IP of the outgoing interface (eth0).
Using nftables (The Modern Method)
For completeness, here is how you would achieve the same result with the newer nftables tool. The logic is identical, but the syntax is different.
How To Use nftables on Linux Systems
This guide from CommandLinux shows the equivalent NAT setup using nftables. The concept of post-routing and masquerading remains the same.
Find the section 'Network Address Translation Setup' and look at the commands for creating a NAT table and adding a masquerade rule. You'll see the same logic applied with the new nft syntax.
The nftables commands are:
# First, create the necessary table and chain
sudo nft add table inet nat
sudo nft add chain inet nat postrouting '{ type nat hook postrouting priority srcnat; }'
# Then, add the rule
sudo nft add rule inet nat postrouting oifname "eth0" masquerade
With IP forwarding enabled and one of these NAT rules in place, node-01 and node-02 can now successfully communicate with the internet through node-03.
Conclusion
You have just configured one of the most essential services in networking. By enabling IP forwarding and setting up a simple masquerading rule, you can provide internet access to an entire private network using a single Linux machine.
Key Takeaways:
- NAT's Purpose: To conserve IPv4 addresses by allowing multiple devices on a private network to share one public IP address.
- SNAT Mechanism: The router modifies the source IP of outgoing packets to its own public IP. It uses a connection tracking table (mapping source port numbers) to direct return traffic to the correct internal device.
- Masquerading: A dynamic form of SNAT in Linux, perfect for when the public IP of the router can change. It's configured in the
POSTROUTINGchain. - Linux Configuration: The process involves two steps:
- Enabling IP forwarding (
/proc/sys/net/ipv4/ip_forward). - Adding a rule using
iptablesornftablesto masquerade traffic leaving the public interface.
- Enabling IP forwarding (
In our next lesson, we will shift from routing (Layer 3) back to the foundational technologies of local area networks (Layer 2). We'll investigate how network switches prevent catastrophic network loops using the Spanning Tree Protocol (STP), ensuring the reliability of the local networks that NAT gateways connect to the internet.