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

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
# dig

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.

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
30 comments… add one
  • Mike Dec 6, 2013 @ 18:40

    Eth1 must have a gateway thru Eth0.

  • Avin Mar 26, 2012 @ 6:41

    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 ?

  • Tris Mar 26, 2012 @ 5:10

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

  • sen Feb 22, 2012 @ 7:19

    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 ?


  • marcuz Feb 4, 2012 @ 1:13

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

  • muthu May 19, 2011 @ 10:10

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

  • chinni Nov 8, 2009 @ 18:33

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

  • bava Oct 24, 2009 @ 8:00

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

  • Avin Sep 18, 2009 @ 13:05

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

    Avin Tokade

  • Sara Sep 6, 2009 @ 20:13

    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.

  • Saidur Morsalin Aug 26, 2009 @ 13:11

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

  • Mustapha Jun 18, 2009 @ 13:59

    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.

  • MIdhun Jun 4, 2009 @ 13:40

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

  • MIdhun Jun 4, 2009 @ 13:38

    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

  • simon Nov 4, 2008 @ 9:18

    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 #

  • Loke Sep 15, 2008 @ 12:23

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

  • Chris Mar 19, 2008 @ 3:43

    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.

  • John Romo Oct 14, 2007 @ 4:21

    Will it work with a , it’s a dedicated line?

  • Nandkishor Sep 14, 2007 @ 8:41

    But what Gatway is used for eth1.

    Cheer’s ,

    • Mike Dec 6, 2013 @ 18:43

      Use Eth 0 gateway.

  • Pawan Kinger Sep 4, 2007 @ 14:37

    1. Those having problems like “…can ping the IP address but can’t ping the site…” you can use the DNS server 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.

  • sunil Aug 29, 2007 @ 11:17

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

  • kumar Aug 18, 2007 @ 3:41

    How to do this if we are running windows?

  • Michael D. Brogada Jul 27, 2007 @ 2:57

    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!

  • 🐧 nixCraft Jul 2, 2007 @ 14:51

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



  • irfan Jul 2, 2007 @ 13:55

    good tutorial,but i have a problem,i can ping IP of, but i can`t ping

  • abhay naik May 8, 2007 @ 15:34

    thanks for solution.

  • Nandkishor Mar 29, 2007 @ 7:22

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

    • niti Oct 5, 2012 @ 10:25

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

  • Anonymous Oct 16, 2005 @ 18:11

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @