Install and Configure VNCServer on CentOS 7
安装TigerVNC Server
建议使用非root用户安装
sudo yum install -y tigervnc-server
配置VNC Service
将默认提供的文件复制到/etc/systemd/system,命令如下
sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
接下来修改该配置文件
sudo vim /etc/systemd/system/vncserver@:1.service
将其中<USER>
替换为你想要的用户名,这里以test_user
为例,所有内容如下
# The vncserver service unit file
#
# Quick HowTo:
# 1. Copy this file to /etc/systemd/system/vncserver@.service
# 2. Replace <USER> with the actual user name and edit vncserver
# parameters appropriately
# (ExecStart=/usr/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
# PIDFile=/home/<USER>/.vnc/%H%i.pid)
# 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
# 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 test_user -c "/usr/bin/vncserver %i"
PIDFile=/home/test_user/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
停止并禁用防火墙
sudo systemctl stop firewalld.service && sudo systemctl disable firewalld.service
设置 VNC Passwords
在用户下运行vncserver
设置密码
You will require a password to access your desktops.
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
New 'cloud021:1 (test_user)' desktop is cloud021:1
Starting applications specified in /home/test_user/.vnc/xstartup
Log file is /home/test_user/.vnc/cloud021:1.log
启动vncserver
vncserver
查看运行状态
sudo systemctl status vncserver@:1.service
设置开机启动
sudo systemctl daemon-reload
sudo systemctl enable vncserver@:1.service
关闭vncserver
vncserver -kill :1