静看光阴荏苒
不管不顾不问不说也不念

linux设置默认网关,网关ip不在同一子网

还是那台机器又碰到个网络问题,机器默认的网关ip不在同一个网段,

以往添加默认网关使用下面这个命令即可:

ip route add default via 148.251.xxx.xxx dev eth0

但是对于这种网关不在同一网段的情况会报错,解决办法是使用onlink:

ip route add default via 148.251.xxx.xxx dev eth0 onlink

机器用的systemd-networkd,编辑对应的网卡配置文件:

nano /etc/systemd/network/20-wired.network

增加一个[Route]然后在里面配置网关地址并使用GatewayOnLink=yes:

[Match]
Name=eth0

[Network]
Address=5.9.xxx.xxx/24
Address=xxx:xxx:xxx:xxx:x::x/80
IPv6AcceptRA=no

[Route]
Gateway=148.251.xxx.xxx
GatewayOnLink=yes

[Route]
Gateway=xxx:xxx:xxx:xxx:x::x
GatewayOnLink=yes

重启networkd:

systemctl restart systemd-networkd
赞(1)
未经允许不得转载:荒岛 » linux设置默认网关,网关ip不在同一子网
分享到: 更多 (0)

评论 4

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    定時過來看看有什麼新活兒!

    John11个月前 (06-15) Google Chrome 114.0.0.0 Google Chrome 114.0.0.0 Android 10 Android 10回复
  2. #2

    怎么会有这么神奇的网络配置 :grin:

    欧文斯8个月前 (08-21) Google Chrome 106.0.0.0 Google Chrome 106.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 很多商家都有这种情况,hetzner的云服务器也是。

      LALA8个月前 (08-23) Google Chrome 116.0.0.0 Google Chrome 116.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复

分享创造快乐

广告合作资源投稿