Table of Contents
Was this article helpful?
Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $600 by adding new articles.

Forwarding Traffic with IPTables on CentOS 6

Last Updated: Sun, Sep 6, 2015
CentOS Linux Guides Networking
Archived content

This article is outdated and may not work correctly for current operating systems or software.


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.

Install IPTables

Before we can start, IPTables must be installed. Install IPTables with the following command.

yum install iptables -y

Enable routing

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:

sysctl -p

Configure IPTables

Step One

Perform the commands below accordingly to your needs. Change "" 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 -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".

Step Two

Add routes for your newly added rules. Rules refer to the directives inside of IPTables. In this example, "" represents your old server's IP address and like before, "" represents the destination/new server's IP. Again, update the port range and TCP/UDP strings as needed.

iptables -t nat -A PREROUTING -d -p tcp -m tcp --dport 80:90 -j DNAT --to-destination

Step Three

Finalize the NAT forwarding. Change "venet0" to your Ethernet adapter.

iptables -t nat -A POSTROUTING -o venet0 -j MASQUERADE

Step Four

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.

Want to contribute?

You could earn up to $600 by adding new articles.