Forward all incoming packets through a certain port
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 PREROUTING -p tcp -i eth0 --dport 60050 -j DNAT --to-destination 10.0.3.50:5044 sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Show all the list in the iptables
sudo iptables -S
or
sudo iptables -t nat -L --line-numbers
Delete all existing rules
sudo iptables -F
Save the existing iptable settings
/etc/init.d/iptables save
To restore the settings
/etc/init.d/iptables start