Tuesday, August 17, 2010

Using a Linux machine as a router

Leave a Comment
Here are simple commands to make a Linux machine as router.

1. Assuming eth1 is private network and eth0 is connected to public network (internet)
2. Assuming you want to access internet from prviate network connected to eth1.
3. On all machines in private network, set IP address of eth1 as default gatteway in network settings on internal machines
4. Run following 4 commands on Linux machine

echo 1 > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
/sbin/iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
/sbin/iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

5. To save the settings to become persistent across reboots, run command
/sbin/iptables-save > /etc/sysconfig/iptables

Please note: above instructions are tested on Fedora 9. Please change paths or specific commands for other Linux distributions

0 comments: