SSH drops the moment your laptop sleeps, your train goes into a tunnel or your iPhone switches from Wi-Fi to 5G. Mosh fixes the connection part. tmux fixes the "my job died with it" part. Together they make a remote shell you can close and reopen from anywhere.
This guide sets up both on a Linux server and shows how to connect from a Mac and from an iPhone.
The short answer
# on the server sudo apt install mosh tmux # Debian / Ubuntu sudo ufw allow 60000:61000/udp # only if ufw is on # on the Mac brew install mosh mosh user@your-server -- tmux new -A -s main tmux new -A -s main attaches to the session called main if it exists, or creates it. Run the same command again tomorrow and you are back where you left off.
What Mosh does, and what it does not
Mosh (mobile shell) logs in over SSH, starts mosh-server on the remote machine, and then talks to it over UDP. Because the session is not tied to one TCP connection:
- it survives a change of IP address (Wi-Fi to cellular, home to office);
- it survives the laptop sleeping or the iPhone locking;
- it shows what you type right away, even on a slow link, instead of waiting for the server's echo.
What it does not do: keep your programs running if the server side of the session ends, scroll back through history the way your terminal does, or forward ports. For the first two, you add tmux.
Install on the server
Debian and Ubuntu:
sudo apt update sudo apt install mosh tmux Fedora, RHEL and friends:
sudo dnf install mosh tmux Mosh uses one UDP port per session in the range 60000 to 61000. If a firewall is on, open it:
sudo ufw allow 60000:61000/udp On a cloud server, add the same range as an inbound UDP rule in the provider's firewall too.
Mosh also needs a UTF-8 locale on the server. If you get an error about LANG or LC_ALL, this usually fixes it:
sudo locale-gen en_US.UTF-8 Connect from a Mac
Install the client with Homebrew and connect the same way you would with SSH:
brew install mosh mosh user@your-server If SSH runs on another port, pass the SSH command through:
mosh --ssh="ssh -p 2222" user@your-server Your ~/.ssh/config is used as usual, so a host alias works too: mosh myserver.
Add tmux so the work survives
Mosh keeps the connection alive. tmux keeps the programs alive, even if you close the Mosh session on purpose or the server reboots your login.
Start or reattach in one step:
mosh myserver -- tmux new -A -s main The keys you need on day one:
| Keys | What it does |
|---|---|
Ctrl-b d | detach: leave everything running and disconnect |
Ctrl-b c | new window |
Ctrl-b n / Ctrl-b p | next / previous window |
Ctrl-b % | split side by side |
Ctrl-b [ | scroll back (press q to leave) |
Later, from any device:
tmux ls # list sessions tmux attach -t main # go back in This is the setup many people use to leave a long build, a database migration or an AI coding agent such as Claude Code or Codex running on a server and check on it from the phone.
Connect from an iPhone or iPad
iOS has no built-in terminal, so you need an SSH app that supports Mosh. Look for three things:
- Mosh support, not only SSH, so the session survives the phone locking;
- key handling you trust, ideally keys that never leave the device;
- a way to reattach to your tmux session without typing the command every time.
The flow is the same as on the Mac: connect with Mosh, then tmux new -A -s main.
We make Wardterm, an SSH client for Mac, iPhone and iPad, and we built it around this exact workflow. Mosh is included on every device, and Keep This Session moves the current session into tmux on the server for you, so you can close the app and pick it up later from the Mac or the phone. If you run AI agents on the server, it also tells you when one is waiting for an answer. The free plan has no limit on servers: https://wardterm.com/
Troubleshooting
"mosh-server: command not found"
Mosh is not installed on the server, or not on the PATH of a non-interactive SSH login. Install it, or point to it: mosh --server=/usr/local/bin/mosh-server user@host.
It hangs at "Connecting..." and then times out
UDP is blocked. Open 60000-61000/udp on the server firewall and on the cloud provider's firewall. Corporate and hotel networks often block UDP: SSH plus tmux still works there.
"The locale requested by LC_ALL=... isn't available"
Generate a UTF-8 locale on the server (sudo locale-gen en_US.UTF-8) or set LANG=en_US.UTF-8 on the client.
Scrolling with the trackpad does nothing
Mosh does not keep scrollback. Use tmux: Ctrl-b [, then arrow keys or Page Up.
Summary
- Mosh keeps the connection alive across network changes and sleep.
- tmux keeps your programs alive and lets you reattach from any device.
-
mosh myserver -- tmux new -A -s mainis the one command to remember.
A longer comparison of Mosh clients for Mac and iPhone is on our blog: https://wardterm.com/blog/best-mosh-client/

