Do you know what's less useful than a screen door on a submarine? A network with no routes. Routing has existed in tandem with networks since the genesis of time, or at least since the 1960's when the concept was invented. Routing as we know it today didn't exist until 1981 when a team of researchers developed the first multiprotocol router. Even though the technology is relatively new, it's highly integrated into our daily lives.
During my time as a support engineer, I found myself troubleshooting network connectivity issues more often than I care to remember. Many times, I would find myself having to engage a senior tech for help because I didn't fully understand the routing from device to device. If only there had been a place where I could get the basics of routing and the commands needed to make changes. Well, you're in luck! I'm going to provide that information here.
There are many ways to do things in Linux, and routing is no different. I want to examine two different commands for displaying and manipulating routing tables: the route
command and the ip route
command. We are going to look at both commands in some very common use cases to see what each utility can offer.
Displaying existing routes
First things first. You never want to make a change to a route until you verify the existing conditions. To do this, simply run the following:
Display existing routes with route
:
[tcarrigan@rhel ~]$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default _gateway 0.0.0.0 UG 100 0 0 enp0s3
10.0.2.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
Display existing routes with ip
:
[tcarrigan@rhel ~]$ ip route show
default via 10.0.2.2 dev enp0s3 proto dhcp metric 100
10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15 metric 100
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown
For a basic listing, I prefer the old school route
command, but your mileage may vary.
Adding new routes
At times, you need to add new routes between devices. To do this, use the examples below.
Using route
:
[root@rhel ~]# route add -net 10.0.2.0/24 gw 192.168.0.1 enp0s3
Here the syntax is: route add -net <network_address> gw <gatewayaddr> <interfacename>
Using ip
:
[root@rhel ~]# ip route add 10.0.2.0/24 via 192.168.0.1 dev enp0s3
The syntax for this command is: ip route add <network you want to connect to> via <ip used to reach the network> dev <interface name>
Removing routes
You can remove routes in a similar fashion.
Using route
:
[root@rhel ~]# route del -net 10.0.2.0/24 gw 192.168.0.1 enp0s3
The syntax is the same as the add
command, except we are using del
instead of add
.
Using ip
:
[root@rhel ~]# sudo ip route del 10.0.2.0/24 via 192.168.0.1 dev enp0s3
Again, we are only altering the syntax slightly from the add
command.
Adding a new default gateway
Another task you may need to accomplish is configuring traffic to flow to a gateway. To accomplish this, use the following commands.
Adding a new gateway with route
:
[root@rhel ~]# route add default gw 192.168.0.1
Adding a new gateway with ip
:
[root@rhel ~]# ip route add default via 192.168.0.1
To verify that the new gateway is set, use the standard route
command or ip route show
.
What do you think?
Well, we took a look at some of the most basic functions of these commands. What are your initial impressions? If I am being honest, I prefer the older route
command in every instance here. The syntax is easier to understand and remember, and the commands are usually shorter. Hopefully, I won't have to use either of these again anytime soon. However, if I do, I know which one I'll work with!
[ Getting started with networking? Check out the Linux networking cheat sheet. ]