<aside> 💡
In this section, we’ll discuss the purpose of Destination NAT (DNAT) and observe how DNAT affects our packets through a ****small experiment.
</aside>
In the previous chapter (09. From Container to Internet: A Deep Dive into Source NAT ), we discussed how, when a network packet travels from a container through the host to the external network, Source NAT usually modifies the packet’s source address (src).
Now, when the network packet returns from the external network back to the host, how does the system know which container it should be delivered to?
Destination NAT (DNAT) refers to modifying the destination IP or port of a packet when it enters the internal network (inbound traffic), allowing external requests to be directed to an internal host or service.
In other words, its purpose is:
“To route external requests to a specific internal service.”
This mechanism is also known as Port Forwarding or Reverse-proxy-style NAT.
| Item | DNAT | SNAT |
|---|---|---|
| Modified Fields | Destination IP / Port | Source IP / Port |
| Traffic Direction | Inbound | Outbound |
| Typical Use Case | Direct external packets to internal services | Allow internal hosts to communicate with the outside world |
| Common Chains | PREROUTING, OUTPUT |
POSTROUTING, OUTPUT |
| Timing | Before routing decision (destination needs to be changed) | Before packet is sent out (source needs to be changed) |

From the diagram on the left, we can see that Destination NAT is located in the PREROUTING and OUTPUT chains.
A closer inspection of the Netfilter hooks in Linux Kernel 6.16 also confirms this.
DNAT in Linux Kernel<aside> 💡
Question:
Why does DNAT need to register hooks in both the PREROUTING and OUTPUT chains?
</aside>
PREROUTING for 『Forwarding-Type』 DNAT
This is the most common scenario:
$ iptables -t nat -A PREROUTING -d 203.0.113.5 -p tcp --dport 8080 -j DNAT --to-destination 10.0.0.5:80
📦 The traffic originates from an external source — the packet enters the network interface and then goes into NF_INET_PRE_ROUTING.
At this stage, DNAT intervenes in the PREROUTING chain to modify the destination IP to 10.0.0.5.
After that, the kernel’s routing subsystem uses the new destination IP to route the packet to the correct internal host.
✅ Applicable scenario: The packet comes from an external source, and the system needs to forward it to an internal host.
OUTPUT for 『Locally Generated』 DNAT
The packet is generated by a local process, and you want to change its destination before it leaves the host.
In this case, the packet does not pass through PREROUTING (since it’s not “coming from outside”),
so DNAT must be attached to the NF_INET_LOCAL_OUT hook instead.
$ iptables -t nat -A OUTPUT -d 127.0.0.1 -p tcp --dport 8080 -j DNAT --to-destination 127.0.0.1:80
With this rule, when you run curl http://127.0.0.1:8080, the request is actually redirected to 127.0.0.1:80.
✅ Applicable scenario: Packets generated by local applications also need to be redirected (loopback or internal redirection).
In the upcoming experiment, we will need to perform a communication test between two machines.