Changing users on Linux lets you run commands under different permissions and profiles. If you’ve ever wondered how to change users linux efficiently, you’re in the right place. This guide covers every method, from the basic su command to advanced sudo tricks, so you can switch accounts like a pro.
Linux is built around multi-user environments. You might need to test something as another user, fix a permission issue, or run a script with elevated rights. Whatever the reason, knowing the right commands saves time and prevents mistakes.
Let’s start with the most common tools and work our way to less obvious techniques. By the end, you’ll have a complete toolkit for user switching.
How To Change Users Linux
The primary way to switch users is the su command. It stands for “substitute user” or “switch user.” You’ll use it constantly once you learn it.
Using The Su Command
Open a terminal. Type su followed by the username you want to switch to. For example:
su john
You’ll be prompted for john’s password. Enter it, and your shell becomes john’s. Your prompt changes to reflect the new user.
To switch to the root user (the superuser), just type:
su
Or:
su root
Both do the same thing. You’ll need the root password, not your own.
Key Options For Su
- -l or –login: Starts a login shell, loading the user’s environment variables and profile. Use
su - johnfor a clean environment. - -c: Runs a single command as the target user. Example:
su -c "whoami" johnprints “john”. - -s: Specifies a shell to use.
su -s /bin/zsh johnswitches to john with Zsh.
Without the -l flag, you stay in your current directory and keep some environment settings. This can cause confusion if the other user has different paths or aliases.
Using Sudo To Switch Users
sudo is more flexible than su. It lets authorized users run commands as others without knowing their passwords. This is the modern, safer approach.
To switch to another user with sudo, use:
sudo -u john -i
The -i flag simulates an initial login, loading john’s environment. Without it, you just run commands as john in your current shell.
To run a single command:
sudo -u john whoami
This prints “john” without fully switching users.
Sudo Vs Su: When To Use Each
- su: Best when you need an interactive session as another user and know their password. Works even if sudo isn’t configured.
- sudo: Preferred for one-off commands or when you don’t want to share passwords. Requires sudo privileges.
On many modern distributions like Ubuntu, the root account is locked by default. You can’t use su to become root without setting a root password first. Instead, use sudo -i or sudo su.
Switching To Root With Sudo
To get a root shell using sudo, run:
sudo -i
Or:
sudo su
Both give you a root prompt. The difference is subtle: sudo -i starts a login shell, while sudo su uses your current environment.
For most tasks, either works fine. Use sudo -i if you want a clean root environment.
Using The Login Command
The login command is an older method. It completely logs out your current session and starts a new one as the specified user. Use it like this:
login john
You’ll be asked for john’s password. After that, your entire terminal session becomes john’s. This is useful for testing login scripts or starting fresh.
Be careful: login ends your current session. You can’t return to your original user without logging in again.
Switching Users Without A Password
Sometimes you need to switch to a user without typing their password. This is common in scripts or automated tasks. Here are two ways:
1. Using Sudo With Nopasswd
Edit the sudoers file with sudo visudo. Add a line like:
yourusername ALL=(ALL) NOPASSWD: /bin/su - john
Now you can run sudo su - john without a password prompt. Be very careful with this—it’s a security risk.
2. Using Ssh
If you have SSH keys set up, you can switch users with:
ssh john@localhost
This logs you in as john using key authentication. It works even if john’s password is unknown.
Checking Current User
Before switching, it’s smart to confirm who you are. Use these commands:
whoami– prints your usernameid– shows user ID, group ID, and groupswho– lists logged-in users
After switching, run whoami to verify the change worked.
Practical Examples
Let’s walk through real-world scenarios. These examples show how to change users linux in daily tasks.
Example 1: Testing A Script As Another User
You wrote a script that only certain users should run. Test it as user “alice”:
sudo -u alice ./myscript.sh
This runs the script with alice’s permissions. If it fails, you know the issue is permission-related.
Example 2: Fixing A Misconfigured File
User “bob” has a broken .bashrc. Switch to bob to fix it:
su - bob
nano ~/.bashrc
Now you’re editing bob’s file directly. Save and exit, then type exit to return to your own account.
Example 3: Running A Command As Root
Need to install a package? Use sudo:
sudo apt update && sudo apt upgrade
Or switch to root first:
sudo -i
apt update && apt upgrade
exit
Both work. The second method is useful for multiple root commands.
Common Mistakes And Fixes
Even experienced users make errors. Here are frequent pitfalls:
- Wrong password: You need the target user’s password, not your own. For sudo, you use your password.
- Environment issues: Without
-lor-i, you might get the wrong PATH or aliases. Always use login flags for clean sessions. - Permission denied: If sudo fails, check the sudoers file. You might not be in the sudo group.
- Stuck in a shell: Type
exitor press Ctrl+D to return to your original user.
Advanced Techniques
For power users, here are more methods to switch users.
Using Runuser
The runuser command is similar to su but designed for scripts. It doesn’t require a password and is often used in init scripts. Example:
runuser -l john -c 'whoami'
This runs whoami as john. Note: runuser is not available on all distributions.
Using Machinectl (Systemd)
If you use systemd, machinectl can switch users in containers or VMs. For local users, it’s overkill but possible:
machinectl shell john@
This opens a shell as john. It’s rarely used for everyday tasks.
Using Tmux Or Screen
You can switch users inside a terminal multiplexer. Start a session as root, then create a new window as another user. This keeps both sessions active.
tmux new -s root
# inside tmux, press Ctrl+B, then c
su - john
Now you have two windows: one as root, one as john. Switch between them with Ctrl+B and the window number.
Security Considerations
Switching users is powerful but risky. Follow these best practices:
- Limit sudo access: Only give sudo to trusted users. Use the sudoers file to restrict commands.
- Avoid root logins: Use
sudofor most tasks. Root shells can cause accidental damage. - Log commands: Enable sudo logging to track who did what. Check
/var/log/auth.log. - Use groups: Instead of switching to a user, add permissions to a group. This reduces the need for user switching.
Scripting User Switches
Automating user switches is common in system administration. Here’s a simple script that runs commands as multiple users:
#!/bin/bash
users=("alice" "bob" "charlie")
for user in "${users[@]}"; do
sudo -u "$user" whoami
done
This prints each user’s name. You can replace whoami with any command.
For scripts that need root, use sudo inside the script. But be careful: scripts with sudo can be dangerous if not tested.
Troubleshooting User Switching
Sometimes things go wrong. Here’s how to fix common issues:
Problem: “User unknown”
You typed su nonexistentuser and got an error. Check if the user exists:
cat /etc/passwd | grep nonexistentuser
If not, create the user with sudo useradd nonexistentuser.
Problem: “Permission denied” With Sudo
You’re not in the sudo group. Add yourself:
sudo usermod -aG sudo yourusername
Log out and back in for the change to take effect.
Problem: “Authentication failure”
You mistyped the password. Try again slowly. If it persists, the account might be locked. Check with sudo passwd -S username.
Comparing User Switching Methods
| Method | Requires Password | Loads Environment | Best For |
|---|---|---|---|
| su | Target user’s | With -l | Interactive sessions |
| sudo -u | Your own | With -i | Single commands |
| login | Target user’s | Yes | Fresh login |
| ssh localhost | Key or password | Yes | Remote-like access |
Choose based on your need. For quick tasks, use sudo -u. For full sessions, use su -.
Frequently Asked Questions
What is the difference between su and sudo?
su switches to another user and requires their password. sudo runs commands as another user using your own password. sudo is more secure and granular.
How do I switch to root without a password?
If you have sudo privileges, use sudo -i. This gives you a root shell without needing the root password. Alternatively, configure sudo with NOPASSWD.
Can I switch users without leaving the current directory?
Yes, use su username without the -l flag. You’ll stay in your current directory. But be aware that environment variables might not match the target user’s settings.
How to change users linux in a script?
Use sudo -u username command inside the script. For multiple commands, wrap them in a subshell: sudo -u username bash -c 'command1; command2'.
What if I forget to exit a user session?
Type exit or press Ctrl+D. If you’re in a nested shell, you might need to exit multiple times. Check your prompt to see which user you are.
Final Tips
Mastering user switching makes you more efficient on Linux. Practice with non-root users first to avoid accidents. Use whoami frequently to stay aware of your current identity.
Remember: the goal is to run commands with the right permissions, not to impersonate others. Always switch back to your own account when done.
With these techniques, you can handle any multi-user task. Whether you’re a beginner or a sysadmin, knowing how to change users linux is an essential skill.
Now go ahead and try it. Open a terminal, create a test user, and switch back and forth. You’ll get comfortable in no time.