How do I keep a Linux terminal session after SSH disconnects?
tmux runs terminal sessions on the Linux server so you can detach and reconnect after a network interruption. It must be installed and allowed on that server. It does not make a task restart automatically after a reboot.
Before you start
- tmux is installed on the Linux server.
- The task is authorised to continue while you are disconnected.
Instructions
1. Check for an existing session
Run this after you have authenticated to Linux, not in local Windows.
tmux list-sessionsAn existing named session can be reattached. A message that no server is running simply means there are no current tmux sessions for this context.
2. Create a clearly named session
Choose a distinct session name and run the intended authorised task inside it.
tmux new-session -s maintenanceThis creates a remote session named maintenance. To detach, press Ctrl+B, release the keys, then press D.
3. Reconnect to the same session
After a later SSH login, attach to the existing session rather than starting a duplicate task.
tmux attach-session -t maintenanceThe task state appears where it was left, provided the session and server have remained alive.
Check the result
- tmux list-sessions shows the intended named session.
- After detaching and reconnecting, the original session is available.
Troubleshooting
- tmux is not installed
- Use the distribution’s approved package process or the application’s existing service mechanism.
- The session vanished after a reboot
- tmux is not a reboot persistence or recovery service. Check the task outcome before running it again.
Official references
Use the documentation for your installed version when a flag, service name or platform feature differs.
