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 126.96.36.199/24 via 188.8.131.52
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 184.108.40.206 netmask 255.255.255.0 up route add -net 220.127.116.11 netmask 255.255.0.0 gw 18.104.22.168
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 "22.214.171.124/24 126.96.36.199"
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.
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