Raspberry Pi Networking Cookbook - Second Edition - Rick Golden - E-Book

Raspberry Pi Networking Cookbook - Second Edition E-Book

Rick Golden

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Connect your Raspberry Pi to the world with this essential collection of recipes for basic administration and common network services

About This Book

  • Install, administer, and maintain your Raspberry Pi
  • Explore a new world of computing with this low cost, credit-card sized computer
  • Connect your Raspberry Pi to other devices on local networks and utilise IoT services

Who This Book Is For

This book is intended for students, scientists, and hobbyists who wish to connect their Raspberry Pi to other devices on a local area network or to the Internet of Things. Whether you are new to the Raspberry Pi, or already have a lot of experience with it, the recipes in this book will be a valuable reference to you and inspire your next project. You will want to have this book handy as a guide whenever you are working on networking projects for the Raspberry Pi.

What You Will Learn

  • Install, update, and upgrade your Raspberry PI
  • Configure a firewall to protect your Raspberry Pi and other devices on your local area network
  • Set up file sharing, remote access, a web server, and your own wiki
  • Create a wireless access point and use it as an Internet gateway
  • Stream video, audio, and local device data to IoT services as well as your own websites
  • Control devices connected to the Raspberry Pi from your phone via the web
  • Create a giant video wall using multiple monitors and Raspberry Pis

In Detail

With increasing interest in Maker Projects and the Internet of Things (IoT), students, scientists, and hobbyists are using the Raspberry Pi as a reliable, inexpensive platform to connect local devices to Internet services.

This book begins with recipes that are essential to installing the Raspberry Pi and configuring it for network access. Then it continues with recipes on installing common networking services such as firewalls and file sharing.

The final chapters include recipes for network monitoring, streaming data from the Raspberry Pi to IoT services, and using clusters of Raspberry Pis to store and analyze large volumes of data.

Style and approach

This book contains a collection of practical, engaging recipes that will guide you through enhancing your Raspberry Pi's existing network.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 441

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Raspberry Pi Networking Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the color images of this book
Errata
Piracy
Questions
1. Installation and Setup
Introduction
Preparing for the initial boot
Getting ready
The basic components
Basic networking
Media centers
Desktop computers
Network hubs
Game consoles
The initial setup
How to do it...
How it works…
There's more…
Interfaces
On-board components
Recommended accessories
Power supply problems
Symptoms
Causes
Solutions
See also
Downloading new SD cards
How to do it…
How it works…
See also
Booting with NOOBS
Getting ready
How to do it...
How it works...
There's more...
See also
Mac OS X disk utilities – diskutil and dd
Getting ready
How to do it...
How it works...
There's more...
See also
Image Writer for Windows – Win32DiskImager.exe
Getting ready
How to do it...
How it works...
There's more...
See also
Convert and copy for Linux – dd
Getting ready
How to do it...
How it works...
There's more...
See also
Booting Raspbian Linux for the first time
Getting ready
How to do it...
How it works...
See also
Shutting down the Raspberry Pi
Getting ready
How to do it...
How it works...
There's more...
See also
2. Administration
Introduction
Executing commands with privileges (sudo)
Getting ready
How to do it...
How it works...
There's more...
Switch user (sudo –u)
Password
The sudo group
See also
Expanding the size of a filesystem (raspi-config)
Getting ready
How to do it...
How it works...
There's more...
See also
Configuring memory usage (raspi-config)
Getting ready
How to do it...
How it works...
There's more...
Configuring remote access (raspi-config)
Getting ready
How to do it...
How it works...
There's more...
See also
Obtaining remote access by using SSH
Getting ready
How to do it...
How it works...
Digital fingerprints
Hostname
There's more...
Finding out the IP address of the Raspberry Pi
Spoofing the secure fingerprint
Each new install creates new digital fingerprints
See also
Obtaining remote access by using PuTTY
Getting ready
How to do it...
How it works...
There's more...
See also
Changing the login password (passwd)
Getting ready
How to do it...
How it works...
See also
Adding a user (useradd)
Getting ready
How to do it...
How it works...
See also
Giving a user sudo privileges (id and usermod)
Getting ready
How to do it...
How it works...
See also
3. Maintenance
Introduction
Updating the operating system (apt-get)
Getting ready
How to do it...
How it works...
There's more…
See also
Upgrading Raspbian from wheezy to jessie using sources.list
Getting ready
How to do it...
How it works...
There's more…
The sources.list and sources.list.d
The sources.list file format
See also
Searching for software packages (apt-cache)
Getting ready
How to do it...
How it works...
There's more…
See also
Installing a package (apt-get)
Getting ready
How to do it...
How it works...
There's more…
See also
Package management (aptitude)
Getting ready
How to do it...
How it works…
There's more...
The command line interface
See also
Reading the built-in documentation (man)
Getting ready
How to do it...
How it works…
There's more...
See also
Reading the built-in documentation (info)
Getting ready
How to do it...
How it works…
There's more...
Coreutils – the most common Raspbian Linux utilities
Searching for info
See also
4. File Sharing
Introduction
Mounting USB disks (pmount)
Getting ready
How to do it...
How it works...
There's more...
Device files
Mount points
Unmounting disks
The plugdev group
Other mount commands
Disk performance
The Raspberry Pi's power is limited
See also
Accessing another computer's files (smbclient)
Getting ready...
How to do it...
How it works…
There's more...
help
Changing remote directories
Fetching a single file
/ versus \
See also
Sharing folders from other computers (mount.cifs)
Getting ready
How to do it...
How it works...
There's more...
Protected shares require a username and a password
Unmounting disks
See also
Auto-mounting USB disks at boot (/etc/fstab)
Getting ready
How to do it...
How it works...
There's more...
Error recovery
The fstab file format
See also
Auto-mounting a shared folder at boot
Getting ready
How to do it...
How it works...
See also
Creating a file server (Samba)
Getting ready
How to do it...
How it works...
There's more...
See also
Sharing an attached USB disk (Samba)
Getting ready
How to do it...
How it works...
There's more...
5. Advanced Networking
Introduction
Configuring a static IP address
Getting ready
How to do it...
How it works...
There's more...
See also
Creating a firewall with ufw
Getting ready
How to do it...
How it works...
There's more...
Don't forget to enable remote access
Disabling the firewall
Allowing a file server through the firewall
Allowing a web server through the firewall
Application rules
Resetting the firewall rules
See also
Remote access to a desktop session (xrdp)
Getting ready
How to do it...
How it works...
There's more...
No streaming video (or Minecraft)
It's not the display
See also
Remote access to the monitor (x11vnc)
Getting ready
How to do it...
How it works...
There's more...
No streaming video (or Minecraft)
See also
Installing a web server (Apache, lighttpd, Nginx)
Getting ready
How it works...
There's more...
Configuration files, static content, and dynamic pages
Other servers
lighttpd
Nginx
See also
Installing a wiki (mediawiki)
Getting ready
How to do it...
How it works...
See also
Creating a wireless access point with hostapd
Getting ready
How to do it...
How it works...
Check the wireless USB adapter
Configure the DHCP server by using udhcpd
Configure the Wireless Access Point server by using hostapd
Set up IP Forwarding
Configure the boot parameters
Start the wireless access point
There's more…
Not all USB wireless adapters support AP mode
Wireless firewall, file server, or web server
See also
Installing a network trace utility
Getting ready
How to do it...
How it works...
There's more...
See also
Installing a network protocol analyzer
Getting ready
How to do it...
How it works...
There's more...
Promiscuous mode
Terminal-based user interface
Capture filters
See also
Enabling the IPv6 network protocol
Getting ready
How to do it...
How it works...
There's more...
Privacy extensions
Use sysctl to change kernel parameters at runtime
See also
6. IoT – Internet of Things
Introduction
Easy access to hardware
Getting ready
How to do it...
How it works...
Connect the pushbutton switch to GPIO port 23
Connect the LED to GPIO port 24
Power on and log in
Navigating the Linux kernel with sysfs
Export GPIO ports 23 and 24
Configure each interface direction as input or output
Testing the input device
Testing the output device
Using the input device to activate the output device
Cleanup
See also
Installing the GrovePi
Getting ready
How to do it...
How it works...
Installing the GrovePi drivers and interfaces
Attach the GrovePi to the Raspberry Pi
Power on and log in
Install the Python API
Test the Grove pushbutton switch
Test the Grove LED
Pressing the pushbutton switch lights the LED
There's more…
See also
Controlling devices from a web page
Getting ready
How to do it...
How it works...
Create and run the ledpage website
Action URLs
Use Ctrl+C to quit
There's more…
Initialization
The request_handler class
The html_page function
The main loop
See also
Connecting to an IoT platform
Getting ready
How to do it...
How it works…
Register your Raspberry Pi with the IoT platform
Install the IoT platform API
Exchange signals with the IoT platform
There's more…
Initialization
The signal handler
The main loop
IoT Rules
See also
Creating an IoT gateway
Getting ready
How to do it...
How it works
Creating the bootable SD card
Configuring wireless networking
The ThingBox is ready
Running the example flow
Go with the flow
Creating the pushbutton LED flow
There's more…
There is a large library of available nodes
Wait 40 seconds before powering off or rebooting
See also
7. Clustering
Introduction
Installing a high-availability load balancer
Getting ready
How to do it...
How it works...
Setting up the web servers
Installing Apache2 on each web server
Creating unique test web pages for each web server
Testing the web servers
Setting up the load balancers
Install haproxy and keepalived on each load balancer
Configuring HAProxy for each load balancer
Enable listening on virtual IP addresses for both load balancers
Configuring Keepalived for both load balancers
Testing the cluster
Testing web server failure
Testing load balancer failure
Restoring normal operation
There's more…
Scaling horizontally by adding more web servers
Session cookies
See also
Installing a distributed filesystem
Getting ready
How to do it...
Installing the GlusterFS server on each Raspberry Pi
How it works...
Installing the GlusterFS server on each Raspberry Pi
Creating a striped replicated volume in the trusted storage pool
Mount the distributed striped replicated volume
Testing the striped replicated volume
Replication
Striping
Testing the high availability of the cluster
Testing the healing of replicated peers
There's more …
Using Keepalived to create a virtual filesystem endpoint
See also
Creating a supercomputer
Getting ready
How to do it...
How it works...
Setting up secure communication between master and slaves
Downloading the Apache Spark software distribution
Installing Apache Spark on each Raspberry Pi in the cluster
Configuring the Spark master
Calculating pi without using the Spark cluster
Calculating pi using the Spark cluster
There's more...
See Also
Index

