DISPLAY变量和xhost(原创)

DISPLAY

在Linux/Unix类操作系统上, DISPLAY用来设置将图形显示到何处. 直接登陆图形界面或者登陆命令行界面后使用startx启动图形, DISPLAY环境变量将自动设置为:0:0, 此时可以打开终端, 输出图形程序的名称(比如xclock)来启动程序, 图形将显示在本地窗口上, 在终端上输入printenv查看当前环境变量, 输出结果中有如下内容:
DISPLAY=:0.0
使用xdpyinfo可以查看到当前显示的更详细的信息.
DISPLAY 环境变量格式如下hostname: displaynumber.screennumber,我们需要知道,在某些机器上,可能有多个显示设备共享使用同一套输入设备,例如在一台PC上连接两台CRT显示器,但是它们只共享使用一个键盘和一个鼠标。这一组显示设备就拥有一个共同的displaynumber,而这组显示设备中的每个单独的设备则拥有自己单独的 screennumber。displaynumber和screennumber都是从零开始的数字。这样,对于我们普通用户来说, displaynumber、screennumber就都是0。 hostname指Xserver所在的主机主机名或者ip地址, 图形将显示在这一机器上, 可以是启动了图形界面的Linux/Unix机器, 也可以是安装了Exceed, X-Deep/32等Windows平台运行的Xserver的Windows机器. 如果Host为空, 则表示Xserver运行于本机, 并且图形程序(Xclient)使用unix socket方式连接到Xserver, 而不是TCP方式. 使用TCP方式连接时, displaynumber为连接的端口减去6000的值, 如果displaynumber为0, 则表示连接到6000端口; 使用unix socket方式连接时则表示连接的unix socket的路径, 如果displaynumber为0, 则表示连接到/tmp/.X11-unix/X0 . screennumber则几乎总是0.
如果使用su username或者su - username切换到别的用户, 并且使用命令
export DISPLAY=:0.0
设置DISPLAY环境变量, 运行图形程序(如xclock)时会收到如下错误:
Xlib: connection to ":0.0" refused by server
Xlib: No protocol specified
Error: Can't open display: :0.0
这是因为Xserver默认情况下不允许别的用户的图形程序的图形显示在当前屏幕上. 如果需要别的用户的图形显示在当前屏幕上, 则应以当前登陆的用户, 也就是切换身份前的用户执行如下命令
xhost +

xhost 是用来控制X server访问权限的,这个命令将允许别的用户启动的图形程序将图形显示在当前屏幕上.。通常当你从hostA登陆到hostB上运行hostB上的应用程序时,做为应用程序来说,hostA是client,但是作为图形来说,是在hostA上显示的,需要使用hostA的Xserver,所以hostA是
server.因此在登陆到hostB前,需要在hostA上运行xhost +,来使其它用户能够访问hostA的Xserver.
xhost + 是使所有用户都能访问Xserver.
xhost + ip使ip上的用户能够访问Xserver.
xhost + nis:user@domain使domain上的nis用户user能够访问
xhost + inet:user@domain使domain上的inet用户能够访问。
在2台Linux机器之间, 如果设置服务器端配置文件/etc/ssh/sshd_config中包含
X11Forwarding no
客户端配置文件/etc/ssh/ssh_config包含
ForwardX11 yes
则从客户端ssh到服务器端后会自动设置DISPLAY环境变量, 允许在服务器端执行的图形程序将图形显示在客户端上. 在服务器上查看环境变量显示如下(这个结果不同的时候并不相同)
DISPLAY=localhost:10.0
在客户机上用netstat -lnp可以看到有程序监听了6010端口
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 4827/1
如果希望允许远程机器上的图形程序将图形显示在本地机器的Xserver上, 除了要设置远端机器的DISPLAY环境变量以外, 还需要设置本地机器的Xserver监听相应的TCP端口. 而现在的Linux系统出于安全的考虑, 默认情况下不再监听TCP端口. 可通过修改/etc/X11/xinit/xserverrc文件, 将
exec /usr/bin/X11/X -dpi 100 -nolisten tcp
修改为
exec /usr/bin/X11/X -dpi 100
允许在直接使用startx启动图形时启动对TCP端口的监听.
修改/etc/kde3/kdm/kdmrc, 将
ServerArgsLocal=-nolisten tcp
修改为
ServerArgsLocal=
允许kdm作为显示管理器时, 启动会话时监听相应的TCP端口.
修改/etc/gdm/gdm.conf, 在[Security]一节增加
DisallowTCP=false
或者在登陆窗口选择"Options" -> "Configure Login Manager..."的Security页面, 取消"Deny TCP connections to Xserver", 允许gdm作为显示管理器时, 启动会话时监听相应的TCP端口.

常见错误和解决方法

cause:
X-Windows emulation is not yet correctly configured or is being mis-used.
solution:
1.Make sure that "DISPLAY” is set properly as either:
     <ip address of the DBA desktop PC>:0.0
          or
     <hostname of the DBA desktop PC>:0.0
note:In this example, ip_address/hostnameis the host name or IP address of the system that you want to use to display Oracle Universal Installer (your workstation or PC).
eg.[root@rac1 ~]# xhost +
access control disabled, clients can connect from any host
[root@rac1 ~]# su - oracle
[oracle@rac1 ~]$ export DISPLAY=192.16.2.98:0.0
[oracle@rac1 ~]$ cd database/
[oracle@rac1 database]$ ./runInstaller
Starting Oracle Universal Installer... 
Checking installer requirements... 
Checking operating system version: must be redhat-3, SuSE-9, redhat-4, UnitedLinux-1.0, asianux-1 or asianux-2
                                      Passed 
All installer requirements met. 
Preparing to launch Oracle Universal Installer from /tmp/OraInstall2009-03-05_03-36-28PM. Please wait ...[oracle@rac1 database]$ Oracle Universal Installer, Version 10.2.0.1.0 Production
Copyright (C) 1999, 2005, Oracle. All rights reserved.

2. Oracle Customer Support has seen instances where use of the UNIX/Linux “su” command, even “su - oracle” has caused this “Can't connect to X11 window server” error. Therefore, please be sure that you are LOGGED ON as the oracle account. Log directly into the install session. Do not use any variety of “su”.
     use oracle log on , not "su - oracle"
eg. [oracle@rac1 database]$ ./runInstaller

3.Oracle Customer Support has seen instances where the incorrect “ulimit” setting for number of files (aka “-n”, aka “nofiles”) has caused this “Can't connect to X11 window server” error. Therefore, please be sure that your values for “nofiles” in the output of “ulimit -a” is at least 65536.

备注:前2个办法只需要满足其中一个就可以避免这个错误了:直接用oracle登陆,不能su - oracle  or 设置display=ip_address_that you want to use to display Oracle Universal Installer:0.0

NO.35 xhost: unable to open display ""
以root用户登录
输入命令xdpyinfo,记录下里面的name of display:后的字符串,如 name of display:127.0.0.1:1.0
在执行xhost +命令(使得所有客户都可以访问)
返回信息为:
access control disabled,clients can connect from any host
如果xhost +不能执行,可以输入命令export DISPLAY=:0执行一下
再执行xhost +
xhost + ip (name表示那个ip机器可以使用该服务)
2、以oracle用户登录
如果前面root登录下执行xdpyinfo后的name of display:后的字符串是127.0.0.1:1.0
那么在该oracle用户下执行一下export DISPLAY=:1.0
然后再执行xdpyinfo命令,如果能出现信息说明你已经成功了
这样估计就可以解决linux下安装oracle出现的xhost问题
如果还是不成功,请确认是否开启了startx

XDMCP被禁用

启用 XDMCP 红帽 Linux 上

在 RHEL,默认情况下仅为本地主机启用 X 11。你需要能够连接从外部框中编辑文件/etc/gdm/custom.conf。RHEL 的默认设置是

DisallowTCP=yes
其中应改为"假"
要启用 XDMCP 一些用户需要你需要启用它。
[xdmcp]
Enable=true
之后重新启动 X 11 服务器使用命令
init 3 && init 5
如果另外您想要提供能力,以 root 用户身份登录,您需要安保科,如下所示
[security]
    DisallowTCP=false
    AllowRemoteRoot=true
如果启用了防火墙,则您需要打开几个端口
177 (UDP) 端口和端口 6000-6005 (TCP)
如果您正在使用 Gnome 打开 TCP 端口 16001 和 TCP 35091 在两个方向。
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 16001 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m udp -p udp --dport 177 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 35091 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6000:6005 -j ACCEPT

posted @ 2019-02-14 10:50  嘻嘻哈哈的人生  阅读(11746)  评论(0编辑  收藏  举报