Take the 2-minute tour ×

I wish to create the following script in .profile, so each SSH session would go directly into its place:

  1. Check the specified host (user@host).
  2. If host = host1 -- cd /var/www/site1


Help! :)

share|improve this question

2 Answers 2

I'm pretty certain SSH doesn't send the hostname to the server (it just uses it locally to look up an IP-address and to check keys etc). At least, not in a way that can be used in a shell script.

See the output of env - the only hostname is the canonical one, regardless of what name was used in the ssh command.

Some SSH clients and some SSH servers allow the client to specify a value for one or more environment variables, conceivably you could use this to tell the server which directory to switch to. For example, see Putty configuration, connection, data or OpenSSH SendEnv option.

Otherwise, you have one user-ID for each website with a different domain name or you have to offer the user a menu.

share|improve this answer
It does set the source IP address in the environment variable "SSH_CONNECTION". –  Pete Ashdown Dec 16 '10 at 17:14
So if all sites are managed using the same common username but from a different static source IP-address for each intended site, you could use that. –  RedGrittyBrick Dec 16 '10 at 17:25

Your question is unclear: are you reaching the same host through multiple aliases? Are you reaching different hosts that share a .profile? Are you trying to execute commands based on the source host name or on the target host names?

If you want different effects on different remote hosts, just test the remote host name:

case "$(hostname)" in
  host1) cd /var/www/site1;;

If you want different effects on the same host, you can use the SendEnv directives in your ~/.ssh/config to send environment variables (assuming you're using the OpenSSH client). These need to be enabled with an AcceptEnv directive on the server side, which is often not the case. However LC_* variables are typically permitted through, so you can pass information using these. On the client side, run LC_VIRTUAL_HOST=site1 ssh common-host.provider.com. In your .profile on the server, write

case "$LC_VIRTUAL_HOST" in
  host1) cd /var/www/site1;;
share|improve this answer

Your Answer


