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?
11 Answers
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.
- 39Also,
su - [user]may be useful -- the extra dash gives you a login shell.ephemient– ephemient2010-10-27 20:40:49 +00:00Commented Oct 27, 2010 at 20:40 - 1I am getting this error "-su: /dev/stderr: Permission denied" after executing this command
echo >>/dev/stderron a login withsu --login ..., any tip? I found this btw unix.stackexchange.com/questions/38538/…Aquarius Power– Aquarius Power2014-11-24 18:58:27 +00:00Commented Nov 24, 2014 at 18:58 - 1Does this allow each new user to have different, overriding values for environment variables? e.g. git config for work, open source, etc.Kevin Suttle– Kevin Suttle2015-12-23 21:13:54 +00:00Commented Dec 23, 2015 at 21:13
- 1One finding, when I listed the
envit 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.will– will2015-12-26 11:47:09 +00:00Commented Dec 26, 2015 at 11:47 - 2if you get "This account is currently not available": su -s /bin/bash - www-datamax4ever– max4ever2019-03-15 12:19:48 +00:00Commented Mar 15, 2019 at 12:19
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
- 1What if my system has neither ssh server or sudo? Can you mention that portion on the answer?tshepang– tshepang2010-10-27 19:52:33 +00:00Commented Oct 27, 2010 at 19:52
- 1ok, Pratt answered that onetshepang– tshepang2010-10-27 20:01:26 +00:00Commented Oct 27, 2010 at 20:01
- 10
sudo -sgives you a shell likesu,sudo -isimulates login likesu -. Can be combined with-u $user, of course.ephemient– ephemient2010-10-27 20:41:41 +00:00Commented Oct 27, 2010 at 20:41 - 1much 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 selinuxFélix Gagnon-Grenier– Félix Gagnon-Grenier2016-10-12 15:01:26 +00:00Commented Oct 12, 2016 at 15:01
$ whoami This command prints the current user. To change users, we will have to use this command (followed by the user's password):
$ su secondUser Password: After entering the correct password, you will be logged in as the specified user (which you can check by rerunning whoami.
- 1Useful if you're not sudoer.Marco Sulla– Marco Sulla2015-06-24 08:55:45 +00:00Commented Jun 24, 2015 at 8:55
- 1Better to real test of password (avoid sudo) or behavior of a new user.Peter Krauss– Peter Krauss2020-05-30 17:46:06 +00:00Commented May 30, 2020 at 17:46
If you're running Ubuntu, and if the user you want to login as doesn't have a password set:
sudo su - username 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:
$|# exec su - [username]
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.
- 1Using exec is a good use-case when you are on an SSH-connection, and want to "hand" an existing connection to another user without the need to reconnect. I wonder if there are vulnerabilities in terms of security with regard to this.Yeti– Yeti2020-01-05 21:12:57 +00:00Commented Jan 5, 2020 at 21:12
- 1It's a direct process fork; nothing should be inherited by the new process... except environment variables and path! Oh, and potentially also ssh-agent. Envars and ssh-agent keyring could be worrisome!SYANiDE– SYANiDE2020-01-13 21:18:43 +00:00Commented Jan 13, 2020 at 21:18
Yet another route is to launch a new shell as a different (non-root) user to run commands as that user.
ubuntu@aws-ip:~$ sudo -u mongodb bash #<-- or zsh, etc... mongodb@aws-ip:~$ mongod --configsvr --dbpath /data/configdb --fork An example of this is the mongodb user. When deploying a sharded MongoDB cluster, all the necessary processes must run as mongodb and it's not necessary (or entirely convenient) to daemonize the processes using init scripts for dozens of nodes.
Let us get this right: You are logged in as UserA and want to "login" as UserB to run some commands, but would like to come back to UserA when done. For the sake of simplicity, I assume that you want to run ls -l /tmp as UserB. If you do not want to leave the current shell of UserA but rather run a command as UserB and still remain logged in as UserA, you should do this:
su - UserB -c "ls -l /tmp" <-- Just an example This assumes you know the password for UserB. However, if you do not know UserB's password, you need to know the root password. Then:
sudo su - UserB -c "ls -l /tmp" <-- UserB's pw not needed here If you would rather temporarily login as UserB to run lots of commands, then just do:
sudo su - UserB This will give you a new shell for UserB (check that by typing id). When done, you can do ctrl-d and return to your login.
If you need to run just a single command, you can use sudo: sudo -u username command
Plenty of answers and all excellent but for such a brief question I need to assume an XY might be in place. Thus this post.
Use VTs. Advantages include
- kernel implemented so fewer attack vectors
- you can run multiple GUIs if wanted
- your boss can't accidentally see your n0rp.
Disadvantages include escaping anything than ASCII or running things that do so. fbi and whatever it's alternative for pdf-s was(would appreciate a reminder) work nicely though.
~$ sudo login Then it will prompt you for the sudo password (the currently logged in user's password).
Also: make sure that the current user is in the sudoers file!
- 2The question was about logging in as a different user.Minix– Minix2015-05-27 15:38:37 +00:00Commented May 27, 2015 at 15:38
$XDG_RUNTIME_DIRin particular) that were driving me nuts. -> unix.stackexchange.com/questions/354826/…