How To Use ‘screen’: The Complete Linux Command Guide

There are many tools in Linux, some useful and others fundamental in our day-to-day life. Screen is one of those tools that facilitates our daily work and increases productivity.

screen is a terminal multiplexer, which allows the execution of multiple terminals on the same computer, with virtual terminals allowing to run different tasks without interrupting each other.

In this article, I will show you how to create, manage, and configure screen sessions that allow you to perform multiple tasks without interruption. I will also present various scenarios in which you can use them.

Want the best experience? Become a premium member for ad-free browsing, access exclusive content, and ask questions in our private forums. Your membership helps support the site!

If you need help with Linux, I’ve got something that can help you right away!
Download my free Linux commands cheat sheet – it’s a quick reference guide with all the essential commands you’ll need to get things done on your system. Click here to get it for free!

Command Syntax

The syntax used in screen is simple. Even without adding options or arguments, using the main command will work to run a virtual terminal session:
screen <option> <argument>

The option section refers to the options you can add to the command. The argument is the complement or a value required in some options (like the name).

Note: You can see more of these options in the ‘Main Options’ section of this article.

If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.

Usage Example

Let’s start with a few examples to make things clearer.

Creating and managing sessions

With screen, you will create, manage, configure, and delete as many sessions as you want. In each of these sections, you will learn how to do it and see unique examples for every case

Create a new session

You can create a screen session in different ways. The most direct and simple one is to use the screen command without any option:
screen

You can also create a session and give it a name with the -S option along with the name you wish to select:
screen -S session_name

Either way, the terminal will start again when you do any of these commands. It will look like it is starting from 0, with no previous commands or info.

Detach a session

To minimize a session and return to your main terminal again, you need to “detach” your session. To do this, you will need to use the following key combination:
(C-a) + d

Note: Keep in mind that when entering a screen session, you will be using key combinations for applying effects related to screen, and the main shortkey that will be used is CTRL + a (I will call it ‘C-a’ from now).

List all the active sessions

After you detach a screen session, you can see the list of sessions you have detached and attached with the following command:
screen -ls

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Note: While the term detach refers to sessions that are disconnected and “minimized”, the term attach refers to sessions that are being used by a terminal or process.

Attaching to an existing session

After detaching a session, if you want to use it again, you need to re-attach to that session. To do this, you can use the following command and you will need to specify the name as an argument in the command:
screen -r session_name

In case you come across a session that is already attached, you can detach it and then re-attach it using this command:
screen -d -r session_name

Switch between sessions

To switch between one screen session and another, there is no direct way to do it since screen does not have an integrated option, so you will have to apply step by step the procedures explained in the previous sections.

The ideal way to switch between sessions would be:

  1. Exit the current session by detaching or exiting directly (using exit or the kill key combination):
    (C-a) + d to detach.
    exit to exit the session without saving.
  2. Check the list of active sessions:
    screen -ls
  3. Re-attach the session you want. If it is already attached, detach and reconnect the session.
    screen -r re-attach the session.
    screen -d -r detach and re-attach the session.

Sharing a session with multiple users

There are cases where it is necessary to use a session with another person. In a normal session, it would not be possible since this session would be “attached” to your terminal. However, there is an option that allows several users to use a session.

First, you need to create a session as you would normally do:
screen -S session_name

Then using the -x option:
screen -x session_name

This way, several people or terminals can connect simultaneously to the same session.

Displaying multiple sessions on different windows

Horizontal & Vertical Splitting

You can split the screen window vertically and horizontally as many times as you want and have different sessions with different processes. This is useful for multi-task different processes.

First, to split a screen horizontally, you will have to use the following key combination:
(C-a) + S

In case you want to split it vertically, you will have to use this key combination:
(C-a) + |

Note: After splitting, it is required to create a window, this can be done using the combination (C-a + c).

Switch between windows

After creating a window, you can switch windows using the following key combination:
(C-a) + n to choose the next window.
(C-a) + p to choose the previous window

Also, if you have a split screen, you can switch between the windows you want using:
(C-a) + tab to toggle between the split windows

Managing windows

Besides changing and splitting the windows, you can also organize and manage your windows efficiently. You can rename a window, close it, or rearrange it according to your preferences. For example, to rename a window you can use:
(C-a) + :title <name-to-choose>

You can also close a window using the following combination, which, when used, will prompt you for confirmation to close it:
(C-a) + k

Having numerous windows created, you can also display them in a list using the following command:
(C-a) + “

Main Options

Command-Line Options

There are different options you can add in the screen command to use additional functions. These are the main ones that you can consider when using it:

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
  • screen: Start a new screen session.
  • screen -ls: List all running screen sessions.
  • screen -r [session_name]: Reattach to a detached screen session.
  • screen -S [session_name]: Start a new screen session with a specific name.
  • screen -d -r [session_name]: Detach and reattach to a screen session, even if it’s already attached elsewhere.
  • screen -d [session_name]: Detach a screen session.
  • screen -X -S [session_name] <command>: Execute a command inside a session.
  • screen -wipe: Remove all dead screen sessions.
  • screen -ls [session_name]: List details of a specific screen session.
  • screen -ls -q: Quiet mode, list only session names.
  • screen -dmS [session_name] command: Start a detached screen session with a specific command.
  • screen -r -d [session_name]: Reattach to a session and detach any other attached clients.
  • screen -x: Attach to a not detached screen session (Multi display mode).
  • screen -h: Display help information.
  • screen -v: Display version information.

