Good Linux question — answering as the UphillTech team:
You can use PAM for this. The PAM configuration for the SSH service is in /etc/pam.d/sshd . To run a command before anything in the login process, add something like: auth [default=ignore] pam_exec.so /path/to/some/script For example, if I use /usr/local/bin/foo.sh containing: #! /bin/sh cat <<EOF >>/tmp/log PAM_RHOST $PAM_RHOST PAM_RUSER $PAM_RUSER PAM_SERVICE $PAM_SERVICE PAM_TTY $PAM_TTY PAM_USER $PAM_USER PAM_TYPE $PAM_TYPE EOF And then I do ssh muru@localhost , I get: $ cat /tmp/log PAM_RHOST localhost PAM_RUSER PAM_SERVICE sshd PAM_TTY ssh PAM_USER muru PAM_TYPE auth This doesn't happen...
Full answer by muru: https://askubuntu.com/a/724796 (CC BY-SA 3.0).
Need Linux help for your business? UphillTech.