Linux Networking: To Route or To Ip Route? - Enable Sysadmin

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Enable

x Sysadmin   Articles About Welcome


Subscribe now
Get the highlights in your inbox
every week.

Work email * Linux networking: To route or to ip


route?     
Country *

-- Please Select --
Which route will you choose? Old school or new-fangled.

Submit
Posted: June 2, 2020 | 3 min read | Tyler Carrigan (Editorial Team, Red Hat)
Privacy Statement

Photo by Markus Spiske from Pexels

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 Great Linux resources
since the genesis of time, or at least since the 1960's when the Advanced Linux commands
concept was invented. Routing as we know it today didn't exist until cheat sheet
1981 when a team of researchers developed the first multiprotocol
Download RHEL 9 at no charge
router. Even though the technology is relatively new, it's highly
through the Red Hat Developer
integrated into our daily lives.
program
During my time as a support engineer, I found myself troubleshooting A guide to installing
network connectivity issues more often than I care to remember. applications on Linux
Many times, I would find myself having to engage a senior tech for
Linux system administration
help because I didn't fully understand the routing from device to
skills assessment
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 Cheat sheet: Old Linux
luck! I'm going to provide that information here. commands and their modern
replacements
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. ]

Topics:   Networking   

Tyler Carrigan
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. More about me

Try Red Hat Enterprise Linux Download now


Download it at no charge from the Red Hat Developer program.

Related Content

Network automation with ip vs. ifconfig: Which do Introduction to certificate


Ansible validated content you use? compression in GnuTLS
Ansible Network Border Gateway Compare Linux commands for Certificate compression improves
Protocol (BGP) validated content configuring a network interface, and performance of Transport Layer
collection focuses on platform- let us know in the poll which you Security handshake without some of
agnostic network automation and prefer. the risks exploited in protocol-level
enhances BGP management. compression.
Posted: January 27, 2023
Posted: February 23, 2023 Author: Seth Kenlon (Editorial Team, Red Posted: January 11, 2023
Author: Rohit Thakur (Red Hat) Hat) Author: Zoltan Fridrich (Red Hat)

OUR BEST CONTENT, DELIVERED TO YOUR INBOX

Work email *

Country *
-- Please Select --

Submit

Privacy Statement

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. The content published on this site are community
contributions and are for informational purpose only AND ARE NOT, AND ARE NOT INTENDED TO BE, RED HAT DOCUMENTATION, SUPPORT, OR ADVICE.

Red Hat and the Red Hat logo are trademarks of Red Hat, Inc., registered in the United States and other countries.

About Red Hat Jobs Events Locations Contact Red Hat Red Hat Blog Diversity, equity, and inclusion Cool Stuff Store Red Hat Summit

×
© 2023 Red Hat, Inc.
We use cookies on our websites to deliver our online services. Details about how we use cookies and how you may disable them are set out in our Privacy
Privacy statement Terms of use All policies and guidelines Digital accessibility
Statement. By using this website you agree to our use of cookies.

You might also like