14

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
  • 358
  • 3
  • 5
  • 17
  • 7
    That feeling when you search for an answer, someone asks exactly the same question and when you want to upvote it, it displays an error that you can't vote for your own question (asked 8 months ago !!!). – Frodik Mar 11 '17 at 07:59

2 Answers2

6

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
  • 831
  • 1
  • 8
  • 11
  • Looks like you accidentally used `ServerAliveInterval=3` in the example but `ServerAliveInterval=5` in the explanation. – Gabriel Staples Sep 11 '20 at 19:30
  • Thanks @GabrielStaples, fixed. – Luke Sep 12 '20 at 09:38
  • @Luke's solution frequently fails with "Transport endpoint not connected", which is unfixable apart from a reboot :( any ideas? – user137063 Mar 10 '23 at 16:53
  • @user137063 as I noted above, the "transport endpoint not connected" error occurs whenever ssh is unable to connect to the server. The error should only persist until you are able to connect again, so not sure why you require a reboot. – Luke May 26 '23 at 18:10
5

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
  • 8,025
  • 7
  • 66
  • 105
  • 2
    This does not answer the question, reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 does not fix the issue that sshfs freezes the whole file system if the remote file system is not available for some reason. – Kuhlambo Mar 24 '20 at 09:38
  • @pindakaas, I'm not sure what you mean. In testing, it does in fact. I can be on the internet, connect to a remote server and mount its drive with `sshfs -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 \ username@server_hostname:/path/on/server/to/mount ~/mnt/my_server`, then **completely disconnect my computer from the network, unplugging it or turning off its wifi entirely**. At that point, trying to use the mount shows it's not available. I can then wait any amount of time (ex: a day), then turn the wifi card back on and it magically works again next time I click on the mount. – Gabriel Staples Jun 09 '20 at 16:58
  • 3
    I had the same experience as @pindakaas -- using this solution causes any process trying to access the mount to hang indefinitely. I wonder if this could be due to differences in sshfs versions. – Luke Sep 11 '20 at 18:57
  • @GabrielStaples What versions are you using? Are you using fuse2 or fuse3? Is it possible you have some other configuration option set somewhere? – hackerb9 Nov 08 '21 at 00:17
  • @Luke, I do not know what `sshfs` versions I had before, but I have updated my answer with my version now, and with a note that says you should probably ensure the versions match on both computers (client and server). – Gabriel Staples Jul 03 '22 at 22:58
  • @hackerb9, I have updated my answer with version information. I do not know what fuse2 and fuse3 mean exactly, but `fusermount -V` shows this for me currently on my Ubuntu 20.04 machine: `fusermount3 version: 3.9.0`. I do _not_ know what my version was when I originally wrote this answer in 2017. `Is it possible you have some other configuration option set somewhere?` I don't know. I suppose it is possible, but I think I had relatively "standard" Ubuntu configurations when I first ran the `sshfs` command I present in my answer. – Gabriel Staples Jul 03 '22 at 23:02