macOS 使用 X11 运行远端 linux 中的 x11 client 图形程序
创始人
2024-04-14 05:39:05
0

安装

brew install --cask xquartz

配置

打开 xquartz,新建一个终端,然后执行 xhost + 开放权限。

linux 中 sshd 配置

/etc/ssh/sshd_config 中开启如下配置后重启 sshd:

X11Forwarding yes

使用普通用户连接到远端 linux 中

ssh 命令中添加 -X 参数,未开启 X11Forwarding 时报错信息如下:

longyudeMacBookPro:~ longyu$ ssh -X  longyu@debian
X11 forwarding request failed on channel 0

开启后,ssh -X 成功建立隧道,此时 netstat -anp 查看 sshd 相关连接信息,能够发现如下内容:

tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      33315/sshd: longyu@ 
tcp        0      0 192.168.31.197:22       192.168.31.66:51014     ESTABLISHED 33306/sshd: longyu  
tcp6       0      0 ::1:6010                :::*                    LISTEN      33315/sshd: longyu@

sshd 监听本地 6010 端口,接收任意 ipv4、ipv6 地址访问数据来进行 X11 转发。

此时在 ssh 连接成功的终端执行 xclock 命令,然后重新查看端口信息,发现增加如下连接:

tcp6       0      0 ::1:6010                ::1:54714               ESTABLISHED 33315/sshd: longyu@

此条连接建立后,xclock 通过此条通道走已经建立的 ssh 隧道转发 X11 图形界面数据到 MacOS,MacOS 中会自动打开 xquartz,弹出 xclock 的图形界面。

strace 跟踪 X11 客户端程序,过滤到如下 connect 连接调用,与上面的连接匹配:

connect(3, {sa_family=AF_INET, sin_port=htons(6010), sin_addr=inet_addr("127.0.0.1")}, 16) = 0

使用 root 用户打开

当图形界面程序需要使用 root 权限打开时,可以执行 ssh -X root@xxx 连接到 linux 中,然后在终端直接运行相关程序即可。

debian sshd maunal 中的信息

X11 FORWARDINGIf the ForwardX11 variable is set to “yes” (or see the description of the -X, -x, and -Y options above) and the user is using X11 (theDISPLAY environment variable is set), the connection to the X11 display is automatically forwarded to the remote side in such a way thatany X11 programs started from the shell (or command) will go through the encrypted channel, and the connection to the real X server willbe made from the local machine.  The user should not manually set DISPLAY.  Forwarding of X11 connections can be configured on the com‐mand line or in configuration files.The DISPLAY value set by ssh will point to the server machine, but with a display number greater than zero.  This is normal, and happensbecause ssh creates a “proxy” X server on the server machine for forwarding the connections over the encrypted channel.ssh will also automatically set up Xauthority data on the server machine.  For this purpose, it will generate a random authorizationcookie, store it in Xauthority on the server, and verify that any forwarded connections carry this cookie and replace it by the realcookie when the connection is opened.  The real authentication cookie is never sent to the server machine (and no cookies are sent inthe plain).If the ForwardAgent variable is set to “yes” (or see the description of the -A and -a options above) and the user is using an authenti‐cation agent, the connection to the agent is automatically forwarded to the remote side.

注意事项

网上搜索到的一些博客中都提到要设置 DISPLAY 参数,实际测试发现设置了后反而不能正常工作。在我的 debian 机器上查看已经运行的 X11 程序的环境变量,发现普通用户 DISPLAY 变量的值为 localhost:10.0, root 用户的值为 localhost:11.0。

测试表明 ssh 会自动设置 DISPLAY 环境的值,用户不需要单独设置。同时 ssh 设置的值中 ip 并非目的 X11 server 机器的 ip,理解 X11 forwarding 实际是先本地转发到 6010 端口,然后通过已经建立的 ssh 隧道转发到 X11 server 机器上进行显示。

当设置 DISPLAY 为 192.168.44.33:0.0 时,运行 X11 client 程序会执行如下系统调用:

connect(3, {sa_family=AF_INET, sin_port=htons(6000), sin_addr=inet_addr("192.168.44.33")},

确认 xquartz 并不会监听 6000 端口,此 connect 会超时失败,程序终止。在 Mac 上查看到 xquartz 启动参数如下:

/opt/X11/bin/Xquartz :0 -nolisten tcp -iglx -auth /Users/longyu/.serverauth.2603

猜测是 -nolisten tcp 参数的影响,修改 /opt/X11/bin/startx 中的逻辑启用 listen tcp 参数,直接执行 startx 命令,能够看到如下两条监听信息:

X11.bin   3431 longyu    9u  IPv4 0xf75fbac47759271d      0t0  TCP *:6000 (LISTEN)
X11.bin   3431 longyu   12u  IPv6 0xf75fbac947417cd5      0t0  TCP *:6000 (LISTEN)

此时仍旧设置了 DISPLAY 后 connect 6000 端口成功,随后 connect ip+6010 地址会失败,而 X11.bin 并不会监听 6010 端口,这个 6010 端口网上搜索到可能与 sshd 配置中的 X11DisplayOffset 10 配置有关,暂且打住。

参考链接

http://t.zoukankan.com/zafu-p-15323704.html

相关内容

热门资讯

山东威海虐狗事件后续 山东威海虐狗事件后续山东威海虐狗事件后续... 山东威海虐狗事件后续 展开 这件事看出来我们中...
博越车上的音效均衡器怎么调整,... 博越车上的音效均衡器怎么调整,音乐才能好听首先需要一个专业修改过的高音质U盘,比普通歌的音质要高6倍...
天涯明月刀、太白用什么心法和石... 天涯明月刀、太白用什么心法和石头、非土豪!现在60级用的归翘、严师、白刃。 太白的主要属性是洞察和力...
华帝抽油烟机i字头和e字头产品... 华帝抽油烟机i字头和e字头产品那个好华帝抽油烟机i字头和e字头产品那个好e好,i是网上型号,e是实体...
竹笛伴我 四十载 转自:衢州日报  厉向军  搬家数次,许多旧物已离我而去,这支竹笛如影随形,伴我走过了四十年的风雨历...
从绰尔河到西辽河的千钧之诺 转自:内蒙古日报□本报记者  霍晓庆  高敏娜  2025年6月30日,内蒙古迄今为止投资规模最大的...
我区420家企业享受研发投入后... 转自:内蒙古日报本报7月6日讯  (记者  白莲)记者从自治区科技厅获悉,自治区科技厅日前会同财政厅...
兴证全球基金杨世进:秉持缓进之...   很会学、很谦虚、成绩好,是为“三好”。同事称赞道,在长达十五年的投研生涯中,兴证全球基金杨世进始...
个人账户余额可跨省转账   三湘都市报7月6日讯 在湖南工作的参保职工,可以给外省的家人转账医保个人账户资金了。记者刚刚从湖...
蜈蚣坝伏击战:大青山铭记的烽火... 转自:内蒙古日报□本报记者  宋爽  通讯员  陈艳平   夏风拂过蜈蚣坝顶,一块沉静的石碑吸引着过...