I would like a file based filesystem (~/Archives/inventory.locker) mounted upon user login and unmounted upon logout (~/Documents/Inventory).

pam_mount seems to provide the functionality I am after, but it has incompatibilites with pam_systemd.

I have tried writing a user based systemd.mount unit, but it fails with:

mount: only root can do that

Even though I have the 'user' mount option defined and can successfully mount as user manually.

The systemd method seems ideal because it requires no other dependencies and is also per user process and not per login session.

I am open to alternative solutions too.

  • Have you tried defining the command in ~/.bash_profile? this file is automatically executed upon login. – Cestarian Jan 29 '16 at 4:52
  • @Cestarian, problem is this would be sourced for every login session, not just the initial. (Though a condition could probably work). How would I ensure it gets unmounted on the last logout? – beanaroo Jan 29 '16 at 5:07
  • You can probably ensure the unmount with ~/.bash_logout. Conditions can be created, for example you can create a condition that you are on a specific tty (like tty1). I believe this is how: [[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && insert command here there are probably other more clever conditions you can make as well, but i'm not an expert. – Cestarian Jan 29 '16 at 5:11
  • @Cestarian, tty is unfortunately not predictable and remote login sessions need to be considered too. – beanaroo Jan 29 '16 at 5:14
  • these commands do support ssh logins. But maybe you can do a simple if command (e.g. if the user does not have an existing session, execute the command, this shouldn't be hard to do. To check if there exists a session, run who | grep username to list existing user sessions, if the output is null then the command should be executed) – Cestarian Jan 29 '16 at 5:20

Latecomer here. It may be a little counterintuitive, but I use the service (rather than mount) systemd user unit and it works for me. I had to add the user and noauto options to /etc/fstab entry.

cat ~/.config/systemd/user/mount@.service

ExecStart=/bin/mount %h/%I
ExecStop=/bin/umount %h/%I


You should enable the unit instance with a command such as:

systemctl --user enable mount@some-directory

Help with the @ in the filename, can be found reading about systemd instantiated units.

| improve this answer | |

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.