How to add a static route in systemd-networkd?

15,772

This is a bug in systemd, still present on version 240.

Per the thread above, a workaround (or solution, I am not sure about the status since the bug is still open) is to add GatewayOnlink=yes in the [Route] section:

[Match]
Name=eth0

[Network]
DHCP=yes

[Route]
Gateway=192.168.0.10
Destination=10.0.0.0/8
GatewayOnlink=yes

This parameter is available from systemd version ~237.

Share:
15,772
WoJ
Author by

WoJ

Updated on September 18, 2022

Comments

  • WoJ
    WoJ over 1 year

    I have a host which is on 192.168.0.0/24 with an IP of 192.168.0.13. Its DHCP-provided gateway is 192.168.0.254.

    The configuration is done via systemd-networkd and the issue below is present at least until version 240 (the version I use today - see my answer regarding further versions).

    I need this host to reach 10.0.0.0/8 networks which are available through a 192.168.0.10 gateway - this can be done by adding a static route:

    # ip r add 10.0.0.0/8 via 192.168.0.10
    

    It works fine (the traffic goes through).

    I now wanted to add this entry to my systemd-networkd configuration, by adding a [Route] to my curent definition

    [Match]
    Name=eth0
    
    [Network]
    DHCP=yes
    
    # the entry below is added to ensure a static route
    
    [Route]
    Gateway=192.168.0.10
    Destination=10.0.0.0/8
    

    It does not work:

    • no static route is added
    • there is an error message in the log mentioning an unreachable network
    Jan 17 11:29:32 rpi-dmz systemd[1]: Stopping Network Service...
    Jan 17 11:29:32 rpi-dmz systemd[1]: Stopped Network Service.
    Jan 17 11:29:32 rpi-dmz systemd[1]: Starting Network Service...
    Jan 17 11:29:33 rpi-dmz systemd-networkd[14584]: wlan0: Gained IPv6LL
    Jan 17 11:29:33 rpi-dmz systemd-networkd[14584]: eth0: Gained IPv6LL
    Jan 17 11:29:33 rpi-dmz systemd-networkd[14584]: Enumeration completed
    Jan 17 11:29:33 rpi-dmz systemd[1]: Started Network Service.
    Jan 17 11:29:33 rpi-dmz systemd-networkd[14584]: eth0: Could not set route: Network is unreachable
    Jan 17 11:29:33 rpi-dmz systemd-networkd[14584]: eth0: DHCPv4 address 192.168.0.13/24 via 192.168.0.254
    

    How to correctly add that route via systemd-networkd?

  • user2948306
    user2948306 over 5 years
    FWIW I would agree this is an open bug in systemd. The equivalent settings in NetworkManager work without hitting this problem; I think it is very reasonable for people to expect that the very similar-looking options in systemd-networkd will also work. It is undesirable for them to have to discover this does not work by experiment and have to look in the log - and then even when you look at the log messages, it is not very helpful.
  • user2948306
    user2948306 over 5 years
    I don't know what the best behaviour would be though. I see "Note that DHCPv6 will by default be triggered by [receiving a packet of type] Router Advertisement, if that is enabled..."
  • tambre
    tambre about 4 years
    @WoJ GatewayOnlink has been deprecated in favour of GatewayOnLink since 242. Worth updating the answer.
  • Arnie97
    Arnie97 almost 4 years
    In my case I got Could not set route: No route to host, and GatewayOnlink=yes fixes it for me. Tested on systemd 241 and 246.