问题

  1. 因为学习 OpenStack ,虚拟机增加一块网卡,结果 apt update 出错了!

    Temporary failure in name resolution

  2. 新增加的网卡 203.0.113.0/24 ,是虚拟的一个外网

    cat 11-installer-config.yaml

     # This is the network config written by 'subiquity'
     network:
       ethernets:
         eth1:
           addresses:
           - 203.0.113.123/24
           gateway4: 203.0.113.1
           nameservers:
             addresses:
             - 223.5.5.5
             - 114.114.114.114
             search: []
       version: 2
    
  3. ping mirrors.aliyun.com
    也报错!

    ping: mirrors.aliyun.com: Temporary failure in name resolution

  4. 甚至找出 mirrors.aliyun.com 的 IP 是 27.128.147.239后
    直接 $ ping 27.128.147.239 也失败!

  5. 其他也有这样子的双网卡配置的虚拟机,人家那里不报错!

  6. 冷静一想,可能是路由问题!duibiyixia

    正确的虚拟机
    ip route list

     default via 192.168.0.81 dev eth0 proto static 
     default via 203.0.113.1 dev brq1c17dcdc-ab proto static metric 100 
     192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.125 
     192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 
     203.0.113.0/24 dev brq1c17dcdc-ab proto kernel scope link src 203.0.113.125 
    

    错误的虚拟机

    ip route list

     default via 203.0.113.1 dev eth1 proto static 
     default via 192.168.0.81 dev eth0 proto static 
     192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.123 
     203.0.113.0/24 dev eth1 proto kernel scope link src 203.0.113.123 
    

    原来是 default 的路由第一条是 203.0.113.1 ,那是一个假的外网,当然是不可以了!

简单处理

直接删除第一条路由规则**

ip route delete default

再次 list 就可以看到第一条 default 改变了,现在一切正常了!

彻底处理

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

Ubuntu 20.04 为什么有时候错,有时候不错?

root@compute1:/etc/netplan# cat 11.yaml

# This is the network config written by 'subiquity'
network:
  ethernets:
    eth1:
      addresses:
      - 203.0.113.123/24
        #  gateway4: 203.0.113.1
      nameservers:
        addresses:
        - 223.5.5.5
        - 114.114.114.114
        search: []
      optional: true
      routes:
         - to: 0.0.0.0/0
           via: 203.0.113.1
           metric: 100
  version: 2

后记

尝试按照 22.04 的方式修改 eth0 ,他竟然不认识 default ,必须 IP 地址
所以,还是保留 gateway4

cat 00-installer-config.yaml

# This is the network config written by 'subiquity'
network:
  ethernets:
    eth0:
      addresses:
      - 192.168.0.123/24
      gateway4: 192.168.0.1
      nameservers:
        addresses:
        - 223.5.5.5
        - 114.114.114.114
        search: []
      optional: true
      #routes:
      #  - to: default
      #    via: 192.168.0.1
      #    metric: 100
  version: 2

后记 2

在 centos 7 上面也遇到这个问题

  1. 临时修改

[root@localhost ~]# ip route list

default via 203.0.113.1 dev eth1 
169.254.0.0/16 dev eth0 scope link metric 1002 
169.254.0.0/16 dev eth1 scope link metric 1003 
192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.145 
203.0.113.0/24 dev eth1 proto kernel scope link src 203.0.113.145 

跑到 eth1 去了

[root@localhost ~]# ip route del default

[root@localhost ~]# ip route list

169.254.0.0/16 dev eth0 scope link metric 1002 
169.254.0.0/16 dev eth1 scope link metric 1003 
192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.145 
203.0.113.0/24 dev eth1 proto kernel scope link src 203.0.113.145 

删除后没有 default 了

[root@localhost ~]# ip route add default via 192.168.0.81

[root@localhost ~]# ip route list

default via 192.168.0.81 dev eth0 
169.254.0.0/16 dev eth0 scope link metric 1002 
169.254.0.0/16 dev eth1 scope link metric 1003 
192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.145 
203.0.113.0/24 dev eth1 proto kernel scope link src 203.0.113.145 

add 之后有 default

  1. 修改配置文件

    [root@controller ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1

     TYPE=Ethernet
     PROXY_METHOD=none
     BROWSER_ONLY=no
     BOOTPROTO=static
     DEFROUTE=yes
     IPV4_FAILURE_FATAL=no
     IPV6INIT=yes
     IPV6_AUTOCONF=yes
     IPV6_DEFROUTE=yes
     IPV6_FAILURE_FATAL=no
     IPV6_ADDR_GEN_MODE=stable-privacy
     NAME=eth0
     # UUID=3a65fcbc-9f5e-44e6-88b9-6cdd80e6ecbb
     DEVICE=eth1
     IPADDR=203.0.113.145
     GATEWAY=203.0.113.1
     DNS1=114.114.114.114
     ONBOOT=yes
     IPV4_ROUTE_METRIC=100
    

    通过 IPV4_ROUTE_METRIC 设置路由优先级,值越小,优先级越高

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