如何在 Linux 上使用 OpenSSH 多路複用器加速 OpenSSH 連線


本文將幫助我們瞭解透過建立主會話,然後使用多路複用器建立後續會話來多路複用 SSH 會話,從而加快 Linux 上 SSH 連線的速度。

多路複用

多路複用只不過是在單個 SSH 連線上傳送多個或更多資料,並且可以重用現有的 TCP/IP 連線用於多個併發 SSH 連線。這將減少在伺服器上建立新 TCP 連線的負載。

SSH 連線上多路複用器的優點。

  • 它使用現有的 *inx 套接字進行連線。
  • 它使用現有的 TCP/IP 連線,不再需要新的 TCP/IP。
  • 不再需要金鑰交換。
  • 無需身份驗證。

配置多路複用

如果您的主目錄中 .ssh 資料夾中不存在配置檔案,請以 600 許可權(僅您可讀寫)建立它。

編輯使用者的 .ssh/config 檔案 –

# vi ~/.ssh/config
ControlMaster auto
   ControlPath ~/.ssh/master-%r@%h:%p.socket
   ControlPersist 30m

示例配置檔案 –

Host *
   ControlPath ~/.ssh/master-%r@%h:%p
   ControlMaster auto
   ControlPersist 30m

說明

Host * or Host client : This start SSH configuration.
ControlPath ~/.ssh/ssh-mux-%r@%h:%p : This specifies the path to the control *inx socket to be used for connection and sharing is as described in the above.
The variables
‘%r’ - remote ssh username
‘%h’ - remote ssh host
‘%p’ - remote ssh port
You need to set all of these three variables for this option.

ControlMaster auto: This enables the sharing of multiple sessions over a single network connection. When this set to yes, the SSH will listen for connections on a control socket specified using the ControlPath argument.
When this set to auto, ssh will try to use a master connection, but the connection falls back to creating a new one connection, if one does not exist.
ControlPersist 30m: This option specifies that the master connection should remain open in the background for 30 minutes.
If the connection is with no clients, then the backgrounded master connection will automatically terminate after it had remained idle for 30 minutes.

如何連線

從客戶端機器使用以下命令連線到伺服器。

#ssh root@192.168.2.225
The authenticity of host '192.168.2.225 (192.168.2.225)' can't be established.
RSA key fingerprint is f7:c8:62:c9:6f:02:50:8e:14:cd:3a:95:ad:b1:67:af.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.225' (RSA) to the list of known hosts.
root@192.168.2.225's password:
Last login: Fri Apr 22 13:26:56 2016 from 192.168.1.84

如何驗證多路複用器是否正在工作?

轉到 SSH 伺服器並執行以下命令。

# lsof -U | grep master
ssh       69518       root       4u       unix0xffff8801378f7580    0t0 607468 /root/.ssh/master-root@192.168.2.225
ssh       69518       root       5u       unix0xffff880139986b80    0t0 607482 /root/.ssh/master root@192.168.2.225

我們可以使用另一個命令檢查

# ssh -O stop 192.168.2.225
Master running (pid=69518)
#

如果我們使用許多終端或指令碼透過 OpenSSH 連線到同一伺服器,則可以使用多路複用來加快它們的速度,這使得第一個連線成為主連線,並允許其他連線共享其到伺服器的 TCP 連線。

更新於: 2020年1月20日

368 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.