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. ]


关于作者

Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. Now that he has surfaced, he lives with his wife and son near Raleigh, where he worked as a data storage engineer before finding his way to the Red Hat team. He has written numerous technical documents, from military procedures to knowledgebase articles and even some training curricula. In his free time, he blends a passion for hiking, climbing, and bushcraft with video games and computer building. He is loves to read and enjoy a scotch or bourbon. Find him on Twitter or on LinkedIn.

UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来