默默守候着伱 發表於 2019-9-30 15:04:00

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

<h1><span style="font-family: &quot;times new roman&quot;, times; font-size: 18px">1&nbsp;<strong>背景描述</strong></span></h1>
<p><span style="font-family: &quot;times new roman&quot;, times">有些LINUX服务器出于性能和效率的考虑,通常都是没有安装图形化界面的,那么图形化程序在服务器上压根儿就跑不起来,或者无法直接显示出来,这就很尴尬了!那么如何解决这个问题呢?可以基于X11 Forwarding技术&nbsp;+ MobaXterm&nbsp;工具,就可以轻松搞定,似不似很简单?</span></p>
<h1><span style="font-family: &quot;times new roman&quot;, times; font-size: 18px">2&nbsp;<strong>原理介绍</strong></span></h1>
<h2><span style="font-family: &quot;times new roman&quot;, times; font-size: 16px">2.1&nbsp;<strong>X协议</strong></span></h2>
<p><span style="font-family: &quot;times new roman&quot;, times">&nbsp;<img src="https://images2018.cnblogs.com/blog/1340101/201803/1340101-20180312101557897-242719802.png"></span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">&nbsp;</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">Linux&nbsp;本身是没有图形化界面的,所谓的图形化界面系统只不过中&nbsp;Linux&nbsp;下的应用程序。这一点和&nbsp;Windows&nbsp;不一样。Windows&nbsp;从Windows 95&nbsp;开始,图形界面就直接在系统内核中实现了,是操作系统不可或缺的一部分。Linux&nbsp;的图形化界面,底层都是基于&nbsp;X&nbsp;协议。</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">X&nbsp;协议由&nbsp;X server&nbsp;和&nbsp;X client&nbsp;组成:</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">l&nbsp;X server&nbsp;管理主机上与显示相关的硬件设置(如显卡、硬盘、鼠标等),它负责屏幕画面的绘制与显示,以及将输入设置(如键盘、鼠标)的动作告知&nbsp;X client。</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">l&nbsp;X client (即&nbsp;X&nbsp;应用程序)&nbsp;则主要负责事件的处理(即程序的逻辑)。</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">举个例子,如果用户点击了鼠标左键,因为鼠标归&nbsp;X server&nbsp;管理,于是&nbsp;X server&nbsp;就捕捉到了鼠标点击这个动作,然后它将这个动作告诉&nbsp;X client,因为&nbsp;X client&nbsp;负责程序逻辑,于是&nbsp;X client&nbsp;就根据程序预先设定的逻辑(例如画一个圆),告诉&nbsp;X server说:“请在鼠标点击的位置,画一个圆”。最后,X server&nbsp;就响应&nbsp;X client&nbsp;的请求,在鼠标点击的位置,绘制并显示出一个圆。</span></p>
<h2><span style="font-family: &quot;times new roman&quot;, times; font-size: 16px">2.2&nbsp;<strong>X11 Forwarding</strong></span></h2>
<p><span style="font-family: &quot;times new roman&quot;, times">&nbsp;<img src="https://images2018.cnblogs.com/blog/1340101/201803/1340101-20180312101604924-1300454876.png"></span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">&nbsp;</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">这么绕,有啥意义呢?当然有!</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">许多时候&nbsp;X server&nbsp;和&nbsp;X client&nbsp;在同一台主机上,这看起来没什么。但是,&nbsp;X server&nbsp;和&nbsp;X client&nbsp;完全可以运行在不同的机器上,只要彼此通过&nbsp;X&nbsp;协议通信即可。于是,我们就可以做一些“神奇”的事情,比如像本文开头谈到的,在本地显示&nbsp;(X server),运行在服务器上的&nbsp;GUI&nbsp;程序&nbsp;(X client)。这样的操作可以通过&nbsp;SSH X11 Forwarding (转发)&nbsp;来实现。</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">X11&nbsp;中的&nbsp;X&nbsp;指的就是&nbsp;X&nbsp;协议,11&nbsp;指的是采用&nbsp;X&nbsp;协议的第&nbsp;11&nbsp;个版本。</span></p>
<h2><span style="font-family: &quot;times new roman&quot;, times; font-size: 16px">2.3&nbsp;<strong>MobaXterm</strong></span></h2>
<p><span style="font-family: &quot;times new roman&quot;, times">&nbsp;<img src="https://images2018.cnblogs.com/blog/1340101/201803/1340101-20180312101609807-295574460.png"></span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">&nbsp;</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">那&nbsp;MobaXterm&nbsp;又是什么鬼?MobaXterm&nbsp;是一款开源、免费的、全功能终端软件。它与&nbsp;PuTTY&nbsp;类似,但却比&nbsp;PuTTY&nbsp;要强大得多,其中一个很实用的功能就是&nbsp;MobaXterm&nbsp;自带&nbsp;X Server。这样我们就不用劳神地去想怎么在&nbsp;Windows&nbsp;上启动&nbsp;X server&nbsp;了。</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">小结一下,整个实现逻辑就是:</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">本地机器采用&nbsp;MobaXterm (自带&nbsp;X server)&nbsp;连接远程服务器。然后,在服务器上运行&nbsp;GUI&nbsp;程序&nbsp;(即&nbsp;X client),通过&nbsp;SSH X11 Forwarding,转发到本地&nbsp;(Windows&nbsp;机器上)。</span></p>
<h1><span style="font-family: &quot;times new roman&quot;, times; font-size: 18px">3&nbsp;<strong>实战介绍</strong></span></h1>
<h2><span style="font-family: &quot;times new roman&quot;, times; font-size: 16px">3.1&nbsp;<strong>服务器端(LINUX7.x)</strong></span></h2>
<h3><span style="font-family: &quot;times new roman&quot;, times; font-size: 15px">3.1.1&nbsp;<strong>修改SSH配置</strong></span></h3>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"><span style="font-family: &quot;times new roman&quot;, times"># cat /etc/ssh/sshd_config

X11Forwarding yes

X11UseLocalhost no -- 禁止将X11转发请求绑定到本地回环地址上

AddressFamily inet -- 强制使用IPv4通道
</span></pre>
</div>
<h3><span style="font-family: &quot;times new roman&quot;, times; font-size: 15px">3.1.2&nbsp;<strong>重启SSH服务</strong></span></h3>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"><span style="font-family: &quot;times new roman&quot;, times">service ssh restart
</span></pre>
</div>
<h3><span style="font-family: &quot;times new roman&quot;, times; font-size: 15px">3.2.1&nbsp;Windows下连接Linux的ssh工具—MobaXterm</span></h3>
<p><span style="font-family: &quot;times new roman&quot;, times">作为一个Windows操作系统用户,如果想要连接Linux服务器来进行文件之间的传送,那么需要一个Secure Shell软件(简称SSH的)。</span><br><span style="font-family: &quot;times new roman&quot;, times">MobaXterm,是一款增强型终端、X服务器和Unix命令集(GNU/ Cygwin)封装在一个单一的便携式exe文件。MobaXterm可以开启多个终端视窗,以最新的X服务器为基础的X.Org,可以轻松地来试用Unix / Linux上的GNU Unix命令。这样一来,我们可以不用安装虚拟机来试用虚拟环境,然后只要通过MobaXterm就可以使用大多数的linux命令。</span><br><span style="font-family: &quot;times new roman&quot;, times">它不仅可以像PuTTY一样通过SSH连接Raspberry Pi等开源硬件,并且还能:&nbsp;</span></p>
<ul>
<li><span style="font-family: &quot;times new roman&quot;, times">内建多标签和多终端分屏&nbsp;</span></li>
<li><span style="font-family: &quot;times new roman&quot;, times">内建SFTP文件传输&nbsp;</span></li>
<li><span style="font-family: &quot;times new roman&quot;, times">内建X server,可远程运行X窗口程序&nbsp;</span></li>
<li><span style="font-family: &quot;times new roman&quot;, times">直接支持VNC/RDP/Xdmcp等远程桌面&nbsp;</span></li>
<li><span style="font-family: &quot;times new roman&quot;, times">默认的UTF-8编码&nbsp;</span></li>
<li><span style="font-family: &quot;times new roman&quot;, times">更加友好的串口连接设置&nbsp;</span></li>
<li><span style="font-family: &quot;times new roman&quot;, times">操作更明确,更少的“神秘技巧”</span></li>
</ul>
<p><strong><span style="font-family: &quot;times new roman&quot;, times">什么是X11&nbsp;</span></strong></p>
<p><span style="font-family: &quot;times new roman&quot;, times">X Window System 常被简称为X11或X,其功能包括窗口的绘制、移动,以及与鼠标、键盘等输入设备的交互。相信很多新手都知道startx这个命令,没有X11的话就只能对着黑乎乎的屏幕敲命令了。&nbsp;</span><br><span style="font-family: &quot;times new roman&quot;, times">它采用C/S模型:一个X server 和多个应用程序(client)通信。server接收client的请求绘制窗口,并将来自鼠标、键盘等设备的输入传递给client。&nbsp;</span><br><span style="font-family: &quot;times new roman&quot;, times">X server和client可以位于同一计算机上,就类似于平时使用的Windows操作系统;当X server和client不在同一计算机时,使用本地的X server 进行绘制、交互,就变成了远程桌面。&nbsp;</span><br><span style="font-family: &quot;times new roman&quot;, times">举个例子:前者是你在披萨店点了一份披萨,店员在你旁边帮你做好拿给你(这家店相当于同一台计算机);后者是你在网上叫了一份披萨,店员接单后到你家帮你做了一份披萨(注意:VNC是使用店里厨房,做好给你送过来;X转发是使用你家厨房做披萨)&nbsp;</span><br><span style="font-family: &quot;times new roman&quot;, times">不理解也没关系,会用能达到目的就好了。Via&nbsp;</span><br><span style="font-family: &quot;times new roman&quot;, times">常用X Server有免费的Xming、收费的Xmanager(非常棒的Xshell终端软件也是他家出的,家庭教育用户免费使用) 等;上述需要手动配置,然而有了MobaXterm,一切都变得超级简单!</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">软件可以通过官网进行下载:</span></p>
<h3><span style="font-family: &quot;times new roman&quot;, times">MobaXterm官网:https://mobaxterm.mobatek.net/</span></h3>
<h3><span style="font-family: &quot;times new roman&quot;, times; font-size: 15px">3.2.3&nbsp;<strong>SSH连接服务器</strong></span></h3>
<p><span style="font-family: &quot;times new roman&quot;, times"><img src="https://img2018.cnblogs.com/blog/1298835/201909/1298835-20190930150001700-241090729.png"></span></p>
<p>&nbsp;</p>
<p><span style="font-family: &quot;times new roman&quot;, times">&nbsp;</span></p>
<h3><span style="font-family: &quot;times new roman&quot;, times">3.2.4&nbsp;<strong>执行</strong><strong>xclock程序</strong></span></h3>
<p><span style="font-family: &quot;times new roman&quot;, times">已经弹出xclock图形界面了,似不似很简单,似不似很Easy,那就一起实践下吧:)</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times"><img src="https://img2018.cnblogs.com/blog/1298835/201909/1298835-20190930150109748-376612269.png"></span></p>
<p>&nbsp;</p>
<p><span style="font-family: &quot;times new roman&quot;, times">&nbsp;</span></p>
<p><span style="font-family: &quot;times new roman&quot;, times"><em id="__mceDel">&nbsp;</em></span></p>
<p><span style="font-family: &quot;times new roman&quot;, times">  </span></p><br><br>
来源:https://www.cnblogs.com/yuanqiangfei/p/11612815.html
頁: [1]
查看完整版本: linux服务器通过X11实现图形化界面显示