Raspberry Pi Networking Cookbook Second Edition

Raspberry Pi Networking Cookbook Second Edition

Copyright © 2015 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: March 2013

Second edition: January 2016

Production reference: 1241215

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78528-021-4

www.packtpub.com

Credits

Author

Rick Golden

Reviewers

Stefan Pietzonke

Werner Ziegelwanger

Acquisition Editor

Vivek Anantharaman

Content Development Editor

Arshiya Ayaz Umer

Technical Editors

Mohit Hassija

Edwin Moses

Copy Editors

Vedangi Narvekar

Jonathan Todd

Project Coordinator

Shipra Chawhan

Proofreader

Safis Editing

Indexer

Priya Sane

Production Coordinator

Shantanu N. Zagade

Cover Work

Shantanu N. Zagade

About the Author

Rick Golden, in the summer of 1972, sat in the computer lab at SUNY Fredonia and completed his first CAI tutorial on programming in APL. He was 9 years old then.

He has been programming computers for over 40 years. He has designed and developed a multitude of projects, from low-level graphics and database drivers to large-volume e-commerce platforms.

At work, Rick is currently focused on developing software to improve healthcare by mining petabytes of healthcare claims to find opportunities to improve healthcare coordination. After work, Rick teaches 10-14 year olds how to program using Raspberry Pi computers.

I would like to thank my dad for giving me the opportunity to learn programming at such a young age, which was unheard of in the 1970s. His wisdom, patience, and faith in my programming abilities has encouraged me to become the man that I am today.

I would also like to thank my wife and family for supporting me during the many hours that I spent working on this book. Without their support, this book would not have been written.

Finally, I would like to thank Packt Publishing for giving me the opportunity to update this book and my editor, Arshiya Umer, for keeping me focused on my deadlines. Without her help and encouragement, this book would not have been published.

About the Reviewers

Stefan Pietzonke grew up in a village and wrote his first computer program at the age of 14. His first computer was an Amiga 500. He learned to program in Amiga BASIC. Now, he mostly writes programs in the C/C++ language.

He developed his own virtual machine called "Nano". The bytecode for the Nano VM can be created by using his assembler or compiler.

He also likes to conduct hardware projects. He built a robot from a computer mouse case. This robot is autonomous, drives around, and seeks light.

