徹底理解Linux的DISPLAY變數的作用

东北小狐狸發表於2024-07-19

背景

最近遇到個兩年前遇到的問題,使用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

找到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

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

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.

相關文章