操作環境:CentOS 5.3 + Windows XP SP3 32bit + RealVNC 4.1.2 i386 + TigerVNC.
參考:瀟湘隱者-Linux系統VNC配置實踐總結,薩米的博客-VNC配置,孤葉風鈴-Linux 開啟VNCSERVER,遠程桌面設置:利用putty進行vnc + ssh tunneling
VNC是基於RFB(Remote FrameBuffer)的一款開源的遠程桌面控制軟件。目前,原來的AT&T版本已經不再使用,因為更多有重大改善的分支版本已經出現,像是RealVNC,VNC tight 和UltraVNC。Real VNC 是當前最活躍和強大的主流應用。
一、安裝VNC Server
CentOS 5.3默認已經安裝vnc與vnc-server,對應的是realvnc的4.1.2版本。
對於沒有安裝vnc的Linux系統可以:
1、yum安裝,CentOS 5.3 yum源自帶了vnc與vnc-server;
[root@localhost ~]# yum install vnc [root@localhost ~]# yum install vnc-server
2、可以從realvnc官網下載,解壓歸檔后得到兩個rpm包,安裝vnc-server即可。
二、配置VNC Server
VNC Server的配置文件為 /etc/sysconfig/vncservers,在文件末尾添加以下兩句:
VNCSERVERS="2:hubery"
VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -nohttpd -localhost"
#桌面分辨率為800*600 ,阻止圖形桌面通過TCP端口 ,不能通過WEB訪問vncserver,不能通過不安全的方式從遠程登錄。
關於參數配置說明:
1:-geometry 表示桌面分辨率,默認為1024x768。
2:-nohttpd 表示不監聽HTTP端口(58xx)。
3:-nolisten tcp 表示不監聽TCP端口(60xx)
4:-localhost 只允許從本機訪問。
5:AlwaysShared 默認只允許一個VNCVIEWER連接,此參數表示同一個顯示端口允許多用戶同時登錄.
6:-depth 表示色深,參數有8,16,24,32.
7: SecurityTypes None 登錄不需要密碼認證VncAuth默認值,要密碼認證。
由於root用戶使用的是第一個VNC Server,我們添加Server是從2開始的,所以這里桌面號是2,用戶是hubery。
VNC使用的起始端口是5900和5800,桌面號是2時,VNC Viewer訪問的端口是5902,WEB方式(java)訪問的端口號是5802。
root用戶的配置也是這個文件,若要配置需要在文件末尾同樣加上類似以上兩句。
以上配置,vncserver將在服務啟動時打開vncserver :2。
三、防火牆配置
如果不熟悉防火牆iptables,可以直接關掉(重啟失效):
[root@localhost ~]# iptables -L
或
[root@localhost ~]# service iptables stop
但一般不建議這樣做,我們需要在防火牆里打開某些端口:
[root@localhost ~]# iptables -I INPUT -p tcp --dport 5901:5902 -j ACCEPT [root@localhost ~]# iptables -I INPUT -p udp --dport 5901:5902 -j ACCEPT
同樣,上述操作在計算機重啟之后也會失效,以下操作將配置保存到配置文件,使其永久生效:
[root@localhost ~]# service iptables save
四、啟動VNC Server
在啟動Server之前需要給遠程控制設置一個訪問密碼:
[root@localhost ~]# su hubery [hubery@localhost root]$ vncpasswd Password: Verify: [hubery@localhost root]$ vncserver :2 xauth: creating new authority file /home/hubery/.Xauthority New 'localhost:2 (hubery)' desktop is localhost:2 Creating default startup script /home/hubery/.vnc/xstartup Starting applications specified in /home/hubery/.vnc/xstartup Log file is /home/hubery/.vnc/localhost:2.log [hubery@localhost root]$
關閉某個vncserver:
vncserver -kill :2
五、VNC Viewer訪問
我的VNC客戶端是在Win XP下安裝的TigerVNC Viwer,可以從其官網免費下載。安裝VNC Viewer后以 Server_IP:桌面號 的形式訪問。
六、桌面配置(可選)
以上配置登陸之后得到的是twm桌面,看起來像一個終端,但它還是桌面,我們可以從命令行啟動桌面應用,例如firefox,但是一般人是用不習慣、不會去用它的,可以換其它的桌面嗎?當然可以,其配置文件為(用戶根目錄下)~/.vnc/xstartup,將twm修改為gnome-session或startkde即可切換成gnome或kde。
#!/bin/sh # Uncomment the following two lines for normal desktop: # unset SESSION_MANAGER # exec /etc/X11/xinit/xinitrc [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources xsetroot -solid grey vncconfig -iconic & xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" & # twm & gnome-session &
切換之后重啟服務是必須的:
[root@localhost .vnc]# service vncserver restart
或
[root@localhost .vnc]# su hubery [hubery@localhost .vnc]$ vncserver -kill :2 Killing Xvnc process ID 20353 [hubery@localhost .vnc]$ vncserver :2 New 'localhost:2 (hubery)' desktop is localhost:2 Starting applications specified in /home/hubery/.vnc/xstartup Log file is /home/hubery/.vnc/localhost:2.log [hubery@localhost .vnc]$
使用Viewer重新登陸,你會發現我們熟悉的桌面又回來了:
七、安全訪問VNC(可選)
如果直接使用vncviewer來進行訪問,有兩點不利因素:
1. 口令傳輸是明文,很容易被偵聽到.
2. 防火牆需要打開59xx端口,這在通常的單位里是不可能的.
幸運的是,我們有ssh這個強大的工具,象X11 Forwarding(另文論述),我們可以使用ssh隧道來保護通訊過程,下面就進行簡單介紹.
我依然使用Win XP下的Tiger VNC做客戶端,其實對於Linux下也是可以的(更簡單)。
1、在Session下配置Host Name為Server IP,Port為SSH端口22;
2、在Connection-》SSH-》Tunnels配置Source port為VNC Server端口號5902,Destination為localhost:5902,並Add添加;
3、使用TigerVNC Viewer訪問,地址現在是localhost:2。
至此,我們使用了加密的VNC,而且也不需要配置防火牆打開端口,也就是說使用ssh隧道在沒有步驟三的情況下也是有效的。