A Linux Primer - Raspberry Pi: A Quick-Start Guide, 2nd Edition (2014)

Raspberry Pi: A Quick-Start Guide, 2nd Edition (2014)

Appendix 1. A Linux Primer

The most popular operating system for the Pi is Linux, especially the Debian Linux distribution (Raspbian). If you’ve worked exclusively with operating systems such as Windows or Mac OS X until now, Linux might produce a little culture shock for you, mainly because on Linux systems the graphical user interface (GUI) is optional. That means you can run a Linux system without using a mouse and without double-clicking colorful icons.

Still, you need a way to interact with the system, and on Linux you have to use a shell for this purpose. A shell is a program that awaits your commands via a keyboard and passes them to the operating system (Linux). After Linux has run the command, the shell passes the results back to you.

The shell itself runs in a terminal that goes back to the very beginnings of computing. In these times, you had to connect to the “real” computers using a more or less dumb terminal that basically forwarded your inputs and displayed the computer’s outputs. Today you no longer have to use explicit terminal devices, but the metaphor still lives on, and Linux depends on it.

So, whenever you log into a Linux computer, it usually starts a shell in a terminal session. In the shell, you can invoke commands that actually run on the Linux computer.

Nowadays, Linux comes with graphical desktop systems that are very similar to Windows and Mac OS X. Still, these desktop systems ship with a terminal emulator you can use to invoke commands directly. The LXDE desktop, for example, comes with a desktop emulator named LXTerminal. You can find a shortcut on the LXDE desktop, so double-click it, and the Pi will start a new terminal session.

A First Encounter

After you’ve logged into the Pi or after you’ve started a new terminal from the desktop, you’ll see the following prompt awaiting your commands:


pi@raspberrypi ~ $

It doesn’t look like much, but it already gives you a lot of information. For example, the first part (pi@raspberrypi) tells you that the host name of your computer is raspberrypi. It also tells you that your username is pi. This is an important piece of information, because Linux is a multiuser operating system. This means that multiple people can work on the same computer simultaneously (over a network, for example). Also, you can switch to another user account whenever you need to, so it’s good to know who you are at the moment.

The next part of the prompt contains the file system path you’re currently in. Here it consists only of the tilde character (~), which is an abbreviation for the user’s home directory. Every Linux user has a home directory for storing personal data and configuration files. It’s similar to the My Documents folder on Windows or the Documents folder on Mac OS X. The dollar sign at the end marks the end of the prompt.

To see the content of the current directory, type ls and press the Enter key to run the ls command.


pi@raspberrypi ~ $ ls

Desktop python_games

The current directory contains two items named Desktop and python_games. From only their names, you cannot tell whether they are regular files or directories. Fortunately, you can control the behavior of most Linux commands using command options. Usually, these options consist of only a single letter preceded by a dash. The ls command supports many options, and when you pass it the -l (short for “long”) option, it displays more information about the files in the current directory.


pi@raspberrypi ~ $ ls -l

total 8

drwxr-xr-x 2 pi pi 4096 Jul 15 19:36 Desktop

drwxr-xr-x 2 pi pi 4096 Jul 15 19:36 python_games

At first it looks a bit scary, but it’s really easy to understand. For every item in the directory, ls displays the information shown in the following figure.


Figure 59. The different components of ls output

The file mode contains the file type and its permissions. If the first character is a dash, the file is a regular file. If it contains a d, it is a directory. So, both Desktop and python_games are directories.

The following nine characters encode the file permissions of three groups of people: the owner, the group, and others. The first three characters are rwx, and they mean that the owner of the file is allowed to read (r), write (w), and execute (x) it. In case of a directory, execute means to enter the directory.

Every file on a Linux system belongs to a user and to a group. Groups help to build teams who work together on the same resources. So, for every file, Linux stores permissions for the group, too. In the current case, they are r-x, which means that group members can read and execute the file but aren’t allowed to change it.

Finally, Linux stores permissions for other users who are not a file’s owner and who don’t belong to the file’s group. Again, r-x means that other users might read and execute the file but aren’t allowed to change it.

