B R I A N W A R D W H A T E V E R Y S U P E R U S E R S H O U L D K N O W H O W L I N U X W O R K S 2 N D E D I T I O N SHELVE IN: OPERATING SYSTEM S/LINUX $39.95 ($41.95 CDN) L I N U X F O R T H E S U P E R U S E R Unlike some operating systems, Linux doesn’t try to hide the important bits from you—it gives you full control of your computer. But to truly master Linux, you need to understand its internals, like how the system boots, how networking works, and what the kernel actually does. In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you’ll find the kind of knowledge that normally comes from years of experience doing things the hard way. You’ll learn: • How Linux boots, from boot loaders to init implementa- tions (systemd, Upstart, and System V) • How the kernel manages devices, device drivers, and processes • How networking, interfaces, firewalls, and servers work • How development tools work and relate to shared libraries • How to write effective shell scripts You’ll also explore the kernel and examine key system tasks inside user space, including system calls, input and output, and filesystems. With its combination of background, theory, real-world examples, and patient explanations, How Linux Works will teach you what you need to know to solve pesky problems and take control of your operating system. A B O U T T H E A U T H O R Brian Ward has been working with Linux since 1993. He is the author of the Linux Kernel-HOWTO, The Book of VMware (No Starch Press), and The Linux Problem Solver (No Starch Press). www.nostarch.com TH E F I N EST I N G E E K E NTE RTA I N M E NT™ W A R D H O W L IN U X W O R K S H O W L IN U X W O R K S 2 N D E D I T I O N
Praise for the first edition of how linux works “A great resource. In roughly 350 pages, the book covers all the basics.” —eWeeK “I would definitely recommend this book to those who are interested in Linux, but have not had the experience to know the inner workings of the OS.” —O’ReillyNet “One of the best basic books on learning Linux, written with the power user in mind. Five stars.” —OpeNsOuRce-BOOK-RevieWs.cOm “Succeeds admirably because of the way in which it’s organized and the level of technical detail it offers.” —KicKstaRt NeWs “This is a very different introduction to Linux. It’s unflashy, concentrates on the command line, and digs around in the internals rather than on GUI front- ends that take the place of more familiar MS Windows tools.” —techBOOKRepORt.cOm “This book does a good job of explaining the nuts and bolts of how Linux operates.” —hOstiNg ResOlve
(This page has no text content)
H o w L i n u x w o r k s 2 n d E d i t i o n w h a t E v e r y s u p e r u s e r s h o u l d k n o w by Brian Ward San Francisco
How linux works, 2nd Edition. Copyright © 2015 by Brian Ward. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed in USA First printing 18 17 16 15 14 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-567-6 ISBN-13: 978-1-59327-567-9 Publisher: William Pollock Production Editor: Laurel Chun Cover and Interior Design: Octopod Studios Cover Illustration: Tina Salameh Developmental Editor: William Pollock Technical Reviewer: Jordi Gutiérrez Hermoso Copyeditor: Gillian McGarvey Compositor: Susan Glinert Stevens Proofreader: Paula L. Fleming For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; info@nostarch.com www.nostarch.com The Library of Congress has cataloged the first edition as follows: Ward, Brian. How Linux works : what every superuser should know / Brian Ward. p. cm. Includes index. ISBN 1-59327-035-6 1. Linux. 2. Operating systems (Computers). I. Title. QA76.76.O63 W3654 2004 005.4'32--dc22 2004002692 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
B r i E f C o n t E n t s Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Chapter 1: The Big Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: Basic Commands and Directory Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 3: Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Chapter 4: Disks and Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Chapter 5: How the Linux Kernel Boots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Chapter 6: How User Space Starts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Chapter 7: System Configuration: Logging, System Time, Batch Jobs, and Users . . . . . . . . 149 Chapter 8: A Closer Look at Processes and Resource Utilization . . . . . . . . . . . . . . . . . . . 171 Chapter 9: Understanding Your Network and Its Configuration . . . . . . . . . . . . . . . . . . . . 189 Chapter 10: Network Applications and Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Chapter 11: Introduction to Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Chapter 12: Moving Files Across the Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Chapter 13: User Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Chapter 14: A Brief Survey of the Linux Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Chapter 15: Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 Chapter 16: Introduction to Compiling Software from C Source Code . . . . . . . . . . . . . . . 329 Chapter 17: Building on the Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
(This page has no text content)
C o n t E n t s i n d E t a i L PrEfacE xix Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx A Hands-On Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx How This Book Is Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi What’s New in the Second Edition? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi A Note on Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii acknowlEdgmEnts xxiii 1 tHE Big PicturE 1 1 .1 Levels and Layers of Abstraction in a Linux System . . . . . . . . . . . . . . . . . . . . . . . . 2 1 .2 Hardware: Understanding Main Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1 .3 The Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1 .3 .1 Process Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1 .3 .2 Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1 .3 .3 Device Drivers and Management . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1 .3 .4 System Calls and Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1 .4 User Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1 .5 Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1 .6 Looking Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2 Basic commands and dirEctory HiErarcHy 11 2 .1 The Bourne Shell: /bin/sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2 .2 Using the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2 .2 .1 The Shell Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2 .2 .2 cat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2 .2 .3 Standard Input and Standard Output . . . . . . . . . . . . . . . . . . . . . . . 13 2 .3 Basic Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2 .3 .1 ls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2 .3 .2 cp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2 .3 .3 mv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2 .3 .4 touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2 .3 .5 rm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .3 .6 echo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .4 Navigating Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .4 .1 cd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .4 .2 mkdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 .4 .3 rmdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 .4 .4 Shell Globbing (Wildcards) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
viii Contents in Detail 2 .5 Intermediate Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2 .5 .1 grep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2 .5 .2 less . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2 .5 .3 pwd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2 .5 .4 diff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2 .5 .5 file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2 .5 .6 find and locate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2 .5 .7 head and tail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 .5 .8 sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 .6 Changing Your Password and Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 .7 Dot Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 .8 Environment and Shell Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 .9 The Command Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2 .10 Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2 .11 Command-Line Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2 .12 Text Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2 .13 Getting Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2 .14 Shell Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2 .14 .1 Standard Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2 .14 .2 Standard Input Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2 .15 Understanding Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2 .15 .1 Anatomy of a UNIX Error Message . . . . . . . . . . . . . . . . . . . . . . . 28 2 .15 .2 Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2 .16 Listing and Manipulating Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2 .16 .1 Command Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2 .16 .2 Killing Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2 .16 .3 Job Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2 .16 .4 Background Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2 .17 File Modes and Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2 .17 .1 Modifying Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2 .17 .2 Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2 .17 .3 Creating Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2 .18 Archiving and Compressing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2 .18 .1 gzip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2 .18 .2 tar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2 .18 .3 Compressed Archives ( .tar .gz) . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 2 .18 .4 zcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2 .18 .5 Other Compression Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2 .19 Linux Directory Hierarchy Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2 .19 .1 Other Root Subdirectories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2 .19 .2 The /usr Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2 .19 .3 Kernel Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2 .20 Running Commands as the Superuser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2 .20 .1 sudo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2 .20 .2 /etc/sudoers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2 .21 Looking Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3 dEvicEs 45 3 .1 Device Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3 .2 The sysfs Device Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Contents in Detail ix 3 .3 dd and Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3 .4 Device Name Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3 .4 .1 Hard Disks: /dev/sd* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 3 .4 .2 CD and DVD Drives: /dev/sr* . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3 .4 .3 PATA Hard Disks: /dev/hd* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3 .4 .4 Terminals: /dev/tty*, /dev/pts/*, and /dev/tty . . . . . . . . . . . . . . . 51 3 .4 .5 Serial Ports: /dev/ttyS* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 3 .4 .6 Parallel Ports: /dev/lp0 and /dev/lp1 . . . . . . . . . . . . . . . . . . . . . . 52 3 .4 .7 Audio Devices: /dev/snd/*, /dev/dsp, /dev/audio, and More . . . . 53 3 .4 .8 Creating Device Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3 .5 udev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 3 .5 .1 devtmpfs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 3 .5 .2 udevd Operation and Configuration . . . . . . . . . . . . . . . . . . . . . . . 55 3 .5 .3 udevadm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 3 .5 .4 Monitoring Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 3 .6 In-Depth: SCSI and the Linux Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3 .6 .1 USB Storage and SCSI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3 .6 .2 SCSI and ATA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3 .6 .3 Generic SCSI Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3 .6 .4 Multiple Access Methods for a Single Device . . . . . . . . . . . . . . . . . 63 4 disks and filEsystEms 65 4 .1 Partitioning Disk Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 4 .1 .1 Viewing a Partition Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 4 .1 .2 Changing Partition Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 4 .1 .3 Disk and Partition Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 4 .1 .4 Solid-State Disks (SSDs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4 .2 Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4 .2 .1 Filesystem Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 4 .2 .2 Creating a Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4 .2 .3 Mounting a Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 4 .2 .4 Filesystem UUID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 4 .2 .5 Disk Buffering, Caching, and Filesystems . . . . . . . . . . . . . . . . . . . . 77 4 .2 .6 Filesystem Mount Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4 .2 .7 Remounting a Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 4 .2 .8 The /etc/fstab Filesystem Table . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 4 .2 .9 Alternatives to /etc/fstab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 4 .2 .10 Filesystem Capacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 4 .2 .11 Checking and Repairing Filesystems . . . . . . . . . . . . . . . . . . . . . . . 82 4 .2 .12 Special-Purpose Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 4 .3 Swap Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 4 .3 .1 Using a Disk Partition as Swap Space . . . . . . . . . . . . . . . . . . . . . . 85 4 .3 .2 Using a File as Swap Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 4 .3 .3 How Much Swap Do You Need? . . . . . . . . . . . . . . . . . . . . . . . . . . 86 4 .4 Looking Forward: Disks and User Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 4 .5 Inside a Traditional Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 4 .5 .1 Viewing Inode Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 4 .5 .2 Working with Filesystems in User Space . . . . . . . . . . . . . . . . . . . . . 90 4 .5 .3 The Evolution of Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
x Contents in Detail 5 How tHE linux kErnEl Boots 93 5 .1 Startup Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 5 .2 Kernel Initialization and Boot Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 5 .3 Kernel Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 5 .4 Boot Loaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5 .4 .1 Boot Loader Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5 .4 .2 Boot Loader Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 5 .5 GRUB Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 5 .5 .1 Exploring Devices and Partitions with the GRUB Command Line . . . 100 5 .5 .2 GRUB Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 5 .5 .3 GRUB Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 5 .6 UEFI Secure Boot Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 5 .7 Chainloading Other Operating Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 5 .8 Boot Loader Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 5 .8 .1 MBR Boot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 5 .8 .2 UEFI Boot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 5 .8 .3 How GRUB Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 6 How usEr sPacE starts 111 6 .1 Introduction to init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 6 .2 System V Runlevels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 6 .3 Identifying Your init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 6 .4 systemd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 6 .4 .1 Units and Unit Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 6 .4 .2 systemd Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 6 .4 .3 systemd Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 6 .4 .4 systemd Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 6 .4 .5 Adding Units to systemd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 6 .4 .6 systemd Process Tracking and Synchronization . . . . . . . . . . . . . . . 123 6 .4 .7 systemd On-Demand and Resource-Parallelized Startup . . . . . . . . . 124 6 .4 .8 systemd System V Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . 129 6 .4 .9 systemd Auxiliary Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 6 .5 Upstart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 6 .5 .1 Upstart Initialization Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . 130 6 .5 .2 Upstart Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 6 .5 .3 Upstart Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 6 .5 .4 Upstart Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 6 .5 .5 Upstart Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 6 .5 .6 Upstart Runlevels and System V Compatibility . . . . . . . . . . . . . . . . 138 6 .6 System V init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 6 .6 .1 System V init: Startup Command Sequence . . . . . . . . . . . . . . . . . . 141 6 .6 .2 The System V init Link Farm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 6 .6 .3 run-parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 6 .6 .4 Controlling System V init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 6 .7 Shutting Down Your System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 6 .8 The Initial RAM Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 6 .9 Emergency Booting and Single-User Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Contents in Detail xi 7 systEm configuration: logging, systEm timE, BatcH JoBs, and usErs 149 7 .1 The Structure of /etc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 7 .2 System Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 7 .2 .1 The System Logger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 7 .2 .2 Configuration Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 7 .3 User Management Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 7 .3 .1 The /etc/passwd File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 7 .3 .2 Special Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 7 .3 .3 The /etc/shadow File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 7 .3 .4 Manipulating Users and Passwords . . . . . . . . . . . . . . . . . . . . . . . 155 7 .3 .5 Working with Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 7 .4 getty and login . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 7 .5 Setting the Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 7 .5 .1 Kernel Time Representation and Time Zones . . . . . . . . . . . . . . . . . 158 7 .5 .2 Network Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 7 .6 Scheduling Recurring Tasks with cron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 7 .6 .1 Installing Crontab Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 7 .6 .2 System Crontab Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 7 .6 .3 The Future of cron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 7 .7 Scheduling One-Time Tasks with at . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 7 .8 Understanding User IDs and User Switching . . . . . . . . . . . . . . . . . . . . . . . . . . 162 7 .8 .1 Process Ownership, Effective UID, Real UID, and Saved UID . . . . . . 162 7 .9 User Identification and Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 7 .9 .1 Using Libraries for User Information . . . . . . . . . . . . . . . . . . . . . . . 164 7 .10 PAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 7 .10 .1 PAM Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 7 .10 .2 Notes on PAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 7 .10 .3 PAM and Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 7 .11 Looking Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 8 a closEr look at ProcEssEs and rEsourcE utilization 171 8 .1 Tracking Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 8 .2 Finding Open Files with lsof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 8 .2 .1 Reading the lsof Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 8 .2 .2 Using lsof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 8 .3 Tracing Program Execution and System Calls . . . . . . . . . . . . . . . . . . . . . . . . . 174 8 .3 .1 strace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 8 .3 .2 ltrace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 8 .4 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 8 .4 .1 Single-Threaded and Multithreaded Processes . . . . . . . . . . . . . . . . 176 8 .4 .2 Viewing Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 8 .5 Introduction to Resource Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 8 .6 Measuring CPU Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 8 .7 Adjusting Process Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
xii Contents in Detail 8 .8 Load Averages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 8 .8 .1 Using uptime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 8 .8 .2 High Loads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 8 .9 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 8 .9 .1 How Memory Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 8 .9 .2 Page Faults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 8 .10 Monitoring CPU and Memory Performance with vmstat . . . . . . . . . . . . . . . . . . 183 8 .11 I/O Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 8 .11 .1 Using iostat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 8 .11 .2 Per-Process I/O Utilization and Monitoring: iotop . . . . . . . . . . . . 186 8 .12 Per-Process Monitoring with pidstat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 8 .13 Further Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 9 undErstanding your nEtwork and its configuration 189 9 .1 Network Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 9 .1 .1 Packets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 9 .2 Network Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 9 .3 The Internet Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 9 .3 .1 Viewing Your Computer’s IP Addresses . . . . . . . . . . . . . . . . . . . . 193 9 .3 .2 Subnets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 9 .3 .3 Common Subnet Masks and CIDR Notation . . . . . . . . . . . . . . . . . 195 9 .4 Routes and the Kernel Routing Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 9 .4 .1 The Default Gateway . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 9 .5 Basic ICMP and DNS Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 9 .5 .1 ping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 9 .5 .2 traceroute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 9 .5 .3 DNS and host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 9 .6 The Physical Layer and Ethernet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 9 .7 Understanding Kernel Network Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 9 .8 Introduction to Network Interface Configuration . . . . . . . . . . . . . . . . . . . . . . . . 200 9 .8 .1 Manually Adding and Deleting Routes . . . . . . . . . . . . . . . . . . . . . 201 9 .9 Boot-Activated Network Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 9 .10 Problems with Manual and Boot-Activated Network Configuration . . . . . . . . . . 202 9 .11 Network Configuration Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 9 .11 .1 NetworkManager Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 9 .11 .2 Interacting with NetworkManager . . . . . . . . . . . . . . . . . . . . . . . 204 9 .11 .3 NetworkManager Configuration . . . . . . . . . . . . . . . . . . . . . . . . 204 9 .12 Resolving Hostnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 9 .12 .1 /etc/hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 9 .12 .2 resolv .conf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 9 .12 .3 Caching and Zero-Configuration DNS . . . . . . . . . . . . . . . . . . . . 208 9 .12 .4 /etc/nsswitch .conf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 9 .13 Localhost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 9 .14 The Transport Layer: TCP, UDP, and Services . . . . . . . . . . . . . . . . . . . . . . . . 209 9 .14 .1 TCP Ports and Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 9 .14 .2 Establishing TCP Connections . . . . . . . . . . . . . . . . . . . . . . . . . . 210 9 .14 .3 Port Numbers and /etc/services . . . . . . . . . . . . . . . . . . . . . . . . 211
Contents in Detail xiii 9 .14 .4 Characteristics of TCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 9 .14 .5 UDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 9 .15 Revisiting a Simple Local Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 9 .16 Understanding DHCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 9 .16 .1 The Linux DHCP Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 9 .16 .2 Linux DHCP Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 9 .17 Configuring Linux as a Router . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 9 .17 .1 Internet Uplinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 9 .18 Private Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 9 .19 Network Address Translation (IP Masquerading) . . . . . . . . . . . . . . . . . . . . . . 217 9 .20 Routers and Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 9 .21 Firewalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 9 .21 .1 Linux Firewall Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 9 .21 .2 Setting Firewall Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 9 .21 .3 Firewall Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 9 .22 Ethernet, IP, and ARP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 9 .23 Wireless Ethernet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 9 .23 .1 iw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 9 .23 .2 Wireless Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 9 .24 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 10 nEtwork aPPlications and sErvicEs 229 10 .1 The Basics of Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 10 .1 .1 A Closer Look . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 10 .2 Network Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 10 .3 Secure Shell (SSH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 10 .3 .1 The SSHD Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 10 .3 .2 The SSH Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 10 .4 The inetd and xinetd Daemons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 10 .4 .1 TCP Wrappers: tcpd, /etc/hosts .allow, and /etc/hosts .deny . . . . 238 10 .5 Diagnostic Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 10 .5 .1 lsof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 10 .5 .2 tcpdump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 10 .5 .3 netcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 10 .5 .4 Port Scanning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 10 .6 Remote Procedure Call (RPC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 10 .7 Network Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 10 .7 .1 Typical Vulnerabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 10 .7 .2 Security Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 10 .8 Looking Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 10 .9 Sockets: How Processes Communicate with the Network . . . . . . . . . . . . . . . . . 246 10 .10 Unix Domain Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 10 .10 .1 Advantages for Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 10 .10 .2 Listing Unix Domain Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . 248 11 introduction to sHEll scriPts 249 11 .1 Shell Script Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 11 .1 .1 Limitations of Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
xiv Contents in Detail 11 .2 Quoting and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 11 .2 .1 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 11 .2 .2 Single Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 11 .2 .3 Double Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 11 .2 .4 Passing a Literal Single Quote . . . . . . . . . . . . . . . . . . . . . . . . . . 253 11 .3 Special Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 11 .3 .1 Individual Arguments: $1, $2, … . . . . . . . . . . . . . . . . . . . . . . . 254 11 .3 .2 Number of Arguments: $# . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 11 .3 .3 All Arguments: $@ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 11 .3 .4 Script Name: $0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 11 .3 .5 Process ID: $$ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 11 .3 .6 Exit Code: $? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 11 .4 Exit Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 11 .5 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 11 .5 .1 Getting Around Empty Parameter Lists . . . . . . . . . . . . . . . . . . . . 257 11 .5 .2 Using Other Commands for Tests . . . . . . . . . . . . . . . . . . . . . . . . 257 11 .5 .3 elif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 11 .5 .4 && and || Logical Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . 258 11 .5 .5 Testing Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 11 .5 .6 Matching Strings with case . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 11 .6 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 11 .6 .1 for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 11 .6 .2 while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 11 .7 Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 11 .8 Temporary File Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 11 .9 Here Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 11 .10 Important Shell Script Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 11 .10 .1 basename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 11 .10 .2 awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 11 .10 .3 sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 11 .10 .4 xargs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 11 .10 .5 expr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 11 .10 .6 exec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 11 .11 Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 11 .12 Including Other Files in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 11 .13 Reading User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 11 .14 When (Not) to Use Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 12 moving filEs across tHE nEtwork 271 12 .1 Quick Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 12 .2 rsync . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 12 .2 .1 rsync Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 12 .2 .2 Making Exact Copies of a Directory Structure . . . . . . . . . . . . . . . 273 12 .2 .3 Using the Trailing Slash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 12 .2 .4 Excluding Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . 275 12 .2 .5 Transfer Integrity, Safeguards, and Verbose Modes . . . . . . . . . . . 276 12 .2 .6 Compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 12 .2 .7 Limiting Bandwidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 12 .2 .8 Transferring Files to Your Computer . . . . . . . . . . . . . . . . . . . . . . 277 12 .2 .9 Further rsync Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Contents in Detail xv 12 .3 Introduction to File Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 12 .4 Sharing Files with Samba . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 12 .4 .1 Configuring the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 12 .4 .2 Server Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 12 .4 .3 Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 12 .4 .4 Starting the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 12 .4 .5 Diagnostics and Log Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 12 .4 .6 Configuring a File Share . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 12 .4 .7 Home Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 12 .4 .8 Sharing Printers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 12 .4 .9 Using the Samba Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 12 .4 .10 Accessing Files as a Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 12 .5 NFS Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 12 .6 Further Network File Service Options and Limitations . . . . . . . . . . . . . . . . . . . 286 13 usEr EnvironmEnts 287 13 .1 Guidelines for Creating Startup Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 13 .2 When to Alter Startup Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 13 .3 Shell Startup File Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 13 .3 .1 The Command Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 13 .3 .2 The Manual Page Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 13 .3 .3 The Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 13 .3 .4 Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 13 .3 .5 The Permissions Mask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 13 .4 Startup File Order and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 13 .4 .1 The bash Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 13 .4 .2 The tcsh Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 13 .5 Default User Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 13 .5 .1 Shell Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 13 .5 .2 Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 13 .5 .3 Pager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 13 .6 Startup File Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 13 .7 Further Startup Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 14 a BriEf survEy of tHE linux dEsktoP 297 14 .1 Desktop Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 14 .1 .1 Window Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 14 .1 .2 Toolkits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 14 .1 .3 Desktop Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 14 .1 .4 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 14 .2 A Closer Look at the X Window System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 14 .2 .1 Display Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 14 .2 .2 Network Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 14 .3 Exploring X Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 14 .3 .1 X Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 14 .3 .2 Understanding X Input and Preference Settings . . . . . . . . . . . . . . 302 14 .4 The Future of X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
xvi Contents in Detail 14 .5 D-Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 14 .5 .1 System and Session Instances . . . . . . . . . . . . . . . . . . . . . . . . . . 306 14 .5 .2 Monitoring D-Bus Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 14 .6 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 14 .6 .1 CUPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 14 .6 .2 Format Conversion and Print Filters . . . . . . . . . . . . . . . . . . . . . . 308 14 .7 Other Desktop Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 15 dEvEloPmEnt tools 309 15 .1 The C Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 15 .1 .1 Multiple Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 15 .1 .2 Header (Include) Files and Directories . . . . . . . . . . . . . . . . . . . . 311 15 .1 .3 Linking with Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 15 .1 .4 Shared Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 15 .2 make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 15 .2 .1 A Sample Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 15 .2 .2 Built-in Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 15 .2 .3 Final Program Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 15 .2 .4 Staying Up-to-Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 15 .2 .5 Command-Line Arguments and Options . . . . . . . . . . . . . . . . . . . 320 15 .2 .6 Standard Macros and Variables . . . . . . . . . . . . . . . . . . . . . . . . 321 15 .2 .7 Conventional Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 15 .2 .8 Organizing a Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 15 .3 Debuggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 15 .4 Lex and Yacc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 15 .5 Scripting Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 15 .5 .1 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 15 .5 .2 Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 15 .5 .3 Other Scripting Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 15 .6 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 15 .7 Looking Forward: Compiling Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 16 introduction to comPiling softwarE from c sourcE codE 329 16 .1 Software Build Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 16 .2 Unpacking C Source Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 16 .2 .1 Where to Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 16 .3 GNU Autoconf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 16 .3 .1 An Autoconf Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 16 .3 .2 Installing Using a Packaging Tool . . . . . . . . . . . . . . . . . . . . . . . 333 16 .3 .3 configure Script Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 16 .3 .4 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 16 .3 .5 Autoconf Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 16 .3 .6 Autoconf Log Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 16 .3 .7 pkg-config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 16 .4 Installation Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 16 .4 .1 Where to Install . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Contents in Detail xvii 16 .5 Applying a Patch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 16 .6 Troubleshooting Compiles and Installations . . . . . . . . . . . . . . . . . . . . . . . . . . 340 16 .6 .1 Specific Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 16 .7 Looking Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 17 Building on tHE Basics 345 17 .1 Web Servers and Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 17 .2 Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 17 .2 .1 Types of Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 17 .3 Virtualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 17 .4 Distributed and On-Demand Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 17 .5 Embedded Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 17 .6 Final Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 BiBliograPHy 351 indEx 355
(This page has no text content)
P r E f a C E I wrote this book because I believe you should be able to learn what your computer does. You should be able to make your software do what you want it to do (within the reasonable limits of its capabilities, of course). The key to attaining this power lies in under- standing the fundamentals of what the software does and how it works, and that’s what this book is all about. You should never have to fight with a computer. Linux is a great platform for learning because it doesn’t try to hide any- thing from you. In particular, most system configuration can be found in plaintext files that are easy enough to read. The only tricky part is figuring out which parts are responsible for what and how it all fits together.
Comments 0
Loading comments...
Reply to Comment
Edit Comment