Linux: X11 forwarding over SSH and minimal Xorg install
Setting up X11 forwarding over SSH, testing it, using a Windows X server and installing a minimal Xorg without a desktop.
X11 forwarding lets you run graphical applications on a remote Linux host and display their windows on your local workstation through an SSH connection. This page covers the server and client settings, how to test the setup, and how to install a minimal Xorg server on a system without a desktop.
Server configuration
Edit /etc/ssh/sshd_config on the server:
AllowTcpForwarding yes
X11UseLocalhost yes
X11DisplayOffset 10
X11Forwarding yes
Then start or restart the SSH daemon:
sudo systemctl start sshd
sudo systemctl status sshd
Client configuration
Enable X11 forwarding in ssh_config on the client (ForwardX11 yes), then connect with -X:
ssh -X user@host
Test the forwarding
Check the display variable first. With X11DisplayOffset 10, it is typically set to a value such as localhost:10.0. An empty value means forwarding is not working.
echo $DISPLAY
xeyes
xclock
xlogo
xcalc
xmessage -center hello!
xterm
xdg-open 'https://example.com'
# Shows whether the session is X11 or Wayland
printf 'Session is: %s\n' "${DISPLAY:+X11}${WAYLAND_DISPLAY:+WAYLAND}"
Windows X server
With an X server on Windows such as Xming, start it with multiple windows and clipboard sharing:
Xming.exe :0 -clipboard -multiwindow
Confirm that the X server listens on TCP port 6000:
netstat -na | grep ':6000 .*LISTEN'
On the client, point DISPLAY to the Windows machine:
export DISPLAY=192.0.2.10:0
Under WSL, the Windows host address is the nameserver in /etc/resolv.conf:
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
X410 is another X server for Windows, see x410.dev.
Forwarding through sudo
When you switch to another account with sudo, the X authorization cookie of the original user is not available. Copy it to the root user's .Xauthority file:
sudo xauth add $(xauth -f ~app_user/.Xauthority list | tail -1)
Allow the local root user to connect to the X server, then keep the current DISPLAY value when running the command as root:
xhost +local:root
sudo DISPLAY=$DISPLAY xclock
Minimal Xorg installation (no desktop)
Check the graphics hardware first:
lspci | grep -i vga | cut -d: -f3
| Distribution | Commands |
|---|---|
| Debian / Ubuntu (test tools) | sudo apt-get install x11-apps |
| Debian (minimal Xorg) | sudo apt-get update && sudo apt-get install xfonts-base xserver-xorg-input-all xinit xserver-xorg xserver-xorg-video-all |
| RHEL / CentOS / Fedora | sudo yum install xorg-x11-server-Xorg xorg-x11-xauth xorg-x11-apps -y |
| Arch Linux | sudo pacman -S xorg-server xorg-xhost |
The Arch Linux OpenSSH documentation is at wiki.archlinux.org.
Removing the GUI on RHEL 8
Switch the default boot target to text mode:
systemctl set-default multi-user.target
systemctl isolate multi-user.target
To go back to the graphical desktop:
systemctl isolate graphical.target
systemctl set-default graphical.target
To replace the "Server with GUI" group with the minimal server group:
yum groups list
sudo dnf groupinstall -y "Minimal Install"
sudo dnf groupremove -y "Server with GUI"
sudo dnf groupinstall -y "Server"
To keep only the minimal X libraries needed for forwarding, enable the CodeReady Builder repository and install the X server packages:
sudo subscription-manager repos --enable=codeready-builder-for-rhel-8-x86_64-rpms
sudo yum install xorg-x11-server-Xorg xorg-x11-xauth xorg-x11-apps -y
Notes
- If
sshdwas already running when you changedsshd_config, restart it so the new settings are loaded. X11UseLocalhost yesmakes the forwarded display listen on the loopback interface only.- On some systems, X applications started with
sudoneed the dbus service running; the source note starts it withsudo /etc/init.d/dbus start. - The
xhostcommand line in the source note was altered by anonymisation;xhost +local:rootis the standard form, check it on your system before use.