Mount network CIFS share on user login (not boot)

Question
Does anyone know how to automatically mount a CIFS share at login (not boot) for a user using specifically their credentials?

If multiple users are to login, then simply mount the share multiple times for each user (in a different read/write protected directory) using each users credentials.

Details
I've been looking into trying to get a network CIFS share to mount on user login (and unmount at user logout). Though I can easily mount the network share at boot, and using a systemd service, I'm struggling to adapt this to mount for a user on login.

Since the network share has tailored permissions for each user, it only makes sense to mount the share for each user. I was thinking that using a systemd user service would be good, but it doesn't want to work.

Below is the systemd user service I've been trying to use (which doesn't work).

# /home/robertfry/.config/systemd/user/media-freenas.saturn.local-Media.mount
[Unit]
Description=Mount FreeNAS Saturn Media (freenas.saturn.local)

[Mount]
What=//freenas.saturn.local/Media
Where=/media/freenas.saturn.local/Media
Type=cifs
Options=credentials=/home/robertfry/.config/freenas.saturn.credentials,iocharset=utf8,rw

Note: I have freenas.saturn.local defined in my /etc/hosts file

Though this service works perfectly as a systemd service (at /etc/systemd/system/), it fails when I try to start it as a user service.

After a little searching, I've read that there should be an entry in /etc/fstab for mounting to work. However, this blocks the systemd service from running, and I'm not able to change which credentials are used for each user.

//freenas.saturn.local/Media    /media/freenas.saturn.local/Media   cifs    _netdev,noauto,x-systemd.automount  0 0

The entire gist of what I want is to mount the share at login using a users credentials at login.

Create a mount script and run it at user login and use gvfs

gio mount smb://freenas.saturn.local/Media

The result of this is the mount point

/run/user/$UID/gvfs/smb-share:server=freenass.saturn.local,share=Media

You can then symlink the folder into the user's folder

 mkdir -p $HOME/Media # no error if the folder exist
 ln -s "/run/user/$UID/gvfs/smb-share:server=freenas.saturn.local,share=Media" "$HOME/Media"

While doing something completely different I had a hunch

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

Forum kindly sponsored by