He has a blog (http://midnight-koder.net/wordpress/) about his software and hardware projects.

I want to thank my family for supporting me.

Werner Ziegelwanger studied game engineering and simulation and got his master's degree in 2011. His master's thesis was published with Terrain Rendering with Geometry Clipmaps for Games as the title. The publisher was Diplomica Verlag. His hobbies are programming, games, and all kinds of technical gadgets.

He worked as a self-employed programmer for some years and mainly did web projects. At that time, he started his own blog (http://developer-blog.net), which is about the Raspberry Pi, Linux, and open source.

Since 2013, Werner has worked as a Magento developer and the head of programming at mStage GmbH, an e-commerce company that is focused on Magento.

www.PacktPub.com

Support files, eBooks, discount offers, and more

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why Subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Free Access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.

Preface

A Raspberry Pi 2, with its 900MHz quad-core processor, has more processing power than a network server from the late-1990s. Created as an educational tool to inspire the next generation of programmers, the Raspberry Pi is also an excellent network server. It can be used to share files, host websites, create Internet access points, and analyze network traffic. Multiple Raspberry Pis can be clustered to create a single, highly available, and fault-tolerant super computer. This book shows you how.

The Raspberry Pi Foundation recognized that computers had become so expensive and arcane that programming experimentation on them had to be forbidden by parents. The parental restrictions on using computers had created a year-on-year decline in the numbers and skills levels of the A Level students applying to read Computer Science. So, the Foundation set out to create a computer that was "affordable, and powerful enough to provide excellent multimedia, a feature we felt would make the board desirable to kids who wouldn't initially be interested in a purely programming-oriented device".

2 million Raspberry Pis were sold in the first two years of its release, which was not limited to educators and school children. Hobbyists were also excited to use the inexpensive Linux-based computer in their projects. In February 2015, the quad-core Raspberry Pi 2 was released with significantly more power and memory than the original, which was more than enough memory and power for many typical server applications.

In this cookbook, you'll find a collection of server-side recipes for the Raspberry Pi, including recipes to set up file servers and web servers, create secure wireless access points, and analyze network traffic. There is even a recipe to create a highly available fault-tolerant supercomputer.

What this book covers

Chapter 1, Installation and Setup, has a number of beginner recipes to set up the Raspberry Pi as a network server, which include instructions on how to download and install new operating system images, boot for the first time, and the proper way to shut down the system.

Chapter 2, Administration, has more beginner recipes to configure the Raspberry Pi as a network server, which includes instructions on how to execute privileged commands, configure remote access, and manage user accounts.

Chapter 3, Maintenance, has intermediate and advanced recipes to maintain the Raspberry Pi server. You'll learn how to update software, read the built-in documentation, and upgrade the system.

Chapter 4, File Sharing, has a number of different intermediate recipes to share files.

Chapter 5, Advanced Networking, has a collection of advanced recipes to set up and monitor network applications, including a firewall, web server, wireless access point, and network protocol analyzer.

Chapter 6, IoT - The Internet of Things, has several intermediate recipes to connect your Raspberry Pi to the Internet of Things.

Chapter 7, Clustering, has advanced recipes to create a highly available fault-tolerant supercomputer from a cluster of Raspberry Pis.

What you need for this book

For most of the recipes in this book, you will need a little more than a basic Raspberry Pi setup (Raspberry Pi and a power supply) that is connected to a local area network.

You may choose to use a display, keyboard, and a mouse with the Raspberry Pi. However, most recipes have been written to remotely access the Raspberry Pi as a server.

Internet access is required to download software and connect to the Internet of Things.

Who this book is for

This book is for students, educators, hobbyists, and computer professionals who would like to use the Raspberry Pi as a network server.

Previous experience with the Raspberry Pi is not required. After completing the beginner and intermediate recipes in this book, you will gain the knowledge and experience that you will need to complete even the advanced recipes.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Use the chown command to give the user pi ownership of the directory (.) and all of the files in it (*)."

Any command-line input or output is written as follows:

pi@web1 /var/www/html $ sudo chown pi:www-data . *

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Press the Tab key to select Finish and then press the Enter key:"

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/Raspberry_Pi_Networking_Cookbook_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Installation and Setup

In this chapter, we will cover the following topics:

Preparing for the initial bootDownloading new SD cardsBooting with NOOBSMac OS X disk utilities – diskutil and ddImage Writer for Windows – Win32DiskImager.exeConvert and copy for Linux – ddBooting Raspbian Linux for the first timeShutting down the Raspberry Pi

Introduction

This chapter introduces the Raspberry Pi. It begins by listing the components that you will need, such as a power supply, in addition to the Raspberry Pi.

The core recipes of this chapter describe how to download, install, and configure a number of common Raspberry Pi operating systems.

The last two recipes describe the initial boot of the official Raspbian Linux distribution and how to safely power off the Raspberry Pi.

Once you've completed this chapter, you will have downloaded, installed, and configured an operating system for your Raspberry Pi and booted your Raspberry Pi for the first time.

Preparing for the initial boot

This recipe explains which components are needed for the initial boot, in addition to the Raspberry Pi, before it can be powered on for the first time.

The Raspberry Pi Foundation has released a number of versions of the Raspberry Pi since it was first released in June 2012, which include Raspberry Pi B (April 2012), Raspberry Pi A (February 2013), Raspberry Pi Compute Module (April 2014), Raspberry Pi Model B+ (July 2014), and the Raspberry Pi 2 Model B (February 2015).

The original Raspberry Pi Model B has a memory of only 512 MB, a single-core processor, and two USB ports. The current model, the Raspberry Pi 2 Model B, has a memory of 1 GB, a quad-core processor, and four USB ports.

Note

The examples in this book feature the Raspberry Pi 2 Model B.

The Raspberry Pi is shipped without a case and power supply. There is no keyboard or monitor. Depending on how you intend to use the Raspberry Pi, you will need additional components. For the majority of the recipes in this book, you will only need a power supply, an SD card, and a network cable.

You may wish to attach additional peripherals, depending on how you intend to use the Raspberry Pi. An HDMI cable, a USB keyboard, and a USB mouse are needed if you'd like to use the Raspberry Pi as you would use a desktop computer. This recipe lists a number of different Raspberry Pi projects and the peripherals needed to complete them.

After completing this recipe, you will be ready for the initial boot of your Raspberry Pi.

Getting ready

To get started with this recipe, there are a few prerequisites that you must be familiar with.

The basic components

These are some of the basic components:

The Raspberry PiAn SD cardA 5V Micro USB Power Supply

The Raspberry Pi draws its power from a 5V micro USB power supply and needs an SD card for its operating system. While no further components are required to boot the Raspberry Pi, many of the networking solutions in this book will require additional components.

A single 4 GB SD Class 10 card has more than enough room and speed to host the base operating system as well as many useful applications. Because the SD card is where the Raspberry Pi stores its operating system, the speed of the operating system is dependent on the speed of the SD card. Class 10 cards will have better performance than Class 4 or Class 6 cards. The Embedded Linux Wiki maintains an SD Card compatibility table, which can be viewed by visiting http://elinux.org/RPi_SD_cards.

In addition to an SD Card, the Raspberry Pi will need additional components for many applications. For most of the recipes in this book, you will need only a network connection. For some, you may also need a display as well as a keyboard and mouse.

The following are a few examples of networking applications and the components that they will need.

Basic networking

By basic networking, we mean having a network connection. For the simplest networking solutions, the only additional component that the Raspberry Pi needs is a network connection—either a direct TCP cable connection, or a wireless network USB dongle. Once the network is configured and remote logins to the Raspberry Pi are possible, the Raspberry Pi can be remotely accessed, updated, and administered.

Media centers

As part of media centers, we will require an HDMI television or monitor.

For the simplest network media solutions, in addition to the basic networking components, the only additional component that the Raspberry Pi needs is an HDMI connection. Both audio and video can be streamed through the Raspberry Pi's HDMI connection. Furthermore, there is enough room on an SD card to store a small collection of music and video files in addition to the operating system.

Desktop computers

If you are using desktop computers, these will be required:

An HMDI television or monitorA USB keyboardA USB mouse

The Raspberry Pi 2 has four USB ports, with enough power to support low-power devices, such as a USB keyboard or a USB mouse. With its quad-core processor, it is powerful enough to browse the Web, send e-mails, and edit documents or images. Because it runs the Linux operating system, the Raspberry Pi can also run hundreds of educational, scientific, and business programs. In short, the Raspberry Pi can run many useful open source desktop applications.

Network hubs

For network hubs, we will require these:

A powered USB hubA USB LAN adapterA USB WLAN adapter (a Wi-Fi dongle)A USB hard driveA USB printer

When using the Raspberry Pi as a firewall or wireless access point, an additional LAN or WLAN network adapter is required. If the network adapter is powered from the USB connection, an additional powered USB connector will be required for the adapter to operate reliably.

Game consoles

A powered USB hubUSB game controllers

The Raspberry Pi is an excellent gaming platform if you wish to create games, or play single-player console games or multi-player network games. Many of the older text-based games can be played on the Raspberry Pi with just a keyboard or via a remote login. However, USB game controllers can also be connected to the Raspberry Pi to further enrich the gameplay of multimedia action games.

The initial setup

The Raspberry PiAn SD cardA 5V Micro USB power supplyA network connectionAn HDMI TV or monitorA USB keyboardA USB mouse

A power supply, preformatted SD card, monitor, keyboard, and mouse are the bare minimum components that are needed for an initial setup. When connected with an HDMI to a television, the television will output audio as well as video.

How to do it...

Perform the following steps to boot the Raspberry Pi:

Download the latest disk image.Write the disk image to an SD card.Insert the formatted SD card into the Raspberry Pi.Attach a display to the HDMI connector.Attach a USB board and a USB mouse to the USB ports.Attach a 5V micro USB power supply to the Raspberry Pi, and it boots.Finally, shut down the Raspberry Pi.

How it works…

Before you can boot the Raspberry Pi, you'll need an SD card with a bootable disk image on it. The official Raspbian Linux image for the Raspberry Pi can be downloaded from http://www.raspberrypi.org/downloads.

Once the disk image has been downloaded, it needs to be written to an SD card (refer to the Setting up new SD cards recipe).

After the SD card has been prepared and inserted into the Raspberry Pi, the display, keyboard, and mouse can be connected to the Raspberry Pi. Then it is ready to be booted (refer to the Booting Raspbian Linux recipe).

Connect the power supply in the end! There is no on-off switch for the Raspberry Pi. When the power supply is connected, the Raspberry Pi immediately boots. Therefore, it is important to have all the cables connected and the SD card inserted before connecting the power supply.

When it is time to turn off the Raspberry Pi, the operating system must first be shut down, which is the opposite of booting (refer to the Shutting down the Raspberry Pi recipe).

There's more…

The Raspberry Pi 2 is a low-cost single-board computer (it costs only $35). It is sold bare bones and requires a power supply, a preformatted SD card to hold its operating system, a keyboard, and a display before it can do anything useful. However, it does have a number of standard I/O interfaces and on-board components that will enable it to connect to a large variety of devices.

Interfaces

The standard connectors and interfaces for the Raspberry Pi are as follows:

Power (5V at 800 mA (4.0 W)): The Raspberry Pi has a Micro USB power connector that should be connected directly to a power supply that is neither the USB port on a computer, nor a USB hub.SD card: The Raspberry Pi is designed to be booted from a preformatted SD card (4 GB or greater is recommended; Class 10 SD cards deliver the best performance).GPIO: This is used in analog and digital I/O connection for expansion and experimentation.Audio output (3.5 mm jack—stereo): The Raspberry Pi does not have an audio input connector. However, a USB microphone or sound card can be added. Audio output includes the I2S protocol to connect to digital audio devices.LEDs: These are disk, power, and network traffic indicators. When these LEDs are flashing, the Raspberry Pi is actively processing. After shutting down, wait until the LEDs stop flashing before unplugging the Raspberry Pi.USB 2.0 (four ports): There is limited power available on these ports. The devices connected to the Raspberry Pi via USB should either have their own power supply, or should be connected via a powered USB hub.Network (10/100 wired Ethernet RJ45): Be aware that the onboard networking competes for bandwidth with attached USB devices.HDMI (rev 1.3 and 1.4): This may be used for both video and audio output. Resolutions from 640x350 to 1920x1200, including the PAL and NTSC standards, are supported.

On-board components

The central on-board components for the Raspberry Pi are as follows:

SoC: This stands for System on Chip. The one we require is Broadcom BCM2836 media processor. Here are its features:
CPU: quad-core ARM Cortex-A7 at 900MHzGPU: 24 GFLOPS of compute powerMemory: 1GB SDRAM
LAN9512
10/100 MB Ethernet (Auto-MDIX)4x USB 2.0

Recommended accessories

In addition to a power supply, the following accessories are recommended:

A Case: This is a protective enclosure for the Raspberry Pi.A powered USB hub: This has its own power supply that is separate from that of the Raspberry Pi. This has enough power to support attached devices.

Power supply problems

It is difficult to say how much power is actually needed by the Raspberry Pi because the power needed varies depending on how busy the Raspberry Pi is and which peripherals are connected. However, problems related to an inadequate supply of power have been reported. These problems are reduced or eliminated when the power supply for the Raspberry Pi produces at least 800mA at 5V and the USB devices are connected indirectly through a powered USB Hub.

Symptoms

Here are some of the symptoms:

A rainbow square glows in the upper-right corner of the displayThe network connection is unreliableThe keyboard does not work after the Desktop GUI is startedIntermittent SD card errors occur

Causes

Here are the causes:

The power supply is rated less than 800mAA complex keyboard or a keyboard with a built-in USB hub, such as Apple Macintosh keyboardsA USB hard disk or an extra large thumb drive is attached directly to the Raspberry Pi instead of indirectly through a powered USB hub

Solutions

Here are the solutions:

Use a regulated power supply of at least 700mA at 5VOnly connect simple USB devices directly to the Raspberry PiConnect USB devices to a powered USB hub and only connect the hub directly to the Raspberry Pi

See also

Wikipedia—the Raspberry Pi (http://en.wikipedia.org/wiki/Raspberry_Pi): This Wikipedia article about the Raspberry Pi includes a comparison of all the Raspberry Pi models, detailed information about each Raspberry Pi component, and an extended history of the Raspberry Pi.The MagPi (http://www.raspberrypi.org/magpi): The MagPi is the official Raspberry Pi magazine. Monthly issues are available online.The Raspberry Pi website (http://www.raspberrypi.org): The official Raspberry Pi website contains history, news, and documentation for the Raspberry Pi as well as a quick start guide, a forum, a wiki, and a download area.R-Pi Hub— eLinux.org (http://elinux.org/R-Pi_Hub): The R-Pi Hub is an Embedded Linux community's wiki page for Raspberry Pi users. This wiki page has a buying guide, a beginner's guide, a list of verified peripherals, and a list of Raspberry Pi distributions that is larger than what's found on the official website. It has a wealth of well-organized, up-to-date information.The hardware history of the Raspberry Pi (http://elinux.org/RPi_HardwareHistory): The Embedded Linux community has chronicled the history of the Raspberry Pi, including detailed specs and images for each version.

Downloading new SD cards

The following recipes explain how to create bootable SD cards from downloaded disk images using Win32DiskImager.exe, dd, and diskutil.

The Raspberry Pi does not come with an operating system. Before the Raspberry Pi can boot, it needs an SD card with the operating system installed. Preinstalled SD cards are available for purchase. However, downloading and installing an operating system image is not difficult.

Once you've completed this recipe, you will know how to download a Raspberry Pi operating system. The following recipes will show you how to write it to an SD card.

How to do it…

Perform the following steps to write an image to the SD card:

Download a Raspberry Pi image.Write the image to an SD card.

How it works…

The easiest way to get started with the Raspberry Pi is to download the NOOBS (New Out Of Box Software) distribution from the Raspberry Pi Foundation website, which can be viewed by visiting http://www.raspberrypi.org/downloads. The files from this distribution can be copied directly to a formatted SD card. No additional disk utilities are required to create a bootable image (refer to the Booting with NOOBS recipe).

Included with NOOBS is the Raspberry Pi Foundation's recommended operating system distribution, Raspbian Linux. On the downloads page, you will also find a link to the Raspbian Linux disk image. There are links to a number of other third-party operating systems too. For more images for the Raspberry Pi, visit the Embedded Linux community's wiki page (http://elinux.org/RPi_Distributions).

Unlike NOOBS, once one of these individual operating system images is downloaded, you'll need to write it to an SD card using a special disk utility.

If you are using the Mac OS operating system, use diskutil and dd to write the operating system image to an SD card (refer to the Mac OS Disk Utilities recipe). If you are writing the SD card from a Windows computer, use Win32DiskImager.exe (refer to the Image Writer for Windows recipe). If you are using the Linux operating system to write the image to the SD card, use the dd command-line utility (refer to the Convert and Copy for Linux recipe).

See also

The Raspberry Pi website—downloads (http://www.raspberrypi.org/downloads): The Raspberry Pi website's download page is where you can find links to the recommended versions of Raspberry Pi to optimize operating system distributions. Currently, the Raspberry Pi Foundation has links to the following operating system distributions:
NOOBS and NOOBS LiteRaspbian (Jessie and Wheezy)Ubuntu Mate (a Linux desktop)Snappy Ubuntu Core (a developer distribution)Windows 10 IoT Core (a developer distribution)Open Source Media Center (OSMC)Open Embedded Linux Entertainment Center (OpenELEC)PINET (a classroom distribution)RISC OS (a non-Linux distribution)

The Raspbian Linux distribution is recommended by the Raspberry Pi Foundation. It also is the operating system distribution that is used throughout this book.

The Embedded Linux for the Raspberry Pi distributions (http://elinux.org/RPi_Distributions): The Embedded Linux community maintains an excellent wiki page on Raspberry Pi operating system distributions. The wiki page has a comparison table and links to downloadable image files. Many of these distributions are specialized for a specific use, such as penetration testing; use as a home theater, firewall, or an inexpensive desktop PC; or the development of software.Windows 10IoT—Downloads (https://ms-iot.github.io/content/Downloads.htm): Windows for IoT and the other tools that you will need to develop for Windows IoT devices such as the Raspberry Pi are available at this website.

As of this revision, the Window 10 IoT distribution does not have a user interface. It is labeled as Windows 10 IoT Core Insider Preview. The tools needed to interact with the IoT Core are available from the downloads page of Windows 10 IoT.

Booting with NOOBS

This recipe explains how to use the Raspberry Pi Foundation's NOOBS to install a Raspberry Pi operating system.

NOOBS is not an operating system distribution. It is a tool that is used to install operating systems. By using NOOBS, you can select an operating system for your Raspberry Pi.

This is the easiest way to get started with the Raspberry Pi. No special disk utilities are required. Therefore, this recipe works from any computer that has an SD card writer.

Once you've completed this recipe, you will be able to use NOOBS to select an operating system for your Raspberry Pi.

Getting ready

Ingredients:

A computer with an SD card writerAn initial setup for the Raspberry Pi (refer to the Preparing for the initial boot recipe)A formatted SD card—4 GB or greater (class 10 has the best speed)The NOOBS ZIP file

The installation of NOOBS on an SD card is not operating system-specific.

Download the NOOBS ZIP file (NOOBS_v1_4_2.zip) from the Raspberry Pi website (https://www.raspberrypi.org/downloads/noobs/).

How to do it...

The following steps are required to copy NOOBS to an SD card:

Insert the formatted SD card into the computer.Decompress the NOOBS ZIP file onto the SD card.Eject the SD card.Insert the SD card into the Raspberry Pi and power on the Raspberry Pi.Select and install an operating system.

How it works...

The NOOBS installation process is not operating system-specific. The files in the NOOBS ZIP file simply need to be expanded onto a newly formatted SD card.

Once the files have been expanded onto the SD card, the SD card can be safely ejected from the computer and inserted into the Raspberry Pi.

After the SD card is firmly inserted into the Raspberry Pi and all of its other components are connected (the HDMI monitor, network connection, USB keyboard, and USB mouse), you can connect the power supply and boot the Raspberry Pi. Ensure that the power supply is connected in the end. Otherwise, your Raspberry Pi will not boot properly.

When the Raspberry Pi is finished booting NOOBS, you will be presented with a choice of operating systems. Select Raspbian [RECOMMENDED] from the top of the list by pressing the Spacebar or clicking on it with the mouse. Click on Install (i) or press the I key to install the Raspbian Linux operating system.

Note

Raspbian Linux is the operating system that is used throughout this book.

NOOBS will then extract the Raspbian Linux operating system and reboot the Raspberry Pi. As NOOBS is extracting the operating system, you will be presented with a few tips on how to use the Raspberry Pi, including the default username and password (the default username is pi and the default password is raspberry).

After the Raspberry Pi is rebooted, you will be ready to use the raspi-config command to complete the installation (refer to the Booting Raspbian Linux for the first time recipe).

There's more...

NOOBS is the easiest way to get started with the Raspberry Pi. It is an installation tool and not a complete operating system.

In this recipe, you used NOOBS to install the Raspbian Linux operating system. NOOBS can also be used to install a number of other operating systems, including Arch, OpenELEC, Pidora, and RaspBMC.

By using NOOBS, Raspbian Linux can be configured so that it boots directly into an easy-to-use programming environment called Scratch. NOOBS also has a built-in configuration editor that can be used by experts to apply additional tweaks to the boot configuration.

See also

NOOBS (the New Out of Box Software) at https://github.com/raspberrypi/noobs

NOOBS is designed to make it easier to select and install operating systems for the Raspberry Pi without having to worry about manually imaging an SD card.

Mac OS X disk utilities – diskutil and dd

This recipe explains how to use the diskutil and dd disk utilities that are found on a Mac OS X computer to install a Raspberry Pi operating system image on an SD card.

You should have already downloaded a Raspberry Pi disk image and you should be ready to write the disk image onto an SD card.

Once you've completed this recipe, you will be able to write an SD card from a Mac OS X computer.

Getting ready

Ingredients:

A computer running Mac OS X with an SD card writerAn SD card of 4 GB or greater (class 10 has the best performance)A Raspberry Pi operating system image file

Both the diskutil and dd disk utility commands are installed by default with the Mac OS X operating system. The diskutil command is used to administer disk devices, and the dd command is used to copy data to and from a disk device.

The dd command requires administrative privileges. Use the sudo command to temporarily give the user administrative privileges.

How to do it...

The following steps need to be performed to write a disk image to an SD card on a Linux computer:

Open a Terminal.Determine the name of the SD drive by using the following command:
diskutil list
Unmount the mounted SD card by using the following command:
diskutil unmountdisk /dev/disk2
Use dd to copy the disk image to the SD card (this requires sudo), as follows:
sudo dd bs=1M if=raspbian.img of=/dev/rdisk2

Note

Choose the disk carefully! Ensure that you do not erase the wrong disk!

Here is an example of a Terminal session that shows the diskutil and dd commands being used to discover the name of the SD card's disk drive, unmount the SD card, and write a Raspberry Pi disk image to the SD card:

macosx:~ $ diskutil list /dev/disk0 #: TYPE NAME SIZEIDENTIFIER 0: GUID_partition_scheme*500.3 GB disk0 1: EFI EFI209.7 MB disk0s1 2: Apple_CoreStorage499.4 GB disk0s2 3: Apple_Boot Recovery HD 650.0 MB disk0s3 /dev/disk1 #: TYPE NAME SIZE IDENTIFIER 0: Apple_HFS Macintosh HD *499.1 GB disk1 /dev/disk2 #: TYPE NAME SIZE IDENTIFIER 0: FDisk_partition_scheme NO_NAME *4.0 GB disk2 1: DOS_FAT_32 NO_NAME 4.0 GB disk2s1 macosx:~ $ diskutilumountdisk /dev/disk2 macosx:~ $ cd Downloads macosx:Downloads $ dd bs=1M if=raspbian.img of=/dev/rdisk2

How it works...

The diskutil command is used to find the name of the SD card and unmount the disk.

The command is first used with the list subcommand to show information about each mounted disk drive.

After the SD card is inserted, the SD card appears in this list as /dev/disk2.

Now that we know that the SD card disk device is /dev/disk2, the SD card is unmounted by using the unmountdisk subcommand.

Finally, the dd command is used to write the Raspberry Pi disk image to the SD card:

Each written disk block is 1 MB (bs=1M)The input file (if) is raspbian.imgThe output file (of) is the SD card disk device (/dev/rdisk2)

Note that the output file is named /dev/rdisk2 and not /dev/disk2. The extra r asks Mac OS X to use the raw mode when writing to the disk. The raw mode is much faster, if you want to write, than the default mode.

There's more...

The diskutil command utility is a feature-rich tool that is used to modify, verify, and repair disks on Mac OS X. More information about the diskutil command can be found by using the built-in man pages (man diskutil).

Before the image is copied to the SD card in the preceding example, the disk partition is unmounted (diskutil unmountdisk). It is important to unmount the disk before formatting or overwriting it.

When the image is copied with the dd command,

if= specifies the input file (raspbian.img)of= specifies the output file (/dev/rdisk2)bs= specifies the size of the blocks written to the disk

The dd utility can also be used as a backup tool. Just exchange the input file (if=) and output file (of=).

Use the following command to create a backup using the disk from the preceding example:

dd bs=1M if=/dev/rdisk2 of=backup-2015-06-20.img

Pressing Ctrl + T while the dd command is running will cause the command to report its progress.

More information about thedd command can be found in its man pages (man dd).

See also

Disk Utility (http://en.wikipedia.org/wiki/Disk_Utility): The diskutil command can be used to unmount disks from a system. This Wikipedia article explains in detail all the features of the diskutil command.diskutil – modify, verify, and repair local disks (https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/diskutil.8.html): The diskutil command is part of the Mac OS X operating system. The Apple man page for diskutil describes the command and its options.dd – convert and copy a file (https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/dd.1.html): The dd command can be used to copy images to and from disks. The Apple man page for dd gives details about the command and its options.

Image Writer for Windows – Win32DiskImager.exe

This recipe shows how to install a Raspberry Pi operating system image on an SD card using the open source Image Writer for Windows, Win32DiskImager.exe.

You should have already downloaded a Raspberry Pi disk image, and you should be ready to write the disk image onto an SD card using a Windows PC.

To complete this recipe, you will also need an Internet connection to download Image Writer for Windows.

Once you've completed this recipe, you will be able to write Raspberry Pi images to SD cards from a Windows computer.

Getting ready

Here are the ingredients:

A computer running Windows with an SD card writerAn SD card of 4 GB or greater (class 10 has the best performance)A Raspberry Pi operating system image fileA precompiled Win32DiskImager binary

The precompiled binary of Win32DiskImager is distributed as a ZIP file and can be downloaded from https://launchpad.net/win32-image-writer.

How to do it...

The following steps are required to write a disk image to an SD card on a Windows computer:

Download the Win32DiskImager ZIP file from https://launchpad.net/win32-image-writer.Expand the ZIP file to a folder on disk, such as C:\Win32DiskImager.Run Win32DiskImager.exe from the install folder.Select the downloaded Raspberry Pi disk image as the source image file and the location of your SD card writer as the target device.Click on the Write button to copy the image to the SD card.

Writing an image to disk takes about 5 minutes for a 2 GB image file. Once the image is written to the SD card, the SD card can be ejected and used to boot the Raspberry Pi.

How it works...

First, you will need to download and install Image Writer for Windows (Win32DiskImager). The Win32DiskImager is a single standalone executable and can be installed to a folder that is located anywhere on your PC.

Double-click on the expanded Win32DiskImager executable to start the application.

Once the application has started, select the downloaded Raspberry Pi disk image as the source Image File and then select the location of the SD card writer as the target device. When you click on the Write button, the Win32DiskImager writes the Raspberry Pi disk image to the SD card.

There's more...

The Win32DiskImager is also an excellent backup tool! After booting and configuring the Raspberry Pi, a backup can be made to preserve the image in case the SD card is damaged or lost.

To create a backup, perform the following steps:

Run Win32DiskImager.exe.Select SD card as the source and a new image file as the target.Click on the Read button to read the SD card in a new image on the disk.

The steps needed to back up a Raspberry Pi disk image from an SD card are similar to those required to write the image. The only difference is that during a backup, the SD card is the source of the copy, and a new image on the disk is the target.