Network routes in Linux

Learn networking basics in the Linux server. What is the network route, how to check routes, how to define static and default routes in the Linux system?

Network routes in Linux

Network routes on Linux servers are the paths or gateways packets should follow to reach their destinations. Routes can be configured on interface level and system level. The default route is also known as default gateway is the IP where the packet should discover their path if the current network or interface (from which packet originated on the server) does not know the path for destination IP. This article is about discussing network routes in Linux. It will cover how to define the network route for the network interface, where and how to define the default route or default gateway in Linux etc.

How to check current routes on system?

Current routes on system can be viewed using below commands.

# ip route
default via 172.31.0.1 dev eth0
172.31.0.0/20 dev eth0  proto kernel  scope link  src 172.31.4.137
# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         ip-172-31-0-1.a 0.0.0.0         UG        0 0          0 eth0
172.31.0.0      *               255.255.240.0   U         0 0          0 eth0
# routel
         target            gateway          source    proto    scope    dev tbl
        default         172.31.0.1                                     eth0
    172.31.0.0/ 20                    172.31.4.137   kernel     link   eth0
      127.0.0.0          broadcast       127.0.0.1   kernel     link     lo local
     127.0.0.0/ 8            local       127.0.0.1   kernel     host     lo local
----- output clipped -----

You can observe 172.31.0.1 is defined as default gateway on above system.

How to configure static route in Linux?

In RHEL: You need to edit below parameters in the config file /etc/sysconfig/network-scripts/route-ethX.

GATEWAY=10.10.0.1
NETMASK=255.0.0.0
IPADDR=10.10.0.22

where –

  • gateway is the default gateway for this interface
  • the netmask is a subnet mask value
  • ipaddr is the IP address of this interface

Caution : Do not change gateways on live production system.

In Debian: You can put your route in the file /etc/network/interfaces under the intended interface.

auto eth0
iface eth0 inet static
      address 10.10.0.22
      netmask 255.0.0.0
      up route add -net 10.10.0.0 netmask 255.0.0.0 gw 10.10.0.1

In all Linux systems you can use ip route command to define the static route. Command syntax is :

# ip route ip-address via dest-address

where, ip-address is IP of host or interface and dest-address is next HOP address i.e. route IP

How to configure default gateway in Linux?

In RHEL: Define default gateway IP against GATEWAY parameter in /etc/sysconfig/network file and then restart network service.

In Debian: Define default gateway IP against gateway parameter in /etc/network/interfaces file and then restart network service.

Or you can try these commands on any Linux (ex gateway as 10.10.0.1).

# ip route add default via 10.10.0.1
# route add default gw 10.10.0.1

Once set verify the default gateway with commands shown at the beginning of this article.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.