CentOS7.6 鲜为人知的/etc/resolv.conf 之 /etc/resolv.conf.save (保持/etc/resolv.conf不被修改:/etc/dhcp/dhclient-enter-hooks 无效之/etc/resolv.conf被清空的特殊案例)

目的:

用户可以自定义/etc/resolv.conf内容,且不被系统修改。

常规方法1:

/etc/sysconfig/network-scripts/ifcfg-eth0 网卡配置文件中增加PEERDNS=no

注意:云平台受cloud-init 管理网卡,制作镜像后创建新机器会被重置

# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=dhcp
DEVICE=eth0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
PEERDNS=no

常规方法2:

创建dhcp 配置文件 /etc/dhcp/dhclient-enter-hooks 写入空函数

#!/bin/sh
make_resolv_conf(){
:
}

chmod +x /etc/dhcp/dhclient-enter-hooks

 

 

 常规方法3:

类似2,直接修改dhcp脚本:/usr/sbin/dhclient-script将里面的make_resolv_conf函数置空同上

 

 

 然并卵..............

案例1:

方法2.方法3 仅配置 /etc/dhcp/dhclient-enter-hooks无效。重启系统后/etc/resolv.conf 并没有保持住,反而被置空

 

 

 

 查看是否有备份文件/etc/resolv.conf.save

[root@localhost ~]# ls -l /etc/resolv.conf*
-rw-r--r-- 1 root root 1 Dec 18 17:10 /etc/resolv.conf
-rw-r--r-- 1 root root 1 Dec 18 17:13 /etc/resolv.conf.save
[root@localhost ~]# cat /etc/resolv.conf.save 

[root@localhost ~]# 

 

解决:

  删除/etc/resolv.conf.save

 原因解析:

/etc/resolv.conf.save 为备份文件,当dhcp脚本make_resolv_conf 为空时,直接从.save文件进行复制,导致配置无效。

案例2:

  https://www.cnblogs.com/xulingjie/p/9227799.html

  每次重启服务器后,/etc/resolv.conf文件就被自动还原了,最后发现是被Network Manager修改了。

查看Network Manager服务状态

systemctl status  NetworkManager

停止Network Manager服务

systemctl stop  NetworkManager

关闭Network Manager开机启动

systemctl disable  NetworkManager

posted on 2020-12-18 17:23  zhangmingda  阅读(2493)  评论(0编辑  收藏  举报

导航