redirecting_packets.md 786 B


title: Redirecting Packets categories: [cheatsheets]

tags: [networking]

Redirecting Packets

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

Redirecting incoming packets to another Interface/Port

  1. 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
    
  2. Masquerade with iptables

    iptables -t nat -A POSTROUTING -j MASQUERADE