温馨提示×

centos vnc远程如何连接

小樊
41
2026-09-02 16:56:07
栏目: 智能运维

CentOS 上通过 VNC 远程连接,一般分三步:服务端安装配置 VNC → 开放防火墙/端口 → 客户端连接。下面以 CentOS 7/8(含 Stream) 为例说明。


一、服务器端安装 VNC 服务

1. 安装桌面环境(如果还没装)

# CentOS 7
yum groupinstall "GNOME Desktop" -y

# CentOS 8 / Stream
dnf groupinstall "Server with GUI" -y

2. 安装 VNC 服务

# CentOS 7
yum install tigervnc-server -y

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

二、配置 VNC 用户

1. 切换到要远程的用户(如 user1)

su - user1

2. 设置 VNC 密码

vncpasswd

(输入连接密码,可设置 view-only 密码)

3. 复制并修改 VNC 服务配置

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

找到:

ExecStart=/usr/bin/vncserver_wrapper <USER> %i

改为:

ExecStart=/usr/bin/vncserver_wrapper user1 :1

三、启动 VNC 服务

sudo systemctl daemon-reload
sudo systemctl enable vncserver@:1.service
sudo systemctl start vncserver@:1.service

查看状态:

systemctl status vncserver@:1.service

四、防火墙设置

# VNC 默认端口 5901(:1)
firewall-cmd --add-port=5901/tcp --permanent
firewall-cmd --reload

如果是云服务器,还要在 安全组 放行 5901


五、客户端连接

Windows / macOS

使用 VNC Viewer(RealVNC / TigerVNC)

连接地址:

服务器IP:5901

例如:

192.168.1.100:5901

输入前面设置的 VNC 密码即可。


六、常见问题

1. 黑屏

检查 .vnc/xstartup 是否正确:

#!/bin/sh
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc

或 GNOME:

exec gnome-session &

2. 多用户

  • :1 → 5901
  • :2 → 5902
  • 每个用户一个服务

3. 更安全方式(推荐)

使用 SSH 隧道

ssh -L 5901:localhost:5901 user@server_ip

然后连接 localhost:5901


如果你用的是 CentOS 6 / 特定版本 / 无桌面版,可以告诉我,我可以给你对应方案。

0