如何从 resolvconf 禁用特定接口 (dhclient)?

如何从 resolvconf 禁用特定接口 (dhclient)?

我正在使用ubuntu/trusty64Vagrant box 和 VirtualBox。我想根据 Vagrant 的默认界面永久禁用nameserver 10.0.2.3set by 。resolvconfeth0

我的网络定义Vagrantfile如下:

server.vm.network "private_network", type: "dhcp", virtualbox__intnet: true

eth1这将创建一个具有 DHCP 拉取设置的接口。看起来resolv.conf像这样:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.0.2.3
nameserver 10.20.30.40
search local
  • nameserver 10.0.2.3search local来自 DHCP 设置eth0
  • nameserver 10.20.30.40来自 DHCP 设置eth1

我想保留后者 (10.20.30.40),同时禁用来自eth0.我可以使用 暂时删除它resolvconf -d eth0.dhclient,但重新启动后设置会重新出现。

我知道我可以按照所述使用静态设置覆盖所有 DHCP DNS 设置这里,但是我想保留eth1接口中的 DHCP 设置并仅禁用eth0

我尝试过编辑/etc/resolvconf/interface-order和更改eth*,但eth1没有效果。

有没有不用修改/etc/dhcp/dhclient-enter-hooks.d/resolvconf脚本的方法?

答案1

这是因为 Vagrant 为 eth0 接口提供 IP 地址,默认设置是通过 DHCP。所以如果你想通过一些补丁来做到这一点,这里就是答案。您需要做的就是编辑您的 /etc/network/interfaces 文件

nano /etc/network/interfaces

post-up resolvconf -d eth0.dhclient在后面添加行iface eth1 inet dhcp

所以你/etc/network/interfaces会看起来像这样

`

 # This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# Source interfaces
# Please check /etc/network/interfaces.d before changing this file
# as interfaces may have been defined in /etc/network/interfaces.d
# NOTE: the primary ethernet device is defined in
# /etc/network/interfaces.d/eth0
# See LP: #1262951
source /etc/network/interfaces.d/*.cfg

#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
auto eth1
iface eth1 inet dhcp
    post-up resolvconf -d eth0.dhclient
#VAGRANT-END` 

答案2

您有几个选择:写保护您的 resolv.conf、更改 dhclient 代码并禁用名称服务器上的自动更新,或者禁用 eth0 的 DNS。

选项1:写保护 /etc/resolv.conf 文件:

$ chattr +i /etc/resolv.conf

+i 选项(属性)写保护 Linux 上的 /etc/resolv.conf 文件,以便任何人都无法修改它,包括 root 用户。

选项2:dhclient 脚本挂钩

打开 dhclient 文件夹中的“resolvconf”文件:

$ nano /etc/dhcp/dhclient-enter-hooks.d/resolvconf

并将代码更改为:

make_resolv_conf(){
    :
}

上面的脚本将用我们自己的函数替换 make_resolv_conf()。这个函数什么也不做。

选项 3:

打开你的 ifcfg 文件:

$ nano /etc/sysconfig/network-scripts/ifcfg-eth0

并将 PEERDNS 选项更改为否:

PEERDNS=no

奖金:这不会覆盖您的 resolv.conf 文件或禁用 DNS 更新,但会覆盖您的路由表。当您发送一些 DNS 请求时,无论 resolv.conf 文件中的内容是什么,它都会检查您的路由表并将请求发送到特定地址:

$ ip route add 8.8.8.8/32 via 192.168.1.1

相关内容