63

I noticed that the terminal recently becomes too slow when I execute a command that needs my password. It takes some seconds to display [sudo] password for ...

I'm using Dell XPS developer edition (i7,8G RAM) with Ubuntu 13.04 64bit.

3
  • Have you tried resetting your terminal from the options displayed at the top of your terminal box when displayed? I had a similar issue with this and corrected the slow down once I selected 'Reset' on the 'Terminal' option list.
    – daslinkard
    Jul 22 '13 at 2:17
  • 1
    It's very strange, but, I receive "Ubuntu could not resolve the host" when connection isn't available. I execute this command "echo 0 | sudo tee /sys/class/backlight/intel_backlight/brightness "
    – Nasreddine
    Jul 25 '13 at 4:30
  • 2
113

Hi I found this answer on another question - The problem is if your hostname is not in your hosts file.

basically, type "hostname" in your terminal. That will tell you what your hostname is.

Next, type:

sudo nano /etc/hosts

and add:

127.0.0.1 yourhostname

then save - and you are done! Sudo should be fast now!

6
  • This post I guess ? : serverfault.com/questions/38114/…
    – monojohnny
    Mar 24 '16 at 0:19
  • 1
    I have the feeling that it can also be caused by DNS setting in NetworkManager: askubuntu.com/questions/898605/… Aug 8 '18 at 8:16
  • 4
    This is still valid in ubuntu 18.04. Thanks Aug 17 '18 at 10:25
  • 1
    Can there be another reason? Because I do have my hostname in /etc/hosts and it takes 25 seconds for the password prompt to appear. Makes productive work impossible.
    – panzi
    Mar 24 '20 at 17:14
  • 1
    Works like a charm -- in my case, I had changed the name of my machine and did not modify the name in the hosts file Oct 4 '20 at 8:50
14

When you change your systems name in Gnome (The part that is displayed in the terminal after the @; e.g. tobias@laptop to tobias@newlaptop you might need to update your /etc/hosts:

127.0.1.1 laptop

needs to be changed to

127.0.1.1 newlaptop

If you get it right sudo should work without delay immediately after saving this setting.

6

Answer 1
Confirmed @Paul Preibisch answer for those who want more detailed answer

I had this issue for a long time and all I did was to run

hostnamectl | grep -i "static hostname"

this will show you your hostname then copy the value and edit your hosts

sudo vim /etc/hosts

and add 127.0.0.1 yourHostName to it
also in some distros 127.0.1.1 yourHostName should be replaced


Answer 2
Please note that in many cases the answer 1 will solve your problem if it didn't you have to check your sudo log which in debian based distros is under
/var/log/auth.log
so you can watch your sudo log with tail command
sudo tail -f -n 100 /var/log/auth.log
then open another terminal and run a sudo command like:
sudo ls /
go back to your first terminal and read the log, in my case the problem was due to pam_krb5 authentication failure the log was:
sudo: pam_krb5(sudo:auth): authentication failure;
after I removed it sudo command worked instantly...

Thanks to @gdm for giving the clue...

6
  • 1
    This is the best, elegant and most valuable response to the question. Aug 30 '20 at 23:06
  • Running hostnamectl, I get "Failed to query system properties: Connection timed out". The hostname returned by hostname already has a 127.0.0.1 entry in /etc/hosts, but the problem persists.
    – appas
    Sep 16 '20 at 4:21
  • By far the best response Apr 8 '21 at 17:43
  • it not works in centos, or rather this is not the only cause
    – gdm
    Nov 17 '21 at 11:01
  • @gdm probably! I had same issue like you but I couldn't figure it out so finally I reinstalled my os. Nov 19 '21 at 10:49
0

For the lazy - Just copy paste this in your terminal :)

echo -e '127.0.0.1\t' $(hostnamectl | grep -i "static hostname:" | cut -f2- -d:) | sudo tee -a /etc/hosts

sudo should be fast after you run this

Edit - Explaining this command in more detail:

  • It first grabs your localhost cutting the label string ahead (hostnamectl | grep -i "static hostname:" | cut -f2- -d:) e.g. mylocalhost

  • It concatenates it with 127.0.0.1 ahead ("\t" means tab character)

  • It take the full string created above (127.0.0.1 mylocalhost) and adds it to the end of /etc/hosts (you need sudo to edit the hosts file)

2
  • Could you please elaborate? Would be nice to explain what that line does - it’s always a bad idea to copy text into the terminal without understanding what it’s doing. Thank you.
    – Will
    Dec 12 '21 at 19:05
  • @Will I just edited my comment to add more detail Dec 13 '21 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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