Linux setup default gateway with route command

How do I setup default gateway with a route command on a Linux operating systems?

You can use the route command to show and/or manipulate the IP routing table under a Linux and UNIX-like operating systems. Modern Linux based operating system recommend the ip command to set the IP routing table.

ADVERTISEMENTS

[donotprint]
Tutorial details
DifficultyEasy (rss)
Root privilegesYes
RequirementsNone
Time5m
[/donotprint]The route command manipulates the kernel’s IP routing tables. Its primary use is to set up static routes to specific hosts or networks via an interface after it has been configured with the ifconfig program. When the add or del options are used, route modifies the routing tables. Without these options, route displays the current contents of the routing tables.

Display default route

Following three-command display the current routing table:
# route
Output:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 ra0
default         dsl-router      0.0.0.0         UG    0      0        0 ra0

OR
$ /sbin/route
Output:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
191.255.255.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
default         191.255.255.1   0.0.0.0         UG    0      0        0 eth0

You can use -n option, to display numerical addresses instead of trying to determine symbolic host names (via dns or /etc/hosts file). This is useful if you are trying to determine why the route to your nameserver has vanished.
$ /sbin/route -nOutput:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
191.255.255.0   0.0.0.0         255.255.255.0   U     0      0        0 venet0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 venet0
0.0.0.0         191.255.255.1   0.0.0.0         UG    0      0        0 venet0

Please note that a destination entry 0.0.0.0 (or default) is the default gateway. In above example 191.255.255.1 is a default gateway.

Add / setup a new route

The syntax is as follows:

route add default gw {IP-ADDRESS} {INTERFACE-NAME}

Where,

  • IP-ADDRESS: Specify router IP address
  • INTERFACE-NAME: Specify interface name such as eth0

For example if your router IP address is 192.168.1.254 type the following command as the root user:
# route add default gw 192.168.1.254 eth0
OR use hostname such as dsl-router:
# route add default gw dsl-router eth0
Or use the ip command (newer syntax) to route all traffic via 192.168.1.254 gateway connected via eth0 network interface:
# ip route add 192.168.1.0/24 dev eth0
OR
# ip route add 192.168.1.0/24 via 192.168.1.254

Setting route using GUI tools/command under Linux

If you find above command hard to use, consider using GUI tools. If your are using Red Hat/CentOS/Fedora core Linux type following command:# redhat-config-networkOR If you are using other Linux distribution use command:# network-admin

See also
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
32 comments… add one
  • John Olivares Mar 6, 2008 @ 20:30

    Great site thanks

    • Veeru Nov 16, 2011 @ 7:37

      Good one! Very usefull info

  • Ken Apr 15, 2008 @ 23:57

    Running BT3 on an eeePC, I’ve been finding that I have to run:

    # route add -net 0.0.0.0 dev ath0

    to get my wireless to connect to the internet.

  • m@m Aug 27, 2008 @ 7:08

    usefull commands..

    Thanks.
    m@m

  • Dan smooth operator Oct 1, 2008 @ 12:42

    Short, distinct and very good!

  • Forum Jan 22, 2009 @ 14:15

    It was really useful!!

  • velayutham Feb 3, 2009 @ 16:25

    It was usefull at the urgent time

  • winifredO Feb 25, 2009 @ 11:24

    so inetresting and useful! thanks

  • robert Apr 13, 2009 @ 5:32

    Thanks for these hints.

  • NannY May 5, 2009 @ 7:15

    Thanks so much
    Very Helpful!!
    Cheers,

  • carlos guerrero May 14, 2009 @ 0:34

    hi very very thanks , for you posted i realy helpme very much!!!!

  • mc.god Jul 16, 2009 @ 20:22

    Very useful hint as a fast reminder, thank you ^^

  • Al Sep 20, 2009 @ 19:29

    great work fellows.good to go,Thank you for tips and tricks

  • admtsh Dec 30, 2009 @ 3:35

    Thank you!
    very nice examples!

  • Quek Apr 1, 2010 @ 5:03

    Thank you very much, is a good and effective guide.

  • sundaram Aug 6, 2010 @ 13:34

    when i used the command, i got the following error:

    # route add default gw 10.102.237.71 eth4
    SIOCADDRT: No such process

    could you please guide me?

    • Chris Happel Sep 13, 2010 @ 14:48

      I was getting that, too. Turns out I was putting in the wrong interface. Use the ‘route’ command without any parameters, and confirm that the 10.102.237.0 network is really attached to interface eth4. My problem now is that the server keeps ‘forgetting’ my default gateway, even though it’s in my /etc/network/interfaces file:

      auto eth0
      iface eth0 inet static
      address 10.10.10.230
      netmask 255.255.255.0
      gateway 10.10.10.254
      network 10.10.10.0
      broadcast 10.10.10.255

      What gives?

      • jojo Oct 8, 2015 @ 5:40

        It may still be pulling that info from another file. Check to make sure if it’s pulling the info from either a copy or original /etc/network/interfaces file.

  • niks Dec 28, 2010 @ 5:29

    useful commands…..thnks a ton

  • Alessandro Apr 21, 2011 @ 15:27

    great tutorial … very usefull… thanks…

  • wri7913 Jun 28, 2011 @ 1:11

    Works on Natty 11.04 on ASUS G73sw laptop. Thanks for script! Ubuntu Devs should have already noticed and fixed this issue. Shame on them.

  • stevo Nov 4, 2011 @ 8:36

    Cool stuff guys…precise and straight

  • Jose Tapia Nov 13, 2011 @ 4:05

    Very good tutorial thank you! you rocks buddy :D

  • Christian Nov 14, 2011 @ 11:15

    Me a gustado mucho, gracias.

  • Sri Apr 17, 2012 @ 7:59

    Thanks a lot, very useful…

    Saved be a lot of time and effort!!

  • Apu Apr 28, 2012 @ 8:15

    Hi All friends

    Im going to Configure a Linux Gateway server for my LAN. It will locate in between router and my LAN.

    I used 192.168.0.x private ip range for my LAN PCs. and im expecting to configure firewall, nat, proxy on that gateway server

    who can guide me to do that.

    Im new for linux and im interesting Linux

    thanks
    Apu

  • krishna Aug 21, 2012 @ 10:31

    hi ..
    i helped me a lot

  • Mighty Oct 15, 2013 @ 5:34

    What is the difference between you example using this command:
    # route add default gw 192.168.1.254 eth0

    And inserting the gateway in the file below?
    # vi /etc/sysconfig/network
    GATEWAY=192.168.1.254

  • bA37 Dec 17, 2013 @ 4:11

    helped me a lot, thanks a lot dude…:)

  • Kapil Awadhwal Apr 24, 2014 @ 11:50

    Thanks for the short & good explaination…

  • AIL Sep 10, 2014 @ 13:54

    Thank you for the info. Worked like a charm on RHEL 5.8 x64 server.
    Question: are changes made with route command persistent? or do they revert on network restart or server restart?

  • Sudhi Oct 29, 2014 @ 9:45

    very useful commands thank you.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.