15

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.

1
  • 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, 2017 at 7:59

2 Answers 2

7

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.
5
  • Looks like you accidentally used ServerAliveInterval=3 in the example but ServerAliveInterval=5 in the explanation. Sep 11, 2020 at 19:30
  • Thanks @GabrielStaples, fixed.
    – Luke
    Sep 12, 2020 at 9:38
  • @Luke's solution frequently fails with "Transport endpoint not connected", which is unfixable apart from a reboot :( any ideas?
    – user137063
    Mar 10, 2023 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, 2023 at 18:10
  • @user137063: You could try reloading your wifi driver Remove it with modprobe -r and then load it again with modprobe.
    – Fritz
    Jan 10 at 12:35
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).

6
  • 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, 2020 at 9: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. Jun 9, 2020 at 16:58
  • 4
    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, 2020 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 8, 2021 at 0: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). Jul 3, 2022 at 22:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .