Search our database

CentOS (and other Linux machines) to Act as a Router

By default, if you have two or more network interfaces in CentOS, they do not know each other until you introduce and connect them together.
 
To connect two network interfaces to forward packets back and forth, here are the two commands to use:
 
Step 1:
Open up the Linux terminal.
 
Step 2:
If you have a STATIC IP address on the network interface that connects to the Internet/External network, use the following command:
iptables -t nat -A POSTROUTING -s <internal-network-address>/<mask> -j SNAT –to-source <interface-address-that-connects-to-internet-or-external-network>
 
For example,
iptables -t nat -A POSTROUTING -s 192.168.102.0/24 -j SNAT –to-source 192.168.101.142
 
If you are NOT using a static IP address on the network interface that connects to the Internet/External network, use this command:
iptables -t nat -A POSTROUTING -s <internal-network-address>/<mask> -j MASQUERADE
 
For example,
iptables -t nat -A POSTROUTING -s 192.168.102.0/24 -j MASQUERADE
 
Step 3:
The next command is to save the iptables rule so that when you reboot next time, the iptable rule will still applies.
To save the iptables rule:
iptables-saves
 
Lastly, try to test ping from the internal network to the interface address that connects to the internet or external network. You should see ping replying from that interface address.