Linux Screen Tutorial With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-screen-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

Linux screen is a tool used to multiplex single ssh terminal easily. Screen has a strong competitor named tmux where we will look next posts. 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.

Ubuntu,Debian,Mint:

Install
Install

Fedora,RedHat,CentOS:

Man Screen

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

Man Screen
Man Screen

Starting Linux Screen

Screen can be started with simple screen command like below. This will create 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.

Press Space to start shell

This is a screen shell which is the same as 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 result that current shell  is screen and color support is 256 color with xterm .

Control Command

Screen provides a lot of features. to activate these fatures 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 screen. Default control command is Ctrl+a 

Create Window

One of the most used feature of screen is creating new windows. As we stated before screen can creates more than one windows with single terminal which is very useful for system administrator. Shortcut for creating 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 Windows

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 Previous Windows

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

Detaching From Screen

Other very popular usage of screen is detaching from screen. Think about these scenario. 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 Sessions

To reattach screen sessions we may need to list current existing sessions. Screen provides -list option to list these detached session 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. 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 will reattach 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 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 multliple windows we can run some command and switch to the next windows. How can we notified if previous windows command is completed? Screen has bility 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 monitorig 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 Screen

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

Stopping Screen
Stopping Screen

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

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-screen-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

Leave a Reply

Your email address will not be published.