Screen是一款由GNU开源计划开发的多视窗远程控制管理服务,简单来说就是为了解决上述情况中网络异常中断或同时控制多个远程窗口而设计的程序。
Screen服务程序不仅能够解决上述问题,而且用户在使用过程中还可以同时在多个终端会话中自由切换,能够做到会话恢复——即便网络中断,也可让会话随时恢复,用户不会失去对命令终端的控制,多窗口——每个会话都是独立运行的,拥有各自独立的编码、输入输出和窗口缓存,会话共享——可以使多个用户从不同终端使用同一个会话,也可让他们看到完全相同的输出信息的。
一、介绍用光盘挂载本地yum的方式安装
第1步,将虚拟机中的CD/DVD光盘选项中选择到下载好的系统iso镜像。
将虚拟机的光盘设备指向iso镜像
第2步:将光盘设备中的系统镜像挂载到/media/cdrom目录:
[root@linux ~]# mkdir -p /media/cdrom [root@linux ~]# mount /dev/cdrom /media/cdrom mount: /dev/sr0 is write-protected, mounting read-only
第3步:使用Vim文本编辑器创建Yum仓库的配置文件
[root@linux ~]# vim /etc/yum.repos.d/CentOS7.repo [CentOS7] name=rhel7 baseurl=file:///media/cdrom enabled=1 gpgcheck=0
开始用yum安装软件
[root@linux ~]# yum install screen Loaded plugins: langpacks, product-id, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. rhel | 4.1 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package screen.x86_64 0:4.1.0-0.19.20120314git3c2946.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: screen x86_64 4.1.0-0.19.20120314git3c2946.el7 rhel 551 k Transaction Summary ================================================================================ Install 1 Package Total download size: 551 k Installed size: 914 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : screen-4.1.0-0.19.20120314git3c2946.el7.x86_64 1/1 Verifying : screen-4.1.0-0.19.20120314git3c2946.el7.x86_64 1/1 Installed: screen.x86_64 0:4.1.0-0.19.20120314git3c2946.el7 Complete!
二、管理远程会话
screen命令能做的事情非常多,可以用-S参数创建会话窗口,用-d参数把指定会话离线,用-r参数把指定会话恢复,用-x参数一次性恢复所有的会话,用-ls参数显示当前已有的会话,用--wipe参数将目前无法使用的会话删除等等功能。
创建一个名称为backup的会话窗口,当敲下这条命令的一瞬间屏幕会快速闪动一下,这时咱们就已经处在screen服务会话中了,在这个里面运行任何操作都会被后台记录下来:
[root@linux ~]# screen -S backup [root@linux ~]#
在刚刚执行screen命令后调用系统的默认shell,所以执行命令后会立即返回一个提示符,虽然看起来跟刚刚没有变化,但实际上可以查看到当前的会话正在工作中:
[root@linux ~]# screen -ls There is a screen on: 32230.backup (Attached) 1 Socket in /var/run/screen/S-root.
想要退出一个会话也十分简单,只需要向命令行中执行exit命令就可以达到目的:
[root@linux ~]# exit [screen is terminating]
日常生产中其实并不是必须先创建会话之后再开始工作,可以直接使用screen命令执行要运行的命令,这样在命令中的一切操作也都会被记录下来,当命令执行结束后screen会话也会自动结束。
[root@linux ~]# screen vim memo.txt welcome to nulige
演示下screen不间断会话服务的强大之处,咱们先来创建一个名为linux的会话,然后强行将窗口关闭掉(这样与远程控制时突然断网效果相同):
[root@linux ~]# screen -S linux [root@linux ~]# [root@linux ~]# tail -f /var/log/messages Feb 20 11:20:01 localhost systemd: Starting Session 2 of user root. Feb 20 11:20:01 localhost systemd: Started Session 2 of user root. Feb 20 11:21:19 localhost dbus-daemon: dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper) Feb 20 11:21:19 localhost dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper) Feb 20 11:21:19 localhost dbus-daemon: dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager' Feb 20 11:21:19 localhost dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager' Feb 20 11:30:01 localhost systemd: Starting Session 3 of user root. Feb 20 11:30:01 localhost systemd: Started Session 3 of user root. Feb 20 11:30:43 localhost systemd: Starting Cleanup of Temporary Directories... Feb 20 11:30:43 localhost systemd: Started Cleanup of Temporary Directories.
刚刚咱们一起把会话窗口关闭了,这样的操作在传统远程控制中就一定会导致正在运行的命令也被终止,但在screen会话中则不会这样,咱们只需要先查看下刚刚离线的会话名称,然后尝试切换回来就可以继续工作了:
[root@linux ~]# screen -ls There is a screen on: 13469.linux (Detached) 1 Socket in /var/run/screen/S-root.
[root@linux ~]# screen -r linux [root@linux ~]# [root@linux ~]# tail -f /var/log/messages Feb 20 11:20:01 localhost systemd: Starting Session 2 of user root. Feb 20 11:20:01 localhost systemd: Started Session 2 of user root. Feb 20 11:21:19 localhost dbus-daemon: dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper) Feb 20 11:21:19 localhost dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper) Feb 20 11:21:19 localhost dbus-daemon: dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager' Feb 20 11:21:19 localhost dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager' Feb 20 11:30:01 localhost systemd: Starting Session 3 of user root. Feb 20 11:30:01 localhost systemd: Started Session 3 of user root. Feb 20 11:30:43 localhost systemd: Starting Cleanup of Temporary Directories... Feb 20 11:30:43 localhost systemd: Started Cleanup of Temporary Directories. Feb 20 11:40:01 localhost systemd: Starting Session 4 of user root. Feb 20 11:40:01 localhost systemd: Started Session 4 of user root.
当然如果咱们突然又想到了还有其他事情要做,也可以再多创建出几个会话窗口来一起使用,而如果这段时间内不会再使用某个会话窗口,可以将它设置为临时断开模式(detach),随后工作需要时再重新连接(attach)回来,这期间会话内的程序也会正常运行。
三、会话共享功能
screen命令不仅能够让使用者在极端情况下也不丢失对系统的远程控制,保证了生产环境远程工作的不间断性,而且还具有会话共享、分屏切割、会话锁定等等实用的功能。会话共享功能是一件很酷的事情,当多个用户同时用某个用户远程控制主机的时候,便可以将屏幕内容共享出来,也就是说每个人都可以看到相同的内容,拓扑如图所示:
Screen实现会话共享功能流程拓扑
要实现会话共享功能,咱们需要先通过客户终端A远程ssh连接到服务器主机端,并创建一个会话窗口:
[root@client A ~]# ssh 192.168.10.10 The authenticity of host '192.168.10.10 (192.168.10.10)' can't be established. ECDSA key fingerprint is 70:3b:5d:37:96:7b:2e:a5:28:0d:7e:dc:47:6a:fe:5c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.10.10' (ECDSA) to the list of known hosts. root@192.168.10.10's password: Last login: Wed May 4 07:56:29 2016 [root@client A ~]# screen -S linuxprobe [root@client A ~]#
然后开启第二台客户终端B远程ssh连接到服务器主机端,执行获取远程会话的命令即可,马上两台主机就能看到相同的内容了:
[root@client B ~]# ssh 192.168.10.10 The authenticity of host '192.168.10.10 (192.168.10.10)' can't be established. ECDSA key fingerprint is 70:3b:5d:37:96:7b:2e:a5:28:0d:7e:dc:47:6a:fe:5c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.10.10' (ECDSA) to the list of known hosts. root@192.168.10.10's password: Last login: Wed Feb 22 04:55:38 2017 from 192.168.10.10
[root@client B ~]# screen -x