The SSH Disconnection Nightmare
If you manage Linux servers remotely, you almost certainly connect to them using SSH (Secure Shell). This is the standard method for executing commands on remote machines. However, a standard SSH session is intrinsically tied to your local terminal window and your network connection.
If you start a massive database compilation, a 500GB file transfer, or a complex system upgrade that takes three hours to complete, you must leave your terminal window open and ensure your internet connection does not waver. If your Wi-Fi briefly drops out, or if your laptop goes to sleep, the SSH connection breaks. When the SSH connection breaks, Linux sends a SIGHUP (hangup) signal to the session, instantly killing whatever long-running process you had started. Hours of work can be lost in a millisecond.
To prevent this, sysadmins use terminal multiplexers. The most ubiquitous and beginner-friendly of these is the screen command. screen allows you to create virtual terminal sessions that continue running in the background on the server, completely immune to SSH disconnections.
Starting and Detaching a Screen Session
Using screen is incredibly simple.
- Connect to your remote Linux server via SSH.
- Simply type
screenand press Enter.
You may see a copyright message (press Space to clear it). At first glance, nothing seems to have changed. You are presented with a standard command prompt. However, you are now operating inside a virtual screen session.
Running Your Process
Now, start your long-running task. For example, let us simulate a script that takes a long time to finish:
ping google.com
The ping command will run continuously, printing output to the screen.
Detaching from the Session
Here is where the magic happens. We want to leave this ping command running on the server, but we want to close our SSH connection and go home.
To “detach” from the screen session (leaving it running in the background), you must use a specific keyboard shortcut:
- Press and hold the Ctrl key.
- Press A.
- Release both keys.
- Press D (for Detach).
You will instantly be returned to your original, normal SSH prompt, and you will see a message confirming the detachment: [detached from 12345.pts-0.server].
You can now safely type exit to close your SSH connection, close your laptop, and disconnect from the internet. The ping command (or your critical database backup) is still running happily on the server.
Reattaching to a Screen Session
When you get home and want to check on the progress of your task, simply reconnect to the server via SSH.
To list all screen sessions currently running in the background, type:
screen -ls
You will see a list of session IDs and their status (e.g., Detached).
To reattach (reconnect) to a session, use the -r flag:
screen -r
If you only have one session running, this command will instantly bring you back to the exact terminal view you left earlier. You will see your ping command still scrolling down the screen.
Handling Multiple Sessions
If you have started several different background tasks, screen -ls will show multiple IDs. To reattach to a specific one, you must append its ID to the command:
screen -r 12345
Naming Your Screen Sessions
If you run multiple background tasks simultaneously, identifying them by their random numerical IDs (like 12345) becomes confusing. It is highly recommended to name your sessions when you create them using the -S flag.
screen -S database_backup
Now, when you run screen -ls, it will clearly list the session as database_backup, and you can reattach to it by name:
screen -r database_backup
Terminating a Screen Session
When your long-running task is finally finished and you no longer need the virtual session, you should close it to free up server resources.
- Reattach to the session using
screen -r. - Ensure your script has finished running (or stop it using Ctrl+C).
- Simply type
exitand press Enter.
The virtual screen will close, and you will be returned to your main SSH terminal with a message confirming: [screen is terminating].
Conclusion
The screen command is a mandatory tool for any Linux administrator. By habitually wrapping your long-running scripts and massive file transfers inside a named screen session, you completely eliminate the anxiety of dropped connections and gain the freedom to manage complex server operations asynchronously.