Lab 4.1: Getting started with Raspberry Pi

This lab is aimed at helping you get up and running with Raspberry Pi. We’ll only cover the very basics of Raspberry Pi. You’ll learn how to install an operating system image to an SD card, how to boot Raspberry Pi from the SD card, and how to configure Raspberry Pi for remote access. In the remaining labs, we’ll try to cover relatively advanced topics.

Things you’ll need:

Please note that you will need to use the monitor, keyboard, and mouse available in the computer lab (disconnect the monitor, keyboard, and mouse from an idle workstation and connect them to your Raspberry Pi. Remember to plug them back once you’re done with the lab) . You’ll need the following items for this lab:

  • Raspberry Pi (Model B).
  • SD Card (8 GB).
  • Power Source. Any micro USB cable or Android phone charger will do.
  • Monitor with HDMI.
  • USB Keyboard and Mouse.
  • Ethernet cable

 

Install operating system image to SD card:

Several operating systems and development environments are available for the Raspberry Pi. We’ll be installing the Raspbian “wheezy” Debian Linux distribution for Raspberry Pi. A Raspbian image is a file that you can download onto an SD card which in turn can be used to boot your Raspberry Pi. Using a Raspbian image is the easiest way for a new user to get started with Raspbian.  Follow the steps given below to install the image to the SD card. You will need a laptop with an SD card reader to install the image.

  1. Download the RASPBIAN image from here and extract it to a directory.
  2. Run sudo fdisk -l to see all devices whether mounted or unmounted.
  3. Insert the SD card into the card reader, then connect the reader to your computer.
  4. Run sudo fdisk -l again. The new device that has appeared is your SD card. The left column gives the device name of your SD card; it will be listed as something like /dev/mmcblk0p1 or /dev/sd1. Note that the SD card can show up more than once in the output if you have previously written a Raspberry Pi image to this SD card. This is because the Raspberry Pi SD images have more than one partition.
  5. Now that you’ve noted what the device name is, you need to unmount it so that files can’t be read or written to the SD card while you are copying over the SD image. First run df -h and check whether the SD card is mounted or not. If the name of SD card appears in the output of df, then it is mounted and you need to unmount it. Run umount /dev/mmcblk0p1, replacing mmcblk0p1 with whatever your SD card’s device name is. If your SD card shows up more than once in the output of fdisk due to having multiple partitions on the SD card, you should unmount all of these partitions.
  6. In the terminal, write the image to the card with the command below, making sure you replace the input file if= argument with the path to your .img file, and the /dev/mmcblk0 in the output file of= argument with the right device name. This is very important, as you will lose all data on the hard drive if you provide the wrong device name. Make sure the device name is the name of the whole SD card, not just a partition of it; for example mmcblk0, not mmcblk0p1. The last part (p1) is the partition number but you want to write to the whole SD card, not just one partition. Therefore you need to remove that part from the name.
  7. The dd command does not give any information of its progress and so may appear to have frozen; it could take more than five minutes to finish writing to the card. Once the writing has finished, run the following command to ensure the write cache is flushed and that it is safe to unmount your SD card.

    You can now remove the SD card.

 

Booting Raspberry Pi

Now that we have installed the operating system image to the SD card, we can use it to boot the Raspberry Pi as follows:

  1. Remove the SD card from your laptop and insert into the Raspberry Pi.
  2. Next, plug in your USB keyboard and Mouse into the USB slots on the Raspberry Pi.
  3. Connect your HDMI cable from your Raspberry Pi to your monitor. If necessary change the monitor to HDMI mode.
  4. Plug in an Ethernet cable into the Ethernet port next to the USB ports.
  5. Finally plug in the micro usb power supply. This action will turn on and boot your Raspberry Pi.

You should see the Pi kick into life. Once the boot process has completed, you’ll get to a login prompt. Use the following username and password to login to Raspberry Pi: Username: pi Password: raspberry When you see the command prompt enter the following to launch the desktop:

