Switching between users in Linux is done with a simple command to access different accounts. Understanding how to change users in linux is essential for system administrators, developers, and anyone sharing a computer. This guide covers every method, from basic commands to advanced tricks, ensuring you can switch users efficiently and securely.
Linux offers multiple ways to change users, each suited for different scenarios. Whether you need to run a single command as another user or fully log into a different account, the process is straightforward. Let’s start with the most common method.
Using The Su Command To Switch Users
The su (substitute user) command is the traditional way to change users in Linux. It allows you to switch to any user account, provided you have the password.
To switch to another user, open a terminal and type:
su - username
The hyphen (-) is crucial. It creates a login shell, loading the target user’s environment variables and home directory. Without it, you stay in your current environment.
Switching To The Root User
To become the root user (superuser), simply type:
su -
Or:
su - root
You’ll be prompted for the root password. Once entered, your prompt changes to #, indicating root privileges.
Running A Single Command As Another User
Sometimes you only need to execute one command as another user. Use the -c option:
su - username -c "command"
For example, to list files in another user’s home directory:
su - jane -c "ls -la /home/jane"
This runs the command and returns you to your original shell.
Exiting A User Session
To return to your original user, type exit or press Ctrl+D. This closes the su session.
Common issues with su:
- Forgotten password: You need the target user’s password, not your own
- No hyphen: Environment variables may not load correctly
- Permission denied: Ensure the target user exists and has a valid shell
Using The Sudo Command For User Switching
The sudo command offers more control and security. It allows authorized users to run commands as another user, typically root, without needing the target user’s password.
Running Commands As Another User With Sudo
The basic syntax is:
sudo -u username command
For instance, to create a file as user “bob”:
sudo -u bob touch /home/bob/newfile.txt
You’ll be prompted for your own password (not bob’s). This is a key security feature.
Switching To A User Shell With Sudo
To start a shell as another user:
sudo -u username -i
The -i flag simulates an initial login, loading the user’s environment. Alternatively:
sudo -u username -s
This starts a shell without changing the environment.
Switching To Root With Sudo
To become root:
sudo -i
Or:
sudo su -
The latter combines sudo and su, a common pattern for gaining root access.
Sudo Configuration With Visudo
The /etc/sudoers file controls who can use sudo. Always edit it with visudo to prevent syntax errors:
sudo visudo
Common configurations:
- Allow a user to run commands as any user:
username ALL=(ALL:ALL) ALL - Allow a group:
%groupname ALL=(ALL:ALL) ALL - Allow passwordless sudo:
username ALL=(ALL) NOPASSWD: ALL
How To Change Users In Linux Using Graphical Interface
Not everyone prefers the command line. Linux desktop environments offer graphical methods to switch users.
Using The System Menu
Most desktop environments (GNOME, KDE, Xfce) have a user switch option:
- Click the system menu (top-right corner)
- Select “Switch User” or “Log Out”
- Choose another user from the login screen
- Enter their password
This keeps your original session running in the background.
Using The Lock Screen
Press Super+L (Windows key + L) to lock the screen. Then click “Switch User” on the lock screen.
Fast User Switching
Some distributions support fast user switching, allowing multiple users to be logged in simultaneously. This is common in Ubuntu, Fedora, and Linux Mint.
Using The Login Command For Direct User Change
The login command is a low-level method. It’s rarely used directly but appears in scripts and recovery scenarios.
To switch users with login:
sudo login username
This prompts for the user’s password and starts a new login session. It’s similar to su - but more primitive.
Using The Ssh Command For Remote User Switching
If you’re managing a remote server, SSH allows you to change users after connecting.
Connecting As A Different User
Specify the user at connection time:
ssh username@hostname
For example:
ssh jane@192.168.1.100
You’ll be prompted for jane’s password.
Switching Users After SSH Login
Once connected, use su or sudo as normal. This is common for accessing root privileges on remote systems.
Managing User Sessions And Processes
When you switch users, understanding sessions and processes is important.
Checking Current User
Use whoami or id to verify your current user:
whoami
id
Listing Logged-In Users
The who and w commands show all active users:
who
w
This helps identify which users are currently using the system.
Running Processes As Another User
To start a background process as another user:
sudo -u username nohup command &
This keeps the process running even after you log out.
Security Considerations When Changing Users
Changing users carries security implications. Follow these best practices.
Use Sudo Instead Of Su When Possible
Sudo provides auditing and fine-grained permissions. All sudo commands are logged in /var/log/auth.log or /var/log/secure.
Avoid Sharing Passwords
Never share passwords between users. Use sudo with appropriate permissions instead.
Limit Root Access
Only grant root access to trusted users. Use groups and sudo rules to restrict commands.
Log Out After Use
Always exit user sessions when done. Leaving a root shell open is a security risk.
Troubleshooting Common User Switching Issues
Even experienced users encounter problems. Here are solutions to frequent issues.
Permission Denied Errors
If you get “Permission denied” when using su or sudo:
- Check if the user exists:
id username - Verify the user has a valid shell in
/etc/passwd - Ensure you’re in the sudoers file (for sudo)
Authentication Failure
If authentication fails:
- Double-check the password (remember, su uses the target user’s password)
- For sudo, use your own password
- Check if the account is locked:
sudo passwd -S username
Environment Variables Not Loading
If environment variables don’t load after switching users:
- Use the hyphen with su:
su - username - Use
-iwith sudo:sudo -u username -i - Manually source profile files:
source ~/.bashrc
Cannot Switch To Root
If you can’t switch to root:
- Ensure root account is enabled:
sudo passwd root - Use sudo instead:
sudo -i - Check if root login is restricted in
/etc/ssh/sshd_config(for remote)
Advanced User Switching Techniques
For power users, these techniques offer more flexibility.
Using Su With Environment Variables
Preserve specific environment variables when switching users:
su - username -c "export VAR=value; command"
Switching Users In Scripts
In bash scripts, use sudo -u to run commands as another user:
#!/bin/bash
sudo -u www-data php /var/www/script.php
Using The Runuser Command
Some distributions include runuser, similar to su but for service accounts:
runuser -l username -c 'command'
Creating Aliases For Frequent Switches
Add aliases to your .bashrc for common switches:
alias tojane='sudo -u jane -i'
alias toroot='sudo -i'
Comparing User Switching Methods
Here’s a quick comparison of the main methods:
| Method | Requires Password | Loads Environment | Best For |
|---|---|---|---|
| su – | Target user’s | Yes | Full user switch |
| sudo -u | Your own | With -i flag | Single commands |
| sudo -i | Your own | Yes | Root shell |
| Graphical | Target user’s | Yes | Desktop users |
| SSH | Target user’s | Yes | Remote access |
Best Practices For User Management
Effective user management goes beyond switching. Follow these practices.
Use Groups For Permission Management
Instead of switching users, add users to groups:
sudo usermod -aG groupname username
Implement Sudo Rules Carefully
Grant minimal necessary permissions. Use command-specific sudo rules:
username ALL=(ALL) /usr/bin/systemctl, /usr/bin/journalctl
Regularly Audit User Activity
Check logs for unusual user switches:
sudo grep 'su\|sudo' /var/log/auth.log
Disable Unused User Accounts
Lock accounts that are no longer needed:
sudo usermod -L username
How To Change Users In Linux On Different Distributions
While the commands are similar, some distributions have quirks.
Ubuntu And Debian
Ubuntu disables the root account by default. Use sudo for all administrative tasks. To enable root:
sudo passwd root
Fedora And Red Hat
Fedora uses sudo but also allows direct root login. The wheel group grants sudo access:
sudo usermod -aG wheel username
Arch Linux
Arch requires manual sudo configuration. Install sudo and edit /etc/sudoers with visudo.
OpenSUSE
OpenSUSE uses sudo similarly. The sudo group is pre-configured.
Automating User Switching With Scripts
For repetitive tasks, automate user switching.
Simple Script To Run Commands As Another User
#!/bin/bash
# run_as_user.sh
if [ $# -lt 2 ]; then
echo "Usage: $0 username command"
exit 1
fi
sudo -u "$1" "${@:2}"
Script To Switch Users And Execute Multiple Commands
#!/bin/bash
sudo -u jane bash -c '
cd /home/jane
ls -la
echo "Running as jane"
'
Understanding User IDs And Groups
Linux identifies users by numeric IDs. Understanding this helps with switching.
Viewing User And Group IDs
Use id to see all IDs:
id username
Output shows UID (user ID), GID (primary group ID), and supplementary groups.
Switching By UID
You can switch users by UID:
su - $(id -un 1001)
This switches to the user with UID 1001.
Common Mistakes When Changing Users
Avoid these frequent errors.
Forgetting The Hyphen With Su
Without the hyphen, environment variables don’t load. This causes path issues and missing aliases.
Using Sudo Without Proper Configuration
If sudo isn’t configured, you’ll get “user is not in the sudoers file.” Fix this by adding the user to the sudo group.
Staying In Root Shell
Forgetting to exit root shells can lead to accidental system changes. Always type exit when done.
Mixing Up Passwords
Remember: su requires the target user’s password; sudo requires your password.
Frequently Asked Questions
What Is The Difference Between Su And Sudo?
Su switches to another user and requires that user’s password. Sudo runs commands as another user using your own password, with permissions defined in /etc/sudoers.
Can I Change Users Without A Password?
Yes, if you have sudo access configured with NOPASSWD in /etc/sudoers. You can also use su if you know the target user’s password.
How Do I Switch To Root User In Linux?
Use sudo -i or su -. On Ubuntu, use sudo -i since the root account is disabled by default.
What Command Shows The Current User In Linux?
Use whoami or id. The who command shows all logged-in users.
How To Change Users In Linux Without Logging Out?
Use