Adding permanent routes in linux

Howto add permanent static routes in Ubuntu

Static routing is the term used to refer to the manual method used to set up routing. An administrator enters routes into the router using configuration commands. This method has the advantage of being predictable, and simple to set up. It is easy to manage in small networks but does not scale well.

Advantages of Static Routes

Disadvantages of Static Routes

  • Network changes require manual reconfiguration
  • Network outages cannot be automatically routed around
  • Does not scale well in large networks.

Add a Static route using «route» command

route add -net 10.10.10.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
route add -host 10.10.1.1 netmask 255.255.255.0 gw 192.168.1.1 dev eth0

This adds the route immediatly to the Kernel IP routing table. To confirm the route has been successfully, simply type the «route» command with no arguements:

Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.254 * 255.255.255.0 U 0 0 0 eth0
localnet * 255.255.255.0 U 0 0 0 eth0
10.10.10.0 * 255.255.255.0 U 0 0 0 eth0
10.10.1.1 * 255.255.255.0 U 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0

to print the Kernel IP Routing table.

To keep the Static Route persistent or you want to add the route entries to the network script files (not using the route command) then all you need to do is to edit the file

and the static routes in the following format:

up route add -net 172.20.11.0/16 gw 172.20.10.254 dev eth1

And the file will like the following

sudo cat /etc/network/interfaces

The output should show something like this

sudo cat /etc/network/interfaces

The output should show something like this

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0 eth1

iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
# dns-* options are implemented by the resolvconf package, if installed

iface eth1 inet static
address 172.20.10.1
netmask 255.255.255.0
broadcast 172.20.10.255
gateway 172.20.10.254

# static route
up route add -net 172.20.11.0/16 gw 172.20.10.254 dev eth1

The above has 2 Ethernet interfaces and the static route is added to the interface eth1.

For the change to /etc/network/interface to take effect. please restart the «networking» service as follows:

sudo /etc/init.d/networking restart

NOTE: If you added the route already using the «route» then there is no need to restart the networking service because, the next time server is restarted this takes effect.

Читайте также:  Linux скрипт бэкапа файлов

Источник

What is a Permanent or Persistent static route

Debian Permanent Static Routes

Debian Permanent Static Routes

In Linux, permanent static routes also called as Persistent routes are the static route entries that will not be deleted when the network restart or when the system restart.

Typically in a Linux System, route add and ip route add commands are used to add static routes to the routing table. But those static route entries get deleted from the routing table when either network or system restart.

If you want to add a route to the network 192.168.1.0 through gateway 192.168.221.1 and print result, you can execute the following commands.

~] ip route add 192.168.1.0/24 via 192.168.221.1 

And print result of previous command:

~] ip route show default via 84.244.68.1 dev ens192 onlink 84.244.68.0/24 dev ens192 proto kernel scope link src 84.244.68.206 192.168.1.0/24 via 192.168.221.1 dev ens192 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.206 

Restart networking service

~] systemctl restart networking 

After print result we can see that static rule is deleted:

~] ip route show default via 84.244.68.1 dev ens192 onlink 84.244.68.0/24 dev ens192 proto kernel scope link src 84.244.68.206 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.206 

So how we can make static routes permanent? We have a several option how to do it.

1. Edit /etc/network/interfaces file

The first option is edit /etc/network/interfaces file.

Following is the sample Debian (Ubuntu) network interface configuration file with permanent static route entries.

# The primary network interface auto ens192 allow-hotplug ens192 iface ens192 inet static  address 192.168.221.54/24  gateway 192.168.221.1  dns-nameservers 82.99.137.41 212.158.133.41  dns-search secar.cz  up ip route del 192.168.0.0/24 via 192.168.221.1 dev ens192  up ip route add 192.168.0.0/24 via 192.168.221.1 dev ens192  up ip route del 192.168.1.0/24 via 192.168.221.1 dev ens192  up ip route add 192.168.1.0/24 via 192.168.221.1 dev ens192 

When next hop (192.168.221.1) is in network subnet with direct attached interface, the dev [interface] in ip route command is optional.

Restart network with /etc/init.d/networking restart or with systemd restart networking command and print the result:

~] ip route show default via 192.168.221.1 dev ens192 onlink 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.54 192.168.0.0/24 via 192.168.221.1 dev ens192 192.168.1.0/24 via 192.168.221.1 dev ens192 

2. Create own file in /etc/network/if-up.d directory

Another way to create a static network route is to create a script file in a directory /etc/network/if-up.d. For me, this is the preferred way to create static routes in debian.

Change working directory to /etc/network/if-up.d, create file my_route, change permissions to 751 with chmod 751 my_route and insert this content:

#!/bin/sh  if [ "$IFACE" = "ens192" ]; then  ip route add 192.168.0.0/24 via 192.168.221.1  ip route add 192.168.1.0/24 via 192.168.221.1 fi 
~] ip route show default via 192.168.221.1 dev ens192 onlink 192.168.221.0/24 dev ens192 proto kernel scope link src 192.168.221.54 192.168.0.0/24 via 192.168.221.1 dev ens192 192.168.1.0/24 via 192.168.221.1 dev ens192 

Источник

Читайте также:  Install android sdk platform tools linux

How to add permanent static routes in Ubuntu Linux

In this tutorial, we are going to learn how to add a permanent static route in Ubuntu Linux.

How to add permanent static routes in Ubuntu Linux

For this tutorial, I am using Ubuntu Server 20.04, But you can use the following method to add a Persistent route in any previous Ubuntu version, including Ubuntu Desktop.

What is a Persistent route ?

