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

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

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

我如何纠正这个错误?

64082 次浏览

You should enable the DNS hostnames on your VPC: http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-dns.html#vpc-dns-updating

If for some reason, you cannot enable it, you can still use the /etc/hosts to make it resolve, but this is definitely not the recommended solution

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

This issue is caused by not enabling enableDnsHostnames in your VPC configuration.

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

This worked for me:

Add the following line to /etc/hosts

127.0.0.1 ip-xxx-xx-x-xx

The ip thing is your private ip address

enableDnsHostnames as described in Michael's comment is one prerequisite. The other is having your VPC's DHCP option set configured correctly. The problem you are coming up against is caused by search line missing from your /etc/resolv.conf ; it will be put in there at the time of DHCP assignment if you set domain-name of your DHCP option set appropriately. Read the linked AWS doc.

Can patch easily from command line as follows:

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

And checked that a reboot, or stop, then start the aws instance would preserve it. In case it is lost, can easily re-apply on boot, and can be added to any provision for new vms.

Example

Before:

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)

Fix:

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

After:

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

Two Options:

  1. Enabling the DNS hostnames for your VPC, so all the instances launched within the VPC will resolve the host

  2. Edit /etc/hosts and add the below line

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

This is something you will need to do for every instance that will be launch within your VPC.

My issue was caused by an invalid DHCP Options set (in the VPC console). The default one that you typically want looks like this:

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

Somehow, my domain-name got changed to us-east-1.compute.internal, which resulted in the sudo: unable to resolve host ip-10-0-xx-xx warning every time I ran sudo. Changing back to the DHCP options above fixed it.

To enable DnsSupport attribute just use this command in your terminal

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

Make sure replace * with your VPC Id for more info https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-vpc-attribute.html

Run the following command;

sudo su -

and work as root. Then the start command will work.

Add the following line to /etc/hosts

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

The ip thing is your private ip address

Please also don't forget to reboot the instance after editing these

I recently faced the same issue even though DSN was enabled in VPC.

I reviewed EC2 instances and found that my storage usage was 99%. So I truncated log files and cleaned up.

Afterwards, I rebooted it and everything worked fine.

I've had a similar case to @bhavesh-odedra's. My storage usage was 100%, I extended it to 5Gb more, restarted instance with sudo reboot and after that I started getting this error.

I checked disk usage with sudo df -h and found out that my Ubuntu system is not aware of extended storage. Rebooting again did not help.

I checked answers on this page. DNS hostname was Enabled in AWS VPC configs. Then I added private IP to /etc/hosts - it did not help too.

After some time and I decided to reboot instance using AWS console (not sudo reboot). I don't know whether AWS console informed my system that new storage is ready for use or something else happened, but then with sudo df -h I've convinced that my new storage space was in use. After that the error disappeared.