Raspberry Pi
Raspberry Pi
#raspberry-
pi
Table of Contents
About 1
Remarks 2
Examples 2
SD card Setup 2
Pretext 5
Proxy Configuration 7
Examples 9
Introduction 15
Remarks 15
Examples 15
Introduction 18
Examples 18
Raspbian 18
OSMC 18
LibreELEC 18
Kali 19
Credits 20
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: raspberry-pi
It is an unofficial and free Raspberry Pi ebook created for educational purposes. All the content is
extracted from Stack Overflow Documentation, which is written by many hardworking individuals at
Stack Overflow. It is neither affiliated with Stack Overflow nor official Raspberry Pi.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to info@zzzprojects.com
https://riptutorial.com/ 1
Chapter 1: Getting started with Raspberry Pi
Remarks
This section provides an overview of what raspberry-pi is {it may also be described on Stack
Exchange with the shortened term of "RPi"}, and why a developer might want to use it.
It has its own dedicated Stack Exchange site here at: http://raspberrypi.stackexchange.com/.
This section provides an overview of why a developer might want to use it.
It should also mention any large subjects within raspberry-pi, and link out to the related topics.
Since the Documentation for raspberry-pi is new, you may need to create initial versions of those
related topics.
Examples
SD card Setup
Start by installing the OS on the MicroSD card, either NOOBS or Raspbian, both provided by the
Raspberry Pi foundation, available on their site. NOOBS, which stands for New Out Of Box
Software, is designed for beginners, and is the easiest to install on your SD card. You can either
follow their official instructions, or see below.
Format the SD card with a single partition of FAT32, using either the system drive management
tool or a third party tool, such as the SD Association's SD Formatter 4.0, or GParted. Download
the NOOBS ZIP file and unzip it. next, copy the files over to the partition that you just created.
Ensure that you eject it before disconnecting it.
Pretext:
These are detailed instructions on how to set up a Raspberry Pi with the Raspbian operating
system.
These instructions are somewhat Windows specific.
Some installation steps may apply to other operating systems as well, but keep the former in mind.
Contents
• Requirements
• Choosing an OS
• Installing the OS
• Basic Configuration
Requirements
https://riptutorial.com/ 2
1. Raspberry Pi (version 1, 2 or 3 - any model)
2. Power supply (5V, 2000mAh usb adapter - recommended)
3. MicroSD card (Class 10 recommended with at least 8GB - see list of compatible cards here:
eLinux)
4. Computer with SD Card reader (or a USB SD Card reader)
5. Monitor / TV with HDMI Port
6. HDMI Cable
7. USB Keyboard
8. Win32 Disk Imager
• NOOBS
A collection of different operating systems for the Raspberry Pi, it contains Raspbian. In
addition to Raspbian, NOOBS also includes Pidora, LibreELEC, OSMC, RISK OS, Arch
Linux, Windows 10 IOT Core, and a few other choices. It is important to note that not all of
these choices are available on every model of Raspberry Pi (for example, Windows IOT
Core is only available on Raspberry Pi 2 and newer) . Installing any other OS than Raspbian
will require an ethernet connection on your Raspberry Pi.
• Raspbian
The officially supported OS for the Raspberry Pi, it is a port of the OS known as Debian. Pre-
installed with educational software and with a large community - this is the recommended
OS for the Raspberry Pi
• Windows 10 IOT Core A simplified version of windows specifically designed for IOT (Internet
Of Things) devices. It is important to note this is not a full version of windows, and does not
have things user might expect, such as a start menu and the ability to run most Windows
applications.
SD card
Raspberry Pi
https://riptutorial.com/ 3
5. Plug in the USB Power cable in the Raspberry Pi
Operating System
If the OS was written correctly to the SD Card and the OS is a valid ARM Operating System, it
should be installed automatically to the Raspberry Pi - with little to no interaction.
(This does however depend greatly on what kind of OS that has been chosen)
If you chose NOOBS an interface will show up and you will be able to choose what to install.
Basic Configuration
Command Line:
The command sudo raspi-config gives you access to the Raspberry Pi configuration.
Setup Options:
1 Expand Filesystem Ensures that all of the SD card storage is available to the
OS
2 Change User Password Change password for the default user (pi)
3 Enable Boot to Desktop/Scratch Choose whether to boot into a desktop environment, Scratch,
or the command line
4 Internationalisation Options Set up language and regional settings to match your location
5 Enable Camera Enable this Pi to work with the Raspberry Pi camera
6 Add to Rastrack Add this Pi to the online Raspberry Pi Map (Rastrack)
7 Overclock Configure overclocking for your Pi
8 Advanced Options Configure advanced settings
9 About `raspi-config` Information about this configuration tool
• After a new and clean installation you are recommended to choose the first option: Expand
Filesystem
• If you're European and not familiar with the English keyboard layout you can go to
Internationalisation Options and change the keyboard language and more.
• It is recommended to avoid the Overclock menu as a beginner. Especially if you have the
Raspberry Pi 3. The RPI 3 is known to get very hot, even if it is not overclocked.
If you are thinking of overclocking the RPI 3, please obtain heatsinks or a fan for your board,
to keep it from overheating.
• Enabling SSH access can be done in the Advanced Options menu, the credentials will be the
default username and password.
GUI:
Installing Raspbian Jessie automatically boots into a graphical user interface, the aforementioned
options are presented in the Options menu in the top left corner.
It is recommended for beginners to keep using the GUI, but if you bought a Raspberry Pi to learn
the command line. The option to boot into the command line can be found in the Settings menu.
Note
Changing settings on the Raspberry Pi usually prompts a restart which happens when you accept
the changes you've made.
https://riptutorial.com/ 4
Extra Info
Default Username: pi
Default Password: raspberry
Shutting down your Raspberry Pi is done by pulling the USB power plug.
(Choosing shutdown in the Raspbian GUI will simply put the Raspberry Pi in a diminished state,
still using power - but less)
Restarting the Raspberry Pi is done by simply plugging in the cable after unplugging it.
(Unless the reboot option is chosen in the GUI or sudo reboot in the command line.
Pretext
This is an introduction to the Hello World program example on the Raspberry Pi written in C.
The following example makes use of the command line interface and is set up as a step-by-step
guide.
Along with creating a Hello World program, the reader will be introduced to simple linux command
line commands.
Second step:
Writing your first program.
Linux systems offer a great variety of text editors, natively you will find Vim or Nano.
This example will make use of the Nano text editor.
The following code is the source code for the Hello World program:
/* My first program */
https://riptutorial.com/ 5
#include<stdio.h>
int main()
{
printf("Hello World\n");
}
• After writing the code hit ctrl + x to exit the editor, hit y and then enter to save the changes.
• Write the command ls to check if the file is present in your directory.
Third step:
Compiling your first program.
• To compile our source code file helloworld.c we need to use a compiler, we will use the
GNU Compiler Collection - also known as GCC.
• The following command compiles the source code to an executable binary program
gcc helloworld.c -o myfirstprogram.bin
The source code file is offered as an argument to the GCC compiler and -o defines another
argument expressing that we would like the compiler to output something.
In this case we want it to output a .bin file that we named ourselves.
There is several other arguments you can use when compiling with GCC, an example would be
-wall which enables all warnings. This gives you information about any error GCC might
encounter.
Fourth step:
Running your first program.
• Running a program on the Raspberry Pi is done by adding ./ in front of the name of the
program that you want to run.
• We can run our program by writing ./myfirstprogram.bin
The command should execute the program and produce Hello World in the console window.
Usually we struggle through the process of making login in the Raspberry Pi using ssh or other
similar tools. But we can make the process more pleasent.
Once your Raspberry Pi is connected to a network it gets an IPv4 address and an IPv6 address,
that is based on the NIC's MAC address. The good thing about this is that the IPv6 address
doesn't change even if the network has a DHCP service.
That being said, let's discover our IPv6 address. You only need to login once using IPv4 or using a
monitor (HDMI or VGA). Open a terminal and type:
ifconfig
https://riptutorial.com/ 6
You will see something like:
As you can see your IPv4 would look like 192.168.0.1 and your IPv6 would be
fe80::21c:c0ff:feae:b5e6 (notice the line that starts with inet6 addr).
So, with this information you can login using the ssh command with the following syntax:
ssh -6 pi@fe80::21c:c0ff:feae:b5e6%eth0
This approach seems more complicated, but the IPv6 address is defined as the link local, and,
as it is based on the MAC address, unless you change it yourself, this will always work regardless
of your IPv4 address.
Proxy Configuration
If you are behind a proxy and need to connect to the internet, you can use:
export http_proxy="http://username:password@host:port/"
cd /etc/apt/apt.conf.d
Acquire::http::Proxy "http://yourproxyaddress:proxyport/";
Acquire::https::Proxy "http://yourproxyaddress:proxyport/";
With authentication:
Acquire::http::Proxy "http://username:password@yourproxyaddress:proxyport/";
The / at the very end was important. Without it it does not work.
https://riptutorial.com/ 7
One of the best Linux distributions currently for Raspberry Pi (from now on, "RPi") is Arch Linux.
This web shows the installation for:
This tutorial advices you not to install the AArch option --- "Some of the hardware on the board
may not work, or it may perform poorly".
https://riptutorial.com/ 8
Chapter 2: Create a scan station with scanbd
(Raspbian)
Examples
Introduction and installation
The purpose of this documentation is to use a scanner without any user interface. A common use
is to upload a PDF scanned file directly to Google Drive or Dropbox just by pressing scanner-
buttons.
scanbd permits to trigger actions from the scanner buttons, it uses sane.
What make things a little bit trickier is that by polling the scanner, scanbd locks the device. As we
also need to scan from local tools (like scanimage), we need to set a proxy that's going to interrupt
scandb from polling when needed. This is the job of scanbm, which monitors the network and give
priority over scanbd to any local access to the scanner.
Thus, we need to :
• Set up the local sane configuration to only scan from the network (through scanbm)
• Configure scanbd to have access and poll the local scanner
As root:
apt-get update
apt-get install libconfuse-dev libsane-dev libudev-dev libusb-dev xinetd
# Uncompress
tar xvzf scanbd-1.4.4.tgz && cd 1.4.4
# install it
make
make install
https://riptutorial.com/ 9
scanbd is now installed but not yet configured.
/etc/sane.d/dll.conf
# The next line enables the network backend; comment it out if you don't need
# to use a remote SANE scanner over the network - see sane-net(5) and saned(8)
net
/etc/sane.d/net.conf :
## saned hosts
# Each line names a host to attach to.
# If you list "localhost" then your backends can be accessed either
# directly or through the net backend. Going through the net backend
# may be necessary to access devices that need special privileges.
localhost
At this point, scanbm is not yet configured, no scanner can be reached from the network. We
need to set up scanbm along with scanbd to test the set up.
pi:# lsusb
pi:# Bus 001 Device 005: ID 04a9:1909 Canon, Inc. CanoScan LiDE 110
https://riptutorial.com/ 10
With that productId, grep the correct configuration file (it depends of your scanner model, for me it
is genesys.conf) :
cp /etc/sane.d/genesys.conf /usr/local/etc/scanbd/
cp /etc/sane.d/dll.conf /usr/local/etc/scanbd/
Edit /usr/local/etc/scanbd/dll.conf and replace net with the correct scanner backend:
# The next line enables the network backend; comment it out if you don't need
# to use a remote SANE scanner over the network - see sane-net(5) and saned(8)
genesys
scanbd -d7 -f
Avoid as much as you can to manipulate the env var SANE_CONFIG_DIR. It can
break things, the default value is already configured in
/usr/local/etc/scanbd/scanbd.conf and in the init script.
scanbd: foreground
scanbd: reading config file /usr/local/etc/scanbd/scanbd.conf
scanbd: debug on: level: 7
scanbd: dropping privs to uid saned
scanbd: dropping privs to gid scanner
scanbd: group scanner has member:
scanbd: saned
scanbd: pi
scanbd: drop privileges to gid: 110
scanbd: Running as effective gid 110
scanbd: drop privileges to uid: 110
scanbd: Running as effective uid 110
scanbd: dbus_init
scanbd: dbus match type='signal',interface='org.freedesktop.Hal.Manager'
scanbd: SANE_CONFIG_DIR=/usr/local/etc/scanbd/sane.d
https://riptutorial.com/ 11
scanbd: sane version 1.0
scanbd: Scanning for local-only devices
scanbd: found device: genesys:libusb:001:005 Canon LiDE 110 flatbed scanner
service sane-port
{
port = 6566
socket_type = stream
wait = no
user = saned
group = scanner
server = /usr/local/sbin/scanbm
server_args = scanbm -c /usr/local/etc/scanbd/scanbd.conf
disable = no
}
Most issues at this stage concern the permissions. Ensure that scanimage -L works
when you are logged as saned by doing sudo -u saned -s.
Actions are located in /usr/local/etc/scanbd/scanbd.conf. I have 4 buttons that are scan, copy,
email and file.
The default config file doesn't include all actions per default, you will probably have to
add the block manually. You can have less or more buttons depending of your scanner
https://riptutorial.com/ 12
model.
For each action, we will set a custom path for the script option.
action scan {
filter = "^scan.*"
numerical-trigger {
from-value = 1
to-value = 0
}
desc = "Scan to file"
# script must be an relative path starting from scriptdir (see above),
# or an absolute pathname.
# It must contain the path to the action script without arguments
# Absolute path example: script = "/some/path/foo.script
script = "/home/pi/scan.sh"
}
Don't forget to comment any other default action at the end of scanbd.conf :
# devices
# each device can have actions and functions, you can disable not relevant devices
#include(scanner.d/avision.conf)
#include(scanner.d/fujitsu.conf)
#include(scanner.d/hp.conf)
#include(scanner.d/pixma.conf)
#include(scanner.d/snapscan.conf)
#include(scanner.d/canon.conf)
You can now create your custom script to handle each action :
Each line relative to /sys/class/leds/led0/trigger are for controlling the LED to monitor
what's going on. You can do whatever you want, cat /sys/class/leds/led0/trigger
gives you all different pattern of lights.
/home/pi/scan.sh
#!/bin/bash
case $SCANBD_ACTION in
scan)
filename=file-$datetime
logger -t "scanbd: $0" "$SCANBD_DEVICE $SCANBD_ACTION - scanning --resolution 150 --mode
Color --depth 8 --format=tiff to $scan_dir/$filename.jpg"
echo timer >/sys/class/leds/led0/trigger
scanimage -d $SCANBD_DEVICE --resolution 150 --mode Color --depth 8 --format=tiff --
brightness 5 --contrast 20 | convert tiff:- -compress jpeg $scan_dir/$filename.pdf
echo none >/sys/class/leds/led0/trigger
logger -t "scanbd: $0" "Finished scanning"
;;
https://riptutorial.com/ 13
email)
logger -t "scanbd: $0" "Emailing $scan_dir/file-*pdf"
echo heartbeat >/sys/class/leds/led0/trigger
# here are the lines to send the file
echo none >/sys/class/leds/led0/trigger
esac
https://riptutorial.com/ 14
Chapter 3: Enable SSH on Raspbian
Introduction
Previous versions of Raspbian (prior to November 2016) had SSH enabled by default. They also
had a default username (pi) and password (raspberry) this was done to make first time setup for
new users easier. But this obviously represents a large security hole. The new release (November
2016) disables SSH by default, and will also display a warning if SSH is enabled without changing
the the default password.
Remarks
The contents of the ssh file do not matter - you can create an empty file like we did above.
During boot up the Pi will look for this file. If it exists SSH will be enabled and the file deleted.
Examples
Enable SSH using a Windows computer
To enable SSH create a file called ssh in the /boot directory of your SD card.
2. Open a command prompt (press Win+R on your keyboard to open the Run window. Then,
type cmd)
3. Enter the following at the command prompt (replacing DriveLetter with the letter you
identified in step 1):
echo.>DriveLetter:/ssh
4. Press Enter
1. Enter the following at the command prompt (again replacing DriveLetter with the letter you
identified in step 1)
dir DriveLetter:
2. Press Enter
To avoid the security warning mentioned above (re: SSH enabled with the default password)
change the default password for the Pi user. This can be done either from the command line with
https://riptutorial.com/ 15
the passwd, the raspi-config script, or settings menu in the Pixel desktop.
Above example helps us how to turn on SSH on Pi. This example is the prerequisite of above
example.
For Linux OS: Open the terminal and write down this ssh pi@your local IP address. Then you will
need to provide the password.
Note: Here pi: your raspberry pi username and your local IP address: the IP address of your
device. You will get this IP address from your router, DHCP client list. However, this topic is out of
scope.
For Windows OS: We can use Putty to login via ssh. At Host Name write your Raspberry pi IP
address.
After pressing Open. You should provide your username and password. After the successful
attempt, you will get this screen.
https://riptutorial.com/ 16
Read Enable SSH on Raspbian online: https://riptutorial.com/raspberry-pi/topic/8355/enable-ssh-
on-raspbian
https://riptutorial.com/ 17
Chapter 4: Operating systems
Introduction
This section contains information about various operating systems available for the Raspberry
Pi's.
Examples
Raspbian
The most popular operating system for the Raspberry Pi is a Debian based Raspbian.
Raspbian can be downloaded from official Raspberry Pi site in one of two variants:
Starting September 2016 Raspbian is shipped with PIXEL desktop environment ("Pi Improved
Xwindows Environment, Lightweight")
For Raspberry Pi- there is a specific layer meta-raspberrypi that needs to be used to create an
image.
Windows 10 IoT Core is available only for Raspberry Pi 2 and 3. It is important to note that this is
not a full version of windows as one might expect, it is designed specifically for IoT (Internet of
Things) applications. It will not run most standard windows applications, and does not have a start
menu. It boots to a page with some example code and videos, and has a command line interface.
OSMC
LibreELEC
https://riptutorial.com/ 18
Kali
Download Link:
https://www.offensive-security.com/kali-linux-arm-images/
Requirements:
Default Login:
Username: root
Password: toor
Notes
https://riptutorial.com/ 19
Credits
S.
Chapters Contributors
No
Enable SSH on
3 M Hornbacher, Saiful Azad, Steve Robillard
Raspbian
https://riptutorial.com/ 20