Skip to content

Instantly share code, notes, and snippets.

@spinxz
Last active October 2, 2024 11:28
Show Gist options
  • Save spinxz/1692ff042a7cfd17583b to your computer and use it in GitHub Desktop.
Save spinxz/1692ff042a7cfd17583b to your computer and use it in GitHub Desktop.
systemd service file for running a vncserver (e.g. tightvncserver)
# Vncserver service file for Debian or Ubuntu with systemd
#
# Install vncserver and tools
# e.g. apt-get install tightvncserver autocutsel gksu
#
# 1. Copy this file to /etc/systemd/system/vncserver@:1.service
# 2. Edit User=
# e.g "User=paul"
# 3. Edit the vncserver parameters appropriately in the ExecStart= line!
# e.g. the -localhost option only allows connections from localhost (or via ssh tunnels)
# 4. Run `systemctl daemon-reload`
# 5. Run `systemctl enable vncserver@:<display>.service`
#
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
User=<user_name>
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/vncserver -geometry 1800x1000 -depth 16 -dpi 120 -alwaysshared -localhost %i
ExecStop=/usr/bin/vncserver -kill %i
[Install]
WantedBy=multi-user.target
#!/bin/sh
# vnc server xstartup file (~/.vnc/xstartup) for LXDE
#
# The sleeps seem to fix a bug where user input is not registered in vncviewer on client
#
# Fix to make GNOME work:
export XKL_XMODMAP_DISABLE=1
# hack to allow sudo in vncviewer:
xhost +localhost
autocutsel -fork
sleep 2
# unclear if the x-window-manager is needed:
x-window-manager &
sleep 2
lxterminal &
sleep 2
/usr/bin/lxsession -s LXDE -e LXDE &
@peter279k
Copy link

The problem is about your service file: /etc/systemd/system/vnc.service.

You need to specify the %i number. And your service file should change into vnc@:1.service.

It means that you specify :1 to use on your service file and let VNC server running and listening on 5901 port number.

@sanfx
Copy link

sanfx commented Jul 31, 2020

why everyone is using xfce why not kde ?

@peter279k
Copy link

why everyone is using xfce why not kde ?

Xfce I think it's very lightweight, memory saving or I just like the "Mouse" on my desktop 😃!

@soulrebell
Copy link

soulrebell commented Oct 6, 2020

Hi, my vnc server or my service don't working anymore. I didn't change anything.

[pi@Mondroid ~]$ systemctl status vncserver@:1
● vncserver@:1.service - Remote desktop service (VNC)
     Loaded: loaded (/etc/systemd/system/vncserver@:1.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Tue 2020-10-06 11:06:57 CEST; 14min ago
    Process: 52670 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill :1 > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
    Process: 52675 ExecStart=/usr/bin/vncserver :1 -geometry 1440x900 -alwaysshared -localhost :1 (code=exited, status=2)
   Main PID: 52675 (code=exited, status=2)

Okt 06 11:06:56 Mondroid systemd[1]: Starting Remote desktop service (VNC)...
Okt 06 11:06:56 Mondroid systemd[52670]: pam_systemd_home(login:account): systemd-homed is not available: Unit dbus-org.freedesktop.home1.servi>
Okt 06 11:06:56 Mondroid systemd[52670]: pam_unix(login:session): session opened for user pi(uid=1001) by (uid=0)
Okt 06 11:06:56 Mondroid systemd[1]: Started Remote desktop service (VNC).
Okt 06 11:06:56 Mondroid systemd[52675]: pam_systemd_home(login:account): systemd-homed is not available: Unit dbus-org.freedesktop.home1.servi>
Okt 06 11:06:57 Mondroid systemd[52675]: pam_unix(login:session): session opened for user pi(uid=1001) by (uid=0)
Okt 06 11:06:57 Mondroid systemd[1]: vncserver@:1.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Okt 06 11:06:57 Mondroid systemd[1]: vncserver@:1.service: Failed with result 'exit-code'.

Here my config.

[pi@Mondroid ~]$ cat /etc/systemd/system/vncserver@:1.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=simple
User=pi
PAMName=login
PIDFile=/home/%u/.vnc/%H%i.pid
#PIDFile=/home/pi/.vnc/%H%i.pid
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
#ExecStart=/usr/bin/vncserver %i -geometry 1440x900 -alwaysshared -fg
ExecStart=/usr/bin/vncserver %i -geometry 1440x900 -alwaysshared -localhost %i
ExecStop=/usr/bin/vncserver -kill %i

[Install]
WantedBy=multi-user.target
[pi@Mondroid ~]$ cat "/home/pi/.vnc/xstartup"
#!/bin/sh

unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
OS=`uname -s`
exec startlxde
if [ $OS = 'Linux' ]; then
  case "$WINDOWMANAGER" in
    *gnome*)
      if [ -e /etc/SuSE-release ]; then
        PATH=$PATH:/opt/gnome/bin
        export PATH
      fi
      ;;
  esac
fi
if [ -x /etc/X11/xinit/xinitrc ]; then
  exec /etc/X11/xinit/xinitrc
fi
if [ -f /etc/X11/xinit/xinitrc ]; then
  exec sh /etc/X11/xinit/xinitrc
fi
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
twm &

Any hint?

@Thatoo
Copy link

Thatoo commented Mar 14, 2022

Hello,
There is something I don't understand but I hope you will

If I stop vncserver@:1.service
sudo systemctl restart vncserver@:1.service
and I launch simply the command
vncserver -geometry 1800x1000 -depth 16 -dpi 120 -alwaysshared -localhost
it works fine
but if then, I kill vncserver

