# 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.

Source: https://www.jpkc.com/db/en/cheatsheets/networking/mosh/

<!-- PROSE:intro -->
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.
<!-- PROSE:intro:end -->

## Basic Usage

`mosh <user>@<host>` — Connect to a remote host (like ssh).

```bash
mosh admin@server.example.com
```

`mosh <host>` — Connect using current username.

```bash
mosh server.example.com
```

`mosh --ssh='ssh -p <port>' <user>@<host>` — Use a custom SSH port for the initial connection.

```bash
mosh --ssh='ssh -p 2222' admin@server.example.com
```

`mosh --ssh='ssh -i <key>' <user>@<host>` — Use a specific SSH identity key.

```bash
mosh --ssh='ssh -i ~/.ssh/id_ed25519' admin@server
```

## Port & Network

`mosh -p <port> <user>@<host>` — Use a specific UDP port for mosh traffic.

```bash
mosh -p 60001 admin@server
```

`mosh -p <start>:<end> <user>@<host>` — Specify a UDP port range.

```bash
mosh -p 60000:60010 admin@server
```

`mosh --bind-server=<addr> <user>@<host>` — Bind mosh-server to a specific IP address.

```bash
mosh --bind-server=0.0.0.0 admin@server
```

## Server Options

`mosh --server=<path> <user>@<host>` — Specify the path to mosh-server on the remote host.

```bash
mosh --server=/usr/local/bin/mosh-server admin@server
```

`mosh --server='mosh-server new -l LANG=en_US.UTF-8' <host>` — Set locale on the remote mosh-server.

```bash
mosh --server='mosh-server new -l LANG=en_US.UTF-8' admin@server
```

`mosh --predict=<mode> <user>@<host>` — Set prediction mode (adaptive, always, never, experimental).

```bash
mosh --predict=always admin@server
```

## Session Management

`Ctrl+^ .` — Disconnect from mosh session (like ssh ~.).

```bash
Press Ctrl+^ then .
```

`mosh <host> -- tmux a` — Connect and reattach to a tmux session.

```bash
mosh admin@server -- tmux a
```

`mosh <host> -- screen -r` — Connect and reattach to a screen session.

```bash
mosh admin@server -- screen -r
```

## Mosh vs SSH

`mosh <host>` — Survives network changes, sleep/wake, and Wi-Fi roaming.

```bash
mosh admin@server (keeps working after laptop sleep)
```

`mosh <host>` — Uses UDP after initial SSH handshake (more resilient under packet loss).

```bash
mosh admin@server (uses UDP port 60000+ after SSH auth)
```

`mosh <host>` — Local echo: shows keystrokes instantly even on slow connections.

```bash
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).

```bash
ssh -L 8080:localhost:80 admin@server (port forwarding needs ssh)
```

<!-- PROSE:outro -->
## 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](https://en.wikipedia.org/wiki/Mosh_(software)) – background and history
- [mosh.org – official website](https://mosh.org/) – project site and documentation
<!-- PROSE:outro:end -->

## Related Commands

- [ssh](https://www.jpkc.com/db/en/cheatsheets/networking/ssh/) – secure remote shell with encryption and port forwarding
- [scp](https://www.jpkc.com/db/en/cheatsheets/networking/scp/) – securely copy files over SSH to remote hosts
- [ssh-keygen](https://www.jpkc.com/db/en/cheatsheets/networking/ssh-keygen/) – create and manage SSH key pairs

