87

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.

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
Nasreddine
  • 1,497
  • 3
  • 16
  • 21

4 Answers4

156

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!

  • 1
    This post I guess ? : http://serverfault.com/questions/38114/why-does-sudo-command-take-long-to-execute – monojohnny Mar 24 '16 at 00:19
  • 1
    I have the feeling that it can also be caused by DNS setting in NetworkManager: https://askubuntu.com/questions/898605/how-to-disable-systemd-resolved-and-resolve-dns-with-dnsmasq – tobias47n9e Aug 08 '18 at 08:16
  • 4
    This is still valid in ubuntu 18.04. Thanks – Alexandre Neto Aug 17 '18 at 10:25
  • @Paul Preibisch, I have the same issue in mac. How can i get the host name? – unknownerror Mar 17 '20 at 16:02
  • 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
  • 2
    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 – Simon Mattes Oct 04 '20 at 08:50
16

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.

11

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...

  • 1
    This is the best, elegant and most valuable response to the question. – Raiden Core 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 04:21
  • By far the best response – étale-cohomology Apr 08 '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. – AH.Pooladvand Nov 19 '21 at 10:49
  • best to see first log of your os, like /var/log/messages or /var/log/audit.log. I discovered that there was a call to the fingerprint daemon every time I used su. So I disabled it authconfig --disablefingerprint --update – gdm Nov 20 '21 at 08:02
4

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)

  • 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 – Vasilis Mavromatis Dec 13 '21 at 17:34