Linux VNC 安装与配置

好的 👍 我来给你整理一份 Ubuntu + TigerVNC + XFCE 从零到成功的完整操作手册,包含 安装、配置、systemd 管理、一键脚本,以及 常见报错排查表


📘 Ubuntu + TigerVNC + XFCE 配置手册

1️⃣ 安装桌面环境 + VNC

# 更新系统
sudo apt update && sudo apt upgrade -y

# 安装 XFCE 桌面环境
sudo apt install -y xfce4 xfce4-goodies

# 安装 TigerVNC 服务端
sudo apt install -y tigervnc-standalone-server tigervnc-common

2️⃣ 设置 VNC 密码

切换到普通用户(比如 ubuntu)执行:

vncpasswd

👉 会在 ~/.vnc/passwd 生成加密密码。


3️⃣ 配置 VNC 启动脚本

编辑 ~/.vnc/xstartup

nano ~/.vnc/xstartup

内容:

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

保存并赋予可执行权限:

chmod +x ~/.vnc/xstartup

4️⃣ 创建 systemd 服务单元

创建服务文件:

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

内容:

[Unit]
Description=TigerVNC Remote Desktop Service
After=syslog.target network.target

[Service]
Type=forking
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu

PIDFile=/home/ubuntu/.vnc/%H:%i.pid
ExecStart=/usr/bin/vncserver :%i -desktop %H -geometry 1920x1080 -localhost no -rfbauth /home/ubuntu/.vnc/passwd
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

5️⃣ 启动并开机自启

# 重新加载 systemd 配置
sudo systemctl daemon-reload

# 启动 VNC 显示器 :1 (即端口 5901)
sudo systemctl start vncserver@1

# 查看状态
systemctl status vncserver@1

# 开机自启
sudo systemctl enable vncserver@1

6️⃣ 连接 VNC


🛠 常见报错排查表

报错 原因 解决方法
Session startup via '/home/ubuntu/.vnc/xstartup' cleanly exited too early xstartup 脚本错误 / 缺少桌面环境 检查 ~/.vnc/xstartup 是否有 exec startxfce4 &,并 chmod +x ~/.vnc/xstartup
dbus-launch: not found 缺少 dbus 工具 sudo apt install dbus-x11
xrdb: can't open file '/home/ubuntu/.Xresources' 没有 .Xresources 文件 忽略即可,或者 touch ~/.Xresources
vncserver@.service not found systemd 配置没写好 确认 /etc/systemd/system/vncserver@.service 文件存在,并执行 sudo systemctl daemon-reload
连接失败(本地能用,远程不能用) 防火墙阻挡 sudo ufw allow 5901/tcp
分辨率太小 geometry 参数太小 在服务文件中修改 -geometry 1920x1080

🚀 一键安装脚本 (可选)

如果想快速部署,可以存成 install_vnc.sh

#!/bin/bash
set -e

# 安装 XFCE + VNC
sudo apt update && sudo apt install -y xfce4 xfce4-goodies tigervnc-standalone-server tigervnc-common dbus-x11

# 配置 VNC 密码
echo "请设置 VNC 密码:"
vncpasswd

# 写 xstartup
mkdir -p ~/.vnc
cat > ~/.vnc/xstartup <<EOF
#!/bin/bash
xrdb \$HOME/.Xresources
startxfce4 &
EOF
chmod +x ~/.vnc/xstartup

# 写 systemd 服务
sudo tee /etc/systemd/system/vncserver@.service > /dev/null <<EOF
[Unit]
Description=TigerVNC Remote Desktop Service
After=syslog.target network.target

[Service]
Type=forking
User=$USER
Group=$USER
WorkingDirectory=/home/$USER

PIDFile=/home/$USER/.vnc/%H:%i.pid
ExecStart=/usr/bin/vncserver :%i -desktop %H -geometry 1920x1080 -localhost no -rfbauth /home/$USER/.vnc/passwd
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target
EOF

# 启动 VNC 服务
sudo systemctl daemon-reload
sudo systemctl enable vncserver@1
sudo systemctl start vncserver@1

echo "✅ VNC 已安装完成,请使用 [服务器IP:5901] 连接。"

运行:

bash install_vnc.sh

要不要我帮你再整理一份 图形化的连接方式对比(VNC Viewer / Remmina / noVNC),方便你以后选择最合适的远程桌面工具?