1

The problem: I have a laptop (running ubuntu 20.04) which is highly mobile and works from multiple locations. While at location A: I have a CIFS share which I need to work. While not at location A, I don't need this particular CIFS share. If I mount the CIFS share manually using everything works, but I would like to automate the process because I change locations at least 2 - 3 times a day. When I connect to the network at location A, I don't want to manually have to mount the cifs file share, I'd rather just like it to be running. Even more importantly, if I disconnect from location A, I don't want my system to crash because it thinks there should be a file system which doesn't exist anymore. Manually completing these tasks is beginning to be tedious very quickly. How do I get the share to mount automatically while connected to a particular network and then unmount when not connected to that same network?

NOTE:This is not a question about fstab, unless of course you know of a way to get the fstab file to behave in this fashion of constantly switching location and networks and only mounting the file system only in the appropriate location/network. I have an fstab entry for the cifs share which makes mounting and unmounting easier, but doesn't actually solve my problem.

Rusty Weber
  • 344
  • 1
  • 3
  • 15
  • How are you connecting to these networks? Wired or wirelessly? Does your notebook have a static IP on the network with the Samba share, or something random each time? Running a script when a network connection is up or down is relatively simple. Having it work for a specific network only may be a challenge unless there’s something unique about the work location to identify – matigo Oct 31 '21 at 22:15
  • No static IP. Connected through hardline, wifi, as well as through VPN. Also have location B, C, and D with the same problem and constraints. – Rusty Weber Oct 31 '21 at 22:20
  • If there’s nothing for the machine to identify a network with, then you may need to mount the samba share manually and have a script run when any interface hits the down and post-down states. The script would check to see if the samba share is mounted and, if it is, unmount – matigo Oct 31 '21 at 22:24
  • This to me looks like a classic use case for Network Manager dispatcher. – user535733 Nov 01 '21 at 01:00

2 Answers2

2

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/SrvAshare.

[2] Then add - as an example - the following in /etc/fstab:

//serverA/sharename /mnt/SrvAshare cifs defaults,uid=1000,noauto,x-systemd.automount,x-systemd.idle-timeout=30,x-systemd.mount-timeout=10 0 0

[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/SrvAshare mount point. It's failry seamless. If for example you just do an ls -l /mnt/SrvAshare 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.

Morbius1
  • 7,761
0

The x-systemd.idle-timeout=30 should be removed and noperm should be added for read/write permission. Username and password part should be added as well.

//serverA/sharename /mnt/SrvAshare cifs username=yourusername,password=yourpassword,defaults,noperm,noauto,x-systemd.automount,x-systemd.mount-timeout=10 0 0
  • A username and password are always required to access any windows share.
  • We need noperm to be able to transfer or edit any files.
  • idle-timeout will cause a remount of the shared folder, even if it stays open in nautilus.