12

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.

T. Zengerink
  • 199
  • 5
  • 13
kapale
  • 405
  • 1
  • 3
  • 8

2 Answers2

19

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.

Jenny D
  • 27,358
  • 21
  • 74
  • 110
  • 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
8

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.

Florin Asăvoaie
  • 6,932
  • 22
  • 35