温馨提示×

CentOS VNC如何远程

小樊
86
2026-08-03 03:58:24
栏目: 智能运维

在 CentOS(7/8/Stream) 上通过 VNC 远程桌面,常见做法如下(以 TigerVNC 为例,最常用、稳定)。


一、环境说明

  • 系统:CentOS 7 / CentOS 8 / CentOS Stream
  • 桌面:GNOME(默认)
  • VNC 服务:TigerVNC
  • 端口:5901、5902…

二、安装桌面环境(若未安装)

CentOS 7

yum groupinstall "GNOME Desktop" -y

CentOS 8 / Stream

dnf groupinstall "Server with GUI" -y

设置默认启动图形界面:

systemctl set-default graphical.target
reboot

三、安装 VNC Server

# CentOS 7
yum install tigervnc-server -y

# CentOS 8 / Stream
dnf install tigervnc-server -y

四、配置 VNC 用户

1️⃣ 切换普通用户(不建议 root 直接 VNC)

su - username

2️⃣ 设置 VNC 密码

vncpasswd

五、配置 VNC 服务

1️⃣ 复制服务模板

cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

2️⃣ 修改配置

vi /etc/systemd/system/vncserver@:1.service

修改 <USER> 为你的用户名,例如:

ExecStart=/usr/bin/vncserver -geometry 1920x1080 -depth 24 :1

或(新版本):

ExecStart=/usr/sbin/runuser -l username -c "/usr/bin/vncserver %i"

六、启动 VNC 服务

systemctl daemon-reload
systemctl start vncserver@:1
systemctl enable vncserver@:1

查看状态:

systemctl status vncserver@:1

七、防火墙放行端口

VNC 默认端口:

  • :1 → 5901
  • :2 → 5902
firewall-cmd --add-port=5901/tcp --permanent
firewall-cmd --reload

八、客户端连接

Windows / macOS

使用:

  • RealVNC Viewer
  • TigerVNC Viewer
  • UltraVNC

连接地址:

服务器IP:1
或
服务器IP:5901

输入 VNC 密码即可。


九、常见问题

❌ 黑屏 / 无法进入桌面

vi ~/.vnc/xstartup

确保内容如下(GNOME):

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec gnome-session &

赋予执行权限:

chmod +x ~/.vnc/xstartup

重启 VNC:

vncserver -kill :1
vncserver :1

十、安全建议(强烈推荐)

✅ 不要直接暴露 5901 到公网

推荐方式:

  • SSH 隧道
ssh -L 5901:localhost:5901 user@server_ip

然后本地连接:

localhost:1

十一、总结

✅ 安装桌面
✅ 安装 TigerVNC
✅ 配置用户 + 密码
✅ 启动 VNC 服务
✅ 防火墙放行
✅ SSH 隧道更安全


如果你告诉我:

  • CentOS 版本
  • 是否有桌面
  • 是否 root 用户

我可以给你 更精确的配置方案 ✅

0 踩