Screen Command in Linux with Examples

Useful Screen Command Examples to Manage Linux Terminals

Screen is one of the useful software which is used to perform multiple programs at the same time without getting interruption from any other process. It helps the user to open many individual terminals inside a single window. It also allows permission to share the sessions with the other users and we can also attach or detach a screen, which are created inside the single window terminal. The installation procedure of screen command is explained.

To install the screen

Run the following command to install the screen in the Linux systems.

For RedHat based system

#yum install screen

For Debian/Ubuntu packages

user1@linuxhelp:~$ sudo apt-get install screen
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  iselect screenie byobu ncurses-term
The following NEW packages will be installed:
  screen
0 upgraded, 1 newly installed, 0 to remove and 227 not upgraded.
Need to get 567 kB of archives.
.
.
.
Setting up screen (4.3.1-2) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (225-1ubuntu9) ...

To start using the screen in the linux use the following command.

user1@linuxhelp:~$ screen

When you run the screen command, the following screen appears.

To show the parameters of the screen hit ‘ ctrl+a and ?’ . Now all the commands and parameters which are used in the screen command will be displayed as shown below.

                       Command key:  ^A   Literal ^A:  a

  break       ^B b                 license     ,               removebuf   =         
  clear       C             lockscreen  ^X x             reset       Z         
  colon       :                   log         H            screen      ^C c      
  copy        ^[ [             login       L               select      '          
  detach      ^D d            meta        a                 silence     _         
  digraph     ^V             monitor     M               split       S         
  displays    *                 next        ^@ ^N sp n       suspend     ^Z z      
  dumptermcap .               number      N                 time        ^T t      
  fit         F                only        Q                 title       A         
  flow        ^F f            other       ^A              vbell       ^G        
  focus       ^I              pow_break   B                 version     v         
  hardcopy    h                 pow_detach  D                  width       W         
  help        ?                 prev        ^H ^P p ^?         windows     ^W w      
  history     { }          quit                     wrap        ^R r      
  info        i                  readbuf     <  writebuf >          
  kill        K k              redisplay   ^L l            xoff        ^S s      
  lastmsg     ^M m            remove      X               xon         ^Q q      

^]   paste .
"     windowlist -b
-    select -
0    select 0
1    select 1
2    select 2
3    select 3
4    select 4
5    select 5
6    select 6
7    select 7
8    select 8
9    select 9
I    login on
O    login off
]    paste .
|    split -v
:kB: focus prev

To detach the screen

The screen command provides an option for detaching the screen.
Consider the following example, Go to the screen by typing the below command in the terminal.

user1@linuxhelp:~$ screen

Now we are starting the updation process by running the following command.

user1@linuxhelp:~$ sudo apt-get update
sudo: unable to resolve host linuxhelp-com
[sudo] password for linuxhelp: 
Hit http://in.archive.ubuntu.com wily InRelease
Get:1 http://security.ubuntu.com wily-security InRelease [65.9 kB]    
Get:2 http://in.archive.ubuntu.com wily-updates InRelease [65.9 kB]            
Get:3 http://security.ubuntu.com wily-security/main Sources [44.2 kB]      
Hit http://in.archive.ubuntu.com wily-backports InRelease             
Get:4 http://security.ubuntu.com wily-security/restricted Sources [2,854 B]
Hit http://in.archive.ubuntu.com wily/main Sources       
Hit http://in.archive.ubuntu.com wily/restricted Sources      
Get:5 http://security.ubuntu.com wily-security/universe Sources [11.7 kB]
Hit http://in.archive.ubuntu.com wily/universe Sources             
Get:6 http://security.ubuntu.com wily-security/multiverse Sources [2,782 B]
Hit http://in.archive.ubuntu.com wily/multiverse Sources                
Get:7 http://security.ubuntu.com wily-security/main amd64 Packages [143 kB]
Hit http://in.archive.ubuntu.com wily/main amd64 Packages             
Hit http://in.archive.ubuntu.com wily/restricted i386 Packages           
Get:8 http://security.ubuntu.com wily-security/restricted amd64 Packages [10.9 kB]
Get:9 http://security.ubuntu.com wily-security/universe amd64 Packages [52.1 kB]
Hit http://in.archive.ubuntu.com wily/universe i386 Packages             
Hit http://in.archive.ubuntu.com wily/restricted Translation-en                
Get:10 http://security.ubuntu.com wily-security/multiverse amd64 Packages [6,253 B]
Get:11 http://security.ubuntu.com wily-security/main i386 Packages [139 kB]    
Hit http://in.archive.ubuntu.com wily/universe Translation-en                
Get:12 http://in.archive.ubuntu.com wily-updates/main Sources [73.0 kB]        
Get:13 http://security.ubuntu.com wily-security/restricted i386 Packages [10.8 kB]
Get:14 http://security.ubuntu.com wily-security/universe i386 Packages [52.2 kB]
Get:15 http://security.ubuntu.com wily-security/multiverse i386 Packages [6,430 B]
Get:16 http://security.ubuntu.com wily-security/main Translation-en [69.2 kB]  
100% [Waiting for headers]                                      21.6 kB/s 0s

