2016年3月28日 星期一

VNC Viewer 使用 SSH 的『中繼』設定

VNC 是 Linux 系統中常用的「遠端桌面」連線工具,但是傳輸時的安全性低,所以一般使用時多會配合 SSH 建立通道,透過此加密通道傳輸「遠端桌面」的資料。參考前面的發文:
連線時先用 SSH 建立重導之通道,再用 VNC Vierwer 建立 VNC 連線。
  之前看過 VNC Viewer 的說明有提到一個參數『 -via <gateway>』,似乎可以同時下 VNC 及 SSH 連線命令。經過一般折騰,終於成功,把過程記錄如下:

一、關於 VNC Viewer 的『-via <gateway>』的線索

先從  VNC Viewer 的命令列說明查起,
[SiB@Celeron-D ~]$ vncviewer -h

TigerVNC Viewer 64-bit v1.5.0
Built on: 2015-08-21 09:59
Copyright (C) 1999-2015 TigerVNC Team and many others (see README.txt)
See http://www.tigervnc.org for information on TigerVNC.
  ‧‧‧‧‧
Global Parameters:
  ‧‧‧‧‧
    via            - Gateway to tunnel via (default=)
  ‧‧‧‧‧
看結果應該是沒錯,再用『man vncviewer』查詢進一步的說明。
  ‧‧‧‧‧
-via gateway
Automatically create encrypted TCP tunnel to the gateway machine before connection, connect to the host through that tunnel (TigerVNC-specific).  By default, this option invokes SSH local port forwarding, assuming that SSH client binary can be accessed as /usr/bin/ssh.  Note that when using the -via option, the host machine name should be specified as known to the gateway machine, e.g. "localhost" denotes the gateway, not the machine where vncviewer was launched.  The environment variable VNC_VIA_CMD can override the default tunnel command of /usr/bin/ssh -f -L "$L":"$H":"$R" "$G" sleep 20.    The tunnel command is executed with the environment variables L, H, R, and G taken the values of the local port number, the remote host, the port number on the remote host, and the gateway machine respectively.
  ‧‧‧‧‧
由上面的結果看來,『VNC_VIA_CMD』這個環境變數應該就是關鍵。

二、設定環境變數『VNC_VIA_CMD』

根據查得的說明,有變動過 SSH 的連接埠的埠號,或登入的帳號名稱不同時,一定要使用『VNC_VIA_CMD』環境變數。設定的例子如下:
[SiB@Celeron-D ~]$ export VNC_VIA_CMD='/usr/bin/ssh -p 5566 -l SiB -f -L %L:%H:%R %G sleep 10'
上例命令中之各項參數說明如下表
參 數說  明
export設定『環境變數』的命令
VNC_VIA_CMD=' . . . 'VNC Viewer 的『-via』參數所參照之『環境變數』名稱
-p 5566建立 SSH 連線所使用的埠號
-l SiB建立 SSH 連線所使用的帳號名稱
-f將 SSH 連線置於背景執行,畢竟要用的是 VNC
-L %L:%H:%R %G建立 SSH 連線時,同時開啟的『加密通道』,其中:
%L:『加密通道』本地入口埠號
%H:『加密通道』遠端出口轉接之網路位址
%R:『加密通道』遠端出口轉接之埠號
%G:『加密通道』遠端出口所在電腦之網路位址,即執行 SSH 服務的電腦。
:以上參數,在 VNC Viewer 執行時會填入,照著填上即可。
sleep 10尚未查到相關說明,省略則在下階段無法建立 VNC 連線。判斷應該 SSH 和 VNC Viewer 兩個命令執行的間隔之用,確保先建立妥 SSH 連線,再建立 VNC 連線。
-2強制使用第二版的協定
-4強制使用 IPv4 網路位址
-x關閉 X11 的轉發
上表中最後三項參數,不一定需要,但似乎可以加快一點點速度,使用者自己斟酌。

三、執行 VNC Viewer

在步驟二設定『環境變數』後,同一個「終端機」接著輸入 VNC Viewer 命令,
[SiB@Celeron-D ~]$ vncviewer -via 192.168.10.17 127.0.0.1:1877
上例中的各項參數說明如下表:
參 數說  明
vncviewer啟動 VNC Viewer,建立 VNC 連線。
-via 192.168.10.17令 VNC Viewer 建立 VNC 連線前,先以建立 SSH 連線至 192.168.10.17
127.0.0.1:1877透過 SSH 通道,建立 VNC 連線。其中:
127.0.0.1 指 VNC 服務與 SSH 『加密通道』出口在同一部電腦。若該網段是安全的,也可以轉發至其它電腦,只要將 IP 位址更換即可。
1877 是 VNC 服務的埠號。

注意:填入的數字是實際的 VNC 埠號減去 5900 (如本例之實際 VNC 埠號為 7777,而填入的數字為 7777 - 5900 = 1877)。


已測試版本:

  • Fedora: 21, 23

參考資料:

沒有留言:

張貼留言

感謝你耐心看完本文,歡迎留下任何指正、建議,筆者會儘快回應。(English is also welcome.)