The screen Command in Linux

Fumbani Banda Feb 02, 2024
  1. What Is screen Command in Linux
  2. Use screen Command to Start Screen in Linux
  3. Use screen Command to List All Screen Sessions in Linux
  4. Use screen Command to Attach Screen Session in Linux
  5. Use screen Command to Start Screen Logging in Linux
The screen Command in Linux

This tutorial explains using the screen command in Linux to create, attach, detach, log, and exit screen sessions.

What Is screen Command in Linux

The screen command is a bash program used to manage the Linux window. Using the screen command, you can initiate multiple screen sessions. The screen session can also have multiple windows.

The screen command helps separate processes running on the terminal by putting them in different sessions. The screen command allows you to switch between sessions and choose which session to attach.

Use screen Command to Start Screen in Linux

To start the screen program in Linux. Type screen as shown below and press Enter.

screen img1

The above command displays the following output. Press Space, and you will be directed to a terminal.

screen img2

Once on the terminal, you can run commands as if you are in the normal terminal environment.

We are now inside a terminal session which we started using the screen command. To detach from the session, press Ctrl + A and D.

You will see similar output to the one below.

screen img3

Use screen Command to List All Screen Sessions in Linux

To list all the available screens on our machine, we use the screen command with the -ls or -list option.

The image below shows the available screens on our machine.

screen img4

Use screen Command to Attach Screen Session in Linux

We use the screen command with the -r option that takes the session ID as an argument to go back to the screen session after detaching it.

We reattach the session with ID 2276 in the image below.

screen img5

Use screen Command to Start Screen Logging in Linux

To enable logging when we start a screen session, we use the screen command with the -L option. It will create a log file for the screen session in the current screenlog.0 folder.

We start a new screen session with logging enabled in the image below.

screen img6

Once inside the screen session, we type the following commands, and then we detach the session by typing Ctrl + A and D.

screen img7

Now that we are outside of the session, we use the ls command to list the contents of the current directory and pass the output to the grep command. The grep command looks for the screenlog match and displays the line that fits the match.

The image below shows that we have the screen log file named screenlog.0.

screen img8

To leave the screen terminal session, you can press Ctrl + A and D to detach the screen. You can also type the exit command on the terminal, and it detaches the screen.

Using Ctrl + A and K kills the screen.

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Linux Command