I'm wondering if it is possible to set different home directories for the same ssh user for each subdomain.

So let's say you login with ssh myuser@example.com your home directory will be: /www/httpdocs/

If you login with ssh myuser@subdomain1.example.com your home directory will be: /www/subdomain1/

If you login with ssh myuser@subdomain2.example.com your home directory will be: /www/subdomain2/


And so on.


SSH can't do that because SSH protocol does not include the requested hostname in the call. (HTTP is one of the few protocols that does include the requested hostname, which is how it can be used for virtual hosting.) There are a couple of other things you might try instead:

  • You could create separate users for each subdomain, but with the same UID as the "main" user. The subdomain-users would have their home directory set to the subdirectory. Example:

    useradd -o -u 4711 -d /var/www/subdomain1 subdomain1

  • Use a PAM module to authenticate based on subdomain. If such a module exists, I have no idea what it would be, but it might be worth looking into.

  • Use separate SSH keys for each subdomain. On the client side, set up a .ssh/config so that you can type ssh subdomain to make it log in with the correct key. On the server side, have each public key in authorized_keys start with the words environment="DOMAIN=subdomain". On the server side, also make a .ssh/rc file that will cd to the right directory based on the DOMAIN environment variable. This requires the server to be configured with PermitUserEnvironment yes.

  • Thank you! Sounds like a good solution. I'll give it a try. – kapale Apr 16 '14 at 13:34
  • I actually wanted to also suggest the solution with the SSH keys and, specially, exporting ENV vars from the connection origin machine but he asked about have HOME directories and not just cd into them so I didn't think it is a good solution. – Florin Asăvoaie Apr 16 '14 at 20:41
  • +1, the SSH keys idea is extremely clever and I didn't know you could set the environment per key like that. – tgies Apr 16 '14 at 21:49

This is NOT possible because SSH protocol does not send the requested hostname anywhere in the packets.

My idea on implementing this would be to use something like OpenVZ to isolate the subdomains and have a separate IP for each subdomain.

  • Would be a possibility, but is way too much effort for the case I need this. Thank you – kapale Apr 16 '14 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.