Getting hostname of the computer connected to the ssh session
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?
Solution 1:
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.
Solution 2:
While the environment variable SSH_CONNECTION may be the best method. Other methods exist.
- ssh and other logins are typically logged in /var/log/secure on many linux distributions.
- Give developers individual logins, and have them use sudo to do app deploys, or have their users have group write access to the app.