How to Switch Users in Ubuntu (Beginner-Friendly Step-by-Step Guide)
How to Switch Users in Ubuntu (Beginner-Friendly Step-by-Step Guide)
Switching between users is a very common task in Ubuntu, especially when you’re managing a VPS, maintaining a server for multiple developers, or simply testing permissions under different accounts.
Whether you’re using Ubuntu on a desktop or running it headless on a cloud server, there are several reliable ways to switch users safely. This guide walks you through all practical methods, explains when to use each one, and highlights common mistakes to avoid.
Why You Might Need to Switch Users in Ubuntu
Before diving into commands, it helps to understand why switching users matters:
- Running administrative tasks as root or another privileged user
- Testing file permissions or application behavior under a different account
- Managing multi-user servers (shared VPS, staging environments)
- Improving security by avoiding unnecessary root access
Ubuntu is designed around the idea of least privilege, so switching users correctly is an essential skill.
Method 1: Switch User Using su
The su command (short for substitute user) allows you to switch to another user account.
Basic Syntax
su usernameExample:
su johnYou’ll be prompted to enter john’s password, and once authenticated, your shell will switch to that user.
Switch to Root User
su -or
su rootNote: On Ubuntu, the root account is disabled by default. If root login is not enabled, this command will fail.
Key Things to Know About su
Requires the target user’s password
Does not load the full login environment unless you use su -
Less commonly used on Ubuntu compared to sudo
Method 2: Switch User with sudo su (Most Common on Ubuntu)
Ubuntu encourages the use of sudo instead of direct root access.
Switch to Root Using sudo
sudo suor to load the full root environment:
sudo su -This uses your own password, not root’s password.
Switch to Another User with sudo
sudo su - usernameExample:
sudo su - deployThis is extremely useful on VPS servers where you don’t know (or don’t want to use) other users’ passwords.
Method 3: Use sudo -i (Recommended for Root Access)
If your goal is to become root, this is the cleanest and safest method:
sudo -iThis command:
Switches to root
Loads root’s environment
Preserves Ubuntu’s security model
For most administrators, sudo -i is preferred over su.
Method 4: Switch User Temporarily for a Single Command
If you don’t need a full shell, you can run a command as another user.
Run Command as Another User
sudo -u username commandExample:
sudo -u www-data ls /var/wwwThis is ideal for:
Testing permissions
Running scripts as service users
Avoiding unnecessary shell switching
Method 5: Switch Users in Ubuntu Desktop (GUI)
If you’re using Ubuntu with a graphical interface:
Click the system menu (top-right corner)
Select Log Out
Choose another user from the login screen
Some desktop environments also support Fast User Switching, allowing multiple users to stay logged in simultaneously.
Common Mistakes to Avoid
Using su without -, which may cause missing environment variables
Running daily tasks as root, increasing security risk
Forgetting to exit root, especially on production servers
Changing file ownership unintentionally while logged in as root
Always double-check which user you’re currently using:
whoamiFAQ:
- What’s the difference between su and sudo su?
su requires the target user’s password, while sudo su uses your password and respects Ubuntu’s permission system. On Ubuntu servers, sudo su is far more common.
- Why does su root not work on Ubuntu?
Ubuntu disables direct root login by default for security reasons. You should use sudo su or sudo -i instead.
- Which is better: sudo su or sudo -i?
For root access, sudo -i is generally recommended because it cleanly loads root’s environment and follows Ubuntu best practices.
- How do I know which user I’m currently logged in as?
Run:
whoamior
id- Can I switch users without a password?
Only if your account has sudo privileges. Otherwise, switching users always requires authentication.