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?
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?
Two 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 forlocalhost
. It should have something like: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.
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:
Replace
penguin
in the above example by your new hostname as stated in the/etc/hostname
file.Add your hostname to
/etc/hosts
like so: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 anL
to this line:So it becomes:
In order to accomplish this, open a console (press Ctrl+Alt+T) and type:
Add the letter
L
as mentioned, save and exit.I had this issue when I was using ubuntu on a VPS. I solved it editing /etc/hosts file.
run this command:
and then add:
I hope that will solve your issue :)
PS: Remember to reboot your computer!
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".
The symptom given in the question may correlate strongly with this more specific problem:
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: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."
https://forums.aws.amazon.com/thread.jspa?messageID=699718