VNC
Jump to navigation
Jump to search
Setting up VNC to startup with a systemd unit file can be an exercise in pain. There are two separate ways to do it:
- A global systemd unit file that launches the VNC server a as a user
- A user level systemd unit file that launches the VNC server as the use who runs it
Both have their pros and cons, with the latter being that the systemd unit file won't run until the user logs in.
Installation
TigerVNC
- Install TigerVNC
sudo apt install tigervnc-standalone-server tigervnc-xorg-extension tigervnc-viewer
Configuration
- Run the following while logged in as the user you want to run the VNC server as to set the vnc password:
vncpasswd
- Configure the desktop environment that will run with the VNC server (this uses xfce) (save into ~/.vnc/xstartup:
#!/bin/sh
# Start Desktop
#[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
#[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
#vncconfig -iconic &
#dbus-launch --exit-with-session gnome-session &
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4
# XFCE: startxfce4 or xfce4-session
#exec startkde
#exec xfce4-session
# Gnome: startx
#exec startx
# To get an Ubuntu desktop
#vncconfig −nowin &
#dbus-launch --exit-with-session env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu &
- Allow script to be executed with
chmod +x ~/.vnc/xstartup
System unit file
- Create /etc/systemd/system/vncserver@.service (note substitute username for actual username of user VNC will run under):
[Unit]
Description=TigerVNC Service
After=syslog.target network.target
[Service]
Type=simple
RemainAfterExit=yes
SuccessExitStatus=0
PIDFile=/home/username/.vnc/%H:%i.pid
ExecStartPre=-/bin/su -l username -c "/usr/bin/tigervncserver -kill :%i > /dev/null"
ExecStart=/bin/su -l username -c "/usr/bin/tigervncserver :%i -localhost no -depth 24 -geometery 1280x800"
ExecStop=/bin/su -l username -c "/usr/bin/tigervncserver -kill :%i"
[Install]
WantedBy=default.target
- Refresh systemd with
systemctl daemon-reload
- Enable VNC with
systemctl enable vncserver@1
User unit file
- Create directories
mkdir -p $HOME/.local/share/systemd/user/
- Create $HOME/.local/share/systemd/user/vncserver@.service:
[Unit]
Description=TigerVNC Service
[Service]
Type=forking
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver :%i -localhost no -depth 24 -geometery 1280x800
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=default.target
- Refresh systemd with
systemctl daemon-reload --user
- Enable VNC with
systemctl enable vncserver@1 --user