How to Switch Users in Ubuntu

Learn how to change to another user in Ubuntu, along with a few useful tips on switching users.

Running as root and want to switch to a normal user or vice versa?

Or perhaps you want to switch to a test user you had created earlier.

In its simplest form, you can change to another user in this way:

su other_username

A few things you should know here:

  • If you were logged in as a normal user, you'd be asked to enter the password of the other user
  • The root user can switch to other users without needing the passwords of those accounts
  • If you have sudo access, you can use sudo su other_username to switch user without the password of the other user

By the way, a better way to switch users with their own login shell and environment would be to use the option -l:

su -l other_username

Intrigued? Let's see all the above in detail, along with a few additional tips and use cases on switching users in Ubuntu.

πŸ—’οΈ You need to know the exact username to which you need to switch to. You can list the users in Ubuntu with the following command and get the desired username from the bottom of its output:

compgen -u

Switching to a normal user from root user

If you are running as the root user, you can simply switch to the other user with its name:

su other_username
Switching to other user in Ubuntu

You can switch back to your logged-in user by entering:

exit
Customer Referral Landing Page - $100
Cut Your Cloud Bills in Half Deploy more with Linux virtual machines, global infrastructure, and simple pricing. No surprise bills, no lock-in, and the
Get started on Linode with a $100, 60-day credit for new users.

Switch to another user from a regular user

If you are logged in as a regular user and want to switch to another user, you can use the same command that you saw previously.

su other_username

However, it will ask for the password of the other_username. Remember that nothing is displayed on the screen when you type the password.

Change users in Ubuntu

If you do not know the password of the other user account, there is still a way around it. If you are a sudo user, you can use the sudo command, enter your own account's password and then change to the other user without entering its password.

sudo su other_username
Change to other user with sudo

Again, to switch to the user abhishek, I used sudo with user abhi's password.

πŸ’‘
If you use su without any username, you switch to the root user.

Switching users into their login shell

Did you notice that when you switched the users, the starting point was the same working directory where you used the su command?

You can start from the other user's home directory when you switch to it. Just use the -l option.

su -l other_username

This way, you start a shell as the login shell of the other user.

Switch user with login shell

You can use -p option instead of -l to preserve the environment instead of testing them in the new shell.

How to Change User Password in Ubuntu Command Line
Forgot your own password or have to reset it for others? Here’s how to change the password in the Ubuntu command line.

Switch to root user

There are several ways to switch to the root user. Since we are discussing the su command here, let's use that only.

If you know the root password, you can switch to root user simply with:

su

When asked, enter the root password:

[email protected]:/root$ su
Password: 
[email protected]:~#

If you don't know the root password but have sudo access (default sudo configuration that comes in most Ubuntu system), you can switch to the root user like this:

sudo su

When asked for the password, enter your account's password:

[email protected]:~$ sudo su
[sudo] password for abhi: 
[email protected]:/home/abhi# 

Bonus tip: Run a command as another user

If the entire idea of switching to another user is to run a command, you don't necessarily change the user accounts.

You can use the -c and run the given command as another user:

su -c command_to_run user_name

The command should be a single argument and hence you should keep it under quotes.

[email protected]:/home/abhishek# su -c "touch a.txt" abhishek
[email protected]:/home/abhishek# ls -l a.txt
-rw-rw-r-- 1 abhishek abhishek 0 Aug 23 07:56 a.txt
[email protected]:/home/abhishek# 

I hope you like this handy Ubuntu tutorial.