如何在Centos 8/RHEL 8上安装和配置VNC服务器

如何在Centos 8/RHEL 8上安装和配置VNC服务器

VNC(虚拟网络计算Virtual Network Computing)服务器是基于 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 是默认的桌面环境。如果你的系统中没有它,请使用以下命令进行安装:

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> dnf groupinstall </span><span class="str">"workstation"</span>
  2. <span class="pun">或者</span>
  3. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> dnf groupinstall </span><span class="str">"Server with GUI</span>

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

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">-</span><span class="kwd">default</span><span class="pln"> graphical</span>

现在重启系统,进入 GNOME 登录页面(LCTT 译注:你可以通过切换运行态来进入图形界面)。

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">reboot</span>

重启后,请取消注释 /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 服务器:

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> dnf install tigervnc</span><span class="pun">-</span><span class="pln">server tigervnc</span><span class="pun">-</span><span class="pln">server</span><span class="pun">-</span><span class="kwd">module</span><span class="pln"> </span><span class="pun">-</span><span class="pln">y</span>

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

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

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">su</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> pkumar</span>
  2. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]</span><span class="pln">$ vncpasswd</span>
  3. <span class="typ">Password</span><span class="pun">:</span>
  4. <span class="typ">Verify</span><span class="pun">:</span>
  5. <span class="typ">Would</span><span class="pln"> you like to enter a view</span><span class="pun">-</span><span class="pln">only password </span><span class="pun">(</span><span class="pln">y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">)?</span><span class="pln"> n</span>
  6. <span class="pln">A view</span><span class="pun">-</span><span class="pln">only password </span><span class="kwd">is</span><span class="pln"> </span><span class="kwd">not</span><span class="pln"> used</span>
  7. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]</span><span class="pln">$</span>
  8. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]</span><span class="pln">$ </span><span class="kwd">exit</span>
  9. <span class="pln">logout</span>
  10. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span>

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

下一步是配置 VNC 服务器配置文件。创建含以下内容的 /etc/systemd/system/[email protected],以便为上面的本地用户 pkumar 启动 tigervnc-server 的服务。

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">vim</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="pln">vncserver@</span><span class="pun">.</span><span class="pln">service</span>
  2. <span class="pun">[</span><span class="typ">Unit</span><span class="pun">]</span>
  3. <span class="typ">Description</span><span class="pun">=</span><span class="typ">Remote</span><span class="pln"> </span><span class="typ">Desktop</span><span class="pln"> VNC </span><span class="typ">Service</span>
  4. <span class="typ">After</span><span class="pun">=</span><span class="pln">syslog</span><span class="pun">.</span><span class="pln">target network</span><span class="pun">.</span><span class="pln">target</span>
  5.  
  6. <span class="pun">[</span><span class="typ">Service</span><span class="pun">]</span>
  7. <span class="typ">Type</span><span class="pun">=</span><span class="pln">forking</span>
  8. <span class="typ">WorkingDirectory</span><span class="pun">=</span><span class="str">/home/</span><span class="pln">pkumar</span>
  9. <span class="typ">User</span><span class="pun">=</span><span class="pln">pkumar</span>
  10. <span class="typ">Group</span><span class="pun">=</span><span class="pln">pkumar</span>
  11.  
  12. <span class="typ">ExecStartPre</span><span class="pun">=</span><span class="str">/bin/</span><span class="pln">sh </span><span class="pun">-</span><span class="pln">c </span><span class="str">'/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'</span>
  13. <span class="typ">ExecStart</span><span class="pun">=</span><span class="str">/usr/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">vncserver </span><span class="pun">-</span><span class="pln">autokill </span><span class="pun">%</span><span class="pln">i</span>
  14. <span class="typ">ExecStop</span><span class="pun">=</span><span class="str">/usr/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">vncserver </span><span class="pun">-</span><span class="kwd">kill</span><span class="pln"> </span><span class="pun">%</span><span class="pln">i</span>
  15.  
  16. <span class="pun">[</span><span class="typ">Install</span><span class="pun">]</span>
  17. <span class="typ">WantedBy</span><span class="pun">=</span><span class="pln">multi</span><span class="pun">-</span><span class="pln">user</span><span class="pun">.</span><span class="pln">target</span>

保存并退出文件,

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

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

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

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

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> daemon</span><span class="pun">-</span><span class="pln">reload</span>
  2. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> start vncserver@</span><span class="pun">:</span><span class="lit">1.service</span>
  3. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> enable vncserver@</span><span class="pun">:</span><span class="lit">1.service</span>
  4. <span class="typ">Created</span><span class="pln"> symlink </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="pln">multi</span><span class="pun">-</span><span class="pln">user</span><span class="pun">.</span><span class="pln">target</span><span class="pun">.</span><span class="pln">wants</span><span class="pun">/</span><span class="pln">vncserver@</span><span class="pun">:</span><span class="lit">1.service</span><span class="pln"> </span><span class="pun">→</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="pln">vncserver@</span><span class="pun">.</span><span class="pln">service</span><span class="pun">.</span>
  5. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span>

使用下面的 netstatss 命令来验证 VNC 服务器是否开始监听 5901 上的请求,

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">netstat</span><span class="pln"> </span><span class="pun">-</span><span class="pln">tunlp </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="lit">5901</span>
  2. <span class="pln">tcp </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">:</span><span class="lit">5901</span><span class="pln"> </span><span class="lit">0.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">:*</span><span class="pln"> LISTEN </span><span class="lit">8169</span><span class="pun">/</span><span class="typ">Xvnc</span>
  3. <span class="pln">tcp6 </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="pun">:::</span><span class="lit">5901</span><span class="pln"> </span><span class="pun">:::*</span><span class="pln"> LISTEN </span><span class="lit">8169</span><span class="pun">/</span><span class="typ">Xvnc</span>
  4. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">ss</span><span class="pln"> </span><span class="pun">-</span><span class="pln">tunlp </span><span class="pun">|</span><span class="pln"> </span><span class="kwd">grep</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i </span><span class="lit">5901</span>
  5. <span class="pln">tcp LISTEN </span><span class="lit">0</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">0.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">:</span><span class="lit">5901</span><span class="pln"> </span><span class="lit">0.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">:*</span><span class="pln"> </span><span class="kwd">users</span><span class="pun">:((</span><span class="str">"Xvnc"</span><span class="pun">,</span><span class="pln">pid</span><span class="pun">=</span><span class="lit">8169</span><span class="pun">,</span><span class="pln">fd</span><span class="pun">=</span><span class="lit">6</span><span class="pun">))</span><span class="pln"> </span>
  6. <span class="pln">tcp LISTEN </span><span class="lit">0</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="pun">[::]:</span><span class="lit">5901</span><span class="pln"> </span><span class="pun">[::]:*</span><span class="pln"> </span><span class="kwd">users</span><span class="pun">:((</span><span class="str">"Xvnc"</span><span class="pun">,</span><span class="pln">pid</span><span class="pun">=</span><span class="lit">8169</span><span class="pun">,</span><span class="pln">fd</span><span class="pun">=</span><span class="lit">7</span><span class="pun">))</span><span class="pln"> </span>
  7. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span>

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

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> status vncserver@</span><span class="pun">:</span><span class="lit">1.service</span>

如何在Centos 8/RHEL 8上安装和配置VNC服务器

vncserver-status-centos8-rhel8

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

  1. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">permanent </span><span class="pun">--</span><span class="pln">add</span><span class="pun">-</span><span class="pln">port</span><span class="pun">=</span><span class="lit">5901</span><span class="pun">/</span><span class="pln">tcp</span>
  2. <span class="pln">success</span>
  3. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span><span class="pln"> firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">reload</span>
  4. <span class="pln">success</span>
  5. <span class="pun">[</span><span class="pln">root@linuxtechi </span><span class="pun">~]#</span>

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

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

如何在Centos 8/RHEL 8上安装和配置VNC服务器

VNC-Viewer-Windows10

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

如何在Centos 8/RHEL 8上安装和配置VNC服务器

VNC-Viewer-Connect-CentOS8-RHEL8-VNC-Server

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

如何在Centos 8/RHEL 8上安装和配置VNC服务器

VNC-Desktop-Screen-CentOS8

相关推荐