Computer on table

How to change user in Linux terminal

Linux is a multi-user operating system. It allows the user to perform various operations with system resources. For example, you can manage groups, add or remove users, modify file permissions and so on.

As a result, there may be a situation when you need to change the current user. You may want to run a specific command using credentials of the other user. Or you may want to have constant access to the shell of another member of a system. Also, there may be much more similar cases when you need to switch privileges in a terminal.

Linux can help you with solving such issues. For that purpose, you may use either the "su" or the "sudo" command.

You can review the following sections of this article to learn how to use these commands. We provide descriptions of the most popular options. You may choose which tool to use depending on your needs.

Using the su command

The su utility is very widely used and should be available on your Linux distribution. It allows you to obtain privileges of some other account.

Please see the specification of this command below. As you can see there, most of the parameters are optional.

su [options] [username] [arguments]

The basic usage is shown below. You just have to type the name of the command and then specify the desired username.

su username

After running the above command, the su utility will switch the current user to the user which is provided in options. Also, it's important to note that the su command may ask you to enter the password. You will have to type it into the console.

You may also use the special "-" flag. The example is shown below.

su - username

The "-" symbol is a short version of the -l or --login options. It specifies that the shell should be configured as like the real user has logged in. In that case, the terminal will also navigate to the directory of a given user.

Running a command with help of the su command

The su utility also provides a special -c flag that allows you to run any command on behalf of another user. This can be useful in various situations, like you need to perform some work with files of a different user of a system.

Please see an example of how to execute the whoami command in the Linux terminal.

su -c whoami username

Please pay attention that you may also need to enter a password after you run the above command. If the whoami tool starts successfully, it should print the name of the target user to the screen. It will be a confirmation that the -c flag works for the su tool.

Using the sudo command

There is another way that allows you to run some commands with credentials of the other user. You can use the sudo utility for that purpose. Please see the specification of the command below.

sudo [options] [your-command]

In most cases, the sudo tool is used to obtain privileges of the super user. This may be required when you need to perform some administrative tasks in the Linux operating system.

It is worth mentioning that the special configuration is needed for the sudo tool. The user that runs this utility must be added to the /etc/sudoers file. If that's not the suitable option, then the other way is to add the current user to the sudo group.

If you would like to start the default shell for another user, then you may run the following command.

sudo -u username -s

Also, it is possible to execute a single command under root privileges. The command is very simple in that case. You just have to enter the sudo keyword and name of the desired application. Please see the following sample

sudo nano

If you run the above command, the nano editor will start in the console with the root access privileges. As a result, please be careful with editing system files when running the editor in that mode.

Switching to the root user

If you need to have access to the root console and run multiple tasks there, then you may start the desired shell with help of the sudo tool. Please see the following sample.

sudo bash

You may also need to enter the root password after running the above command. When the shell is started, you may run the command with the root access rights.

In addition, there is another way that allows you to start a privileged shell. The example is given below.

sudo su

There is an important note for the above commands. It's recommended to have backups of the critical data or system files before performing any tasks in the root mode. If something is not properly configured, the system may not work correctly. As a result, you are responsible for all actions that you perform with the root access rights.

Listing users in Linux

There may be a situation when you want to switch to some other account, but you don't know the exact username of the target account. In such a case, you can try to list all the accounts that are currently available in the system and then try to find the desired one.

Please see the following sample.

cat /etc/passwd

The above command will output all user names that are stored in the passwd file. Depending on the configuration, the list may be large. Example of the output is given below:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin

There, you can try to find the desired username. Then you may specify it when running the su command in the Linux terminal.

Also, the alternative way to list the users is to use the getent command. As an argument, you have to provide the name of the database that must be queried. In our case, it can be the passwd value. The full command may look like the following:

getent passwd

From the output of the above command, you can see a list of the users that are available in the system.

The whoami command

In case if you don't know which user is currently logged in, you may use the whoami command. It's a simple utility that will print the name of the current user to the screen. Please see the usage below.

whoami

The output may be like the following:

username@username-pc:~$ whoami
username

This tool can help you to ensure that you really need to use the su command. If you are already signed to the target account, you can run commands directly in the terminal without getting credentials of the other user.

The id command

Another way to get information about the current user is to use the id command. It allows you to print various details about the user to the console. The specification of this command is given below.

id [options] [username]

If the username is not provided, then the tool will output information about the user which is logged in.

The id has various options. But in our specific case we want to get only the username. To get that information, you may run the following command in the console.

id -un

The output will contain a name of the current user, as it's shown below.

username@username-pc:~$ id -un
username

The who command

Also, there may be a situation when you need to run some command on behalf of the logged in user. However, you may not know which users are currently logged into the Linux operating system.

To solve such an issue, you may run the who command in the terminal.

who

The specified tool is very simple. It prints a list of the logged in users to the console. The output may be like the following:

james    :1           2023-01-14 10:26 (:1)
robert   :2           2023-01-14 11:32 (:2)

Now, you can choose the desired username from the given list and use it when running the su command in the console.

Which command to use to switch a user?

Each command has a different purpose. The su utility may be used mostly for obtaining credentials of a regular account. For example, you can start a shell with access rights of the other user, then perform some actions and exit when the work is done.

On the other hand, the sudo tool can be used mostly for running some commands under the root account. Sometimes, that is necessary when you need to modify system configuration files.

However, both those commands have various advanced options that may be helpful. There may be other use cases when these tools are needed. You may read official man pages to get more information on how these commands work in the Linux console.

Conclusion

The Linux operating system provides various useful tools that you can use to start a terminal for another user. Usually, this is needed when you have a multi-user Linux installation in some shared environment.

Also, it is worth noting that you may use either the su or the sudo utility. Which one to choose is your own decision, since it totally depends on your preferences and system configuration. In any case, those tools are easy to use and you should be able to find the appropriate solution.

As a result, you have the ability to start the shell for another user and continuously run multiple commands. If that's not the desired option, it's also possible to run a single command with the privileges of a different user.

Related Articles

Typing on a keyboard

How to unzip a file in Linux terminal

Magnifying glass

How to find all files containing a specific text on Linux

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *