On Linux, there are many ways to change the current user for the session.
If you are not using a graphical interface, you might wonder how you can change the user easily without having to install a complete user interface.
Luckily for you, there are some commands that you can run in order to achieve this.
Today, we are focusing on the su (for switch user) and sudo commands on Linux.
We will see the command syntax and how it can be used to switch users.
Change user on Linux using su
The first way to change your user account in a shell is to use the su command.
$ su <option> <user>
For example, in order to log in to the account named “john”, you would run the following command.
$ su john
Password:
[john@localhost user ~]$
As you can see, by not specifying the hyphen when running su, you inherit from the environment variables of the account you are coming from.
To verify it, try to run the following command
$ echo $USERNAME
user
In order to change user by creating a new environment, you have to run su with a hyphen.
$ su - john
Password:
[john@localhost ~]$
Change user on Linux using sudo
Another way to change the current user is to use the sudo command.
By default, you will need privileged rights in order to run this command, so make sure that you have sudo rights either on Debian based distributions, or on Red Hat based distributions.
Now that you have sudo, to change user you have to run this command
$ sudo -u <user> -s
[john@localhost antoine~]$
As you can see, you also inherit the environment variables when you use this command.
In order to launch a command as another user, you can also append a command to the sudo command.
$ sudo -u <user> <command>
For example, to change a user password, you would run
$ sudo -u john passwd
Changing user password for user john
Current password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
Change user to root account on Linux
In some cases, you may need to change the current user in order to log as the root account.
To change user to root account, simply run “su” or “su –” without any arguments.
$ su -
Password:
[root@localhost ~]#
$ su
Password:
[root@localhost user]#
Change user account using GNOME interface
If you installed Debian or CentOS with a GNOME interface, you can also change the user account by following those steps.
To change the user using GNOME, find the power button at the top right corner of your screen and click on it.
Click on the username label, then click on “Switch user” in order to change the current user.
You will be redirected to the lock screen where you are free to change to the account that you want.
Conclusion
Today, you learnt how you can easily change the user account on Linux either by using su, or by using the sudo command if you are a privileged account.
You also learnt that it is possible to do it when you are running Linux graphically with a GNOME GUI.
If you are looking for more Linux System Administration tutorials, make sure to check our other tutorials on the subject.
1 comment
[…] How To Change User on Linux […]