How to Change Users in Linux Command Line
Linux systems have different users and different types of users. Learn how to switch the users in Linux command line or how to change to the root user in Linux.
Linux systems have different types of users with different types or permissions as well.
Not all users can execute all commands and not all users are allowed to switch to other users neither. This all might sound confusing but, I will try to explain these so it can be easy to understand.
For the moment, here’s a quick summary of how to switch users in Linux command line.
To switch users, you need to know the password of that user. You can switch the users with this command:
To switch to root user in Ubuntu, you can use this command:
Various user types in Linux
If you list all users in Linux, you’ll see a lot of users that you didn’t know about. Who are these users? Where did they come from? I could write an entire article in regards of how users work in Linux, however, this is not the idea for this one.
Basically, there are 3 types of users in Linux:
1. System Users
These are the users that are automatically created in Linux systems to be able to run services or applications and are not intended to log in to the system (in fact you can’t log in as any of these users).
2. Regular Users
These are the (human) users who can log in to a system. Each of these users might have or not different permissions or levels in the system which is given by the groups they belong to.
3. Super Users
These are system administrators or users who can perform high-level tasks that can be considered critical or system dangerous.
Switch users in the command line
When using a Linux system you can log in with a user and then simply “switch” to another user through the same command line session. In order to do this, there is a command “su -“, which allows you to switch to become another user:
In the above example, you need to know the password of janedoe in order to switch to that user. Which makes sense because if you are going to switch to a user, you need to know the password of that user else it will be a security risk.
Switch to root user
For security reasons, some systems have ‘root’ account blocked for direct login, either locally or remotely, so this means it will not accept someone who tries to log in using ‘root’ even with the correct password.
So, how do you perform actions as the ‘root’ user? That’s what the ‘sudo’ command allows you to.
The sudo command will basically execute anything you want in the system as if the ‘root’ was doing it. You don’t need to know the ‘root’ user’s password, in fact, probably nobody knows it or there is no password assigned to ‘root’. You only need to know your own user’s password and that user must be in the ‘sudoers’ group, which is basically the group of users which can use ‘sudo’ in the system.
Normally, it is a good practice to run the commands with sudo that needs to run with root permission like this:
But if you want to change to root user so that all the subsequent commands will be run as root, you can use:
You’ll use your own password here, not the root account’s password.
As a sudo user yourself, you can create sudo user by adding the user to sudo group.
Linux systems allow you to easily switch users or execute high-level commands with the usage of ‘su‘ and ‘sudo’ commands. And remember: with great sudo power comes great responsibility!
How to switch between users on one terminal?
I’d like to log in as a different user without logging out of the current one (on the same terminal). How do I do that?
10 Answers 10
How about using the su command?
If you want to log in as root, there’s no need to specify username:
Generally, you can use sudo to launch a new shell as the user you want; the -u flag lets you specify the username you want:
There are more circuitous ways if you don’t have sudo access, like ssh username@localhost, but sudo is probably simplest, provided that it’s installed and you have permission to use it.
/.Xauthority` file is: -rw——- . I made a copy and that let me run gedit as an experiment. – will Dec 26 ’15 at 11:47
Generally you use sudo to launch a new shell as the user you want; the -u flag lets you specify the username you want:
There are more circuitous ways if you don’t have sudo access, like ssh username@localhost , but I think sudo is probably simplest if it’s installed and you have permission to use it
This command prints the current user. To change users, we will have to use this command (followed by the user’s password):
After entering the correct password, you will be logged in as the specified user (which you can check by rerunning whoami .
If you’re running Ubuntu, and if the user you want to login as doesn’t have a password set:
Enter your own password and you should be set. Of course, this requires that your user has rights to gain root privileges with sudo .
To switch the terminal session to a different user, where that user can’t exit back into the original user, use exec:
This will technically login the new user in a new term process, and close out the current one. That way when the user attempts exit or Ctrl-D, the terminal will close as though that user was the one who instantiated it, i.e., the user can’t exit back into the original user’s term. Kind of pointless, considering they can still just start a new terminal session and automatically be in the original user term login, but there it is.
EDIT: For what it’s worth, you can use linux vlock command in your
/.bashrc to lock terminal sessions by default, requiring the password of the term session user to unlock. This would somewhat prevent the aforementioned term restart under the original user context, given the term isn’t instantiated using the non-default
How To Change User on Linux
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.
For example, in order to log in to the account named “john”, you would run the following command.
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
In order to change user by creating a new environment, you have to run su with a hyphen.
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
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.
For example, to change a user password, you would run
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.
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.
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.
How to switch user in terminal
I wish to change to:
How does one do this. I tried: su sensu but nothing happens.
1 Answer 1
If you specify the login option (either — , or -l , or —login : all are synonyms), you will get an environment similar to if you directly logged in as that user. Otherwise you stay in the current environment (e.g. many env variables, the current directory, etc).
Example: (note the usernames and current working directories!)
PS: If you run su not as root, you will of course be prompted for the password of the user as who you want to log in.
Not the answer you’re looking for? Browse other questions tagged command-line su or ask your own question.
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2020.9.18.37632