1

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?

Thanks!

1 Answer 1

0

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.

2
  • Thanks for the feedback - but su --login didn't solve this issue.
    – user72692
    Jun 16, 2014 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, 2014 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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