Statistics
61
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2025-11-23

AuthorBrian Ward

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 third edition of the bestselling How Linux Works, author Brian Ward peels back the layers of this well-loved operating system to make Linux internals accessible. This edition has been thoroughly updated and expanded with added coverage of Logical Volume Manager (LVM), virtualization, and containers. You'll learn: • How Linux boots, from boot loaders to init (systemd) • 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, 3rd edition will teach you what you need to know to solve pesky problems and take control of your operating system.

Tags
No tags
ISBN: 1718500416
Publisher: No Starch Press
Publish Year: 2021
Language: 英文
Pages: 464
File Format: PDF
File Size: 9.9 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

B R I A N W A R D H O W L I N U X W O R K S 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 3 R D E D I T I O N
(This page has no text content)
REVIEWS FOR HOW LINUX WORKS “If you are interested in Linux, How Linux Works: What Every Superuser Should Know is a must-read title.” — LinuxInsider “Lots to offer on almost every aspect of the Linux architecture.” — Everyday Linux User “You’ll get an essential understanding of what’s going on under the hood with- out getting bogged down in minutiae—making this a very refreshing (and wholly recommended) addition to the Linux literature.” — Phil Bull, co-author of UbUntU Made easy and member of the Ubuntu documentation team “Dives straight into the transparent depths of Linux-based operating systems and shows us how all the pieces fit together.” — DistroWatch “Earns its place on the shelf as an essential reference.” — the MagPi magazine
(This page has no text content)
San Francisco H O W L I N U X W O R K S 3 r 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
HOW LINUX WORKS, 3RD EDITION. Copyright © 2021 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. ISBN-13: 978-1-7185-0040-2 (print) ISBN-13: 978-1-7185-0041-9 (ebook) Publisher: William Pollock Executive Editor: Barbara Yien Production Editor: Rachel Monaghan Developmental Editors: Larry Wake and Jill Franklin Cover and Interior Design: Octopod Studios Technical Reviewers: Jordi Gutiérrez Hermoso and Petros Koutoupis Copyeditor: Rachel Monaghan Compositor: Cody Gates, Happenstance Type-O-Rama Proofreader: James M. Fraleigh For information on book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1-415-863-9900; info@nostarch.com www.nostarch.com The Library of Congress has catalogued 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.
About the Author Brian Ward has been working with Linux since 1993. He is also the author of The Linux Kernel HOWTO, The Book of VMware (No Starch Press), and The Linux Problem Solver (No Starch Press). About the Technical Reviewers Jordi Gutiérrez Hermoso is a GNU/Linux user and developer with almost two decades of experience, with occasional contributions in vari- ous circles of the free software communities, such as GNU Octave and Mercurial. His professional work has allowed him to collaborate with and learn about diverse topics such as digital cryptographic signatures, medical imaging, and most recently greenhouse gas accounting and ecological map data, all built entirely with Linux and other free soft- ware. When he is not near a computer he enjoys swimming, mathematics, and knitting. Petros Koutoupis is currently a senior performance software engineer at HPE (formerly Cray Inc.) for its Lustre High Performance File System division. He is also the creator and maintainer of the RapidDisk Project (www.rapiddisk.org). Petros has worked in the data storage industry for well over a decade and has helped pioneer the many technologies unleashed in the wild today.
(This page has no text content)
B R I E F C O N T E N T S Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Chapter1: The Big Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: Basic Commands and Directory Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 3: Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Chapter 4: Disks and Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Chapter 5: How the Linux Kernel Boots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Chapter 6: How User Space Starts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Chapter 7: System Configuration: Logging, System Time, Batch Jobs, and Users . . . . . . . . 167 Chapter 8: A Closer Look at Processes and Resource Utilization . . . . . . . . . . . . . . . . . . . 199 Chapter 9: Understanding Your Network and Its Configuration . . . . . . . . . . . . . . . . . . . . 223 Chapter 10: Network Applications and Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Chapter 11: Introduction to Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Chapter 12: Network File Transfer and Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Chapter 13: User Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Chapter 14: A Brief Survey of the Linux Desktop and Printing . . . . . . . . . . . . . . . . . . . . . 347 Chapter 15: Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 Chapter 16: Introduction to Compiling Software from C Source Code . . . . . . . . . . . . . . . 385 Chapter 17: Virtualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
(This page has no text content)
C O N T E N T S I N D E T A I L PREFACE XXI Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii A Hands-on Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii How This Book Is Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii What’s New in the Third Edition? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii A Note on Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2 .2 .2 cat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2 .2 .3 Standard Input and Standard Output . . . . . . . . . . . . . . . . . . . . . . . . 14 2 .3 Basic Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2 .3 .1 ls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2 .3 .2 cp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2 .3 .3 mv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .3 .4 touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .3 .5 rm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .3 .6 echo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .4 Navigating Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .4 .1 cd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 .4 .2 mkdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 .4 .3 rmdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 .4 .4 Shell Globbing (“Wildcards”) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 C O N T E N T S I N D E T A I L About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .v About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .v ACKNOWLEDGMENTS XX PREFACE XXI Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii A Hands-on Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii How This Book Is Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii What’s New in the Third Edition? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii A Note on Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv 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 6 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 13 2 .2 .2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . cat 13 2 .2 .3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Standard Input and Standard Output 14 2 .3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basic Commands 15 2 .3 .1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ls 15 2 .3 .2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . cp 15 2 .3 .3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . mv 16 2 .3 .4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . touch 16 2 .3 .5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .rm 16 2 .3 .6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . echo 16 2 .4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Navigating Directories 16 2 .4 .1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . cd 17 2 .4 .2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .mkdir 17
x   Contents in Detail 2 .5 Intermediate Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2 .5 .1 grep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2 .5 .2 less . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2 .5 .3 pwd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2 .5 .4 diff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 .5 .5 file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 .5 .6 find and locate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 .5 .7 head and tail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 .5 .8 sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2 .6 Changing Your Password and Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2 .7 Dot Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2 .8 Environment and Shell Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2 .9 The Command Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2 .10 Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2 .11 Command-Line Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2 .12 Text Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2 .13 Getting Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2 .14 Shell Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2 .14 .1 Standard Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2 .14 .2 Standard Input Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2 .15 Understanding Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2 .15 .1 Anatomy of a Unix Error Message . . . . . . . . . . . . . . . . . . . . . . . . . 30 2 .15 .2 Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2 .16 Listing and Manipulating Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2 .16 .1 Command Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2 .16 .2 Process Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2 .16 .3 Job Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2 .16 .4 Background Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2 .17 File Modes and Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2 .17 .1 Modifying Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2 .17 .2 Working with Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 2 .18 Archiving and Compressing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2 .18 .1 gzip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2 .18 .2 tar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2 .18 .3 Compressed Archives ( .tar .gz) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2 .18 .4 zcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2 .18 .5 Other Compression Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2 .19 Linux Directory Hierarchy Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2 .19 .1 Other Root Subdirectories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2 .19 .2 The /usr Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 2 .19 .3 Kernel Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 2 .20 Running Commands as the Superuser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 2 .20 .1 sudo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 2 .20 .2 /etc/sudoers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 2 .20 .3 sudo Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 2 .21 Looking Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3 DEVICES 47 3 .1 Device Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3 .2 The sysfs Device Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
   xi 3 .3 dd and Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 3 .4 Device Name Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3 .4 .1 Hard Disks: /dev/sd* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 3 .4 .2 Virtual Disks: /dev/xvd*, /dev/vd* . . . . . . . . . . . . . . . . . . . . . . . . 53 3 .4 .3 Non-Volatile Memory Devices: /dev/nvme* . . . . . . . . . . . . . . . . . . . 53 3 .4 .4 Device Mapper: /dev/dm-*, /dev/mapper/* . . . . . . . . . . . . . . . . . 53 3 .4 .5 CD and DVD Drives: /dev/sr* . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3 .4 .6 PATA Hard Disks: /dev/hd* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3 .4 .7 Terminals: /dev/tty*, /dev/pts/*, and /dev/tty . . . . . . . . . . . . . . . . 53 3 .4 .8 Serial Ports: /dev/ttyS*, /dev/ttyUSB*, /dev/ttyACM* . . . . . . . . . . . 55 3 .4 .9 Parallel Ports: /dev/lp0 and /dev/lp1 . . . . . . . . . . . . . . . . . . . . . . . 55 3 .4 .10 Audio Devices: /dev/snd/*, /dev/dsp, /dev/audio, and More . . . . 55 3 .4 .11 Device File Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 3 .5 udev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 3 .5 .1 devtmpfs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 3 .5 .2 udevd Operation and Configuration . . . . . . . . . . . . . . . . . . . . . . . . 58 3 .5 .3 udevadm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3 .5 .4 Device Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3 .6 In-Depth: SCSI and the Linux Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3 .6 .1 USB Storage and SCSI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3 .6 .2 SCSI and ATA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3 .6 .3 Generic SCSI Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 3 .6 .4 Multiple Access Methods for a Single Device . . . . . . . . . . . . . . . . . . . 67 4 DISKS AND FILESYSTEMS 69 4 .1 Partitioning Disk Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4 .1 .1 Viewing a Partition Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4 .1 .2 Modifying Partition Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 4 .1 .3 Creating a Partition Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 4 .1 .4 Navigating Disk and Partition Geometry . . . . . . . . . . . . . . . . . . . . . . 78 4 .1 .5 Reading from Solid-State Disks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 4 .2 Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 4 .2 .1 Filesystem Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 4 .2 .2 Creating a Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 4 .2 .3 Mounting a Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 4 .2 .4 Filesystem UUID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 4 .2 .5 Disk Buffering, Caching, and Filesystems . . . . . . . . . . . . . . . . . . . . . 86 4 .2 .6 Filesystem Mount Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 4 .2 .7 Remounting a Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 4 .2 .8 The /etc/fstab Filesystem Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 4 .2 .9 Alternatives to /etc/fstab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 4 .2 .10 Filesystem Capacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 4 .2 .11 Checking and Repairing Filesystems . . . . . . . . . . . . . . . . . . . . . . . . 91 4 .2 .12 Special-Purpose Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 4 .3 Swap Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 4 .3 .1 Using a Disk Partition as Swap Space . . . . . . . . . . . . . . . . . . . . . . . 94 4 .3 .2 Using a File as Swap Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 4 .3 .3 Determining How Much Swap You Need . . . . . . . . . . . . . . . . . . . . . 95 4 .4 The Logical Volume Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 4 .4 .2 Working with LVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 4 .4 .3 The LVM Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Contents in Detail
xii   Contents in Detail 4 .5 Looking Forward: Disks and User Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 4 .6 Inside a Traditional Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 4 .6 .1 Inode Details and the Link Count . . . . . . . . . . . . . . . . . . . . . . . . . . 113 4 .6 .2 Block Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 4 .6 .3 Working with Filesystems in User Space . . . . . . . . . . . . . . . . . . . . . 115 5 HOW THE LINUX KERNEL BOOTS 117 5 .1 Startup Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 5 .2 Kernel Initialization and Boot Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 5 .3 Kernel Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 5 .4 Boot Loaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 5 .4 .1 Boot Loader Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 5 .4 .2 Boot Loader Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 5 .5 GRUB Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 5 .5 .1 Exploring Devices and Partitions with the GRUB Command Line . . . . 125 5 .5 .2 GRUB Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 5 .5 .3 GRUB Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 5 .6 UEFI Secure Boot Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 5 .7 Chainloading Other Operating Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 5 .8 Boot Loader Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 5 .8 .1 MBR Boot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 5 .8 .2 UEFI Boot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 5 .8 .3 How GRUB Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 6 HOW USER SPACE STARTS 137 6 .1 Introduction to init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 6 .2 Identifying Your init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 6 .3 systemd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 6 .3 .1 Units and Unit Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 6 .3 .2 Booting and Unit Dependency Graphs . . . . . . . . . . . . . . . . . . . . . . 140 6 .3 .3 systemd Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 6 .3 .4 systemd Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 6 .3 .5 systemd Process Tracking and Synchronization . . . . . . . . . . . . . . . . 147 6 .3 .6 systemd Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 6 .3 .7 systemd On-Demand and Resource-Parallelized Startup . . . . . . . . . . 151 6 .3 .8 systemd Auxiliary Components . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 6 .4 System V Runlevels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 6 .5 System V init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 6 .5 .1 System V init: Startup Command Sequence . . . . . . . . . . . . . . . . . . . 158 6 .5 .2 The System V init Link Farm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 6 .5 .3 run-parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 6 .5 .4 System V init Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 6 .5 .5 systemd System V Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 6 .6 Shutting Down Your System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 6 .7 The Initial RAM Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 6 .8 Emergency Booting and Single-User Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 6 .9 Looking Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
   xiii 7 SYSTEM CONFIGURATION: LOGGING, SYSTEM TIME, BATCH JOBS, AND USERS 167 7 .1 System Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 7 .1 .1 Checking Your Log Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 7 .1 .2 Searching and Monitoring Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 7 .1 .3 Logfile Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 7 .1 .4 Journal Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 7 .1 .5 A Closer Look at System Logging . . . . . . . . . . . . . . . . . . . . . . . . . . 173 7 .2 The Structure of /etc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 7 .3 User Management Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 7 .3 .1 The /etc/passwd File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 7 .3 .2 Special Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 7 .3 .3 The /etc/shadow File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 7 .3 .4 Manipulating Users and Passwords . . . . . . . . . . . . . . . . . . . . . . . . 179 7 .3 .5 Working with Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 7 .4 getty and login . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 7 .5 Setting the Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 7 .5 .1 Kernel Time Representation and Time Zones . . . . . . . . . . . . . . . . . . 182 7 .5 .2 Network Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 7 .6 Scheduling Recurring Tasks with cron and Timer Units . . . . . . . . . . . . . . . . . . . . 183 7 .6 .1 Installing Crontab Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 7 .6 .2 System Crontab Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 7 .6 .3 Timer Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 7 .6 .4 cron vs . Timer Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 7 .7 Scheduling One-Time Tasks with at . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 7 .7 .1 Timer Unit Equivalents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 7 .8 Timer Units Running as Regular Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 7 .9 User Access Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 7 .9 .1 User IDs and User Switching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 7 .9 .2 Process Ownership, Effective UID, Real UID, and Saved UID . . . . . . . 189 7 .9 .3 User Identification, Authentication, and Authorization . . . . . . . . . . . . 191 7 .9 .4 Using Libraries for User Information . . . . . . . . . . . . . . . . . . . . . . . . 192 7 .10 Pluggable Authentication Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 7 .10 .1 PAM Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 7 .10 .2 Tips on PAM Configuration Syntax . . . . . . . . . . . . . . . . . . . . . . . . 196 7 .10 .3 PAM and Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 7 .11 Looking Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 8 A CLOSER LOOK AT PROCESSES AND RESOURCE UTILIZATION 199 8 .1 Tracking Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 8 .2 Finding Open Files with lsof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 8 .2 .1 Reading the lsof Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 8 .2 .2 Using lsof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 8 .3 Tracing Program Execution and System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . 202 8 .3 .1 strace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 8 .3 .2 ltrace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 8 .4 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 8 .4 .1 Single-Threaded and Multithreaded Processes . . . . . . . . . . . . . . . . . 204 8 .4 .2 Viewing Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Contents in Detail  
xiv   Contents in Detail 8 .5 Introduction to Resource Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 8 .5 .1 Measuring CPU Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 8 .5 .2 Adjusting Process Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 8 .5 .3 Measuring CPU Performance with Load Averages . . . . . . . . . . . . . . 208 8 .5 .4 Monitoring Memory Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 8 .5 .5 Monitoring CPU and Memory Performance with vmstat . . . . . . . . . . 212 8 .5 .6 I/O Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 8 .5 .7 Per-Process Monitoring with pidstat . . . . . . . . . . . . . . . . . . . . . . . . 216 8 .6 Control Groups (cgroups) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 8 .6 .1 Differentiating Between cgroup Versions . . . . . . . . . . . . . . . . . . . . 217 8 .6 .2 Viewing cgroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 8 .6 .3 Manipulating and Creating cgroups . . . . . . . . . . . . . . . . . . . . . . . . 220 8 .6 .4 Viewing Resource Utilization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 8 .7 Further Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 9 UNDERSTANDING YOUR NETWORK AND ITS CONFIGURATION 223 9 .1 Network Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 9 .2 Packets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 9 .3 Network Layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 9 .4 The Internet Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 9 .4 .1 Viewing IP Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 9 .4 .2 Subnets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 9 .4 .3 Common Subnet Masks and CIDR Notation . . . . . . . . . . . . . . . . . . 229 9 .5 Routes and the Kernel Routing Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 9 .6 The Default Gateway . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 9 .7 IPv6 Addresses and Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 9 .7 .1 Viewing IPv6 Configuration on Your System . . . . . . . . . . . . . . . . . . 232 9 .7 .2 Configuring Dual-Stack Networks . . . . . . . . . . . . . . . . . . . . . . . . . . 233 9 .8 Basic ICMP and DNS Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 9 .8 .1 ping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 9 .8 .2 DNS and host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 9 .9 The Physical Layer and Ethernet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 9 .10 Understanding Kernel Network Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 9 .11 Introduction to Network Interface Configuration . . . . . . . . . . . . . . . . . . . . . . . 237 9 .11 .1 Manually Configuring Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . 237 9 .11 .2 Manually Adding and Deleting Routes . . . . . . . . . . . . . . . . . . . . . 238 9 .12 Boot-Activated Network Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 9 .13 Problems with Manual and Boot-Activated Network Configuration . . . . . . . . . . 239 9 .14 Network Configuration Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 9 .14 .1 NetworkManager Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 9 .14 .2 NetworkManager Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 9 .14 .3 NetworkManager Configuration . . . . . . . . . . . . . . . . . . . . . . . . . 242 9 .15 Resolving Hostnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 9 .15 .1 /etc/hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 9 .15 .2 resolv .conf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 9 .15 .3 Caching and Zero-Configuration DNS . . . . . . . . . . . . . . . . . . . . . 245 9 .15 .4 /etc/nsswitch .conf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 9 .16 Localhost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 9 .17 The Transport Layer: TCP, UDP, and Services . . . . . . . . . . . . . . . . . . . . . . . . . 247 9 .17 .1 TCP Ports and Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 9 .17 .2 UDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
   xv 9 .18 Revisiting a Simple Local Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 9 .19 Understanding DHCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 9 .19 .1 Linux DHCP Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 9 .19 .2 Linux DHCP Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 9 .20 Automatic IPv6 Network Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 9 .21 Configuring Linux as a Router . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 9 .22 Private Networks (IPv4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 9 .23 Network Address Translation (IP Masquerading) . . . . . . . . . . . . . . . . . . . . . . 256 9 .24 Routers and Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 9 .25 Firewalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 9 .25 .1 Linux Firewall Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 9 .25 .2 Setting Firewall Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 9 .25 .3 Firewall Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 9 .26 Ethernet, IP, ARP, and NDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 9 .27 Wireless Ethernet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 9 .27 .1 iw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 9 .27 .2 Wireless Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 9 .28 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 10 NETWORK APPLICATIONS AND SERVICES 269 10 .1 The Basics of Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 10 .2 A Closer Look . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 10 .3 Network Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 10 .3 .1 Secure Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 10 .3 .2 The sshd Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 10 .3 .3 fail2ban . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 10 .3 .4 The SSH Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 10 .4 Pre-systemd Network Connection Servers: inetd/xinetd . . . . . . . . . . . . . . . . . . . . . 278 10 .5 Diagnostic Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 10 .5 .1 lsof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 10 .5 .2 tcpdump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 10 .5 .3 netcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 10 .5 .4 Port Scanning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 10 .6 Remote Procedure Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 10 .7 Network Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 10 .7 .1 Typical Vulnerabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 10 .7 .2 Security Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 10 .8 Looking Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 10 .9 Network Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 10 .10 Unix Domain Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 11 INTRODUCTION TO SHELL SCRIPTS 291 11 .1 Shell Script Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 11 .1 .1 Limitations of Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 11 .2 Quoting and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 11 .2 .1 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 11 .2 .2 Single Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 11 .2 .3 Double Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 11 .2 .4 Literal Single Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Contents in Detail   
xvi   Contents in Detail 11 .3 Special Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 11 .3 .1 Individual Arguments: $1, $2, and So On . . . . . . . . . . . . . . . . . . 296 11 .3 .2 Number of Arguments: $# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 11 .3 .3 All Arguments: $@ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 11 .3 .4 Script Name: $0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 11 .3 .5 Process ID: $$ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 11 .3 .6 Exit Code: $? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 11 .4 Exit Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 11 .5 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 11 .5 .1 A Workaround for Empty Parameter Lists . . . . . . . . . . . . . . . . . . . 299 11 .5 .2 Other Commands for Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 11 .5 .3 elif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 11 .5 .4 Logical Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 11 .5 .5 Testing Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 11 .5 .6 case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 11 .6 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 11 .6 .1 for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 11 .6 .2 while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 11 .7 Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 11 .8 Temporary File Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 11 .9 Here Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 11 .10 Important Shell Script Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 11 .10 .1 basename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 11 .10 .2 awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 11 .10 .3 sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 11 .10 .4 xargs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 11 .10 .5 expr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 11 .10 .6 exec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 11 .11 Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 11 .12 Including Other Files in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 11 .13 Reading User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 11 .14 When (Not) to Use Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 12 NETWORK FILE TRANSFER AND SHARING 315 12 .1 Quick Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 12 .2 rsync . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 12 .2 .1 Getting Started with rsync . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 12 .2 .2 Making Exact Copies of a Directory Structure . . . . . . . . . . . . . . . . 318 12 .2 .3 Using the Trailing Slash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 12 .2 .4 Excluding Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . 320 12 .2 .5 Checking Transfers, Adding Safeguards, and Using Verbose Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 12 .2 .6 Compressing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 12 .2 .7 Limiting Bandwidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 12 .2 .8 Transferring Files to Your Computer . . . . . . . . . . . . . . . . . . . . . . . 322 12 .2 .9 Further rsync Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 12 .3 Introduction to File Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 12 .3 .1 File Sharing Usage and Performance . . . . . . . . . . . . . . . . . . . . . . 323 12 .3 .2 File Sharing Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
   xvii 12 .4 Sharing Files with Samba . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 12 .4 .1 Server Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 12 .4 .2 Server Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 12 .4 .3 Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 12 .4 .4 Manual Server Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 12 .4 .5 Diagnostics and Logfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 12 .4 .6 File Share Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 12 .4 .7 Home Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 12 .4 .8 Printer Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 12 .4 .9 The Samba Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 12 .5 SSHFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 12 .6 NFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 12 .7 Cloud Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 12 .8 The State of Network File Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 13 USER ENVIRONMENTS 335 13 .1 Guidelines for Creating Startup Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 13 .2 When to Alter Startup Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 13 .3 Shell Startup File Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 13 .3 .1 The Command Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 13 .3 .2 The Manual Page Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 13 .3 .3 The Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 13 .3 .4 Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 13 .3 .5 The Permissions Mask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 13 .4 Startup File Order and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 13 .4 .1 The bash Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 13 .4 .2 The tcsh Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 13 .5 Default User Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 13 .5 .1 Shell Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 13 .5 .2 Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 13 .5 .3 Pager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 13 .6 Startup File Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 13 .7 Further Startup Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 14 A BRIEF SURVEY OF THE LINUX DESKTOP AND PRINTING 347 14 .1 Desktop Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 14 .1 .1 Framebuffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 14 .1 .2 The X Window System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 14 .1 .3 Wayland . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 14 .1 .4 Window Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 14 .1 .5 Toolkits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 14 .1 .6 Desktop Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 14 .1 .7 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 14 .2 Are You Running Wayland or X? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 14 .3 A Closer Look at Wayland . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 14 .3 .1 The Compositing Window Manager . . . . . . . . . . . . . . . . . . . . . . 351 14 .3 .2 libinput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 14 .3 .3 X Compatibility in Wayland . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Contents in Detail   
xviii   Contents in Detail 14 .4 A Closer Look at the X Window System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 14 .4 .1 Display Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 14 .4 .2 Network Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 14 .4 .3 Ways of Exploring X Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 14 .4 .4 X Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 14 .4 .5 X Input and Preference Settings . . . . . . . . . . . . . . . . . . . . . . . . . . 357 14 .5 D-Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 14 .5 .1 System and Session Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 14 .5 .2 D-Bus Message Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 14 .6 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 14 .6 .1 CUPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 14 .6 .2 Format Conversion and Print Filters . . . . . . . . . . . . . . . . . . . . . . . 361 14 .7 Other Desktop Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 15 DEVELOPMENT TOOLS 363 15 .1 The C Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 15 .1 .1 Compiling Multiple Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . 365 15 .1 .2 Linking with Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366 15 .1 .3 Working with Shared Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 15 .1 .4 Working with Header (Include) Files and Directories . . . . . . . . . . . . 371 15 .2 make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 15 .2 .1 A Sample Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 15 .2 .2 Built-in Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 15 .2 .3 Final Program Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 15 .2 .4 Dependency Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 15 .2 .5 Command-Line Arguments and Options . . . . . . . . . . . . . . . . . . . . 376 15 .2 .6 Standard Macros and Variables . . . . . . . . . . . . . . . . . . . . . . . . . 377 15 .2 .7 Conventional Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 15 .2 .8 Makefile Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 15 .3 Lex and Yacc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 15 .4 Scripting Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 15 .4 .1 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 15 .4 .2 Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 15 .4 .3 Other Scripting Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 15 .5 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 15 .6 Looking Forward: Compiling Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 16 INTRODUCTION TO COMPILING SOFTWARE FROM C SOURCE CODE 385 16 .1 Software Build Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 16 .2 Unpacking C Source Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 16 .3 GNU Autoconf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 16 .3 .1 An Autoconf Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 16 .3 .2 Installation Using a Packaging Tool . . . . . . . . . . . . . . . . . . . . . . . 390 16 .3 .3 configure Script Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 16 .3 .4 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 16 .3 .5 Autoconf Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 16 .3 .6 Autoconf Logfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 16 .3 .7 pkg-config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393