Network engineers often have to plan their network infrastructure and establish links between computers and networks using routing tables. As a part of this process, they need to add new network routes. There are several free tools that help you add routes in Linux. In this article, we will learn how to add route in Linux.
How to Add Route in Linux
Here are the different ways to add route in Linux.
1. Using ip command
The simplest way to add network route in Linux is to use ‘ip route add’ command, followed by the network address to be reached and gateway to be used.
$ ip route add <network_ip>/<cidr> via <gateway_ip> # Example $ ip route add 100.0.3.0/24 via 100.0.3.1
If you don’t specify network device, then your first network card will be selected, excluding your local loopback.
If you want to establish connection via specific device, you can mention it at the end of your command.
$ ip route add <network_ip>/<cidr> via <gateway_ip> dev <network_card_name>
Once you have added the new routes, you can use ip r command to view the list of routes already defined in your network.
$ ip route OR $ ip r
You can also use ping command to ping other systems on your new route to ensure that you are actually able to connect to them.
2. Using /etc/network/interfaces
You can also edit /etc/network/interfaces file to add a new route. It basically stores information about all network interfaces on your system and is used by many system administrators to manage their networks. Open it in a text editor.
$ sudo vi /etc/network/interfaces
Add the following lines to it, to add the network route described in previous step.
auto eth0 iface eth0 inet static address 100.0.2.2 netmask 255.255.255.0 up route add -net 100.0.3.0 netmask 255.255.0.0 gw 100.0.2.1
3. Using nmcli
You can also use nmcli tool to add new network routes in Linux. Here is the command to add new network route.
$ sudo nmcli connection modify <interface_name> +ipv4.routes "<network_ip> <gateway_ip>"
Here is the command to add new network route mentioned above in previous steps.
$ sudo nmcli connection modify enp0s3 +ipv4.routes "100.0.3.0/24 100.0.3.1"
You can apply changes using the following command.
$ sudo nmcli connection reload
In this short article, we have learnt how to add new network routes in Linux.
Also read:
How to Change Root Password in CentOS, RHEL, Fedora
How to Ping Specific Port in Linux
How to Count Number of Files in Linux
How to Increase Your Security on the Internet
How to Encrypt Partition in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.