How to switch from root to another user in Ubuntu terminal?

In Ubuntu and any other Linux you are using, the root user is a superuser and quite important. Because this Linux user account has all files and service access. This is the reason, it is not recommended to use a root user account for regular system tasks as it could pose a security risk. Instead of using root, a user can create a separate account with limited access and privileges.

Also, in Ubuntu, all users have a unique user ID or UID and a set of permissions that specify what activities the user can perform on the system. The root user account has a UID of 0 and full access to all files and directories on the system. In this tutorial, we discuss how you can switch to another standard Linux user account from root in Ubuntu distros.

To switch from the root user account to another user account, follow these steps:

Step 1: Open your Ubuntu terminal

You can be on the Ubuntu CLI server or GUI Desktop but to switch the user account from the root in the terminal, we need to access it. So, either SSH your remote Ubuntu server or open the terminal app, if you using a Graphical desktop, the shortcut is Ctrl+Alt+T.

Step 2: Check the current user

Let’s first check which user you are using. Well, if you are on root then it is already visible there on the Terminal prompt in the username@hostname format.

Yet, if you still want to confirm then can use the given command and then press the Enter key. The output will show the current username.

whoami

Step 3: Switch to another Ubuntu user from Root

If you don’t know what are the other available users on your Ubuntu Linux apart from the Root then you can list all of them using the given command:

cat /etc/passwd | cut -d: -f1

The command will print all the users including those who are associated with some specific service such as Apache, SQL, etc. Find the one to which you want to switch, if you don’t know already.

Alternatively, you can only print the standard login users which generally have UID from min- 1000 and max- 60000. For example:

getent passwd {1000..2000}

Once you have the user name you want to switch, use the given command.

su - username

Replace the username with the user account that you want to switch to. If you go as our example then our user is “h2s“, hence the command will be like this:

su h2s

If you are using root then the command will not ask for other user’s password.

Type cd to exit the root directory.

Also, learn- How to add a new user in Linux with Examples

switch from root to another user in Ubuntu 1

Step 4: Verify the new user

So, you have seen it is quite easy, just a single command and you can start using some other user account to execute the command. However, after the switch, if you want to confirm the same then can again use the whoami command. It will display the current user name.

whoami

Step 5: Sudo Access

Your new user may or may not be a part of the sudo group. If it is not then you won’t be able to run commands that require root privileges such as the system update command that is – sudo apt update.

Therefore, those who want their new or existing user account apart from root to be a part of the sudo group, so that they can run the command which requires superuser permissions, see our article – Add user in sudo group on Debian or Ubuntu Linux.

Step 6: Exit the new user account

To logout the current standard user and get back to the root user account, if you require it, simply type exit and press Enter:

exit

In this way, Ubuntu, especially the server user with a root account as their default can switch to new or existing standard users or vice versa.  However, it is important to know what is the purpose of a user account before adding it to the sudo group.

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.