CentOS 7部署vncserver,通过vncviewer远程查看图形界面

一、简介

> VNC (Virtual Network Console)是虚拟网络控制台的缩写。它 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的。VNC 是在基于 UNIX 和 Linux 操作系统的免费的开源软件,远程控制能力强大,高效实用,其性能可以和 Windows 和 MAC 中的任何远程控制软件媲美。 在 Linux 中,VNC 包括以下四个命令:vncserver,vncviewer,vncpasswd,和 vncconnect。大多数情况下用户只需要其中的两个命令:vncserver 和 vncviewer。

二、前期准备

> 系统环境:CentOS 7、Windows(用于测试远程连接,版本不做要求)
> 软件环境:VNC Viewer(Windows版),VNC Server(用yum安装即可)

三、Linux 安装部署VNC Server

1、安装图形化界面插件

[root@micle ~]:yum groupinstall "GNOME Desktop" "Graphical Administration Tools" //过程有点慢,耐心等待
...
//此处省略好多行代码...
...
Dependency Updated:
  NetworkManager.x86_64 1:1.18.0-5.el7_7.2            NetworkManager-libnm.x86_64 1:1.18.0-5.el7_7.2              NetworkManager-team.x86_64 1:1.18.0-5.el7_7.2   NetworkManager-tui.x86_64 1:1.18.0-5.el7_7.2 
  NetworkManager-wifi.x86_64 1:1.18.0-5.el7_7.2       alsa-lib.x86_64 0:1.1.8-1.el7                               audit.x86_64 0:2.8.5-4.el7                      audit-libs.x86_64 0:2.8.5-4.el7
  bind-libs-lite.x86_64 32:9.11.4-9.P2.el7            bind-license.noarch 32:9.11.4-9.P2.el7                      cryptsetup-libs.x86_64 0:2.0.3-5.el7            cups-client.x86_64 1:1.6.3-40.el7
  cups-libs.x86_64 1:1.6.3-40.el7                     device-mapper.x86_64 7:1.02.158-2.el7_7.2                   device-mapper-libs.x86_64 7:1.02.158-2.el7_7.2  dhclient.x86_64 12:4.2.5-77.el7.centos
  dhcp-common.x86_64 12:4.2.5-77.el7.centos           dhcp-libs.x86_64 12:4.2.5-77.el7.centos                     elfutils-libelf.x86_64 0:0.176-2.el7            elfutils-libs.x86_64 0:0.176-2.el7
  firewalld.noarch 0:0.6.3-2.el7_7.3                  firewalld-filesystem.noarch 0:0.6.3-2.el7_7.3               kpartx.x86_64 0:0.4.9-127.el7                   krb5-libs.x86_64 0:1.15.1-37.el7_7.2
  linux-firmware.noarch 0:20190429-72.gitddde598.el7  nspr.x86_64 0:4.21.0-1.el7                                  nss.x86_64 0:3.44.0-7.el7_7                     nss-softokn.x86_64 0:3.44.0-8.el7_7
  nss-softokn-freebl.x86_64 0:3.44.0-8.el7_7          nss-sysinit.x86_64 0:3.44.0-7.el7_7                         nss-tools.x86_64 0:3.44.0-7.el7_7               nss-util.x86_64 0:3.44.0-4.el7_7
  plymouth.x86_64 0:0.8.9-0.32.20140113.el7.centos    plymouth-core-libs.x86_64 0:0.8.9-0.32.20140113.el7.centos  policycoreutils.x86_64 0:2.5-33.el7             python-firewall.noarch 0:0.6.3-2.el7_7.3
  systemd.x86_64 0:219-67.el7_7.3                     systemd-libs.x86_64 0:219-67.el7_7.3                        systemd-sysv.x86_64 0:219-67.el7_7.3

Failed:
  python-urllib3.noarch 0:1.10.2-7.el7

Complete!
[root@iZ2zef2r37ijinyvlpu77lZ ~]# //以上信息说明"GNOME Desktop" "Graphical Administration Tools" 安装完毕

2、登陆Linux系统(root用户)安装配置VNC Server

