The Short Answer
Private addresses such as 10.10.0.20 are not routed on the internet (see public vs private IP). A NAT gateway stands between the private network and the internet. On the way out it replaces the private source address with its public one; it remembers each connection in a table; and when the reply comes back, it puts the private address back in and delivers it.
Source NAT and Masquerading
Source NAT (SNAT) changes the source address of outgoing traffic to a fixed public address. Masquerading is the same, except that the gateway uses whatever address its outgoing interface has at the moment, which suits interfaces whose address can change. Because many internal machines share one address, the gateway also changes source ports when needed, so it can tell the connections apart.
Destination NAT and Port Forwarding
Destination NAT (DNAT) changes the destination of incoming traffic. Port forwarding is its everyday form: a connection to port 8080 on the gateway's public address is sent to port 80 on an internal machine. Without such a rule, nothing from the internet can open a connection to a machine behind NAT.
Where a Server Meets NAT
- Private-network gateways: servers that have only a private address reach the internet through a gateway server that masquerades their traffic.
- Containers: Docker gives containers private addresses, masquerades their outgoing traffic, and adds a NAT rule for each port published with
-p. See how to install Docker on Ubuntu. - 1:1 NAT: on some platforms the server's interface has a private address and the platform maps a public address to it.
- Home and office connections: your own computer is almost always behind NAT, sometimes twice when the internet provider uses carrier-grade NAT.
An nftables Example
Before changing firewall or NAT rules on a remote server, keep your SSH session open, have the console in your provider's control panel at hand, and save the current rules with sudo nft list ruleset > ~/ruleset-backup.nft. Loading a rule set can cut your SSH session if it drops your traffic, and a file that starts with flush ruleset removes every existing rule, those of ufw, firewalld, and Docker included, so use it only in a file that contains your complete firewall.
A gateway with public interface eth0 and private network 10.10.0.0/24:
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iifname "eth0" tcp dport 8080 dnat to 10.10.0.20:80
}
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
oifname "eth0" ip saddr 10.10.0.0/24 masquerade
}
}
The priorities dstnat and srcnat are the standard names for -100 and 100, the usual places for NAT chains. Check the file with sudo nft -c -f nat.nft, which tests it without applying anything, then load it with sudo nft -f nat.nft. The gateway must also forward packets: set net.ipv4.ip_forward = 1 in a file under /etc/sysctl.d/ and apply it with sudo sysctl --system. If your firewall has a forward chain with a drop policy, allow the forwarded traffic there too.
On Your Server: See NAT in Action
ip -4 a
sysctl net.ipv4.ip_forward
sudo nft list ruleset
sudo conntrack -L
- Different local and outside address: if the address on the interface differs from the one the internet sees, checked as shown in public vs private IP, your server is behind NAT.
ip_forward = 1: the machine forwards packets, as a gateway or container host does.- Ruleset: look for
masquerade,snat, anddnatlines; on a Docker host you will see the rules Docker added. - conntrack: lists the connections the kernel is tracking, with original and translated addresses. It is in the
conntrackpackage on Ubuntu and Debian and inconntrack-toolson AlmaLinux and Rocky Linux.
What NAT Breaks
- Inbound connections: they need a forwarding rule for every service.
- Protocols that carry addresses inside: active FTP and SIP need helpers or extra setup.
- Logs: a server behind someone else's NAT sees many users as one address, which matters for rate limits and bans.
- Connection table limits: a busy gateway can fill its tracking table; the kernel then logs that the table is full and drops packets. Check with dmesg.
Why Servers Usually Get Public Addresses
A server's job is to accept connections, which NAT makes harder. That is why servers normally have a public address on the interface, and NAT stays at the edges: gateways for private networks, containers, and home connections. With IPv6 NAT is not needed at all; see IPv4 vs IPv6.