To add static route in 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.

Читайте также:  Запуск libreoffice в linux

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.

Источник

How to Permanently add Static Route in Linux

Static routing is the process of manually entering the routes to the routing table to reach a particular destination. Basically, two commands are used in Linux to add routes. The first command is the old traditional route add and second is the IP route command.

In this tutorial, we learn how to add permanent static routes in Linux distributions such as CentOS Stream and Ubuntu.

Adding temporary static routes

In order to add a static route temporarily on your Linux machine, you can use either route or ip command.

You can list the current routing table as follows.

Output Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.206.1 0.0.0.0 UG 100 0 0 eno1 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eno1 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 192.168.206.0 0.0.0.0 255.255.255.0 U 100 0 0 eno1

If you want to add a route to the network 198.161.1.0 through gateway 192.168.206.1, you can execute the following command.

sudo route add -net 198.161.1.0 netmask 255.255.255.0 gw 192.168.206.1 eno1

Alternatively, you can use ip command as follows:

sudo ip route add 198.161.1.0/24 via 198.168.206.1 dev eno1

Adding permanent static routes

In the above section, we saw how to add routes in Linux. But the system will forget those routes on next reboot.

Читайте также:  Linux drivers dell latitude

So we have to add routes to config file to make it persistent.

On RHEL or CentOS, you need to modify the interface file in ‘/etc/sysconfig/network-scripts’.

For example, here, we have to add routes on network interface ens192. Hence, the file we need to modify will be ‘/etc/sysconfig/network-scripts/route-ens192’.

The basic syntax of static route in this file is:

target-address — Destination network address.

gateway-address — Router address to reach the destination address.

dev — Indicates which interface to reach the destination, interface naming schemes are eno[1-N], ens[1-N], and or eth[0-N].

So, in order to add the above route in ‘route-ens192’ file, append the following line.

‘10.9.8.0/24 via 10.9.8.100 dev ens192’

Then, you need to restart the network service.

sudo service network restart

Finally, verify that the new routes are visible in the routing table using the following command.

$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.9.8.100 0.0.0.0 UG 100 0 0 ens192 10.0.0.0 0.0.0.0 255.0.0.0 U 100 0 0 ens192 10.9.8.0 10.9.8.100 255.255.255.0 UG 100 0 0 ens192

On Ubuntu Linux, if you want to add a permanent static route to your system, you have to add route entry to the ‘/etc/network/interfaces’. Use your favorite editor (nano, vim. ) to open the network interface file and insert the following lines to it.

ip route add -net 10.9.7.0/24 gw 10.9.8.100 dev ens160
source /etc/network/interfaces.d/* # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto ens160 iface ens160 inet static address 10.9.8.41 netmask 255.255.255.0 gateway 10.9.8.100 ip route add -net 10.9.7.0/24 gw 10.9.8.100 dev ens160

In order to make the change take effect, you need to restart the networking service by running:

sudo /etc/init.d/networking restart

Verify that the route has been configured correctly:

$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.9.8.100 0.0.0.0 UG 0 0 0 ens160 10.9.7.0 10.9.8.100 255.255.255.0 UG 0 0 0 ens160 10.9.8.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160 10.233.70.0 10.9.8.45 255.255.255.0 UG 0 0 0 tunl0 10.233.90.0 0.0.0.0 255.255.255.0 U 0 0 0 *

From Ubuntu 18.04 LTS and higher version, the operating system uses the netplan YAML files for network configuration — it is located in the ‘/etc/netplan’ directory. For example:

$ ls /etc/netplan 01-network-manager-all.yaml

In order to add persistent static route, you have to add the following lines to the netplan configuration file ‘/etc/netplan/01-network-manager-all.yaml’

routes: - to: 192.168.205.0/24 via: 192.168.206.1
# Let NetworkManager manage all devices on this system network: version: 2 renderer: NetworkManager ethernets: eno1: dhcp4: true routes: - to: 192.168.205.0/24 via: 192.168.206.1

Apply the change by running:

Читайте также:  Перенаправление всего трафика linux

You can verify the route configuration by running the command route -n or ip route .

Conclusion

In this tutorial, we learned how to add a permanent route by adding to respective config files in Linux. Thanks for reading and please leave your suggestion in the below comment section.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

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