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 gw
$ ip route show via 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:




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

$ ip route show 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?


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. is the IP address to assign to eth1. 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
    # The management network interface
    auto eth1
    allow-hotplug eth1
    iface eth1 inet static
      post-up ip route add dev eth1 src table mgmt
      post-up ip route add default via dev eth1 table mgmt
      post-up ip rule add from table mgmt
      post-up ip rule add to 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.