温馨提示×

怎么设 Ubuntu VNC

小樊
57
2026-09-28 08:18:49
栏目: 智能运维

在 Ubuntu 上配置 VNC(以最常见的 TigerVNC / tightvnc + XFCE/GNOME 为例),可以按下面的步骤来。


一、安装桌面环境(如果服务器没有图形界面)

如果是最小化安装的 Ubuntu:

sudo apt update
sudo apt install xfce4 xfce4-goodies -y

(也可以用 Ubuntu GNOME,但 XFCE 更轻量,适合 VNC)


二、安装 VNC Server

推荐使用 TigerVNC:

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

三、设置 VNC 密码

切换到你要远程登录的用户(不要用 root):

vncpasswd

会生成:

~/.vnc/passwd

四、配置 VNC 启动文件

编辑 ~/.vnc/xstartup:

nano ~/.vnc/xstartup

内容如下(XFCE 示例):

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

保存后赋予执行权限:

chmod +x ~/.vnc/xstartup

五、启动 VNC Server

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

查看正在运行的 VNC:

vncserver -list

六、客户端连接

使用 VNC Viewer(RealVNC / TigerVNC / Remmina):

服务器IP:5901

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


七、关闭 VNC

vncserver -kill :1

八、开机自启(systemd,可选)

创建服务文件:

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

内容(替换 username):

[Unit]
Description=VNC Server
After=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

九、安全建议(重要)

⚠️ 不要直接暴露 5901 到公网

推荐用 SSH 隧道:

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

然后本地连接:

localhost:5901

如果你告诉我:

  • Ubuntu 版本(20.04 / 22.04 / 24.04)
  • 是否云服务器
  • 用 root 还是普通用户

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

0 踩