内容导读本篇说明在国产麒麟系统的服务器上如何开通远程图形桌面,便于 O2OA 平台部署、巡检与排障时进行可视化操作。流程是先确认系统架构与发行版本,安装远程桌面服务端与客户端组件,再按用户复制服务模板并修改启动参数,重载配置后启动并设为开机自启。注意非管理员账户启动失败常与脚本权限有关,需要放开执行权限,同时确认防火墙未拦截远程连接端口。
NeoKylin Linux Advanced Server release V7Update6 (Chromium)

查看操作系统信息
[root@ecs-f61c-0001 ~]# nkvers
############## NeoKylin Linux Version#################
Release:
NeoKylin Linux Advanced Server release V7Update6 (Chromium)
Kernel:
4.14.0-115.5.1.el7a.06.aarch64
Build:
NeoKylin Linux Advanced Server
release V7Update6/(Chromium)-aarch64
b06.lic/20190619
安装 vncserver
以 root 用户运行以下命令安装 vncserver
[root@ecs-f61c-0001 sysconfig]# yum install tigervnc-server
同样运行以下命令安装 vncviewer
[root@ecs-f61c-0001 sysconfig]# yum install vnc
停止并禁用防火墙,或开放 5901、5902 端口
[root@ecs-f61c-0001 sysconfig]# systemctl stop firewalld.service
[root@ecs-f61c-0001 sysconfig]# systemctl disable firewalld.service
VNC 配置和应用
vncserver 的配置:创建一个新的配置文件,以开启 1 号窗口为例(也可以同时开启多个窗口,修改数字即可),方法如下(以下命令复制了两个 vncserver 服务,即可同时允许 2 个桌面启动,多个桌面以此类推):
[root@ecs-f61c-0001 sysconfig]# cp /lib/systemd/system/vncserver@.service /lib/systemd/system/vncserver@:1.service
[root@ecs-f61c-0001 sysconfig]# cp /lib/systemd/system/vncserver@.service /lib/systemd/system/vncserver@:2.service
编辑 /lib/systemd/system/vncserver@:1.service,设置用户 root 相关参数,最终内容如下(如有多个服务,需全部查看,一般不需要修改):
# The vncserver service unit file
#
# Quick HowTo:
# 1. Copy this file to /etc/systemd/system/vncserver@.service
# 2. Replace <USER> with the actual user name and edit vncserver
# parameters appropriately
# (ExecStart=/usr/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
# PIDFile=/home/<USER>/.vnc/%H%i.pid)
# 3. Run `systemctl daemon-reload`
# 4. Run `systemctl enable vncserver@:<display>.service`
#
# DO NOT RUN THIS SERVICE if your local area network is
# untrusted! For a secure way of using VNC, you should
# limit connections to the local host and then tunnel from
# the machine you want to view VNC on (host A) to the machine
# whose VNC output you want to view (host B)
#
# [user@hostA ~]$ ssh -v -C -L 590N:localhost:590M hostB
#
# this will open a connection on port 590N of your hostA to hostB's port 590M
# (in fact, it ssh-connects to hostB and then connects to localhost (on hostB).
# See the ssh man page for details on port forwarding)
#
# You can then point a VNC client on hostA at vncdisplay N of localhost and with
# the help of ssh, you end up seeing what hostB makes available on port 590M
#
# Use "-nolisten tcp" to prevent X connections to your VNC server via TCP.
#
# Use "-localhost" to prevent remote VNC clients connecting except when
# doing so through a secure tunnel. See the "-via" option in the
# `man vncviewer' manual page.
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
更新 systemctl 以使其生效
[root@ecs-f61c-0001 sysconfig]# systemctl daemon-reload
设置 vncserver 的密码
[root@ecs-f61c-0001 sysconfig]# vncpasswd root
Password:
Verify:
Would you like to enter a view-only password (y/n)? y
Password:
Verify:
按提示输入密码并确认密码。
启动指定的桌面服务
[root@ecs-f61c-0001 sysconfig]# systemctl start vncserver@:1.service
或者
[root@ecs-f61c-0001 sysconfig]# vncserver :1
其他桌面类似
关闭指定的桌面服务
[root@ecs-f61c-0001 sysconfig]# systemctl stop vncserver@:1.service
或者
[root@ecs-f61c-0001 sysconfig]# vncserver -kill :1
设置为开机自动启动
[root@ecs-f61c-0001 sysconfig]# systemctl enable vncserver@:1.service
[root@ecs-f61c-0001 sysconfig]# systemctl enable vncserver@:2.service
测试 VNC 连接
安装 VNC 客户端
RealVNC 官网:https://www.realvnc.com/en/connect/download/viewer/
下载地址:https://www.realvnc.com/download/file/viewer.files/VNC-Viewer-6.20.529-Windows.exe

安装客户端(略)
启动客户端:

连接服务器桌面:

输入密码连接成功:

正常登录服务器并进行操作:

可能遇到的问题(CentOS 环境下)
1.设置为非 root 用户报错:
[root@ecs-f61c-0001 sysconfig]# systemctl status -l vncserver@:2.service
vncserver@:2.service - Remote desktop service (VNC)
Loaded: loaded (/etc/systemd/system/vncserver@:2.service; enabled; vendor preset:
disabled)
Active: failed (Result: exit-code) since 一 2016-04-25 19:07:23 CST; 7s ago
Process: 10305 ExecStart=/usr/sbin/runuser -l fang -c /usr/bin/vncserver %i (code=exited,
status=1/FAILURE)
Process: 10291 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || :
(code=exited, status=0/SUCCESS)
4月 25 19:07:23 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)...
4月 25 19:07:23 localhost.localdomain systemd[1]: vncserver@:2.service: control process
exited, code=exited status=1
4月 25 19:07:23 localhost.localdomain systemd[1]: Failed to start Remote desktop service
(VNC).
4月 25 19:07:23 localhost.localdomain systemd[1]: Unit vncserver@:2.service entered failed
state.
4月 25 19:07:23 localhost.localdomain systemd[1]: vncserver@:2.service failed.
2.Type=forking 报错,改为 Type=simple
[root@ecs-f61c-0001 sysconfig]# systemctl start vncserver@:2.service
Job for vncserver@:2.service failed because the control process exited with error code. See
"systemctl status vncserver@:2.service" and "journalctl -xe" for details.
[root@ecs-f61c-0001 sysconfig]# systemctl status -l vncserver@:2.service
● vncserver@:2.service - Remote desktop service (VNC)
Loaded: loaded (/etc/systemd/system/vncserver@:2.service; enabled; vendor preset:
disabled)
Active: failed (Result: exit-code) since 一 2016-04-25 19:09:41 CST; 6s ago
Process: 12367 ExecStart=/usr/sbin/runuser -l root -c /usr/bin/vncserver %i (code=exited,
status=98)
Process: 12354 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || :
(code=exited, status=0/SUCCESS)
4月 25 19:09:41 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)...
4月 25 19:09:41 localhost.localdomain systemd[1]: vncserver@:2.service: control process
exited, code=exited status=98
4月 25 19:09:41 localhost.localdomain systemd[1]: Failed to start Remote desktop service
(VNC).
4月 25 19:09:41 localhost.localdomain systemd[1]: Unit vncserver@:2.service entered failed
state.
4月 25 19:09:41 localhost.localdomain systemd[1]: vncserver@:2.service failed.
3.vnc viewer 客户端连接黑屏,改变 xstartup 的权限
这是一个容易被忽视的问题,也是连接成功后黑屏的原因之一。
在 CentOS 中,xstartup 的文件路径为:/root/.vnc/(可以通过 locate xstartup 查看该路径)
所以需要执行:
[root@ecs-f61c-0001 sysconfig]# chmod 777 /root/.vnc/xstartup
关闭防火墙
[root@ecs-f61c-0001 sysconfig]# systemctl stop firewalld.service #停止
[root@ecs-f61c-0001 sysconfig]# systemctl disable firewalld.service #禁用

兰德网络O2OA平台软件
开源、自主可控的协同办公底座
研发与运营
纵横翱途(杭州)科技有限公司
企业级协同应用 · 工作流引擎 · AI 智能体
www.o2oa.net | 400-888-0545