When I run sudo the terminal is stuck for a few seconds and then outputs an error message. My terminal looks like this:

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

What can I do to solve it?

  • 12
    Please post the contents of /etc/hostname and /etc/hosts.
    – arrange
    Commented Aug 31, 2011 at 20:24
  • 61
    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. Commented Aug 18, 2012 at 11:09
  • 15
    make sure your hostname same with hosts. e.g. the hostname is ubuntu-pc and hosts is ubuntu-pc must be same. Commented Apr 1, 2013 at 8:17
  • 1
    I ran into this today. The problem was that what I had in hostname wasn't in /etc/hosts. To wit: $ hostname => 'mybox' $ grep 'mybox' /etc/hosts => mybox.example.com. I needed to add 'mybox' after my domain name in /etc/hosts => mybox.example.com mybox
    – Jim
    Commented Mar 25, 2017 at 21:16
  • 4
    I can't post an answer because this question is protected and I don't have enough reputation here. In my case, I solved the problem by restarting network-manager: sudo /etc/init.d/network-manager restart. However, I'm wondering why in the first place sudo wastes time waiting for network-related stuff. Shouldn't sudo work without problems when network is not available?
    – bli
    Commented Jul 31, 2017 at 8:45

20 Answers 20


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:    localhost.localdomain localhost    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.

  • 46
    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.
    – Yashvit
    Commented May 5, 2013 at 15:22
  • 2
    Note: since you can't sudo to begin with, it is difficult to edit those files. My solution was I was somehow able to sudo visudo and change #%admin ALL=(ALL) ALL to %admin ALL=NOPASSWD: ALL , then reboot, and sudo su -, edit those files, set/correct hostname, reboot again, and everything worked.
    – Ian M
    Commented Jul 17, 2016 at 20:19
  • 4
    I'm using Linux Subsystem in Windows and I faced this problem. After following your answer, It has been resolved. Commented Dec 28, 2016 at 15:43
  • 3
    you may also need to add ::1 localhost to /etc/hosts (this is the IPv6 version of, aka the loopback address) Commented Sep 27, 2017 at 14:51
  • 22
    Why does your example have localhost but my-machine?
    – Adam
    Commented Dec 21, 2017 at 23:11

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

Mine looks like:       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.

  • 4
    How can he edit the /etc/hosts file if he can't sudo? Unless he created a rood account with a password (bad idea)
    – Dennis
    Commented Mar 29, 2015 at 22:31
  • 12
    @Dennis You can still execute sudo even if that message is displayed. IIRC you still have to enter your password at each invocation though. If this does not work, you can reboot into the recovery console and apply the changes. A root account with password is discouraged.
    – Lekensteyn
    Commented Mar 29, 2015 at 23:31
  • helpful answer.
    – Bonn
    Commented Jun 27, 2020 at 7:25
  • as a debian user, there is no gksudo or gksu, i had to resort to using sed: step 1. run touch myscript step 2. edit "myscript" (you can do this without sudo most of the time) and insert sed -i 's/ localhost/ localhost NEWHOSTHERE/' /etc/hosts step 3. sudo bash myscript. Please note the tabs are there on purpose because it searches for localhost to be replaced with localhost NEWHOSTHERE.
    – petrosmm
    Commented Aug 27, 2020 at 20:35

Add your hostname to /etc/hosts like so:

echo $(hostname -I | cut -d\  -f1) $(hostname) | sudo tee -a /etc/hosts
  • 9
    It's clever, but I don't think it tends to be a good idea to automate something like this.
    – mwfearnley
    Commented Jan 4, 2018 at 13:59
  • 1
    This does an append of the hostname to the hosts file. There is an inherent assumption that there is nothing in between the loopback ip the mapping to localhost and end of file, but there is now some IPv6 stuff between this line and end of file, in which case this solution does not really end up giving you what you would want. A related comment: Editing this or other files requires use of sudo and it is sudo that we are trying to fix. We still need to be able to run sudo. In this case sudo -h hostname can be used to first change permissions on the files or gain the elevation to edit them. Commented Sep 21, 2018 at 21:05
  • 3
    this fails because I can't "sudo tee.." but this did the trick: echo $(hostname -I | cut -d\ -f1) $(hostname) | sudo -h tee -a /etc/hosts Commented Jan 7, 2019 at 11:31
  • working completely fine Commented Feb 12, 2023 at 14:45

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:   dave00-G31M-ES2

So it becomes:   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.

  • 11
    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
    Commented Sep 26, 2014 at 13:54
  • 2
    And there is another one here who suggest sudo when there is no longer sudo. sudo doesn't work, sir. sudo: unable to resolve host ...
    – Green
    Commented Jul 12, 2016 at 20:18
  • @Green: No sudo? The error message you mention comes from the sudo command. Perhaps you meant something different?
    – Thor
    Commented Jul 14, 2016 at 13:49
  • @Green sudo works just fine. It just can't store any state (i.e. as Lekensteyn said elsewhere you have to enter your password every time).
    – Wlerin
    Commented Jul 28, 2016 at 19:16
  • 4
    If you have this issue on W10's Bash and came to this question from google, this is the answer that worked for me. I changed the to look like " localhost DESKTOP-SLQK4CV" (by doing "sudo vim /etc/hosts" (quick tip for vim newbies: press i before typing to switch to insert mode, press esc to exit that, write ":wq" to save and exit or ":q!" to exit without saving), in my case sudo worked but just said that it can't connect to DESKTOP-SLQK4CV) and it started worked for me.
    – ave
    Commented Aug 5, 2016 at 2:13

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:   localhost.localdomain localhost   ubuntu

