一、安裝GoTTY
使用go get
安裝
此方法需要 golang
環境支持,go的版本需要>=1.9
。
go version
go version go1.16.3 linux/amd64
go get
命令會從最新的 master 分支編譯出一個二進制文件放在 $GOPATH/bin
下,其中可能包括不穩定或重大的更改。
go get github.com/yudai/gotty
注:請檢查 $GOPATH/bin
是否已經加入到 path
中。export PATH="$PATH:$GOPATH/bin"
使用二進制安裝
下載地址:https://github.com/yudai/gotty/releases/tag/v1.0.1
下載對應版本到本地,我這邊下載的是 gotty_linux_amd64.tar.gz linux 64位的版本。
解壓文件,並將文件移動到/usr/bin下
tar -zxvf gotty_linux_amd64.tar.gz
sudo mv ./gotty /usr/bin
檢查是否安裝成功
gotty -v
gotty version 1.0.1
二、使用GoTTY
GoTTY的常規語法
gotty [options] <command> [<arguments...>]
GoTTY命令的常用參數
--address value, -a value IP address to listen (default: "0.0.0.0") [$GOTTY_ADDRESS]
--port value, -p value Port number to liten (default: "8080") [$GOTTY_PORT]
--permit-write, -w Permit clients to write to the TTY (BE CAREFUL) [$GOTTY_PERMIT_WRITE]
--credential value, -c value Credential for Basic Authentication (ex: user:pass, default disabled) [$GOTTY_CREDENTIAL]
--random-url, -r Add a random string to the URL [$GOTTY_RANDOM_URL]
--random-url-length value Random URL length (default: 8) [$GOTTY_RANDOM_URL_LENGTH]
--tls, -t Enable TLS/SSL [$GOTTY_TLS]
--tls-crt value TLS/SSL certificate file path (default: "~/.gotty.crt") [$GOTTY_TLS_CRT]
--tls-key value TLS/SSL key file path (default: "~/.gotty.key") [$GOTTY_TLS_KEY]
--tls-ca-crt value TLS/SSL CA certificate file for client certifications (default: "~/.gotty.ca.crt") [$GOTTY_TLS_CA_CRT]
--index value Custom index.html file [$GOTTY_INDEX]
--title-format value Title format of browser window (default: "{{ .command }}@{{ .hostname }}") [$GOTTY_TITLE_FORMAT]
--reconnect Enable reconnection [$GOTTY_RECONNECT]
--reconnect-time value Time to reconnect (default: 10) [$GOTTY_RECONNECT_TIME]
--max-connection value Maximum connection to gotty (default: 0) [$GOTTY_MAX_CONNECTION]
--once Accept only one client and exit on disconnection [$GOTTY_ONCE]
--timeout value Timeout seconds for waiting a client(0 to disable) (default: 0) [$GOTTY_TIMEOUT]
--permit-arguments Permit clients to send command line arguments in URL (e.g. http://example.com:8080/?arg=AAA&arg=BBB) [$GOTTY_PERMIT_ARGUMENTS]
--width value Static width of the screen, 0(default) means dynamically resize (default: 0) [$GOTTY_WIDTH]
--height value Static height of the screen, 0(default) means dynamically resize (default: 0) [$GOTTY_HEIGHT]
--ws-origin value A regular expression that matches origin URLs to be accepted by WebSocket. No cross origin requests are acceptable by default [$GOTTY_WS_ORIGIN]
--term value Terminal name to use on the browser, one of xterm or hterm. (default: "xterm") [$GOTTY_TERM]
--close-signal value Signal sent to the command process when gotty close it (default: SIGHUP) (default: 1) [$GOTTY_CLOSE_SIGNAL]
--close-timeout value Time in seconds to force kill process after client is disconnected (default: -1) (default: -1) [$GOTTY_CLOSE_TIMEOUT]
--config value Config file path (default: "~/.gotty") [$GOTTY_CONFIG]
--version, -v print the version
使用詳解
基本用法
gotty top
此命令將 top
命令綁定到默認端口 8080,使用瀏覽器訪問 http://127.0.0.1:8080/ 即可看到 top
命令的輸出內容

默認情況下,GoTTY 不允許客戶端發送任何按鍵或命令,如果需要輸入內容,可添加 -w
參數(后跟bash表示進入bash終端)
gotty -w bash
指定端口訪問
添加 -port
參數,用於指定端口訪問。
gotty -port 8899 -w bash
開啟身份驗證
使用 -c
參數限制客戶端的訪問,用戶需要輸入用戶名和密碼來登入終端。用戶名和密碼以 :
分割(用戶名:密碼)
gotty -port 8899 -w -c "root:7777" bash
自定義配置
你可以在 ~/.gotty
配置文件中修改默認選項,如果該文件存在,GoTTY會在每次啟動時加載這個文件。
vim ~/.gootty
// 默認的監聽端口
port = "9000"
// 默認是否開啟tls
enable_tls = true
// 設置控制台顯示的字體大小和背景顏色
preferences {
font_size = 10
background_color = "rgb(16, 16, 32)"
}
開啟 TLS/SSL
選項
默認情況下 GoTTY 不加密服務器與客戶端之間的所有流量,可通過添加 -t
參數在會話上開啟TLS / SSL的選項。默認會加載 ~/.gotty.crt
和 ~/.gootty.key
密鑰文件,可以使用 --tls-crt
和 --tls-key
選項覆蓋這些文件路徑。
可使用以下命令生成簽名文件:
openssl req -x509 -nodes -days 9999 -newkey rsa:2048 -keyout ~/.gotty.key -out ~/.gotty.crt
為了提高安全性,可以通過 --tls-ca-crt
選項提供CA證書文件來使用 SSL / TLS 客戶端證書認證(此選項需要設置 -t
或 --tls
)。
此選項要求所有客戶端發送由指定證書頒發機構簽名的有效客戶端證書。
隨機 URL
除了身份驗證外,限制訪問服務器的另一種方法是使用隨機 URL。通過添加 -r
參數,GoTTY會生成一個隨機URL,以便只有知道該URL的人才能訪問該服務器。
❯ gotty -w -r -c root:777 bash
2021/04/12 16:47:56 Loading config file at: /home/sunls/.gotty
2021/04/12 16:47:56 Permitting clients to write input to the PTY.
2021/04/12 16:47:56 Using Basic Authentication
2021/04/12 16:47:56 Server is starting with command: bash
2021/04/12 16:47:56 URL: http://127.0.0.1:8080/63lzf3xi/
2021/04/12 16:47:56 URL: http://[::1]:8080/63lzf3xi/
2021/04/12 16:47:56 URL: http://10.45.90.86:8080/63lzf3xi/
2021/04/12 16:47:56 URL: http://[fe80::e09d:ae92:2a99:a974]:8080/63lzf3xi/
此處生成了一個隨機 URL 63lzf3xi
,通過 http://127.0.0.1:8080/63lzf3xi/
才可訪問到終端。
多個客戶端共享
當新的客戶端連接到服務器時,GoTTY將使用給定的命令啟動新進程。這意味着默認情況下,用戶無法與其他人共享一個終端。但是,可以使用終端多路復用器與多個客戶端共享一個進程。
例如,可以通過下面的命令啟動一個新的 gotty
以 top
命令命名的 tmux 會話。
❯ gotty tmux new -A -s gotty top
該命令不允許客戶端發送擊鍵,但是,可以從本地終端附加會話並運行諸如切換 top
命令模式之類的操作。要從終端連接到 tmux 會話,可以使用以下命令。
❯ tmux new -A -s gotty
三、Tmux 的簡單使用
安裝
# Arch Linux
$ sudo pacman -S tmux
# Ubuntu 或 Debian
$ sudo apt-get install tmux
# CentOS 或 Fedora
$ sudo yum install tmux
# Mac
$ brew install tmux
基本用法
新建一個無名稱的會話
$ tmux
上面的命令會啟動 一個Tmux 窗口,按下 Ctrl+d
或者輸入 exit
命令,就可以退出 Tmux 窗口。
$ exit
會話管理
新建會話
第一個啟動的 Tmux 窗口,編號是 0
,第二個窗口編號是 1
,以此類推。
更好的方法是為會話起名:
$ tmux new -s <session-name>
分離會話
在 Tmux 窗口下,按下 Ctrl+b d
或者輸入 tmux detach
命令,就會將當前會話與窗口分離。
$ tmux detach
tmux ls
命令可以查看當前所有的 Tmux 會話。
$ tmux ls
接入會話
tmux attach
命令用於重新接入某個已存在的會話。
# 使用會話編號
$ tmux attach -t 0
# 使用會話名稱
$ tmux attach -t <session-name>
殺死會話
tmux kill-session
命令用於殺死某個會話。
# 使用會話編號
$ tmux kill-session -t 0
# 使用會話名稱
$ tmux kill-session -t <session-name>
切換會話
tmux switch
命令用於切換會話。
# 使用會話編號
$ tmux switch -t 0
# 使用會話名稱
$ tmux switch -t <session-name>
重命名會話
tmux rename-session
命令用於重命名會話。
$ tmux rename-session -t 0 <new-name>
會話快捷鍵
下面是一些會話相關的快捷鍵。
Ctrl+b d:分離當前會話。
Ctrl+b s:列出所有會話。
Ctrl+b $:重命名當前會話。
四、GoTTY 搭配 Tmux 實現多會話共享
共享終端
將 GoTTY 指定為 tmux new
命令,當 web 訪問時 GoTTY 時會新建一個名稱為 gotty
的 tmux 窗口,-A
如果窗口已存在則會直接進入;-s
如果不存在則創建一個新的 tmux 窗口。
$ gotty -w tmux new -A -s gotty
實現效果:

指定 Tmux 的默認命令
Tmux 新建一個窗口默認是打開一個終端,我們可以在后面追加命令來指定打開 Tmux 窗口時所執行的命令。
$ gotty -w tmux new -A -s gotty mysql -uroot -p7410
添加 mysql -uroot -p7410
命令指定 Tmux 窗口為 MySQL 的控制台:
