View and create network routes using the ROUTE command in Linux

Home > Search > Linux commands
  by

net-tools may need to be installed to use the route command..

[root@server1 ~]# yum install net-tools

 

The route command can be used to view the routing table. In this example, the routing table has two routes. The default route is for packets addressed to external hosts (WAN), and the other route is for internal hosts (LAN).

[root@server1 ~]# route
Kernel IP routing table
Destination    Gateway          Genmask           Flags    Metric    Ref   Use  Iface
default        r1.example.com   0.0.0.0           UG       0         0     0    eth0
192.168.1.0    *                255.255.255.0     U        100       0     0    eth0

 

Flags:

  • U = the interface is up
  • G = the route uses a gateway

 

The netstat -r option can be used to print the routing table. This is the same exact output as the route command.

[root@server1 ~]# netstat -r
Kernel IP routing table
Destination    Gateway          Genmask           Flags    Metric    Ref   Use  Iface
default        r1.example.com   0.0.0.0           UG       0         0     0    eth0
192.168.1.0    *                255.255.255.0     U        100       0     0    eth0

 

Similarly, the ip route command also displays the routing table. However, the output of the ip route command formatted differently than the route command.

[root@server1 ~]# ip route
default via 192.168.1.1 dev eth0 proto static metric 0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.6 metric 100

 


Display IP addresses only

The -n option command can be used to only display IP addresses, and to not display hostnames. In this example, instead of r1.example.com being displayed, 192.168.1.1 is displayed.

[root@server1 ~]# route -n
Kernel IP routing table
Destination    Gateway          Genmask           Flags    Metric    Ref   Use  Iface
0.0.0.0        192.168.1.1      0.0.0.0           UG       0         0     0    eth0
192.168.1.0    0.0.0.0          255.255.255.0     U        100       0     0    eth0

 


Temporarily delete route

The del command can be used to temporarily remove an entry from the routing table. The routing table will return to it's prior configuration when the machine is restarted.

[root@server1 ~]# route del -net 0.0.0.0 gw 192.168.1.1 netmask 0.0.0.0

 

Use default to delete the default gateway.

[root@server1 ~]# route del default 192.168.1.1

 


Temporarily add route

The add command can be use to temporarily add an entry from the routing table.

[root@server1 ~]# route add -net 0.0.0.0 gw 192.168.1.1 netmask 0.0.0.0

 

Add the word default to temporarily add a record for the default gateway.

[root@server1 ~]# route add default -net 0.0.0.0 gw 192.168.1.1 netmask 0.0.0.0

 


Permanently add or delete routes

On a Debian distribution, permanent routes can be created in the /etc/network/interfaces file. In this example, the default gateway has been set to 192.168.1.1 in the /etc/sysconfig/network file.

On Red Hat, some distributions will use the /etc/sysconfig/network-scripts/ifcfg-xxxxxxxxx file. Some distribution will use the /etc/sysconfig/network file to set up permanent routes. 

[root@server1 ~]# cat /etc/sysconfig/network
NETWORKING=yes
GATEWAY=192.168.1.1

 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments