domingo, fevereiro 10, 2013

Project 2 - Raspberry Pi - Setting Up a Headless X Windows Control from a Windows machine

As I stated in my last post, while the Raspberry Pi does provide a superb picture over HDMI, there are a lot of scenarios where one might not need a display, and connecting remotely via VNC will suffice.  For instance, any programming could be done remotely, while still having access to the physical hardware.  It's also particularly useful when the TV is in use by the kids (as in my case) and getting it back to "play on" isn't an option.

As before I'm connecting from my Windows 8 laptop.  To connect to the Pi I've installed TightVNC on my laptop - you can download a free copy from here: TightVNC Downloads Page

Now, once  the VNC server is up and running on the Pi you can VNC from your laptop into your Pi without doing anything on the Pi first.  If you've just turned it on however, you'll need to first start the VNC server on the Pi.

And if you're using this as a truly headless box as I am (I don't care about connecting a keyboard, mouse and HDMI cable to the R-Pi), that can be a little difficult.  To get around this I've used again the PuTTY SSH client, as I did in my last post, which you can download here: PuTTY Downloads Page

The last thing you need to know is the Pi IP Address.

If you have access to your wireless router, you might be able to connect to this and see a list of all devices that have connected to it, including your Raspberry Pi. On the image on the left, it's easy to get the Pi IP (, which I'll be using forthwith.

The configuration of the SSH server on the Pi is straightforward (see my last post).

Once I'm connected to the Pi, I needed to start the VNC server.  If this is the first time you're doing this, you'll also need to install VNC first.  To do this, run the following command:

sudo apt-get install tightvncserver

As mentioned above, the Putty SSH Client lets you run commands on your Pi directly, so by doing this you're installing a VNC application on your Pi.

Once installed, you'll need to start the VNC server.  The command I've used is vncserver :1 -geometry 1280x800 -depth 16 -pixelformat rgb565. It also works with just the plain vncserver

This will start the VNC server process on the Pi.  What this means is that it will start a virtual X session (a virtual instance of the Raspberry Pi GUI) on your Pi, in a very similar way to when you run startx when your Pi is connected to a display.  When you VNC into your Pi you will be connecting to this X session.

So - the important port of this command is :1.  This defines the logical port display that the VNC process will be started on - you can make this whatever you want, but will need to remember it to ensure you connect to the right logical port display. As for the resolution (-geometry) this will be the resolution that the X session will run under - you can make this pretty much whatever you want.

The first time you launch VNCServer you'll be prompted for a password - you'll need this to connect in using VNC. Once VNC server has been started the only thing left to do is connect.  Start up TightVNC, type in the IP Address (or machine name) with a port of 1 and connect (this port is the logical port display I've used when starting the VNC process above):

We let magic work and voilá, the X Windows Session through the TightVNC:

NB: The procedure I've just described here has to be repeated every time I boot the Pi, which is not ideal. The ideal approach is to add the start of the TightVNC server to the init.d, so that the process starts before the login. For this I need to delve into some basic scripting.

First open a Terminal session on the Pi, or connect using SSH. A new terminal or SSH session will automatically start you off in your home directory of /home/pi. If you are not in this directory, change to it by typing:
  1. $ cd /home/pi
Then cd to the .config directory by typing:
  1. $ cd .config
Note the '.' at the start of the folder name. This makes it a hidden folder that will not show up when you type 'ls'.

Issue the command below to create a new directory indide .config called 'autostart'.
  1. $ mkdir autostart
cd into that new directory by typing:
  1. $ cd autostart
All that remains is to edit a new configuration file. So type the following command to open the nano editor on the new file:
  1. $ nano tightvnc.desktop
Edit the contents of the file with the following text.
  1. [Desktop Entry]
  2. Type=Application
  3. Name=TightVNC
  4. Exec=vncserver :1
  5. StartupNotify=false
Type ctrl-X and then Y to save the changes to the file.

That's all there is to it. The next time you reboot, the VNC server will restart automatically.

On the next post I'll configure the Wi-Fi dongle, which is a must-have, if I want to have total mobility around the house.

Happy Hacking: You don’t learn to hack – you hack to learn...


5 comentários:

Anónimo disse...

A minha chegou agora. Como primeiro teste ligar à TV ou fazer remote shell? Ligar à TV parece-me mais linear.


Manuel Antão disse...

Depende. Se o objectivo for usar a Pi como Media Server, tens de ligar à TV. Se quiseres usá-la como file server, web server, etc, basta utilizá-la como dispositivo remoto (remote shell, vnc, ssh, etc).

Como correu?


Anónimo disse...

Manuel, ich habe ein nerviges Problem nach erfogreicher Installation des VNC-Servers. Mein kleinen Raspberry Pi habe ich in Sachen Ländereinstellungen Deutsch beigebracht. Per ssh und Konsole kann er auch Deutsch. Gehe ich aber über VNC rein (UltraVNC auf Win7), spricht er englisch mit mir.

Was ist passiert?


Manuel Antão disse...

Hau mal in das script die Spracheinstellung mit rein.

# User, unter dem der VNC-Server ausgefuehrt wird
export VNC_USER

# Set HOME of User
export HOME

# Set Language
export LANG

Das sollte dir dann auch über VNC eine deutsche Oberfläche liefern.

Anónimo disse...

Vielen Dank Manuel. Das war der richtige Hinweis.