Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Screen is very useful to help you administer server via SSH or console. It allows multiple shell session to be controlled by single SSH session.
As server administrator, Screen usually being used to:
- Create multiple windows with each of the window is running different command
- Create 2 windows. One window managing local server, another one is managing other remote server
- Share window with other user remotely. They can see what you type and run in the shell via SSH
Lets install Screen first via SSH (in this example I will use Putty) and I will show you on how to use and take advantages by having this application installed:
yum install screen -y
apt-get install screen
cd /usr/ports/sysutils/screen make make install clean
notes: (on FreeBSD, they might have some prompt, just accept the default value)
Installation done. I will show some example so you can follow and understand on how we should use Screen:
1. Type following command to start screen:
[[email protected] ~]# screen
2. You have enter first window of your screen. Run following command:
[[email protected] ~]# top
3. Now, press ‘Ctrl-A-C’ in the keyboard. This will create another window next to it. Run following command afterwards:
[[email protected] ~]# netstat -na
4. Now, press another ‘Ctrl-A-C’ in the keyboard. This will create another windows next to 2nd window (the netstat window). Run following command:
[[email protected] ~]# dmesg
5. So you have 3 window running in one SSH session. To check this, type following command and you should see something like below:
[[email protected] ~]# w 20:03:34 up 42 days, 23:57, 3 users, load average: 0.01, 0.06, 0.02 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/2 192.168.211.100: 22Jun10 0.00s 0.41s 0.41s /bin/bash root pts/3 192.168.211.100: 22Jun10 10days 0.11s 0.11s /bin/bash root pts/0 192.168.211.100: 21Jun10 13days 0.10s 0.10s /bin/bash
6. You can see that system is detecting 3 session in one SSH with different TTY because we have create 3 screen just now. Now we will navigate to those screen so we can check what is happening between them. Press ‘Ctrl-A-N’ to navigate to next screen. This will show you the ‘top’ screen just now. Press ‘Ctrl-A-P’ to navigate to previous screen. This will show you the ‘dmesg’ screen. To navigate to ‘netstat’ screen, press ‘Ctrl-A-P’ again or ‘Ctrl-A-N’ two times.
7. So you know how to navigate between screen. Now lets see the status of our screen. Run following command and you should something similar as below:
[[email protected] monitor]# screen -ls There is a screen on: 15807.pts-0.server (Attached) 1 Socket in /var/run/screen/S-root.
8. You can see that you are ‘attached’ to a screen. Means you are inside the screen and actively using it. The ‘15807.pts-0.server’ means pid 15807, terminal id is ‘pts-0’ and ‘server’ is the hostname of this server.
9. Now we will disconnect from the server immediately. Just close your Putty or any terminal program you used to connect to the server. (or you can detach the screen by pressing ‘Ctrl-A-D’).
8. SSH back to the server. Run following command:
[[email protected] ~]# screen -ls There is a screen on: 15807.pts-0.server (Detached) 1 Socket in /var/run/screen/S-root.
9. You will see that there is one screen running but no one attached to it (because just now we have disconnect to it). We can resume back on what screen we have established previously by typing following command:
[[email protected] ~]# screen -r
10. You are entering back your session in screen. No worries on connectivity anymore. If you have more than 1 screen like below, you need to specify pid after ‘screen -r’ statement to resume back the detached screen:
[[email protected] ~]# screen -ls There are screens on: 15807.pts-0.server (Detached) 8370.pts-1.server (Detached) 2 Sockets in /var/run/screen/S-root. [[email protected] ~]# screen -r 8370
11. If there are other users want to see your screen via console or SSH, they need to specified -x options with screen number on the attached screen. Example as below:
[[email protected] ~]# screen -ls There are screens on: 15807.pts-0.server (Attached) 8370.pts-1.server (Detached) 2 Sockets in /var/run/screen/S-root. [[email protected] ~]# screen -x 15807
12. Now you and another user in multi-display mode which allow him to see what you done. This will greatly improved communications between users. To terminate Screen, just type ‘exit’ on each of window you want to closed. This will result something like this:
[screen is terminating]