10

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

 4 years ago
source link: https://wsgzao.github.io/post/x11/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在 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 等第三方工具,就可以轻松搞定,是不是很简单?

20201120140500.png

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

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

  • X server 管理主机上与显示相关的硬件设置(如显卡、硬盘、鼠标等),它负责屏幕画面的绘制与显示,以及将输入设置(如键盘、鼠标)的动作告知 X client。
  • X client (即 X 应用程序) 则主要负责事件的处理(即程序的逻辑)。

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

X11 Forwarding

20201120140613.png

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

许多时候 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 图形化界面显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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 [email protected]
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
20201120142156.png
20201120143321.png

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.
1
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.

1
2
3
4
# 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.
1
ssh -Y user@address
  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:

1
2
3
4
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 实现图形化界面显示


Recommend

  • 75

    centos6.9在命令行界面安装图形化桌面一、Linux桌面环境    X Windows系统        控制视频环境的两个基本元素是PC中的显卡和监视器,要实现图形化就要了解这两个元素。X Windows软件就是展现图形的核心元素。X Windows核心软件仅仅

  • 48

    问题描述:-Redhat 6.6Mini无法用自带yum仓库安装图形化界面;-执行Yum操作均提示This system is not registered to Red Hat Subscription Management-不断调整yum仓库方式,依旧提示Redhat默认自带的Yum源需要注册等;具体提示如下:This system is not registere...

  • 34

    把其中的id:3:initdefault中的3改为5,这样以后系统启动后将直接进入文本模式。 以下是配置引导进入图形模式的etc/inittab文件的部分内容:  #Default runlevel.The runlevels used by RHS are:   # 0-halt(Do NOT set initdefa...

  • 15

    JDK 内置图形界面工具:海阔凭鱼跃,天高任鸟飞

  • 9

    很多人都吐槽,使用 Tkinter、PyQt5等工具制作出来的图形界面程序太丑了。既然觉得它丑,我们来想想,它为什么会那么丑。 文章目录 [显示] 功能性是开发的第一要务 ...

  • 12

    Python编写的桌面图形界面程序实现更新检测和下载安装 2020/6/29 0 人评论 8,015 次阅读 在Python中我们有很多种方案来...

  • 10
    • zmister.com 4 years ago
    • Cache

    Kivy 图形界面开发初体验

    Kivy 图形界面开发初体验 2021/1/27 0 人评论 910 次阅读 本文是觅道文档连载教程《使用Kivy构建现代桌...

  • 12

    Kivy 5种常用图形界面布局大赏 2021/3/2 0 人评论 660 次阅读 对于任何图形界面的框架而言,布局都是非常重要的一个组...

  • 6

    文章目录 [显示] 电脑里的文件积累得越来越多,想要快速找一个文件就是一个麻烦事儿。 一个麻烦在于不知道文件在什么地方,这可以通过系统自带的文件管理器或者第三方的文件搜...

  • 5

    一般来说,购买云服务器的用途是将其作为服务,比如构建web网站、博客、小程序、API服务等。这些场景都不需要服务器操作系统的图形界面,直接将相应的服务部署在云服务器上即可;而当前遇到的一个问题是:仍然需要通过云服务器来提供服务(即提供API接口),但背后的...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK