- How to switch between users on one terminal?
- 10 Answers 10
- How to Switch Users in Ubuntu and Other Linux Distributions [Quick Beginner Tip]
- Change user in Linux command line
- Change users in Linux graphically (for desktop Linux)
- How to Switch User in Linux [Linux Change User]
- What is SU command? [ Switch User Linux ]
- SU Command Syntax
- SU Command Options
- How to Switch User in Linux ? [C hange User Linux ]
- Change Linux user to an Another User
- As a Different User, Execute a Specific Command
- Switching to a Different Shell Environment
- In the Same Environment, Use a Different User
- Conclusion
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?
I asked a similar question, because I ran into issues with XDG environment variables ( $XDG_RUNTIME_DIR in particular) that were driving me nuts. -> unix.stackexchange.com/questions/354826/…
10 Answers 10
How about using the su command?
$ whoami user1 $ su - user2 Password: $ whoami user2 $ exit logout
If you want to log in as root, there’s no need to specify username:
$ whoami user1 $ su - Password: $ whoami root $ exit logout
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:
$ whoami user1 $ sudo -u user2 zsh $ whoami user2
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.
I am getting this error «-su: /dev/stderr: Permission denied» after executing this command echo >>/dev/stderr on a login with su —login . , any tip? I found this btw unix.stackexchange.com/questions/38538/…
Does this allow each new user to have different, overriding values for environment variables? e.g. git config for work, open source, etc.
One finding, when I listed the env it saw that everything was in order as well as a visual inspection can go; And one thing was incorrect: XAUTHORITY=/home/user1/.Xauthority’. Not sure _why_? So X-window doesn’t work by default because the protection on ~/.Xauthority` file is: -rw——- . I made a copy and that let me run gedit as an experiment.
Generally you use sudo to launch a new shell as the user you want; the -u flag lets you specify the username you want:
[mrozekma@etudes-1 ~] % whoami mrozekma [mrozekma@etudes-1 ~] % sudo -u nobody zsh [nobody@etudes-1 ~] % whoami nobody
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
sudo -s gives you a shell like su , sudo -i simulates login like su — . Can be combined with -u $user , of course.
much more efficient. being able to «login» as a user who can’t normally login is a great asset. totally allowed me to run a database instance without messing with permissions or selinux
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 ~/.bashrc of the user, as configured.
How to Switch Users in Ubuntu and Other Linux Distributions [Quick Beginner Tip]
It is really simple to switch users in Ubuntu or any other Linux distribution using the terminal.
All you need to do is to know the unsername and its account password and you can switch users with su command:
You’ll be asked to enter the password of the user you want to switch to.
As you can see in the screenshot above, I changed to user prakash from user abhishek in the terminal.
There are some minor details with this method that I’ll share with you in a moment. I’ll also share the graphical way of switching users in Linux if you are using desktop Linux.
If you want to switch to the root user in Ubuntu, you can use the following command:
sudo su
You’ll have to enter your own user password here.
Change user in Linux command line
Let’s see things a bit in detail. To switch users, you need to first know the exact username because tab competition doesn’t work here. You can list all the users in Linux command line by viewing the content of the /etc/passwd file.
You’ll also need to know the password of the user account you want to switch to. This is for security reason, of course.
If you are the admin user or have sudo access, you can change account password with passwd command.
You’ll notice that some people use a — between su and the username. There is a specific reason for that.
When you use -, -l or –login option, you start the shell as a login shell. This means that it will initialize the environment variables like PATH and changes to the home directory of the changed user. It will be as if you logged into the terminal as the second user.
Note: though — is more popular, it is advised to use –login option.
Change users in Linux graphically (for desktop Linux)
If you are using desktop Linux, the above method may not be sufficient for you. Why? Because you switch the user in the terminal only. It is confined to the terminal. Nothing is changed outside the terminal.
If you want to switch users so that you can log in as another user and use all the system (browser, applications etc) graphically, you’ll have to log out and then log back in.
Now the screenshots may look different but the steps remain the same. Here’s how to switch users in Ubuntu Linux.
Go to the top right corner and click the Power Off/Log out option to open the dropdown and you can choose either of Switch User or Log Out.
- Switch User: You get to keep your session active (applications keep on running) for current user. Good for temporarily switching users as you won’t lose your work.
- Log out: Current session ends (all applications are closed). Good when you want to switch to the other user for a long time.
You can choose whichever option is more suited for your need.
Now, you’ll be at the login screen with all the available users for your system. Choose the user account of your choice.
Clearly, you need to know the password of the user account you want to use.
That’s it. I hope you find this quick beginner tip helpful in changing users in Ubuntu and other Linux distributions. Questions and suggestions are always welcome.
How to Switch User in Linux [Linux Change User]
How do I switch user in Linux? What is the su command? How does it differ from sudo? If you ever had these questions, you are in the right place.
List of content you will read in this article:
Linux has a unique and different way of switching users than other operating systems have. To switch or change user in Linux, simply input the command «su newuser», ensuring to replace «newuser» with the username of the account you wish to switch to. If you’re new to Linux, you may wonder how to switch users in Linux from the command line. The «su» command is a simple way to change which user you are logged in as. In this article, we’ll show you how to use the su command for linux change user .
To linux switch user , simply type «su» followed by the username of the user you want to switch to. For example, if you wanted to switch to the user «mvm», you would type:
You will then be prompted for that user’s password. Once entered, you will be logged in as that user.
If you want to switch users temporarily, you can use the «-l» option. This stands for «login shell.» It will give you a full login shell for the specified user. For example:
This is useful if you need to run a command as another user and don’t want to leave your current session. Remember that when using the «-l» option, you will need to enter the password for the other user twice (once for su and once for login).
What is SU command? [ Switch User Linux ]
Su allows you to change the existing user to some other user. Use the –l [username] method to define a user account if you need to execute a command as someone other than root. Switching between shell interpreters on one go is also possible using the su command. Su is a more powerful but older command. It can mimic sudo’s features by passing a single command to the shell with the –c option.
The su command in Linux is short for «substitute user or switch user» and allows a user to gain temporary access to another user’s account. This can be useful if you need to run a command as another user or troubleshoot an issue and need to access another user’s account.
SU Command Syntax
To use the su command, type the following into the command prompt:
su [options] [username [arguments]]
su defaults to the superuser if no username is specified (root). Locate the required user and include it in the su command syntax.
SU Command Options
Use -h to see a list of commands options in su:
You can use the su command with the following options:
- Username – Change your username with the username you’d like to use to log in. Any consumer, not just root, can do this.
- –command or –c [cmd] – Executes a command as the user-defined.
- –login or –l [username] – Changes the username by running a login script. For that person, you are required to enter a password.
- –s or –shell [shell] – Allows you to execute the command in a separate shell context.
- –h or –help – Displays the su command’s help file.
- –p or ––preserve-environment – Protect the shell environment (HOME, SHELL, USER, LOGNAME).
Linux VPS
How to Switch User in Linux ? [C hange User Linux ]
Change Linux user to an Another User
Enter the following command to change user Linux in this terminal window:
A password would be needed. When you type it in, the username will be changed to that account.
As a Different User, Execute a Specific Command
Using the –c alternative to execute a particular command as a separate user:
su –c [command] [other_user_name]
The system would then prompt you for your username. When you execute this command, the system will run the ls (list the directory contents) command for the defined account.
Switching to a Different Shell Environment
Enter the following to use a new shell or operating environment:
In Z shell, this command creates a root user account.
In the Same Environment, Use a Different User
With the –p alternative, you can hold the current user account’s environment:
[another user] can be replaced with the real username you choose to use. The user account will change, but your home directory will remain the same. This is helpful if you execute a command as a new user but need access to the current user’s files.
Use the echo $HOME command to show the directory you’re operating in to ensure you’re still in the same home environment.
Conclusion
In conclusion, the SU command is a powerful tool that can be used to switch users in Linux. This article has provided a comprehensive guide on using Linux change user command and some useful tips and tricks. We hope that you have found this information helpful and that you can now use the SU command confidently.