You now have a fully working desktop environment. You need to register your Raspberry Pi through Masdar Device Registration Portal in order to access internet. Open the Raspberry Pi default web browser called Midori by clikcing on its icon on Raspbian desktop. Try to open some website and you’ll be directed to Masdar device registration portal. Type your username and password to login and register your Raspberry Pi. Now your Raspberry Pi is fully functional. Play around with the desktop environment for some time before moving on to the next section.

The Raspberry Pi Config Tool

The Raspberry Pi Configuration Tool is a very useful tool pre-packaged with the Raspbian “wheezy” distro. You’ll probably see it during installation; but you can launching manually with the following command:

It is recommended to run the following:

  • Expand Filesystem: This will expand the file system to the full size of your SD card; allowing you to use the full capacity for programs etc.
  • Internationalization Options => Change Timezone: Change the timezone so you have the right time.

Reboot Raspberry Pi for changes to take effect.

Configuring Raspberry Pi for Remote Access

Now we learn to remotely access the Raspberry Pi because what’s the point in having a computer smaller than your TV remote if you need to connect a massive Keyboard and monitor? For remote access with command line interface, we can simply use SSH, but first we need to find the IP address of Raspberry Pi. Run the following command:

You should see something like:

The field highlighted in red shows the IP address of Raspberry Pi. Now we can remotely access the Raspberry Pi by entering the following command from a terminal on your laptop, replacing the IP address with your actual IP address. When prompted, enter the password (remember that the dafault password is raspberry):

However, if we want remote GUI access, we need to install something like VNC. We’re going to use Tight VNC (server on the Raspberry Pi and viewer on your laptop). First of all install the Tight VNC Server on Raspberry Pi:

When installation is complete start the server:

You’ll be asked to create a password, enter one and confirm. You can use raspberry as your password for ease of use. When asked to create a view only password, say No. Every time you start VNC you’ll see something like:

Note the :1. This is the desktop session created. You can add more by running VNC again. Install the VNC viewer on your laptop. To connect run the VNC Viewer and enter the IP address of your Raspberry Pi followed by :1

Where :1 matches the desktop session. When prompted enter the Password used when installing VNC Server and you have gained GUI access to your Raspberry Pi.

It is highly desirable to start VNC Server automatically on bootup. There’s no point in having VNC if you have to login to Raspberry Pi every time and start the server manually. So let’s start it on bootup before the login prompt. In your Raspberry Pi, create a new file in the init.d directory:

Copy the following code to the file:

Give the script executable permission:

We can now start or stop the service:

To make Tight VNC Server start every time the Raspberry Pi starts up, execute the following:

Now just reboot the Raspberry Pi and make sure it’s connected to the network. You can now connect remotely using the VNC Viewer!

Passwordless SSH access

It is possible to configure your Pi to allow your computer to access it without providing a password each time you try to connect. To do this you need to generate an SSH key

First, check whether there are already keys on your computer (the one you’re connecting from):

If you see files named id_rsa.pub or id_dsa.pub you have keys set up already, so you can skip the generating keys step (or delete these files with rm id* and make new keys).

To generate new SSH keys enter the following command. Replace key-name with your name or any other valid string of your choice.

Upon entering this command, you’ll be asked where to save the key. We suggest you save it in the default location (/home/pi/.ssh/id_rsa) by just hitting Enter. You’ll also be asked to enter a passphrase. This is extra security which will make the key unusable without your passphrase, so if someone else copied your key, they could not impersonate you to gain access. If you choose to use a passphrase, type it here and press Enter, then type it again when prompted. Leave empty for no passphrase.

Now you should see the files id_rsa and id_rsa.pub in your .ssh directory in your home folder:

The id_rsa file is your private key. Keep this on your computer. The id_rsa.pub file is your public key. This is what you put on machines you want to connect to. When the machine you try to connect to matches up your public and private key, it will allow you to connect. Take a look at your public key to see what it looks like:

