Total Pageviews

Thursday 27 December 2012

Tmux -在同一个窗口中打开多个终端(类似WINDOWS下的MTPUTTY)

As a sysadmin, most of my time is spent working on remote machines and different task. tmux is a terminal multiplexer, meaning it allows you to run multiple terminals in the same window.
This tutorial will explain the basics features of tmux that should help you be more productive with your every day task.

If you do now have tmux installed yet, well, it is time to fire up a terminal and get it installed:
# apt-get install tmux
Just like screen, tmux has the great feature of being able to detach the sessions, this means that you can start a session on a remote machine from your office, do your work there during the day, detach the session and go how and finally, the day after re-attach to your session and have everything set up just like you left the office the previous day.
So, let’s start with handling sessions first

Session Management

Single Session

If you plan to only run a single session on your session, it will be enough to just run:
$ tmux
This should give you a session called [0] and is nothing more than shell.
If you want to exit that process, you can exit like you would do with a normal command prompt, and that would kill the session, or you can type Ctrl-b d and you will detach the session.
The next time you want to attach the session, you will run
$ tmux attach
and that will get you back to it.
Nice, we can now create a session, do some stuff in it, detach it and re-attach it later.

Multiple Session

Let’s get a step further and handle multiple session, but before that I think it is worth understanding the jargon used in tmux lingua.

Lingua

tmux
tmux sessions
Tmux can create multiple session, each session is composed of windows, which can then be split vertically and horizontally. In the picture, you can see that there is 2 sessions (session1 and session2) which respectively contain s1win1, s1win2 for the former and s2win1, s2win2 for the latter.
The window which has the focus is s2win1 in session2. The active session is mentionned between square brackets [session2] while the active window is marked with an asterisk s2win1.

Managing Sessions and Windows

Here are a few commands that we will be using:
  • Ctrl-b c create a new window
  • Ctrl-b , rename window
  • Ctrl b n next window
  • Ctrl b p previous window
  • Ctrl-b : command prompt
  • Ctrl-b w choose window
  • Ctrl-b s choose tree
  • Ctrl-b $ rename session
  • Ctrl-b ? help
Now, let’s create some sessions and windows and play a bit with them to get a better understanding of what can be done.
First, we will create a first session called session1 which will have its first window called s1win1:
$ tmux new -s session1 -n s1win1
then, we will create a second session from within the running tmux client instance we are using and will call the sesion session2 with its first window called s2win1. Type Ctrl-b :, this gives you a prompt. now type:
new -s session2 -n s2win1
You should now have created a new session with a new window and this should be your active window.
Let create a new window within that session:
Ctrl-b c
and rename it to s2win2:
Ctrl-b ,
At the prompt, change the name of the window to s2win2 and hit enter.
Good, we now have 2 sessions with some window in them. Let’s detach from it and play a bit more with it.
Ctrl-b d
now, from the shell prompt, we can list the sessions with:
$ tmux ls
and re-attach session 1 using:
$ tmux a -t session1
Now, lets rename session1 to myfirstsession:
Ctrl-b $
and type the the name of the session.
Now, let get back to session2 s2win2 by typing:
Ctrl-b s
And using the left-right arrows to fold/unfold the sessions’windows list and up-down to move around.
Finally, once the cursor is on (4) └─> 1: s2win2* hit enter.
To go back to s2win1, type:
Ctrl-b p
and
Ctrl-b n
To switch back to s2win2, or we could have used
Ctrl-b w
to move between windows.
Finally, get close all this up and press
Ctrl-d
twice, this should tell you that the session [exited].
Let’s check the sessions left:
$ tmux ls
myfirstsession: 1 windows (created Fri Dec 7 00:00:42 2012) [238x56]
Let’s attach to it:
$ tmux a
and close it
Ctrl-d
That’s it! we have created multiple sessions and windows, moved around, rename things and finally cleaned everything. There is much more that can be done with tmux but that will be it for this post, and hopefully, more to come soon!
In the meantime, dont forget to use Ctrl-b ? within tmux to access the help along with man tmux for even more help!

FROM  http://www.debuntu.org/how-to-tmux-a-terminal-multiplexer/
----------------------------------------------

强大的tmux


 刚刚在网上找到了一个软件,说是用来代替screen的,在之前的文章中我已经介绍过了screen的功能,简单来说,Screen是一个可以在多个进程之间多路复用一个物理终端的窗口管理器,而tmux比screen更加的强大.

