Linux setup default gateway with route command

by Vivek Gite on August 2, 2006 · 23 comments

Q. How do I setup default gateway with a route command?

A. route command show and/or manipulate the IP routing table under Linux and UNIX oses.

Route 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

$ /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 destionation entry 0.0.0.0 (or default) is the default gatway. In above example 191.255.255.1 is a default gatway.

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 eth0OR use hostname such as dsl-router:# route add default gw dsl-router eth0

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 23 comments… read them below or add one }

1 John Olivares March 6, 2008

Great site thanks

Reply

2 Veeru November 16, 2011

Good one! Very usefull info

Reply

3 Ken April 15, 2008

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.

Reply

4 m@m August 27, 2008

usefull commands..

Thanks.
m@m

Reply

5 Dan smooth operator October 1, 2008

Short, distinct and very good!

Reply

6 Forum January 22, 2009

It was really useful!!

Reply

7 velayutham February 3, 2009

It was usefull at the urgent time

Reply

8 winifredO February 25, 2009

so inetresting and useful! thanks

Reply

9 robert April 13, 2009

Thanks for these hints.

Reply

10 NannY May 5, 2009

Thanks so much
Very Helpful!!
Cheers,

Reply

11 carlos guerrero May 14, 2009

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

Reply

12 mc.god July 16, 2009

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

Reply

13 Al September 20, 2009

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

Reply

14 admtsh December 30, 2009

Thank you!
very nice examples!

Reply

15 Quek April 1, 2010

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

Reply

16 sundaram August 6, 2010

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?

Reply

17 Chris Happel September 13, 2010

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?

Reply

18 niks December 28, 2010

useful commands…..thnks a ton

Reply

19 Alessandro April 21, 2011

great tutorial … very usefull… thanks…

Reply

20 wri7913 June 28, 2011

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.

Reply

21 stevo November 4, 2011

Cool stuff guys…precise and straight

Reply

22 Jose Tapia November 13, 2011

Very good tutorial thank you! you rocks buddy :D

Reply

23 Christian November 14, 2011

Me a gustado mucho, gracias.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 3 + 4 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: