Debian / Ubuntu Linux Setting a Default Gateway

Q. How do I setup a default gateway on Debian or Ubuntu Linux? My router IP is and I’d like to setup this as a default gateway for Debian Linux.

A. You can use command line tool such as
a] ip command – show / manipulate routing, devices, policy routing and tunnels

b] route command – show / manipulate the IP routing table

c] Save routing information to a configuration file so that after reboot you get same default gateway.

ip command to set a default router to

Login as the root and type:
# ip route add default via
$ sudo ip route add default via

route command to set a default router to

Login as the root and type:
# route add default gw
$ sudo route add default gw

Save routing information to a configuration file /etc/network/interfaces

Open /etc/network/interfaces file
# vi /etc/network/interfaces
$ sudo vi /etc/network/interfaces
Find eth0 or desired network interface and add following option
Save and close the file. Restart networking:
# /etc/init.d/networking restart
$ sudo /etc/init.d/networking restart

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 24 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig 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
24 comments… add one
  • Rajesh Feb 28, 2008 @ 17:31

    I also want to set my defualt DNS server and proxy settings, just like in windows. How do I do that ??

    • Alec May 20, 2013 @ 15:25


      In Ubuntu, you can either add the ‘nameserver’ lines in /etc/resolv.conf like “shutty” shows, or use the recommended method in Ubuntu Precise (12.04 LTS) by adding “dns-nameservers ” and “dns-search ” in /etc/network/interfaces, like so:

      # The primary network interface
      auto eth0
      iface eth0 inet static
      # dns-* options are implemented by the resolvconf package, if installed

      dns-search lab.internal

  • shutty Jul 16, 2008 @ 13:01


    for dns, edit file /etc/resolv.conf and add lines
    (where and are your nameservers)

    for proxy, use your browser configuration

  • Petrus Sep 2, 2008 @ 10:09

    How do I add a second gateway on higher metric incase the first goes down?

  • 🐧 nixCraft Sep 2, 2008 @ 10:46


    You need to install routed and configure /etc/gateway. Another recommended option is install zebra / Quagga server and configure it. Both allows you to define and configure various routing parameters including metric.

  • Mauro Oct 18, 2008 @ 10:05

    How can I get (in a variable) the current default gateway? Is there a sort of “default gateway enviroment variable” that I can read?

  • 🐧 nixCraft Oct 18, 2008 @ 12:01

    Run following to display default gateway:
    ip route show | grep default | awk '{ print $3}'
    Use following to store gateway IP in a variable called GW:
    GW=$(ip route show | grep default | awk '{ print $3}')
    echo $GW

  • Mauro Oct 18, 2008 @ 17:26

    vivek, many many many thanks. It works!

  • adam Oct 30, 2008 @ 6:22

    For two interfaces that both do dhcp the metrics on the default gateway can be added like this.

    auto eth1
    iface eth1 inet dhcp
    metric 10

    auto eth2
    iface eth2 inet dhcp
    metric 20

  • Kuldeep Yadav Mar 6, 2009 @ 14:30

    Is there any way,so that we can give proxy addresss frm command line apart from browser.because if we give this from command line,it will be applicable to all system.

  • Peter Holmes Sep 1, 2009 @ 11:30

    I have two interfaces, eth0 & eth1. eth0 fixed IP hardwired to test machine with fixed IP, (not easily altered).
    I want eth1 to connect to internet via a DHCP Server Router. Without eth1 enabled there is no problem but as soon as I enable eth1 as DHCP client I lose any access to
    Have spent many hours altering the route(s), etc., with no success.
    Do I have to use ipchains or can it be achieved with route?

  • catalin Sep 16, 2009 @ 9:42

    I am having some routing problems with Ubuntu 9.04:
    Whenever I start up my system, the Internet won’t work unless I do:
    "sudo route add default gw"
    I tried to alter /etc/network/interfaces, but with no luck. After restart, it doesn’t work. executing route prints:

    catalin@lolinux:~$ route
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface * U 0 0 0 wlan0

    After I manually add the gateway, everything is fine, and route prints another gateway:

    catalin@lolinux:~$ route
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface * U 0 0 0 wlan0
    default dsldevice.lan UG 0 0 0 wlan0

    Is there any difference from what I altered and the kernel routing table? If there is, how do I modify that? Or would a startup script be a better solution?

  • Stuart Smith Oct 13, 2009 @ 14:30

    I have the “gateway” in my network file but I’m still required to manually set the route after reboot. This behavior began only after I “bonded” my two NIC’s. The routing table before the manual route command shows
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface U 0 0 0 bond0 U 1 0 0 eth1 U 1 0 0 eth0 U 1000 0 0 eth0 UG 0 0 0 eth1 UG 100 0 0 bond0

    and after adds this one line just below the 169.254… line UG 0 0 0 bond0

    Am I missing something or am I experiencing a true bug?

    I’m using Kubuntu 9.04 Jaunty.

  • joshlondon Nov 24, 2009 @ 1:39

    I am having the same problem..I have to manually set my default gateway after each boot up even though all my network settings include it. How do I get it to stick?

  • Stuart Smith Nov 26, 2009 @ 2:33

    I eventually removed Network Manager. Set everything up manually and it works fine.

  • Josh Nov 26, 2009 @ 4:04

    I just switched to static ip’s and set everything through perfectly. Also I used the network admin tool and I believe that screwed up lots of things.

  • Daniel Smith Mar 20, 2010 @ 12:07

    How can I find Gateway server !? to add the numbers to the address code!?

  • Mahmoud Ibrahim Feb 20, 2011 @ 13:49

    How can i use 2 gateway , I have a complex network using 2 ISP and Linux as a cache and firewall and a router.

    i WANT lINUX router to be able to use both ISP in same time and could nat global IP.

  • Wael Hawa Jul 25, 2011 @ 20:55

    I am using my nanostation 5 as a router… but I can’t receive ping from the ISP gateway… even though I have my ip in the same range. I found this site but the GW isn’t adding… Here are my tables

    Destination Gateway Genmask Flags Metric Ref Use Iface * UH 0 0 0 ppp0 * U 0 0 0 eth0 * U 0 0 0 eth0
    default * U 0 0 0 ppp0

    My ip range is… I need to add as GW

    • Ajet May 1, 2014 @ 18:07

      youtr ip 169.254 its not corect. Reset your router

  • TooMeeK Mar 20, 2012 @ 10:27

    funny thing here! very old Debian distro, I’m trying to add new default gateway via.. tun interface (VPN), but it’s refusing to add anything :D

    ip route dev tun0 proto kernel scope link src via dev tun0 dev eth1 proto kernel scope link src dev eth0 proto kernel scope link src
    default via dev eth0

    PING ( 56(84) bytes of data.
    64 bytes from icmp_seq=1 ttl=64 time=0.726 ms
    64 bytes from icmp_seq=2 ttl=64 time=0.544 ms
    64 bytes from icmp_seq=3 ttl=64 time=1.60 ms
    64 bytes from icmp_seq=4 ttl=64 time=1.58 ms

    sudo ip route add default via
    RTNETLINK answers: No such process

    sudo route add default gw
    SIOCADDRT: Nie ma takiego procesu

  • TooMeeK Mar 20, 2012 @ 11:01

    solved, I’ve used as gateway which is IP alias of VPN

  • Michael Apr 14, 2012 @ 18:11

    add the following to the bottom of your /etc/network/interfaces file

    # Static route
    up route add -net netmask dev ethX

  • Steve Storniak Sep 16, 2015 @ 16:49

    I use the following commands to add a new gateway and remove an existing one :

    sudo route add default gw eth1
    sudo route delete default gw eth0

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum