如何在 Centos 8 / RHEL 8 上安装和配置 VNC 服务器
VNC
![](/data/attachment/album/201910/14/120057n9t849m3uzswhkg8.jpg)


VNC（<ruby> 虚拟网络计算 <rt>  Virtual Network Computing </rt></ruby>）服务器是基于 GUI 的桌面共享平台，它可让你访问远程桌面计算机。在 Centos 8 和 RHEL 8 系统中，默认未安装 VNC 服务器，它需要手动安装。在本文中，我们将通过简单的分步指南，介绍如何在 Centos 8 / RHEL 8 上安装 VNC 服务器。


### 在 Centos 8 / RHEL 8 上安装 VNC 服务器的先决要求


要在你的系统中安装 VNC 服务器，请确保你的系统满足以下要求：


* CentOS 8 / RHEL 8
* GNOME 桌面环境
* root 用户权限
* DNF / YUM 软件包仓库


### 在 Centos 8 / RHEL 8 上安装 VNC 服务器的分步指导


#### 步骤 1）安装 GNOME 桌面环境


在 CentOS 8 / RHEL 8 中安装 VNC 服务器之前，请确保已安装了桌面环境（DE）。如果已经安装了 GNOME 桌面或安装了 GUI 支持，那么可以跳过此步骤。


在 CentOS 8 / RHEL 8 中，GNOME 是默认的桌面环境。如果你的系统中没有它，请使用以下命令进行安装：



```
[root@linuxtechi ~]# dnf groupinstall "workstation"
或者
[root@linuxtechi ~]# dnf groupinstall "Server with GUI
```

成功安装上面的包后，请运行以下命令启用图形模式：



```
[root@linuxtechi ~]# systemctl set-default graphical
```

现在重启系统，进入 GNOME 登录页面（LCTT 译注：你可以通过切换运行态来进入图形界面）。



```
[root@linuxtechi ~]# reboot
```

重启后，请取消注释 `/etc/gdm/custom.conf` 中的 `WaylandEnable=false`，以使通过 vnc 进行的远程桌面会话请求由 GNOME 桌面的 xorg 处理，来代替 Wayland 显示管理器。


注意： Wayland 是 GNOME 中的默认显示管理器 （GDM），并且未配置用于处理 X.org 等远程渲染的 API。


#### 步骤 2）安装 VNC 服务器（tigervnc-server）


接下来，我们将安装 VNC 服务器，有很多 VNC 服务器可以选择，出于安装目的，我们将安装 `TigerVNC 服务器`。它是最受欢迎的 VNC 服务器之一，并且高性能还独立于平台，它使用户可以轻松地与远程计算机进行交互。


现在，使用以下命令安装 TigerVNC 服务器：



```
[root@linuxtechi ~]# dnf install tigervnc-server tigervnc-server-module -y
```

#### 步骤 3）为本地用户设置 VNC 密码


假设我们希望用户 `pkumar` 使用 VNC 进行远程桌面会话，然后切换到该用户并使用 `vncpasswd` 命令设置其密码，



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

#### 步骤 4）设置 VNC 服务器配置文件


下一步是配置 VNC 服务器配置文件。创建含以下内容的 `/etc/systemd/system/vncserver@.service`，以便为上面的本地用户 `pkumar` 启动 tigervnc-server 的服务。



```
[root@linuxtechi ~]# vim /etc/systemd/system/vncserver@.service
[Unit]
Description=Remote Desktop VNC Service
After=syslog.target network.target

[Service]
Type=forking
WorkingDirectory=/home/pkumar
User=pkumar
Group=pkumar

ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/vncserver -autokill %i
ExecStop=/usr/bin/vncserver -kill %i

[Install]
WantedBy=multi-user.target
```

保存并退出文件，


注意：替换上面文件中的用户名为你自己的。


默认情况下，VNC 服务器在 tcp 端口 5900+n 上监听，其中 n 是显示端口号，如果显示端口号为 “1”，那么 VNC 服务器将在 TCP 端口 5901 上监听其请求。


#### 步骤 5）启动 VNC 服务并允许防火墙中的端口


我将显示端口号设置为 1，因此请使用以下命令在显示端口号 “1” 上启动并启用 vnc 服务，



```
[root@linuxtechi ~]# systemctl daemon-reload
[root@linuxtechi ~]# systemctl start vncserver@:1.service
[root@linuxtechi ~]# systemctl enable vncserver@:1.service
Created symlink /etc/systemd/system/multi-user.target.wants/vncserver@:1.service → /etc/systemd/system/vncserver@.service.
[root@linuxtechi ~]#
```

使用下面的 `netstat` 或 `ss` 命令来验证 VNC 服务器是否开始监听 5901 上的请求，



```
[root@linuxtechi ~]# netstat -tunlp | grep 5901
tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN      8169/Xvnc
tcp6       0      0 :::5901                 :::*                    LISTEN      8169/Xvnc
[root@linuxtechi ~]# ss -tunlp | grep -i 5901
tcp   LISTEN  0       5                    0.0.0.0:5901           0.0.0.0:*      users:(("Xvnc",pid=8169,fd=6))                    
tcp   LISTEN  0       5                       [::]:5901              [::]:*      users:(("Xvnc",pid=8169,fd=7))                    
[root@linuxtechi ~]#
```

使用下面的 `systemctl` 命令验证 VNC 服务器的状态，



```
[root@linuxtechi ~]# systemctl status vncserver@:1.service
```

![vncserver-status-centos8-rhel8](/data/attachment/album/201910/14/120203mn7yuodtyww45r55.jpg)


上面命令的输出确认在 tcp 端口 5901 上成功启动了 VNC。使用以下命令在系统防火墙中允许 VNC 服务器端口 “5901”，



```
[root@linuxtechi ~]# firewall-cmd --permanent --add-port=5901/tcp
success
[root@linuxtechi ~]# firewall-cmd --reload
success
[root@linuxtechi ~]#
```

#### 步骤 6）连接到远程桌面会话


现在，我们已经准备就绪，可以查看远程桌面连接是否正常工作。要访问远程桌面，请在 Windows / Linux 工作站中启动 VNC Viewer，然后输入 VNC 服务器的 IP 地址和端口号，然后按回车。


![VNC-Viewer-Windows10](/data/attachment/album/201910/14/120203amvu9lpzepdrvr2m.jpg)


接下来，它将询问你的 VNC 密码。输入你先前为本地用户创建的密码，然后单击 “OK” 继续。


![VNC-Viewer-Connect-CentOS8-RHEL8-VNC-Server](/data/attachment/album/201910/14/120205jnkuqnxfnjpjqj5y.jpg)


现在你可以看到远程桌面，


![VNC-Desktop-Screen-CentOS8](/data/attachment/album/201910/14/120205dj2bncobbr6brj7n.jpg)


就是这样，你已经在 Centos 8 / RHEL 8 中成功安装了 VNC 服务器。


### 总结


希望这篇在 Centos 8 / RHEL 8 上安装 VNC 服务器的分步指南为你提供了轻松设置 VNC 服务器并访问远程桌面的所有信息。请在下面的评论栏中提供你的意见和建议。下篇文章再见。谢谢再见！！！




---


via: <https://www.linuxtechi.com/install-configure-vnc-server-centos8-rhel8/>


作者：[Pradeep Kumar](https://www.linuxtechi.com/author/pradeep/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
A **VNC** (Virtual Network Computing) Server is a GUI based desktop sharing platform that allows you to access remote desktop machines. In **Centos 8** and **RHEL 8** systems, VNC servers are not installed by default and need to be installed manually. In this article, we’ll look at how to install VNC Server on CentOS 8 / RHEL 8 systems with a simple step-by-step installation guide.

#### Prerequisites to Install VNC Server on Centos 8 / RHEL 8

To install VNC Server in your system, make sure you have the following requirements readily available on your system:

- CentOS 8 / RHEL 8
- GNOME Desktop Environment
- Root access
- DNF / YUM Package repositories

#### Step by Step Guide to Install VNC Server on Centos 8 / RHEL 8

#### Step 1) Install GNOME Desktop environment

Before installing VNC Server in your CentOS 8 / RHEL 8, make sure you have a desktop Environment (DE) installed. In case GNOME desktop is already installed or you have installed your server with gui option then you can skip this step.

In CentOS 8 / RHEL 8, GNOME is the default desktop environment. if you don’t have it in your system, install it using the following command:

[root@linuxtechi ~]# dnf groupinstall "workstation" Or [root@linuxtechi ~]# dnf groupinstall "Server with GUI"

Once the above packages are installed successfully then run the following command to enable the graphical mode

[root@linuxtechi ~]# systemctl set-default graphical

Now reboot the system so that we get GNOME login screen.

[root@linuxtechi ~]# reboot

Once the system is rebooted successfully uncomment the line “**WaylandEnable=false**” from the file “**/etc/gdm/custom.conf**” so that remote desktop session request via vnc is handled by xorg of GNOME desktop in place of wayland display manager.

**Note:** Wayland is the default display manager (GDM) in GNOME and it not is configured to handled remote rendering API like X.org

VNC server will not work properly if SElinux is enabled on your system, as of now work around is to disable it using following commands,

[root@linuxtechi ~]# setenforce 0 [root@linuxtechi ~]# sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

#### Step 2) Install VNC Server (tigervnc-server)

Next we’ll install the VNC Server, there are lot of VNC Servers available, and for installation purposes, we’ll be installing **TigerVNC Server**. It is one of the most popular VNC Server and a high-performance and platform-independent VNC that allows users to interact with remote machines easily.

Now install TigerVNC Server using the following command:

[root@linuxtechi ~]# dnf install tigervnc-server tigervnc-server-module -y

#### Step 3) Set VNC Password for Local User

Let’s assume we want ‘pkumar’ user to use VNC for remote desktop session, then switch to the user and set its password using vncpasswd command,

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

#### Step 4) Setup VNC Server Configuration File

Next step is to configure VNC Server Configuration file. Create a file “**/etc/systemd/system/ [email protected]**” with the following content so that tigervnc-server’s service started for above local user “pkumar”.

[root@linuxtechi ~]# vim /etc/systemd/system/[[email protected]][Unit] Description=Remote desktop service (VNC) After=syslog.target network.target [Service] Type=forking ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' ExecStart=/sbin/runuser -l pkumar -c "/usr/bin/vncserver %i -geometry 1280x1024" PIDFile=/home/pkumar/.vnc/%H%i.pid ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' [Install] WantedBy=multi-user.target

Save and exit the file,

**Note: **Replace the user name in above file which suits to your setup.

By default, VNC server listen on tcp port 5900+n, where n is the display number, if the display number is “1” then VNC server will listen its request on TCP port 5901.

#### Step 5) Start VNC Service and allow port in firewall

I am using display number as 1, so use the following commands to start and enable vnc service on display number “1”,

[root@linuxtechi ~]# systemctl daemon-reload [root@linuxtechi ~]# systemctl start vncserver@:1.service [root@linuxtechi ~]# systemctl enable vncserver@:1.service Created symlink /etc/systemd/system/multi-user.target.wants/vncserver@:1.service → /etc/systemd/system/[[email protected]]. [root@linuxtechi ~]#

Use below **netstat** or **ss** command to verify whether VNC server start listening its request on 5901,

[root@linuxtechi ~]# netstat -tunlp | grep 5901 tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 8169/Xvnc tcp6 0 0 :::5901 :::* LISTEN 8169/Xvnc [root@linuxtechi ~]# ss -tunlp | grep -i 5901 tcp LISTEN 0 5 0.0.0.0:5901 0.0.0.0:* users:(("Xvnc",pid=8169,fd=6)) tcp LISTEN 0 5 [::]:5901 [::]:* users:(("Xvnc",pid=8169,fd=7)) [root@linuxtechi ~]#

Use below systemctl command to verify the status of VNC server,

[root@linuxtechi ~]# systemctl status vncserver@:1.service

Above command’s output confirms that VNC is started successfully on port tcp port 5901. Use the following command allow VNC Server port “5901” in os firewall,

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

#### Step 6) Connect to Remote Desktop Session

Now we are all set to see if the remote desktop connection is working. To access the remote desktop, Start the VNC Viewer from your Windows / Linux workstation and enter your **VNC server IP Address** and **Port Number** and then hit enter

Next, it will ask for your VNC password. Enter the password that you have created earlier for your local user and click OK to continue

Now you can see the remote desktop,

That’s it, you’ve successfully installed VNC Server in Centos 8 / RHEL 8.

**Conclusion**

Hope the step-by-step guide to install VNC server on Centos 8 / RHEL 8 has provided you with all the information to easily setup VNC Server and access remote desktops. Please provide your comments and suggestion in the feedback section below. See you in the next article…Until then a big THANK YOU and BYE for now!!!

Read Also : [How to Configure Rsyslog Server in CentOS 8 / RHEL 8](https://www.linuxtechi.com/configure-rsyslog-server-centos-8-rhel-8/)

Bill SanfordAfter you install the BaseOS of RHEL 8 and add the repo files, yum update -y

After you install the workstation or server: yum install gnome* -y

Setting Graphical Login as Default – As root: systemctl set-default graphical.target and reboot.

To enable and use tigervnc, in the following steps:

yum install tigervnc-server -y

semodule -i /usr/share/selinux/packages/vncsession.pp

restorecon /usr/sbin/vncsession /usr/libexec/vncsession-start

If you already have VNC installed, you need to remove “$HOME/.vnc folder” if it exists and recreate again with vncpasswd. This is needed to have “.vnc” folder with correct SELinux context.

Alternatively you can use: restorecon -RFv $HOME/.vnc

vim /etc/tigervnc/vncserver-config-defaults and add “session=gnome”

You can also set other options here. See manual page of Xvnc.

NOTE: You can also have configuration which is specific for a given user, to do that just modify “$HOME/.vnc/config”. You can copy content of “/etc/tigervnc/vncserver-config-defaults” there and do your own modifications.

vim /etc/tigervnc/vncserver.users

Added “:2=test” If you want to add more users, just realize you need to start each instance separately.

NOTE: Spaces are a no-no

NOTE: Example is: :2= and there should be examples in the file for existing users

systemctl start vncserver@:2.service

Connect to the VNCServer

vncviewer or :2

Example: vncviewer 10.10.10.1:2

BillThat last bit about connecting should be

vncviewer or :2

Example: vncviewer 10.10.10.1:2

RomanFollowed your tutorial, but systemctl could not start the service.

Error message was ‘geometry 1280×1024 is invalid’

RobinI have the same situation.

ChrisThe x in the example service file isn’t a proper x when you copy and paste the text. Replace the × with an x and it will then give you a new error with SELinux because the first instruction on disabling SELinux doesn’t actually disable it. This tutorial is rife with FAILS.

Pradeep KumarThanks Chris, I have fixed the letter x as you mentioned in comments. I hope it helps our readers.

LuisAnyone display Black Screen?

Chanme too. did you solve it?

Chan KimI followed it, but when I type ‘netstat -tunlp | grep 5901’ there is no entry shown.

actually the vnc server doesn’t respond. What’s wrong?

ArashGUYS! I found the black screen symptom here!

Make sure the content doesn’t change while copying and pasting which happens for encoding issues or other problems.

That happened to me and the text of geometry 1280×1024 changed to 1280 ^1024. Fix it and reload the daemon and start VNC server again!

CarlI can not start the VNC server which complains geometry 1280×1024.

[root@m2-mapreng-vm167100 ~]# systemctl start vncserver@:1.service

Job for vncserver@:1.service failed because the control process exited with error code.

See “systemctl status vncserver@:1.service” and “journalctl -xe” for details.

Oct 28 09:59:46 m2-mapreng-vm167100 runuser[127322]: vncserver: geometry 1280×1024 is invalid

If I change the ‘x’ between the two number to lower case ‘x’ the VNC server started OK.

[root@m2-mapreng-vm167100 ~]# systemctl status vncserver@:1.service

● vncserver@:1.service – Remote desktop service (VNC)

Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: disabled)

Active: active (running) since Wed 2020-10-28 10:20:23 PDT; 42s ago

Main PID: 150965 (Xvnc)

Tasks: 0 (limit: 152821)

Memory: 320.0K

CGroup: /system.slice/system-vncserver.slice/vncserver@:1.service

‣ 150965 /usr/bin/Xvnc :1 -auth /root/.Xauthority -desktop m2-mapreng-vm167100:1 (root) -fp catalogue:/etc/X11/fontpath.d -geometry 1280×1024 -pn -rfbauth /root/.vnc/passwd -rfbport 5901 -rfbwait 300>

Oct 28 10:20:20 m2-mapreng-vm167100 systemd[1]: Starting Remote desktop service (VNC)…

Oct 28 10:20:23 m2-mapreng-vm167100 systemd[1]: Started Remote desktop service (VNC).

However, when connect with VNC viewer, after enter password etc, I got black screen.

Mike Landismy \ got excised… the post should have read…

I’m still experimenting so take this with a grain of salt, but I’ve found that you need to start the daemon with…

“systemctl start vncserver_\@:\.service” to match the name of its config file in /etc/systemd/system/, e.g. /etc/systemd/system/vncserver_\@:\.service

the default resolution of port 5900 (n=0) is 16x1024x768

per: ‘https://wiki.centos.org/HowTos/VNC-Server’

There will also be a /etc/systemd/system/[email protected] config file that is not specific to a user or port.

if you want to change the display geometry (e.g. to 1280×1024), you can do something like…

VNCSERVERARGS[]=”-geometry 1280×1024 -localhost”‘

Lokesh KHi,

How can I authenticate using unixpwd (PAM Authentication), We no need to manage two passwords for user.

System Password and VNC passwd have to be same and if we change the system password vnc should also update.

Please help

Regards,

Lokesh