Persistent SSHFS Mounts: Handling Disconnections and IP Changes


2 views

SSHFS is incredibly useful for mounting remote directories via SSH, but it has one major pain point: it doesn't handle network interruptions gracefully. When your laptop suspends, changes networks, or experiences a temporary disconnect, the SSHFS mount typically breaks and requires manual remounting.

The underlying issue is that SSHFS relies on a single persistent SSH connection. When that connection drops due to:

  • Network timeouts (default SSH server disconnects after ~5 minutes)
  • IP address changes (switching from WiFi to cellular)
  • System suspension

The FUSE layer has no automatic reconnection mechanism.

We can make SSHFS more resilient by combining several techniques:


# Basic persistent mount command
sshfs -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 \
user@remotehost:/remote/path /local/mountpoint

Key options:

  • reconnect: Allows automatic reconnection
  • ServerAliveInterval: Sends keepalive packets every N seconds
  • ServerAliveCountMax: Number of failed keepalives before disconnecting

For even better reliability, combine SSHFS with systemd automount:


# /etc/systemd/system/mnt-remote.mount
[Unit]
Description=SSHFS Mount for Remote
After=network-online.target

[Mount]
What=user@remotehost:/remote/path
Where=/mnt/remote
Type=fuse.sshfs
Options=_netdev,reconnect,ServerAliveInterval=15,ServerAliveCountMax=3,IdentityFile=/home/user/.ssh/id_rsa

[Install]
WantedBy=multi-user.target

# /etc/systemd/system/mnt-remote.automount
[Unit]
Description=Automount SSHFS for Remote

[Automount]
Where=/mnt/remote
TimeoutIdleSec=300

[Install]
WantedBy=multi-user.target

For dynamic IP situations, configure SSH to ignore host key changes:


# ~/.ssh/config
Host remotehost
    HostName remote.example.com
    User username
    StrictHostKeyChecking no
    UserKnownHostsFile /dev/null
    IdentityFile ~/.ssh/id_rsa

Create a watchdog script to check and remount when needed:


#!/bin/bash
MOUNT_POINT="/mnt/remote"
REMOTE_PATH="user@remotehost:/remote/path"

if ! mountpoint -q "$MOUNT_POINT"; then
    echo "Attempting to remount $MOUNT_POINT"
    fusermount -uz "$MOUNT_POINT" 2>/dev/null
    sshfs -o reconnect "$REMOTE_PATH" "$MOUNT_POINT"
fi

Add this to cron to run every minute:


* * * * * /path/to/remount_script.sh

For maximum reliability, use autossh to maintain the underlying SSH connection:


autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" \
-N -L /tmp/sshfs.sock:/var/run/ssh.sock user@remotehost &
sshfs -o ssh_command="ssh -S /tmp/sshfs.sock" user@remotehost:/remote/path /mnt/remote

Working with SSHFS mounts across unstable networks presents a common pain point for developers. The moment your laptop suspends or changes networks, you're left with a broken mount that requires manual reconnection. This becomes particularly frustrating when working with remote servers for extended development sessions.

SSHFS relies on an underlying SSH connection, which typically has these default behaviors:

  1. ServerAliveInterval defaults to 0 (no keepalives)
  2. TCPKeepAlive is usually enabled but may not suffice
  3. Connection timeouts occur after 5-10 minutes of inactivity

Here's the most effective approach combining multiple techniques:

# In your ~/.ssh/config
Host persistent-remote
    HostName your.server.com
    User yourusername
    ServerAliveInterval 30
    ServerAliveCountMax 10
    TCPKeepAlive yes
    ControlMaster auto
    ControlPath ~/.ssh/control:%h:%p:%r
    ControlPersist 1h

When creating your mount, add these options:

sshfs -o reconnect,ServerAliveInterval=30,ServerAliveCountMax=10 \
persistent-remote:/remote/path /local/mount/point

The key options are:

  • reconnect: Attempts to re-establish connection
  • ServerAliveInterval: Sends keepalive packets every 30 seconds
  • ServerAliveCountMax: Allows 10 failed keepalives before disconnecting

For complete resilience, create a watchdog script:

#!/bin/bash
MOUNT_POINT="/local/mount/point"

while true; do
    if ! mountpoint -q "$MOUNT_POINT"; then
        echo "Reconnecting SSHFS mount..."
        fusermount -uz "$MOUNT_POINT" 2>/dev/null
        sshfs -o reconnect persistent-remote:/remote/path "$MOUNT_POINT"
    fi
    sleep 60
done

For extreme cases where SSHFS proves unreliable, consider setting up a WireGuard VPN:

# Sample WireGuard config for persistent tunneling
[Interface]
PrivateKey = your_private_key
Address = 10.8.0.2/24

[Peer]
PublicKey = server_public_key
AllowedIPs = 10.8.0.1/32
Endpoint = your.server.com:51820
PersistentKeepalive = 25

Then mount via the VPN's stable IP address.