Network routes in Linux

Network routes in Linux   TESLA INSTITUTE

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. Default route also known as default gateway is the IP where packet should discover their path if current network or interface (from which packet originated on server) does not know path for destination IP. This article is about discussing network routes in Linux. It will cover how to define network route for network interface, where and how to define 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


# 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
    

# 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

    

# 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

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 config file /etc/sysconfig/network-scripts/route-ethX.


GATEWAY=10.10.0.1
NETMASK=255.0.0.0
IPADDR=10.10.0.22

    

GATEWAY=10.10.0.1
NETMASK=255.0.0.0
IPADDR=10.10.0.22

where –

    gateway is default gatway for this interface
    netmask is subnet mask value
    IPaddr is IP address of this interface

    Caution : Do not change gateways on live production system.

In Debian : You can put your route in file /etc/network/interfaces under 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

    

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 static route. Command syntex 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

    

# ip route add default via 10.10.0.1

# route add default gw 10.10.0.1

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

 

 

 

 

 

 

Top