10

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

Add up 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?

  • 1
    looks like you're focused on a Linux system? (we have AIX, HPUX, Irix, Solaris, ....) on the site. – Jeff Schaller Nov 7 '16 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 '16 at 9:21
13

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
  • Very complete answer. Thanks for adding this second part. – Pozinux Nov 21 '16 at 20:52
  • 2
    Upvoted cause it's good to see people giving answers using ip route on Linux instead of using route add. People know for decades that iproute2 is deprecating all legacy network tools. – user34720 Feb 21 '18 at 17:12
  • post-up ip route add 172.16.100.0/24 dev eth1 src 172.16.100.1 table mgmt the line src 172.16.100.1 should be 172.16.100.10. The Interface IP of eth1 – JohannesM Nov 16 '18 at 11:00
  • 1
    @JohannesM Quite right. Nice catch, and thank you! – Christopher Nov 16 '18 at 12:34
  • hi Christopher, seems you have Knowledge i could Need, may you have a look at serverfault.com/questions/948940/…, would be very kind, thanks ;) – mech Jan 14 at 9:14
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
  • 3
    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 '17 at 23:23
  • 1
    echo "sth" | sudo tee filename – JSBach Sep 15 '17 at 9:53
  • this is not working for debian instances in gcp – Parv Sharma Feb 2 at 15:26

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.