How To Switch Users In Linux

7 min read

Switching users in Linux is a fundamental skill for system administrators and power users, and mastering how to switch users in linux can improve security, multitasking, and workflow efficiency. This guide explains the command‑line and graphical methods, common pitfalls, and best practices so you can transition between accounts safely and effectively.

It sounds simple, but the gap is usually here.

Understanding User Switching in Linux

In a multi‑user Linux environment, each account has its own home directory, processes, and permissions. Switching users means starting a new shell session under a different user account without logging out of the current one. This capability allows you to:

  • Run commands with the privileges of another user.
  • Test configurations as a non‑root user.
  • Perform administrative tasks while keeping your primary session active.

The two primary mechanisms for user switching are the su command and the sudo command. Both interact with the PAM (Pluggable Authentication Modules) system, but they differ in how they handle authentication and privilege escalation.

Command‑Line Methods

Using su

The su (switch user) command launches a shell as the target user. The basic syntax is:

su - username
  • - (dash) ensures a login shell, reinitializing the environment.
  • You will be prompted for the target user’s password unless you are root.

Key points:

  • Root access: Only the root account can switch to any user without a password.
  • Non‑root users: Regular users can switch only to accounts they know the password for.
  • Environment: Using su - preserves the target user’s shell configuration (.bashrc, .profile, etc.).

Using sudo

sudo (superuser do) allows a user to run a single command or a shell as another user, typically root. The typical workflow is:

sudo -u username command
  • You enter your own password; sudo validates it against the /etc/sudoers file.
  • If configured, you can run sudo -i to obtain a full root shell with the target user’s environment.

Advantages:

  • Fine‑grained permissions: The sudoers file can restrict which commands a user may run as another user.
  • Auditability: Every sudo invocation is logged, enhancing security monitoring.
  • No password sharing: The target user’s password never travels over the network.

Switching Without a Password (sudoers configuration)

System administrators often enable password‑less sudo for specific tasks:

username ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart apache2

This line lets username restart the Apache service as root without being prompted for a password. Use this feature sparingly, as it reduces accountability.

Graphical Methods

Using the Login Manager

Most desktop environments (GNOME, KDE, Xfce) display a login screen (GDM, SDDM, LightDM) that lets you select a different user before logging in. The steps are:

  1. Click the session or user list on the login screen.
  2. Choose the desired user account.
  3. Enter the password for that account and press Enter.

This method is ideal for a complete session switch, especially when you need a fresh desktop environment.

Switching Sessions in Desktop Environments

Many modern desktops support session switching without logging out:

  • GNOME: Click your username in the top‑right corner → select Switch User.
  • KDE Plasma: Click the power button → choose Switch User or Log Out and then log in as another user.
  • Xfce: Use the session menu to Log Out or Switch User.

These GUI options internally invoke su or sudo to start a new session, so the underlying principles remain the same Not complicated — just consistent..

Best Practices and Security Considerations

  • Prefer sudo over su for regular use. sudo limits exposure by requiring authentication and logs actions.
  • Avoid running entire sessions as root unless absolutely necessary. Use sudo -u for specific commands instead.
  • Configure the sudoers file to restrict which users can switch to which accounts, and limit the commands they may execute.
  • Set proper file permissions on home directories (chmod 700 /home/username) to prevent unauthorized access.
  • Audit logs: Regularly review /var/log/auth.log (Debian/Ubuntu) or /var/log/secure (RHEL/CentOS) for suspicious user‑switching attempts.

Common Troubleshooting

  • “Authentication token is not set”: Ensure you are using sudo or su - correctly; the dash flag forces a login environment.
  • Permission denied when switching: Verify that the target user’s password is correct and that the invoking user has permission via /etc/sudoers.
  • Environment variables missing: Use su - username to load the target user’s login environment; plain su username may inherit the current environment.

FAQ

Q: Can I switch to another user without knowing their password?
A: Only if you have sudo privileges configured to run as that user without a password, or if you are already root And it works..

Q: Does switching users affect running processes?
A: Yes. When you switch, the new session starts with its own set of processes. Existing processes continue under the original user unless you explicitly change their ownership (which requires root) The details matter here. And it works..

Q: Is it possible to switch users remotely over SSH?
A: Absolutely. Use ssh user@host to log in as a different user, or execute ssh user@host "sudo -u otheruser command" to run a command as another user without an interactive shell.

Q: What happens to file ownership when I switch users?
A: Files remain owned by the user who created them. Switching users only changes which user’s UID the shell runs under; it does not alter file permissions Most people skip this — try not to..

Q: Can I switch back to my original user easily?
A: Yes. After completing tasks with the other user, simply exit the new shell (exit) or run su - yourusername to return.

Conclusion

Mastering how to switch users in linux enhances both security and productivity. Remember to follow best practices, keep permissions tight, and audit your actions regularly. Now, by leveraging su for full session switches and sudo for targeted command execution, you can manage multi‑user environments with confidence. With these tools and guidelines, you’ll be able to manage user accounts efficiently, whether you’re working locally on a workstation or remotely across a server fleet.

Worth pausing on this one.

sudo -u for specific commands instead.

  • Configure the sudoers file to restrict which users can switch to which accounts, and limit the commands they may execute. Practically speaking, * Set proper file permissions on home directories (chmod 700 /home/username) to prevent unauthorized access. Now, * Audit logs: Regularly review /var/log/auth. log (Debian/Ubuntu) or /var/log/secure (RHEL/CentOS) for suspicious user‑switching attempts.

No fluff here — just what actually works.

Common Troubleshooting

  • “Authentication token is not set”: Ensure you are using sudo or su - correctly; the dash flag forces a login environment.
  • Permission denied when switching: Verify that the target user’s password is correct and that the invoking user has permission via /etc/sudoers.
  • Environment variables missing: Use su - username to load the target user’s login environment; plain su username may inherit the current environment.

FAQ

Q: Can I switch to another user without knowing their password?
A: Only if you have sudo privileges configured to run as that user without a password, or if you are already root Small thing, real impact..

Q: Does switching users affect running processes?
A: Yes. When you switch, the new session starts with its own set of processes. Existing processes continue under the original user unless you explicitly change their ownership (which requires root) Still holds up..

Q: Is it possible to switch users remotely over SSH?
A: Absolutely. Use ssh user@host to log in as a different user, or execute ssh user@host "sudo -u otheruser command" to run a command as another user without an interactive shell Simple, but easy to overlook..

Q: What happens to file ownership when I switch users?
A: Files remain owned by the user who created them. Switching users only changes which user’s UID the shell runs under; it does not alter file permissions Most people skip this — try not to..

Q: Can I switch back to my original user easily?
A: Yes. After completing tasks with the other user, simply exit the new shell (exit) or run su - yourusername to return Simple, but easy to overlook. Practical, not theoretical..

Conclusion

Mastering how to switch users in linux enhances both security and productivity. This leads to by leveraging su for full session switches and sudo for targeted command execution, you can manage multi‑user environments with confidence. Remember to follow best practices, keep permissions tight, and audit your actions regularly. With these tools and guidelines, you’ll be able to manage user accounts efficiently, whether you’re working locally on a workstation or remotely across a server fleet.

We're talking about the bit that actually matters in practice.

New on the Blog

New Today

Neighboring Topics

Up Next

Thank you for reading about How To Switch Users In Linux. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home