How To Change To Root User In Linux : Root User Login Commands

Gaining root user access in Linux requires using the correct command-line privileges. Understanding how to change to root user in linux is essential for system administration tasks like installing software, modifying system files, or managing users.

You might need root access to fix a broken configuration or set up a server. This guide walks you through every method clearly and safely.

What Is The Root User In Linux

The root user is the superuser account with full control over the system. It can read, write, and execute any file, change system settings, and manage other users. Because of this power, you should use root only when necessary.

Think of root as the admin account on Windows or the superuser on macOS. It bypasses all permission checks, so mistakes can break your system.

Why You Might Need To Switch To Root

Common reasons include:

  • Installing or removing software packages
  • Editing system configuration files in /etc
  • Managing user accounts and groups
  • Mounting drives or changing network settings
  • Running system updates or backups

Always use a regular user account for daily tasks. Switch to root only for specific commands.

How To Change To Root User In Linux

There are three primary methods to become root: using the su command, the sudo command, or logging in directly. Each has its own use case and security implications.

Method 1: Using The Su Command

The su command stands for “substitute user.” When used without a username, it defaults to root. You need the root password to proceed.

  1. Open a terminal window.
  2. Type su - and press Enter.
  3. Enter the root password when prompted.
  4. Your prompt changes to # indicating root access.

The dash (-) starts a login shell, loading root’s environment variables. Without it, you keep your current user’s environment, which can cause issues.

Example:

$ su -
Password: 
# whoami
root

To return to your normal user, type exit or press Ctrl+D.

Method 2: Using The Sudo Command

sudo lets authorized users run commands as root without needing the root password. Instead, you use your own password. This is more secure because it logs all commands and limits access.

To run a single command as root:

$ sudo apt update
[sudo] password for youruser:

To start a root shell session:

$ sudo -i

Or:

$ sudo su -

The -i flag simulates an initial login, giving you root’s environment. This is common on Ubuntu and Debian systems where the root account is disabled by default.

Check if your user has sudo privileges:

$ sudo -l

If you see a list of allowed commands, you’re good. If not, you need to be added to the sudo group by an existing admin.

Method 3: Logging In Directly As Root

Some systems allow you to log in as root from the login screen or via SSH. This is generally discouraged because it’s less secure and harder to audit.

To enable root login via SSH, edit the SSH configuration file:

# /etc/ssh/sshd_config
PermitRootLogin yes

Then restart the SSH service:

# systemctl restart sshd

But consider using sudo instead for better security.

Comparing Su And Sudo

Here’s a quick comparison:

  • su: Requires root password. Gives full root access. No command logging.
  • sudo: Requires user password. Can limit commands. Logs all actions.

For most modern distributions, sudo is the recommended method. It reduces the risk of accidental damage and provides an audit trail.

How To Set Up Sudo Access

If your user doesn’t have sudo privileges, you need to add them to the sudo group. You must already have root access or another sudo user.

  1. Log in as root or a sudo user.
  2. Run usermod -aG sudo username (on Debian/Ubuntu) or usermod -aG wheel username (on RHEL/CentOS).
  3. Log out and back in for changes to take effect.

Example:

# usermod -aG sudo johndoe

Now johndoe can use sudo commands.

Best Practices For Root Access

Follow these guidelines to stay safe:

  • Never log in as root for routine tasks. Use sudo instead.
  • Always double-check commands before pressing Enter.
  • Use sudo -i sparingly; prefer sudo command for single tasks.
  • Keep the root password strong and private.
  • Disable root login over SSH if not needed.
  • Monitor sudo logs in /var/log/auth.log or /var/log/secure.

Common Mistakes And How To Avoid Them

Here are frequent errors beginners make:

  • Typing the wrong password: Linux passwords are case-sensitive. Check Caps Lock.
  • Using su without the dash: This keeps your user’s PATH, which can break scripts. Always use su -.
  • Running graphical apps as root: Use sudo -H to preserve home directory or gksudo for GUI apps.
  • Forgetting to exit root: Stay in root shell too long increases risk. Exit immediately after finishing.

How To Verify You Are Root

Check your current user with:

$ whoami

If it returns “root,” you have full privileges. The command prompt also changes from $ to # for root.

Another way:

$ id -u

A value of 0 means root.

What To Do If You Forget The Root Password

If you can’t remember the root password and have no sudo access, you can reset it from recovery mode or a live USB.

  1. Reboot the system and hold Shift (or Esc) to enter GRUB menu.
  2. Select “Advanced options” and choose a kernel with “(recovery mode)”.
  3. From the recovery menu, select “root – Drop to root shell prompt.”
  4. Remount the filesystem as read-write: mount -o remount,rw /
  5. Reset the password: passwd root
  6. Type a new password twice and reboot.

On some systems, you may need to use sudo passwd root from a live environment.

Using Sudo Without A Password

For convenience, you can configure sudo to not ask for a password for specific commands. Edit the sudoers file with visudo:

# visudo

Add a line like:

username ALL=(ALL) NOPASSWD: /usr/bin/apt

This allows the user to run apt without a password. Be cautious—this reduces security.

How To Change To Root User In Linux On Different Distributions

The methods are similar across distributions, but default settings vary:

  • Ubuntu/Debian: Root account disabled by default. Use sudo -i or sudo su -.
  • Fedora/RHEL/CentOS: Root account enabled. Use su - or sudo -i if configured.
  • Arch Linux: Root account enabled. Use su - or sudo after setup.
  • openSUSE: Root account enabled. Use su - or sudo.

Always check your distribution’s documentation for specifics.

Security Considerations

Root access is powerful and dangerous. Here are key security tips:

  • Use strong passwords for both root and user accounts.
  • Enable two-factor authentication for SSH if possible.
  • Limit sudo access to specific commands for each user.
  • Regularly review sudo logs for unauthorized attempts.
  • Disable root login via SSH and use key-based authentication.

Frequently Asked Questions

What is the difference between su and sudo?

su switches to root using the root password, while sudo runs commands as root using your own password. Sudo offers more control and logging.

Can I become root without a password?

Only if you have sudo privileges configured with NOPASSWD, or if you boot into recovery mode. Otherwise, you need the root or sudo password.

Why does sudo -i give a different environment than su -?

Both start a login shell, but sudo -i uses root’s home directory and environment, while su - also loads root’s profile. They are essentially the same.

How do I know if I have sudo access?

Run sudo -l. If it lists commands, you have sudo access. If it says “user is not in the sudoers file,” you don’t.

Is it safe to enable root login via SSH?

Generally no. It increases attack surface. Use sudo and key-based authentication instead.

Troubleshooting Common Issues

If you encounter problems switching to root, check these:

  • Permission denied: You may not be in the sudo group. Contact an admin.
  • Authentication failure: Wrong password. Reset it if needed.
  • Command not found: Use full path or load root’s PATH with su -.
  • sudo: command not found: Install sudo with apt install sudo or yum install sudo as root.

Conclusion

Mastering how to change to root user in linux is a fundamental skill for any Linux user. Whether you use su, sudo, or direct login, always prioritize security and caution. Remember to exit root sessions promptly and use sudo for single commands whenever possible.

Practice these methods in a safe environment like a virtual machine. Over time, you’ll become comfortable managing system privileges efficiently and safely.