macOS 使用 XQuartz 支持 X11 实现 Linux 图形化界面显示

2020-11-25 11:13:59 +08:00
 wsgzao

前言

在 Windows 中相信大家已经很熟悉使用 Xmanager(Xshell), MobaXterm, SecureCRT 通过 X11 实现 Linux 图形化界面显示,我的需求是在 macOS 下使用 iTerm2 作为 Terminal 实现 X11 图形化界面显示,网上大部分教程只提到安装 Xquartz 但并没有结合实际问题给出完整的解决步骤,我把实践过程做了详细的记录方便大家按照最简单的步骤实现 Linux 图形化显示效果。

更新历史

2020 年 11 月 20 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/x11/


X11 介绍

有些 Linux 服务器出于性能和效率的考虑,通常都是没有安装图形化界面的,那么图形化程序在服务器上压根儿就跑不起来,或者无法直接显示出来,这就很尴尬了!那么如何解决这个问题呢?可以基于 X11 Forwarding 技术 + MobaXterm/Xshell/SecureCRT/XQuartz 等第三方工具,就可以轻松搞定,是不是很简单?

X 协议

Linux 本身是没有图形化界面的,所谓的图形化界面系统只不过中 Linux 下的应用程序。这一点和 Windows 不一样。Windows 从 Windows 95 开始,图形界面就直接在系统内核中实现了,是操作系统不可或缺的一部分。Linux 的图形化界面,底层都是基于 X 协议。

X 协议由 X server 和 X client 组成:

举个例子,如果用户点击了鼠标左键,因为鼠标归 X server 管理,于是 X server 就捕捉到了鼠标点击这个动作,然后它将这个动作告诉 X client,因为 X client 负责程序逻辑,于是 X client 就根据程序预先设定的逻辑(例如画一个圆),告诉 X server 说:“请在鼠标点击的位置,画一个圆”。最后,X server 就响应 X client 的请求,在鼠标点击的位置,绘制并显示出一个圆。

X11 Forwarding

这么绕,有啥意义呢?当然有!

许多时候 X server 和 X client 在同一台主机上,这看起来没什么。但是,X server 和 X client 完全可以运行在不同的机器上,只要彼此通过 X 协议通信即可。于是,我们就可以做一些“神奇”的事情,比如像本文开头谈到的,在本地显示 (X server),运行在服务器上的 GUI 程序 (X client)。这样的操作可以通过 SSH X11 Forwarding (转发) 来实现。

X11 中的 X 指的就是 X 协议,11 指的是采用 X 协议的第 11 个版本。

macOS 实现 X11 图形化界面显示

# macOS 安装 xquartz
brew cask install xquartz

# 启动 xquartz,实测`Allow connections from clients`选项非必须条件
Run Applications > Utilities > XQuartz.app

# 设置 DISPLAY 环境变量
export DISPLAY=:0

# 没有使用 xquartz 中 terminal 的话不会自动设置 DISPLAY 环境变量,可能会出现以下错误
[root@VM-2-11-centos ~]# firefox
Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
Running without a11y support!
Error: no DISPLAY environment variable specified

# ssh 添加-Y flag 登录远程主机
ssh -Y user@host

# 登录成功后可能出现以下错误,安装 xauth 即可解决
ssh -Y root@192.168.117.148
X11 forwarding request failed on channel 0

# 远程主机安装 xauth,以 centos 为例,使用 xclock 可以测试图形化效果
yum install -y xauth xclock
xclock

# 如果需要浏览器支持安装 firefox 或者 chrome 即可
yum install firefox
firefox

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
/usr/bin/google-chrome-stable %U --no-sandbox

Steps

  1. Install Xquartz to get X11 support on MacOS. You can google Xquartz and download it from its official site, or install using HomeBrew.
brew cask install xquartz
  1. Launch Xquartz. Go to Preference -> Security, click the box Allow connections from clients. NOTE: You have to lauch Xquartz with Allow connections from clients enable everytime you want to ssh to remote server with X11 forwarding support.

  2. Lauch terminal or iterm. Add environment DISPLAY.

# to add an environment entry only working on current terminal, use `export`
export DISPLAY=:0
# to add an environment entry working on every terminal,
# append `export DISPLAY=:0` to `.bashrc` or `.zshrc` in case you use zsh.
  1. Lauch terminal or iterm, use flag -Y instead of -X with ssh.
ssh -Y user@address

FAQ

  1. Why do I need to install Xquartz?

From https://stackoverflow.com/a/50182736/6769366

XQuartz is standard. It used to come bundled with the OS, but Apple removed it back around Mavericks.

  1. Why do I have to add environment DISPLAY?

Please see https://askubuntu.com/a/432257/745885

  1. Why do I have to use -Y instead of -X?

I got the following error when trying to run a python script which draws some curves using matplotlib:

X Error of failed request:  BadAccess (attempt to access private resource denied)
Major opcode of failed request:  18 (X_ChangeProperty)
Serial number of failed request:  12
Current serial number in output stream:  15

This problem is sovled when using -Y instead of -X. Haven't got enough time to find exact explanation, just post a link for those who are curious:

Can't run “ssh -X” on MacOS Sierra

  1. How to fix X11 forwarding request failed on channel 0?

Install X authority file utility

sudo yum install xauth

ssh returns message “X11 forwarding request failed on channel 0”

参考文章

Enable X11 forward to load images from remote server on MacOS Mojave

linux 服务器通过 X11 实现图形化界面显示

2333 次点击
所在节点    macOS
7 条回复
IgniteWhite
2020-11-25 11:23:25 +08:00
不错的入门教程。进阶一点的应用可以有 tmux 和 vim 的剪切板同步
clyecao
2020-11-25 13:31:32 +08:00
很详细的教程,正有这个需求。感谢分享。
choury
2020-11-25 18:05:42 +08:00
用这玩意路还不如开个 linux 的虚拟机,之前折腾过,glxgears 都跑不了,支持的很残缺
abbottcn
2020-11-25 19:18:45 +08:00
No need to set DISPLAY.

ssh -X -Y will set everything you need.
IgniteWhite
2020-11-25 19:37:37 +08:00
@abbottcn 如果用 tmux 的话,re-attach session 要专门设置 DISPLAY 的。还有很多其他情况。
NilChan
2020-11-25 22:18:42 +08:00
@choury 这个是用于远程的,不需要装 vnc 之类的远程桌面。
Tink
2020-11-25 22:33:24 +08:00
额,我能说我挂学习强国就是用 x11 把树莓派 forward 到本机上弄得么

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/729024

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX