温馨提示×

怎样在 Ubuntu 开 VNC

小樊
40
2026-09-28 07:51:48
栏目: 智能运维

在 Ubuntu 上开启 VNC,一般流程是:安装桌面环境(若服务器版)→ 安装 VNC Server → 配置 → 启动 → 客户端连接。下面给你一套最常用、稳妥的做法(以 Ubuntu 20.04 / 22.04 / 24.04 为例)。


一、安装桌面环境(服务器版必做)

如果已经是 Ubuntu 桌面版,可跳过。

sudo apt update
sudo apt install ubuntu-desktop -y

或轻量一些:

sudo apt install xfce4 xfce4-goodies -y

二、安装 VNC Server(以 TigerVNC 为例)

sudo apt install tigervnc-standalone-server tigervnc-common -y

三、设置 VNC 密码

vncpasswd

密码保存在 ~/.vnc/passwd


四、配置 VNC(重要)

编辑或创建配置文件:

nano ~/.vnc/config

示例(使用 XFCE):

session=xfce
geometry=1280x720
localhost=no

如果是 GNOME:

session=gnome

五、启动 VNC Server

vncserver :1
  • :1 表示端口 5901
  • :2 → 5902,以此类推

查看状态:

vncserver -list

停止:

vncserver -kill :1

六、防火墙(如有)

sudo ufw allow 5901

⚠️ 不建议直接公网暴露 5901,建议用 SSH 隧道。


七、安全连接(推荐 SSH 隧道)

本地电脑执行:

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

然后用 VNC 客户端连接:

localhost:5901

八、设置开机自启(可选)

sudo systemctl edit --full vncserver@1.service

示例 service(简要):

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

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

[Install]
WantedBy=multi-user.target

启用:

sudo systemctl enable vncserver@1

常见问题

  • 黑屏 → 桌面环境未正确配置
  • 连不上 → 端口/防火墙/SSH 隧道问题
  • 分辨率低 → 修改 geometry

如果你告诉我:

  • Ubuntu 版本
  • 桌面版还是服务器版
  • 用哪种 VNC(TigerVNC / RealVNC / x11vnc)

我可以给你更精确的一步步命令。

0 踩