Home directories outside of '/home'

The snap daemon (snapd) looks for a user’s home directory ($HOME) under /home on the local filesystem. However, from snapd 2.59 onwards, the snap daemon can access an additional location using the homedirs system option:

sudo snap set system homedirs=<destination-directory>

This allows a snap’s user data to be stored in a user’s home location other under /home.

The following command will permit home directories to be accessible from /remote/users, for instance:

$ sudo snap set system homedirs=/remote/users

The new location needs to exist and be accessible, but it can be on a different filesystem or even mounted across a network. The original /home location remains valid but it is no longer a requirement that directories be stored there.

Once set, the homedirs system option can be retrieved with the snap get command:

$ sudo snap get system homedirs
/remote/users

The homedirs value can be cleared and restored to only /home with the snap unset command:

sudo snap unset system homedirs

Bind mount home directories

While the homedirs system option should work for the majority users, it’s also possible to bind mount an alternative $HOME location to /home to allow other locations to be found by snapd. This process is outlined below.

:information_source: A bind mount allows a mounted filesystem to be accessible from more than one location at the filesystem level. This is unlike a hard or symbolic link, for instance, which operate as special additional files that point to a destination.

There are two steps to bind mount a home directory to a different location:

  1. the bind mount: create the mount point and run the mount command:
    $ sudo mkdir -p /home/$USER
    $ sudo mount --bind <original-home-location> /home/$USER
    
  2. edit /etc/passwd: backup passwd and edit the home location for your user:
    $ cp /etc/passwd passwd.backup
    $ # sudo edit /etc/passwd with your favourite editor
    $ cat /etc/passwd | grep $USER
      ubuntu:x:1000:1000:ubuntu,,,:/home/ubuntu:/bin/bash
    
    The following awk command can be used to edit /etc/passwd (change OLD_HOME to your old home directory):
    $ awk -vold=$"OLD_HOME" -vnew=$"/home/$USER" -F: ' BEGIN {OFS = ":"} \
      {sub(old,new,$6);print}' /etc/passwd > passwd.new
    $ sudo cp passwd.new /etc/passwd
    

Log out and back in again, and snap will work from the freshly mounted home location. If you run into difficulties, copy the backup passwd file to /etc/passwd.


Last updated 10 months ago.