Rocky Linux noVNC

我在 在浏览器中访问arch linux(noVNC) 实践了一个简单的 X11vnc+noVNC 桌面,现在则尝试在Rocky Linux(类似CentOS)构建一个远程VNC桌面。

安装VNC服务器

  • 安装桌面,我使用 xfce :

安装 xfce 桌面
# xfce是通过EPEL提供的
dnf -y install epel-release
# dnf --enablerepo=epel group

# 通过 dnf group list | grep -i xfce 命令可以看到程序分组是 Xfce
dnf -y groupinstall "Xfce" "base-x"
  • 安装TigerVNC服务器:

安装TigerVNC服务器
dnf -y install tigervnc-server
  • 如果运行了firewalld,则需要允许VNC服务(这步我没有执行,因为我是通过 SSH隧道 来访问服务):

配置firewalld允许访问VNC
firewall-cmd --add-service=vnc-server
firewall-cmd --runtime-to-permanent
配置SELinux策略(验证失败)
cat << EOF > vncsession.te

# create new
module vncsession 1.0;

require {
        type default_t;
        type xdm_home_t;
        type vnc_session_t;
        class dir { add_name create write };
        class file { create open write };
}

#============= vnc_session_t ==============
allow vnc_session_t default_t:dir { add_name write };
allow vnc_session_t default_t:file { create open write };
allow vnc_session_t xdm_home_t:dir create;

EOF

checkmodule -m -M -o vncsession.mod vncsession.te
semodule_package --outfile vncsession.pp --module vncsession.mod
semodule -i vncsession.pp

我检查了阿里云的Rocky Linux镜像安装的云主机,发现 /etc/selinux/config 中配置的 SELINUX=disabled ,不激活执行上述命令会出现错误:

配置SELinux策略错误(没有激活SELinux的话)
libsemanage.semanage_direct_install_info: Overriding vncsession module at lower priority 200 with module at priority 400.
Failed to resolve typeattributeset statement at /var/lib/selinux/targeted/tmp/modules/400/vncsession/cil:3
Failed to resolve AST
semodule:  Failed!
配置SELinux策略(成功)
cd /usr/share/selinux/packages/targeted
bizp2 -d vncsession.pp.bz2

# TigerVNC自带的 tigervnc-selinux-1.14.1-1.el9_5.noarch 软件包
# 提供的 vncsession.pp 验证成功
semodule -i /usr/share/selinux/packages/targeted/vncsession.pp

# 重置安全上下文
restorecon /usr/sbin/vncsession /usr/libexec/vncsession-start
配置TigerVNC的 Systemd进程管理器 Unit
# 修改 /etc/tigervnc/vncserver-config-defaults
mv /etc/tigervnc/vncserver-config-defaults /etc/tigervnc/vncserver-config-defaults.bak
cat << EOF > /etc/tigervnc/vncserver-config-defaults
session=xfce
localhost
EOF

# 注意,这里是为VNC添加运行桌面的对应用户名,请按照实际用户账号配置
echo ":1=admin" | sudo tee -a /etc/tigervnc/vncserver.users
#echo ":2=huatai" | sudo tee -a /etc/tigervnc/vncserver.users

# 启动vnc服务
systemctl enable --now vncserver@:1
# 如果要启动多个用户VNC,可以使用
# systemctl enable --now vncserver@:1 vncserver@:2

备注

实践验证配置 ~/.vnc/config 无效,所以还是采用修订 /etc/tigervnc/vncserver-config-defaults

异常排查

这里我遇到一个启动报错,检查日志:

启动Tiger VNC server报错
Dec 22 17:38:22 iZuf6ito4ipt00sco1edgpZ vncsession-restore[41035]: Failed to get home directory for huatai
Dec 22 17:38:22 iZuf6ito4ipt00sco1edgpZ systemd[1]: vncserver@:1.service: Control process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited

参考 VNC service does not start when user home directory is in a custom path 原来 tigervnc 实现原生 Systemd进程管理器 支持,服务是通过 vncsession 启动,而 vncsession 是通过特定的SELinux上下文来获得用户目录的

乌龙了 我配置错 /etc/tigervnc/vncserver.users ,我的系统只设置了 admin 用户,没有配置 huatai 用户,所以导致了上述服务启动错误。前面启用SELinux的配置可能也可以取消

另一个异常排查是启动没有报错,但是看不到 vncserver 进程,检查 systemctl status vncserver@:1 显示启动后立即停止了,但是不是报错:

vncserver启动后立即结束,显示 Deactivated successfully
○ vncserver@:1.service - Remote desktop service (VNC)
     Loaded: loaded (/usr/lib/systemd/system/[email protected]; enabled; preset: disabled)
     Active: inactive (dead) since Sun 2024-12-22 15:30:24 UTC; 8s ago
   Duration: 114ms
    Process: 1726 ExecStartPre=/usr/libexec/vncsession-restore :1 (code=exited, status=0/SUCCESS)
    Process: 1737 ExecStart=/usr/libexec/vncsession-start :1 (code=exited, status=0/SUCCESS)
   Main PID: 1744 (code=exited, status=0/SUCCESS)
        CPU: 95ms

Dec 22 15:30:23 serv-rocky systemd[1]: Starting Remote desktop service (VNC)...
Dec 22 15:30:23 serv-rocky systemd[1]: Started Remote desktop service (VNC).
Dec 22 15:30:24 serv-rocky systemd[1]: vncserver@:1.service: Deactivated successfully.

vncserver@:1.service starts and then stops immediately 给了我启发: 是没有为vncserver的个人用户账号设置密码导致的,也就是说,这里我用了 admin 用户,但是没有给这个账号设置VNC密码。使用以下命令为 admin 设置密码:

vncpasswd 设置账号密码后就能解决启动立即退出问题
vncpasswd

安装noVNC

  • 安装

安装noVNC
dnf -y install novnc
  • 启动

启动noVNC
# 生成自签名证书
openssl req -x509 -nodes -newkey rsa:3072 -keyout novnc.pem -out novnc.pem -days 3650

# 运行noVNC
novnc_server --listen 8081 --cert ~/novnc.pem --vnc localhost:5901 --ssl-only

然后访问 https://127.0.0.1:8081 就可以看到onVNC登陆连接,认证通过之后,可以看到 Rocky Linux 桌面

参考