ubuntu加路由 使用netplan - xd21303/Notebook GitHub Wiki

传统方式

sudo route add -net 0.0.0.0 netmask 0.0.0.0 gw 172.16.98.254 metric 80

sudo route add -net 60.60.60.0 netmask 255.255.255.0 gw 80.80.80.254 metric 90

推荐方式

在/etc/netplan目录下,新增config.yaml文件。

network:
  ethernets:
    enp5s0:
      addresses:
        - 80.80.80.229/24
      dhcp4: false
      gateway4: 80.80.80.254
      routes:
        - to: 60.60.60.0/24
          via: 80.80.80.254
          metric: 90
    enp0s31f6:
      dhcp4: true
      nameservers:
        addresses:
          - 114.114.114.114
          - 218.2.135.1
        search: []
      routes:
        - to: 0.0.0.0/0
          via: 172.16.98.254
          metric: 80
  version: 2

修改完成后使设置生效,重启后仍然生效。

sudo netplan apply config.yaml