79

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?

3
  • 1
    Use the IP address, i.e., 10.0.xx.xx (where you have replaced the xxs with digits, of course). ip-10-0-xx-xx is not the actual IP or hostname.
    – elixenide
    Oct 30, 2015 at 17:42
  • 1
    @EdCottrell, I am not doing any operations with the ip. I am doing a sudo apt-get update and I still get that error. Whenever I need to access the ip, I use the public ip which is different than the private ip(ip-10-0-xx-xx)
    – Rahul
    Oct 30, 2015 at 17:47
  • 1
    Ah, that's important information. You should add that to your question; I thought you meant a sudo command referencing ip-10-0-xx-xx.
    – elixenide
    Oct 30, 2015 at 17:48

12 Answers 12

86

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

13
  • 1
    I feel this is a better answer as it solve the problem for all machines on the VPC, not just one at a time.
    – SunSparc
    Mar 28, 2016 at 18:15
  • 11
    It doesn't solve a problem for me. Though enableDnsHostnames and enableDnsSupport attributes are set to True.
    – kikulikov
    Sep 22, 2016 at 11:05
  • 1
    @cyrillk did you change to this or was it already set that way? If you changed it, you may now need to reboot the instance. If not, then are you using custom name servers in your VPC's DHCP DNS settings? Sep 22, 2016 at 15:06
  • 1
    @Michael-sqlbot no, I didn't change it. It has been set to True already. My server names look like ip-172-16-13-42 which is default hostname afaik.
    – kikulikov
    Sep 27, 2016 at 16:35
  • 2
    Just ran into this issue too. I was using a 'Custom VPC' which defaults to DNS Hostnames and DNS Resolution setting turned off. In order to fix issue, turn on DNS Hostnames and DNS Resolution in the VPC, and reboot the instance through the AWS CLI. Note that 'rebooting the instance' from the guest O/S does not seem to allow the DNS setting to propogate from the hypervisor to the guest.
    – user7376755
    Jan 19, 2017 at 16:51
55

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
1
  • 1
    Though its enabled I still see this problem.
    – Satyam
    Apr 28, 2022 at 3:44
23

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

17

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

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.

2
  • In my case, problem is with the DHCP option set configuration which I changed recently. Thanks for giving the hint!
    – Vignesh
    Jun 30, 2017 at 16:36
  • In my case, the VPC was configured correctly, but it was linked to bad DHCP settings. I needed to use ec2.internal as the domain name for my DHCP settintg, and then all was resolved. Aug 5, 2017 at 20:16
8

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.

7

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.

1
  • use sudo apt-get -y autoremove to get rid of old linux-aws-header files in /usr/src/
    – enharmonic
    Mar 22 at 22:29
3

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

1

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.

1

Run the following command;

sudo su -

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

1

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.

1
  • Same here. In my case, I couldn't even write the additional line to /etc/hosts because the disk was completely full.
    – nofinator
    Dec 21, 2022 at 19:20
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.