Wednesday, September 3, 2014

Initial setups to Raspberry Pi

There are tons of documentation about how to setup the Raspberry Pi in many different ways, so I decide to document what I have done to setup my Raspberry Pi, just in case I break it and have to rebuild it. In this setup the mouse is not needed and by the end of it you won't need the keyboard neither the network cable, so you'll have one USB port available (in the other is the WiFi Dongle) because you'll be accessing your Raspberry Pi from you Mac in different ways.

These settings are:
  1. Install Raspbian
  2. VNC Access
  3. Setup WiFi
  4. SSH Access
  5. Sharing Files and Share Screen to a Mac with AFP
  6. Sharing Files with Samba
  7. Access form iPad or iPhone
All these instructions were made with a Mac and to connect to the Raspberry PI from a Mac. The instructions for Windows or Linux are different, so check the source pages if you are not using Mac OS.

What do you need?

  • Raspberry Pi (This was tested with a Raspberry Pi Model B)
  • Mac (I'm using OS X 10.9.4)
  • Internet connection
  • Wi-Fi Dongle (I'm using an Edimax EW-7811Un)
  • Network cable
  • HDMI Cable
  • Keyboard
  • Mini SD Memory Card 8GB with SD Card Adapter
A Mouse is not required but if you need it I recommend to use a USB Hub to connect all the devices or a Logitech Wireless Mouse and Keyboard with a Unifying USB receiver (like this one). Just make sure to pair both devices to the Unifying USB receiver in other computer (Mac or Windows).

Install Raspbian

The easy way is to install Raspbian with NOOBS but you can also download the image and install it in the SD card.
  1. Download NOOBS from
  2. Format the SD card:
    1. Identify the disk ID with Utility Disk. Select the SD card and press Cmd+I, in the Disk Identifier there is a value diskN, where the number N is the disk ID. Or, using the command line with the diskutil command to get the value of N (get N from diskN not diskNsM):
    2. diskutil list
    3. Format the SD card with FAT 32 using Utility Disk. Select the SD card and click on Erase tab, select MS-DOS (FAT) in Format, write the disk name - I use RASBERRYPI - and click on the Erase button. Or, using the command line with the diskutil command replacing the N by the disk ID of your SD card:
    4. diskutil eraseDisk FAT32 RASPBERRYPI /dev/diskN
  3. Copy the zip file to the SD Card already formatted with FAT32:
  4. cd /Volumes/RASPBERRYPI/
    cp ~/Downloads/ .
  5. Unzip the NOOBS file from the SD Card:
  6. unzip
    diskutil eject /dev/diskN
  7. Insert the SD Card to the Raspberry Pi, hook up everything (keyboard, network cable, HDMI and power supply). A mouse is not required.
  8. Select Raspbian
  9. Press l and select English (US)
  10. Press i to install it and wait.

The install image way is:
  1. Download the Raspbian image from and unzip the downloaded file.
  2. wget -O
  3. Format the SD card as explained above.
  4. Unmount the SD Card with Disk Util by selecting the disk and click on Unmount at the top. Or, using the command diskutil:
  5. diskutil unmountDisk /dev/diskN
  6. Install the image with dd command replacing N by the disk ID of the SD card and the name of the img file.
  7. sudo dd bs=1m if=raspbian.img of=/dev/diskN
  8. Insert the SD card, hook up keyboard, network cable, HDMI and the power supply. You will not need a mouse.

Once Raspbian is installed and boot the Raspberry Pi Software Configuration Tool (raspi-config) will start:
  1. Select option #1 Expand Filesystem to ensure the SD card is being used properly.
  2. Do not change password of the pi user now, the default is raspberry.
  3. Select what to see when the Raspberry Pi boot: the Desktop, Scratch or CLI. I choose CLI then I'll change it to Desktop.
  4. (Optional) Inside Internationalisation Options change the local to en_US.UTF-8 UTF-8 and unselect en_GB.UTF-8 UTF-8
  5. (Optional) Inside Internationalisation Options change the Timezone
  6. (Optional) Enable Camera if you have a one like this one.
  7. Enable SSH inside Advanced Options.
  8. Finish and Reboot. When the Raspberry Pi boot you'll see the login prompt or the Desktop (as you choose in step #3). The user is pi and the password is raspberry if you didn't change it.
  9. Once you are back into the console upgrade the system with
  10. sudo apt-get update
    sudo apt-get -y upgrade
  11. Install any package you need. For example vim and chromium
  12. sudo apt-get install -y vim chromium
  13. Finally, restart the Raspberry Pi
  14. sudo shutdown -r now

VNC Access

If you enable SSH with raspi-config (step #7 in configuration subsection) you can ssh to the server if you are in the same network. Log in locally to the server to identify the IP address:
ifconfig eth0
From your Mac open the Terminal and type: ssh pi@IP_ADDRESS, in my case is:
ssh pi@

Install and setup the VNC Server
sudo apt-get install -y tightvncserver
Start the X server (Desktop) and check it is running correctly. This command will prompt you to enter a Password and an optional View Only Password (I choose not to set a View Only Password)
startx &
You will see a line with something like this: raspberrypi:1
In your Mac open Finder and press Ctrl+K or go to the menu: Go -> Connect to Server. In the new windows enter:
Notice the last 2 digits are for the port you get when you started the VNC server, in this case it was 1 (raspberrypi:1) so you need to connect to the VNC server in port 5901 (vnc://

Create the VNC service
You do not want to login through SSH, start the X Server and the VNC server every time you need to access the Graphic Interface. So, it is time to create a service that will start the X Server and the VNC Server when the server boot.

Type the raspi-config command:
sudo raspi-config
Go to Enable Boot to Desktop/Scratch and select the second option Desktop Log in as user pi at the graphical desktop. Go to Finish and then Reboot.
Login again to the server through SSH or open the Terminal in the Desktop. Create a new file named /etc/init.d/vncserver:
sudo vim /etc/init.d/vncserver
(If you do not have vim yet, install it with: sudo apt-get install -y vim )
To type this:
# Provides: vncserver
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $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.


export USER HOME

case "$1" in
  echo "Starting VNC Server"
  su $USER -c "/usr/bin/vncserver :1 -geometry 1920x1080 -depth 24 -dpi 96"

  echo "Stopping VNC Server"
  su $USER -c "/usr/bin/vncserver -kill :1"

  echo "Usage: $0 {start|stop}"
  exit 1

exit 0
And execute the following commands to add it as a service:
sudo chmod 755 /etc/init.d/vncserver
sudo update-rc.d vncserver defaults
Reboot and now, as you did before, connect from your Mac to the Raspberry Pi server using VNC protocol. Open Finder and press Ctrl+K or go to the menu: Go -> Connect to Server. In the new window enter:
Do not save the URL yet, you'll do it in the next session.

Setup WiFi

We have access to the Raspberry Pi and it have access to Internet through a network cable but lets get rid off that cable and setup the WiFi. Get the Wi-Fi Dongle (I have an Edimax EW-7811Un), hook it up to the USB port and start up the Raspberry Pi. We'll continue with the network cable in the Raspberry Pi during this process, then you can unplug it and never use it again. There are 2 ways to setup WiFi: Using the GUI or the Command Line.

Setup WiFi using the GUI
Using the GUI to setup WiFi is easier and (in my case) works at first time. For this you need access to the Raspberry Desktop remotely with VNC (see below how to setup VNC access).

You can do it also locally but if so you will need a mouse and we do not have more USB port for a mouse, so you need a USB Hub or a Logitech Wireless Combo (Mouse and Keybpard) with a Unifying USB receiver. If this is your case then start the X server with:

Open WiFi Config application, it is located in the Desktop. Click on the Scan button and double click in the network you want to connect to. A new window will appear, keep the defaults unless you are sure something is wrong. Enter the password in the PSK field and click on the Add button.

The WiFi network should be ready, to confirm and know the IP address enter this command:
ifconfig wlan0
If there is an IP address, copy it and now you can remove the network cable and restart the Raspberry Pi.

Setup WiFi using the GUI without a mouse
In case you are logged in the Raspberry Pi locally and do not have a mouse it is possible to do it but is not as simple as explained below. Start up the X server as before with the startx command.
Open the WiFi Config program by pressing Cmd+r (Cmd is the key with the Windows icon), in the new opened window type: wpa_gui
In the WPA GUI application you can use the tab, space, enter and arrows keys to move around.
NOTE: I could not find a way to do a double click with the keyboard. If you know how, please, let me know.
Go to the tab "Managed Networks", then go to the "Add" button and press space. In the new window enter the name of the SSID (previously you can go to Scan, press Esc to exit this screen without saving), the Authentication method, encryption and PSK (password). Go to the "Add" button and press space.

To exit press Cmd+r again and type sudo shutdown -r now to reboot the Raspberry Pi. When it boot the WiFi network should be working, if not, try to find a way to simulate a mouse double click with the keyboard and follow steps in previous sub-session.

Setup WiFi using the Command Line
This way is a more difficult than the GUI way but it may be needed in case there is no VNC or X Server available.

Once you are in logge in (remotely with SSH or locally), save a copy of the file /etc/network/interfaces and open it with nano, vi or vim
sudo cp /etc/network/interfaces /etc/network/
sudo vim /etc/network/interfaces
to have these lines in the file:
auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
If at the end of the process the WiFi is not working, replace the highlighted manual to dhcp, and the roam to conf.
Check the available WiFi networks and copy the SSID you want to connect:
iwlist wlan0 scan | grep SSID
Save a copy and open the file /etc/wpa_supplicant/wpa_supplicant.conf with nano, vi or vim:
sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/
sudo vim /etc/wpa_supplicant/wpa_supplicant.conf
to add these lines:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

I'm not sure if it is important, but the lines inside the network block start with a tab character. Depending of your WiFi network configuration you may need to change some values such as proto (could be RSN or WPA), key_mgmt (could be WPA-PSK or WPA-EAP) and pairwise (could be CCMP or TKIP). You can identify these values with the command:
iwlist wlan0 scan
Restart the network service and force to get a new IP if you are log in locally or shutdown the Raspberry Pi if you are log in remotely with SSH:
sudo /etc/init.d/networking stop
sudo /etc/init.d/networking start
sudo dhclient

sudo shutdown -r now
Check you have an IP address for wlan0:
ifconfig wlan0
Copy the IP address, you will need it later.

Setup WiFi using the wpa_cli command
Other way, instead of modifying files, is to execute the wpa_cli command but I have not tested enough this way. The commands to execute in wpa_cli are:
Selected interface 'wlan0'

Interactive mode

> scan
> scan_results
bssid / frequency / signal level / flags / ssid
07:b1:23:a6:83:55   2412    62  [WPA2-PSK-CCMP][WPS][ESS]   MY_NETWORK_SSID
> add_network
> set_network 1 ssid "MY_NETWORK_SSID"
> set_network 1 psk "MY_NETWORK_PASSWORD"
The next output should be the client trying to connect. Once connected will try to get an IP address, if not working then force it to get one with:
sudo dhclient
Again, check you have an IP address for wlan0:
ifconfig wlan0

SSH Access

Static IP Address
The IP is assigned by DHCP and eventually the IP could be different. So, it is better to assign an static IP address.
Open the file /etc/network/interfaces again
sudo vim /etc/network/interfaces
to change this line iface wlan0 inet manual into iface wlan0 inet static. If in case of manual you have dhcp, replace it to static too.
In the line having wpa_roam, replace it to wpa_conf. And add these lines below the line iface wlan0 inet static:
address IP_ADDRESS
Identify the router IP address with:
netstat -r -n
The second column named Gateway will tell us the gateway ROUTER_IP_ADDRESS.
And identify the current IP address with the command:
ifconfig wlan0
This will give you the value of IP_ADDRESS which is IP the DHCP server assign to the Raspberry Pi, you can use that (recommended) or you can use other in the same network.
In my case, the file /etc/network/interfaces have the values:
Summarizing, the file /etc/network/interfaces should look like:
auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet static
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Using hostname instead of IP
The hostname of the Raspberry Pi is raspberrypi but you can confirm that with the command:
Instead of use the IP address of the Raspberry Pi every time we want to connect to it lets use the hostname. Open in your mac the file /etc/hosts and add this line to the end:    raspberrypi
The first value is the Static IP address of the Raspberry Pi and the second value is the hostname of the Raspberry Pi. Both values are separated by a tabulator character (press tab key).

Get rid of the password
Now lets setup the ssh access to not enter the password every time we connect with ssh from our Mac. Generate a private and public key if you don't have any. If you have the files ~/.ssh/id_rsa and ~/.ssh/, then you have them. If not generate them with the command:
Press enter when ask for a passphrase (no passphrase)
In the Raspberry Pi create the .ssh directory in the home of pi:
mkdir ~/.ssh
chmod 700 ~/.ssh
Now send the public key of your Mac to the Raspberry Pi executing this in the Terminal of your Mac:
cat ~/.ssh/ | ssh pi@raspberrypi 'cat >> ~/.ssh/authorized_keys'
Change the pi password
This step is optional, you can do it if you want nobody enter to your Raspberry Pi. Also, besides sharing SSH, you can share the Screen and Folders.
To change the pi password type:
You need to enter the current password (raspberry) then the new one, twice. In case the password is not accepted (too weak) you can force it with:
sudo passwd pi
But, this is not recommended, the goal of this is to have a strong password.

Creating shortcuts
Lets get more lazy and create a function named rpi to do some commands like ssh to the Raspberry Pi, ping the Raspberry Pi to know if it is alive or not, and others we may want.
Open the .bashrc file in your home directory of your Mac:
rpi() {
  [[ "${1}" == "ssh" ]] && ssh pi@raspberrypi
  [[ "${1}" == "ping" ]] && ping raspberrypi
  [[ "${1}" == "hostname" ]] && echo "raspberrypi"
  [[ "${1}" == "ip" ]] && ping -q -c 1 -t 1 raspberrypi | grep PING | sed 's/.*(\(.*\)).*/\1/'
Close the file and reload it
. .bashrc
or close and open your session. Now if you type:
rpi ssh
You will login to the Raspberry Pi without enter the password.
Or, typing:
rpi ping
Will ping the Raspberry Pi to know if it is alive.
Create as many options as you want inside the rpi function.

More alias, in the Raspberry Pi
Log in to the Raspberry Pi and open the file ~/.bashrc. There are some alias that are comment out. Feel free to uncomment or add new alias or functions. In my case I uncomment:
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'
Close the file and reload the .bashrc file or close and open the SSH session.

In the VNC Session section you had to open a VNC session with Ctrl+K and enter the VNC URL. Now use this URL:
And save it pressing the button with the plus next to the URL. Next time you don't have to type it.

Improve performance by eliminating one X session
When you start the Raspberry Pi in the monitor will start a X session (you will see the Desktop), with number 0, and with the VNC server there is other X session with number 1.
Unless you need to work with the first X session, number 0, you can disable this one. So, when you start your Raspberry Pi what you will see in the monitor will be the prompt requesting the user to log in. And, if you need eventually the Desktop in the monitor, you can always start it with the command: startx
To disable the first X session type the raspi-config command:
sudo raspi-config
Go to Enable Boot to Desktop/Scratch and select the first option Console Text console, requiring login (default). Go to Finish and then Reboot.
When the Raspberry Pi boot, you won't see the Desktop, just the login request, but you can continue using the Graphic Interface with VNC and later (continue reading) with Share Screen of Mac.

Sharing Files and Share Screen to a Mac with AFP

We have VNC but if we are using a Mac it is better to use AFP because is built-in in OSX. Lets start by installing nettalk:
sudo apt-get install -y netatalk
When it finish you should see raspberrypi in the Finder side bar but it allows you only to share files in the pi home directory.
Now lets install the open source version of Bonjour with:
sudo apt-get install -y avahi-daemon
sudo update-rc.d avahi-daemon defaults
sudo vim /etc/avahi/services/afpd.service
The last command is to open the file /etc/avahi/services/afpd.service and then copy-paste this:
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
  <name replace-wildcards="yes">%h</name>
Now restart the service:
sudo /etc/init.d/avahi-daemon restart
Again, you should see in the Finder side bar the Raspberry Pi and in the right side the option to Connect and Share Screen.
Now you can use your favorite text editor (Sublime Text, Text Mate, ...) in your Mac to edit files in the Raspberry Pi. And, you can open the Raspberry Pi screen easily to open graphical programs.

Sharing other directories
By default just the pi home directory will be shared (or the home of the user you use connect to the Raspberry Pi). In case you want to add other directory open the file /etc/netatalk/AppleVolumes.default
sudo vim /etc/netatalk/AppleVolumes.default
Go to the end of the file and add as many directories you want after the line to share the "Home Directory":
# By default all users have access to their home directories.
~/                      "Home Directory"
/var/www                "WWW"
# End of File

Restart the avahi-daemon and in Finder, connect and disconnect form the raspberrypi.
sudo /etc/init.d/avahi-daemon restart
In this example I'm sharing /var/www because I installed Apache2:
sudo apt-get install -y apache2
Modify the index.html file and view it from your Mac with:
In case you want to share only files and not the screen, replace these 2 lines of the file /etc/avahi/services/afpd.service:
To these lines:
You can also save the AFP URL to connect to the shared home directory of user pi. In Finder press Ctrl+K and in the windows type:
Now save the URL pressing the button with the plus sign. Next time you won't have to type the URL.

Sharing Files with Samba

Why share with Samba if I'm already sharing with AFP?
AFP is just for OSX, if you want to share a folder in your network for any computer (Windows or Linux) then Samba is the solution. You can hook up an external hard drive and use it as a NAS.
First, install samba and create a Samba user:
sudo apt-get install -y samba samba-common-bin
sudo smbpasswd -a pi
Now open the smb.conf file
sudo apt-get install -y apache2
to change:
  • (Optional) Windows Workgroup:
  • Change the WORKGROUP to the Windows Workgroup you have in your network. Check it in your Windows computer.
    workgroup = WORKGROUP
  • Enable Authentication:
  • Find this line and remove the #.
    #   security = user
  • Share a directory:
  • Go to the end of the file to the Share Definitions to add these lines
       path = /path/to/the/share/folder
       comment = Comment about this folder
       valid users = pi
       writeable = yes
       browseable = yes
       create mask = 0777
       public = yes
    Replace NAME to the name to be displayed from other computers, /path/to/the/share/folder to the path of the directory to share, and Comment about this folder to a description about this share directory. The other settings may change according to what you want to share. In case this is a NAS, NAME could be NAS, the path is /media/NAS and the comment could be: Raspberry Pi NAS
    Finally, restart Samba:
    sudo /etc/init.d/samba restart
    and you can connect to the Raspberry Pi with Samba, just open Finder, press Ctrl+K and enter the URL:

    Access form iPad or iPhone

    Now that you have access to the Raspberry Pi through SSH and VNC it is just a matter to have a SSH and/or VNC client for your iPad or iPhone. You can choose the one you prefer or can afford, in my case I use: vSSH (this is just for SSH) and Remote VNC (I think the free version is just for VNC, the paid version allow any kind of protocol, including SSH)
    Depending of your application, add the server entering the server name, IP address (do not use raspberrypi, this is a name just for your Mac), port (5901), user (pi) and password. Remember to have the iPad or iPhone in the same network as the Raspberry Pi.


    Post a Comment