# Install and configure X11

> How the X Window System is built, where its config lives, and how to show programs on another screen safely.

Source: https://aminbytes.dev/bytes/user-interfaces-and-desktops/x11/


Every graphical Linux program, from a terminal window to a web browser, needs something that owns the screen, the keyboard and the mouse. For almost 40 years that something has been the **X Window System**, also called X11 or just X. It still runs on millions of machines, and its successor, Wayland, keeps it around for older apps.

You need X knowledge when a desktop won't start, when the keyboard layout is wrong on the login screen, or when you want to run a program on a server and see its window on your laptop. The exam also loves the odd part of X: its idea of "client" and "server" looks backwards at first.

## How X is built

X is split into two halves that talk over the **X protocol**:

- The **X server** runs on the machine with the screen. It owns the graphics card, the monitor, the keyboard and the mouse. On Linux the X server program is **Xorg** (from the X.Org project). The command `X` is usually a link to it.
- **X clients** are the programs: `xterm`, Firefox, a file manager. They ask the server "draw this rectangle here" and the server sends them events like "a key was pressed".

```text
 your laptop (has the screen)            any machine
+----------------------------+        +-----------------+
|  X server (Xorg)           | <----> |  X client       |
|  screen, keyboard, mouse   |   X    |  (xterm, gedit) |
+----------------------------+protocol+-----------------+
```

That's why it feels backwards: the *server* sits in front of you, and the *clients* can run anywhere, even on a machine in another building. X was built to be **network transparent**: a client doesn't care whether its server is local or across the network.

On top of the bare X server, a desktop is made of more clients:

| Component | What it does | Examples |
| --- | --- | --- |
| **Display manager** | The graphical login screen. Starts the X server and your session. | GDM (GNOME), SDDM (KDE), LightDM, XDM |
| **Window manager** | Draws borders and title bars, moves, resizes and stacks windows. | Mutter, KWin, Xfwm4, Openbox, i3 |
| **Compositor** | Blends windows into the final image (shadows, transparency). Often built into the window manager. | Mutter, KWin, picom |
| **Desktop environment** | The full package: window manager plus panels, file manager, settings, apps. | GNOME, KDE Plasma, Xfce |
| **Toolkit** | The library apps use to draw buttons and menus. | GTK, Qt |

Without a window manager, X still works, but windows have no borders and you can't move them. Try it once and you'll never forget what a window manager does.

## The DISPLAY variable

A client finds its server through the `DISPLAY` environment variable:

```bash
echo $DISPLAY
```

```console
:0
```

The format is `host:display.screen`:

| Part | Meaning | Default |
| --- | --- | --- |
| `host` | Machine running the X server. Empty means "this machine, through a local socket". | local |
| `display` | Which X server on that host. The first is `0`, the next `1`, and so on. | required |
| `screen` | Which screen of that server (rare today). | `0` |

So `:0`, `:0.0` and `unix:0` all mean the first local display, and `office-pc:0` means the first display on `office-pc`. A local server listens on a Unix socket called `/tmp/.X11-unix/X0` for display 0. Over the network, display *n* uses TCP port **6000 + n**, so display `:1` is port 6001.

You can point a single program at another display without changing your shell:

```bash
DISPLAY=:1 xterm &
xterm -display :1 &
```

## Installing and starting X

Most desktop installs already include Xorg. On a minimal system:

```bash
# Debian / Ubuntu
sudo apt install xorg

# RHEL / Fedora
sudo dnf install xorg-x11-server-Xorg xorg-x11-xinit
```

To see which version you have:

```bash
Xorg -version
```

```console
X.Org X Server 1.21.1.7
X Protocol Version 11, Revision 0
```

There are two ways X gets started:

1. **A display manager** (the usual way). It's a systemd service, so `systemctl status display-manager` shows which one you have. Boot into the graphical target with `sudo systemctl set-default graphical.target`. On Debian, the chosen display manager is also written in `/etc/X11/default-display-manager`.
2. **By hand from a text console** with `startx`. It's a friendly wrapper around `xinit`, which starts the X server and then runs the clients listed in `~/.xinitrc` (or the system default `/etc/X11/xinit/xinitrc`). When the last line of that file exits, X stops.

