Wondering how to use the route command in Linux?
If you’re someone familiarizing themselves with networking essentials, you’ll need to know how to work with IPs and network traffic.
For the manipulation of IP tables, you can make use of the
$ route command in Linux.
The route command is primarily used to re-link the routes of a given network to specific hosts. It achieves this with the help of network interfaces.
The command comes with a lot of flags and options, some of which can have drastic impacts on your active network.
If you’re someone who wishes to learn how to use the route command in Linux, then this guide is meant for you. We will cover the basic syntax of the code, the available flags, and options, along with examples on how to use the command effectively.
Without further ado, let’s begin!
Initializing the Route Command.
Before you can use the route command, make sure that it’s available on your system. As most Linux distros don’t have this command available by default, it is advised to install the command with the help of the Command Terminal.
We’ll be using Ubuntu 20.044 LTS for this guide, however, the process shouldn’t be any different for other Linux distributions.
Follow these steps to install the route command on your system:
- First, open the Command Terminal by pressing Ctrl + Alt + T on your keyboard.
- Next, type the command that’s suitable for your Linux distribution.
For Debian/Ubuntu, type the following:
$ sudo apt-get install net-tools
For RedHat/CentOS, type the following:
$ sudo yum install net-tools
For Fedora OS, type the following:
$ sudo dnf install net-tools
With the steps followed correctly, you should have the route command installed on your system, along with other commands essential for networking. Now, we can move on to the next step.
Syntax of the Route Command.
The syntax for the route command is as follows:
$ route <option>
$ route <option1> <option 2> <option 3>... <option N>
The command comes with a variety of options. A list of useful options, along with their descriptions are given below.
- -F Default mode. This works on FIB tables of the kernel.
- -v Used to enable verbose mode. This mode gives detailed information.
- -c Used to work on the routing cache memory of the kernel.
- del Used to delete gateways or routes.
- add Used to add/create new gateways or routes.
- -net Designates the target as a network.
- -host Designates the target as a host.
A detailed list of other options can be found in the official Linux Manual.
You can find out more about the route command by typing the following in the Command Terminal:
$ route --help
$ route -h
Using the Route Command.
Now that you have familiarized yourself with the syntax of the route command, you can proceed to learn how to use it in Linux.
Using the route command should give you the details of the IP route tables in the Terminal.
Follow these steps to use the route command in Linux:
- Start by opening the Command Terminal on your keyboard. You can do so by pressing Ctrl + Alt + T on your keyboard.
- Once that’s finished, type the following command:
$ sudo route
You should see a list of IPs and gateways as shown in the image below.
If you’d like to see the IP addresses instead of the names of the destination, type the following in the Command Terminal:
$ sudo route -n
Additional Route Commands.
Here is a list of commands that use
$ root at their core.
If you wish to add a new gateway, type the following command:
$ sudo route add default gw <ip address>
This will assign a specific gateway for all unnecessary packets.
The route command can also be used to display the information present in the cache. This can be achieved by using the following option with the route command:
$ sudo route -Cn
The route command can also be used to deny access to certain IPs. This can be accomplished with the help of the “add” and “reject” options.
The syntax for the command is as follow:
$ sudo route add -host <ip address> reject
Risks of Using the Route Command.
The route command comes with its own share of issues. You might find yourself in serious trouble if you don’t know what you’re doing.
An example would be deleting the wrong gateway. For example, typing the following will cause your default gateway to get deleted:
$ sudo route del default
This can lead to serious problems when using the internet. To counter this, it is recommended that you keep a copy of all your default IPs gateways so you can add them back when needed.
The $ ip Command.
$ ip command is often used together with the
$ route command.
Using the two commands in a pair can provide detailed information about the IPs that would otherwise be difficult to obtain.
You can get the route tables with the help of the ip command by typing the following in the Command Terminal:
$ ip route
The ip command can also be configured to display either IPv4 or IPv6 information only. That can be achieved by using the commands below.
In case you wish to display IPv4:
$ ip -4 r
In case you wish to display IPv6:
$ ip -6 r
To use the ip command for displaying the local tables, type the following in the Command Terminal:
$ ip route show table local
Aside from the route command, there are other protocols you can use to maximize the benefits of your Linux system. One of those is network bonding. This combines two or more interfaces to provide more bandwidth to manage your network traffic. Learning about network bonding is sure to help you in the long run.
We hope this guide helped you learn how to use the route command in Linux. If you have any queries or if you think we missed something, let us know in the comment section down below.