SSH – Getting Hostname of the Computer Connected to the SSH Session

ssh

As part of our deployment script for one of our apps I'm trying to log the date, time, and other relevant information for the deployment.

The way we have it set up, developers use a generic "app" account to perform deployments, so logging the user wouldn't be particularly helpful.

Would it be possible to get the hostname of the machine that connected to the SSH session?

Best Answer

There's an environment variable you can look for called SSH_CONNECTION that gets set when you log in with ssh. If you want the hostname, you may need to do a reverse lookup with dig -x <ip address>.

From the man page:

 SSH_CONNECTION
          Identifies the client and server ends of the connection.  The variable contains four space-separated values: client ip-address, client port
          number, server ip-address and server port number.