Prevent Hanging sshfs Connections

Written Tagged , and

sshfs allows mounting files located on a server over ssh. It is hugely convenient in that it allows mounting any server with sftp access without further configuration.

An annoyance I had in using it on my laptop was that when the connection was lost (due to network loss or suspending/hibernate) the filesystem would remail mounted and could not be unmounted cleanly (although a umount -l was a temporary workaround). The problem is that my ssh was not configured to time out quickly when a connection to the server was lots. Adding a couple lines to my user's ssh config allows the connection to time out very quickly.

1
2
3
4
5
6
Host mysshfshost
    TCPKeepAlive yes
    ServerAliveCountMax 1
    ServerAliveInterval 5

ServerAliveInterval specifies the frequency that the client will request a response to the server. ServerAliveCountMax specifies how many of requests will be sent before the client exits.