You are on page 1of 2

Method 2: Automatically start a VNC session via a systemd service

script
To configure VNC for this method, you must have root privileges. You will set up a
systemd service file for all intended VNC users with their own preassigned unique
display ID.

1. Log in and get root privileges.


2. sudo -s
3. Make sure the user accounts already exist. Use the following command to list all
users.
4. cut -d: -f1 /etc/passwd
5. Create the path /etc/systemd/system .

6. mkdir -p /etc/systemd/system
7. Create a systemd service script file vncserver@:[X].service , where [X] is the display
ID, for each user in /etc/systemd/system Each user must be assigned a unique display
ID. Be sure the correct username is entered in the User field. The example below
shows user vnc-user-b who is assigned the display ID 5.
8. # cat > /etc/systemd/system/vncserver@:5.service << EOF
9.
10. [Unit]
11. Description=VNC Remote Desktop Service for "vnc-user-b" with display ID "5"
12. After=syslog.target network.target
13.
14. [Service]
15. Type=simple
16. User=vnc-user-b
17. PAMName=login
18. PIDFile=/home/%u/.vnc/%H%i.pid
19. ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
20. ExecStart=/usr/bin/vncserver %i -geometry 2000x1200 -alwaysshared -fg
21. ExecStop=/usr/bin/vncserver -kill %i
22.
23. [Install]
24. WantedBy=multi-user.target
25.
26. EOF
27. Have each user log into their account and set a VNC password with
the vncpasswd command before proceeding to the next step.
28. Start the VNC service script and set it to start automatically on boot for each user.
Replace the [X] with the display ID.
29. systemctl daemon-reload
30. systemctl start vncserver@:[X].service
31. systemctl enable vncserver@:[X].service
32. After starting the services, verify they are running.
33. systemctl | grep vnc
The example below shows 2 VNC sessions that were successfully started for users
vnc-user-b with display ID 5 and vnc-user-c with display ID 6.
# systemctl | grep vnc

vncserver@:5.services loaded active running VNC Remote Desktop Service for "vnc-user-b"
with display ID "5"
vncserver@:6.services loaded active running VNC Remote Desktop Service for "vnc-user-c"
with display ID "6"
system-vncserver.slice loaded active active system-vncserver.slice

You might also like