It should be in the form:

Now copy your public key to your Raspberry Pi, use the following command to append the public key to your authorized_keys file on the Pi, sending it over SSH:

Note that this time you will have to authenticate with your password.

Now try ssh <USER>@<IP-ADDRESS> and you should connect without a password prompt.

If you see a message “Agent admitted failure to sign using the key.” then add your RSA or DSA identities to the authentication agent, ssh-agent the execute the following command:

If this did not work, delete your keys with rm ~/.ssh/id* and follow the instructions again.

 

Accessing Raspberry Pi using its Host Name

If your RPi’s IP is likely to change frequently (say, for instance, you’re just powering it up every so often to play, and your network assigns IPs first-come first-served {like most home routers} ), it’s a good idea to set up a consistent network address for your Pi. The quick way to achieve this is to assign static IP to RPi but it is not reliable so we will use the more flexible way as given below. Note that the procedure given below only works on Linux. For Windows, additional configuration is required.

    1. Change RPi’s Host Name: The default hostname for the Raspberry Pi is “raspberrypi“, but you want a different hostname to avoid hostname conflicts on your local network. To change host name, login to your Raspberry Pi and execute the following command:

      Your hosts file will look like so:

      Leave all of the entries alone except for the very last entry labeled 127.0.1.1 with the hostname “raspberrypi“. This is the only line you want to edit. Replace “raspberrypi” with whatever hostname you desire (e.g. your Masdar user name). Press CTRL+X to close the editor; agree to overwrite the existing file and save it. Back at the terminal, type the following command to open the hostname file:

      This file only contains your current hostname:

      Replace the default “raspberrypi” with the same hostname you put in the previous step. Again, press CTRL+X to close the editor, agree to overwrite the existing file and save it. Finally, reboot the system to commit the changes:
    2. Install avahi with the following commands on the Pi:

      and then on older Debian installs:

      or on newer Raspbian installs:

      (if in doubt, you’re probably on the newer one).
    3. Create Config File: create a configfile for Avahi at /etc/avahi/services/multiple.service. I did this with the following command:

      The contents of this file should be something like the following

      Apply the new configuration with:

      The Pi should now be addressable from other machines as raspberrypi.local, for example:

Note: your computer can only connect to the raspberry pi using host name if they are on the same network be it WiFi or LAN.

Updating and Upgrading Debian Raspbian “wheezy” Linux Distribution

It is recommended to update the the package index files (essentially just a file pointing to the latest version of compatible software – for example VNC that we just installed). Make sure you have an internet connection and run the following command. It may take a few minutes.

Followed by:

It’s also good to make sure the whole Linux distribution is up-to-date. You can do this now, or later (it may take a while). Again make sure you have an internet connection and run:

You can run these commands every now and again to make sure you have the latest software.

Headless Setup (Optional)

You can also use raspberry pi device without a monitor and a keyboard (like a server). This is frequently called headless mode. You simply can connect your raspberry pi device to your laptop by an ethernet cable and mini-usb cable to power up the device.

First, set up a simple network using static IPs.

      • In your laptop:
      • Insert the sd card to your laptop card reader, mount  the card, and open a file named “<dir-to-mouted-sd-card>/cmdline.txt”
      • Append to the first line:
      • Run “sync”, remove the sd card and insert it back to the device.

We simply created a simple local network. Notice that you can use different private IP addresses as well. To check the network connectivity, run

If ping works properly, you can communicate in two ways between your PC and the Raspberry Pi. However, you can’t connect to the internet from your Raspberry pi. To do so, you need to configure your PC to act as a simple router. Namely, perform packet routing as well as NATing (tutorial).

References

  1. http://www.raspberrypi.org/help/quick-start-guide/
  2. www.neil-black.co.uk/raspberry-pi-beginners-guide
  3. http://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md
  4. http://elinux.org/RPi_Advanced_Setup