有時候常常需要 SSH 進 Linux Terminal 進行管理或問題排查,現在動不動就是幾十幾百台 Host,有時候要同時執行命令或看畫面實在不方便。其實有一個很好用的工具叫做 tmux,tmux 全名為 Terminal Multiplexer (終端多工器),可以讓你丟掉滑鼠同時開啟不同的 Terminal 進行工作,全部使用鍵盤進行操作。無論系統管理工作做得如何,至少看起來很炫,學 Hack 一樣炫技必備的工具,如果搭配裝忙神器 genact 就像下面這樣,笑...
tmux 安裝與介紹
Linux 要安裝很簡單,透過標準套件即可安裝,如下:
Ubuntu 環境:
sudo apt-get install tmux
CentOS 環境:
sudo yum install tmux
安裝完成後只要輸入 tmux 就可以進入多終端機管理操作介面,底下綠色的就是 Command Bar,有點像是 vim 的概念。畫面如下:
tmux 的概念是先有 Session,Session 包含了許多 Window,每一個 Window 可以想像為一個虛擬桌面,Window 內包含了很多 Panel,我們可以 Window 中改變 Panel 之間的並列顯示 Layout 等等。
我的習慣會在家目錄 (Home) 創建 .tmux.conf 設定檔,把一些常用的 Hot Key 進行設定,分享一下我的設定如下:
vim ~/.tmux.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# Toggle synpanes on
bind k \
set synchronize-panes on \;\
display 'SynPanes: ON'
# Toggle synpanes off
bind K \
set synchronize-panes off \;\
display 'SynPanes: OFF'
# Toggle mouse on
bind m \
set -g mode-mouse on \;\
set -g mouse-resize-pane on \;\
set -g mouse-select-pane on \;\
set -g mouse-select-window on \;\
display 'Mouse: ON'
# Toggle mouse off
bind M \
set -g mode-mouse off \;\
set -g mouse-resize-pane off \;\
set -g mouse-select-pane off \;\
set -g mouse-select-window off \;\
display 'Mouse: OFF'
# History-limit
set -g history-limit 30000
# Use 256 colors
set -g default-terminal "screen-256color"
bind -n C-v run-shell "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer" \; display-message "Pasted"
bind -n C-s run "tmux save-buffer - | xclip -selection clipboard > /dev/null" \; display-message "Copied"
# default mouse on
set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on
|
上述的設定檔會開啟滑鼠選擇 Panel 的功能、還有鍵盤同步輸入 Hot Key 方便同時在好幾個終端機執行命令,範例 .tmux.conf 可以從 GitHub 下載。
常用 tmux 熱鍵介紹
選擇 Panel:
建立新的 Panel:
當下工作 Panel 全螢幕:
Panel 垂直均分:
Panel 水平均分:
Panel 刪除:
顯示 Panel 的號碼:
Mouse No 按下
關閉滑鼠功能:
啟用滑鼠功能:
關閉同步輸入:
啟用同步輸入:
顯示 Window:
建立新的 Window:
切換下一個 Window:
切換前一個 Window:
選擇 Session:
將 Session 丟到背景 (detached) 並離開 tmux:
如果忽然斷線了,再次登入主機可以透過以下方法取回原來的工作:
先列出所有 Session
tmux ls
取回某一個 Seesion
tmux attach -t [Session ID]
以上這些功能我用過以後就回不去了,太好用了。尤其是鍵盤同步輸入...真的超方便der