Website Vidya
SecurityStep-by-Step Guide

How to Enable and Disable Root User Account in Ubuntu

Learn how to enable and disable root user account in ubuntu 24.04 safely with passwd locking, SSH PermitRootLogin policies, and sudo -i workflows.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
How to Enable and Disable Root User Account in Ubuntu
AI Executive SummaryWebsiteVidya AI

5 min read

Learn how to enable and disable root user account in ubuntu 24.04 safely with passwd locking, SSH PermitRootLogin policies, and sudo -i workflows.

Key Highlights
  • Prerequisites and Supported Releases
  • Understanding the Ubuntu Root Account Security Model
  • Step 1: How to Enable the Root User Account
Grounded in full engineering runbookWebsiteVidya AI Engine

On traditional UNIX and Linux operating systems, system administration is performed by logging in directly as the root user.

However, Ubuntu departs from this conventional model by purposefully disabling the root user password by default upon system installation.

In the system shadow database (/etc/shadow), the root account is marked with an exclamation point (!), rendering direct password-based logins impossible.

Instead, Ubuntu delegates full administrative capabilities to members of the sudo group, providing accountable audit logging and preventing automated root brute-force attacks.

Advertisement

In this guide, we explain how to enable and disable root user account in ubuntu, configure SSH root restrictions, and adopt the modern sudo -i workflow.

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

Advertisement
  • A computer or server running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
  • An unprivileged user account with sudo administrative privileges.
  • Access to a terminal shell or SSH console.
  • Understanding of password security standards.

Understanding the Ubuntu Root Account Security Model

A common misconception among new Linux administrators is that Ubuntu has deleted or removed the root account.

The root account exists on every Ubuntu installation with UID 0, but it has no assigned password hash.

Inspect the root account entry in /etc/shadow to observe the default locked state:

The ! symbol indicates that the password hash field cannot match any encrypted password string:

Advertisement
# Inspect the root account shadow entry:
sudo grep '^root:' /etc/shadow
# Default output:
# root:!:19800:0:99999:7:::

# Notice the exclamation mark (!) in the second field.
# This signifies an account with a locked password.

Step 1: How to Enable the Root User Account

Certain legacy software suites, specialized hypervisors, or single-user rescue environments require an active root password.

To enable the root account, assign it a secure password using the passwd command with sudo escalation:

Enter and confirm a strong, high-entropy password for the superuser:

# Set a password for the root superuser:
sudo passwd root
# Terminal prompt:
# New password: 
# Retype new password: 
# passwd: password updated successfully

# Verify the shadow entry now contains a valid cryptographic hash (e.g. $y$):
sudo grep '^root:' /etc/shadow
# Output: root:$y$j9T$ABC123xyz...:19800:0:99999:7:::

Testing Root Login via su

With a password assigned, you can switch directly to the root user session using the standard su (substitute user) utility:

Enter the newly created root password to open an interactive superuser shell:

# Switch to root user shell:
su -
# Enter root password.

# Verify active session identity:
whoami
# Output: root

Step 2: How to Disable the Root User Account

Once maintenance tasks or emergency recovery operations are complete, re-locking the root password restores Ubuntu's standard security posture.

Use the passwd utility with the -l (lock) option:

This prepends an exclamation point to the encrypted hash, immediately disabling password logins while preserving SSH key authentication if configured:

# Lock the root account password:
sudo passwd -l root
# Output: passwd: password expiry information changed.

# Verify the locked status in /etc/shadow:
sudo grep '^root:' /etc/shadow
# Output: root:!$y$j9T$...:19800:0:99999:7:::

# Test that direct su login is now rejected:
su -
# Output: su: Authentication failure

Step 3: Disabling Root Login over SSH (/etc/ssh/sshd_config)

Even when the root account has a password enabled locally, exposing direct root logins over SSH presents extreme security vulnerabilities to internet scanners.

Harden the OpenSSH server daemon by configuring PermitRootLogin in /etc/ssh/sshd_config:

Set the directive to no (or prohibit-password for key-only access):

# Update SSH configuration to forbid direct root login:
sudo sed -i 's/^#*PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config

# Verify configuration syntax:
sudo sshd -t

# Restart the OpenSSH service daemon:
sudo systemctl restart ssh

You do not need to enable the root account to obtain a persistent superuser shell.

Ubuntu provides clean command-line alternatives that grant root shell access using your personal account password:

Review this comparison of interactive root shell techniques:

CommandShell EnvironmentWorking DirectoryRecommended Use Case
sudo -iSimulates a full login shell; loads root's .profile and .bashrcSwitches to /rootStandard administration when full root environment is required
sudo -sStarts a shell using the current user's environment variablesRetains current working directoryQuick multi-command operations without path changes
sudo [command]Executes a single command with root privileges and exitsRetains current working directoryBest practice for daily system administration

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

Authentication failure when attempting su - with root password

Cause: The root account password was locked using passwd -l or was never initialized.

Resolution: Use sudo passwd root from an authorized sudoer account to assign a new password.

sudo passwd root

Permission denied (publickey,password) when logging into SSH as root

Cause: PermitRootLogin no is enforced in /etc/ssh/sshd_config.

Resolution: Log in using your non-root sudoer account and elevate privileges with sudo -i.

ssh username@server-ip

Cannot execute sudo commands after modifying root settings

Cause: User accidentally removed themselves from the sudo group.

Resolution: Reboot into Ubuntu Recovery Mode from GRUB, select 'root - Drop to root shell prompt', and run usermod -aG sudo username.

usermod -aG sudo username

Security Best Practices and Hardening

Keeping the root account locked is the single most effective way to eliminate automated dictionary attacks targeting user 'root'.

Always verify that your non-root user has working sudo access in a separate terminal before locking or unlocking root.

Review privileged audit events in /var/log/auth.log to monitor when administrative privileges are exercised.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
Disabling the root password prevents automated network bots from attempting brute-force attacks against the root account, creates an immutable audit trail in /var/log/auth.log of who executed administrative actions, and prevents accidental system-destroying typos.
sudo -i prompts for the individual user's personal password and verifies membership in the sudoers file. su - prompts for the global root superuser password, which requires sharing a single credential across multiple administrators.
No. System cron jobs configured in /etc/crontab and /etc/cron.d/ execute under the root user UID 0 directly via system scheduling without requiring password authentication.
Yes. If PermitRootLogin prohibit-password is set in /etc/ssh/sshd_config and an authorized public key resides in /root/.ssh/authorized_keys, cryptographic SSH key login remains functional while password logins are blocked.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout