温馨提示×

ubuntu vnc怎么配置

小樊
55
2026-07-12 17:48:49
栏目: 智能运维

在 Ubuntu 上配置 VNC(远程桌面)通常使用 TigerVNC / x11vnc / GNOME 自带的 VNC,下面我给你一套最常用、最稳定的方案,适合 Ubuntu 20.04 / 22.04 / 24.04


一、推荐方案:使用 TigerVNC(适合桌面用户)

1️⃣ 安装桌面环境(如果还没有)

sudo apt update
sudo apt install ubuntu-desktop

如果是服务器,可以用轻量桌面:

sudo apt install xfce4 xfce4-goodies

2️⃣ 安装 TigerVNC

sudo apt install tigervnc-standalone-server tigervnc-common

3️⃣ 设置 VNC 密码

vncpasswd

4️⃣ 配置 VNC 启动脚本

mkdir -p ~/.vnc
nano ~/.vnc/xstartup

✅ 如果你是 GNOME

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

或(推荐)

#!/bin/sh
export XKL_XMODMAP_DISABLE=1
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec gnome-session

✅ 如果你是 XFCE

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4

保存后给执行权限:

chmod +x ~/.vnc/xstartup

5️⃣ 启动 VNC 服务

vncserver :1 -geometry 1920x1080 -depth 24
  • :1 → 端口 5901
  • :2 → 端口 5902

查看端口:

ss -lntp | grep 59

6️⃣ 客户端连接

使用:

  • Windows:RealVNC Viewer / TightVNC
  • macOS:屏幕共享 / RealVNC
  • Linux:remmina

连接地址:

IP:5901

二、设置 VNC 为 systemd 服务(开机自启)

sudo nano /etc/systemd/system/vncserver@.service

内容:

[Unit]
Description=VNC Server
After=syslog.target network.target

[Service]
Type=forking
User=你的用户名
ExecStart=/usr/bin/vncserver :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

启用:

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

三、云服务器 / 公网访问(重要)

不要直接暴露 5901 端口

推荐方式:

ssh -L 5901:localhost:5901 user@服务器IP

然后客户端连接:

localhost:5901

四、常见问题

❌ 黑屏 / 灰屏

一般原因:

  • xstartup 没执行权限
  • 桌面环境没装
  • DBUS 没有 unset

❌ 端口连不上

检查:

sudo ufw allow 5901/tcp

五、替代方案(快速)

x11vnc(适合已有桌面)

sudo apt install x11vnc
x11vnc -display :0

六、我可以进一步帮你

你可以告诉我:

  1. ✅ Ubuntu 版本
  2. ✅ 桌面环境(GNOME / XFCE / 无桌面)
  3. ✅ 本地 or 云服务器
  4. ✅ 是否需要开机自启

我可以直接给你定制命令

0