Fix Sudo Hanging Issues on Ubuntu Linux
When the sudo command becomes unresponsive on an Ubuntu system, it is usually caused by hostname resolution errors or locked configuration files. This guide explains how to diagnose the root cause and apply immediate fixes to restore command functionality. We will cover editing host files, verifying sudoers syntax, and accessing root privileges when sudo is unavailable.
Resolve Hostname Mismatch
Sudo often hangs because it cannot resolve the system hostname quickly. You must ensure your hostname is explicitly listed in the hosts file to prevent DNS lookup delays.
- Identify your current hostname by running
hostnamein the terminal. - Gain root access via Recovery Mode if sudo is currently unresponsive.
- Open
/etc/hostswith a text editor like nano or vim. - Add a line mapping
127.0.1.1to your hostname if it is missing. - The line should look like:
127.0.1.1 your-hostname. - Save the file and reboot the system.
Check Sudoers File Syntax
Invalid entries in the sudoers configuration can cause execution delays or complete freezes.
- Run
visudo -cto check the configuration for syntax errors. - If errors are reported, run
visudoto edit the file safely. - Look for recent changes and revert them if necessary.
- Ensure no stale lock files exist in
/var/run/sudo/or/etc/sudoers.d/.
Use Recovery Mode for Access
If you cannot run commands with elevated privileges because sudo hangs, you must bypass it using the GRUB menu.
- Restart the computer and hold Shift or Esc to access the GRUB menu.
- Select Advanced options for Ubuntu.
- Choose a kernel version marked with (recovery mode).
- Select root from the menu to drop into a root shell.
- Perform necessary edits to
/etc/hostsor/etc/sudoerswithout using sudo. - Type
rebootto restart the system normally.