11 Best Linux route command examples(How to add route in Linux)

In this article, I will take you through 11 Best Linux route command examples. Linux route command is used to manage Kernel IP routing tables. When we say route then we are only talking about static routes which can be manipulated using route command and not the dynamic routes. You might be frequently using this command to create/delete/manage static route through your network interfaces. We will try to see the examples of How to add route in Linux.

11 Best Linux route command examples(How to add route in Linux) 1

Linux route command examples

Also Read: 10 Popular examples of sudo command in Linux(CentOS/RedHat 7/8)

Example 1: How to Check Linux route command version

If you want to check the route command version then you need to use route -V command as shown below. As you can from below output, route command is a part of net-tools package version 2.10-alpha. You might want to check How to Install route command on Linux (RedHat/CentOS 7/8) Using 5 Easy Steps to know more about route command installation in Linux.

[root@localhost ~]# route -V
net-tools 2.10-alpha
+NEW_ADDRT +RTF_IRTT +RTF_REJECT +I18N +SELINUX
AF: (inet) +UNIX +INET +INET6 +IPX +AX25 +NETROM +X25 +ATALK +ECONET +ROSE -BLUETOOTH
HW: +ETHER +ARC +SLIP +PPP +TUNNEL -TR +AX25 +NETROM +X25 +FR +ROSE +ASH +SIT +FDDI +HIPPI +HDLC/LAPB +EUI64

-V : display Linux route command version and exit

NOTE:

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to User.

Example 2: How to Display routes in Linux

You can simply check your current route by running Linux route command as shown below. From the below route, you can see that if any packets goes through this System has a destination in the IP address range from 192.168.0.0 to 192.168.0.255 will be routed to 0.0.0.0 gateway which is our local system and hence will not be routed anywhere else.

If any of the packets destination is out of the above range, then it will be forwarded to default gateway which is again 0.0.0.0. Hence all the packets will end here in the System itself.

Advertisements
[root@localhost ~]# route
Kernel IP routing table
Destination  Gateway   Genmask         Flags   Metric       Ref    Use     Iface
default      gateway    0.0.0.0         UG        0          0      0      enp0s3
192.168.0.0  0.0.0.0   255.255.255.0    U         0          0      0      enp0s3

Example 3: How to display routes in netstat format in Linux

If you want to check the routing table in netstat format then you need to use -e option with Linux route command as shown below.

[root@localhost ~]# route -e
Kernel IP routing table
Destination Gateway Genmask      Flags MSS Window irtt  Iface
default     gateway 0.0.0.0       UG    0    0      0   enp0s3
172.17.0.0  0.0.0.0 255.255.0.0   U     0    0      0   docker0
192.168.0.0 0.0.0.0 255.255.255.0 U     0    0      0   enp0s3

-e :  use netstat format for displaying the routing table. More info on Linux route command Man Page.

Example 4: How to display IP instead of hostname in the Routing table

By default Linux route command output will show hostname but if you want to show the IP instead of hostname then you need to use route -n command as shown below.

