When you’re working on a Linux server and need to run long-running processes, a sudden disconnection can interrupt everything. This is where screen comes to the rescue. It allows you to keep commands alive even if your connection drops.

In this cheatsheet, we’ll cover:

  1. How to install screen.
  2. Why you need it.
  3. Key commands to use screen effectively.

1. Installing Screen

Most Linux distributions include screen by default. If not, you can install it quickly:

  • On Debian/Ubuntu:

    sudo apt update  
    sudo apt install screen
    
  • On CentOS/RHEL:

    sudo yum install screen
    
  • On Arch Linux:

    sudo pacman -S screen
    

Verify it’s installed by running:

screen --version

2. Why You Need to Know Screen

When you’re managing a Linux server, some tasks take time: compiling code, transferring large files, or running backups. If your SSH connection drops, these processes terminate, which can be frustrating.

With screen, you can:

  • Detach from a session while keeping tasks running.
  • Reattach later and pick up where you left off.
  • Avoid losing progress due to disconnections.

Think of screen as a virtual terminal you can leave and return to whenever you need.


3. How to Use Screen

Here’s a quick reference guide for the most useful screen commands.


Start a Screen Session

To create a new screen session:

screen

You’ll see a new shell. Start any long-running process here.


Name Your Screen Session

Naming sessions helps you identify them later:

screen -S session_name

Example:

screen -S backup_job

Detach From a Screen Session

To detach from the session (and keep it running), press:

Ctrl + A, then D

This sends the session to the background.


List All Active Screen Sessions

To see your active sessions:

screen -ls

Example output:

There are screens on:  
    12345.backup_job   (Detached)  

Reattach to a Screen Session

To reattach to a specific session:

screen -r session_name

Or, if you only have one session:

screen -r

Kill a Screen Session

To exit a screen session completely (if your task is done):

exit

Or, to force kill a session:

screen -S session_name -X quit

Shortcut Summary

Action Command
Start a session screen
Start with a name screen -S session_name
Detach Ctrl + A, then D
List sessions screen -ls
Reattach screen -r session_name
Exit exit
Kill a session screen -S name -X quit

4. Practical Example

Imagine you’re copying files and need to keep the task running:

  1. Start a session:

    screen -S file_copy
    
  2. Run your command:

    rsync -av /source/ /destination/
    
  3. Detach from the session:
    Press Ctrl + A, then D.

  4. Check your sessions later:

    screen -ls
    
  5. Reattach to monitor progress:

    screen -r file_copy
    

Conclusion

The screen command is a must-know tool for any Linux user who works on servers or remote systems. It ensures your processes keep running even if your connection drops. Save this cheatsheet, and you’ll never lose your progress again!