{"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<p>In this tutorial, you will learn how to set static routes via an interface\/IP on CentOS\/Ubuntu systems. In a network, a <a href=\"https:\/\/www.cloudflare.com\/learning\/network-layer\/what-is-routing\/\" target=\"_blank\" rel=\"noreferrer noopener\">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<h2 class=\"wp-block-heading\">Set Static Routes via an Interface\/IP on CentOS\/Ubuntu<\/h2>\n\n\n\n<p>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<p>The following is the network architecture that we will use in this guide.<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>                                +-----------------+\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<p>So how can you set static routes via an interface\/IP on CentOS\/Ubuntu?<\/p>\n\n\n\n<p>Considering an example network architecture above;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Systems on Site A, LAN A cannot reach systems on Site B, LAN B. However, it can reach Site B Public router IP (21.22.23.10).<\/li><li>Similarly, systems on Site B, LAN B cannot reach systems on Site A, LAN A, but it can reach Site A Public router IP (11.12.13.10).<\/li><li>Main Router can only communicate with LAN B via Router B.<\/li><li>Main Router can only communicate with LAN A via Router A<\/li><li>Router A\/LAN A can only communicate with LAN B via Main router.<\/li><li>Router B\/LAN B can only communicate with LAN A via Main router.<\/li><li>Systems on LAN A can only know how to communicate to systems on LAN B via Router A.<\/li><li>Systems on LAN B can only know how to communicate to systems on LAN A via Router B.<\/li><\/ul>\n\n\n\n<p>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&#8217;s routing table.<\/p>\n\n\n\n<p>Note, all the routers we have in place are Linux based routers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Add Static Routes to LAN A and LAN B on Main Router<\/h3>\n\n\n\n<p>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<p>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<p>There are different ways in which you can set static routes via an interface\/IP on CentOS\/Ubuntu systems;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"#ip-command\">Using IP command<\/a><\/li><li><a href=\"#network-manager\">Using NetworkManager (nmtui and nmcli)<\/a><\/li><li><a href=\"#using-netplan\">Using Netplan<\/a><\/li><li><a href=\"#edit-network-interfaces\">Via static configurations in \/etc\/network\/interfaces file on Debian systems<\/a><\/li><li><a href=\"#network-scripts\">Via Static route definition in \/etc\/sysconfig\/network-scripts\/route-INT config on CentOS\/RHEL systems<\/a><\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"ip-command\">How to Set Static Routes using IP command<\/h4>\n\n\n\n<p>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<p>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<p>IP address configuration on Main Router;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip -o -c -4 a | awk -v OFS='\\t' '{print $1,$2,$3,$4}'<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>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<p>Add static route to LAN B (172.16.10.0\/24) via Router B on Main Router;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ip route add 172.16.10.0\/24 via 21.22.23.10<\/code><\/pre>\n\n\n\n<p>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<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/static-routes-to-LAN-B.png\" class=\"td-modal-image\"><figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1045\" height=\"536\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/static-routes-to-LAN-B.png\" alt=\"Set Static Routes via an Interface\/IP on CentOS\/Ubuntu\" class=\"wp-image-13844\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/static-routes-to-LAN-B.png?v=1662199722 1045w, https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/static-routes-to-LAN-B-768x394.png?v=1662199722 768w\" sizes=\"(max-width: 1045px) 100vw, 1045px\" \/><\/figure><\/a><\/div>\n\n\n\n<p>Add static route to LAN A (10.163.10.0\/24) via Router A on Main Router;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ip route add 10.163.10.0\/24 via 11.12.13.10<\/code><\/pre>\n\n\n\n<p>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<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/static-routes-to-LAN-A.png\" class=\"td-modal-image\"><figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1069\" height=\"520\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/static-routes-to-LAN-A.png\" alt=\"Set Static Routes via an Interface\/IP on CentOS\/Ubuntu\" class=\"wp-image-13843\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/static-routes-to-LAN-A.png?v=1662199655 1069w, https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/static-routes-to-LAN-A-768x374.png?v=1662199655 768w\" sizes=\"(max-width: 1069px) 100vw, 1069px\" \/><\/figure><\/a><\/div>\n\n\n\n<p>You can list all the routes using the command below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip route<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ip r<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ip route show<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ip r s<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ip r s &lt;interface><\/code><\/pre>\n\n\n\n<p>And that is how to set static routes using IP command. You can delete the routes by just replacing <strong><code>add<\/code><\/strong> with <strong><code>delete<\/code><\/strong> in the above commands.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"network-manager\">Set Static Routes using Using NetworkManager (nmtui and nmcli)<\/h4>\n\n\n\n<p>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<p>NetworkManager is installed by default on CentOS\/RHEL distros.<\/p>\n\n\n\n<p>On Ubuntu systems, you have to install it, <strong><code>sudo apt install network-manager<\/code><\/strong>.<\/p>\n\n\n\n<p>You can launch NetworkManger GUI by just typing;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nmtui<\/code><\/pre>\n\n\n\n<p>If it is not running, just start it, <strong><code>sudo systemctl start NetworkManager<\/code><\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>When NetworkManager GUI tool launches, select <strong>Edit a connection<\/strong> and press Enter.<\/li><li>Select and Interface to add the route via, for example, in our setup, to add a route to LAN B on Main Router, then select enp0s10 or enp0s9 for adding route to LAN A.<\/li><li>Edit the interface and scroll down to Routing > Edit (to add custom routes) > Add;<\/li><\/ul>\n\n\n\n<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/add-routes-via-nmtui.png\" class=\"td-modal-image\"><figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1023\" height=\"444\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/add-routes-via-nmtui.png\" alt=\"Set Static Routes via an Interface\/IP on CentOS\/Ubuntu\" class=\"wp-image-13845\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/add-routes-via-nmtui.png?v=1662199780 1023w, https:\/\/kifarunix.com\/wp-content\/uploads\/2022\/09\/add-routes-via-nmtui-768x333.png?v=1662199780 768w\" sizes=\"(max-width: 1023px) 100vw, 1023px\" \/><\/figure><\/a><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>Click Ok > Ok and exit the TUI tool.<\/li><\/ul>\n\n\n\n<p>In desktop systems, you can edit the interface from network settings and add the route to your network.<\/p>\n\n\n\n<p>In most cases, I would prefer the use of NMCLI command;<\/p>\n\n\n\n<p>To use NMCLI command to set static routes;<\/p>\n\n\n\n<p>First get the name of the connection associated with your interface;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nmcli con s<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>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<p>Now, modify the connection and add the route, for example to add route to LAN B;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nmcli conection modify enp0s10 +ipv4.routes \"172.16.10.0\/24 21.22.23.10\"<\/code><\/pre>\n\n\n\n<p>To add route to LAN A;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nmcli conection modify enp0s9 +ipv4.routes \"10.163.10.0\/24 11.12.13.10\"<\/code><\/pre>\n\n\n\n<p>You can then reload the connections to apply the changes;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nmcli connection reload<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"using-netplan\">Set Static Routes using Netplan<\/h4>\n\n\n\n<p>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<p>Edit the Netplan configuration file;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vim \/etc\/netplan\/00-installer-config.yaml<\/code><\/pre>\n\n\n\n<p>Add routes against each interface as follows;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\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<strong>      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<strong>      routes:\n              - to: 172.16.10.0\/24\n                via: 21.22.23.10<\/strong>\n<\/code><\/pre>\n\n\n\n<p>Save and apply the changes, (you can test with netplan try before saving the changes);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo netplan apply<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"edit-network-interfaces\">Via static configurations in <strong><code>\/etc\/network\/interfaces<\/code><\/strong> file<\/h4>\n\n\n\n<p>On Debian systems, the network interface configs are defined in the <strong><code>\/etc\/network\/interfaces<\/code><\/strong> file.<\/p>\n\n\n\n<p>Thus, to add static routes, edit the file;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vim \/etc\/network\/interfaces<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n...\n<strong>auto 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<p>Save and exit the configuration.<\/p>\n\n\n\n<p>Restart networking<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart networking.service <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"network-scripts\">Via Static route definition in <strong><code>\/etc\/sysconfig\/network-scripts\/route-INT<\/code><\/strong> config on CentOS\/RHEL systems<\/h4>\n\n\n\n<p>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 <strong><code>\/etc\/sysconfig\/network-scripts\/<\/code><\/strong>. <strong><code>INT<\/code><\/strong> refers to the interface device name.<\/p>\n\n\n\n<p>To use this approach, you need to install the old network-scripts package;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo yum install network-scripts<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vim \/etc\/sysconfig\/network-scripts\/route-enp0s9<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ADDRESS0=172.16.10.0\nNETMASK0=255.255.255.0\nGATEWAY0=21.22.23.10<\/code><\/pre>\n\n\n\n<p>It can also take the format;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>172.16.10.0\/24 via 21.22.23.10 dev enp0s10<\/code><\/pre>\n\n\n\n<p>Save and exit the file.<\/p>\n\n\n\n<p>Restart network service;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudoc systemctl restart network<\/code><\/pre>\n\n\n\n<p>And that is how you can set static routes via an interface\/IP on CentOS\/Ubuntu systems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-and-use-nmcli-tool-on-debian-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install and use NMCLI tool on Debian Linux<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/connect-to-wifi-in-linux-using-nmcli-command\/\" target=\"_blank\" rel=\"noreferrer noopener\">Connect to WiFi in Linux Using NMCLI command<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/create-virtual-secondary-ip-addresses-on-an-interface-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">Create Virtual\/Secondary IP addresses on an Interface in Linux<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to set static routes via an interface\/IP on CentOS\/Ubuntu systems. In a network, a route is a path<\/p>\n","protected":false},"author":1,"featured_media":13847,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[63,121],"tags":[5731,5736,5732,5735,5733,5734,5730],"class_list":["post-13834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-networking","category-howtos","tag-ip-command-set-static-route","tag-netplan-add-static-routes","tag-nmcli-add-static-route","tag-set-static-routes-on-centos","tag-set-static-routes-on-debian","tag-set-static-routes-on-rocky","tag-set-static-routes-via-an-interface-ip-on-centos-ubuntu","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/13834"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=13834"}],"version-history":[{"count":10,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/13834\/revisions"}],"predecessor-version":[{"id":13848,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/13834\/revisions\/13848"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/13847"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=13834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=13834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=13834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}