大网数据广告活动
服务器运维问答

systemd-networkd and direct routes

· 52CE 我爱测

I have problem adding direct routes through systemd-networkd. What I'm trying to achieve is similar to: ip a a 192.168.0.2/32 dev enp0s3 ip r a 192.168.0.1/32 dev enp0s3 ip r a default via 192.168.0.1 This is weird setup, I know,…

问题原文

原帖链接 · Ineu · CC BY-SA 3.0

I have problem adding direct routes through systemd-networkd. What I'm trying to achieve is similar to:

ip a a 192.168.0.2/32 dev enp0s3
ip r a 192.168.0.1/32 dev enp0s3
ip r a default via 192.168.0.1

This is weird setup, I know, but I can't use usual ip a a 192.168.0.2/24 to reach gateway. For some reason whole /24 subnet is reachable through the gateway in this subnet. So this default route is not only for catch-all routes, but to reach subnet as well. So, I've created /etc/systemd/network/enp0s3.network:

[Match]
Name=enp0s3

[Address]
Address=192.168.0.2/32

[Route]
Destination=192.168.0.1/32

[Route]
Gateway=192.168.0.1

The problem with this setup is that second Route block is never executed. I did some debugging and here is the difference between what ip and systemd-networkd does for the first Route block.

# ip
192.168.0.1 dev enp0s3 scope link
# networkd
192.168.0.1 dev enp0s3 proto static

So systemd-networkd doesn't add scope link to the route. That's why an attempt to add default route via 192.168.0.1 fails with Network is unreachable error. This is fixable by

ip r c 192.168.0.1/32 dev enp0s3 # automatically adds `scope link`
ip r a default via 192.168.0.1   # added successfully

So my question is: should such behaviour be considered a systemd-networkd bug or am I using it wrong? I couldn't find any documentation regarding direct routes in networkd. For now I've disabled systemd-networkd and created service to run ip commands directly. But I'd like to use standard approach as this is CoreOS I'm trying to configure.

已采纳解答

原帖链接 · Ineu · CC BY-SA 3.0

So the solution here is a bit tricky: you need to add gateway as a pointopoint parameter with /32 mask. This works:

[Match]
Name=enp0s3

[Address]
Address=192.168.0.2
Peer=192.168.0.1/32

[Network]
Gateway=192.168.0.1

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-02-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。