前言

在Windows中置信大家曾经很相熟应用Xmanager(Xshell), MobaXterm, SecureCRT通过X11实现Linux图形化界面显示,我的需要是在macOS下应用iTerm2作为Terminal实现X11图形化界面显示,网上大部分教程只提到装置Xquartz但并没有结合实际问题给出残缺的解决步骤,我把实际过程做了具体的记录不便大家依照最简略的步骤实现Linux图形化显示成果。

更新历史

2020年11月20日 - 初稿

浏览原文 - https://wsgzao.github.io/post...


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 client。
  • X client (即 X 应用程序) 则次要负责事件的解决(即程序的逻辑)。

举个例子,如果用户点击了鼠标左键,因为鼠标归 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装置xquartzbrew 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 ~]# firefoxFailed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11Running without a11y support!Error: no DISPLAY environment variable specified# ssh增加-Y flag登录近程主机ssh -Y user@host# 登录胜利后可能呈现以下谬误,装置xauth即可解决ssh -Y root@192.168.117.148X11 forwarding request failed on channel 0# 近程主机装置xauth,以centos为例,应用xclock能够测试图形化成果yum install -y xauth xclockxclock# 如果须要浏览器反对装置firefox或者chrome即可yum install firefoxfirefoxyum 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/5...

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/43225...

  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:  12Current 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实现图形化界面显示