What is use case of OUTPUT DNAT in iptables?

In iptables there are PREROUTING, INPUT, FORWARD, OUTPUT and POSTROUTING chains. Mostly people do DNAT on PREROUTING. In case of PREROUTING packet comes in firewall before routing decision.

iptables OUTPUT DNAT allows us to redirect outgoing packet from internal process from machine. While in PREROUTING use for before any decision on routing.

Command is

iptables -t nat -A OUTPUT -p tcp –dport 21 -d cm-in-f138.1e100.net -j DNAT –to-destination 127.0.0.1:21

This command redirect ftp cm-in-f138.1e100.net to localhost ftp server.

Example output

root@localhost ~]# ftp cm-in-f113.1e100.net
Trying 142.251.106.113…
Connected to cm-in-f113.1e100.net (142.251.106.113).
220 (vsFTPd 3.0.5)
Name (cm-in-f113.1e100.net:root): test
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.

Above command connect to ftp server running on localhost

Leave a Reply