How to Switch Users in Ubuntu

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 – 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.

Sponsored

How to switch users in ubuntu 1

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

See also  Let’s talk about open source, AI and cloud infrastructure at GITEX 2024

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.

How to switch users in ubuntu 2

Method – 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).

See also  Which Ubuntu Flavor Should You Choose?

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).

Sponsored

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:

$ su -l ubuntu

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:

$ su -p linuxhint

How to switch users in ubuntu 3

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:

$ su -c top

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

How to switch users in ubuntu 4

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.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply