Home > Linux > How to setup Linux OS as router

How to setup Linux OS as router


There are a few ways to set up a Linux machine as route. Here is a relatively straight forward and common method. This method requires that the system use iptables for Network Address Translation (NAT).

Assuming eth0=WAN and eth1=LAN

This step by step small howto will help you to setup Linux router only in 2 minutes.

Configuration Step

1) Enable packet forwarding
2) Setup Network Address Translation using IPTABLES MASQUERADE targets

Step#1: Turn on IP forwarding in kernel

Open linux kernel configuration file (you must be a root user or use su – command to become a root user):
# vi /etc/sysctl.conf

Add/modify following line:
net.ipv4.ip_forward = 1

Step#2: Restart network

# service network restart

Step#3: Setup IP forwarding and Masquerading (to act as router), you need to use NAT option of iptables as follows (add following rules to your iptables shell script) :
# iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
# iptables --append FORWARD --in-interface eth1 -j ACCEPT

Step#4: Point all desktop client to your eth1 IP address as Router/Gateway. Or use DHCP to distribute this information (recommended)

Step#5: Put code described in Step#3 to script and call it from /etc/rc.local file.

Advertisements
Categories: Linux Tags: , , , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: