Skip to content

Instantly share code, notes, and snippets.

@felipecsl
Created September 14, 2025 02:32
Show Gist options
  • Save felipecsl/0b89532f5dc85abf25eee91125779c27 to your computer and use it in GitHub Desktop.
Save felipecsl/0b89532f5dc85abf25eee91125779c27 to your computer and use it in GitHub Desktop.
Run x11vnc as a service for headless VNC
sudo tee /usr/local/bin/headless-xfce-vnc.sh >/dev/null <<'EOF'
#!/usr/bin/env bash
set -Eeuo pipefail
DISPLAY_NUM=${DISPLAY_NUM:-1}
GEOM=${GEOM:-1920x1080x24}
RFBPORT=${RFBPORT:-5900}
DISPLAY=":${DISPLAY_NUM}"
export DISPLAY
# 1) Start Xvfb
/usr/bin/Xvfb "$DISPLAY" -screen 0 "$GEOM" -nolisten tcp -noreset &
XVFB_PID=$!
# 2) Wait for the X socket to appear
for i in {1..60}; do
[ -S "/tmp/.X11-unix/X${DISPLAY_NUM}" ] && break
sleep 0.5
done
# 3) Start a desktop session under a D-Bus session
# (dbus-run-session cleans up on exit)
(/usr/bin/dbus-run-session /usr/bin/startxfce4) &
# 4) Serve it via VNC
exec /usr/bin/x11vnc -display "$DISPLAY" \
-rfbauth "$HOME/.vnc/passwd" \
-forever -shared -rfbport "$RFBPORT"
EOF
sudo chmod +x /usr/local/bin/headless-xfce-vnc.sh
sudo systemctl daemon-reload
sudo systemctl enable x11vnc-headless@<username>.service
sudo systemctl start x11vnc-headless@<username>.service
sudo apt update
sudo apt install x11vnc xvfb xfce4 xfce4-goodies dbus-x11
# make sure you have a VNC password file:
x11vnc -storepasswd
sudo tee /etc/systemd/system/[email protected] >/dev/null <<'EOF'
[Unit]
Description=Headless XFCE desktop over VNC for user %i
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=%i
Environment=HOME=/home/%i
# Tweak these to taste:
Environment=DISPLAY_NUM=1
Environment=GEOM=1920x1080x24
Environment=RFBPORT=5900
ExecStart=/usr/local/bin/headless-xfce-vnc.sh
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment