How to become root in Linux

Updated: 01/24/2018 by Computer Hope

root in linuxOn every Linux system, the root account is a special user that has administrative rights. Logging in as root (or executing commands with root privileges) is necessary for many tasks. If you need to perform tasks as the root user, make sure that you fully understand the commands you are running, and what consequences they have. A single careless or malformed command, run as root, can make the entire operating system unusable. Do your research, and always double-check every command before pressing enter.

Logging in as root

The root account is similar to any other account in that it has a username ("root") and a password. If you know root's password, you can use it to log into the root account from the command line.

There is a special command named su (for "super user," or "switch user"), that allows you to temporarily run commands as the root account. From the command line, type:

su

Enter the password once prompted for the password. If successful, you are switched to the root user, and can run commands with full system privileges.

Be careful while logged in as root; it is easy to forget that you are currently the root user, and you might inadvertently run a command thinking you are only a mere mortal. One way to remind yourself if you are root or not is to check your command prompt. Many systems end their command prompt with a dollar sign ("$") if you are logged in as a normal user, but with a pound sign ("#") if you are root.

Or, you can use the whoami command to determine the account you are using.

When finished with your administrative tasks, you can run the exit or logout command to return to your standard user account.

When logging in as root, it is often useful to use a single dash after the su command, like this:

su -

This command simulates a complete root login. It executes all of the root user's shell initialization scripts and sets all environment variables as if the root user were logged into a fresh shell session. Depending on what task you need to perform, and how the root account is configured, this form of the su command might be optimal for you.

Running commands as root without the root password

It is possible, and often preferable, to run commands as root without logging into the root account by using the sudo command, which stands for "superuser do". If you prefix a command with sudo, you are prompted for your password (not the root password), and your name is checked against a special file called sudoers. If your account is listed there, your command will be run with root privileges.

Using sudo makes it much more difficult to forget that you are root since you are not logged into the root account, and are never going to forget to log out. Also, typing sudo every time you run a potentially destructive command reminds you to be extra careful and double check yourself.

If you need to add a user to the sudoers list, you should use the visudo command that requires root privileges to run, and allow you to safely edit the sensitive sudoers file.