CentOS 上 TigerVNC 连接失败的排查与修复
一 快速定位问题
systemctl status vncserver@:1.servicess -lntp | grep 5901 或 netstat -lunpt | grep vncsystemctl start vncserver@:1.service;设置开机自启:systemctl enable vncserver@:1.servicefirewall-cmd --permanent --add-port=5901/tcp && firewall-cmd --reloadfirewall-cmd --permanent --add-service=vnc-server && firewall-cmd --reloadtelnet 服务器IP 5901 或 nc -zv 服务器IP 5901)。journalctl -xe~/.vnc/主机名:显示号.log(如 /root/.vnc/centos:1.log)二 常见错误与对应修复
ss -lntp | grep 5901 → 结束对应 PID。systemctl status 显示失败或反复重启。/etc/systemd/system/vncserver@.service 中 User/Group、PIDFile、ExecStart/ExecStop 与显示号一致;必要时 systemctl daemon-reload 后重启服务。gsettings set org.gnome.Vino require-encryption false(需在图形会话中执行)~/.vnc/xstartup 是否存在且具备可执行权限(chmod +x),内容需能启动桌面(如 exec gnome-session 或 startxfce4),并确保未被异常终止;必要时查看 ~/.vnc/*.log 的错误提示。三 最小可用配置示例
sudo yum install -y tigervnc-server tigervnc-server-modulevncpasswd(设置连接密码)cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.serviceUser=your_user、Group=your_groupPIDFile=/home/your_user/.vnc/%H:%i.pidExecStart=/usr/bin/vncserver %i -geometry 1280x1024 -depth 24ExecStop=/usr/bin/vncserver -kill %isystemctl daemon-reloadsystemctl enable --now vncserver@:1.servicefirewall-cmd --permanent --add-port=5901/tcp && firewall-cmd --reload服务器IP:1(或 服务器IP:5901)四 安全与加固建议
ssh -L 5901:localhost:5901 用户名@服务器IPlocalhost:1(隧道流量经 SSH 加密)