fix ‘could not connect to display’ issue

Today I tried to run foxit reader installer in my Ubuntu Linux as root account. I got the below error message:

QXcbConnection: Could not connect to display

After struggling for a while, I found the below solution that fix this issue.

 

First, set DISPLAY environment variable for root account:

export DISPLAY=:0

The display capability of the machine is provided by an X server. From the user’s perspective, every X server has a display name of the below form:

hostname:displaynumber.screennumber

hostname specifies the hostname of the machine, where X server is running. This field can be omitted. If so, the most efficient local transport will be used. Usually this is a Unix domain socket.

displaynumber is the number of the display, usually 0. There can be multiple displays attached to a machine. Each display can be assigned a unique number. This field can not be omitted.

screennumber is refers to the screen of the display, usually 0. There can be multiple screens in a display. This field can be omitted. If so, number 0 will be used.

In Linux, the display name will be stored in the DISPLAY environment variable, which can be used by applications to connect to X server. This variable usually is set automatically by the Linux OS. If this variable is not, you can set it manually. For my Ubuntu Linux, I login to the OS using account xiaoliang. The value of the DISPLAY environment variable is :0 for the account xiaoliang.

 

Second, run the below command as xiaoliang to grant the access to the display to root

xhost +si:localuser:root

An X server has several types of access control, for example, host access, MIT-MAGIC-COOKIE-1, server interpreted, and so on. The above command grants the access to root.

 

After these two steps, the display issue has gone, when I run foxit reader installer as root.