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

Amazon Web-ServicesSudo

Amazon Web-Services Problem Overview


I launched a new aws instance. My private ip is ip-10-0-xx-xx as per amazon console. Everytime when I do a sudo command, I get the following error

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

How can I rectify this error?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

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

Solution 2 - Amazon Web-Services

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

Solution 3 - Amazon Web-Services

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

Solution 4 - Amazon Web-Services

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:~$

Solution 5 - Amazon Web-Services

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.

Solution 6 - Amazon Web-Services

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.

Solution 7 - Amazon Web-Services

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

Solution 8 - Amazon Web-Services

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.

Solution 9 - Amazon Web-Services

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

Solution 10 - Amazon Web-Services

Run the following command;

sudo su -

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahulView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesMichael - sqlbotView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesloopingzView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesSamAkoView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesarntgView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesMaheshView Answer on Stackoverflow
Solution 6 - Amazon Web-Servicesm.kocikowskiView Answer on Stackoverflow
Solution 7 - Amazon Web-ServicesvishnunView Answer on Stackoverflow
Solution 8 - Amazon Web-ServicesYevgeniy BrikmanView Answer on Stackoverflow
Solution 9 - Amazon Web-ServicesSai VankinaView Answer on Stackoverflow
Solution 10 - Amazon Web-ServicesNoy TsarfatyView Answer on Stackoverflow