Network filesystem client moving between networks

At home, I have a network drive and devices that mount it from a local IPv4 address. Away, my laptop mounts that same drive with sshfs and a DNS-resolvable hostname.

Moving the laptop from network to network is still a problem. I never remember to unmount the filesystem before closing the laptop (and who would?). Shells become unresponsive, lock the mount point, and generally make it a fuss to get back to work.

I imagine the system silently remounting as needed, ideally without interrupting processes with open files. Perhaps this process could be triggered by a wifi rule that runs a certain script upon every network affiliation.

Is there a best practice or a better way to look at this problem?


Solution 1:

The way I solve this problem - the practicality of which depends on your network architecture - is to make extensive use of OpenVPN - and by accessing the devicesthrough IPs which are reachable through the VPN if the underlying IP addresses change it does not matter - OpenVPN reconnnects with the same internal IPs and the TCP session resume.

Solution 2:

For this purpose I use autofs.

autofs is a program for automatically mounting directories on an as-needed basis. Auto-mounts are mounted only as they are accessed, and are unmounted after a period of inactivity.

The actual mounting is done via VPN to my home router; this way I mount the same share existing at the same address, regardless whether I'm away or at home (relevant: my router supports hairpinning).

In my case it's plain NFS mount without authentication, the whole security is at VPN level. Autofs can handle other mounts, see this answer of mine where it was used with cifs.


Another possibility is systemd automount (see this question: Systemd automount vs autofs) but I have never used it yet.