V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wsgzao
V2EX  ›  macOS

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

  •  
  •   wsgzao · 2020-11-25 11:13:59 +08:00 · 2302 次点击
    这是一个创建于 1219 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前言

    在 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 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 安装 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
    
    

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

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

    ssh -X -Y will set everything you need.
    IgniteWhite
        5
    IgniteWhite  
       2020-11-25 19:37:37 +08:00 via iPhone
    @abbottcn 如果用 tmux 的话,re-attach session 要专门设置 DISPLAY 的。还有很多其他情况。
    NilChan
        6
    NilChan  
       2020-11-25 22:18:42 +08:00 via Android
    @choury 这个是用于远程的,不需要装 vnc 之类的远程桌面。
    Tink
        7
    Tink  
       2020-11-25 22:33:24 +08:00 via Android
    额,我能说我挂学习强国就是用 x11 把树莓派 forward 到本机上弄得么
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3263 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:07 · PVG 22:07 · LAX 07:07 · JFK 10:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.