Sunday 9 February 2014

How to VNC into a Headless RaspberryPi

Much has been written about using VNC with a headless RaspberryPi, but I seem to struggle with some of the suggested methods.


I also generally prefer to use the VNC server "x11vnc", as I like to view the same session as one open by a local user.


So here are my notes...


Configure RaspberryPi


Install x11vnc, either using Synaptic or simply from a terminal like this:-

sudo apt-get install x11vnc

Create an auto-launcher by writing a text file with these lines:-

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=x11VNC
Comment=
Exec=x11vnc -forever -display :0

Icon=/usr/share/pixmaps/gksu-root-terminal.png
StartupNotify=false
Terminal=false


...and save the file as x11VNC.desktop in this location: /home/pi/.config/autostart/

Note: the file name extension ".desktop" is important, otherwise this launcher will not appear in "Desktop Session Settings" list.  In a rather Windows Explorer like fashion, the LXDE file manager (pcManFM) does not show the .desktop extension, but if you use "ls" in terminal you can confirm that the file name & extension are correct.

Reboot the Pi and then go to:-

 start menu > Preferences > Desktop Session Settings

...and enable x11VNC

Note: I had a frustrating issue on one RaspberryPi where the x11VNC option did not show up in Desktop Session Settings list. This turned out to be because I had a backup of x11VNC.desktop file which was auto created by my editor (in this case gEdit). It seems that with both x11VNC.desktop and x11VNC.desktop~ files in the autostart folder, LXSession was getting confused. Deleting the backup (~) file seems to fix it.

So now, booting the Pi will automatically start the x11vnc server.

The VNC Client


To VNC into the RaspberryPi from my Lubuntu laptop I use vncviewer by typing into a terminal:-

vncviewer 192.168.0.31


...where the IP number is obviously the one allocated to the Pi.

Image Issues


Great, but there are a couple of problems. First of all, the VNC image on my laptop is a bit small. I'd quite like it to fill the screen on my laptop.

Secondly, it only shows a small part of the RaspberryPi desktop. There is something terribly wrong with the resolution.


The image size can be increased by editing the VNC launcher on the RaspberryPi. As the resolution on my laptop is 1366 x 768, I need to specify this with the "geometry" option.

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=x11VNC
Comment=
Exec=x11vnc -forever -geometry 1366x768 -display :0
icon=/usr/share/pixmaps/gksu-root-terminal.png
StartupNotify=false
Terminal=false

For the second problem, the RaspberryPi /boot/config.txt file must be edited (as root) to include:-

hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=16 


Now the VNC image is more useful...


...and the image can be switched to full-screen from a pop-up menu by pressing F8, so that the local task-bar is hidden by the remote (RaspberryPi) task-bar.

8 comments:

  1. Hi Steve,
    Thanks for the blog about using the RPI remotely. I have it working within my internal network great. Now I would like to access my pi from outside so I need to set up my router for port forwarding. Does x11vnc use a particular port? Thanks!

    ReplyDelete
    Replies
    1. The default port for vnc is 5900. If this port is already in use, vnc will increment until it finds one free.

      On a Linux box you can type: man x11vnc ...but this can be difficult to read as there are a fantastic number of command options.

      Take a look at this excellent site: http://www.karlrunge.com/x11vnc/

      You will probably want to use Secure SHell (as discussed on that site) when using vnc across the net.

      Good luck.

      Delete
    2. Thanks for the great information! I changed my port forwarding to 5900 and I installed http://www.realvnc.com/download/viewer/ on my windows machines and on my Android phone and can gain access to my pi anywhere.

      Delete
  2. my x11vnc shows up in desktop session settings, and is ticked, but won't start up on boot. It will work if I manually run it though. Any ideas?

    ReplyDelete
    Replies
    1. How do you manually run it? Can you run it from the x11VNC.desktop file in the autostart folder?

      Delete
  3. Hi, whenever I connect to my pi through vncviewer, I am not able to use my keyboard plugged directly to my pi anymore. Do you know why this happens?

    ReplyDelete
  4. Do these instructions work for Raspberry Pi 4? Given the date of the article, it was written before the Pi 4 was released.

    On the Pi4 I don't see the Desktop Settings menu.

    ReplyDelete
  5. Yes it was written a long time ago...but if the "Desktop Session Settings" menu is not available it is because it has not been installed for your version of the operation system. It may have been replaced by some other session app, so you need to check your menu for similar applications or ask a question on the Pi forum.

    You are basically looking for some way to autostart a program whos launcher has been added to the autostart folder. Good luck.

    ReplyDelete