温馨提示×

如何 Ubuntu 装 VNC

小樊
46
2026-09-28 07:47:48
栏目: 智能运维

在 Ubuntu 上安装和配置 VNC(以最常见的 TigerVNC / TightVNC + Xfce 为例,适合服务器/桌面版)可以分为以下几步:


一、更新系统

sudo apt update
sudo apt upgrade -y

二、安装桌面环境(如果没有图形界面)

如果是 Ubuntu Server,需要先装桌面环境,推荐轻量的 Xfce:

sudo apt install xfce4 xfce4-goodies -y

三、安装 VNC Server

以 TigerVNC 为例(推荐):

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

四、设置 VNC 密码

vncpasswd

会生成:

~/.vnc/passwd

五、配置 VNC 启动脚本

编辑 ~/.vnc/xstartup:

nano ~/.vnc/xstartup

内容如下(使用 Xfce):

#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &

保存后赋予执行权限:

chmod +x ~/.vnc/xstartup

六、启动 VNC Server

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

查看正在运行的 VNC:

vncserver -list

停止 VNC:

vncserver -kill :1

七、配置开机自启(可选)

方法 1:systemd(推荐)

创建服务文件:

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

内容(将 username 改为你的用户名):

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

[Service]
Type=forking
User=username
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.service
sudo systemctl start vncserver@1.service

八、防火墙(如有)

sudo ufw allow 5901/tcp

九、客户端连接

  • Windows / macOS:使用 RealVNC / TigerVNC Viewer
  • 地址填写:
服务器IP:5901
  • 输入前面设置的 VNC 密码

十、安全建议(重要)

⚠ 不要直接暴露 5901 到公网 推荐:

ssh -L 5901:localhost:5901 user@server_ip

然后本地连接:

localhost:5901

如果你告诉我:

  • Ubuntu 版本(20.04 / 22.04 / 24.04)
  • 是否有桌面
  • 是否用 root

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

0 踩