Back to articles

Mosh - Mobile Shell

Computer TechTerminalMosh - Mobile Shell
Updated 4/23/2026

Mosh - Mobile Shell

Mosh is SSH with roaming support - maintain persistent terminal sessions even when switching networks, dealing with intermittent connectivity, or closing your laptop.

Key advantage: Connection survives network changes, sleep/wake cycles, and temporary disconnections.

Installation

macOS:

bash
brew install mosh

Linux:

bash
# Debian/Ubuntu
sudo apt install mosh

# Arch
sudo pacman -S mosh

# Fedora
sudo dnf install mosh

Server-side: Must be installed on both client and server.

Basic Usage

Replace ssh with mosh:

bash
# Instead of:
ssh user@server

# Use:
mosh user@server

With SSH options:

bash
mosh user@server --ssh="ssh -p 2222 -i ~/.ssh/custom_key"

Roaming Support

The magic: Switch networks without disconnection.

Scenario 1: Laptop sleep/wake

bash
mosh user@server
# Work on remote server
# Close laptop (sleep)
# Open laptop (wake, different network)
# Session instantly reconnects - no "broken pipe"

Scenario 2: Wi-Fi → Cellular

bash
# Connected via office Wi-Fi
mosh user@server
# Leave office, switch to phone hotspot
# Session continues seamlessly

Scenario 3: Spotty connection

bash
# On flaky coffee shop Wi-Fi
mosh user@server
# Connection drops momentarily
# Mosh maintains session, reconnects automatically
# No lost work

How It Works

Mosh uses UDP (not TCP):

  1. Initial connection via SSH (authentication)
  2. Switches to UDP for session (ports 60000-61000)
  3. State synchronization protocol keeps session alive
  4. Client/server sync state even during disconnection

Benefits:

  • Survives IP address changes
  • Tolerates packet loss
  • Instant reconnection
  • Local echo (responsive typing)

Use Cases

Remote Work (Unreliable Networks)

Coffee shop, airport, train:

bash
mosh user@work-server
# Connection may drop, but session persists

Long-Running Tasks

Start process, close laptop:

bash
mosh user@server
./long-running-script.sh
# Close laptop
# Come back hours later
# Session still running, script output visible

Tmux Integration

Perfect combo:

bash
# On server: Start tmux
tmux new -s work

# From laptop:
mosh user@server -- tmux attach -t work

Triple persistence:

  1. Tmux keeps session alive on server
  2. Mosh maintains connection during network changes
  3. Can disconnect Mosh, reconnect later, tmux session intact

Comparison: Mosh vs SSH

FeatureSSHMosh
RoamingNo (breaks on IP change)Yes
Sleep/wakeBreaksSurvives
Spotty Wi-FiDropsTolerates
Local echoNoYes
Port forwardingYesNo
ProtocolTCPUDP (after SSH auth)

Use SSH when: Need port forwarding, agent forwarding.

Use Mosh when: Mobile work, unreliable networks, long sessions.

NetworkChuck Video: 37 INSANE Linux Commands