Linux route Add Command Examples – ciscostuff

How do we add a route to linux system? what if we have to define custom routes for networking scheme required in my environment? How to use linux route command to show, add or modify the current routing table?

in order to modify the routing table, we can use any of the two commands; ip route or route. Both shows their magic and achieve the same goal of adding, deleting or modifying route parameters.

Display Current Routing Table

From the terminal, issue the following command

# route                                          

or

# ip route list

Output will look something like this:

[root@redHat~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.1 0.0.0.0 UG 100 0 0 ens192
10.21.0.0   0.0.0.0   255.255.0.0 U 100 0 0 ens192
192.168.122.0  0.0.0.0  255.255.255.0  U 0 0 0 virbr0

Additionally, following commands can be executed as well:

# route -n                                        

or

# ip route show                                   


Linux Add a Default Route using route add command

# route add default gw 192.168.1.1 dev ens192     

 

Linux add a default gateway via ens192 using ip route  add commmand

# ip route add 192.168.1.0/24 dev ens192          

Added routes can be verified using the above commands i.e ip route or route -n

 

Persistent route in Red Hat, Centos or Fedora

If you wish to add the routes and let them stay persistent regardless of the system reboots, edit the following configuration file in rpm based  operating systems.

# vi /etc/syconfig/network                        
GATEWAY=192.168.1.1

 

Share your thoughts

*