{"id":13834,"date":"2022-09-03T13:19:49","date_gmt":"2022-09-03T10:19:49","guid":{"rendered":"https:\/\/kifarunix.com\/?p=13834"},"modified":"2022-09-03T13:19:54","modified_gmt":"2022-09-03T10:19:54","slug":"set-static-routes-via-an-interface-ip-on-centos-ubuntu","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/set-static-routes-via-an-interface-ip-on-centos-ubuntu\/","title":{"rendered":"Set Static Routes via an Interface\/IP on CentOS\/Ubuntu"},"content":{"rendered":"\n
In this tutorial, you will learn how to set static routes via an interface\/IP on CentOS\/Ubuntu systems. In a network, a route<\/a> is a path between networks. Routes through which a packet, in a packet-switching network, uses to travel from one network to the other is decided by a router in a specific network. So, how can one set static routes via an interface\/IP on CentOS\/Ubuntu systems?<\/p>\n\n\n\n Now, you may want to set static routes for one reason or another. This simple guide will show you how you can be able to set static routes via an interface\/IP on CentOS\/Ubuntu systems.<\/p>\n\n\n\n The following is the network architecture that we will use in this guide.<\/p>\n\n\n\n So how can you set static routes via an interface\/IP on CentOS\/Ubuntu?<\/p>\n\n\n\n Considering an example network architecture above;<\/p>\n\n\n\n Therefore, for systems on Site A, LAN A and Site B, LAN B to communicate, static routes to each network need to be defined explicitly on the MAIN router’s routing table.<\/p>\n\n\n\n Note, all the routers we have in place are Linux based routers.<\/p>\n\n\n\n Main router can only be able to communicate with LAN B (172.16.10.0\/24) via Router B, 21.22.23.10. This will also means that, once the route to LAN B has added to the Main Router routing table, Router A and subsequently LAN A, can be able to communicate with LAN B.<\/p>\n\n\n\n The same applies to systems on LAN B. Once the route to LAN A is added to the Main Router, Router B and subsequently LAN B, can be able to communicate with LAN A.<\/p>\n\n\n\n There are different ways in which you can set static routes via an interface\/IP on CentOS\/Ubuntu systems;<\/p>\n\n\n\n IP command can be used to set static routes on Linux system. Any configuration set using IP are temporary and can be lost by restarting network services, turning the interface off, or even system reboot.<\/p>\n\n\n\n As an example to show how to set static routes on Linux systems using IP command, consider our Main router above.<\/p>\n\n\n\n IP address configuration on Main Router;<\/p>\n\n\n\n Add static route to LAN B (172.16.10.0\/24) via Router B on Main Router;<\/p>\n\n\n\n Once you do this, LAN A, via Router A via Main Router via Router B can now connect to LAN B; See the output for ping, and traceroute commands below;<\/p>\n\n\n\n Add static route to LAN A (10.163.10.0\/24) via Router A on Main Router;<\/p>\n\n\n\n Similarly, once you do this, LAN B, via Router B via Main Router via Router A can now connect to LAN A; See the output for ping, and traceroute commands below;<\/p>\n\n\n\n You can list all the routes using the command below;<\/p>\n\n\n\n And that is how to set static routes using IP command. You can delete the routes by just replacing You can use the GUI NetworkManager or the command line nmcli tool to also set static routes to your specific network. The use of NetworkManger will require interfaces created and Managed using NetworkManager itself.<\/p>\n\n\n\n NetworkManager is installed by default on CentOS\/RHEL distros.<\/p>\n\n\n\n On Ubuntu systems, you have to install it, You can launch NetworkManger GUI by just typing;<\/p>\n\n\n\n If it is not running, just start it, In desktop systems, you can edit the interface from network settings and add the route to your network.<\/p>\n\n\n\n In most cases, I would prefer the use of NMCLI command;<\/p>\n\n\n\n To use NMCLI command to set static routes;<\/p>\n\n\n\n First get the name of the connection associated with your interface;<\/p>\n\n\n\n Sample output;<\/p>\n\n\n\n Now, modify the connection and add the route, for example to add route to LAN B;<\/p>\n\n\n\n To add route to LAN A;<\/p>\n\n\n\n You can then reload the connections to apply the changes;<\/p>\n\n\n\n At least from Ubuntu 18.04 on-wards, network settings are now managed via netplan on command line. You can as well use the network setting wizard on desktop systems.<\/p>\n\n\n\n Edit the Netplan configuration file;<\/p>\n\n\n\n Add routes against each interface as follows;<\/p>\n\n\n\n Save and apply the changes, (you can test with netplan try before saving the changes);<\/p>\n\n\n\n On Debian systems, the network interface configs are defined in the Thus, to add static routes, edit the file;<\/p>\n\n\n\n Save and exit the configuration.<\/p>\n\n\n\n Restart networking<\/p>\n\n\n\n On CentOS and RHEL based systems, you can set static route by creating route configuration file and define the network and gateway to that network under To use this approach, you need to install the old network-scripts package;<\/p>\n\n\n\n It can also take the format;<\/p>\n\n\n\n Save and exit the file.<\/p>\n\n\n\n Restart network service;<\/p>\n\n\n\n And that is how you can set static routes via an interface\/IP on CentOS\/Ubuntu systems.<\/p>\n\n\n\n Install and use NMCLI tool on Debian Linux<\/a><\/p>\n\n\n\n Connect to WiFi in Linux Using NMCLI command<\/a><\/p>\n\n\n\nSet Static Routes via an Interface\/IP on CentOS\/Ubuntu<\/h2>\n\n\n\n
+-----------------+\n | [ MAIN ROUTER ] |\n +--------------------+-------+---------+---------------+\n | | [ 11.12.13.1 ] | |\n + | [ 21.22.23.1 ] | |\n | +-----------------+ +\n | |\n | |\n+----------+----------+ +----------+---------+\n| ROUTER A | | ROUTER B |\n+ [ 11.12.13.10 ] + + [ 21.22.23.10 ] +\n| [ 10.163.10.1 ] | | [ 172.16.10.1 ] |\n+----------+----------+ +----------+---------+\n | |\n + +\n | |\n + +\n | |\n +---------+---------+ +---------+---------+\n | [ LAN A ] | | [ LAN B ] |\n + [ 10.163.10.10 ] + + [ 172.16.10.10 ] +\n +-------------------+ +-------------------+\n<\/code><\/pre>\n\n\n\n
Add Static Routes to LAN A and LAN B on Main Router<\/h3>\n\n\n\n
How to Set Static Routes using IP command<\/h4>\n\n\n\n
ip -o -c -4 a | awk -v OFS='\\t' '{print $1,$2,$3,$4}'<\/code><\/pre>\n\n\n\n
1:\tlo\tinet\t127.0.0.1\/8\n2:\tenp0s3\tinet\t10.0.2.15\/24\n3:\tenp0s8\tinet\t192.168.57.46\/24\n4:\tenp0s9\tinet\t11.12.13.1\/24\n5:\tenp0s10\tinet\t21.22.23.1\/24<\/code><\/pre>\n\n\n\n
sudo ip route add 172.16.10.0\/24 via 21.22.23.10<\/code><\/pre>\n\n\n\n
<\/figure><\/a><\/div>\n\n\n\n
sudo ip route add 10.163.10.0\/24 via 11.12.13.10<\/code><\/pre>\n\n\n\n
<\/figure><\/a><\/div>\n\n\n\n
ip route<\/code><\/pre>\n\n\n\n
ip r<\/code><\/pre>\n\n\n\n
ip route show<\/code><\/pre>\n\n\n\n
ip r s<\/code><\/pre>\n\n\n\n
ip r s <interface><\/code><\/pre>\n\n\n\n
add<\/code><\/strong> with
delete<\/code><\/strong> in the above commands.<\/p>\n\n\n\n
Set Static Routes using Using NetworkManager (nmtui and nmcli)<\/h4>\n\n\n\n
sudo apt install network-manager<\/code><\/strong>.<\/p>\n\n\n\n
nmtui<\/code><\/pre>\n\n\n\n
sudo systemctl start NetworkManager<\/code><\/strong>.<\/p>\n\n\n\n
<\/figure><\/a><\/div>\n\n\n\n
nmcli con s<\/code><\/pre>\n\n\n\n
NAME UUID TYPE DEVICE \n...\n...\nenp0s9 9981365e-b8f2-4394-b508-1233b4f95315 ethernet enp0s9 \nenp0s10 51292e4b-6b9a-4d07-9b89-3c6efea80f1d ethernet enp0s10<\/code><\/pre>\n\n\n\n
sudo nmcli conection modify enp0s10 +ipv4.routes \"172.16.10.0\/24 21.22.23.10\"<\/code><\/pre>\n\n\n\n
sudo nmcli conection modify enp0s9 +ipv4.routes \"10.163.10.0\/24 11.12.13.10\"<\/code><\/pre>\n\n\n\n
sudo nmcli connection reload<\/code><\/pre>\n\n\n\n
Set Static Routes using Netplan<\/h4>\n\n\n\n
sudo vim \/etc\/netplan\/00-installer-config.yaml<\/code><\/pre>\n\n\n\n
\n# This is the network config written by 'subiquity'\nnetwork:\n version: 2\n ethernets:\n enp0s3:\n dhcp4: true\n enp0s8:\n dhcp4: false\n addresses:\n - 192.168.57.46\/24\n enp0s9:\n dhcp4: false\n addresses:\n - 11.12.13.1\/24\n routes:\n - to: 10.163.10.0\/24\n via: 11.12.13.10<\/strong>\n enp0s10:\n dhcp4: false\n addresses:\n - 21.22.23.1\/24\n routes:\n - to: 172.16.10.0\/24\n via: 21.22.23.10<\/strong>\n<\/code><\/pre>\n\n\n\n
sudo netplan apply<\/code><\/pre>\n\n\n\n
Via static configurations in
\/etc\/network\/interfaces<\/code><\/strong> file<\/h4>\n\n\n\n
\/etc\/network\/interfaces<\/code><\/strong> file.<\/p>\n\n\n\n
sudo vim \/etc\/network\/interfaces<\/code><\/pre>\n\n\n\n
\n...\nauto enp0s9\niface enp0s9 inet static\n address 10.11.12.1\n netmask 255.255.255.0\n up ip route add 10.163.10.0\/24 via 11.12.13.10\n\nauto enp0s10\niface enp0s10 inet static\n address 21.22.23.1\n netmask 255.255.255.0\n up ip route add 172.16.10.0\/24 via 21.22.23.10<\/strong>\n<\/code><\/pre>\n\n\n\n
sudo systemctl restart networking.service <\/code><\/pre>\n\n\n\n
Via Static route definition in
\/etc\/sysconfig\/network-scripts\/route-INT<\/code><\/strong> config on CentOS\/RHEL systems<\/h4>\n\n\n\n
\/etc\/sysconfig\/network-scripts\/<\/code><\/strong>.
INT<\/code><\/strong> refers to the interface device name.<\/p>\n\n\n\n
sudo yum install network-scripts<\/code><\/pre>\n\n\n\n
sudo vim \/etc\/sysconfig\/network-scripts\/route-enp0s9<\/code><\/pre>\n\n\n\n
ADDRESS0=172.16.10.0\nNETMASK0=255.255.255.0\nGATEWAY0=21.22.23.10<\/code><\/pre>\n\n\n\n
172.16.10.0\/24 via 21.22.23.10 dev enp0s10<\/code><\/pre>\n\n\n\n
sudoc systemctl restart network<\/code><\/pre>\n\n\n\n
Other Tutorials<\/h3>\n\n\n\n