On every Linux system, the root account is a special user with 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 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 lets you run commands as the root account temporarily. 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 you’re 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’re logged in as a normal user and a pound sign ("#") if you’re 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 logged in to a fresh shell session. Depending on what task you need to perform, and how root 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 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 runs with root privileges.
Using sudo makes it difficult to forget you’re root since you’re not logged in to 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, use the visudo command that requires root privileges to run, and lets you safely edit the sensitive sudoers file.
Related information
- How do I access and use the Linux command line?
- How to change a user account to an administrator account.
- su command syntax, options, and examples
- sudo command syntax, options, and examples
- visudo command syntax, options, and examples
- Linux help and support.