[root@micle ~]:yum list | grep tigervnc //检测可以下载哪些vncserver
tigervnc.x86_64                           1.8.0-17.el7                 base
tigervnc-icons.noarch                     1.8.0-17.el7                 base
tigervnc-license.noarch                   1.8.0-17.el7                 base
tigervnc-server.x86_64                    1.8.0-17.el7                 base
tigervnc-server-applet.noarch             1.8.0-17.el7                 base
tigervnc-server-minimal.x86_64            1.8.0-17.el7                 base
tigervnc-server-module.x86_64             1.8.0-17.el7                 base
[root@micle ~]# yum -y install tigervnc-server //开始安装
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package tigervnc-server.x86_64 0:1.8.0-17.el7 will be installed
--> Processing Dependency: xorg-x11-xinit for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: xorg-x11-xauth for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: tigervnc-server-minimal for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libgnutls.so.28(GNUTLS_2_12)(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libgnutls.so.28(GNUTLS_1_4)(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libjpeg.so.62()(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libgnutls.so.28()(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libXtst.so.6()(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libXext.so.6()(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libXdamage.so.1()(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libX11.so.6()(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libSM.so.6()(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Processing Dependency: libICE.so.6()(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64
--> Running transaction check
---> Package gnutls.x86_64 0:3.3.29-9.el7_6 will be installed
--> Processing Dependency: trousers >= 0.3.11.2 for package: gnutls-3.3.29-9.el7_6.x86_64
--> Processing Dependency: libnettle.so.4()(64bit) for package: gnutls-3.3.29-9.el7_6.x86_64
//代码太多,此处省略...
Installed:
  tigervnc-server.x86_64 0:1.8.0-17.el7

Dependency Installed:
  gnutls.x86_64 0:3.3.29-9.el7_6                        libICE.x86_64 0:1.0.9-9.el7                      libSM.x86_64 0:1.2.2-2.el7                         libX11.x86_64 0:1.6.7-2.el7
  libX11-common.noarch 0:1.6.7-2.el7                    libXau.x86_64 0:1.0.8-2.1.el7                    libXcursor.x86_64 0:1.1.15-1.el7                   libXdamage.x86_64 0:1.1.4-4.1.el7
  libXdmcp.x86_64 0:1.1.2-6.el7                         libXext.x86_64 0:1.3.3-3.el7                     libXfixes.x86_64 0:5.0.3-1.el7                     libXfont2.x86_64 0:2.0.3-1.el7
  libXi.x86_64 0:1.7.9-1.el7                            libXinerama.x86_64 0:1.1.3-2.1.el7               libXmu.x86_64 0:1.1.2-2.el7                        libXrandr.x86_64 0:1.5.1-2.el7
  libXrender.x86_64 0:0.9.10-1.el7                      libXt.x86_64 0:1.1.5-3.el7                       libXtst.x86_64 0:1.2.3-1.el7                       libXxf86misc.x86_64 0:1.0.3-7.1.el7
  libXxf86vm.x86_64 0:1.1.4-1.el7                       libfontenc.x86_64 0:1.1.3-3.el7                  libglvnd.x86_64 1:1.0.1-0.8.git5baa1e5.el7         libglvnd-glx.x86_64 1:1.0.1-0.8.git5baa1e5.el7
  libjpeg-turbo.x86_64 0:1.2.90-8.el7                   libxcb.x86_64 0:1.13-1.el7                       libxkbfile.x86_64 0:1.0.9-3.el7                    libxshmfence.x86_64 0:1.2-1.el7
  llvm-private.x86_64 0:7.0.1-1.el7                     mesa-dri-drivers.x86_64 0:18.3.4-6.el7_7         mesa-filesystem.x86_64 0:18.3.4-6.el7_7            mesa-libGL.x86_64 0:18.3.4-6.el7_7
  mesa-libglapi.x86_64 0:18.3.4-6.el7_7                 nettle.x86_64 0:2.7.1-8.el7                      pixman.x86_64 0:0.34.0-1.el7                       tigervnc-license.noarch 0:1.8.0-17.el7
  tigervnc-server-minimal.x86_64 0:1.8.0-17.el7         trousers.x86_64 0:0.3.14-2.el7                   xkeyboard-config.noarch 0:2.24-1.el7               xorg-x11-server-utils.x86_64 0:7.7-20.el7
  xorg-x11-xauth.x86_64 1:1.0.9-1.el7                   xorg-x11-xinit.x86_64 0:1.3.4-2.el7              xorg-x11-xkb-utils.x86_64 0:7.7-14.el7

Complete!
//到此vncserver安装完毕,接下来开始配置vncserver

3、配置VNC Server

[root@micle ~]# vim /lib/systemd/system/vncserver@.service //编辑该文件,将<USER> 替换为root,保存并退出编辑
[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"
ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"
# PIDFile=/home/<USER>/.vnc/%H%i.pid
PIDFile=/root/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

//下面这一步,网上很多博客建议是件/lib/systemd/system/vncserver@.service 重命名为/lib/systemd/system/vncserver@:1.service
//这里我不太建议,因为按此命名会出现一个异常:localhost.localdomain systemd[1]: PID file /root/.vnc/localhost.localdomain:1.pid not readable (yet?) after start.
//检查 /root/.vnc/,发现没有localhost.localdomain:1.pid
//他们给的解释是:
//原来目录/tmp/下有一个/.X11-unix目录会占用这个pid序号资源。
//把这个目录改名/.X11-unix.bak后,再次执行
//#vncserver,系统正常生成了1.pid。

//然并卵,因此为了避免这个问题,个人建议设置为2,亲测有效

[root@micle ~]# mv /lib/systemd/system/vncserver@.service /lib/systemd/system/vncserver@:2.service
[root@micle ~]# systemctl daemon-reload //重启systemd
[root@micle ~]# vncpasswd //按界面要求设置vncserver服务密码,一定记住该密码,在远程连接时会用到
Password:
Verify:
Would you like to enter a view-only password (y/n)? y
Password:
Verify:
[root@micle ~]# systemctl enable vncserver@:2.service       //设置开机启动,这不根据个人需要开启,建议开启
Created symlink from /etc/systemd/system/multi-user.target.wants/vncserver@:2.service to /usr/lib/systemd/system/vncserver@:2.service.
[root@micle ~]# systemctl start vncserver@:2.service       //开启vncserver服务
[root@micle ~]# ps -ef | grep vnc               //看到有5902端口服务,说明vncserver服务运行起来了
root      1518     1  0 16:10 ?        00:00:00 /usr/bin/Xvnc :2 -auth /root/.Xauthority -desktop iZ2zef2r37ijinyvlpu77lZ:2 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1024x768 -pn -rfbauth /root/.vnc/passwd -rfbport 5902 -rfbwait 30000
root      1538  1236  0 16:10 pts/1    00:00:00 grep --color=auto vnc
[root@micle ~]#

4、将vncserver服务端口加入防火墙,具体参考[《CentOS 6、CentOS 7系统设置防火墙及区别》](https://www.cnblogs.com/MicleZhou2020/p/12463877.html "《CentOS 6、CentOS 7系统设置防火墙及区别》")

四、安装VNC Viewer(Windows)

1、进入官网下载vncviewer安装程序,官网地址:

[https://www.realvnc.com/de/connect/download/viewer/](https://www.realvnc.com/de/connect/download/viewer/ "https://www.realvnc.com/de/connect/download/viewer/")

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229170519783-1766974343.png)

2、双击安装程序,默认往下安装

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175136401-169730386.png)

3、选择语言,因为我下载的英文版,所以选择“English”

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175249059-1922573000.png)

4、继续往下走

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175318914-1321516768.png)

5、接受协议继续往下

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175355080-38677798.png)

6、选择安装目录,默认即可

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175420298-2118412695.png)

7、开始安装

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175437523-1640111003.png)

8、完成

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175459516-647488543.png)

9、在开始打开VNC Viewer,只需输入ip、端口即可。端口就是在安装vncserver是开启的5902,也可以用2,连接名随意输入,保存

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175521724-2136532475.png)

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175744820-788896890.png)

10、双击刚才新建的连接,出现如下画面说明服务能够正常连接,如果出现链接超时提示,确认下服务名、IP、端口是否正确

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175815527-198698252.png)

11、输入此前安装vncseerver时设置的密码

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229175957391-1143384194.png)

12、接下来就是见证奇迹的时刻了,

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229180041513-51879389.png)

![](https://img2018.cnblogs.com/blog/1920103/202002/1920103-20200229180049498-638748576.png)

至此,vncserver、vncviewer 服务端和客户端都安装完毕,进入你的Liunx世界吧!!!

声明:该文章系转载,转载该文章的目的在于更广泛的传递信息,并不代表本网站赞同其观点,文章内容仅供参考。

本站是一个个人学习和交流平台,网站上部分文章为网站管理员和网友从相关媒体转载而来,并不用于任何商业目的,内容为作者个人观点, 并不代表本网站赞同其观点和对其真实性负责。

我们已经尽可能的对作者和来源进行了通告,但是可能由于能力有限或疏忽,导致作者和来源有误,亦可能您并不期望您的作品在我们的网站上发布。我们为这些问题向您致歉,如果您在我站上发现此类问题,请及时联系我们,我们将根据您的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。