in Linux

在AWS Ubuntu上安装和配置VNC并通过SSH隧道连接

在AWS Ubuntu上安装和配置tightvncserver, 并通过SSH隧道进行了远程桌面连接,记录下实验流程。

1,安装tightvncserver
由于已经安装了 xfce4 xfce4-goodies,所以此处不需要安装。以下指令安装tightvncserver。
sudo apt-get update
sudo apt-get install tightvncserver

2,设置vncserver的密码,登录使用
如果没有设置用户名和密码,需要设置,我在先前的实验中已经设置了用记名ubuntu和密码,所以我这里不需要设置。设置用户名密码具体参见:连接AWS EC2 Linux instance Ubuntu远程桌面

设置vncserver的密码,这里密码最长为8个字符,设置密码的同时,系统对vncserver时行了相应的设置。
ncserver

3,设置VNC服务器
主要是设置VNC服务器启动时的执行的指令,VNC开始启动时,自动启动一个服务器例程,使用显示端口5901,VNC识别为 :1,同理新开的端口为:2,:3,依此类推,端口使用为5900+x。
要配置5901设置,首先停止在端口5901上运行的例程,
vncserver -kill :1

配置 xstartup 文件,首先备份原始的文件以防以后用到,
mv ~/.vnc/xstartup ~/.vnc/xstartup.bak

创建一个新的xstartup 文件,
emacs ~/.vnc/xstartup

在文件中加入以下三行内容,
#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &

第一行指令xrdb $HOME/.Xresources告诉GUI的框架读取服务器的.Xresources文件,.Xresources是用户改变图形桌面设置,例如终端颜色,字符指针风格和字体等。
第二行指令告诉服务器启动XFCE,能够通过相应图形界面软件管理服务器。

修改为可执行,
sudo chmod +x ~/.vnc/xstartup

4,设置VNC服务文件
为了更好地控制VNC服务器,将其配置成Ubuntu的VCN服务,更加方便地进行start,stop, restart操作。
创建一个新的服务文件vncserver,
sudo emacs /etc/init.d/vncserver

第一部分是声明一些VNC会用到的通用的设置,例如用户名和分辨率等。
#!/bin/bash
PATH="$PATH:/usr/bin/"
export USER="ubuntu"
DISPLAY="1"
DEPTH="16"
GEOMETRY="1024x768"
OPTIONS="-depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY} -localhost"
. /lib/lsb/init-functions

这里使用ubuntu为用户名以及分辨率为1024×768,根据需要改动。

加入指令来管理新的服务,以下代码块使用start关键字开启VNC服务器,并返回服务正在开启,
case "$1" in
start)
log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
;;

以下代码块使用stop关键字关闭VNC服务器,并停止现有服务,
stop)
log_action_begin_msg "Stopping vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
;;

最后一个代码块使用restart关键字将先前的stop和start组合起来使用,
restart)
$0 stop
$0 start
;;
esac
exit 0

将文件设置为可执行,
sudo chmod +x /etc/init.d/vncserver

5,记得设置AWS Ubuntu的安全组
如果不使用隧道,就需要放行端口5901数据。

6,通过SSH隧道测试VNC连接

以下是关闭开启VNC服务,
ubuntu@ip-:~$ sudo service vncserver start
* Starting vncserver for user 'ubuntu' on localhost:1...
New 'X' desktop is ip-:1

Starting applications specified in /home/ubuntu/.vnc/xstartup
Log file is /home/ubuntu/.vnc/ip-:1.log

ubuntu@ip-:~$ sudo service vncserver stop
* Stopping vncserver for user 'ubuntu' on localhost:1... Killing Xtightvnc process ID 20143
ubuntu@ip-:~$

如下图所示:
UltraVNC_Viewer4

Putty设置,在Tennels下添加Source port为5901以及Destination为localhost:5901并点add,

Putty_tunnel

使用SSH连接AWS Ubuntu服务器,进行命令行,连接已建立。

开启UltraVNC Viewer, VNC Server设置为:localhost:5901, 点Connect,
UltraVNC_Viewer

会弹出对话框输入密码,先前设置的8个字符,如下图所示,
UltraVNC_Viewer1

进入远程AWS Ubuntu桌面如下图所示,
UltraVNC_Viewer2

这个VNC server很耗费资源,如下图所示,load average:3.06, 2.85, 2.49
UltraVNC_Viewer3

参考链接:
1,set_up_an_ssh_tunnel_with_putty
2,How to Install and Configure VNC on Ubuntu 14.04
3,How To Install VNC Server On Ubuntu 14.04
4, How to Install VNC Server on Ubuntu 14.04 LTS
5, How To Install And Configure VNC On Ubuntu 14.04

其它VNC链接:
VNC Reference link:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/ch-TigerVNC.html#proc-configuring-vncserver
http://www.uvnc.com/home.html
https://www.realvnc.com/products/open/4.1/winvncviewer.html
http://www.putorius.net/2015/04/how-to-install-vnc-server-in-red-hat-7.html

Write a Comment

Comment