15

I need to add a route that won't be deleted after reboot. I read these two ways of doing it :

Add ip route add -net 172.X.X.0/24 gw 172.X.X.X dev ethX to the file /etc/network/interfaces

or

Create the file /etc/network/if-up.d/route with:

#!/bin/sh
route add -net 172.X.X.0/24 gw 172.X.X.X dev ethX

and make it executable :

chmod +x /etc/network/if-up.d/route

So I'm confused. What is the best way of doing it?

3
  • 1
    looks like you're focused on a Linux system? (we have AIX, HPUX, Irix, Solaris, ....) on the site.
    – Jeff Schaller
    Nov 7, 2016 at 17:50
  • You are right sorry I didn't specify that it was on a linux Debian system. Thanks for reminding it to me.
    – Pozinux
    Nov 8, 2016 at 9:21
  • You should use iproute2 commands like ip route add <network> via <IP> dev <DEV> instead of net-tools commands (ifconfig, route etc.)
    – AdamKalisz
    Apr 24, 2020 at 14:22

2 Answers 2

22

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.

1
  • 1
    For the Debian approach, you likely need to add equivalent pre-down or post-down commands to clean up the routes when the interface is taken down, otherwise a systemctl restart networking will error out when it tries to add an existing route for the interface. Apr 10, 2021 at 7:23
-1

On Debian based distro you can add a static route permanently as follows:

 echo "up route add -net 172.X.X.X/24 gw 172.X.X.X dev ethX" | sudo tee --append /etc/network/interfaces

On RHEL based distro:

echo "172.X.X.X/24 via 172.X.X.X" | sudo tee --append /etc/sysconfig/network-scripts/route-ethX
6
  • 4
    The sudo is meaningless in both your two commands. Either you're already root, so the >> works, or you're not, in which case the >> is applied as your original user and only the echo is run as root. Also, this fails dismally if there are multiple interfaces defined in /etc/network/interfaces.
    – roaima
    Jan 12, 2017 at 23:23
  • 1
    echo "sth" | sudo tee filename
    – JSBach
    Sep 15, 2017 at 9:53
  • this is not working for debian instances in gcp Feb 2, 2019 at 15:26
  • Indeed, concerning the Debian version, the given command highly depends on what interface is configured last in the file /etc/network/interfaces
    – Gohu
    Oct 7, 2019 at 16:13
  • Any scripts in Debian should now use iproute2 commands like ip route add <network> via <IP> dev <DEV> instead of net-tools route and ifconfig commands that are not installed by default.
    – AdamKalisz
    Apr 24, 2020 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.