在Windows中相信你们已经很熟悉使用Xmanager(Xshell), MobaXterm, SecureCRT经过X11实现Linux图形化界面显示,个人需求是在macOS下使用iTerm2做为Terminal实现X11图形化界面显示,网上大部分教程只提到安装Xquartz但并无结合实际问题给出完整的解决步骤,我把实践过程作了详细的记录方便你们按照最简单的步骤实现Linux图形化显示效果。html
2020年11月20日 - 初稿python
阅读原文 - https://wsgzao.github.io/post...linux
有些Linux服务器出于性能和效率的考虑,一般都是没有安装图形化界面的,那么图形化程序在服务器上压根儿就跑不起来,或者没法直接显示出来,这就很尴尬了!那么如何解决这个问题呢?能够基于X11 Forwarding技术 + MobaXterm/Xshell/SecureCRT/XQuartz 等第三方工具,就能够轻松搞定,是否是很简单?git
Linux 自己是没有图形化界面的,所谓的图形化界面系统只不过中 Linux 下的应用程序。这一点和 Windows 不同。Windows 从 Windows 95 开始,图形界面就直接在系统内核中实现了,是操做系统不可或缺的一部分。Linux 的图形化界面,底层都是基于 X 协议。github
X 协议由 X server 和 X client 组成:chrome
举个例子,若是用户点击了鼠标左键,由于鼠标归 X server 管理,因而 X server 就捕捉到了鼠标点击这个动做,而后它将这个动做告诉 X client,由于 X client 负责程序逻辑,因而 X client 就根据程序预先设定的逻辑(例如画一个圆),告诉 X server说:“请在鼠标点击的位置,画一个圆”。最后,X server 就响应 X client 的请求,在鼠标点击的位置,绘制并显示出一个圆。shell
这么绕,有啥意义呢?固然有!ubuntu
许多时候 X server 和 X client 在同一台主机上,这看起来没什么。可是, X server 和 X client 彻底能够运行在不一样的机器上,只要彼此经过 X 协议通讯便可。因而,咱们就能够作一些“神奇”的事情,好比像本文开头谈到的,在本地显示 (X server),运行在服务器上的 GUI 程序 (X client)。这样的操做能够经过 SSH X11 Forwarding (转发) 来实现。centos
X11 中的 X 指的就是 X 协议,11 指的是采用 X 协议的第 11 个版本。浏览器
# 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
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
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.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.
terminal
or iterm
, use flag -Y
instead of -X
with ssh
.ssh -Y user@address
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.
DISPLAY
?Please see https://askubuntu.com/a/43225...
-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
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