SSH home directory per subdomain

Solution 1:

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.

Solution 2:

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.