I usually am working on my servers through ssh. Occasionally, though, I find that I need to use the GUI. In those cases, I always tunnel my VNC connection over ssh, but then I need to ssh into the machine and manually start the x11vnc server. This can become a little cumbersome, and I decided to make a service for x11vnc to make it available all the time. Do note that for security reasons, I don’t recommend opening the VNC port towards the internet. However, in my case it is safe because the only open port is that of ssh, and I still tunnel the VNC port over that connection.

So, I created the following file:

sudo nano /lib/systemd/system/x11vnc.service

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

[Service]
Type=simple
ExecStart=/usr/bin/x11vnc -forever -create -display :0 -auth <EDITED> -passwd <EDITED>
ExecStop=/usr/bin/killall x11vnc
Restart=on-failure

[Install]
WantedBy=multi-user.target

Note that even though I tunnel this over ssh, I still have a password for my VNC connection that is different from the ssh password.

After creating this file, it was just a matter of enabling and starting the service:

 $ sudo systemctl enable x11vnc.service
 $ sudo systemctl start x11vnc.service

Now I can start, stop, and check the status of the x11vnc service, and it will automatically create a new x11vnc connection every time I close my current one. This will allow me to reconnect as long as my ssh tunnel is open!

Linux – keep it simple.

References:

[1] https://askubuntu.com/questions/1294368/how-do-i-configure-x11vnc-service-to-run-x11vnc-as-root

[2] https://unix.stackexchange.com/questions/402201/creating-x11vnc-system-service

Leave a Reply

Your email address will not be published. Required fields are marked *