Mosh survives a switch from wifi to 4G/5G because it does not rely on a TCP connection. It sends UDP datagrams, on ports 60000-61000 by default. Its SSP protocol synchronises the screen state, and the server accepts a client whose IP address changes. SSH is used only at the start, to authenticate and launch mosh-server.
How Mosh starts: SSH first, then mosh-server over UDP
Mosh first opens a normal SSH connection, usually to TCP port 22, and uses it to launch mosh-server on the remote machine. mosh-server picks a UDP port from the 60000-61000 range, generates a session key and hands it back to the client over that SSH channel. The SSH connection then closes, and all traffic travels encrypted over UDP.
This has three practical consequences. The server needs mosh-server installed. Authentication is plain SSH: an Ed25519 key, a FIDO2 ed25519-sk key or a password. And the firewall must allow UDP as well as TCP 22. With only port 22 open, the login succeeds, but the client never hears back from the server and you never reach a prompt.
SSH vs Mosh on 4G/5G: what happens when the network changes
A TCP connection is identified by the IP address and port at each end. When a phone moves from wifi to mobile data and gets a new IP, that connection is no longer valid. The SSH session dies even though the server is still up.
| What is measured | SSH over TCP | Mosh over UDP |
|---|---|---|
| Transport | TCP stream, usually port 22 | UDP datagrams, ports 60000-61000 by default |
| Client IP changes | Connection breaks; you must reconnect | Server replies to the new address |
| Brief loss of coverage | Session hangs, then eventually drops | Current screen syncs when the network returns |
| Echo of what you type | Waits for the server round trip | Instant local prediction |
| Port and X11 forwarding | Yes | No |
| Firewall ports | TCP 22 | TCP 22 to start, plus the UDP range |
SSP and IP roaming: how Mosh follows a phone across networks
SSP (State Synchronization Protocol) is the Mosh protocol that synchronises state objects between client and server instead of carrying a byte stream. From server to client, the object is the terminal screen. From client to server, it is your keystrokes.
Every datagram is encrypted and authenticated with the session key. When the server receives a valid datagram from a new IP address or port, it starts replying to that address. That is IP roaming. There is no renegotiation and no fresh login: the new network, whether wifi, 4G or 5G, simply starts delivering packets.
Because Mosh syncs state rather than a stream, it does not have to resend everything lost during an outage. It sends the difference between the last acknowledged screen and the current one. If a process prints a lot of output while you are out of coverage, you see the final screen when you reconnect. You do not wait for a backlog of stale text to drain.
Mosh local prediction: instant echo on high-latency 4G/5G
The Mosh client shows what you type before the server confirms it. That is local prediction, controlled with the --predict option. In adaptive mode, the default, it only kicks in when latency warrants it. always keeps it on and never turns it off.
Until the server confirms a prediction, Mosh shows it underlined. If the real response differs, for example in a program that does not echo keys, the client repaints the screen. On a phone you notice it most when editing in vim or nano over mobile data: the cursor responds without waiting on every round trip.
Mosh commands for firewalls, non-standard SSH ports and tmux
These commands use documented options from mosh, ufw and tmux. Replace the user, host and example ports with your own.
# On the server: open Mosh's default UDP range (ufw)
sudo ufw allow 60000:61000/udp
# SSH listens on another port (2222 is an example)
mosh --ssh="ssh -p 2222" user@server
# Pin a single UDP port for strict firewalls
mosh -p 60001 user@server
# Go straight into tmux: create or reattach the "main" session
mosh user@server -- tmux new-session -A -s main
# Turn local prediction off
mosh --predict=never user@serverThe -- separates mosh options from the remote command. tmux new-session -A -s main attaches to the main session if it already exists and creates it if it does not. The same command therefore works for the first connection and for every one after it.
What Mosh does not do: forwarding, X11, scrollback or reboots
Mosh does no port forwarding and no X11 forwarding. That is the limitation the official Mosh documentation highlights most, but it is not the only one:
- Tunnels: for
-L,-Ror X11 you need a separate SSH connection. - Scrollback: Mosh syncs the visible screen, not everything printed earlier. Use tmux copy mode to get history back.
- Reboots: if the server reboots or
mosh-serverdies, the session is gone. Work inside tmux survives a client disconnect, not a reboot. - Networks without UDP: if a network blocks UDP, Mosh cannot connect. For machines with no public IP or open ports, a private mesh network without open ports such as Prialo, or Tailscale, gives access without exposing the UDP range.
- Files: Mosh does not transfer files. Use SFTP, or
scp, which has used the SFTP protocol by default since OpenSSH 9.0.
Mosh on iOS and Android with termi1: tmux, keyboard and SFTP
termi1 keeps the session alive with Mosh when you move from wifi to mobile data, and uses tmux so whatever you left running is still there when you return. It is an SSH and Mosh mobile terminal that works fully without an account. The account is optional and only syncs connections and shortcuts across devices.
The custom keyboard covers what tmux needs and the system keyboard hides: Ctrl for the Ctrl+b prefix, Esc, Tab, arrows and an fn layer with F1–F12, PgUp/PgDn, Home and End. Because Mosh does not move files, the built-in SFTP lets you browse, upload and download without leaving the session. SSH keys are generated on the phone and stored in the system's secure storage, and the private key never leaves the device.
The AI turns a sentence into a command that is typed at the prompt without pressing Enter. On connections tagged as production, Production Guard holds back anything that deletes, stops or changes something. For a Mac, termi1 Companion (macOS 14 or later) checks remote login, mosh-server and tmux. You can get it on the App Store or on Google Play.
Frequently asked questions
Which ports does Mosh need on the server?
Mosh needs the SSH TCP port, 22 by default, for the initial connection that starts mosh-server. It also needs UDP on the 60000-61000 range, its default. With mosh -p you can pin a single UDP port and open only that one in the firewall.
Does Mosh replace SSH?
Mosh does not replace SSH: it uses SSH to authenticate and launch mosh-server, then moves the interactive session to UDP. Authentication, keys and authorized_keys are still handled by OpenSSH. For port forwarding, X11 or file transfers you still need SSH or SFTP.
Does Mosh keep the session if the server reboots?
Mosh does not keep the session if the server reboots, because the mosh-server process goes down with it. It survives IP changes, coverage drops and client sleep. To protect long-running work from phone disconnects, run it inside tmux.
Why does Mosh fail to connect on some mobile or corporate networks?
Mosh fails to connect when a network allows SSH over TCP but blocks UDP traffic. The login succeeds, but the client never receives a reply from mosh-server. The fix is to open the UDP range or reach the host through a private mesh network such as Prialo or Tailscale.