Skip to main content

Command Palette

Search for a command to run...

How to Switch User in Linux

Updated
6 min read

Switching users in Linux is a common task that you might need to do often. Whether you want to run a program as another user or just switch accounts without logging out, knowing how to switch users efficiently can save you time and effort. In this article, I’ll guide you through the different ways to switch users in Linux, using both command-line tools and graphical interfaces.

You don’t have to be a Linux expert to understand these methods. I’ll explain everything clearly and simply, so you can follow along easily. By the end, you’ll know how to switch users quickly and safely, no matter what Linux distribution you use.

Understanding User Switching in Linux

In Linux, every user has a unique account with specific permissions. Sometimes, you need to switch from one user to another to perform tasks that require different access rights. This is especially common for system administrators who need to switch to the root user or other accounts.

There are two main ways to switch users:

  • Temporarily switching to another user in the current session.
  • Logging out and logging back in as a different user.

Switching users without logging out is faster and more convenient, especially when you need to run commands as another user. The most common tool for this is the su command, but there are others like sudo and graphical options.

Using the su Command to Switch Users

The su command stands for "substitute user" or "switch user." It allows you to switch to another user account from the terminal.

How to Use su

To switch to another user, open your terminal and type:

su - username

Replace username with the name of the user you want to switch to. The - option ensures you get the new user's environment variables, like their PATH and home directory.

For example, to switch to the user john, you would type:

su - john

You will be prompted to enter the password for that user. After entering it, your shell will change to that user’s environment.

Switching to Root User

If you want to switch to the root user, simply type:

su -

or

su

You will need to enter the root password. Once authenticated, you have full administrative privileges.

Exiting the su Session

To return to your original user, type:

exit

or press Ctrl + D.

Important Notes About su

  • You need to know the password of the user you want to switch to.
  • The root account might be disabled on some systems for security reasons.
  • Using su switches the entire shell session to the new user.

Using sudo to Switch Users

sudo is another powerful command that lets you run commands as another user, usually root. It’s often preferred over su because it doesn’t require knowing the other user’s password, only your own.

Switching to Another User with sudo

To switch to another user, use:

sudo -i -u username

For example, to switch to user john:

sudo -i -u john

This opens a login shell as john. You will need to enter your own password, not john’s.

Switching to Root with sudo

To switch to root, type:

sudo -i

This gives you a root shell after entering your password.

Benefits of Using sudo

  • No need to know the target user’s password.
  • Actions are logged for security.
  • You can limit which users can use sudo through configuration.

Switching Users Graphically in Linux

If you use a Linux desktop environment like GNOME, KDE, or XFCE, you can switch users without using the terminal.

How to Switch Users Graphically

  1. Click on the system menu or user menu in the top-right or top-left corner.
  2. Look for an option called "Switch User," "Switch Account," or "New Login."
  3. Select it, and you will be taken to the login screen.
  4. Choose the user you want to switch to and enter their password.

This method keeps your current session active, so you can switch back without closing programs.

Fast User Switching

Many desktop environments support fast user switching, which lets multiple users stay logged in simultaneously. This is useful if several people share the same computer.

Switching Users in Remote Sessions

When working on remote Linux servers via SSH, switching users is common.

Using su or sudo Over SSH

You can use the same commands (su or sudo) to switch users after logging in remotely.

For example:

ssh user@server
sudo -i

This switches you to root on the remote server.

Using ssh to Directly Log in as Another User

You can also log in directly as another user by specifying the username in the SSH command:

ssh username@server

This avoids switching users after login.

Tips for Safe User Switching

Switching users can be risky if you’re not careful, especially when using root privileges.

  • Always use sudo when possible instead of logging in as root.
  • Avoid running graphical applications as root.
  • Log out or exit the user session when done.
  • Use strong passwords for all user accounts.
  • Limit sudo access to trusted users only.

Common Errors and Troubleshooting

Sometimes, switching users might not work as expected. Here are some common issues and fixes:

  • Permission denied: You might not have permission to switch to that user or the password is incorrect.
  • Root account disabled: Some Linux distributions disable root by default. Use sudo instead.
  • Command not found: Make sure you typed the command correctly (su or sudo).
  • Environment not loaded: Use su - username with the dash to load the user’s environment.

Summary Table: Commands to Switch Users in Linux

CommandDescriptionPassword Required
su - usernameSwitch to another userTarget user’s password
su -Switch to root userRoot password
sudo -i -u usernameSwitch to another user with sudoYour own password
sudo -iSwitch to root user with sudoYour own password
ssh username@serverLog in directly as another userTarget user’s password

Conclusion

Now you know several ways to switch users in Linux, both from the command line and the graphical interface. Whether you’re managing a server or using a desktop, these methods help you work efficiently and securely. Remember to use sudo when possible for safer privilege escalation, and always exit user sessions when you’re done.

Switching users is a basic but essential skill in Linux. With practice, you’ll find it easy to switch accounts without interrupting your workflow. Keep these commands handy, and you’ll be ready for any user-switching task that comes your way.

FAQs

How do I switch to the root user in Linux?

You can switch to root by typing su - and entering the root password, or use sudo -i if your system uses sudo for root access.

Can I switch users without knowing their password?

Yes, if you have sudo privileges, you can switch to another user using sudo -i -u username by entering your own password.

How do I switch users in a graphical Linux environment?

Use the system menu to select "Switch User" or "New Login," then choose the user account and enter the password.

What is the difference between su and sudo?

su switches the entire shell to another user and requires their password. sudo runs commands as another user (usually root) using your password and is more secure.

How do I switch users in an SSH session?

You can switch users after logging in with su or sudo, or log in directly as another user using ssh username@server.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts