Table Of ContentLearn Raspberry Pi
with Linux
Peter Membrey
David Hows
Apress
Learn Raspberry Pi with Linux
Copyright © 2013 by Peter Membrey and David Hows
Th is work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
is concerned, specifi cally the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfi lms or in any other physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material
supplied specifi cally for the purpose of being entered and executed on a computer system, for exclusive use by the
purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the
Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from
Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are
liable to prosecution under the respective Copyright Law.
ISBN 978-1-4302-4821-7
ISBN 978-1-4302-4822-4 (eBook)
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion
and to the benefi t of the trademark owner, with no intention of infringement of the trademark.
Th e use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identifi ed
as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither
the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may
be made. Th e publisher makes no warranty, express or implied, with respect to the material contained herein.
President and Publisher: Paul Manning
Lead Editor: Michelle Lowman
Developmental Editor: Matthew Moodie
Technical Reviewer: Stewart Watkiss
Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel,
Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew
Moodie, Jeff Olson, Jeff rey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan
Spearing, Matt Wade, Tom Welsh
Coordinating Editor: Jill Balzano
Copy Editor: Nancy Sixsmith
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected] , or visit
www.springeronline.com . Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science +
Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected] , or visit w ww.apress.com .
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook
Licensing web page at www.apress.com/bulk-sales .
Any source code or other supplementary materials referenced by the author in this text is available to readers
at www.apress.com . For detailed information about how to locate your book’s source code, go to
www.apress.com/source-code/ .
To my parents, Peter and Bev, for all their support.
— David Hows
For my wife Sarah, son Kaydyn, and my close friends Jacqui Koo and Mark Stephens,
who together make my world go ‘round.
— Peter Membrey
Contents at a Glance
About the Authors ..............................................................................................................xv
About the Technical Reviewer ..........................................................................................xvii
Acknowledgments .............................................................................................................xix
Introduction .......................................................................................................................xxi
■ Chapter 1: Your First Bite of Raspberry Pi .........................................................................1
■ Chapter 2: Surveying the Landscape ...............................................................................31
■ Chapter 3: Getting Comfortable .......................................................................................53
■ Chapter 4: The File-Paths to Success ..............................................................................69
■ Chapter 5: Essential Commands ......................................................................................89
■ Chapter 6: Editing Files on the Command Line ..............................................................109
■ Chapter 7: Managing Your Pi .........................................................................................129
■ Chapter 8: A LAMP of Your Own ....................................................................................149
■ Chapter 9: WiPi: Wireless Computing ............................................................................181
■ Chapter 10: The Raspberry sPi ......................................................................................199
■ Chapter 11: Pi Media Center ..........................................................................................223
Index .................................................................................................................................243
v
Contents
About the Authors ..............................................................................................................xv
About the Technical Reviewer ..........................................................................................xvii
Acknowledgments .............................................................................................................xix
Introduction .......................................................................................................................xxi
■ Chapter 1: Your First Bite of Raspberry Pi .........................................................................1
Your Freshly Baked Pi Arrives ........................................................................................................1
List of Ingredients .........................................................................................................................3
Micro USB lead ......................................................................................................................................................4
USB Power Adapter ...............................................................................................................................................4
HDMI Lead .............................................................................................................................................................5
HDMI Capable Display ...........................................................................................................................................5
SD Card ..................................................................................................................................................................5
SD Card Reader .....................................................................................................................................................6
USB Keyboard and Mouse .....................................................................................................................................7
Whew, We’re Done! ................................................................................................................................................7
Don’t Panic! ...........................................................................................................................................................8
Linux ..............................................................................................................................................8
What Is Linux? .......................................................................................................................................................9
Downloading Raspbian ........................................................................................................................................10
Getting Raspbian onto Your SD Card ...........................................................................................10
Using Image Writer on Windows ..........................................................................................................................11
Using dd on the Mac ............................................................................................................................................14
First Boot .....................................................................................................................................17
vii
■ CONTENTS
Confi guring Your Pi ......................................................................................................................18
Expanding the Filesystem ....................................................................................................................................19
Confi guring the Keyboard ....................................................................................................................................20
Changing the Password .......................................................................................................................................23
Confi guring the Locale .........................................................................................................................................24
Changing the Time Zone ......................................................................................................................................26
Allocating Memory ...............................................................................................................................................27
At Last! It’s Confi gured! .......................................................................................................................................28
Summary .....................................................................................................................................30
■ Chapter 2: Surveying the Landscape ...............................................................................31
Welcome to LXDE ........................................................................................................................31
What Do We Have Here? ......................................................................................................................................32
The Start Menu and Bottom Left of the Task Bar .................................................................................................33
And on the Right Side… ......................................................................................................................................33
The Start Menu ............................................................................................................................34
Accessories .........................................................................................................................................................36
Education .............................................................................................................................................................43
Internet ................................................................................................................................................................46
Programming .......................................................................................................................................................47
System Tools ........................................................................................................................................................49
Preferences .........................................................................................................................................................50
Summary .....................................................................................................................................52
■ Chapter 3: Getting Comfortable .......................................................................................53
Ye Olde Computer ........................................................................................................................54
Say Hello to the Dumb Terminal ..........................................................................................................................54
Modern Terminals ................................................................................................................................................55
Why Do We Still Care About These Things? .........................................................................................................56
Three Terminals ...........................................................................................................................58
Console ................................................................................................................................................................58
Opening a Virtual Terminal in the GUI ..................................................................................................................59
Connecting via SSH .............................................................................................................................................59
viii
■ CONTENTS
Welcome to the Command Line ..................................................................................................66
Different Shells ............................................................................................................................68
Summary .....................................................................................................................................68
■ Chapter 4: The File-Paths to Success ..............................................................................69
What Is a Filing System? .............................................................................................................69
More than One Filesystem ...................................................................................................................................70
Separate Roots ....................................................................................................................................................70
Unifi ed Filesystem ...............................................................................................................................................71
The Mac has to be Different ................................................................................................................................71
Bring it All Together .............................................................................................................................................71
Everything as a File .....................................................................................................................72
Filesystem Layout .......................................................................................................................73
/ (Root Directory) ................................................................................................................................................73
/root .....................................................................................................................................................................73
/etc ......................................................................................................................................................................73
/proc ....................................................................................................................................................................73
/var ......................................................................................................................................................................74
/boot ....................................................................................................................................................................74
/bin and /sbin ......................................................................................................................................................74
/dev ......................................................................................................................................................................74
/home ..................................................................................................................................................................74
/lib .......................................................................................................................................................................75
/lost+found ..........................................................................................................................................................75
/mnt .....................................................................................................................................................................75
/media .................................................................................................................................................................75
/usr ......................................................................................................................................................................75
/opt ......................................................................................................................................................................75
/srv ......................................................................................................................................................................75
/sys ......................................................................................................................................................................76
/tmp .....................................................................................................................................................................76
Wrapping it Up .....................................................................................................................................................76
ix
■ CONTENTS
Putting it to Work .........................................................................................................................76
Where Are We? Using pwd ...................................................................................................................................76
What’s in Here with Us? Using ls .........................................................................................................................77
Creating Files to Play with: Using Touch ..............................................................................................................77
Somewhere to Store our Files: Using mkdir ........................................................................................................78
Making Use of a New Directory: Using the mv Command ...................................................................................79
Time for Some Cloning: How to Use the cp Command ........................................................................................79
The Power to Destroy: Using the rm Command ...................................................................................................80
Fully Qualifi ed and Relative Paths .......................................................................................................................81
Users and Groups ........................................................................................................................81
File Permissions ..........................................................................................................................82
Setting File Permissions ......................................................................................................................................84
Shortcuts and Links ....................................................................................................................87
Summary .....................................................................................................................................88
■ Chapter 5: Essential Commands ......................................................................................89
Become the Boss ........................................................................................................................89
RTFM ...........................................................................................................................................91
System Resource Monitoring ......................................................................................................91
Uptime and Load Average ....................................................................................................................................92
Tasks....................................................................................................................................................................93
CPU Utilization Percentage ..................................................................................................................................93
Memory Usage .....................................................................................................................................................94
Process Table .......................................................................................................................................................94
Using free to View Memory Allocation .................................................................................................................95
Disk Usage ...........................................................................................................................................................95
Managing Processes ...........................................................................................................................................97
File Commands ..........................................................................................................................100
Combining Commands.......................................................................................................................................102
User Environment ..............................................................................................................................................104
The cron Command ...........................................................................................................................................107
Summary ...................................................................................................................................107
x
■ CONTENTS
■ Chapter 6: Editing Files on the Command Line ..............................................................109
What is a Text File? ...................................................................................................................109
The Contenders .........................................................................................................................110
Starting out with nano ...............................................................................................................111
So What Does This All Mean? ............................................................................................................................112
Saving Your File .................................................................................................................................................113
Moving Around in nano ......................................................................................................................................115
Wrapping Up nano .............................................................................................................................................117
Getting Started with vim ...........................................................................................................117
The vim Modes ..................................................................................................................................................118
Saving Your Changes .........................................................................................................................................119
Getting Out of vim ..............................................................................................................................................120
Searching in vim ................................................................................................................................................121
Moving to a Specifi c Line Number .....................................................................................................................122
Moving About in vim ..........................................................................................................................................123
Deleting in vim ...................................................................................................................................................123
Miscellaneous Little Commands ........................................................................................................................126
Visual Mode .......................................................................................................................................................126
Indenting Text ....................................................................................................................................................126
Summary ...................................................................................................................................128
■ Chapter 7: Managing Your Pi .........................................................................................129
Remotely Accessing the Pi ........................................................................................................129
Networking ........................................................................................................................................................129
DNS ....................................................................................................................................................................130
SSH ....................................................................................................................................................................130
BASH: Basic Coding ...................................................................................................................130
What Is BASH? ...................................................................................................................................................131
Starting in BASH ................................................................................................................................................131
Interpreted versus Compiled .............................................................................................................................132
Output in BASH ..................................................................................................................................................132
xi