103

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
  • 20,125
  • 57
  • 82
  • 84
Nasreddine
  • 1,687
  • 4
  • 17
  • 23

6 Answers6

184

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
  • @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
  • 3
    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
  • Even worked for me in Ubuntu 24.04 – NaN Apr 30 '24 at 17:21
  • @panzi it's annoying, but what do you do for a living? how many times are you typing sudo each day and why is your cache period 0? wild claim – Ed Swangren Aug 13 '24 at 13:17
  • docker(-compose) needs sudo. While working on docker setups this an issue. Although 4 years later the issue is solved of course. Can't remember how it was solved. Also your tone makes me think you're not interested in helping anyone, just here to be condescending. – panzi Aug 14 '24 at 14:17
  • 10 years from original answer. in 2025 i have installed a system with name worker_1 and it faces same issue.. Becuse host is worker1 and in file it worker_1 – Vetal.lebed Sep 03 '25 at 21:43
23

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.

14

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
7

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
0

I've been battling this issue for weeks, trying all sort of solutions found online which mostly pointed at the /etc/hosts file, without success.

Yesterday I decided to go through this again and, whilst at it, disable all my unnecessary services (using Debian here) and disabled "winbind" and "smbd", which in my case had no use.

this problem is now GONE.

Steps:

    sudo service --status-all
[...]
 [ + ]  smbd
 [ + ]  winbind
[...]
    $ sudo systemctl disable winbind smbd
    $ sudo service winbind status

○ winbind.service - Samba Winbind Daemon
     Loaded: loaded (/usr/lib/systemd/system/winbind.service; disabled; preset: enabled)
     Active: inactive (dead)
       Docs: man:winbindd(8)
             man:samba(7)
             man:smb.conf(5)


$ sudo service --status-all

[...] [ - ] smbd [ - ] winbind [...]

I hope this helps!

Jc N
  • 1
  • 1
0

For those who are still facing the problem of slow sudo, try commenting out your /etc/krb5.conf file.

Herox
  • 1