title: Redirecting Packets categories: [cheatsheets]
To forward packets on the way back to the sender, after redirecting them, the following command needs to be issued:
echo "1" > /proc/sys/net/ipv4/ip_forward
Use iptables to redirect the packets:
iptables -t nat -A PREROUTING -p tcp --dport 1111 -j DNAT --to-destination 2.2.2.2:111
iptables -t nat -A PREROUTING -s 192.168.1.1 -p tcp --dport 1111 -j DNAT --to-destination 2.2.2.2:111
iptables -t nat -A PREROUTING -s 192.168.1.1/24 -p tcp --dport 1111 -j DNAT --to-destination 2.2.2.2:111
Masquerade with iptables
iptables -t nat -A POSTROUTING -j MASQUERADE