When we use the Terminal on 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, Ubuntu, Manjaro, etc.
Different ways to change user
To change the user, you will be using the su
command. The way it works is, you simply run an interactive shell with the new user as if it were them. It can also be used to execute commands with another user without switching users.
Change from any user
When we are already using another user without so many privileges, the ideal is to use the su
command.
To switch to the root user, we execute.
su 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.
su user
In the latter case, it asks for the password of user in case we do not have privileges.
Change with users that have administrator 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 command, 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
Change user along with current directory
When we use the su
command in a simple way, the current working directory of the Shell will be the one we had before executing the command, and in many cases it is not ideal. For example, if we want to change to the user hector
, it is most logical that we would also need to change to hector
home in /home/hector
. We have two options.
Change directory automatically
We can achieve this with the su
command and the -
option.
su - user
The -
, -l
, and --login
options are equivalent. What this option does is use the same environment as if it were a login, it even uses the default Shell of this user.
Change directory manually
If we can't memorize the --login
option, we can just do it manually.
su user
cd ~
Run command with another user
To run a command with a different user from the terminal, we use the --command
option of the su
command.
su user --command="echo 'OK'"
It must be clarified that the ideal is to escape the necessary characters so that they do not interfere, this is due to the way quotes work in shells. I also recommend specifying the Shell to be used with the --shell
option to avoid problems with the syntax. For example, in Manjaro the default Shell is ZSH and when we execute a command as simple as wget https://example.com /download?file
is going to give an error because some characters like the question mark ?
, which need to be escaped with a diagonal. On the contrary, if we simply use bash, we already know how it is going to behave and that we don't need to escape so many characters.
su user --shell=bash --command="echo 'OK'"
Run script with another user
The same --command
option is used, but we specify the shell directly. It is not necessary to use the --login
option, but I recommend it for the environment variables.
su - user --command="/usr/bin/bash /home/user/script.sh"
Switch to root at 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.
To exit the user
To return to the main user, we just have to use the exit
command.
exit
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.
How to find out which my current user is
I decided to share this because with my little experience on 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.
Differences between sudo and su commands
sudo
only runs a command with root
privileges, it does not change the shell interactively. It is limited to users with administrator privileges, or members of the adm
group. Its indiscriminate use can cause a change in the owners of files and folders.
su
starts an interactive shell with the given user, regardless of whether we are members of the adm
group. If we do not have permissions, we will simply be asked for the credentials of the required user. The exception is, if we use the --command
option, the command is executed and the output is returned.
su command options
Option | Description |
---|---|
-c, --command= | With this option, a command is executed with the specified user and the output is returned. |
-f, --fast | With this option, you pass -f to the shell. |
-g, --group= | The primary group for the root user only. |
-G, --supp-group= | A subgroup for the root user only. |
-, -l, --login | With this option, the same environment variables are used as if it were a login. |
-m, -p, --preserve-environment | The current user's entire environment is preserved in the new shell. |
-P, --pty | A pseudo-terminal is created for the session. |
-s, --shell= | The Shell to be used is specified, ignoring the default. If the shell does not exist or is restricted, the option is ignored. |
--session-command= | It is the same as --command , but without creating a new session. Not recommended. |
-w, --whitelist-environment= | Normally, environment variables are cleared with --login , with this whitelist you establish which variables not to clear. For example, PATH. |
-h, --help | Command help is displayed along with these options listed. |
-V, --version | The version is displayed. |
Helpful examples
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.
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 -rf
.