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?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityTwo things to check (assuming your machine is called my-machine
, you can change this as appropriate):
That the /etc/hostname
file contains just the name of the machine.
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.
#%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.
::1 localhost
to /etc/hosts
(this is the IPv6 version of 127.0.0.1, aka the loopback address)
Sep 27, 2017 at 14:51
127.0.0.1 localhost
but 127.0.1.1 my-machine
?
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.
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.
Mar 29, 2015 at 23:31
touch myscript
step 2. edit "myscript" (you can do this without sudo most of the time) and insert sed -i 's/127.0.0.1 localhost/127.0.0.1 localhost NEWHOSTHERE/' /etc/hosts
step 3. sudo bash myscript
. Please note the tabs are there on purpose because it searches for 127.0.0.1 localhost
to be replaced with 127.0.0.1 localhost NEWHOSTHERE
.
Add your hostname to /etc/hosts
like so:
echo $(hostname -I | cut -d\ -f1) $(hostname) | sudo tee -a /etc/hosts
echo $(hostname -I | cut -d\ -f1) $(hostname) | sudo -h 127.0.0.1 tee -a /etc/hosts
Jan 7, 2019 at 11:31
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.
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.
sudo
when there is no longer sudo
. sudo
doesn't work, sir. sudo: unable to resolve host ...
sudo
? The error message you mention comes from the sudo
command. Perhaps you meant something different?
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).
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!
hosts
file ("ubuntu" in Luca's example). The first line may also be just "localhost".
Oct 26, 2013 at 14:33
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.
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".
sudo --host 127.0.0.1 vi /etc/hosts
, as suggested in other answers.
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 127.0.0.1'" >> ~/.bash_aliases
source ~/.bashrc
Aliases don't work in bash scripts, but we can use variables: sudo='sudo -h 127.0.0.1'
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.
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:
hostname
And see if the output is the hostname of the machine. If not, the problem is the host configuration, not sudo.
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.
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.
sudo
. root
is its own account, which doesn't have a password set by default.
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:
127.0.1.1 Megabyte Megabyte
127.0.0.1 localhost
To this:
127.0.1.1 Debian Debian
127.0.0.1 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
127.0.0.1 localhost
127.0.1.1 localhost myhostname
#vi /etc/hostname
myhostname
/etc/hosts
without sudo
. sudo
doesn't work sudo: unable to resolve host ...
If you are using Vagrant, then login into the guest and run
apt-get --no-install-recommends install virtualbox-guest-utils
/etc/hostname
and/etc/hosts
.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.hostname
same withhosts
. e.g. the hostname is ubuntu-pc and hosts is ubuntu-pc must be same.sudo /etc/init.d/network-manager restart
. However, I'm wondering why in the first placesudo
wastes time waiting for network-related stuff. Shouldn'tsudo
work without problems when network is not available?