0

For accounts, in containers, that have no shell (e.g. user www-data) I'd like to use sudo, without ending up in /root/ folder.

Why does this only work after using it for the second time ? How can I avoid ending up in /root/ dir with sudo ?

$ sudo lxc-attach -n hugo
root@hugo:~# sudo -H -u golanger /bin/bash && cd /home/golanger
golanger@hugo:/root$
golanger@hugo:/root$ exit
root@hugo:/home/golanger# sudo -H -u golanger /bin/bash && cd /home/golanger
golanger@hugo:~$ 
1

In sudo -H -u golanger /bin/bash && cd /home/golanger, your current shell executes sudo -H -u golanger /bin/bash and when that finishes successfully, runs cd /home/golanger. So the cd doesn't affect the sudo command.

sudo itself doesn't reset the working directory (-H only resets the home directory, not the current working directory). If you want to make sudo cd to the home directory of the target user, there are no command-line options which can directly do the job. You'll have to workaround it:

sudo -H -u golanger /bin/sh -c 'cd; /bin/bash'

Note that, with the -i option, sudo will try to cd to the home directory of the target user, but it also uses the user's login shell, which will make it fail in this case.

| improve this answer | |

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.