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

How to translate "ip route add local 0.0.0.0/0 dev lo table 100" to systemd-networkd?

· 52CE 我爱测

I am trying to setup a software router with the goal of routing packets to a proxy server on the router. The tutorial I am reading has the following routing rules: ip rule add fwmark 1 table 100 ip route add local 0.0.0.0/0 dev l…

问题原文

原帖链接 · Minsheng Liu · CC BY-SA 4.0

I am trying to setup a software router with the goal of routing packets to a proxy server on the router. The tutorial I am reading has the following routing rules:

ip rule add fwmark 1 table 100
ip route add local 0.0.0.0/0 dev lo table 100

and the following iptables rules:

iptables -t mangle -N V2RAY
iptables -t mangle -A V2RAY -d [Private Addresses] -j RETURN
iptables -t mangle -A V2RAY -p udp -j TPROXY --on-port 12345 --tproxy-mark 1
iptables -t mangle -A V2RAY -p tcp -j TPROXY --on-port 12345 --tproxy-mark 1
iptables -t mangle -A PREROUTING -j V2RAY

The whole thing works perfectly. But now I am moving to use systemd-networkd to avoid any startup scripts, and I am having trouble to convert ip route add local 0.0.0.0/0 dev lo table 100 to systemd-networkd language.

My first question is, what does this route do? My understanding so far is:

  • iptables add a firewall wall mark 1.
  • The rule selects all packets with mark 1 to use routing table 100.
  • The route is added to table 100, so it only applies to packets with mark 1.
  • The "0.0.0.0/0" part means it is a default route.
  • But what does "local" mean here? Why device lo? Why do we need a route here? Can't iptables handle this directly?

My second question is how to do this in systemd-networkd.

For matching packets with mark 1, it seems easy (and worked on my router):

[Match]
Name = *

[RoutingPolicyRule]
FirewallMark = 1
Table = 100

I am not sure if Name = * is required though. Does this make sure that any packet from any device, as long as it has mark 1, will be routed using table 100? Or could I match some particular network device here?

I cannot, however, make the route in systemd-networkd. My attempt so far is:

[Match]
Name = lo

[Route]
Type = local
Destination = 0.0.0.0/0
Table = 100

But it seems that this has no effect: ip route show table 100 actually gives:

Error: ipv4: FIB table does not exist.
Dump terminated

How should I approach this?

已采纳解答

原帖链接 · bbaa bbaa · CC BY-SA 4.0

[Match]
Name = lo

[RoutingPolicyRule]
FirewallMark = 1
Table = 100
Priority = 100

[Route]
Table = 100 
Destination = 0.0.0.0/0
Type = local

Combine them into one file

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

资料来源与适用范围

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

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

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