SSH sessions die when your laptop sleeps, you switch from Wi-Fi to mobile data, or the connection is lossy, as with trains, hotel Wi-Fi or long-distance links. mosh (mobile shell) keeps the session alive through all of that and shows keystrokes immediately, even on high-latency connections. It’s ideal for administering a server far away, say a VPS in Tokyo or Los Angeles from another continent.
How it works
- mosh logs in with regular SSH, using your keys and SSH config.
- It starts a
mosh-serveron the remote machine and then switches to its own encrypted protocol over UDP. - The SSH connection closes. The session now continues over UDP, so it doesn’t care if your IP changes. When your laptop wakes up on a new network, the session simply resumes.
- Local echo: mosh predicts what your keystrokes will show and displays them instantly, correcting if the server disagrees. Typing feels local even at 300 ms latency.
Install
On the server and on your computer:
sudo apt install mosh # Debian/Ubuntu
sudo dnf install mosh # RHEL family (EPEL)
brew install mosh # macOS
On Windows, use mosh inside WSL, or a terminal app with mosh support.
Open the firewall
mosh uses UDP ports 60000–61000 by default (one port per session):
sudo ufw allow 60000:61000/udp
To expose fewer ports, open a small range (for example 60000:60010/udp) and pin the client to it with -p:
mosh -p 60001 user@server
See UFW.
Connect
mosh [email protected]
mosh web1 # uses your ~/.ssh/config alias
mosh --ssh="ssh -p 2222" user@server # SSH on a custom port
mosh web1 -- tmux new -A -s main # run tmux directly
Close your laptop, open it on another network, and the session is still there. If the network is gone, mosh shows a blue bar (“Last contact 20 seconds ago”) and reconnects automatically when it can.
Locale requirement
mosh needs a UTF-8 locale on both sides. If you see The locale requested by LANG=… isn't available here, generate it on the server:
sudo locale-gen en_US.UTF-8
See locale settings.
mosh + tmux: the best of both
mosh keeps the connection alive across network changes. tmux keeps the session alive even if mosh-server or your client exits (a client crash, a reboot of your laptop), and gives you windows, splits and scrollback.
mosh web1 -- tmux new -A -s main
new -A attaches to the main session if it exists, and creates it otherwise. See tmux.
Limitations
- No scrollback in mosh itself, because it syncs the screen state, not the whole output history. Use tmux for scrollback.
- No port forwarding, agent forwarding or X11. Use regular SSH for those. See SSH tunnels.
- UDP must be allowed by the networks in between. Some strict corporate or hotel networks block it, and then fall back to SSH.
- Orphaned mosh-server processes can remain if a client disappears for good. They exit eventually, or you can clean them up with
pkill -u $USER mosh-server(it also kills your active mosh sessions). - Security: mosh’s protocol is encrypted (AES-OCB) and authenticated via the SSH login. The server process runs as your user. Keep it updated like any network-facing software.
mosh vs SSH with keepalives
| Situation | Use |
|---|---|
| Stable office or home connection | SSH with ServerAliveInterval |
| Laptop that sleeps, changes networks | mosh (+ tmux) |
| High latency (intercontinental) | mosh, for typing responsiveness |
| File transfer, tunnels, Git | SSH |
| UDP blocked | SSH + tmux |
Quick reference
| Task | Command |
|---|---|
| Install | apt install mosh (both sides) |
| Firewall | ufw allow 60000:61000/udp |
| Connect | mosh user@host |
| Custom SSH port | mosh --ssh="ssh -p 2222" user@host |
| Fixed UDP port | mosh -p 60001 user@host |
| With tmux | mosh host -- tmux new -A -s main |
Related: SSH config file and screen.