How To Use Linux Screen

When working on a remote machine/server VIA SSH over long periods you will have experienced your internet connection dropping and your SSH session breaking. This can be very annoying as the terminal will freeze for a while before giving you a broken pipe message, wiping out whatever work you were in the middle of.

 

Instead we can use Linux Screen to preserve sessions so we can carry on working where we left off.

 

Installation

You probably already have Screen installed if you are using a popular Linux distribution or are using macOS. To test whether Screen is installed you can run the Screen version command.

 

screen --version

 

Screen version

 

If Screen is not found you can install on Ubuntu or Debian using

 

sudo apt install screen

 

or on CentOs

 

sudo yum install screen

 

Start a Screen session

 

To start a Screen session type "screen" into your console

 

screen

 

Then list all of the available commands by typing "ctrl+a" and then "?" (control + a) on a Mac.

 

Named Screen Session

 

If you want to have multiple screens you can manage them easier by passing in a name as you start a new session.

 

screen -S session1

 

Create a new window typing "ctrl+a" and then "a". Your original window is preserved.

 

Detaching and Reattaching Screen

 

After running any tasks you need you can detach this screen by pressing CTRL+A and then D

 

When you want to come back and carry on working in this screen you can just reattach it by typing:

 

screen -r

 

If you have multiple sessions running you will need to pass the session ID as the third parameter, you can do this by typing:

 

screen -ls

 

Common Screen Commands

 

Some Common Screen Commands

  • CTRL+A C Create a new screen
  • CTRL+A " List all screens
  • CTRL+A 0 Switch to screen number
  • CTRL+A A Rename the current session
  • CTRL+A S Split current region horizontally into two regions
  • CTRL+A | Split current region vertically into two regions
  • CTRL+A TAB Switch the input focus to the next region
  • CTRL+A CTRL+A Toggle between the current and previous region
  • CTRL+A Q Close all regions but the current one
command line screen terminal ubuntu