Linux is a multi-user operating system; that means it allows more than one user to log in and perform operations without affecting other users in the system.

This tutorial will go over various ways to switch users from the terminal and the graphical environment.

Method #1 – Graphical Environment

If you run Ubuntu Desktop with a graphical desktop environment such as Gnome, you can easily switch users.

On Ubuntu, navigate to the top bar and click on the Power Icon. Select logout or switch user from the dropdown menu —this may be unavailable depending on the version you are running.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image1-27.png" data-lazy- height="593" src="data:image/svg xml,” width=”792″>

Once you select log out, the system will navigate you to a login window to specify the user’s username and password you wish to use. Doing this will terminate your session and kill all your running sessions

If you select switch user, you will specify the username and password of the user while preserving your session and all your running processes.

TIP: If the switch user option is not available, press the CTRL ALT F2 keys. That will take you to a shell prompt where you can specify the username and password. Revert by pressign CTRL ALT F2.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image3-25.png" data-lazy- height="270" src="data:image/svg xml,” width=”654″>

Method #2 – The su command

For those who prefer to work with the terminal, the su command allows you to switch to a different user while still preserving the current session. If the root user cannot log in to the system—either via GUI session or SSH—the su command can come in very handy.

NOTE: Most Linux distributions disable the root user by default. Although you can enable it, I do not recommend it: it might break the system (CentOS is a prime culprit).

Basic su Command Usage

The su command is easy to use with straightforward syntax.

$ su [OPTIONS] [] [USER [args]]

By default, invoking the su command with no username specified runs the shell as root, which requires the password for the root user (Must be enabled).

Using the su command with no option does not change the current working directory to /root.

The most common use case of the su command is with the – or -l option. The -l allows su to changes the home directory and spawn a shell session similar to a real-login (set in /etc/passwd).

How to use the su command to switch to a different user

To log in as a different user, use the -l option followed by the user’s username. For example:

This command will prompt you for the password and log in to the specified user.

How to use the su command to log in with a different shell

By default, su uses the environment variables stated in the /etc/passwd file. Such options include the default shell. However, you can change the login shell using the -s option.

For example:

$ su -l ubuntu -s /bin/bash

The command will launch the ubuntu user with the bash shell.

How to use the su command while preserving the environment

To run as a user with the su command while preserving the environment variables (home directory, user, logname), use the -p or –preserve-environment.

For example, suppose we are the ubuntu user and would like to log in as the linuxhint user while still preserving the environment variables.

In that case, we can use the command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image2-25.png" data-lazy- height="173" src="data:image/svg xml,” width=”552″>

As you can see from the above example, we are still in the /home/ubuntu directory and using the zsh shell (default for the ubuntu user).

How to use the su command to run a command as a different user

To run a command as a different user without spawning an interactive shell session, we can use the -c option as:

Method #3 – SSH

Another way to switch to a different user is the good old SSH. To log in as a local user, use the command as:

$ ssh linuxhint@127.0.0.1

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image4-23.png" data-lazy- height="352" src="data:image/svg xml,” width=”646″>

Conclusion

The su user is an excellent tool to switch users or run commands as another user. Compared to the sudo command that invokes the root user, su allows you to set a specific user.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/john-150×150.png60f2fc8b0db36.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list