The next information ls outputs is the number of links to a file. For your first tour through Linux, you can safely ignore it.

Then you can find the name of the file’s owner and its group. In this case, both are pi; that is, on the current Linux system, there’s both a user named pi and a group named pi.

Next you can find the file size. On Linux, directories are files too, and they simply contain the names of the files stored in the directory. By default, Linux allocates some space for this list of files up front, and in case of Debian on the Raspberry Pi, it’s 4,096 bytes.

To the right of the file size, you can see the date the file was modified for the last time. And, finally, ls outputs the file’s name.

Navigate Through the File System

The pwd (print working directory) command outputs the directory you’re currently in.


pi@raspberrypi ~ $ pwd


As you can see, your home directory (~) expands to the absolute path /home/pi. Linux distinguishes between absolute and relative paths. Absolute paths always begin with a forward slash (/) and reference the same file no matter where you are in the file system. Relative paths, on the contrary, are relative to your current position in the file system. The following example will clarify the difference between absolute and relative paths.

As you saw in the preceding section, the pi user’s home directory contains two directories named Desktop and python_games.


pi@raspberrypi ~ $ ls

Desktop python_games

Using the cd (change directory) command, you can change the current directory to another one.


pi@raspberrypi ~ $ cd Desktop/

pi@raspberrypi ~/Desktop $

Now your current working directory has changed. You can see that your prompt has changed, and you can also check it using the pwd command.


pi@raspberrypi ~/Desktop $ pwd


To go back to the pi user’s home directory, you have a couple of options. First, you can invoke cd with the absolute path to the home directory.


pi@raspberrypi ~/Desktop $ cd /home/pi

pi@raspberrypi ~ $

Alternatively, you can use a relative path like this:


pi@raspberrypi ~/Desktop $ cd ..

pi@raspberrypi ~ $ pwd


The abbreviation .. stands for the parent directory of the current directory. In the previous command, your current working directory is /home/pi/Desktop. When you run cd .., you change the working directory to the parent directory of Desktop, which is /home/pi.

You can move to your user’s home directory from any location in the file system by running cd with no arguments:


pi@raspberrypi ~/Desktop $ cd

pi@raspberrypi ~ $ pwd


This command is really useful, so it’s worth remembering.

Edit Text Files

Many Linux tools depend on configuration files. Most of these files are regular text files, and you have to edit them from time to time. On Linux, you’ll find many powerful text editors for the terminal. If you’re used to graphical text editors, most Linux text editors look a bit awkward at first. One of the easiest and most intuitive editors is nano. It permanently displays shortcuts to its most important commands so you don’t have to remember them. The following command starts nano and creates an empty text file named hello.txt:


pi@raspberrypi ~ $ nano hello.txt

In the following figure, you can see how nano looks in your terminal.


Figure 60. nano on the terminal

You can use most of the screen for editing the text, so type in a few words and move the cursor around using the cursor keys. At the bottom of the screen, you’ll see the most important nano commands. To invoke them, you have to press the Ctrl key and the letter belonging to the command. (The ^ character is an abbreviation for the Ctrl key.) For example, you can exit nano by pressing Ctrl+X.

When you do this, nano doesn’t simply discard your changes and exit. It asks you whether you’d like to save your changes (see the following figure).


Figure 61. Saving a file with the nano text editor

Enter y if you’d like to save your changes and n otherwise. If you pressed y, you aren’t finished yet, because nano asks you to confirm the filename (see the following figure).


Figure 62. nano always asks you to confirm the filename.

Usually, you’ll just press Enter to confirm the current filename, and nano will save the file. At the bottom of the screen, you can see some useful options allowing you to store the file in different formats, for example.

If you’re going to work with Linux more often, you should get familiar with one of its text editors. For beginners, nano is an excellent choice, so play around with it for at least a few minutes.

Manage Users

