In this chapter, we will cover the following recipes:
Connecting the Raspberry Pi
Using NOOBS to set up your Raspberry Pi SD card
Networking and connecting your Raspberry Pi to the Internet via the LAN connector
Using built-in Wi-Fi and Bluetooth on the Raspberry Pi
Configuring your network manually
Networking directly to a laptop or computer
Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle
Connecting to the Internet through a proxy server
Connecting remotely to the Raspberry Pi over the network using VNC
Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding)
Sharing the home folder of the Raspberry Pi with SMB
Keeping the Raspberry Pi up to date
This chapter introduces the Raspberry Pi and the process to set it up for the first time. We will connect the Raspberry Pi to a suitable display, power, and peripherals. We will install an operating system on an SD card. This is required for the system to boot. Next, we will ensure that we can connect successfully to the Internet through a local network.
Finally, we will make use of the network to provide ways to remotely connect to and/or control the Raspberry Pi from other computers and devices, as well as to ensure that the system is kept up to date.
Once you have completed the steps within this chapter, your Raspberry Pi will be ready for you to use for programming. If you already have your Raspberry Pi set up and running, ensure that you take a look through the following sections as there are many helpful tips.
The Raspberry Pi is a single-board computer created by the Raspberry Pi Foundation, a charity formed with the primary purpose of reintroducing low-level computer skills to children in the UK. The aim was to rekindle the microcomputer revolution of the 1980s, which produced a whole generation of skilled programmers.
Even before the computer was released at the end of February 2012, it was clear that the Raspberry Pi had gained a huge following worldwide and, at the time of writing this book, has sold over 10 million units. The following image shows several different Raspberry Pi Models:

Raspberry Pi Model 3B, Model A+, and Pi Zero
The name, Raspberry Pi, was the combination of the desire to create an alternative fruit-based computer (such as Apple, BlackBerry, and Apricot) and a nod to the original concept of a simple computer that can be programmed using Python (shortened to Pi).
In this book, we will take this little computer, find out how to set it up, and then explore its capabilities chapter by chapter using the Python programming language.
It is often asked, "Why has Python been selected as the language to use on the Raspberry Pi?" The fact is that Python is just one of the many programming languages that can be used on the Raspberry Pi.
There are many programming languages that you can choose, from high-level graphical block programming, such as Scratch, to traditional C, right down to BASIC, and even raw Machine Code Assembler. A good programmer often has to be code multilingual to be able to play to the strengths and weaknesses of each language in order to best meet the needs of their desired application. It is useful to understand how different languages (and programming techniques) try to overcome the challenge of converting "what you want" into "what you get" as this is what you are trying to do as well while you program.
Python has been selected as a good place to start when learning about programming, by providing a rich set of coding tools while still allowing simple programs to be written without fuss. This allows beginners to gradually be introduced to the concepts and methods on which modern programming languages are based without requiring them to know it all from the start. It is very modular with lots of additional libraries that can be imported to quickly extend the functionality. You will find that over time, this encourages you to do the same, and you will want to create your own modules that you can plug into your own programs, thus taking your first steps into structured programming.
Like all programming languages, Python isn't perfect; things such as adding a space at the start of a line will often break your code (indents matter a lot in Python; they define how blocks of code are grouped together). Generally, Python is slow; since it is interpreted, it takes time to create a module while it is running the program. This can be a problem if you need to respond to time critical events. However, you can precompile Python or use modules written in other languages to overcome this. It hides the details; this is both an advantage and disadvantage. It is excellent for beginners but can be difficult when you have to second-guess aspects such as data-types. However, this in turn forces you to consider all the possibilities, which can be a good thing.
A massive source of confusion for beginners is that there are two versions of Python on the Raspberry Pi (Version 2.7 and Version 3.4), which are not compatible with one another, so code written for Python 2.7 may not run with Python 3.4 (and vice versa).
The Python Software Foundation is continuously working to improve and move forward with the language, which sometimes means they have to sacrifice backward compatibility in order to embrace new improvements (and importantly, remove redundant and legacy ways of doing things).
Note
Supporting both Python 2 or Python 3
There are many tools that will ease the transition from Python 2 to Python 3, including converters such as 2to3
, which will parse and update your code to use Python 3 methods. This process is not perfect, and in some cases, you'll need to manually rewrite sections and fully retest everything. You can write the code and libraries that will support both. The import __future__
statement allows you to import the friendly methods of Python 3 and run them using Python 2.7.
Essentially, the selection of which version to use will depend on what you intend to do. For instance, you may require Python 2.7 libraries, which are not yet available for Python 3.4. Python 3 has been available since 2008, so these tend to be older or larger libraries that have not been translated. In many cases, there are new alternatives to legacy libraries; however, their support can vary.
In this book, we have used Python 3.4, which is also compatible with Python 3.3 and 3.2.
Since its release, the Raspberry Pi has come in various iterations, featuring both small and large updates and improvements to the original Raspberry Pi Model B unit. Although it can be confusing at first, there are three basic types of Raspberry Pi available (and one special model).
The main flagship model is called Model B . This has all the connections and features, as well as the maximum RAM and the latest processor. Over the years, there have been several versions, most notably Model B (which had 256 MB and then 512 MB RAM) and then Model B+ (which increased the 26-pin GPIO to 40 pins, switched to using a micro SD card slot, and had four USB ports instead of two). These original models all used the Broadcom BCM2835 SOC (short for System On Chip), consisting of a single core 700 MHz ARM11 and VideoCore IV GPU (short for Graphical Processing Unit).
The release of the Raspberry Pi 2 Model B (also referred to as 2B) in 2015 introduced a new Broadcom BCM2836 SOC, providing a quad-core 32-bit ARM Cortex A7 1.2 GHz processor and GPU, with 1 GB of RAM. The improved SOC added support for Ubuntu and Windows 10 IoT. Finally we had the latest Raspberry Pi 3 Model B, using another new Broadcom BCM2837 SOC, which provides a quad-core 64-bit ARM Cortex-A53 and GPU, alongside adding on-board Wi-Fi and Bluetooth.
Model A has always been targeted as a cut-down version. While having the same SOC as Model B, there are limited connections consisting of a single USB port and no wired network (LAN). Model A+ again added more GPIO pins and a micro SD slot. However, the RAM was later upgraded to 512 MB of RAM and again only a single USB port/no LAN. The Broadcom BCM2835 SOC on Model A has not been updated so far (so is still a single core ARM11); however, a Model 3A (most likely using the BCM2837) is expected 2016/2017.
The Pi Zero is an ultra-compact version of the Raspberry Pi intended for embedded applications where cost and space are a premium. It has the same 40-pin GPIO and micro SD card slot as the other models, but lacks the on-board display (CSI and DSI) connection. It does still have HDMI (via a mini-HDMI) and a single micro USB OTG (on-the-go) connection. Although not present in the first revision of the Pi Zero, the most recent model also includes a CSI connection for the on-board camera.
Note
The Pi Zero was famously released in 2015 by being given away with the Raspberry Pi foundations magazine The MagPi, giving the magazine the kudos of being the first magazine to give away a computer on its cover! This did make me rather proud since (as you may have read in my biography at the start of this book) I was one of the founders of the magazine.
The special model is known as the compute module. This takes the form of a 200-pin SO-DIMM card. It is intended for industrial use or within commercial products, where all the external interfaces would be provided by a host/motherboard, into which the module would be inserted. Example products include the Slice media player (http://fiveninjas.com) and the Otto camera. The current module uses the BCM2835, although an updated compute module (CM3) is expected in 2016.
The Raspberry Pi Wikipedia page provides a full list of the all different variants and their specifications:
All sections of this book are compatible will all current versions of the Raspberry Pi, but Model 3B is recommended as the best model to start with. This offers the best performance (particularly useful for the GPU examples in Chapter 5, Creating 3D Graphics, and the OpenCV examples used in Chapter 8, Creating Projects with the Raspberry Pi Camera Module), lots of connections, and built-in Wi-Fi, which can be very convenient.
The Pi Zero is recommended for projects where you want low power usage or reduced weight/size but do not need the full processing power of Model 3B. However, due to its ultra-low cost, the Pi Zero is ideal for deploying a completed project after you have developed it.
There are many ways to wire up the Raspberry Pi and use the various interfaces to view and control content. For typical use, most users will require power, display (with audio), and a method of input such as keyboard and mouse. To access the Internet, refer to the Networking and connecting your Raspberry Pi to the Internet via the LAN connector or Using built-in Wi-Fi and Bluetooth on the Raspberry Pi recipes.
Before you can use your Raspberry Pi, you will need an SD card with an operating system installed or with the New Out Of Box System (NOOBS) on it, as discussed in the Using NOOBS to set up your Raspberry Pi SD card recipe.
The following section will detail the types of devices you can connect to the Raspberry Pi and, importantly, how and where to plug them in.
As you will discover later, once you have your Raspberry Pi set up, you may decide to connect remotely and use it through a network link, in which case you only need power and a network connection. Refer to the following sections: Connecting remotely to the Raspberry Pi over the network using VNC and Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding).
The layout of the Raspberry Pi is shown in the following figure:

The Raspberry Pi connection layout (Model 3 B, Model A+, and Pi Zero)
The description of the preceding figure is as follows:
Display: The Raspberry Pi supports the following three main display connections; if both HDMI and Composite video are connected, it will default to HDMI only.
HDMI
For best results, use a TV or monitor that has an HDMI connection, thus allowing the best resolution display (1080p) and also digital audio output. If your display has a DVI connection, you may be able to use an adapter to connect through the HDMI. There are several types of DVI connections; some support analogue (DVI-A), some digital (DVI-D), and some both (DVI-I). The Raspberry Pi is only able to provide a digital signal through the HDMI, so an HDMI-to-DVI-D adapter is recommended (shown with a tick mark in the following screenshot). This lacks the four extra analogue pins (shown with a cross mark in the following screenshot), thus allowing it to fit into both DVI-D and DVI-I type sockets:
HDMI-to-DVI connection (DVI-D adaptor)
If you wish to use an older monitor (with a VGA connection), an additional HDMI-to-VGA converter is required. The Raspberry Pi also supports a rudimentary VGA adaptor (VGA Gert666 Adaptor), which is driven directly off of the GPIO pins. However, this does use up all but 4 pins of the 40-pin header (older 26-pin models will not support the VGA output).
Analogue
An alternative display method is to use the analogue composite video connection (via the phono socket); this can also be attached to an S-Video or European SCART adapter. However, the analogue video output has a maximum resolution of 640 x 480 pixels, so it is not ideal for general use.
3.5mm phono analogue connections
When using the RCA connection or a DVI input, audio has to be provided separately by the analogue audio connection. To simplify the manufacturing process (by avoiding through-hole components), the Pi Zero does not have analogue audio or an RCA socket for analogue video (although they can be added with some modifications).
Direct Display DSI
A touch display produced by the Raspberry Pi Foundation will connect directly into the DSI socket. This can be connected and used at the same time as the HDMI or analogue video output to create a dual display setup.
Stereo Analogue Audio (all except Pi Zero): This provides an analogue audio output for headphones or amplified speakers. The audio can be switched via the Raspberry Pi configuration tool on the desktop between analog (Stereo Socket) and digital (HDMI), or via the command line using
amixer
oralsamixer
.Network (excluding models A and Pi Zero): The network connection is discussed in the Networking and connecting your Raspberry Pi to the Internet via the LAN connector recipe later in this chapter. If we use the Model A Raspberry Pi, it is possible to add a USB network adapter to add wired or even wireless networking (refer to the Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle recipe).
Onboard Wi-Fi and Bluetooth (Model 3 B only):
The Model 3 B has built-in 802.11n Wi-Fi and Bluetooth 4.1; see the Using the built-in Wi-Fi and Bluetooth on the Raspberry Pi recipe.
USB (x1 Model A/Zero, x2 Model 1 B, x4 Model 2 B and 3 B)—using a keyboard and mouse:
The Raspberry Pi should work with most USB keyboards and mice available. You can also use wireless mice and keyboards, which use RF Dongles. However, additional configuration is required for items that use the Bluetooth dongles.
If there is a lack of power supplied by your power supply or the devices are drawing too much current, you may experience the keyboard keys appearing to stick, and in severe cases, corruption of the SD card.
Note
USB power can be more of an issue with the early Model B revision 1 boards that were available prior to October 2012. They included additional Polyfuses on the USB output and tripped if an excess of 140 mA was drawn. The Polyfuses can take several hours or days to recover completely, thus causing unpredictable behavior to remain even when the power is improved.
You can identify a revision 1 board as it lacks the four mounting holes that are present the later models.
Debian Linux (upon which Raspbian is based) supports many common USB devices, such as flash storage drives, hard disk drives (external power may be required), cameras, printers, Bluetooth, and Wi-Fi adapters. Some devices will be detected automatically while others will require drivers to be installed.
Micro USB Power: The Raspberry Pi requires a 5V power supply that can comfortably supply at least 1000 mA (1,500 mA or more is recommended, particularly with the more power-hungry Model 2 and 3) with a micro USB connection. It is possible to power the unit using portable battery packs, such as the ones suitable for powering or recharging tablets. Again, ensure that they can supply 5V at 1000 mA or over.
You should aim to make all other connections to the Raspberry Pi before connecting the power. However, USB devices, audio, and network may be connected and removed while it is running without problems.
In addition to the standard primary connections you would expect to see on a computer, the Raspberry Pi also has a number of other connections.
Each of the following connections provides additional interfaces for the Raspberry Pi:
20 x 2 GPIO pin header (Model A+, B+, 2 B, 3 B, and Pi Zero): This is the main 40-pin GPIO header of the Raspberry Pi used for interfacing directly with hardware components. We use this connection in Chapters 6, 7, 9, and 10. The recipes in this book are also compatible with older models of the Raspberry Pi that have a 13 x 2 GPIO pin header.
P5 8 x 2 GPIO pin header (Model 1 B revision 2.0 only): We do not use this in the book.
Reset connection: This is present on later models (no pins fitted). A reset is triggered when Pin 1 (reset) and Pin 2 (GND) are connected together. We use this in the A controlled shutdown button recipe in Chapter 6, Using Python to Drive Hardware.
GPU/LAN JTAG: The Joint Test Action Group (JTAG) is a programming and debugging interface used to configure and test processors. These are present on newer models as surface pads. A specialist JTAG device is required to use this interface. We do not use this in the book.
Direct camera CSI: This connection supports the Raspberry Pi Camera module (as used in Chapter 8, Creating Projects with the Raspberry Pi Camera Module). Note that the Pi Zero has a smaller CSI connector than the other models, so it requires a different ribbon connector.
Direct display DSI: This connection supports a directly connected display, a 7-inch 800 x 600 capacitive touch screen.
The Raspberry Pi requires the operating system to be loaded onto an SD card before it starts up. The easiest way to set up the SD card is to use NOOBS; you may find that you can buy an SD card with NOOBS already loaded on it.
NOOBS provides an initial start menu that provides options to install several of the available operating systems onto your SD card.
Since NOOBS creates a RECOVERY partition to keep the original installation images, an 8-GB SD card or larger is recommended. You will also need an SD card reader (experience has shown that some built-in card readers can cause issues, so an external USB type reader is recommended).
If you are using an SD card that you have used previously, you may need to reformat it to remove any previous partitions and data. NOOBS expects the SD card to consist of a single FAT32 partition. If using Windows or Mac OS X, you can use the SD association's formatter, as shown in the following screenshot (available at https://www.sdcard.org/downloads/formatter_4/):

Get rid of any partitions on the SD card using SD formatter
From the Option Setting dialog box, set Format Size Adjustment. This will remove all the SD card partitions that were created previously.
If using Linux, you can use gparted
to clear any previous partitions and reformat it as a FAT32 partition.
The full NOOBS package (typically just over 1 GB) contains the Raspbian, the most popular Raspberry Pi operating system image built in. A lite version of NOOBS is also available that has no preloaded operating systems (although a smaller initial download of 20 MB and a network connection on the Raspberry Pi are required to directly download the operating system you intend to use).
NOOBS is available at http://www.raspberrypi.org/downloads, with the documentation available at https://github.com/raspberrypi/noobs.
By performing the following steps, we will prepare the SD card to run NOOBS. This will then allow us to select and install the operating system we want to use:
Get your SD card ready.
On a freshly formatted or new SD card, copy the contents of the
NOOBS_vX.zip
file. When it has finished copying, you should end up with something like the following screenshot of the SD card:NOOBS files extracted onto the SD card
You can now put the card into your Raspberry Pi, connect it to a keyboard and display, and turn the power on. Refer to the Connecting up the Raspberry Pi recipe for details on what you need and how to do this.
By default, NOOBS will display via the HDMI connection. If you have another type of screen (or you don't see anything), you will need to manually select the output type by pressing 1, 2, 3, or 4 according to the following functions:
Key 1 stands for the Standard HDMI mode (the default mode)
Key 2 stands for the Safe HDMI mode (alternative HDMI settings if the output has not been detected)
Key 3 stands for Composite PAL (for connections made via the RCA analogue video connection)
Key 4 stands for Composite NTSC (again, for connections via the RCA connector)
This display setting will also be set for the installed operating system.
After a short while, you will see the NOOBS selection screen that lists the available distributions (the offline version only includes Raspbian). There are many more distributions that are available, but only the selected ones are available directly through the NOOBS system. Click on Raspbian as this is the operating system being used in this book.
Press Enter or click on Install OS, and confirm that you wish to overwrite all the data on the card. This will overwrite any distributions previously installed using NOOBS but will not remove the NOOBS system; you can return to it at any time by pressing Shift when you turn the power on.
It will take around 20 to 40 minutes to write the data to the card depending on its speed. When it completes and the Image Applied Successfully message appears, click on OK and the Raspberry Pi will start to boot into the Raspberry Pi Desktop
.
The purpose of writing the image file to the SD card in this manner is to ensure that the SD card is formatted with the expected filesystem partitions and files required to correctly boot the operating system.
When the Raspberry Pi powers up, it loads some special code contained within the GPU's internal memory (commonly referred to as binary blob
by the Raspberry Pi Foundation). The binary blob provides the instructions required to read the BOOT Partition
on the SD card, which (in the case of a NOOBS install) will load NOOBS from the RECOVERY
partition. If at this point Shift is pressed, NOOBS will load the recovery and installation menu. Otherwise, NOOBS will begin loading the OS as specified by the preferences stored in the
SETTINGS Partition.
When loading the operating system, it will boot via the BOOT
partition using the settings defined in config.txt
and options in cmdline.txt
to finally load to the desktop on the root Partition. Refer to the following diagram:

NOOBS creates several partitions on the SD card to allow installation of multiple operating systems and provide recovery
NOOBS allows the user to optionally install multiple operating systems on the same card and provides a boot menu to select between them (with an option to set a default value in the event of a time-out period).
If you later add, remove, or reinstall an operating system, ensure first that you make a copy of any files, including system settings you wish to keep, as NOOBS may overwrite everything on the SD card.
When you power up the Raspberry Pi for the first time, it will start directly with the desktop. You can now configure the system settings using the Raspberry Pi Configuration program (under the Preferences menu on the Desktop or via the sudo raspi-config
command), which will allow you to perform changes to your SD card and set up your general preferences.

Raspberry Pi Configuration program
Ensure that you change the default password for the pi
user account once you have logged in, as the default password is well known. This is particularly important if you connect to public networks. You can do this with the passwd
command, as shown in the following screenshot:

Setting a new password for the pi user
This gives greater confidence because if you later connect to another network, only you will be able to access your files and take control of your Raspberry Pi.
To avoid any data corruption, you must ensure that you correctly shut down the Raspberry Pi by issuing a shutdown
command, as follows:
sudo shutdown –h now
Or use this one:
sudo halt
You must wait until this command completes before you remove power from the Raspberry Pi (wait for at least 10 seconds after the SD card access light has stopped flashing).
You can also restart the system with the reboot
command, as follows:
sudo reboot
An alternative to using NOOBS is to manually write the operating system image to the SD card. While this was originally the only way to install the operating system, some users still prefer it. It allows the SD cards to be prepared before they are used in the Raspberry Pi. It can also provide easier access to startup and configuration files, and it leaves more space available for the user (unlike NOOBS, a RECOVERY
partition isn't included).
The default Raspbian image actually consists of two partitions, BOOT
and SYSTEM
, which will fit into a 2 GB SD card (4 GB or more is recommended).
You need a computer running Windows/Mac OS X/Linux (although it is possible to use another Raspberry Pi to write your card, be prepared for a very long wait).
Download the latest version of the operating system you wish to use. For the purpose of this book, it is assumed you are using the latest version of Raspbian available at http://www.raspberrypi.org/downloads.
Perform the following steps depending on the type of computer you plan to use to write to the SD card (the .img
file you need is sometimes compressed, so before you start, you will need to extract the file).
The following steps are for Windows:
Ensure that you have downloaded the Raspbian image, as previously detailed, and extracted it to a convenient folder to obtain an
.img
file.Obtain the
Win32DiskImager.exe
file available at http://www.sourceforge.net/projects/win32diskimager.Run
Win32DiskImager.exe
from your downloaded location.Click on the folder icon and navigate to the location of the
.img
file and click on Save.If you haven't already done so, insert your SD card into your card reader and plug it into your computer.
Select the Device drive letter that corresponds to your SD card from the small drop-down box. Double-check that this is the correct device (as the program will overwrite whatever is on the device when you write the image).
Finally, click on the Write button and wait for the program to write the image to the SD card, as shown in the following screenshot:
Manually write operating system images to the SD card using Disk Imager
Once completed, you can exit the program. Your SD card is ready!
The following steps should work for the most common Linux distributions, such as Ubuntu and Debian:
Using your preferred web browser, download the Raspbian image and save it in a suitable place.
Extract the file from the file manager or locate the folder in the terminal and unzip the
.img
file with the following command:unzip filename.zip
If you haven't already done so, insert your SD card into your card reader and plug it into your computer.
Use the
df –h
command and identify the sdX identifier for the SD card. Each partition will be displayed as sdX1, sdX2, and so on, where X will bea
,b
,c
,d
, and so on for the device ID.Ensure that all the partitions on the SD card are unmounted using the
umount /dev/sdXn
command for each partition, wheresdXn
is the partition being unmounted.Write the image file to the SD card with the following command:
sudo dd if=filename.img of=/dev/sdX bs=4M
The process will take some time to write to the SD card, returning to the terminal prompt when complete.
Unmount the SD card before removing it from the computer using the following command:
umount /dev/sdX1
The following steps should work for most of the versions of OS X:
Using your preferred web browser, download the Raspbian image and save it somewhere suitable.
Extract the file from the file manager or locate the folder in the terminal and unzip the
.img
file with the following command:unzip filename.zip
If you haven't already done so, insert your SD card into your card reader and plug it into your computer.
Use the
diskutil list
command and identify the disk# identifier for the SD card. Each partition will be displayed as disk#s1, disk#s2, and so on, where # will be1
,2
,3
,4
, and so on for the device ID.Ensure that the SD card is unmounted using the
unmountdisk /dev/diskX
command, wherediskX
is the device being unmounted.Write the image file to the SD card with following command:
sudo dd if=filename.img of=/dev/diskX bs=1M
The process will take some time to write to the SD card, returning to the terminal prompt when complete.
Unmount the SD card before removing it from the computer using the following command:
unmountdisk /dev/diskX
The boot process of a manually installed OS image
A manually written image will be of a fixed size (usually made to fit the smallest-sized SD card possible). To make full use of the SD card, you will need to expand the system partition to fill the remainder of the SD card. This can be achieved using the Raspberry Pi Configuration tool.
Select Expand Filesystem,
as shown in the following screenshot:

The Raspberry Pi Configuration tool
Windows and Mac OS X do not support the ext4
format, so when you read the SD card, only the File Allocation Table (FAT) partitions will be accessible. In addition, Windows only supports the first partition on an SD card, so if you've installed NOOBS, only the RECOVERY
partition will be visible. If you've written your card manually, you will be able to access the BOOT
partition.
The data
partition (if you installed one via NOOBS) and the root
partition are in ext4
format and won't usually be visible on non-Linux systems.
Note
If you do need to read files from the SD card using Windows, a freeware program, Linux Reader (available at www.diskinternals.com/linux-reader) can provide read-only access to all of the partitions on the SD card.
Access the partitions from the Raspberry Pi. To view the currently mounted partitions, use df
, as shown in the following screenshot:

The result of the df command
To access the BOOT
partition from within Raspbian, use the following command:
cd /boot/
To access the RECOVERY
or data
partition, we have to mount it by performing the following steps:
Determine the name of the partition as the system refers to it by listing all the partitions, even the unmounted ones. The
sudo fdisk -l
command lists the partitions, as shown in the following screenshot:The partition table of a NOOBS install of Raspbian and data partition
mmcblk0p1
(vfat) RECOVERY
mmcblk0p2
(Extended partition) contains (root, data, BOOT)
mmcblk0p5
(ext4) root
mmcblk0p6
(vfat) BOOT
mmcblk0p7
(ext4) SETTINGS
If you have installed additional operating systems on the same card, the partition identifiers shown in the preceding table will be different.
Create a folder and set it as the mount point for the partition, as follows:
For the
RECOVERY
partition, use the following command:mkdir ~/recovery sudo mount –t vfat /dev/mmcblk0p1 ~/recovery
To ensure that they are mounted each time the system is started, perform the following steps:
Add the sudo mount commands to
/etc/rc.local
beforeexit 0
. If you have a different username, you will need to changepi
to match:sudo nano /etc/rc.local sudo mount -t vfat /dev/mmcblk0p1 /home/pi/recovery
Save and exit by pressing Ctrl + X, Y, and Enter.
Note
Commands added to /etc/rc.local
will be run for any user who logs on to the Raspberry Pi. If you only want the drive to be mounted for the current user, the commands can be added to .bash_profile
instead.
If you have to install additional operating systems on the same card, the partition identifiers shown here will be different.
You can use Win32 Disk Imager to make a full backup image of your SD card by inserting your SD card into your reader, starting the program, and creating a filename to store the image in. Simply click on the Read button instead to read the image from the SD card and write it to a new image file.
To make a backup of your system, or to clone to another SD card using the Raspberry Pi, use the SD Card Copier (available from the desktop menu via the Accessories | SD Card Copier).
Insert an SD card into a card reader into a spare USB port of the Raspberry Pi and select the new storage device, as shown in the following screenshot:

SD Card Copier program
Before continuing, the SD Card Copier will confirm that you wish to format and overwrite the target device and, if there is sufficient space, make a clone of your system.
The dd
command can similarly be used to back up the card, as follows:
For Linux, replacing
sdX
with your device ID, use this command:sudo dd if=/dev/sdX of=image.img.gz bs=1M
For OS X, replacing
diskX
with your device ID, use the following command:sudo dd if=/dev/diskX of=image.img.gz bs=1M
You can also use
gzip
and split to compress the contents of the card and split them into multiple files if required for easy archiving, as follows:sudo dd if=/dev/sdX bs=1M | gzip –c | split –d –b 2000m – image.img.gz
To restore the split image, use the following command:
sudo cat image.img.gz* | gzip –dc | dd of=/dev/sdX bs=1M
The simplest way to connect the Raspberry Pi to the Internet is by using the built-in LAN connection on the Model B. If you are using a Model A Raspberry Pi, a USB-to-LAN adapter can be used (refer to the There's more… section of the Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle recipe for details on how to configure this).
You will need access to a suitable wired network, which will be connected to the Internet, and a standard network cable (with an RJ45 type connector for connecting to the Raspberry Pi).
Many networks connect and configure themselves automatically using Dynamic Host Configuration Protocol (DHCP), which is controlled by the router or switch. If this is the case, simply plug the network cable into a spare network port on your router or network switch (or wall network socket if applicable).
Alternatively, if a DHCP server is not available, you shall have to configure the settings manually (refer to the There's more… section for details).
You can confirm this is functioning successfully with the following steps:
Ensure that the two LEDs on either side of the Raspberry Pi light up (the left orange LED indicates a connection and the green LED on the right shows activity by flashing). This will indicate that there is a physical connection to the router and the equipment is powered and functioning.
Test the link to your local network using the
ping
command. First, find out the IP address of another computer on the network (or the address of your router perhaps, often192.168.0.1
or192.168.1.254
). Now, on the Raspberry Pi terminal, use theping
command (the-c 4
parameter is used to send just four messages; otherwise, press Ctrl + C to stop) to ping the IP address as follows:sudo ping 192.168.1.254 -c 4
Test the link to the Internet (this will fail if you usually connect to the Internet through a proxy server) as follows:
sudo ping www.raspberrypi.org -c 4
Finally, you can test the link back to the Raspberry Pi by discovering the IP address using
hostname -I
on the Raspberry Pi. You can then use the ping command on another computer on the network to ensure it is accessible (using the Raspberry Pi's IP address in place of www.raspberrypi.org). The Windows version of theping
command will perform five pings and stop automatically, and will not need the-c 4
option.
If the aforementioned tests fail, you will need to check your connections and then confirm the correct configuration for your network.
If you find yourself using your Raspberry Pi regularly on the network, you won't want to have to look up the IP address each time you want to connect to it.
On some networks, you may be able to use the Raspberry Pi's hostname instead of its IP address (the default is raspberrypi
). To assist with this, you may need some additional software such as Bonjour
to ensure hostnames on the network are correctly registered. If you have an OS X Mac, you will have Bonjour running already. On Windows, you can either install iTunes (if you haven't got it) which also includes the service, or you can install it separately (via the Apple Bonjour Installer available from https://support.apple.com/kb/DL999). Then you can use the hostname, raspberrypi
or raspberrypi.local
, to connect to the Raspberry Pi over the network. If you need to change the hostname, then you can do so in the Raspberry Pi configuration tool, shown previously.
Alternatively, you may find it helpful to fix the IP address to a known value by manually setting the IP address. However, remember to switch it back to use DHCP when connecting on another network.
Some routers will also have an option to set a Static IP DHCP address, so the same address is always given to the Raspberry Pi (how this is set will vary on the router itself).
Knowing your Raspberry Pi's IP address or using the hostname is particularly useful if you intend to use one of the remote access solutions described later on, which avoids the need for a display.
Many home networks provide a wireless network over Wi-Fi; if you have a Raspberry Pi 3, then you can make use of the on-board Broadcom Wi-Fi to connect to it. The Raspberry Pi 3 also supports Bluetooth, so you can connect most standard Bluetooth devices and use them like you would on any other computer.
This method should also work for any supported USB Wi-Fi and Bluetooth devices, see the Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle recipe for extra help on identifying device and installing firmware (if required).
The latest version of Raspbian includes helpful utilities to quickly and easily configure your Wi-Fi and Bluetooth through the graphical interface.
Note
Note: If you need to configure the Wi-Fi via the command line, then see the Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle recipe for details.

Wi-Fi and Bluetooth configuration applications
You can use the built-in Bluetooth to connect a wireless keyboard, a mouse or even wireless speakers. This can be exceptionally helpful for projects where additional cables and wires are an issue, such as robotic projects, or when the Raspberry Pi is installed in hard-to-reach locations (acting as a server or security camera).
To configure your Wi-Fi connection, click on the networking symbol to list the local available Wi-Fi networks:

Wi-Fi listing of the available access points in the area
Select the required network (for example, Demo
) and if required enter your password (also known as Pre Shared Key
):

Providing the password for the access point
After a short while, you should see that you have connected to the network and the icon will change to a Wi-Fi symbol. If you encounter problems, ensure you have the correct password/key.

Successful connection to an access point
That is it, as easy as that!
You can now test your connection and ensure it is working by using the web browser to navigate to a website or by using the following command in the terminal:
sudo ping www.raspberrypi.com
To start, we need to put the Bluetooth device into discoverable mode, by clicking on the Bluetooth icon and selecting Make Discoverable. You will also need to make the device you want to connect to discoverable and ready to pair; this may vary from device to device (such as pressing a pairing button).

Set the Bluetooth as discoverable
Next, select Add Device... and select the target device and Pair:

Select and pair the required device
The pairing process will then start; for example, the BTKB-71DB keyboard will need the pairing code 467572
to be entered onto the keyboard for the pairing to complete. Other devices may use default pairing codes, often set to 0000, 1111, 1234, or similar.

Follow the instructions to pair the device with the required pairing code
Once the process has completed, the device will be listed and will connect automatically each time the devices are present and booted.
If your network does not include a DHCP server or it is disabled (typically, these are built into most modern ADSL/cable modems or routers), you may need to configure your network settings manually.
Before you start, you will need to determine the network settings for your network.
You will need to find out the following information from your router's settings or another computer connected to the network:
IPv4 address: This address will need to be selected to be similar to other computers on the network (typically, the first three numbers should match, that is,
192.168.1.X
ifnetmask
is255.255.255.0
), but it should not already be used by another computer. However, avoidx.x.x.255
as the last address since this is reserved as a broadcast address.Subnet mask: This number determines the range of addresses the computer will respond to (for a home network, it is typically
255.255.255.0
, which allows up to 254 addresses). This is also sometimes referred to as the netmask.Default gateway address: This address is usually your router's IP address, through which the computers connect to the Internet.
DNS servers: The DNS server (Domain Name Service) converts names into IP addresses by looking them up. Usually, they will already be configured on your router, in which case you can use your router's address. Alternatively, your Internet Service Provider (ISP) may provide some addresses, or you can use Google's public DNS servers at the addresses
8.8.8.8
and8.8.4.4
. These are also called nameservers in some systems.
For Windows, you can obtain this information by connecting to the Internet and running the following command:
ipconfig /all
Locate the active connection (usually called Local Area Connection 1 or similar if you are using a wired connection, or if you are using Wi-Fi, it is called wireless network connection) and find the information required, as follows:

The ipconfig/all command shows useful information about your network settings
For Linux and Mac OS X, you can obtain the required information with the following command (note that it is ifconfig
rather than ipconfig
):
ifconfig
The DNS servers are called nameservers and are usually listed in the resolv.conf
file. You can use the less
command as follows to view its contents (press Q to quit when you have finished viewing it):
less /etc/resolv.conf
To set the network interface settings, edit /etc/network/interfaces
using the following code:
sudo nano /etc/network/interfaces
Now, perform the following steps:
We can add the details for our particular network, the IP
address
number we want to allocate to it, thenetmask
address of the network, and thegateway
address, as follows:iface eth0 inet static address 192.168.1.10 netmask 255.255.255.0 gateway 192.168.1.254
To set the nameservers for DNS, edit
/etc/resolv.conf
using the following code:sudo nano /etc/resolv.conf
Add the addresses for your DNS servers as follows:
nameserver 8.8.8.8 nameserver 8.8.4.4
Save and exit by pressing Ctrl + X, Y, and Enter.
You can configure the network settings by editing cmdline.txt
in the BOOT
partition and adding settings to the startup command line with ip
.
The ip option takes the following form:
ip=client-ip:nfsserver-ip:gw-ip:netmask:hostname:device:autoconf
The
client-ip
option is the IP address you want to allocate to the Raspberry PiThe
gw-ip
option will set the gateway server address if you need to set it manuallyThe
netmask
option will directly set the netmask of the networkThe
hostname
option will allow you to change the defaultraspberrypi
hostnameThe
device
option allows you to specify a default network device if more than one network device is presentThe
autoconf
option allows the automatic configuration to be switched on or off
It is possible to connect the Raspberry Pi LAN port directly to a laptop or computer using a single network cable. This will create a local network link between the computers, allowing all the things you can do if connected to a normal network without the need for a hub or router, including connection to the Internet, if Internet Connection Sharing (ICS) is used as follows:

Make use of the Raspberry Pi with just a network cable, a standard imaged SD card, and power
ICS allows the Raspberry Pi to connect to the Internet through another computer. However, some additional configuration is required for the computers in order to communicate across the link, as the Raspberry Pi does not automatically allocate its own IP address.
We will use the ICS to share a connection from another network link, such as a built-in Wi-Fi on a laptop. Alternatively, we can use a direct network link (refer to the Direct network link section under the There's more… section) if the Internet is not required or if the computer has only a single network adapter.
Note
Although this setup should work for most of the computers, some setups are more difficult than the others. For additional information, see www.pihardware.com/guides/direct-network-connection.
You will need the Raspberry Pi with power and a standard network cable.
Note
The Raspberry Pi Model B LAN chip includes Auto-MDIX (Automatic Medium-Dependent Interface Crossover). Removing the need to use a special crossover cable (a special network cable wired so that the transmit lines connect to receive lines for direct network links), the chip will decide and change the setup as required automatically.
It may also be helpful to have a keyboard and monitor available to perform additional testing, particularly if this is the first time you have tried this.
To ensure that you can restore your network settings to their original values, you should check whether it has a fixed IP address or the network is configured automatically.
To check the network settings on Windows 10, perform these steps:
Open Settings from the start menu, then select Network & Internet, then Ethernet, and click on Change adapter options from the list of Related Settings.
To check the network settings on Windows 7 and Vista, perform the following steps:
Open Network and Sharing Center from the Control Panel and click on Change adapter settings on the left-hand side.
To check the network settings on Windows XP, open Network Connections from the Control Panel.
Find the item that relates to your wired network adapter (by default, this is usually called Ethernet or Local Area Connection, as shown in the following screenshot):
Locating your wired network connection
Right-click on its icon and click on Properties. A dialog box will appear, as shown in this screenshot:
Selecting the TCP/IP properties and checking the settings
Select the item called Internet Protocol (TCP/IP) or Internet Protocol Version 4 (TCP/IPv4) if there are two versions (the other is Version 6), and click on the Properties button.
You can confirm that your network is set by using automatic settings or a specific IP address (if so, take note of this address and the remaining details as you may want to revert the settings at a later point).
To check the network settings on Linux, perform the following steps:
To check the network settings on Mac OS X, perform the following steps:
Open System Preferences and click on Networks. You can then confirm whether the IP address is allocated automatically (using DHCP) or not.
Ensure that if any settings are manually set you take note of them so you can restore them later if you want to. Refer to the following screenshot:
OS X Network Settings dialog box
If you just need to access or control the Raspberry Pi without an Internet connection, refer to the Direct network link section in the There's more…section.
First, we need to enable ICS on our network devices. In this case, we will be sharing the Internet, which is available on Wireless Network Connection through the Ethernet connection to the Raspberry Pi.
For Windows, perform these steps:
Return to the list of network adapters, right-click on the connection that links to the Internet (in this case, the WiFi or Wireless Network Connection device), and click on Properties.
Right-click on your wireless device and select Properties
At the top of the window, select the second tab (in Windows XP, it is called Advanced; in Windows 7 and Windows 10, it is called Sharing), as shown in the following screenshot:
Selecting the TCP/IP properties and noting the allocated IP address
In the Internet Connection Sharing section, check the box for Allow other network users to connect through this computer's Internet connection (if present, use the drop-down box to select the Home networking connection: option as Ethernet or Local Area Connection). Click on OK and confirm whether you previously had a fixed IP address set for Local Area Connection.
For Mac OS X, to enable the ICS, perform the following steps:
Click on System Preferences and then click on Sharing.
Click on Internet Sharing and select the connection from which we want to share the Internet (in this case, it will be the Wi-Fi AirPort). Then select the connection that we will connect the Raspberry Pi to (in this case, Ethernet).
For Linux to enable the ICS, perform the following steps:
From the System menu, click on Preferences and then on Network Connections. Select the connection you want to share (in this case, Wireless) and click on Edit or Configure. In the IPv4 Settings tab, change the Method option to Shared to other computers.
The IP address of the network adapter will be the Gateway IP address to be used on the Raspberry Pi, and be assigned an IP address within the same range (matching except the last number). For instance, if the computer's wired connection now has 192.168.137.1
, the Gateway IP of the Raspberry Pi will be 192.168.137.1
and its own IP address might be set to 192.168.137.10
.
Fortunately, thanks to updates in the operating system, Raspbian will now automatically allocate a suitable IP address to join the network and set the gateway appropriately. However, unless we have a screen attached to the Raspberry Pi or scan for devices on our network, we do not know what IP address the Raspberry PI has given itself.
Fortunately (as mentioned in the Networking and connecting your Raspberry Pi to the Internet via the LAN connector recipe in the There's more… section), Apple's Bonjour software will automatically ensure hostnames on the network are correctly registered. As stated previously, if you have an OSX Mac you will have Bonjour running already. On Windows you can either install iTunes, or you can install it separately (available from https://support.apple.com/kb/DL999). By default, the hostname raspberrypi can be used.
We are now ready to test the new connection, as follows:
Connect the network cable to the Raspberry Pi and the computer's network port, and then power up the Raspberry Pi, ensuring that you have reinserted the SD card if you previously removed it. To reboot the Raspberry Pi if you edited the file there, use
sudo reboot
to restart it.Allow a minute or two for the Raspberry Pi to fully power up. We can now test the connection.
From the connected laptop or computer, test the connection by pinging with the hostname of the Raspberry Pi, as shown in the following command (on Linux or OS X, add
-c 4
to limit to four messages or press Ctrl + C to exit):ping raspberrypi
Hopefully, you will find you have a working connection and receive replies from theRaspberry Pi.
If you have a keyboard and screen connected to the Raspberry Pi, you can perform the following steps:
You can ping the computer in return (for example,
192.168.137.1
) from the Raspberry Pi terminal as follows:sudo ping 192.168.137.1 -c 4
You can test the link to the Internet by using
ping
to connect to a well-known website as follows, assuming you do not access the Internet through a proxy server:sudo ping www.raspberrypi.org -c 4
If all goes well, you will have full Internet available through your computer to the Raspberry Pi, allowing you to browse the web as well as update and install new software.
If the connection fails, perform the following steps:
Repeat the process, ensuring that the first three sets of numbers match with the Raspberry Pi and the network adapter IP addresses.
You can also check that when the Raspberry Pi powers up, the correct IP address is being set using the following command:
hostname -I
Check your firewall settings to ensure it is not blocking internal network connections.
When we enable ICS on the primary computer, the operating system will automatically allocate a new IP address to the computer. Once connected and powered up, the Raspberry Pi will set itself to a compatible IP address and use the primary computer IP address as an Internet Gateway.
By using Apple Bonjour, we are able to use the raspberrypi
hostname to connect to the Raspberry Pi from the connected computer.
Finally, we check whether the computer can communicate over the direct network link to the Raspberry Pi, back the other way, and also through to the Internet.
If you do not require the Internet on the Raspberry Pi, or your computer only has a single network adapter, we can still connect the computers together through a direct network link. Refer to the following diagram:

Connecting and using the Raspberry Pi with just a network cable, a standard imaged SD card, and power
For a network link to work between two computers, they need to be using the same address range. The allowable address range is determined by the subnet mask (for example, 255.255.0.0
or 255.255.255.0
would mean all IP addresses should be the same except for the last two or just the last number in the IP address; otherwise, they will be filtered).
To use a direct link without enabling ICS, check the IP settings of the adapter you are going to connect to and determine whether it is automatically allocated or fixed to a specific IP address.
Most PCs connected directly to another computer will allocate an IP address in the range 169.254.X.X
(with a subnet mask of 255.255.0.0
). However, we must ensure that the network adaptor is set to Obtain an IP address automatically.
For the Raspberry Pi to be able to communicate through the direct link, it needs to have an IP address in the same address range, 169.254.X.X
. As mentioned before, the Raspberry Pi will automatically give itself a suitable IP address and connect to the network.
Therefore, assuming we have Apple Bonjour (see previously for details), we only need to know the hostname given to the Raspberry Pi (raspberrypi
).
If you don't have a keyboard or screen connected to the Raspberry Pi, you can use this network link to remotely access the Raspberry Pi just as you would on a normal network (just use the new IP address you have set for the connection). Refer to the Connecting remotely to the Raspberry Pi over the network using VNC and Connecting remotely to the Raspberry Pi over the network using SSH (and X11 Forwarding) recipes.
There is lots of additional information available on my website, https://pihw.wordpress.com/guides/direct-network-connection, including additional troubleshooting tips and several other ways to connect to your Raspberry Pi without needing a dedicated screen and keyboard.
By adding a USB Wi-Fi dongle to the Raspberry Pi's USB port, even models without built-in Wi-Fi can connect to and use the Wi-Fi network.
You will need to obtain a suitable USB Wi-Fi dongle; and in some cases, you may require a powered USB hub (this will depend on the hardware version of the Raspberry Pi you have and the quality of your power supply). General suitability of USB Wi-Fi dongles will vary depending on the chipset that is used inside and the level of Linux support available. You may find that some USB Wi-Fi dongles will work without installing additional drivers (in which case you can jump to configuring it for the wireless network).
A list of supported Wi-Fi adapters is available at http://elinux.org/RPi_USB_Wi-Fi_Adapters.
You will need to ensure that your Wi-Fi adapter is also compatible with your intended network; for example, it supports the same types of signals 802.11bgn and the encryptions WEP, WPA, and WPA2 (although most networks are backward compatible).
You will also need the following details of your network:
Service set identifier (SSID): This is the name of your Wi-Fi network and should be visible if you use the following command:
sudo iwlist scan | grep SSID
Encryption type and key: This value will be None, WEP, WPA, or WPA2, and the key will be the code you normally enter when you connect your phone or laptop to the wireless network (sometimes, it is printed on the router).
You will require a working internet connection (that is, wired Ethernet) in order to download the required drivers. Otherwise, you may be able to locate the required firmware files (they will be the .deb
files), and copy them to the Raspberry Pi (that is, via a USB flash drive; the drive should be automatically mounted if you are running in desktop mode). Copy the file to a suitable location and install it with the following command:
sudo apt-get install firmware_file.deb
This task has two stages; first, we identify and install firmware for the Wi-Fi adapter, and then we need to configure it for the wireless network.
We will try to identify the chipset of your Wi-Fi adapter (the part that handles the connection); this may not match the actual manufacturer of the device.
An approximate list of supported firmware can be found with this command:
sudo apt-cache search wireless firmware
This will produce results similar to the following output (disregarding any results without firmware
in the package title):
atmel-firmware - Firmware for Atmel at76c50x wireless networking chips. firmware-atheros - Binary firmware for Atheros wireless cards firmware-brcm80211 - Binary firmware for Broadcom 802.11 wireless cards firmware-ipw2x00 - Binary firmware for Intel Pro Wireless 2100, 2200 and 2915 firmware-iwlwifi - Binary firmware for Intel PRO/Wireless 3945 and 802.11n cards firmware-libertas - Binary firmware for Marvell Libertas 8xxx wireless cards firmware-ralink - Binary firmware for Ralink wireless cards firmware-realtek - Binary firmware for Realtek wired and wireless network adapters libertas-firmware - Firmware for Marvell's libertas wireless chip series (dummy package) zd1211-firmware - Firmware images for the zd1211rw wireless driver
To find out the chipset of your wireless adapter, plug the Wi-Fi-adapter into Raspberry Pi, and from the terminal, run the following command:
dmesg | grep 'Product:\|Manufacturer:'
Note
This command stitches together two commands into one. First, dmesg
displays the message buffer of the kernel (this is an internal record of system events that have occurred since power on, such as detected USB devices). You can try the command on its own to observe the complete output.
The |
(pipe) sends the output to the grep
command, grep 'Product:\|Manuf'
checks it and only returns lines that contain Product
or Manuf
(so we should get a summary of any items that are listed as Product
and Manufacturer
). If you don't find anything or want to see all your USB devices, try grep 'usb'
instead.
This should return something similar to the following output (in this case, I've got a
ZyXEL device, which has a ZyDAS chipset (a quick Google search reveals that zd1211-firmware
is for ZyDAS devices):
[ 1.893367] usb usb1: Product: DWC OTG Controller [ 1.900217] usb usb1: Manufacturer: Linux 3.6.11+ dwc_otg_hcd [ 3.348259] usb 1-1.2: Product: ZyXEL G-202 [ 3.355062] usb 1-1.2: Manufacturer: ZyDAS
Once you have identified your device and the correct firmware, you can install it, as you would for any other package available through apt-get
(where zd1211-firmware
can be replaced with your required firmware). This is shown in the following command:
sudo apt-get install zd1211-firmware
Remove and reinsert the USB Wi-Fi dongle to allow it to be detected and the drivers loaded. We can now test if the new adapter is correctly installed with ifconfig
. The output is shown as follows:
wlan0 IEEE 802.11bg ESSID:off/any Mode:Managed Access Point: Not-Associated Tx-Power=20 dBm Retry long limit:7 RTS thr:off Fragment thr:off Power Management:off
The command will show the network adapters present on the system. For Wi-Fi, this is usually as wlan0
, or wlan1
, or so on if you have installed more than one. If not, double-check the selected firmware, and perhaps try an alternative or check on the site for troubleshooting tips.
Once we have the firmware installed for the Wi-Fi adapter, we will need to configure it for the network we wish to connect. We can use the GUI as shown in the previous recipe, or we can manually configure it through the terminal as shown in the following steps:
We will need to add the wireless adapter to the list of network interfaces, which is set in
/etc/network/interfaces
, as follows:sudo nano -c /etc/network/interfaces
Using the previous
wlan#
value in place ofwlan0
if required, add the following command:allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
When the changes have been made, save and exit by pressing Ctrl + X, Y, and Enter.
We will now store the Wi-Fi network settings of our network in the
wpa_supplicant.conf
file (don't worry if your network doesn't use thewpa
encryption; it is just the default name for the file):sudo nano -c /etc/wpa_supplicant/wpa_supplicant.conf
It should include the following:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=GB
The network settings can be written within this file as follows (that is, if the SSID is set as
theSSID
):If no encryption is used, use this code:
network={ ssid="theSSID" key_mgmt=NONE }
With the
WEP
encryption (that is, if theWEP
key is set astheWEPkey
), use the following code:network={ ssid="theSSID" key_mgmt=NONE wep_key0="theWEPkey" }
For the
WPA
orWPA2
encryption (that is, if theWPA
key is set astheWPAkey
), use the following code:network={ ssid="theSSID" key_mgmt=WPA-PSK psk="theWPAkey" }
You can enable the adapter with the following command (again, replace
wlan0
if required):sudo ifup wlan0
Use the following command to list the wireless network connections:
iwconfig
You should see your wireless network connected with your SSID listed as follows:
wlan0 IEEE 802.11bg ESSID:"theSSID" Mode:Managed Frequency:2.442 GHz Access Point: 00:24:BB:FF:FF:FF Bit Rate=48 Mb/s Tx-Power=20 dBm Retry long limit:7 RTS thr:off Fragment thr:off Power Management:off Link Quality=32/100 Signal level=32/100 Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 Tx excessive retries:0 Invalid misc:15 Missed beacon:0
If not, adjust your settings and use
sudo ifdown wlan0
to switch off the network interface, and thensudo ifup wlan0
to switch it back on.This will confirm that you have successfully connected to your Wi-Fi network.
Finally, we will need to check whether we have access to the Internet. Here, we have assumed that the network is automatically configured with DHCP and no proxy server is used. If not, refer to the Connecting to the Internet through a proxy server recipe.
Unplug the wired network cable, if still connected, and see if you can ping the Raspberry Pi website, as follows:
sudo ping www.raspberrypi.org
The Model A version of the Raspberry Pi does not have a built-in network port; so in order to get a network connection, a USB network adapter will have to be added (either a Wi-Fi dongle, as explained in the preceding section, or a LAN-to-USB adapter, as described in the following section).
Just like USB Wi-Fi, the adapter support will depend on the chipset used and the drivers available. Unless the device comes with Linux drivers, you may have to search the Internet to obtain the suitable Debian Linux drivers.
If you find a suitable .deb
file, you can install it with the following command:
sudo apt-get install firmware_file.deb
Also check using ifconfig
, as some devices will be supported automatically, appear as eth1
(or eth0
on Model A), and be ready for use immediately.
Some networks, such as workplaces or schools, often require you to connect to the Internet through a proxy server.
You will need the address of the proxy server you are trying to connect to, including the username and password if one is required.
You should confirm that the Raspberry Pi is already connected to the network and that you can access the proxy server.
Use the ping
command to check this as follows:
ping proxy.address.com -c 4
If this fails (you get no responses), you will need to ensure your network settings are correct before continuing.
Create a new file using nano
as follows (if there is already some content in the file, you can add the code at the end):
sudo nano -c ~/.bash_profile
To allow basic web browsing through programs such as midori while using a proxy server, you can use the following script:
function proxyenable { # Define proxy settings PROXY_ADDR="proxy.address.com:port" # Login name (leave blank if not required): LOGIN_USER="login_name" # Login Password (leave blank to prompt): LOGIN_PWD= #If login specified - check for password if [[ -z $LOGIN_USER ]]; then #No login for proxy PROXY_FULL=$PROXY_ADDR else #Login needed for proxy Prompt for password -s option hides input if [[ -z $LOGIN_PWD ]]; then read -s -p "Provide proxy password (then Enter):" LOGIN_PWD echo fi PROXY_FULL=$LOGIN_USER:$LOGIN_PWD@$PROXY_ADDR fi #Web Proxy Enable: http_proxy or HTTP_PROXY environment variables export http_proxy="http://$PROXY_FULL/" export HTTP_PROXY=$http_proxy export https_proxy="https://$PROXY_FULL/" export HTTPS_PROXY=$https_proxy export ftp_proxy="ftp://$PROXY_FULL/" export FTP_PROXY=$ftp_proxy #Set proxy for apt-get sudo cat <<EOF | sudo tee /etc/apt/apt.conf.d/80proxy > /dev/null Acquire::http::proxy "http://$PROXY_FULL/"; Acquire::ftp::proxy "ftp://$PROXY_FULL/"; Acquire::https::proxy "https://$PROXY_FULL/"; EOF #Remove info no longer needed from environment unset LOGIN_USER LOGIN_PWD PROXY_ADDR PROXY_FULL echo Proxy Enabled } function proxydisable { #Disable proxy values, apt-get and git settings unset http_proxy HTTP_PROXY https_proxy HTTPS_PROXY unset ftp_proxy FTP_PROXY sudo rm /etc/apt/apt.conf.d/80proxy echo Proxy Disabled }
Once done, save and exit by pressing Ctrl + X, Y, and Enter.
Note
The script is added to the user's own .bash_profile
file, which is run when that particular user logs in. This will ensure that the proxy settings are kept separately for each user. If you want all users to use the same settings, you can add the code to /etc/rc.local
instead (this file must have exit 0
at the end).
Many programs that make use of the Internet will check for the http_proxy
or HTTP_PROXY
environment variables before connecting. If they are present, they will use the proxy settings to connect through. Some programs may also use the HTTPS
and FTP
protocols, so we can set the proxy setting for them here too.
Note
If a username is required for the proxy server, a password will be prompted for. It is generally not recommended to store your passwords inside scripts unless you are confident that no one else will have access to your device (either physically or through the Internet).
The last part allows any programs that execute using the sudo
command to use the proxy environment variables while acting as the super user (most programs will try accessing the network using normal privileges first, even if running as a super user, so it isn't always needed).
We also need to allow the proxy settings to be used by some programs, which use super user permissions while accessing the network (this will depend on the program; most don't need this). We need to add the commands into a file stored in /etc/sudoers.d/
by performing the following steps:
Note
It is important to use visudo
here, as it ensures the permissions of the file are created correctly for the sudoers
directory (read only by the root
user).
Use the following command to open a new
sudoer
file:sudo visudo -f /etc/sudoers.d/proxy
Enter the following text in the file (on a single line):
Defaults env_keep += "http_proxy HTTP_PROXY https_proxy HTTPS_PROXY ftp_proxy FTP_PROXY"
Once done, save and exit by pressing Ctrl + X, Y, and Enter; don't change the
proxy.tmp
filename (this is normal forvisudo
; it will change it to proxy when finished).If prompted
What now?
, there is an error in the command. Press X to exit without saving and retype the command!After a reboot (using
sudo reboot
), you will be able to use the following commands to enable and disable the proxy respectively:proxyenable proxydisable
Often, it is preferable to remotely connect to and control the Raspberry Pi across the network, for instance, using a laptop or desktop computer as a screen and keyboard, or while the Raspberry Pi is connected elsewhere, perhaps even connected to some hardware it needs to be close to.
VNC is just one way in which you can remotely connect to the Raspberry Pi. It will create a new desktop session that will be controlled and accessed remotely. The VNC session here is separate from the one that may be active on the Raspberry Pi's display.
Ensure that your Raspberry Pi is powered up and connected to the Internet. We will use the Internet connection to install a program using apt-get
. This is a program that allows us to find and install applications directly from the official repositories.
First, we need to install the TightVNC server on the Raspberry Pi with the following commands. It is advisable to run an update
command first to get the latest version of the package you want to install as follows:
sudo apt-get update sudo apt-get install tightvncserver
Accept the prompt to install and wait until it completes. To start a session, use the following command to start a session:
vncserver :1
The first time you run this, it will ask you to enter a password (of no more than eight characters) to access the desktop (you will use this when you connect from your computer).
The following message should confirm that a new desktop session has been started:
New 'X' desktop is raspberrypi:1
If you do not already know the IP address of the Raspberry Pi, use hostname –I
and take note of it.
Next, we need to run a VNC client, VNC Viewer is suitable program, which is available at http://www.realvnc.com/ and should work on Windows, Linux, and OS X.
When you run VNC Viewer, you will be prompted for the Server address and Encryption type. Use the IP address of your Raspberry Pi with :1
. That is, for the IP address 192.168.1.69, use the 192.168.1.69:1
address.
You can leave the Encryption type as Off or Automatic.
Depending on your network, you may be able to use the hostname; the default is raspberrypi, that is raspberrypi:1
.
You may have a warning about not having connected to the computer before or having no encryption. You should enable encryption if you are using a public network or if you are performing connections over the Internet (to stop others from being able to intercept your data).
You can add options to the command line to specify the resolution and also the color depth of the display. The higher the resolution and color depth (can be adjusted to use 8 to 32 bits per pixel to provide low or high color detail), the more data has to be transferred through the network link. If you find the refresh rate a little slow, try reducing these numbers as follows:
vncserver :1 –geometry 1280x780 –depth 24
To allow the VNC server to start automatically when you switch on, you can add the vncserver
command to .bash_profile
(this is executed each time the Raspberry Pi starts).
Use the nano
editor as follows (the -c
option allows the line numbers to be displayed):
sudo nano -c ~/.bash_profile
Add the following line to the end of the file:
vncserver :1
The next time you power up, you should be able to remotely connect using VNC from another computer.
An SSH (Secure SHell) is often the preferred method for making remote connections, as it allows only the terminal connections and typically requires fewer resources.
An extra feature of SSH is the ability to transfer the X11 data to an X Windows server running on your machine. This allows you to start programs that would normally run on the Raspberry Pi desktop, and they will appear in their own Windows on the local computer, as follows:

X11 Forwarding on a local display
X Forwarding can be used to display applications, which are running on the Raspberry Pi, on a Windows computer.
If you are running the latest version of Raspbian, SSH and X11 Forwarding will be enabled by default (otherwise, double-check the settings explained in the How it works… section).
Linux and OS X have built-in support for X11 Forwarding; but if you are using Windows, you will need to install and run the X Windows server on your computer.
Download and run xming
from the Xming
site (http://sourceforge.net/projects/xming/).
Install xming
, following the installation steps, including the installation of PuTTY if you don't have it already. You can also download PuTTY separately from http://www.putty.org/.
Next, we need to ensure that the SSH program we use has X11 enabled when we connect.
For Windows, we shall use PuTTY to connect to the Raspberry Pi.
In the PuTTY Configuration dialog box, navigate to Connection | SSH | X11 and tick the checkbox for X11 Forwarding. If you leave the X display location option blank, it will assume the default Server 0:0
as follows (you can confirm the server number by moving your mouse over the Xming icon in the system tray when it is running):

Enabling X11 Forwarding within the PuTTY configuration
Enter the IP address of the Raspberry Pi in the Session settings (you may also find that you can use the Raspberry Pi's hostname here instead; the default hostname is raspberrypi
).
Save the setting using a suitable name, RaspberryPi
, and click on Open to connect to your Raspberry Pi.
You are likely to see a warning message pop up stating you haven't connected to the computer before (this allows you to check whether you have everything right before continuing).

Opening an SSH connection to the Raspberry Pi using PuTTY
For OS X or Linux, click on Terminal to open a connection to the Raspberry Pi.
To connect with the default pi
username, with an IP address of 192.168.1.69
, use the following command; the -X
option enables X11 Forwarding:
ssh -X pi@192.168.1.69
All being well, you should be greeted with a prompt for your password (remember the default value for the pi
user is raspberry
).
Ensure that you have Xming running by starting the Xming program from your computer's Start menu. Then, in the terminal window, type a program that normally runs within the Raspberry Pi desktop, such as leafpad
or scratch
. Wait a little while and the program should appear on your computer's desktop (if you get an error, you have probably forgotten to start Xming, so run it and try again).
X Windows and X11 is what provides the method by which the Raspberry Pi (and many other Linux-based computers) can display and control graphical Windows as part of a desktop.
For X11 Forwarding to work over a network connection, we need both SSH and X11 Forwarding enabled on the Raspberry Pi. Perform the following steps:
To switch on (or off) SSH, you can access the Raspberry Pi Configuration program under the Preferences menu on the Desktop and click on SSH within the Interfaces tab, as shown in the following screenshot (SSH is often enabled by default for most distributions to help allow remote connections without needing a monitor to configure it):
The Interfaces tab in the Raspberry Pi Configuration tool
Ensure that X11 Forwarding is enabled on the Raspberry Pi (again most distributions now have this enabled by default).
Use
nano
with the following command:sudo nano /etc/ssh/sshd_config
Look for a line in the
/etc/ssh/sshd_config
file that controls X11 Forwarding and ensure that it saysyes
(with no#
sign before it), as follows:X11Forwarding yes
Save if required by pressing Ctrl + X, Y, and Enter and reboot (if you need to change it) as follows:
sudo reboot
SSH and X 11 Forwarding is a convenient way to control the Raspberry Pi remotely; we will explore some additional tips on how to use it effectively in the following sections.
If you want to run an X program, but still be able to use the same terminal console for other stuff, you can run the command in the background with &
as follows:
leafpad &
Just remember that the more programs you run, the slower everything will get. You can switch to the background program by typing fg
and check for background tasks with bg
.
You can even run a complete desktop session through X11, although it isn't particularly user-friendly and VNC will produce better results. To achieve this, you have to use lxsession
instead of startx
(in the way you would normally start the desktop from the terminal).
An alternative is to use lxpanel
, which provides the program menu bar from which you can start and run programs from the menu as you would on the desktop.
When you have the Raspberry Pi connected to your network, you can access the home folder by setting up file sharing; this makes it much easier to transfer files and provides a quick and easy way to back up your data. Server Message Block (SMB) is a protocol that is compatible with Windows file sharing, OS X, and Linux.
Ensure that you have the Raspberry Pi powered and running with a working connection to the Internet.
You will also need another computer on the same local network to test the new share.
First, we need to install samba
, a piece of software that handles folder sharing in a format that is compatible with Windows sharing methods.
Ensure that you use update
as follows to obtain the latest list of available packages:
sudo apt-get update sudo apt-get install samba
The install will require around 20 MB of space and take a few minutes.
Once the install has completed, we can make a copy of the configuration file as follows to allow us to restore to defaults if needed:
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup sudo nano /etc/samba/smb.conf
Scroll down and find the section named Authentication
; change the # security = user
line to security = user
.
As described in the file, this setting ensures that you have to enter your username and password for the Raspberry Pi in order to access the files (this is important for shared networks).
Find the section called Share Definitions
and [homes]
, and change the read only = yes
line to read only = no
.
This will allow us to view and also write files to the shared home folder. Once done, save and exit by pressing Ctrl + X, Y, and Enter.
Note
If you have changed the default user from pi
to something else, substitute it in the following instructions.
Now, we can add pi
(the default user) to use samba
:
sudo pdbedit -a -u pi
Now, enter a password (you can use the same password as your login or select a different one, but avoid using the default Raspberry password, which would be very easy for someone to guess). Restart samba
to use the new configuration file, as follows:
sudo /etc/init.d/samba restart [ ok ] Stopping Samba daemons: nmbd smbd. [ ok ] Starting Samba daemons: nmbd smbd.
In order to test, you will need to know either the Raspberry Pi's hostname (the default hostname is raspberrypi
) or its IP address. You can find both of these with the following command:
hostname
For the IP address, add -I
:
hostname –I
On another computer on the network, enter the \\raspberrypi\pi
address in the explorer path.
Depending on your network, the computer should locate the Raspberry Pi on the network and prompt for a username and password. If it can't find the share using the hostname, you can use the IP address directly, where 192.168.1.69
should be changed to match the IP address \\192.168.1.69\pi
.
The Linux image used by the Raspberry Pi is often updated to include enhancements, fixes, and improvements to the system, as well as adding support for new hardware or changes made to the latest board. Many of the packages that you install can be updated too.
This is particularly important if you plan on using the same system image on another Raspberry Pi board (particularly a newer one) as older images will lack support for any wiring changes or alternative RAM chips. New firmware should work on older Raspberry Pi boards, but older firmware may not be compatible with the latest hardware.
Fortunately, you need not reflash your SD card every time there is a new release, since you can update it instead.
You will need to be connected to the Internet in order to update your system. It is always advisable to make a backup of your image first (and at a minimum, take a copy of your important files).
You can check your current version of firmware with the uname -a
command, as follows:
Linux raspberrypi 4.4.9-v7+ #884 SMP Fri May 6 17:28:59 BST 2016 armv7l GNU/Linux
The GPU firmware can be checked using the /opt/vc/bin/vcgencmd version
command, as follows:
May 6 2016 13:53:23 Copyright (c) 2012 Broadcom version 0cc642d53eab041e67c8c373d989fef5847448f8 (clean) (release) This is important if you are using an older version of firmware (pre-November 2012) on a newer board since the original Model B board was only 254 MB RAM. Upgrading allows the firmware to make use of the extra memory if available.
The free -h
command will detail the RAM available to the main processor (the total RAM is split between the GPU and ARM cores) and will give the following output:
total used free shared buffers cached Mem: 925M 224M 701M 7.1M 14M 123M -/+ buffers/cache: 86M 839M Swap: 99M 0B 99M
You can then recheck the preceding output following a reboot to confirm that they have been updated (although they may have already been the latest).
Before running any upgrades or installing any packages, it is worth ensuring you have the latest list of packages in the repository. The update
command gets the latest list of available software and versions:
sudo apt-get update
If you just want to obtain an upgrade of your current packages, upgrade
will bring them all up to date:
sudo apt-get upgrade
To ensure that you are running the latest release of Raspbian, you can run dist-upgrade
(be warned; this can take an hour or so depending on the amount that needs to be upgraded). This will perform all the updates that upgrade
will perform but will also remove redundant packages and clean up:
sudo apt-get dist-upgrade
Both methods will upgrade the software, including the firmware used at boot and startup (bootcode.bin
and start.elf
).
You will often find that you will want to perform a clean installation of your setup; however, this will mean you will have to install everything from scratch. To avoid this, I developed the Pi-Kitchen project (https://github.com/PiHw/Pi-Kitchen), based on the groundwork of Kevin Hill. This aims to provide a flexible platform for creating customized setups that can be automatically deployed to an SD card.

Pi Kitchen allows the Raspberry Pi be configured before powering up
The Pi-Kitchen allows a range of flavors to be configured, which can be selected from the NOOBS menu. Each flavor consists of a list of recipes, each providing a specific function or feature to the final operating system. Recipes can range from setting up custom drivers for Wi-Fi devices, to mapping shared drives on your network, to providing a fully functional web server out of the box, all combining to make your required setup.
This project is in beta, developed as a proof of concept, but once you have everything configured, it can be incredibly useful to deploy fully working setups directly onto an SD card. Ultimately, the project could be combined with Kevin Hill's advanced version of NOOBS, called PINN (short for PINN Is Not NOOBS), which aims to allow extra features for advanced users, such as allowing operating systems and configurations to be stored on your network or on an external USB memory stick.