vncserver -kill :*
Killing Xtigervnc process ID 13965... success!

and I launch vncserver@:1.service
with the content given above

sudo systemctl daemon-reload
sudo systemctl restart vncserver@:1.service

then I get this screen when I try to connect with VNC

image

Any idea how to solve that?

@Thatoo
Copy link

Thatoo commented Mar 14, 2022

I get this same strange result if in crontab -e I add
@reboot vncserver -kill :* ; vncserver

it works only when I launch vncserver in a shell through ssh
Isn't it weird?

@Thatoo
Copy link

Thatoo commented Mar 14, 2022

well to be honest, my startupx file is different as I'm working with a gnome server

#!/bin/sh
# Start Gnome 3 Desktop
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
vncconfig -iconic &
dbus-launch --exit-with-session gnome-session &

@peter279k
Copy link

peter279k commented Mar 15, 2022

Sorry. I'm not very good at using the VNC server on the Gnome desktop.

Perhaps your issue is about the desktop environment and it's the internal error on the Gnome desktop environment or VNC server settings.

And I don't use the systemctl and vncserver to control the VNC server at the same time when they're available. I only use the systemctl when the VNC server service is enabled on the operating system daemon. Or using the vncserver command to control the VNC server when the VNC server daemon is not existed/enabled on the operating system daemon.

@Thatoo
Copy link

Thatoo commented Mar 15, 2022

Well I don't use the systemctl and vncserver to control the VNC server at the same time.
I'd prefer using systemctl but with it, it doesn't work whereas without it but launching the vncserver command works (but not within a boot cron)...
Thank you anyway.

@peter279k
Copy link

Perhaps your VNC server service file setting is incorrect, but I have no experience and available time to test the VNC server with Gnome desktop.

@Thatoo
Copy link

Thatoo commented Mar 15, 2022

I understand, thank you. It seems like VNC isn't yet a plug&play technology... I can't find how to make VNC working through SSH on Ubuntu 20.04 that launch automatically on startup. Never mind, I'll give xrdp a try.

@Mastiffen
Copy link

I am trying to use the file on Raspbian x86 in a virtual machine under VMware (whicn doesn't have RealVNC installed in the image, or for free). It seems like it's starting, but I can't connect to it. Here's the status, when I run vnc@:1.service:

`● vnc@:1.service - Remote desktop service (VNC)
Loaded: loaded (/lib/systemd/system/vnc@:1.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-10-29 21:22:38 CEST; 19s ago
Process: 4739 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill :1 > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
Process: 4747 ExecStart=/usr/bin/vncserver -geometry 1800x1000 -depth 16 -dpi 120 -alwaysshared -localhost :1 (code=exite>
Tasks: 49 (limit: 4614)
Memory: 64.0M
CPU: 3.981s
CGroup: /system.slice/system-vnc.slice/vnc@:1.service
├─4758 Xtightvnc :1 -desktop X -auth /home/pi/.Xauthority -geometry 1800x1000 -depth 16 -rfbwait 120000 -rfbauth>
├─4764 /bin/sh /home/pi/.vnc/xstartup
├─4767 /usr/bin/lxsession -s LXDE-pi -e LXDE
├─4780 dbus-launch --autolaunch 79becc19a60b43158ba55566836eef1e --binary-syntax --close-stderr
├─4781 /usr/bin/dbus-daemon --syslog-only --fork --print-pid 5 --print-address 7 --session
├─4806 /usr/bin/dbus-launch --exit-with-session --sh-syntax
├─4807 /usr/bin/dbus-daemon --syslog --fork --print-pid 5 --print-address 7 --session
├─4828 /usr/bin/ssh-agent x-session-manager
├─4856 /usr/libexec/gvfsd
├─4861 /usr/libexec/gvfsd-fuse /home/pi/.cache/gvfs -f
├─4869 lxpolkit
├─4877 pcmanfm --desktop --profile LXDE-pi
├─4887 /usr/bin/ssh-agent -s
├─4901 /usr/bin/python3 /usr/share/system-config-printer/applet.py
├─4970 /usr/libexec/gvfs-udisks2-volume-monitor
├─4976 /usr/libexec/gvfs-mtp-volume-monitor
├─4984 /usr/libexec/gvfs-gphoto2-volume-monitor
├─4989 /usr/libexec/gvfs-goa-volume-monitor
├─4994 /usr/libexec/gvfs-afc-volume-monitor
├─5057 /usr/libexec/gvfsd-trash --spawner :1.2 /org/gtk/gvfs/exec_spaw/0
└─5384 /usr/lib/vmware-tools/sbin32/vmtoolsd -n vmusr --blockFd 3

okt. 29 21:22:39 raspberry dbus-daemon[4807]: [session uid=1000 pid=4805] Activating service name='org.gtk.vfs.UDisks2VolumeM>
okt. 29 21:22:40 raspberry dbus-daemon[4807]: [session uid=1000 pid=4805] Successfully activated service 'org.gtk.vfs.UDisks2>
okt. 29 21:22:40 raspberry dbus-daemon[4807]: [session uid=1000 pid=4805] Activating service name='org.gtk.vfs.MTPVolumeMonit>
`

Any Idea why I can't connect on 5900, 5901, 5800 or 5801?

@lasag-2511
Copy link

Has anyone configured this with newer versions of tigervnc ? Tigervnc recommends that we need to put users in /etc/tigervnc/tigervnc.users

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment