title: Dynamically updating hosts file with new hostname for EC2 VPC instance
body: <p>I am getting the following error when running a <code>sudo</code> command on Ubuntu 14 server on EC2/VPC on Amazon Web Services.</p>
  
  <pre><code>sudo: unable to resolve host ip-xxx-xx-x-xx 
  </code></pre>
  
  <p>I replaces the private IP address of the server with x's. </p>
  
  <p>In order to solve this, I need to edit <code>/etc/hosts</code> file. When I add the following line to the hosts file:</p>
  
  <pre><code>127.0.0.1 ip-xxx-xx-x-xx
  </code></pre>
  
  <p>Now I don't get the error. The value after the 127.0.0.1 resolves to the <code>hostname</code> 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 <a href="https://forums.aws.amazon.com/message.jspa?messageID=545056" rel="nofollow">AWS forum</a>, they recommended to add a script that automatically update the hosts file with the new IP (the <code>hostname</code> value) so it will work when restoring a server from AMI or when autoscaling.</p>
  
  <p>How can I update the hosts file with the hostname value, so I won't need to worry about any future issues?</p>