In Ubuntu, permanent static routes, also called as Persistent routes are the static route entries that will not be deleted when the network restarts, or when the system restarts.

Typically in a Linux System, route add and ip route add commands are used to add static routes to the routing table. But those static route entries get deleted from the routing table when either network or system restart.

So how can we make static routes permanent?

Making Static Routes Persistent in Ubuntu Linux

In Ubuntu Linux, to make Static Routes Persistent, we need to add route entries to the network interface file (YAML text files in the /etc/netplan folder) using the routes property.

The /etc/netplan directory is the location where network configuration files are stored on Ubuntu Linux. Under the /etc/netplan directory, you will find at least one network configuration file with .yaml extension.

Following is a sample Ubuntu network interface configuration file with permanent static route entries.

# This is the network config written by 'subiquity' network: ethernets: enp0s3: dhcp4: false addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8] routes: - to: 192.168.2.0/24 via: 192.168.1.100 metric: 100 - to: 192.168.10.100 via: 192.168.1.100 metric: 100 version: 2

As per the above example, we have added two permanent static route entries using the routes argument under the network interface enp0s3.

routes: - to: 192.168.2.0/24 via: 192.168.1.100 metric: 100 - to: 192.168.10.100 via: 192.168.1.100 metric: 100

The gateway to use for the 192.168.2.0/24 network is 192.168.1.100. We have also set a host route to a host 192.168.10.100
via the 192.168.1.100 IP.

You need to reload netplan configuration via netplan apply command, if you add a new route entry to the YAML file.

You can view the routing table using the ip route show command.

If you are working on an older version of Ubuntu (16.04 or earlier) that still uses /etc/network/interfaces file, you need to use the up lines make static routes permanent, as shown in the following example:

auto enp0s3 iface enp0s3 inet static address 192.168.1.10 netmask 255.255.255.0 gateway 192.168.1.1 up route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.100

So that is how we add permanent routes in Ubuntu Linux. This method is used to add Persistent routes in all Debian Based Linux Distributions.

Источник

What is the best way to add a permanent route?

You are right sorry I didn’t specify that it was on a linux Debian system. Thanks for reminding it to me.

Читайте также:  What difference in linux and windows

You should use iproute2 commands like ip route add via dev instead of net-tools commands (ifconfig, route etc.)

2 Answers 2

You mentioned /etc/network/interfaces , so it’s a Debian system.

Create a named routing table. As an example, I have used the name, «mgmt,» below.

echo '200 mgmt' >> /etc/iproute2/rt_tables 

Above, the kernel supports many routing tables and refers to these by unique integers numbered 0-255. A name, mgmt, is also defined for the table.

Below, a look at a default /etc/iproute2/rt_tables follows, showing that some numbers are reserved. The choice in this answer of 200 is arbitrary; one might use any number that is not already in use, 1-252.

# # reserved values # 255 local 254 main 253 default 0 unspec # # local # 

Below, a Debian 7/8 interfaces file defines eth0 and eth1 . eth1 is the 172 network. eth0 could use DHCP as well. 172.16.100.10 is the IP address to assign to eth1 . 172.16.100.1 is the IP address of the router.

source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The production network interface auto eth0 allow-hotplug eth0 # iface eth0 inet dhcp # Remove the stanzas below if using DHCP. iface eth0 inet static address 10.10.10.140 netmask 255.255.255.0 gateway 10.10.10.1 # The management network interface auto eth1 allow-hotplug eth1 iface eth1 inet static address 172.16.100.10 netmask 255.255.255.0 post-up ip route add 172.16.100.0/24 dev eth1 src 172.16.100.10 table mgmt post-up ip route add default via 172.16.100.1 dev eth1 table mgmt post-up ip rule add from 172.16.100.10/32 table mgmt post-up ip rule add to 172.16.100.10/32 table mgmt 

Reboot or restart networking.

Update — Expounding on EL

I noticed in a comment that you were «wondering for RHEL as well.» In Enterprise Linux («EL» — RHEL/CentOS/et al), create a named routing table as mentioned, above.

The EL /etc/sysconfig/network file:

NETWORKING=yes HOSTNAME=host.sld.tld GATEWAY=10.10.10.1 

The EL /etc/sysconfig/network-scripts/ifcfg-eth0 file, using a static configuration (without NetworkManager and not specifying «HWADDR» and «UUID» for the example, below) follows.

DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=no BOOTPROTOCOL=none IPADDR=10.10.10.140 NETMASK=255.255.255.0 NETWORK=10.10.10.0 BROADCAST=10.10.10.255 

THE EL /etc/sysconfig/network-scripts/ifcfg-eth1 file (without NetworkManager and not specifying «HWADDR» and «UUID» for the example, below) follows.

DEVICE=eth1 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=no BOOTPROTOCOL=none IPADDR=172.16.100.10 NETMASK=255.255.255.0 NETWORK=172.16.100.0 BROADCAST=172.16.100.255 

The EL /etc/sysconfig/network-scripts/route-eth1 file:

172.16.100.0/24 dev eth1 table mgmt default via 172.16.100.1 dev eth1 table mgmt 

The EL /etc/sysconfig/network-scripts/rule-eth1 file:

from 172.16.100.0/24 lookup mgmt 

Update for RHEL8

This method described above works with RHEL 6 & RHEL 7 as well as the derivatives, but for RHEL 8 and derivatives, one must first install network-scripts to use the method described above.

dnf install network-scripts 

The installation produces a warning that network-scripts will be removed in one of the next major releases of RHEL and that NetworkManager provides ifup / ifdown scripts as well.

Источник

Оцените статью
Adblock
detector