How to Fix “Cannot Open Display” Error in Ubuntu

Attempting to launch a graphical application—such as a text editor (gedit), web browser, media tool, or administrative GUI—from an Ubuntu terminal only to be blocked by an immediate failure stating “Error: Can’t open display: :0” or “cannot open display” is an everyday Linux problem. Whether working locally under a root terminal or remotely over an SSH session, the graphical window refuses to render.

The “cannot open display” error signifies that the graphical software cannot locate or authenticate with an active X11 display server or Wayland compositor. In the Linux display architecture, applications do not draw directly to the screen; they communicate via socket protocols directed by the $DISPLAY environment variable and verified by cryptographic cookies stored in ~/.Xauthority. When this variable is unset, when using sudo without carrying user credentials, or when SSH X11 forwarding is unconfigured, graphical connections fail. This guide details how to resolve display errors across Ubuntu desktop and server.

Method 1: Export the Proper DISPLAY Variable Locally

If you are working directly in a virtual terminal (TTY), running inside a container, or switched users via su, the shell environment often lacks the $DISPLAY environment variable.

  1. Open your terminal session.
  2. Check if the DISPLAY variable is currently defined by running:
    echo $DISPLAY
  3. If the output is completely blank, your shell does not know which display server socket to communicate with.
  4. On standard single-user desktop systems, the primary display server runs on display :0 or :1. Export the variable:
    export DISPLAY=:0
  5. If using Wayland on modern Ubuntu releases (22.04 LTS or 24.04 LTS), also define the Wayland display if required:
    export WAYLAND_DISPLAY=wayland-0
  6. Now test launching your application (e.g., xclock or gedit).

Method 2: Grant Display Access to Sudo or Root Using xhost

For security reasons, modern X11 and Wayland display servers restrict screen access strictly to the currently logged-in desktop user. Running a graphical program with sudo program_name frequently causes the command to fail because the root user does not have permission to access your desktop session.

  1. In your regular user terminal (do not use sudo for this command), grant local root processes access to your active X server:
    xhost +si:localuser:root
  2. The terminal will respond with: “localuser:root being added to access-control list”.
  3. Alternatively, to permit all local users on the machine to open windows:
    xhost +local:
  4. Now run your administrative graphical application using sudo -E program_name. The -E flag instructs sudo to preserve your user’s existing environment variables, including $DISPLAY and $XAUTHORITY.

Method 3: Configure SSH X11 Forwarding for Remote Sessions

If you are connected to a remote Ubuntu server over SSH and wish to launch GUI applications on the remote server and have their windows appear on your local computer, X11 forwarding must be enabled on both ends.

  1. On the remote Ubuntu server, ensure the X11 authentication utility is installed:
    sudo apt update && sudo apt install xauth
  2. Verify that X11 forwarding is permitted in the remote SSH daemon configuration:
    sudo nano /etc/ssh/sshd_config
  3. Ensure the line X11Forwarding yes is present and uncommented (remove the leading #).
  4. If you made changes, restart the SSH daemon:
    sudo systemctl restart ssh
  5. Now disconnect, and reconnect from your client computer using the -X (standard) or -Y (trusted) forwarding flag:
    ssh -X username@server_ip
  6. Once logged in, verify that the remote shell automatically assigned a display:
    echo $DISPLAY
    It will typically return localhost:10.0. Launch your GUI application cleanly.

Method 4: Fix Corrupted .Xauthority Permissions

If you accidentally ran commands like sudo chown across your home directory, root ownership may have locked your ~/.Xauthority cookie file, preventing your desktop user from authenticating display sessions.

  1. Check the ownership of your .Xauthority file:
    ls -la ~/.Xauthority
  2. If the owner is listed as root instead of your username, reclaim ownership:
    sudo chown $USER:$USER ~/.Xauthority
  3. If display errors persist, remove the potentially corrupted authority file:
    rm ~/.Xauthority
  4. Log out of Ubuntu and log back in. The desktop display manager will automatically regenerate a pristine .Xauthority cookie file with proper permissions.

Ubuntu Display Error Troubleshooting Reference Table

Use the matrix below to assess and fix display connection failures:

Terminal Environment Probable Root Cause Standard Remedial Command
“Can’t open display: :0” inside cron / script DISPLAY environment variable unpopulated Add “export DISPLAY=:0” at start of script
Error occurs exclusively when using “sudo” Root user barred by X server access-control list xhost +si:localuser:root && sudo -E <app>
“cannot open display” over SSH connection SSH X11 forwarding flag omitted or xauth missing Install xauth on server; connect via ssh -X user@host
“Invalid MIT-MAGIC-COOKIE-1 key” Corrupted or desynchronised ~/.Xauthority file rm ~/.Xauthority && re-login
App fails under Wayland desktop session Legacy X11 software incompatible with pure Wayland Install xwayland or launch via GDK_BACKEND=x11

Running Legacy X11 Applications Under Wayland

Modern Ubuntu installations use Wayland by default rather than the legacy X.Org server. Certain older Linux software packages (such as legacy scientific or CAD tools) do not support the Wayland protocol natively and crash with display errors. To launch these applications, ensure the XWayland compatibility bridge is active by installing it via sudo apt install xwayland, and launch the application forcing the X11 backend: GDK_BACKEND=x11 application_name.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.