是否可以阻止应用程序修改我的 /etc/resolv.conf dns

是否可以阻止应用程序修改我的 /etc/resolv.conf dns

最近遇到一个问题,技术上困扰了我很长时间。我的 Fedora 32 dns 配置总是/etc/resolv.conf被未知应用程序修改。修改后的配置如下所示:

[dolphin@MiWiFi-R4CM-srv]~/Documents/GitHub/reddwarf-music% sudo cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 192.168.31.1
nameserver 8.8.8.8
nameserver 114.114.114.114

我想要的 dnsconf 看起来像这样:

nameserver 8.8.8.8

我只想要8.8.8.8dns。如果我修改了/etc/resolv.conf,就会变成错误的配置,我该怎么做才能使dns配置得到控制?

答案1

将此写至/etc/NetworkManager/NetworkManager.conf

[main]
dns=none

它保持/etc/resolv.conf不变,因为 NetworkManager 将其标记为不受其自身和它已知的程序管理(unbounddnsmasqsystemd-resolved)。来自man NetworkManager.conf,下一节Main,和关键dns

none: NetworkManager will not modify resolv.conf. This implies rc-manager unmanaged

或者,您可以将其设置为您首选的 DNS 服务器systemd-resolved并进行编辑/etc/systemd/resolved.conf

[Resolve]
DNS=192.162.0.1, 9.9.9.9 # Whatever DNS server IP addresses you want, comma separated.
FallbackDNS=8.8.8.8, 1.1.1.1 # Same as above. 

不要忘记systemd-resolved.service之后启用。

答案2

真正的答案是chattr

是的,您可以阻止 NetworkManager 更改该文件。但是另一个应用程序或进程呢?防止文件被任何内容更改:

chattr +i file

允许文件改回:

chattr -i file

答案3

根据dhcpcd.conf手册页,

nohook script
    Don't run this hook script.  Matches full name, or prefixed with 2 numbers optionally ending with .sh.

    So to stop dhcpcd from touching your DNS settings or starting wpa_supplicant you would do:-
    nohook resolv.conf, wpa_supplicant

所以附加到你的dhcpcd.conf(我的位于/etc/dhcpcd.conf):

nohook resolv.conf

dhcpcd或者,使用will运行-C应该会起到相同的作用,但只是暂时的:

dhcpcd -C resolv.conf eth0

相关内容