一般地,無論是AWS EC2還是阿里雲的雲主機,linux系統默認都只提供ssh登錄方式。如果你是一個技術控,非常希望把圖形化界面給折騰出來,這其中就不需有vnc server的支持,除此之外,還涉及到如vnc client如何和vnc server通信的問題。 有兩個方案:
1. 手工在安全組中開放更多的端口
2. 通過ssh隧道來橋接vnc client和server的通信鏈路
第一種方式非常容易理解,我們重點討論第2種,也就是在不開放更多端口情況下,僅使用ssh的22端口來實現vnc連接
具體地,需要以下的步驟
1. 創建好aws ec2或者阿里雲主機
2. 在windows客戶端上安裝Realvnc
3. ssh到ec2上執行以下命令(以ubutu為例子)安裝vncserver及其依賴的組件
sudo apt-get update sudo apt-get upgrade sudo apt-get install ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal xfce4 vnc4server
4.啟動vncserver,輸入vnc password
隨后編輯~/.vnc/xstartup文件並替換為以下內容:
#!/bin/sh # Uncomment the following two lines for normal desktop: unset SESSION_MANAGER # exec /etc/X11/xinit/xinitrc unset DBUS_SESSION_BUS_ADDRESS startxfce4 & [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources xsetroot -solid grey vncconfig -iconic & gnome-panel & gnome-settings-daemon & metacity & nautilus & gnome-terminal &
5. 重點來了,開啟ssh的端口forwarding
ssh -L 5902:localhost:5902 -i amazon.pem ubuntu@ec2–52–90–172–228.compute-1.amazonaws.com
再啟動vncserver
vncserver -geometry 1340x750
需要注意的是,1340x750是指的我的windows桌面次寸。隨后
6.使用Realvnc來連接localhost:5902並且輸入上面已經設置過的pasword.(there is a correlation between them (eg. display 1 = vnc port 5901, 6001 and display 2 = vnc port 5902 etc)