SSH on your phone without disconnects: a mosh and tmux guide
Fixing dropped SSH on a phone takes two pieces. tmux keeps programs running on the server, so after a drop you reattach and land on the same screen. mosh carries the terminal over UDP instead of TCP, so the phone can change IP or sleep for a few minutes without logging in again. tmux alone means you never lose work but have to reconnect after each drop; with both, it feels like the connection never broke.
Checked against mosh 1.4.0 and tmux 3.x; commands use Ubuntu / Debian as the example.
Why SSH keeps dropping on a phone
- Changing networks: SSH runs over TCP, and a TCP connection is tied to the phone's IP address at the time. Walk from Wi-Fi onto mobile data, the IP changes, and the connection is dead.
- Locking the screen and backgrounding: iOS suspends an app's networking soon after it goes to the background, and Android's power saving freezes background apps too. Leave a connection idle long enough and a NAT or firewall along the way silently forgets it.
- Programs die with the connection: a program started straight from an SSH session is a child of that connection. When the connection goes, the server sends SIGHUP and the program usually exits.
The first two are connection problems, which mosh solves; the third is a process problem, which tmux solves.
Step one: keep the work alive with tmux
sudo apt install tmux
After every login, go into the same session, attaching if it exists and creating it otherwise:
tmux new -A -s main
Run builds, tests or AI agents inside it. After a drop, log in again and run the same command to get back to the screen you left. To leave on purpose and let the work continue, press Ctrl+B then D.
tmux settings for a phone
Put these in ~/.tmux.conf and run tmux source ~/.tmux.conf:
set -g mouse on # finger swipes scroll tmux's history
set -g history-limit 50000 # keep more history; agent output is long
set -g status off # no status bar on a small screen (turn it back on when needed)
set -s escape-time 10 # no delay after Esc, so TUIs like Claude Code feel responsive
Enter tmux automatically on login
If you only want this when logging in from the phone, set the phone client's "command to run after login" to tmux new -A -s main. Starting tmux unconditionally from ~/.bashrc isn't recommended: it gets in the way of scp, rsync and logins from your computer.
Step two: follow the phone across networks with mosh
mosh logs in once over ordinary SSH, starts mosh-server on the server, and then switches the terminal to encrypted UDP. The client sends a heartbeat every few seconds, and the server always replies to the address of the most recent authentic packet, so the session carries on when the phone's IP changes, without a new login. On a slow link mosh shows your keystrokes locally right away (underlined until confirmed) instead of waiting for the server's echo. For why this works, see how mosh works.
Server setup
# Ubuntu / Debian
sudo apt install mosh
# macOS (Homebrew)
brew install mosh
Then open the UDP ports. mosh takes the first free port from 60001 up, so if you run only a few sessions at once, a small range is enough:
# ufw
sudo ufw allow 60000:61000/udp
On a cloud server, open the same UDP ports in the security group as well. For a server behind your home router, Tailscale beats port forwarding: the phone and server share a private network, and no UDP port faces the internet.
Using both together
mosh only synchronises the current screen and keeps no scrollback of its own, so tmux provides the history. From a command-line client:
mosh user@server -- tmux new -A -s main
Now mosh handles network changes and screen locks, and when mosh itself ends (say the phone was off overnight), the work is still in tmux; just connect again.
Don't let mosh-server processes pile up
By default, mosh-server waits forever for a client that has gone away, so every time the phone "disconnects" it may leave one behind. Set a timeout in the server's ~/.bashrc or /etc/environment:
export MOSH_SERVER_NETWORK_TMOUT=3600 # exit after an hour without hearing from the client
The work lives in tmux, so mosh-server exiting doesn't affect it. Clear out existing leftovers with pkill -u "$USER" mosh-server (this also ends all of your current mosh connections).
Common errors
| Symptom | Cause and fix |
|---|---|
mosh-server needs a UTF-8 native locale to run | The server lacks a UTF-8 locale. Run sudo locale-gen en_US.UTF-8, or set LANG=C.UTF-8 for the connection |
It can't connect and says Please verify that UDP port 60001 is not firewalled and can reach the server. | UDP is blocked: check ufw, the cloud security group and your router. A proxy or VPN app running on the phone may not forward UDP |
mosh-server: command not found | A non-login shell can't find it, common on macOS. Pass the path: mosh --server=/opt/homebrew/bin/mosh-server user@host |
| macOS asks whether mosh-server may accept incoming connections | Allow it in the firewall settings, otherwise UDP can't get in |
| Swiping doesn't scroll back | mosh has no scrollback. Turn on set -g mouse on in tmux, or press Ctrl+B [ for copy mode and page through |
| Port forwarding, X11 or ssh-agent forwarding doesn't work | mosh doesn't support them; open a separate plain SSH connection when you need them |
mosh clients for Android and iPhone
mosh needs the client to implement the mosh protocol itself; an SSH-only app can't use it. As of October 2026:
| Client | Platforms | mosh | Notes |
|---|---|---|---|
| Blink Shell | iPhone, iPad | Yes | The iOS client listed on mosh.org; command-line style, suits people at home in a terminal |
| Termius | Android, iPhone, desktop | Yes | Graphical; mosh works on the free plan, syncing hosts across devices needs an account |
| Termux | Android | Yes | A Linux environment on the phone; pkg install mosh, then connect from the command line |
| JuiceSSH | Android | Yes | Still listed on mosh.org, but no longer on Google Play and not updated for years |
| Roamtty | Android, iPhone | Yes | Pairs mosh with tmux automatically; see the next section |
How Roamtty handles it
Roamtty does all of the above by default; just leave the session mode on tmux when adding a host:
- One-tap install: Install plugins in the host menu installs tmux and mosh with the server's package manager (apt, dnf, pacman and others on Linux, with sudo; Homebrew on macOS; psmux through winget on Windows), and finds a mosh-server installed by Homebrew, MacPorts or snap outside the default path.
- tmux inside mosh: each session is its own tmux session on the server, attached as mosh-server starts, with tmux mouse mode on so swiping scrolls tmux's history.
- Locales handled: it tries
en_US.UTF-8and thenC.UTF-8, so mosh works even if the server never generated a locale. - Automatic fallback: with no UDP reply within 8 seconds it switches to SSH and points out that UDP 60000–61000 may be blocked.
- No leftovers: in tmux mode, mosh-server exits after 15 minutes without hearing from the phone; if the phone slept longer, the app logs in again and reattaches tmux when you come back.
- Files still use SSH: while the terminal runs over mosh, SFTP browsing and uploads use SSH, which logs back in quietly in the background after a drop.
Roamtty will soon be open source; the iPhone app is on the App Store (not yet available in mainland China), and the Android app is coming soon to Google Play. See the documentation for details; to reach a home server from outside, see the Tailscale section and self-hosting a DERP relay.
FAQ
| Question | Answer |
|---|---|
| Can I use tmux without mosh? | Yes. You won't lose work; you'll just log in again after each network change or screen lock, which usually takes a few seconds. |
| What about mosh without tmux? | Short outages are fine, but once mosh-server exits (timeout, server reboot, phone off overnight), the programs in it end too, and there's no scrollback. Use both. |
| Is mosh secure? | Login still goes through SSH. After that, every UDP packet is encrypted and authenticated with AES-128 in OCB mode, using a key handed to the client over SSH. |
| Does mosh drop when my iPhone locks? | iOS suspends networking for background apps, so nothing arrives meanwhile, but mosh-server keeps waiting. When you unlock and return to the app, mosh resynchronises the screen without a new login. |
| Does mosh work over Tailscale? | Yes. mosh uses plain UDP; connect to the server's 100.x.y.z address and make sure the server's firewall allows those ports on the tailscale0 interface. |