首先它可以自由的分割窗口,则在我们对比文件的时候非常有用.

2. 支持 UTF-8 及 256 色终端;

3. 可在多个缓冲区进行复制和粘贴;

4. 可通过交互式菜单来选择窗口、会话及客户端;

5. 支持跨窗口搜索;

6. 非正常断线后不需重新detach;

7. 可以在多个ssh会话中使用同一组tmux会话。

和screen相比tmux明显的比screen好很多.

当然那,耳听为虚,眼见为实,大家想看看这个软件的截图.

点击查看原图

这里我们来介绍一下tmux的安装和用法.

安装:

ubuntu:

1    sudo apt-get install tmux
openwrt:

1    opkg update
2    opkg install tmux
其实linux下软件的安装方法大致是雷同的,不同的是管理软件的不同,比如debian系用deb包,fedora使用rmp包,好了咱们接着说tmux的用法.



tmux的基本用法:

tmux #启动

C-b d #挂起,效果如同screen中的C-a d

tmux attach #恢复会话,效果如同screen中的screen -r

更多功能(需要在tmux窗口中使用):

C-b ? // 列出所有快捷键;按q返回

C-b 空格键 //采用下一个内置布局

C-b ! // 将当前面板置于新窗口;即新建一个窗口,其中仅包含当前面板

C-b " // 将当前面板平分为上下两块

C-b % // 将当前面板平分为左右两块

C-b q // 显示分隔窗口的编号

C-b o // 跳到下一个分隔窗口

C-b 方向键 // 上一个及下一个分隔窗口

C-b C-方向键 //以1个单元格为单位移动边缘以调整当前面板大小

C-b a/t-方向键 //以5个单元格为单位移动边缘以调整当前面板大小

C-b & // 关闭但前窗口

C-b c // 创建新窗口

C-b 0~9 //切换至指定窗口

C-b n // 选择下一个窗口

C-b l // 在前后两个窗口间互相切换

C-b p // 选择前一个窗口

C-b w // 以菜单方式显示及选择窗口

C-b s // 以菜单方式显示和选择会话

C-b t //显示时钟

C-b f //在所有的窗口中进行搜索

好了,一般情况下常用的tmux的选项也就大概是这几个,如果大家想要更多的帮助的话可以使用一下命令查看tmux的帮助文件

1    man tmux
要是大家看不懂英文也没有关系,我们可以到下面的网站上去查看tmux的使用方法,不过介绍就没有man强了,好了废话不多说,下面附上tmux使用及帮助的网站:
http://wiki.freebsdchina.org/software/t/tmux
------------------------------------------------

终端控制利器——tmux




Linux下终端使用率是非常高的,每次开机或者重启之后就要重新打开一堆终端窗口或者标签,管理起来也不太方便。之前我同时开着xterm、xfce4自带的terminal和guake三种终端,也听说过screen,不过那时还是一知半解。
今天好好看了下wiki,比较深的也没看,简单点说tmux&screen就是终端内置的窗口、面板、会话管理器,可以在一个终端内实现多窗口管理、多面板操作和会话保存和恢复。
xterm这样不支持多标签的终端不仅可以在一个终端窗口内实现类似多标签的窗口管理,还可以进行面板分割操作,对大屏幕用户很实用。
最方便的还是其会话管理,可以保存退出会话时的多窗口、面板状态和命令输出信息,类似系统待机,重载会话后完全恢复退出时的状态。
tmux和screen会话默认关机不会保存,找到一个简单的方法,系统重启后,可以让tmux自动打开几个窗口。
把下面的代码复制到.tmux.conf里,第一行命令建立一个名为sessionname的会话,窗口名为windowname,并运行 Command命令,第二行在会话内再新建一个名为windowname的窗口并运行Command命令,以此类推。sessionname和 windowname改成自己喜欢的,Command需要加上,不需要就去掉。
1
2
3
4
#start with preloaded windows
new -d -s sessionname -n windowname Command
neww -d -n windowname  Command
neww -d -n windowname  Command
开机或者重启后在终端输入tmux attach就会自动打开此自定义会话。
再说说tmux和screen,两者是功能非常类似的软件,连基本操作都差不多,由于screen的授权协议tmux设计用来取代screen。二 者最大的不同就是面板操作时tmux能垂直、水平分割窗口,screen只能水平分割窗口。其它会话管理、窗口管理方面都差不多。
最后还是选择了tmux,似乎很多screen用户也转到tmux了。把terminal、guake都卸载了,就留下xterm配合tmux使用。
tmux和screen都在arch官方软件库里,直接安装就行了。tmux安装配置参考:Tmux – ArchWiki,基本操作参考:使用tmux [FreeBSDChina Wiki]。screen的配置和操作都可以参考:GNU Screen (简体中文) – ArchWiki
-------------------

