3RD EDITION 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 B R I A N W A R D 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 H O W L I N U X W O R K S 3 r d E d i t i o n What Every Superuser Should Know by Brian Ward San Francisco 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; [email protected] 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. BRIEF CONTENTS 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 CONTENTS IN DETAIL CONTENTS IN DETAIL About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v PREFACE XXI About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxii ACKNOWLEDGMENTS XX How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxii A Hands-on Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxii How This Book Is Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii PREFACE XXI What’s New in the Third Edition? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi A Note on Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii 1 A Hands-on Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii THE BIG PICTURE 1 How This Book Is Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii What’s New in the Third Edition? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii 1 .1 Levels and Layers of Abstraction in a Linux System . . . . . . . . . . . . . . . . . . . . . . . . . 2 A Note on Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv 1 .2 Hardware: Understanding Main Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1 .3 The Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1 .3 .1 Process Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1 1 .3 .2 Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1 .3 .3 Device Drivers and Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 THE BIG PICTURE 1 1 .3 .4 System Calls and Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1 .4 User Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1 .1 . . . . . . . . . . . . . . . . . . . . Levels and Layers of Abstraction in a Linux System 2 1 .5 Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1 .2 . . . . . . . . . . . . . . . . . . . . . . . . . . Hardware: Understanding Main Memory 4 1 .6 Looking Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1 .3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Kernel 4 1 .3 .1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Process Management 5 1 .3 .2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Memory Management 6 2 1 .3 .3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Device Drivers and Management 6 BASIC COMMANDS AND DIRECTORY HIERARCHY 11 1 .3 .4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . System Calls and Support 7 2 .1 The Bourne Shell: /bin/sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1 .4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . User Space 8 2 .2 Using the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1 .5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Users 9 2 .2 .1 The Shell Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1 .6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Looking Forward 10 2 .2 .2 cat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2 .2 .3 Standard Input and Standard Output . . . . . . . . . . . . . . . . . . . . . . . . 14 2 2 .3 Basic Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2 .3 .1 ls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2 .3 .2 cp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 BASIC COMMANDS AND DIRECTORY HIERARCHY 11 2 .3 .3 mv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Bourne Shell: /bin/sh 12 2 .3 .4 touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the Shell 12 2 .3 .5 rm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .2 .1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Shell Window 13 2 .3 .6 echo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .2 .2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . cat 13 2 .4 Navigating Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2 .2 .3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Standard Input and Standard Output 14 2 .4 .1 cd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 .3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basic Commands 15 2 .4 .2 mkdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 .3 .1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ls 15 2 .4 .3 rmdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 .3 .2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . cp 15 2 .4 .4 Shell Globbing (“Wildcards”) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 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 .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 x Contents in Detail