How to login as a different user in terminal?

10

3

I'm currently logged in as a normal user in OS X.

I'd like to start a terminal session and login as user foo.

Is this possible?

macek

Posted 2010-11-04T20:58:03.670

Reputation: 5 157

1Have you tried using su - foo? – kraftan – 2010-11-04T21:02:56.307

Answers

16

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 -.

kraftan

Posted 2010-11-04T20:58:03.670

Reputation: 351

May be obvious to everyone else, but I needed to run it as sudo: sudo su - foo – Xiao – 2015-10-05T21:48:57.333

The login works fine but I tried mate myfile and I'm getting a "failed to connect to WindowServer" error. This isn't a critical fix, but it would be great to get this working. – macek – 2010-11-04T21:11:05.407

2@macek: That's because there's no Window Server (aqua) attached to the terminal. If the user is currently logged on in the graphic environment, try open -a TextMate myfile. – Georg Schölly – 2010-11-04T21:13:18.540

1@macek: I'm not sure, but maybe su foo could do it. Omitting the - keeps the values environment variables. – kraftan – 2010-11-04T21:15:16.077

Thank you for the help. @kraftan, perhaps update the answer to include both with a brief explanation. :) – macek – 2010-11-04T21:26:53.337

@Georg, that didn't seem to work. – macek – 2010-11-04T21:29:07.063

-1

I have tried su many times but I found sudo to be easier. You can try

sudo -u <username>

iPav

Posted 2010-11-04T20:58:03.670

Reputation: 1

My sudo doesn't seem to have a -u option – Xiao – 2015-10-05T21:51:28.057