In Linux, the “root” user is the superuser, an account with administrative privileges that allows the user to perform tasks that would otherwise be unavailable to a regular user. The root user controls the system, including modifying files, installing and uninstalling software, and managing system users and groups.
This guide teaches about the methods using which a user can become a root user in Linux (All Distros), and the following aspects will be covered thoroughly.
- How to Run Commands in the Terminal as a root User?
- How to Become Root In Linux?
- How to Set a Password for root?
How to Run Commands in the Terminal as a root User?
There are some scenarios where you want to use the terminal as a root but don’t want to switch, which you’ve done many times, as seen below.
$ sudo apt update
The above command, when executed, will ask for a password which is your user password. Once it is correctly entered, you can use the terminal with the same username with root privileges. This will be applicable until the terminal is closed.
How to Become Root In Linux?
There are multiple methods to become the root user in Linux, and their details are as follows.
Method 1: Using the “su” Command
The switch user or su command of Linux can switch to root in the current terminal session using this format.
$ sudo su
As you can see in the image above, we’ve successfully switched to the root user, and the username is also changed to “root@foss”.
Method 2: Using the “-s” Command
The ‘sudo -s’ command allows you to run a command or shell as the superuser or root user. When you run this command, you will be prompted for your password. If you enter the correct password, the command or shell will be run with superuser privileges, which means you will have access to all the files and system resources on the computer.
$ sudo -s
Using the above command, the user ‘itslinux’ is now a root user, which can be seen in the above output.
How to Set a Password for root?
The root user in Ubuntu is, by default, disabled, but it can be activated by setting up a password using this command.
$ sudo passwd root
Activating the root account raises security concerns, especially if a service is exposed to the web and you’re using it, so it is not recommended to activate it. Professionals should only use this account, and to deactivate it, remove the password using this command.
$ sudo passwd -dl root
If you see the message “password expiry information changed.” Then the root account is locked again.
Method 1: Become root Using the “-login” Command
Assuming you’ve unlocked the root account, as we’ve done above, you can use the ‘–login’ command to become root as in the following syntax.
$ su --login
In the above image, you can see it is asking for a password, which is the same as you’ve set by following the above section, and user ‘itslinux’ is now the root user.
Method 2: Become root Using the “-l” Command
Another method to become the root user that involves unlocking the root account is using the ‘-l’ command as in this format.
$ su -l
As seen in the above image, the user ‘itslinux’ is now the root using the that was set while unlocking the root account earlier in this guide.
Becoming a root user is often required in Linux because specific permissions are root only. Multiple methods are discussed in this guide, but we’d recommend not using methods (3 & 4) because of the security issues, and unlocking the root account is for professionals only.