Forward incoming traffic to different server

# stop iptables
/etc/init.d/iptables stop
# remove all rules
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
# enable ip forwarding
sysctl net.ipv4.ip_forward=1
# add a rule telling to forward the incoming traffic on port to ip 2.2.2.2:
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 2.2.2.2:80
iptables -t nat -A PREROUTING -p tcp --dport 110 -j DNAT --to-destination 2.2.2.2:110
iptables -t nat -A PREROUTING -p tcp --dport 25 -j DNAT --to-destination 2.2.2.2:25
iptables -t nat -A PREROUTING -p tcp --dport 143 -j DNAT --to-destination 2.2.2.2:143
iptables -t nat -A PREROUTING -p tcp --dport 465 -j DNAT --to-destination 2.2.2.2:465
iptables -t nat -A PREROUTING -p tcp --dport 585 -j DNAT --to-destination 2.2.2.2:585
iptables -t nat -A PREROUTING -p tcp --dport 993 -j DNAT --to-destination 2.2.2.2:993
iptables -t nat -A PREROUTING -p tcp --dport 995 -j DNAT --to-destination 2.2.2.2:995
iptables -t nat -A PREROUTING -p tcp --dport 21 -j DNAT --to-destination 2.2.2.2:21
iptables -t nat -A PREROUTING -p tcp --dport 20 -j DNAT --to-destination 2.2.2.2:20
# tell iptables to masquerade:
iptables -t nat -A POSTROUTING -j MASQUERADE
Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp