0404---通过SSH连接远程服务器运行图形界面程序问题

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

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

​ 如果服务端安装了图形桌面,还可通过在服务端安装 tigervnc-server,在客户端安装 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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值