VNC server in Jessie (using a systemd service)


21 posts
by xanthor » Fri Oct 16, 2015 4:54 pm
VNC server in Jessie (using a systemd service)

Hello,

I couldn't find anywhere how to create a vnc server (using TightVNC) service for systemd, which replaces old System V init starting with Debian / Raspbian 8 (Jessie). So here's how I managed to do that.

1. Install Tight VNC server:
Code: Select all
sudo apt-get install tightvncserver

2. Start it manually (for example by running
Code: Select all
vncserver :1
) and enter new VNC password.
3. Create a new file /etc/systemd/system/vncserver@.service with the following contents:

Code: Select all
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=forking
User=pi
PAMName=login
PIDFile=/home/pi/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target


4. (Optional) Replace the parameters in the unit file, if you want to.
5. Run
Code: Select all
sudo systemctl daemon-reload && sudo systemctl enable vncserver@1.service

6. Reboot.

I also changed
Code: Select all
xsetroot -solid grey

to
Code: Select all
xsetroot -solid grey -cursor_name left_ptr


in my .vnc/xstartup file (in my home directory), since there was a cross (X) displayed instead of a normal mouse cursor icon after connecting to the VNC server.

That's all. Have fun. :-)
Posts: 10
Joined: Fri Oct 16, 2015 3:16 pm
by diederik » Fri Oct 16, 2015 6:52 pm
I don't use vnc myself, but still ... nice contribution 8-)
Posts: 389
Joined: Wed Mar 26, 2014 11:17 pm
by Bosse_B » Sat Oct 17, 2015 8:52 am
GREAT job xantor!
I have been looking for this solution for about a week now since I first tried Jessie.
A couple of questions regarding this:
1) The file to create
Is it named:
Code: Select all
/etc/systemd/system/vncserver@.service

Name includes @ and a period?

2) The vnc display number
I have noticed that one cannot use display :0 on Jessie so I expected the display number :1 to appear in the file.
But what I see is:
Code: Select all
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i

How does that work? From where does %i get its value???

I use the following inside the vncboot script that works on Wheezy but has no effect whatsoever in Jessie:
Code: Select all
  su - pi -c "/usr/bin/vncserver :1 -geometry 1400x1000 -depth 24 -dpi 96"


3) VNC user account
I am very concerned that the VNC server runs as user pi, because I had to scrap a whole installation and start over when the original tutorial I followed did everything as root and therefore I could not use the VNC window for anything sensible.
So will these lines ensure that the service vncserver runs as user pi?
Code: Select all
[Service]
Type=forking
User=pi
PAMName=login
PIDFile=/home/pi/.vnc/%H:%i.pid


4) How to start the service without reboot?
Is it not possible to start the service without a reboot?
Or did you mean to reboot in order to check that it actually works on reboot?

5) Removing the service?
If all goes astray, how can I remove the service so I can run the tightvncserver manually instead?

All of this of course is due to the shift to systemd as the boot manager....

6) Comment regarding the cursor
In TightVNC client on Windows there is a "Connection options" setting to disable the cursor from the Pi and use the normal Windows cursor instead. It might not be what you want, though, because then the cursor does not change to indicate possibility to stretch the current window in the display for instance. But the ugly cross is at least gone.
Now I tested your suggestion and it works really well! :D
I had to change the VNC client settings to:
Mouse cursor: Let remote server deal with cursor
Local cursor shape: Do not show

Thanks for your post!
Bo Berglund
Sweden
Posts: 503
Joined: Thu Jan 30, 2014 9:53 am
by xanthor » Sat Oct 17, 2015 4:16 pm
First, I must say I'm definitely not a systemd expert, so I recommend googling for more information (at least the basic systemctl commands like list-unit-files, daemon-reload, enable, disable, start, stop, restart and status, which are basically self-explanatory anyway). But I'll try to answer your questions as best I can:

1. Yes, it's a "template unit".

2. It's a template unit, so %i is replaced with whatever text I use after the @ character in commands like
Code: Select all
sudo systemctl enable vncserver@1.service
So if you use vncserver@1.service in your commands, all instances of %i are replaced with 1 when the unit file is processed.

I don't know whether su even works in systemd services...

3. If you want to run it as a different user, first start vncserver manually as that user and enter the password (to make sure it can find the password when run as a systemd service), then change the User and PIDFile parameters in the unit file.

