验证是否已经安装过VNC
[root@localhost ~]# rpm -q tigervnc tigervnc-serve
若没安装的话会出现
package tigervnc is not installed
package tigervnc-server is not installed
安装VNC package
[root@localhost ~]# yum install tigervnc-server -y
修改配置信息
[root@localhost ~]# cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
打开配置文件/etc/systemd/system/vncserver@:1.service,替换掉如下默认用户名<USER>
ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H%i.pid
重新加载systemd
[root@localhost ~]# systemctl daemon-reload
设置当前用户VNC连接密码
如果使用CentOS 7默认防火墙
[root@localhost ~]# firewall-cmd --permanent --add-service vnc-server
重启firewalld
[root@localhost ~]# systemctl restart firewalld.service
如果使用iptables防火墙
vi /etc/sysconfig/iptables
在合适的位置加上
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5900:5903 -j ACCEPT
重启iptables
service iptables restart
设置VNC默认启动
[root@localhost ~]# systemctl enable vncserver@:1.service
开启VNC
[root@localhost ~]# systemctl start vncserver@:1.service
当VNC突然启动失败时可以尝试
[root@localhost ~]# rm -rf /tmp/.X11-unix/*