Linux is a multiuser operating system—you can work with several different users on the same computer at the same time. In this book, you’ll mainly use the user pi, because it comes with the Raspbian image automatically. This is convenient, but sometimes it’s handy to create different users for different tasks. Also, pi is a very powerful user that has full administrative rights and can change nearly every aspect of the system. You don’t want to grant all privileges to all users. It’s always a good idea to work with only the administrative rights you need to get the job done. That way, you can’t harm the system by accident.

Adding a new user to Linux is easy using the adduser command.


pi@raspberrypi ~ $ sudo adduser maik



Adding user `maik' ...

Adding new group `maik' (1002) ...

Adding new user `maik' (1002) with group `maik' ...

Creating home directory `/home/maik' ...ß

Copying files from `/etc/skel' ...

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

Changing the user information for maik

Enter the new value, or press ENTER for the default


Full Name []: Maik Schmidt


Room Number []:

Work Phone []:

Home Phone []:

Other []:


Is the information correct? [Y/n] Y

You have to provide only a username (by convention it should contain only lowercase letters), a password, and a few optional attributes, such as your full name. After you’ve confirmed that all information is correct, Linux will create a new user with its own home directory. The next time you boot the Pi, you can use it to log into the system. If you’re impatient, you can use the su (substitute user identity) command to switch to the new user.


pi@raspberrypi ~ $ su - maik


maik@raspberrypi ~ $ pwd


maik@raspberrypi ~ $ startx

su asks for the user’s password, and if it’s correct, it switches to the new user. The pwd command prints the current working directory; in this case, it’s the home directory of the newly created user. If you start the LXDE desktop with the startx command, it greets you with the standard LXDE background image (see the following figure), because in contrast to the pi user, the new user starts with the desktop’s defaults.


Figure 63. The default look of LXDE

When working with the pi user, you’ve often used sudo to run commands with administrative privileges. See what happens if you try to delete a file that you don’t own using the rm (remove file) command.


maik@raspberrypi ~ $ sudo rm /boot/config.txt

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.

#2) Think before you type.

#3) With great power comes great responsibility.

[sudo] password for maik:

maik is not in the sudoers file. This incident will be reported.

The command prints a warning and then asks for your password. Obviously, the new user isn’t allowed to delete files in the /boot directory, so Linux refuses to invoke the rm command.

While it’s a good default behavior to deny new users access to dangerous operations, sometimes users need more privileges. If you want to give your new users the same rights as the pi user, you have to add the user to the sudoers file. This file contains a list of all users who are allowed to run the sudo command, and it specifies which operations the users are allowed to perform. You can’t edit the sudoers file directly; you have to use the visudo command, which invokes the text editor vi by default. If you want to edit the file using a different text editor, such as nano, you have to specify it on the command line. (Make sure you’re using the pi user again.)


pi@raspberrypi ~ $ sudo EDITOR=nano visudo

This opens the /etc/sudoers file using the nano text editor. In the file, you’ll find a section that looks like this:


# User privilege specification

root ALL=(ALL) ALL

suse ALL=(ALL) ALL


Add a new line that looks exactly like one of the previous three lines, but replace the username with the name of your new user. If you’re using nano to edit the file, press Ctrl+X and confirm that you’d like to save the changes. Then confirm the filename, and you’re finished—your new user now has the same rights as the original pi user.

If you no longer need a certain user, it’s reasonable to delete it.


pi@raspberrypi ~ $ sudo userdel maik

The previous command will delete the user’s account but not the user’s files. The user can no longer log into the system, but all the files he or she has created in the home directory are still available. If you want to delete the files as well, run the following:


pi@raspberrypi ~ $ sudo userdel -r maik

If you ever need to change a user’s attributes, such as his or her home directory, you can use the usermod command. You can use it to lock or unlock accounts, for example.


pi@raspberrypi ~ $ sudo usermod -L maik

This will lock the account of the user named maik. The user can no longer log into the system. To unlock the account, run the following command:


pi@raspberrypi ~ $ sudo usermod -U maik

You can read usermod’s manual page (and the manual page of every other Linux command) using the man command.


pi@raspberrypi ~ $ man usermod

This displays the command’s manual pages. You can stop the man command by pressing Q.

One important action is changing a user’s password. For this, you can use the passwd command.


