Adding static routes in linux

How to Add and Delete Static Route in Linux using IP Command

Part of the skill set for any Linux user, and particularly a systems administrator, is the ability to perform some network tweaks on a Linux system. This includes adding and deleting routes to enable the system to communicate with other systems o a local network. In this guide, we explore exactly how you can go about adding and deleting routes on a Linux system.

Viewing the existing routing table

Before we embark on adding or deleting routes, it’s prudent to check the existing default routes on a system. To do so, simply launch your terminal and issue the command:

$ ip route show Or $ ip route list

ip-route-show-command-output-linux

Similar statistics can be displayed using route command,

route-n-command-output-linux

route-command-output-linux

Also, you can use the good old netstat command, which is usually used for printing interface statistics as well as the routing table to achieve the same result.

netstat-nr-command-output-linux

With the default routing statistics in mind, let’s now move a step further and add some routes to our system.

Adding a static route using IP command

Suppose you want to take a backup of a Linux machine and push the backup file to another backup server in the subnet 10.0.2.0/24 . However, for one reason or the other, you cannot reach the backup server via the default gateway. In this case, you will have to create a new route for backup server subnet via another IP, say 192.168.43.223 via the interface enp0s3 .

The command for this will be

$ sudo ip route add 10.0.2.0/24 via 192.168.43.223 dev enp0s3

ip-route-add-command-linux

  • 10.0.2.0 -> is the network you want to connect to
  • /24 -> is the subnet mask
  • 192.168.43.223 -> is the IP through which we will reach the server
  • enp0s3 -> is the network interface

You can confirm whether new static route add been in route table using “ip route show” command.

ip-route-command-output-linux

To add the specific IP of the backup server, say 10.0.2.15 run the command:

$ sudo ip route add 10.0.2.15 via 192.168.43.223 dev enp0s3

ip-route-add-single-ip-linux

Once again, you can check the routing changes to see if the changes exist using the ip route show command:

Читайте также:  Source linux not found

ip-route-show-linux-command

route-n-linux-command-ouput

Permanently adding static route (RHEL, Fedora, CentOS)

The routes we have just added are temporary and will not survive a reboot. To make the routes persistent, you need to manually add them.

In the /etc/sysconfig/network-scripts/ directory, create an interface file route-interface where the interface attribute is your network interface name. In our case, this will be route-enp0s3 .

$ vim /etc/sysconfig/network-scripts/route-enps03

Next, we will add the routes as shown:

10.0.2.0/32 via 192.168.43.1 10.0.2.15 via 192.168.43.1

Save the file and exit. Then restart NetworkManager Service

$ sudo systemctl restart NetworkManager

Permanently adding static route (Ubuntu / Debian)

For Debian distributions, edit the file /etc/network/interfaces

$ sudo vim /etc/network/interfaces

Append the following line:

up route add -net 10.0.2.0 netmask 255.255.255.0 gw 192.168.43.1 dev enp0s3

Save and exit the file. Finally, for the changes to come into effect, run below commands

$ sudo ifdown enp0s3 && sudo ifup enp0s3

Deleting a static route

To delete a specific route, use the ip route del command. For example, to remove the route address we just added, run the command:

$ sudo ip route del 10.0.2.0/24 via 192.168.43.223 dev enp0s3

ip-route-del-subnet-linux-command

To delete a single IP route in a subnet run the command

$ sudo ip route del 10.0.2.15 via 192.168.43.223 dev enp0s3

ip-route-del-ip-linux-command

To delete default route run:

$ sudo ip route del default

To add a default route run below ‘ip route add’ command,

$ sudo ip route add default via dev interface

$ sudo ip route add default via 192.168.43.1 dev eth0

We hope that this tutorial was informative and provided you with insights into how you can go about adding and deleting static route in Linux.

Also Read : 12 ip Command Examples for Linux Users

Источник

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.

Читайте также:  Astra linux apache2 astramode

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 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.

Читайте также:  Гис независимый регистратор linux

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 

Источник

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