X Window System

Sources:

  • X Window 配置介绍

X Window System

The X Window System (X11) is a windowing protocol (and system design) used on Unix-like systems. It allows you to run a graphical application on one machine while displaying it on another. To do this, it splits the GUI into two roles:

  • X server: runs where your display and keyboard/mouse are; it renders windows and handles input.
  • X client: a GUI application that connects to an X server via the X11 protocol.

In practice, “run a GUI app remotely, display it locally” means running an X client on the remote host and having it connect to your local X server (usually your laptop).

X11 forwarding over SSH

SSH suuports X11 forwarding by tunneling X11 traffic through the SSH connection so that remote X clients show up as local windows:

  1. sshd allocates a forwarded display on the remote side (e.g. DISPLAY=localhost:10.0).
  2. Remote GUI apps connect to that display.
  3. X11 messages are forwarded over the encrypted SSH channel.
  4. Your local X server renders the windows and handles input.

Local + remote checklist

Local machine (where windows should appear):

Ensure an X server exists and is running:

  • macOS: XQuartz
  • Windows: Xming / VcXsrv
  • Linux desktop: usually already available (Xorg / Xwayland)

Remote host:

sshd allows forwarding (/etc/ssh/sshd_config):

1
2
X11Forwarding yes
X11DisplayOffset 10

Then restart:

1
sudo systemctl restart sshd

Install at least one small X client for testing, e.g. xclock / xeyes / xterm.

Usage + quick test

Connect with forwarding:

  • -X: untrusted (safer, more restrictions; some apps break)
  • -Y: trusted (works more often; only for hosts you trust)
1
2
ssh -X username@remotehost
# or: ssh -Y username@remotehost

Then on the remote host:

1
2
echo "$DISPLAY"   # expect localhost:10.0 (or similar)
xclock # should pop up locally

DISPLAY

The DISPLAY environment variable tells an X client where to find the X server. Its common forms are:

  • :0
    The “local” X server on the same machine (display number 0). This is what you usually see in a normal desktop session.

  • hostname:N.M (e.g. localhost:10.0)
    Connect to an X server via a network endpoint. N is the display number, and .M is the screen number (almost always 0).

For example, with SSH X11 forwarding, sshd sets DISPLAY on the remote host to something like localhost:10.0. This does not mean “use your laptop’s X server directly at localhost”. It means:

  • on the remote machine, X clients connect to a local loopback listener owned by sshd (the localhost:10.0 endpoint),
  • and sshd forwards the X11 protocol over the SSH channel to the real X server on your local machine.

macOS caveat (XQuartz + OpenGL/GLX)

XQuartz can be unreliable for OpenGL/GLX over X11 forwarding. GLX-heavy apps (e.g. glxgears) may show static windows or fail.

Sometimes forcing indirect rendering helps:

1
2
export LIBGL_ALWAYS_INDIRECT=1
glxgears

If you need usable remote graphics on macOS, prefer VNC (full remote desktop) or xpra (per-app remoting) over raw X11 forwarding.