当前位置:   article > 正文

彻底理解Linux的DISPLAY变量的作用_linux display 变量和 session 中的display

linux display 变量和 session 中的display

背景

最近遇到个两年前遇到的问题,使用virt-manager提示(virt-manager:873): Gtk-WARNING **: 14:53:28.147: cannot open display: :1,当时专门运维的同事帮忙临时调了下DISPLAY变量,好像是将:1改成了SSH用户本地IP:10.0,当时的确好了,用完就关了再没用到,也没深究原因,那个运维同事也不大理解(网上查到的解决办法)。然而最近在做资产盘点,领导让我把我挂名管理的服务器作置换申请,需要知道虚拟机的信息,赶上盘到两年前有问题的机器上,又出现同样的问题,经过查找了资料找到了个几乎万无一失的理解,记一记。

DISPLAY变量是啥

首先,它是Linux X11 server(显示服务)用到的一个环境变量,用来指示你的显示(也可以包含键盘和鼠标)指向的显示服务地址,通常桌面PC该值会被设为:0.0

其次,它的格式有三部分: [主机名]:显示服务端口号-6000.显示器编号

  • [主机名] :一般是可以省略的,可以不写,也可以写成$HOSTNAME变量表示的主机名 或 localhost
  • 显示服务端口号-6000:意思是sshd服务的X11Forwarding占用端口减去6000的值
  • 显示器编号:一般都是0,表示第一个显示器

如何正确设置DISPLAY变量

分两种情况:

  • Linux桌面系统:直接设置:0.0
  • SSH连接的Linux服务器:需要按照格式进行检查。

检查步骤如下:

[root@hz ~]# netstat -anpt |grep sshd |grep LISTEN |grep 60
tcp   0  0 127.0.0.1:6010  0.0.0.0:*  LISTEN   30346/sshd: root@pt
tcp6  0  0 ::1:6010        :::*       LISTEN   30346/sshd: root@pt
  • 1
  • 2
  • 3

找到60开头的sshd端口,这时是6010,减去6000是10,SSH只写第一显示器编号

则我的DISPLAY变量可设为 :10.0 或者 hz:10.0

如果上边的命令查不出来6000左右的端口号,请检查 /etc/ssh/sshd_config,确认X11Forwarding yes参数已配置并systemctl restart sshd,使用exit退出当前ssh,重新连接再尝试。

附:参考

The magic word in the X window system is DISPLAY. A display consists (simplified) of:

  • a keyboard,
  • a mouse
  • and a screen.

A display is managed by a server program, known as an X server. The server serves displaying capabilities to other programs that connect to it.

The remote server knows where it has to redirect the X network traffic via the definition of the DISPLAY environment variable which generally points to an X Display server located on your local computer.

The value of the display environment variable is:

hostname:D.S
  • 1

where:

hostname is the name of the computer where the X server runs. An omitted hostname means the localhost.

D is a sequence number (usually 0). It can be varied if there are multiple displays connected to one computer.

S is the screen number. A display can actually have multiple screens. Usually, there’s only one screen though where 0 is the default.

Example of values

localhost:4
google.com:0
:0.0
  • 1
  • 2
  • 3

hostname:D.S means screen S on display D of host hostname; the X server for this display is listening at TCP port 6000+D.

host/unix:D.S means screen S on display D of host host; the X server for this display is listening at UNIX domain socket /tmp/.X11-unix/XD (so it’s only reachable from host).

:D.S is equivalent to host/unix:D.S, where host is the local hostname.

:0.0 means that we are talking about the first screen attached to your first display in your local host

Read more here: support.objectplanet.com and here: superuser.com and here: docstore.mik.ua.

From a X(7) man page:

From the user’s perspective, every X server has a display name of the form:

hostname:displaynumber.screennumber

This information is used by the application to determine how it should connect to the server and which screen it should use by default (on displays with multiple monitors):

hostname The hostname specifies the name of the machine to which the display is physically connected. If the hostname is not given, the most efficient way of communicating to a server on the same machine will be used. displaynumber The phrase “display” is usually used to refer to a collection of monitors that share a common keyboard and pointer (mouse, tablet, etc.). Most workstations tend to only have one keyboard, and therefore, only one display. Larger, multi-user systems, however, frequently have several displays so that more than one person can be doing graphics work at once. To avoid confusion, each display on a machine is assigned a display number (beginning at 0) when the X server for that display is started. The display number must always be given in a display name. screennumber Some displays share a single keyboard and pointer among two or more monitors. Since each monitor has its own set of windows, each screen is assigned a screen number (beginning at 0) when the X server for that display is started. If the screen number is not given, screen 0 will be used.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/964164
推荐阅读
相关标签
  

闽ICP备14008679号