Screen 终端复用器
(重定向自 Screen on console)
screen 是一个终端复用器。它是一个很好的工具,例如用于远程支持。它也可以用于启动您希望在关闭控制台会话后继续运行的命令(您可以稍后连接到您正在运行的会话)。当您进入一个 screen 会话时,您不会注意到太多。
要了解您是否在一个 screen 会话中,请使用下面 '获取帮助' 部分中提到的说明。
安装
为了使用 screen,您需要安装它
apk add screen
用法
启动新会话
要进入一个 screen 会话,您只需输入
screen
列出现有会话
当您启动了一些会话后,您可以列出它们
screen -list
您可能会得到一个如下所示的列表
There are screens on: 11151.pts-1.mhlab01 (Attached) 11131.pts-3.mhlab01 (Attached) 2 Sockets in /var/run/screen/S-root.
连接到现有会话
假设您想连接到一个现有会话(例如,上面的 11131.pts-3.mhlab01
会话)。
screen -x 11131
或
screen -x pts-3
提示: 如果您看到
Attaching from inside of screen?
,您已经在一个 screen 会话中。控制 screen 会话
在 screen 会话中,您可以使用键盘快捷键来控制它。我们只会描述其中一些替代方法。
获取帮助
最有用的命令之一是给您 '帮助' 的命令。
在您的 screen 会话中,点击
^A ?
(点击 ? 时不要按住 CTRL)
从会话中分离
有时,仅从会话中分离而不终止它是很有用的。
^A d
提示: 尝试在 screen 会话中启动
稍等片刻后,使用
请注意,“seq” 值表示 ping 在您从会话中分离时仍在继续运行。
ping 127.0.0.1
,然后从会话中分离。稍等片刻后,使用
screen -x
重新连接到会话。请注意,“seq” 值表示 ping 在您从会话中分离时仍在继续运行。
关闭或终止会话
要“终止”会话
^A k
在提示时点击 y 确认。
您也可以通过输入以下命令“终止”您的会话
exit
额外
连接到串口控制台
screen 当您需要连接到串口控制台时,是一个很好的工具(例如,如果您想使用交换机的串口配置它)。连接到串口控制台可能如下所示
screen /dev/ttyS0 9600
强制控制台用户进入 screen 会话
在某些情况下,您可能只想强制控制台用户进入 screen 会话。
请注意,此配置不会强制 SSH 用户进入 screen。
编辑 /etc/profile 并添加以下代码到其中
if [ -n "$PS1" ] && [ -z "$STARTED_SCREEN" ] && [ -z "$SSH_TTY" ]; then STARTED_SCREEN=1 ; export STARTED_SCREEN screen -RR && exit 0 echo "Screen failed! continuing with normal startup" fi
强制控制台和 SSH 用户进入 screen 会话
上面的示例包含 if 语句
&& [ -z "$SSH_TTY" ]
从上述配置中删除此部分以强制 SSH 会话进入 screen 会话。
注意: 按照这些说明操作时,控制台用户也将被强制进入 screen 会话
其他终端复用器
配置 .screenrc (示例)
#hardstatus off hardstatus alwayslastline '%{= kG}[ %{y}%H%? %1`%?%{g} ][%= %{= kw}%-w%{+b yk} %n*%t%?(%u)%? %{-}%+w %=%{g}][ %{y}%l %{g}][%{W}%c:%s %{g}]' msgwait 1 vbell off # Huge scrollback buffer defscrollback 5000 # No welcome message startup_message off # Clear the screen after closing some programs altscreen on # Get rid of the vertical bars rendition so =00 caption string "%{03} " # 256 colors term screen-256color terminfo rxvt-unicode 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm' termcapinfo xterm* ti@:te@ attrcolor b ".I" # UTF-8 #defutf8 on #utf8 on # Default Windows # Switch windows with F3 (prev) and F4 (next) bindkey "^[OR" prev bindkey "^[OS" next # Get rid of silly xoff stuff bind s split bind c screen 1 bind ^c screen 1 bind 0 select 10 screen 1 # remove some stupid / dangerous key bindings bind k bind ^k bind . bind ^\ bind \\ bind ^h bind h hardcopy bind 'K' kill bind '}' history bind L screen -t dmesg 10 watch "dmesg | tail -n $((LINES-42))" bind T screen -t htop 11 htop bind A screen -t atop 12 atop bind N screen -t nethogs 15 nethogs bind V screen -t vnstat 16 vnstat bind S screen -t ss 17 ss -s
在哪里找到更多示例
- Archlinux 论坛 (https://bbs.archlinux.org/viewtopic.php?id=55618)
- GithubGist (https://gist.github.com/search?utf8=%E2%9C%93&q=screenrc)
- Github (https://github.com/search?utf8=%E2%9C%93&q=screenrc)