How to setup Linux as a router for DSL, T1 line etc

by on November 23, 2004 · 30 comments· LAST UPDATED November 29, 2007

in , ,

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).

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

Configuration steps

=> First enable packet forwarding
=> Next setup Network Address Translation using IPTABLES MASQUERADE targets
=> Save the changes
=> Verify everything is working

I'm assuming that your setup is as follows:
A) You are using any Linux distro

B) eth0 is internet interface (connected to router for example) and eth1 connected to your internal lan (connected to your HUB/Switch for example).

My Linux   eth0  --> Internet
box       eth1  --> Lan

Step # 1 Turn on ip forwarding in kernel

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

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

Step # 2 Restart network
# /etc/init.d/network restartOR# 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 You are done! Test it with ping or dig:
# ping your-isp.com
# dig yahoo.com

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

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 30 comments… read them below or add one }

1 Anonymous October 16, 2005 at 6:11 pm

very useful, you got really good stuff. Keep it up.

Reply

2 Nandkishor March 29, 2007 at 7:22 am

Hi ,
I put this rules, My client m/c able to ping to my internet router but not ping to any internet site like yahoo.com or google.com.
Give me solution for this problem.

Reply

3 niti October 5, 2012 at 10:25 am

abe configuration to daal …..tabhi to solution milega na…

Reply

4 abhay naik May 8, 2007 at 3:34 pm

thanks for solution.

Reply

5 irfan July 2, 2007 at 1:55 pm

good tutorial,but i have a problem,i can ping IP of http://www.google.com, but i can`t ping http://www.google.com

Reply

6 nixCraft July 2, 2007 at 2:51 pm

Set up DNS name server by editing /etc/resolv.conf file:
vi /etc/resolv.conf
Modify/set as follows:

nameserver 208.67.222.222
nameserver 208.67.220.220

HTH

Reply

7 Michael D. Brogada July 27, 2007 at 2:57 am

using a client pc (xp box) i can ping my linux box 2 lan cards but i can’t connect to internet

help pls. ;) thanks!

Reply

8 kumar August 18, 2007 at 3:41 am

How to do this if we are running windows?

Reply

9 sunil August 29, 2007 at 11:17 am

I do the above steps but cannot get internet from client machnies working in windows

Reply

10 Pawan Kinger September 4, 2007 at 2:37 pm

1. Those having problems like “…can ping the IP address but can’t ping the site…” you can use the DNS server 4.2.2.2 in /etc/resolv.conf(open as root)
2. those who can’t even ping the IP address ensure you have the right gateway specified in your network setttings.

Reply

11 Nandkishor September 14, 2007 at 8:41 am

Hi,
But what Gatway is used for eth1.

Cheer’s ,
Nandkishor

Reply

12 Mike December 6, 2013 at 6:43 pm

Use Eth 0 gateway.

Reply

13 John Romo October 14, 2007 at 4:21 am
14 Chris March 19, 2008 at 3:43 am

I would be more than happy to pay you to make my headache go away!!!

:)

I’m so frustrated with routing and switching I could scream. I can’t ping any LAN computer from the router box.

Reply

15 Loke September 15, 2008 at 12:23 pm

My DSL modem is having static IP, example 210.127.9.22
how to configure eth0 and eth1?

Reply

16 simon November 4, 2008 at 9:18 am

Guz Job well done on this tutorial.Am wondering whether when you are MASQUERADING AND FORWARDING you need to retain the comments on your script as shown that is

——————————————-
# iptables –table nat –append POSTROUTING –out-interface eth0 -j MASQUERADE
# iptables –append FORWARD –in-interface eth1 -j ACCEPT
———————————————-

The comment symbol #
cheers
simo

Reply

17 MIdhun June 4, 2009 at 1:38 pm

i can ping the linux server from clients. but can’t connect to internet from clients. in my network ETH1 is internet & ETHO is the link to client.. i am using debian etch. please help

Reply

18 MIdhun June 4, 2009 at 1:40 pm

i interchanged the ETH0 ETH1 during step3. still same problem …………….

Reply

19 Mustapha June 18, 2009 at 1:59 pm

Hello
I got everything working on CentOS 5.3. However iptables rules will not get saved unless you do # /etc/init.d/iptables save

This will make your settings survive a reboot.

* DNS is not working even when specifying it in /etc/resolv.conf

I had to install bind on the router in cash-only mode.

Reply

20 Saidur Morsalin August 26, 2009 at 1:11 pm

Hi ,
My client m/c able to ping to my internet router but not ping to any internet site like yahoo.com or google.com.
Give me solution for this problem.

Reply

21 Sara September 6, 2009 at 8:13 pm

Dears,
I have bought D-Link 2520U ADSL modem,
my laptop model is SONY VGN-CR140E,
and I use ubuntu 9.04, but I can not set up this modem on my laptop by LAN port. In fact I am beginner.
Please help me to solve this problem.

Thanks a lot.

Reply

22 Avin September 18, 2009 at 1:05 pm

Hi,
Thanks for good tutorial, but I am confused about what is gateway address for eth1. Plz help

Avin Tokade

Reply

23 bava October 24, 2009 at 8:00 am

pls, inform step by step, setup linux router configuration steps

Reply

24 chinni November 8, 2009 at 6:33 pm

pls, inform step by step, setup linux router configuration steps
and of the material about the configuration of linux machine as router

Reply

25 muthu May 19, 2011 at 10:10 am

i am able to ping to my gateway of router but not ping to any internet site like yahoo.com or google.com.

Reply

26 marcuz February 4, 2012 at 1:13 am

thanks man. this is really good tutorial. I set up my Linux Router in just about minute after reading your post.

Reply

27 sen February 22, 2012 at 7:19 am

Hi Vivek,

Great tutorial.
I have configured a Linux router with DHCP server and it is working great.

When I blocked an IP using the following rule on Server, I am not able to ping the IP from the linux router however this IPs are pinging from other systems on that LAN

iptables -A OUTPUT -s -j DROP
iptables -A INPUT -d -j DROP

I am not sure why this is happening , any input on this will be very helpful.
Also if you have any suggestion on an easy firewall on top of iptables on the Linux router ?

Thanks
Seena

Reply

28 Tris March 26, 2012 at 5:10 am

Thank you !!! You really save my ass out. Been struggling for a whole day just try to ping google from client and now it works. :-x…..

Reply

29 Avin March 26, 2012 at 6:41 am

I have configured my Linux box as Router and I also used Squid (transparent mode) to improve our network bandwidth. Now I want to in my network client must pass my authentication (Username/ Password) but I found Squid Transparent mode not support authentication.

Is there anyway to implement authentication for client ? Any third-party utility ?

Reply

30 Mike December 6, 2013 at 6:40 pm

Eth1 must have a gateway thru Eth0.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , ,

Previous post:

Next post: