Centos8 安装VNC配置远程连接

介绍

  VNC:VNC(Virtual Network Computing),为一种使用RFB协议的屏幕画面分享及远程操作软件。此软件借由网络,可发送键盘与鼠标的动作及即时的屏幕画面。VNC与操作系统无关,因此可跨平台使用,例如可用Windows连线到某Linux的计算机,反之亦同。甚至在没有安装客户端程序的计算机中,只要有支持JAVA的浏览器,也可使用。VNC包含客户端和服务器两个操作软件。
  VNC Server:VNC服务器,接收VNC Viewer的连接请求,传输画面到客户端。
  VNC Viewer:VNC客户端,将键盘和鼠标的动作转递给服务器,

系统信息

[root@anolis ~]# cat /etc/redhat-release
Anolis OS release 8.6

安装桌面环境

  通常,服务器没有安装任何的桌面环境。如果你拥有的一台机器没有 GUI,第一步就是安装它。否则,跳过这个步骤。
  运行下面的命令来在你的远程机器上安装 Gnome,CentOS 8 下的默认桌面环境。

sudo dnf groupinstall "Server with GUI"

  下载和安装 Gnome 软件包,以及依赖软件包需要花费一些时间。

安装VNC服务器

  TigerVNC 是一个维护很活跃的开源高性能 VNC 服务器。它在默认的 CentOS 源仓库中可用,输入下面的命令,安装它:

dnf install -y tigervnc-server tigervnc-server-module

复制配置文件

cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

编辑 vim /etc/tigervnc/vncserver.users 添加用户

[root@centos8 ~]# vim /etc/tigervnc/vncserver.users
# TigerVNC User assignment
#
# This file assigns users to specific VNC display numbers.
# The syntax is <display>=<username>. E.g.:
#
# :2=andrew
# :3=lisa
:1=root

如果是给非root创建的话需要切换到该用户下产生vncpasswd

[root@centos8 ~]# su user
[user@centos8 ~]$ vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used
[user@centos8 ~]$ exit
exit

开启服务

[root@centos8 ~]# systemctl daemon-reload
[root@centos8 ~]# systemctl start vncserver@:1.service

测试是否VNCServer是否开起来

[root@centos8 ~]# nc -zv localhost 5901
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connected to ::1:5901.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.

关闭防火墙或开启端口权限

关闭防火墙(不推荐)

[root@centos8 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-06-21 22:40:18 CST; 14min ago
     Docs: man:firewalld(1)
 Main PID: 890 (firewalld)
    Tasks: 2 (limit: 11460)
   Memory: 29.7M
   CGroup: /system.slice/firewalld.service
           └─890 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

Jun 21 22:40:16 centos8 systemd[1]: Starting firewalld - dynamic firewall daemon...
Jun 21 22:40:18 centos8 systemd[1]: Started firewalld - dynamic firewall daemon.
[root@centos8 ~]# systemctl stop firewalld

开启端口权限

[root@centos8 ~]# firewall-cmd --zone=public --add-port=5901/tcp --permanent
success
[root@centos8 ~]# firewall-cmd --reload
success

安装Chrome

首先在CentOS/RHEL系统上安装wget才能用于下载,运行以下命令:

        sudo dnf -y install wget

    现在使用wget命令下载最新版本的Google Chrome:

        wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

    导航到包含下载包的目录并执行以下命令:

        $ sudo dnf localinstall google-chrome-stable_current_x86_64.rpm -y

    执行结果如下图:

    打开Chrome浏览器的快捷方式文件

        $ vi /usr/share/applications/google-chrome.desktop
    修改 Exec=/usr/bin/google-chrome-stable %U 在其后面加上 --no-sandbox

参考: