Systemd unit for x0vncserver - TigerVNC/tigervnc GitHub Wiki

To get autostart for x0vncserver, you need to prepare a start script and a systemd unit.

Start script

Create a file with path /usr/local/bin/x0vnc.sh.

#! /bin/bash

# Export an environment variable of the Display Manager
export XAUTHORITY="/var/run/lightdm/root/:0"

# Start VNC server for :0 display in background
## Set path to binary file
VNC_BIN=/usr/bin/x0vncserver

## Set parameters
## WARNING: This use of SecurityTypes is insecure.
##          Anyone can connect to the VNC server without any authentication.
PARAMS="-display :0 -fg -SecurityTypes None"

## Launch VNC server
($VNC_BIN $PARAMS)

# Provide dirty exit code so that systemd
# will restart the server when a user logs out
exit 1

Systemd service unit

Create a file with path /etc/systemd/system/x0vncserver.service.

[Unit]
Description=Remote desktop service (VNC) for :0 display

# Require start of
Requires=display-manager.service

# Wait for
After=network-online.target
After=display-manager.service

[Service]
Type=forking

# Set environment
Environment=HOME=/root

# Start command
ExecStart=/usr/local/bin/x0vnc.sh

# Restart service after session log out
Restart=on-failure
RestartSec=500ms

[Install]
WantedBy=multi-user.target

Check out work of script

Give permissions for an execution

sudo chmod +x /usr/local/bin/x0vnc.sh

Launch the script

sudo /usr/local/bin/x0vnc.sh

Try to connect via VNC. No password is required.

Check out work of systemd unit

sudo systemctl daemon-reload
sudo systemctl start x0vncserver.service
sudo systemctl status x0vncserver.service

Enable autostart

sudo systemctl enable x0vncserver.service

Stop service/disable autostart

sudo systemctl stop x0vncserver.service
sudo systemctl disable x0vncserver.service