Managing user sessions in Linux involves understanding the difference between su and sudo. If you’re wondering how to switch users in linux, you’ve come to the right place. This guide will walk you through every method, from basic commands to advanced tricks. Let’s get started.
Linux is built around multi-user support. That means you can have several people—or just yourself—using the same system with different permissions. Switching users is a core skill for any sysadmin or power user. It’s simple once you know the tools.
You might need to switch to a root account for admin tasks. Or maybe you want to test something as another user. Whatever the reason, Linux gives you several ways to do it. We’ll cover them all here.
How To Switch Users In Linux
The most common command for switching users is su. It stands for “substitute user” or “switch user.” With su, you can become any other user on the system. You just need their password.
Let’s look at the basic syntax:
su - username
The hyphen (-) is important. It tells Linux to start a login shell for that user. This means their environment variables, home directory, and shell settings are loaded. Without it, you stay in your current shell’s environment.
For example, to switch to a user named john:
su - john
You’ll be prompted for john’s password. Enter it, and you’re now john. Type exit or press Ctrl+D to return to your original user.
Switching To The Root User
Root is the superuser with full system access. To switch to root, use:
su -
Or:
su - root
Both do the same thing. You’ll need the root password. Once in, you can run any command without restrictions.
Be careful with root. One wrong command can break your system. Use it only when necessary.
Using Sudo To Switch Users
sudo is another way to switch users. It stands for “superuser do.” Unlike su, sudo uses your own password, not the target user’s. This is more secure in shared environments.
To switch to another user with sudo:
sudo -u username -i
The -i flag starts a login shell for that user. For example:
sudo -u jane -i
You’ll be asked for your own password (if you have sudo privileges). Then you’re jane.
You can also run a single command as another user:
sudo -u username command
Like:
sudo -u jane whoami
This prints “jane” without switching your session.
Difference Between Su And Sudo
Here’s a quick comparison:
- su: Requires the target user’s password. Gives you a full shell.
- sudo: Requires your own password. Can run single commands or start a shell.
- Security: Sudo is better for auditing. It logs every command. Su doesn’t.
- Flexibility: Sudo allows fine-grained permissions. You can limit what commands a user can run.
Most modern Linux distros recommend sudo over su. But both work fine.
Switching Users Without A Password
Sometimes you want to switch users without entering a password. This is usefull for scripts or automation. You can do it with sudo by configuring the sudoers file.
Edit the sudoers file safely with visudo:
sudo visudo
Add a line like this:
yourusername ALL=(targetuser) NOPASSWD: ALL
Replace yourusername with your account name and targetuser with the user you want to switch to. Save and exit. Now you can run:
sudo -u targetuser -i
Without a password prompt.
Be careful with this. It weakens security. Only use it if you understand the risks.
Checking Current User
Before switching, you might want to know who you are. Use:
whoami
Or:
id
The id command shows your user ID, group ID, and groups. It’s handy for debugging.
Listing All Users On The System
To see all users, look at the /etc/passwd file:
cat /etc/passwd
This shows a list of usernames and their details. You can also use:
getent passwd
This queries the system database. It’s more reliable on some systems.
Switching Users With SSH
If you’re logged in remotely via SSH, you can still switch users. The same commands work: su and sudo. But be aware of session management.
When you switch users over SSH, your SSH connection stays active. The new user session runs inside it. Type exit to return to your original user, then exit again to disconnect SSH.
Using Login Shells Vs Non-Login Shells
When you switch users, you can choose between a login shell and a non-login shell. A login shell sources the user’s profile files (.profile, .bash_profile, etc.). A non-login shell doesn’t.
Use the hyphen (-) for a login shell:
su - username
Without it, you get a non-login shell:
su username
For sudo, use -i for a login shell:
sudo -u username -i
Or -s for a non-login shell:
sudo -u username -s
Login shells are recommended for full environment setup.
Switching Users With Graphical Interface
If you’re using a desktop environment like GNOME or KDE, you can switch users from the GUI. Look for the “Switch User” option in the system menu. This logs out your current session and shows the login screen for another user.
Alternatively, you can use the terminal even in GUI mode. Open a terminal and use su or sudo as described.
Common Errors And Fixes
Here are some issues you might face:
- “su: Authentication failure”: You entered the wrong password. Double-check caps lock.
- “sudo: unknown user”: The username doesn’t exist. Check
/etc/passwd. - “Permission denied”: You don’t have sudo privileges. Ask your admin to add you to the sudo group.
- “Cannot execute”: The target user’s shell might be broken. Check
/etc/passwdfor their shell path.
Best Practices For Switching Users
Follow these tips to stay safe:
- Use
sudoinstead ofsuwhen possible. It logs actions. - Avoid staying as root for long. Switch back when done.
- Use strong passwords for all users.
- Limit sudo access to specific commands.
- Regularly audit user accounts and sessions.
Switching Users In Scripts
Automation often requires switching users. In bash scripts, you can use sudo -u to run commands as another user. For example:
#!/bin/bash
sudo -u john /path/to/script.sh
If you need a full session, use sudo -u john -i with a here-document:
sudo -u john -i <<'EOF'
echo "Running as john"
whoami
EOF
This runs multiple commands as john.
Switching Users With Runuser
Some systems have the runuser command. It’s similar to sudo -u but designed for system services. Use it like this:
runuser -l username -c 'command'
It’s less common than su or sudo. Check your distro’s documentation.
Switching Users With Ssh
You can also switch users by SSH-ing into localhost:
ssh username@localhost
This creates a new login session. You’ll need the user’s password. It’s overkill for most tasks but works.
Understanding User IDs
Every user has a numeric ID (UID). Root has UID 0. Regular users start at 1000 (usually). You can switch to a user by UID:
su - #1001
But it’s easier to use the username.
Switching Users And Environment Variables
When you switch users, environment variables change. This can affect programs. For example, PATH might be different. Use login shells to get the correct environment.
Check environment with:
env
Or:
printenv
Switching Users With Pkexec
For graphical applications, pkexec is an alternative to sudo. It runs commands as another user with a GUI password prompt:
pkexec -u username command
It’s part of PolicyKit. Not all distros have it installed.
Switching Users In Docker Containers
Inside a Docker container, you can switch users with su or sudo if installed. But containers often run as root by default. Use the USER directive in Dockerfiles instead.
Switching Users With Tmux Or Screen
If you use terminal multiplexers like tmux or screen, you can switch users inside a session. Just run su or sudo as normal. The multiplexer keeps your session alive.
Switching Users And File Permissions
When you switch users, you inherit their file permissions. You can read, write, and execute files based on their ownership. Use ls -l to check permissions.
Switching Users With Sudoers Aliases
For complex setups, you can define user aliases in the sudoers file. For example:
User_Alias ADMINS = alice, bob
ADMINS ALL=(ALL) ALL
This gives alice and bob full sudo access.
Switching Users And Logging
Sudo logs all commands to /var/log/auth.log (or /var/log/secure on some systems). Use this to audit user switches. Su doesn’t log by default.
Switching Users With Su And Sudo Combined
You can combine both commands. For example, use sudo su - to become root without the root password. This is common on Ubuntu where root is disabled.
Switching Users In Recovery Mode
If you’re in recovery mode, you might be root automatically. Use su to switch to other users if needed.
Switching Users And Shells
Each user has a default shell defined in /etc/passwd. When you switch, you get that shell. You can override it with:
su - username -s /bin/bash
Or for sudo:
sudo -u username -s /bin/bash
Switching Users And Home Directories
When you switch to a user, you’re placed in their home directory (with a login shell). Use pwd to confirm. You can navigate elsewhere with cd.
Switching Users With Ssh Keys
If you have SSH keys set up, you can switch users without a password. Add your public key to the target user’s ~/.ssh/authorized_keys. Then use:
ssh username@localhost
This works without a password if key authentication is enabled.
Switching Users And Sudoers Groups
Add users to the sudo or wheel group to grant sudo access:
sudo usermod -aG sudo username
Then they can use sudo to switch users.
Switching Users In Cron Jobs
Cron jobs run as the user who creates them. To run a job as another user, edit their crontab:
sudo crontab -u username -e
Switching Users And Systemd Services
Systemd services can run as any user. Set the User= directive in the service file. Then use systemctl to manage it.
Switching Users With Su -C
The -c flag lets you run a single command:
su - username -c 'command'
This is usefull for one-off tasks.
Switching Users With Sudo -U
We covered this earlier. It’s the most flexible method for single commands.
Switching Users And Environment Cleanup
When switching users, you might carry over environment variables. Use env -i to start clean:
sudo -u username env -i command
Switching Users With Chroot
For advanced isolation, you can use chroot to switch to a different filesystem as another user. This is beyond most use cases.
Switching Users And Sudoers Timeout
Sudo caches your password for a few minutes. You can adjust this in /etc/sudoers with the timestamp_timeout setting.
Switching Users With Su And Sudo In Combination
Some admins use sudo su - to become root. This uses sudo’s logging while giving you a root shell.
Switching Users And Sudoers Defaults
You can set defaults like env_reset to clean environment variables. This improves security.
Switching Users With Pseudo Terminals
When you switch users, Linux allocates a pseudo terminal (PTY). This allows interactive commands like vim to work.
Switching Users And Sudoers Logging
Enable logging with logfile in sudoers. This records every sudo command.
Switching Users With Su And Sudo In Scripts
In scripts, use sudo