Remote automatic desktop session with Debian and VNC/X2Go

This post briefly describes a setup used for performing UI-based testing of Bitsquare. It consists of a headless Debian server (physical, though it may perfectly be some virtual machine or even a container) which, on boot, automatically starts an X session and makes it available via VNC and X2Go (since there is no monitor to access the session on).

This setup allows to automatically start UI tests on boot and later connect to the graphical session if needed.

We start by installing a plain Debian Jessie setup with just root and a normal user. We avoid the complete X.Org installation (with video card drivers and hardware detection) on purpose, so to keep the system minimal we only select the SSH server and base system profiles when asked, and we leave desktop environment out.

Headless X

Once the new system has booted, we install the xserver-xorg-video-dummy package, which provides an X server that can run completely headless:

# apt-get install xserver-xorg-video-dummy

We configure X to use this server with the following /etc/X11/xorg.conf file (as described in Add Fake Display when No Monitor is Plugged In):

Section "Device"
    Identifier  "Configured Video Device"
    Driver      "dummy"

Section "Monitor"
    Identifier  "Configured Monitor"
    HorizSync   31.5-48.5
    VertRefresh 50-70

Section "Screen"
    Identifier  "Default Screen"
    Monitor     "Configured Monitor"
    Device      "Configured Video Device"
    DefaultDepth 24
    SubSection  "Display"
    Depth       24
    Modes       "1280x1024"

Please note that we have no desktop environment yet. The following command installs the base X.Org system (which will use the existing dummy server) and the i3 window manager (though you may choose something heavier like GNOME, KDE or XFCE):

# apt-get install xorg i3

Remote desktop support

Since there is no display manager too, X sessions need to be started with the good old startx command. However that would still be of little use since graphics are nowhere to be shown. That is no problem as long as we are somehow able to access the graphical session remotely. VNC and X2Go are two solutions for that, with the latter being more amenable to low bandwidth and high latency links (thanks Eloi Rebes and Ramon Selga from for pointing me to this one!).

The following commands install one of the many VNC servers available in Debian and the X2Go package needed for serving an existing session (X2Go can also run new sessions, but we are not interested in this setup; also note that we need to add an external repository for X2Go server packages):

# apt-get install x11vnc
# echo 'deb jessie main' > /etc/apt/sources.list.d/local-x2go.list
# apt-key adv --recv-keys --keyserver E1F958385BFE2B6E
# apt-get update
# apt-get install x2godeskstopsharing

The following ~user/.xsession script for the user X session starts the VNC server and the X2Go server along a terminal emulator and the i3 window manager:

x11vnc -usepw -ncache -shared -forever &
x2godesktopsharing --activate-desktop-sharing &
x-terminal-emulator &
exec i3

Remember to make the script executable:

$ chmod 0750 ~user/.xsession

Please note that the VNC server will listen on port 5900 with no encryption, so it should only be used in trusted local networks. Otherwise use X2Go, which is tunneled over SSH (that is why we selected the SSH server profile above). You may also use -localhost and tunnel VNC over SSH, but it may get really slow. Also note that the VNC server will ask for a password which you should first write in ~user/.vnc/passwdfile (remember to make it unreadable to others with chmod 0600 =~user/.vnc/passwdfile).

Automatic desktop on boot

Finally we want the user to automatically login on boot and start a desktop session. Since Debian Jessie uses systemd by default, we create the /etc/systemd/system/getty@tty1.service.d/autologin.conf unit file to start a user login (as explained in Debian Jessie and autologin):

ExecStart=-/sbin/agetty --autologin user %I

Then we need to make the user login automatically start an X session if none is running. This is easy to do, we just add the following line to ~user/.profile (as indicated in Start X after automatic login):

test -z "$DISPLAY" -a "$(tty)" = /dev/tty1 && exec startx

Now we can reboot the server and everything should start as expected (X server messages will be shown in the console).

Connecting remotely

To acces user's desktop session from a trusted local network you may use any VNC client:

$ xtightvncviewer SERVER_ADDRESS

To access it using X2Go you may use x2goclient or the leaner pyhoca-cli:

$ pyhoca-cli --server SERVER_ADDRESS --username user -D user@:0 --share-mode 1