Linux/Unix is a multi-user operating system that means more than one user can use system resources simultaneously. In order to use the system resources, users are required to login to their respective user accounts.
These user accounts are created to prevent unauthorized users from using the system as well as to control the data access. Most of the Linux distribution create two user accounts at the time of OS installation.
One is created by you and the other one is the root account which gets created automatically. In this article, we will discuss user accounts in Linux/Unix and how we can switch among the users in a terminal.
A root account has complete access to the system. This is also called superuser and it can run any command without any restrictions. You can assume this account as the administrator account on the Windows machine. It gets automatically created while OS installation. Ubuntu comes sudo only by default. At the time of installing Ubuntu, a standard root account is created with no password assigned to it.
Here we are talking about a normal user account that is created by the user by providing the required information while installation of OS. Usually, these accounts have limited access to the system. It provides interactive access to the system for users and groups of users. You can add a new user account after OS is installed.
Creating a user account
After installation of the operating system if you want to create a new user account. You can create it by using the following command in your terminal-
sudo adduser new_user_name
And if you want to delete this account then you can use the following command –
sudo deluser new_user_name
The su command
Su is a Unix command which stands for substitute user. It is used by a user to execute commands with the privileges of another user account. If you want to change to another user type su followed by the username. This doesn’t change the current working directory or user environment. Now if you use su with a hyphen (su – ) followed by user name then it will change the current working directory as well as the environment for the target user.
Now if you want to switch to another user account you can use su command following by user name. First, check the current user account by using the following command-
and then use the-
This will ask the password of user2, enter it. And please note that this doesn’t change the current working directory and environment variables. Now again if you run whoami command it will display user2 in the terminal –
To logout from this account use-
Now if you want to change the current working directory as well as the environment variable when a user is changed then use a hyphen with su command-
su - user2
Again this will ask the password of user2, enter it. And now if you check the current working directory this will be changed to /home/user2 in this case.
Switch to root
There are various ways to switch to the root account –
Use the following command-
Or use the following command if you don’t want to change the current working directory-
Switch to the root account by using su command –
And this will ask you to enter the password of the root account. As discussed above Ubuntu and its derivatives come sudo-only by default. At the time of installing it, a standard root account is created with no password assigned to it.
So, to complete the execution of the above command first you will have to set a password for the root account.
Now once the password is set. Again execute the above command and enter the password. Now execute the following command to see the current user.
This will display the root as the result of the above command.