mosh — Roaming-Tolerant Remote Shell over UDP
Practical guide to mosh — a latency-tolerant SSH alternative for unreliable connections, network changes, and mobile work with UDP-based roaming.
mosh is a latency-tolerant alternative to SSH for unreliable connections: the session stays alive when you switch networks, close your laptop lid, or roam across Wi-Fi access points. After an initial SSH handshake, mosh switches to UDP – making network changes and brief interruptions fully transparent. Local echo means your terminal responds instantly even on high-latency links. That makes mosh the go-to choice for remote work over mobile data, train Wi-Fi, or flaky VPNs.
Basic Usage
mosh <user>@<host> — Connect to a remote host (like ssh).
mosh admin@server.example.commosh <host> — Connect using current username.
mosh server.example.commosh --ssh='ssh -p <port>' <user>@<host> — Use a custom SSH port for the initial connection.
mosh --ssh='ssh -p 2222' admin@server.example.commosh --ssh='ssh -i <key>' <user>@<host> — Use a specific SSH identity key.
mosh --ssh='ssh -i ~/.ssh/id_ed25519' admin@serverPort & Network
mosh -p <port> <user>@<host> — Use a specific UDP port for mosh traffic.
mosh -p 60001 admin@servermosh -p <start>:<end> <user>@<host> — Specify a UDP port range.
mosh -p 60000:60010 admin@servermosh --bind-server=<addr> <user>@<host> — Bind mosh-server to a specific IP address.
mosh --bind-server=0.0.0.0 admin@serverServer Options
mosh --server=<path> <user>@<host> — Specify the path to mosh-server on the remote host.
mosh --server=/usr/local/bin/mosh-server admin@servermosh --server='mosh-server new -l LANG=en_US.UTF-8' <host> — Set locale on the remote mosh-server.
mosh --server='mosh-server new -l LANG=en_US.UTF-8' admin@servermosh --predict=<mode> <user>@<host> — Set prediction mode (adaptive, always, never, experimental).
mosh --predict=always admin@serverSession Management
Ctrl+^ . — Disconnect from mosh session (like ssh ~.).
Press Ctrl+^ then .mosh <host> -- tmux a — Connect and reattach to a tmux session.
mosh admin@server -- tmux amosh <host> -- screen -r — Connect and reattach to a screen session.
mosh admin@server -- screen -rMosh vs SSH
mosh <host> — Survives network changes, sleep/wake, and Wi-Fi roaming.
mosh admin@server (keeps working after laptop sleep)mosh <host> — Uses UDP after initial SSH handshake (more resilient under packet loss).
mosh admin@server (uses UDP port 60000+ after SSH auth)mosh <host> — Local echo: shows keystrokes instantly even on slow connections.
mosh admin@server (responsive even on 500ms latency)ssh <host> — Use SSH when you need port forwarding, X11, or SOCKS proxy (mosh doesn't support these).
ssh -L 8080:localhost:80 admin@server (port forwarding needs ssh) Conclusion
mosh does not replace SSH entirely, but for mobile work and unreliable connections it is the superior choice. Pair it with tmux or screen to resume sessions seamlessly even after genuine disconnects. If you regularly work on trains, in cafés, or over mobile data, mosh deserves a permanent place in your workflow.
Further Reading
- Mosh – Wikipedia – background and history
- mosh.org – official website – project site and documentation
Related Commands
- ssh – secure remote shell with encryption and port forwarding
- scp – securely copy files over SSH to remote hosts
- ssh-keygen – create and manage SSH key pairs