Linux: Screen Command Examples
In Linux we have one command called screen which is used world wide for multiplexes a physical console between several processes. In this way we could use one open terminal windows for various terminal instances inside a one single screen terminal.
Screen command is very useful for linux, sometime we need to deal with multiple programs or terminal on one screen only. For same we can use screen which allow us to work on multiple windows without switching on multiple tabs. It also allow to share sessions with other users and detach/attach session even after disconnection console.
On some of Linux distributions screen is installed by-default, but on others we have to install it. like below
apt-get install screen (On debian based systems) yum install screen (On RedHat based Systems)
In this post we will try to see how we could work and use screen command in Linux machines.
To start screen session, we just need to use command screen and it start. On some distribution it show it version and summary page before start but in most distribution it just start.
It start with name process-id.pts-number.hostname, like below.
#screen -ls There is a screen on: 8590.pts-18.jarvis (Friday 31 March 2017 03:24:10 IST) (Attached) 1 Socket in /var/run/screen/S-root.
But it’s possible to start with some easy names, like below
#screen -ls There is a screen on: 5585.Screen1 (Wednesday 05 April 2017 03:28:22 IST) (Attached) 1 Socket in /var/run/screen/S-root.
Inside screen command terminal, we can operate all functions used in normal linux console. It also provides some additional functions. Ctl+a is main key used to apply these functions with screen command. We could take help for all these within screen windows with ctl+a and ?, like below.
Let’s discuss some of feature of screen command
1. Start and naming of screen
To start screen session, we just have to type screen command like below
This way it create screen session with default name as described above, but we could name these session for remember it properly.
screen -S Screen1
2. Attach and detach of screen
Any normal process running on Linux SSH console could terminate because of network glitch on any Client and server side. But this will remian safe in screen. in case of screen it just detach and can attach later once network is back online.
We can also detach any SSH console if any long time taking process is running, So we could detach console and attach it later again.
For detach — ctl+a and d
For suspend — ctl+a and z
For attach — screen -r screen-name
3. Multiple windows in one Screen session
when we are connecting to screen session and we like to connect other servers or need to run other program, In such case we could open multiple windows with in that same screen session. For this we Just have to open another window with “ctl+a and c”. With this way we could open another terminal windows on same screen session.
We could take an example of such case, like we need to connect one jump server to connect various servers located in DataCenter. But with normal terminal session we could only connect one server once. But with screen multiple windows we could able to connect many servers from one screen session that connected to Jump server.
Create multiple windows in screen — ctl+a and c
Switch to next window in screen — ctl+a and n
Switch between two windows in screen — ctl+a and a
See all windows screen — ctl+a and “
4. Splitting of screen window session
Sometime we require to see multiple screen windows in one terminal screen, This provide us more flexible vision for various programs running on screen sessions.
we could split screen terminal window horizontally and vertically both. But some older version has only horizontally split feature not vertical. We could also divide screen terminal on basis of percentage.Below image show an example of same.
Split Vertivally — ctl+a and |
Split Horizontally — ctl+a and S
Re-size window vertically — ctl+a and :resize -v and <70% Whatever percent you want>
Re-size windows Horizontally — ctl+a and :resize -h and <70% Whatever percent you want>
5. Lock screen session
We can also lock screen session with ctl+a and x. This will ask for password to connect screen session again, User need to provide its own Linux password to unlock that session.
Screen used by root on jarvis. Password:
6. Change Windows Title and number
Whenever we create new window inside screen session , it generate its own Windows name and number by-default. But we can manage these things with some small shortcuts like below.
Change Window name — ctl+a and A
Change Window number — ctl+a and :number
7. Logging whatever you do
Sometime we have to capture log for our activity on console. Like in PuTTY we have putty log option. In same way screen have its own way to logging all activities on machine. To activate it we have to use keyword ctl+a and H. it will create a screenlog.0 file in user’s home directory.In case you like to deactivate logging just repeat same ctl+a and H again.
Sometime we also need to take text format screenshot, that we can do in screen with ctl+a and h. This will create hardcopy.1 file in user’s home directory. It is only for onetime, it will not going to append anything. Every time it will create same file with new content.
8. Kill a screen session
We always advise to quit properly in case you don’t want a screen session for any purpose. Sometime i saw some guys left their screen session live on Jump machines without any reasons. we could kill our screen windows one by one or all in one command.
Kill one screen window — ctl+a and k
Kill all screen window — ctl+a and \