I hope that will solve your issue :)

PS: Remember to reboot your computer!

  • 2
    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". Commented Oct 26, 2013 at 14:33
  • 2
    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
    Commented Sep 26, 2014 at 13:55
  • Isn't this answer essentially the same as this older one? Commented Jun 29, 2020 at 10:20

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.


In AWS, go to your vpc and turn on "DNS Hostnames".

  • 2
    Welcome to askubuntu! Can you expand a bit on this? It's not abundantly clear what you mean (at least to me)..
    – Elder Geek
    Commented Jan 15, 2015 at 13:56
  • 2
    This was the answer that helped me. Amazon AWS changed since the last time I looked at it. VPCs have DNS options, and they need to be turned on before any DNS resolution will work. Commented Jul 1, 2015 at 1:16
  • 1
    the Enable DNS Hostnames option can be found (for example) in the right-click menu of the vpc entry Commented Jan 3, 2016 at 17:32
  • There is no DNS Hostnames
    – Green
    Commented Jul 12, 2016 at 20:11
  • Similar issue occurred on Azure for me when I changed the default DNS servers in the VNet. I fixed it by adding my current hostname to the hosts file using sudo --host vi /etc/hosts, as suggested in other answers. Commented Sep 4, 2022 at 9:28

The symptom given in the question may correlate strongly with this more specific problem:

$ hostname --fqdn
hostname: Temporary failure in name resolution

There are different ways that this could be resolved, one of which is to add your hostname as localhost in /etc/hosts (as shown in several other answers). This may be the right thing to do in general, but it isn't the only possible resolution.

A "fully qualified domain name" may be supplied by an external DNS server or similar (if such is available on your network). In this case, sudo will not complain, despite the missing entry in /etc/hosts.

Note: sudo attempts to dereference the hostname, even though it isn't necessarily required, due to optional capabilities in the sudoers file. See sudo command trying to search for hostname.

As long as the delay isn't too long, this error message is typically harmless.


Everybody advises to modify /etc/hosts. But in some cases this may not be possible (for example inside a docker container). So, I had to find a better way and I came up with this:

echo "alias sudo='sudo -h'" >> ~/.bash_aliases
source ~/.bashrc

Aliases don't work in bash scripts, but we can use variables: sudo='sudo -h'


I encountered this same error message. I think this discussion thread at AWS Developer Forums is a better solution:

"Go the the VPC management console, select the VPC, click on Actions, select Edit DNS Hostnames and select Yes."



Some terminal emulators will not update prompt with the correct hostname until you close and restart the emulator (lxterminal, I'm talking to you).

I spent 30min fighting with this error after editing my hostname and hosts files and running sudo service hostname restart until I ran sudo hostname and saw that the hostname was the new value, even though the prompt was showning the old value.

  • 1
    The issue isn't the terminal emulators, it's the shells that have cached the value. Commented Feb 16, 2017 at 19:53

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

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


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

  • I did that got the hostname of my machine. I also have entry in /etc/hosts. I'm still getting the error.
    – CKM
    Commented May 6, 2017 at 13:22
  • Did you reboot the machine after changing the hosts file? Commented May 7, 2017 at 18:52
  • No. Though my IT team said there was issue with lock file as well as gpg key with puppetlabs. Now, it got resolved without restart..
    – CKM
    Commented May 8, 2017 at 9:28

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

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


you might be getting an error if your hosts or hostname file contain illegal characters. Only these symbols are permitted: a-z, A-Z, 0-9


if you can't sudo you CAN log in as root via su. IE: su root (in an x-term). then give the root password when prompted, then you can edit the files with nano. The root password in 'buntu is the same as the password you would use for sudo.

  • 5
    The root password in Ubuntu is not the same password for sudo. root is its own account, which doesn't have a password set by default. Commented Aug 24, 2016 at 19:38

I had this same problem! I changed my VPS's name through the online admin control panel which did not change the machine name in the hosts file All I did was run:

sudo nano /etc/hosts

Then I edited it from this: Megabyte Megabyte localhost

To this: Debian Debian localhost

and that fixed my error! Hope this helped!


In case your problem is that /etc/hostname file and /etc/hosts, both files have your desired hostname and still your machine is showing the error

sudo: unable to resolve host

Try, forcing the hostname

sudo hostname -F /etc/hostname

You will probably still get the same error, but try logging out and logging back in. It worked for me.


I had the same problem. I solved it by editing the /etc/hosts and /etc/hostname files... on the /etc/hosts file, just edit the top part as shown below.

#vi /etc/hosts   localhost   localhost  myhostname

#vi /etc/hostname
  • localhost myhostname or myhostname? Commented Mar 26, 2016 at 18:43
  • 2
    How can you edit /etc/hosts without sudo. sudo doesn't work sudo: unable to resolve host ...
    – Green
    Commented Jul 12, 2016 at 20:02

If you are using Vagrant, then login into the guest and run apt-get --no-install-recommends install virtualbox-guest-utils

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .