If you’re going to get the most out of your Raspberry Pi, you’ll need to learn a little Linux. The goal of this chapter is to present a whirlwind tour of the operating system and give you enough context and commands to get around the filesystem, install packages from the command line or desktop environment, and point out the most important tools you’ll need day to day.
Raspbian comes with the Lightweight X11 Desktop Environment (LXDE) graphical desktop environment installed (Figure 2-1). This is a trimmed-down desktop environment for the X Window System that has been powering the GUIs of Unix and Linux computers since the 1980s. Some of the tools you see on the desktop and in the menu are bundled with LXDE (the Leafpad text editor and the LXTerminal shell, for instance).
Running on top of LXDE is Openbox, a window manager that handles the look and feel of windows and menus. If you want to tweak the appearance of your desktop, click the Raspberry menu in the upper left, then choose Preferences→Appearance Settings. Unlike OS X or Windows, it is relatively easy to completely customize your desktop environment or install alternative window managers. Some of the other distributions for Raspberry Pi have different environments tuned for applications like set-top media boxes, phone systems, or network firewalls. See http://elinux.org/RPi_Distributions and Chapter 3 for more.
As of October 2015’s update to Raspbian, the default behavior is to log in and launch the desktop environment immediately after booting. If you find yourself at a prompt for a username and password, the default user is pi
and the password is raspberry
. If you find yourself at the text command line (which looks like pi@raspberrypi ~ $
) and want to launch the desktop environment, just type startx
and press Enter. We’ll cover the command line more in-depth later in this chapter.
The Raspberry Pi software engineers and designers have customized Raspbian Linux and its desktop environment for general-purpose computing, making, and learning. If you browse the programs in the Raspberry menu in the upper-left corner of your screen (Figure 2-2), you’ll notice that there are programming environments, office tools, accessories, games, and Internet programs preinstalled. Feel free to click around and explore!
A few applications that you encounter may be a little different than those in other desktop environments:
If you prefer not to move files around using the command line (more on that in a moment), select the File Manager from the taskbar or within the Raspberry menu→Accessories. You’ll be able to browse the filesystem using icons and folders the way you’re probably used to doing.
The default web browser has the code name Epiphany, and it works well with limited resources. It’s easy to forget how much work web browsers do these days. Because Raspbian is designed to be a very lightweight OS distribution, there are a number of features you may expect in a web browser that may not be available. There are a couple of other browser options, notably Chromium, which is a popular choice among Raspberry Pi users. You’ll have to install it separately (see “Installing New Software”).
Multimedia playback is handled by omxplayer
, which is complicated but powerful. It is only available as a command-line utility. Omxplayer is specially designed to work with the Graphics Processing Unit (GPU) on the processor; other free software like VLC and mPlayer won’t work well with the GPU.
To keep the price down, certain video licenses were not included with the Raspberry Pi. If you want to watch recorded TV and DVDs encoded in the MPEG-2 format (or Microsoft’s VC-1 format), you’ll need to purchase a license key from the Foundation’s online shop; the cost is less than $5. A license for H.264 (MPEG-4) decoding and encoding is included with the Raspberry Pi.
Bundled with Raspbian is a pilot release of the Wolfram language and Mathematica. In fact, you’ll notice icons for both in the taskbar. Mathematica is the frontend interface for the Wolfram programming language. Together, they’re commonly used for complex computations in math, science, and engineering fields. To see what Wolfram and Mathematica are capable of, the Wolfram Language and System Documentation is a great place to start.
Leafpad is the default text editor, which is available under Raspberry menu→Accessories. For editing text files outside the desktop environment, you can use Nano, which is an easy-to-learn, bare-bones text editor. Vim is also installed; execute it with the command vi
. Other common Unix text editors like Emacs are not installed by default, but can be easily added (see “Installing New Software”).
Copy and paste functions work between applications pretty well, although you may find some oddball programs that aren’t consistent. If your mouse has a middle button, you can select text by highlighting it as you normally would (click and drag with the left mouse button) and paste it by pressing the middle button while you have the mouse cursor over the destination window.
A lot of tasks are going to require you to get to the command line and run commands there. The LXTerminal program provides access to the command line or shell. It can be launched from the taskbar icon or from the Raspberry menu→Accessories. The default shell on Raspbian is the Bourne-again shell (bash), which is very common on Linux systems. There’s also an alternative called dash. You can change shells via the program menu or with the chsh
command.
If it helps, you can think of using the command line as playing a text adventure game, but with the files and the filesystem in place of grues and mazes. If that metaphor doesn’t help you, don’t worry: all the commands and concepts in this section are standard Linux and are valuable to learn.
Before you start, open up the LXTerminal program (Figure 2-3). There are two tricks that make life much easier in the shell: autocomplete and command history. Often you will only need to type the first few characters of a command or filename, then hit Tab. The shell will attempt to autocomplete the string based on the files in the current directory or programs in commonly used directories (the shell will search for executable programs in places like /bin or /usr/bin/). If you hit the up arrow on the command line, you’ll be able to step back through your command history, which is useful if you mistyped a character in a long string of commands.
Table 2-1 shows some of the important directories in the filesystem. Most of these follow the Linux standard of where files should go; a couple are specific to the Raspberry Pi. The /sys directory is where you can access all of the hardware on the Raspberry Pi.
Directory | Description |
---|---|
/bin |
Programs and commands that all users can run |
/boot |
All the files needed at boot time |
/dev |
Special files that represent the devices on your system |
/etc |
Configuration files |
/etc/init.d |
Scripts to start up services |
/etc/X11 |
X11 configuration files |
/home |
User home directories |
/home/pi |
Home directory for Pi user |
/lib |
Kernel modules/drivers |
/media |
Mount points for removable media |
/proc |
Virtual directory with details about running processes and the OS |
/sbin |
Programs for system maintenance |
/sys |
A special directory on the Raspberry Pi that represents the hardware devices |
/tmp |
Space for programs to create temporary files |
/usr |
Programs and data usable by all users |
/usr/bin |
Most of the programs in the operating system reside here |
/usr/games |
Yes, games |
/usr/lib |
Libraries to support common programs |
/usr/local |
Software that may be specific to this machine goes here |
/usr/sbin |
More system administration programs |
/usr/share |
Supporting files that aren’t specific to any processor architecture |
/usr/src |
Linux is open source; here’s the source! |
/var |
System logs and spool files |
/var/backups |
Backup copies of all the most vital system files |
/var/cache |
Programs such as |
/var/log |
All of the system logs and individual service logs |
/var/mail |
All user email is stored here, if you’re set up to handle email |
/var/spool |
Data waiting to be processed (e.g., incoming email, print jobs) |
You’ll see your current directory displayed before the command prompt. In Linux, your home directory has a shorthand notation: the tilde (~
). When you open the LXTerminal, you’ll be dropped into your home directory, and your prompt will look like this:
pi@raspberrypi ~ $
Here’s an explanation of that prompt:
pi@raspberrypi ~ $
Your username, pi, followed by the at (@) symbol.
The name of your computer (raspberrypi is the default hostname).
The current working directory of the shell. You always start out in your home directory (~
).
This is the shell prompt. Any text you type will appear to the right of it. Press Enter or Return to execute each command you type.
pi@raspberrypi ~
portion of the prompt and just show you the $
in some examples, to keep them less cluttered.Use the cd
(change directory) command to move around the filesystem. The following two commands have the same effect (changing to the home directory) for the Pi user:
cd /home/pi/ cd ~
If the directory path starts with a forward slash, it will be interpreted as an absolute path to the directory. Otherwise, the directory will be considered relative to the current working directory. You can also use .
and ..
to refer to the current directory and the current directory’s parent. For example, to move up to the top of the filesystem:
pi@raspberrypi ~ $cd ..
pi@raspberrypi /home $cd ..
You could also get there with the absolute path /:
pi@raspberrypi ~ $ cd /
Once you’ve changed to a directory, use the ls
command to list the files there:
pi@raspberrypi / $ ls
bin dev home lost+found mnt proc run selinux sys usr
boot etc lib media opt root sbin srv tmp var
Most commands have additional parameters, or switches, that can be used to turn on different behaviors. For example, the -l
switch will produce a more detailed listing, showing file sizes, dates, and permissions:
pi@raspberrypi ~ $ ls -l
total 8
drwxr-xr-x 2 pi pi 4096 Oct 12 14:26 Desktop
drwxrwxr-x 2 pi pi 4096 Jul 20 14:07 python_games
The -a
switch will list all files, including invisible ones:
pi@raspberrypi ~ $ ls -la
total 80
drwxr-xr-x 11 pi pi 4096 Oct 12 14:26 .
drwxr-xr-x 3 root root 4096 Sep 18 07:48 ..
-rw------- 1 pi pi 25 Sep 18 09:22 .bash_history
-rw-r--r-- 1 pi pi 220 Sep 18 07:48 .bash_logout
-rw-r--r-- 1 pi pi 3243 Sep 18 07:48 .bashrc
drwxr-xr-x 6 pi pi 4096 Sep 19 01:19 .cache
drwxr-xr-x 9 pi pi 4096 Oct 12 12:57 .config
drwx------ 3 pi pi 4096 Sep 18 09:24 .dbus
drwxr-xr-x 2 pi pi 4096 Oct 12 14:26 Desktop
-rw-r--r-- 1 pi pi 36 Sep 18 09:35 .dmrc
drwx------ 2 pi pi 4096 Sep 18 09:24 .gvfs
drwxr-xr-x 2 pi pi 4096 Oct 12 12:53 .idlerc
-rw------- 1 pi pi 35 Sep 18 12:11 .lesshst
drwx------ 3 pi pi 4096 Sep 19 01:19 .local
-rw-r--r-- 1 pi pi 675 Sep 18 07:48 .profile
drwxrwxr-x 2 pi pi 4096 Jul 20 14:07 python_games
drwx------ 4 pi pi 4096 Oct 12 12:57 .thumbnails
-rw------- 1 pi pi 56 Sep 18 09:35 .Xauthority
-rw------- 1 pi pi 300 Oct 12 12:57 .xsession-errors
-rw------- 1 pi pi 1391 Sep 18 09:35 .xsession-errors.old
Use the mv
command to rename a file. The touch
command can be used to create an empty dummy file:
pi@raspberrypi ~ $touch foo
pi@raspberrypi ~ $ls
foo Desktop python_games pi@raspberrypi ~ $mv foo baz
pi@raspberrypi ~ $ls
baz Desktop python_games
Remove a file with rm
. To remove a directory, you can use rmdir
if the directory is empty, or rm -r
if it isn’t. The -r
is a parameter sent to the rm
command that indicates it should recursively delete everything in the directory.
rm -r
is a bit like nuclear war—it is easy to start, and it does great damage. Make sure you’re in exactly the right directory before you set it off.
If you want to find out all the parameters for a particular command, use the man
command (or you can often use the --help
option):
pi@raspberrypi ~ $man curl
pi@raspberrypi ~ $rm --help
To create a new directory, use mkdir
. To bundle all of the files in a directory into a single file, use the tar
command, originally created for tape archives. You’ll find a lot of bundles of files or source code are distributed as tar files, and they’re usually also compressed using the gzip
command. Try this:
pi@raspberrypi ~ $mkdir myDir
pi@raspberrypi ~ $cd myDir
pi@raspberrypi ~ $touch foo bar baz
pi@raspberrypi ~ $cd ..
pi@raspberrypi ~ $tar -cf myDir.tar myDir
pi@raspberrypi ~ $gzip myDir.tar
You’ll now have a .tar.gz archive of that directory that can be distributed via email or the Internet.
One of the reasons that Linux (and Unix) is so successful is that the main design goal was to build a very complicated system out of small, simple modular parts that can be chained together. You’ll need to know a little bit about two pieces of this puzzle: pipes and redirection.
Pipes are simply a way of chaining two programs together so the output of one can serve as the input to another. All Linux programs can read data from standard input (often referred to as stdin), write data to standard output (stdout), and throw error messages to standard error (stderr). A pipe lets you hook up stdout from one program to stdin of another (Figure 2-4). Use the |
operator, as in this example:
pi@raspberrypi ~ $ ls -la | less
(Press q to exit the less
program.)
Now (for something a little more out there) try:
pi@raspberrypi ~ $ sudo cat /boot/kernel.img | aplay
You may want to turn the volume down a bit first; this command reads the kernel image and spits all of the 1s and 0s at the audio player. That’s what your kernel sounds like!
In some of the examples later in the book, we’ll also be using redirection, where a command is executed and the stdout output can be sent to a file. As you’ll see later, many things in Linux are treated as ordinary files (such as the Pi’s general-purpose input/output pins), so redirection can be quite handy. To redirect output from a program, use the >
operator:
pi@raspberrypi ~ $ ls > directoryListing.txt
Sometimes you’ll want to display the contents of a file on the screen. If it’s a text file and you want to read it one screen at a time, use less
:
pi@raspberrypi ~ $ls > flob.txt
pi@raspberrypi ~ $less flob.txt
If you want to just dump the entire contents of a file to standard output, use cat
(short for concatenate). This can be handy when you want to feed a file into another program or redirect it somewhere.
For example, this is the equivalent of copying one file to another with a new name (the second line concatenates the two files first):
pi@raspberrypi ~ $ls > wibble.txt
pi@raspberrypi ~ $cat wibble.txt > wobble.txt
pi@raspberrypi ~ $cat wibble.txt wobble.txt > wubble.txt
To look at just the last few lines of a file (such as the most recent entry in a log file), use tail
(to see the beginning, use head
). If you are searching for a string in one or more files, use the venerable program grep
:
pi@raspberrypi ~ $ grep Puzzle */*
grep
is a powerful tool because of the rich language of regular expressions that was developed for it. Regular expressions can be a bit difficult to read, and may be a major factor in whatever reputation Linux has for being opaque to newcomers.
Every program on the Pi runs as a separate process; at any particular point, you’ll have dozens of processes running. When you first boot up, about 75 processes will start, each one handling a different task or service. To see all these processes, run the top
program, which will also display CPU and memory usage. top
will show you the processes using the most resources; use the ps
command to list all the processes and their ID numbers (see Figure 2-5).
Try:
pi@raspberrypi ~ $ ps aux | less
Sometimes you may want to kill a rogue or unresponsive process. To do that, use ps
to find its ID, then use kill
to stop it:
pi@raspberrypi ~ $ kill 95689
In the case of some system processes, you won’t have permission to kill it (though you can circumvent this with sudo
, covered in the next section).
Linux is a multiuser operating system; the general rule is that everyone owns their own files and can create, modify, and delete them within their own space on the filesystem. The root (or super) user can change any file in the filesystem, which is why it is good practice to not log in as root on a day-to-day basis.
There are some tools like sudo
(“su
peruser do
”) which allow users to act like superusers for performing tasks, like installing software without the dangers (and responsibilities) of being logged in as root. You’ll be using sudo
a lot when interacting with hardware directly, or when changing system-wide configurations.
As the pi
user, there’s not much damage you can do to the system. As superuser, you can wreak havoc, accidentally or by design. Be careful when using sudo
, especially when moving or deleting files. Of course, if things go badly, you can always make a new SD card image (see Appendix A).
Each file belongs to one user and one group. Use chown
and chgrp
to change the file’s owner or group. You must be root to use either:
pi@raspberrypi ~ $sudo chown pi garply.txt
pi@raspberrypi ~ $sudo chgrp staff plugh.txt
Each file also has a set of permissions that show whether a file can be read, written, or executed. These permissions can be set for the owner of the file, the group, or for everyone (see Figure 2-6).
You set the individual permissions with the chmod
command. The switches for chmod
are summarized in Table 2-2.
u |
User |
g |
Group |
o |
Others not in the group |
a |
All/everyone |
r |
Read permission |
w |
Write permission |
x |
Execute permission |
+ |
Add permission |
- |
Remove permission |
Here are a few examples of how you can combine these switches:
chmod u+rwx,g-rwx,o-rwx wibble.txt chmod g+wx wobble.txt chmod -rw,+r wubble.txt
Allow only the user to read, write, and execute.
Add permission to write and execute to entire group.
Make read-only for everyone.
The only thing protecting your user space and files from other people is your password, so you better choose a strong one. Use the passwd
command to change it, especially if you’re putting your Pi on a network.
Once you’re on a network, there are a number of Linux utilities that you’ll be using on a regular basis. When you’re troubleshooting an Internet connection, use ifconfig
, which displays all of your network interfaces and the IP addresses associated with them (see Figure 2-7).
The ping
command is actually the most basic tool for troubleshooting network connections. You can use ping (think sonar) to test whether there is a two-way connection between two IP addresses on the network or Internet. Note that many websites block ping traffic, so you may need to ping multiple sites to accurately test a connection:
ping yahoo.com ping altavista.com ping lycos.com ping netscape.com
To log in to another computer remotely (and securely, with encrypted passwords), you can use the Secure Shell (SSH). The computer on the remote side needs to be running an SSH server for this to work, but the SSH client comes built into Raspbian. In fact, this is a great way to work on your Raspberry Pi without a monitor or keyboard, as discussed in “Running Headless”.
Related to SSH is the sftp
program, which allows you to securely transfer files from one computer to another. Rounding out the set is scp
, which you can use to copy files from one computer to another over a network or the Internet. The key to all of these tools is that they use the Secure Sockets Layer (SSL) to transfer files with encrypted login information. These tools are all standard stalwart Linux tools.
The /etc directory holds all of the system-wide configuration files and startup scripts. When you ran the configuration scripts the first time you started up, you were changing values in various files in the /etc directory. You’ll need to invoke superuser powers with sudo
to edit files in /etc; if you come across some tutorial that tells you to edit a configuration file, use a text editor to edit and launch it with sudo
:
pi@raspberrypi ~ $ sudo nano /etc/hosts
A typical laptop or desktop will have additional hardware and a backup battery (usually a coin cell) to save the current time and date. The Raspberry Pi does not, but Raspbian is configured to automatically synchronize its time and date with a Network Time Protocol (NTP) server when plugged into a network.
Having the correct time can be important for some applications (see the example in Chapter 6 using cron
to control a lamp). To set the time and date manually, use the date
program:
$ sudo date --set="Sun Nov 18 1:55:16 EDT 2012"
If the time was set automatically via the Internet with NTP, you may want to update your timezone. In order to do this, go to the Internationalisation Settings within the raspi-config
utility (see “Booting Up”).
One of the areas where Linux completely trounces other operating systems is in software package management. Package managers handle the downloading and installation of software, and they automatically handle downloading and installing dependencies. Keeping with the modular approach, many software packages on Linux depend on other pieces of software. The package manager keeps it all straight, and the package managers on Linux are remarkably robust.
Raspbian comes with a pretty minimal set of software, so you will soon want to start downloading and installing new programs. The examples in this book will all use the command line for this, because it is the most flexible and quickest way of installing software.
The program apt-get
with the install
option is used to download software. apt-get
will even download any extra software or libraries required so you don’t have to go hunting around for dependencies. Software has to be installed with superuser permissions, so always use sudo
(this command installs the Emacs text editor):
pi@raspberrypi ~ $ sudo apt-get install emacs
One of the first things we needed to figure out when writing this book was how to take screenshots on the Pi. Pre-installed in Raspbian is a program called scrot (an abbreviation for SCReenshOT). To take a screenshot, just type scrot
on the command line. A .png file will be saved in your home directory. Scrot is powerful and has a lot of command-line options; type scrot -h
to see a short guide on how to use it. Another way to invoke scrot is to press your keyboard’s Print Screen button.
Raspberry Pi has the built-in capability to play sound. This makes it a popular platform for DIY projects that play sound effects or stream music from the Internet. Raspbian uses the Advanced Linux Sound Architecture, or ALSA, for low-level control of audio devices. You can test it with a pre-loaded sound file and the sound playback utility aplay
:
$ aplay /usr/share/scratch/Media/Sounds/Human/PartyNoise.wav
If you’re using an HDMI display, by default, the sound will be played through HDMI. To force your Raspberry Pi to play through the analog output jack, run sudo raspi-config
and choose Advanced Options→Audio→Force 3.5mm (headphone) jack.
To adjust the volume output, run alsamixer
and use your arrow keys to change the gain.
You’re not limited to the onboard sound; you can also add USB audio devices. Many USB audio devices even have an audio input so that you can use your Raspberry Pi to record audio in addition to playing it.
To enable a USB audio device, you’ll need to edit the ALSA configuration file:
$ sudo nano /etc/modprobe.d/alsa-base.conf
Find the line that says:
options snd-usb-audio index=-2
And change it to:
options snd-usb-audio index=0
Save the file and reboot your Raspberry Pi. Now your Raspberry Pi will default to using the USB audio device.
Some of your Raspberry Pi’s firmware is stored on the SD card and includes much of the low-level instructions that need to be executed before the boot process is handed over to your operating system.
While it’s typically not necessary, if you run into some strange behavior, you may want try to update the firmware on your SD card. With an Internet-connected Raspberry Pi, this is very easy to do:
$ sudo rpi-update
If you’d like to see what’s being updated when you run the utility, you can review the latest changes in Raspberry Pi’s firmware repository on GitHub.
To view what version of the Raspberry Pi firmware you currently have, run vcgencmd version
:
$ vcgencmd version
Sep 23 2015 12:12:01
Copyright (c) 2012 Broadcom
version c156d00b148c30a3ba28ec376c9c01e95a77d6d5 (clean) (release)
There’s much more to Linux and many places to continue learning about it. Some good starting points are:
Handy as a quick reference.
More detailed, but still a quick reference guide.
Raspbian is based on Debian, so a lot of the info on the Debian wiki applies to Raspbian as well.
Also published as the New Hacker’s Dictionary, this collection of definitions and stories is required reading on the Unix/Linux subculture.