How to Switch Users in Ubuntu Safely (Complete Guide for Server & VPS Environments)
User switching is a fundamental skill when working with Ubuntu, especially in server and VPS environments where security, permissions, and isolation matter. Whether you are performing administrative maintenance, debugging permission issues, or managing multiple accounts on the same system, knowing how to switch users correctly can prevent mistakes and reduce risk.
This guide explains every practical way to switch users in Ubuntu, clarifies when each method should be used, and focuses on best practices for production servers.
Understanding User Switching in Ubuntu
Ubuntu is designed around controlled privilege escalation. Unlike some Linux distributions, direct root access is disabled by default, and most administrative tasks are handled through sudo.
Switching users allows you to:
- Execute tasks with the correct permission level
- Avoid running applications as root unnecessarily
- Test application behavior under different user contexts
- Maintain better security on shared or production systems
Before switching users, it’s always important to know who you are currently logged in as:
whoami
Method 1: Switching Users with su
The su command allows you to become another user by providing that user’s password.
Basic Usage
su username
Example:
su alex
Once authenticated, your shell session changes to that user.
Using a Login Shell
su - username
This loads the full login environment, including:
-
User-specific environment variables
-
Home directory settings
-
Default shell configuration
Important Notes About su
-
Requires the target user’s password
-
Not commonly recommended on Ubuntu servers
-
Root access via su is usually disabled by default
Method 2: Switching Users with sudo su
On Ubuntu systems, sudo is the preferred access control mechanism.
Switch to Root
sudo su
Or with full environment initialization:
sudo su -
This approach:
-
Uses your own password
-
Respects sudo permissions
-
Avoids enabling direct root login
Switch to Another User
sudo su - username
Example:
sudo su - deploy
This method is widely used on VPS servers where only one administrative account exists.
Method 3: Using sudo -i (Best Practice for Root Access)
For administrative work, this is often the cleanest option:
sudo -i
It provides:
-
A root login shell
-
Correct environment variables
-
Behavior closest to a real root session
For long maintenance sessions, sudo -i is generally safer and more predictable than sudo su.
Method 4: Running Commands as Another User (Without Switching Shells)
If you only need to execute a single command, switching shells is unnecessary.
Run a Command as a Specific User
sudo -u username command
Example:
sudo -u www-data php artisan cache:clear
This method is ideal for:
-
Service accounts
-
Automation scripts
-
Permission testing
-
CI/CD workflows
Method 5: Switching Users in Desktop Ubuntu
On systems with a graphical interface:
-
Log out from the current session
-
Select another user from the login screen
-
Log in normally
Some desktop environments support concurrent sessions, but on servers this method is rarely relevant.
Common Security Considerations
-
Avoid staying logged in as root longer than necessary
-
Always confirm your active user before modifying files
-
Be cautious when creating files as root inside user directories
-
Use sudo -u for scripts instead of full shell switching
To verify identity and privileges:
id
FAQ
What is the safest way to become root on Ubuntu?
The recommended method is sudo -i, as it follows Ubuntu’s security model and initializes the root environment correctly.
Why does su root fail on most Ubuntu systems?
Ubuntu disables direct root logins by default. Administrative access is intentionally routed through sudo.
When should I use sudo -u instead of switching users?
If you only need to run a specific command or script, sudo -u is safer and avoids unnecessary privilege escalation.
How do I check which user executed a command?
Use whoami for the current shell or inspect logs that record the effective user ID.
Can I switch users without a password?
Only users with sudo privileges can do so. Ubuntu always enforces authentication to prevent unauthorized access.