Just remember that tightvncserver creates new desktop (just for remote use), it doesn't use the desktop you see when you connect your Pi to a screen directly. For that you must use something like x11vnc.

4. It was simpler for me to write and to make sure everything works as it should. But if you just start the service with
Code: Select all
sudo systemctl start vncserver@1.service
it should work too.

5. You can either disable it:
Code: Select all
sudo systemctl disable vncserver@1.service

or remove it completely:
Code: Select all
sudo systemctl remove vncserver@1.service

(maybe you can/should remove 1 from the name when removing, I'm not sure about that).

6. I'll probably keep using the remote cursor + local "dot" cursor. :-)

Have a nice day!
Posts: 10
Joined: Fri Oct 16, 2015 3:16 pm
by Bosse_B » Sat Oct 17, 2015 10:26 pm
Thanks for your explanation!
It works for me too. :D
Now I will put this into my documentation file for future reference.
Bo Berglund
Sweden
Posts: 503
Joined: Thu Jan 30, 2014 9:53 am
by KDMcM » Sun Dec 06, 2015 10:32 pm
Worked for me after this approach failed. (For a start, no-one mentions that it assumes you;re running the Pi without the GUI.)

Many thanks.
Posts: 7
Joined: Tue Dec 01, 2015 11:07 pm
Location: Redditch, England
by avalloneandrea » Thu Dec 17, 2015 9:14 am
The script works great, but I always get an annoying error popup which says "No Session for PID". How can I get rid of it?

EDIT: never mind, I forgot the pamname=login, now it works!
Posts: 30
Joined: Sun May 24, 2015 3:16 pm
by prich » Sat Dec 26, 2015 5:48 am
Hi,

Thanks for this posting xanthor, I basically now have this system working. There is however one problem. My Raspberry Pi has two users (pi and aaron). I maintain the machine as pi and my grandson uses it as aaron. This scheme appears to lock use to pi - is this correct? If so, can it be altered to cover 2 (or more) users.

Any assistance in this regard will be greatly appreciated.

Peter
Posts: 10
Joined: Wed Aug 15, 2012 7:01 am
by AndyD » Sat Dec 26, 2015 6:22 am
prich wrote:Hi,

Thanks for this posting xanthor, I basically now have this system working. There is however one problem. My Raspberry Pi has two users (pi and aaron). I maintain the machine as pi and my grandson uses it as aaron. This scheme appears to lock use to pi - is this correct? If so, can it be altered to cover 2 (or more) users.

Any assistance in this regard will be greatly appreciated.

Peter

Hi,

I think what you really want is the VNC Desktop to display the LightDM greeter, which will ask for username and password. I haven't done this myself, but I found a blog post Remote Desktop using VNC from 2013, that explains how to get lightdm to start VNC. If that works you won't need (or want) to start the vnc server as a service.
User avatar
Posts: 2316
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
by tony359 » Wed Jan 20, 2016 8:42 am
Thanks for this!!

Appreciated!
It's also probably going to help me setting up NTP under systemd!
Posts: 13
Joined: Mon Dec 21, 2015 9:59 pm
Location: Swindon, UK
by CleoQc » Sun Jan 31, 2016 1:14 am
Just wanted to add my "thank you" too!

I don't think I could have managed without this!
Sometimes I can do things that make sense
User avatar
Posts: 33
Joined: Thu Jan 03, 2013 11:49 pm
by just_some_guy » Mon Mar 28, 2016 5:51 pm
Thank you for this, it worked exactly as described.

I am illiterate in linux, to the point where I did not know how to make a file in the directory.

If anyone else this new happens by here is how I accomplished it:

For step: "3. Create a new file /etc/systemd/system/vncserver@.service with the following contents: " navigate to the directory 'system'
Code: Select all
cd /etc/systemd/system

and then open a text editor - I chose nano, which is included and already installed

Code: Select all
sudo nano vncserver@.service

then you can cut and paste the text into the editor.

'Ctrl-X' gets you out of edit mode, and it asks if you want to save. 'Y'. At the bottom of the nano window it will ask you for the file name to save, but with the above command, the name 'vncserver@.service' will already be filled in for you. Hit 'Enter' and you are done. You can check for it with a 'dir' command.

Editing for the cursor is about the same, from the home directory
Code: Select all
sudo nano .vnc/xstartup

will open the file, make the change shown, which is adding ' -cursor_name left_ptr' to the line that says 'xsetroot -solid grey'
' Ctrl-X' to exit edit mode, 'Y' to save, check to make sure the file being saved is ".vnc/xstartup" 'Enter' to exit nano, and you are done
Posts: 2
Joined: Wed Jan 07, 2015 2:12 am
by bris_pi_guy » Thu May 12, 2016 7:25 pm
Thank you for this post.

I am running the tightvnc server in ubuntu mate from https://ubuntu-mate.org/raspberry-pi/

If I use your systemd unit to start tightvnc, log in from a tightvnc client and then run mate terminal, it starts with the /bin/sh shell. I think this has something to do with systemd using exec() to start the tightvnc server.

If I start the tightvnc server from the command line in a /bin/bash shell, log in from a tightvnc client and then run mate terminal, it starts with the /bin/bash shell.

Can the tightvnc systemd unit be modified such that mate terminal starts with the /bin/bash shell?
Posts: 10
Joined: Thu May 12, 2016 7:04 pm
by pleriche » Thu May 12, 2016 8:30 pm
This works fine for me - thanks xanthor.

Just one little niggle: when I start LXterminal the current directory is / rather than /home/pi. I've compared /etc/profile, /etc/basr.bashrc, .profile, .bashrc and .vnc/xstartup with a Wheezy system which starts LXterminal in $HOME and can see no significant differences. Can anyone suggest what the problem is please and howto fix it?

Regards - Philip
Posts: 70
Joined: Mon Oct 14, 2013 8:44 am
by Dylan144GT » Sat May 14, 2016 11:32 am
Thank you xanthor, this worked very well for my!

I was having problems with the method described on Adafruit where an error would pop up every time I logged in. Your method has solved this! Much appreciated!

Thanks,

Dylan
Posts: 4
Joined: Mon Mar 07, 2016 10:08 pm
by bris_pi_guy » Tue Jul 12, 2016 7:38 pm
This seems to have done what I wanted. In /etc/systemd/system/vncserver@.service

replace
Code: Select all
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i


with
Code: Select all
ExecStart=/usr/bin/sudo /usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/sudo /usr/bin/vncserver -kill :%i
Posts: 10
Joined: Thu May 12, 2016 7:04 pm
by kwaknet02 » Wed Aug 17, 2016 12:46 pm
i cannot click on any applications on VNC viewer.. i followed all the instructions above .. im using retropie img .installed with jessie.. can anybody help me with this problem.. thanks in advance..Image
Posts: 2
Joined: Thu Sep 18, 2014 1:59 pm
by kwaknet02 » Thu Aug 18, 2016 2:50 pm
kwaknet02 wrote:i cannot click on any applications on VNC viewer.. i followed all the instructions above .. im using retropie img .installed with jessie.. can anybody help me with this problem.. thanks in advance..Image


nevermind. i've solved it already.. i just removed the log, pid, passwd at /home/pi/.vnc.. now it works perfectly :)
Posts: 2
Joined: Thu Sep 18, 2014 1:59 pm
by Diegored » Fri Dec 09, 2016 10:37 pm
Thank you, it works perfectly.

By the way, for mac you can use: cmd+K vnc://pi.local:5901 as a client.

Regards,
D
Posts: 2
Joined: Fri Dec 09, 2016 10:55 am
by aparadox » Thu Feb 16, 2017 8:50 pm
Thank you for this tutorial, worked great first time and makes my life easier!
Posts: 1
Joined: Thu Feb 16, 2017 8:47 pm
by pleriche » Sat Feb 18, 2017 6:30 pm
Having come back to this a good few months later I find that the answer is very simple: in .vnc/xsession insert:
Code: Select all
cd $HOME

anywhere before the last line. I don't know why this is necessary on Jessie but not on Wheezy.

pleriche wrote:This works fine for me - thanks xanthor.

Just one little niggle: when I start LXterminal the current directory is / rather than /home/pi. I've compared /etc/profile, /etc/basr.bashrc, .profile, .bashrc and .vnc/xstartup with a Wheezy system which starts LXterminal in $HOME and can see no significant differences. Can anyone suggest what the problem is please and howto fix it?

Regards - Philip
Posts: 70
Joined: Mon Oct 14, 2013 8:44 am