I want the default command for a Docker image to start up a login shell for the container's user.¹
Something like:
…
USER someuser
CMD /bin/zsh --login
That above gets a login shell, but does not change the working directory to someuser's home, nor does it honor someuser's /etc/passwd entry. sudo --user someuser --login will achieve the desired result (correct shell, working directory as user's home), but that depends on someuser being properly provisioned in /etc/sudoers and leaves the parent sudo process hanging around. I don't want to rely on sudo at all, if I don't have to.
I have tried /usr/bin/login -p -f someuser as the command, but this doesn't seem to work (not sure what the error is).
One can try something like CMD sh -c 'cd "${HOME}" ; SHELL="${SHELL}" exec -a "-${SHELL##*/}" "${SHELL}"' (derived from a discussion with a different, but related context). This seems to work, but that depends on SHELL being set and the referenced shell inferring that it should start as a login shell when it's zeroth argument starts with a -. Is this idiomatic? I don't know if SHELL is always set or whether prefixing the user shell with - will always work. (Note that CMD sh -c '… exec -l "${SHELL}"' doesn't get it quite right, because the zeroth argument gets set to, e.g., -/usr/bin/zsh.)
This seems to suggest agetty can create the desired effect, but seems like a mismatch on how to get there. I also don't want to engage in my own grep/awk shenanigans with /etc/passwd (although getent passwd "$( id -u )" | cut -d : -f 7 seems to be the least obnoxious of these approaches).
Is there a way a user can start its own login shell as if the user were logging in without naming the shell or the home directory explicitly and without a password?
There has to be something more idiomatic than exec python -c 'import os, pwd, re ; ent = pwd.getpwuid(os.getuid()) ; os.chdir(ent.pw_dir) ; os.execv(ent.pw_shell, (re.sub(r"^.*/", "-", ent.pw_shell),))'.
¹ The image is meant to create a persistent, named container that houses a sandboxed, interactive environment for experimentation with a specific set of pre-installed applications. (This isn't some containerized web application. Think of running a highly customized suite of Linux-only math/science apps on a Windows host with minimal required configuration.) Power users may want to change the login shell for someuser inside the container via chsh, and that should be honored by the default command.
dockercommands? – aviro Feb 08 '22 at 15:05getentis a shell wrapper around) are howloginand friends work. – Gilles 'SO- stop being evil' Feb 08 '22 at 17:08/etc/passwd. As you say,loginalready does this. What I'm asking about is whether it can do it without requiring the user to reenter a password if a user is starting its own login shell.logindoes some other stuff that is helpful (e.g., sets the home directory, changes the current working directory to home, etc.). I would like to invoke that machinery, whatever it is, but I don't know how to discover that. – posita Feb 08 '22 at 18:23weirdsh(not your login shell) interactively assomeuserand want to replace that process with a clean shell (listed insomeuser's entry in/etc/passwd) as ifsomeuserhad just logged in (complete with changing your working directory to your home as listed in/etc/passwd). What command do you type? – posita Feb 08 '22 at 18:35CMD. – aviro Feb 08 '22 at 19:55-lmakes the shell a login shell. – Vilinkameni Feb 09 '22 at 09:08su -l "${USER}"prompts for the user's password. – posita Feb 09 '22 at 13:35setuid. As stated in the title, the user is starting its own login shell. There's no escalation. Thepython"one-liner" above doesn't needsetuid. – posita Feb 09 '22 at 17:03