19

I am using SSHFS with reconnect attribute. It works fine, when I suspend notebook and start it again. But problem is when I take notebook somewhere - meaning different IP and different connection (might be Wifi or Ethernet).

Mounted SSHFS folder just freezes and also freezes all the applications which were using it. And the only solution to it I have so far is to use

sudo umount -f /mnt/sshfs_folder

Which is nowhere near to beeing good solution, but so far the only which works. Is there a way how to make SSHFS seemlessly take care of this problem so changing network connection after suspend will not cause any problems ?

Oh and I have Ubuntu 14.04, but I suppose that this is SSHFS specific.

Frodik
  • 408

2 Answers2

9

I wrote a shell script called sshfs-reconnect with the following:

while true
do
    sshfs -f -o ConnectTimeout=3,ConnectionAttempts=1,ServerAliveInterval=5,ServerAliveCountMax=3 $@
    echo "disconnected from $@"
    sleep 3
    echo "retry $@ ..."
done

If I leave this running in the background, it has most of the desired behaviors:

  • If the server disappears briefly, processes accessing the mount will hang until the server comes back
  • If the server disappears for too long (about 15 seconds in this case) then waiting processes will get an I/O error
  • If we try to access the mount again while the server is gone, we either get a "transport endpoint is not connected" error or file-not-found (I would prefer to get only the transport error, but haven't figured out how to achieve that)
  • Once the server comes back, we can access the mount again with no intervention

Details about this solution:

  • I do not use -o reconnect here because I do not want sshfs to attempt to reconnect indefinitely, since this causes any process waiting on the mounted filesystem to hang forever. Instead, I let sshfs disconnect after a certain timeout and let those waiting processes fail gracefully.
  • The options ServerAliveInterval=5,ServerAliveCountMax=3 say that ssh checks every 5 seconds to see whether the remote server is still responding. If it does not receive a response after 3 of these checks, then it disconnects automatically. Thus any process waiting on this mount should hang about 15 seconds max.
  • The options ConnectTimeout=3,ConnectionAttempts=1 are necessary because otherwise ssh will wait a very long time when attempting to reestablish connections, which again causes other processes to hang if they try to access the mount during this time.
Luke
  • 919
6

More thorough answer here: SSHFS - auto reconnect.

In short, use:

# create local folder to mount into
mkdir -p ~/mnt/my_server

now mount your SSH File System

sshfs -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3
username@server_hostname:/path/on/server/to/mount ~/mnt/my_server

To unmount the file system:

sudo umount ~/mnt/my_server

Added July 2022:

I am not sure, but it may be important to ensure that both sides have the same version of sshfs installed. That means that both the computer you are running sshfs on and the computer you are connecting to should have the same version. Check the version with sshfs -V. Here is an example run of that command on my Ubuntu 20.04 computer, for example, showing both the command and the output:

$ sshfs -V
SSHFS version 3.7.1
FUSE library version 3.9.0
using FUSE kernel interface version 7.31
fusermount3 version: 3.9.0

You can see that the last line above is the output from the command fusermount -V, which for me shows this:

$ fusermount -V
fusermount3 version: 3.9.0

I have used the above command on Ubuntu 14.04, 16.04, 18.04, and 20.04. I did not verify the sshfs versions prior to now (July 2022), so I cannot state what versions I had at the time I originally wrote this answer (Aug. 2017).

Gabriel Staples
  • 11,502
  • 14
  • 97
  • 142