Ubuntu GNOME 桌面安装和配置 TigerVNC,包含服务设置、桌面环境适配和防火墙配置

一、安装 TigerVNC

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

二、首次配置

1. 设置 VNC 密码(长度需 ≥6 位)

vncpasswd

密码文件会保存在 ~/.vnc/passwd。

2. 创建 xstartup 配置文件(适配不同桌面环境)

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

根据桌面环境选择配置:

GNOME 桌面:

#!/bin/bash
export GNOME_SHELL_SESSION_MODE=ubuntu
export XDG_CURRENT_DESKTOP=ubuntu:GNOME
export XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
dbus-launch --exit-with-session /usr/bin/gnome-session --session=ubuntu

XFCE 桌面(轻量级推荐):

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

KDE Plasma 桌面:

#!/bin/bash
startplasma-x11 &

3.赋予执行权限:

chmod +x ~/.vnc/xstartup

三、启动 VNC 服务

临时启动(端口 5901):

vncserver :1 -geometry 1920x1080 -depth 24 -localhost no
  • -localhost no:允许外部连接
  • :1 对应端口 5901(:2 → 5902,依此类推)

查看运行中的实例:

vncserver -list

四、设置系统服务(开机自启)

1. 创建 systemd 服务文件

sudo nano /etc/systemd/system/[email protected]

粘贴以下内容(注意替换 USER):

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=simple
User=root  # 改为实际用户名
ExecStart=/usr/bin/vncserver :%i -geometry 1920x1080 -depth 24 -localhost no
ExecStop=/usr/bin/vncserver -kill :%i
Restart=on-failure

[Install]
WantedBy=multi-user.target

2. 启用并启动服务

sudo systemctl daemon-reload
sudo systemctl enable vncserver@1  # 对应 :1 实例
sudo systemctl start vncserver@1

3. 检查状态

sudo systemctl status vncserver@1

五、防火墙放行

sudo ufw allow 5901/tcp
sudo ufw reload

六、客户端连接

地址:<服务器IP>:5901
或者ssh客户端内部连接:localhost:5901

客户端工具:
Windows: TigerVNC Viewer

Linux/macOS:

vncviewer 服务器IP:5901

七、常见问题解决

1. 连接后黑屏

  • 确保桌面环境已安装:
sudo apt install ubuntu-desktop  # GNOME
或
sudo apt install xfce4          # XFCE
  • 检查日志:
cat ~/.vnc/ubuntu:1.log

2. 修改分辨率

停止服务后重新启动:

vncserver -kill :1
vncserver :1 -geometry 1280x720 -depth 24

3. 卸载 TigerVNC

sudo apt remove --purge tigervnc-*
rm -rf ~/.vnc

附:安全建议

使用 SSH 隧道加密:

ssh -L 5901:localhost:5901 用户名@服务器IP

然后连接 localhost:5901。

限制访问 IP:

sudo ufw allow from 192.168.1.100 to any port 5901

按照以上步骤,你可以在 Ubuntu 上快速搭建一个支持多用户的 TigerVNC 远程桌面服务。

文章作者: Billy
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 billy blog
教程 服务器调教
喜欢就支持一下吧
打赏
微信 微信