Commands.page Logo

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.

  1. Identify your current hostname by running hostname in the terminal.
  2. Gain root access via Recovery Mode if sudo is currently unresponsive.
  3. Open /etc/hosts with a text editor like nano or vim.
  4. Add a line mapping 127.0.1.1 to your hostname if it is missing.
  5. The line should look like: 127.0.1.1 your-hostname.
  6. Save the file and reboot the system.

Check Sudoers File Syntax

Invalid entries in the sudoers configuration can cause execution delays or complete freezes.

  1. Run visudo -c to check the configuration for syntax errors.
  2. If errors are reported, run visudo to edit the file safely.
  3. Look for recent changes and revert them if necessary.
  4. 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.

  1. Restart the computer and hold Shift or Esc to access the GRUB menu.
  2. Select Advanced options for Ubuntu.
  3. Choose a kernel version marked with (recovery mode).
  4. Select root from the menu to drop into a root shell.
  5. Perform necessary edits to /etc/hosts or /etc/sudoers without using sudo.
  6. Type reboot to restart the system normally.