linuxauthenticationsshauthorized-keys

Check identity of remote-user after password-less ssh-login?


After password-less ssh-login, is there any way in Linux to retrieve the identity of the remote-user that logged in?

I would like to take some different actions in the login-scripts, depending on from which remote host/userid I do ssh-login.


Solution

  • The originating system's username is not recorded unless you use something like this answer - i.e. push the username as part of the connection. The remote host is encoded in the SSH_CLIENT environment variable, so that can be determined.

    You could try to finger the remote system, but that requires fingerd to be running, which is not a common service these days.

    You'll have better luck using specific keys for users, which can have options set at the start of the key such as environment="NAME=value" in the authorized_keys file to kind-of determine the remote user that connected. e.g.

    environment="REMOTEUSER=fred" ssh-rsa <blahblahkey> <comment>
    

    The use of the environment option in the key will only work if you've got PermitUserEnvironment set in the sshd config, otherwise the line in the authorized_keys gets ignored and you'll be prompted for a password.