How to login as a different user in terminal?

Solution 1:

Have you tried using su - foo?

Update based on the comments:

The empty - tells su to make a full login. That means (from the man page),

The environment is discarded except for HOME, SHELL, PATH, TERM, and USER. HOME and SHELL are modified as above. USER is set to the target login. PATH is set to ``/bin:/usr/bin''. TERM is imported from your current environment. The invoked shell is the target login's, and su will change directory to the target login's home directory.

So, if you need any other environment variables, such as DISPLAY to open programs that use the window server, you have to omit the empty -.

Solution 2:

On MacOS X use login


$ login
login: your username
password: your password
Last login: Day Month Date HH:MM:SS on ttys000
$ whoami
your username

I was having issues running nano after using su - admin to edit a .bashrc file. When adjusting the window size the text would become garbled. The answer I found below explains why and led me in the correct direction.

"The terminal emulator will send a Window Change signal (SIGWINCH) to the shell that was started along with the terminal emulator. The shell is supposed to pass it on to its children - but when you've transitioned to another user account (using su or otherwise), it won't be able to signal that shell because it's running as a different user. So the su'd shell and any programs run from it will keep using the old window size, oblivious to the fact that the real window size has changed." --telcoM