安装vnc server
yum install tigervnc tigervnc-server
配置,参考HOWTO.md
在/etc/tigervnc/vncserver.users文件中添加用户映射,:1=root,指的是5901端口,root用户
vim /etc/tigervnc/vncserver.users
# TigerVNC User assignment
#
# This file assigns users to specific VNC display numbers.
# The syntax is <display>=<username>. E.g.:
#
# :2=andrew
# :3=lisa
:1=root
设置默认配置vncserver-config-defaults,其中添加localhost,则不能外部访问
vim /etc/tigervnc/vncserver-config-defaults
## Default settings for VNC servers started by the vncserver service
#
# Any settings given here will override the builtin defaults, but can
# also be overriden by ~/.vnc/config and vncserver-config-mandatory.
#
# See HOWTO.md and the following manpages for more details:
# vncsession(8) Xvnc(1)
#
# Several common settings are shown below. Uncomment and modify to your
# liking.
# session=gnome
# securitytypes=vncauth,tlsvnc
# geometry=2000x1200
# localhost
# alwayran.shared
#securitytypes=vncauth,tlsvnc
desktop=sandbox
geometry=2000x1200
alwaysshared
设置全局配置vncserver-config-mandatory,优先级比vncserver-config-defaults高,会覆盖vncserver-config-defaults中的配置
vim /etc/tigervnc/vncserver-config-mandatory
## Mandatory settings for VNC servers started by the vncserver service
#
# Any settings given here will override the builtin defaults and
# settings specified in ~/.vnc/config or vnc-config-defaults.
#
# See HOWTO.md and the following manpages for more details:
# vncsession(8) Xvnc(1)
#
# Several common settings are shown below. Uncomment and modify to your
# liking.
# session=gnome
# securitytypes=vncauth,tlsvnc
# geometry=2000x1200
# localhost
# alwaysshared
#securitytypes=vncauth,tlsvnc
desktop=sandbox
geometry=2000x1200
alwaysshared
重新加载配置文件
systemctl daemon-reload
开启vnc服务
systemctl start vncserver@:1
无反应,执行 systemctl status vncserver@:1.service查看
systemctl status vncserver@:1.service
○ vncserver@:1.service - Remote desktop service (VNC)
Loaded: loaded (/etc/systemd/system/vncserver@:1.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2023-01-11 09:50:35 CST; 5s ago
Process: 3604 ExecStart=/usr/libexec/vncsession-start :1 (code=exited, status=0/SUCCESS)
Main PID: 3611 (code=exited, status=0/SUCCESS)
1月 11 09:50:35 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)...
1月 11 09:50:35 localhost.localdomain systemd[1]: Started Remote desktop service (VNC).
1月 11 09:50:35 localhost.localdomain systemd[1]: vncserver@:1.service: Deactivated successfully.
参考https://github.com/TigerVNC/tigervnc/issues/1189,执行setenforce 0
setenforce 0
再次启动vnc服务,成功
systemctl status vncserver@:1.service
● vncserver@:1.service - Remote desktop service (VNC)
Loaded: loaded (/etc/systemd/system/vncserver@:1.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-01-11 10:04:15 CST; 9s ago
Process: 3667 ExecStart=/usr/libexec/vncsession-start :1 (code=exited, status=0/SUCCESS)
Main PID: 3674 (vncsession)
Tasks: 0 (limit: 1224632)
Memory: 732.0K
CGroup: /system.slice/system-vncserver.slice/vncserver@:1.service
‣ 3674 /usr/sbin/vncsession root :1
1月 11 10:04:15 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)...
1月 11 10:04:15 localhost.localdomain systemd[1]: Started Remote desktop service (VNC).
查看端口运行状态,端口以启动监听
netstat -antulp | grep 5901
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 3680/Xvnc
tcp6 0 0 :::5901 :::* LISTEN 3680/Xvnc
官方提供的service里有一个属性 SELinuxContext=system_u:system_r:vnc_session_t:s0,我添加后启动失败,然后我就给它注了,就不报错了,原因不详
× vncserver@:1.service - Remote desktop service (VNC)
Loaded: loaded (/etc/systemd/system/vncserver@:1.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2023-01-11 09:49:28 CST; 15s ago
Process: 3570 ExecStart=/usr/libexec/vncsession-start :1 (code=exited, status=229/SELINUX_CONTEXT)
1月 11 09:49:28 localhost.localdomain systemd[1]: Starting Remote desktop service (VNC)...
1月 11 09:49:28 localhost.localdomain systemd[1]: vncserver@:1.service: Control process exited, code=exited, status=229/SELINUX_CONTEXT
1月 11 09:49:28 localhost.localdomain systemd[1]: vncserver@:1.service: Failed with result 'exit-code'.
1月 11 09:49:28 localhost.localdomain systemd[1]: Failed to start Remote desktop service (VNC).