4

I am getting the following error when running a sudo command on Ubuntu 14 server on EC2/VPC on Amazon Web Services.

sudo: unable to resolve host ip-xxx-xx-x-xx 

I replaces the private IP address of the server with x's.

In order to solve this, I need to edit /etc/hosts file. When I add the following line to the hosts file:

127.0.0.1 ip-xxx-xx-x-xx

Now I don't get the error. The value after the 127.0.0.1 resolves to the hostname command value. The problem is that I might create an AMI and restore the server in the future, so the private IP will change. In AWS forum, they recommended to add a script that automatically update the hosts file with the new IP (the hostname value) so it will work when restoring a server from AMI or when autoscaling.

How can I update the hosts file with the hostname value, so I won't need to worry about any future issues?

Liron Harel
  • 10,819
  • 26
  • 118
  • 217
  • There should be no need to update the hosts file, if you configure your VPC to resolve the names, [as discussed here](http://stackoverflow.com/a/27747125/1695906). – Michael - sqlbot Dec 21 '15 at 12:43
  • Possible duplicate of [Cloud Platforms- sudo: unable to resolve host](http://stackoverflow.com/questions/27741098/cloud-platforms-sudo-unable-to-resolve-host) – Michael - sqlbot Dec 21 '15 at 12:44

1 Answers1

5

If you, for some reason, cannot enable DNS hostnames on your VPC, then you should do exactly what was suggested in the AWS forum. Write a script to automatically change the /etc/hosts file. Example:

#!/bin/bash
LOCAL_HOSTNAME=$(curl http://169.254.169.254/latest/meta-data/local-hostname)
cat << EOF >> /etc/hosts
127.0.0.1 $LOCAL_HOSTNAME
EOF

NOTE: This will overwrite the entire hosts file, so make sure you write all you need in there.

According to the AWS documentation, local-hostname will return something like ip-10-251-50-12.ec2.internal. If that's not exactly what you want, check the other available metadata in the documentation beforementioned.

Suppose you don't want the .ec2.internal part, you can customize the script to get rid of that part using cut. Example:

curl http://169.254.169.254/latest/meta-data/local-hostname | cut -d '.' -f1

Run on startup

There are a few alternatives to make it run on startup. I recommend you to call it from rc.local (see here and here). Don't forget to give your script the execute permission.

Community
  • 1
  • 1
jweyrich
  • 31,198
  • 5
  • 66
  • 97
  • a few noob questions: What is the ip in the curl, where I write the script and what extension file is it? – Liron Harel Dec 21 '15 at 11:54
  • 1
    1) That IP is used by AWS (and possible other cloud providers) to distribute metadata to their instances. Refer to the [AWS documentation](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for more informations; 2) You should write the script in the root home directory (/root generally). I'd name it something like `rewrite-etc-hosts.sh`, then give it execute permission: `chmod u+x /root/rewrite-etc-hosts.sh`. – jweyrich Dec 21 '15 at 12:02
  • Thanks a lot, I'll follow your guidance. – Liron Harel Dec 21 '15 at 12:03
  • UPDATE: Typo error, I am checking again, should work now. – Liron Harel Dec 21 '15 at 12:34