背景
在 Windows + ssh(Cygwin) + Linux(運行在虛擬機中的Ubuntu) 是一個很舒服的方案,但是偶爾需要用到 圖形界面。
如果需要通過ssh打開遠程服務器端的程序,需要X11 forwarding。否則,會顯示:
$ gedit
Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
(gedit:49652): Gtk-WARNING **: cannot open display:
經過搜索,我找到了有關的解決方案,這個方案適用於:
- Cygwin
- MobaXterm
- Putty
- 從Linux連接到Linux
ssh 服務器配置
這里以Ubuntu為例
ubuntu安裝ssh客戶端和服務器 並做相對於配置:
# 安裝ssh
sudo apt-get install openssh-server -y
# 配置允許X11轉發(設置 X11 有關服務)
cp /etc/ssh/sshd_config /tmp/sshd_config
## ssh 服務器添加配置
echo "X11Forwarding yes" >> /tmp/sshd_config
sudo cp /tmp/sshd_config /etc/ssh/sshd_config
# 讓配置生效
sudo service ssh restart
# 如果需要 這一台主機連接到別的地方也想運行GUI程序
## ssh 客戶端添加配置(ssh_config)
cp /etc/ssh/sshd_config /tmp/ssh_config
echo "ForwardAgent yes" >> /tmp/ssh_config
echo "ForwardX11 yes" >> /tmp/ssh_config
echo "ForwardX11Trusted yes" >> /tmp/ssh_config
sudo cp /tmp/ssh_config /etc/ssh/ssh_config
ssh 客戶端 的配置
Cygwin
Cygwin的方案是比較復雜的:
- 在Cygwin中新安裝3個包:
xinit、cygutils-x11、xterm - 啟動"XWin Server":建議是在啟動菜單中:
Cygwin-x-XWin server - 打開Cygwin,輸入:
export DISPLAY=:0.0 - 登錄:
ssh -Y ${loginName}@{ipAddress} [-p ${port=22}]
效果如圖:

另外一個 Linux
以Ubuntu為例,同上文,需要安裝 ssh客戶端
Ubuntu安裝ssh客戶端和服務器 並做相對於配置:
# 安裝ssh
sudo apt-get install openssh-server -y
## ssh 客戶端添加配置(ssh_config)
cp /etc/ssh/sshd_config /tmp/ssh_config
echo "ForwardAgent yes" >> /tmp/ssh_config
echo "ForwardX11 yes" >> /tmp/ssh_config
echo "ForwardX11Trusted yes" >> /tmp/ssh_config
sudo cp /tmp/ssh_config /etc/ssh/ssh_config
登錄:ssh -X ${loginName}@{ipAddress} [-p ${port=22}]
效果如圖:

Windows
以下的軟件為了支持圖形界面,需要,額外安裝有關服務:X Window 服務端 程序 XMing
Xming只是在windows下實現了一個X服務端,X客戶端還還是遠程程序本身,ssh起傳輸作用
運行XMing,運行后在托盤有圖標,提示信息為”Xming Server:0.0” 。
根據登錄軟件的不同,具體有一些設置差異。
Putty
Putty的設置很容易,只需要勾選這個設置即可:
Category
└── Connection
├── SSH
└── X11 - Enable X11 forwarding
效果如圖:

MobaXterm
MobaXterm的設置很容易,只需要勾選這個設置即可:
Session
└── SSH
└── Advanced SSH settings - X11-Forwarding
效果如圖:

