ShellSage
ShellSage mobile terminal productivity workflow
SSH BASICS

How to Keep Your SSH Connection Alive on Mobile

Learn strategies to maintain SSH connections and how ShellSage simplifies it on your phone.


To keep an SSH connection alive, configure ServerAliveInterval and ServerAliveCountMax on the client-side, or ClientAliveInterval and ClientAliveCountMax on the server-side, and use tmux or screen for long-running sessions.

Understanding SSH Connection Timeouts

SSH connections can drop if they remain idle for too long. This is often due to network configurations or server settings that close idle sessions to conserve resources. For developers and sysadmins, an unexpected drop can disrupt workflows, especially during long-running tasks.

The SSH protocol allows for configurations that help maintain these connections. On the client side, you can use the ServerAliveInterval and ServerAliveCountMax options in your ~/.ssh/config file. ServerAliveInterval specifies the time in seconds between sending a message to the server to keep the connection alive, while ServerAliveCountMax determines the number of times the client sends the keep-alive message without getting a response before giving up.

On the server side, similar settings exist: ClientAliveInterval and ClientAliveCountMax in the sshd_config file. These settings control the server's behavior in terms of keeping the connection open.

Configuring keep-alive settings prevents unexpected SSH session drops.

How Can You Configure SSH Keep-Alive Settings?

To configure SSH keep-alive settings on the client side, edit your ~/.ssh/config with the following:

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 3

This configuration sends a keep-alive message every 60 seconds and allows up to 3 missed responses before the connection is closed. You may adjust these values based on your network reliability and needs.

For server-side configuration, you'll need to edit /etc/ssh/sshd_config with:

ClientAliveInterval 60
ClientAliveCountMax 3

After making changes to sshd_config, restart the SSH service to apply them:

sudo systemctl restart sshd

These settings ensure your SSH connection remains active even during periods of inactivity, reducing the chance of unexpected disconnects.

Using Terminal Multiplexers for Long-Running Tasks

Even with keep-alive settings configured, network issues can still cause disconnections. To safeguard long-running tasks, use tools like tmux or screen. These terminal multiplexers allow you to detach and reattach to sessions, preserving the state of your terminal.

After logging into your server, start a tmux session by simply typing:

tmux

You can detach from the session with Ctrl-b followed by d, and later reattach with:

tmux attach

This way, even if your SSH connection drops, the processes inside the tmux session continue running, and you can reattach to pick up right where you left off.

Terminal multiplexers like tmux keep your tasks running despite network interruptions.

How Does ShellSage Keep Your SSH Connection Alive?

ShellSage enhances the SSH experience on mobile by managing background connections efficiently. On Android, a connected session remains alive even if you leave the app or lock your phone, thanks to a foreground service that displays a "ShellSage session active" notification. This ensures that long-running jobs keep going in the background and only disconnect after about 10 minutes of idle time at a bare prompt. You will receive a 2-minute warning to stay connected before this happens.

On iOS, the operating system restricts long-running background networking, causing the session to drop when the app is backgrounded. However, ShellSage automatically reconnects when you reopen it. For uninterrupted tasks, you should run tmux or screen on the server, allowing the session to resume upon reconnecting.

ShellSage also allows you to save preset commands, like a keep-alive or tmux attach, in its Quick Presets, enabling you to re-establish a long-running session instantly.

How Does ShellSage Compare to Other Mobile SSH Clients?

While apps like Termius and JuiceSSH provide basic SSH connectivity, they often require manual configuration to maintain connections. ShellSage simplifies this with its AI assistant, Sage, which can explain output and fix commands, and a blast-radius preview to prevent dangerous commands from executing without review.

Unlike other clients, ShellSage's ability to keep sessions alive on Android and its automatic reconnection feature on iOS offer enhanced reliability for mobile SSH users. The integration of voice input and real SFTP further distinguishes ShellSage as a comprehensive mobile SSH client.

Most mobile SSH clients don't offer the same level of automation and convenience, making ShellSage an ideal choice for those who need a robust SSH solution on their phone.

⚡ Key takeaways

Devon Hale
Your Questions, Answered

Frequently Asked Questions

Prevent SSH session timeouts by configuring keep-alive settings such as ServerAliveInterval and ServerAliveCountMax on the client side, and ClientAliveInterval and ClientAliveCountMax on the server side.
The best way to keep SSH connections alive is by setting appropriate keep-alive intervals and using terminal multiplexers like tmux or screen to manage long sessions.
Start a tmux session by typing 'tmux' after logging into your server. You can detach using Ctrl-b followed by d, and reattach with 'tmux attach'.
ShellSage uses a foreground service to keep sessions alive on Android, even when you leave the app or lock your phone, ensuring long-running jobs continue.
On iOS, ShellSage reconnects sessions when you reopen the app. For uninterrupted tasks, use tmux or screen on the server to maintain session state.
Yes, ShellSage automatically reconnects to SSH sessions when you reopen the app on iOS, and keeps sessions alive in the background on Android.
Yes, ShellSage supports long-running SSH tasks by maintaining active sessions on Android and reconnecting on iOS, with support for tmux and screen.
ShellSage offers AI assistance, blast-radius previews, voice input, and real SFTP, along with unique session management features that other mobile SSH clients like Termius and JuiceSSH may lack.
Download ShellSage free
ShellSage is an AI-first SSH terminal with Sage built in: it explains output, fixes broken commands, and previews the blast radius before anything runs. Start on the free Hobby plan with no credit card, and get a 7-day full-Pro trial when you want everything.
← More tutorials