How to use screen command to improve your productivity on Linux terminal

Last updated on March 8, 2021 by Brian Nichols

Similar to many tools utilized by system administrators, the Linux screen command is another great tool that helps with productivity. screen can be seen as an alternative to Tmux, but it has many other useful options outside of just saving screen space. screen allows you to create multiple sessions of terminals/interactive shells. In this tutorial I will give you a quick rundown of screen and present several use cases of the command.

Major Benefits of screen Command

To highlight some of the main benefits of screen:

Install screen on Linux

You can install screen by utilizing your operating system package manager.

For Debian/Ubuntu:

$ sudo apt-get install screen

For RHEL/CentOS/Fedora:

$ sudo yum install screen

screen Session Basics

1. Start a screen Session

To start a new screen session, simply type:

$ screen

You will now see a new blank terminal opened up. To check whether or not you are inside a screen session, type this command:

$ echo $TERM

If you are inside a screen session, the output will be screen or something similar (e.g., screen.xterm-256color). Otherwise, it will print xterm.

2. Detach from a screen Session

Now let's detach from our screen session. Type CTRL-a d to detach. You will see some output showing that you detached from your session.

You are now back to your original terminal.

3. Check a List of Available screen Sessions

Next, let's check what screen sessions are running/available. The following command will display a list of currently available screen sessions and their respective status (i.e., attached or detached). Since there is only one session available, you will see your one screen session. You will also see that you are currently detached from that session.

$ screen -ls 

4. Attach to an Existing screen Session

Next, in order to connect back to your terminal in the screen session you created earlier, you will need to attach to that session again with:

$ screen -x

If there is more than one available screen sessions, you will need to specify the name of the session after -x (see the multi-session example below).

5. Terminate a screen Session

If you type exit inside a screen session, the session will be automatically terminated, and you won't be able to re-attached to it later.

6. Scroll Up/Down inside a screen Session

When you are inside a screen session, you will notice that you cannot scroll up and down your terminal session using the terminal program's scrollbar. That is because the output of your terminal session is controlled by the screen utility. To enable scrolling inside a screen session, you need to press "CTRL-a ESC" to enter the so-called "copy" mode. Once in that mode, you can use "Up"/"Down" keys or your mouse' scroll wheel to scroll up/down your screen session. Press "ESC" again to exit the "copy" mode.

That's the quick rundown of screen command. While some system administrators' screen knowledge stops here, this tool has many other options that again further help with productivity. In the rest of the tutorial I will show several example use cases of screen.

Handle Constant SSH Connection Drops with screen

If you are attached to your screen session over SSH, working away, and you lose your SSH connection, your screen session will still be running. The screen session will be in an attached state even though you lost a connection. You can see this after logging back in and running your screen -ls command:

To reattach to the screen session that is still in the "attached" state, run the following:

$ screen -d -x

You will then be able to resume the SSH terminal session where you left off.

Maintain Multiple screen Sessions

What if you want to have multiple screen sessions? For example, you have multiple coworkers working on the same server under the same user account? Multiple screen sessions can be helpful in this way. Further, you can also help dampen the confusion of multiple screen sessions by naming each session. To create a name for your screen session, use -S <session-name> option:

$ screen -S session1

Now you can run your screen -ls command again and see the multiple screen sessions. If you want to connect to a specific session, use -x <session-name> option:

$ screen -x session1 

Alternatively, you can specify the PID of the screen session you want to connect to:

$ screen -x 925247

Send Commands Through screen

Another great option within screen is the ability to send commands through screen if you have a running process that is waiting for input. For example, when starting a dedicated Minecraft server on your Linux server, you are not able to use your terminal. The prompt has been replaced with the server prompt that is waiting for input/commands. This is where screen comes in handy. You can create your screen session, run the Minecraft server, and detach to go back to your normal terminal prompt. Then, with screen, you can send commands through it without attaching to your session. To send commands through screen, run the following.

$ screen -S session1 -X stuff "save$(printf \\r)"

The above command utilizes -S for the session name, -X stuff is used as an input buffer to your indicated screen session, with the save as the command you are wanting ran. You also want to include the "$(printf \\r)" to input a carriage return to run the command.

Log Your screen Session

Another built-in option within screen is logging. One thing you will notice through your increased experience with screen is that it can be difficult to scroll up through your terminal history from within your screen session. You are able to scroll through your screen session, but it takes keystrokes and fiddling up and down the screen as describe above. Instead, an easier way to look through your terminal history is the logging option with screen. To utilize logging for your screen sessions, simply run the -L command line option with screen:

$ screen -L

When you're needing to check your terminal history, simply detach from your screen session and view your log. By default, the log for screen lands in the cwd (current working directory), which means where you first launched the screen session. If you are unsure where the cwd is for your screen session, you can check it by typing CTRL-a :exec pwd. This will output on screen the cwd - that is where you log will be located. Also by default, the log file name will be named screenlog.0.


This tutorial gave you a quick rundown of the basics on utilizing screen on Linux hosts. It has many other benefits other than saving screen space, as discussed by detaching and reattaching on other computers/devices, saving precious work from being lost due to network disconnects, and sending commands to long-running processes. Make sure to check out screen and the many other commands/options this tool can use.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for Us ‒ Feed ‒ Powered by DigitalOcean