tmux使用教程及技巧

tmux简介
tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD,采用BSD授权。使用它最直观的好处就是,通过一个终端登录远程主机并运行tmux后,在其中可以开启多个控制台而无需再“浪费”多余的终端来连接这台远程主机;当然其功能远不止于此。
直接使用yum安装: yum install tmux
tmux使用C/S模型构建,主要包括以下单元模块:

-说明
server服务器。输入tmux命令时就开启了一个服务器。
session会话。一个服务器可以包含多个会话。
window窗口。一个会话可以包含多个窗口。
pane面板。一个窗口可以包含多个面板。
操作
类似各种平铺式窗口管理器,tmux使用键盘操作,常用快捷键包括:

Ctrl+b激活控制台;此时以下按键生效
系统操作?列出所有快捷键;按q返回
d脱离当前会话;这样可以暂时返回Shell界面,输入tmux attach能够重新进入之前的会话
D选择要脱离的会话;在同时开启了多个会话时使用
Ctrl+z挂起当前会话
r强制重绘未脱离的会话
s选择并切换会话;在同时开启了多个会话时使用
:进入命令行模式;此时可以输入支持的命令,例如kill-server可以关闭服务器
[进入复制模式;此时的操作与vi/emacs相同,按q/Esc退出
~列出提示信息缓存;其中包含了之前tmux返回的各种提示信息
窗口操作c创建新窗口
&关闭当前窗口
数字键切换至指定窗口
p切换至上一窗口
n切换至下一窗口
l在前后两个窗口间互相切换
w通过窗口列表切换窗口
,重命名当前窗口;这样便于识别
.修改当前窗口编号;相当于窗口重新排序
f在所有窗口中查找指定文本
面板操作将当前面板平分为上下两块
%将当前面板平分为左右两块
x关闭当前面板
!将当前面板置于新窗口;即新建一个窗口,其中仅包含当前面板
Ctrl+方向键以1个单元格为单位移动边缘以调整当前面板大小
Alt+方向键以5个单元格为单位移动边缘以调整当前面板大小
Space在预置的面板布局中循环切换;依次包括even-horizontal、even-vertical、main-horizontal、main-vertical、tiled
q显示面板编号
o在当前窗口中选择下一面板
方向键移动光标以选择面板
{向前置换当前面板
}向后置换当前面板
Alt+o逆时针旋转当前窗口的面板
Ctrl+o顺时针旋转当前窗口的面板
tmux配置
tmux的系统级配置文件为/etc/tmux.conf,用户级配置文件为~/.tmux.conf。配置文件实际上就是tmux的命令集合,也就是说每行配置均可在进入命令行模式后输入生效。
下面是我的~/.tmux.conf配置:
# base
set -g display-time 3000    # 提示信息的持续时间;设置足够的时间以避免看不清提示,单位为毫秒
set -g history-limit 65535  # 每个窗口中可展示的历史行数

# mouse
set -g mode-mouse on         # 开启鼠标控制
set -g mouse-resize-pane on  # 开启鼠标可调整pane大小
set -g mouse-select-pane on  # 开启鼠标可选择pane
set -g mouse-select-window on # 开启鼠标可选择窗口

# bind key
unbind '"'           # 取消 '"' 的绑定, 原用于上下分割窗口
bind _ splitw -v     # 绑定 '_' 上下分割窗口 
unbind %             # 取消 '%' 的绑定, 原用于左右分割窗口
bind | splitw -h     # 绑定 '|' 左右分割窗口
bind r source-file ~/.tmux.conf \; display "Reloaded!" # 绑定 'r' 用于重载配置文件,重载后显示 "Reloaded!"
bind s setw synchronize-panes on  # 开启 pane 命令同步
bind a setw synchronize-panes off # 关闭 pane 命令同步

# window
set -w -g utf8 on    # 窗口显示内容使用utf8字符集显示
set -w -g window-status-current-bg red  # 当前选中窗口背景色为红色

# title
set -g set-titles on           # 开启终端程序的标题显示
set -g set-titles-string "#T"  # 标题显示内容为 ~/.bashrc 中 $PROMPT_COMMAND 变量的内容

# status bar
set -g status-utf8 on          # 状态栏使用utf8字符集
set -g status-bg black         # 状态栏背景色为 黑色
set -g status-fg white         # 状态栏前景色为 白色
set -g status-interval 2       # 状态栏刷新频率 2秒
set -g status-justify "left"   # 窗口列表的位置 靠左
set -g status-left "#[fg=yellow]#S "  # 状态栏最左端: Session的名称(颜色为yellow)
set -g status-right "#[fg=black]#T #[fg=yellow]%H:%M"  # 状态栏最右端: $PROMPT_COMMAND及时间
set -g status-right-length 50  # 状态栏右端的长度
tmux的session管理
Seesion 可以有效地分离工作环境。如我有三个网站, 可以分别设置'siteA','siteB','siteC'三个Session, 可以针对不同网站的需求和服务器的分布情况进行特定管理.
tmux new -s session_name # 创建一个叫做 session_name 的 tmux session
tmux attach -t session_name  # 重新开启叫做 session_name 的 tmux session
tmux switch -t session_name # 转换到叫做 session_name 的 tmux session
tmux list-sessions  # 列出现有的所有 session
tmux ls  # 列出现有的所有 session
tmux detach (prefix + d) # 离开当前开启的 session
tmux rename-session -t [current-name] [new-name] # 重命名session
Session的批量重建和管理脚本
#!/bin/bash


Session=$1
session_arr=(siteA siteB siteC)

function tmux_siteA()
{
    # mg
    tmux new-session -d -s 'siteA' -n 'mg' # 创建session siteA, 并给默认窗口命名为 mg
    tmux select-window -t 'mg'
    tmux split-window -h -p 50
    tmux send-keys -t 0 'ssh 192.168.1.101' C-m        # 其中 C-m 表示回车键 

    # ngx.web
    tmux new-window -n 'ngx.web'           # 新建 ngx.web 的窗口
    tmux split-window -h -p 50             # 左右分割 ngx.web 窗口为两个pane, 新建的pane的百分比为50%
    tmux send-keys -t 0 'ssh 1.1.1.0' C-m  # 引号内的内容是该pane中需要预先执行的命令
    tmux send-keys -t 1 'ssh 1.1.1.1' C-m  # 我这里是分别登录到两台机器中去
}

function tmux_siteB()
{
    # mg.zbx
    tmux new-session -d -s 'siteB' -n 'mg.zbx'
    tmux select-window -t 'mg.zbx'
    tmux split-window -h -p 50
    tmux select-pane -t 0
    tmux split-window -v -p 50
    tmux send-keys -t 1 'pwd' C-m
    tmux send-keys -t 2 'ssh 192.168.1.100' C-m

    # ngx.web
    tmux new-window -n 'ngx.web'
    tmux split-window -h -p 50
    tmux select-pane -t 1
    tmux split-window -v -p 50
    tmux select-pane -t 0
    tmux split-window -v -p 50
    tmux send-keys -t 0 'ssh 192.168.1.100' C-m
    tmux send-keys -t 1 'ssh 192.168.1.101' C-m
    tmux send-keys -t 2 'ssh 192.168.1.102' C-m
    tmux send-keys -t 3 'ssh 192.168.1.103' C-m

}

function tmux_siteC()
{
    # mg
    tmux new-session -d -s 'siteC' -n 'mg'
    tmux select-window -t 'mg'
    tmux split-window -h -p 50
    tmux send-keys -t 2 'g 80' C-m

    # push
    tmux new-window -n 'push'
    tmux split-window -h -p 50
    tmux select-pane -t 1
    tmux split-window -v -p 50
    tmux select-pane -t 0
    tmux split-window -v -p 50
    tmux send-keys -t 0 'ssh 192.168.1.100' C-m
    tmux send-keys -t 1 'ssh 192.168.1.101' C-m
    tmux send-keys -t 2 'ssh 192.168.1.102' C-m
    tmux send-keys -t 3 'ssh 192.168.1.103' C-m

}

for i in ${session_arr[@]}
do
     if [ "X$i" == "X$Session" ]
     then
        tmux start-server
        tmux has-session -t $Session 2>/dev/null
        Res=$?
          if [ $Res != 0 ]
          then
               tmux_$Session
          fi

        tmux attach-session -d -t $Session
     fi
done