What is a Root user?
Before starting with how to become a root user in order to perform sensitive Ubuntu operations, it is important to have some understanding of the significance of a root user in Linux. The latest versions of Ubuntu, by default, do not come with a root user with a specially encrypted password; it has hence created a functionality by the name of “sudo” which you can use to perform various administrative operations. You can either temporarily become root user or you need to provide a password with the sudo command that helps in keeping accountability for all actions of the user as an administrator. By default, the first user created when you install Ubuntu is given the sudo rights. Ubuntu gives it complete root privileges and it is added to the list of sudoers in the /etc/sudoers file. It is important to note that you need to be an authorized sudoer to perform all the root user operations described in this article.
About the Article
In this article, we will explain how to become a root user in the Ubuntu command line. This will help you in performing all the software installation, removal, customization, and also system configuration operations. We have run the commands and procedures mentioned in this article on a Ubuntu 20.04 LTS system. We will be using the Ubuntu command line, the Terminal, in order explain how to become and also how to change the password for root user. Therefore, you need to open the Terminal application either through the system Dash or the Ctrl+Alt+T shortcut.
How to become root?
Open your Ubuntu Terminal and enter the following command:
$ su -
This command will ask the system to log you in as a superuser.
The system will ask you the password for root. Enter the password and hit Enter.
Ideally, you now will be able to see the “#” sign in front of your command prompt. This would mean that you are now logged in as root.
In case you get the following authentication failure message when you enter the password for root, it means that the root account is blocked at the moment.
You can read later in the article about how to lock/unlock the root account.
To become root user from an unprivileged (normal user account) account when there is no root password set, use this command:
sudo -s
Enter the password of your Ubuntu user when requested. Only Ubuntu users that are members of the sudo group are able to become root user like this. The user that is created during Ubuntu installation is a member of the sudo group by default.
Now that you are logged in as root, you can perform all the administrative operations on Ubuntu.
Let us explain this with an example.
The file adduser.conf located in /etc/ needs root privileges for you to make any changes to it. When we open it as a normal user, you will be able to see the following lines at the end of the file:
“File ‘adduser.conf; is unwritable’
$ nano /etc/adduser.conf
The system file is mentioned as unwritable because an unauthorized user can not make any changes to its configurations.
You can only edit it if you are logged in as root or if you are using the sudo command to temporarily avail root privileges.
Since we are now logged in as root, we will be able to see the file in the following manner:
We can make any changes to the file and save the changes by hitting Ctrl+X.
Note: It is very important to stay careful while making any sensitive changes to your system files while logged in as root. Any undue change to the system files can mess up with your entire system settings.
You can exit the root command prompt and become a normal user again by using the exit command as follows:
# exit
Get Temporary root access through the sudo command
Instead of logging in as a root in the command line and getting on to the # (root user) command prompt, you can use the sudo command with your other commands to temporarily become root.
For example, we can open the same adduser.conf file that we mentioned before in an editable format by availing the root rights as follows:
$ sudo nano adduser.conf
This is especially useful because any authorized user(the one added in the sudoers list) can enter their own password and temporarily gain root access for running a few commands. Another benefit of using this command is that you can perform tasks that require root privileges even when the root account is in a blocked state.
Unlock/lock root account
When you get the following message when logging in as root, it is most probably because the root account is locked. Most Ubuntu systems have this account locked by default.
You can, however, unlock it by entering the following command as sudo:
$ sudo passwd root
The system will ask you to enter a new UNIX password twice. After that, it will update the root password and the account will be unlocked.
After you are done with performing your root operations, you can lock the root account again by entering the following command:
$ sudo passwd -dl root
How to change the root password (An alternative approach)?
Apart from the method we just described above to change the root password, you can use the following alternative method:
Only a root user can change his/her own password. Therefore you need to login as root first. Enter the following command to do so:
$ sudo -i
The system will prompt you to enter the current sudo password. Please enter the password and hit Enter.
Now that you are logged in as root, you can change the passwd for the current (root) user by using the passwd command as follows:
$ passwd
After working along with this article, you are now capable of becoming root on your Ubuntu command line. You are now free to perform all the operations that only an extremely privileged user is entitled to perform. But be very careful as Uncle Ben would say, With great power comes great responsibility!