```bash {title="~/.xinitrc"}
xsetroot -solid steelblue
xterm &
exec xfce4-session
```

You can also start a bare second server with `X :1` (or `Xorg :1`) and switch to it with Ctrl+Alt+F-keys. It's an empty grey or black screen until a client connects.

## Configuration files

Modern Xorg detects your graphics card, monitors and input devices by itself, so most systems have **no** `/etc/X11/xorg.conf` at all. You only add configuration to change something the auto-detection got wrong.

| Path | What it is |
| --- | --- |
| `/etc/X11/xorg.conf` | The classic single config file. Optional today. |
| `/etc/X11/xorg.conf.d/*.conf` | Small snippets, read in name order. **Your** overrides go here. |
| `/usr/share/X11/xorg.conf.d/*.conf` | Defaults shipped by packages (for example `40-libinput.conf`). Don't edit them, updates overwrite them. |
| `/var/log/Xorg.0.log` | Log of display `:0` when X runs as root. |
| `~/.local/share/xorg/Xorg.0.log` | The same log when X runs as your user ("rootless" X, the default with GDM on many distros). |

The file is made of **sections**. Each one starts with `Section "Name"` and ends with `EndSection`:

| Section | Describes |
| --- | --- |
| `ServerLayout` | Ties screens and input devices together. The top level. |
| `Files` | Paths to fonts and modules. |
| `Module` | Extra server modules to load. |
| `InputDevice` | One specific keyboard or mouse (old style). |
| `InputClass` | Rules that match *all* devices of a kind, for example "every keyboard". |
| `Device` | A graphics card and its driver. |
| `Monitor` | A monitor and its supported modes. |
| `Screen` | Joins one `Device` and one `Monitor`, sets color depth and resolutions. |

A small example that forces a driver and a resolution:

```text {title="/etc/X11/xorg.conf.d/20-screen.conf"}
Section "Device"
    Identifier "Card0"
    Driver     "modesetting"
EndSection

Section "Monitor"
    Identifier "Monitor0"
EndSection

Section "Screen"
    Identifier   "Screen0"
    Device       "Card0"
    Monitor      "Monitor0"
    DefaultDepth 24
    SubSection "Display"
        Depth 24
        Modes "1920x1080" "1280x1024"
    EndSubSection
EndSection
```

Need a full starting file? Stop X first, then as root run `Xorg -configure`. It probes the hardware and writes `/root/xorg.conf.new`, which you can test with `Xorg -config /root/xorg.conf.new` before copying it to `/etc/X11/xorg.conf`.

### Override one thing: the keyboard layout

The exam's favorite override is the keyboard. You don't need a full `xorg.conf` for that, just one `InputClass` snippet:

```text {title="/etc/X11/xorg.conf.d/00-keyboard.conf"}
Section "InputClass"
    Identifier      "system-keyboard"
    MatchIsKeyboard "on"
    Option "XkbLayout"  "us,de"
    Option "XkbModel"   "pc105"
    Option "XkbOptions" "grp:alt_shift_toggle"
EndSection
```

`XkbLayout` sets the layouts, `XkbVariant` picks a variant like `dvorak`, and `XkbOptions` adds extras: `grp:alt_shift_toggle` switches layout with Alt+Shift, and `terminate:ctrl_alt_bksp` lets Ctrl+Alt+Backspace kill a frozen X server.

You rarely write this file by hand. Each distro family has a tool:

```bash
# RHEL / Fedora (and other systemd distros): writes 00-keyboard.conf for you
sudo localectl set-x11-keymap de
localectl status
```

```console
   System Locale: LANG=en_US.UTF-8
       VC Keymap: de
      X11 Layout: de
```

```bash
# Debian / Ubuntu: the layout lives in /etc/default/keyboard
sudo dpkg-reconfigure keyboard-configuration
grep XKB /etc/default/keyboard
```

```console
XKBMODEL="pc105"
XKBLAYOUT="de"
XKBVARIANT=""
XKBOPTIONS=""
```

