ROOT command in Linux: what it is and how it works

If we have been using Linux for a relatively short time or we plan to give Linux a try to see if we can really do the same as in Windows, we must be clear about a series of concepts that differ from Windows but are similar to other operating systems that we probably use. such as Android smartphones. We are talking about the term root.

When we talk about rooting Android, we are talking about a method that removes all protections (to call it in a way that is easily understood) from the system and that allows you to make changes to the device that would be impossible with them. This protection is not implemented to limit the possibilities of modification that users have on their devices (which is also the case), but rather to prevent changes to the system that endanger its integrity and operation.

ROOT command in Linux

If we talk about Linux, unlike Android, where the device comes standard with the operating system and the corresponding customization layer of each manufacturer, the users are the ones who install the distro that we want to use on our computer, so we don’t have to No limitation when making modifications to the equipment, modifications that affect any part of the system.

What is root

The root user in Linux is the user that has administrative access to the entire system and can make any type of modification to modify its operation. It is not recommended to log in with root permissions if we are not very clear about what we are doing or if we do not want to put the stability of the system at risk if we make any changes.

If we want to make any changes to the system, it is not necessary to log in with administrator permissions, but we can request permission using the sudo application to perform administrative tasks by entering our user’s account to confirm that we are the legitimate users of that account and not it is a third person who has had access to our equipment. We can also use the su command if we need to maintain root privileges over time.

Types of users in Linux

One of the most striking features of Linux is its user control, user control with which we can manage the power of action that users have at all times. Basically, in Linux we can find 2 types of users:

  • root . This user has absolute power and control over the computer and can do and undo as they please without any type of limitation beyond their knowledge. This type of user is, if we compare it with Windows, the administrator user, although in Linux we have much more decision-making power than in Windows.
  • User . Normal users are user accounts in which a series of limitations are established based on their needs and priorities, without having the option of carrying out administrative tasks reserved for root accounts. These types of users have limited options when it comes to installing applications or creating directories on the system. However, they can gain root permissions to make any changes via the sudo or su commands.

When to use sudo and when to use su

Once we are clear that it is root and what are the possibilities of these accounts and the limitations of user accounts, it is time to talk about the sudo and su commands, commands that allow us to obtain administrator permissions to make changes to the system.

Sudo

Normal user accounts have limited ability to install off-system applications. The sudo command allows us to execute a command as root, that is, with full privileges over the system, but without ceasing to be a normal user. We must accompany this word always before any operation.

For example, if we want to install an application, as normal users we cannot carry out this process. But, if we use the sudo command first, we’re going to get root permissions to perform that operation. Once it has been done, we will once again have the limitations associated with a normal user account. Whenever we use the sudo command, we will have to enter the root password.

sudo apt install gparted

If we want to install another application, we will have to precede the command, again, with the word sudo and enter the administrator password.

His

While sudo allows us to obtain administrator permissions to perform certain administrative tasks such as installing applications, creating users and so on, once the command is executed, we are normal users again. If we have to perform various operations on the computer for which administrator privileges are necessary, and we do not want to add sudo to each command, we will use su and enter the root password.

su

As of now, we have root privileges, so we can make any changes to the system without using sudo. Being the previous example, to install the Gparted application, once we have root permissions, instead of preceding the installation command with the word sudo, we can do it without it.

apt install gparted

When we no longer need root privileges, it is recommended to exit this mode to avoid making any unintentional changes, the exit command is used.

exit

If we want to change the user, the command to use is the following.

su nombrecuentausuario

Disable root account in Linux

If we use a computer with Linux that has different user accounts, it is possible that the root password ends up circulating freely in our environment without realizing it. With this password in the wrong hands, users can make any kind of changes and install applications on the system that are not necessary for everyday use. The solution to this problem, as long as we run that risk, is to disable the root account in Linux.

In this way, without a root account, users will not be able to use either sudo or su to enable administrator privileges in the system, having a positive impact on the security of the work computer. To deactivate the root account, we must open terminal and enter the following command

sudo passwd l root

In this way, the computer blocks this user so that it cannot be used either with the su command or with sudo . If we want to recover the user account again, we will have to configure a new one.

Recover root password

Whether we have disabled the root account or if we have lost the master password, Linux allows us to regenerate a new password through two methods: from the boot GRUB or using a LiveCD with a Linux distribution.

with GRUB

Once the bootable GRUB is displayed, we must access the advanced options, an option that is not on the main screen, and then select Recovery Mode to then select the root option for a superuser console.

From the command line, the first thing we will do is mount the drive with write permissions with the command

mount -o rw, remount /

Next, we will use the passwd command to set a new password.

passwd root

Finally, with the commands we use the sync and reboot commands to reboot the computer and for the changes to be applied.

sync

[car]reboot[/code]

From a LiveCD

To recover the root password, we can use any LiveCD distro, although from ITIGIC we recommend Ubuntu. Once we have started the computer with this distribution, on the welcome screen, we select Spanish so that the interface is displayed in our language and then Try Ubuntu.

Next, we open the Terminal application and enter the following command:

sudo su

Next, we must identify the partition where the Linux distribution we want to recover the root password is located. To do this we will use the command:

fdisk -l

In the next step, we need to mount the system unit:

mkdir /mnt/recover mount /dev/sda1 /mnt/recover

In our case, the partition is sda1. If it is different on our computer, we must modify sda1 for the partition corresponding to our computer. Once we have mounted the unit, we can start working with it to recover the root password. The first thing is to go to the recovery directory with the command:

chroot /mnt/recover

And finally, we use the passwd command to enter a new root password

passwd root