How do you become a root user in Ubuntu?
Either you run commands with root privilege like this:
sudo any_command
Or you switch user in Ubuntu to root user like this:
sudo su
In both cases, you’ll have to enter your own user account’s password. But there’s more to root account in Ubuntu that you should know.
When you have just started using Linux, you’ll find many things that are different from Windows. One of those ‘different things’ is the concept of the root user.
In this beginner series, I’ll explain a few important things about the root user in Ubuntu.
Please keep in mind that while I am writing this from Ubuntu user’s perspective, it should be valid for most Linux distributions.
You’ll learn the following in this article:
- Why root user is disabled in Ubuntu
- Using commands as root
- Switch to root user
- Unlock the root user
What is root user? Why is it locked in Ubuntu?
In Linux, there is always a super user called root. This is the super admin account that can do anything and everything with the system. It can access any file and run any command on your Linux system.
With great power comes great responsibility. Root user gives you complete power over the system and hence it should be used with great caution. Root user can access system files and run commands to make changes to the system configuration. And hence, an incorrect command may destroy the system.
This is why Ubuntu and other Ubuntu-based distributions lock the root user by default to save you from accidental disasters.
You don’t need to have root privilege for your daily tasks like moving file in your home directory, downloading files from internet, creating documents etc.
Take this analogy for understanding it better. If you have to cut a fruit, you use a kitchen knife. If you have to cut down a tree, you have to use a saw. Now, you may use the saw to cut fruits but that’s not wise, is it?
Does this mean that you cannot be root in Ubuntu or use the system with root privileges? No, you can still have root access with the help of ‘sudo’ (explained in the next section).
How to run commands as root user in Ubuntu?
You’ll need root privileges for some system specific tasks. For example, if you want to update Ubuntu via command line, you cannot run the command as a regular user. It will give you permission denied error or show ‘are you root’ error.
apt update
Reading package lists... Done
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
So, how do you run commands as root? The simple answer is to add sudo before the commands that require to be run as root.
sudo apt update
Ubuntu and many other Linux distributions use a special mechanism called sudo. Sudo is a program that controls access to running commands as root (or other users).
Sudo is actually quite a versatile tool. It can be configured to allow a user to run all commands as root. You may configure it run only a selected few commands as root. You can also configure to run sudo without password. It’s an extensive topic and maybe I’ll discuss it in details in another article.
For the moment, you should know that when you install Ubuntu, you are forced to create a user account. This user account works as the admin on your system and as per the default sudo policy in Ubuntu, it can run any command on your system with root privileges.
The thing with sudo is that running sudo doesn’t require root password but the user’s own password.
And this is why when you run a command with sudo, it asks for the password of the user who is running the sudo command:
abhishek@nuc:~$ sudo apt update
[sudo] password for abhishek:
As you can see in the example above, user abhishek was trying to run the ‘apt update’ command with sudo and the system asked the password for abhishek.
If you are absolutely new to Linux, you might be surprised that when you start typing your password in the terminal, nothing happens on the screen. This is perfectly normal because as the default security feature, nothing is displayed on the screen. Not even the asterisks (*). You type your password and press enter.
How to become root user in Ubuntu?
You can use sudo to run the commands as root. However, in situations, where you have to run several commands as root and you keep forgetting to add sudo before the commands, you may switch to root user temporarily.
The sudo command allows you to simulate a root login shell with this command:
sudo -i
abhishek@nuc:~$ sudo -i
[sudo] password for abhishek:
root@nuc:~# whoami
root
root@nuc:~#
Though I have shown you how to become the root user, I must warn you that you should avoid using the system as root. It’s discouraged for a reason after all.
You can use su command to switch users in Ubuntu. You can use it with sudo to temporarly switch to root user:
sudo su
If you try to use the su command without sudo, you’ll encounter ‘su authentication failure’ error.
You can go back to being the normal user by using the exit command.
exit
How to enable root user in Ubuntu?
By now, you know that the root user is locked by default in Ubuntu-based distributions.
Linux gives you the freedom to do whatever you want with your system. Unlocking the root user is one of those freedoms.
If, for some reason, you decide to enable the root user, you can do so by setting up a password for it:
sudo passwd root
Again, this is not recommended and I won’t encourage you to do that on your desktop. If you forget it, you won’t be able to change the root password in Ubuntu again.
You can lock the root user again by removing the password:
sudo passwd -dl root
In the end…
I hope you have a slightly better understanding of the root concept now. If you still have some confusion and questions about it, please let me know in the comments. I’ll try to answer your questions and might update the article as well.