How can I have a filesystem mounted during user login?

6,683

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/[email protected]
[Unit]
Requires=home-me.mount
After=home-me.mount

[Service]
ExecStart=/bin/mount %h/%I
ExecStop=/bin/umount %h/%I
RemainAfterExit=yes

[Install]
WantedBy=default.target

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.

Share:
6,683

Related videos on Youtube

beanaroo
Author by

beanaroo

Linux System Administrator and DevOps Engineer focused on Amazon Web Services

Updated on September 18, 2022

Comments

  • beanaroo
    beanaroo over 1 year

    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.

    • Cestarian
      Cestarian over 8 years
      Have you tried defining the command in ~/.bash_profile? this file is automatically executed upon login.
    • beanaroo
      beanaroo over 8 years
      @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?
    • Cestarian
      Cestarian over 8 years
      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.
    • beanaroo
      beanaroo over 8 years
      @Cestarian, tty is unfortunately not predictable and remote login sessions need to be considered too.
    • Cestarian
      Cestarian over 8 years
      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)
    • Centimane
      Centimane over 7 years
      @beanaroo have you considered using automount? It will mount the filesystem only when it's path is accessed. If the mountpoint is within the users home directory it should only be accessible by that user, so only when they're logged in can it be mounted.