Product10/16/2026·7 min read

SSH on iPhone and iPad Without Losing Your Session

iOS suspends backgrounded apps within seconds and a Wi-Fi to 5G switch breaks TCP. What keepalives, Mosh and tmux each fix when SSH on iPhone keeps dropping.

An SSH session on an iPhone or iPad usually dies for one of two reasons. iOS suspends the app shortly after you switch away from it, and a move from Wi-Fi to cellular strands the TCP connection on an address the phone no longer has. Keepalives only make either failure show up sooner. What keeps the work is a stack: Mosh for network changes, tmux on the server for everything else, and a login command that reattaches you automatically.

What happens to an SSH connection when the app goes to the background?

The app stops running. Apple's documentation on extending background execution time gives applicationDidEnterBackground(_:) five seconds to return, and "shortly after that method returns, the system puts your app into the suspended state." An app can ask for extra time with beginBackgroundTask, but that time is for finishing work such as saving state, and the system terminates an app that does not end those tasks promptly.

Suspension is worse for a terminal than it sounds. Apple's networking technote TN2277 explains that no code in a suspended app executes, so it cannot read incoming data, and that the system may reclaim the resources behind the app's sockets, "thereby closing the network connection." A suspended SSH client cannot answer a keepalive, cannot drain output, and may return to a socket that was closed underneath it.

The server sees silence. If its sshd_config sets ClientAliveInterval, it hangs up after the configured number of unanswered probes; if not, the dead session can linger until TCP gives up. Either way the remote shell is eventually hung up, and so is whatever was running in it.

Why does switching from Wi-Fi to 5G break SSH?

A TCP connection is defined by the addresses and ports at both ends. When the phone joins a cellular network it gets a new address, and every packet the server sends to the old one goes nowhere. A ten-second signal drop on the same Wi-Fi is different: the address survives, so TCP retransmits until the path comes back.

You do not have to walk anywhere for the address to change. iOS can move traffic to cellular by itself: Connectivity Assist, which replaced Wi-Fi Assist, "uses cellular data to improve your connection when Wi-Fi is slow or unusable," and it is on by default if Wi-Fi Assist was on when you updated. The switch lives under Settings > Wi-Fi.

A mesh VPN changes this one case. Tailscale says its device addresses "remain constant regardless of the device's physical location," so a connection to the server's tailnet address is not invalidated by the handover itself. It survives if the tunnel recovers before TCP or a keepalive gives up. It does nothing about suspension.

Which fix covers which failure?

No single setting covers the three ways a phone session ends. This table maps each approach to the failures it survives.

ApproachWi-Fi to cellular switchApp suspended in the backgroundApp killed or phone restartedWhat the server needs
SSH keepalivesDetected sooner, not survivedCannot run while suspendedNot survivedOptional ClientAliveInterval in sshd_config
MoshSurvived: the server follows the new addressSurvived: mosh-server waits for the clientSurvived only if the client kept its session statemosh-server installed, UDP 60000–61000 reachable
tmux or screenConnection lost, programs keep runningConnection lost, programs keep runningPrograms keep runningtmux installed
Mesh VPNAddress stays constant, TCP can ride it outNo effectNo effectVPN agent running on the server
Reattach at loginNew connection lands in the old sessionSameSametmux plus a login command

The pattern: Mosh protects the connection, tmux protects the programs, and keepalives protect neither. They shorten the time it takes to learn that a connection is gone.

What do SSH keepalives actually do?

They turn silence into a decision. OpenSSH's ServerAliveInterval makes the client send a request "through the encrypted channel" after that many seconds without data from the server, and after ServerAliveCountMax unanswered requests, 3 by default, the client disconnects. The manual's own example: an interval of 15 with the default count ends an unresponsive session after about 45 seconds. The interval defaults to 0, which means off.

The server-side mirror is ClientAliveInterval and ClientAliveCountMax, with the same arithmetic. Neither is TCPKeepAlive, which is on by default, works at the TCP layer and, as both manuals note, is spoofable where the SSH-level messages are not.

Two consequences matter on a phone:

  • Keepalives stop an idle session from looking idle to a NAT or firewall. RFC 5382 requires a NAT to hold an idle TCP mapping for at least 2 hours 4 minutes; keepalive traffic is what protects you from a device configured more aggressively than that.
  • They also make a short outage fatal. At 15 × 3, a 50-second dead zone ends a session that TCP alone might have survived. On a phone, a longer interval with a higher count, such as ServerAliveInterval 30 with ServerAliveCountMax 6 (about three minutes), catches real failures without ending the session in an elevator.

How does Mosh follow a phone across networks?

Mosh logs in over SSH, starts mosh-server on a UDP port between 60000 and 61000, and from then on synchronizes terminal state with its State Synchronization Protocol. Every datagram is authenticated, and when the server receives an authentic packet with a newer sequence number, that packet's source address "becomes the server's new target for its outgoing packets." That is the whole roaming mechanism; the client never needs to know its address changed.

Mosh also echoes keystrokes locally and underlines predictions it has not confirmed. In the USENIX ATC 2012 paper that introduced it, Mosh immediately displayed the effect of 70% of keystrokes across 40 hours of traces, and over a 3G EV-DO link its median keystroke response was under 5 ms, against 503 ms for SSH.

