Linux setup default gateway with route command

Posted on in Categories , , , , , last updated August 11, 2015

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.

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

32 comment

    1. 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?

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

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

Leave a Comment