89

I'm using SSHFS mounts from my laptop to a central server.

Obviously, the SSHFS mount is broken after a longer disconnect (eg. during suspend), cause the underlying SSH connection timed out.

Is there a way to get SSHFS mounts surviving long lasting disconnections (> 5 min) or even a re-dialin with a different IP?

bene
  • 2,214
  • 2
  • 19
  • 14

6 Answers6

107

Use -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3

The combination ServerAliveInterval=15,ServerAliveCountMax=3 causes the I/O errors to pop out after one minute of network outage. This is important but largely undocumented. If ServerAliveInterval option is left at default (so without the alive check), processes which experience I/O hang seem to sleep indefinitely, even after the sshfs gets reconnect'ed. I regard this a useless behavior.

In other words what happens on -o reconnect without assigning ServerAliveInterval is that any I/O will either succeed, or hang the application indefinitely if the ssh reconnects underneath. A typical application becomes entirely hung as a result. If you'd wish to allow I/O to return an error and resume the application, you need ServerAliveInterval=1 or greater.

The ServerAliveCountMax=3 is the default anyway, but I like to specify it for readability.

kubanczyk
  • 13,502
  • 5
  • 40
  • 55
  • This made it for me. Also you can use a shorter interval, afaik it shouldn't cause too much trouble and makes the whole thing more responsive. – Manux Jan 12 '16 at 16:12
  • I didn't read the docs, but I saw another solution say "ServerAliveCountMax=0" means it will keep trying indefinitely. I assume your solution gives it 3 retries after each failure? – PJ Brunet May 28 '18 at 06:18
  • @PJBrunet `man 5 ssh_config` for more details, but the gist is that every 15 seconds ssh will send something like a 'keep-alive' ping every 15 seconds to make sure the computers are still responding to each other. If three consecutive pings fail (45 seconds), reconnect. – Wyatt Ward Jun 17 '18 at 19:22
  • @Wyatt8740 I studied this more and read the documentation, I believe `ServerAliveCountMax=3` has a different meaning. If there is a failure, it will try reconnecting 3 more times and then give up. At some point retrying is futile, but that depends on the application. Then again, I think the man page could be more specific, there's different ways to interpret the way it's worded IMO. FWIW my specific problem went away after switching to ProtonVPN. I think it's also worth checking ssh configuration on the client and the server, they have separate options, so all you really need is `-o reconnect` – PJ Brunet Jun 18 '18 at 17:23
  • @PJBrunet The real reason is stated in the answer but largely undocumented: "Without these options [ServerAliveInterval], processes which experience I/O hang seem to sleep indefinitely, even after the sshfs gets reconnect'ed". I'm expanding my answer a bit to clarify this even more. You are right - ServerAliveCountMax=3 is the default hence superfluous. – kubanczyk Sep 20 '18 at 11:12
  • 1
    I'd like the I/O to retry and never return ANY error, continuing like nothing ever happened. The fact that an I/O error is happening at all is causing me much grief as my my video recording unceremoniously gets terminated making the resulting video file unplayable! – Michael Apr 16 '20 at 04:59
  • How am I supposed to add those options on /etc/fstab configuration? The current syntax of the filesystem is `sshfs#user@hostname:/path`. – Soullivaneuh Apr 27 '20 at 21:54
60

Thanks for the tips of autossh and autofs.

However, for my direct purpose I found a much simpler solution which wasn't documented so well:

sshfs -o reconnect server:/path/to/mount
muru
  • 569
  • 7
  • 26
bene
  • 2,214
  • 2
  • 19
  • 14
12

Autossh automatically reconnects ssh sessions when it notices ssh has died or stopped passing traffic. Since it is just automated ssh, it will work from different IP's and from suspend (even if the laptop wakes up on a different lan).

kband
  • 459
  • 2
  • 6
  • Perhaps [Mosh](https://mosh.org/) is even better for an interactive SSH session. – Martin Ueding Apr 14 '18 at 14:28
  • 2
    @MartinUeding I looked up Mosh but a) doesn't work with sshfs b) it appears you need to install it on the server as well, which makes it less appealing, IMO. – PJ Brunet May 28 '18 at 06:14
9

One thing you could do is mount your filesystems via autofs. Autofs is a tool that will mount a filesystem when you to use something in the directory that the filesystem will be mounted to. When it detects activity the filesystem is mounted. When nothing is happening on the filesystem is it unmounted.

Here is a howto I found on google to accomplish this, there where several others.

Zoredache
  • 128,755
  • 40
  • 271
  • 413
2

I suspect there isn't, because even if you can configure your SSH client not to drop the connection, the server might be configured to do so after a specified period of inactivity, and you wouldn't be able to override that. Even if you could, if you never resume the connection, the server would be left hanging, and over time that could lead to a significant waste of server resources.

A better technique, I think, is to unmount the filesystem before suspending your computer and remount it when the computer wakes up again. The mechanism for doing so may depend on exactly how you suspend your computer - I use the tuxonice kernel and to do something like this I have a directive like

Unmount /mnt/sshfs

in /etc/hibernate/common.conf.

David Z
  • 5,376
  • 2
  • 24
  • 22
0

The answer of kubanczyk is great. I had a problem with freezing entire the interface because of too greedy sshfs, now for easy connection started by a script that reconnects when laptop is opened and that does not freeze when the connection gets slower, you can use a bash script like that (maybe not very secure, but convenient for many web projects for example):

#!/bin/bash
echo PWD | sshfs USER@SERVER:/ MOUNT_PATH -o password_stdin,reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 -p PORT -C -oStrictHostKeyChecking=no
if xhost >& /dev/null ; then
    pcmanfm MOUNT_PATH
fi
  • I suspect it's a bad idea to use `password_stdin` and `-oStrictHostKeyChecking=no`. This would leak the password to anyone who manages to feed you a bogus DNS / reroute the IP address to their machine, i.e. any free wi-fi you connect to could get your password. – Suzanne Soy Nov 13 '20 at 19:42