How To Use Linux Screen

Updated on

4 min read

How To Use Linux Screen

Have you ever started a long task on a remote machine, only to lose your connection and see your SSH session end, taking all your progress with it? It’s a common frustration for many of us. Fortunately, the GNU Screen utility lets you resume sessions seamlessly.

Introduction

Screen, also known as GNU Screen, is a terminal multiplexer. In other words, it means that you can start a screen session and then open any number of windows (virtual terminals) inside that session. Processes in the Screen continue running even if their window isn’t visible or if you disconnect.

Install Linux GNU Screen

The screen package is pre-installed on most Linux distros nowadays. Check if it’s available by running:

screen --version
Screen version 4.09.01 (GNU) 20-Aug-23

If the screen is not already installed on your system,you can install it using the package manager of your distribution.

Install Linux Screen on Ubuntu and Debian

sudo apt updatesudo apt install screen

Install Linux Screen on CentOS and Fedora

sudo yum install screen

Starting Linux Screen

To launch a new session, simply type screen in your console:

screen

This command opens a screen session, creates a new window, and starts a shell in it.

Now that you have opened a screen session, you can get a list of commands by typing:

Ctrl+a ?

Starting Named Session

Named sessions are useful when you run multiple screen sessions. To create a named session, run:

screen -S session_name

It’s always a good idea to choose a descriptive session name, such as the purpose or task you will use in that session. This makes it easier to identify and manage multiple sessions later.

Working with Linux Screen Windows

When you start a new screen session, it creates a single window with a shell in it.

You can have multiple windows inside a Screen session.

To create a new window with shell type Ctrl+a c, the first available number from the range 0...9 will be assigned to it.

Below are some of the most common commands for managing Linux Screen wWindows:

  • Ctrl+a c Create a new window (with shell).
  • Ctrl+a " List all windows.
  • Ctrl+a 0 Switch to window 0 (by number).
  • Ctrl+a A Rename the current window.
  • Ctrl+a S Split the current region horizontally into two regions.
  • Ctrl+a | Split the 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 windows
  • Ctrl+a Q Close all regions but the current one.
  • Ctrl+a X Close the current region.

Detach from Linux Screen Session

You can detach from the screen session at any time by typing:

Ctrl+a d

The program running in the screen session will continue to run after you detach from the session.

Reattach to a Linux Screen

To resume your screen session, use the following command:

screen -r

If you have multiple screen sessions running on your machine, you will need to append the screen session ID after the r switch.

To find the session ID, list the current running screen sessions with:

screen -ls
There are screens on:
    10835.pts-0.linuxize-desktop   (Detached)
    10366.pts-0.linuxize-desktop   (Detached)
2 Sockets in /run/screens/S-linuxize.

If you want to restore screen 10835.pts-0, then type the following command:

screen -r 10835

Customize Linux Screen

When screen is started, it reads its configuration parameters from /etc/screenrc and ~/.screenrc if the file is present. We can modify the default Screen settings according to our preferences using the .screenrc file.

Here is a sample ~/.screenrc configuration with a customized status line and a few additional options:

~/.screenrc
# Turn off the welcome message
startup_message off

# Disable visual bell
vbell off

# Set scrollback buffer to 10000
defscrollback 10000

# Customize the status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
Gnu Screen Terminal

Basic Linux Screen Usage

Below are the most basic steps for getting started with screen:

  1. On the command prompt, type screen.
  2. Run the desired program.
  3. Use the key sequence Ctrl-a + Ctrl-d to detach from the screen session.
  4. Reattach to the screen session by typing screen -r.

Conclusion

You’ve now learned the basics of GNU Screen. Use it to create multiple windows in a single session, switch between them, detach and reattach, and customize via the .screenrc file.

To further enhance your knowledge, explore the GNU Screen User’s Manual page.

Questions or feedback? Leave a comment.