背景

最近遇到个两年前遇到的问题,使用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

附:参考

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.

彻底理解Linux的DISPLAY变量的作用的更多相关文章

  1. Linux PATH环境变量及作用(初学者必读)

    Linux PATH环境变量及作用(初学者必读) < 什么是环境变量,Linux环境变量有哪些?Linux打包(归档)和压缩 > <Linux就该这么学>是一本基于最新Linu ...

  2. 理解 Linux 条件变量

    理解 Linux 条件变量 1 简介 当多个线程之间因为存在某种依赖关系,导致只有当某个条件存在时,才可以执行某个线程,此时条件变量(pthread_cond_t)可以派上用场.比如: 例1: 当系统 ...

  3. Linux中PATH环境变量的作用和使用方法

    关于PATH的作用:PATH说简单点就是一个字符串变量,当输入命令的时候LINUX会去查找PATH里面记录的路径.比如在根目录/下可以输入命令ls,在/usr目录下也可以输入ls,但其实ls这个命令根 ...

  4. 什么是环境变量,Linux环境变量及作用 echo

    什么是环境变量,Linux环境变量及作用 < Linux命令的执行过程是怎样的?(新手必读)Linux PATH环境变量是什么,有什么用?(入门必读) > <Linux就该这么学&g ...

  5. 转: 深入理解Linux修改hostname

    from: http://www.cnblogs.com/kerrycode/p/3595724.html 写的相当详细!!! 深入理解Linux修改hostname 2014-03-12 10:17 ...

  6. 一步一图带你深入理解 Linux 物理内存管理

    1. 前文回顾 在上篇文章 <深入理解 Linux 虚拟内存管理> 中,笔者分别从进程用户态和内核态的角度详细深入地为大家介绍了 Linux 内核如何对进程虚拟内存空间进行布局以及管理的相 ...

  7. Linux /dev目录详解和Linux系统各个目录的作用

    Linux /dev目录详解(转http://blog.csdn.net/maopig/article/details/7195048) 在linux下,/dev目录是很重要的,各种设备都在下面.下面 ...

  8. 深入理解Linux中内存管理

    前一段时间看了<深入理解Linux内核>对其中的内存管理部分花了不少时间,但是还是有很多问题不是很清楚,最近又花了一些时间复习了一下,在这里记录下自己的理解和对Linux中内存管理的一些看 ...

  9. 理解 Linux shell 中的一个方言:2>&1

    理解 Linux shell 中的一个方言:2>&1  2016-11-14 杜亦舒 前言 在使用 linux 命令或者 shell 编程时,这个用法常会遇到 2>&1 如 ...

  10. 深入理解linux网络技术内幕读书笔记(三)--用户空间与内核的接口

    Table of Contents 1 概论 1.1 procfs (/proc 文件系统) 1.1.1 编程接口 1.2 sysctl (/proc/sys目录) 1.2.1 编程接口 1.3 sy ...

随机推荐

  1. SASS 运算 (Operations)符的基本使用

    ​ sass 运算符虽然没有像那些编程语言那么强大,但为了更灵活的输出css,也增强了一些运算符的功能,例如赋值运算符.等号操作符.比较运算符.逻辑运算符.字符串运算符...等等,接下来就来详细介绍下 ...

  2. 将大量文件的拓展名中大写字母改为小写:Python实现

      本文介绍基于Python语言,基于一个大文件夹,遍历其中的多个子文件夹,对于每一个子文件夹中的大量文件,批量将其文件的名称或后缀名中的字母由大写修改为小写的方法.   本文期望实现的需求为:现有一 ...

  3. CSS——动画

    @keyframes 规则 要创建 CSS 动画,您首先需要了解 @keyframes 规则,@keyframes 规则用来定义动画各个阶段的属性值,类似于 flash 动画中的关键帧,语法格式如下: ...

  4. 为WPF框架Prism注册Nlog日志服务

    这篇文章介绍了为WPF框架Prism注册Nlog日志服务的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧   无论是Nlog还是Serilog, 它们都提供 ...

  5. 鸿蒙极速入门(五)-路由管理(Router)

    页面路由指在应用程序中实现不同页面之间的跳转和数据传递.HarmonyOS提供了Router模块,通过不同的url地址,可以方便地进行页面路由,轻松地访问不同的页面. 一.基础使用 Router模块提 ...

  6. MySQL学习笔记-事务

    事务 事务:是一组操作的集合,是一个不可分割的工作单位,事务会把所有操作作为一个整体一起向系统提交或撤销操作请求,即这些操作要么同时成功,要么同时失败(当操作中某一步出现异常时,前面已执行的步骤也会失 ...

  7. 还在拼冗长的WhereIf吗?100行代码解放这个操作

    通常我们在做一些数据过滤的操作的时候,经常需要做一些判断再进行是否要对其进行条件过滤. 普通做法 最原始的做法我们是先通过If()判断是否需要进行数据过滤,然后再对数据源使用Where来过滤数据. 示 ...

  8. 铭瑄主板重启后USB3.0失效键盘鼠标无反应需要重新插拔

    铭瑄主板重启后USB3.0失效键盘鼠标无反应需要重新插拔 环境: 铭瑄B760 主板,使用鼠标键盘使用USB 3.0 HUB 连接到主板 USB 3.0 口. 重启后,键盘鼠标无反应,需要重新插拔. ...

  9. C# JAVA UrlEncode

    C#  JAVA urlencode 默认是不兼容的.C# 需要encode的部分是小写.JAVA 需要encode的部分是大写. /// <summary> /// java UrlEn ...

  10. CountDownLatch demo演示裁判和选手赛跑

    # CountDownLatch demo演示裁判和选手赛跑 package com.example.core.mydemo; import java.util.concurrent.CountDow ...