桌面控制技能
为无头 Linux 服务器提供完整的桌面 GUI 控制。创建虚拟显示(Xvfb + XFCE),让你可以在 VPS/云服务器上运行和控制桌面应用,无需物理显示器。
环境配置
- 显示器:
:99 - 分辨率: 1024x768 (XGA,Anthropic 推荐)
- 桌面: XFCE4(最小化 — 仅 xfwm4 + 面板)
快速设置
运行设置脚本安装所有组件(systemd 服务、无闪烁 VNC):
./scripts/setup-vnc.sh
这将安装:
- Xvfb 虚拟显示器 :99
- 最小化 XFCE 桌面(xfwm4 + 面板,无 xfdesktop)
- x11vnc 带稳定性参数
- noVNC 浏览器访问
所有服务开机自启,崩溃自动重启。
操作参考
| 操作 | 脚本 | 参数 | 描述 |
|---|---|---|---|
| screenshot | screenshot.sh |
— | 截屏 → base64 PNG |
| cursor_position | cursor_position.sh |
— | 获取当前鼠标 X,Y 坐标 |
| mouse_move | mouse_move.sh |
x y | 移动鼠标到指定坐标 |
| left_click | click.sh |
x y left | 左键点击 |
| right_click | click.sh |
x y right | 右键点击 |
| middle_click | click.sh |
x y middle | 中键点击 |
| double_click | click.sh |
x y double | 双击 |
| triple_click | click.sh |
x y triple | 三击(选中行) |
| left_click_drag | drag.sh |
x1 y1 x2 y2 | 从起点拖拽到终点 |
| left_mouse_down | mouse_down.sh |
— | 按下鼠标键 |
| left_mouse_up | mouse_up.sh |
— | 释放鼠标键 |
| type | type_text.sh |
"text" | 输入文本(50字符分块,12ms延迟) |
| key | key.sh |
"combo" | 按键(Return, ctrl+c, alt+F4) |
| hold_key | hold_key.sh |
"key" secs | 按住按键指定时长 |
| scroll | scroll.sh |
dir amt [x y] | 向上/下/左/右滚动 |
| wait | wait.sh |
seconds | 等待后截屏 |
| zoom | zoom.sh |
x1 y1 x2 y2 | 裁剪区域截图 |
使用示例
export DISPLAY=:99
# 截屏
./scripts/screenshot.sh
# 点击坐标
./scripts/click.sh 512 384 left
# 输入文本
./scripts/type_text.sh "Hello world"
# 按键组合
./scripts/key.sh "ctrl+s"
# 向下滚动
./scripts/scroll.sh down 5
工作流程模式
- 截屏 — 始终先查看屏幕
- 分析 — 识别 UI 元素和坐标
- 操作 — 点击、输入、滚动
- 截屏 — 验证结果
- 重复
使用技巧
- 屏幕尺寸 1024x768,原点 (0,0) 在左上角
- 在文本框输入前先点击获取焦点
- 在浏览器中使用
ctrl+End跳转到页面底部 - 大多数操作在 2 秒延迟后自动截屏
- 长文本分块处理(50字符),按键延迟 12ms
实时桌面查看(VNC)
通过浏览器或 VNC 客户端实时观看桌面。
通过浏览器连接
# SSH 隧道(在本地机器运行)
ssh -L 6080:localhost:6080 your-server
# 在浏览器中打开
http://localhost:6080/vnc.html
通过 VNC 客户端连接
# SSH 隧道
ssh -L 5900:localhost:5900 your-server
# VNC 客户端连接到 localhost:5900
SSH 配置(推荐)
添加到 ~/.ssh/config 实现自动隧道:
Host your-server
HostName your.server.ip
User your-user
LocalForward 6080 127.0.0.1:6080
LocalForward 5900 127.0.0.1:5900
然后只需 ssh your-server,VNC 即可使用。
系统服务
# 检查状态
systemctl status xvfb xfce-minimal x11vnc novnc
# 需要时重启
sudo systemctl restart xvfb xfce-minimal x11vnc novnc
服务链
xvfb → xfce-minimal → x11vnc → novnc
- xvfb: 虚拟显示器 :99 (1024x768x24)
- xfce-minimal: 运行 xfwm4+panel 的看门狗,杀死 xfdesktop
- x11vnc: VNC 服务器,使用
-noxdamage提高稳定性 - novnc: WebSocket 代理,带心跳保持连接稳定
打开应用程序
export DISPLAY=:99
# Chrome — 仅在内核缺少用户命名空间支持时使用 --no-sandbox
# 检查: cat /proc/sys/kernel/unprivileged_userns_clone
# 1 = 沙箱可用,不要使用 --no-sandbox
# 0 = 沙箱失败,需要 --no-sandbox 作为后备
# 不必要时使用 --no-sandbox 会导致不稳定和崩溃
if [ "$(cat /proc/sys/kernel/unprivileged_userns_clone 2>/dev/null)" = "0" ]; then
google-chrome --no-sandbox &
else
google-chrome &
fi
xfce4-terminal & # 终端
thunar & # 文件管理器
注意: Snap 浏览器(Firefox、Chromium)在无头服务器上有沙箱问题。改用 Chrome .deb:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt-get install -f
手动设置
如果不想使用 setup-vnc.sh,可以手动设置:
# 安装软件包
sudo apt install -y xvfb xfce4 xfce4-terminal xdotool scrot imagemagick dbus-x11 x11vnc novnc websockify
# 运行设置脚本(生成 systemd 服务,屏蔽 xfdesktop,启动所有服务)
./scripts/setup-vnc.sh
如果更喜欢完全手动设置,setup-vnc.sh 脚本在内联生成所有 systemd 服务文件 — 阅读它获取确切的服务定义。
故障排除
VNC 显示黑屏
- 检查 xfwm4 是否运行:
pgrep xfwm4 - 重启桌面:
sudo systemctl restart xfce-minimal
VNC 闪烁/闪屏
- 确保 xfdesktop 已屏蔽(检查
/usr/bin/xfdesktop) - xfdesktop 在 Xvfb 上因 clear→draw 循环导致闪烁
VNC 频繁断开
- 检查 noVNC 是否有
--heartbeat 30参数 - 检查 x11vnc 是否有
-noxdamage参数
x11vnc 崩溃 (SIGSEGV)
- 添加
-noxdamage -noxfixes参数 - DAMAGE 扩展在 Xvfb 上会导致崩溃
依赖要求
由 setup-vnc.sh 安装:
xvfb xfce4 xfce4-terminal xdotool scrot imagemagick dbus-x11 x11vnc novnc websockify