How to keep an SSH session alive after disconnect with tmux

Run tmux new -A -s work on the server and do your work inside it. tmux keeps running on the server, so a dropped connection does not stop your programs. After you log in again, the same command puts you back in the session. To leave on purpose and keep it running, press Ctrl+b then d.

Updated . tmux 3.5a and nohup on Debian 13 in a throwaway container, disconnecting by killing the OpenSSH client on macOS.

Why programs stop when SSH drops

A program you start in an SSH session is tied to that session's terminal. When the connection ends, the terminal closes and the program normally gets a hangup signal and stops. tmux holds the terminal on the server instead, so the connection can come and go.

tmux has to run on the server, not on your laptop. Install it with sudo apt install tmux or sudo dnf install tmux.

The commands you need

TaskCommand
Attach to session work, or create ittmux new -A -s work
Detach and leave it runningCtrl+b, then d
List sessionstmux ls
Attach to an existing sessiontmux attach -t work
Start a session in the backgroundtmux new -d -s work
End one sessiontmux kill-session -t work
End all sessionstmux kill-server
Scroll backCtrl+b, then [, and q to leave

tmux new -A -s work is the one to remember, because it works whether or not the session already exists. You can also go straight there from your own machine:

ssh -t user@server tmux new -A -s work

The -t is required, because tmux needs a terminal.

What a real disconnect looks like

We started a loop inside tmux, then killed the SSH client with kill -9 to imitate a lost connection. From a new login:

$ tmux ls
work: 1 windows (created Tue Nov  3 18:22:28 2026)

(attached) is gone from the line, because no client is connected, and the loop was still writing to its file. When no session exists at all, tmux ls says no server running on /tmp/tmux-1000/default. That means your sessions ended, for example after a reboot.

tmux does not survive a reboot of the server. It also dies at logout if the system kills user processes, with KillUserProcesses=yes in logind.conf. Debian and Ubuntu ship with it off. If your tmux dies each time you log out, set it back to no.

Without tmux: nohup

For a single job with no need to return to its screen:

nohup ./job.sh > job.log 2>&1 &

In our test we started three jobs in one session and closed the connection. The nohup job finished and wrote its log. The plain background job with & and the foreground job both died.

If you already started a job with & and forgot nohup, this bash builtin protects it from the hangup signal:

disown -h %1

Its output still goes to the old terminal, so a job that writes a lot to the screen may fail after you leave. Both methods give you no way to interact with the job later.

With systemd: systemd-run

If you have sudo, you can run the job as a temporary service. It is then fully separate from your login, and systemd keeps its output:

sudo systemd-run --unit=longjob /usr/local/bin/job.sh
systemctl status longjob
journalctl -u longjob -f

It runs as root unless you add --uid=user, and it starts in / unless you set a folder with --working-directory=, so use full paths.

When the job ends successfully, the unit disappears: in our test systemctl status longjob then said Unit longjob.service could not be found, but journalctl -u longjob still had the output. A failed job stays in the list until you run sudo systemctl reset-failed longjob.

Which one to use

  • tmux for interactive work: editors, database shells, anything you want to come back to.
  • nohup for one job on a server where you cannot install anything.
  • systemd-run for a long job you want logged and separate from your login.

If the connection itself keeps dropping, see Broken pipe and keep-alive. tmux keeps your work safe, and keep-alive settings make the drops less frequent. On a network that changes often, Mosh with tmux on the server keeps both the connection and the work.

Questions

How do I reattach to tmux after SSH disconnects?

Log in again and run tmux attach -t work, or tmux new -A -s work, which also creates the session if it is gone. tmux ls lists the sessions that still exist.

Does tmux survive a server reboot?

No. tmux is an ordinary process on the server, so a reboot ends every session. For work that must start again after a reboot, use a systemd service.

Why does my tmux session disappear when I log out?

The system is probably set to kill user processes at logout, with KillUserProcesses=yes in /etc/systemd/logind.conf. Set it to no, then restart systemd-logind or the server.

How do I keep a command running after closing SSH without tmux?

Start it with nohup ./job.sh > job.log 2>&1 &. It keeps running after you log out and writes its output to job.log, but you cannot return to its screen.

Do I need tmux on my own computer?

No. tmux has to run on the server. Your own computer only needs an SSH client.

Sources

More in Sessions and tmux

From the blog

All guides