When we use the Terminal in Linux, the most common thing is that we are connected with a user without many privileges, and in many cases the need arises to use the root user, therefore we use "sudo" to get some magic, and the problem is that if we want to use it directly in the most modern systems, the root login is disabled. It could also be that we simply switch between different users to preserve file and folder permissions each time we execute commands. These steps apply to any Linux distribution, including Debian and Ubuntu.
1. Real use cases
These are some cases that we need to solve:
- On the Debian desktop, we need to perform a task from Terminal, but for this we need to use the root user.
- On the Ubuntu desktop we need to perform an administrative task from the Terminal, in this case we can use the "sudo" command, but it becomes annoying when we have many things to do, and the need is to use root.
- We are connected as root, but we need to execute "composer" with the user of the project or folder.
2. Different ways to change user
2.1. Boot With "Recovery mode"
Firstly in the case of "root", we must not forget that we can use the recovery mode, normally the second option when starting in the boot menu, and thus we enter as "root" directly.
2.2. Change from any user with or without privileges
When we are already using another user without so many privileges, the ideal is to use the "su" command.
In this way, we access with root:
Even if we leave the su command with no arguments, it is assumed that the user is root.
Then it will ask us for the root password, and we will automatically access it. It could not be easier, for example, if we forget to select the recovery option from the beginning, now we will no longer have to restart the computer, we simply open the terminal and place the previous command.
It should be noted that it works with any user and not only with root.
In the latter case, it asks for the password of "user" in case we do not have privileges.
2.3. Change with users limited by sudo but with privileges
Usually a typical Ubuntu user after installation. We will use the following command in case we are using Ubuntu with a user limited by sudo, in order to access root:
sudo su -
To access any other user without using the password of the other user, rather using our own password:
sudo su user
3. To exit the user
To return to the main user, we just have to use the
You just have to be careful when using an SSH connection, not to run it with our main user because the connection will be closed.⇧
4. Extra details that might interest us
I decided to share this because with my little experience in Linux I noticed that using root is very necessary, and within months I discovered that it was possible in a very simple way without the use of "sudo". Also in the case of managing multiple users, as when running multiple web page installations on the same server.
To know which user I am currently using, we execute the
whoami command, and it will appear to us. We can also know which user I am using by checking the terminal prompt, just before the at sign, the format is user at host, but in some systems it can be very personalized and not show it.
5. Security concerns
Although it is quite useful, it should be used sparingly, and only in strictly necessary cases. Personally I have accidentally changed the owner and group of all system folders with the command "chown" using root indiscriminately, causing my Ubuntu installation to stop working completely, the only solution was to reinstall everything and recover the data by hand directly from disk. This is just a case, and it was on a personal computer, the detail is that it could also happen to us in a production web server or in any critical server. The command "chown -hR" was in my case, in another case it could be "rm -fr".⇧