To detach the running process screen press ‘ Ctrl+a and d’ . We will be detached from the screen, but the process continues in the back end.

user1@linuxhelp:~$ screen
[detached from 2265.pts-17.linuxhelp]

To Re-attach the screen

This command is used to reattach the screen, In my first screen I was doing the monitoring process and I have detached so now. I am going to attach the screen again to see the top process.
Syntax:
screen &ndash r < pid>

To list and see all the process that are stopped in the various screen

user1@linuxhelp:~$ screen &ndash ls
 
2472.pts-17.linuxhelp (Tuesday 12 April 2016 05:15:48 IST) (Detached)
2265.pts-17.linuxhelp (Tuesday 12 April 2016 05:03:17 IST) (Detached)
2 Sockets in /var/run/screen/S-linuxhelp.

Run the following command to reattach the screen.

user1@linuxhelp.com:~$ screen -r 2265.pts-17.linuxhelp

Various options available in Screens

  • To switch between the screen Press ‘ ctrl+a and n’ it will move to the next screen
  • To move to the previous screen Press ‘ ctrl+a and p
  • To create a new screen Press ‘ ctrl+a and c

To create a logging file

This command is used to note down the screen log features and functions of the system.
To activate the screen logging function press ‘ ctrl+a and H
While activating you will be getting the notification at the bottom of the screen showing that you have created the log file ‘ screenlog.0’ . Deactivate the screen logging function again press ‘ ctrl+a and H
To activate the screen logging features at the starting time of the screen, use the parameter ‘ -L’ as shown below.

user1@linuxhelp:~$ screen -L

To lock the screen

The hot key ‘ ctrl+a and x’ is used to lock the screen and enter the password to login into the screen.

Screen used by linuxhelp on linuxhelp.
Password:

To Add password to lock screen

Run the following command to add the password to the lock screen. While reattaching the screen we have to provide the password that we generated
To Generate a password:

user1@linuxhelp:~$ mkpasswd linux123
14dg7rth532cff

After the hash value has been created copy the hash value and paste it in the file in ‘ .screenrc‘ which is available in the home directory or else create a new file in the name of ‘ .screenrc’ .
In the format
Password 14dg7rth532cff
And save the file
Now whenever you open the screen it will ask for the authentication.

To leave the screen

The following hotkeys are used to exit the screen ‘ ctrl+a and d’ .
To Kill the screen use ‘ ctrl+a and k’ .

FAQ
Q
How to lock the screen using screen commands?
A
To lock the screen
The hot key &lsquo ctrl+a and x&rsquo is used to lock the screen and enter the password to login into the screen.
Q
What is features of screen command?
A
Mainly it is used to detach the screen from the desktop.
Q
How to install this screen command?
A
yum install screen or apt-get install screen
Q
can I use this command on Mac OS
A
yes
Q
similar commands like screen
A
tmux, dvtm & dtach