The detail that matters for suspension is on the server. The mosh-server manual says that unless MOSH_SERVER_NETWORK_TMOUT is set, "mosh-server will wait indefinitely for a client to reappear." A phone that sat suspended for an hour can return to the same shell, provided the client app saved the session's key and port.

The costs are real. Mosh must be installed on both ends, every firewall on the path must pass UDP, and Mosh synchronizes only the visible screen, so scrollback is limited to what fits on it. Its own FAQ recommends screen or tmux on the remote side for that.

Why tmux keeps the work when every connection fails

When an SSH connection dies, the login shell is hung up. By default bash then exits and "resends the SIGHUP to all jobs, running or stopped," which is why a build started in a bare shell dies with the connection. tmux breaks that chain: per its manual, a tmux session "may be detached from a screen and continue running in the background, then later reattached."

The habit that makes tmux practical on a phone is one command, tmux new-session -A -s main. The -A flag "makes new-session behave like attach-session if session-name already exists," so the same line creates the session the first time and reattaches every time after. Let the client send it for you:

Host build
    HostName build.example.com
    RemoteCommand tmux new-session -A -s main
    RequestTTY force
    ServerAliveInterval 30
    ServerAliveCountMax 6

RemoteCommand runs after login and RequestTTY force gives tmux the terminal it needs. For a single long command on a host without tmux, nohup runs it "immune to hangups" and writes its output to nohup.out.

What YoloShell automates, and where it stops

YoloShell stacks the same layers per host profile instead of leaving them to memory.

  • The reattach habit is built in. With tmux enabled and the attach-or-create policy, every connection to that profile sends tmux new-session -A -s <name>, so a fresh login lands in the old session without typing anything.
  • Mosh state is saved before suspension. When the app moves to the background, YoloShell uses a background task to save each Mosh tab's session state. When you return to the tab, it checks that the host's SSH port answers within 1.5 seconds, resumes the same Mosh session, and gives it up to 25 seconds to prove it is alive before starting a fresh session, which tmux then reattaches.
  • State is visible. Each tab's status dot separates connecting, active, suspended and failed, so a dead connection does not pass for an idle one.

The limits follow from the table. A plain SSH tab cannot outlive iOS reclaiming its socket. If the app is killed, YoloShell restores Mosh tabs, and SSH tabs whose profile uses tmux, by reconnecting and reattaching; an SSH tab without tmux is not restored, because the shell it held is gone. Roaming needs UDP and mosh-server on the host, so a network that allows only TCP 22 gets no roaming. The free tier keeps one concurrent session, three hosts and two SSH keys; more concurrent sessions come with the YoloShell Pro subscription. The per-host settings are in how YoloShell keeps a session alive when Wi-Fi drops.

If the session you are protecting runs an AI agent, the same stack applies, and running a coding agent from your phone covers what is specific to agents. Files travel on a separate SSH channel with failure modes of their own, covered in SFTP file management on iPhone.

Setting up a host you will reach from a phone

Work from the server outward, because the server is where state survives:

  1. Install tmux and log in with tmux new-session -A -s main, typed or as a RemoteCommand. This alone turns every disconnect into a reattach.
  2. If you control the firewall, install Mosh and open UDP 60000–61000. Leave MOSH_SERVER_NETWORK_TMOUT unset unless orphaned servers become a problem; a timeout is exactly what ends the "back after an hour" case.
  3. Set ClientAliveInterval on the server only if you want it to reap dead sessions, with a count generous enough to outlast a dead zone.
  4. Set client keepalives last, with a long interval and a high count, so they catch real failures without causing new ones.

The decision rule: if you can install only one thing on the server, install tmux. Add Mosh when your phone changes networks mid-session and the network passes UDP. Keepalives never substitute for either. YoloShell is on the App Store if you want to try the stack against a host you control.

Frequently asked questions

Why does my SSH session disconnect when I switch apps on iPhone?
Because iOS suspends the app. Shortly after an app moves to the background the system stops running its code, and Apple's networking technote TN2277 warns that it may also reclaim the app's sockets, closing the connection. A suspended client cannot answer keepalives either. Mosh, whose server waits for the client to return, and tmux, which keeps programs running on the server, are the two layers that survive it.
Should I set ServerAliveInterval or ClientAliveInterval?
They are mirror images. ServerAliveInterval in the client's ssh_config makes the client probe the server; ClientAliveInterval in sshd_config makes the server probe the client. Both are off by default, both travel inside the encrypted channel, and both disconnect after the CountMax number of unanswered probes, 3 by default. Use the server setting to clean up dead sessions and the client setting to notice a dead link sooner.
Do I still need tmux if I use Mosh?
Yes. Mosh keeps the connection alive across network changes, but the shell still belongs to mosh-server: if that process ends, or the client loses the state it needs to resume, the shell ends with it. Mosh also synchronizes only the visible screen, and its own FAQ recommends running screen or tmux on the remote side to get scrollback.
Why did my long-running command stop when the SSH connection dropped?
When the connection drops, the login shell receives SIGHUP. By default bash then exits and resends SIGHUP to every job, running or stopped, so a build or download started in that shell is killed with it. Run long jobs inside tmux or screen, or start a single command with nohup, which runs it immune to hangups and sends its output to nohup.out.

Products in this post

Keep reading