0

Due to some network peculiarities involving VPNs and conflicting IP ranges, I have two subnets routing to two different interfaces. I would like to make one IP address in one subnet go out via a different gateway.

I can accomplish this by running:

$ route add -host 1.2.3.4 gw 5.6.7.8
$ ip route show
1.2.3.4 via 5.6.7.8 dev eth0 scope link 

I would like to make this change permanent. As I'm using systemd-networkd, I am trying to do this by updating the existing /etc/systemd/network/50-dhcp.conf:

[Match]
Name=eth0

[Network]
DHCP=ipv4

[Route]
#Gateway=5.6.7.8
Destination=1.2.3.4/32

This works, but without the Gateway line it doesn't set the route:

$ ip route show
1.2.3.4 dev eth0 proto static scope link

If I uncomment the Gateway line then the new route does not appear at all!

How can I specify a gateway when adding the static route using systemd-networkd?

-1

You should look at this post:

What is the best way to add a permanent route?

It explains how you should:

  1. Create a named routing table, in the case below the routing table is called "mgmt" and gets the number "200".

    echo '200 mgmt' >> /etc/iproute2/rt_tables
    
    • Originally the /etc/iproute2/rt_tables file looks like this, with some reserved numbers:

      #
      # reserved values
      #
      255     local
      254     main
      253     default
      0       unspec
      #
      # local
      #
      
  2. The post goes on specifying how to add the routes:

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

Thanx to user Christopher for this answer.

  • Unfortunately this answer only applies to Debian, which I'm not using, so /etc/network doesn't exist on my system. I am using systemd-networkd instead of the Debian network manager, so I'm after a solution that works with systemd. Maybe you could update your answer with a systemd solution instead of a Debian-only solution? – Malvineous Aug 9 '18 at 1:08
  • Check out the link I posted in the answer. – aliex Aug 17 '18 at 10:34
  • The link still only explains for Debian and RHEL, it does not mention systemd-networkd at all! – Malvineous Aug 20 '18 at 3:52

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.