How to install x11vnc vnc server as a service on Ubuntu 20.04, for remote access or screen sharing

Hey All – revisiting an old one again here with an update for 20.04 (and also the versions in between (most probably).

So why you might want to do this is for remote access via a GUI, or screen sharing, or to a headless server, or virtual machine? Installing x11vnc from the gnome-software centre doesn’t ensure that it starts on boot without a graphical session being logged in each time the machine restarts, hence a slightly longer process is needed.

First of all lets do a
sudo apt-get update

First thing we need to do is change the display manager over to lightdm (away from gnome display manager 3).
sudo apt-get install lightdm
followed by a quick reboot
sudo reboot
and then
sudo apt-get install x11vnc

You can skip this step if you like but I would recommend running x11vnc from a terminal and trying to connect remotely. (this is insecure but it’s fine for testing on a LAN I guess.)

Next comes the service bit:
we need to create this file
sudo nano /lib/systemd/system/x11vnc.service
and copy paste in the following

[Unit]
Description=x11vnc service
After=display-manager.service network.target syslog.target

[Service]
Type=simple
ExecStart=/usr/bin/x11vnc -forever -display :0 -auth guess -passwd password
ExecStop=/usr/bin/killall x11vnc
Restart=on-failure

[Install]
WantedBy=multi-user.target

So what does this mean?

  • start after some other services have been started / target points have been reached,
  • forking as the x11vnc creates a child process from the -forever clause.
  • the ExecStart and Stop commands should be self explanatory,
  • we want it to be restarted on failure,
  • we also want the service to be started before the process reaches the multi-user target.

Super – now we need to tell the systemd system there is a change and to reload systemctl daemon-reload, followed by enabling the new service systemctl enable x11vnc.service. While we’re there we may aswell start the service then test it systemctl start x11vnc.service.

You can check on the status of this service, like others, using systemctl status x11vnc.service.

One last test before calling it done is to reboot the system once again. It’s important to check that you can log in from the ‘login’ screen.

OH – A GOTYA – don’t lock the screen as you wont be able to VNC back in.
So change your settings>privacy>screen lock to the following.

So this isn’t so secure so you might need to install something called xscreensaver.

great file here:
https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files