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:
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_usernameto 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
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:
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:
You can switch back to your logged-in user by entering:
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.
However, it will ask for the password of the
other_username. Remember that nothing is displayed on the screen when you type the password.
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
Again, to switch to the user abhishek, I used sudo with user abhi's password.
suwithout 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
su -l other_username
This way, you start a shell as the login shell of the other user.
You can use
-p option instead of
-l to preserve the environment instead of testing them in the new shell.
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:
When asked, enter the root password:
abhi@learnubuntu:/root$ su Password: root@learnubuntu:~#
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:
When asked for the password, enter your account's password:
abhi@learnubuntu:~$ sudo su [sudo] password for abhi: root@learnubuntu:/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.
root@learnubuntu:/home/abhishek# su -c "touch a.txt" abhishek root@learnubuntu:/home/abhishek# ls -l a.txt -rw-rw-r-- 1 abhishek abhishek 0 Aug 23 07:56 a.txt root@learnubuntu:/home/abhishek#
I hope you like this handy Ubuntu tutorial.