Welcome! In this tutorial, I’ll explain how to forward TCP/UDP traffic to a new server. This can especially be useful when you’re migrating your Vultr VPS to a new location. This tutorial covers both 32 and 64 bit versions of CentOS 6.
Before we can start, IPTables must be installed. Install IPTables with the following command.
yum install iptables -y
Packet forwarding must be enabled on your server for this method to work. Open the file
/etc/sysctl.conf. Make sure the following line is enabled, and set to “1”:
net.ipv4.ip_forward = 1
Save the file, then run:
Perform the commands below accordingly to your needs. Change “184.108.40.206” to your new server’s IP address and “venet0” to your Ethernet adapter. Also, change “80:90” to the desired port range that will be forwarded.
iptables -A FORWARD -d 220.127.116.11 -i venet0 -p tcp -m tcp --dport 80:90 -j ACCEPT
If you want to forward a single port, simply replace the port range above with a single port. To forward UDP instead, replace instances of “tcp” above with “udp”.
Add routes for your newly added rules. Rules refer to the directives inside of IPTables. In this example, “18.104.22.168” represents your old server’s IP address and like before, “22.214.171.124” represents the destination/new server’s IP. Again, update the port range and TCP/UDP strings as needed.
iptables -t nat -A PREROUTING -d 126.96.36.199 -p tcp -m tcp --dport 80:90 -j DNAT --to-destination 188.8.131.52
Finalize the NAT forwarding. Change “venet0” to your Ethernet adapter.
iptables -t nat -A POSTROUTING -o venet0 -j MASQUERADE
Save our newly added rules and soft-restart IPTables.
service iptables save service iptables reload
Congratulations! You have now successfully configured IPTables to forward traffic to your new server.