2013年8月24日 星期六

Secure Shell 的 X11 Forwarding

SSH (Secure Shell) 的 X11 Forwarding 提供了一個很方便的功能,使用者可以從 A 電腦經由 SSH 連線到執行 Linux 的 B 電腦,然後利用 X11 Forwarding 將 B 電腦上的應用程式 GUI 畫面傳到 A 電腦。這是因為 X 視窗採用了 X server 及 X client 的架構。也就是,當我們在 A 電腦執行了 X server,而在 B 電腦執行 X client 時,X client 的 UI 畫面就會在 A 電腦上呈現了。

以 PuTTY 為例,在建立 SSH 連線時,先在選項設定裡將 X11 Forwarding 啟用就可以了。

這樣,就能將遠端執行的程式畫面傳回來了。例如,我在遠端執行了 virt-manager 這支程式,但它的畫面會顯示在執行 PuTTY 程式的這一端 (本地端)。

這裡,我的本地端使用的是 Linux Mint 這個系統,遠端則是 Debian GNU/Linux 系統。所以,只要單純在建立 SSH 連線啟用 X11 Forwarding,就可以在 Debian GNU/Linux 上執行任何視窗程式,畫面也會顯示在 Linux Mint 這端。

那麼,如果我不用 PuTTY 這個工具程式呢?在 Linux 裡有一個 ssh 指令,怎麼用它來使用 X11 Forwarding 呢?首先來看一下 ssh 這支工具程式的 manpage:

-X      Enables X11 forwarding.  This can also be specified on a per-host
        basis in a configuration file.

        X11 forwarding should be enabled with caution.  Users with the
        ability to bypass file permissions on the remote host (for the
        user's X authorization database) can access the local X11 display
        through the forwarded connection.  An attacker may then be able
        to perform activities such as keystroke monitoring.

        For this reason, X11 forwarding is subjected to X11 SECURITY
        extension restrictions by default.  Please refer to the ssh -Y
        option and the ForwardX11Trusted directive in ssh_config(5) for
        more information.

-Y      Enables trusted X11 forwarding.  Trusted X11 forwardings are not
        subjected to the X11 SECURITY extension controls.

這很容易使用,在終端程式使用 ssh 指令建立與遠端的 SSH 連線時,只要執行下面的指令即可 (這裡假設遠端的 IP 為 192.168.1.101):

$ ssh -l 你的帳號 -X 192.168.1.101

$ ssh -l 你的帳號 -Y 192.168.1.101

那,如果我的本地端是 Windows,怎麼辦呢?在之前,我介紹過 Xming 這個軟體,在 Windows 端安裝 Xming 後,再用 PuTTY 登入遠端的 Linux 就可以了。

在 Windows 可以使用的 X server 軟體,除了 Xming 之後,還有需要付費購買授權的 X-Deep/32X-Win32Xmanager 等。

而支援 SSH 的工具軟體,除了 PuTTY 之外,也還有需要付費購買授權的 SecureCRTXshell 等。其中,Xshell 提供了 Free for Home/School 的選擇。此外,SecureCRT 和 Xshell 更支援了 Zmodem 這個傳輸協定,相當方便。

例如下圖就是在 Windows 8.1 裡,使用 Xshell 這個工具程式登入遠端的 Debian GNU/Linux,然後將執行的 virt-manager 畫面傳回到 Windows 8.1 端。

沒有留言: