CentOS 7.8 配置 VNC 服務

前言

安裝的 Centos 服務器需要進行 GUI 操作。

環境說明

CentOS 7.8(Desktop Install)

因爲使用圖形用戶界面,本例使用 GNOME 桌面環境。

$ cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)

系統用戶有兩個 root 和 user,VNC Viewer 使用 user 進行訪問

安裝

安裝桌面環境

如果沒有安裝 Desktop 版本,需要先安裝 X Windows

注意:
安裝桌面環境,未測試

$ sudo yum check-update
$ sudo yum groupinstall "X Window System"
$ sudo yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts

設置默認啓動圖形界面:

$ sudo unlink /etc/systemd/system/default.target
$ sudo ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target

重啓服務器:

$ sudo reboot

重啓之後,應該有 CentOS 7 的桌面環境

安裝 VNC Server

yum 安裝:

$ sudo yum install tigervnc-server

配置 VNC Server

VNC Server 支持多種配置,如:

  • 單用戶單界面配置(一個用戶訪問,使用一個界面)
  • 多用戶單界面配置(多個用戶訪問,使用同一個界面)
  • 多用戶多界面配置(多個用戶訪問,使用各自的界面)

本例比較簡單,只介紹單用戶單界面配置。其他類型的配置,可以參考 CHAPTER 12. TIGERVNC - RedHat Customer Portal

配置單用戶單界面

拷貝模板:

$ sudo cp /usr/lib/systemd/system/[email protected] /etc/systemd/system/[email protected]

網上其他資料,拷貝的文件名爲 vncserver@:1.service,沒有必要,後續說明。

編輯配置文件:

$ sudo vi /etc/systemd/system/[email protected]

編輯完:

# The vncserver service unit file
#
# Quick HowTo:
# 1. Copy this file to /etc/systemd/system/[email protected]
# 2. Replace <USER> with the actual user name and edit vncserver
#    parameters in the wrapper script located in /usr/bin/vncserver_wrapper
# 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
User=root

# Clean any existing files in /tmp/.X11-unix environment
#ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
#ExecStart=/usr/bin/vncserver_wrapper <USER> %i
#ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

# 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 -geometry 1280x1024"
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

注意:

  • 啓動服務的用戶爲 root,添加 User=root, 這樣 VNC Client 訪問時可以看到菜單欄
  • <USER> 替換爲 user (本機的非 root 用戶), 這樣用戶登錄到 user 的界面;
  • :wq 保存配置之後,重啓 systemd
$ sudo systemctl daemon-reload

配置訪問密碼

本例使用 user 用戶的桌面環境,如果使用其他用戶,請先切換到 user 用戶

# su user
$ vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n

開啓服務

$ sudo systemctl start vncserver@:1

這樣就開啓了第一個界面

注意
服務的文件 /etc/systemd/system/[email protected]沒有 :1
:1 使當參數啓動服務器,表示啓動第一個界面

打開防火牆

我們需要配置防火牆, 打開 VNC 服務

$ sudo firewall-cmd --permanent --add-service vnc-server
success
$ sudo firewall-cmd --reload
success

客戶端訪問

下載 VNC Viewer

設置如下:

VNC Server: YOUR_SERVER_IP:1
Name: YOUR_Display_1

在這裏插入圖片描述

連接之後,輸入 user 的 vpnpasswd,既可看到界面了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章