安装

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

macOS 使用 X11 运行远端 linux 中的 x11 client 图形程序相关推荐

  1. Linux中crontab无法执行java程序的问题

    Linux中crontab无法执行java程序的问题 jdk环境变量配置问题:可以用全路径来解决: 竟然是kill -9之后,后面的脚本都不运行,这个还没有找到原因啊: 即便是系统级别的 ls 命令, ...

  2. linux dhcp 绑定mac地址,●DHCP协议的功能是 (58) 。在Linux中提供DHCP服务的程序是 (59) ;DHCP服务将主机的MAC地址和IP地 - 赏学吧...

    ●DHCP协议的功能是 (58) .在Linux中提供DHCP服务的程序是 (59) :DHCP服务将主机的MAC地址和IP地址绑定在一起的方法是在 (60) 文件中添加"host主机名{h ...

  3. Linux如何运行pipe1,Linux中的管道

    一.管道是进程间通信的一种重要手段,在linux中没有使用专门的数据结构,而是借助了文件系统的file结构和VFS索引节点inode.通过两个file结构指向同一个临时的VFS索引节点,而这个索引节点 ...

  4. linux php运行用户,Linux中普通用户如何以root身份运行命令

    Linux遵循非常艰难的权限模型,root用户可以执行任何操作:但普通用户没有权限,无法运行任何命令.下面本篇文章就来给大家介绍普通用户以root身份运行命令的方法,希望对大家有所帮助. 方法一:使用 ...

  5. linux代码怎么运行gedit,Linux中gedit命令起什么作用呢?

    摘要: 下文讲述Linux中gedit的功能说明,如下所示: gedit命令是GNOME桌面环境中的官方文本编辑器 gedit命令功能: 用于创建和编辑各种文本文件 gedit命令的语法格式: ged ...

  6. linux运行echo,Linux中echo命令起什么作用呢?

    摘要: 下文讲述Linux中echo的功能说明,如下所示: echo命令功能: 1.用于向终端设备上输出字符串 2.输出变量值 echo命令的语法格式: echo [参数] [字符串] -----常用 ...

  7. linux如何运行sshpass,Linux中sshpass命令起什么作用呢?

    摘要: 下文讲述Linux中sshpass的功能说明,如下所示: sshpass命令功能: 用于非交互的ssh 密码验证 可直接使用密码进行远程连接 和远程获取文件 sshpass命令的语法格式: s ...

  8. DNF可以在Linux系统运行吗,Linux中dnf命令起什么作用呢?

    摘要: 下文讲述Linux中dnf的功能说明,如下所示: dnf简介: dnf是一个新的rpm软件包管理器,它出现的目的是取代yum dnf在用户体验,内存占用,依赖分析,运行速度等方面进行了性能提升 ...

  9. Linux中包的管理与程序安装

    包是捆绑在一起并准备安装在 Linux 系统上的软件,安装和卸载包是任何 Linux 用户和管理员的基本操作.本篇将提供 Red Hat Package Manager(RPM)和 YUM 软件管理工 ...

最新文章

  1. python 编码问题
  2. pwn(ctf)中常见的系统调用
  3. 解决最短路径的Dijkstra算法详解,附加Java代码
  4. POJ3734-Blocks【EGF】
  5. jmeter之录制控制器与代理的使用
  6. 通过IDE生成和手动call调用webservice
  7. phpmyadmin的安装和使用
  8. android 电池(三):android电池系统
  9. iOS之app审核遭遇5.2.1的条例被拒
  10. Json for the .NET Compact Framework
  11. 11. Window open() 方法
  12. 如何使用git把本地代码上传(更新)到github上
  13. 数车计算机编程教学caxa,caxa数控车视频教程软件编程全套2016/2015
  14. SPSS数据分析全套教程(2)—— 变量处理
  15. scratch编程我的世界3D史蒂夫
  16. 除权除息,前复权,后复权,不复权
  17. 保研er:9月,我太南了...
  18. golang直接调用ffmpeg预编译类库(windows)
  19. qq邮箱如何在win10邮箱连接到服务器,Win10系统自带Mail应用添加QQ邮箱的方法
  20. (毕业设计资料)基于单片机GPS轨迹定位和里程统计系统

热门文章

  1. 休眠(Hibernate)和睡眠(Sleep)的区别
  2. js 字符串转化成数字
  3. PHP判断用户是否已经登录,如果登录则显示首页,如果未登录则进入登录页面或注册页面
  4. 阿尔法元并未否定AI需要“人类师父”
  5. STM32+OpenMV+AS608实现人脸识别
  6. 分布式系统中的一致性模型
  7. Android 第三方库AgentWeb的使用
  8. 自闭症青年的突显网络、默认模式网络和中央执行网络功能连接的差异
  9. vue项目搭建和配置
  10. DOS命令批处理 及 硬盘修复