Setting Up VNC

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 and getting it back to "play on" isn't an option.

EDIT 25/04/2012: Ive now identified how to enable SSH automatically - see below (thanks Abishur!!)

EDIT 20/08/2012: I've worked out how to start VNC automatically on bootup - see my other post here http://myraspberrypiexperience.blogspot.co.uk/2012/08/start-vnc-automatically.html

Pre-requisites
As a bit of background, I'm using the latest Debian image (19/04/2012) and I'm connecting from my Windows 7 laptop.  To connect I've installed UltraVNC on my laptop - you can download a free copy from here:
UltraVNC 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, that can be a little difficult.  To get around this I've used the PuTTY SSH client, which you can download here:
PuTTY Downloads Page

The last thing you need to know is the IP Address.  If you've configured your Pi with a static IP Address, that's great.  If you have access to your router to look this up that's fine too.  You might however be in a situation where you have no way to look up the IP Address (like me) - for this I needed a utility to scan the network for all machines.  For this I used:
Advanced IP Scanner
This will scan your network listing all devices it can find.  Simply look through the list of devices and find your Rashberry Pi. It'll have a fairly obvious name, like "RaspberryPi".

Alternatively, 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.  As each wireless router differs, I'll leave this up to you to figure out :-)

Starting SSH on the Pi
First off - what is SSH?  It's a process that can be run in Linux that will allow you to access your Raspberry Pi remotely.  You'll be able to run anything that you would normally run from the command line on your Raspberry Pi, remotely from an SSH client - i.e. Putty.  #

I've found that the latest Debian image doesn't come with SSH enabled by default.  If you have a monitor and keyboard attached, once you've logged in you can start SSH by typing sudo /etc/init.d/ssh start as shown below:



Now, the idea of SSH is that we can connect remotely without a display, so you don't necessarily want to have to do this every time you boot up. 



EDIT 28/01/2013: There are two approaches to enabling SSH.


First option
For older images like Debian there is a file in place that can be used to do this - boot_enable_ssh.rc.  This file is on the boot partition of the SD card - i.e. the partition that can be read by Windows.  So - put the card into your PC card reader and open it up:


Now rename boot_enable_ssh.rc to boot.rc:



Once you've done this put the card back into the Pi and boot up - SSH will now be automatically enabled.



Second option
If you're using a newer distro like Raspbian Wheezy, when you first boot up the image you'll be taken to a config screen. From here you'll be able to enable SSH (along with a number of other useful options!).



Connecting via SSH
As mentioned above, to be able to connect into the Pi using VNC you first need to start the VNC server, and without a display connected you need to start this remotely.  SSH provides this mechanism.  To start with, we take our SSH Client - PuTTY.  Once you've downloaded it, simply launch it - it's a standalone exe - you'll be presented with this screen:

Simply type in Raspberry Pi IP Address in the Host Name field that you found earlier and click open.  Once Putty has connected to the SSH process running on the Pi, a terminal window will pop up and you'll be prompted with a login. 

I still have my UN & PW set to pi and raspberry, so just typed that in.  Once logged in I'm taken to the standard terminal prompt.  From here you can run pretty much any command you want on the Raspberry Pi.

Setting up VNC Server via SSH
Now that you're connected, you need to start the VNC server.  If this is the first time you're doing this, you'll also need to install VNC first.  I've assumed you'll have internet access as this will be required to install this.  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.

NOTE: during the install you'll be prompted to install without verification - type y here.

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

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 port 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 port.  You'll see more details on this below.  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.

You'll also be asked for a view-only password (optional).  When providing the password via VNC the remote use will only have read-only access to the Raspberry Pi:


Connecting via UltraVNC 
Once VNC server has been started the only thing left to do is connect.  Start up UltraVNC, type in the IP Address (or machine name) with a port of 1 and connect.  
NOTE:  This port is the port you used when starting the VNC process above:

And that's it - connected!


EDIT 06/05/2012: I've found out there are two potential desktops you might see, and this is determined by the user that launches vncserver (or startx for that matter).  Using sudo will give you the LXDE desktop above.  Launching it without sudo will give the intended Raspberry Pi desktop as shown below.

