Diagnosing X11 'cannot open display' Failures on Linux
A diagnostic guide for X11 cannot open display errors, covering DISPLAY, X authority cookies, and SSH X11 forwarding.
22 Jul 2025, 08:21 UTC

Problem and what to check first
You launch a graphical program and it exits immediately with cannot open display: :0, Unable to init server: Could not connect: Connection refused, or X11 connection rejected because of wrong authentication. No window appears.
The useful takeaway is that almost every one of these failures comes down to one of three conditions: the DISPLAY variable names a display that no X server owns, the client lacks a valid X authority cookie for that display, or SSH X11 forwarding is disabled on the remote host. This guide checks them in that order.
This guide covers the X Window System (X11), including the Xorg server and Xwayland. It does not apply to native Wayland clients, which use WAYLAND_DISPLAY instead of DISPLAY and do not use xauth cookies in the same way.
Symptom, likely cause, and first check
| Symptom | Likely cause | First check |
|---|---|---|
cannot open display: :0 or connection refused | DISPLAY is unset, wrong, or names a stopped server | echo $DISPLAY then xdpyinfo |
X11 connection rejected because of wrong authentication | Missing or mismatched MIT-MAGIC-COOKIE-1 in ~/.Xauthority | xauth list |
| Remote GUI client over SSH cannot open a window | X11Forwarding disabled, xauth missing, or DISPLAY not set on remote host | grep X11Forwarding /etc/ssh/sshd_config on the remote host |
| Clients connect to the wrong server or a second server will not start | Display-number collision or stale socket in /tmp/.X11-unix | ps -ef | grep "[X]" and ls /tmp/.X11-unix |
Terms: DISPLAY is an environment variable that tells an X client which server to contact. MIT-MAGIC-COOKIE-1 is the default authentication cookie stored in ~/.Xauthority that proves a client may use a particular display. Xwayland is an X server that runs inside a Wayland session so legacy X11 clients can still open windows.
Ordered checks
Check 1: Confirm what DISPLAY names
echo $DISPLAY
xdpyinfo
Where: local terminal. Permissions: none. Expected check: echo $DISPLAY prints something like :0, :1, or localhost:10.0. If it is empty, DISPLAY is unset. xdpyinfo should exit with code 0 and print server information. A non-zero exit or Unable to open display confirms the client cannot reach the X server.
Check 2: Verify an X server owns that display
ps -ef | grep "[X]"
ls -l /tmp/.X11-unix/
Where: local terminal. Permissions: none. Expected check: ps shows a process such as /usr/lib/xorg/Xorg :0 or X :0. The directory /tmp/.X11-unix/ should contain a socket file named X0 for display :0, X1 for :1, and so on. If no process or socket matches your DISPLAY value, no server is listening there.
Check 3: Check the X authority cookie
xauth list
Where: local terminal as the user running the GUI client. Permissions: none. Expected check: an entry exists whose display name matches $DISPLAY, for example hostname/unix:0 MIT-MAGIC-COOKIE-1 123ab.... If no entry matches, or if the cookie differs from the one the server created, the connection is rejected with an authentication error. If XAUTHORITY is set to a non-default file, xauth list reads that file instead of ~/.Xauthority.
Check 4: Inspect the SSH remote host
grep X11Forwarding /etc/ssh/sshd_config
which xauth
echo $DISPLAY
Where: remote host over SSH. Permissions: none for reading; root or sudo for changing sshd_config. Expected check: X11Forwarding yes. which xauth must print a path such as /usr/bin/xauth. Inside the SSH session, echo $DISPLAY should show a forwarded display such as localhost:10.0. If any of these fail, X11 forwarding will not work.
Fixes tied to each finding
Fix 1: Set or correct DISPLAY
If DISPLAY is empty or wrong, export the correct value in the current shell:
export DISPLAY=:0
Replace :0 with the display number confirmed in Check 2. If you switched users with su or sudo, the new shell may not inherit the original user's DISPLAY or authority file. In that case, set DISPLAY explicitly and point XAUTHORITY to the original user's ~/.Xauthority only if you understand the sharing implications (see the warning below).
Fix 2: Add or repair the X authority cookie
If xauth list lacks an entry for your display, add the cookie from the display owner's authority file. As the user who owns the running X session:
xauth extract - $DISPLAY
Then, as the target user, merge the output into their authority file using xauth merge or add it manually with xauth add. Do not paste cookie values you have not generated yourself.
Risk: copying an X authority cookie to another user on a multiuser machine lets that user connect to your X server, potentially capturing keystrokes and screen contents. Only share cookies on a single-user workstation or with deliberate intent.
Fix 3: Enable SSH X11 forwarding
On the remote host, back up and edit /etc/ssh/sshd_config:
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
Set or confirm:
X11Forwarding yes
Install the xauth package if which xauth failed, then restart sshd:
systemctl restart sshd
Where: remote host. Permissions: root or sudo. Risk: an error in sshd_config can interrupt remote access. Keep an existing SSH session open while testing, and use the backup to restore if needed. After restarting, open a new SSH session with ssh -X user@host and confirm echo $DISPLAY shows a forwarded value.
Fix 4: Resolve display-number collision or stale socket
If ps shows a server on :1 but DISPLAY=:0, either change DISPLAY to :1 or start the intended server on :0. If /tmp/.X11-unix/X0 exists but no Xorg process owns :0, the socket is stale. Remove it only after confirming no process uses it:
rm /tmp/.X11-unix/X0
Risk: removing the socket of a live server breaks that session. Verify with ps first.
What to avoid
- Do not run
xhost +on a shared machine. It disables host-based access control and can expose keystrokes and screen contents to other local users. - Do not open TCP port 6000 for remote X11. Local X11 sockets are not encrypted. Use SSH X11 forwarding instead.
Escalation criteria
xdpyinfoworks but one specific application still fails: the problem is likely application-specific, not the X infrastructure.- Cookie mismatches persist after correcting
XAUTHORITY: possible PAM orxauthmisconfiguration; involve a system administrator. sshd_configsaysX11Forwarding yesbutsshd -Tstill reportsx11forwarding no: a distribution-specific override orMatchblock may be in effect; escalate.- The session is native Wayland (no
/tmp/.X11-unixsockets,WAYLAND_DISPLAYset): use Wayland-native tools or launch through Xwayland; X11 diagnostics do not apply.
Verify the result
After applying a fix, run:
xdpyinfo > /dev/null && echo "X reachable"
Then start a lightweight client such as xclock or xterm. If a window appears, the display connection is working.
Limitations
This guide covers X client-to-server connectivity and authentication. It does not diagnose GPU driver crashes, X server segfaults, or application rendering bugs. For server crashes, inspect /var/log/Xorg.0.log or the relevant display manager log. Remote X11 over the public internet is not covered because forwarding through SSH is the preferred and safer path.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.