The Fragility of the SSH Connection
When you connect to a remote Linux server using SSH, your entire workflow is tied to the physical stability of your network connection. If you start a database migration that takes three hours to complete, and your laptop’s Wi-Fi drops for a single second, the SSH connection instantly severs.
When the SSH connection drops, the Linux server immediately terminates the terminal session, instantly killing your three-hour database migration and potentially corrupting the entire database. You cannot reconnect and “resume” the session, because the session no longer exists.
To completely eliminate this risk, system administrators use a tool called a “terminal multiplexer.” The most modern and powerful multiplexer available on Linux today is the tmux command. It creates indestructible, background terminal sessions that survive completely independently of your SSH connection.
Step 1: Installing and Starting tmux
While some modern distributions include it by default, you may need to install it from your package manager.
- On Ubuntu/Debian:
sudo apt-get install tmux - On CentOS/RHEL:
sudo dnf install tmux
Once installed, starting a secure session is incredibly simple. Do not just type your long-running script. First, type the word tmux and press Enter.
Your screen will clear, and a green status bar will appear at the very bottom of the terminal. You are now safely inside a tmux session.
Step 2: Detaching from a Session
Inside this green-bordered session, you can start your massive three-hour database script.
Now, imagine it is 5:00 PM, and you want to close your laptop and go home, but the script is only 50% finished. If you just close the SSH window, the script dies.
Instead, you must manually “detach” from the session. This tells the server: “Keep this terminal running in the background, but disconnect my screen from it.”
To detach, you use the tmux keyboard shortcut prefix (which is Ctrl + B). Press Ctrl + B, release both keys, and then press the letter D (for Detach).
The green bar will disappear, and your terminal will say: [detached (from session 0)]. You are now safe to close your laptop, turn off your Wi-Fi, and go home. The script is still happily running on the server.
Step 3: Reattaching to a Session
When you get home, you open your laptop, connect to the VPN, and SSH back into the server. You are greeted with a completely blank, new terminal screen.
To magically reconnect to your background session and check the progress of the script, use the attach command:
tmux attach
Instantly, the green status bar returns, and your screen is filled with the output of the database script, exactly where it left off.
Step 4: Managing Multiple Windows
The tmux command is called a “multiplexer” because it allows you to run multiple different terminal windows inside a single SSH connection.
If your database script is running in Window 0, but you urgently need to edit an Nginx configuration file, you do not need to open a second SSH connection.
While inside tmux, press Ctrl + B, release, and then press the letter C (for Create).
Your screen will clear, giving you a fresh terminal. Look at the green bar at the bottom: it now shows 0:bash- 1:bash*. The asterisk indicates you are actively working in Window 1.
To instantly jump back to Window 0 to check on the database, press Ctrl + B, release, and press the number 0.
By mastering tmux, you ensure your remote work is never destroyed by a faulty internet connection, and you drastically improve your multitasking speed on the command line.