Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A while back, when playing with unprivileged LXC, I discovered that the command to create a container failed if I used su to switch to the user, but it worked if I used ssh user@localhost to login.

This is not a LXC related question, but it made me wonder how to best switch user without leaving references or bringing variables from the old one?


share|improve this question

A difference between su and ssh is that the latter actually involves a login shell, whereas the former does not by default -- although it does create a new interactive shell (see INVOCATION at the top of man bash for the significance of interactive and login shells). From man su:

For backward compatibility su defaults to not change the current directory and to only set the environment variables HOME and SHELL (plus USER and LOGNAME if the target user is not root). It is recommended to always use the --login option (instead of its shortcut -) to avoid side effects caused by mixing environments.

So, you could try su --login and see if that does what you want.

I'm presuming you're using a GUI, which is why you don't simply exit and log in again. However, that may be possible anyway (depending, I believe, on your init system), by simply running login from whatever terminal you are using. If it works, you should see the normal login: prompt, and when done you can exit from that as per su.

share|improve this answer
Thanks for the feedback - but su --login didn't solve this issue. – user72692 Jun 16 '14 at 20:24
Just in case there was any confusion, the login command mentioned in the last paragraph is distinct from su --login, so if you haven't tried that do (but again: login may not work at all this way on some systems). You may want to clarify the nature of your problem in the question since "bringing references" is very ambiguous and if su --login doesn't help then you may have an XY problem. – goldilocks Jun 16 '14 at 21:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.