Linux Screen Tutorial With Examples – POFTUT

Linux Screen Tutorial With Examples

Linux screen is a tool used to multiplex single ssh terminal easily. The screen has a strong competitor named tmux where we will look next posts. The screen has these abilities to make system administrators life easy.

  • Multiple screens in a single ssh connection
  • Running commands after ssh disconnect
  • Resuming screen sessions after ssh disconnect
  • Easily dividing and multiplexing single window

Install

Installing screen is easy like below.

We will use the following command in order to install screen in the deb based distributions like Ubuntu, Mint, Kali, and Debian.

Install

Install

We will use the following command in order to install screen in the rpm based distributions like Fedora, CentOS, Red Hat.

Man Screen

To get more detailed help about screen command use man screen command like below

Man Screen

Man Screen

Starting Linux Screen

The screen can be started with a simple screen command like below. This will create a new shell same as default shell.

After issuing screen command we will see a notice about shell we will start like below. This page provides summary information about screen shell where we look more details later this post.

Starting Linux Screen

Starting Linux Screen

Press Space to start a shell

Starting Linux Screen

Starting Linux Screen

This is a screen shell which is the same as the bash shell.

Check If Screen Whether Running

While working a lot of servers we may need to check if the screen is running. to get status of current shell bash environment variables provides infomraion about terminal with TERM variable. We can see current terminal from here.

Check If Screen Whether Running

Check If Screen Whether Running

We can see from the result that the current shell is screen and color support is 256 color with xterm .

Control Command

The screen provides a lot of features. to activate these features some keyboard short cuts used. We call these shortcuts as screen commands. To send command to the screen there is a command named control command. Control command simple specifies next command is about the screen. The default control command is Ctrl+a 

Create New Window

One of the most used features of the screen is creating new windows. As we stated before the screen can create more than one windows with a single terminal which is very useful for system administrator. Shortcut for creating a new window is

First press control command which is by default Ctrl+a and after press .This will create a new window with shell-like below.

Create Window

Create Window

Switch Next Window

In a typical screen usage there will be more than one windows so how can we switch between these windows. In order to switch next window use

Switch the Previous Window

Another navigation shortcut for the screen is switching the previous window. Switching between windows do not end the process or close the current window.

Detaching From Screen

Another very popular usage of the screen is detaching from the screen. Think about these scenarios. We are copying some big files but have go out by closing ssh session. In this situation, we can detach current screen session and then reattach.

List Existing Sessions

To reattach screen sessions we may need to list current existing sessions. The screen provides -list option to list these detached sessions with the date-time information and session name.

List Sessions

List Sessions

List Screen Process

We can look for existing screen sessions in a server. The screen runs as a process like a bash session.

List Screen Process

List Screen Process

As we see processes id 12336 is our current screen terminal

Reattach

We can terminate our connection to the remote server. Then we can reconnect the server and continue with the previously created screen sessions. This is called as reattach. We will reattach the screen session after connection our server with ssh.

Reattach

Reattach

As we see if there is only one screen session to reattach it will automatically be reattached but there are more than one screen session so we should specify which one we want to reattach

Save Terminal Commands

While working in remote systems we will issue a lot of commands. Remembering these commands can be hard some times especially if we try a lot of things to solve a problem. Screen provides logging commands of running sessions. After a session ends we can look commands issued at this session. To enable command history use following command.

After issuing shortcuts a logfile named screenlog.0 is created.

Read History Log

History log files by default named screenlog.id . They are simple text files and can be read with cat like below.

Read History Log

Read History Log

Disable Saving Terminals

After enabling terminal logging we disable it with the same command for enabling like below.

Disable Saving Terminals

Disable Saving Terminals

Enable Monitoring

While working multiple windows we can run some command and switch to the next windows. How can we notify if previous windows command is completed? The screen has ability to monitor windows and provides notifications if the commands have finished.

Enable Monitoring

Enable Monitoring

Disable Monitoring

Simply issuing same command t enable monitor will disable monitoring for the current window.

Disable Monitoring

Disable Monitoring

Lock Screen Session

Screen session have the ability to lock which is very similar like desktop managers screen lock. To unlock screen current users system password should be entered.

Lock Screen Session

Lock Screen Session

Stopping or Exit Screen

The best practice screen after usage is closing the screen session with the shortcut below. This will close all windows of the current session.

Stopping Screen

Stopping Screen

This will ask question “Really kill this window [y/n]” press to kill session

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)