How to switch users in Ubuntu

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Switching between users on your Ubuntu system is a common operation when the machine is used by multiple people or when several user accounts exist for different tasks or permissions. Learning how to switch users can help maintain the separation of concerns, privacy, and security for your system.

In this tutorial, we will explore several methods to switch users in Ubuntu, which is a popular Linux-based operating system. We will begin with basic commands and proceed to more advanced ones, enabling you to change users whether you’re working on a graphical interface or via the command line.

Graphical Method: Using the Desktop Environment

If you’re using Ubuntu with its default desktop environment (GNOME), switching users is straightforward. Follow the steps below:

  1. Click on the system menu located at the top-right corner of the screen. It is typically represented by your username or an icon resembling a gear.
  2. From the dropdown, select ‘Log Out’ or ‘Switch User.’ In the case of ‘Log Out,’ you’ll be taken to the login screen where you can choose another user to log in as.
  3. If you selected ‘Switch User,’ Ubuntu will bring up a list of available user accounts on the system. Click on the user you want to switch to and enter their password when prompted.

Note: Using the graphical method, only one user can be logged in at a time, unless you are using a display manager that supports multiple concurrent sessions.

Command Line Method: Using su Command

To switch users on the command line, you can use the su (substitute user) command, which allows you to switch to another user account. With proper permissions, you can execute commands as another user.

su - username

After issuing the above command, you’ll be prompted for the user’s password. If the authentication is successful, you will be switched to the indicated user’s environment.

Example:

$ su - alice
Password:

Once logged in as ‘alice,’ you could verify the switch by using the whoami command:

$ whoami
alice

Understanding the su Options

The su command has several options that you can use to modify its behavior:

  • -: Also known as -l or --login, this option starts a login shell for the user, which means that the environment of the new user will be loaded completely, including startup files like .profile or .bashrc.
  • -c: Followed by a command, it runs the given command as the specified user without fully switching to their environment.

Example of running a single command as another user:

$ su -c 'whoami' alice
Password:
alice

Advanced Usage: Using sudo for User Switching

The sudo command allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file.

sudo -u username command

This method is more secure than su and does not require you to know the password of the user to switch to, just your own password, provided your user has permissions to use sudo.

Example of switching to user ‘bob’ and listing their home directory contents:

$ sudo -u bob ls /home/bob

You will be prompted for your password, and you’ll be able to run the command without fully switching to ‘bob’s environment.

To start an interactive shell as another user with sudo:

$ sudo -i -u bob

Creating and Managing User Sessions with dm-tool

In systems with a display manager like LightDM, you can use the dm-tool command to switch user sessions. This allows for multiple users to be logged in simultaneously:

dm-tool switch-to-user username

An example command to switch to user ‘charlie’:

$ dm-tool switch-to-user charlie

Conclusion

In conclusion, Ubuntu offers flexible ways to manage user sessions, from simple graphical switching to robust command line techniques. Whether you’re a system administrator or a casual user, understanding how to effectively switch users is an essential aspect of managing a secure and well-organized system.