AWS 错误 - sudo:无法解析主机 ip-10-0-xx-xx

AWS error - sudo: unable to resolve host ip-10-0-xx-xx

我启动了一个新的 aws 实例。根据亚马逊控制台,我的私人 IP 是 ip-10-0-xx-xx。每次执行 sudo 命令时,都会收到以下错误

sudo: unable to resolve host ip-10-0-xx-xx

我该如何纠正这个错误?

您应该在您的 VPC 上启用 DNS 主机名http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-dns.html#vpc-dns-updating

如果由于某种原因无法启用,您仍然可以使用/etc/hosts来解决,但这绝对不是推荐的解决方案

10.0.xx.xx ip-10-0-xx-xx

此问题是由于未在您的 VPC 配置中启用 enableDnsHostnames 引起的。

enableDnsHostnames

Indicates whether the instances launched in the VPC get DNS hostnames. If this attribute is true, instances in the VPC get DNS hostnames; otherwise, they do not. If you want your instances to get DNS hostnames, you must also set the enableDnsSupport attribute to true.

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

这对我有用:

将以下行添加到 /etc/hosts

127.0.0.1 ip-xxx-xx-x-xx

ip 是你的私人 ip 地址

enableDnsHostnames 如 Michael 的评论所述是一个先决条件。另一个是您的 VPC DHCP option set configured correctly。您遇到的问题是由 /etc/resolv.conf 中缺少 search 行引起的;如果您适当地设置了 DHCP 选项的 domain-name,它将在分配 DHCP 时被放在那里。阅读链接的 AWS 文档。

可以从命令行轻松打补丁,如下所示:

sudo sed -i /etc/hosts -e "s/^127.0.0.1 localhost$/127.0.0.1 localhost $(hostname)/"

并检查重启或停止,然后启动 aws 实例是否会保留它。 万一它丢失了,可以在启动时轻松重新应用,并且可以添加到新 vms 的任何配置中。

例子

之前:

ubuntu@ip-177-11-22-333:~$ sudo id
sudo: unable to resolve host ip-177-11-22-333
uid=0(root) gid=0(root) groups=0(root)

修复:

ubuntu@ip-177-11-22-333:~$ sudo sed -i /etc/hosts -e "s/^127.0.0.1 localhost$/127.0.0.1 localhost $(hostname)/"
sudo: unable to resolve host ip-177-11-22-333

之后:

ubuntu@ip-177-11-22-333:~$ sudo id
uid=0(root) gid=0(root) groups=0(root)
ubuntu@ip-177-11-22-333:~$

两个选项:

  1. 为您的 VPC 启用 DNS 主机名,因此在 VPC 内启动的所有实例都将解析主机

  2. 编辑 /etc/hosts 并添加以下行

    127.0.0.1 localhost    
    123.0.0.1 ip-10-0-1-18 ## (Replace with the private ip)
    

这是您需要为将在您的 VPC 中启动的每个实例执行的操作。

我的问题是由无效的 DHCP 选项集(在 VPC 控制台中)引起的。您通常需要的默认值如下所示:

domain-name = ec2.internal
domain-name-servers = AmazonProvidedDNS

不知何故,我的 domain-name 变成了 us-east-1.compute.internal,这导致我每次 运行 sudo 时都会收到 sudo: unable to resolve host ip-10-0-xx-xx 警告。改回上面的 DHCP 选项修复它。

要启用 DnsSupport 属性,只需在您的终端中使用此命令

aws ec2 describe-vpc-attribute --vpc-id vpc-****** --attribute enableDnsSupport

确保将 * 替换为您的 VPC Id 了解更多信息 https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-vpc-attribute.html

运行以下命令;

sudo su -

并以 root 身份工作。然后启动命令将起作用。

将以下行添加到 /etc/hosts

127.0.0.1 localhost
127.0.0.1 ip-xxx-xx-x-xx

ip 是你的私人 ip 地址

请不要忘记在编辑这些后重启实例