Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have this issue on AWS on some servers. Whenever I run sudo the terminal is stuck doing seemingly nothing, until it finally spits out this error message. My terminal looks like this:

ubuntu@(none):~$ sudo true
sudo: unable to resolve host (none)

What can I do to solve it?

share|improve this question
    
Please post the contents of /etc/hostname and /etc/hosts. –  arrange Aug 31 '11 at 20:24
11  
I recommend against closing this question as too localized. There are many users who may mistakenly think they've put one name in their hosts file but put in a different name instead, especially since on many networks, computers are similarly named. This question (and answer) would show up when someone searches with that problem, and the answer would prompt them to check for such discrepancies, even though the exact misspelling would be different. –  Eliah Kagan Aug 18 '12 at 11:09
2  
make sure your hostname same with hosts. e.g. the hostname is ubuntu-pc and hosts is ubuntu-pc must be same. –  Muhammad Sholihin Apr 1 '13 at 8:17

10 Answers 10

up vote 103 down vote accepted

Two things to check (assuming your machine is called my-machine, you can change this as appropriate):

  1. That the /etc/hostname file contains just the name of the machine.

  2. That /etc/hosts has an entry for localhost. It should have something like:

     127.0.0.1    localhost.localdomain localhost
     127.0.1.1    my-machine
    

If either of these files aren't correct (since you can't sudo), you may have to reboot the machine into recovery mode and make the modifications, then reboot to your usual environment.

share|improve this answer
1  
The hostname will not change until you reboot. If you wish to change it without rebooting the machine then follow the above steps and after that run:- "sudo hostname my-machine" to see if this has worked run "sudo hostname" It will show your machine's host name. This method maybe used as a temporary method to change hostname also. after a restart, the value from the /etc/hostname file is used. –  Yash May 5 '13 at 15:22

Edit /etc/hosts and append your new hostname to the 127.0.0.1 line (or create a new line if you prefer that).

Mine looks like:

127.0.0.1       localhost localhost.localdomain penguin

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Replace penguin in the above example by your new hostname as stated in the /etc/hostname file.

share|improve this answer
    
Thanks, it works! –  Harris6310 Apr 7 '12 at 13:56
6  
don't do this, 192.168.2.2 is probably a DHCP-assigned external IP, this will eventually lead to confusion. Instead just add the hostname after "localhost": 127.0.0.1 localhost penguin –  pascal May 1 at 14:52
1  
@pascal In my case it is a static IP, but yeah, for DHCP addresses it would make less sense. –  Lekensteyn May 1 at 16:16
    
@VolkerSiegel Fixed, I probably wanted to clarify that you can have multiple lines having a host. –  Lekensteyn Oct 2 at 22:26

Note, this is an answer to this question which has been merged with this one.

Your hostname (dave00-G31M-ES2L) is not represented in /etc/hosts. Add an L to this line:

127.0.1.1   dave00-G31M-ES2

So it becomes:

127.0.1.1   dave00-G31M-ES2L

In order to accomplish this, open a console (press Ctrl+Alt+T) and type:

sudo gedit /etc/hosts

Add the letter L as mentioned, save and exit.

share|improve this answer
    
Remember! Use sudoedit (or sudo -e). To specify preferred editor, use the EDITOR environment variable (eg. export EDITOR=vim) as it creates an offline copy for editing and then cleanly overwrites after editing. –  Jan Sep 26 at 13:54

I had this issue when I was using ubuntu on a VPS. I solved it editing /etc/hosts file.

run this command:

sudo nano /etc/hosts

and then add:

127.0.0.1   localhost.localdomain localhost
127.0.1.1   ubuntu

I hope that will solve your issue :)

PS: Remember to reboot your computer!

share|improve this answer
    
Also, see if your device name (printed on the Terminal title bar after the @ sign) matches the name on the second line of the hosts file ("ubuntu" in Luca's example). The first line may also be just "localhost". –  Waldir Leoncio Oct 26 '13 at 14:33
    
Remember! Use sudoedit (or sudo -e). To specify preferred editor, use the EDITOR environment variable (eg. export EDITOR=vim) as it creates an offline copy for editing and then cleanly overwrites after editing. –  Jan Sep 26 at 13:55

Sorry I can't help you much but, since it says "can't resolve host" try running:

hostname

And see if the output is the hostname of the machine. If not, the problem is the host configuration, not sudo.

share|improve this answer

OP wrote:

It was all in /etc/hostname. On two of our sick servers it looked like this:

ubuntu@(none):~$ cat /etc/hostname
linux-web-n ip-10-128-##-##

While on a server without this issue we had:

ubuntu@ip-10-128-##-###:~$ cat /etc/hostname
ip-10-128-##-###

Removed the linux-web-n portion, rebooted and everything was fine.

share|improve this answer

I was having the same issue even though the hostname in my /etc/hostname file and /etc/hosts file matched.

My hostname was "staging_1". It turns out that you can't have an underscore in your hostname, which is why I was getting this error. Changing the underscore to a hyphen fixed my problem.

share|improve this answer

In my case it was the problem, I changed the hostname to man because I wanted to know if there are some parameters you can use on hostname. Instead it changed my hostname to man and I always got the same message like you

sudo: unable to resolve host (none)

after changing the hostname back to `localhost everything worked fine again

hostname localhost
share|improve this answer

Add your hostname to /etc/hosts like so:

echo $(hostname -I | cut -d\  -f1) $(hostname) | sudo tee -a /etc/hosts
share|improve this answer

First you have to edit the hostname in /etc/hostname file.

Then you have to edit the hostname in /etc/hosts file.

If you type different hostnames in /etc/hostname and /etc/hosts then you will get the error like unable to resolve host.

So you can enter same hostname into the /etc/hostname and /etc/hosts.

After edited, just start the services for hostname sudo service hostname start, then if you check hostname command it will give the new name.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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