Setting up VNC on Raspberry Pi for Mac access

I wanted to set up VNC on my Raspberry Pi so I could use it via a remote desktop on my MacBook Air. There are lots of instructions scattered in various places around the web, but now I’ve put together a comprehensive set of things to do, I thought it might be useful to share it in one place.

Set up file sharing and autodiscovery

[Source: Getting Started With Raspberry Pi]

On the Pi:

sudo apt-get install netatalk

Now from the Mac, open Finder, and hit ⌘K. Enter afp://192.168.0.22 (using the IP address of your Pi).

On the Pi:

sudo apt-get install avahi-daemon
sudo update-rc.d avahi-daemon defaults

Now create a file /etc/avahi/services/afpd.service (as root):

sudo vim /etc/avahi/services/afpd.service

and add this content:

<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
   <name replace-wildcards="yes">%h</name>
   <service>
      <type>_afpovertcp._tcp</type>
      <port>548</port>
   </service>
</service-group>

Then run this command:

sudo /etc/init.d/avahi-daemon restart

You should now be able to see the Pi in the Finder sidebar.

Setting up vncserver

[Source: Raspberry Pi forum]

On the Pi:

sudo apt-get install tightvncserver
vncserver

Enter an eight character password.

Now do:

cd /etc/init.d
sudo vim tightvncserver

Add the following content to the file:

#!/bin/bash
# /etc/init.d/tightvncserver
#

# Carry out specific functions when asked to by the system
case "$1" in
start)
    su pi -c '/usr/bin/vncserver -geometry 1440x900'
    echo "Starting VNC server "
    ;;
stop)
    pkill vncserver
    echo "VNC Server has been stopped (didn't double check though)"
    ;;
*)
    echo "Usage: /etc/init.d/blah {start|stop}"
    exit 1
    ;;
esac

exit 0

(Change the geometry setting if your monitor is a different size.)

Now do:

sudo chmod +x tightvncserver
sudo pkill Xtightvnc

Check the VNC server is not running:

ps aux | grep vnc

Then do:

sudo /etc/init.d/tightvncserver start
cd /etc/init.d
sudo update-rc.d tightvncserver defaults

Finding your VNC server using Bonjour

[Source: Getting Started With Raspberry Pi]

Create the avahi rfb service file:

sudo vim /etc/avahi/services/rfb.service

and add this content:

<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
  <name replace-wildcards="yes">%h</name>
  <service>
    <type>_rfb._tcp</type>
    <port>5901</port>
  </service>
</service-group>

Then run:

sudo /etc/init.d/avahi-daemon restart

Now you should be able to see the Screen Share option for the Pi in your Finder sidebar.

If you’re also running an X session directly on the Pi (i.e. not through VNC), consider disabling the screensaver. You might forget that this X session is running if you’re not looking at the monitor (or if the monitor is turned off), and you’ll wonder what’s eating all the CPU suddenly after ten minutes. (Yes, this happened to me!)

UPDATE:

Tom A commented below with an improved version of the startup script. Rather than leave it hidden in the comments, I decided to bring it up into the article. I haven’t tested the script, but I present it here for information.

Hi Pat,
Thanks so much for pulling this together. It saved me a lot of time getting this to work. In the spirit of adding to the solution, I can provide the following: I’ve tested this and found it to work on OSX 10.5.8, 10.6.8, 10.7.5, and 10.8.3. And I bashed your startup script against Andrew Berry’s generalized one to come up with the following version that does not pester you when you update-rc.d or perform various apt functions. This fixes what Ray was was asking about back in June of last year.

#!/bin/bash
### BEGIN INIT INFO
# Provides: tightvncserver
# Required-Start: $remotefs $syslog
# Required-Stop: $remotefs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start VNC server at boot time
# Description: Start VNC Server at boot time
### END INIT INFO

# The Username:Group that will run VNC
export USER=”pi”
#${RUNAS}

# The display that VNC will use
DISPLAY=”1″

# Color depth (between 8 and 32)
DEPTH=”16″

# The Desktop geometry to use.
#GEOMETRY=”x”
#GEOMETRY=”800×600″
#GEOMETRY=”1440×900″
#GEOMETRY=”1280×1024″
GEOMETRY=”1920×1080″

# The name that the VNC Desktop will have.
NAME=”my-vnc-server”

OPTIONS=”-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}”

. /lib/lsb/init-functions

case “$1″ in
start)
log_action_begin_msg “Starting vncserver for user ‘${USER}’ on localhost:${DISPLAY}”
su ${USER} -c “/usr/bin/vncserver ${OPTIONS}”
;;

stop)
log_action_begin_msg “Stoping vncserver for user ‘${USER}’ on localhost:${DISPLAY}”
su ${USER} -c “/usr/bin/vncserver -kill :${DISPLAY}”
;;

restart)
$0 stop
$0 start
;;
esac

exit 0

About Pat Galea

Engineer.
This entry was posted in Mac, Raspberry Pi. Bookmark the permalink.

56 Responses to Setting up VNC on Raspberry Pi for Mac access

  1. Ray Stuckey says:

    > cd /etc/init.d
    > sudo vim tightserver

    shouldn’t that be tightvncserver?

  2. Ray Stuckey says:

    I end up with what looks like an error:
    pi@raspberrypi:/etc/init.d$ sudo update-rc.d tightvncserver defaults
    update-rc.d: using dependency based boot sequencing
    insserv: warning: script ‘K01tightvncserver’ missing LSB tags and overrides
    insserv: warning: script ‘tightvncserver’ missing LSB tags and overrides

  3. Axel says:

    That last command should be
    >sudo /etc/init.d/avahi.deamon restart

    • Axel says:

      I mean
      >sudo /etc/init.d/avahi-daemon restart

      • Pat Galea says:

        Well spotted! I’ll fix it.

        (The original article has it wrong, but when I run the command myself I use tab completion, so I wouldn’t have noticed that it was wrong.)

        Thanks!

  4. Paul says:

    I keep getting an error when I click share screen in finder – “make sure that Screen Sharing is enabled on the computer to which you are attempting to connect”. I can access the folders within finder and it’s finding the RPi. Do you have any Idea why this may be happening?

    • Pat Galea says:

      The only thing I can think of is that the vncserver is not actually running on the Pi. Try doing “sudo /etc/init.d/tightvncserver start” again to see whether that helps.

    • Snos says:

      Check the value of your Pi’s VNC port. Is it 5901? 5900? That port number must match the /service-group/service/port node in your .service file:

      _rfb._tcp
      5900

  5. Phil Glau says:

    Awesome. Exactly what I was looking for.

    Got the same error as Ray Stuckey (but it still works)

    pi@raspberrypi /etc/init.d $ sudo update-rc.d tightvncserver defaults
    update-rc.d: using dependency based boot sequencing
    insserv: warning: script ‘tightvncserver’ missing LSB tags and overrides

    Also confirming typo at end should be:

    sudo /etc/init.d/avahi-daemon restart

  6. karansamel says:

    what version of OSX are you using as in 10.7, there is no Bonjour. Also i was able to install everything on my pi and was able to access it as a shared device on my mac, but couldn’t see any screen share option, even with it enabled in system preferences.

    • karansamel says:

      i was not able to figure out what was wrong but i was able to get Chicken of the VNC to work instead :P

    • Pat Galea says:

      I’m running 10.7.4.

    • bbuddy says:

      That is a 100% incorrect statement. OS X has had Bonjour since version 10.0 (may be 10.1). 10.7 has Bonjour. I don’t usually point out people’s errors but that is a ludicrous statement to make! Apple actively uses Bonjour for their own services, and has been for years and years.

  7. Christopher says:

    Hi Pat,

    Thanks for this, really clear instructions.
    I am still a bit stuck, I can get the PI to be on the left in the finder and I can browse it, but can seem to get the VNC bit to work. The installs all seem to go fine?
    Any suggestions?

    Thanks,

    Christopher

  8. Chase says:

    Thanks a lot! Everything working perfectly. No longer need a USB mouse or keyboard and sitting on the floor… I can run it all from my laptop.

  9. James says:

    Hi Pat,

    Thanks for these instructions – so much clearer than the other forums I’d found first. I wish I’d come straight here and saved myself some time!

    What I would like to achieve is to play a video on my TV from the Pi and control it from my laptop. How should I go about this?

    Thanks,

    James

  10. James says:

    Pat,

    Please ignore last I’ve got it to work.

    Thanks,

    James

  11. Keith says:

    Brillant instructions, worked first time. Many thanks indeed.

  12. mike says:

    Brilliant many thanks. Still don’t like vi – and vim was not on the wheezy pi I used. Had to execute “sudo /etc/init.d/tightvncserver start” to wake up the link. Great to get shot of the second keyboard – always typing on the wrong one…

    Mike

  13. Jaxx says:

    Pat,

    Thanks for this, really clear instructions. I am also a little stuck, I can get the PI to be on the left in the finder and I can browse it, but not sharing screens. Ive ran the “sudo /etc/init.d/tightvncserver start” but it didn’t fix the problem.
    Any suggestions?

    /Users/hausofwalker/Desktop/Screen Shot 2012-12-06 at 20.08.32.png

    • Pat Galea says:

      I don’t have any brilliant ideas. Maybe just run through the process again to ensure there aren’t any typos in the files, or something like that. Or maybe there’s a firewall problem on your Mac that’s blocking it, but that’s unlikely unless you’ve specifically gone in there to block it.

      • Stephen says:

        I had a similar problem and I fixed it by connecting to the pi using the specific port the VNC server was directing to. The log file of the vncserver instance in question will mention a TCP port it’s directing to. For instance, I type

        $ sudo /etc/init.d/tightvncserver start

        New ‘X’ desktop is raspberrypi:1

        Starting applications specified in /home/pi/.vnc/xstartup
        Log file is /home/pi/.vnc/raspberrypi:1.log

        Starting VNC server

        $ less /home/pi/.vnc/raspberrypi:1.log

        05/02/13 23:48:45 Xvnc version TightVNC-1.3.9
        05/02/13 23:48:45 Copyright (C) 2000-2007 TightVNC Group
        05/02/13 23:48:45 Copyright (C) 1999 AT&T Laboratories Cambridge
        05/02/13 23:48:45 All Rights Reserved.
        05/02/13 23:48:45 See http://www.tightvnc.com/ for information on TightVNC
        05/02/13 23:48:45 Desktop name ‘X’ (raspberrypi:1)
        05/02/13 23:48:45 Protocol versions supported: 3.3, 3.7, 3.8, 3.7t, 3.8t
        05/02/13 23:48:45 Listening for VNC connections on TCP port 5901
        Font directory ‘/usr/share/fonts/X11/75dpi/’ not found – ignoring
        Font directory ‘/usr/share/fonts/X11/100dpi/’ not found – ignoring
        xrdb: No such file or directory
        xrdb: can’t open file ‘/home/pi/.Xresources’

        So the target TCP port is 5901. Then, to connect using my macbook (running OS X 10.6), I open finder, hit Apple-K, and type vnc://134.173.xx.xxx:5901

        where the x’s are the actual IP address of my pi. Then I enter the password I set, ignore the warning message, and get my Pi’s screen!

        Hope this helps!

  14. Steve Ellis says:

    Good grief…. that was a marathon…. but it works…. absolutely brilliant… and I am a novice at Linux and Mac. The only parts that didn’t work were the VIM commands so I used File Manager and created the files that way…. Thank you for the time you must have put into this….

  15. ZeDctr says:

    Thank you for this great guide. I copy-and-pasted till the end … and it worked! Yes, i am quite new to the Unix-command line (after i switched to osx several years ago, even my little dos-skills almost vanished). Biggest hurdle was vim, used nano instead.
    Btw, is VNC for X only or can i somehow view the command line too? I have some applications, that need X not to run.

  16. Spencer says:

    Great stuff, good instructions and made me learn vim at the same time with vim tutor.

  17. tceleven says:

    Thanks for the instructions, they are great. Could you help with an error I’m getting when I start the screen sharing?

    When I start screen sharing nn error pops up saying “error: Xsession: unable to start X session..no home/pi/.xsession file. No session manager, no window managers, and no terminal emulators found.” . I an left with only grey screen.

    Thoughts?

    Thanks

  18. Brian Krogh Jensen says:

    Excellent guide!!

  19. Thorsten says:

    Thanks a million! That saved me a lot of time. Excellent guide.

  20. newpi says:

    sudo apt-get install avahi-daemon
    This installs 2GB of files, which will result in over 8GB once everything is built
    You should warn uses of this before starting. You could not do this on an SD card smaller than 16GB

  21. Thanks so much for posting this. Amazing instructions. Worked the first time!

  22. toffifee says:

    Thanks for the awesome post!
    Did you manage to configure the keyboard layout correctly? Special characters like \ [ ] etc don’t work for me.

  23. gerrycurry says:

    I just set this up and everything works perfectly with 10.8.3, EXCEPT, I need to manually restart VNC every time I restart my pi. Is there a way to have VNC automatically start on boot?

    • Pat Galea says:

      That’s strange. Did you create the /etc/init.d/tightvncserver script? That’s the one that makes it start up after booting.

  24. Tom A says:

    Hi Pat,
    Thanks so much for pulling this together. It saved me a lot of time getting this to work. In the spirit of adding to the solution, I can provide the following: I’ve tested this and found it to work on OSX 10.5.8, 10.6.8, 10.7.5, and 10.8.3. And I bashed your startup script against Andrew Berry’s generalized one to come up with the following version that does not pester you when you update-rc.d or perform various apt functions. This fixes what Ray was was asking about back in June of last year.

    #!/bin/bash
    ### BEGIN INIT INFO
    # Provides: tightvncserver
    # Required-Start: $remotefs $syslog
    # Required-Stop: $remotefs $syslog
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Start VNC server at boot time
    # Description: Start VNC Server at boot time
    ### END INIT INFO

    # The Username:Group that will run VNC
    export USER=”pi”
    #${RUNAS}

    # The display that VNC will use
    DISPLAY=”1″

    # Color depth (between 8 and 32)
    DEPTH=”16″

    # The Desktop geometry to use.
    #GEOMETRY=”x”
    #GEOMETRY=”800×600″
    #GEOMETRY=”1440×900″
    #GEOMETRY=”1280×1024″
    GEOMETRY=”1920×1080″

    # The name that the VNC Desktop will have.
    NAME=”my-vnc-server”

    OPTIONS=”-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}”

    . /lib/lsb/init-functions

    case “$1″ in
    start)
    log_action_begin_msg “Starting vncserver for user ‘${USER}’ on localhost:${DISPLAY}”
    su ${USER} -c “/usr/bin/vncserver ${OPTIONS}”
    ;;

    stop)
    log_action_begin_msg “Stoping vncserver for user ‘${USER}’ on localhost:${DISPLAY}”
    su ${USER} -c “/usr/bin/vncserver -kill :${DISPLAY}”
    ;;

    restart)
    $0 stop
    $0 start
    ;;
    esac

    exit 0

  25. Dominus says:

    Two things I noticed that might be of help
    1. When I copy/pasted the vim entries from here to my terminal (OS X) window stuff at the beginning got cut. So before I realized it all the xml were invalid and so was the tightvncserver file. Very annoying.

    2. If you want to make it work with Apple Remote Desktop you will need to enter username pi and the password you gave when first running vncserver.
    BUT it will fail to verify. Choose to add anyway and then edit the settings for this connection in Apple Remote Desktop and set the port to 5901 (or 590x with x for the tightvnc session you want to view – typically 1) see http://gettingstartedwithraspberrypi.tumblr.com/post/24142374137/setting-up-a-vnc-server.

  26. samuelmoss92 says:

    Hi thanks for the help.

    when i enter the code below it shows the following errors.

    pi@raspberrypi ~ $ sudo /etc/init.d/tightvncserver start
    /etc/init.d/tightvncserver: line 4: $’\302\240′: command not found
    /etc/init.d/tightvncserver: line 8: $’\302\240\302\240\302\240\302\240su’: command not found
    /etc/init.d/tightvncserver: line 9: $’\302\240\302\240\302\240\302\240echo’: command not found
    /etc/init.d/tightvncserver: line 10: $’\302\240\302\240\302\240\302\240′: command not found
    /etc/init.d/tightvncserver: line 20: $’\302\240′: command not found
    I’ve followed everything to the dot and i even have the screen sharing option but it asks me to activate it on the pi end.

    Any help is appreciated.

    • Pat Galea says:

      \302\240 represents a Unicode non-breaking space. You shouldn’t have those in your file! I’m not sure how you got them.

      If you copy the script from this page and paste it into your text editor, you shouldn’t get any Unicode spaces.

  27. Andreas Kopp says:

    Imstead of this –> sudo vim /etc/avahi/services/afpd.service for me it worked this –> sudo vi /etc/avahi/services/afpd.service

  28. Lukas Blakk says:

    Thanks for this awesome walk-through. Some notes from my experience (10.8.3):
    * Was able to see/connect to my pi in the finder sidebar right after installing netatalk without the afpd service
    * don’t change the name of the vnc server to “remote window” — I don’t know if it’s because of the lack of hyphen or the choice of words but once I rebooted with that name I got a very different screen over vnc and also a “GDBus.Error:org.freedesktop.PolicyKit1.Error.Failed: An authentication agent already exists for the given subject” and once I changed it back to a word-word title, all was well again.

  29. Peshawa says:

    Thank you for this clear walk through.

    It worked at first, but when I upgraded the raspberry pi using (sudo apt-get upgrade), the screen sharing bit no longer works. any suggestion of how to fix it? Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s