Score:0

Automatically mount CIFS share while connected to a particular network and then automatically unmount either just after or before disconnecting

cn flag

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.

in flag
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](https://askubuntu.com/a/1010638/1222991) 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
cn flag
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.
in flag
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
user535733 avatar
cn flag
This to me looks like a classic use case for [Network Manager dispatcher](https://developer-old.gnome.org/NetworkManager/stable/NetworkManager.html).
Score:1
es flag

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.

Score:0
lr flag

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.
Frobozz avatar
ng flag
Can you please provide complete syntax for your suggested fstab entry?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.