Creating a systemd service unit for x0vncserver in DietPi

64 views
Skip to first unread message

Ricky Saull

unread,
Apr 26, 2024, 8:56:47 AMApr 26
to TigerVNC User Discussion/Support
Hello everyone.

I am very much a hobbyist/novice when it comes to Linux. I was wondering if someone could help me successfully create a systemd service for x0vncserver. I have found this official guide on the GitHub repo. Just from looking at it, I can tell it won't work for me "out of the box", but I haven't had much luck after trying to modify the start script and service file on my own.

I am running the DietPi distro (very lightweight Debian bookworm, no desktop) on a Raspberry Pi 5 (4GB). As of right now, I manually start the x0vncserver with the following command as the non-root user "dietpi": 

x0vncserver -passwordfile ~/.vnc/passwd -display :0 -securitytypes tlsplain

Currently, the only thing that I use my Pi for is automatically launching chromium in kiosk mode (a feature of DietPi). As far as I know, the DietPi script that handles this just uses X to launch chromium and nothing else.

I think that my problems lie within the service file. Executing the start script launches the x0vncserver just fine. The service file appears to get stuck in a loop of trying to start the x0vncserver. I would offer an explanation of my logic for what you'll see in the snippets below, but I must admit that I attempted to use ChatGPT to assist me. :-P

Here's the bit of code from the script that launches chromium:
# Use "startx" as non-root user to get required permissions via systemd-logind
STARTX='xinit'
[ "$USER" = 'root' ] || STARTX='startx'

exec "$STARTX" "$FP_CHROMIUM" $CHROMIUM_OPTS "${URL:-https://dietpi.com/}" -- -nocursor



Here are the changes I've made to the start script and service file:

x0vnc.sh
#! /bin/bash

# x0vncserver -passwordfile ~/.vnc/passwd -display :0 -geometry 1920x1080 -securitytypes tlsplain

# Export an environment variable of the Display Manager
export DISPLAY=:0
export XAUTHORITY="/home/dietpi/.Xauthority"

# 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="-passwordfile ~/.vnc/passwd -display $DISPLAY -geometry 1920x1080 -fg -SecurityTypes tlsplain"

## Launch VNC server
($VNC_BIN $PARAMS)

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



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

# Require start of
Requires=network-online.target

# Wait for
After=network-online.target
After=xinit.service

[Service]
Type=forking

# Set environment
User=dietpi
Group=dietpi
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/dietpi/.Xauthority
Environment=HOME=/home/dietpi

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

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

[Install]
WantedBy=multi-user.target


Reply all
Reply to author
Forward
0 new messages