74 comments:

  1. Hi there. Starting Putty, I get "Network error. Connection refused." when at logon prompt in PI.

    Else after "startx" and then running VNC all work fine. Any idea what I am doing wrong?
    Regards

    ReplyDelete
  2. Update on my post, it may help newbies like me. After many different test etc, even changing firewalls I logged into the Pi itself, and looked at the filename. There I discovered the renamed file is "boot.rc.rc". Yep, using windows, I changed the name to "boot.rc", and windows must have added the extra ".rc" as per original file. Now all work as should. Thank you for your description of SSH.

    ReplyDelete
    Replies
    1. Ah yes - the wonderful "Hide known file extensions" setting in Windows - got to love it!

      Delete
  3. Could you add details about how to start vnc automatically upon boot up please.

    ReplyDelete
    Replies
    1. I've seen a few posts on the Raspberry Pi site about this, but haven't tried it out yet. I'll take a look and let you know once I figure it out.

      Delete
  4. There's no need to remove the SD from the Pi to get SSH on startup. Simply 'sudo mv /boot/boot_enable_ssh.rc /boot/boot.rc'. You can test that this is working as expected with a reboot 'sudo shutdown -r now'

    ReplyDelete
  5. Superb article (and blog as a whole), I can see I'm going to be mirroring you on my Pi :)

    As someone who likes to know why they are entering commands rather than just following instructions I found these links that explain the etc and etc/init.d folders quite well.

    etc: http://www.linuxjournal.com/article/8601
    etc/init.d: http://www.ghacks.net/2009/04/04/get-to-know-linux-the-etcinitd-directory/

    Also whilst browsing around this area I found an alternative to renaming /boot/boot_enable_ssh.rc to get the ssh server to startup automatically. Which was to use

    sudo update-rc.d sshd defaults

    I found this on http://www.ghacks.net/2009/11/04/starting-services-at-boot-in-linux/

    Again, as I like to know the why as well as the how, does anyone know which of these methods is preferable and why?

    ReplyDelete
  6. Thanks Dave - glad you like it. And thanks very much for these links - it's great to have options (and understanding WHY makes a big difference!). I don't really know which option might be preferable, but maybe someone will post the answer here for us :)

    ReplyDelete
  7. as a linux sysadmin , the sudo update-rc.d sshd defaults is the more preferable (and correct) way of enabling ssh (and most other services) at startup, as it will apply on most (if not all) linux/unix systems. the cp /boot/boot_enable_ssh.rc.....is very raspberry pi specific.

    cheers, and enjoy your bit of pi....

    ReplyDelete
    Replies
    1. Thanks for the clarification Matt - good to know!

      Delete
    2. Cheers for that information Matt :o)

      Delete
  8. Worked perfectly for me - thanks for this.

    ReplyDelete
  9. Thanks very much for the article.

    If you're interested in how to autorun this command at bootup, then I have go Headless & setting up a webserver guide on the RasPi.

    http://rix.uk.to/RasPi/RasPi2HeadlessWebserver.html

    ReplyDelete
  10. Hi,
    thanks for the tutorial. I followed it and I found that :1 it's not the port used by the server to connect to clients, but the DISPLAY to use.
    The port is specified by the switch -rfbport. This last is the port to forward.

    Please correct me if I'm wrong.

    Ciao!

    ReplyDelete
  11. Thank you so much. I had been trying to make this work for 2 days. It took 1/2 hour after reading this.

    Great work

    ReplyDelete
  12. One query on this (as a Linux newbie, apologies if I've missed something obvious or basic). As a regular user of Ultra VNC to connect to PCs remotely, I'm used to the remote screen duplicating the host screen, but although I am controlling the Pi over VNC, it looks as if I'm controlling a virtual screen, as nothing on the screen, keyboard or mouse connected directly to the Pi is following what I'm doing on the remote PC and vice versa. This causes problems when the Pi goes into screensaver mode and the CPU usage goes up to 100%, resulting in slow response on the VNC connection. Activity on the remote PC doesn't bring it out of screensaver mode.

    I'm guessing it's something to do with the way I've got VNC set up on the Pi, as I'm not familiar with launching VNC from the command line - something to do with the :1 display selection perhaps?

    Thanks

    Mike

    ReplyDelete
    Replies
    1. I've got the same issue and i was wondering if anyone had some insight?

      Delete
    2. Hi Brook

      Yes, you should be able to do this. Take a look at my post on how to start VNC automatically - http://myraspberrypiexperience.blogspot.co.uk/2012/08/start-vnc-automatically.html. This should start it when booting up and should use your main display.

      Delete
  13. I would just like to say thanks, for your instructions worked first time.

    A complete newbie to Linux and raspberry PI

    ReplyDelete
  14. Hi, I have just downloaded the Raspbian “wheezy” distro from the foundation site and looking at the boot partition from a linux box I realize I don't have the boot_enable_ssh.rc file there ...

    Would it be possible for you to share its content so I can create the file? I assume (hope? :)) it would work ... Thanks!

    Alejandro

    ReplyDelete
    Replies
    1. Sure. Create a file called boot.rc and add the following (note: it appear as a single line in notepad)

      # sourced from rc.local on Raspberry Pi
      #
      # Name this file as "boot.rc" and put it on the boot partition if you
      # want to run it.

      # echo "Checking ssh and enabling if absent"
      if ! ls /etc/rc5.d | grep "^S..ssh\$" >/dev/null; then
      insserv ssh
      service ssh start
      fi

      Delete
    2. Sorry, I should add that this is from the Debian image, but hopefully it'll work for Raspbian

      Delete
    3. Thanks!
      Works like a charm... Following your tutorial made me have my raspi on screen very fast. Thanks for sharing!

      As a side note for those using a linux box like myself, after having the "boot.rc" file in place, I've used the ssh shell ("ssh @") and "vinegar" as VNC client (both standard in ubuntu) to follow your post

      Great work!
      Alejandro

      Delete
  15. Great job!!! it really works for me, thank you, guy.

    ReplyDelete
  16. I just got my RPi a week ago and using the 2012-07015-wheezy image, I get ssh loaded at boot up, so I was able to skip most of the first part of this tutorial.
    But the rest of it was very useful.
    As a result, I now have VNC working on both Windows installed from:
    http://www.tightvnc.com/download.php
    and Ubuntu 12.04 installed using:
    sudo apt-get install xtightvncviewer

    With Windows, you get both Server and Service programs, but the one you need to run is Viewer.

    ReplyDelete
  17. Hi, all, I wonder how can i let the raspbian's vnc service automatically started~

    ReplyDelete
    Replies
    1. Hi Milton

      Sorry for the delay - yes, you can configure VNC to start up fairly easily. Take a look here:
      http://myraspberrypiexperience.blogspot.co.uk/2012/08/start-vnc-automatically.html

      Delete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Really detailed tutorial.
    great job!

    ReplyDelete
  20. I got vnc server to work with this and some other articles, but I would like for it to start in the same X session as the auto-login connected to a monitor.

    I want this to remote control a rpi connected to a tv on our office wall, so that anyone can at any moment remote control what is being shown on the wall, not a new invisible session!

    Any tips on how to do that?

    ReplyDelete
    Replies
    1. I had the same issue as you - it looks like tightvnc doesn't allow you to do this. I think I found that x11vnc worked quite well, and allowed you to connect to the exact session that was running on your TV. When you started x11vnc you could specify a session, and you could connect to that session by specifying a port when connecting using VNC. I can't quite remember the exact details, but I'll see if I can dig them up for you.

      Delete
    2. Were you able to dig up those details?

      Delete
    3. Hi Rick, sorry for the delay in replying - I had a look, but couldn't find anything I've used before. I keep meaning to look into this to see if I can figure it out, but my pi has been spending more time on the shelf than powered on unfortunately!

      Delete
  21. worked like a charm
    the only problem i had was user error I kept on inserting the wrong ip for the server 5 insted of 105 :D

    ReplyDelete
  22. To change desktops simply connect to the relevant port.

    Desktop 0 is port 5900
    Desktop 1 is port 5901
    Desktop 2 is port 5902

    Each port will only be open if the relevant desktop is set to a vnc connection. In most examples / cases itll be port 5901 that you need to connect to.

    ReplyDelete
  23. Hi I just got my Pi and I'll like to know if I can do this with wi-fi?

    ReplyDelete
    Replies
    1. Yes, there isn't any reason you shouldn't be able to. You'll need to make sure you get wireless working by itself first - once you have that working, you should be able to implement VNC as normal

      Delete
  24. I have tried several times to install tightvnc. It installs fine but when I connect to it, I get the following error: Xsession: unable to start X session..no home/pi/.xsession file. No session manager, no window managers, and no terminal emulators found.

    ReplyDelete
    Replies
    1. Sorry, I haven't come across this issue before. Try search on the Raspberry Pi forums - they're very helpful there!

      Delete
    2. Did you find a solution to this? I have the same issue.

      Delete
  25. hi, good tut, all is working good :)
    just a question: is it possible to redirect the vnc connection to lightdm to choose the sessoin instead of jumping directly in the dm? thanks

    ReplyDelete
    Replies
    1. Sorry, I haven't tried anything like this before

      Delete
  26. Hello,

    I'm sorry if it is too obvious, but is there a way to connect to raspberry not from the same network? meaning connecting the pi to the internet and from a distance computer log in to the raspberry.

    Daniel.

    ReplyDelete
    Replies
    1. Hey Daniel,

      You'll need to setup port forwarding on your home router.

      There are loads of great tutorials if you search for your router model number and port forwarding!

      Merry Christmas

      Delete
    2. Hey Matthew,

      Thanks for the answer, were trying to connect the raspbbery via 3G, it will use the cell phone as a router, so ill need to port forwarding the router meaning the cellphone?
      Have you done anything like that?

      Thank you!

      Happy new year!

      Delete
  27. Hi,

    Great page, lots of useful info here any it all worked a dream thanks.

    I have my pi plugged into my TV and wanted to use my laptop as a remote control - I assume when I connect to port 5901 I am connecting to a separate X instance which is why I cannot see my mouse control on the TV. Is there a way to connect to the same instance as the HDMI uses/views?

    Thanks,

    Danny

    ReplyDelete
    Replies
    1. Hi Danny. I know what you're referring to - it's something I struggled with initially. Try connecting on port 5900 - from what I recall that's the initial X instance displayed on the TV, whereas the next X instances (not visible) would be 5901, 5902, etc.

      Delete
    2. Hi Tass,

      Thanks for te reply - I've connected on [:0] which is desktop 0 which from what I can tell is port 5900, unfortunately I still have the same issue...

      Thanks,

      Danny

      Danny

      Delete
    3. Hi,

      I cracked it - I was running an instance of vncserver aswell which was listening also. I plugged a keyboard and mouse into the pi, started x11 and when I tried vnc again it connected again it connected tithe same view as the hdmi.

      Thanks for the help.

      Danny

      Delete
  28. Hi Tass,

    Although its been a while since you had a post I would like to first of all thank you for this super tutorial. Everything went fine and I can enjoy my RPi without extra monitors.

    I have a problem thought. Does anybody managed to access the VNC outside local network?

    I've done all the port forwarding but still I cannot access it. Are there any limitations that we can lift? Any settings to change in TightVNC server?

    Thanks
    Vagelis

    ReplyDelete
    Replies
    1. Hi Vangelis

      I haven't really tried this as so far I haven't had a need to access it remotely. Definitely something I see being useful. Going to sort out a new wireless card (current one has issues) - will see what I can find out.

      Delete
    2. Might be worth having a poke around here - seems relevant:

      http://www.raspberrypi.org/phpBB3/viewtopic.php?p=249505#p249505

      Delete
  29. Getting invalid resoulution error..

    ReplyDelete
    Replies
    1. Hi Shadaab

      What resolutions have you started tightvnc with and have you tried any other resolutions?

      Delete
  30. Hi, I have just downloaded the Raspbian “wheezy” distro from the foundation site and looking at the boot partition from a linux box I realize I don't have the boot_enable_ssh.rc file there .Would it be possible for you to share its content so I can create the file?
    Regard's,
    Ben Lines,
    Cliclk on my link nitrotek.nl

    ReplyDelete
    Replies
    1. Hi Ben

      I don't have the details to hand, but in the latest version of Wheezy there is actually a bootup screen that allows you to configure this. When you first boot up the image you'll be presented with a menu containing roughly 10 items, one of which will be to enable SSH. There are several other useful options available, including expanding your partition to fill your SD card, modifying the GPU/CPU memory split, setting the default boot behavior (boot into X) and checking for updates. This should let you do what you want, but if you still want the details let me know and I'll dig them out tonight.

      Tass

      Delete
  31. Thanks. I am new to RPi, and this helped me get connected from my Windows 7 box.

    ReplyDelete
  32. Its stunning work....thanks for sharing keep it up..!! Plastic cards

    ReplyDelete
  33. Great page Informative Blog Thanks for sharing..
    Discount card Printing

    ReplyDelete
  34. I could do it in a few seconds, thanks to you.

    ReplyDelete
  35. When I run sudo apt-get install tightvncserver it says it cannot connect. I tried running the url on alaptop and it says the age is unavailbale is there a mirror I could use?

    ReplyDelete
  36. I have MOST of this working, but the client on my Windows 8 PC keeps asking for a VNC password, but the Pi VNC server did not ask for one, so I appear to be stuck - blank does not wor. I tried shutting down the Pi server & re-starting it, but that did not prompt for a password. Can anybody tell me what I am doing wrong please?

    ReplyDelete
  37. I have no idea why this did not work. I did uninstall & re-install TightVNCServer, to no avail, THEN, I discovered 'VNCPASSWD' - Eureka.

    ReplyDelete
  38. This comment has been removed by the author.

    ReplyDelete
  39. Hi Guys,
    Great post and I have my Pi VNC server running.
    One thing that tricked me is the :1 port thing.
    When I look in the start up log /home/pi/.vnc/raspberrypi:1.log
    I notice my port number for connections is 5901
    So my remote host address in Tightvnc Client is
    10.1.1.1::5901 not 10.1.1.1::1 as was my first guess.
    Otherwise I was getting the follwoing message
    "no connection could be made because the target machine actively refused it"

    ReplyDelete
  40. Great post, your method word well on my PC. try free keylogger here

    ReplyDelete
  41. This comment has been removed by the author.

    ReplyDelete
  42. just find a good video converter to help to do so, such as aiseesoft video converter for mac is such a tooll which I have been using for a long time for convert mts to mov for editing and replay. just have a try at: http://www.ivideotool.com/ . good luck!!

    ReplyDelete
  43. Nice and cool stuff.
    Informative Blog !!

    Thanks for Sharing

    Linksys Router Technical Support

    ReplyDelete