Ubuntu 22.04 双网卡网关设置报错:Conflicting default route declarations for IPv4

问题

最近 VBox 虚拟机服务器双网卡(一个外网、一个内网)时不时出现网卡故障,每次恢复都需要断开、重连才可以恢复。尝试修改会报出一下警告

$ sudo netplan apply

** (generate:1060): WARNING **: 03:19:21.684: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

尝试和解决

  1. 据说 ubuntu 22.04 的网关路由设置方式改变了,但是,查询他的安装文档
    Network Configuration ,没有发现和 20.04 有什么差别

  2. 参考网上很多文章后,改成以下 (假设外网地址 12.34.56.78,内网 192.168.0.5)

    $ cat 00-installer-config.yaml

     # This is the network config written by 'subiquity'
     network:
       ethernets:
         enp0s8:
           addresses:
           - 12.34.56.78/28
             #  gateway4: 12.34.56.1
           nameservers:
             addresses:
             - 223.5.5.5
             - 114.114.114.114
             search: []
           optional: true
           routes:
             - to: default
               via: 12.34.56.1
               metric: 100
         enp0s3:
           addresses:
           - 192.168.0.5/24
             # gateway4: 192.168.0.1
           nameservers:
             addresses:
             - 223.5.5.5
             - 114.114.114.114
             search: []
           optional: true
           routes:
             - to: 0.0.0.0/0
               via: 192.168.0.1
               metric: 100
       version: 2
    
  3. 但是 $ sudo netplan apply 出现警告和报错

     ** (generate:2624): WARNING **: 02:29:33.628: Problem encountered while validating default route consistency.Please set up multiple routing tables and use `routing-policy` instead.
     Error: Conflicting default route declarations for IPv4 (table: main, metric: 100), first declared in enp0s8 but also in enp0s3
     
     ** (process:2622): WARNING **: 02:29:34.210: Problem encountered while validating default route consistency.Please set up multiple routing tables and use `routing-policy` instead.
     Error: Conflicting default route declarations for IPv4 (table: main, metric: 100), first declared in enp0s8 but also in enp0s3
    

    $ ip route list

     default via 192.168.0.1 dev enp0s8 proto static metric 100 
     default via 12.34.56.1 dev enp0s3 proto static metric 100 
     192.168.0.0/24 dev enp0s8 proto kernel scope link src 192.168.0.5 
     12.34.560/28 dev enp0s3 proto kernel scope link src 12.34.56.78 
    

    会发现确实出现 2 个 default

  4. 当时在实际环境,着急使用,所以,简单删除了内网的route,可以正常使用了

    $ sudo ip route del 192.168.0.1
    其实只是改变了 2 个 default 的次序

  5. 今天重新思考并测试:
    既然外网已经有网关,那么,服务器访问外网不会有问题
    内网访问其他资源,应该仅限于内网区域,所以,修改他的 to 就应该可以了!

    $ cat 00-installer-config.yaml
    修改一下即可

       routes:
         - to: 192.168.0.0/24 # 0.0.0.0/0
           via: 192.168.0.1
           metric: 100
    

    再次 $ sudo netplan apply 不再报错和警告

    $ ip route list

     default via 12.34.1 dev enp0s8 proto static metric 100 
     192.168.0.0/24 dev enp0s3 proto kernel scope link src 192.168.0.5 
     192.168.0.0/24 via 192.168.0.81 dev enp0s3 proto static metric 100 
     12.34.56.0/28 dev enp0s8 proto kernel scope link src 12.34.56.78 
    

    现在只有一个 default 了

  6. 其他
    安装设置虚拟机时,特意将外网卡放在前面,但是,不知道为什么,他竟然网卡名字对应反了,结果,apt update 时报错,ping 外网死活不通!
    改正过来就好了!

你可能感兴趣的:(日常记录,ubuntu,服务器,linux)