Skip to end of metadata
Go to start of metadata

In this example all incoming traffic on port 80 redirect to port 8123

iptables -t nat -I PREROUTING --src 0/0 --dst 192.168.1.5 -p tcp --dport 80 -j REDIRECT --to-ports 8123

Below is a case to forward all the incoming packets to two different local mysql servers

sudo iptables -F
sudo iptables -t nat -F
sudo iptables -t mangle -F
sudo iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 63001 -j DNAT --to-destination 10.0.3.10:3306
sudo iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 63101 -j DNAT --to-destination 10.0.3.20:3306
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Below is a case to forward all the incoming packets to two different local mysql servers as well as Redis

sudo iptables -F
sudo iptables -t nat -F
sudo iptables -t mangle -F
sudo iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 63001 -j DNAT --to-destination 10.0.3.10:3306
sudo iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 64001 -j DNAT --to-destination 10.0.3.10:6379
sudo iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 63101 -j DNAT --to-destination 10.0.3.20:3306
sudo iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 64101 -j DNAT --to-destination 10.0.3.20:6379
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Show all the list in the iptables

sudo iptables -S

Delete all existing rules

sudo iptables -F