Mosh and tmux: SSH sessions that survive your phone
Why SSH drops on a phone, how Mosh keeps the connection through network changes and sleep, how tmux keeps programs alive, and how to run Claude Code or Codex inside both.
SSH from a phone breaks all the time. You walk out of Wi-Fi range, the phone switches to mobile data, the app goes to the background for a minute — and the session is gone, along with whatever was running in it. Two old, boring tools fix this between them: Mosh keeps the connection alive when the network changes, and tmux keeps your programs alive when the connection dies anyway. Here is how to set up both and why you want both, not one.
1. Why SSH sessions die on a phone
An SSH session is a single TCP connection between two fixed addresses. When your phone
moves from Wi-Fi to LTE, its address changes and the connection has nowhere to go. When
iOS or Android suspends a backgrounded app, it stops answering keepalives and the server
eventually gives up. Either way sshd closes the session, and the shell —
with every program you started in it — gets a hangup signal and exits.
That is two separate problems: the connection is fragile, and the programs are tied to the connection. Mosh solves the first, tmux the second.
2. What Mosh does differently
Mosh (mobile shell) logs in over ordinary SSH, starts a small mosh-server
on the remote machine, and then drops the SSH connection. From that point on the client
and the server talk over encrypted UDP and the session is identified by
a key, not by your IP address. In practice:
- Roaming: switch networks, and the session simply continues from the new address.
- Sleep: lock the phone for an hour, open the app, and you are back where you were.
- Local echo: on a slow link Mosh predicts what you type and shows it immediately (underlined until confirmed), so typing does not lag.
What Mosh does not do: no port forwarding, no agent forwarding, no X11, and no scrollback — it only syncs what is on the screen. The scrollback is the reason to put tmux inside it.
3. Install Mosh on the server and the phone
On the machine you connect to:
# Debian, Ubuntu, Raspberry Pi OS
sudo apt install mosh
# macOS (Homebrew)
brew install mosh
# Fedora
sudo dnf install mosh
Mosh needs UDP ports 60000–61000 open towards the server. On a public
server, open them in the firewall (sudo ufw allow 60000:61000/udp). Over
Tailscale you do not have to open
anything on your router — the tailnet carries UDP between your devices like any other
traffic.
On the phone, pick a client that speaks Mosh:
- Blink Shell (iOS, iPadOS) — Mosh is built in and well done;
- Termius (iOS and Android) — turn on Mosh in the host settings;
- Termux (Android) —
pkg install mosh, then use it like on a computer.
4. Connect with Mosh
mosh you@dev-machine
Your SSH config, keys and host aliases all apply, because the login itself is SSH. Two things commonly go wrong:
- “mosh-server: command not found” on a Mac: a non-interactive SSH
login does not see Homebrew's path. Point the client at it:
mosh --server=/opt/homebrew/bin/mosh-server you@mac(on Intel Macs/usr/local/bin/mosh-server). - Locale errors: Mosh requires a UTF-8 locale on both sides. On a
minimal Linux server,
sudo locale-gen en_US.UTF-8and setLANG=en_US.UTF-8.
If Mosh hangs after the login with no prompt, the UDP traffic is being blocked somewhere — usually a firewall on the server or a restrictive network.
5. Add tmux so programs survive anything
Mosh keeps the connection alive, but if the phone dies, or you close the session on purpose, the programs inside still go with it. tmux runs them in a session that belongs to the server, not to your connection. Detach, disconnect, come back from a different device — the session is still there.
tmux new -s work # start a named session
# Ctrl-b d # detach, leave it running
tmux attach -t work # reattach later
tmux ls # list sessions
A few lines in ~/.tmux.conf make it pleasant on a touch screen:
set -g mouse on # scroll and select panes by touch
set -g history-limit 50000 # long agent output stays scrollable
set -g status-position top # keyboard does not cover the status bar
The two combined into one command — connect and land straight in your session:
mosh you@dev-machine -- tmux new -A -s work
new -A attaches to work if it exists and creates it if it
does not, so the same command works the first time and every time after.
6. Running Claude Code or Codex in Mosh and tmux
This is the setup most people mean when they say they run an AI coding agent “from the phone”: the agent runs in tmux on the computer, the phone connects with Mosh and reattaches.
mosh you@dev-machine -- tmux new -A -s agent
cd ~/projects/app
claude # or: codex
Lock the phone, take the train, open the app again — the agent kept working the whole time, and you see exactly where it is. Tips from daily use:
- Agents need Esc to interrupt and often Shift+Tab to switch modes. Blink and Termius put them on an extra key row; in Termux, enable the extra keys bar.
- Use one tmux window per agent or project (
Ctrl-b cnew,Ctrl-b nnext) instead of separate SSH sessions. - Scroll back through long output in tmux copy mode (
Ctrl-b [), or with your finger once mouse mode is on.
7. What Mosh and tmux will not fix
Together they make a terminal on the phone reliable. They do not make it comfortable, and they cannot tell you anything on their own:
- nobody notifies you when the agent finishes or stops to ask for permission — you find out when you next look;
- diffs and long logs are still a six-inch terminal;
- your computer still has to be reachable, which usually means a VPN like Tailscale.
If steering an agent is the main thing you do from the phone, have a look at coden: the agent runs on your computer, the phone reaches it behind NAT without a VPN over an end-to-end encrypted connection that carries on when you switch from Wi-Fi to mobile data, and you get a notification when the agent finishes or waits for permission — with Allow and Deny right in it. Our comparison of ways to use Claude Code from a phone puts it side by side with SSH and tmux.
Do you see it differently? Write to support@coden-app.com.