#!/bin/bash
PATH="$PATH:/usr/bin/"
export USER="mike"
DISPLAY="1"
DEPTH="16"
GEOMETRY="1024x768"
OPTIONS="-depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
. /lib/lsb/init-functions
case "$1" in
start)
log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
;;
stop)
log_action_begin_msg "Stoping vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
;;
restart)
$0 stop
$0 start
;;
esac
exit 0
保存文件并关闭它。 使其可执行
# chmod +x /etc/init.d/vncserver
开始服务
开始并测试我们的步骤。
首先重新加载systemctl,以便它可以使用vncserver启动脚本。
systemctl守护进程重新加载
现在启动vncserver。 它在端口5901上启动服务器
#service vncserver start
检查它的运行
root@linuxidc:~# service vncserver status
● vncserver.service
Loaded: loaded (/etc/init.d/vncserver; bad; vendor preset: enabled)
Active: active (exited) since Thu 2017-03-02 05:36:42 UTC; 6s ago
Docs: man:systemd-sysv-generator(8)
Process: 24877 ExecStart=/etc/init.d/vncserver start (code=exited, status=0/SUCCESS)
Mar 02 05:36:40 linuxidc systemd[1]: Starting vncserver.service...
Mar 02 05:36:40 linuxidc vncserver[24877]: * Starting vncserver for user 'vnc' on localhost:1...
Mar 02 05:36:40 linuxidc su[24885]: Successful su for vnc by root
Mar 02 05:36:40 linuxidc su[24885]: + ??? root:vnc
Mar 02 05:36:40 linuxidc su[24885]: pam_unix(su:session): session opened for user vnc by (uid=0)
Mar 02 05:36:42 linuxidc vncserver[24877]: New 'X' desktop is linuxidc:1
Mar 02 05:36:42 linuxidc vncserver[24877]: Starting applications specified in /home/vnc/.vnc/xstartup
Mar 02 05:36:42 linuxidc vncserver[24877]: Log file is /home/vnc/.vnc/linuxidc:1.log
Mar 02 05:36:42 linuxidc systemd[1]: Started vncserver.service.$ cat ~/.vnc/*.pid
18577
18731# ps -ef | grep tightvnc
vnc 24574 1 0 05:32 ? 00:00:00 Xtightvnc :1 -desktop X -auth /home/vnc/.Xauthority -geometry 1024x768 -depth 16 -rfbwait 120000 -rfbauth /home/vnc/.vnc/passwd -rfbport 5901 -fp /usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/share/fonts/X11/100dpi/ -co /etc/X11/rgb
root 24744 10412 0 05:33 pts/0 00:00:00 grep --color=auto tightvnc
root@linuxidc:~#
检查vnc服务器的打开端口。 从vnc客户端连接时,需要正确的端口号
# netstat -nlp | grep vnc
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 24574/Xtightvnc
tcp 0 0 0.0.0.0:6001 0.0.0.0:* LISTEN 24574/Xtightvnc
unix 2 [ ACC ] STREAM LISTENING 5225386 24574/Xtightvnc /tmp/.X11-unix/X1
Vnc server can also be started by calling the script directly.
也可以通过直接调用脚本来启动Vnc服务器。
# /etc/init.d/vncserver start
[ ok ] Starting vncserver (via systemctl): vncserver.service.
root@linuxidc:~#
停止vncserver
# service vncserver stop
在桌面上安装vncviewer客户端
现在,我们将vnc服务器启动并运行GUI桌面环境。
在Ubuntu上安装xtightvncviewer。
$ sudo apt-get install xtightvncviewer
现在使用vncviewer命令连接到远程vnc服务器。
$ vncviewer -quality 5 -encodings“copyrect tight hextile zlib corre rre raw”-compresslevel 5 IPADDR:5901
我们使用较低质量和压缩编码来压缩正在传输的图像数据并使其更快。
使用像KRDC这样的其他vnc查看器可能会更慢。