第一步:禁用 Wayland,切换到 Xorg
Ubuntu 24.04 默认使用 Wayland,x11vnc 需要 Xorg 才能工作,必须先禁用 Wayland。
sudo vim /etc/gdm3/custom.conf
找到这一行,去掉注释:
WaylandEnable=false
保存后重启或重新登录。登录界面右下角选 "Ubuntu on Xorg"。
第二步:安装 x11vnc
sudo apt update
sudo apt install x11vnc -y
第三步:设置 VNC 密码
x11vnc -storepasswd ~/.vnc/passwd
第四步:启动 VNC
x11vnc -auth guess -forever -loop -noxdamage -repeat \
-rfbauth ~/.vnc/passwd -rfbport 5900 -shared -bg
连接地址:你的IP:5900
第五步(可选):设置开机自启
创建 systemd 服务文件:
sudo nano /etc/systemd/system/x11vnc.service
内容如下(替换 yourusername):
[Unit]
Description=Start x11vnc at startup
After=graphical.target
[Service]
Type=simple
User=yourusername
ExecStart=/usr/bin/x11vnc -auth /home/yourusername/.Xauthority \
-forever -loop -noxdamage -repeat \
-rfbauth /home/yourusername/.vnc/passwd -rfbport 5900 -shared
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable x11vnc
sudo systemctl start x11vnc
如果是多屏幕只共享其中一个
-clip 裁剪区域
xrandr
比如输出:
Screen 0: minimum 320 x 200, current 3840 x 1419, maximum 16384 x 16384
eDP-1 connected primary 1920x1200+1920+219 (normal left inverted right x axis y axis) 344mm x 215mm
2560x1600 120.01 + 120.01 60.00
2560x1440 120.01
2048x1536 120.01
1920x1440 120.01
1856x1392 120.01
1792x1344 120.01
2048x1152 120.01
1920x1200 120.01*
...
如果想只共享Screen 0, 则使用vnc启动命令为:
x11vnc -clip 1920x1200+1920+219 \
-auth guess -forever -loop -noxdamage -repeat \
-rfbauth ~/.vnc/passwd -rfbport 5903 -shared -bg
补充
禁用客户端鼠标覆盖, 为了显示服务器鼠标可以增加-nocursorshape -cursorpos参数
比如
x11vnc -clip 1920x1080+0+0 \
-auth guess -forever -loop -repeat \
-rfbauth ~/.vnc/passwd -rfbport 5903 -shared -bg \
-nocursorshape -cursorpos \
-wait 5 -defer 5
当开启vnc server后,如果你想用windows rdp连接, 而不是vnc客户端, 你可以在使用xrdp,
这样 xrdp 只是作为 RDP→VNC 的协议转换层,底层还是你的 x11vnc。
先安装xrdp sudo apt-get install xrdp
配置方式
编辑 /etc/xrdp/xrdp.ini,使用 vnc 类型指向你现有的 X 会话:
[xrdp1]
name=VNC to existing session
lib=libvnc.so
ip=127.0.0.1
port=5903 # 指向你本地已有的 x11vnc 端口
username=na
password=ask
然后启动
sudo systemctl restart xrdp
客户端直接用 Windows 自带的 mstsc.exe 连接,体验和 RDP 几乎一致,光标、剪贴板、音频都原生支持。为了显示效果, 可能需要客户端设置与vncserver分辨率中的参数保持一致
评论 · 0
发表评论