dotlinux blog

How to Enable and Disable Root Login in Ubuntu

In the Linux ecosystem, the root user (also known as the superuser) is the most powerful account, with unrestricted access to all system files, settings, and commands. By default, Ubuntu (and many Debian-based distributions) disables direct root login to enhance security. Instead, it relies on sudo (superuser do), which allows authorized users to execute commands with root privileges temporarily.

However, there may be scenarios where you need to enable root login—for example, troubleshooting, server administration, or legacy application requirements. Conversely, disabling root login is critical for securing your system against unauthorized access.

This blog will guide you through the step-by-step process of enabling and disabling root login in Ubuntu, including SSH and GUI login methods, along with security best practices.

2026-05

Table of Contents#

  1. What is the Root User?
  2. Why Enable or Disable Root Login?
  3. Prerequisites
  4. How to Enable Root Login in Ubuntu
  5. How to Disable Root Login in Ubuntu
  6. Security Best Practices
  7. Conclusion
  8. References

What is the Root User?#

The root user (UID 0) is the "administrator" of the Linux system, with full access to modify system files, install/uninstall software, and manage user accounts. Unlike regular users, root has no restrictions—this power makes it both useful and dangerous. A single mistake (e.g., rm -rf /) could irreversibly damage the system.

Ubuntu disables direct root login by default to mitigate this risk. Instead, users with sudo privileges (configured during installation) can run commands as root using sudo [command].

Why Enable or Disable Root Login?#

When to Enable Root Login:#

  • Troubleshooting: Some system recovery tasks may require direct root access.
  • Legacy Applications: Older software may hardcode root dependencies.
  • Server Administration: In controlled environments (e.g., private networks), root login might simplify management.

When to Disable Root Login:#

  • Security: Root is a prime target for brute-force attacks. Disabling direct login reduces this risk.
  • Accountability: sudo logs all actions, making it easier to track who did what.
  • Accidental Damage: Restricting root access prevents unintended system changes.

Prerequisites#

Before proceeding, ensure you have:

  • An Ubuntu system (tested on 20.04 LTS, 22.04 LTS, and 24.04 LTS).
  • A user account with sudo privileges (created during Ubuntu installation).
  • Basic familiarity with the terminal (Ctrl+Alt+T to open).

How to Enable Root Login in Ubuntu#

Step 1: Set a Root Password#

Ubuntu disables root by default, meaning the root account has no password. To enable root login, first set a strong password for the root user:

  1. Open the terminal.

  2. Run the following command to set the root password:

    sudo passwd root  
  3. You’ll be prompted to enter your user password (to verify sudo access), then set and confirm the new root password.

    Example output:

    [sudo] password for your_username:  
    New password:  
    Retype new password:  
    passwd: password updated successfully  
    

    Note: Use a strong password (mix of letters, numbers, and symbols) to protect the root account.

Step 2: Enable Root Login via SSH#

By default, SSH (Secure Shell) blocks root login for security. To allow root to log in via SSH:

  1. Edit the SSH daemon configuration file:

    sudo nano /etc/ssh/sshd_config  
  2. Find the line containing PermitRootLogin. By default, it may be set to no or prohibit-password. Change it to:

    PermitRootLogin yes  

    Caution: PermitRootLogin yes allows password-based root login. For slightly better security, use PermitRootLogin without-password to restrict root to SSH key-based authentication only (see Security Best Practices).

  3. Save the file (Ctrl+O, then Enter) and exit (Ctrl+X).

  4. Restart the SSH service to apply changes:

    sudo systemctl restart sshd  

Now, you can log in as root via SSH:

ssh root@your_server_ip  

Step 3: Root Login via GUI (Graphical Interface)#

Important: Ubuntu does not officially support direct root login through the graphical interface. The display managers (GDM3 for GNOME, LightDM for Xfce/LXDE) block root login by default, and simply adding configuration options like AllowRoot=true or allow-root=true will not enable it. Enabling root GUI login requires modifying PAM (Pluggable Authentication Modules) configuration, which is complex, unsupported, and carries significant security risks.

If you genuinely require root access in a graphical environment, consider these alternatives:

  • Use sudo -i or sudo -s from the terminal to obtain a root shell
  • Use pkexec for graphical applications requiring elevated privileges
  • For remote server management, use SSH with key-based authentication instead

For security reasons, Ubuntu recommends avoiding root GUI login entirely and using sudo for administrative tasks.

How to Disable Root Login in Ubuntu#

If you’ve enabled root login and no longer need it, follow these steps to secure your system.

Step 1: Lock the Root Account#

Locking the root account prevents direct login by disabling the password.

  1. Run the following command to lock the root account:

    sudo passwd -l root  

    Output:

    passwd: password expiry information changed.  
    

    To verify, try switching to root:

    su -  

    You’ll get an error: su: Authentication failure.

Step 2: Disable Root Login via SSH#

Revert the SSH configuration to block root login:

  1. Edit the SSH daemon file:

    sudo nano /etc/ssh/sshd_config  
  2. Set PermitRootLogin back to the default (recommended):

    PermitRootLogin no  

    Or, for key-based only (if previously enabled):

    PermitRootLogin prohibit-password  
  3. Restart SSH:

    sudo systemctl restart sshd  

Step 3: Disable Root Login via GUI#

Since Ubuntu's graphical login managers do not support direct root login (as explained above), there is no GUI-based root login to disable. The default configuration already blocks root access through the display managers.

To verify root cannot log in via GUI, ensure the root account remains locked (sudo passwd -l root) and rely on sudo for administrative tasks.

Security Best Practices#

  • Avoid Root Login: Use sudo instead of direct root access. sudo logs actions to /var/log/auth.log, aiding accountability.
  • Limit sudo Access: Restrict sudo privileges to trusted users via /etc/sudoers (edit with visudo).
  • Use SSH Keys: If root SSH access is necessary, disable password login and use SSH keys (set PermitRootLogin without-password in sshd_config).
  • Strong Passwords: If root login is enabled, use a complex password and change it regularly.
  • Update Regularly: Keep your system updated with sudo apt update && sudo apt upgrade to patch vulnerabilities.

Conclusion#

Managing root login is a critical part of Ubuntu security. While enabling root can simplify certain tasks, it introduces significant risks. By default, Ubuntu’s approach of using sudo is safer and more accountable.

If you must enable root login, do so temporarily and follow security best practices (e.g., SSH keys, strong passwords). Always disable root login when it’s no longer needed to protect your system from unauthorized access.

References#