Linux NetworkManager 管理下DNS配置
系统重启后/etc/resolv.conf 被重置
1 [root@my-test-host ~]# cat /etc/resolv.conf 2 # Generated by NetworkManager
当前网卡配置文件
1 [root@my-test-host ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 2 3 # Created by cloud-init on instance boot automatically, do not edit. 4 # 5 DEVICE=eth0 6 HWADDR=FA:16:3E:C1:B6:67 7 MTU=1450 8 ONBOOT=yes 9 TYPE=Ethernet10 USERCTL=no11 IPADDR=10.200.5.16412 PREFIX=2413 GATEWAY=10.200.5.114 IPV6INIT=yes15 IPV6ADDR=2409:8c60:2500:28::405:5a4/11216 IPV6_DEFAULTGW=2409:8c60:2500:28::405:117 PROXY_METHOD=none18 BROWSER_ONLY=no19 DEFROUTE=yes20 IPV4_FAILURE_FATAL=no21 IPV6_AUTOCONF=yes22 IPV6_DEFROUTE=yes23 IPV6_FAILURE_FATAL=no24 NAME="System eth0"25 UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e0326 BOOTPROTO=none
使用nmcli添加DNS服务器
[root@my-test-host ~]# nmcli c modify "System eth0" ipv4.dns "10.200.5.4 10.200.6.4"
nmcli添加DNS后/etc/resolv.conf 中并没有相应的配置生成。此时也不能完成域名解析,也就是说虽然通过nmcli已经添加DNS server但系统最终还是会去/etc/resolv.conf查询dns server配置。
1 [root@my-test-host ~]# cat /etc/resolv.conf 2 # Generated by NetworkManager 3 [root@my-test-host ~]# nmcli c s "System eth0" |grep dns 4 connection.mdns: -1 (default) 5 ipv4.dns: 10.200.5.4,10.200.6.4 6 ipv4.dns-search: -- 7 ipv4.dns-options: -- 8 ipv4.dns-priority: 0 9 ipv4.ignore-auto-dns: no10 ipv6.dns: --11 ipv6.dns-search: --12 ipv6.dns-options: --13 ipv6.dns-priority: 014 ipv6.ignore-auto-dns: no15 [root@my-test-host ~]# ping www.baidu.com16 ping: www.baidu.com: Name or service not known
查看网卡配置,多出的两条DNS配置是通过nmcli添加DNS时自动更新的。
1 [root@my-test-host ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 2 # Created by cloud-init on instance boot automatically, do not edit. 3 # 4 DEVICE=eth0 5 HWADDR=FA:16:3E:C1:B6:67 6 MTU=1450 7 ONBOOT=yes 8 TYPE=Ethernet 9 USERCTL=no10 IPADDR=10.200.5.16411 PREFIX=2412 GATEWAY=10.200.5.113 IPV6INIT=yes14 IPV6ADDR=2409:8c60:2500:28::405:5a4/11215 IPV6_DEFAULTGW=2409:8c60:2500:28::405:116 PROXY_METHOD=none17 BROWSER_ONLY=no18 DEFROUTE=yes19 IPV4_FAILURE_FATAL=no20 IPV6_AUTOCONF=yes21 IPV6_DEFROUTE=yes22 IPV6_FAILURE_FATAL=no23 NAME="System eth0"24 UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e0325 BOOTPROTO=none26 DNS1=10.200.5.427 DNS2=10.200.6.4
尝试reload/load网卡配置,还是无法进行域名解析
[root@my-test-host ~]# nmcli c reload /etc/sysconfig/network-scripts/ifcfg-eth0[root@my-test-host ~]# cat /etc/resolv.conf # Generated by NetworkManager[root@my-test-host ~]# ping www.baidu.comping: www.baidu.com: Name or service not known[root@my-test-host ~]# nmcli c load /etc/sysconfig/network-scripts/ifcfg-eth0[root@my-test-host ~]# ping www.baidu.comping: www.baidu.com: Name or service not known
重启NetworkManager 后/etc/resolv.conf 自动更新了DNS server
1 [root@my-test-host ~]# systemctl restart NetworkManager2 [root@my-test-host ~]# cat /etc/resolv.conf 3 # Generated by NetworkManager4 nameserver 10.200.5.45 nameserver 10.200.6.4
通过命令nmcli清除DNS并重启NetworkManager,发现/etc/resolv.conf 中之前添加的nameserver并没有被清理,但ifcfg-eth0中的配置被清理了。也就是说重启NetworkManager会增加/etc/resolv.conf中的配置,但似乎不会删除已有配置。当然reboot后/etc/resolv.conf还是会被重置的。
1 [root@my-test-host ~]# nmcli c modify "System eth0" ipv4.dns "" 2 [root@my-test-host ~]# systemctl restart NetworkManager 3 [root@my-test-host ~]# cat /etc/resolv.conf 4 # Generated by NetworkManager 5 nameserver 10.200.5.4 6 nameserver 10.200.6.4 7 [root@my-test-host ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 8 # Created by cloud-init on instance boot automatically, do not edit. 9 #10 DEVICE=eth011 HWADDR=FA:16:3E:C1:B6:6712 MTU=145013 ONBOOT=yes14 TYPE=Ethernet15 USERCTL=no16 IPADDR=10.200.5.16417 PREFIX=2418 GATEWAY=10.200.5.119 IPV6INIT=yes20 IPV6ADDR=2409:8c60:2500:28::405:5a4/11221 IPV6_DEFAULTGW=2409:8c60:2500:28::405:122 PROXY_METHOD=none23 BROWSER_ONLY=no24 DEFROUTE=yes25 IPV4_FAILURE_FATAL=no26 IPV6_AUTOCONF=yes27 IPV6_DEFROUTE=yes28 IPV6_FAILURE_FATAL=no29 NAME="System eth0"30 UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e0331 BOOTPROTO=none32 [root@my-test-host ~]#
再测试下发现,当通过nmcli清空DNS时,重启NetworkManager服务/etc/resolv.conf 不会被重置,但通过nmcli改变了原来DNS配置(也就是ifcfg-eth0中的配置),重启NetworkManage是会更新/etc/resolv.conf的。
1 [root@my-test-host ~]# nmcli c modify "System eth0" ipv4.dns "8.8.8.8" 2 [root@my-test-host ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 3 # Created by cloud-init on instance boot automatically, do not edit. 4 # 5 DEVICE=eth0 6 HWADDR=FA:16:3E:C1:B6:67 7 MTU=1450 8 ONBOOT=yes 9 TYPE=Ethernet10 USERCTL=no11 IPADDR=10.198.5.16412 PREFIX=2413 GATEWAY=10.198.5.114 IPV6INIT=yes15 IPV6ADDR=2409:8c60:2500:28::405:5a4/11216 IPV6_DEFAULTGW=2409:8c60:2500:28::405:117 PROXY_METHOD=none18 BROWSER_ONLY=no19 DEFROUTE=yes20 IPV4_FAILURE_FATAL=no21 IPV6_AUTOCONF=yes22 IPV6_DEFROUTE=yes23 IPV6_FAILURE_FATAL=no24 NAME="System eth0"25 UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e0326 BOOTPROTO=none27 DNS1=8.8.8.828 [root@my-test-host ~]# cat /etc/resolv.conf 29 # Generated by NetworkManager30 nameserver 10.198.5.431 nameserver 10.198.6.432 [root@my-test-host ~]# systemctl restart NetworkManager33 [root@my-test-host ~]# cat /etc/resolv.conf 34 # Generated by NetworkManager35 nameserver 8.8.8.836 [root@my-test-host ~]# sed -i '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth037 [root@my-test-host ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth038 # Created by cloud-init on instance boot automatically, do not edit.39 #40 DEVICE=eth041 HWADDR=FA:16:3E:C1:B6:6742 MTU=145043 ONBOOT=yes44 TYPE=Ethernet45 USERCTL=no46 IPADDR=10.198.5.16447 PREFIX=2448 GATEWAY=10.198.5.149 IPV6INIT=yes50 IPV6ADDR=2409:8c60:2500:28::405:5a4/11251 IPV6_DEFAULTGW=2409:8c60:2500:28::405:152 PROXY_METHOD=none53 BROWSER_ONLY=no54 DEFROUTE=yes55 IPV4_FAILURE_FATAL=no56 IPV6_AUTOCONF=yes57 IPV6_DEFROUTE=yes58 IPV6_FAILURE_FATAL=no59 NAME="System eth0"60 UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e0361 BOOTPROTO=none62 [root@my-test-host ~]# systemctl restart NetworkManager63 [root@my-test-host ~]# cat /etc/resolv.conf 64 # Generated by NetworkManager65 nameserver 8.8.8.866 [root@my-test-host ~]#
赞 (0)