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?

2 Answers 2


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:

          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.
  • 4
    This would work very badly for secure accountability, because users could simply type SSH_CONNECTION=sneaky_sneaky /path/to/yourscript evil-params and you would not know the originator. May 3, 2012 at 16:37
  • 3
    True but a secure method was not what was asked for. I also assume that the logging of the ssh connection would be done before the user receives a prompt (i.e. in a .bashrc or some other startup script). If you wanted a proper secure method of accountability you'd not have them all logging in with the same user account.
    – webtoe
    May 3, 2012 at 16:56
  • For sure. Shared accounts are to be avoided. I know he didn't ask for it to be secure, but I thought it was worth mentioning, because there are not a lot of reasons you would need accountability but not need that accountability to be nontrivial to circumvent. May 3, 2012 at 16:57
  • 2
    Good call. Upvoted your comment.
    – webtoe
    May 4, 2012 at 12:12
  • I have marked this one as the correct answer as it does get the hostname that connected. I wanted it to go a bit further (the hostname from the user's machine), as we all connect through a VPN (same ip for everyone). You guys are right about the need to change the setup so that each user can use their own account. I was trying to avoid having multiple copies of the repository on the server (and the potential of having different developer with the repo on a different revision). I'll look into it.
    – delirial
    May 8, 2012 at 16:31

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .