远程运行 linux 服务图形界面程序问题

​ 通常部署在数据中心机房中的服务器是没有图形桌面的,对服务器的日常运维也往往通过远程客户端命令窗口来进行,但有时候往往需要服务器上远程安装运行图形窗口类软件,图形窗口的本地显示往往困扰很多人,譬如在远程安装 oracle数据库系统时候,因安装系统需要图形窗口支持,就必须实现图形窗口在用户工作计算机显示本文详细讨论几种常用方法,并给出最佳实践。

​ 如果服务端安装了图形桌面,还可通过服务端安装 tigervncserver,在客户端安装 tightvnc viewer实现远程操作服务器,本文对此不做详细讨论

服务器端环境准备

1 安装必要的组件

服务器的操作系统centos7

# 安装 X 系统组件
yum install xorg-x11-xauth xorg-x11-apps
# 如果显示乱码可以用户目录下的环境配置文件加入如下内容
vim .bash_profile
#文件最后加入
export LANG=C

2 设置 sshd_config

vim  /etc/ssh/sshd_config
# 设置
X11Forwarding yes
#将X11UseLocalhost去掉注释改为no
X11UseLocalhost no				

客户工具准备

1 Xshell

打开连接属性,设置X11 转移
在这里插入图片描述

2 Mobaxterm

MobaXterm ssh 客户软件自带 X server 服务器,可直接使用。有关设置,参见下图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

实现方法

下面以 mobaXterm 为例进行说明

1 在公司局域网络环境下,工作电脑和服务器之间通过IP地址可以相互访问

  1. ip 地址
# server IP 
10.31.100.107

#个人工作电脑
10.31.122.199
  1. 操作测试过程
[root@orc19c ~]# xclock
#时钟显示正常
[root@orc19c ~]# echo $DISPLAY
#输出结果
localhost:10.0

切换到其它用户
su - oracle
[oracle@orc19c ~]$ echo $DISPLAY
# 输出为空
#把DISPLAY环境变量设置为 root下的值
[oracle@orc19c ~]$ export DISPLAY=localhost:10.0

[oracle@orc19c ~]$ xclock
#显示结果
MoTTY X11 proxy: Authorisation not recognised
# MoTTY X11 代理:不认识的授权
# 因有独立的IP,把DISPLAY设置为工作电脑的IP,一切OK
[oracle@orc19c ~]$ export DISPLAY=10.31.122.199:0.0
[oracle@orc19c ~]$ xclock
#时钟显示正常
  1. 鼠标点击 MobaXterm 的 Xserver 图标可以观察 处于侦听状态的 DISPLAY列表
    在这里插入图片描述

2 针对家庭网络连接公司server,因没有直通的IP地址,设置与操作步骤如下

[root@orc19c ~]# xclock
# X 图形窗口显示正常
#直接切换用户
[root@orc19c ~]# su - oracle
[oracle@orc19c ~]$ xclock
Error: Can't open display:
[oracle@orc19c ~]$ echo $DISPLAY
# DISPLAY环境变量为空
# 切回root
[oracle@orc19c ~]$ exit
#查看 root环境下的DISPLAY环境变量
[root@orc19c ~]# echo $DISPLAY
10.31.100.107:10.0
# 载切回 oracle用户,并设置和root环境相同的 DISPLAY
[root@orc19c ~]# su - oracle
[oracle@orc19c ~]$ export DISPLAY=10.31.100.107:10.0
# 运行 xclock
[oracle@orc19c ~]$ xclock
#显示信息
# MoTTY X11 proxy: Authorisation not recognised
# MoTTY X11 代理:不认识的授权
# 切回root,查找 xauth授权信息
root@orc19c ~]# xauth list $DISPLAY
orc19c:10  MIT-MAGIC-COOKIE-1  9f793a8b51f502a3a3c3359055d13556
#切回 oracle,增加xauth授权
[root@orc19c ~]# su - oracle
Last login: Tue Apr  4 10:44:58 CST 2023 on pts/0
[oracle@orc19c ~]$ xauth add orc19c:10  MIT-MAGIC-COOKIE-1  9f793a8b51f502a3a3c3359055d13556
#设置 DISPLAY环境变量
[oracle@orc19c ~]$ export DISPLAY=10.31.100.107:10.0
[oracle@orc19c ~]$ xclock
#xclock运行正常

四 最佳实践

1 显示器编号从10开始,这是由 sshd_config 配置文件确定

[root@orc19c ~]# vim /etc/ssh/sshd_config
....
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
X11UseLocalhost no
.....

2 如果同时打开多个ssh客户端窗口, 每打开一个连接终端 Display号 增加1

#terminal 1
DISPLAY=10.31.100.107:10.0
#terminal 2
DISPLAY=10.31.100.107:11.0
#terminal 3
DISPLAY=10.31.100.107:12.0
....

3 打开新的连接终端xclock都能运行正常

无论是在 公司环境或是在家工作环境,如果条件允许(有充分的权限),利用SSH工具可以同时打开两个客户端窗口,一个以 root登录,可以随时进行系统的设定,另一个以其它用户进行登录进行软件操作,避免了来回切换带来的图形窗口显示的麻烦。

五 补充资料

The display part is also used on X over SSH. Every new SSH connection with X forwarding enabled is assigned a different display, because these screens correspond internally to a TCP port number offset, e.g. DISPLAY=localhost:10.0 will cause the client to direct graphical output to host localhost port 6010. This is required for SSH X forwarding, because if you have multiple connections to the same computer, your program must send different outputs to different ports so that the SSH server can forward the X output to the proper destination.

DISPLAY=:0 will use a unix domain socket, DISPLAY=localhost:0 will use a internet domain socket (IP).

When you connect to a remote by ssh -X this automatically sets up a reverse channel, over the same connection, to your originating display. It uses the first free port starting from 6010 and initialises DISPLAY to this minus 6000. If you are getting localhost:11.0 then probably someone else has already connected and taken that port, so you get the next one. You would have to kill them and reconnect to get 10.

2023-04-04
tting localhost:11.0 then probably someone else has already connected and taken that port, so you get the next one. You would have to kill them and reconnect to get 10.

2023-04-04

原文地址:https://blog.csdn.net/gmriwyf/article/details/129958946

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_41770.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注