How To Switch User In Linux : Linux User Account Switching

Changing user accounts in Linux can be accomplished through the graphical settings or command line. Knowing how to switch user in Linux is essential for managing multiple accounts, testing permissions, or accessing different environments without logging out. This guide covers every method, from GUI clicks to terminal commands, ensuring you can move between users smoothly and securely.

Whether you are a system administrator or a regular user, switching accounts is a common task. Linux offers several ways to do this, each suited for different scenarios. You will learn the fastest approaches, how to avoid common mistakes, and tips for keeping your session secure.

How To Switch User In Linux

The core methods for switching users involve the su and sudo commands, as well as graphical options in desktop environments. Below, we break down each technique with step-by-step instructions.

Using The Su Command

The su command (short for “substitute user”) is the most traditional way to switch accounts. It requires the target user’s password unless you run it with sudo privileges.

  1. Open a terminal window.
  2. Type su - username and press Enter. Replace “username” with the actual account name.
  3. Enter the password for that user when prompted.
  4. You are now logged in as that user. The hyphen (-) ensures a clean environment, loading the user’s profile.

To return to your original user, type exit or press Ctrl+D. This method works on all Linux distributions, including Ubuntu, Fedora, and Debian.

Using The Sudo Command

If you have sudo privileges, you can switch to another user without knowing their password. This is useful for administrators.

  1. In the terminal, type sudo su - username.
  2. Enter your own password (not the target user’s).
  3. You are now in the target user’s shell.

Alternatively, use sudo -i -u username to start a login shell as that user. This method is faster and more secure because it logs the action in system logs.

Using The Login Command

The login command starts a new login session, effectively logging you out of the current user. Use it when you want a fresh environment.

  1. Type login username in the terminal.
  2. Enter the user’s password.
  3. The session switches completely, and you cannot return to the previous user without logging in again.

This method is less common but useful for testing login scripts or troubleshooting profile issues.

Graphical User Interface Methods

Most Linux desktop environments offer a graphical way to switch users. Here are the steps for common desktops:

  • GNOME: Click the system menu (top-right corner), select “Switch User” from the power options. A new login screen appears.
  • KDE Plasma: Open the application launcher, click “Leave,” then choose “Switch User.”
  • XFCE: Click the user menu or use the “Switch User” option in the session menu.
  • Unity (Ubuntu): Click the gear icon in the top-right, select “Switch User Account.”

Graphical switching keeps your original session running in the background. This is ideal for sharing a computer without closing applications.

Switching Users Without A Password

Sometimes you need to switch to a user without typing a password, such as for automation or script testing. This requires careful configuration.

Using Sudoers Configuration

Edit the sudoers file with sudo visudo. Add a line like:

yourusername ALL=(targetuser) NOPASSWD: ALL

Replace “yourusername” and “targetuser” with actual names. Now you can run sudo -u targetuser command without a password.

Be cautious: this grants broad privileges. Use specific commands instead of “ALL” for better security.

Using Ssh Keys

For remote switching, set up SSH key authentication. Generate a key pair with ssh-keygen, then copy the public key to the target user’s ~/.ssh/authorized_keys file.

Now you can switch with ssh targetuser@localhost without a password. This method is secure and script-friendly.

Common Pitfalls And Solutions

Switching users seems simple, but users often encounter issues. Here are frequent problems and how to fix them.

Permission Denied Errors

If you see “Permission denied” when using su, you likely entered the wrong password. Double-check caps lock and keyboard layout. For sudo, ensure your user is in the sudo group.

Environment Variables Not Loading

Using su username without the hyphen keeps the original user’s environment. This can cause unexpected behavior. Always use su - username to load the target user’s profile.

Session Locked After Switch

Graphical switching sometimes leaves the screen locked. Press the switch user button again or use Ctrl+Alt+F1 to access a virtual console. Log in there and restart the display manager if needed.

Cannot Switch To Root

If su - fails, root account might be disabled. Use sudo -i instead, which gives you a root shell with your own password.

