# DockerSpawner / SystemUserSpawner - manually set the user's home directory?

**URL:** https://discourse.jupyter.org/t/dockerspawner-systemuserspawner-manually-set-the-users-home-directory/21506
**Category:** JupyterHub
**Tags:** jupyterhub, help-wanted
**Created:** [September 19, 2023, 3:10pm UTC](https://discourse.jupyter.org/t/dockerspawner-systemuserspawner-manually-set-the-users-home-directory/21506 "2023-09-19T15:10:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![alexlod](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@alexlod](https://discourse.jupyter.org/u/alexlod)
#### Post date: [September 19, 2023, 3:10pm UTC](https://discourse.jupyter.org/t/dockerspawner-systemuserspawner-manually-set-the-users-home-directory/21506/1 "2023-09-19T15:10:27Z")

</div>

Hi community,

JupyterHub’s DockerSwarmer / SystemUserSpawner assumes that the user’s home directory is `/home/$NB_USER`.

The Linux host I’m running JupyterHub on unfortunately doesn’t follow this convention. Usernames are `<user>@<domain.com>` and home directories are `/home/<user>`. For example, a username might be `foo@bar.com` and their home directory is `/home/foo`. I have no way of changing this behavior. In case it matters, I’m using the `AzureAdOAuthenticator` authenticator.

Is there an easy, straightforward way to manually configure the home directory or workaround this issue?

I feel confident I can make this change by heavily modifying start scripts and such, but I’m hoping to avoid that if possible. I’ve also considered symlinking `/home/foo@bar.com` to `/home/foo` but that feels very hacky too.

Thanks and best wishes!

---

<div class="post-metadata">

### Author: ![mahendrapaipuri](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/mahendrapaipuri/32/6800_2.png) [@mahendrapaipuri](https://discourse.jupyter.org/u/mahendrapaipuri)
#### Post date: [September 19, 2023, 6:46pm UTC](https://discourse.jupyter.org/t/dockerspawner-systemuserspawner-manually-set-the-users-home-directory/21506/2 "2023-09-19T18:46:41Z")

</div>

There is a [`normalize_username`](https://github.com/jupyterhub/jupyterhub/blob/46508a31d32d90ae616fdb6ca25734e63f6ac885/jupyterhub/auth.py#L420) method on Authenticator class of JupyterHub. You can use it to change the username from `foo@bar.com` to `foo` in the JupyterHub context. A sample code will be

```auto
class MyAzureAdOAuthenticator(AzureAdOAuthenticator):

    def normalize_username(self, username):
        return username.split('@')[0]

c.JupyterHub.authenticator_class = 'MyAzureAdOAuthenticator'

```

Now the usernames in the JupuyterHub will be without domains in the email ids and it should work out of the box in creating home dirs.

---

<div class="post-metadata">

### Author: ![alexlod](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@alexlod](https://discourse.jupyter.org/u/alexlod)
#### Post date: [September 19, 2023, 6:53pm UTC](https://discourse.jupyter.org/t/dockerspawner-systemuserspawner-manually-set-the-users-home-directory/21506/3 "2023-09-19T18:53:35Z")

</div>

Thanks [mahendrapaipuri](https://discourse.jupyter.org/u/mahendrapaipuri)! Will this not mess with the way SystemUserSpawner works, though, because the username differs between the Linux host and the container? I’ll give it a try nonetheless.

---

<div class="post-metadata">

### Author: ![mahendrapaipuri](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/mahendrapaipuri/32/6800_2.png) [@mahendrapaipuri](https://discourse.jupyter.org/u/mahendrapaipuri)
#### Post date: [September 19, 2023, 7:10pm UTC](https://discourse.jupyter.org/t/dockerspawner-systemuserspawner-manually-set-the-users-home-directory/21506/4 "2023-09-19T19:10:25Z")

</div>

Ahh! Sorry! I misread your question. I thought your authenticator is passing the usernames as email ids and linux machine has plain usernames without email domains. So, the solution I proposed is not relevant to your issue. Sorry about that!

Maybe you can override the [`host_homedir`](https://github.com/jupyterhub/dockerspawner/blob/b280cd9eddac77463b08ca2c3d07b0c48643d416/dockerspawner/systemuserspawner.py#L83) in `SystemUserSpawner` to get correct home dirs for your machine?

---

<div class="post-metadata">

### Author: ![alexlod](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@alexlod](https://discourse.jupyter.org/u/alexlod)
#### Post date: [September 19, 2023, 8:27pm UTC](https://discourse.jupyter.org/t/dockerspawner-systemuserspawner-manually-set-the-users-home-directory/21506/5 "2023-09-19T20:27:18Z")

</div>

No worries! I’ve overridden both `host_homedir` and `homedir`, which solved part of the problem but not all of the problem. With that change, the user’s `$HOME` env variable is still incorrect as is their entry in `/etc/passwd`. Looking more closely at the code, I don’t see a way to modify the home directory without changing `start.sh` as well. I’ll start experimenting with that. Thanks for your help!

---

<div class="post-metadata">

### Author: ![alexlod](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@alexlod](https://discourse.jupyter.org/u/alexlod)
#### Post date: [September 21, 2023, 5:42pm UTC](https://discourse.jupyter.org/t/dockerspawner-systemuserspawner-manually-set-the-users-home-directory/21506/6 "2023-09-21T17:42:51Z")

</div>

Okay I have a solution that appears to be working.

I subclassed SystemUserSpawner like so:

```auto
def get_username_no_domain(username):
    return username.split('@')[0].lower()

class CustomSystemUserSpawner(SystemUserSpawner):
    def _get_home_dir(self):
        return '/home/' + get_username_no_domain(self.user.name)

    @property
    def host_homedir(self):
        return self._get_home_dir()

    @property
    def homedir(self):
        return self._get_home_dir()

    def get_env(self):
        env = super().get_env()
        env["HOME"] = self.homedir
        return env

```

Then I needed to heavily modify the `start.sh` script in the Docker container that runs individual servers. I more-or-less replaced every occurrence of `/home/${USER}` with `${HOME}`.

I’ll report back if I notice any issues with this approach.

---

<div class="post-metadata">

### Author: ![markperri](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/markperri/32/2187_2.png) [@markperri](https://discourse.jupyter.org/u/markperri)
#### Post date: [September 25, 2023, 11:29pm UTC](https://discourse.jupyter.org/t/dockerspawner-systemuserspawner-manually-set-the-users-home-directory/21506/7 "2023-09-25T23:29:16Z")

</div>

Make sure your users are only coming from one domain, or you will have a huge security issue!