[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway     Genmask       Flags Metric Ref Use Iface
0.0.0.0     192.168.0.1 0.0.0.0        UG    100    0   0  enp0s3
172.17.0.0  0.0.0.0     255.255.0.0    U      0     0   0  docker0
192.168.0.0 0.0.0.0     255.255.255.0  U     100    0   0  enp0s3

-n : show numerical addresses instead of trying to determine symbolic host names.

Example 5: How to Add a route in Linux

If you want to add a route then you need to use below Linux route command. In this example, we are adding a route to 192.16.36.0 through enp0s3 network interface as shown below.

[root@localhost ~]# route add -net 192.16.36.0 netmask 255.255.255.0 dev enp0s3

add : add a new route.

-net : the target is a network.

After adding the route you can check again the route by using route -n command. From the below output, you can check the route is added now.

[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway     Genmask      Flags Metric Ref Use Iface
0.0.0.0     192.168.0.1 0.0.0.0       UG    100    0   0  enp0s3
172.17.0.0   0.0.0.0    255.255.0.0    U      0    0   0  docker0
192.16.36.0  0.0.0.0    255.255.255.0  U      0    0   0  enp0s3
192.168.0.0  0.0.0.0    255.255.255.0  U     100   0   0  enp0s3

Example 6: How to Reject a Route in Linux

If you want to add a route to rejection list then you need to use below route command. In this example, we are adding 2.2.2.0 route to the rejection list which will force any lookup to fail for this route. It means any of packets destined for an IP address range from 2.2.2.0 to 2.2.2.255 will not be forwarded anywhere else and will hence be terminated here.

[root@localhost ~]# route add -net 2.2.2.0 netmask 255.255.255.0 reject

reject : install a blocking route, which will force a route lookup to fail. More info on Linux route command Man Page.

Now check again if the above route is added in the list or not using route -n command.

[root@localhost ~]# route -n
Kernel IP routing table
Destination  Gateway     Genmask       Flags Metric Ref Use Iface
0.0.0.0     192.168.0.1  0.0.0.0        UG    100    0   0  enp0s3
2.2.2.0         -        255.255.255.0  !      0     -   0    -
112.14.16.0  0.0.0.0     255.255.255.0  U      0     0   0  enp0s3
172.17.0.0   0.0.0.0     255.255.0.0    U      0     0   0  docker0
192.16.36.0  0.0.0.0     255.255.255.0  U      0     0   0  enp0s3
192.168.0.0  0.0.0.0     255.255.255.0  U     100    0   0  enp0s3

Example 7: How to add a default gateway using Linux route command

If you want to add a default gateway then you need to use below route command. In this example, we are adding a default gateway 177.16.72.1 in our System to forward our packets through this.

[root@localhost ~]# route add default gw 177.16.72.1

add : add a new route.

gw : route packets via a gateway.

Example 8: How to Delete a Route using Linux route command

You can also delete any route using route command. For that you need to first identify the route which needs to be deleted using route -n command.

[root@localhost ~]# route -n
Kernel IP routing table
Destination    Gateway     Genmask      Flags Metric Ref Use Iface
0.0.0.0      192.168.0.1   0.0.0.0       UG    100    0   0  enp0s3
2.2.2.0          -        255.255.255.0  !      0     -   0    -
112.14.16.0   0.0.0.0     255.255.255.0  U      0     0   0  enp0s3
172.17.0.0    0.0.0.0     255.255.0.0    U      0     0   0  docker0
192.16.36.0   0.0.0.0     255.255.255.0  U      0     0   0  enp0s3
192.168.0.0   0.0.0.0     255.255.255.0  U     100    0   0  enp0s3

Now delete the route by using route delete command as shown in the below output. In this example, we have identified to remove any routing to destination 192.16.36.0-192.16.36.255 address range. So if any packets destined to this IP Range will not get forwarded to any gateway as the route entry is removed now.

[root@localhost ~]# route delete -net 192.16.36.0 netmask 255.255.255.0 dev enp0s3

delete : delete a route.

-net : the target is a network.

netmask : when adding a network route, the netmask to be used

dev : associate the rule with the device

enp0s3 : network interface

Now check if the route is removed or not by using route -n command as shown in the below output.

[root@localhost ~]# route -n
Kernel IP routing table
Destination   Gateway       Genmask       Flags Metric Ref Use Iface
0.0.0.0      192.168.0.1    0.0.0.0        UG    100    0   0  enp0s3
2.2.2.0        -           255.255.255.0   !      0     -   0   -
112.14.16.0  0.0.0.0       255.255.255.0   U      0     0   0  enp0s3
172.17.0.0   0.0.0.0       255.255.0.0     U      0     0   0  docker0
192.168.0.0  0.0.0.0       255.255.255.0   U     100    0   0  enp0s3

Example 9: How to Add a host in Linux 

If you want to add a host then you need to use below Linux route command. In this example, we are adding a host 12.123.0.10 through enp0s3 network interface. Below command means that any packets destined for host 12.123.0.10 will now get forwarded to a gateway 192.168.1.1 through network interface enp0s3.

[root@localhost ~]# route add -host 12.123.0.10 gw 192.168.1.1 enp0s3

-host : the target is a host.

Now you can again run route -n command and check if the route is added or not.

[root@localhost ~]# route -n
Kernel IP routing table
Destination   Gateway       Genmask          Flags Metric Ref Use  Iface
0.0.0.0       192.168.0.1   0.0.0.0           UG      0    0   0   enp0s3
12.123.0.10   192.168.0.1   255.255.255.255   UGH     0    0   0   enp0s3
192.168.0.0    0.0.0.0      255.255.255.0      U      0    0   0   enp0s3

Example 10: How to Delete a default gateway

If you want to delete your default gateway then you need to use below Linux route command. In this example, we are deleting our default gateway 177.16.72.1 which we previously added.

[root@localhost ~]# route del default gw 177.16.72.1

del : delete a route.

Example 11: How to Check other route options

If you want to check all the other available options with Linux route command then you need to use route --help command as shown below.

[root@localhost ~]# route --help
Usage: route [-nNvee] [-FC] [<AF>] List kernel routing tables
route [-v] [-FC] {add|del|flush} ... Modify routing table for AF.

route {-h|--help} [<AF>] Detailed usage syntax for specified AF.
route {-V|--version} Display version/author and exit.

-v, --verbose be verbose
-n, --numeric don't resolve names
-e, --extend display other/more information
-F, --fib display Forwarding Information Base (default)
-C, --cache display routing cache instead of FIB

<AF>=Use -4, -6, '-A <af>' or '--<af>'; default: inet
List of possible address families (linux route command):
inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)
netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP)
x25 (CCITT X.25)

–help : display all the available options.

 

Recommended Posts:-

Understanding Kafka Console Producer and Consumer in 10 Easy Steps

Popular firewalld examples to open a port on RedHat/CentOS 7

8 Most Popular mkdir command in Linux with Examples

26 Useful Firewall CMD Examples on RedHat/CentOS 7

12 Most Popular rm command in Linux with Examples

9 useful w command in Linux with Examples

Popular Apache Kafka Architecture Explained Using 4 Basic Components

5 Easy Steps to recover LVM2 Partition , PV , VG , LVM metadata in Linux

Advertisements

Leave a Reply