Sudo command hangs

16,731

Solution 1

Had the same problem on Ubuntu 16.04 on a newly created openstack remote vm. sudo su hanged for many seconds, maybe up to a minute, before it either worked or not. When it worked it could be made quick and reliable by running this once as root:

echo 127.0.0.1 localhost $(hostname) >> /etc/hosts

Solution 2

In my case the problem was caused by multiple sudo processes running/hanging in the background, processes like sudo chmod g+rwx -R folders_with_many_files.

What helped was

ps -aleF | grep sudo

first to inspect, later something like

ps -aleF | grep sudo | awk '{print $4}' | xargs kill -9

Solution 3

The problem can reproduced when the hostname is changed , edit your /etc/hosts by adding the output of echo $HOSTNAME after 127.0.0.1:

127.0.0.1  hostname
Share:
16,731

Related videos on Youtube

Ahmed Soliman
Author by

Ahmed Soliman

Updated on September 18, 2022

Comments

  • Ahmed Soliman
    Ahmed Soliman almost 2 years

    Every time I try to execute sudo su - or any other sudo command for this matter my server hangs, sometimes it take 10 mins to respond and sometimes it never does.

    I checked /etc/hosts file which was configured correctly. I also checked /etc/resolv.conf file which looked good as well. any suggestions?

    I am running redhat 6.9

    • Admin
      Admin over 6 years
      So,  do you have any way of becoming root?
    • Admin
      Admin over 6 years
      Yes.. Sometimes it allows me to become root
    • Admin
      Admin over 6 years
      Have you tried running sudo after you’re already root?  Does it still hang then?  If so, you might try running strace (or some similar program) on it.
    • Admin
      Admin over 6 years
      When does it hang -- before asking for the password or after? (Also, strace sudo -i or so might be your friend, even if its output is daunting, because you can see what it's trying to do when it hangs.)
    • Admin
      Admin over 6 years
    • Admin
      Admin over 6 years
      @UlrichSchwarz strace is usually a good idea and I almost suggested this myself, but since sudo has the setuid bit set, strace will (should) not be able to attach :-| @Ahmed: if you eventually manage to get root, does it also hang when you do sudo -u $someuser -i? If so, then try that with strace.
    • Admin
      Admin over 6 years
      Do you have PAM installed. If so, check the config files if you're using some kind of remote authentification (e.g., LDAP)
    • Admin
      Admin over 6 years
      On your /etc/hosts you shoud have 127.0.0.1 hostname ; hostname =echo $HOSTNAME
    • Admin
      Admin over 6 years
      @GAD3R I think that resolved my issue
  • Ahmed Soliman
    Ahmed Soliman over 6 years
    I configured it correctly, the issue was fixed but now its back again for no reason. Not sure what the issue is still :(
  • SmallChess
    SmallChess about 3 years
    Same problem. It solved for me.