SSHing into system with ZSH as default shell doesn't run /etc/profile

sshd runs a login shell when the client doesn't send any command to run. That's to mimic the behaviour of rsh which was calling the rlogind service instead of the rshd one when not given any command to run.

To tell the shell it is to be a login shell, like rlogind or login or telnetd, sshd starts your shell with an argv[0] that starts with a -.

Or course, zsh like every other shell understands it as well. If it didn't, it couldn't ever be invoked in login mode.

Here, the symptom that made you think you didn't get a login shell was because /etc/profile was not interpreted.

/etc/profile is the login session initialisation file for Bourne-like shells. Syntax in there is expected to be in the Bourne shell syntax (or possibly POSIX sh syntax as the Bourne shell is hardly ever used these days) and is read upon login by all the shells whose syntax is (mostly) backward compatible with the Bourne or POSIX sh syntax.

csh and tcsh which have a completely different syntax use /etc/csh.login instead. fish uses /etc/fish/config.fish (regardless of whether it's in login mode or not).

zsh's syntax is also not completely compatible with that of Bourne/POSIX, so by default, it doesn't read /etc/profile. Its configuration files in login mode are /etc/zsh/zprofile (or /etc/zprofile depending on how zsh was configured at build time) and /etc/zsh/zlogin (or /etc/zlogin) the latter being sourced after the zshrc.

zsh only sources /etc/profile when in sh or ksh emulation in which case its syntax is much closer to that of POSIX sh than in the default zsh emulation mode.

If you want zsh to source /etc/profile when in login mode, you'd need to add a source /etc/profile in /etc/zsh/zprofile, but you'd only want to do that when you have made sure the syntax of /etc/profile and any other file it sources is compatible with that of zsh.

Alternatively, you could add:

emulate sh -c 'source /etc/profile'

To /etc/zsh/zprofile. Then /etc/profile would be sourced in sh emulation mode (and functions declared in there would also retain the sh emulation mode).


ZSH just works in this way. /etc/profile is NOT an init file for ZSH. ZSH uses /etc/zprofile and ~/.zprofile.

Init files for ZSH:

  1. /etc/zshenv
  2. ~/.zshenv
  3. login mode:
    1. /etc/zprofile
    2. ~/.zprofile
  4. interactive:
    1. /etc/zshrc
    2. ~/.zshrc
  5. login mode:
    1. /etc/zlogin
    2. ~/.zlogin

enter image description here

References

  • Unix shell initialization
  • Shell Startup Scripts