温馨提示×

ubuntu vnc怎么开启

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

在 Ubuntu 上开启 VNC 有多种方式,下面给你最常用、最稳定的做法,适用于 Ubuntu 20.04 / 22.04 / 24.04(桌面版)。


一、最推荐方案:使用 TigerVNC + GNOME

(适合远程桌面)

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

sudo apt update
sudo apt install ubuntu-desktop

2️⃣ 安装 TigerVNC

sudo apt install tigervnc-standalone-server tigervnc-common

3️⃣ 设置 VNC 密码

vncpasswd

(生成 ~/.vnc/passwd


4️⃣ 创建 VNC 启动配置

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

写入以下内容(GNOME):

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec /etc/X11/xinit/xinitrc
gnome-session &

赋予执行权限:

chmod +x ~/.vnc/xstartup

5️⃣ 启动 VNC 服务

vncserver :1 -geometry 1920x1080 -depth 24
  • :1 → 端口 5901
  • :25902
  • 以此类推

停止:

vncserver -kill :1

6️⃣ 防火墙放行(如启用 UFW)

sudo ufw allow 5901/tcp

二、使用 VNC 客户端连接

  • Windows:RealVNC Viewer / TightVNC / UltraVNC
  • macOS:Screen Sharing / RealVNC
  • Linux:Remmina / Vinagre

连接地址:

IP:5901

三、如果 不想手动启动(systemd 自启)

sudo nano /etc/systemd/system/vncserver@.service
[Unit]
Description=VNC Server
After=syslog.target network.target

[Service]
Type=forking
User=你的用户名
WorkingDirectory=/home/你的用户名

ExecStartPre=-/usr/bin/vncserver -kill :%i
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

四、服务器版 Ubuntu(无桌面)

建议用 X11VNC + 轻量桌面

sudo apt install x11vnc xfce4

启动:

x11vnc -display :0 -forever -shared

五、常见问题

❌ 黑屏

  • 没配置 xstartup
  • 桌面环境未安装

❌ 连接被拒绝

  • 防火墙
  • VNC 没启动
  • 端口不对

六、安全建议(非常重要)

强烈建议通过 SSH 隧道连接

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

然后 VNC 连接:

localhost:5901

如果你愿意,可以告诉我:

  • ✅ Ubuntu 版本
  • ✅ 桌面版 / 服务器版
  • ✅ 本地还是云服务器

我可以给你完全定制的一步到位方案

0