To change the layout only for the running session, with no file at all, use `setxkbmap`:

```bash
setxkbmap de
setxkbmap -query
```

```console
rules:      evdev
model:      pc105
layout:     de
```

 (Side note: GNOME and KDE keep their own per-user list of input sources. The system layout mostly decides the login screen and the default for new users, so if a user's layout looks "stuck", check their desktop settings too.)

## Troubleshooting with the logs

When the screen stays black, read the X log. Every line starts with a marker:

| Marker | Meaning |
| --- | --- |
| `(EE)` | Error |
| `(WW)` | Warning |
| `(II)` | Information |
| `(**)` | Set in a config file |
| `(==)` | Default value |
| `(--)` | Probed (detected) |
| `(++)` | Set on the command line |

```bash
grep -E '\((EE|WW)\)' /var/log/Xorg.0.log
```

```console
[    12.418] (WW) Falling back to old probe method for fbdev
[    12.512] (EE) Failed to load module "nvidia" (module does not exist, 0)
```

The X log is about the *server*. Errors from your *session* and the programs in it go to **`~/.xsession-errors`**. The session scripts that display managers run (on Debian, `/etc/X11/Xsession`) send everything the clients print on stderr into that file. If your desktop logs in and drops you straight back to the login screen, look there first:

```bash
tail -n 20 ~/.xsession-errors
```

Newer GNOME and KDE sessions send this output to the systemd journal instead, so the file may be missing or old. Then try `journalctl --user -b`.

## Who may connect: xhost and xauth

Any client that can connect to your X server can read your keystrokes, take screenshots and draw over your windows. So X has two ways to decide who gets in.

### xhost: allow by host or user

`xhost` manages a list of allowed hosts. Run without arguments, it shows the current state:

```bash
xhost
```

```console
access control enabled, only authorized clients can connect
SI:localuser:amin
```

| Command | Effect |
| --- | --- |
| `xhost +192.168.1.50` | Allow every user on that host |
| `xhost -192.168.1.50` | Remove that host again |
| `xhost +si:localuser:bob` | Allow local user `bob` (a "server interpreted" entry) |
| `xhost +` | **Turn access control off.** Anyone can connect. |
| `xhost -` | Turn access control back on |

> [!CAUTION]
> `xhost +` with nothing after it opens your display to everyone who can reach it. Old tutorials use it as a quick fix. Don't.

### xauth: allow whoever has the cookie

The better way is a secret token, the **MIT-MAGIC-COOKIE-1**. When X starts, the display manager creates a random cookie and stores it in `~/.Xauthority` (or the file named in the `XAUTHORITY` variable). A client must present the matching cookie to connect. `xauth` manages those cookies:

```bash
xauth list
```

```console
laptop/unix:0  MIT-MAGIC-COOKIE-1  5c2e0a9d1f8b4c37a1e6d2b9f04c8e11
```

To let another machine use your display, copy the cookie for it. This one-liner extracts the cookie for your display and merges it into your `~/.Xauthority` on the remote host:

```bash
xauth extract - $DISPLAY | ssh amin@server xauth merge -
```

## Showing programs on a remote X server

### The modern way: SSH X11 forwarding

SSH tunnels the X protocol through its encrypted connection and handles the cookie for you:

```bash
ssh -X amin@server
echo $DISPLAY
xclock &
```

```console
localhost:10.0
```

The clock runs on `server` but its window opens on your laptop. SSH created a fake display `:10` on the server and forwards it back home. It needs `X11Forwarding yes` in the server's `/etc/ssh/sshd_config` and the `xauth` program installed on the server.

- `ssh -X` treats the remote programs as *untrusted*: they can't snoop on your other windows. Some programs misbehave with it.
- `ssh -Y` gives them full, *trusted* access. Use it only with servers you trust.

### The old way: plain X over the network

Before SSH, you pointed `DISPLAY` straight at the other machine:

```bash
# on the laptop with the screen: allow the server
xhost +server.example.com

# on the server: send windows to the laptop
export DISPLAY=laptop.example.com:0
xclock &
```

This only works if the X server listens on TCP port 6000, and current Xorg and display managers turn that off by default (`-nolisten tcp`). Everything, including your typing, would travel unencrypted. Know it for the exam, use SSH in real life.

## Wayland, the successor

**Wayland** is a newer display protocol that replaces X on most modern desktops. The big change: there's no separate X server and window manager. The **compositor** (Mutter in GNOME, KWin in KDE, or Sway, Weston) *is* the display server, talking straight to the kernel's graphics drivers.

| | X11 | Wayland |
| --- | --- | --- |
| Display server | Xorg plus a separate window manager | The compositor does both |
| Apps can see other windows and keystrokes | Yes | No, each app is isolated |
| Network transparency | Built in | No; use RDP, VNC or app-level tools |
| `xhost`, `xauth`, `DISPLAY` | Yes | No (`WAYLAND_DISPLAY` instead) |
| Old X apps | Native | Run through **XWayland**, a small X server inside the session |

Wayland is the default on Fedora, RHEL 8 and later, Debian 12 (GNOME) and Ubuntu 22.04 and later. RHEL 10 goes further and no longer ships the Xorg server at all, only XWayland. To see what you're running:

```bash
echo $XDG_SESSION_TYPE
```

```console
wayland
```

On a GDM login screen you can still pick "GNOME on Xorg" from the gear menu where it's installed. Admins can disable Wayland with `WaylandEnable=false` in the `[daemon]` section of `/etc/gdm/custom.conf` (RHEL/Fedora) or `/etc/gdm3/custom.conf` (Ubuntu; Debian uses `/etc/gdm3/daemon.conf`).

 (Side note: Even in a Wayland session, `echo $DISPLAY` often prints `:0`. That's XWayland waiting for old X programs. Running `xeyes` and moving the mouse over a native Wayland window is a fun test: the eyes freeze, because X apps can't see Wayland windows.)

## Exam checklist

- In X, the **server** is on the machine with the screen and keyboard; **clients** are the programs, local or remote.
- `DISPLAY` is `host:display.screen`; `:0` is the first local display, and display *n* uses TCP port 6000 + n.
- `/etc/X11/xorg.conf` is optional; put small overrides in `/etc/X11/xorg.conf.d/*.conf`. `Xorg -configure` writes `xorg.conf.new`.
- Keyboard layout: an `InputClass` section with `Option "XkbLayout"`; tools are `localectl set-x11-keymap`, `dpkg-reconfigure keyboard-configuration` and `setxkbmap` (session only).
- Server problems: `/var/log/Xorg.0.log`, `(EE)` errors. Session problems: `~/.xsession-errors`.
- `xhost` allows hosts or users (`xhost +` disables all control). `xauth` manages MIT magic cookies in `~/.Xauthority`.
- `ssh -X` (untrusted) or `ssh -Y` (trusted) forwards X securely; needs `X11Forwarding yes`.
- Display managers (GDM, SDDM, LightDM, XDM) log you in; window managers (Mutter, KWin, Xfwm4) manage windows.
- Wayland: the compositor is the display server, no network transparency, X apps run through XWayland.


## Byte check

1. You log in with `ssh -X amin@server` and run `echo $DISPLAY` on the server. What do you most likely see?
   - `:0`
   - **`localhost:10.0`** (correct)
   - `server:0.0`
   SSH sets up a fake display on the server, usually number 10 and up, and tunnels it back to your local X server. Display `:0` would be the server's own screen.

2. Which file is the safe place to change only the keyboard layout of Xorg?
   - **`/etc/X11/xorg.conf.d/00-keyboard.conf`** (correct)
   - `/usr/share/X11/xorg.conf.d/40-libinput.conf`
   - `~/.xsession-errors`
   Small snippets in `/etc/X11/xorg.conf.d/` override one thing and survive updates. Files in `/usr/share/X11/` belong to packages, and `~/.xsession-errors` is a log.

3. What does `xhost +` do?
   - Adds the current user to the access list
   - **Turns off access control, so any host can connect to your X server** (correct)
   - Starts a second X server on display `:1`
   A bare `+` disables access control completely. Anyone who can reach the display can then see your screen and read your keystrokes.


