Linux screen Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the powerful screen command in Linux. The screen command allows you to create and manage multiple terminal sessions, which is particularly useful for long-running processes, remote sessions, or when you need to switch between different tasks without losing your work. You will start by installing the screen package, then create and navigate between multiple screen sessions. Finally, you will learn how to detach and reattach to screen sessions, ensuring your work is preserved even if you need to disconnect from the terminal.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/PackagesandSoftwaresGroup -.-> linux/software("`Linux Software`") subgraph Lab Skills linux/software -.-> lab-422902{{"`Linux screen Command with Practical Examples`"}} end

Introduction to the screen Command

In this step, you will learn about the screen command, a powerful tool that allows you to create and manage multiple terminal sessions. The screen command is particularly useful for long-running processes, remote sessions, or when you need to switch between different tasks without losing your work.

First, let's install the screen package on our Ubuntu 22.04 Docker container:

sudo apt-get update
sudo apt-get install -y screen

Now, let's create a new screen session:

screen

This will start a new screen session, and you will see a message indicating that you are now in a screen session.

To list all the currently running screen sessions, use the following command:

screen -ls

Example output:

There is a screen on:
	12345.pts-0.labex	(Detached)
1 Socket in /run/screen/S-labex.

To detach from the current screen session, press Ctrl+A followed by d. This will leave the session running in the background.

To reattach to the screen session, use the following command:

screen -r

This will reconnect you to the running screen session.

In this step, you will learn how to create and navigate multiple screen sessions, which is a powerful feature of the screen command.

First, let's create a new screen session with a custom name:

screen -S mysession

This will create a new screen session named "mysession".

Now, let's create another screen session:

screen -S othersession

You now have two screen sessions running.

To list all the current screen sessions, use the following command:

screen -ls

Example output:

There are screens on:
	12345.mysession	(Detached)
	67890.othersession	(Detached)
2 Sockets in /run/screen/S-labex.

To switch between the screen sessions, use the following commands:

## Switch to the "mysession" screen session
screen -r mysession

## Switch to the "othersession" screen session
screen -r othersession

When you are inside a screen session, you can use the following keyboard shortcuts to navigate:

  • Ctrl+A c: Create a new screen window
  • Ctrl+A n: Switch to the next screen window
  • Ctrl+A p: Switch to the previous screen window
  • Ctrl+A ": List all the screen windows

Try creating and switching between multiple screen sessions to get a feel for how it works.

Detaching and Reattaching Screen Sessions

In this step, you will learn how to detach from a screen session and then reattach to it later.

First, let's create a new screen session:

screen -S myapp

Now, let's start a long-running process inside the screen session, such as running a web server:

python3 -m http.server 8000

To detach from the screen session, press Ctrl+A followed by d. This will leave the process running in the background.

You can verify that the screen session is still running using the screen -ls command:

screen -ls

Example output:

There is a screen on:
	12345.myapp	(Detached)
1 Socket in /run/screen/S-labex.

To reattach to the screen session, use the following command:

screen -r myapp

This will reconnect you to the "myapp" screen session, and you should see the web server still running.

To exit the screen session, press Ctrl+C to stop the web server, then press Ctrl+D to exit the screen session.

Summary

In this lab, you learned about the powerful screen command, which allows you to create and manage multiple terminal sessions. You installed the screen package and created a new screen session, detached from it, and then reattached to the running session. Additionally, you learned how to create and navigate between multiple screen sessions, which is particularly useful for long-running processes or when you need to switch between different tasks without losing your work.

You also discovered how to list all the currently running screen sessions and switch between them using the screen -r command. These skills will help you become more efficient and productive when working in a terminal environment.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like