tmux终端复用/tmate终端共享/screen

tmux

一种终端复用工具,是终端中的窗口管理器。另一种用途是在关闭父shell时保持程序运行(常用于ssh访问断开连接后保持运行)。windows上借助cygwin也可以使用。[1]

基本操作

  1. 创建一个session

    tmux new -s myname

  2. 连接到运行中的session

    tmux a -t myname          # -t 通过名称,可以是名称前缀,如my; a 可以写成全称 attach
    tmux a 4                  # 通过数字id
    
  3. 销毁session

    tmux kill-session -t myname

  4. 列出session

    tmux ls

进入tmux后操作panel和window的操作有:先按prefix(默认的配置是CTRL + b),释放后再按下边的:

Panes

 %                          vertical split
 "                          horizontal split
 d                          detach from session  (it keeps running in the background)
 x                          kill pane
 Up/Down/Left/Right         move between panes
 : + resize-pane -D         Resizes the current pane down
 : + resize-pane -U         Resizes the current pane upward
 : + resize-pane -L         Resizes the current pane left
 : + resize-pane -R         Resizes the current pane right
 : + resize-pane -D 20      Resizes the current pane down by 20 cells

Windows

按键 功能
c 创建create window
w 列出windows
n next window
p previous window
& kill window
, 重命名rename window

翻页及内容搜索

prefix+ PgUp/PgDn 上下翻页, 此时进入scrollback mode, 可以按ctrl-S 进行内容搜索, 搜索到的多个内容按n/N调转到下一个/上一个匹配项(这与vim用法相同) .而 Ctrl-S 是 emacs bindings. 如果将 tmux 设置成了 vim bindings, 进入 copy mode 再用 slash (/) 进行搜索.

状态栏设置

tmux 设置状态栏显示的信息,此处仅显示hostname,运行命令
tmux set -g status-right "#H"

或者在tmux配置文件(~/.tmux.conf)中写入set -g status-right "#H"
引号中还可以是自定义的文字,比如"-------"。

应用场景:一种场景是在Xshell等工具中当命令行中有内容变化时状态图标也会跟着变化,而当tmux的状态栏上时间每分钟发生一次变化时会导致Xshell的状态图标跟着发生变化,并且在用鼠标滚轮向上滚屏看历史输出时也会因为底部状态栏的变化导致视野跳到最后一行。

关闭状态栏:set -g status off
开启状态栏:set -g status on

更多tmux内容,参考

tmate

特性

  1. 远程终端共享(支持ssh和web上的命令行,且有只读模式)
  2. 基于tmux,多路终端复用
  3. 完全开源,支持自己搭建服务器,默认使用tmate.io的服务器

主页 https://tmate.io
如同ssh反向隧道。
简易用法:

tmate # 进入tmux,并创建隧道链接,底部显示链接地址,几秒后消失
tmate show-messages # 显示刚才建立的ssh地址与web地址

后台运行(daemon):

tmate -S /tmp/tmate.sock new-session -d               # Launch tmate in a detached state
tmate -S /tmp/tmate.sock wait tmate-ready             # Blocks until the SSH connection is established
tmate -S /tmp/tmate.sock display -p '#{tmate_ssh}'    # Prints the SSH connection string
tmate -S /tmp/tmate.sock display -p '#{tmate_ssh_ro}' # Prints the read-only SSH connection string
tmate -S /tmp/tmate.sock display -p '#{tmate_web}'    # Prints the web connection string
tmate -S /tmp/tmate.sock display -p '#{tmate_web_ro}' # Prints the read-only web connection string

首先远程端和本地端安装ssh相关工具,使用ssh-keygen产生公钥和私钥(否则会提示Permission denied (publickey)之类的错误)。

screen

GNU Screen 也是一个终端多路复用器(窗口管理器)。GNU Screen 允许我们轻松创建多个 Screen 会话,在不同会话之间切换,在会话之间复制文本,随时连上或脱离会话等等。
由于tmux可能没有被系统预装,而screen可能被CentOS等系统预装,因此可以临时使用screen命令。在 Screen 中所有命令的默认前缀快捷方式是 Ctrl + a。

创建新的 Screen 会话

screen # 创建会话

screen -S name # 创建命名会话,id.name

列出屏幕会话

screen -ls # 列出所有Detached和Attached会话

连上 Screen 会话

如果你想连上会话,例如 29415.ostechnix,只需运行:

screen -r 29415.ostechnix	# 全名
screen -r 29415				# 或使用 Screen ID
screen -r ostechnix			# 或使用后缀名

从 Screen 会话脱离

要从屏幕会话中脱离,按 Ctrl + ad。类似tmux的操作。

创建嵌套会话

操作类似tmux:

在会话中按 Ctrl + ac 创建另一个会话。只需重复此操作即可创建任意数量的嵌套 Screen 会话。每个会话都将分配一个号码。号码将从 0 开始。

键盘快捷键列表:

Ctrl + a " - 列出所有会话
Ctrl + a 0 - 切换到会话号 0
Ctrl + a n - 切换到下一个会话
Ctrl + a p - 切换到上一个会话
Ctrl + a S - 将当前区域水平分割为两个区域
Ctrl + a l - 将当前区域垂直分割为两个区域
Ctrl + a Q - 关闭除当前会话之外的所有会话
Ctrl + a X - 关闭当前会话
Ctrl + a \ - 终止所有会话并终止 Screen
Ctrl + a ? - 显示键绑定。要退出,请按回车

screen Attached 状态异常[2]

有时候由于正在运行screen的终端异常关闭时,这里列出的会话依然显示Attached。

screen -ls, 显示某会话的当前状态为Attached, 但当前并没有终端连接到这个会话,此时正常状态应该为Detached。此时用screen -r <session-id>,无法attach。原因是screen没有正常detach。
解决方法:screen -D -r <session-id>

-D -r 先踢掉前一用户,再登陆。


  1. http://wiki.fast.ai/index.php/Tmux ↩︎

  2. http://blog.csdn.net/gdali/article/details/72799581 ↩︎

posted @ 2018-04-30 14:54  康行天下  阅读(1264)  评论(0编辑  收藏  举报