【问题标题】:Remove EC2's entry from resolv.conf从 resolv.conf 中删除 EC2 的条目
【发布时间】:2014-06-07 15:04:52
【问题描述】:

我有私有 DNS 服务器,我想在 AWS/EC2 上的 Debian 上使用 resolvconf 将它们写入 resolv.conf。 名称服务器条目的顺序存在问题。 在我的resolv.conf 中,EC2 的默认名称服务器总是写在第一行,如下所示:

# 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 172.16.0.23
nameserver 10.0.1.185
nameserver 10.100.0.130
search ap-northeast-1.compute.internal

172.16.0.23 是 EC2 的默认域名服务器,其他的是我的。

如何删除 EC2 条目?或者,如何将 EC2 条目移到第三个?

这里有一个接口文件:

% ls -l /etc/resolvconf/run/interface/
-rw-r--r-- 1 root root  62 Jun  7 23:35 eth0

eth0 文件似乎是 dhcp 自动生成的,所以无法永久删除。

% cat /etc/resolvconf/run/interface/eth0
search ap-northeast-1.compute.internal
nameserver 172.16.0.23

我的私人 DNS 条目在这里:

% cat /etc/resolvconf/resolv.conf.d/base
nameserver 10.0.1.185
nameserver 10.100.0.130

请帮忙。

【问题讨论】:

  • 你试过禁用dhcpclient服务吗?

标签: amazon-ec2 debian resolve


【解决方案1】:

我想我刚刚解决了一个非常相似的问题。我被 Amazon EC2 蹩脚的内部 DNS 服务器所困扰,所以我想运行一个本地缓存 dnsmasq 守护进程并在 /etc/resolv.conf 中使用它。起初我只是做了echo nameserver 127.0.0.1 > /etc/resolv.conf,但后来我意识到我的更改最终会在重新启动或 DHCP 租约刷新后被 DHCP 客户端覆盖。

我现在所做的是编辑/etc/dhcp3/dhclient.conf 并取消注释prepend domain-name-servers 127.0.0.1; 行。您应该能够以非常相似的方式使用prepend 指令。

更新:这些说明基于 Ubuntu Linux,但我想一般概念也适用于其他系统,甚至其他 DHCP 客户端也必须具有类似的配置选项。

【讨论】:

  • 这很有帮助。我在 AWS 机器上设置 Consul DNS,需要它首先查看 localhost。在 CentOS 7 上,我运行了 echo "prepend domain-name-servers 127.0.0.1;" >> "/etc/dhcp/dhclient.conf"
  • 要立即查看更改,我需要运行service network restart
【解决方案2】:

这里有人提出了解决方案,基本上使用 rc.local 替换启动时的文件

https://forums.aws.amazon.com/thread.jspa?threadID=74497

  1. 编辑 /etc/sysconfig/network-scripts/ifcfg-eth0 说 PEERDNS=no
  2. 根据需要创建一个名为 /etc/resolv.backup 的文件
  3. 将以下 2 行添加到 /etc/rc.local:

rm -f /etc/resolv.conf cp /etc/resolv.backup /etc/resolv.conf

【讨论】:

  • 在多次故障排除后我们做了同样的事情。
【解决方案3】:

我正在从另一个方向(想要内部名称服务器)解决这个问题,我学到的大部分内容可能会引起人们的兴趣。

在 VPC 管理控制台中有几个选项可以控制名称解析。

VPC -> DHCP option sets -> Create dhcp option set

您可以在那里指定自己的名称服务器。 http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_DHCP_Options.html 请务必将此 dhcp 选项集附加到您的 VPC 以使其生效。

如果在 VPC 设置中禁用了以下设置,则不设置本地 dns 服务器(我错误地发现了这一点):

DnsHostnames

DnsSupport

http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-dns.html

也可以在本地覆盖设置(如果您在 vpc 之间移动实例,您会注意到这一点)。 /etc/dhcp/dhclient.conf

以下行可能很有趣:

prepend domain-name-servers

更改当然会在 dhclient 启动时生效。

【讨论】:

    【解决方案4】:

    How do I assign a static DNS server to a private Amazon EC2 instance running Ubuntu, RHEL, or Amazon Linux?

    简短说明

    与虚拟私有云 (VPC) 关联的 EC2 实例的默认行为是在启动时使用动态主机配置协议 (DHCP) 请求 DNS 服务器地址。 VPC 使用内部 DNS 服务器的地址响应 DHCP 请求。 DHCP 响应中返回的 DNS 服务器地址被写入本地 /etc/resolv.conf 文件,并用于 DNS 名称解析请求。重新启动实例时会覆盖对 resolv.conf 文件的任何手动修改。

    分辨率

    要将运行 Linux 的 EC2 实例配置为使用静态 DNS 服务器条目,请使用 vim 等文本编辑器编辑文件 /etc/dhcp/dhclient.conf 并将以下行添加到文件末尾:

    supersede domain-name-servers xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx;

    Ubuntu - dhclient.conf - DHCP client configuration file 

    The supersede statement
    
    supersede [ option declaration ] ;
    
    If for some option the client should always  use  a  locally-configured  value  or  values
    rather  than  whatever  is  supplied  by  the  server,  these values can be defined in the
    supersede statement.
    
    The prepend statement
    
    prepend [ option declaration ] ;
    
    If for some set of options the client should use a value you  supply,  and  then  use  the
    values  supplied  by  the  server,  if  any,  these  values  can be defined in the prepend
    statement.  The prepend statement can only be used for options which allow more  than  one
    value  to  be  given.   This restriction is not enforced - if you ignore it, the behaviour
    will be unpredictable.
    
    The append statement
    
    append [ option declaration ] ;
    
    If for some set of options the client should first use the values supplied by the  server,
    if  any,  and  then  use  values  you  supply,  these  values can be defined in the append
    statement.  The append statement can only be used for options which allow  more  than  one
    value  to  be  given.   This restriction is not enforced - if you ignore it, the behaviour
    will be unpredictable.
    

    【讨论】:

      【解决方案5】:

      这就是我们在环境中为我们的服务器所做的。

      interface "eth0"
      {
       prepend domain-name-servers 10.x.x.x;
       supersede host-name "{Hostname}";
       append domain-search "domain";
       supersede domain-name "DOMAIN";
      }
      

      希望这会有所帮助。

      【讨论】:

      • 谢谢,带有“附加域搜索”的行帮助了我。我希望将我的自定义内部域附加到堡垒主机名,以便更容易登录到堡垒主机后面的主机。
      【解决方案6】:

      以下内容在 AWS EC2 上的 Debian 延伸中工作。

      只需创建/etc/dhcp/dhclient-enter-hooks.d/nodnsupdate:

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

      然后您可以修改/etc/resolv.conf,它会在重新启动时保留您的更改。

      【讨论】:

        【解决方案7】:

        在 crontab 中设置为

        @reboot cp -r /home/.../resolv.conf /etc/resolv.conf
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-16
          • 2019-03-13
          • 2013-06-27
          • 1970-01-01
          • 1970-01-01
          • 2017-05-26
          • 2011-12-20
          相关资源
          最近更新 更多