Beruflich Dokumente
Kultur Dokumente
by Antun Peicevic
First edition
Technical editor: Marko Maslac
Copyright© 2015 Geek University Press
Disclaimer
This book is designed to provide information about selected topics for the Raspberry Pi.
Every effort has been made to make this book as complete and as accurate as possible, but
no warranty is implied. The information is provided on an as is basis. Neither the authors,
Geek University Press, nor its resellers, or distributors will be held liable for any damages
caused or alleged to be caused either directly or indirectly by this book. The opinions
expressed in this book belong to the author and are not necessarily those of Geek
University Press.
Note that this is not an unofficial book. The Raspberry Pi Foundation, the charitable
organization responsible for developing the Raspberry Pi, is in no way affiliated with this
book or its content.
Trademarks
Geek University is a trademark of Signum Soft, LLC, and may not be used without written
permission.
Feedback Information
At Geek University Press, our goal is to create in-depth technical books of the highest
quality and value. Readers’ feedback is a natural continuation of this process. If you have
any comments about how we could improve our books and learning resources for you, you
can contact us through email at books@geek-university.com. Please include the book title
in your message. For more information about our books, visit our website at http://geek-
university.com.
About the author
Antun Peicevic is a systems engineer with more than 10 years of experience in the
internetworking field. His certifications include CCNA Routing and Switching, CompTIA
Network+, CompTIA Security+, and much more. He is the founder and editor of geek-
university.com, an online education portal that offers courses that cover various aspects of
the IT system administration. Antun can be reached at antun@geek-university.com.
About this book
This book was written to help you start working with the Raspberry Pi, a powerful credit-
card-sized computer developed with the intention of promoting the teaching of computer
science and programming in schools.
Audience
This book is written for beginners looking to get into the exciting world of Raspberry Pi.
Although the book presumes some knowledge about computer systems in general, it is
customized for beginners.
Prerequisites
You should have a basic understanding of computers. You should know how to download
a program from the internet and format an SD card. It would be helpful is you have some
basic knowledge of Windows and Linux operating systems, but this is not required.
What you’ll learn
You will learn how and where to buy the device. You will learn how to dowload and
install Raspbian, the most popular operating system for Raspberry Pi. After completing
this book, you should know how to install applications on the Raspberry Pi using
command line and GUI tools.
Note that this is not an unofficial book. Raspberry Pi Foundation, the charitable
organization responsible for developing the Raspberry Pi, is in no way affiliated with this
book or its course.
Table of Contents
Chapter 1 - Introduction
What is the Raspberry Pi?
Raspberry Pi hardware
Setting up Raspberry Pi
Formatting an SD card
Operating systems
Chapter 2 - Raspbian OS
Installing Raspbian
Using Raspbian
Connecting to the Internet
The Raspberry Pi Store
Install an application from the Pi Store
Raspbian repository
Updating Raspbian
Synaptic Package Manager
Chapter 3 - raspi-config
raspi-config tool
Expand filesystem
Change user password
Boot behaviour
Internationalisation options
Enable camera
Add your location to Rastrack
Overclock your Raspberry Pi
Change hostname
Memory split
Chapter 5 - Terminal
What is a terminal?
Basic terminal commands
Manage directories
Manage files
man command
sort command
grep command
find command
tail command
ps command
top command
tar command
ifconfig command
df command
The Raspberry Pi is a fully functional credit card-sized computer. It was developed by the
Raspberry Pi Foundation with the intention of promoting the teaching of computer
science and programming in schools. The device is designed to be easy to use and as
affordable as possible.
The Raspberry Pi comes as a printed circuit board, but you can buy all the extra stuff
needed to make it fully functional. The Raspberry Pi size is 85.60mm x 56mm x 21mm (or
3.37” x 2.21” x 0.83”) and weighs 45g. As of December 2015, five models are available:
Raspberry Pi Model A – it has 256MB RAM, only one USB port and no network
connection.
Raspberry Pi Model B – it has 512MB RAM, 2 USB ports and a network connection.
Raspberry Pi Model B+ – it has the similar specifications as the Model B, but comes with
4 USB ports, more GPIO pins, and uses less power than the Model B. This model costs
$35.
Raspberry Pi 2 Model B – the latest version of the device, with 900 MHz quad-core ARM
Cortex-A7 CPU and 1 GB of RAM.
Raspberry Pi Zero - a stripped-down (but still powerful) version of the Raspberry Pi. This
model is equipped with 1GHz ARM11 core and 512 MB of RAM, but comes without an
Ethernet port.
Here is how the Raspberry Pi Model A looks like (source: Wikipedia):
You can buy your model directly from the official site or from main distributors: Premier
Farnell/Element 14 and RS Components/Allied Electronics. There are also many resellers
worldwide and many sellers on e-commerce websites like eBay and Amazon.
The Raspberry Pi primarily uses Linux-based operating systems, with Raspbian usually
the first choice for beginners. Raspbian is based on a popular Linux distribution called
Debian and features a GUI similar to the one found in Windows:
Raspberry Pi hardware
When you get your hands on the Raspberry Pi, the first thing you will notice is that it is
tiny - just 85.60mm x 56mm x 21mm (3.37 in x 2.21 in x 0.83 in). The device usully comes
without a protective case, so you have a good overview of all the components that make
up the device. Let’s examine each component on the board:
Setting up Raspberry Pi
Although it might seem daunting at first, setting up your Raspberry Pi is very simple; you
just need to put specific cables into specific slots, just like you would do with a normal
computer. The Raspberry Pi usually comes without all the necessary components, such as
an SD card, keyboard, mouse. You will also need a monitor or a TV as a display.
Here is a list and a brief description of all the components needed to start working with
your Raspberry Pi:
Power cable - a standard micro USB connector that runs as 5V can be used to power
the Raspberry Pi. The connector can be plugged into a PC USB port or into a wall
socket.
SD card - the Raspberry Pi uses a standard SD card (usually found in digital cameras)
instead of a hard disk. This card will hold the operating system that runs the
Raspberry Pi, such as Raspbian. The Raspberry Pi Model B+ uses microSD cards. A
minimum size SD card of 6GB is required.
Keyboard and mouse - any USB keyboard and mouse will suffice.
Display - the HDMI port is used for the video output of the Raspberry Pi, allowing
the Full HD resolution video (1080p) on your monitor or TV. If your monitor doesn’t
support HDMI but supports DVI, you will need to buy an HDMI-to-DVI adapter.
The following components are not necesarry to get you started with the Raspberry Pi, but
expand the feature set of the device:
Case - to protect your device from dust or accidental damage. Or just to make it look
more attractive.
Networking - the Raspberry Pi comes with a standard Ethernet port that can give you
Internet acess.
USB hub - although the Raspberry Pi Model B+ comes with four USB ports, you
might need additional USB ports for some more complex project. USB hubs are
usually very cheap, costing just a couple of dollars.
Camera board - provides connectivity for a camera capable of capturing pictures and
video.
Wi-Fi adapter - connects to one of the USB ports on the device and provides you
with the wireless connectivity.
Formatting an SD card
The Raspberry Pi Model B+ uses a standard microSD card to hold the operating system
(such as Raspbian). However, before installing the OS, you will need to format the SD
card to use the FAT32 filesystem. To do this, follow these steps:
1. If your computer has an SD card slot, insert your SD card in it. If not, you will need an
adapter to be able to format the card.
2. Start up your browser and go to the https://www.sdcard.org/downloads/formatter_4, the
webpage of SD Formatter, a free software for SD cards formatting.
3. Select your operating system on which you will install SD Formatter:
Operating systems
Rasbian is the operating system of choice for a beginner. Raspbian is based on a popular
Linux distribution called Debian and was specially designed for the Raspberry Pi. It
features a nice GUI, similar to the one found in Windows:
If you are familiar with Linux-based operating systems and the shell, you can install Arch
Linux ARM, a port of Arch Linux for ARM processors.
Chapter 2 - Raspbian OS
Installing Raspbian
Using Raspbian
Connecting to the Internet
The Raspberry Pi Store
Install an application from the Pi Store
Raspbian repository
Updating Raspbian
Synaptic Package Manager
Installing Raspbian
Rasbian is the operating system of choice for a beginner. It is based on a popular Linux
distribution called Debian and was specially designed for the Raspberry Pi. Raspbian
contains a lot of pre-installed programs that will help get you up and running and features
a nice GUI:
Raspbian runs LXDE (Lightweight X11 Desktop Environment) as the desktop environment,
and Openbox as the window manager. It can be installed using NOOBS, the recommended
install manager for the Raspberry Pi. Here is how you can do just that:
1. Download the NOOBS zip file from https://www.raspberrypi.org/downloads.
2. Extract the zip file to a folder of your choice and transfer the files to your SD card:
3. Place the SD card in your Raspberry Pi. Connect the USB keyboard and the USB
mouse. Use the HDMI port to connect your monitor.
4. Finally, power on your device by connecting it to the power outlet or the USB port on
your PC. The NOOBS installer should start. Tick the checkbox next to Raspbian and click
the Install button. The installation procedure should start:
5. When the install process is completed, the Raspberry Pi configuration menu (raspi-
config) will load. To start Raspbian, select Finish:
Raspbian uses the default username pi and password raspberry. To load the graphical
user interface, type the startx command in the shell.
Using Raspbian
The Raspbian desktop environment is similar to the one found in Windows operating
systems. You have a file manager, a web browser, a menu bar, a terminal, etc:
Raspbian features a Menu bar similar to the Start menu found in Windows. This Menu bar
allows you to quickly start applications or turn of your device:
Raspbian also includes a file manager called PCMan File Manager (PCManFM) that
allows you to manipulate files and folders. To start it, click the second icon on the right of
the Menu bar:
A web browser included in Raspbian is called Epiphany. It can be accessed by clicking on
the first icon on the right of the Menu bar:
Raspbian features a terminal called LXTerminal where you can issue text commands to the
system. It can be accessed by clicking on the third icon on the right of the Menu bar:
Connecting to the Internet
The Raspberry Pi models B and B+ both come with a standard 10/100 Mbit/s Ethernet port
that can be used to connect the device to the Internet. Simply plug an Ethernet cable to the
Rasbperry Pi and connect it to your Internet router. Internet routers usually serve as DHCP
servers, and your Raspberry Pi should automatically obtain network parameters, such as
the IP address and subnet mask.
If your router is too far away from your Raspberry Pi or you own a model that doesn’t
have an Ethernet port, you might think about buying a Wi-Fi dongle, like this one (Edimax
EW-7811Un):
Of course, before buying a Wi-Fi dongle, make sure that your router supports wireless
networks!
Once you connect your device to the Internet router, run the ip addr command from the
terminal to display network settings:
In the picture above you can see that our device has an IP address of 192.168.5.11.
To verify that your device can access the Internet, enter the ping google.com command:
If you receive responses from google.com, the Raspberry Pi is able to access the Internet.
Now you can start the browser and begin surfing the Internet.
The Raspberry Pi Store was launched in 2012 to make it easier for developers to share
their applications with the rest of the Raspberry Pi community. It also makes the process
of finding and downloading applications much easier for total beginners.
The Pi Store can be accessed via http://store.raspberrypi.com or by using the dedicated Pi
app. To access the Pi Store from Raspbian, go to Menu > Internet > Pi Store:
Most of the applications available in the Pi Store are either free or low-priced.
The Pi Store enables you to browse and download content, and even to upload your own
application. To do all that, you will need to register. Click on the Log In link in the upper
right corner and click the Register button:
Enter your email address and password and click the Sign Me Up! button:
After you’ve logged in, you can browse and download applications and even upload your
own works. To browse applications, select the Explore tab. Applications are divided into
five sections:
Games
Apps
Tutorials
Dev Tools
Media
The Games and Apps sections offer you a number of applications and games you
can download and use. The Tutorials section contains useful books and videos that can
help you get started with the Raspberry Pi. The Dev Tools section contains tools to help
developers, and the Media section contains magazines and other Raspberry Pi related
resources.
To download and install application from the Pi Store you will need to install the Pi Store
application. If you’re using the official operating system image, the Pi Store application
will already be installed on your device. If you don’t have it installed, you can install it by
opening the terminal and running the following command:
sudo apt-get update && sudo apt-get install pistore
Next, if the Pi Store application isn’t already running, start it (Menu > Internet > Pi
Store):
Installing applications in the Raspberry Pi Store is pretty much straightforward. Go to the
Explore tab and browse for the application or game you would like to install. You can sort
the applications by popularity, ranking, price, etc. You can also search for an application.
Let’s install Libre Office, a free suite of applications similar to the ones found in Microsoft
Office.
First, search for the application by typing libre in the search box and clicking the search
icon:
Click on the Libre Office logo and click on the Download link:
After all the necessary files have been downloaded, the installation should start:
After the installation is completed, go to the My Library tab in the Pi Store application.
This tab lists all application you’ve downloaded and installed. Select the Libre Office
application and click Launch:
apt-cache
The apt-cache tool is used to search for an application. For example, let’s say that we want
to install a chess game on our Raspberry Pi. We would use the apt-cache search chess
command to search for such application:
We got a huge list of packages with the word chess in their name or description. One of
the packages found is called dreamchess. To get additional information about that
package, we can use the apt-cache show dreamchess command:
The apt-cache show dreamchess command show us a lot of useful information about the
package, such as the current version, architecture, homepage, size, and a brief description.
apt-get
Now that we’ve found the application, we can install it using the sudo apt-get install
dreamchess command:
The sudo command allowed us to run the command as root. The apt-get command must be
preceded with the sudo keyword.
The apt-get tool can also be used to remove installed packages. The syntax of the
command is sudo apt-get remove PACKAGE NAME. For example. to remove an
application we’ve installed previously, we would run the following command:
Updating Raspbian
It is a good practice to occasionaly update your Raspbian operating system. This can be
done with these two apt-get commands:
sudo apt-get update - updates your system’s package list:
sudo apt-get upgrade - upgrades all installed packages to the newest version. Before
running this command, make sure that you have enough disk space (you can check the
available disk space with the df -h command). The sudo apt-get upgrade command will
display how much space is needed to perform the upgrade.
Synaptic Package Manager
If you are new to Linux and the command line, you can install several graphical front-ends
to Advanced Packaging Tool (APT) to install and manage your applications. One of them
is Synaptic Package Manager, a package manager program with a nice and intuitive GUI.
To install it, start the terminal (the third icon on the right of the Menu bar) and enter the
sudo apt-get install synaptic command:
After the installation finishes, start Synaptic by running the sudo synaptic command:
You can search for a package by clicking the search icon and typing keywords in the
search box. For example, if you want to install a text editor, enter the text editor keywords
in the box and click Search:
Select the application you would like to install and choose Mark for installation:
If additional changes are required in order to proceed, click the Mark button:
You can use the arrow keys to move around the menus. When done making changes,
select Finish and press the Enter key. We will describe most of the configuration options
in the following sections.
Expand filesystem
The first option available in the raspi-config tool is the Expand Filesystem option. By
default, the size of the Raspbian root file system is 2GB. If you have an SD card with
more capacity (8GB, for example), the portion of disk space will be unused. This option
enables you to expand your Raspbian installation to fill the rest of the SD card, giving you
more space to use for files.
If you’ve installed Raspbian using NOOBS, ignore this option. The root partition was
expanded automatically during the installation procedure.
To expand the root filesystem, highlight the Expand Filesystem option and press Enter:
The process of extending the partition begins immediately; there is no confirmation
dialog. You will need to reboot your device. Be patient, the expansion process lasts for a
couple of minutes.
Boot behaviour
By default, Raspbian will boot into the command line mode. You can change the default
boot behaviour by selecting the Enable Boot to Desktop/Scratch option in raspi-config:
Console Text console, requiring login (default) - boots into the command line. This is
the default option.
Desktop Log in as user ‘pi’ at the graphical desktop - boots into a graphical desktop
environment. You might prefer this GUI environment if you are not familiar with the
command line.
Scratch Start the Scratch programming environment upon boot - boots into the
Scratch programming environment. Scratch is a visual programming tool which
allows you to create games with a drag-and-drop interface.
Choose your preferred boot option and select Ok:
Internationalisation options
You can change your timezone, keyboard layout, and language and regional settings by
selecting Internationalisation Options in raspi-config:
Change Locale
If you’re happy using your Raspberry Pi in English, you don’t need to use this option.
Choose the Change Locale option to configure your language and regional settings. Upon
selecting this option, a configuration wizard opens:
Select locales you would like to generate (by pressing Space) and select Ok. If promted to
choose the default locale for the system environment, select a locale and select Ok:
Change Timezone
Choose this option to select your timezone. Upon selecting this option, a configuration
wizard opens:
Next, you can choose the key to function as the ALtGr key, if your keyboard doesn’t have
one:
You can also choose the Compose key:
Finally, choose whether you would like that the Ctrl+Alt+Backspace combination
terminates the X server:
Enable camera
The Enable Camera option in the raspi-config tool is used to enable or disable support for
the Raspberry Pi camera module:
In the window that opens, choose Enable to enable the camera support:
This will ensure that at least 128MB of RAM is dedicated to the GPU (Graphics Processor
Unit).
To add your device to the map, select the Add to Rastrack option in raspi-config:
This will open up a wizard that will guide you through the process. Note that the internet
connection is required.
Change hostname
By default, the Raspberry Pi hostname is set to raspberrypi. This name will be visible on
the network. If you have more than one device, you might want to change the default
hostname, so you can differentiate between devices and prevent name conflicts. This can
be done by choosing the Hostname option under Advanced Options in raspi-config:
Read the notice and click Ok:
Enter the new hostname. The name should contain only letters, numbers, and the hyphen
(-):
Reboot your device. The device should have the new hostname after the reboot.
Another way you can change your hostname is by using the terminal. Launch the terminal
(third icon on the right of the Menu bar) and enter the sudo nano /etc/hostname command.
This will open up the hostname file in the nano text editor. Type the new hostname in
nano, save the changes (Ctrl+o) and exit (Ctrl+x) :
Now, open the /etc/hosts file in nano using the sudo nano /etc/hosts command. Change all
occurances of the old hostname with the new hostname:
Reboot your Raspberry Pi. The device should have the new hostname after the reboot.
Memory split
Your Raspberry Pi device has two processors: CPU (Central Processing Unit) and GPU
(Graphics Processing Unit). You probably know that CPU is used for calculations, and
GPU is used graphical tasks, such as playing videos and games. The memory of your
device is shared between these two processors. By default, 64MB of RAM is allocated for
the GPU.
If you use your Raspberry Pi for graphics-intensive work, increasing the amount of RAM
allocated to GPU can improve the performance. You can change the amount of memory
available to the GPU in raspi-config by selecting the Memory Split option under Advanced
Options:
Choose the amount of memory allocated to the GPU:
Of course, if you are not using graphics, you can decrease the amount of memory
allocated for the GPU. This can be useful in situations when you are using your Raspberry
Pi as a server and GUI is not needed.
Chapter 4 - Fun & games
Play music
Play videos
Play games
Take pictures and videos
Create a script that takes a picture
Record slow-motion videos
Raspberry Pi motion detector
Play music
Your Raspberry Pi device can be used to play your favourite MP3s. There are several MP3
players available for Raspbian. We will use the one called LXMusic.
To install this program, simply start the terminal (the third icon on the right of the Menu
bar), enter the sudo apt-get install lxmusic command and type Y when prompted:
After the installation process is completed, start the application by selecting Menu >
Sound & Video > Music Player:
LXMusic has a familiar interface - you can play and pause MP3s, create playlists, adjust
volume, etc. To add a file to the playlist, select the blue plus icon in the lower left corner:
Choose the files you would like to add and click Add:
To play the file, simply choose the play button above the playlist.
Play videos
The video should start in full screen. To enable the sound output on the HDMI port, use
the -o hdmi option:
You can issue various command while a video is playing by using the following keys:
With omxplayer, you don’t even need to enter the GUI to play videos; you can watch
videos even if Raspbian boots into the command line.
Play games
Although you won’t be able to play the latest version of Far Cry on it, the Raspberry Pi
can run a reasonable amount of games. The first place to look for games is the Raspberry
Pi Store. Start it by selecting Menu > Internet > Pi Store:
Under the Explore tab, select Games. You can sort the available games by popularity,
price, and ranking:
Select the game of your interest and install it as any other application.
We can also use the command line to install a game in Raspbian. Let’s install a tetris game
called atris by using the sudo apt-get install atris command:
After the installation finishes, start the game by typing atris in the terminal. To quit the
game, press Q.
Here you have a comprehensive list of games available for Raspbian:
https://www.raspberrypi.org/forums/viewtopic.php?t=51794
Chapter 5 - Terminal
What is a terminal?
Basic terminal commands
Manage directories
Manage files
man command
sort command
grep command
find command
tail command
ps command
top command
tar command
ifconfig command
df command
What is a terminal?
Although Raspbian features a nice GUI in which you can do many day-to-day tasks, to get
a greater control of your Raspberry Pi you will need to use the terminal. The terminal
(also known as the shell or command-line interface) is a text-based interface that accepts
and interprets your commands. You can use terminal commands in Raspbian to run
programs, execute scripts, manipulate files, etc.
The default terminal on the Raspberry Pi devices running Raspbian is LXTerminal. If
you’ve ever worked in the Command prompt in Windows, you will find the interface
similar - it is text-based interface, where you type in commands and get a response. To
open LXTerminal from the GUI, click on the third icon on the right of the Menu bar:
The terminal has accepted our input and displayed the result of the command execution (in
this case, the current date is displayed on the screen). We will learn many more useful
commands in the following sections. Note that most of the commands we will go through
are included in other Linux distributions, such as Ubuntu or Red Hat.
In this section we will go through some of the basic commands in the terminal. We will
learn how to list the content of a directory, create and manipulate files, print a line of text,
change the working directory, etc.
ls command
One of the most commonly used commands it the ls command. This command lists the
content of the specified directory. If no directory is specified, the ls command displays the
content of the working directory:
pwd command
The pwd command is used to print the path of the current working directory (the directory
we are currently in):
cd command
The cd command is used to change the current working directory. The path of the
directory you would like to access needs to be specified in the command. For example, to
change the working directory to /opt, the cd /opt command is used:
Note how the prompt changed to reflect the changes.
echo command
The echo command is used to output text to the screen. The text that will be displayed is
entered after the echo keyword:
whoami command
To display the current user, the whoami command is used:
Manage directories
As you probably know, a directory is a file that stores other files on your system. In
Raspbian, all files are stored under the root directory (represented by the slash (/)
character) in a tree-like structure. In this section, we will describe some basic commands
used to manage directories in Raspbian.
Create a directory
The mkdir command is used to create a directory:
Move a directory
To move a directory, the mv command is used. Two parameters must be specified: the first
paramter is the directory you would like to move and the second parameter is the location
where you would like to move the directory to. For example, if we want to move the
directory named new-directory located at /home/pi/ to /tmp/, we need to enter the
following command:
Delete a directory
The rmdir command is used to delete a directory. For example, to delete the directory
/tmp/new-directory, the following command needs to be used:
If you get the rmdir: failed to remove ‘/tmp/new-directory/ ‘: Directory not empty error,
it means that your directory contain files. To remove the directory along with the content
of the directory, use the rm command with the -r flag:
Copy a directory
To copy a directory, use the cp command with the -r flag and two parameters: the source
directory you would like to copy and the destination. For example, to copy the
/home/pi/new-directory to the /tmp/ directory, use the following command:
There is a difference between the cp and mv command. With cp, the file is being
duplicated, and the original file is retained in the original location. With mv, the file is
being moved from one location to another, and the original file will not be present in the
original directory anymore.
Manage files
You can use the terminal in Raspbian to manipulate files. In this section, we will describe
some basic commands used to manage files in Raspbian.
Create files
Although there are several ways to create a file in the terminal, the most common way to
create a file is by using the touch command. The primary purpose of this command is to
update the timestamp of a file, but it also can be used to create one. The syntax of this
command is simple: just type touch, followed by the filename. If the file with that name
doesn’t exist, a new empty file is created. For example, to create a new file called
textfile.txt in the /home/pi/ directory, we can use the following command:
As you can see in the picture above, a file called textfile.txt was created.
Deleting files
The rm command is used to delete a file. For example, to delete the file we’ve just created
(/home/pi/textfile.txt), the following command can be used:
Copy a file
To copy a file, use the cp command with two parameters: the source file you would like to
copy and the destination directory. For example, to copy the /home/pi/textfile.txt to the
/tmp/ directory, we can use the following command.
man command
Raspbian contains thousands of terminal commands, each one with its own set of options.
Since it is impossible to remember all of the commands and their options, Linux
distributions (including Raspbian) include a program called man (short for manual page).
The purpose of this program is to display information about a command of your interest.
For example, to find out more information about the ls command, type man ls in the
terminal:
The man ls command will display all information about the ls command - how it is
invoked, its purpose, options, description, etc:
Note that the less program is used to display information. To display the next page of the
output, press Space. To move up and down a page, press the Page Up and Page Down
keys. To display all keys available in less, press h. To quit the program, press q.
If you don’t know the name of the command for something you would like to do, you can
use the man command to search for the desired command. For example, if you would like
to cancel print jobs but don’t know the exact command, you can type man -k cancel
command to get the name of the command:
The man -k cancel command displayed all commands with the word cancel in their name
or description, including the command we were looking for - lprm.
sort command
You can use the sort command in the terminal to sort the lines of a text file. This
command can be useful if you have a file with unordered lines and want to sort the lines in
some order, for example in alphabetical order.
Here’s our example text file:
We can use the sort command without any options to sort the lines in the file in the
alphabetical order:
To sort the lines in reverse alphabetical order, we use the -r flag with the sort command:
To check if the lines in the file are already sorted in alphabetical order, we use sort with
the -c flag. This command will report the first unsorted line:
To get more information about the sort command, check the command’s man pages with
the man sort command.
grep command
The grep utility is used to search plain-text data. It can search a single file or a whole
directory of files for lines containing the specified string. If a match is found, the line
containing the string is printed on the screen. The syntax of the grep command is:
grep [OPTIONS] PATTERN FILE
Here is an example. Let’s say that we have a text file called textfile.txt with the following
content:
To search textfile.txt for each occurence of the word text, we can use the following
command:
Two occurences of the word text have been found in textfile.txt. The lines containing the
word text have been printed on the screen.
Here is another example. To search the /home/pi/files/ directory (the working directory, in
this case) for each occurence of the word text, we can use the following command:
As you can see from the output of the ls command, three files are stored in the
/home/pi/files/ directory: file.txt, new-file.txt, and textfile.txt. The grep command has
searched the whole working directory (hence the ./* string) and found and printed three
matches of the word text.
The grep command is often used in conjunction with commands that produce a lot of
output, so you can sift through the output more easily. For example, to list all processes on
the system, you can use the ps -A command:
This command produces a lot of output - hundreds of lines of text, in fact. We can use the
grep command along with ps -A to find the specific process we are looking for. For
example, to search for a process started by the nano text editor, we can use the ps -A | grep
nano command:
Now we got only a single line of output - the line that contains the word nano. Note that
the pipe (|) was used to send the output of the ps -A command to the grep command.
By default, grep searches the text in the case-sensitive way. To do case-insensitive
searches, use the -i option with the command.
find command
The find command is used to search for files in the directory tree starting from the
specified location. It is most often used to search for files by their filenames, but you can
also search for files of specific sizes, permissions, group ownership, etc.
The syntax of the command is simple:
find PATH EXPRESSION
The PATH parameter represent the directory in which you would like to start the search.
The EXPRESSION parameter specifies whether to search for files by filename, size,
permission, etc.
Here is an example. To search for files in the /home/pi/files/ directory that begin with the
string new, we can use the following command:
Here is how we can find files bigger then 60 bytes:
In the picture above you can see that only one file is bigger than 40 bytes - textfile.txt.
When using the -size expression, c is used for bytes, k for Kilobytes, M for Megabytes and
G for Gigabytes.
To find files owned by the user pi, use the following command:
In the picture above you can that the find command listed all files owned by the pi user.
tail command
The tail command is used to display (by default) the last 10 lines of a text file. It is very
useful when examining log files, since it can show you only the most recent log events.
The /var/log/messages is probably the most important log file in Raspbian. It records a
variety of events, such as system error messages, system startups and shutdowns, network
configuration changes, etc. This is probably the first place to look for error messages if
your system is experiencing problems. To display the most recent events logged in this
file, we can use the tail command:
As you can see in the picture above, the tail command has displayed only the last 10 lines
of the /var/log/messages file. If you want to display more that the last 10 lines, use the -n
option to specify the number of lines that will be displayed:
In the picture above you can see that we’ve displayed the last 13 lines of the
/var/log/messages file.
Another useful option that can be used with the tail command is the -f option. When used
with this option, the tail command keeps the file open and displalys new lines as they are
being added to the file:
ps command
The output above doesn’t give us many useful information. We need to run the ps
command with various options in order to get more info about our system. For example, to
get information about all processes running on our system, we can use the ps -A
command:
The output above gives us some useful information, such as the PIDs of the running
processes and the commands used to start them. But the most commonly used options with
the ps command are a, u and x (ps aux). Used with these options, the ps command will
display all processes running on our system, along with useful information such as the
username of the process′s owner, CPU loads, the starting time of the process, the
command that initiated the process, etc:
Consider the following line from output:
USER - the user who owns the process (the user pi in this case)
PID - process ID of the process (2570)
%CPU - the CPU time used divided by the time the process has been running.
%MEM - the ratio of the process’s resident set size to the physical memory on the
machine
VSZ - virtual memory usage of entire process (in KiB)
RSS - resident set size, the non-swapped physical memory that a task has used (in
KiB)
TTY - controlling tty (terminal)
STAT - multi-character process state
START - starting time or date of the process
TIME - cumulative CPU time
COMMAND - the command used to start the process (tail -f /var/log/messages)
top command
Just like the ps command, the top command is used to list processes currently running on
your system. But top has one advantage over ps - it updates its display every few seconds,
so you can see how resource usage changes in almost real-time.
Consider the following top output (to invoke the command, simply type top in the
terminal):
As you can see from the picture above, the top command displays a variety of useful
information about processes. Here is a brief description of the most important ones:
By default, top sorts its entries by CPU usage, but you can sort the entries by other
columns as well:
The Raspberry Pi camera module attaches via a 15cm ribbon cable into the CSI connector
on your Raspberry Pi, located between the Ethernet and HDMI ports. The silver
connectors are facing the HDMI port:
Once you’ve connected the camera module, turn on your Raspberry Pi and start the
terminal (the third icon on the right of the Menu bar). Make sure you are using the latest
version of Raspbian by running the sudo apt-get update and sudo apt-get upgrade
commands:
To record a video, we can use a similar tool called raspivid. The command is raspivid -o
video.h264:
This command will take five seconds of video. The video will be named video.h264 and
you can play it in a video player such as omxplayer.
2. Now, open up the nano text editor (or some other text editor of your choice). Enter the
following text:
#!/bin/bash
DATE=$(date +%Y-%m-%d_%H%M)
raspistill -o /home/pi/pics/$DATE.jpg
The code above will take the picture and named it with a timestamp. Save the file (Ctrl +
o) and exit the program (Ctrl + x).
3. To make the script work, you need to make it executable. This is done using the chmod
+x FILENAME command:
4. Now you can run your script. You can do this simply by specifying the full path to the
script in the terminal:
In the picture above you can see that the script has taken a picture and named it using the
current date and time.
You don’t have to go to the terminal to run the script; you can do that from the GUI
environment. Simply browse to the directory in which the script is stored and double-click
the file. In the prompt that opens, click Execute:
The camera module for Raspberry Pi allows you to record slow-motion videos. You don’t
need to install any additional software; you simply specify the frames per second and
resolution parameters in the raspivid command.
The camera module can record 90 frames per second at 640 x 480 resolution. To do this,
open the terminal and enter the raspivid -w 640 -h 480 -fps 90 -t 10000 -o
slowMotion.h264 command. This command will record the video at 90 frames per second
for 10 seconds and save it to the file slowMotion.h264:
To play the video, you will need to install a video player, such as VLC. To install VLC,
open the terminal and run the sudo apt-get install vlc command:
To play the video, simply open it in VLC.
The Raspberry Pi camera module can take picture when a motion occurs, so you can use it
for surveillance purposes. Here is how this can be done:
1. First, make sure that your Raspberry Pi is running the latest firmware by running the
sudo rpi-update command:
2. If not already installed, install the picamera python module by using the sudo apt-get
install python-picamera command in the terminal:
3. You will also need to install Python Imaging Library (PIL) to add image processing
catabilities to your Python interpreter. This can be done by running the sudo apt-get install
python-imaging-tk command in the terminal:
4. Now, copy the code on this link to the text editor of your choice: http://goo.gl/GVUmg9
Make sure you get the right indentation of the code. Save the file with the extension .py.
5. Make the file executable by running the chmod +x FILENAME command:
6. Run the script with the python FILENAME command:
That’s it! Your Raspberry Pi should now detect motion and take a picture every time it
happens.
tar command
The tar (tape archive) program is used to create an archive by combining files and
directories into a single file. Tarballs (archive files created with tar and usually
compressed with gzip or bzip2) are often used to distribute software packages in the Linux
world, and you might encounter them while working with your Raspberry Pi device.
These software packages need to be decompressed and extracted before they can be
installed.
Create an archive
To archive and compress files textfile1.txt and textfile2.txt into a tarball file called tar-
example.tgz, use the tar cvfz tar-example.tgz textfile1.txt textfile2.txt command:
Extract an archive
To extract the archive created above, use the tar xvfz tar-example.tgz command:
ifconfig command
One of the ways to display your Raspberry Pi’s IP address is by using the ifconfig
command. This command can also used to configure, disable, and enable a network
interface. Used without any options, the ifconfig command displays network settings of all
network interfaces on your device:
As you can see from the picture above, the ifconfig command displayed information about
the eth0 interface (the 10/100 MBps Ethernet port on the Raspberry Pi board) and the lo
interface, a virtual network interface used for diagnostics and troubleshooting. Consider
the following line of the output:
The inet addr parameter represents the IP address of the interface (192.168.5.11 in our
case).
By default, a Raspberry Pi device running Raspbian will get network information (such as
the IP address and subnet mask) from the DHCP server. You can also configure the IP
address manually by using the ifconfig IP address. Here is how this can be done:
As you can see from the picture above, the ifconfig eth0 192.168.5.11 netmask
255.255.255.0 command was used to assign a new IP address to our device.
df command
The df command displays the available and used disk space. When invoked without any
options, this command shows disk space usage on all partitions:
As you can see from the picture above, the df command displayed the device files
associated with the file systems, the total amount of space on the file systems, the used
space on the file systems, the free space on the file systems, the percentage of used space,
and the mount point. However, the disk space is shown in 1KB blocks. You can use the -h
option to produce the output in a more human-readable form:
Now we got a more meaningful output, with disk sizes in GB. Notice that the size of our
root filesystem (rootfs) is 6,3 GB. Out of this 6,3 GB, 2,5 GB is used (or 41% of the total
space available).
Chapter 6 - Remote access
Access the Raspberry Pi remotely
Enable SSH access to the Raspberry Pi
Transfer files with SFTP
Access the Raspberry Pi remotely
A Raspberry Pi device running Raspbian can be configured for remote desktop. This
means that you don’t even need to have a spare monitor for the Raspberry Pi; you can
access the device from your computer running other operating system, such as Windows.
There are a couple of programs available to enable remote desktop. We will use an open
source application called xrdp.
To install xrdp, open the terminal (the third icon on the right of the Menu bar) and type the
sudo apt-get install xrdp command:
xrdp should start automatically once the installation process finishes. On your Windows
device, open the Remote Desktop Connection program (Start > Run > mstsc):
This will open the Remote Desktop Connection window. Type the IP address of your
Raspberry Pi device and click Connect:
You should get the Login to xrdp prompt. Type your username and password (pi and
raspberry, by default), and click OK to start the remote desktop session:
You can use SSH to access the command line of your Raspberry Pi. SSH (Secure Shell) is
a network protocol that enables you to log into the Raspberry Pi device over the network
and execute commands. All data transmitted over your network (including usernames and
passwords) is encrypted and secure from eavesdropping.
SSH is a client-server protocol; you usually install an SSH client program on your
Windows machine and enable an SSH server on the Raspberry Pi. The SSH server should
be enabled by default in Raspbian. To make sure that this is the case, open the raspi-config
tool (by entering the sudo raspi-config command in the terminal) and navigate to
Advanced Options > SSH. Make sure that the SSH server is enabled:
On the client computer you can use a program like Putty as the SSH client. Download
Putty and start it. Then enter the IP address of your Raspberry Pi device and make sure
that SSH is selected as the connection type:
The first time you are connecting to a new device, you will get the warning that the
server’s key is not cached in the registry. Click Yes to add the key to the registry and carry
on with the connection:
Log in with your username and password (pi and raspberry, by default):
And that’s it! Now you can execute commands and manage your Raspberry Pi remotely:
You can transfer files to and from your Raspberry Pi device by using a protocol called
SFTP. SFTP (Secure File Transfer Protocol) is a network protocol that provides secure file
transfers over a network. This protocol can also be used to change, browse and edit files
on your Raspberry Pi. Because SSH is used, all data transmitted over the network
(including usernames and passwords) is encrypted and secure from eavesdropping.
SFTP is a client-server protocol; you need to install an SFTP client on your computer to be
able to access the SFTP server (enabled by default in Raspbian). We will use a program
named FileZilla as our SFTP client.
Download FileZilla from https://filezilla-project.org/download.php?type=client, install it
and start it. In the FileZilla window, type the IP address of your device, enter the username
and password (pi and raspberry by default), enter the port 22, and click Quickconnect to
establish a remote connection:
On the right side of the FileZilla window, you should see the home directory of your user: