If you are managing a remote Linux server via SSH, dropping your connection (due to bad Wi-Fi or a closed laptop) will immediately kill any active scripts or downloads you were running. To solve this problem, system administrators use terminal multiplexers. tmux is the modern standard for keeping terminal sessions alive in the background, allowing you to disconnect and reconnect at any time without interrupting your work.
How to Install tmux
Most modern Linux distributions have tmux in their default repositories.
- Open your terminal or SSH into your server.
- Update your package list:
sudo apt update - Install tmux:
sudo apt install tmux
Using tmux to Keep Sessions Alive
Once installed, using tmux is incredibly simple.
- To start a new, persistent session, type:
tmuxand press Enter. Your terminal will clear, and you will see a green status bar at the bottom. You are now inside a protected session. - Start your long-running script, server update, or massive file download.
- To safely disconnect (detach) from the session while leaving the script running, press Ctrl + B, let go of both keys, and then press D (for detach).
- You will be returned to your normal terminal prompt. You can now safely close your SSH connection or turn off your computer.
Reconnecting to Your Session
When you want to check on the progress of your script, simply SSH back into your server and run the following command:
tmux attach
Your terminal will instantly restore the exact state you left it in, complete with your running script and command history.