Automatically mount CIFS share while connected to a particular network and then automatically unmount
One possibility is a systemd automount.
[1] Create a mount point.
It cannot be under your home directory or /media. So for example create one at /mnt/4tb.
[2] Then add – as an example – the following in /etc/fstab:
nano /etc/fstab
Add:
//192.168.1.2/4tb /mnt/4tb cifs username=winuser,password=TopSecret,defaults,uid=1000,noauto,noperm,x-systemd.automount,x-systemd.idle-timeout=90
[3] Then make systemd happy:
sudo systemctl daemon-reload
sudo systemctl restart remote-fs.target
It works by not mounting automatically on boot ( noauto ) but as required ( x-systemd.automount ) when you or some application or some process accesses the /mnt/4tb mount point. It’s failry seamless. If for example you just do an ls -l /mnt/4tb
it will mount the share.
x-systemd.idle-timeout=30 will unmount the share if it has not been used for 30 seconds ( user specified ).
x-systemd.mount-timeout=10 will try to mount the share for 10 seconds ( user specified ) then stop if unreachable. Useful if you inadvertently select the mount point when you are in the wrong location.
- We need
noperm
to be able to transfer or edit any files.