Screen Short-Keys

When inside a session, most of the commands or features related to screen must be manipulated by using key combinations.

These are the main combinations:

  • (C-a) + c: Create a new window within a screen session.
  • (C-a) + n: Switch to the next window within a screen session.
  • (C-a) + p: Switch to the previous window within a screen session.
  • (C-a) + “: List all windows for selection within a screen session.
  • (C-a) + A: Rename the current window.
  • (C-a) + k: Close the current window, prompting for confirmation.
  • (C-a) + K: Forcefully close the current window without confirmation.
  • (C-a) + d: Detach from the current screen session.
  • (C-a) + [: Enter copy mode to scroll and copy text.
  • (C-a) + ]: Paste copied text into the current window.
  • (C-a) + S: Split the current window horizontally.
  • (C-a) + |: Split the current window vertically.
  • (C-a) + Tab: Switch focus between split regions.
  • (C-a) + X: Close the active split region.
  • (C-a) + :title: Change the window’s title.
  • (C-a) + :sessionname: Change the name of your current session.
  • (C-a) + :resize: Resize the current region.
  • (C-a) + :layout: Apply predefined layouts to split regions.
  • (C-a) + :suspend: Suspend screen (equivalent to Ctrl+z).
  • (C-a) + :help: Display help information within screen.
  • (C-a) + :quit: Quit screen, terminating all windows.

You can find more information about the command-line options and the combination keys inside using main screen in your Linux system or using this link.

Tips

Don’t forget to rename your sessions

If you plan to have different sessions, I advise you give each a reasonable name. After that, it’s easier to access and identify it each time you use it. You can create it with the -S option.
screen -S name

In case you have already created a session and you decide to rename it, you can do it using the following command:
screen -S <current-session-name> -X sessionname <new-name>

You can also do it even inside of a session using this key combination:
(C-a) + :sessionname <new_name>

Where <new_name> is the new name you want for your session.

Practical Uses of screen in Real-Life

Screen is a versatile tool that you can use in different real-life situations. Some of these situations can be:

  • Running a process in the background: It is quite useful to run processes continuously. You can use a session to start a process and then detach it so you can continue using your main terminal without affecting the session. It will work even if you disconnect from your terminal.
  • Multi-Tasking: By having different windows, you can open different processes, and this is especially useful in development environments where you need to run multiple applications, write scripts, or configure your server at the same time.
  • Monitoring: You can use screen as a monitoring tool using different windows with different apps running to monitor a service status, the system data (using tools like nmon for example), log tracking in real-time, and so on. You can do this using the window-splitting feature.

Advanced ‘screen’ configuration

Beyond the commands and the key combination, we can also use the screen configuration which is in a file in /etc/screenrc in which we can change from the main key combination (C-a), the default shell, and even customize the bottom bar.

# Example .screenrc configuration
# Set a custom status bar at the bottom with detailed information:
# - %H: Hostname of the system.
# - (%n): Window number.
# - %w: Window name.
# - %l:%M %d-%b-%Y: Current time and date.
hardstatus alwayslastline "GNU Screen: %H (%n) - %w - %l:%M %d-%b-%Y"

# Remap the escape key to Ctrl-z
escape ^Zz

# Set the default shell to zsh
shell /bin/zsh

There are many more settings that you can modify, and you can see more about these settings in the manual screen.

Note: You should be cautious when modifying the settings, as changing the values without any knowledge can cause damage when using screen.

Alternative

tmux

Tmux is a modern alternative to screen that you can try once you are familiar with screen.
Among the features it has, these are the most important:

  • It is still under active development so you will find more updates compared to screen.
  • It has greater support for shared connections for a session, offering greater stability to multiple users connected to a single session.
  • It offers greater compatibility and support of scripts, allowing you to automate tasks and processes.
  • Tmux is currently with an active community, so it will offer more support and tutorials for its configuration, unlike screen.
Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Related Questions

How do I exit a screen window safely?

To safely exit a screen session, you can do it in two different ways:

  • You can use the exit command. When you use it, your current session will end and you will return to your main terminal.
  • You can also use the key combination (C-a) + k to kill the current session and return to the main terminal.

Both options are correct, but note that using them will not save the session and it will not show up in the session list using the -ls option.

When should I choose screen over tmux?

Both tools perform similar functions, but depending on the case and the situation, screen could be better than tmux.

Normally, when you need a simple solution and want the easiest one, it is best to use screen. Otherwise, when you need specific solutions and advanced configuration, you should consider using tmux, as it allows more customization.

How do I delete all the screen sessions?

To delete all the sessions that you have detached or attached, you can use the following command:
sudo pkill screen

Using that command, you will indiscriminately delete all the screen sessions listed in the -ls command.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.

Additional Resources

Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.

VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

How would you rate this article?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

As you found this post useful...

Spread the word!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Similar Posts