How do I configure x11vnc.service to run x11vnc as root?

I'd like to access my "real" display remotely. Basically use my macbook to pilot what I see on the display of my local server. My goal is that the same image is duplicated: in the actual display of the linux box and via VNC on my macbook. I'm running Ubuntu 20.04 and it's November 2020.

As this dude says very poetically "I installed nomachine and it took the grand total of 2 minutes to setup and it works as I wanted".

The link also explains you must switch to lightdm. Fine.

if I try:

/usr/bin/x11vnc -display :0 -auth guess

I get:

22/11/2020 20:23:51 x11vnc version: 0.9.16 lastmod: 2019-01-05  pid: 4745
22/11/2020 20:23:51 -auth guess: failed for display=':0'

if I try:

/usr/bin/x11vnc -display :0 

I get:

22/11/2020 20:29:26 x11vnc version: 0.9.16 lastmod: 2019-01-05  pid: 5167
Invalid MIT-MAGIC-COOKIE-1 key22/11/2020 20:29:26 XOpenDisplay(":0") failed.
22/11/2020 20:29:26 Trying again with XAUTHLOCALHOSTNAME=localhost ...
Invalid MIT-MAGIC-COOKIE-1 key
22/11/2020 20:29:26 ***************************************
22/11/2020 20:29:26 *** XOpenDisplay failed (:0)

*** x11vnc was unable to open the X DISPLAY: ":0", it cannot continue.
*** There may be "Xlib:" error messages above with details about the failure.

if I try:

sudo /usr/bin/x11vnc -display :0

I get:

22/11/2020 20:30:22 x11vnc version: 0.9.16 lastmod: 2019-01-05  pid: 5171
No protocol specified
22/11/2020 20:30:22 XOpenDisplay(":0") failed.
22/11/2020 20:30:22 Trying again with XAUTHLOCALHOSTNAME=localhost ...
No protocol specified

22/11/2020 20:30:22 ***************************************
22/11/2020 20:30:22 *** XOpenDisplay failed (:0)

*** x11vnc was unable to open the X DISPLAY: ":0", it cannot continue.
*** There may be "Xlib:" error messages above with details about the failure.

Finally if I try:

sudo /usr/bin/x11vnc -display :0 -auth guess

I get:

22/11/2020 20:32:24 x11vnc version: 0.9.16 lastmod: 2019-01-05  pid: 5188
xauth:  file /root/.Xauthority does not exist
22/11/2020 20:32:24 -auth guess: using 'XAUTHORITY=/var/run/lightdm/root/:0' for disp=':0'
22/11/2020 20:32:24 Using X display :0
22/11/2020 20:32:24 rootwin: 0x583 reswin: 0x600001 dpy: 0xfd57d280
The VNC desktop is:      NAS:0
PORT=5900

Which is great!

But now how can I make x11vnc run without sudo so that it can run automatically using systemd?

This is my current /etc/systemd/system/x11vnc.service

# Description: Custom Service Unit file
# File: /etc/systemd/system/x11vnc.service
[Unit]
Description="x11vnc"
Requires=display-manager.service
After=display-manager.service

[Service]
ExecStart=/usr/bin/x11vnc -loop -nopw -xkb -repeat -noxrecord -noxfixes -noxdamage -forever -rfbport 5900 -display :0 -auth guess
ExecStop=/usr/bin/killall x11vnc
Restart=on-failure
RestartSec=2

[Install]
WantedBy=multi-user.target

Two more interesting links on this topic: one with x11vnc and one with TigerVNC


Solution 1:

You could try adding -auth /run/lightdm/root/:0 instead of guess, as this is the default path for quite a while now with most distros/display managers.

If this doesn't help, it seems there is a race condition despite the ordering you enforced via your service file. Maybe the display-manager service signals "ready" before everything is actually properly up. A not-so-nice-but-works workaround for that could be adding ExecStartPre=/bin/sleep 5 to your service to make sure X11 is properly up and running.

There is also libvnc.so from the TigerVNC package (apt install tigervnc-xorg-extension), which is a module you can load directly into your X11 server. In theory that should yield the best performance, but last time I tried it there were issues with some applications (like VMware Player) just displaying as a black window remotely.

Create /etc/X11/xorg.conf.d/10-vnc.conf with content:

Section "Module" 
Load "vnc" 
EndSection

Section "Screen" 
Identifier "Screen0" 
Option "UserPasswdVerifier" "VncAuth" 
Option "PasswordFile" "/path/to/passwd" 
EndSection

Where /path/to/passwd was created via the vncpasswd command.