Connecting over unreliable networks (mobile, WiFi)
Use this skill when:
# macOS
brew install mosh
# Ubuntu/Debian
sudo apt install mosh
# Server must also have mosh installed
# Instead of SSH
ssh user@server
# Use Mosh
mosh user@server
# With specific port
mosh --ssh="ssh -p 2222" user@server
# With different mosh port range
mosh -p 60001 user@server
# Start mosh session
mosh user@server
# Network disconnects (sleep laptop, switch WiFi)
# Session continues when network returns
# Compare to SSH:
# ssh user@server
# [network disconnect]
# -> Connection lost, need to reconnect
# Mosh predicts typing locally
# No lag when typing even on high-latency links
# Underlined text = predicted (not yet confirmed by server)
# Normal text = confirmed by server
# Start mosh on WiFi
mosh user@server
# Switch to cellular
# Session continues seamlessly
# SSH would require reconnect
# Open UDP ports (mosh uses 60000-61000 by default)
sudo ufw allow 60000:61000/udp
# Or specific range
sudo ufw allow 60001/udp
# Server: restrict port range
export MOSH_SERVER_PORT_RANGE=60001-60010
# Client: use restricted range
mosh -p 60001 user@server
# Use mosh over Tailscale
mosh user@100.64.0.5
# Benefits:
# - Encrypted by Tailscale
# - No need to open ports publicly
# - Resilient connection
# ~/.ssh/config
Host myserver
HostName server.example.com
User myuser
Port 2222
# Now use
mosh myserver
# Ultimate resilient setup
mosh user@server -- tmux attach
# Benefits:
# - Mosh handles network disruption
# - Tmux persists session on server
# - Can disconnect mosh and reconnect to same tmux
# โ Can't use scp/rsync over mosh
# Mosh is terminal-only
# โ
Use SSH for file transfers
scp file.txt user@server:
# Or rsync
rsync -avz --progress file.txt user@server:
# Mosh needs UDP ports open
# Some restrictive networks block UDP
# Fallback to SSH if UDP blocked
# SSH:
# - Works everywhere (TCP port 22)
# - File transfers (scp/sftp)
# - Port forwarding
# - Breaks on network change
# Mosh:
# - Survives disconnects
# - IP roaming
# - Low-latency typing
# - Requires UDP ports
# - Terminal only