pi@raspberrypi ~ $ passwd maik

Changing password for maik.

Old Password:

New Password:

Retype New Password:

passwd asks for the current password and then for the new password. If everything is OK, it prints no message, and your user has a new password.

Manage Processes

Whenever you run a command or an application on a Linux system, the operating system’s kernel spawns a new process. You can list your current processes using the ps command.


pi@raspberrypi ~ $ ps


1880 pts/2 00:00:00 bash

1892 pts/2 00:00:00 ps

At the moment, you own only two processes. The first has the process ID (PID) 1880, and it belongs to a command named bash. (The process IDs on your system will vary.) This is the process that belongs to the shell in which you’re currently working. The process with the PID 1892 belongs to the ps command you’ve used to list your current processes. At the moment, you see the output of the ps command; process 1892 will be gone already. To see the effect, run ps again.


pi@raspberrypi ~ $ ps


1880 pts/2 00:00:00 bash

1894 pts/2 00:00:00 ps

As you can see, the shell still has the PID 1880, but your latest call to ps was handled by a new process with PID 1894.

You can get more information about your processes using the -f option.


pi@raspberrypi ~ $ ps -f


pi 1880 1879 0 12:51 pts/2 00:00:00 -bash

pi 1895 1880 0 12:58 pts/2 00:00:00 ps -f

Now you can see the user ID (UID) of the user who has spawned a certain process. Not surprisingly, the UID is pi for all of your processes. In addition to the PID, you can see the parent process ID (PPID). This is the ID of the process that has created another process. For example, the ps -fcommand you’ve run before has the PPID 1880. This is the PID of the shell you’re using. So, the shell is the parent of the process created by the ps -f command.

To see all information about all processes currently running on your Pi, run the following command:


pi@raspberrypi ~ $ ps -ef

This will output a fairly long list of processes that contains every single Linux service your Pi has started.

Getting a list of all active processes is useful, but most often you’ll be looking for a certain process for a reason. Perhaps the process uses too many resources or takes too long, and you’d like to terminate it. But how can you terminate a process?

Terminating a long-running process is easy when you start it directly from the shell. To demonstrate, the following command searches for all text files on your SD card, so it will take a long time to finish:


pi@raspberrypi ~ $ find / -name '*.txt'

While the process is running, you can terminate it by pressing Ctrl+C on your keyboard. When you press Ctrl+C, the shell recognizes your keypress and sends a signal to the process that is currently running. Signals are small messages that all processes listen for in the background. Pressing Ctrl+C generates a signal named SIGINT that tells a process it got interrupted. When a process receives a SIGINT signal, it usually cleans up and terminates.

For processes that are still running in your terminal, pressing Ctrl+C is a good option, but what if you need to terminate a process that is running in the background? For example, most Linux services run in the background by default, and you don’t start them yourself. In this case, you have to find out the PID of the process and pass it to the kill command.


pi@raspberrypi ~ $ kill 4711

The previous command sends the SIGTERM command to the process with the ID 4711. You can send other signals with the kill command, too. For example, the following command will terminate the process with the PID 4711 in any case:


pi@raspberrypi ~ $ kill -KILL 4711

Of course, you need to have the permission to terminate a process. Usually you are allowed to kill only your own processes.

Shut Down and Reboot the Pi

When you’re finished with your work, don’t simply switch off the Pi. It might result in the loss of data. Always shut it down using the following command:


pi@raspberrypi ~ $ sudo halt

If you need to reboot the Pi, use the following command:


pi@raspberrypi ~ $ sudo reboot

Get Help

Since their beginnings, the Unix/Linux operating systems came with a great manual system named man pages. Whenever you need to look up the options of a certain command, you can display its manual using the man command. To look up all options of the ls command, for example, run the following command:


pi@raspberrypi ~ $ man ls

To scroll down a line, press the down cursor key. Press the up cursor key to scroll up a line. To scroll down a page, press the spacebar. Press Ctrl+B to scroll up a page. To leave the program, press Q.

The man command has many more options. To learn more about it, run the following:


pi@raspberrypi ~ $ man man