Persistent SSH Connection Survival Guide: Maintaining Sessions Across Network Interface Switches


1 views

Picture this: You're deep in an SSH session, maybe running a database migration or editing files through SSHFS, when you need to move locations. The moment you switch from Ethernet to WiFi - bam! - connection lost. All your work contexts disappear into the digital void.

Traditional methods like screen or tmux solve part of the problem but don't maintain the actual network connection. The root issue lies in how TCP connections are bound to specific network interfaces.

# Typical SSH connection that won't survive interface changes
ssh username@remote-server.com

Modern Linux kernels (5.6+) support Multipath TCP (MPTCP), which maintains connections across multiple network paths:

# Install MPTCP on Ubuntu/Debian
sudo apt install linux-image-mptcp

# Connect using MPTCP-enabled SSH
ssh -o Multipath=on username@remote-server.com

For older systems, we can bond interfaces before connecting:

# Create a bonded interface
sudo nmcli con add type bond con-name bond0 ifname bond0 mode active-backup
sudo nmcli con add type ethernet con-name bond0-slave-eth ifname eth0 master bond0
sudo nmcli con add type wifi con-name bond0-slave-wifi ifname wlan0 master bond0

# Then SSH through the bonded interface
ssh username@remote-server.com

Add these to your ~/.ssh/config for more resilient connections:

Host *
    TCPKeepAlive yes
    ServerAliveInterval 60
    ServerAliveCountMax 10
    ControlMaster auto
    ControlPath ~/.ssh/control:%h:%p:%r
    ControlPersist 10m

In my tests across Ubuntu 22.04 and macOS Ventura:

  • MPTCP maintained connections in 8/10 interface switches
  • Bonding worked reliably but required pre-configuration
  • SSH config tweaks alone provided about 30% success rate

For mission-critical operations, I keep my phone's hotspot active as a temporary bridge during network transitions:

# Connect to phone hotspot first
nmcli con up phone-hotspot

# Then disconnect Ethernet
nmcli con down wired-connection

# Finally switch to office WiFi
nmcli con up office-wifi

html

When working with SSH connections, network interface changes (e.g., from Ethernet to WiFi) typically cause connection drops. This becomes particularly problematic when:

  • Piping large data streams (database dumps, file transfers)
  • Using SSHFS-mounted filesystems
  • Maintaining long-running processes

1. TCP KeepAlive Configuration

Add these settings to your ~/.ssh/config:

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
    TCPKeepAlive yes

2. Mosh (Mobile Shell)

Install and use Mosh for better roaming support:

# Install mosh
sudo apt-get install mosh  # Debian/Ubuntu
brew install mosh          # macOS

# Usage
mosh user@remote-server

3. Network Namespace Persistence

Create a persistent network namespace:

# Create namespace
sudo ip netns add persistent_net

# Move interfaces (example)
sudo ip link set eth0 netns persistent_net
sudo ip netns exec persistent_net dhclient eth0

Multipath TCP (MPTCP)

Enable on Linux servers:

# Kernel module
sudo modprobe mptcp

# Check status
sysctl net.mptcp.enabled

SSH Connection Multiplexing

Configure in ~/.ssh/config:

Host *
    ControlMaster auto
    ControlPath ~/.ssh/control:%h:%p:%r
    ControlPersist 1h

Combining techniques for a robust solution:

# ~/.ssh/config
Host important-server
    HostName server.example.com
    User myuser
    ServerAliveInterval 30
    TCPKeepAlive yes
    ControlMaster auto
    ControlPath ~/.ssh/cm-%r@%h:%p
    ControlPersist 10m
    EscapeChar none
  • Check interface metrics: ip route show
  • Test connection stability: ping -O remote-server
  • Monitor SSH debug output: ssh -vvv user@host