Switching Users In Scripts

Automating user switches requires careful handling of passwords and permissions. Here are safe approaches.

Using Expect Scripts

The expect tool can automate password entry. Install it with sudo apt install expect (Debian/Ubuntu) or sudo dnf install expect (Fedora).

Example script:

#!/usr/bin/expect
spawn su - targetuser
expect "Password:"
send "targetpassword\r"
interact

This is not recommended for production because passwords are visible in plaintext. Use SSH keys or sudoers instead.

Using Runuser Command

The runuser command runs a command as another user without a password, but only root can use it. Example: runuser -l targetuser -c 'command'.

This is useful in systemd service files or startup scripts where root privileges are available.

Security Considerations

Switching users can expose your system to risks if done carelessly. Follow these best practices.

Avoid Storing Passwords

Never hardcode passwords in scripts. Use environment variables or encrypted files if absolutely necessary. Prefer passwordless methods like sudoers or SSH keys.

Limit Sudo Access

Grant sudo privileges only to trusted users. Use the sudoers file to restrict which commands each user can run. For example:

username ALL=(ALL) /usr/bin/apt, /usr/bin/systemctl

This allows only specific commands, reducing the risk of abuse.

Monitor Logs

Check /var/log/auth.log (or /var/log/secure on some distributions) for su and sudo usage. Unusual patterns may indicate a security breach.

Advanced Techniques

For power users, there are additional ways to manage user switching.

Using Tmux Or Screen

Terminal multiplexers like tmux or screen allow you to have multiple sessions as different users. Start a session as one user, then detach and start another as a different user.

Example with tmux:

  1. Open tmux as user1: tmux new -s session1
  2. Detach with Ctrl+B, D.
  3. Switch to user2: su - user2
  4. Start another tmux session: tmux new -s session2
  5. Switch between sessions with tmux attach -t session1 or session2.

This method keeps both users active without logging out.

Using Systemd Run

Systemd can run commands as a different user using systemd-run. Example:

sudo systemd-run --user --uid=1001 /path/to/command

Replace 1001 with the target user’s UID. This is useful for running services or one-off tasks in a clean environment.

Troubleshooting Common Errors

Even experienced users hit snags. Here are fixes for frequent error messages.

“User Does Not Exist”

Double-check the username spelling. Use cat /etc/passwd to list all users. Case matters: “John” and “john” are different.

“Authentication Failure”

This usually means wrong password. If you are sure the password is correct, check if the account is locked with sudo passwd -S username. Unlock it with sudo passwd -u username.

“Cannot Execute Command”

When using sudo -u username command, the command might not be in the user’s PATH. Use the full path, e.g., /usr/bin/command.

“No Passwd Entry For User”

This occurs when the user exists but has no login shell. Check /etc/passwd and ensure the shell field is valid (e.g., /bin/bash).

Frequently Asked Questions

What is the difference between su and sudo?

su switches to another user and requires that user’s password. sudo runs a single command as another user using your own password. sudo su - combines both.

Can I switch user without logging out?

Yes, using the graphical “Switch User” option or the su command in a terminal. Your original session remains active in the background.

How do I switch to root user?

Use su - (requires root password) or sudo -i (requires sudo privileges). On Ubuntu, root account is disabled by default, so use sudo -i.

Why does su ask for a password even though I am root?

Root can switch to any user without a password using su username. If it asks, you might not be root. Check with whoami.

Is it safe to switch users frequently?

Yes, it is safe as long as you follow security practices. Avoid leaving sessions unlocked, and log out when done to prevent unauthorized access.

Conclusion

Mastering how to switch user in Linux is a fundamental skill that enhances your workflow and system management. From the simple su command to graphical interfaces and advanced automation, you now have a complete toolkit. Practice each method in a safe environment, and always prioritize security by limiting privileges and monitoring logs. With these techniques, you can navigate multiple user accounts efficiently, whether for personal use or enterprise administration.