tmux for Remote Servers: Keep Sessions Alive Across SSH Drops
Use tmux on remote servers to survive SSH drops: auto-attach, a minimal tmux.conf, logout pitfalls, and when systemd or a desktop workspace fits better.
To use tmux for remote servers, install it on the server, not your laptop. Start or reattach a named session with tmux new-session -A -s main each time you SSH in, and detach with Ctrl-b d. The session and everything running in it survive dropped connections and closed laptops. For long jobs that must outlive a reboot, use systemd. To bring back your multi-server layout, a saved DockLab workspace on top of tmux is often simpler.
Quick answer
- Install on the server:
sudo apt install tmux(Ubuntu 24.04 ships tmux 3.4). - Connect and attach in one step:
ssh -t web1 tmux new-session -A -s main. - Detach with
Ctrl-b d, list sessions withtmux ls, end one withtmux kill-session -t main. - tmux survives SSH drops, not reboots. Run long jobs that must survive reboots as systemd units.
- If
KillUserProcesses=yesis set in logind, tmux dies at logout unless you exempt it.
Why tmux runs on the server, not on your laptop
The most common mistake we see is running tmux locally and opening SSH connections inside its panes. That gives you a tidy layout, but when the Wi-Fi drops, every SSH connection drops with it, and so does every process those connections were running.
Persistence only works when the tmux server process lives on the remote machine. Your SSH connection is just a client attached to it. In the words of the tmux Getting Started guide, the server starts on the first tmux command and keeps running as long as any session exists. When your connection dies, the client goes away and the session carries on.
That gives you two separate concerns, and it helps to keep them apart:
- Process persistence: a migration, a
tail -f, an interactive REPL keeps running when you disconnect. Only something on the server can do this: tmux, screen, or systemd. - Layout persistence: which servers you had open, in which tabs and splits. That is a client-side problem, and a terminal app can handle it.
Most "tmux for remote servers" setups mix the two together. Keeping them apart makes the rest of this guide simpler.
The minimal setup that works
Install tmux on each server
sudo apt update && sudo apt install -y tmux tmux -VOn Ubuntu 24.04 LTS this installs tmux 3.4. Everything below works on 3.x.
Attach or create in a single command
ssh -t web1 tmux new-session -A -s mainThe tmux man page describes
-Aas makingnew-session"behave like attach-session if session-name already exists". You type the same command every time, whether or not the session exists yet. The-tflag makes ssh allocate a terminal, which tmux needs.Make it an SSH alias
Host web1 HostName web1.example.com User deploy Host web1-tmux HostName web1.example.com User deploy RequestTTY yes RemoteCommand tmux new-session -A -s mainWith this,
ssh web1-tmuxdrops you into your session, and plainssh web1(orscp,rsync,sftp) stays untouched.RemoteCommandandRequestTTYare standard OpenSSH client options. Keep the separate alias: ifRemoteCommandis set on the host you use for file transfers, those transfers break. More patterns are in our SSH config file examples.Detach on purpose
Press
Ctrl-b, thend. tmux prints[detached (from session main)]and your SSH session ends. Reconnect later with the same command.
Auto-attach from the server's shell instead
If you connect from several machines or clients that don't read ~/.ssh/config, put the logic on the server. Add this at the end of ~/.bashrc:
if [ -z "$TMUX" ] && [ -n "$SSH_TTY" ] && command -v tmux >/dev/null 2>&1; then
exec tmux new-session -A -s main
fi
$TMUX is set inside tmux, so this check prevents nesting. $SSH_TTY is only set for interactive SSH logins, so SFTP and scp are not affected. The stock Ubuntu .bashrc already returns early for non-interactive shells, which is why the snippet goes at the end.
The exec means that exiting tmux also closes the SSH connection. To get a plain shell for repair work, use ssh -t web1 bash --norc.
A tmux.conf for server work
tmux reads ~/.tmux.conf (or $XDG_CONFIG_HOME/tmux/tmux.conf) when the server starts, not when a session is created. Changes don't apply until you reload with tmux source-file ~/.tmux.conf or restart the server. A short config for remote work:
# ~/.tmux.conf
set -g history-limit 50000
set -g mouse on
set -g default-terminal "tmux-256color"
set -g set-clipboard on
set -g status-left "#[bold]#H #[default]| "
setw -g mode-keys vi
What each line is for:
history-limitsets how many lines of scrollback each pane keeps. Raise it if you read build output and logs. It applies to new panes only.mouse onlets you scroll and resize panes with the mouse. Hold Shift (or Option on some macOS terminals) to select text with your local terminal's selection.default-terminalhas to bescreen,tmuxor a derivative, per the man page. Runinfocmp tmux-256colorfirst. If that terminfo entry is missing, usescreen-256color.set-clipboard onlets copy mode send text to your local clipboard through the OSC 52 escape sequence, if your terminal supports it.#Hin the status bar shows the hostname. When you run sessions on five servers, this tells you which one you're typing into.
If you also run tmux locally, change the prefix on one side (for example set -g prefix C-a on the server). Otherwise Ctrl-b goes to the outer tmux and you'll have to press it twice.
Failure modes that catch people
tmux dies when you log out
Upstream systemd defaults KillUserProcesses= to yes, and the logind.conf documentation warns that this "will break tools like screen and tmux". Ubuntu builds with this set to no by default, so stock Ubuntu servers are fine. Hardened images and some other distributions turn it on. Check:
grep -rh KillUserProcesses /etc/systemd/logind.conf /etc/systemd/logind.conf.d/ 2>/dev/null
loginctl show-user "$USER" -p Linger
If it is on, either add your user to KillExcludeUsers=, or enable lingering and start tmux outside the login session scope, as the systemd-run docs show for screen:
loginctl enable-linger
systemd-run --scope --user tmux new-session -d -s main
Agent forwarding stops working after reattach
If you use ForwardAgent, the shells inside tmux remember the SSH_AUTH_SOCK path from the connection that created them. Once that connection ends, the socket is gone and git pull asks for a password or fails. tmux's update-environment option refreshes the session environment on attach, but shells that are already running keep the old value. The usual fix is a stable symlink. In ~/.ssh/rc on the server:
if [ -n "$SSH_AUTH_SOCK" ] && [ "$SSH_AUTH_SOCK" != "$HOME/.ssh/agent.sock" ]; then
ln -sf "$SSH_AUTH_SOCK" "$HOME/.ssh/agent.sock"
fi
Then add set-environment -g SSH_AUTH_SOCK $HOME/.ssh/agent.sock to ~/.tmux.conf. If you're forwarding the agent to reach other hosts, a jump host is usually the safer pattern. See ProxyJump vs agent forwarding.
The SSH connection freezes instead of dropping
A half-dead TCP connection can hang for minutes before ssh notices. ServerAliveInterval defaults to 0 (off). Setting it to 15 with the default ServerAliveCountMax of 3 makes ssh give up after about 45 seconds. Then you can reconnect and reattach.
Host *
ServerAliveInterval 15
Sessions are gone after a reboot
tmux is an ordinary process. A kernel update reboot ends it, along with everything inside. If a job has to survive that, it isn't a tmux job.
When tmux is the wrong tool
tmux is great for interactive work you want to come back to. It is a poor process supervisor: there's no restart on failure, no log retention beyond the scrollback, and no start on boot. Anything long-running or important should be a systemd unit, even a temporary one:
sudo systemd-run --unit=backfill-2026-10 --working-directory=/srv/app \
/srv/app/bin/backfill --since 2026-01-01
journalctl -u backfill-2026-10 -f
systemctl status backfill-2026-10
You get exit codes and journald logs you can read after the fact. If the unit fails, our systemd failed-to-start checklist applies. Containerized workloads belong in docker compose with a restart policy, not in a tmux pane.
Mosh fixes a different problem: it keeps the connection itself alive across network changes and high latency, over UDP (ports in the 60000 range must be open). It only syncs the visible screen, and the Mosh docs themselves suggest running tmux on the server to get scrollback. Mosh and tmux work well together.
| Need | Best fit | What it doesn't do |
|---|---|---|
| Reattach to an interactive shell after a drop | tmux on the server | Survive reboots |
| Typing on a flaky or roaming connection | Mosh plus tmux | Work without open UDP ports |
| Long job with logs and exit status | systemd-run or a unit file | Give you an interactive pane |
| Bring back "these 6 servers in these splits" | A client workspace | Keep remote processes running |
| Same command on several servers at once | tmux synchronize-panes, or a script | Protect you from typing into the wrong pane |
Where a desktop workspace is the simpler answer
Many people we talk to run tmux for layout rather than persistence: a local tmux with one pane per server, a status bar of hostnames, and a set of keybindings to move between them. If that's your setup, a desktop workspace does the same job with less configuration to maintain.
In DockLab, a saved workspace restores your tabs and up to four split panes across servers. Saved layouts can sync between your devices. Next to each terminal you get SFTP and a Monaco-based editor on the same host, so editing a config doesn't need a separate tool (we compared approaches in edit files on a remote server). Saving a layout does not keep processes alive, though. When you close the app, the remote shells end like any SSH connection.
What we actually do: the DockLab workspace handles which servers are open and where, and each tab runs tmux new-session -A -s main on its server (the .bashrc snippet above makes this automatic). If the laptop sleeps, we reopen the workspace and every pane reattaches to the session that kept running. Persistence stays on the server, and the client handles the layout.
To be fair to the alternatives: if you live in one terminal and like keyboard-only control, plain tmux in any terminal is lighter and costs nothing. iTerm2's native tmux integration on macOS is excellent. Warp has a more mature terminal-native AI agent than ours, and MobaXterm offers far more protocols if you're on Windows. Our comparison page lays out the differences. If you manage many hosts across a team, the Team plan on the pricing page lists unlimited hosts and encrypted sync. For the broader inventory question, see manage multiple servers at once.
Frequently Asked Questions
Should I run tmux on my laptop or on the remote server?
Run it on the remote server if you want processes to survive disconnects. A local tmux only arranges your SSH connections, and when the network drops, every one of them drops too. You can run both, but change the prefix key on one of them.
Does a tmux session survive a server reboot?
No. tmux is a regular user process, and a reboot ends it along with every shell and program inside. Plugins can save and restore window layouts, but they can't restore running processes, so put reboot-critical work in a systemd unit.
Why does my tmux session disappear when I log out?
Usually logind has KillUserProcesses=yes, which stops everything in your login session scope at logout. Ubuntu defaults it to no, but hardened images often turn it on. Exempt your user with KillExcludeUsers=, or enable lingering and start tmux with systemd-run --scope --user.
Is screen still a reasonable alternative to tmux?
Yes, for basic detach and reattach. screen is still packaged and works on most systems. tmux has a scriptable command set, clearer configuration and better pane handling, which is why we standardize on it. Both need the same logind care.
Can DockLab keep my remote sessions running without tmux?
No. A DockLab saved workspace restores tabs and split panes, but saving a layout does not keep processes alive on the server. Run tmux (or systemd for long jobs) on the server for persistence, and use the workspace to reopen your multi-server layout.
SSH, SFTP, a remote editor, a browser and AI in one desktop workspace.
DockLab runs on macOS, Windows and Linux. Start free and keep your whole server workflow in one window.