Free Essay

Linux

In: Computers and Technology

Submitted By jame2014
Words 315186
Pages 1261
www.it-ebooks.info

Praise for Previous Editions of A Practical

Guide to Fedora and Red Hat Enterprise Linux
®

®

“Since I’m in an educational environment, I found the content of Sobell’s book to be right on target and very helpful for anyone managing Linux in the enterprise. His style of writing is very clear. He builds up to the chapter exercises, which I find to be relevant to real-world scenarios a user or admin would encounter. An IT/IS student would find this book a valuable complement to their education. The vast amount of information is extremely well balanced and Sobell manages to present the content without complicated asides and meandering prose. This is a ‘must have’ for anyone managing Linux systems in a networked environment or anyone running a Linux server. I would also highly recommend it to an experienced computer user who is moving to the Linux platform.” —Mary Norbury IT Director Barbara Davis Center University of Colorado at Denver from a review posted on slashdot.org “I had the chance to use your UNIX books when I when was in college years ago at Cal Poly, San Luis Obispo, CA. I have to say that your books are among the best! They’re quality books that teach the theoretical aspects and applications of the operating system.” —Benton Chan IS Engineer “The book has more than lived up to my expectations from the many reviews I read, even though it targets FC2. I have found something very rare with your book: It doesn’t read like the standard technical text, it reads more like a story. It’s a pleasure to read and hard to put down. Did I say that?! :-)” —David Hopkins Business Process Architect “Thanks for your work and for the book you wrote. There are really few books that can help people to become more efficient administrators of different workstations. We hope (in Russia) that you will continue bringing us a new level of understanding of Linux/UNIX systems.” —Anton Petukhov

www.it-ebooks.info

“Mark Sobell has written a book as approachable as it is authoritative.” —Jeffrey Bianchine Advocate, Author, Journalist “Excellent reference book, well suited for the sysadmin of a Linux cluster, or the owner of a PC contemplating installing a recent stable Linux. Don’t be put off by the daunting heft of the book. Sobell has striven to be as inclusive as possible, in trying to anticipate your system administration needs.” —Wes Boudville Inventor “A Practical Guide to Red Hat® Linux® is a brilliant book. Thank you Mark Sobell.” —C. Pozrikidis University of California at San Diego “This book presents the best overview of the Linux operating system that I have found. . . . [It] should be very helpful and understandable no matter what the reader’s background: traditional UNIX user, new Linux devotee, or even Windows user. Each topic is presented in a clear, complete fashion and very few assumptions are made about what the reader knows. . . . The book is extremely useful as a reference, as it contains a 70-page glossary of terms and is very well indexed. It is organized in such a way that the reader can focus on simple tasks without having to wade through more advanced topics until they are ready.” —Cam Marshall Marshall Information Service LLC Member of Front Range UNIX Users Group [FRUUG] Boulder, Colorado “Conclusively, this is THE book to get if you are a new Linux user and you just got into RH/Fedora world. There’s no other book that discusses so many different topics and in such depth.” —Eugenia Loli-Queru Editor in Chief OSNews.com

www.it-ebooks.info

Praise for Other Books by Mark G. Sobell
“This book is a very useful tool for anyone who wants to ‘look under the hood’ so to speak, and really start putting the power of Linux to work. What I find particularly frustrating about man pages is that they never include examples. Sobell, on the other hand, outlines very clearly what the command does and then gives several common, easy-tounderstand examples that make it a breeze to start shell programming on one’s own. As with Sobell’s other works, this is simple, straightforward, and easy to read. It’s a great book and will stay on the shelf at easy arm’s reach for a long time.” —Ray Bartlett Travel Writer “Overall I found this book to be quite excellent, and it has earned a spot on the very front of my bookshelf. It covers the real ‘guts’ of Linux— the command line and its utilities—and does so very well. Its strongest points are the outstanding use of examples, and the Command Reference section. Highly recommended for Linux users of all skill levels. Well done to Mark Sobell and Prentice Hall for this outstanding book!” —Dan Clough Electronics Engineer and Slackware Linux User “Totally unlike most Linux books, this book avoids discussing everything via GUI and jumps right into making the power of the command line your friend.” —Bjorn Tipling Software Engineer ask.com “This book is the best distro-agnostic, foundational Linux reference I’ve ever seen, out of dozens of Linux-related books I’ve read. Finding this book was a real stroke of luck. If you want to really understand how to get things done at the command line, where the power and flexibility of free UNIX-like OSes really live, this book is among the best tools you’ll find toward that end.” —Chad Perrin Writer, TechRepublic

www.it-ebooks.info

“I currently own one of your books, A Practical Guide to Linux®. I believe this book is one of the most comprehensive and, as the title says, practical guides to Linux I have ever read. I consider myself a novice and I come back to this book over and over again.” —Albert J. Nguyen “Thank you for writing a book to help me get away from Windows XP and to never touch Windows Vista. The book is great; I am learning a lot of new concepts and commands. Linux is definitely getting easier to use.” —James Moritz “I am so impressed by how Mark Sobell can approach a complex topic in such an understandable manner. His command examples are especially useful in providing a novice (or even an advanced) administrator with a cookbook on how to accomplish real-world tasks on Linux. He is truly an inspired technical writer!” —George Vish II Senior Education Consultant Hewlett-Packard Company “Overall, I think it’s a great, comprehensive Ubuntu book that’ll be a valuable resource for people of all technical levels.” —John Dong Ubuntu Forum Council Member Backports Team Leader “The JumpStart sections really offer a quick way to get things up and running, allowing you to dig into the details of the book later.” —Scott Mann Aztek Networks “I would so love to be able to use this book to teach a class about not just Ubuntu or Linux but about computers in general. It is thorough and well written with good illustrations that explain important concepts for computer usage.” —Nathan Eckenrode New York Local Community Team

www.it-ebooks.info

“Ubuntu is gaining popularity at the rate alcohol did during Prohibition, and it’s great to see a well-known author write a book on the latest and greatest version. Not only does it contain Ubuntu-specific information, but it also touches on general computer-related topics, which will help the average computer user to better understand what’s going on in the background. Great work, Mark!” —Daniel R. Arfsten Pro/ENGINEER Drafter/Designer “I read a lot of Linux technical information every day, but I’m rarely impressed by tech books. I usually prefer online information sources instead. Mark Sobell’s books are a notable exception. They’re clearly written, technically accurate, comprehensive, and actually enjoyable to read.” —Matthew Miller Senior Systems Analyst/Administrator BU Linux Project Boston University Office of Information Technology

“This is well written, clear, comprehensive information for the Linux user of any type, whether trying Ubuntu on for the first time and wanting to know a little about it, or using the book as a very good reference when doing something more complicated like setting up a server. This book’s value goes well beyond its purchase price and it’ll make a great addition to the Linux section of your bookshelf.” —Linc Fessenden Host of The LinuxLink TechShow tllts.org “The author has done a very good job at clarifying such a detail-oriented operating system. I have extensive Unix and Windows experience and this text does an excellent job at bridging the gaps between Linux, Windows, and Unix. I highly recommend this book to both ‘newbs’ and experienced users. Great job!” —Mark Polczynski Information Technology Consultant

www.it-ebooks.info

“When I first started working with Linux just a short 10 years or so ago, it was a little more difficult than now to get going. . . . Now, someone new to the community has a vast array of resources available on the web, or if they are inclined to begin with Ubuntu, they can literally find almost every single thing they will need in the single volume of Mark Sobell’s A Practical Guide to Ubuntu Linux®. “I’m sure this sounds a bit like hyperbole. Everything a person would need to know? Obviously not everything, but this book, weighing in at just under 1200 pages, covers so much so thoroughly that there won’t be much left out. From install to admin, networking, security, shell scripting, package management, and a host of other topics, it is all there. GUI and command line tools are covered. There is not really any wasted space or fluff, just a huge amount of information. There are screen shots when appropriate but they do not take up an inordinate amount of space. This book is information-dense.” —JR Peck Editor GeekBook.org “I have been wanting to make the jump to Linux but did not have the guts to do so—until I saw your familiarly titled A Practical Guide to Red Hat® Linux® at the bookstore. I picked up a copy and am eagerly looking forward to regaining my freedom.” —Carmine Stoffo Machine and Process Designer to pharmaceutical industry “I am currently reading A Practical Guide to Red Hat® Linux® and am finally understanding the true power of the command line. I am new to Linux and your book is a treasure.” —Juan Gonzalez “Overall, A Practical Guide to Ubuntu Linux® by Mark G. Sobell provides all of the information a beginner to intermediate user of Linux would need to be productive. The inclusion of the Live DVD of the Gutsy Gibbon release of Ubuntu makes it easy for the user to test-drive Linux without affecting his installed OS. I have no doubts that you will consider this book money well spent.” —Ray Lodato Slashdot contributor www.slashdot.org

www.it-ebooks.info

A Practical Guide to Fedora and Red Hat Enterprise Linux
® ®

SIXTH EDITION

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

A Practical Guide to Fedora and Red Hat Enterprise Linux
® ®

SIXTH EDITION

Mark G. Sobell

Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City

www.it-ebooks.info

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United States, please contact: International Sales international@pearsoned.com Visit us on the Web: informit.com/ph Library of Congress Cataloging-in-Publication Data Sobell, Mark G. A practical guide to Fedora and Red Hat Enterprise Linux / Mark G. Sobell.—6th ed. p. cm. Includes index. ISBN 978-0-13-275727-0 (pbk. : alk. paper) 1. Linux. 2. Operating systems (Computers) I. Title. QA76.76.O63S5945 2012 005.4'32—dc23 2011023929 Copyright © 2012 Mark G. Sobell All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290. ISBN-13: 978-0-13-275727-0 ISBN-10: 0-13-275727-3 Text printed in the United States on recycled paper at Edwards Brothers in Ann Arbor, Michigan. First printing, August 2011

www.it-ebooks.info

For my great-niece Casey Rose. Welcome to the world!

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

Brief Contents
Contents xv List of JumpStarts xxxix Preface xli 1 Welcome to Linux 1

PART I
2 3

Installing Fedora/RHEL Linux

23

Installation Overview 25 Step-by-Step Installation 51

PART II Getting Started with Fedora/RHEL 87
4 5 6 7 Introduction to Fedora and Red Hat Enterprise Linux The Linux Utilities 145 The Linux Filesystem 185 The Shell 225 89

PART III

Digging into Fedora/RHEL

255

8 Linux GUIs: X and GNOME 257 9 The Bourne Again Shell 279 10 Networking and the Internet 359

xiii

www.it-ebooks.info

xiv Brief Contents

PART IV
11 12 13 14 15 16 17

System Administration

405

System Administration: Core Concepts 407 Files, Directories, and Filesystems 501 Finding, Downloading, and Installing Software Printing with CUPS 559 Building a Linux Kernel 583 Administration Tasks 601 Configuring and Monitoring a LAN 645

531

PART V Using Clients and Setting Up Servers
18 19 20 21 22 23 24 25 26 OpenSSH: Secure Network Communication 673 FTP: Transferring Files Across a Network 701 sendmail: Setting Up Mail Servers, Clients, and More 729 NIS and LDAP 759 NFS: Sharing Directory Hierarchies 791 Samba: Linux and Windows File and Printer Sharing 817 DNS/BIND: Tracking Domain Names and Addresses 845 system-config-firewall and iptables: Setting Up a Firewall 891 Apache (httpd): Setting Up a Web Server 917

671

PART VI
27 28

Programming Tools 967
969

Programming the Bourne Again Shell The Perl Scripting Language 1057

PART VII

Appendixes

1103

A Regular Expressions 1105 B Help 1115 C Security 1125 D The Free Software Definition 1145 Glossary 1149 JumpStart Index 1199 File Tree Index 1201 Utility Index 1205 Main Index 1211

www.it-ebooks.info

Contents
List of JumpStarts xxxix Preface xli Chapter 1: Welcome to Linux 1
The History of UNIX and GNU–Linux 2 The Heritage of Linux: UNIX 2 Fade to 1983 3 Next Scene, 1991 4 The Code Is Free 5 Have Fun! 6 What Is so Good About Linux? 6 Why Linux Is Popular with Hardware Companies and Developers 9 Linux Is Portable 10 The C Programming Language 10 Overview of Linux 11 Linux Has a Kernel Programming Interface 11 Linux Can Support Many Users 12 Linux Can Run Many Tasks 12 Linux Provides a Secure Hierarchical Filesystem 12 The Shell: Command Interpreter and Programming Language 14 A Large Collection of Useful Utilities 15 Interprocess Communication 16 System Administration 16

xv

www.it-ebooks.info

xvi

Contents

Additional Features of Linux 16 GUIs: Graphical User Interfaces 16 (Inter)Networking Utilities 17 Software Development 17 Conventions Used in This Book 18 Chapter Summary 20 Exercises 20

PART I

Installing Fedora/RHEL Linux
25

23

Chapter 2: Installation Overview

The Desktop Live CD and the Install DVD 26 More Information 27 Planning the Installation 28 Considerations 28 Requirements 28 Processor Architecture 30 Interfaces: Installer and Installed System 31 Which Are You Installing: Fedora or Red Hat Enterprise Linux? 32 Fedora/RHEL Releases 33 Fedora Standard Versions 33 Fedora Spins 33 Installing a Fresh Copy or Upgrading an Existing Fedora/RHEL System? Setting Up the Hard Disk 34 RAID 41 LVM: Logical Volume Manager 42 The Installation Process 43 Downloading and Burning a CD/DVD 44 The Easy Way to Download a CD/DVD ISO Image File 44 Other Ways to Download a CD/DVD ISO Image File 45 Verifying an ISO Image File 47 Burning the CD/DVD 48 Gathering Information About the System 48 Chapter Summary 49 Exercises 50 Advanced Exercises 50

34

Chapter 3: Step-by-Step Installation 51
Running a Fedora Live Session 52 Booting the System 53 Installing Fedora/RHEL 54 Installing from a Live Session (Fedora) 55 Installing/Upgrading from the Install DVD 56

www.it-ebooks.info

Contents

xvii

The Anaconda Installer 58 Firstboot: When You Reboot 65 Initializing Databases and Updating the System 67 Installation Tasks 67 Modifying Boot Parameters (Options) 67 Using Disk Druid to Partition the Disk 71 palimpsest: The GNOME Disk Utility 77 Using the Kickstart Configurator 81 Setting Up a Dual-Boot System 82 gnome-control-center/Displays: Configures the Display 85 Chapter Summary 85 Exercises 86 Advanced Exercises 86

PART II Getting Started with Fedora/RHEL 87
Chapter 4: Introduction to Fedora and Red Hat Enterprise Linux 89
Curbing Your Power (Superuser/root Privileges) 90 A Tour of the Fedora/RHEL Desktop 90 Logging In on the System 91 Configuring Fallback Mode (Fedora) 92 Installing and Using gnome-tweak-tool (Fedora) 94 Introduction to the Desktop 95 Launching Programs from the Desktop 96 Switching Workspaces 98 Setting Personal Preferences 99 Mouse Preferences 101 Working with Windows 102 Using Nautilus to Work with Files 102 Updating Software 109 Session Management 110 Getting Help 111 Feel Free to Experiment 111 Logging Out 111 Getting the Most Out of the Desktop 112 GNOME Desktop Terminology 112 Opening Files 113 Panels 113 The Main Menu 114 Windows 115 The Object Context Menu 119

www.it-ebooks.info

xviii

Contents

Updating, Installing, and Removing Software Packages 122 Updates 123 Add/Remove Software 124 Where to Find Documentation 125 GNOME Desktop Help Window 125 man: Displays the System Manual 126 apropos: Searches for a Keyword 127 info: Displays Information About Utilities 128 The ––help Option 131 HOWTOs: Finding Out How Things Work 131 Getting Help 132 More About Logging In 134 The Login Screen 134 What to Do if You Cannot Log In 135 Logging In Remotely: Terminal Emulators, ssh, and Dial-Up Connections 135 Logging In from a Terminal (Emulator) 136 Changing Your Password 137 Using Virtual Consoles 138 Working from the Command Line 139 Correcting Mistakes 139 Repeating/Editing Command Lines 141 Chapter Summary 142 Exercises 143 Advanced Exercises 144

Chapter 5: The Linux Utilities

145

Special Characters 146 Basic Utilities 147 ls: Lists the Names of Files 148 cat: Displays a Text File 148 rm: Deletes a File 148 less Is more: Display a Text File One Screen at a Time 149 hostname: Displays the System Name 149 Working with Files 149 cp: Copies a File 149 mv: Changes the Name of a File 150 lpr: Prints a File 151 grep: Searches for a String 152 head: Displays the Beginning of a File 152 tail: Displays the End of a File 153 sort: Displays a File in Order 154 uniq: Removes Duplicate Lines from a File 154 diff: Compares Two Files 154 file: Identifies the Contents of a File 156

www.it-ebooks.info

Contents

xix

| (Pipe): Communicates Between Processes 156 Four More Utilities 157 echo: Displays Text 157 date: Displays the Time and Date 158 script: Records a Shell Session 158 unix2dos: Converts Linux and Macintosh Files to Windows Format Compressing and Archiving Files 159 bzip2: Compresses a File 160 bzcat and bunzip2: Decompress a File 161 gzip: Compresses a File 161 tar: Packs and Unpacks Archives 162 Locating Utilities 164 which and whereis: Locate a Utility 164 locate: Searches for a File 166 Displaying User and System Information 166 who: Lists Users on the System 166 finger: Lists Users on the System 167 w: Lists Users on the System 168 Communicating with Other Users 170 write: Sends a Message 170 mesg: Denies or Accepts Messages 170 Email 171 Tutorial: Using vim to Create and Edit a File 172 Starting vim 172 Command and Input Modes 174 Entering Text 175 Getting Help 176 Ending the Editing Session 179 The compatible Parameter 179 Chapter Summary 179 Exercises 182 Advanced Exercises 183

159

Chapter 6: The Linux Filesystem
The Hierarchical Filesystem 186 Directory Files and Ordinary Files Filenames 188 The Working Directory 190 Your Home Directory 191 Pathnames 191 Absolute Pathnames 192 Relative Pathnames 193 187

185

www.it-ebooks.info

xx

Contents

Working with Directories 194 mkdir: Creates a Directory 194 cd: Changes to Another Working Directory 196 rmdir: Deletes a Directory 197 Using Pathnames 198 mv, cp: Move or Copy Files 198 mv: Moves a Directory 199 Important Standard Directories and Files 199 Access Permissions 202 ls –l: Displays Permissions 202 chmod: Changes Access Permissions 203 Setuid and Setgid Permissions 205 Directory Access Permissions 207 ACLs: Access Control Lists 208 Enabling ACLs 209 Working with Access Rules 209 Setting Default Rules for a Directory 212 Links 213 Hard Links 214 Symbolic Links 216 rm: Removes a Link 218 Chapter Summary 219 Exercises 221 Advanced Exercises 222

Chapter 7: The Shell 225
The Command Line 226 Syntax 226 Processing the Command Line 229 Executing a Command 231 Editing the Command Line 231 Standard Input and Standard Output 232 The Screen as a File 232 The Keyboard and Screen as Standard Input and Standard Output 233 Redirection 234 Pipes 239 Running a Command in the Background 242 Filename Generation/Pathname Expansion 244 The ? Special Character 245 The Special Character 246 The [ ] Special Characters 247 Builtins 249 Chapter Summary 250 Utilities and Builtins Introduced in This Chapter 250 Exercises 251 Advanced Exercises 252

*

www.it-ebooks.info

Contents xxi

PART III Digging into Fedora/RHEL 255
Chapter 8: Linux GUIs: X and GNOME 257
X Window System 258 Using X 260 Desktop Environments/Managers 265 The Nautilus File Browser Window 266 The View Pane 267 The Sidebar 267 Control Bars 269 Menubar 269 The Nautilus Spatial View (RHEL) 272 GNOME Utilities 273 Pick a Font Window 273 Pick a Color Window 274 Run Application Window 274 Searching for Files 274 GNOME Terminal Emulator/Shell 276 Chapter Summary 277 Exercises 277 Advanced Exercises 278

Chapter 9: The Bourne Again Shell 279
Background 280 Shell Basics 281 Startup Files 282 Commands That Are Symbols 285 Redirecting Standard Error 285 Writing a Simple Shell Script 288 Separating and Grouping Commands 292 Job Control 296 Manipulating the Directory Stack 298 Parameters and Variables 301 User-Created Variables 302 Variable Attributes 305 Keyword Variables 307 Special Characters 315 Processes 316 Process Structure 316 Process Identification 317 Executing a Command 318 History 319 Variables That Control History 319 Re-executing and Editing Commands 320 The Readline Library 328

www.it-ebooks.info

xxii

Contents

Aliases 334 Single Versus Double Quotation Marks in Aliases 335 Examples of Aliases 336 Functions 338 Controlling bash: Features and Options 340 Command-Line Options 340 Shell Features 341 Processing the Command Line 344 History Expansion 345 Alias Substitution 345 Parsing and Scanning the Command Line 345 Command-Line Expansion 345 Chapter Summary 354 Exercises 356 Advanced Exercises 357

Chapter 10: Networking and the Internet 359
Introduction to Networking 360 Types of Networks and How They Work 362 Broadcast Networks 362 Point-to-Point Networks 363 Switched Networks 363 LAN: Local Area Network 364 WAN: Wide Area Network 367 Internetworking Through Gateways and Routers 367 Network Protocols 370 IPv4 372 IPv6 373 Host Address 376 CIDR: Classless Inter-Domain Routing 380 Hostnames 380 Communicate Over a Network 381 finger: Displays Information About Remote Users 381 Mailing List Servers 382 Network Utilities 382 Trusted Hosts 382 OpenSSH Tools 383 telnet: Logs In on a Remote System 383 ftp: Transfers Files Over a Network 385 ping: Tests a Network Connection 386 traceroute: Traces a Route Over the Internet 387 host and dig: Query Internet Nameservers 388 whois: Looks Up Information About an Internet Site 388

www.it-ebooks.info

Contents xxiii

Distributed Computing 390 The Client/Server Model 390 DNS: Domain Name Service 391 Ports 393 NIS: Network Information Service 394 NFS: Network Filesystem 394 Network Services 394 Common Daemons 395 Proxy Servers 398 RPC Network Services 398 WWW: World Wide Web 400 Browsers 401 Search Engines 401 URL: Uniform Resource Locator 401 Chapter Summary 402 Exercises 403 Advanced Exercises 404

PART IV

System Administration

405
407

Chapter 11: System Administration: Core Concepts

Running Commands with root Privileges 409 The Special Powers of a Privileged User 410 Gaining root Privileges 410 Using su to Gain root Privileges 413 Using sudo to Gain root Privileges 415 sudoers: Configuring sudo 419 Locking the root Account (Removing the root Password) 425 consolehelper: Allows an Ordinary User to Run a Privileged Command 425 The init Daemon 426 The systemd init Daemon (Fedora) 426 The Upstart init Daemon (RHEL) 436 SysVinit (rc) Scripts: Start and Stop System Services (Fedora/RHEL) 442 System Operation 448 Runlevels 448 Booting the System 449 Single-User Mode 449 Going to Graphical Multiuser Mode 451 Logging In 451 Logging Out 453 Bringing the System Down 453 Crash 455

www.it-ebooks.info

xxiv

Contents

Rescue Installed System 457 Securing a System 458 Avoiding a Trojan Horse 458 SELinux 459 PAM 463 System Administration Tools 469 Textual Administration Utilities 469 Graphical Configuration Tools 475 Setting Up a Server 477 Standard Rules in Configuration Files 478 rpcinfo: Displays Information About rpcbind 480 The xinetd Superserver 481 Securing a Server 484 DHCP: Configures Network Interfaces 489 More Information 490 How DHCP Works 490 DHCP Client 491 DHCP Server 491 nsswitch.conf: Which Service to Look at First 494 Information 494 Methods 495 Search Order 495 Action Items 495 compat Method: ± in passwd, group, and shadow Files Getting Help 497 Chapter Summary 497 Exercises 498 Advanced Exercises 499

496

Chapter 12: Files, Directories, and Filesystems 501
Important Files and Directories 502 File Types 514 Ordinary Files, Directories, Links, and Inodes 515 Device Special Files 515 Filesystems 519 mount: Mounts a Filesystem 520 umount: Unmounts a Filesystem 523 fstab: Keeps Track of Filesystems 524 fsck: Checks Filesystem Integrity 525 tune2fs: Changes Filesystem Parameters 526 Chapter Summary 528 Exercises 528 Advanced Exercises 528

www.it-ebooks.info

Contents xxv

Chapter 13: Finding, Downloading, and Installing Software 531
Introduction 532 JumpStart: Installing and Removing Packages Using yum 534 Finding the Package That Holds an Application or File You Need yum: Keeps the System Up-to-Date 538 Updating Packages 539 yum Commands 540 yum Groups 540 Downloading RPM Package Files with yumdownloader 541 yum.conf: Configures yum 542 yum Repositories 543 BitTorrent 545 RPM: The RPM Package Manager 547 Querying Packages and Files 548 Installing, Upgrading, and Removing Packages 550 Installing a Linux Kernel Binary 551 Installing Non-rpm Software 551 The /opt and /usr/local Directories 551 GNU Configure and Build System 552 Keeping Software Up-to-Date 553 Bugs 553 Errata 554 Red Hat Network (RHEL) 554 wget: Downloads Files Noninteractively 555 Chapter Summary 556 Exercises 557 Advanced Exercises 557

537

Chapter 14: Printing with CUPS

559

Introduction 560 Prerequisites 561 More Information 561 Notes 561 Fedora/RHEL Configures a Local Printer Automatically 562 JumpStart I: Configuring a Printer Using system-config-printer 562 Configuration Selections 563 JumpStart II: Setting Up a Local or Remote Printer 565 Working with the CUPS Web Interface 568 Configuring Printers 570 Modifying a Printer 570 The CUPS Web Interface 571 CUPS on the Command Line 572 Sharing CUPS Printers 576

www.it-ebooks.info

xxvi

Contents

Traditional UNIX Printing 577 Print from Windows 579 Print Using CUPS 579 Print Using Samba 580 Printing to Windows 581 Chapter Summary 581 Exercises 582 Advanced Exercises 582

Chapter 15: Building a Linux Kernel

583

Downloading, Installing, and Prepping the Kernel Source Code 585 Prerequisites 585 More Information 585 Downloading the Source Code 585 Installing the Source Code 587 Prepping the Source Code 587 Configuring and Compiling the Linux Kernel 588 Labeling the Kernel 588 .config: Configures the Kernel 589 Customizing a Kernel 590 Cleaning the Source Tree 592 Copying the Configuration File 593 Compiling a Kernel Image File and Loadable Modules 593 Using Loadable Kernel Modules 593 Installing the Kernel, Modules, and Associated Files 594 GRUB: The Linux Boot Loader 595 Configuring GRUB 596 grub-install: Installs the MBR and GRUB Files 597 dmesg: Displays Kernel Messages 597 Chapter Summary 598 Exercises 598 Advanced Exercises 599

Chapter 16: Administration Tasks 601
Configuring User and Group Accounts 602 system-config-users: Manages User Accounts 602 Managing User Accounts from the Command Line Backing Up Files 605 Choosing a Backup Medium 606 Backup Utilities 607 Performing a Simple Backup 610 Scheduling Tasks 611 crond and anacron: Schedule Routine Tasks 611 at: Runs Occasional Tasks 615 604

www.it-ebooks.info

Contents xxvii

System Reports 615 vmstat: Reports Virtual Memory Statistics 615 top: Lists Processes Using the Most Resources 616 Maintaining the System 617 parted: Reports on and Partitions a Hard Disk 617 logrotate: Manages Log Files 621 rsyslogd: Logs System Messages 623 Keeping Users Informed 625 Creating Problems 626 Solving Problems 627 MySQL 635 More Information 635 Terminology 635 Syntax and Conventions 636 Prerequisites 636 Notes 636 JumpStart: Setting Up MySQL 636 Options 637 The .my.cnf Configuration File 638 Working with MySQL 638 Chapter Summary 643 Exercises 643 Advanced Exercises 644

Chapter 17: Configuring and Monitoring a LAN
Setting Up the Hardware 646 Connecting the Computers 646 Routers 647 NIC: Network Interface Card 647 Tools 648 Configuring the Systems 650 NetworkManager: Configures Network Connections 651 The NetworkManager Applet Right-Click Menu 651 Setting Up Servers 656 Introduction to Cacti 657 Configuring SNMP 658 Setting Up LAMP 658 Enabling the Cacti Poller 661 Configuring Cacti 661 Basic Cacti Administration 663 Setting Up a Remote Data Source 664 More Information 668 Chapter Summary 668 Exercises 669 Advanced Exercises 669

645

www.it-ebooks.info

xxviii

Contents

PART V Using Clients and Setting Up Servers 671
Chapter 18: OpenSSH: Secure Network Communication 673
Introduction to OpenSSH 674 How OpenSSH Works 675 Files 675 More Information 677 Running the ssh, scp , and sftp OpenSSH Clients 677 Prerequisites 677 JumpStart: Using ssh and scp to Connect to an OpenSSH Server Configuring OpenSSH Clients 678 ssh: Logs in or Executes Commands on a Remote System 681 scp: Copies Files to and from a Remote System 683 sftp: A Secure FTP Client 685 ~/.ssh/config and /etc/ssh/ssh_config Configuration Files 686 Setting Up an OpenSSH Server (sshd) 688 Prerequisites 688 Notes 688 JumpStart: Starting an OpenSSH Server 688 Authorized Keys: Automatic Login 689 ssh-agent: Holds Your Private Keys 691 Command-Line Options 692 /etc/ssh/sshd_config Configuration File 692 Troubleshooting 695 Tunneling/Port Forwarding 696 Forwarding X11 696 Port Forwarding 697 Chapter Summary 698 Exercises 699 Advanced Exercises 699

677

Chapter 19 FTP: Transferring Files Across a Network 701
Introduction to FTP 702 Security 702 FTP Connections 703 FTP Clients 703

www.it-ebooks.info

Contents xxix

More Information 703 Notes 704 Running the ftp and sftp FTP Clients 704 Prerequisites 704 JumpStart I: Downloading Files Using ftp 704 Anonymous FTP 707 Automatic Login 708 Binary Versus ASCII Transfer Mode 708 ftp Specifics 709 Setting Up an FTP Server (vsftpd) 712 Prerequisites 712 Notes 713 JumpStart II: Starting a vsftpd FTP Server 713 Trouble Shooting 713 Configuring a vsftpd Server 715 Chapter Summary 726 Exercises 726 Advanced Exercises 726

Chapter 20: sendmail: Setting Up Mail Servers, Clients, and More 729
Introduction to sendmail 730 Outbound Email 731 Inbound Email 731 Alternatives to sendmail 731 More Information 732 Setting Up a sendmail Mail Server 732 Prerequisites 733 Notes 733 JumpStart I: Configuring sendmail on a Client 733 JumpStart II: Configuring sendmail on a Server 734 Working with sendmail Messages 735 Mail Logs 736 Aliases and Forwarding 736 Related Programs 738 Configuring sendmail 739 The sendmail.mc and sendmail.cf Files 739 Other Files in /etc/mail 741 SpamAssassin 744 How SpamAssassin Works 744 Prerequisites 745 Testing SpamAssassin 745 Configuring SpamAssassin 746

www.it-ebooks.info

xxx

Contents

Additional Email Tools 749 Webmail 749 Mailing Lists 752 Setting Up an IMAP or POP3 Mail Server 754 Authenticated Relaying 754 Chapter Summary 756 Exercises 757 Advanced Exercises 757

Chapter 21: NIS and LDAP 759
Introduction to NIS 760 How NIS Works 760 More Information 763 Running an NIS Client 763 Prerequisites 763 Notes 764 Configuring an NIS Client 764 Troubleshooting the Client 766 yppasswd: Changes NIS Passwords 767 Setting Up an NIS Server 769 Prerequisites 769 Notes 770 Configuring the Server 770 Troubleshooting the Server 775 yppasswdd: The NIS Password Update Daemon 775 LDAP 776 More Information 778 Setting Up an LDAP Server 779 Prerequisites 779 Note 779 Step-by-Step Setup 779 Tools for Working with LDAP 784 Evolution Mail 785 Chapter Summary 788 Exercises 789 Advanced Exercises 789

Chapter 22: NFS: Sharing Directory Hierarchies 791
Introduction to NFS 793 More Information 795

www.it-ebooks.info

Contents xxxi

Running an NFS Client 795 Prerequisites 796 JumpStart I: Mounting a Remote Directory Hierarchy 796 mount: Mounts a Directory Hierarchy 797 Improving Performance 800 /etc/fstab: Mounts Directory Hierarchies Automatically 801 Setting Up an NFS Server 801 Prerequisites 801 Notes 802 JumpStart II: Configuring an NFS Server Using system-config-nfs (Fedora) Manually Exporting a Directory Hierarchy 804 Where the System Keeps NFS Mount Information 808 exportfs: Maintains the List of Exported Directory Hierarchies 809 Troubleshooting 810 automount: Mounts Directory Hierarchies on Demand 811 Chapter Summary 814 Exercises 815 Advanced Exercises 815

802

CHAPTER 23: Samba: Linux and Windows File and Printer Sharing 817
Introduction to Samba 818 More Information 819 Notes 819 Samba Users, User Maps, and Passwords 820 smbpasswd and pdbedit: Work with Samba Users and Passwords 821 Running Samba Clients 822 Prerequisites 822 Working with Shares from Linux 823 Working with Shares from Windows 825 Setting Up a Samba Server 826 Prerequisites 826 JumpStart: Configuring a Samba Server Using system-config-samba (Fedora) 827 swat: Configures a Samba Server 830 smb.conf: Manually Configuring a Samba Server 834 Troubleshooting 840 Chapter Summary 843 Exercises 844 Advanced Exercises 844

www.it-ebooks.info

xxxii

Contents

Chapter 24: DNS/BIND: Tracking Domain Names and Addresses 845
Introduction to DNS 846 Nodes, Domains, and Subdomains 847 Zones 849 Queries 850 Servers 850 Resource Records 851 DNS Queries and Responses 855 Reverse Name Resolution 856 How DNS Works 857 More Information 858 Setting Up a DNS Server 858 Prerequisites 858 Notes 859 JumpStart I: Setting Up a DNS Cache 860 JumpStart II: Setting Up a Domain Using system-config-bind (Fedora) Configuring a DNS Server 866 named.conf: The named Configuration File 866 Zone Files 868 Setting Up a DNS Cache 869 DNS Glue Records 874 TSIGs: Transaction Signatures 875 Running BIND in a chroot Jail 877 Troubleshooting 878 Setting Up Different Types of DNS Servers 879 A Full-Functioned Nameserver 880 A Slave Server 884 A Split Horizon Server 884 Chapter Summary 889 Exercises 890 Advanced Exercises 890

861

Chapter 25: system-config-firewall and iptables: Setting Up a Firewall 891
JumpStart: Building a Firewall Using system-config-firewall 893 Introduction to iptables 895 More Information 898 Prerequisites 898 Notes 899 Anatomy of an iptables Command 900

www.it-ebooks.info

Contents

xxxiii

Building a Set of Rules Using iptables 902 Commands 902 Packet Match Criteria 903 Display Criteria 904 Match Extensions 904 Targets 906 Copying Rules to and from the Kernel 908 system-config-firewall: Generates a Set of Rules 909 Sharing an Internet Connection Using NAT 910 Connecting Several Clients to a Single Internet Connection 911 Connecting Several Servers to a Single Internet Connection 913 Chapter Summary 914 Exercises 914 Advanced Exercises 915

Chapter 26: Apache (httpd): Setting Up a Web Server 917
Introduction 918 More Information 919 Notes 919 Running an Apache Web Server 920 Prerequisites 921 JumpStart I: Getting Apache Up and Running 921 Filesystem Layout 923 Configuration Directives 925 Directives I: Directives You Might Want to Modify as You Get Started 925 Contexts and Containers 930 Directives II: Advanced Directives 935 The Fedora/RHEL httpd.conf File 947 Section 1: Global Environment 948 Section 2: Main Server Configuration 949 Section 3: Virtual Hosts 950 Advanced Configuration 950 Redirects 950 Content Negotiation 951 Server-Generated Directory Listings (Indexing) 952 Virtual Hosts 953 Troubleshooting 956 Modules 957 mod_cgi and CGI Scripts 958 mod_ssl 958 Authentication Modules and .htaccess Files 961 Scripting Modules 962 Multiprocessing Modules (MPMs) 962

www.it-ebooks.info

xxxiv

Contents webalizer: Analyzes Web Traffic

MRTG: Monitors Traffic Loads Error Codes 964 Chapter Summary 965 Exercises 965 Advanced Exercises 966

963 964

PART VI Programming Tools 967
Chapter 27: Programming the Bourne Again Shell 969
Control Structures 971 if...then 971 if...then...else 974 if...then...elif 976 for...in 983 for 984 while 986 until 990 break and continue 992 case 992 select 999 Here Document 1001 File Descriptors 1003 Parameters and Variables 1006 Array Variables 1006 Locality of Variables 1008 Special Parameters 1010 Positional Parameters 1012 Expanding Null and Unset Variables 1017 Builtin Commands 1018 type: Displays Information About a Command 1019 read: Accepts User Input 1019 exec: Executes a Command or Redirects File Descriptors trap: Catches a Signal 1025 kill: Aborts a Process 1028 getopts: Parses Options 1028 A Partial List of Builtins 1031 Expressions 1032 Arithmetic Evaluation 1032 Logical Evaluation (Conditional Expressions) 1033

1022

www.it-ebooks.info

Contents xxxv

String Pattern Matching 1034 Operators 1035 Shell Programs 1040 A Recursive Shell Script 1041 The quiz Shell Script 1044 Chapter Summary 1050 Exercises 1052 Advanced Exercises 1053

Chapter 28: The Perl Scripting Language 1057
Introduction to Perl 1058 More Information 1059 Help 1059 perldoc 1059 Terminology 1061 Running a Perl Program 1062 Syntax 1064 Variables 1066 Scalar Variables 1068 Array Variables 1069 Hash Variables 1072 Control Structures 1073 if/unless 1074 if...else 1076 if...elsif...else 1077 foreach/for 1077 last and next 1079 while/until 1080 Working with Files 1082 Sort 1086 Subroutines 1087 Regular Expressions 1090 Syntax and the =~ Operator 1090 CPAN Modules 1095 Examples 1098 Chapter Summary 1101 Exercises 1102 Advanced Exercises 1102

www.it-ebooks.info

xxxvi

Contents

PART VII

Appendixes

1103
1105

Appendix A: Regular Expressions
Characters 1106 Delimiters 1106 Simple Strings 1106 Special Characters 1106 Periods 1107 Brackets 1107 Asterisks 1108 Carets and Dollar Signs 1108 Quoting Special Characters 1109 Rules 1109 Longest Match Possible 1109 Empty Regular Expressions 1110 Bracketing Expressions 1110 The Replacement String 1110 Ampersand 1111 Quoted Digit 1111 Extended Regular Expressions 1111 Appendix Summary 1113

Appendix B: Help

1115

Solving a Problem 1116 Finding Linux-Related Information 1117 Documentation 1117 Useful Linux Sites 1118 Linux Newsgroups 1119 Mailing Lists 1119 Words 1120 Software 1120 Office Suites and Word Processors 1122 Specifying a Terminal 1122

Appendix C: Security

1125
1127

Encryption 1126 Public Key (Asymmetric) Encryption Symmetric Key Encryption 1129 Encryption Implementation 1130 GnuPG/PGP 1130 File Security 1131

www.it-ebooks.info

Contents

xxxvii

Email Security 1131 MTAs (Mail Transfer Agents) 1132 MUAs (Mail User Agents) 1132 Network Security 1132 Network Security Solutions 1133 Network Security Guidelines 1133 Host Security 1135 Login Security 1136 Remote Access Security 1137 Viruses and Worms 1138 Physical Security 1139 Security Resources 1140 Appendix Summary 1143

Appendix D: The Free Software Definition 1145 Glossary 1149 JumpStart Index 1199 File Tree Index 1201 Utility Index 1205 Main Index 1211

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

JumpStarts
JumpStarts get you off to a quick start when you need to use a client or set up a server. Once you have the client or server up and running, you can refine its configuration using the information presented in the sections following each JumpStart.

yum (Software Packages)
Installing and Removing Packages Using yum 534

CUPS (Printing)
Configuring a Printer Using system-config-printer Setting Up a Local or Remote Printer 565 562

MySQL (Database)
Setting Up MySQL 636

OpenSSH (Secure Communication)
Using ssh and scp to Connect to an OpenSSH Server Starting an OpenSSH Server 688 677

FTP (Download and Upload Files)
Downloading Files Using ftp Starting a vsftpd FTP Server 704 713 xxxix www.it-ebooks.info

xl JumpStarts

Email
Configuring sendmail on a Client Configuring sendmail on a Server 733 734

NFS (Network Filesystem)
Mounting a Remote Directory Hierarchy 796 Configuring an NFS Server Using system-config-nfs (Fedora) 802

Samba (Linux/Windows File Sharing)
Configuring a Samba Server Using system-config-samba (Fedora) 827

DNS (Domain Name Service)
Setting Up a DNS Cache 860 Setting Up a Domain Using system-config-bind (Fedora) 861

system-config-firewall (Firewall)
Configuring a Firewall Using system-config-firewall 893

Apache (HTTP)
Getting Apache Up and Running 921

www.it-ebooks.info

M

Preface

Preface
The book

Whether you are an end user, a system administrator, or a little of both, this book explains with step-by-step examples how to get the most out of a Fedora or RHEL (Red Hat Enterprise Linux) system. In 28 chapters, this book takes you from installing a Fedora/RHEL system, through understanding its inner workings, to setting up secure servers that run on the system. This book is designed for a wide range of readers. It does not require you to have programming experience, although having some experience using a general-purpose computer, such as a Windows, Macintosh, UNIX, or another Linux system is certainly helpful. This book is appropriate for • Students who are taking a class in which they use Linux • Home users who want to set up and/or run Linux • Professionals who use Linux at work • System administrators who need an understanding of Linux and the tools that are available to them, including the bash and Perl scripting languages • Computer science students who are studying the Linux operating system • Technical executives who want to get a grounding in Linux A Practical Guide to Fedora™ and Red Hat® Enterprise Linux®, Sixth Edition, gives you a broad understanding of many facets of Linux, from installing Fedora/RHEL, through using and customizing it. No matter what your background, this book provides the knowledge you need to get on with your work. You will come away from this book understanding how to use Linux, and this book will remain a valuable reference for years to come. xli The audience

Benefits

www.it-ebooks.info

xlii Preface
Features in this edition

This edition covers many topics to help you get your work done using Fedora/RHEL. • A chapter on the Perl programming language (Chapter 28; page 1057) • Seven chapters on system administration (Section IV; page 405) • A chapter on writing programs using bash (Chapter 27; page 1057) • Coverage of LDAP in Chapter 21 (page 776) • Coverage of the MySQL relational database in Chapter 16 (page 635). • A section on the Cacti network monitoring tool in Chapter 17 (page 657). • New coverage of IPv6 in Chapter 10 (page 373). • Updated chapters reflecting new features in Fedora 15 and RHEL 6 • Four indexes, making it easier to quickly find what you are looking for. These indexes locate tables (page numbers followed by the letter t), provide definitions (italic page numbers), and differentiate between light and comprehensive coverage (light and standard fonts).


The JumpStart index (page 1199) lists all JumpStart sections in this book. These sections help you set up servers and clients as quickly as possible. The File Tree index (page 1201) lists, in hierarchical fashion, most files mentioned in this book. These files are also listed in the Main index. The Utility index (page 1205) supplies the location of all utilities mentioned in this book. A page number in a light font indicates a brief mention of the utility, while the regular font indicates more substantial coverage. The Utility index also appears on the inside of the front and back covers of the print book. The revised Main index (page 1211) is designed for ease of use.







Overlap

If you have read A Practical Guide to Linux® Commands, Editors, and Shell Programming, Second Edition, you will notice some overlap between that book and the one you are reading now. The first chapter; the chapters on the utilities and the filesystem; the appendix on regular expressions; and the Glossary are very similar in the two books, as are the three chapters on the Bourne Again Shell (bash) and the chapter on Perl. Chapters that appear in this book but do not appear in A Practical Guide to Linux® Commands, Editors, and Shell Programming, Second Edition, include Chapters 2 and 3 (installation), Chapters 4 and 8 (Fedora/RHEL and the GUI), Chapter 10 (networking), all of the chapters in Part IV (system administration) and Part V (servers), and Appendix C (security). While this book explains how to use Linux from a graphical interface and from the command line (a textual interface), A Practical Guide to Linux® Commands, Editors, and Shell Programming, Second Edition, works exclusively with the command

Differences

www.it-ebooks.info

Features of This Book

xliii

line and covers Mac OS X in addition to Linux. It includes full chapters on the vim and emacs editors, as well as chapters on the gawk pattern processing language, the sed stream editor, and the rsync secure copy utility. In addition, it has a command reference section that provides extensive examples of the use of 100 of the most important Linux and Mac OS X utilities. You can use these utilities to solve problems without resorting to programming in C.

This Book Includes a Fedora 15 (Lovelock) DVD
The print book includes an install DVD that holds Fedora 15 (Lovelock). You can use this DVD to install or upgrade to Fedora 15. Chapter 2 helps you get ready to install Fedora/RHEL. Chapter 3 provides step-by-step instructions for installing Fedora from this DVD as well as installing RHEL. This book guides you through learning about, using, and administrating a Fedora/RHEL system.
DVD features

The DVD includes many of the software packages supported by Fedora. You can use it to perform a graphical or textual (command line) installation of either a graphical or a textual Fedora system. If you do not have an Internet connection, you can use the DVD as a software repository: After you have installed Fedora, you can install supported software packages from the DVD.

Features of This Book
This book is designed and organized so you can get the most out of it in the least amount of time. You do not have to read this book straight through in page order. Instead, once you are comfortable using Linux, you can use this book as a reference: Look up a topic of interest in the table of contents or in an index and read about it. Or think of the book as a catalog of Linux topics: Flip through the pages until a topic catches your eye. The book includes many pointers to Web sites where you can obtain additional information: Consider the Internet to be an extension of this book. A Practical Guide to Fedora™ and Red Hat® Enterprise Linux®, Sixth Edition, is structured with the following features: • In this book, the term “Fedora/RHEL” refers to both Fedora and Red Hat Enterprise Linux. Features that apply to only one operating system or the other are marked as such using these indicators: (Fedora) or (RHEL). • Optional sections enable you to read the book at different levels, returning to more difficult material when you are ready to delve into it. • Caution boxes highlight procedures that can easily go wrong, giving you guidance before you run into trouble.

www.it-ebooks.info

xliv Preface

• Tip boxes highlight ways you can save time by doing something differently or situations when it may be useful or just interesting to have additional information. • Security boxes point out places where you can make a system more secure. The security appendix presents a quick background in system security issues. • Concepts are illustrated by practical examples throughout the book. • Each chapter starts with a list of Chapter objectives—a list of important tasks you should be able to perform after reading the chapter. • Chapter summaries review the important points covered in each chapter. • Review exercises are included at the end of each chapter for readers who want to further hone their skills. Answers to even-numbered exercises are posted at www.sobell.com. • The Glossary defines more than 500 commonly encountered terms. • The chapters covering servers include JumpStart sections that get you off to a quick start using clients and setting up servers. Once a server is up and running, you can test and modify its configuration, as is explained in the rest of each of these chapters. • This book provides resources for finding software on the Internet. It also explains how to download and install software using yum, the Add/Remove Software window, BitTorrent, and, for RHEL, Red Hat Network (RHN). It details controlling automatic updates using the Software Update Preferences window. • This book describes in detail many important GNU tools, including the GNOME desktop, the Nautilus File Browser, the parted, palimpsest, and gparted partition editors; the gzip compression utility; and many commandline utilities that come from the GNU project. • Pointers throughout the text provide help in obtaining online documentation from many sources, including the local system, the Fedora/RHEL Web sites, and other locations on the Internet. • The multiple comprehensive indexes help you locate topics quickly and easily.

Key Topics Covered in This Book
This section distills and summarizes the information covered by this book. In addition, “Details” (starting on page xlvii) describes what each chapter covers. Finally, the Table of Contents (starting on page xv) provides more detail. This book:

www.it-ebooks.info

Key Topics Covered in This Book
Installation

xlv

• Describes how to download Fedora ISO images from the Internet and burn the Fedora live CD or the Fedora install DVD. • Helps you plan the layout of the system’s hard disk. It includes a discussion of partitions, partition tables, and mount points, and assists you in using Disk Druid, the GNOME palimpsest disk utility, or the gparted graphical partition editor to examine and partition the hard disk. • Explains how to set up a dual-boot system so you can install Fedora/RHEL on a Windows system and boot either operating system. • Discusses booting into a live Fedora session and installing Fedora from that session. • Describes in detail how to use Anaconda, Fedora/RHEL’s installation program, to install Fedora/RHEL from an install DVD. • Covers testing a Fedora/RHEL CD/DVD for defects, setting boot commandline parameters (boot options), and creating a RAID array. • Explains how to use the Logical Volume Manager (LVM2) to set up, grow, and migrate logical volumes, which are similar in function to traditional disk partitions.

Working with Fedora/RHEL

• Introduces the GNOME desktop (GUI) and explains how to use desktop tools, including the panels, panel objects, the Main menu, object context menus, the Workspace Switcher, the Nautilus File Browser, and the GNOME terminal emulator. • Covers the Bourne Again Shell (bash) in three chapters, including an entire chapter on shell programming, which includes many sample shell scripts. These chapters provide clear explanations and extensive examples of how bash works both from the command line in day-to-day work and as a programming language in which to write shell scripts. • Explains the textual (command-line) interface and introduces more than 30 command-line utilities. • Presents a tutorial on the vim textual editor. • Covers types of networks, network protocols (including IPv6), and network utilities. • Explains hostnames, IP addresses, and subnets, and explores how to use host and dig to look up domain names and IP addresses on the Internet. • Covers distributed computing and the client/server model. • Explains how to use ACLs (Access Control Lists) to fine-tune user access permissions.

CentOS

• Describes CentOS by virtue of that operating system’s compatibility with RHEL. CentOS (www.centos.org) is a free Linux distribution that has more long-term stability than Fedora but has less support than RHEL.

www.it-ebooks.info

xlvi Preface
System administration

• Explains how to use the Fedora/RHEL graphical and textual (commandline) tools to configure the display, DNS, NFS, Samba, Apache, a firewall, a network interface, and more. You can also use these tools to add users and manage local and remote printers. • Describes how to use su to work with root privileges (become Superuser), and the advantages and dangers of working with escalated privileges. • Goes into detail about using sudo to allow specific users to work with root privileges and customizing the way sudo works by editing the sudoers configuration file. • Describes how to use the following tools to download and install software to keep a system up-to-date and to install new software:


If you do not have an Internet connection, you can use the Software Update Preferences window to set up the DVD included with this book as a software repository. You can then install from this repository software packages that Fedora/RHEL supports. Based on how you set up updates in the Software Update Preferences window, the Software Update window appears on the desktop to let you know when software updates are available. You can download and install updates from the Software Update window. The Add/Remove Software window provides an easy way to select, download, and install a wide range of software packages. The yum utility downloads and installs software packages from the Internet, keeping a system up-to-date and resolving dependencies as it processes the packages. BitTorrent is a good choice for distributing large amounts of data such as the Fedora/RHEL installation DVD and CDs. The more people who use BitTorrent to download a file, the faster it works.









• Covers graphical system administration tools, including the many tools available from the GNOME Main menu. • Explains system operation, including the boot process, init scripts, rescue (single-user) and multiuser modes, and steps to take if the system crashes. • Details the workings of the new systemd init daemon, which replaces both the Upstart and System V init daemons (Fedora). • Describes how to use and program the new Upstart init daemon, which replaces the System V init daemon (RHEL). • Explains how to set up and use the Cacti network monitoring tool to graph system and network information over time, including installing and setting up the LAMP (Linux, Apache, MySQL, and PHP) stack. • Provides instructions on installing and setting up a MySQL relational database.

www.it-ebooks.info

Key Topics Covered in This Book

xlvii

• Describes files, directories, and filesystems, including types of files and filesystems, fstab (the filesystem table), and automatically mounted filesystems, and explains how to fine-tune and check the integrity of filesystems. • Covers backup utilities, including tar and cpio. • Describes compression/archive utilities, including gzip, bzip2, compress, and zip. • Explains how to customize and build a Linux kernel.
Security

• Helps you manage basic system security issues using ssh (secure shell), vsftpd (secure FTP server), Apache (Web server), iptables (firewalls), and more. • Describes how to use the graphical system-config-firewall utility to protect the system. • Provides instructions on using iptables to share an Internet connection over a LAN and to build advanced firewalls. • Presents a complete section on SELinux (Security-Enhanced Linux), including instructions for using system-config-selinux to configure SELinux. • Describes how to set up a chroot jail to help protect a server system. • Explains how to use TCP wrappers to control who can access a server. • Explains how to set up and use the most popular Linux servers, providing a chapter on each: Apache, Samba, OpenSSH, sendmail, DNS, NFS, FTP, system-config-firewall and iptables, and NIS/LDAP. • Describes how to set up a CUPS printer server. • Explains how to set up and use a MySQL relational database. • Describes how to set up and use a DHCP server.

Clients and servers

Programming

• Provides a chapter on the Perl programming language and a full chapter covering shell programming using bash, including many examples.

Details
Chapter 1

Chapter 1 presents a brief history of Linux and explains some of the features that make it a cutting-edge operating system. The “Conventions Used in This Book” section on page 18 details the typefaces and terminology used in this book. Part I, “Installing Fedora and Red Hat Enterprise Linux,” discusses how to install Fedora/RHEL Linux. Chapter 2 presents an overview of the process of installing Fedora/RHEL Linux, including hardware requirements, downloading and burning a CD or DVD, and planning the layout of the hard disk. Chapter 3 is a step-by-step guide to installing Fedora and RHEL; it covers installing from a CD/DVD and from a live session (Fedora).

Part I

www.it-ebooks.info

xlviii Preface
Part II

Part II, “Getting Started with Fedora and Red Hat Enterprise Linux,” familiarizes you with Fedora/RHEL, covering logging in, the GUI, utilities, the filesystem, and the shell. Chapter 4 introduces desktop features, including the panels and the Main menu; explains how to use the Nautilus File Browser to manage files, run programs, and connect to FTP and HTTP servers; covers finding documentation, dealing with login problems, and using the window manager; and presents some suggestions on where to find documentation, including manuals, tutorials, software notes, and HOWTOs. Chapter 5 introduces the shell command-line interface, describes more than 30 useful utilities, and presents a tutorial on the vim text editor. Chapter 6 discusses the Linux hierarchical filesystem, covering files, filenames, pathnames, working with directories, access permissions, and hard and symbolic links. Chapter 7 introduces the Bourne Again Shell (bash) and discusses command-line arguments and options, redirecting input to and output from commands, running programs in the background, and using the shell to generate and expand filenames.

Experienced users may want to skim Part II tip If you have used a UNIX or Linux system before, you may want to skim or skip some or all of the chapters in Part II. Two sections that should not be skipped are: “Conventions Used in This Book” (page 18), which explains the typographic and layout conventions used in this book, and “Where to Find Documentation” (page 125), which points out both local and remote sources of Linux and Fedora/RHEL documentation.
Part III

Part III, “Digging into Fedora and Red Hat Enterprise Linux,” goes into more detail about working with the system. Chapter 8 discusses the GUI (desktop) and includes a section about running a graphical program on a remote system and having the display appear locally. The section on GNOME describes several GNOME utilities and goes into more depth about the Nautilus File Browser. Chapter 9 extends the bash coverage from Chapter 7, explaining how to redirect error output, avoid overwriting files, and work with job control, processes, startup files, important shell builtin commands, parameters, shell variables, and aliases. Chapter 10 explains networks, network security, and the Internet, and discusses types of networks, subnets, protocols, addresses, hostnames, and various network utilities. A new section covers the all-important IPv6 protocol. The section on distributed computing describes the client/server model and some of the servers you can use on a network. (Details of setting up and using clients and servers are reserved until Part V.) Part IV covers system administration. Chapter 11 discusses core concepts, such as the use of su and sudo; working with root privileges; SELinux (Security-Enhanced Linux); system operation, including a discussion of the Upstart (RHEL) and systemd (Fedora) init daemons; chroot jails; TCP wrappers; general information about how to set up a server; DHCP; and PAM. Chapter 12 explains the Linux filesystem, going into detail about types of files, including special and device files; the use of fsck to verify the integrity of and repair filesystems; and the use of tune2fs to change filesystem parameters. Chapter 13 explains how to keep a system up-to-date by downloading software from the Internet and installing it, including examples that use yum to perform these tasks. It also covers the RPM software packaging system and the use

Part IV

www.it-ebooks.info

Key Topics Covered in This Book

xlix

of the rpm utility. Finally, it explains how to use BitTorrent from the command line to download files. Chapter 14 explains how to set up the CUPS printing system so you can print on both local and remote systems. Chapter 15 details customizing and building a Linux kernel. Chapter 16 covers additional administration tasks, including setting up user accounts, backing up files, scheduling automated tasks, tracking disk usage, solving general problems, and setting up a MySQL relational database. Chapter 17 explains how to set up a local area network (LAN), including both hardware (including wireless) and software configuration, and how to set up Cacti to monitor the network.
Part V

Part V goes into detail about setting up and running servers and connecting to them using clients. Where appropriate, these chapters include JumpStart sections, which get you off to a quick start in using clients and setting up servers. The chapters in Part V cover the following clients/servers: • OpenSSH—Set up an OpenSSH server and use ssh, scp, and sftp to communicate securely over the Internet. • FTP—Set up a vsftpd secure FTP server and use any of several FTP clients to exchange files with the server. • Email—Configure sendmail and use Webmail, POP3, or IMAP to retrieve email; use SpamAssassin to combat spam. • NIS and LDAP—Set up NIS to facilitate system administration of a LAN and LDAP to maintain databases. • NFS—Share filesystems between systems on a network. • Samba—Share filesystems and printers between Windows and Linux systems. • DNS/BIND—Set up a domain nameserver to let other systems on the Internet know the names and IP addresses of local systems they may need to contact. • system-config-firewall and iptables—Set up a firewall to protect local systems and share a single Internet connection between systems on a LAN. • Apache—Set up an HTTP server that serves Web pages, which browsers can then display. This chapter includes many suggestions for increasing Apache security.

Part VI

Part VI covers two important programming tools that are used extensively in Fedora/RHEL system administration and general-purpose programming. Chapter 27 continues where Chapter 9 left off, going into greater depth about shell programming using bash, with the discussion enhanced by extensive examples. Chapter 28 introduces the popular, feature-rich Perl programming language, including coverage of regular expressions and file handling. Part VII includes appendixes on regular expressions, helpful Web sites, system security, and free software. This part also includes an extensive Glossary with more than 500 entries plus the JumpStart index, the File Tree index, the Utility index, and a comprehensive Main index.

Part VII

www.it-ebooks.info

l Preface

Supplements
The author’s home page (www.sobell.com) contains downloadable listings of the longer programs from this book, as well as pointers to many interesting and useful Linux sites on the World Wide Web, a list of corrections to the book, answers to even-numbered exercises, and a solicitation for corrections, comments, and suggestions.

Thanks
First and foremost, I want to thank Mark L. Taub, Editor-in-Chief, Prentice Hall, who provided encouragement and support through the hard parts of this project. Mark is unique in my 30 years of book writing experience: an editor who works with the tools I write about. Because Mark runs Linux on his home computer, we shared experiences as I wrote this book. Mark, your comments and direction are invaluable; this book would not exist without your help. Thank you, Mark T. The production people at Prentice Hall are wonderful to work with: Julie Nahil, Full-Service Production Manager, worked with me day-by-day during production of this book providing help and keeping everything on track, while John Fuller, Managing Editor, kept the large view in focus. Thanks to Bob Campbell and Linda Seifert, Proofreaders, who made each page sparkle and found the mistakes the author left behind. Thanks also to the folks at Prentice Hall who helped bring this book to life, especially Kim Boedigheimer, Editorial Assistant, who attended to the many details involved in publishing this book; Heather Fox, Publicist; Stephane Nakib, Marketing Manager; Dan Scherf, Media Developer; Cheryl Lenser, Senior Indexer; Sandra Schroeder, Design Manager; Chuti Prasertsith, Cover Designer; and everyone else who worked behind the scenes to make this book come into being. Susan Lauber, Lauber System Solutions, Inc., had a big impact on this book. She reviewed drafts of many chapters, providing insights, tips, and corrections throughout. She also helped with the objectives and exercises. I was very impressed with the depth and breadth of her knowledge of Fedora/RHEL systems. I am also indebted to Denis Howe, Editor of The Free On-Line Dictionary of Computing (FOLDOC). Denis has graciously permitted me to use entries from his compilation. Be sure to visit www.foldoc.org to look at this dictionary. A big “thank you” to the folks who read through the drafts of the book and made comments that caused me to refocus parts of the book where things were not clear or were left out altogether: Doug Hughes, long-time system designer and administrator, who gave me a big hand with the system administration chapter; Richard Woodbury, Site Reliability Engineer, Google, whose knowledge of IPv6 proved

www.it-ebooks.info

Thanks

li

invaluable; Max Sobell, Intrepidus Group; Lennart Poettering, Red Hat, Inc.; George Vish II, Senior Education Consultant, Hewlett-Packard; Matthew Miller, Senior Systems Analyst/Administrator, BU Linux Project, Boston University Office of Information Technology; Garth Snyder; Nathan Handler; Dick Seabrook, Emeritus Professor, Anne Arundel Community College; Chris Karr, Audacious Software; Scott McCrea, Instructor, ITT Technical Schools. Thanks also to the following people who helped with my previous Linux books, which provided a foundation for this book: John Dong, Ubuntu Developer, Forums Council Member; Andy Lester, author of Land the Tech Job You Love: Why Skill and Luck Are Not Enough; Scott James Remnant, Ubuntu Development Manager and Desktop Team Leader; David Chisnall, Swansea University; Scott Mann, Aztek Networks; Thomas Achtemichuk, Mansueto Ventures; Daniel R. Arfsten, Pro/Engineer Drafter/Designer; Chris Cooper, Senior Education Consultant, Hewlett-Packard Education Services; Sameer Verma, Associate Professor of Information Systems, San Francisco State University; Valerie Chau, Palomar College and Programmers Guild; James Kratzer; Sean McAllister; Nathan Eckenrode, New York Ubuntu Local Community Team; Christer Edwards; Nicolas Merline; Michael Price; Mike Basinger, Ubuntu Community and Forums Council Member; Joe Barker, Ubuntu Forums Staff Member; James Stockford, Systemateka, Inc.; Stephanie Troeth, Book Oven; Doug Sheppard; Bryan Helvey, IT Director, OpenGeoSolutions; and Vann Scott, Baker College of Flint. Also, thanks to Jesse Keating, Fedora Project; Carsten Pfeiffer, Software Engineer and KDE Developer; Aaron Weber, Ximian; Cristof Falk, Software Developer at CritterDesign; Steve Elgersma, Computer Science Department, Princeton University; Scott Dier, University of Minnesota; Robert Haskins, Computer Net Works; Lars Kellogg-Stedman, Harvard University; Jim A. Lola, Principal Systems Consultant, Privateer Systems; Eric S. Raymond, Cofounder, Open Source Initiative; Scott Mann; Randall Lechlitner, Independent Computer Consultant; Jason Wertz, Computer Science Instructor, Montgomery County Community College; Justin Howell, Solano Community College; Ed Sawicki, The Accelerated Learning Center; David Mercer; Jeffrey Bianchine, Advocate, Author, Journalist; John Kennedy; and Jim Dennis, Starshine Technical Services. Thanks also to Dustin Puryear, Puryear Information Technology; Gabor Liptak, Independent Consultant; Bart Schaefer, Chief Technical Officer, iPost; Michael J. Jordan, Web Developer, Linux Online; Steven Gibson, Owner, SuperAnt.com; John Viega, Founder and Chief Scientist, Secure Software; K. Rachael Treu, Internet Security Analyst, Global Crossing; Kara Pritchard, K & S Pritchard Enterprises; Glen Wiley, Capital One Finances; Karel Baloun, Senior Software Engineer, Looksmart; Matthew Whitworth; Dameon D. Welch-Abernathy, Nokia Systems; Josh Simon, Consultant; Stan Isaacs; and Dr. Eric H. Herrin II, Vice President, Herrin Software Development. More thanks go to consultants Lorraine Callahan and Steve Wampler; Ronald Hiller, Graburn Technology; Charles A. Plater, Wayne State University; Bob

www.it-ebooks.info

lii Preface

Palowoda; Tom Bialaski, Sun Microsystems; Roger Hartmuller, TIS Labs at Network Associates; Kaowen Liu; Andy Spitzer; Rik Schneider; Jesse St. Laurent; Steve Bellenot; Ray W. Hiltbrand; Jennifer Witham; Gert-Jan Hagenaars; and Casper Dik. A Practical Guide to Fedora™ and Red Hat® Enterprise Linux®, Sixth Edition, is based in part on two of my previous UNIX books: UNIX System V: A Practical Guide and A Practical Guide to the UNIX System. Many people helped me with those books, and thanks here go to Pat Parseghian; Dr. Kathleen Hemenway; Brian LaRose; Byron A. Jeff, Clark Atlanta University; Charles Stross; Jeff Gitlin, Lucent Technologies; Kurt Hockenbury; Maury Bach, Intel Israel; Peter H. Salus; Rahul Dave, University of Pennsylvania; Sean Walton, Intelligent Algorithmic Solutions; Tim Segall, Computer Sciences Corporation; Behrouz Forouzan, DeAnza College; Mike Keenan, Virginia Polytechnic Institute and State University; Mike Johnson, Oregon State University; Jandelyn Plane, University of Maryland; Arnold Robbins and Sathis Menon, Georgia Institute of Technology; Cliff Shaffer, Virginia Polytechnic Institute and State University; and Steven Stepanek, California State University, Northridge, for reviewing the book. I continue to be grateful to the many people who helped with the early editions of my UNIX books. Special thanks are due to Roger Sippl, Laura King, and Roy Harrington for introducing me to the UNIX system. My mother, Dr. Helen Sobell, provided invaluable comments on the original manuscript at several junctures. Also, thanks go to Isaac Rabinovitch, Professor Raphael Finkel, Professor Randolph Bentson, Bob Greenberg, Professor Udo Pooch, Judy Ross, Dr. Robert Veroff, Dr. Mike Denny, Joe DiMartino, Dr. John Mashey, Diane Schulz, Robert Jung, Charles Whitaker, Don Cragun, Brian Dougherty, Dr. Robert Fish, Guy Harris, Ping Liao, Gary Lindgren, Dr. Jarrett Rosenberg, Dr. Peter Smith, Bill Weber, Mike Bianchi, Scooter Morris, Clarke Echols, Oliver Grillmeyer, Dr. David Korn, Dr. Scott Weikart, and Dr. Richard Curtis. I take responsibility for any errors and omissions in this book. If you find one or just have a comment, let me know (mgs@sobell.com) and I will fix it in the next printing. My home page (www.sobell.com) contains a list of errors and credits those who found them. It also offers copies of the longer scripts from the book and pointers to interesting Linux pages on the Internet. You can follow me at twitter.com/marksobell. Mark G. Sobell San Francisco, California

www.it-ebooks.info

1
Welcome to Linux

C 1 hapter1

In This Chapter
The History of UNIX and GNU–Linux 2 The Heritage of Linux: UNIX . . . . . . . 2 What Is So Good About Linux?. . . . . 6 Overview of Linux . . . . . . . . . . . . . . 11 Additional Features of Linux. . . . . . 16 Conventions Used in This Book . . . 18

Objectives
After reading this chapter you should be able to: Discuss the history of UNIX, Linux, and the GNU project Explain what is meant by “free software” and list characteristics of the GNU General Public License List characteristics of Linux and reasons the Linux operating system is so popular

1

www.it-ebooks.info

2 Chapter 1 Welcome to Linux

An operating system is the low-level software that schedules tasks, allocates storage, and handles the interfaces to peripheral hardware, such as printers, disk drives, the screen, keyboard, and mouse. An operating system has two main parts: the kernel and the system programs. The kernel allocates machine resources—including memory, disk space, and CPU (page 1159) cycles—to all other programs that run on the computer. The system programs include device drivers, libraries, utility programs, shells (command interpreters), configuration scripts and files, application programs, servers, and documentation. They perform higher-level housekeeping tasks, often acting as servers in a client/server relationship. Many of the libraries, servers, and utility programs were written by the GNU Project, which is discussed shortly.
Linux kernel

The Linux kernel was developed by Finnish undergraduate student Linus Torvalds, who used the Internet to make the source code immediately available to others for free. Torvalds released Linux version 0.01 in September 1991. The new operating system came together through a lot of hard work. Programmers around the world were quick to extend the kernel and develop other tools, adding functionality to match that already found in both BSD UNIX and System V UNIX (SVR4) as well as new functionality. The name Linux is a combination of Linus and UNIX. The Linux operating system, which was developed through the cooperation of numerous people around the world, is a product of the Internet and is a free operating system. In other words, all the source code is free. You are free to study it, redistribute it, and modify it. As a result, the code is available free of cost—no charge for the software, source, documentation, or support (via newsgroups, mailing lists, and other Internet resources). As the GNU Free Software Definition (reproduced in Appendix D) puts it:

Free beer

“Free software” is a matter of liberty, not price. To understand the concept, you should think of “free” as in “free speech,” not as in “free beer.”

The History of UNIX and GNU–Linux
This section presents some background on the relationships between UNIX and Linux and between GNU and Linux.

The Heritage of Linux: UNIX
The UNIX system was developed by researchers who needed a set of modern computing tools to help them with their projects. The system allowed a group of people working together on a project to share selected data and programs while keeping other information private. Universities and colleges played a major role in furthering the popularity of the UNIX operating system through the “four-year effect.” When the UNIX operating system

www.it-ebooks.info

The History of UNIX and GNU–Linux 3

became widely available in 1975, Bell Labs offered it to educational institutions at nominal cost. The schools, in turn, used it in their computer science programs, ensuring that computer science students became familiar with it. Because UNIX was such an advanced development system, the students became acclimated to a sophisticated programming environment. As these students graduated and went into industry, they expected to work in a similarly advanced environment. As more of them worked their way up the ladder in the commercial world, the UNIX operating system found its way into industry. In addition to introducing students to the UNIX operating system, the Computer Systems Research Group (CSRG) at the University of California at Berkeley made significant additions and changes to it. In fact, it made so many popular changes that one version of the system is called the Berkeley Software Distribution (BSD) of the UNIX system (or just Berkeley UNIX). The other major version is UNIX System V (SVR4), which descended from versions developed and maintained by AT&T and UNIX System Laboratories.

Fade to 1983
Richard Stallman (www.stallman.org) announced1 the GNU Project for creating an operating system, both kernel and system programs, and presented the GNU Manifesto,2 which begins as follows: GNU, which stands for Gnu’s Not UNIX, is the name for the complete UNIX-compatible software system which I am writing so that I can give it away free to everyone who can use it. Some years later, Stallman added a footnote to the preceding sentence when he realized that it was creating confusion: The wording here was careless. The intention was that nobody would have to pay for *permission* to use the GNU system. But the words don’t make this clear, and people often interpret them as saying that copies of GNU should always be distributed at little or no charge. That was never the intent; later on, the manifesto mentions the possibility of companies providing the service of distribution for a profit. Subsequently I have learned to distinguish carefully between “free” in the sense of freedom and “free” in the sense of price. Free software is software that users have the freedom to distribute and change. Some users may obtain copies at no charge, while others pay to obtain copies—and if the funds help support improving the software, so much the better. The important thing is that everyone who has a copy has the freedom to cooperate with others in using it.

1. www.gnu.org/gnu/initial-announcement.html 2. www.gnu.org/gnu/manifesto.html

www.it-ebooks.info

4 Chapter 1 Welcome to Linux

In the manifesto, after explaining a little about the project and what has been accomplished so far, Stallman continues: Why I Must Write GNU I consider that the golden rule requires that if I like a program I must share it with other people who like it. Software sellers want to divide the users and conquer them, making each user agree not to share with others. I refuse to break solidarity with other users in this way. I cannot in good conscience sign a nondisclosure agreement or a software license agreement. For years I worked within the Artificial Intelligence Lab to resist such tendencies and other inhospitalities, but eventually they had gone too far: I could not remain in an institution where such things are done for me against my will. So that I can continue to use computers without dishonor, I have decided to put together a sufficient body of free software so that I will be able to get along without any software that is not free. I have resigned from the AI Lab to deny MIT any legal excuse to prevent me from giving GNU away.

Next Scene, 1991
The GNU Project has moved well along toward its goal. Much of the GNU operating system, except for the kernel, is complete. Richard Stallman later writes: By the early ’90s we had put together the whole system aside from the kernel (and we were also working on a kernel, the GNU Hurd,3 which runs on top of Mach4). Developing this kernel has been a lot harder than we expected, and we are still working on finishing it.5 ...[M]any believe that once Linus Torvalds finished writing the kernel, his friends looked around for other free software, and for no particular reason most everything necessary to make a UNIX-like system was already available. What they found was no accident—it was the GNU system. The available free software6 added up to a complete system because the GNU Project had been working since 1984 to make one. The GNU Manifesto had set forth the goal of developing a free UNIX-like system, called GNU. The Initial Announcement of the GNU Project also outlines some of the original plans for the

3. www.gnu.org/software/hurd/hurd.html 4. www.gnu.org/software/hurd/gnumach.html 5. www.gnu.org/software/hurd/hurd-and-linux.html 6. See Appendix D or www.gnu.org/philosophy/free-sw.html.

www.it-ebooks.info

The History of UNIX and GNU–Linux 5

GNU system. By the time Linux was written, the [GNU] system was almost finished.7 Today the GNU “operating system” runs on top of the FreeBSD (www.freebsd.org) and NetBSD (www.netbsd.org) kernels with complete Linux binary compatibility and on top of Hurd pre-releases and Darwin (developer.apple.com/opensource) without this compatibility.

The Code Is Free
The tradition of free software dates back to the days when UNIX was released to universities at nominal cost, which contributed to its portability and success. This tradition eventually died as UNIX was commercialized and manufacturers came to regard the source code as proprietary, making it effectively unavailable. Another problem with the commercial versions of UNIX related to their complexity. As each manufacturer tuned UNIX for a specific architecture, the operating system became less portable and too unwieldy for teaching and experimentation.
MINIX

Two professors created their own stripped-down UNIX look-alikes for educational purposes: Doug Comer created XINU, and Andrew Tanenbaum created MINIX. Linus Torvalds created Linux to counteract the shortcomings in MINIX. Every time there was a choice between code simplicity and efficiency/features, Tanenbaum chose simplicity (to make it easy to teach with MINIX), which meant this system lacked many features people wanted. Linux went in the opposite direction. You can obtain Linux at no cost over the Internet (page 44). You can also obtain the GNU code via the U.S. mail at a modest cost for materials and shipping. You can support the Free Software Foundation (www.fsf.org) by buying the same (GNU) code in higher-priced packages, and you can buy commercial packaged releases of Linux (called distributions), such as Fedora/RHEL, that include installation instructions, software, and support.

GPL

Linux and GNU software are distributed under the terms of the GNU General Public License (GPL, www.gnu.org/licenses/licenses.html). The GPL says you have the right to copy, modify, and redistribute the code covered by the agreement. When you redistribute the code, however, you must also distribute the same license with the code, thereby making the code and the license inseparable. If you get source code off the Internet for an accounting program that is under the GPL and then modify that code and redistribute an executable version of the program, you must also distribute the modified source code and the GPL agreement with it. Because this arrangement is the reverse of the way a normal copyright works (it gives rights instead of limiting them), it has been termed a copyleft. (This paragraph is not a legal interpretation of the GPL; it is intended merely to give you an idea of how it works. Refer to the GPL itself when you want to make use of it.)

7. www.gnu.org/gnu/linux-and-gnu.html

www.it-ebooks.info

6 Chapter 1 Welcome to Linux

Have Fun!
Two key words for Linux are “Have Fun!” These words pop up in prompts and documentation. The UNIX—now Linux—culture is steeped in humor that can be seen throughout the system. For example, less is more—GNU has replaced the UNIX paging utility named more with an improved utility named less. The utility to view PostScript documents is named ghostscript, and one of several replacements for the vi editor is named elvis. While machines with Intel processors have “Intel Inside” logos on their outside, some Linux machines sport “Linux Inside” logos. And Torvalds himself has been seen wearing a T-shirt bearing a “Linus Inside” logo.

What Is So Good About Linux?
In recent years Linux has emerged as a powerful and innovative UNIX work-alike. Its popularity has surpassed that of its UNIX predecessors. Although it mimics UNIX in many ways, the Linux operating system departs from UNIX in several significant ways: The Linux kernel is implemented independently of both BSD and System V, the continuing development of Linux is taking place through the combined efforts of many capable individuals throughout the world, and Linux puts the power of UNIX within easy reach of both business and personal computer users. Using the Internet, today’s skilled programmers submit additions and improvements to the operating system to Linus Torvalds, GNU, or one of the other authors of Linux.
Standards

In 1985, individuals from companies throughout the computer industry joined together to develop the POSIX (Portable Operating System Interface for Computer Environments) standard, which is based largely on the UNIX System V Interface Definition (SVID) and other earlier standardization efforts. These efforts were spurred by the U.S. government, which needed a standard computing environment to minimize its training and procurement costs. Released in 1988, POSIX is a group of IEEE standards that define the API (application programming interface), shell, and utility interfaces for an operating system. Although aimed at UNIX-like systems, the standards can apply to any compatible operating system. Now that these standards have gained acceptance, software developers are able to develop applications that run on all conforming versions of UNIX, Linux, and other operating systems. A rich selection of applications is available for Linux—both free and commercial—as well as a wide variety of tools: graphical, word processing, networking, security, administration, Web server, and many others. Large software companies have recently seen the benefit in supporting Linux and now have on-staff programmers whose job it is to design and code the Linux kernel, GNU, KDE, or other software that runs on Linux. For example, IBM (www.ibm.com/linux) is a major Linux supporter. Linux conforms increasingly more closely to POSIX standards, and some distributions and parts of others meet this standard. These developments indicate that Linux is becoming mainstream and is respected as an attractive alternative to other popular operating systems.

Applications

www.it-ebooks.info

What Is So Good About Linux?
Peripherals

7

Another aspect of Linux that appeals to users is the amazing range of peripherals that is supported and the speed with which support for new peripherals emerges. Linux often supports a peripheral or interface card before any company does. Unfortunately some types of peripherals—particularly proprietary graphics cards—lag in their support because the manufacturers do not release specifications or source code for drivers in a timely manner, if at all. Also important to users is the amount of software that is available—not just source code (which needs to be compiled) but also prebuilt binaries that are easy to install and ready to run. These programs include more than free software. Netscape, for example, was available for Linux from the start and included Java support before it was available from many commercial vendors. Its sibling Mozilla/Thunderbird/ Firefox is now a viable browser, mail client, and newsreader, performing many other functions as well. Linux is not just for Intel-based platforms (which now include Apple computers): It has been ported to and runs on the Power PC—including older Apple computers (ppclinux), Compaq’s (née Digital Equipment Corporation) Alpha-based machines, MIPS-based machines, Motorola’s 68K-based machines, various 64-bit systems, and IBM’s S/390. Nor is Linux just for single-processor machines: As of version 2.0, it runs on multiple-processor machines (SMPs; page 1188). It also includes an O(1) scheduler, which dramatically increases scalability on SMP systems. Linux supports programs, called emulators, that run code intended for other operating systems. By using emulators you can run some DOS, Windows, and Macintosh programs under Linux. For example, Wine (www.winehq.com) is an open-source implementation of the Windows API that runs on top of the X Window System and UNIX/Linux. A virtual machine (VM or guest) appears to the user and to the software running on it as a complete physical machine. It is, however, one of potentially many such VMs running on a single physical machine (the host). The software that provides the virtualization is called a virtual machine monitor (VMM) or hypervisor. Each VM can run a different operating system from the other VMs. For example, on a single host you could have VMs running Windows 7, Ubuntu 10.10, Ubuntu 11.04, and Fedora 15. A multitasking operating system allows you to run many programs on a single physical system. Similarly, a hypervisor allows you to run many operating systems (VMs) on a single physical system. VMs provide many advantages over single, dedicated machines: • Isolation—Each VM is isolated from the other VMs running on the same host: Thus, if one VM crashes or is compromised, the others are not affected. • Security—When a single server system running several servers is compromised, all servers are compromised. If each server is running on its own VM, only the compromised server is affected; other servers remain secure.

Software

Platforms

Emulators

Virtual machines

www.it-ebooks.info

8 Chapter 1 Welcome to Linux

• Power consumption—Using VMs, a single powerful machine can replace many less powerful machines, thereby cutting power consumption. • Development and support—Multiple VMs, each running a different version of an operating system and/or different operating systems, can facilitate development and support of software designed to run in many environments. With this organization you can easily test a product in different environments before releasing it. Similarly, when a user submits a bug, you can reproduce the bug in the same environment it occurred in. • Servers—In some cases, different servers require different versions of system libraries. In this instance, you can run each server on its own VM, all on a single piece of hardware. • Testing—Using VMs, you can experiment with cutting-edge releases of operating systems and applications without concern for the base (stable) system, all on a single machine. • Networks—You can set up and test networks of systems on a single machine. • Sandboxes—A VM presents a sandbox—an area (system) that you can work in without regard for the results of your work or for the need to clean up. • Snapshots—You can take snapshots of a VM and return the VM to the state it was in when you took the snapshot simply by reloading the VM from the snapshot.
Xen

Xen, which was created at the University of Cambridge and is now being developed in the open-source community, is an open-source virtual machine monitor (VMM). A VMM enables several virtual machines (VMs), each running an instance of a separate operating system, to run on a single computer. Xen isolates the VMs so that if one crashes it does not affect any of the others. In addition, Xen introduces minimal performance overhead when compared with running each of the operating systems natively. Fedora 15 supports Xen 4.1. This book does not cover the installation Xen or the use of Xen. For more information on Xen, refer to the Xen home page at www.cl.cam.ac.uk/research/srg/netos/xen and wiki.xensource.com/xenwiki.

VMware

VMware, Inc. (www.vmware.com) offers VMware Server, a free, downloadable, proprietary product you can install and run as an application under Fedora/RHEL. VMware Server enables you to install several VMs, each running a different operating system, including Windows and Linux. VMware also offers a free VMware player that enables you to run VMs you create with the VMware Server. The Kernel-based Virtual Machine (KVM; www.linux-kvm.org and libvirt.org) is an open-source VM and runs as part of the Linux kernel.

KVM

www.it-ebooks.info

What Is So Good About Linux?
Qemu

9

Qemu (wiki.qemu.org), written by Fabrice Bellard, is an open-source VMM that runs as a user application with no CPU requirements. It can run code written for a different CPU than that of the host machine. Fedora/RHEL has merged KVM and Qemu. For more information visit the page at fedoraproject.org/wiki/Features/KVM_and_QEMU_merge. VirtualBox (www.virtualbox.org) is a VM developed by Sun Microsystems. If you want to run a virtual instance of Windows, you might want to investigate VirtualBox.

KVM/Qemu merge

VirtualBox

Why Linux Is Popular with Hardware Companies and Developers
Two trends in the computer industry set the stage for the growing popularity of UNIX and Linux. First, advances in hardware technology created the need for an operating system that could take advantage of available hardware power. In the mid-1970s, minicomputers began challenging the large mainframe computers because, in many applications, minicomputers could perform the same functions less expensively. More recently, powerful 64-bit processor chips, plentiful and inexpensive memory, and lower-priced hard disk storage have allowed hardware companies to install multiuser operating systems on desktop computers.
Proprietary operating systems

Second, with the cost of hardware continually dropping, hardware manufacturers could no longer afford to develop and support proprietary operating systems. A proprietary operating system is one that is written and owned by the manufacturer of the hardware (for example, DEC/Compaq owns VMS). Today’s manufacturers need a generic operating system they can easily adapt to their machines. A generic operating system is written outside of the company manufacturing the hardware and is sold (UNIX, Windows) or given (Linux) to the manufacturer. Linux is a generic operating system because it runs on different types of hardware produced by different manufacturers. Of course, if manufacturers can pay only for development and avoid per-unit costs (which they have to pay to Microsoft for each copy of Windows they sell), they are much better off. In turn, software developers need to keep the prices of their products down; they cannot afford to create new versions of their products to run under many different proprietary operating systems. Like hardware manufacturers, software developers need a generic operating system. Although the UNIX system once met the needs of hardware companies and researchers for a generic operating system, over time it has become more proprietary as manufacturers added support for their own specialized features and introduced new software libraries and utilities. Linux emerged to serve both needs: It is a generic operating system that takes advantage of available hardware power.

Generic operating systems

www.it-ebooks.info

10 Chapter 1 Welcome to Linux

Linux Is Portable
A portable operating system is one that can run on many different machines. More than 95 percent of the Linux operating system is written in the C programming language, and C is portable because it is written in a higher-level, machine-independent language. (The C compiler is written in C.) Because Linux is portable, it can be adapted (ported) to different machines and can meet special requirements. For example, Linux is used in embedded computers, such as the ones found in cellphones, PDAs, and the cable boxes on top of many TVs. The file structure takes full advantage of large, fast hard disks. Equally important, Linux was originally designed as a multiuser operating system—it was not modified to serve several users as an afterthought. Sharing the computer’s power among many users and giving them the ability to share data and programs are central features of the system. Because it is adaptable and takes advantage of available hardware, Linux runs on many different microprocessor-based systems as well as mainframes. The popularity of the microprocessor-based hardware drives Linux; these microcomputers are getting faster all the time at about the same price point. This widespread acceptance benefits both users, who do not like having to learn a new operating system for each vendor’s hardware, and system administrators, who like having a consistent software environment. The advent of a standard operating system has given a boost to the development of the software industry. Now software manufacturers can afford to make one version of a product available on machines from different manufacturers.

The C Programming Language
Ken Thompson wrote the UNIX operating system in 1969 in PDP-7 assembly language. Assembly language is machine-dependent: Programs written in assembly language work on only one machine or, at best, on one family of machines. For this reason, the original UNIX operating system could not easily be transported to run on other machines: It was not portable. To make UNIX portable, Thompson developed the B programming language, a machine-independent language, from the BCPL language. Dennis Ritchie developed the C programming language by modifying B and, with Thompson, rewrote UNIX in C in 1973. Originally, C was touted as a “portable assembler.” The revised operating system could be transported more easily to run on other machines. That development marked the start of C. Its roots reveal some of the reasons why it is such a powerful tool. C can be used to write machine-independent programs. A programmer who designs a program to be portable can easily move it to any computer that has a C compiler. C is also designed to compile into very efficient code. With the advent of C, a programmer no longer had to resort to assembly language to produce code that would run well (that is, quickly—although an assembler will always generate more efficient code than a high-level language).

www.it-ebooks.info

Overview of Linux

11

Compilers

Database Management Systems

Word Processors

Mail and Message Facilities

Shells

Linux Kernel

Hardware
Figure 1-1

A layered view of the Linux operating system

C is a good systems language. You can write a compiler or an operating system in C. It is a highly structured but not necessarily a high-level language. C allows a programmer to manipulate bits and bytes, as is necessary when writing an operating system. At the same time, it has high-level constructs that allow for efficient, modular programming. In the late 1980s the American National Standards Institute (ANSI) defined a standard version of the C language, commonly referred to as ANSI C or C89 (for the year the standard was published). Ten years later the C99 standard was published; it is mostly supported by the GNU Project’s C compiler (named gcc). The original version of the language is often referred to as Kernighan & Ritchie (or K&R) C, named for the authors of the book that first described the C language. Another researcher at Bell Labs, Bjarne Stroustrup, created an object-oriented programming language named C++, which is built on the foundation of C. Because object-oriented programming is desired by many employers today, C++ is preferred over C in many environments. Another language of choice is Objective-C, which was used to write the first Web browser. The GNU Project’s C compiler supports C, C++, and Objective-C.

Overview of Linux
The Linux operating system has many unique and powerful features. Like other operating systems, it is a control program for computers. But like UNIX, it is also a well-thought-out family of utility programs (Figure 1-1) and a set of tools that allow users to connect and use these utilities to build systems and applications.

Linux Has a Kernel Programming Interface
The Linux kernel—the heart of the Linux operating system—is responsible for allocating the computer’s resources and scheduling user jobs so each one gets its fair share of system resources, including access to the CPU; peripheral devices, such as

www.it-ebooks.info

12 Chapter 1 Welcome to Linux

hard disk, DVD, and tape storage; and printers. Programs interact with the kernel through system calls, special functions with well-known names. A programmer can use a single system call to interact with many kinds of devices. For example, there is one write() system call, rather than many device-specific ones. When a program issues a write() request, the kernel interprets the context and passes the request to the appropriate device. This flexibility allows old utilities to work with devices that did not exist when the utilities were written. It also makes it possible to move programs to new versions of the operating system without rewriting them (provided the new version recognizes the same system calls).

Linux Can Support Many Users
Depending on the hardware and the types of tasks the computer performs, a Linux system can support from 1 to more than 1,000 users, each concurrently running a different set of programs. The per-user cost of a computer that can be used by many people at the same time is less than that of a computer that can be used by only a single person at a time. It is less because one person cannot generally take advantage of all the resources a computer has to offer. That is, no one can keep all the printers going constantly, keep all the system memory in use, keep all the disks busy reading and writing, keep the Internet connection in use, and keep all the terminals busy at the same time. By contrast, a multiuser operating system allows many people to use all of the system resources almost simultaneously. The use of costly resources can be maximized, and the cost per user can be minimized—the primary objectives of a multiuser operating system.

Linux Can Run Many Tasks
Linux is a fully protected multitasking operating system, allowing each user to run more than one job at a time. Processes can communicate with one another but remain fully protected from one another, just as the kernel remains protected from all processes. You can run several jobs in the background while giving all your attention to the job being displayed on the screen, and you can switch back and forth between jobs. If you are running the X Window System (page 16), you can run different programs in different windows on the same screen and watch all of them. This capability helps users be more productive.

Linux Provides a Secure Hierarchical Filesystem
A file is a collection of information, such as text for a memo or report, an accumulation of sales figures, an image, a song, or an executable program. Each file is stored under a unique identifier on a storage device, such as a hard disk. The Linux filesystem provides a structure whereby files are arranged under directories, which are like folders or boxes. Each directory has a name and can hold other files and

www.it-ebooks.info

Overview of Linux

13

/

home

tmp

etc

max

sam

hls

bin

notes

report

log

Figure 1-2

The Linux filesystem structure

directories. Directories, in turn, are arranged under other directories and so forth in a treelike organization. This structure helps users keep track of large numbers of files by grouping related files in directories. Each user has one primary directory and as many subdirectories as required (Figure 1-2).
Standards

With the idea of making life easier for system administrators and software developers, a group got together over the Internet and developed the Linux Filesystem Standard (FSSTND), which has since evolved into the Linux Filesystem Hierarchy Standard (FHS). Before this standard was adopted, key programs were located in different places in different Linux distributions. Today you can sit down at a Linux system and expect to find any given standard program at a consistent location (page 199). A link allows a given file to be accessed by means of two or more names. The alternative names can be located in the same directory as the original file or in another directory. Links can make the same file appear in several users’ directories, enabling those users to share the file easily. Windows uses the term shortcut in place of link to describe this capability. Macintosh users will be more familiar with the term alias. Under Linux, an alias is different from a link; it is a command macro feature provided by the shell (page 334). Like most multiuser operating systems, Linux allows users to protect their data from access by other users. It also allows users to share selected data and programs with certain other users by means of a simple but effective protection scheme. This level of security is provided by file access permissions, which limit the users who can read from, write to, or execute a file. Linux also implements ACLs (Access Control Lists), which give users and administrators finer-grained control over file access permissions.

Links

Security

www.it-ebooks.info

14 Chapter 1 Welcome to Linux

The Shell: Command Interpreter and Programming Language
In a textual environment, the shell—the command interpreter—acts as an interface between you and the operating system. When you enter a command on the screen, the shell interprets the command and calls the program you want. A number of shells are available for Linux. The four most popular shells are • The Bourne Again Shell (bash), an enhanced version of the original Bourne Shell (the original UNIX shell). • The Debian Almquist Shell (dash; page 281), a smaller version of bash with fewer features. Many startup shell scripts call dash in place of bash to speed the boot process. • The TC Shell (tcsh), an enhanced version of the C Shell, developed as part of BSD UNIX. • The Z Shell (zsh), which incorporates features from a number of shells, including the Korn Shell. Because different users might prefer different shells, multiuser systems can have several different shells in use at any given time. The choice of shells demonstrates one of the advantages of the Linux operating system: the ability to provide a customized interface for each user.
Shell scripts

Besides performing its function of interpreting commands from a keyboard and sending those commands to the operating system, the shell is a high-level programming language. Shell commands can be arranged in a file for later execution (Linux calls these files shell scripts; Windows calls them batch files). This flexibility allows users to perform complex operations with relative ease, often by issuing short commands, or to build with surprisingly little effort elaborate programs that perform highly complex operations.

Filename Generation
Wildcards and ambiguous file references

When you type commands to be processed by the shell, you can construct patterns using characters that have special meanings to the shell. These characters are called wildcard characters. The patterns, which are called ambiguous file references, are a kind of shorthand: Rather than typing in complete filenames, you can type patterns; the shell expands these patterns into matching filenames. An ambiguous file reference can save you the effort of typing in a long filename or a long series of similar filenames. For example, the shell might expand the pattern mak* to make-3.80.tar.gz. Patterns can also be useful when you know only part of a filename or cannot remember the exact spelling of a filename.

Completion
In conjunction with the Readline library, the shell performs command, filename, pathname, and variable completion: You type a prefix and press TAB, and the shell

www.it-ebooks.info

Overview of Linux

15

lists the items that begin with that prefix or completes the item if the prefix specifies a unique item.

Device-Independent Input and Output
Redirection

Devices (such as a printer or a terminal) and disk files appear as files to Linux programs. When you give a command to the Linux operating system, you can instruct it to send the output to any one of several devices or files. This diversion is called output redirection. In a similar manner, a program’s input, which normally comes from a keyboard, can be redirected so that it comes from a disk file instead. Input and output are device independent; that is, they can be redirected to or from any appropriate device. As an example, the cat utility normally displays the contents of a file on the screen. When you run a cat command, you can easily cause its output to go to a disk file instead of the screen.

Device independence

Shell Functions
One of the most important features of the shell is that users can use it as a programming language. Because the shell is an interpreter, it does not compile programs written for it but rather interprets programs each time they are loaded from the disk. Loading and interpreting programs can be time-consuming. Many shells, including the Bourne Again Shell, support shell functions that the shell holds in memory so it does not have to read them from the disk each time you execute them. The shell also keeps functions in an internal format so it does not have to spend as much time interpreting them.

Job Control
Job control is a shell feature that allows users to work on several jobs at once, switching back and forth between them as desired. When you start a job, it is frequently run in the foreground so it is connected to the terminal. Using job control, you can move the job you are working with to the background and continue running it there while working on or observing another job in the foreground. If a background job then needs your attention, you can move it to the foreground so it is once again attached to the terminal. The concept of job control originated with BSD UNIX, where it appeared in the C Shell.

A Large Collection of Useful Utilities
Linux includes a family of several hundred utility programs, often referred to as commands. These utilities perform functions that are universally required by users. The sort utility, for example, puts lists (or groups of lists) in alphabetical or numerical order and can be used to sort lists by part number, last name, city, ZIP code, telephone number, age, size, cost, and so forth. The sort utility is an important programming tool that is part of the standard Linux system. Other utilities allow users to create, display, print, copy, search, and delete files as well as to edit, format, and typeset text. The man (for manual) and info utilities provide online documentation for Linux.

www.it-ebooks.info

16 Chapter 1 Welcome to Linux

Interprocess Communication
Pipes and filters

Linux enables users to establish both pipes and filters on the command line. A pipe sends the output of one program to another program as input. A filter is a special kind of pipe that processes a stream of input data to yield a stream of output data. A filter processes another program’s output, altering it as a result. The filter’s output then becomes input to another program. Pipes and filters frequently join utilities to perform a specific task. For example, you can use a pipe to send the output of the sort utility to head (a filter that lists the first ten lines of its input); you can then use another pipe to send the output of head to a third utility, lpr, that sends the data to a printer. Thus, in one command line, you can use three utilities together to sort and print part of a file.

System Administration
On a Linux system the system administrator is frequently the owner and only user of the system. This person has many responsibilities. The first responsibility might be to set up the system, install the software, and possibly edit configuration files. Once the system is up and running, the system administrator is responsible for downloading and installing software (including upgrading the operating system), backing up and restoring files, and managing such system facilities as printers, terminals, servers, and a local network. The system administrator is also responsible for setting up accounts for new users on a multiuser system, bringing the system up and down as needed, monitoring the system, and taking care of any problems that arise.

Additional Features of Linux
The developers of Linux included features from BSD, System V, and Sun Microsystems’ Solaris, as well as new features, in their operating system. Although most of the tools found on UNIX exist for Linux, in some cases these tools have been replaced by more modern counterparts. This section describes some of the popular tools and features available under Linux.

GUIs: Graphical User Interfaces
The X Window System (also called X or X11) was developed in part by researchers at MIT (Massachusetts Institute of Technology) and provides the foundation for the GUIs available with Linux. Given a terminal or workstation screen that supports X, a user can interact with the computer through multiple windows on the screen, display graphical information, or use special-purpose applications to draw pictures, monitor processes, or preview formatted output. X is an across-the-network protocol that allows a user to open a window on a workstation or computer system that is remote from the CPU generating the window.
Desktop manager

Usually two layers run on top of X: a desktop manager and a window manager. A desktop manager is a picture-oriented user interface that enables you to interact

www.it-ebooks.info

Additional Features of Linux 17

with system programs by manipulating icons instead of typing the corresponding commands to a shell. Fedora/RHEL runs the GNOME desktop manager (www.gnome.org) by default, but it can also run KDE (www.kde.org) and a number of other desktop managers.
Window manager

A window manager is a program that runs under the desktop manager and allows you to open and close windows, run programs, and set up a mouse so it has different effects depending on how and where you click it. The window manager also gives the screen its personality. Whereas Microsoft Windows allows you to change the color of key elements in a window, a window manager under X allows you to customize the overall look and feel of the screen: You can change the way a window looks and works (by giving it different borders, buttons, and scrollbars), set up virtual desktops, create menus, and more. Several popular window managers run under X and Linux. RHEL provides both Metacity (the default under GNOME 2) and kwin (the default under KDE). In addition to KDE, Fedora provides Mutter (the default under GNOME 3). Mutter is short for Metacity Clutter (the graphics library is named Clutter). Other window managers, such as Sawfish and WindowMaker, are also available. Chapters 4 and 8 present information on GUIs.

(Inter)Networking Utilities
Linux network support includes many utilities that enable you to access remote systems over a variety of networks. In addition to sending email to users on other systems, you can access files on disks mounted on other computers as if they were located on the local system, make your files available to other systems in a similar manner, copy files back and forth, run programs on remote systems while displaying the results on the local system, and perform many other operations across local area networks (LANs) and wide area networks (WANs), including the Internet. Layered on top of this network access is a wide range of application programs that extend the computer’s resources around the globe. You can carry on conversations with people throughout the world, gather information on a wide variety of subjects, and download new software over the Internet quickly and reliably. Chapter 10 discusses networks, the Internet, and the Linux network facilities.

Software Development
One of Linux’s most impressive strengths is its rich software development environment. Linux supports compilers and interpreters for many computer languages. Besides C and C++, languages available for Linux include Ada, Fortran, Java, Lisp, Pascal, Perl, and Python. The bison utility generates parsing code that makes it easier to write programs to build compilers (tools that parse files containing structured information). The flex utility generates scanners (code that recognizes lexical patterns in text). The make utility and the GNU Configure and Build System make it easier to manage complex development projects. Source code management systems, such as CVS, simplify version control. Several debuggers, including ups and gdb, can help

www.it-ebooks.info

18 Chapter 1 Welcome to Linux

you track down and repair software defects. The GNU C compiler (gcc) works with the gprof profiling utility to help programmers identify potential bottlenecks in a program’s performance. The C compiler includes options to perform extensive checking of C code, thereby making the code more portable and reducing debugging time. Table 0-4 on page 1120 lists some sites you can download software from.

Conventions Used in This Book
This book uses conventions to make its explanations shorter and clearer. The following paragraphs describe these conventions.
Widgets

A widget is a simple graphical element that a user interacts with, such as a text box, radio button, or combo box. When referring to a widget, this book specifies the type of widget and its label. The term “tick” refers to the mark you put in a check box, sometimes called a check mark. For example, “put a tick in the check box labeled Run in terminal.” See the glossary for definitions of various widgets. Tabs allow windows to display sets of related information, one set at a time. For example, Figure 4-21 on page 122 shows an Object Properties window with three tabs. A frame isolates a set of information within a window. See Figure 14-2 on page 563 for an example. The menu selection path is the name of the menu or the location of the menu, followed by a colon, a SPACE, and the menu selections separated by markers. The entire menu selection path appears in bold type. You can read Main menu: Applications System Tools Terminal as “From the Main menu, select Applications; from Applications, select System Tools; and then select Terminal.” The text is set in this type, whereas examples are shown in a monospaced font (also called a fixed-width font):
$ cat practice This is a small file I created with a text editor.

Tabs and frames

Menu selection path

Text and examples

Items you enter

Everything you enter at the keyboard is shown in a bold typeface. Within the text, this bold typeface is used; within examples and screens, this one is used. In the previous example, the dollar sign ($) on the first line is a prompt that Linux displays, so it is not bold; the remainder of the first line is entered by a user, so it is bold. Names of utilities are printed in this sans serif typeface. This book references the emacs text editor and the ls utility or ls command (or just ls) but instructs you to enter ls –a on the command line. In this way the text distinguishes between utilities, which are programs, and the instructions you enter on the command line to invoke the utilities.

Utility names

Filenames

Filenames appear in a bold typeface. Examples are memo5, letter.1283, and reports. Filenames might include uppercase and lowercase letters; however, Linux is case sensitive (page 1155), so memo5, MEMO5, and Memo5 name three different files.

www.it-ebooks.info

Conventions Used in This Book 19
Character strings

Within the text, characters and character strings are marked by putting them in a bold typeface. This convention avoids the need for quotation marks or other delimiters before and after a string. An example is the following string, which is displayed by the passwd utility: Sorry, passwords do not match. Words appear in a bold typeface in the sections of the book that describe a GUI. This font indicates you can click a mouse button when the mouse pointer is over these words on the screen or over a button with this name: Click Next. This book uses SMALL CAPS for three kinds of items: • Keyboard keys, such as the SPACE bar and the RETURN,8 ESCAPE, and TAB keys. • The characters that keys generate, such as the SPACEs generated by the SPACE bar. • Keyboard keys that you press with the CONTROL key, such as CONTROL-D. (Even though D is shown as an uppercase letter, you do not have to press the SHIFT key; enter CONTROL-D by holding the CONTROL key down and pressing d.)

Buttons and labels

Keys and characters

Prompts and RETURNs

Most examples include the shell prompt—the signal that Linux is waiting for a command—as a dollar sign ($), a hashmark (#), or sometimes a percent sign (%). The prompt does not appear in a bold typeface in this book because you do not enter it. Do not type the prompt on the keyboard when you are experimenting with examples from this book. If you do, the examples will not work. Examples omit the RETURN keystroke that you must use to execute them. An example of a command line is
$ vim memo.1204

To use this example as a model for running the vim text editor, enter the command vim memo.1204 and press the RETURN key. (Press ESCAPE ZZ to exit from vim; see page 172 for a vim tutorial.) This method of entering commands makes the examples in the book correspond to what appears on the screen.
Definitions

All glossary entries marked with FOLDOC are courtesy of Denis Howe, editor of the Free Online Dictionary of Computing (foldoc.org), and are used with permission. This site is an ongoing work containing definitions, anecdotes, and trivia.

optional

Optional Information
Passages marked as optional appear in a gray box. This material is not central to the ideas presented in the chapter but often involves more challenging concepts. A good strategy when reading a chapter is to skip the optional sections and then return to them when you are comfortable with the main ideas presented in the chapter. This is an optional paragraph.

8. Different keyboards use different keys to move the cursor (page 1160) to the beginning of the next line. This book always refers to the key that ends a line as the RETURN key. The keyboard you are using might have a RET, NEWLINE, ENTER, RETURN , or other key. Use the corresponding key on your keyboard each time this book asks you to press RETURN.

www.it-ebooks.info

20 Chapter 1 Welcome to Linux
URLs (Web addresses)

Web addresses, or URLs, have an implicit http:// prefix, unless ftp:// or http:// is shown. You do not normally need to specify a prefix when the prefix is http://, but you must use a prefix from a browser when you specify an FTP or secure HTTP site. Thus you can specify a URL in a browser exactly as shown in this book. This book uses the output of ls –l commands as produced by including the option ––time-style=ISO. This output produces shorter lines, making the examples more readable. The following boxes highlight information that might be helpful while you are using or administrating a Linux system.

ls output

Tip, caution, and security boxes

This is a tip box tip A tip box might help you avoid repeating a common mistake or might point toward additional information. This box warns you about something caution A caution box warns you about a potential pitfall. This box marks a security note security A security box highlights a potential security issue. These notes are usually intended for system administrators, but some apply to all users.

Chapter Summary
The Linux operating system grew out of the UNIX heritage to become a popular alternative to traditional systems (that is, Windows) available for microcomputer (PC) hardware. UNIX users will find a familiar environment in Linux. Distributions of Linux contain the expected complement of UNIX utilities, contributed by programmers around the world, including the set of tools developed as part of the GNU Project. The Linux community is committed to the continued development of this system. Support for new microcomputer devices and features is added soon after the hardware becomes available, and the tools available on Linux continue to be refined. Given the many commercial software packages available to run on Linux platforms and the many hardware manufacturers offering Linux on their systems, it is clear that the system has evolved well beyond its origin as an undergraduate project to become an operating system of choice for academic, commercial, professional, and personal use.

Exercises
1. What is free software? List three characteristics of free software.

www.it-ebooks.info

Exercises 21

2. Why is Linux popular? Why is it popular in academia? 3. What are multiuser systems? Why are they successful? 4. What is the Free Software Foundation/GNU? What is Linux? Which parts of the Linux operating system did each provide? Who else has helped build and refine this operating system? 5. In which language is Linux written? What does the language have to do with the success of Linux? 6. What is a utility program? 7. What is a shell? How does it work with the kernel? With the user? 8. How can you use utility programs and a shell to create your own applications? 9. Why is the Linux filesystem referred to as hierarchical? 10. What is the difference between a multiprocessor and a multiprocessing system? 11. Give an example of when you would want to use a multiprocessing system. 12. Approximately how many people wrote Linux? Why is this project unique? 13. What are the key terms of the GNU General Public License?

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

I

PART I Installing Fedora/RHEL Linux
CHAPTER 2 Installation Overview 25 51 CHAPTER 3 Step-by-Step Installation

23

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

2
Installation Overview

C 2 hapter2

In This Chapter
More Information . . . . . . . . . . . . . . 27 Planning the Installation . . . . . . . . 28 Setting Up the Hard Disk . . . . . . . . 34 LVM: Logical Volume Manager . . . . 42 The Installation Process . . . . . . . . . 43 Downloading and Burning a CD/DVD . . . . . . . . . . . . . . . . . . . . 44 Using BitTorrent . . . . . . . . . . . . . . . 47 Gathering Information About the System . . . . . . . . . . . . . . . . . . . . . 48

Objectives
After reading this chapter you should be able to: Choose the best distribution and version to meet your needs Download the ISO image for the install DVD or the Live CD Burn the CD/DVD ISO images to physical media Use a live session to test hardware and check system requirements List considerations of disk partitioning Describe advantages and disadvantages of software RAID and LVM partitioning schemes

25

www.it-ebooks.info

26 Chapter 2 Installation Overview

Installing Fedora/RHEL is the process of copying operating system files from a CD, DVD, or USB flash drive to hard disk(s) on a system and setting up configuration files so Linux runs properly on the hardware. Several types of installations are possible, including fresh installations, upgrades from older releases of Fedora/RHEL, and dual-boot installations. This chapter discusses the installation process in general: planning, partitioning the hard disk, obtaining the files for the installation, burning a CD or a DVD, and collecting information about the hardware that might be helpful for installation and administration. Chapter 3 covers the process of installing Fedora/RHEL. Anaconda is a user-friendly tool that installs Fedora/RHEL. To install Fedora/RHEL on standard hardware, you can typically insert a live CD or an install DVD and boot the system. After you answer a few questions, you are done. Of course, sometimes you might want to customize the system or you might be installing on nonstandard hardware: The installer presents you with these kinds of choices as the installation process unfolds. Fedora/RHEL also provides a textual installer that gives you more control over the installation. Refer to “Installing Fedora/RHEL” (page 54) and “Installation Tasks” (page 67) for information about installing and customizing Fedora/RHEL.

The Desktop Live CD and the Install DVD
Live CD

This book refers to the Fedora Desktop Live Media as a live CD. The live CD runs a live session: it runs Fedora without installing it on the hard disk. To boot from a live CD, make sure the computer is set up to boot from a CD; see “BIOS setup” and “CMOS” both on page 29 for more information. When you boot a live CD, it brings up a GNOME desktop: You are running a live session. When you exit from the live session, the system returns to the state it was in before you booted from the CD. If the system has a Linux swap partition (most Linux systems have one; see page 39), the live session uses it to improve its performance but does not otherwise write to the hard disk. You can also install Fedora from a live session. RHEL does not provide a live session. Running a live session is a good way to test hardware and fix a system that will not boot from the hard disk. A live session is ideal for people who are new to Fedora or Linux and want to experiment with Fedora but are not ready to install it on their system.

Saving files during a live session tip You cannot save a file to a live/install CD/DVD as these are readonly media. During a live session, even though you might appear to save a file, it will not be there after you exit from the live session. To save data from a live session, save it to a network share or a USB flash drive, or mail it to yourself. Alternatively, you can boot from a Live USB flash drive with persistent storage; for more information see fedoraproject.org/wiki/FedoraLiveCD/USBHowTo.
Install DVD

This book refers to the Install Media, which is provided as a single DVD (included with this book), as an install DVD. An install DVD does not bring up a desktop

www.it-ebooks.info

More Information 27

before you install Fedora/RHEL. When you boot an install DVD, it brings up a menu that allows you to install Fedora/RHEL. An install DVD gives you more choices when you install Fedora than does a live CD. For example, an install DVD allows you to use the graphical installer or the textual installer (use the text parameter when booting; page 70) or to rescue an installed system (select Rescue installed system from the Welcome menu; page 57). You can also rescue an installed system using the first installation CD or the Net Install CD.
Net Install CD

The Net Install CD (previously called the Net Boot CD) boots a system and displays the same menu as an install DVD (previous). It does not hold the software packages needed to install Fedora/RHEL but does allow you to install a new system from a hard disk or over a network. See askmethod on page 69 for more information.

More Information
In addition to the following references, see “Where to Find Documentation” on page 125 and refer to Appendix B for additional resources.
Web memtest86+:

www.memtest.org Partition HOWTO: tldp.org/HOWTO/Partition LVM Resource Page (includes many links): sourceware.org/lvm2 LVM HOWTO: www.tldp.org/HOWTO/LVM-HOWTO Transmission BitTorrent client: www.transmissionbt.com X.org release information: wiki.x.org Hardware compatibility: hardware.redhat.com RHEL versions: www.redhat.com/rhel/compare Upgrading: fedoraproject.org/wiki/YumUpgradeFaq Boot command-line parameters: fedoraproject.org/wiki/Anaconda/Options and www.tldp.org/HOWTO/BootPrompt-HOWTO.html Release notes: docs.fedoraproject.org/en-US/Fedora/15/html/Release_Notes Swap space: docs.fedoraproject.org/en-US/Fedora/15/html/Installation_Guide/ s2-diskpartrecommend-x86.html Burning a CD/DVD: docs.fedoraproject.org/en-US/Fedora/15/html/Installation_Guide/ sn-making-media.html#sn-making-disc-media Installing from a USB flash drive: docs.fedoraproject.org/en-US/Fedora/15/html/ Installation_Guide/Making_USB_Media.html RAID: raid.wiki.kernel.org/index.php/Linux_Raid PXE: www.kegel.com/linux/pxe.html Easiest download: fedoraproject.org/get-fedora Downloads: fedoraproject.org/en/get-fedora-all Torrents: torrent.fedoraproject.org Fedora and RHEL Mirrors: mirrors.fedoraproject.org/publiclist RHEL extra packages: fedoraproject.org/wiki/EPEL Spins: spins.fedoraproject.org

Download Fedora/RHEL

www.it-ebooks.info

28 Chapter 2 Installation Overview

Planning the Installation
The major decision when planning an installation is determining how to divide the hard disk into partitions or, in the case of a dual-boot system, where to put the Linux partitions. Once you have installed Fedora/RHEL, you can decide which software packages you want to add to the base system (or whether you want to remove some). In addition to these topics, this section discusses hardware requirements for Fedora/RHEL and fresh installations versus upgrades.

Considerations
SELinux

SELinux (Security-Enhanced Linux) improves system security by implementing mandatory access control policies in the Fedora/RHEL kernel (page 459). By default, Fedora installs SELinux in Enforcing mode. If you do not plan to use SELinux, you can change it to Permissive mode once the system is installed. Because SELinux sets extended attributes on files, it can be a time-consuming process to enable SELinux on a system on which it has been turned off. On most systems, except for servers, you probably want to install a graphical user interface (a desktop). Fedora/RHEL installs GNOME by default. As you install more software packages on a system, the number of updates and the interactions between the packages increase. Server packages that listen for network connections make the system more vulnerable by increasing the number of ways the system can be attacked. Including additional services can also slow the system down. If you want a development system or a system to learn on, additional packages and services might be useful. For a more secure production system, it is best to install and maintain the minimum number of packages required and to enable only needed services. See page 426 for information on the systemd Fedora init daemon and page 436 for information on the RHEL Upstart init daemon, both of which start and stop system services.

GUI

Software and services

Requirements
Hardware

This chapter and Chapter 3 cover installing Fedora/RHEL on 32-bit Intel and compatible processor architectures (including AMD processors) as well as 64-bit processor architectures such as AMD64 processors and Intel processors with Intel EM64T technology. Within these processor architectures, Fedora/RHEL runs on much of the available hardware. You can view Fedora/RHEL’s list of compatible and supported hardware at hardware.redhat.com. Although these lists pertain to RHEL, they serve as a good guide to what Fedora will run on. The release notes also provide hardware information (see the install DVD, the first install CD, or the Fedora/RHEL Web sites). Many Internet sites discuss Linux hardware; use Google (www.google.com) to search for linux hardware, fedora hardware, or linux and the specific hardware you want more information on (for example, linux sata or linux a8n). In addition, many HOWTOs cover specific hardware. The Linux Hardware Compatibility HOWTO is also available, although it might not be up-to-date at

www.it-ebooks.info

Planning the Installation

29

the time you read it. Fedora/RHEL usually runs on the same systems Windows runs on, unless the system includes a very new or unusual component. The hardware required to run Fedora/RHEL depends on which kind of system you want to set up. A very minimal system that runs a textual (command-line) interface and has very few software packages installed requires very different hardware from a system that runs a GUI, an Apache Web server, and has many installed packages. A network connection is invaluable for keeping Fedora/RHEL up-to-date. A sound card (or a sound chip on the motherboard) is nice to have for multimedia applications. If you are installing Fedora on old or minimal hardware and want to run a GUI, consider installing LXDE (spins.fedoraproject.org/lxde), as it provides a lightweight desktop that uses system resources more efficiently than GNOME does.
CPU (processor)

At a minimum, Fedora requires an Intel Pentium Pro processor; it is optimized for i686 and later processors. Minimum recommended processor speed for a textual system is 200 megahertz and for a graphical system is 400 megahertz. See www.redhat.com/rhel/compare for information on RHEL systems. Memory requirements are the same for installing 32-bit and 64-bit Fedora systems. See the tip regarding gibibytes/mebibytes versus gigabytes/megabytes on page 38. At a minimum, a textual (command-line) system requires 256 mebibytes of RAM and a graphical (desktop) system requires 384-512 mebibytes of RAM. In some cases you can get by with less RAM for a very minimal textual installation. See www.redhat.com/rhel/compare for information on RHEL systems. Running a live CD requires a minimum of 256 mebibytes of RAM. Linux makes good use of extra memory: The more memory a system has, the faster it runs. Adding memory is one of the most cost-effective ways you can speed up a Linux system.

RAM (memory)

Hard disk space

The amount of hard disk space Fedora/RHEL requires depends on which edition of Fedora/RHEL you install, which packages you install, how many languages you install, and how much space you need for user data (your files). The Fedora operating system typically requires 2–8 gibibytes, although a minimal system can make do with as little as 90 mebibytes. User data requires additional space. Leave at least five percent free space for proper filesystem operation. See www.redhat.com/rhel/compare for information on RHEL systems. Modern computers can be set up to boot from a CD/DVD, hard disk, or USB flash drive. The BIOS determines the order in which the system tries to boot from each device. You might need to change this order: Make sure the BIOS is set up to try booting from the CD/DVD before it tries to boot from the hard disk. See page 595 for more information. CMOS is the persistent memory that stores hardware configuration information. To change the BIOS setup, you need to edit the information stored in CMOS. When the system boots, it displays a brief message about how to enter System Setup or CMOS Setup mode. Usually you need to press DEL or F2 while the system is booting. Press the key that is called for and then move the cursor to the screen and line that deal with booting the system. Generally there is a list of three or four devices that the system

BIOS setup

CMOS

www.it-ebooks.info

30 Chapter 2 Installation Overview

tries to boot from; if the first attempt fails, the system tries the second device, and so on. Manipulate the list so the CD/DVD is the first choice, save the list, and reboot. Refer to the hardware/BIOS manual for more information.

Processor Architecture
Fedora/RHEL CDs and DVDs hold programs compiled to run on a specific processor architecture (class of processors, or CPUs). The following list describes each of the architectures Fedora/RHEL is compiled for. See docs.fedoraproject.org/en-US/ Fedora/15/html/Installation_Guide/ch-new-users.html#sn-which-arch for a detailed list of processors in each architecture. Because Linux source code is available to everyone, a knowledgeable user can compile Fedora/RHEL to run on other processor architectures.

Should I install 32-bit or 64-bit Fedora/RHEL on a 64-bit-capable processor? tip The following information might help you decide whether to install 32-bit or 64-bit Fedora/RHEL on a 64-bit-capable processor. • EM64T/AMD64 processors can run either version of Fedora/RHEL equally well. • A 64-bit distribution allows each process to address more than 4 gigabytes of RAM. Larger address space is the biggest advantage of a 64-bit distribution. It is typically useful only for certain engineering/scientific computational work and when you are running multiple virtual machines. • A 64-bit processor is not faster than a 32-bit processor in general; most benchmarks show more or less similar performance. In some cases the performance is better and in some cases it is worse: There is no clear performance advantage for either type of processor. • The memory model for 64-bit Linux makes pointers twice as big as those in 32-bit Linux. This size difference translates to a more than 5 percent RAM usage increase, depending on the application. If a system is low on RAM, this overhead might make performance worse. • ASLR (Address Space Layout Randomization) works better with the larger address space provided by 64-bit Fedora/RHEL. ALSR can help improve system security. See en.wikipedia.org/wiki/Address_space_layout_randomization. • Some multimedia encoders run 10–30 percent faster under 64-bit Fedora/RHEL. • Because more people are using 32-bit Linux, bugs in 32-bit Linux tend to be discovered and fixed faster than those in 64-bit Linux. • Fedora/RHEL can set up Flashplayer and Java with a single click on 64-bit systems just as it can on 32-bit systems. However, for some applications, such as Skype, you must apply ugly workarounds to run them on 64-bit systems. • Some features of proprietary third-party applications are not available for 64-bit architecture. • There is no simple way to go back and forth between 32-bit and 64-bit versions of Fedora/RHEL without reinstalling Fedora/RHEL. • If you are not sure which distribution to use, install the 32-bit version of Fedora/RHEL.

www.it-ebooks.info

Planning the Installation i386/i686 (Intel x86)

31

Software on an Fedora/RHEL 32-bit PC CD/DVD is compiled to run on Intel x86compatible processors, including most machines with Intel and AMD processors, almost all machines that run Microsoft Windows, and newer Apple Macintosh machines that use Intel processors. The N and Z Series Atom processors are also based on this architecture. If you are not sure which type of processor a machine has, assume it has this type of processor. Software on a Fedora/RHEL 64-bit PC CD/DVD is compiled to run on AMD64 processors, including the Athlon64, Opteron, and Intel 64-bit processors that incorporate EM64T technology, such as the EMT64 Xeon. The 230 and 330 Series Atom processors are also based on this architecture. The Fedora/RHEL PPC release runs on the Apple Macintosh G3, G4, G5, PowerBook, and PPC-based Macintoshes.

x86_64 (AMD64 and Intel EM64T)

Mac PowerPC (ppc)

Interfaces: Installer and Installed System
When you install Fedora/RHEL, you have a choice of interfaces to use while you install it (to work with the installer). You also have a choice of interfaces to use to work with the installed system. This section describes the two basic interfaces: textual and graphical.
Textual (TUI/CLI)

A textual user interface (TUI), also called a command-line interface (CLI) or characterbased interface, displays characters and some simple graphical symbols. It is line oriented; you give it instructions using a keyboard only. A graphical user interface (GUI) typically displays a desktop (such as GNOME) and windows; you give it instructions using a mouse and keyboard. You can run a textual interface within a GUI by opening a terminal emulator window (page 117). A GUI uses more computer resources (CPU time and memory) than a textual interface does. A pseudographical interface is a textual interface that takes advantage of graphical elements on a text-based display device such as a terminal. It might also use color. This interface uses text elements, including simple graphical symbols, to draw rudimentary boxes that emulate GUI windows and buttons. Pressing the TAB key frequently moves the cursor from one element to the next and pressing the RETURN key selects the element the cursor is on. A GUI is user friendly, whereas the textual interface is compact, uses fewer system resources, and can work on a text-only terminal or over a text-only connection. Because it is more efficient, a textual interface is useful for older, slower systems and systems with minimal amounts of RAM. Server systems frequently use a textual interface because it allows the system to dedicate more resources to carrying out the job it is set up to do and fewer resources to pleasing the system administrator. Not running a GUI can also improve system security. Even though it uses a graphical interface, Fedora/RHEL’s live installer installs Fedora/RHEL faster than the textual installer. The live installer copies an installed system image to the hard disk and then sets up the system, whereas the textual installer uses yum and RPM to unpack hundreds of packages one by one.

Graphical (GUI)

Pseudographical

Advantages

www.it-ebooks.info

32 Chapter 2 Installation Overview

Figure 2-1
Installer interfaces

Graphical (left) and textual (pseudographical, inset) installers

Fedora/RHEL provides a user-friendly installer named Anaconda that works in both graphical and pseudographical modes. Used in pseudographical mode, Anaconda offers more options and gives you greater control over the installation (Figure 2-1). Both interfaces accomplish the same task: They enable you to tell the installer how you want it to configure Fedora/RHEL.

Which Are You Installing: Fedora or Red Hat Enterprise Linux?
This book describes two products: Fedora and RHEL (Red Hat Enterprise Linux). This section briefly highlights the differences between these products.
Fedora

The Fedora Project is sponsored by Red Hat and supported by the open-source community. With releases, called Fedora, coming out about every six months, this Linux distribution incorporates cutting-edge code. It is not a supported Red Hat product and is not recommended for production environments where the set of software packages and features must remain constant over a longer period of time. Fedora aims to reflect the upstream projects it incorporates, including the kernel. It is widely regarded as the most stable “free” Linux distribution. In contrast, RHEL includes many changes introduced by Fedora. Red Hat Enterprise Linux is typically sold through an annual subscription that includes access to the Red Hat Network (RHN; page 554) and technical support. It is more stable but less cutting edge than Fedora. RHEL provides at least 7 years of updates; Fedora provides 13 months.

RHEL

www.it-ebooks.info

Planning the Installation

33

Red Hat Enterprise Linux comes in several configurations; for more information see www.redhat.com/wapps/store/catalog.html. See www.redhat.com/rhel/compare for more information on the various versions of RHEL.
CentOS

CentOS (www.centos.org) is a free, RHEL-compatible Linux distribution. It has more long-term stability than Fedora but has less support than RHEL. Because CentOS is RHEL compatible, this book also describes CentOS.

Fedora/RHEL Releases
The Fedora Project distributes a new release of Fedora about every six months. Each release has both a number and a name. For example, Fedora 15 is named Lovelock and Fedora 16 is named Verne. Previous versions were named Constantine (12), Goddard (13), and Laughlin (14). See fedoraproject.org/wiki/Releases/Names for a description of how names are chosen. Red Hat distributes a new release of RHEL every few years. For information on the RHEL life cycle see access.redhat.com/support/policy/updates/errata.

Fedora Standard Versions
Standard versions of Fedora are compiled for i686 and x86_64 architectures and are available via BitTorrent, direct download, and mirrors. To download a standard version visit fedoraproject.org/en/get-fedora-all.
Install media

Install media is available as an install DVD or a series of install CDs. From these media you can install a graphical or textual system using a graphical or textual installer. Installing from install media gives you many more options than installing from a live session (next). You can also use install media to boot a minimal textual system you can use to rescue an installed system. Desktop live media is available as a live CD. From this CD you can run a live session. From the live session you can install a basic Fedora system. Once installed, you can use an Internet connection to add more software packages to the system. The KDE live media is the same as the desktop live media except it runs a KDE live session instead of a GNOME live session. When you install from KDE live media you install a KDE-based Fedora system.

Desktop live media

KDE live media

Fedora Spins
In addition to the standard versions, Fedora releases many alternative versions called spins. Some of the most popular spins are • KDE—A complete, modern desktop built using the KDE Plasma Desktop • LXDE—A light, fast, less-resource hungry desktop environment • XFCE—A complete, well-integrated Xfce desktop • Security—Security analysis tools • Games—Includes many games See spins.fedoraproject.org for a complete list of spins.

www.it-ebooks.info

34 Chapter 2 Installation Overview

Installing a Fresh Copy or Upgrading an Existing Fedora/RHEL System?
Clean install

An installation, sometimes referred to as a clean install, writes all fresh data to a disk. The installation program overwrites all system programs and data as well as the kernel. You can preserve some user data during an installation depending on where it is located and how you format/partition the hard disk. Alternatively, you can perform a clean install on an existing system without overwriting data by setting up a dual-boot system (page 82). An upgrade replaces all installed software packages with the most recent version available on the new release. During an upgrade, the installation program preserves both system configuration and user data files. An upgrade brings utilities that are present in the old release up-to-date and installs new utilities. Before you upgrade a system, back up all files on the system. In general, all new features are provided by an upgrade.

Upgrade

Setting Up the Hard Disk
Free space

A hard disk must be prepared in several ways so Linux can write to and read from it. Low-level formatting is the first step in preparing a disk for use. You do not need to perform this task, as it is done at the factory where the hard disk is manufactured. The next steps in preparing a hard disk for use are to write a partition table to it and to create partitions on the disk. Finally, you need to create a filesystem on each partition. The area of a partitioned disk that is not occupied by partitions is called free space. A new disk has no partition table, no partitions, and no free space. Under DOS/Windows, the term formatting means creating a filesystem on a partition; see “Filesystems” below. A partition, or slice, is a logical section of a hard disk that has a device name, such as /dev/sda1, so you can refer to it separately from other sections. For normal use, you must create at least one partition on a hard disk. From a live session before you install Fedora/RHEL, you can use palimpsest, the GNOME Disk Utility (page 77), to view, resize, and create partitions on an existing system. During installation, you can use the Disk Druid partition editor (page 71) to create partitions. After installation, you can use palimpsest, parted (page 617), or fdisk to manipulate partitions. See /dev on page 503 for more information on device names. A partition table holds information about the partitions on a hard disk. Before the first partition can be created on a disk, the program creating the partition must set up an empty partition table on the disk. As partitions are added, removed, and modified, information about these changes is recorded in the partition table. If you remove the partition table, you can no longer access information on the disk except by extraordinary means.

Partitions

Partition table

www.it-ebooks.info

Planning the Installation
LVM

35

By default, during installation Disk Druid sets up logical volumes (LVs) that function like partitions. With LVs, you can use the Logical Volume Manager (LVM; page 42) to change the sizes of volumes easily after the system is installed. Using LVM to manipulate LVs is more convenient than working with one of the tools that manipulates partitions. Before most programs can write to a partition, a data structure (page 1160), called a filesystem, needs to be written to the partition. This data structure holds inodes (page 515) that map locations on the disk that store files to the names of the files. At the top of the data structure is a single unnamed directory. As will be explained shortly, this directory joins the system directory structure when the filesystem is mounted. When the Fedora/RHEL installer creates a partition, it automatically writes a filesystem to the partition. You can use the mkfs (make filesystem; page 472) utility, which is similar to the DOS/Windows format utility, to manually create a filesystem on a partition. Table 12-1 on page 519 lists some common types of filesystems. Fedora/RHEL typically creates ext4 filesystems for data; unless you have reason to use another filesystem type, use ext4. Windows uses FAT16, FAT32, and NTFS filesystems. Apple uses HFS (Hierarchical Filesystem) and HFS+. OS X uses either HFS+ or UFS. Different types of filesystems can coexist in different partitions on a single hard disk, including both Windows and Linux filesystems.

Filesystems

Primary, Extended, and Logical Partitions
You can divide an IDE/ATA/SATA disk into a maximum of 63 partitions and a SCSI disk into a maximum of 15 partitions. You can use each partition independently for swap devices, filesystems, databases, other resources, and even other operating systems.
Primary and extended partitions

Unfortunately, disk partitions follow the template established for DOS machines a long time ago. At most, a disk can hold four primary partitions. You can divide one (and only one) of these primary partitions into multiple logical partitions; this divided primary partition is called an extended partition. If you want more than four partitions on a drive—and you frequently do—you must set up an extended partition. A typical disk is divided into three primary partitions (frequently numbered 1, 2, and 3) and one extended partition (frequently numbered 4). The three primary partitions are the sizes you want the final partitions to be. The extended partition occupies the rest of the disk. Once you establish the extended partition, you can subdivide it into additional logical partitions (numbered 5 or greater), each of which is the size you want. You cannot use the extended partition (number 4)—only the logical partitions it holds. Figure 16-3 on page 619 illustrates the disk described in this paragraph. See the Linux Partition HOWTO (tldp.org/HOWTO/Partition) for more information.

www.it-ebooks.info

36 Chapter 2 Installation Overview

The Linux Directory Hierarchy
Skip this section for a basic installation tip This section briefly describes the Linux directory hierarchy so you might better understand some of the decisions you might need to make when you divide the hard disk into partitions while installing Linux. You do not have to read this section to install Linux. You can use default partitioning (page 62) to set up the disk and return to this section when and if you want to. See the beginning of Chapter 6 for a more thorough explanation of the Linux directory hierarchy.
Namespace Windows versus Linux

A namespace is a set of names (identifiers) in which each name is unique. As differentiated from a Windows machine, a Linux system presents a single namespace that holds all files, including directories, on the local system. The Linux system namespace is called the directory hierarchy or directory tree. Under Windows, C:\ is a separate namespace from D:\. The directory hierarchy rooted at C:\ is separate from the directory hierarchy rooted at D:\ and there is no path or connection between them. Under Linux, the single system namespace is rooted at /, which is the root directory. Under the root directory are top-level subdirectories such as bin, boot, etc, home, and usr. All files on a Linux system, including directories, have a unique identifier called an absolute pathname. An absolute pathname traces a path through the directory hierarchy starting at the root directory and ending at the file or directory identified by the pathname. Thus the absolute pathname of the top-level directory named home is /home. For more information refer to “Absolute Pathnames” on page 192. Within a pathname, a slash (/) follows (appears to the right of) the name of a directory. Thus /home/sam specifies that the ordinary or directory file named sam is located in the directory named home, which is a subdirectory of the root directory (/). The pathname /home/sam/ (with a trailing slash) explicitly specifies sam is a directory file. In most instances this distinction is not important. The root directory is implied when a slash appears at the left end of a pathname or when it stands alone. The Linux system namespace comprises the set of absolute pathnames of all files, including directories, in the directory hierarchy of a system.

Absolute pathnames

Slashes (/) in pathnames

Linux system namespace

Mount Points
A filesystem on a partition holds no information about where it will be mounted in the directory hierarchy (the top-level directory of a filesystem does not have a name). When you use the installer to create most partitions, you specify the type of filesystem to be written to the partition and the name of a directory that Fedora/RHEL associates with the partition. Mounting a filesystem associates the filesystem with a directory in the directory hierarchy. You can mount a filesystem on any directory in the directory hierarchy. The directory that you mount a filesystem on is called a mount point. The directory you specify when you use the installer to create a partition is the mount point for

www.it-ebooks.info

Planning the Installation

37

the partition. Most mount points are top-level subdirectories, with a few exceptions (such as /usr/local, which is frequently used as a mount point).

Do not create files on mount points before mounting a filesystem caution Do not put any files in a directory that is a mount point while a filesystem is not mounted on that mount point. Any files in a directory that is used as a mount point are covered up while the filesystem is mounted on that directory; you will not be able to access them. They reappear when the filesystem is unmounted.

For example, suppose the second partition on the first hard disk has the device name /dev/sda2. To create an ext4 filesystem that you want to appear as /home in the directory hierarchy, you must instruct Linux to mount the /dev/sda2 partition on /home when the system boots. With this filesystem mounted on its normal mount point, you can access it as the /home directory.
Filesystem independence

The state of one filesystem does not affect other filesystems: One filesystem on a drive might be corrupt and unreadable, while other filesystems function normally. One filesystem might be full so you cannot write to it, while others have plenty of room for more data. The file that holds the information relating partitions to mount points is /etc/fstab (filesystem table; page 524). The associations stored in the fstab file are the normal ones for the system, but you can easily override them. When you work in recovery mode, you might mount a filesystem on the /target directory so you can repair the filesystem. For example, if you mount on /target the partition holding the filesystem normally mounted on /home, the directory you would normally find at /home/sam will be located at /target/sam. A partition and any filesystem it holds have no name or identification other than a device name (and a related UUID value—see page 524). Instead, the partition and the filesystem are frequently referred to by the name of the partition’s normal mount point. Thus “the /home partition” and “the /home filesystem” refer to the partition that holds the filesystem normally mounted on the /home directory. See page 520 for more information on mounting filesystems.

/etc/fstab

Naming partitions and filesystems

Partitioning a Disk
During installation, the installer calls the Disk Druid partition editor to set up disk partitions. This section discusses how to plan partition sizes. Although this section uses the term partition, planning and sizing LVs (logical volumes; page 42) works the same way. For more information refer to page 71 and to the Linux Partition HOWTO at www.tldp.org/HOWTO/Partition.

Default Partitioning
It can be difficult to plan partition sizes appropriately if you are not familiar with Linux. During installation, Fedora/RHEL provides default partitioning. Without asking any questions, default partitioning divides the portion of the disk allotted to Fedora/RHEL into three or four partitions. One partition is a small /boot partition,

www.it-ebooks.info

38 Chapter 2 Installation Overview

and one is the swap partition which can be any size from 512 megabytes to 2 or more gigabytes. Another partition is designated as / (root) and occupies up to about 50 gigabytes. If enough disk space is available, default partitioning creates a partition designated as /home. This partition takes up the remainder of the disk space. The next section discusses the advantages of manual partitioning.

GiB versus GB tip Historically a gigabyte (GB) meant either 230 (1,073,741,824) or 109 (1,000,000,000) bytes.
Recently the term gibibyte (giga binary byte; abbreviated as GiB) has been used to mean 230 bytes; in turn, gigabyte is used more frequently to mean 109 bytes. Similarly, a mebibyte (MiB) is 220 (1,048,576) bytes. The Fedora/RHEL partitioner still uses megabytes and gigabytes for specifying the size of partitions.

Manual Partitioning: Planning Partitions
This section discusses additional partitions you might want to create. Consider setting up LVM (page 42) before you create partitions; LVM allows you to change partition sizes easily after the system is installed. Under Fedora/RHEL, default partitioning sets up LVM. If you decide to manually partition the hard disk and set up partitions other than a root partition (/), a swap partition, and possible a /home partition, first consider which kinds of activities will occur under each top-level subdirectory. Then decide whether it is appropriate to isolate that subdirectory by creating a filesystem in a partition and mounting it on its own mount point. Advantages of creating additional filesystems include the following points: • Separating data that changes frequently (e.g., /var and /home) from data that rarely changes (e.g., /usr and /boot) can reduce fragmentation on the less frequently changing filesystems, helping to maintain optimal system performance. • Isolating filesystems (e.g., /home) can preserve data when you reinstall Linux. • Additional filesystems can simplify backing up data on a system. • If all directories are part of a single filesystem, and if a program then runs amok or the system is the target of a DoS attack (page 1162), the entire disk can fill up. System accounting and logging information, which might contain data that can tell you what went wrong, might be lost. On a system with multiple filesystems, such problems typically fill a single filesystem and do not affect other filesystems. Data that might help determine what went wrong will likely be preserved and the system is less likely to crash.
/ (root)

The following paragraphs discuss the advantages of making each of the major top-level subdirectories a separate, mountable filesystem. Any directories you do

www.it-ebooks.info

Planning the Installation

39

not create filesystems for automatically become part of the root (/) filesystem. For example, if you do not create a /home filesystem, /home is part of the root (/) filesystem.
(swap)

Linux temporarily stores programs and data on a swap partition when it does not have enough RAM to hold all the information it is processing. The swap partition is also used when a system hibernates (is suspended to disk). The size of the swap partition should be 2 gigabytes for systems with up to 4 gigabytes of RAM, 4 gigabytes for 4–16 gigabytes of RAM, and 8 gigabytes for 16–64 gigabytes of RAM. The worst-case hibernation requires a swap size that is one and a half times the size of RAM. For example, a system with 1 gigabyte of RAM should have a 2-gigabyte swap partition. Although a swap partition is not required, most systems perform better when one is present. On a system with more than one drive, having swap partitions on each drive can improve performance even further. A swap partition is not mounted, so it is not associated with a mount point. See swap on page 513 for more information. The /boot partition holds the kernel and other data the system needs when it boots. This partition is typically approximately 500 megabytes, although the amount of space required depends on how many kernel images you want to keep on hand. It can be as small as 100 megabytes. Although you can omit the /boot partition, it is useful in many cases. Many administrators put an ext2 filesystem on this partition because the data on it does not change frequently enough to justify the overhead of the ext4 journal. Systems that use software RAID (page 41) or LVM (page 42) require a separate /boot partition. Some BIOSs, even on newer machines, require the /boot partition [or the / (root) partition if there is no /boot partition] to appear near the beginning of the disk (page 595).

/boot

Where to put the /boot partition caution On some systems, the /boot partition must reside completely below cylinder 1023 of the hard disk.
An easy way to ensure compliance with this restriction is to make the /boot partition one of the first partitions on the disk. When a system has more than one hard disk, the /boot partition must also reside on a drive in the following locations: • Multiple IDE or EIDE drives: the primary controller • Multiple SCSI drives: ID 0 or ID 1 • Multiple IDE and SCSI drives: the primary IDE controller or SCSI ID 0
/var

The name var is short for variable: The data in this partition changes frequently. Because it holds the bulk of system logs, package information, and accounting data, making /var a separate partition is a good idea. Then, if a user runs a job that consumes all of the users’ disk space, system log files in /var/log will not be affected. The /var partition can occupy from 500 megabytes to as much as several gigabytes for

www.it-ebooks.info

40 Chapter 2 Installation Overview

extremely active systems with many verbose daemons and a lot of printer and mail activity (the print queues reside in /var/spool/cups and incoming mail is stored in /var/mail). For example, software license servers are often extremely active systems. By default, Apache content (the Web pages it serves) is stored on /var under Fedora/RHEL; you might want to change the location Apache uses. Although such a scenario is unlikely, many files or a few large files might be created under the /var directory. Creating a separate filesystem to hold the files in /var will prevent these files from overrunning the entire directory structure, bringing the system to a halt, and possibly creating a recovery problem.
/var/log

Some administrators choose to put the log directory in a separate partition to isolate system logs from other files in the /var directory. It is a common strategy to put user home directories on their own filesystem. Such a filesystem is usually mounted on /home. Having /home as a separate filesystem allows you to perform a clean install without risking damage to or loss of user files. Also, having a separate /home filesystem prevents a user from filling the directory structure with her data; at most she can fill the /home filesystem, which will affect other users but not bring the system down.

/home

Set up partitions to aid in making backups tip Plan partitions based on which data you want to back up and how often you want to back it up.
One very large partition can be more difficult to back up than several smaller ones.
/usr

Separating the /usr partition can be useful if you plan to export /usr to another system and want the security that a separate partition can give. Many administrators put an ext2 filesystem on this partition because the data on it does not change frequently enough to justify the overhead of the ext4 journal. The size of /usr depends on the number of packages you install. On a default system, it is typically 2–4 gigabytes. Both /usr/local and /opt are candidates for separation. If you plan to install many packages in addition to Fedora/RHEL, such as on an enterprise system, you might want to keep them on a separate partition. If you install the additional software in the same partition as the users’ home directories, for example, it might encroach on the users’ disk space. Many sites keep all /usr/local or /opt software on one server; from there, they export the software to other systems. If you choose to create a /usr/local or /opt partition, its size should be appropriate to the software you plan to install. Table 2-1 gives guidelines for minimum sizes for partitions used by Linux. Set the sizes of other partitions, such as those for /home, /opt, and /usr/local, according to need and the size of the hard disk. If you are not sure how you will use additional disk space, you can create extra partitions using whatever names you like (for example, /b01, /b02, and so on). Of course, you do not have to partition the entire drive when you install Linux; you can wait until later to divide the additional space into partitions.

/usr/local and /opt

www.it-ebooks.info

Planning the Installation

41

Table 2-1 Example minimum partition sizesa
Partition
/boot / (root) (swap) /home /tmp /usr /var

Example size
100–500 megabytes 1 gigabyte 2–8 gigabytes (page 39) As large as necessary; depends on the number of users and the type of work they do Minimum of 500 megabytes Minimum of 2–16 gigabytes; depends on which and how many software packages you install Minimum of 500 megabytes—much larger if you are running a server

a. The sizes in this table assume you create all partitions separately. For example, if you create a 1-gigabyte / (root) partition and do not create a /usr partition, in most cases you will not have enough room to store all of the system programs.

RAID
RAID (Redundant Array of Inexpensive/Independent Disks) employs two or more hard disk drives or partitions in combination to improve fault tolerance and/or performance. Applications and utilities see these multiple drives/partitions as a single logical device. RAID, which can be implemented in hardware or software (Fedora/RHEL gives you this option), spreads data across multiple disks. Depending on which level you choose, RAID can provide data redundancy to protect data in the case of hardware failure. Although it can improve disk performance by increasing read/write speed, software RAID uses quite a bit of CPU time, which might be a consideration. True hardware RAID requires hardware designed to implement RAID and is not covered in this book (but see “Fake RAID” on the next page).

RAID does not replace backups caution The purposes of RAID are to improve performance and/or to minimize downtime in the case of a disk failure. RAID does not replace backups. Do not use RAID as a replacement for regular backups. If the system experiences a catastrophic failure, RAID is useless. Earthquake, fire, theft, and other disasters might leave the entire system inaccessible (if the hard disks are destroyed or missing). RAID also does not take care of the simple case of replacing a file when a user deletes it by accident. In these situations, a backup on a removable medium (which has been removed) is the only way you will be able to restore a filesystem.

www.it-ebooks.info

42 Chapter 2 Installation Overview

RAID can be an effective addition to a backup. Fedora/RHEL offers RAID software that you can install either when you install a Fedora/RHEL system or as an afterthought. The Linux kernel automatically detects RAID arrays (sets of partitions) at boot time if the partition ID is set to 0xfd (raid autodetect). Software RAID, as implemented in the kernel, is much cheaper than hardware RAID. Not only does this approach avoid the need for specialized RAID disk controllers, but it also works with the less expensive ATA disks as well as SCSI disks.
Fake RAID

Fedora/RHEL does not officially support motherboard-based RAID (known as fake RAID) but accepts it through the dmraid driver set. Linux software RAID is almost always better than fake RAID. Visit help.ubuntu.com/community/FakeRaidHowto for background information on fake RAID. The partition editor gives you the choice of implementing RAID level 0, 1, or 5, 6, or 10. For levels 1 and 5, be sure to put member partitions on different drives. That way, if one drive fails, the data will be preserved. • RAID level 0 (striping)—Improves performance but offers no redundancy. The storage capacity of the RAID device is equal to that of the member partitions or disks. • RAID level 1 (mirroring)—Provides simple redundancy, improving data reliability, and can improve the performance of read-intensive applications. The storage capacity of the RAID device is equal to one of the member partitions or disks. • RAID level 5 (disk striping with parity)—Provides redundancy and improves performance (most notably, read performance). The storage capacity of the RAID device is equal to that of the member partitions or disks, minus one of the partitions or disks (assuming they are all the same size). • RAID level 6 (disk striping with double parity)—Improves upon level 5 RAID by protecting data when two disks fail at once. Level 6 RAID is inefficient with a small number of drives. • RAID level 10 (mirroring and striping)—A combination of RAID 1 and RAID 0 (also called RAID 1+0), RAID 10 uses mirroring to improve fault tolerance and striping to improve performance. Multiple RAID 1 arrays (mirroring) are overlaid with a RAID 0 array (striping). The storage capacity of the RAID device is equal to one-half that of the member partitions or disks. You must use at least four partitions or disks. For more information see raid.wiki.kernel.org/index.php/Linux_Raid.

LVM: Logical Volume Manager
The Logical Volume Manager (LVM2, which this book refers to as LVM) allows you to change the size of logical volumes (LVs, the LVM equivalent of partitions) on the fly. With LVM, if you make a mistake in setting up LVs or if your needs change, you can make LVs either smaller or larger without affecting user data. You must choose to use LVM at the time you install the system or add a hard disk; you cannot

www.it-ebooks.info

The Installation Process

43

Disk A 40 GB

Disk B 80 GB

Disk C 40 GB

Disk D 20 GB

Physical volumes (PVs)

boot
40 + 80 + 40 + 20 GB (– boot partition)

Boot partition Volume group (VG)

/ (root)

home

var

usr

(swap)

Logical volumes (LVs)

Figure 2-2

LVM: Logical Volume Manager

retroactively apply it to a disk full of data. LVM supports IDE and SCSI drives as well as multiple devices such as those found in RAID arrays. LVM groups disk components (partitions, hard disks, or storage device arrays), called physical volumes (PVs), into a storage pool, or virtual disk, called a volume group (VG). See Figure 2-2. You allocate a portion of a VG to create a logical volume (LV). An LV is similar in function to a traditional disk partition in that you can create a filesystem on an LV. It is much easier, however, to change and move LVs than partitions: When you run out of space on a filesystem on an LV, you can grow (expand) the LV and its filesystem into empty or new disk space, or you can move the filesystem to a larger LV. For example, you can add a hard disk to a system and incorporate it into an LV to expand the capacity of that LV. LVM’s disk space manipulation is transparent to users; service is not interrupted. LVM also eases the burden of storage migration. When you outgrow the PVs or need to upgrade them, LVM can move data to new PVs. To read more about LVM, refer to the resources listed on page 27.

The Installation Process
The following steps outline the process of installing Fedora/RHEL from the install DVD. Installation from other media follows similar steps. See Chapter 3 for specifics. 1. Make sure the BIOS is set to boot from a CD/DVD (page 29). Insert the install DVD in and reset the computer. The computer boots from the DVD and displays the install DVD Welcome menu (Figure 3-5, page 57). 2. You can make a selection from the Welcome menu, press the TAB key to display the boot command line (which you can modify), and begin installing Fedora/RHEL when you are ready. One of the menu items checks the installation medium. If you do nothing, the Fedora install DVD starts installing the system after 60 seconds

www.it-ebooks.info

44 Chapter 2 Installation Overview

3. As part of the process of bringing up a live session or installing Fedora/RHEL, Fedora/RHEL creates RAM disks (page 1184) that it uses in place of the hard disk used for a normal boot operation. The installer copies tools required for the installation or to bring up a system from a live CD or an install DVD to the RAM disks. The use of RAM disks allows the installation process to run through the specification and design phases without writing to the hard disk and enables you to opt out of the installation at any point before the last step of the installation. If you opt out before this point, the system is left in its original state. The RAM disks also allow a live session to leave the hard disk untouched. 4. The installer prompts you with questions about how you want to configure Fedora/RHEL. 5. When the installer is finished collecting information, it writes the operating system files to the hard disk. 6. When you reboot the system, Firstboot asks questions that are required to complete the installation (page 65). 7. The Fedora/RHEL system is ready for you to log in and use.

Downloading and Burning a CD/DVD
There are several ways to obtain an Fedora/RHEL CD/DVD. Fedora/RHEL makes available releases of Linux as CD and DVD ISO image files (named after the ISO 9660 standard that defines the CD filesystem). This section describes how to download one of these images and burn a CD/DVD. You can also purchase a CD/DVD from a Web site. If you cannot obtain Fedora/RHEL by any other means, you can point a browser at fedoraproject.org/wiki/Distribution/FreeMedia to display a Web page with links that enable you to request a free Fedora CD.

The Easy Way to Download a CD/DVD ISO Image File
This section explains the easiest way to download a CD/DVD ISO image file. This technique works in most situations; it is straightforward but limited. For example, it does not allow you to use BitTorrent to download the file. To begin, point a browser at fedoraproject.org and click Download Now!. The download should start shortly. If the browser gives you a choice of what to do with the file, save it to the hard disk. The browser saves the ISO image file to the hard disk. Continue reading at “Burning the CD/DVD” on page 48.

You can find ISO images for all supported architectures here tip If you cannot find an ISO image for a CD that supports the type of hardware you want to install
Fedora on, visit mirrors.fedoraproject.org/publiclist. For RHEL visit access.redhat.com and select Downloads and then Evaluations and Demos.

www.it-ebooks.info

Downloading and Burning a CD/DVD 45

Fedora 15 Fedora 15 for i386 architecture

Figure 2-3

The mirrors.fedoraproject.org Web page

Other Ways to Download a CD/DVD ISO Image File
This section explains how to download a DVD image or a release that is not downloaded automatically, and how to download a torrent that enables you to use BitTorrent to download an ISO image file. See “Download Fedora/RHEL” on page 27 for other locations you can download Fedora/RHEL from. Browser When you use a Web browser to download a file, the browser contacts a Web (HTTP) or FTP server and downloads the file from that server. If too many people download files from a server at the same time, the downloads become slower. BitTorrent BitTorrent efficiently distributes large amounts of static data, such as ISO image files. Unlike using a browser to download a file from a single server, BitTorrent distributes the functions of a server over its clients. As each client downloads a file, it becomes a server for the parts of the file it has downloaded. To use BitTorrent, you must download a small file called a torrent (or have a Web browser do it for you). This file, which holds information that allows clients to communicate with one another, has a filename extension of .torrent. As more people use a torrent to download a file at the same time, the downloads become faster. Downloading an ISO image file using BitTorrent is covered later in this section. Mirrors Many sites mirror (hold copies of) the Fedora ISO image files and BitTorrent torrent files. Some mirrors use HTTP, while others use FTP; you can use a browser to download files from either type of site. FTP and HTTP sites appear slightly different. Locate a mirror site by pointing a browser at mirrors.fedoraproject.org. Below the heading information, the page displays Public Active Mirrors. Narrow your choices by clicking a selection in the Mirror list filter at the upper-right corner of the page (Figure 2-3). For example, click 15 to list sites from which you can download Fedora 15 or click i386 in the row that starts with 15 to list sites from which you can download the i386 version of Fedora 15.

www.it-ebooks.info

46 Chapter 2 Installation Overview

Figure 2-4

A Fedora mirror site

The list on the newly displayed Web page is in country code order. For example, FR is France and US is the United States. To conserve network bandwidth, scroll to and download from a mirror site close to you. Look at the Content, Bandwidth, and Comments columns. Pick the row of an appropriate site and click the protocol you want to use in the Content column. From a browser there is little difference between the FTP and HTTP protocols, although accessing a site using FTP might be a bit faster. When you click a protocol, the browser displays a page similar to the one shown in Figure 2-4. Follow these steps to download the ISO image file you want: 1. Click releases. The browser displays a list of Fedora releases. All sites have the current release. Most sites keep at least one or two older releases while some keep many older releases. 2. Click the number of the release of Fedora you want to download. The browser displays a list that includes Fedora and Live. 3. Click Live if you want to download a live CD and Fedora if you want to download an install DVD. Make sure you have room for the file on the hard disk: A DVD ISO image file occupies almost 4 gigabytes. 4. Click the architecture (page 30) you want to install Fedora on: i386 or i686 for 32-bit systems and x86_64 for 64-bit systems. 5. If you are downloading an install ISO image, click iso. If you are downloading a live CD, go to the next step. 6. Download the CHECKSUM file. 7. Click the name of the ISO image file you want to download and choose to download (not install) the image. Live gives you the choice of a Fedora-15i686-Live-Desktop.iso, which runs and installs a Fedora/GNOME desktop and Fedora-15-i686-Live-KDE.iso, which runs and installs a KDE desktop. Fedora gives you the choice of downloading the Install DVD ISO image file, the Install CD ISO image files, or the Net Install CD ISO image file.

www.it-ebooks.info

Downloading and Burning a CD/DVD 47
Using BitTorrent

As mentioned earlier, you can use BitTorrent to obtain an ISO image file. BitTorrent is especially effective for downloading an ISO image file shortly after a new release of Fedora/RHEL is made available. To download a torrent, point a browser at fedoraproject.org/en/get-fedora-all or torrent.fedoraproject.org and click the filename of the torrent. You can identify a torrent file by its filename extension of .torrent. A BitTorrent client should start automatically and ask where to put the downloaded file. You can also download the torrent manually. Once you have a BitTorrent client such as Transmission (www.transmissionbt.com; installed with most Fedora/RHEL editions), you can start downloading the file from the command line (page 546) or by clicking it in a file browser such as Nautilus (page 102).

You can download and burn the CD/DVD on any operating system tip You can download and burn the CD/DVD on any computer that is connected to the Internet, has a browser, has enough space on the hard disk to hold the ISO image file (about 700 megabytes for a CD and 4 gigabytes for a DVD), and can burn a CD/DVD. You can often use ftp (page 704) or, on a Linux system, Nautilus menubar: File Places Connect to Server (page 270) in place of a browser to download the file.

Verifying an ISO Image File
This section assumes you have an ISO image file and a CHECKSUM file saved on the hard disk and explains how to verify the ISO IMAGE file is correct. The CHECKSUM file contains the SHA2 (page 1187) sums for each of the available ISO image files. When you process a file using the sha256sum utility, sha256sum generates a number based on the file. If that number matches the corresponding number in the CHECKSUM file, the downloaded file is correct. With the –c option and the name of the CHECKSUM file, sha256sum checks each of the files listed in the CHECKSUM file. The following example shows the Live Desktop ISO image file is OK and the Live KDE ISO image file is not present:
$ sha256sum -c *-CHECKSUM Fedora-15-i686-Live-Desktop.iso: OK sha256sum: Fedora-15-i686-Live-KDE.iso: No such file or directory Fedora-15-i686-Live-KDE.iso: FAILED open or read ...

Computing an SHA2 sum for a large file can take a while. If sha256sum does not report the file is OK, you must download the file again.

Make sure the software is set up to burn an ISO image tip Burning an ISO image is not the same as copying files to a CD/DVD. Make sure the CD/DVD burning software is set up to burn an ISO image. If you simply copy the ISO file to a CD/DVD, it will not work when you try to install Fedora/RHEL.

www.it-ebooks.info

48 Chapter 2 Installation Overview

Burning the CD/DVD
An ISO image file is an exact image of what needs to be on the CD/DVD. Putting that image on a CD/DVD involves a different process than copying files to a CD/DVD. For that reason, CD/DVD burning software has a special selection for burning an ISO image, which bears a label similar to Record CD from CD Image or Burn CD Image. Refer to the instructions for the software you are using for information on how to burn an ISO image file to a CD/DVD.

The CD-RW blank must be large enough to hold the ISO file tip When you burn a Fedora/RHEL CD from an ISO image, you might need to use a 700-megabyte blank; in some cases a 650-megabyte blank is not large enough.

Gathering Information About the System
It is not difficult to install and bring up an Fedora/RHEL system. Nevertheless, the more you know about the process before you start, the easier it will be. The installation software collects information about the system and can help you make decisions during the installation process. However, the system will work better when you know how you want to partition the hard disk rather than letting the installation program create partitions without your input. There are many details, and the more details you take control of, the more pleased you are likely to be with the finished product. Finding the information this section asks for will help ensure you end up with a system you understand and know how to change when necessary. To an increasing extent, the installation software probes the hardware and figures out what it is. Newer equipment is more likely to report on itself than older equipment is.

Test the ISO image file and test the CD/DVD tip It is a good idea to test the ISO image file and the burned CD/DVD before you use it to install
Fedora/RHEL. When you boot the system from the CD/DVD, Fedora/RHEL gives you the option of checking the CD/DVD for defects (see the tip on page 58). A bad file on a CD might not show up until you finish installing Fedora/RHEL and have it running. At that point, it might be difficult and time-consuming to figure out where the problem lies. Testing the file and CD/DVD takes a few minutes but can save you hours of trouble if something is not right. If you decide to perform one test only, test the CD/DVD.

It is critical to have certain pieces of information before you start. One thing Linux can never figure out is all the relevant names and IP addresses (unless you are using DHCP, in which case the addresses are set up for you). Following is a list of items you might need information about. Gather as much information about each item as you can: manufacturer, model number, size (megabytes, gigabytes, and so forth), number of buttons, chipset (for cards), and so on. Some items, such as the network interface card and the sound card, might be built into the motherboard.

www.it-ebooks.info

Chapter Summary 49

• Hard disks • Memory. You don’t need it for installation, but it is good to know. • SCSI interface card • Network interface card (NIC) • Video interface card (including the amount of video RAM/memory) • Sound card and compatibility with standards, such as SoundBlaster • Mouse (PS/2, USB, AT, and number of buttons) • Monitor (size and maximum resolution) • IP addresses and names, unless you are using DHCP (page 489; most routers are set up as DHCP servers), in which case the IP addresses are automatically assigned to the system. Most of this information comes from the system administrator or ISP.
◆ ◆ ◆ ◆

System hostname (anything you like) System address Network mask (netmask) Gateway address (the connecting point to the network or Internet) or a phone number when you use a dial-up connection Addresses for nameservers, also called DNS addresses Domain name (not required)

◆ ◆

Chapter Summary
A live CD can run a live Fedora session without installing Fedora on the system. You can install Fedora from a live session. Running a live session is a good way to test hardware and fix a system that will not boot from the hard disk. When you install Fedora/RHEL, you copy operating system files from a CD or DVD to hard disk(s) on a system and set up configuration files so that Linux runs properly on the hardware. Operating system files are stored on a CD or DVD as ISO image files. You can use a Web browser, FTP, or BitTorrent to download an ISO image file. It is a good idea to test the ISO image file when it is downloaded and the burned CD/DVD before you use it to install Fedora/RHEL. When you install Fedora/RHEL, you can let the installer decide how to partition the hard disk (default partitioning) or you can manually specify how you want to partition it. The Fedora Project is sponsored by Red Hat and supported by the open-source community. Fedora is a Linux release that contains cutting-edge code; it is not recommended for production environments. RHEL (Red Hat Enterprise Linux) is more stable than Fedora.

www.it-ebooks.info

50 Chapter 2 Installation Overview

Exercises
1. Briefly, what does the process of installing an operating system such as Fedora/RHEL involve? 2. What is an installer? 3. Would you set up a GUI on a server system? Why or why not? 4. A system boots from the hard disk. To install Linux, you need it to boot from a CD/DVD. How can you make the system boot from a CD/DVD? 5. What is free space on a hard disk? What is a filesystem? 6. What is an ISO image? How do you burn an ISO image to a CD/DVD?

Advanced Exercises
7. Give two reasons why RAID cannot replace backups. 8. What are RAM disks? How are they used during installation? 9. What is SHA2? How does it work to ensure that an ISO image file you download is correct?

www.it-ebooks.info

3
Step-by-Step Installation

Chapter3 3, , u

In This Chapter
Running a Fedora Live Session . . . 52 Installing from a Live Session (Fedora) . . . . . . . . . . . . . . . . . . . . 55 Installing/Upgrading from the Install DVD . . . . . . . . . . . . . . . . . . 56 The Anaconda Installer . . . . . . . . . . 58 Using Disk Druid to Partition the Disk . . . . . . . . . . . . . . . . . . . . 71 Working with LVs (Logical Volumes) . . . . . . . . . . . . . . . . . . . 73 Setting Up a RAID Device . . . . . . . . 77 palimpsest: The GNOME Disk Utility . . . . . . . . . . . . . . . . . . . . . . 77 Setting Up a Dual-Boot System . . . 82 gnome-control-center/Displays: Configures the Display . . . . . . . . 85

Objectives
After reading this chapter you should be able to: Run a live session and use palimpsest to view and change disk partitioning Install Fedora from a Live session Install or upgrade Fedora/RHEL using the Fedora/RHEL install DVD Modify system behavior with boot time parameters Modify partitions during installation Select software during installation Perform initial configuration with Firstboot List the requirement and considerations for a dual-boot configuration

51

www.it-ebooks.info

52 Chapter 3 Step-by-Step Installation

Figure 3-1

Live session, automatic boot screen

Chapter 2 covered planning the installation of Fedora/RHEL: determining the requirements; performing an upgrade versus a clean installation; planning the layout of the hard disk; obtaining the files you need for the installation, including how to download and burn CD/DVD ISO images; and collecting information about the system. This chapter focuses on installing Fedora/RHEL. Frequently the installation is quite simple, especially if you have done a good job of planning. Sometimes you might run into a problem or have a special circumstance; this chapter gives you tools to use in these cases. Read as much of this chapter as you need to; once you have installed Fedora/RHEL, continue with Chapter 4, which covers getting started using the Fedora/RHEL desktop. If you install a textual (command line) system, refer to Chapter 5.

Running a Fedora Live Session
As discussed in Chapter 2, a live session is a Linux session you run on a computer without installing Linux on the computer. When you reboot after a live session, the computer is untouched. If you are running Windows, after a live session Windows boots the way it did before the live session. If you choose, you can install Fedora from a live session. Red Hat Enterprise Linux does not offer a live session. A live session gives you a chance to preview Fedora without installing it. Boot from the live CD to begin a live session and work with Fedora as explained in Chapter 4. When you are finished, remove the CD and reboot the system. The system will then boot as it did before the live session took place. Because a live session does not write to the hard disk (other than using a swap partition, if one is available), none of the work you save will be available once you reboot. You can use a USB flash drive, Webmail, or another method to transfer files you want to preserve to another system.

www.it-ebooks.info

Running a Fedora Live Session

53

Figure 3-2

The Fedora Live Welcome menu

Booting the System
Before Fedora can display the desktop of a live session or install itself on a hard disk, the Linux operating system must be read into memory (booted). This process can take a few minutes on older, slower systems and systems with minimal RAM (memory). In most cases, you can boot Fedora to run a live session that displays a desktop without doing anything after you boot from a live CD. To begin, insert the live CD (the standard GNOME Fedora Desktop Live Media) into the CD drive and turn on or reset the system. Refer to “BIOS setup” on page 29 if the system does not boot from the CD. Refer to “Modifying Boot Parameters (Options)” on page 67 if Fedora does not boot or displays an error message. A few moments after you start the system, Fedora displays a screen that says Automatic boot in 10 seconds and counts down from 10 to 1 (Figure 3-1). Next the system displays a graphical screen showing a shaded blue progress bar.
Checking the CD

The first time you use a CD, it is a good idea to check it for defects. To do so, interrupt the automatic boot by pressing a key such as the SPACE bar while Fedora is counting down. Fedora displays the Welcome menu (Figure 3-2). Use the DOWN ARROW key to highlight the Verify and Boot line and press RETURN (the mouse will not work yet). Fedora displays a shaded blue progress bar as it verifies the contents of the CD; nothing happens for a while. If the CD is good, the system boots. Selecting Memory Test from the Welcome menu runs memtest86+, a GPL-licensed, stand-alone memory test utility for x86-based computers. Press C to configure the test; press ESCAPE to exit and reboot. See www.memtest.org for more information. If you are booting from Fedora Desktop Live Media (what this book refers to as the live CD), the system will run the GNOME desktop manager. When you boot from

Memory test

GNOME

www.it-ebooks.info

54 Chapter 3 Step-by-Step Installation

this CD, Fedora automatically logs in as the user named liveuser and displays the GNOME desktop (Figure 3-3).
KDE

If you are booting from Fedora KDE Live Media, the system will run the KDE desktop manager. When you boot from this disk, Fedora next displays a KDE startup screen and then the KDE desktop—there is no need to log in.

optional

Seeing What Is Going on
If you are curious and want to see what Fedora is doing as it boots from a live CD, remove quiet, which controls kernel messages, and rhgb (Red Hat graphical boot), which controls messages from the graphical installer, from the boot parameters. See Figure 3-13 on page 68; the list of parameters on the screen will be different from those in the figure. With the Fedora Live Welcome menu displayed (Figure 3-2), press TAB to display the boot command-line parameters. Use the LEFT ARROW key to back up over—but not remove—any words to the right of quiet. Press BACKSPACE or DEL to back up over and erase quiet and rhgb from the boot command line. Press RETURN. Now as Fedora boots, it displays information about what it is doing. Text scrolls on the screen, although sometimes too rapidly to read. When you boot Fedora from a DVD and when you boot RHEL, this information is displayed by default: You do not have to change the command line.

Installing Fedora/RHEL
You can install Fedora from a live session (preceding) or install Fedora/RHEL from the install DVD. Installing from a live session is simpler but does not give you the flexibility installing from the install DVD does. For example, you cannot select the language the installer uses, nor can you choose which software packages you want to install when you install from a live session.

Check to see what is on the hard disk before installing Fedora/RHEL caution Unless you are certain the hard disk you are installing Fedora/RHEL on has nothing on it (it is a new disk) or you are sure the disk holds no information of value, it is a good idea to examine the contents of the disk before you start the installation. You can use the palimpsest GNOME Disk Utility (page 77) from a live session for this purpose.

The install DVD holds many of the software packages that Fedora/RHEL supports. You can install whichever packages you like from this DVD without connecting to the Internet. However, without an Internet connection, you will not be able to update the software on the system. The live CD holds a limited set of software packages. Once you install from this CD, you must connect to the Internet to update the software on the system and to download and install additional packages. To begin most installations, insert the live CD or the install DVD into the CD/DVD drive and turn on or reset the system. For hard disk and network-based installations, you can use the Net Install CD, the install DVD, or a USB flash drive.

www.it-ebooks.info

Installing Fedora/RHEL 55

Figure 3-3

A full GNOME 3 Live desktop; install to hard drive

Installing from a Live Session (Fedora)
Bring up a live GNOME session as explained on page 52. GNOME will display either • A full GNOME 3 desktop that has the word Activities in the upper-left corner of the screen or • A window with the words GNOME 3 Failed to Load. When you click Close on that window, GNOME displays a desktop running in Fallback mode with the words Applications and Places in the upper-left corner of the screen.
GNOME 3 desktop

From a full GNOME 3 desktop, click Activities; GNOME displays buttons labeled Windows and Applications with the button labeled Windows highlighted (Figure 3-3). At the bottom of the icons on the left side of the window is an icon depicting a hard drive with a green tick above it. Left-click this icon to begin installing Fedora.

www.it-ebooks.info

56 Chapter 3 Step-by-Step Installation

Figure 3-4
Fallback mode desktop

A GNOME Fallback Live desktop; install to hard drive

To begin installing Fedora from a desktop running in Fallback mode, select Main menu: Applications System Tools Install to Hard Drive (Figure 3-4) by (left-) clicking Applications at the upper-left corner of the screen, clicking System Tools, and finally clicking Install to Hard Drive. Continue reading at “Using Anaconda” on page 59.

Installing/Upgrading from the Install DVD
To install/upgrade Fedora/RHEL from the install DVD, insert the DVD into the DVD drive and turn on or reset the system. After a few moments, the system displays the Welcome to Fedora/RHEL menu (Figure 3-5) and a message that says Automatic boot in 60 seconds. Press a key, such as the SPACE bar, within 60 seconds to stop the countdown and display the message Press [TAB] to edit options as shown in Figure 3-5. If you do not press a key, after 60 seconds Fedora/RHEL begins a graphical install/upgrade. Refer to “BIOS setup” on page 29 if the system does not boot from the DVD. Refer to “Modifying Boot Parameters (Options)” on page 67 if Fedora/RHEL does not boot or displays an error message.
Install a new system or upgrade an existing system Install system with basic video driver

The Welcome menu has the following selections: Installs a graphical Fedora/RHEL system using the graphical installer. Installs a graphical Fedora/RHEL system using the graphical installer. Fedora/RHEL does not attempt to determine the type of display attached to the system; it uses a

www.it-ebooks.info

Installing Fedora/RHEL 57

Figure 3-5

The install DVD Welcome menu

basic video driver that works with most displays. Choose this selection if the previous selection fails just after the Disc Found screen (below).
Rescue installed system

Brings up a minimal Fedora/RHEL system but does not install it. After detecting the system’s disks and partitions, the system enters single-user/rescue mode and allows you to mount an existing Linux filesystem. For more information refer to “Rescue Installed System” on page 457. Boots the system from the hard disk. This selection frequently has the same effect as booting the system without the CD/DVD (depending on how the BIOS [page 29] is set up). Runs the memory test described on page 53 (Fedora).

Boot from local drive

Memory test

Starting the Installation
Make a selection from the Welcome menu and press scrolls by as the system boots.
RETURN

to boot the system. Text

The Disc Found Screen
The first screen the DVD installation process displays is the pseudographical Disc Found screen. Because it is not a true graphical screen, the mouse does not work. Instead, you must use the TAB or ARROW keys to highlight different choices and then press RETURN to select the highlighted choice. This screen allows you to test as many installation CD/DVDs as you like. Choose OK to test the media or Skip to bypass the test. See the caution box on the next page.

www.it-ebooks.info

58 Chapter 3 Step-by-Step Installation

Test the install DVD caution It is possible for data to become corrupted while fetching an ISO image; it is also possible for a transient error to occur while writing an image to recordable media. When you boot Fedora/RHEL from an install DVD, Anaconda displays the Disc Found screen before starting the installation. From this screen, you can verify that the install DVD does not contain any errors. Testing the DVD takes a few minutes but can save you hours of aggravation if the installation fails due to bad media.

A DVD might fail the media test if the software that was used to burn the disk did not include padding. If a DVD fails the media test, try booting with the nodma parameter. See page 67 for information on adding parameters to the boot command line. If the DVD passes the media test when you boot the system with the nodma parameter, the DVD is good; reboot the system without this parameter before installing Fedora/RHEL. If you install Linux after having booted with this parameter, the kernel will be set up to always use this parameter. As a consequence, the installation and operation of the system can be slow.

The Anaconda Installer
Anaconda, which is written in Python and C, identifies the hardware, builds the filesystems, and installs or upgrades the Fedora/RHEL operating system. Anaconda can run in textual or graphical (default) interactive mode or in batch mode (see “Using the Kickstart Configurator” on page 81). Exactly which screens Anaconda displays depends on whether you are installing Fedora from a live session or from the install DVD, whether you are installing RHEL, and which parameters you specify on the boot command line. With some exceptions—most notably if you are running a textual installation—Anaconda probes the video card and monitor, and starts a native X server. While it is running, Anaconda opens the virtual consoles (page 138) shown in Table 3-1. You can display a virtual console by pressing CONTROL-ALT-Fx, where x is the virtual console number and Fx is the function key that corresponds to the virtual console number.

Table 3-1 Virtual console assignments during installation
Information displayed during installation Virtual console
1 2 3 4 5 6 7

Install DVD
Installation dialog Shell Installation log System messages X server output GUI interactive installation screen GUI interactive installation screen a a

Live CD
Installation dialog Login prompt (log in as liveuser) Installation log Login prompt (log in as liveuser) Login prompt (log in as liveuser) Login prompt (log in as liveuser) GUI interactive installation

a. The GUI appears on virtual console 6 or 7.

www.it-ebooks.info

Installing Fedora/RHEL 59

Figure 3-6

The language screen

At any time during the installation, you can switch to virtual console 2 (press CONTROL-ALT-F2) and give commands to see what is going on. Do not give any commands that change any part of the installation process. To switch back to the graphical installation screen, press CONTROL-ALT-F6 or CONTROL-ALT-F7.

Using Anaconda
Anaconda displays a button labeled Next at the lower-right corner of each installation screen and a button labeled Back next to it on most screens. When you have completed the entries on an installation screen, click Next or press F12; from a textual installation, press the TAB key until the Next button is highlighted and then press RETURN. Select Back to return to the previous screen.

Anaconda Screens
Anaconda displays different screens depending on which commands you give and which choices you make. During a graphical installation, Anaconda starts, loads drivers, and probes for the devices it will use during installation. After probing, it starts the X server. This section describes the screens Anaconda displays during a default installation and explains the choices you can make on each of them.
Language

Anaconda displays the language screen (Figure 3-6) after it obtains enough information to start the X Window System. Installing from a live session does not display this screen. RHEL displays a welcome screen before it displays the language screen. Select the language you want to use for the installation. This language is not necessarily the same language the installed system will display. Click Next. Select the type of keyboard attached to the system. Anaconda displays this warning if the hard disk has not been used before. The dialog box says the drive might need to be initialized. When you initialize a drive, all data on the drive is lost. Click Re-initialize drive if it is a new drive or if you do not need the data on the drive. Anaconda initializes the hard disk immediately.

Keyboard Error processing drive

www.it-ebooks.info

60 Chapter 3 Step-by-Step Installation

Figure 3-7
Devices

The Install or Upgrade screen

This screen asks you to specify the type of devices you are installing Linux on. In most cases click the radio button labeled Basic Storage Devices. If you are installing on an enterprise device, such as a SAN (Storage Area Network), click the radio button labeled Specialized Storage Devices. If Anaconda does not detect information on the hard drive you are installing Linux on, it displays the Storage Device Warning window. If you are sure there is no data you want to keep on the hard drive you are installing Linux on, put a tick in the check box labeled Apply my choice to all devices with undetected partitions or filesystems and click Yes, discard my data (Fedora) or click Re-initialize all (RHEL). Specify the name of the system on this screen. If the system will not use DHCP to configure its network connection, click Configure Network at the lower-left corner of the screen to display the Network Connections window; see page 651 for more information. Click Next. The time zone screen allows you to specify the time zone where the system is located (Figure 2-1, page 32). Use the scroll wheel on the mouse or the slider to the left of the map to zoom in or out on the selected portion of the map, drag the horizontal and vertical thumbs (page 1193) to position the map in the window, and then click a city in the local system’s time zone. Alternatively, you can scroll through the list and highlight the appropriate selection. Remove the tick from the check box labeled System clock uses UTC if the system clock is not set to UTC (page 1195). Click Next. Enter and confirm the password for the root user (Superuser). See page 409 for more information on root privileges. If you enter a password that is not very secure, Anaconda displays a dialog box with the words Weak password; click Cancel or Use Anyway, as appropriate. Click Next. (This choice is not available from the live CD.) Anaconda displays the Install or Upgrade screen (Figure 3-7) only if it detects a version of Fedora/RHEL it can upgrade on the hard disk. Anaconda gives you the choice of upgrading the existing installation or overwriting the existing installation with a new one. Refer to

Warning

Hostname

Time zone

Root password

Install or Upgrade

www.it-ebooks.info

Installing Fedora/RHEL 61

Figure 3-8

The Type of Installation screen

“Installing a Fresh Copy or Upgrading an Existing Fedora/RHEL System?” on page 34 for help in making this selection. Select one of the entries and click Next.
Type of Installation

The Type of Installation screen (Figure 3-8) allows you to specify partition information, encrypt the filesystem, and review partition information. Anaconda presents the following choices in the upper part of the screen: • Use All Space—Deletes all data on the hard disk and creates a default layout on the entire hard disk, as though you were working with a new hard disk. • Replace Existing Linux System(s)—Removes all Linux partitions, deleting the data on those partitions and creating a default layout in place of one or more of the removed partitions. If there is only a Linux system on the hard disk, this choice is the same as the previous one. • Shrink Current System—Shrinks the partitions that are in use by the operating system that is already installed on the hard disk. This choice creates a default layout in the space it has recovered from the installed operating system. • Use Free Space—Installs Fedora/RHEL in the free space (page 34) on the disk. This choice does not work if there is not enough free space. • Create Custom Layout—Does not alter hard disk partitions. This choice causes Anaconda to run Disk Druid (page 71) so you can preserve those partitions you want to keep and overwrite other partitions. It is a good choice for installing Fedora/RHEL over an existing system where you want to keep /home, for example, but want a clean installation and not an upgrade. Click the radio button adjacent to the choice you want and click Next. To encrypt the filesystems you are creating, put a tick in the check box labeled Encrypt system. If you choose to encrypt the filesystems, Anaconda will ask for a passphrase. You will need this passphrase to log in on the system.

Encrypt system

www.it-ebooks.info

62 Chapter 3 Step-by-Step Installation

Figure 3-9
Default layout

The Boot Loader Configuration screen

The default layout the first four choices create includes two or three logical volumes (swap, root [/], and if the hard disk is big enough, /home) and one standard partition (/boot). With this setup, most of the space on the disk is assigned to the /home partition, or if there is no /home partition, to the root partition. For information on the Logical Volume Manager, see page 42. Anaconda runs Disk Druid only if you put a tick in the check box labeled Review and modify partitioning layout or if you select Create custom layout from the list described earlier. You can use Disk Druid to verify and modify the layout before it is written to the hard disk. For more information refer to “Using Disk Druid to Partition the Disk” on page 71. Anaconda displays a warning if you are removing or formatting partitions. Click Yes, Format, or Write changes to disk to proceed. If the machine you are installing Fedora/RHEL on has two or more hard drives, Anaconda displays a screen that allows you to specify which drives are data storage devices and which are install target devices. Disk Druid sets up data storage devices to be mounted on the installed system; they are not formatted. Install target devices are the devices the system is installed on. Some or all of the partitions on these devices will be formatted, destroying any data on them. This screen also allows you to specify which of the install target devices is to hold the boot loader. Anaconda displays the Boot Loader Configuration screen (Figure 3-9) only when you put a tick in the check box labeled Review and modify partitioning layout or select Create custom layout from the list in the Type of Installation screen. By default, Anaconda installs the grub boot loader (page 595). If you do not want to install a boot loader, remove the tick from the check box labeled Install boot loader on /dev/xxx. To change the device the boot loader is installed on, click Change device. When you install Fedora/RHEL on a machine that already runs another operating system, Anaconda frequently recognizes the other operating system and sets up grub so you can boot from either operating system. Refer to “Setting Up a Dual-Boot System” on page 82. To manually add other operating systems to grub’s

Disk Druid

Warning

Two hard drives

Boot Loader Configuration

www.it-ebooks.info

Installing Fedora/RHEL 63

Figure 3-10

The Software Selection screen

list of bootable systems, click Add and specify a label and device to boot from. For a more secure system, specify a boot loader password.
Select Network Interface Installing from a live CD

This window is displayed at this time by the Net Install CD only. See “Select Network Interface” on page 64. If you are installing from a live CD you are done with the first part of the installation. Click Close and then reboot the system by first clicking Live System User at the upper-right corner of the screen to display a drop-down menu. If Suspend appears at the bottom of this menu, you are running full GNOME 3; press and hold the ALT key to cause Suspend to change to Power Off. Click Power Off and then click Restart from the resulting window. If Shut Down appears at the bottom of the drop-down menu you are running in Fallback mode (page 92); click Shut Down and then click Restart from the resulting window. Anaconda ejects the CD as the system shuts down. Continue with “Firstboot: When You Reboot” on page 65. As the Software Selection screen explains, by default Anaconda installs a basic system, including software that allows you to use the Internet. See Figure 3-10. Near the top of the screen are four check boxes that you can put ticks in to select categories of software to install: Graphical Desktop (selected by default), Software Development, Web Server, and Minimal (Fedora; RHEL selections differ slightly). Fedora/RHEL software is kept in repositories (page 533). When you install Fedora, middle of the screen holds check boxes you can put ticks in to select repositories that hold the following items: • Installation Repo—Indicates Anaconda is to install from the repository included on the installation medium. • Fedora 15 – xxx—Indicates Anaconda is to use the online Fedora 15 repository. The xxx indicates the system architecture (e.g., i386). • Fedora 15 – xxx – Updates—Indicates Anaconda is to use the online Fedora 15 Updates repository. The xxx indicates the system architecture (e.g., i386).

Software Selection

www.it-ebooks.info

64 Chapter 3 Step-by-Step Installation

Selecting either of the last two choices gives you more software packages to choose from later in the installation process if you decide to customize the software selection during installation.
Select Network Interface

When you put a tick in either of the last two check boxes, Anaconda displays the Select Network Interface window. Select the interface you want to use from this window. Click OK; Anaconda opens the Network Connections window. If the system is configured using DHCP click Close. Otherwise configure the network connection as explained on page 651 and then click Close. Anaconda takes a moment to configure the network and then retrieves information from the repository you specified. Below the repository selection frame in the Software Selection screen are buttons labeled Add additional software repositories and Modify repository (Fedora and RHEL). Toward the bottom of the screen are two radio buttons: • Customize later—Installs the default packages plus those required to perform the tasks selected from the list at the top of the screen. • Customize now—Displays the package selection screen (next) after you click Next on this screen so you can select specific categories of software and package groups you want to install. If you want to set up servers as described in Part V of this book, select Customize now and install them in the next step. In most cases it is a good idea to customize the software selection before installation. Regardless of which software groups and packages you select now, you can change which software groups and packages are installed on a system any time after the system is up and running (as long as the system can connect to the Internet).

Package selection

If you selected Customize now, Anaconda displays a package selection screen that contains two side-by-side frames near the top of the screen (Figure 3-11). If you added repositories in addition to the Installation repository or if you are installing RHEL, this screen will display more choices. Select a software category from the frame on the left and package groups from the frame on the right. Each package group comprises many software packages, some mandatory (the base packages) and some optional. For example, to install KDE, which is not installed by default, click Desktop Environments in the left frame. Anaconda highlights your selection and displays a list of desktop environments you can install in the right frame. Put a tick in the check box labeled KDE Software Compilation; Anaconda highlights KDE, displays information about KDE in the frame toward the bottom of the window, displays the number of optional packages that are selected, and activates the button labeled Optional packages. Click this button to select which optional

www.it-ebooks.info

Installing Fedora/RHEL 65

Figure 3-11

The package selection screen

packages you want to install in addition to the base packages. If you install KDE and you do not want to install GNOME too, you must remove the tick from the check box labeled GNOME Desktop Environment. To get started, accept the default optional packages. If you will be running servers on the system, click Servers on the left and select the servers you want to install from the list on the right. Select other package categories in the same manner. When you are done, click Next; Anaconda begins writing to the hard disk.

Beginning Installation
After going through some preliminary steps, Anaconda installs Fedora/RHEL based on your choices in the preceding screens, placing a Kickstart file (page 81) in /root/anaconda-ks.cfg. To change the way you set up Fedora/RHEL, you can press CONTROL-ALT-DEL to reboot the system and start over. If you reboot the system, you will lose all the work you did up to this point. Installing Fedora/RHEL can take a while. The amount of time depends on the hardware you are installing the operating system on and the number of software packages you are installing.
Installation Complete

When Anaconda is finished, it tells you the installation is complete. An installation from a live CD ejects the CD. If you are using another installation technique, you must remove the CD/DVD or other installation medium. Click Reboot.

Firstboot: When You Reboot
When the system reboots, it is running Fedora/RHEL. The first time it boots, Fedora/RHEL runs Firstboot, which asks a few questions before allowing you to log in.

www.it-ebooks.info

66 Chapter 3 Step-by-Step Installation

Figure 3-12
Welcome License Information

The Welcome screen

There is nothing to do on the Welcome screen (Figure 3-12). Click Forward. After the Welcome screen, Firstboot displays the License Information screen. If you understand the license information, click Forward. When installing RHEL, the next screen asks you to set up software updates by registering with RHN (Red Hat Network; page 554). The next screen allows you to set up a user account. For more information refer to “Configuring User and Group Accounts” on page 602. Putting a tick in check box labeled Add to Administrators group (Fedora) adds the user to the wheel group. The user can then gain root privileges by running sudo and providing her password (not the root password). See page 415 for more information on using sudo and page 422 for more information on the wheel group. Click the button labeled Use Network Login to set up a network login using Kerberos, LDAP, or NIS. Click Advanced to display the User Manager window (page 602).

Software Updates (RHEL) Create User

Date and Time

The next screen allows you to set the system date and time. Running the Network Time Protocol (NTP) causes the system clock to reset itself periodically from a clock on the Internet. If the system is connected to the Internet, you can enable NTP by putting a tick in the check box labeled Synchronize date and time over the network. Click Forward. Kdump captures and preserves kernel dump information. When installing RHEL, put a tick in the box labeled Enable kdump to enable Kdump. When you are installing Fedora, the next screen allows the system to share its profile with the Fedora Project. The information is shared anonymously and helps build an upto-date Linux hardware database that includes distribution information. You can use this database to help you choose components when you buy or build a Linux system.

Kdump (RHEL)

Hardware Profile (Fedora)

www.it-ebooks.info

Installation Tasks 67

Click the radio button labeled Send Profile to cause the smolt hardware profiler to send monthly updates of the system’s hardware profile to smolts.org. Select the radio button labeled Do not send profile if you do not want to share the system’s profile. Click Finish. When the Hardware Profile (Fedora) or Kdump (RHEL) screen closes, the installation is complete. You can now use the system and set it up as you desire. For example, you might want to customize the desktop (as explained in Chapters 4 and 8) or set up servers (as discussed in Part V of this book).

Initializing Databases and Updating the System
Updating the mandb (Fedora) or makewhatis (RHEL) database ensures the whatis (page 128) and apropos (page 127) utilities will work properly. Similarly, updating the locate database ensures that locate will work properly. (The locate utility indexes and allows you to search for files on the system quickly and securely.) Instead of updating these databases when you install the system, you can wait for crond (page 611) to run them, but be aware that whatis, apropos, and locate will not work for a while. The best way to update these databases is via the cron scripts that run daily. Working with root privileges (page 409), give the following commands:
# /etc/cron.daily/man-db.cron # /etc/cron.daily/makewhatis.cron # /etc/cron.daily/mlocate.cron

(Fedora) (RHEL) (Fedora/RHEL)

These utilities run for several minutes and might complain about not being able to find a file or two. When the system displays a prompt, the mandb/makewhatis and locate databases are up-to-date.

Installation Tasks
This section details some common tasks you might need to perform during or after installation. It covers modifying the boot parameters, using Disk Druid to partition the disk during installation, using palimpsest to view and modify partitions, using logical volumes (LVs) to facilitate disk partitioning, using Kickstart to automate installation, and setting up a system that will boot either Windows or Linux (a dualboot system).

Modifying Boot Parameters (Options)
To modify boot parameters, you must interrupt the automatic boot process by pressing a key such as the SPACE bar while Fedora/RHEL is counting down when you first boot from a live CD (page 53) or install DVD (page 56). When you press a key, Fedora displays the Welcome menu (Figure 3-2 on page 53 or Figure 3-5 on page 57). Use the ARROW keys to highlight the selection you want

www.it-ebooks.info

68 Chapter 3 Step-by-Step Installation

Figure 3-13

The Welcome screen displaying boot parameters (options)
TAB

before proceeding. With the desired selection highlighted, press the display the boot command-line parameters (Figure 3-13).

key to

Type a SPACE before you enter any parameters. You can specify multiple parameters separated by SPACEs. Press RETURN to boot the system. For more information on boot parameters, refer to www.kernel.org/doc/Documentation/kernel-parameters.txt, www.kernel.org/pub/linux/kernel/people/gregkh/lkn/lkn_pdf/ch09.pdf, or the Web page at fedoraproject.org/wiki/Anaconda/Options. Alternatively, you can use Google to search for linux boot parameters.

What to do if the installation does not work tip On some hardware, the installation might pause for as long as ten minutes. Before experimenting with other fixes, try waiting for a while. If the installation hangs, try booting with one or more of the boot parameters described in this section. Try running the installer in pseudographical (textual) mode.

Following are some of the parameters you can add to the boot command line. If you encounter problems with the display during installation, supply the nofb parameter, which turns off video memory. If you are installing from a medium other than a DVD— that is, if you are installing from files on the local hard disk or from files on another system using FTP, NFS, or HTTP—supply the askmethod or method parameter. Many of these parameters can be combined. For example, to install Linux in text mode using a terminal running at 115,200 baud, no parity, 8 bits, connected to the first serial device, supply the following parameters (the ,115200n8 is optional). text console=ttyS0,115200n8

www.it-ebooks.info

Installation Tasks 69

The next set of parameters installs Fedora/RHEL on a monitor with a resolution of 1024 768, without probing for any devices. The installation program asks you to specify the source of the installation data (CD, DVD, FTP site, or other) and requests a video driver. resolution=1024x768 noprobe askmethod noacpi

Disables ACPI (Advanced Configuration and Power Interface). This parameter is useful for systems that do not support ACPI or that have problems with their ACPI implementation. The default is to enable ACPI. Specifying acpi=off has the same effect. Disables APIC (Advanced Programmable Interrupt Controller). The default is to enable APIC. Disables APM (Advanced Power Management). The default is to enable APM. Specifying apm=off has the same effect. Displays the Installation Method screen, which presents a choice of installation sources: Local CD/DVD, Hard drive, NFS directory, and URL (first installation CD, Net Install CD, and install DVD only). • Local CD/DVD—Displays the Disc Found screen, which allows you to test the installation media (the same as if you had not entered any boot parameters). • Hard drive—Prompts for the partition and directory that contain the installation tree or the ISO image of the install DVD. Do not include the name of the mount point when you specify the name of the directory. For example, if the ISO images are in the /home/sam/FC15 directory and /dev/sda6 holds the partition that is normally mounted on /home, you would specify the partition as /dev/sda6 and the directory as sam/FC15 (no leading slash). • The next two selections attempt to use NetworkManager (page 651) to set up a DHCP connection automatically. Manual configuration requires you to enter the system’s IP address and netmask as well as the IP addresses of the default gateway and primary nameserver.


noapic

noapm

askmethod

NFS directory—Displays the NFS Setup screen, which allows you to enter the NFS server name, the name of the directory that contains the installation tree or the ISO image of the install DVD, and optionally NFS mount options (page 797). Enter the server’s IP address and the name of the exported directory, not its device name. The remote (server) system must export (page 805) the directory hierarchy that holds the installation tree or the ISO image of the install DVD. URL—Displays the URL Setup screen, which allows you to enter the URL of the directory that contains the installation tree or the ISO image of the install DVD, and optionally the URL of a proxy server, a username, and a password.



www.it-ebooks.info

70 Chapter 3 Step-by-Step Installation nodma Turns off direct memory access (DMA) for all disk controllers. This parameter might make buggy controllers (or controllers with buggy drivers) more reliable, but also causes them to perform very slowly because the connected devices have to run in PIO mode instead of DMA mode. It can facilitate testing CD/DVDs that were not written correctly. For more information refer to “The Disc Found Screen” on page 57. (no framebuffer) Turns off the framebuffer (video memory). This option is useful if problems arise when the graphical phase of the installation starts. Changes the way the kernel handles interrupts. Specifies the location of a Kickstart (page 81) file to use to control the installation process. The URI is the pathname or network location of the Kickstart file. Disables local APIC. The default is to enable local APIC. Runs the installation program at a resolution of 640 (below). 480 pixels. See also resolution

nofb

irqpoll ks=URI

nolapic lowres

mem=xxxM

Overrides the detected memory size. Replace xxx with the number of megabytes of RAM in the computer. Specifies an installation method and location without prompting as askmethod does. For example, you can use the following parameter to start installing from the specified server:

repo=URI

repo=ftp://fedora.mirrors.pair.com/fedora/linux/releases/15/Fedora/i386/os noprobe

Disables hardware probing for all devices, including network interface cards (NICs), graphics cards, and the monitor. This option forces you to select devices from a list. You must know exactly which cards or chips the system uses when you use this parameter. Use noprobe when probing causes the installation to hang or otherwise fail. This parameter allows you to supply arguments for each device driver you specify. Sets the system up to rescue an installed system; see page 457 for details. Specifies the resolution of the monitor you are using for a graphical installation. For example, resolution=1024x768 specifies a monitor with a resolution of 1024 768 pixels. Installs Linux in pseudographical (page 31) mode. Although the images on the screen appear to be graphical, they are composed entirely of text characters. Installs Linux via a VNC (virtual network computing) remote desktop session. After providing an IP address, you can control the installation remotely using a VNC client from a remote computer. You can download a free VNC client that runs on several platforms from www.realvnc.com. Use yum (page 534) to install the vnc software package to run a VNC client on a Fedora/RHEL system. Enables a password for a VNC connection. This option requires you also use the vnc option.

rescue resolution=WxH

text

vnc

vncpassword= passwd

www.it-ebooks.info

Installation Tasks 71

Figure 3-14

Disk Druid: main screen, default layout

Using Disk Druid to Partition the Disk
See page 34 for a discussion of the setup of the hard disk and partitions. Disk Druid, a graphical disk-partitioning program that can add, delete, and modify partitions on a hard disk, is part of the Fedora/RHEL installation system. You can use Disk Druid only while you are installing a system; it cannot be run on its own. You can use palimpsest (page 77), parted (page 617), or fdisk to manipulate partitions and system-config-lvm to work with LVs after you install Fedora/RHEL. As explained earlier, if you want a basic set of partitions, you can allow Anaconda to partition the hard disk automatically. See page 42 for a discussion of LVM (Logical Volume Manager) including PVs, VGs, and LVs. Anaconda runs Disk Druid when you put a tick in the check box labeled Review and modify partitioning layout or when you select Create custom layout in the Type of Installation screen (Figure 3-8, page 61).
Default layout

Figure 3-14 shows the Disk Druid main screen as it appears when you have chosen the default layout for the hard disk (see “Type of Installation” on page 61). The middle of the screen holds a table listing hard drives and LVM Volume Groups and the partitions or LVs each holds, one per line. Fedora names the Volume Group after the hostname you specified earlier in the installation. If you specified tiger as the hostname, the Volume Group name would be vg_tiger. If you accept the default hostname of localhost.localdomain, Fedora names the Volume Group VolGroup as in the examples in this section.

www.it-ebooks.info

72 Chapter 3 Step-by-Step Installation

The highlighted logical volume, lv_root, is in the Volume Group named VolGroup, which is depicted graphically at the top of the screen. The lv_swap logical volume is very small; it is the sliver at the right end of the graphical representation. The following buttons appear near the bottom of the screen: • Create—Displays the Create Storage window (next) that allows you to create a partition or set up software RAID or LVM LVs • Edit—Edits the highlighted device (next page) • Delete—Deletes the highlighted device • Reset—Cancels the changes you have made and causes the Disk Druid table to revert so it matches the layout of the disk The Disk Druid table contains the following columns: • Device—The name of the device in the /dev directory (for example, /dev/sda1) or the name of the LV • Size (MB)—The size of the partition or LV in megabytes • Mount Point/RAID/Volume—Specifies where the partition will be mounted when the system is brought up (for example, /usr); it is also used to specify the RAID device or LVM volume the partition/LV is part of • Type—The type of the partition, such as ext4, swap, or physical volume (LVM) • Format—A tick in this column indicates the partition will be formatted as part of the installation process; all data on the partition will be lost

The Create Storage Window
Clicking Create on the Disk Druid main screen displays the Create Storage window (Figure 3-15). This window has three sections, each of which has one or more selections: • Create Partition (pages 34 and 74)


Standard Partition—Create a partition RAID Partition—Create a software RAID partition RAID Device—Join two or more RAID partitions into a RAID device LVM Volume Group—(VG) Specify PVs that make up a VG; also allows you to specify LVs that are in the VG

• Create Software RAID (pages 41 and 77)
◆ ◆

• Create LVM (pages 42 and 73)


www.it-ebooks.info

Installation Tasks 73

Figure 3-15
◆ ◆

The Create Storage window

LVM Logical Volume—(LV) Specify LVs that are in a VG LVM Physical Volume—(PV) Specify PVs that make up a VG

Make a selection by clicking the adjacent radio button. Click Information to display information about the adjacent section.

Working with LVs (Logical Volumes)
When you instruct Anaconda to partition the hard disk with a default layout (see “Type of Installation” on page 61), it uses LVM (page 42) to set up most of the hard disk, creating LVs (logical volumes) instead of partitions. It places /boot on the first partition on the drive, not under the control of LVM. LVM creates a VG (volume group) named VolGroup that occupies the rest of the disk space. Within this VG it creates two or three LVs: root (/, lv_root), swap (lv_swap), and if there is room, /home (lv_home). The swap LV occupies up to a few gigabytes; the root LV takes up to about 50 gigabytes. If there is room for a /home LV, it occupies the rest of VolGroup. This section explains how to make the /home LV smaller so you can add an additional LV to VolGroup. If the hard drive on the machine you are working with is small and Anaconda does not create a /home LV, you can follow this example by making the root LV smaller instead. If you highlight VolGroup and click Create, Disk Druid displays a Create Storage window in which Create LVM/LVM Logical Volume is grayed out. You cannot add a logical volume because there is no free space in VolGroup. To make the /home LV smaller and make room for additional partitions, first highlight the /home LV (lv_home) and click Edit. Disk Druid displays the Edit

www.it-ebooks.info

74 Chapter 3 Step-by-Step Installation

Figure 3-16 Disk Druid: Edit Logical Volume window

LVM Volume Group window with the Edit Logical Volume window on top of it (Figure 3-16). Initially, the size of lv_home is the same as its maximum size. Replace the numbers in the text box labeled Size (MB) with the number of megabytes you want to assign to the lv_home LV. Figure 3-16 shows the size of the lv_home LV being changed to 100 gigabytes (100,000 megabytes). Click OK. Once you decrease the size of the lv_home LV, the Disk Druid main screen shows a device named Free in VolGroup indicating VolGroup has free space. You can now add another LV to VolGroup. Click Create to display the Create Storage window (Figure 3-15 on the previous page), which no longer has Create LVM/LVM Logical Volume grayed out. Click the radio button labeled LVM Logical Volume; Disk Druid puts the name of the only possible VG in the adjacent spin box: VolGroup. Click Create. Disk Druid displays the Make Logical Volume window (Figure 3-17). Select/specify a mount point, filesystem type, and size for the LV. You can change the LV name if you like, although Disk Druid assigns logical, sequential names that are easy to use. Figure 3-17 shows a mount point of /extra with a name of lv_extra and a filesystem type of ext4 being created with a size of 20 gigabytes. Click OK when the LV is set up the way you want. Figure 3-18 shows the modified Disk Druid main screen with the new /extra LV.

Working with Partitions
Create a new partition

To create a new partition on a hard disk, the hard disk must have enough free space to accommodate the partition; see “Resizing a partition” on page 77. Click

www.it-ebooks.info

Installation Tasks 75

Figure 3-17 Disk Druid: Make Logical Volume window

the Create button to add a partition. In response, Disk Druid displays the Create Storage window (Figure 3-15, page 73). Click the radio button labeled Create

Figure 3-18

Disk Druid: main screen with the new /extra LV

www.it-ebooks.info

76 Chapter 3 Step-by-Step Installation

Figure 3-19

Disk Druid: Add Partition window, ext4 filesystem

Partition/Standard Partition and then click Create (in the Create Storage window). Disk Druid displays the Add Partition window (Figure 3-19). Specify the mount point (the name of the directory the partition will be mounted on; page 36) and the filesystem type; use the arrow buttons at the right ends of these boxes to display drop-down lists. If more than one drive is available, put a tick in the check box adjacent to the drive you want the partition to be created on in the Allowable Drives frame. Specify the size of the partition and, in the Additional Size Options frame, click the radio button labeled Fixed size to create the partition close to the size you specify. Because of block-size constraints, the final partitions are not usually exactly the size you specify. Click the radio button labeled Fill all space up to (MB) and fill in the maximum size you want the partition to be to create a partition that takes up the existing free space, up to the maximum size you specify. In other words, Disk Druid will not complain if it cannot create the partition as large as you would like. Click the radio button labeled Fill to maximum allowable size to cause the partition to occupy all of the remaining free space on the disk, regardless of size. (If you create another partition after creating a Fill to maximum allowable size partition, the new partition will pull blocks from the existing maximum size partition.) Put a tick in the check box labeled Force to be a primary partition to create a primary partition (page 35). Put a tick in the check box labeled Encrypt to encrypt the partition. Click OK, and Disk Druid adds the partition to its table (but does not write the changes to the hard disk).

www.it-ebooks.info

Installation Tasks 77
Edit an existing partition

To modify an existing partition, highlight the partition in the Disk Druid table or the graphical representation of the hard disk and click Edit; Disk Druid displays the Edit Partition window. Using this window, you can change the mount point or size of a partition, or format the partition as another type (e.g., ext3, vfat, swap, etc.).

Always back up the data on a hard disk caution If you are installing Fedora/RHEL on a disk that holds data that is important to you, always back up the data before you start the installation process. Things can and do go wrong. The power might go out in the middle of an installation, corrupting the data on the hard disk. A bug in the partitioning software might destroy a filesystem. Although it is unlikely, you might make a mistake and format a partition holding data you want to keep.
Resizing a partition

When you are working with a hard disk with a single partition that occupies the entire disk, such as when you are setting up a dual-boot system by adding Fedora/RHEL to a Windows system (page 82), you might be able to resize the partition to install Fedora/RHEL. The process of resizing a partition is the same regardless of the type of partition: You can use the following technique to resize Windows, Linux, or other types of partitions. To install Fedora/RHEL on this system, you must resize (shrink) the partition to make room for Fedora/RHEL. Before you resize a Windows partition, you must boot Windows and defragment the partition using the Windows defragmenter; see the tip on page 83. To resize the partition, highlight the partition in the Disk Druid table or the graphical representation of the hard disk and click Edit; Disk Druid displays the Edit Partition window. In the Edit Partition window, put a tick in the check box labeled Resize. Then enter the size, in megabytes, you want to shrink the filesystem to. Make sure that the size you specify is larger than the amount of space the data on the filesystem occupies. When you click OK, Disk Druid shrinks the partition.

Setting Up a RAID Device
To set up a RAID array (page 41), you must first create two or more partitions of the same size. Usually these partitions will be on different hard disks. You create RAID partitions by pressing the button labeled Create to display the Create Storage window (Figure 3-15, page 73), selecting Create Software RAID/RAID Partition, and specifying the partition. Typically a RAID device comprises partitions of the same size. Once you have created two or more RAID partitions, click Create, select Create Software RAID/RAID Device from the Create Storage window, and specify the RAID partitions that make up the RAID device and the mount point for the RAID device.

palimpsest: The GNOME Disk Utility
The palimpsest graphical disk utility can create, remove, and modify partitions and filesystems on many types of media, including internal and external hard disks,

www.it-ebooks.info

78 Chapter 3 Step-by-Step Installation

Figure 3-20

Selecting the palimpsest disk utility from the Main menu

CD/DVDs, and USB flash drives. It can encrypt partitions and change passwords on already encrypted partitions. Unless you are certain the hard disk on which you are installing Fedora/RHEL has nothing on it (it is a new disk) or you are sure the disk holds no information of value, it is a good idea to examine the contents of the disk before you start the installation. The palimpsest disk utility is a good tool for this job. It is part of the gnome-disk-utility package. Open the Palimpsest Disk Utility window by selecting Main menu: Applications Accessories Disk Utility (Fedora; Figure 3-20; if Applications is not visible see “Configuring Fallback Mode” on page 92) or Main menu: Applications System Tools Disk Utility (RHEL). Alternatively, you can give the command palimpsest from a terminal emulator (page 117) or Run Application window (ALT-F2). With a hard disk selected, the palimpsest Disk Utility window is divided into three sections (Figure 3-21): Storage Devices holds a list of CD/DVD drives, hard disks, and other devices; Drive holds information about the hard disk that is highlighted in the list of storage devices; and Volumes displays information about the partition highlighted in the graphical representation of the hard drive. When you select a hard disk in the Storage Devices section, palimpsest displays information about that disk in the Drive section of the window. Click one of the partitions in the graphical representation of the hard disk, and palimpsest displays information about that partition in the Volumes section. From this window you can view, create, and delete partitions. Although you can create partitions using palimpsest, you cannot specify the mount point (page 36) for a partition—this step must wait until you are installing Fedora/RHEL and using the Disk Druid partition editor. You can save time if you use palimpsest to examine a hard disk and Disk Druid to set up the partitions you install Fedora/RHEL on.

www.it-ebooks.info

Installation Tasks 79

Storage Devices section

Drive section

Peripheral Devices

Graphical representation of volumes on the disk

Volumes section

Figure 3-21

The palimpsest Disk Utility window

Displaying the Contents of a Filesystem
To display the contents of a filesystem, select the partition holding the filesystem as described on the previous page and click Mount Volume in the Volumes section of the Disk Utility window. Figure 3-21 shows Unmount Volume because the partition is already mounted. When you click the mount point (the link following Mount Point: mounted at) in the Volumes section, Nautilus displays the filesystem in a Nautilus window (Figure 3-22; more about Nautilus on page 102). When you have finished examining the contents of the filesystem, click Unmount Volume to unmount the filesystem.

Figure 3-22

Browsing a partition using palimpsest

www.it-ebooks.info

80 Chapter 3 Step-by-Step Installation

Figure 3-23

A disk without a partition table

Writing a Partition Table
A new disk does not have a partition table (page 34) and looks similar to the disk highlighted in Figure 3-23. In the Drive section of a Disk Utility window, Not Partitioned follows the Partitioning label, the graphical representation of the disk is marked Unknown or Unrecognized, and Usage is blank. If the disk you are working with already has a partition table, skip to the next section. To partition a hard disk, click Format Drive in the Drive section of the Disk Utility window: palimpsest opens a Format window holding a drop-down list labeled Scheme. Select a scheme. In most cases you will want to accept the default scheme of Master Boot Record. Click Format. After checking that you really want to format the drive, palimpsest creates the partition table. Now Master Boot Record follows the Partitioning label, the graphical representation of the disk is marked Free (free space; page 34), and Unallocated Space follows the Usage label. If you want to create a single filesystem that occupies the entire disk drive, instead of following the instructions in the preceding paragraph, click Format Volume in the Volumes section of the Disk Utility window: palimpsest opens a Format wholedisk volume window. To create a filesystem, follow the instructions for the Create partition window in the next section.

Creating a Partition and a Filesystem
Once you have created a partition table, you will be able to create a partition that holds a filesystem in the free space (page 34; labeled Free). When you click Create Partition, palimpsest opens a Create partition window (Figure 3-24). In this window, use the slider labeled Size or the adjacent spin box to specify the size of the new partition. Next specify a filesystem type; ext4 filesystems are the most common. You can optionally enter a disk label in the text box labeled Name. This name is not the mount point for the disk. Typically you will want to own the filesystem, so allow the tick to remain in the check box labeled Take

www.it-ebooks.info

Installation Tasks 81

Figure 3-24

The palimpsest Create partition window

ownership of file system. If you want the filesystem to be encrypted, put a tick in the check box labeled Encrypt underlying device. Click Create. After checking with you, palimpsest creates the filesystem. Now the graphical representation of the disk is divided to represent the division of the hard disk, and Usage corresponds to the highlighted section of the graphical representation (Filesystem or Unallocated Space). If you did not use all the free space, you can create additional partitions and filesystems in the same manner.

Deleting a Partition
Before deleting a partition, make sure it does not contain any data you need. To use the palimpsest utility to delete a partition, highlight the partition you want to delete in the graphical representation of the hard disk and click Delete Partition. After checking with you, palimpsest deletes the partition.

Using SMART to Display Disk Performance Information
SMART (Self-Monitoring, Analysis, and Reporting Technology) monitors hard disks and attempts to predict hard disk failures. To see a SMART report for a disk on the system, highlight the disk in the Storage Devices section and click Smart Data in the Drive section; palimpsest displays a window similar to the one shown in Figure 3-25 on the next page. From this window you can run various self-tests and scroll through the information at the bottom of the window.

Using the Kickstart Configurator
Kickstart is a Fedora/RHEL program that completely or partially automates the same installation and post-installation configuration on one or more machines. To use Kickstart, you create a single file that answers all the questions that are normally asked during an installation. Anaconda then refers to this file instead of asking you questions during installation. See the ks boot parameter on page 70.

www.it-ebooks.info

82 Chapter 3 Step-by-Step Installation

Figure 3-25

SMART data as displayed by palimpsest

Using Kickstart, you can automate language selection, network configuration, keyboard selection, boot loader installation, disk partitioning, X Window System configuration, and more. The system-config-kickstart utility displays the Kickstart Configurator window (Figure 3-26), which creates a Kickstart installation script. This utility is part of the system-config-kickstart software package but is not installed by default. You can install this package using yum; see page 534. To run this utility, enter system-configkickstart on a command line or select Main menu: Applications System Tools Kickstart. Figure 3-26 shows the first window the Kickstart Configurator displays. To generate a Kickstart file (ks.cfg by default), go through each section of this window (the items along the left side) and fill in the answers and put ticks in the appropriate check boxes. It might be helpful to start with the Kickstart installation script that Anaconda generated when you installed the system (/root/anaconda-ks.cfg). Click Help on the menubar for instructions on completing these tasks. When you are finished, click File Save. The Kickstart Configurator gives you a chance to review the generated script before it saves the file.

Setting Up a Dual-Boot System
A dual-boot system is one that can boot one of two (or more) operating systems. This section describes how to add Fedora/RHEL to a system that can boot Windows, thereby creating a system that can boot Windows or Linux. You can use the same technique for adding Fedora/RHEL to a system that runs a different version or distribution of Linux.

www.it-ebooks.info

Installation Tasks 83

Figure 3-26

The Kickstart Configurator window

Before you start setting up a dual-boot system, you need to find disk space for the new Fedora/RHEL system. The next section discusses several ways to create the needed space.

Creating Free Space on a Windows System
Typically you install Fedora/RHEL in free space on a hard disk. To add Fedora/RHEL to a Windows system, you must have enough free space on a hard disk that already holds Windows. There are several ways to provide or create this free space. The following paragraphs discuss these options in order from easiest to most difficult. Add a new hard disk. Add another hard disk to the system and install Linux on the new disk, which contains only free space. This technique is very easy and clean but requires a new hard disk. Use existing free space. If there is sufficient free space on the Windows disk, you can install Linux there. This technique is the optimal choice, but there is rarely enough free space on an installed hard disk.

Always defragment before resizing caution You must boot Windows and defragment a Windows partition before you resize it. Sometimes you might need to run the Windows defragmenter several times to consolidate most file fragments. Not only will defragmenting give you more space for a Linux partition, but it might also keep the process of setting up a dual-boot system from failing.

Resize Windows partitions. Windows partitions often occupy the entire disk, which explains why resizing a Windows partition is the technique most commonly used to free

www.it-ebooks.info

84 Chapter 3 Step-by-Step Installation

Figure 3-27

The Displays window

up space. Windows systems typically use NTFS, FAT32, and/or FAT16 filesystems. You can resize an existing Windows partition when you install Fedora/RHEL. Alternatively, you can use the palimpsest utility from a live session to examine and resize a partition to open up free space in which to install Linux (page 77). Remove a Windows partition. If you can delete a big enough Windows partition, you can install Linux in its place. To delete a Windows partition, you must have multiple partitions under Windows and be willing to lose the data in the partition you delete. In many cases, you can save this data by moving it from the partition you will delete to another Windows partition. Once you are sure a partition contains no useful information, you can delete it when you install Fedora/RHEL. Alternatively, you can use palimpsest from a live session to delete it (page 81). After deleting the partition, you can install Fedora/RHEL in the free space formerly occupied by the partition you deleted.

Installing Fedora/RHEL as the Second Operating System
After you have created enough free space on a Windows system, you can begin installing Fedora/RHEL. When you get to the Type of Installation screen (Figure 3-8, page 61), choose Use free space to have Anaconda partition the free space on the hard disk automatically. If you need to delete a Windows partition, you must choose Create custom layout; this selection calls Disk Druid (page 71) so you can delete the appropriate Windows partition and create Linux partitions in the free space. When you boot the system, you will be able to choose which operating system you want to run.

www.it-ebooks.info

Chapter Summary 85

gnome-control-center/Displays: Configures the Display
Most of the time the monitor attached to the system just works: Fedora/RHEL probes the monitor and autoconfigures X.org to work properly. This section explains how to configure the display in case you need to. Under Fedora you can use the Displays window (Figure 3-27) to configure the monitor. To display this window, select Main menu: Applications System Tools System Settings (if Applications is not visible see “Configuring Fallback Mode” on page 92) or give the command gnome-control-center from a terminal emulator or Run Application window (ALT-F2) and click Displays in the Hardware section. Check the specifications for the monitor and select the appropriate resolution value from the dropdown list. RHEL uses the Display Preferences window for the same purpose. To display this window, select Main menu: System Preferences Display or give the command gnome-display-properties from a terminal emulator or Run Application window (ALT-F2).

Chapter Summary
Most installations of Fedora/RHEL begin by booting from the live CD or the install DVD. When the system boots from the CD/DVD, it displays a message saying when it will boot automatically. During the time it displays this message, you can give various commands and then have the system continue booting. The program that installs Fedora/RHEL is named Anaconda. Anaconda calls tools that identify the hardware, build the necessary filesystems, and install or upgrade the Fedora/RHEL operating system. Anaconda can run in textual or graphical (default) interactive mode or in batch mode using Kickstart. The Disk Druid graphical disk-partitioning program can add, delete, and modify partitions and logical volumes (LVs) on a hard disk during installation. The palimpsest utility reports on and manipulates hard disk partitions before or after installation. The system-config-lvm utility works with logical volumes after installation. A dual-boot system can boot one of two or more operating systems, frequently Windows and Linux. The biggest task in setting up a dual-boot system, assuming you want to add Linux to a Windows system, is finding enough disk space to hold Linux. Fedora/RHEL uses the X.org X Window System. Fedora/RHEL uses the GNOME display manager (gdm) to provide a graphical login.

www.it-ebooks.info

86 Chapter 3 Step-by-Step Installation

Exercises
1. What is a live system? What advantages does it have over an installed system? 2. Which boot parameter would you use to begin an FTP installation? 3. Describe the Anaconda installer. 4. Where on the disk should you put your /boot partition or the root (/) partition if you do not use a /boot partition? 5. If the graphical installer does not work, what three steps should you try? 6. When might you specify an ext2 filesystem instead of ext4? 7. Describe Disk Druid.

Advanced Exercises
8. When does a Fedora/RHEL system start X by default? 9. If you do not install grub on the master boot record of the hard disk, how can you boot Linux? 10. Why would you place /var at the beginning of the disk? 11. How does Anaconda set up a hard disk by default?

www.it-ebooks.info

I

PART II Getting Started with Fedora/RHEL
CHAPTER 4 Introduction to Fedora and Red Hat Enterprise Linux 89 CHAPTER 5 The Linux Utilities CHAPTER 6 The Linux Filesystem CHAPTER 7 The Shell 225 145 185

87

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

4
Introduction to Fedora and Red Hat Enterprise Linux

C 4 hapter4

In This Chapter
Curbing Your Power (Superuser/root Privileges) . . . . . . . . . . . . . . . . . . 90 A Tour of the Fedora/RHEL Desktop 90 Mouse Preferences . . . . . . . . . . . . 101 Using Nautilus to Work with Files 102 Updating Software . . . . . . . . . . . . 109 The Main Menu . . . . . . . . . . . . . . . 114 Updating, Installing, and Removing Software Packages . . . . . . . . . . 122 Where to Find Documentation . . . 125 More About Logging In . . . . . . . . . 134 What to Do if You Cannot Log In. . 135 Working from the Command Line 139

Objectives
After reading this chapter you should be able to: Log in on the Fedora desktop Understand root privileges Configure GNOME to run in Fallback mode Change the desktop background Install and work with gnome-tweak-tool Change the number of workspaces on the desktop Use Nautilus to work with files Explain what you can do using a window titlebar Update and install software Find documentation Open a terminal emulator and launch programs from the command line
89

www.it-ebooks.info

90 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

One way or another you are sitting in front of a computer that is running Fedora or RHEL (Red Hat Enterprise Linux). After describing root (Superuser) privileges, this chapter takes you on a tour of the system to give you some ideas about what you can do with it. The tour does not go into depth about choices, options, menus, and so on; that is left for you to experiment with and to explore in greater detail in Chapter 8 and throughout later chapters of this book. Instead, this chapter presents a cook’s tour of the Linux kitchen: As you read it, you will have a chance to sample the dishes that you will enjoy more fully as you read the rest of this book. Following the tour is a section that describes where to find Linux documentation (page 125). The next section offers more about logging in on the system, including information about passwords (page 134). Be sure to read the warning about the dangers of misusing the powers of root (Superuser) in the next section. While heeding that warning, feel free to experiment with the system: Give commands, create files, click objects, choose items from menus, follow the examples in this book, and have fun.

Curbing Your Power (Superuser/root Privileges)
While you are logged in as the user named root, you are referred to as Superuser or administrator; you are working with root privileges and have extraordinary systemwide powers. Running the su or sudo utility can give you similar privileges. When working with root privileges, you can read from or write to almost any file on the system, execute programs that ordinary users cannot, and more. On a multiuser system you might not be permitted to know the root password and so might not be able to run certain programs. Nevertheless, someone—the system administrator—knows the root password, and that person maintains the system. When you are running Linux on your own computer, you will assign a password to root when you install Linux. Refer to “Running Commands with root Privileges” on page 409 for more information.

Do not experiment while you are working with root privileges caution Feel free to experiment when you are not working with root privileges. When you are working with root privileges, do only what you have to do and make sure you know exactly what you are doing. After you have completed the task at hand, revert to working as yourself. When working with root privileges, you can damage the system to such an extent that you will need to reinstall Linux to get it working again.

A Tour of the Fedora/RHEL Desktop
This section presents new words (for some readers) in a context that explains the terms well enough to get you started using the Linux desktop. If you would like exact definitions as you read this section, refer to “GNOME Desktop Terminology”

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop 91

Figure 4-1

The Fedora GNOME Login screen

on page 112 and to the Glossary. The Glossary also describes the data entry widgets (page 1197), such as the combo box (page 1157), drop-down list (page 1163), list box (page 1173), and text box (page 1192).
GNOME

GNOME (www.gnome.org), a product of the GNU project (page 5), is the userfriendly default desktop manager under Fedora/RHEL. KDE, the K Desktop Environment, is a powerful desktop manager and complete set of tools you can use in place of GNOME (www.kde.org/community/whatiskde). Use the Fedora KDE live media or the Fedora KDE spin to install KDE. This tour describes GNOME, a full-featured, mature desktop environment that boasts a rich assortment of configurable tools. After discussing logging in, this section covers desktop features—including panels, objects, and workspaces—and explains how to move easily from one workspace to another. It describes several ways to launch objects (run programs) from the desktop, how to set up the desktop to meet your needs and please your senses, and how to manipulate windows. As the tour continues, it explains how to work with files and folders using the Nautilus File Browser window, one of the most important GNOME tools. The tour concludes with a discussion of the Software Update window, the tool that allows you to keep a system up-to-date with the click of a button; getting help; and logging out.

Logging In on the System
When you boot a standard Fedora/RHEL system, GDM (GNOME display manager) displays a Login screen (Figure 4-1) on the system console. In the middle of the screen is a window that holds a list of names. When you click a name, Fedora/RHEL displays a text box labeled Password. In addition, at the upper-right corner of the Fedora login screen and the lower-right corner of the RHEL login screen is an icon that allows you to restart or shut down the system. For more information refer to “The Login Screen” on page 134.

www.it-ebooks.info

92 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-2

The System Settings window

To log in, click your name. A text box labeled Password appears. Enter your password and press RETURN. If Fedora/RHEL displays an error message, try clicking your name and entering your password again. Make sure the CAPS LOCK key is not on (Fedora/RHEL displays a message if it is) because the routine that verifies your entries is case sensitive. See page 135 if you need help with logging in and page 137 if you want to change your password. The system takes a moment to set things up and then displays a workspace.

Configuring Fallback Mode (Fedora)
Fedora 15 introduced GNOME 3, the GNOME shell, and the Mutter window manager. (Mutter is short for Metacity Clutter; the graphics library is named Clutter). RHEL uses GNOME 2 and the Metacity window manager. GNOME 3 is radically different from GNOME 2, following the trend toward simpler, more graphical desktops that have more icons and fewer menus. This book uses the Linux textual interface for most tasks, including setting up servers and programming in bash and Perl. Where this book does refer to the graphical interface (GUI), it refers to a desktop running in Fallback mode, which appears similar to GNOME 2/Metacity. Most modern graphics cards support the full GNOME 3 desktop. When you log in on a Fedora system that has a graphics card that cannot support GNOME 3, GNOME displays a window with the message GNOME 3 Failed to Load. When you click Close on that window, GNOME displays a desktop running in Fallback mode. Figure 3-3 on page 55 shows part of a full GNOME 3 desktop, and Figure 3-4 on page 56 shows menus on a desktop running in Fallback mode.

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop 93

Figure 4-3

The System Info window showing Graphics settings

To follow the examples in this book, you must run GNOME in Fallback mode tip The following instructions explain how to force Fedora to run in Fallback mode.
To follow the examples in this book while running Fedora, you must run GNOME in Fallback Mode. No action is needed if you are running RHEL. The following instructions explain how to force Fedora to run GNOME in Fallback mode: 1. With GNOME running the full GNOME 3 desktop, it displays the word Activities at the upper-left corner of the screen, and your name (if you are logged in on an installed system) or Live System User (if you are running a live session) at the upper-right corner of the screen. Click your name or Live System User; GNOME displays a menu. 2. Click System Settings on the menu; GNOME displays the System Settings window (Figure 4-2). 3. Click System Info in the section labeled System at the bottom of the window; GNOME displays the System Info window (Figure 4-3). 4. Click Graphics at the right of the window; GNOME displays a switch labeled Forced Fallback Mode on the right side of the window. 5. To run in Fallback mode, the switch labeled Forced Fallback Mode must display ON. If it displays OFF, click the switch so it displays ON; GNOME displays The next login will use the fallback mode ... below the switch. 6. Click the x at the upper-right corner of the window to close the window. 7. Log off by clicking your name or Live System User at the upper-right corner of the screen, click Log Out from the menu GNOME displays, and then click the button labeled Log Out from the small window GNOME displays.

www.it-ebooks.info

94 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

8. Log in on the system and GNOME will be running in Fallback mode. The desktop is in Fallback mode if the words Applications and Places appear at the upper-left corner of the screen.

Installing and Using gnome-tweak-tool (Fedora)
GNOME 3 turns off many desktop manager and file manager features that were available under GNOME 2. The Tweak Tool window enables you to turn on some of these features. One of these features is the correspondence between the files displayed on the desktop and the files in your Desktop directory. By default under GNOME 3, the files displayed on the desktop do not correspond to the files in your Desktop directory. To follow some of the examples in this book you must establish this correspondence. See the following tip for instructions on how to set up this correspondence. The section titled “The Desktop Directory” on page 105 demonstrates this correspondence. Before you can display and work with the Tweak Tool window, you must install the gnome-tweak-tool software package. To do so, open a terminal emulator window by selecting Main menu: Applications System Tools Terminal as shown in Figure 4-5 on page 96. (If Applications does not appear on the Main menu, set GNOME to run in Fallback mode as explained in the previous section.) Then give the following command:
$ su -c 'yum install gnome-tweak-tool'

The su utility will prompt for the root password. Once you enter the root password and press RETURN, yum will ask for your confirmation and then install the gnometweak-tool package. For more information on using yum to install packages see page 534. The Tweak Tool window (Figure 4-4) enables you to set up the Nautilus file manager to handle the desktop and to configure other aspects of the desktop and file manager. To display this window, select Main menu: Applications Accessories Tweak Advanced Settings or give the command gnome-tweak-tool from a terminal emulator or Run Application window (ALT-F2).

To follow the examples in this book, you must have the file manager handle the desktop tip By default, the files displayed on the desktop do not correspond to the files in your Desktop directory. To follow some of the examples in this book you must establish this correspondence. To do so, install gnome-tweak-tool and open the Tweak Tool window as explained in this section. Then click File Manager at the left of the Tweak Tool window (Figure 4-4); GNOME displays a switch labeled Have file manager handle the desktop at the right of the window. Click the switch so it displays ON and close the window. Now the files on the desktop will (mostly; see the optional section on page 105 for exceptions) correspond to those in your Desktop directory. You might have to take an action, such as selecting Main menu: Places Desktop before icons appear on the desktop.

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop 95

Figure 4-4

The Tweak Tool window showing the File Manager selection

The left side of the Tweak Tool window displays the following selections. Click a selection to change the display on the right side of the window. Click x at the upperright corner of the window to close the window. • Fonts—Changes and resizes the fonts used on the desktop • Shell—Controls what the system does when you close the lid on a laptop, which buttons appear on window titlebars, and more • Windows—Selects the window theme and controls how windows respond to various mouse clicks • File Manger—Controls whether the files on the desktop correspond to the files in your Desktop directory (page 105) and activates the desktop rightclick menu (page 117) • Interface—Selects the desktop, icon, and cursor themes

Introduction to the Desktop
You can use the desktop as is or you can customize it until it looks and functions nothing like the initial desktop. If you have a computer of your own, you might want to add a user (page 602) and work as that user while you experiment with the desktop. When you figure out which features you like, you can log in as yourself and implement those features. That way you need not concern yourself with “ruining” your desktop and not being able to get it back to a satisfactory configuration.
Panels and objects

When you log in, GNOME displays a workspace that includes Top and Bottom panels (bars) that are essential to getting your work done easily and efficiently

www.it-ebooks.info

96 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Sound Top panel Main menu NetworkManager

Clock

Username

Tooltip

Bottom panel

Workspace Switcher

Figure 4-5

The initial workspace showing Main menu: Applications System Tools Terminal

(Figure 4-5). Each of the panels holds several icons and words called objects. (Buttons, applets, and menus, for example, are all types of objects.) When you click an object, something happens. A panel does not allow you to do anything you could not do otherwise, but rather collects objects in one place and makes your work with the system easier.
Workspaces and the desktop

What you see displayed on the screen is a workspace. Initially Fedora/RHEL configures GNOME with two workspaces. The desktop, which is not displayed all at once, is the collection of all workspaces. “Switching Workspaces” on page 98 describes some of the things you can do with workspaces.

Launching Programs from the Desktop
This section describes three of the many ways you can start a program running from the desktop.
Click an object

The effect of clicking an object depends on what the object is designed to do. Clicking an object might, for example, start a program, display a menu or a folder, or open a file, a window, or a dialog box.

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop 97

Figure 4-6

The Run Application window

For example, to display the volume control, (left-) click the sound object on the Top panel (Figure 4-5). GNOME opens the volume control. When you are done using the volume control, click anywhere on the desktop (but not on the volume control) to close it. When you (left-) click the date and time on the Top panel, the Clock applet displays a calendar for the current month. Click the date and time again to close the calendar.

Click and right-click tip This book uses the term click when you need to click the left mouse button. It uses the term rightclick when you need to click the right mouse button. See page 101 for instructions on adapting the mouse for left-handed use.
Select from the Main menu

The second way to start a program is by selecting it from a menu. The Main menu is the object at the left end of the Top panel that includes the words Applications and Places. Click one of these words to display the corresponding menu. Each menu selection that holds a submenu displays an open triangle (pointing to the right) to the right of the name of the menu (Figure 4-5). When you move the mouse pointer over one of these selections and leave it there for a moment (this action is called hovering), the menu displays the submenu. When you allow the mouse cursor to hover over one of the submenu selections, GNOME displays a tooltip (page 112). Experiment with the Main menu. Start Solitaire (Main menu: Applications Games AisleRiot Solitaire [Fedora; RHEL does not include games by default]), a terminal emulator (Main menu: Applications System Tools Terminal, and other programs from the Applications menu. The Places menu is discussed on page 114.

Use the Run Application window

You can also start a program by pressing ALT-F2 to display the Run Application window (Figure 4-6). As you start to type firefox in the text box at the top of the window, for example, the window recognizes what you are typing and displays the Firefox logo and the rest of the word firefox. Click Run to start Firefox.

www.it-ebooks.info

98 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

optional
Running textual applications

You can run command-line utilities, which are textual (not graphical), from the Run Applications window. When you run a textual utility from this window, you must put a tick in the check box labeled Run in terminal (click the check box to put a tick in it; click it again to remove the tick). The tick tells GNOME to run the command in a terminal emulator window. When the utility finishes running, GNOME closes the window. For example, type vi (the name of a text-based editor) in the text box, put a tick in the check box labeled Run in terminal, and click Run. GNOME opens a Terminal (emulator) window and runs the vim text editor in that window. When you exit from vim (press ESCAPE:q!RETURN sequentially to do so), GNOME closes the Terminal window. You can run a command-line utility that only displays output and then terminates. Because the window closes as soon as the utility is finished running and because most utilities run quickly, you will probably not see the output. Type the following command in the text box to run the df (disk free; page 794) utility and keep the window open until you press RETURN (remember to put a tick in the check box labeled Run in terminal): bash -c "df -h ; read"

This command starts a bash shell (Chapter 7) that executes the command line following the –c option. The command line holds two commands separated by a semicolon. The second command, read (page 1019), waits for you to press RETURN before terminating. Thus the output from the df –h command remains on the screen until you press RETURN. Replace read with sleep 10 to have the window remain open for ten seconds.

Switching Workspaces
Workspace Switcher

Each rectangle in the Workspace Switcher applet (or just Switcher)—the group of rectangles near the right end of the Bottom panel—represents a workspace (Figure 4-5, page 96). When you click a rectangle, the Switcher displays the corresponding workspace and highlights the rectangle to indicate which workspace is displayed. You can also press CONTROL-ALT-RIGHT ARROW to display the workspace to the right of the current workspace; pressing CONTROL-ALT-LEFT ARROW works in the opposite direction. Click the rightmost rectangle in the Switcher and then select Main menu: Applications System Tools System Settings and select Hardware/Mouse and Touchpad (Fedora) or Main menu: System Preferences Mouse (RHEL). GNOME opens the Mouse and Touchpad window. The Switcher rectangle that corresponds to the workspace you are working in displays a small rectangle. This rectangle corresponds in size and location within the Switcher rectangle to the window within the workspace. Click and hold the left mouse button with the mouse pointer on the titlebar at the top of the window and drag the window to the edge of the desktop. The small rectangle within the Switcher moves to the corresponding location within the Switcher rectangle.

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop 99

Figure 4-7

The Workspace Switcher Preferences window

Now click a different rectangle in the Switcher and open another application—for example, the Desktop Help window (select Main menu: Applications Accessories Help [Fedora] or Main menu: System Help [RHEL]). With the Desktop Help window in one workspace and the Mouse Preferences window in another, you can click the corresponding rectangles in the Switcher to switch back and forth between the workspaces (and applications). You can move a window from one workspace to another by right-clicking the Window List applet (page 113) on the Bottom panel and selecting one of the choices that starts with Move.

Right-click to display an Object context menu tip A context menu is one that is appropriate to its context. When you right-click an object, it displays an Object context menu. Each object displays its own context menu, although similar objects have similar context menus. Most Object context menus have either a Preferences or Properties selection. See the following section, “Setting Personal Preferences,” and page 119 for more information on Object context menus.

Setting Personal Preferences
You can set preferences for many objects on the desktop; you can also change the desktop background.
Workspace Switcher

To display the Workspace Switcher Preferences window (Figure 4-7), first right-click anywhere on the Switcher to display the Switcher menu and then select Preferences (the only entry in this menu). Specify the number of workspaces you want in the spin

www.it-ebooks.info

100 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-8

The Background window

box labeled Number of workspaces. The number of workspaces the Switcher displays changes as you change the number in the spin box—you can see the result of your actions before you close the Preferences window. Four workspaces is typically a good number to start with. Click Close.
Clock applet

The Clock applet has an interesting Preferences window. Right-click the Clock applet (Figure 4-5, page 96) and select Preferences. GNOME displays the General tab of the Clock Preferences window. This tab enables you to customize the date and time displayed on the Top panel. The clock immediately reflects the changes you make in this window. Click the Locations tab and then the Add button and enter the name of the city you are in or near to cause the Clock applet to display weather information. Click the Weather tab to specify temperature and wind speed units. Different objects display different Preferences windows. In contrast, objects that launch programs display Properties windows and do not have Preferences windows. Experiment with different Preferences and Properties windows and see what happens.

Desktop background

The Background window (Figure 4-8) enables you to change the desktop background. To display this window, select Main menu: Applications System Tools System Settings or give the command gnome-control-center from a terminal emulator or Run Application window (ALT-F2). Then click Personal/Background. Or, under RHEL or with the file manager handling the desktop (page 94) under Fedora, right-click on an empty space on the desktop and select Change Desktop Background. Click the drop-down list near the upper-left corner of the window to select from Wallpapers, Pictures Folder, and Colors & Gradients. Click the drop-down list at

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop

101

Figure 4-9

The Mouse and Touchpad window

the lower-right corner of the window to select how you want the image displayed on the desktop (e.g., zoom, center, tile). Click the colored button to the left of this drop-down list to select a color for the desktop from the Pick a Color window (page 274).

Mouse Preferences
The Mouse and Touchpad window (Figure 4-9) enables you to change the characteristics of the mouse to suit your needs. To display this window, select Main menu: Applications System Tools System Settings or give the command gnome-controlcenter from a terminal emulator or Run Application window (ALT-F2). Then click Hardware/Mouse and Touchpad.
Left-handed mouse

To change the orientation of the mouse buttons for use by a left-handed person, click the radio button labeled Left-handed. If you change the setup of the mouse buttons, remember to reinterpret the descriptions in this book accordingly. That is, when this book asks you to click the left button or does not specify a button to click, click the right button, and vice versa. See “Remapping Mouse Buttons” on page 264 for information on changing the orientation of the mouse buttons from the command line. Use the Double-Click Timeout slider to change the speed with which you must double-click a mouse button to have the system recognize your action as a doubleclick rather than as two single clicks. You can also control the acceleration and sensitivity of the mouse. The Drag and Drop Threshold specifies how far you must drag an object before the system considers the action to be the drag part of a drag-and-drop operation.

Double-click timeout

www.it-ebooks.info

102 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Working with Windows
To resize a window, position the mouse pointer over an edge of the window; the pointer turns into an arrow pointing to a line. When the pointer is an arrow pointing to a line, you can click and drag the side of a window. When you position the mouse pointer over a corner of the window, you can resize both the height and the width of the window simultaneously. Some windows are not resizeable. To move a window, click and drag the titlebar (the bar across the top of the window with the name of the window in it). Alternatively, when you hold the ALT key down you can move a window by clicking and dragging any part of the window. For fun, try moving the window past either side of the workspace. What happens?
Titlebar

At the right of the titlebar are up to three icons that control the window (Figure 4-18, page 115). Clicking the line, which usually appears at the left end of the set of icons, minimizes (iconifies) the window so the only indication of the window is the object with the window’s name in it on the Bottom panel (a Window List applet; page 113). Click the Window List applet to toggle the window between visible and minimized. Clicking the square icon, which usually appears in the middle of the three icons, maximizes the window (displays the window at its maximum size). Clicking the rectangle again returns the window to its normal size. Double-clicking the titlebar toggles the window between its normal and maximum size. The Shell selection of the Tweak Tool window (page 94) controls which buttons appear on titlebars. Clicking the x closes the window and usually terminates the program running in the window. In some cases you might need to click several times. Some programs, such as Rhythmbox Music Player, do not terminate but rather continue to run in the background. When in this state, the program displays an icon on the Top panel. Right click the icon and select Quit from the drop-down list to terminate the program.

Terminating a program

Using Nautilus to Work with Files
Nautilus, the GNOME file manager, is a simple, powerful file manager. You can use it to create, open, view, move, and copy files and folders as well as to execute programs and scripts. One of its most basic and important functions is to create and manage the desktop. This section introduces Nautilus and demonstrates the correspondence between Nautilus and the desktop. See page 266 for more detailed information on Nautilus.

Set up desktop Fallback mode and file manager control of the desktop tip To follow the examples in this section, you must run the desktop in Fallback mode: See “Configuring
Fallback Mode” on page 92. The examples also require you to install gnome-tweak-tool and configure the file manager to handle the desktop: See page 94.
Terms: folder and directory

Nautilus displays the File Browser window, which displays the contents of a folder. The terms folder and directory are synonymous; “folder” is frequently used in graphical contexts, whereas “directory” might be used in textual or command-line contexts. This book uses these terms interchangeably.

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop

103

Figure 4-10
Term: File Browser

The Nautilus Spatial view (left) and File Browser window (right)

This book sometimes uses the terms File Browser window and File Browser when referring to the Nautilus File Browser window. Select Main menu: Places Home Folder to open a Nautilus File Browser window that shows the files in your home folder. Double-clicking an object in a File Browser window has the same effect as doubleclicking an object on the desktop: Nautilus takes an action appropriate to the object. For example, when you double-click a text file, Nautilus opens the file with a text editor. When you double-click an LibreOffice document, Nautilus opens the file with LibreOffice. If the file is executable, Nautilus runs it. If the file is a folder, Nautilus opens the folder and displays its contents in place of what had previously appeared in the window. From within a Nautilus File Browser window, you can open a folder in a new tab. To do so, middle-click the folder or right-click the folder and select Open in New Tab from the drop-down list; Nautilus displays a new tab named for the folder you clicked. Click the tab to display contents of the directory.

Opening Nautilus

To follow examples under RHEL, turn off Nautilus Spatial view, turn on File Browser windows tip By default, RHEL display the Nautilus Spatial view. The examples in this book show Nautilus displaying File Browser windows. To make the Nautilus windows on your desktop correspond to the windows in the figures in this book, turn on Nautilus File Browser windows by following the instructions under the next section, “The Two Faces of Nautilus.”

The Two Faces of Nautilus (RHEL)
Under RHEL, the appearance of Nautilus differs depending on how it is set up: It can display a Spatial view or a File Browser window. Figure 4-10 shows an example of each type of display. By default, RHEL is set up to display the Spatial view. Because the Spatial view is less conventional, this book uses the Nautilus File Browser window in examples. The following steps make File Browser windows the Nautilus default and ensure that Nautilus windows on the screen look similar to those in this book. See page 272 for information on the Spatial view.

www.it-ebooks.info

104 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-11

The File Management Preferences window, Behavior tab

To turn off the Nautilus Spatial view and turn on File Browser windows under RHEL: 1. Select Main menu: Places Home folder. Nautilus opens a window that displays a Spatial view of the contents of your home folder (also called your home directory; page 191). It appears similar to the left side of Figure 4-10 (previous page). 2. From the menubar of the new window, select Edit Preferences. Nautilus displays the File Management Preferences window. 3. Click the tab labeled Behavior. See Figure 4-11. 4. Click the check box labeled Always open in browser windows. A tick will appear in the check box. 5. Click the button labeled Close at the lower-right corner of the File Management Preferences window to close the window. 6. Click the x at the upper-right corner of the home folder window to close the window. After following these steps, Nautilus will always display File Browser windows as shown in this book.

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop

105

Figure 4-12

Part of a workspace with a Nautilus File Browser window

The Desktop Directory
Before you start reading this section ... tip The examples in this section do not work with the default Fedora/RHEL settings. Before continuing:
Fedora: If you are running Fedora, set up the desktop to run in Fallback mode (page 92) and use gnome-tweak-tool to set up the Nautilus file manager to handle the desktop (page 94). RHEL: If you are running RHEL, turn off the Nautilus Spatial view (page 103).

The files on the desktop are held in a directory that has a pathname (page 191) of /home/username/Desktop, where username is your login name or, if you are logged in on a live session, Live System User. The simple directory name is Desktop. When you select Main menu: Places Desktop, GNOME opens a File Browser window showing the files on the desktop (Figure 4-12). Nautilus does not show the Computer, home directory, and Trash directory in the browser window; see the following optional section for an explanation. The buttons below the toolbar and to the right of Computer or Devices show the pathname of the directory Nautilus is displaying (Home Desktop in the Figure 4-12).

optional Although icons for the Computer, home directory, and Trash directory appear on the desktop, these files are not stored in the /home/username/Desktop directory. Because they are not in the Desktop directory, they do not appear in a Nautilus window that displays the contents of the Desktop directory. These icons appear on the desktop because of the way the GNOME configuration files are set up. The

www.it-ebooks.info

106 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

GNOME configuration files are XML (page 1198) files that reside in the directory hierarchy with its root at /home/username/.gconf. Although it is not recommended, you can edit these files with gconf-editor. You might need to install the gconf-editor package to use this editor. See projects.gnome.org/gconf for more information. To see the correspondence between the graphical desktop and the Desktop directory, right-click anywhere within the large clear area of the Desktop File Browser window. Select Create New Document Empty Document. Nautilus creates a new file on the desktop and displays its object in this window. When you create this file, GNOME highlights the name Untitled Document under the file: You can type any name you like at this point. Press RETURN when you are finished entering the name. If you double-click the new file, Nautilus assumes it is a text file and opens the file in a gedit window. (The gedit utility is a simple text editor.) Type some text and click Save on the toolbar. Close the window either by selecting Quit from the File menu or by clicking the x at the left end of the titlebar. You have created a text document on the desktop. You can now double-click the document object on the desktop or in the File Browser window to open and edit it. Next, create a folder by right-clicking the root window (any empty part of the workspace) and selecting Create New Folder. You can name this folder in the same way you named the file you created previously. The folder object appears on the desktop and within the Desktop File Browser window. On the desktop, drag the file until it is over the folder; the folder opens. Release the mouse button to drop the file into the folder; GNOME moves the file to the folder. Again on the desktop, double-click the folder you just moved the file to. GNOME opens another File Browser window, this one displaying the contents of the folder. The file you moved to the folder appears in the new window. Now drag the file from the window to the previously opened Desktop File Browser window. The file is back on the desktop, although it might be hidden by one of the File Browser windows. Next, open a word processing document by selecting Main menu: Applications Office LibreOffice Writer. If that program is not available, select Main menu: Applications Accessories gedit Text Editor. Type some text and click the Save icon (the floppy disk) or select menubar: File Save to save the document. The editor displays a Save window (Figure 4-13). Type the name you want to save the document as (use memo for now) in the text box labeled Name. You can specify the directory in which you want to save the document in one of two ways: by using the drop-down list labeled Save in folder or by using the Browse for other folders section of the Save window. Click the triangle to the left of Browse for other folders to open this section of the window. When you open this section, the arrow changes orientation; click the arrow again to close this section. Figure 4-13 shows the Save window with this

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop

107

Figure 4-13

The Save window

section closed. With the Browse for other folders section closed, you can select a directory from the drop-down list labeled Save in folder. This technique is quick and easy but presents a limited number of choices of folders. By default, it saves the document in Documents (/home/username/Documents). If you want to save the document to the desktop, select Desktop from this drop-down list and then click Save. LibreOffice saves the document with a filename extension of .odt, which indicates it is an LibreOffice word processing document. The object for this type of file has some text and a stripe or picture in it.

optional
Browse/Save window

With the Browse for other folders section opened (click the triangle to the left of Browse for other folders), the Save window grays out the drop-down list labeled Save in folder and expands the Browse for other folders section, as shown in Figure 4-14 on the next page. This expanded section holds two large side-by-side list boxes: Places and Name. The list box labeled Places displays directories and locations on the system, including File System. The list box labeled Name lists the files within the directory named in the highlighted button above the Places list box. The Browse for other folders section of the Save window allows you to look through the filesystem and select a directory or file. GNOME utilities and many applications use this window, although sometimes applications call it a Browse window. In this example, the word processor calls it a Save window and uses it to locate the directory in which it will save the document. Assume you want to save a file in the /tmp directory. Click File System in the list box on the left. The list box on the right displays the files and directories in the root directory (represented by /; see “Absolute Pathnames” on page 192 for more information). Next, double-click tmp in the list box on the right. The buttons above the list box on the left change to reflect the directory displayed in the list box on the right. Click Save. The buttons above the left-side list box represent directories. The right-side list box displays the directories found within the directory named in the highlighted (darker)

www.it-ebooks.info

108 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-14

The Save window with Browse for other folders open

button. This directory is the one you would save the file to if you clicked Save at this point. Click one of these buttons to display the corresponding directory in the list box on the right and then click Save to save the file in that directory. When you have finished editing the document, close the window. If you have made changes since you last saved it, the word processor asks if you want to save the document. If you choose to save it, the word processor saves the revised version over (in the same file as) the version you saved previously. Now the memo.odt object appears on the desktop and in the Desktop File Browser window. Double-click either object to open it.
The Desktop directory is special

In summary, the Desktop directory is like any other directory, except GNOME displays its contents on every workspace of the desktop (but only when you have set up the file manager to handle the desktop as explained on page 94). It is as though the desktop were a large, plain Desktop File Browser window. You can work with the Desktop directory because it is always displayed. Within the GUI,

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop

109

you must use a utility, such as Nautilus, to display and work with the contents of any other directory.

Selecting Objects
The same techniques can be used to select one or more objects in a File Browser window or on the desktop. Select an object by clicking it once; GNOME highlights the object. Select additional objects by holding down the CONTROL key while you click each object. To select a group of adjacent objects, highlight the first object and then, while holding down the SHIFT key, click the last object; GNOME highlights all objects between the two objects you clicked. Alternatively, you can use the mouse pointer to drag a box around a group of objects. To experiment with these techniques, open a File Browser window displaying your home folder. Select a few objects, right-click, and select Copy. Now move the mouse pointer over an empty part of the desktop, right-click, and select Paste. You have copied the selected objects from your home folder to the desktop. You can drag and drop objects to move them, although you cannot move the Desktop folder on top of itself.

Trash
Selecting File Browser menubar: Edit Move to Trash moves the selected (highlighted) object to the Trash directory. Because files in the trash take up space on the hard disk (just as any files do), it is a good idea to remove them periodically. To view the files in the trash, double-click the Trash icon on the desktop (visible if you have the file manager handling the desktop [page 94]) or click the word Trash on the left side of a File Browser window; Nautilus displays the Trash File Browser window.
Emptying the trash

Select Empty Trash from the Trash icon right-click (context) menu to permanently remove all files from the trash. (This selection does not appear if there are no files in the trash.) You can also select Empty Trash from the context menu Nautilus displays when you right-click the word Trash on the left side of a File Browser window. Alternatively, you can right-click an object in the Trash File Browser window and select Delete Permanently to remove only that object (file), or you can select Restore to move the file back to its original location. You can drag and drop files to and from the trash just as you can with any other folder.

Updating Software
On systems connected to the Internet, Fedora/RHEL is initially set up to search for daily and notify you when software updates are available. GNOME displays the message Important software updates are available in a popup notification window and places a notification indicator (an envelope icon) toward the right end of the Top panel when updates are available. Clicking this object opens the

www.it-ebooks.info

110 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-15

The Software Update window

Software Update window (Figure 4-15). You can open this window manually by selecting Main menu: Applications System Tools Software Update (Fedora) or Main menu: System Administration Software Update (RHEL) or by giving the command gpk-update-viewer from a terminal emulator or Run Application window (ALT-F2).
Software Update window

When the Software Update window opens, it displays the message Checking for updates; after it finishes checking it displays the number of available updates. If no updates are available, the window displays the message All software is up to date. If updates are available, click Install Updates. As it downloads and installs the software packages, the Software Update window displays messages and a progress bar. When it is finished, the Software Update window closes. If the updates require you to reboot the system or log out, the Software Update window prompts you to do so. For more information refer to “Updating, Installing, and Removing Software Packages” on page 122.

Session Management
A session starts when you log in and ends when you log out or reset the session. With fully GNOME-compliant applications, GNOME can manage sessions so the desktop looks the same when you log in as it did when you logged out: The same windows will be positioned as they were on the same workspaces, and programs will be as you left them. The Startup Applications Preferences window allows you to select which applications you want to run each time you log in. It also allows you to save automatically those applications that were running and those windows that were open when you log out;

www.it-ebooks.info

A Tour of the Fedora/RHEL Desktop

111

they will start running when you log on again. To open the Startup Applications Preferences window give the command gnome-session-properties from a terminal emulator or Run Application window (ALT-F2). You must give this command while logged in as yourself (not while working with root privileges). To save your sessions automatically when you log out, click the Options tab in the Startup Applications Preferences window and put a tick in the check box labeled Automatically remember running applications when logging out.

Getting Help
Fedora/RHEL provides help in many forms. Selecting Main menu: Applications Accessories Help displays the Desktop Help browser, which provides information on the desktop. To display other information, click a topic in this window. You can also enter text to search for in the text box at the top of the window and then press RETURN. In addition, most windows provide a Help object or menu. See “Where to Find Documentation” on page 125 for more resources.

Feel Free to Experiment
Try selecting different items from the Main menu and see what you discover. Following are some applications you might want to explore: • The gedit text editor is a simple text editor. Select Main menu: Applications Accessories gedit Text Editor to start it. • LibreOffice’s Writer is a full-featured word processor that can import and export Microsoft Word documents. Select Main menu: Applications Office LibreOffice Writer. If this application is not available on the system, install the libreoffice-writer software package as explained on page 534. The Office menu also offers a drawing program, presentation manager, and spreadsheet. • Firefox is a powerful, full-featured Web browser. Select Main menu: Applications Internet Firefox. • Empathy is a graphical IM (instant messaging) client that allows you to chat on the Internet with people who are using IM clients such as AOL, MSN, and Yahoo! To start Empathy, select Main menu: Applications Internet Empathy Internet Messaging. The first time you start Empathy, it opens a window that says Welcome to Empathy. Follow the instructions to access an existing IM account or open a new one. Visit live.gnome.org/Empathy for more information.

Logging Out
Log off by clicking your name or Live System User at the upper-right corner of the screen, click Log Out from the menu GNOME displays, and then click the button labeled Log Out from the small window GNOME displays. Select Shut down to

www.it-ebooks.info

112 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

shut down or restart the system, among other options. From a textual environment, press CONTROL-D or give the command exit in response to the shell prompt.

Getting the Most Out of the Desktop
The GNOME desktop is a powerful tool with many features. This section covers many aspects of its panels, the Main menu, windows, terminal emulation, and ways to update, install, and remove software. Chapter 8 continues where this chapter leaves off, discussing the X Window System, covering Nautilus in more detail, and describing a few of the GNOME utilities.

GNOME Desktop Terminology
The following terminology, which is taken from the GNOME Users Guide, establishes a foundation for discussing the GNOME desktop. Figure 4-5 on page 96 shows the initial Fedora GNOME desktop.
Desktop

The desktop comprises all aspects of the GNOME GUI. While you are working with GNOME, you are working on the desktop. There is always exactly one desktop. Panels are bars that appear on the desktop and hold (panel) objects. There are two panels: one along the top of the screen (the Top Edge panel, or just Top panel) and one along the bottom (the Bottom Edge panel, or just Bottom panel). See the next page for more information on panels. Panel objects appear as words or icons on panels. You can click these objects to display menus, run applets, or launch programs. The four types of panel objects are applets, launchers, buttons, and menus. See the next page for more information on panel objects. A graphical application typically displays a window and runs within that window. At the top of most windows is a titlebar you can use to move, resize, and close the window. The root window is the unoccupied area of the workspace and is frequently obscured. The desktop can have no windows, one window, or many windows. Although most windows have decorations (page 118), some, such as the Logout window, do not. Workspaces divide the desktop into one or more areas, with one such area filling the screen at any given time. Initially there are two workspaces. Because panels and objects on the desktop are features of the desktop, all workspaces display the same panels and objects. By default, a window appears in a single workspace. The Switcher (page 98) enables you to display any one of several workspaces. Tooltips (Figure 4-5, page 96) is a minicontext help system that you activate by moving the mouse pointer over a button, icon, window border, or applet (such as those on a panel) and allowing it to hover there. When the mouse pointer hovers over an object, GNOME displays a brief explanation of the object called a tooltip.

Panels

Panel objects

Windows

Workspaces

Tooltips

www.it-ebooks.info

Getting the Most Out of the Desktop 113

Figure 4-16

Window List applets

Opening Files
By default, you double-click an object to open it; alternatively, you can right-click the object and select Open from the drop-down list. When you open a file, GNOME figures out the appropriate tool to use by determining the file’s MIME (page 1176) type. GNOME associates each filename extension with a MIME type and each MIME type with a program. Initially GNOME uses the filename extension to try to determine a file’s MIME type. If it does not recognize the filename extension, it examines the file’s magic number (page 1174). For example, when you open a file with a filename extension of ps, GNOME calls the Evince document viewer, which displays the PostScript file in a readable format. When you open a text file, GNOME uses gedit to display and allow you to edit the file. When you open a directory, GNOME displays its contents in a File Browser window. When you open an executable file such as Firefox, GNOME runs the executable. When GNOME uses the wrong tool to open a file, the tool generally issues an error message. See “Open With” on page 121 for information on how to use a tool other than the default tool to open a file.

Panels
As explained earlier, panels are the bars that initially appear at the top and bottom of the desktop. They are part of the desktop, so they remain consistent across workspaces.

Panel Objects
The icons and words on a panel, called panel objects, display menus, launch programs, and present information. The panel object with the speaker icon, for example, displays the volume control. The envelope icon tells you a notification is waiting for you, and you can click your name on the Top panel to log off or shut down the system. You can start almost any utility or program on the system using a panel object. This section describes the various types of panel objects.
Applets

An applet is a small program that displays its user interface on or adjacent to the panel. You interact with the applet using its Applet panel object. The Clock (date and time) and Workspace Switcher (both shown in Figure 4-5 on page 96) are applets. Although not a distinct type of object, the Window List applet is a unique and important tool. One Window List applet (Figure 4-16) appears on the Bottom panel for each open or iconified window on the displayed workspace. Left-clicking this object minimizes its window or restores the window if it is minimized. Right-click this object

Window List applet

www.it-ebooks.info

114 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Buttons

Titlebar

Menubar

Toolbar

Vertical scrollbar

Window contents

Figure 4-17 A typical window

to display the Window Operations menu (page 115). If a window is buried under other windows, click its Window List applet to make it visible.
Launchers

When you open a launcher, it can execute a command, start an application, display the contents of a folder or file, open a URI in a Web browser, and so on. In addition to appearing on panels, launchers can appear on the desktop. Under Main menu: Applications, you can find launchers that start other applications. Under Main menu: Places, the Home Folder, Documents, Desktop, and Computer objects are launchers that open File Browser windows that display folders. A button performs a single, simple action. The Sound button (Figure 4-5, page 96) displays a volume control. A menu displays a list of selections you can choose from. Some of the selections can be submenus with more selections. All other selections are launchers. The next section discusses the Main menu.

Buttons

Menus

The Main Menu
The Main menu appears at the left end of the Top panel and includes Applications and Places. Click one of these words to display the corresponding menu.
Applications

The Applications menu holds several submenus, each named for a category of applications (e.g., Games, Graphics, Internet, Office—the list varies depending on the software installed on the system). Selections from the submenus launch applications—peruse these selections, hovering over those you are unsure of to display the associated tooltips. The Places menu holds a variety of launchers, most of which open a File Browser window. The Home Folder, Desktop, and Documents objects display your directories with corresponding names. The Computer, CD/DVD Creator, and Network objects

Places

www.it-ebooks.info

Getting the Most Out of the Desktop 115

Minimize Window title Maximize Close

Figure 4-18

A window titlebar

display special locations. Each of these locations enables you to access file manager functions. For example, the CD/DVD Creator selection enables you to burn a CD or DVD. The Connect to Server selection opens a window that allows you to connect to various types of servers, including SSH and FTP servers (see “File” on page 270). The Search for Files selection enables you to search for files (page 274).

Windows
In a workspace, a window is a region that runs, or is controlled by, a particular program (Figure 4-17). Because you can control the look and feel of windows— even the buttons they display—your windows might not look like the ones shown in this book. Each window in a workspace has a Window List applet (page 113) on the Bottom panel. Titlebar A titlebar (Figures 4-17 and 4-18) appears at the top of most windows and controls the window it is attached to. You can change the appearance and function of a titlebar, but it will usually have at least the functionality of the buttons shown in Figure 4-18. You can use gnome-tweak-tool (page 94) to change titlebars. The minimize (iconify) button collapses the window so the only indication of its presence is its Window List applet on the Bottom panel; click this applet to restore the window. Click the maximize button to expand the window so that it occupies the whole workspace; click the same button to restore the window to its former size. You can also double-click the titlebar to maximize and restore a window. Clicking the close button closes the window and usually terminates the program running in it. To reposition the window, left-click the titlebar and drag the window to the desired location. Window Operations The Window Operations menu contains operations you can perform on any winmenu dow. Right-click either the titlebar or the Window List applet (page 113) to display this menu. You can use this menu to move a window to another workspace, keep the window on top of or below other windows, and cause the window to always be visible on the displayed workspace.
Toolbar

A toolbar (Figure 4-17) usually appears near the top of a window and contains icons, text, applets, menus, and more. Many kinds of toolbars exist. The titlebar is not a toolbar; rather, it is part of the window decorations placed there by the window manager (page 118).

www.it-ebooks.info

116 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Changing the Input Focus (Window Cycling)
The window with the input focus is the one that receives keyboard characters and commands you type. In addition to using the Window List applet (page 113), you can change which window on the displayed workspace has the input focus by using the keyboard; this process is called window cycling. When you press ALT-TAB, GNOME displays in the center of the workspace a box that holds icons representing the programs running in the windows in the workspace. It also shifts the input focus to the window that was active just before the currently active window, making it easy to switch back and forth between two windows. When you hold ALT and press TAB multiple times, the focus moves from window to window. Holding ALT and SHIFT and repeatedly pressing TAB cycles in the other direction.

Cutting and Pasting Objects Using the Clipboard
There are two similar ways to cut/copy and paste objects and text on the desktop and both within and between windows. In the first method, you use the clipboard, technically called the copy buffer, to copy or move objects or text. To do so, you explicitly copy an object or text to the buffer and then paste it somewhere else. Applications that follow the user interface guidelines use CONTROL-X to cut, CONTROL-C to copy, and CONTROL-V to paste. Application context menus frequently provide these same options. You might not be familiar with the second method to copy and paste text—using the selection or primary buffer, which always contains the text you most recently selected (highlighted). You cannot use this method to copy objects. Clicking the middle mouse button (click the scroll wheel on a mouse that has one) pastes the contents of the selection buffer at the location of the mouse pointer. If you are using a two-button mouse, click both buttons at the same time to simulate clicking the middle button. With both these techniques, start by highlighting an object or text to select it. You can drag a box around multiple objects to select them or drag the mouse pointer over text to select it. Double-click to select a word or triple-click to select a line or a paragraph. Next, to use the clipboard, explicitly copy (CONTROL-C) or cut (CONTROL-X) the objects or text. If you want to use the selection buffer, skip this step. To paste the selected objects or text, position the mouse pointer where you want to put it and then either press CONTROL-V (clipboard method) or press the middle mouse button (selection buffer method).

Use SHIFT-CONTROL-C and SHIFT-CONTROL-V within a terminal emulator tip The CONTROL-C, CONTROL-X, and CONTROL-V characters do not work in a terminal emulator window because the shell running in the window intercepts them before the terminal emulator can receive them. However, you can use SHIFT-CONTROL-C and SHIFT-CONTROL-V in place of CONTROL-C and CONTROL-V, respectively. There is no keyboard shortcut for CONTROL-X. You can also use the selection buffer in this environment or use copy/paste from the Edit selection on the menubar or from the context menu (right-click).

www.it-ebooks.info

Getting the Most Out of the Desktop 117

Figure 4-19

A Terminal (emulator) window

When using the clipboard, you can give as many commands as you like between the CONTROL-C or CONTROL-X and CONTROL-V, as long as you do not press CONTROL-C or CONTROL-X again. When using the selection buffer, you can give other commands after selecting text and before pasting it, as long as you do not select (highlight) other text.

Using the Root Window
The root window is any part of a workspace that is not occupied by a window, panel, or object. It is the part of the workspace where you can see the background.
Desktop menu

Right-click the root window to display the Desktop menu which enables you to create a folder, launcher, or document. The Change Desktop Background selection opens the Background window (page 100). You must set up the file manager to handle the desktop (page 94) before GNOME will display the Desktop menu.

Running Commands from a Terminal Emulator/Shell
A terminal emulator is a window that presents a command-line interface (CLI); it functions as a textual (character-based) terminal and is displayed in a graphical environment. To display the GNOME terminal emulator named Terminal (Figure 4-19), select Main menu: Applications System Tools Terminal or enter the command gnometerminal from a Run Application window (ALT-F2). Because you are already logged in and are creating a subshell in a desktop environment, you do not need to log in again. Once you have opened a terminal emulator window, try giving the command man man to read about the man utility (page 126), which displays Linux manual pages. Chapter 5 describes utilities you can run from a terminal emulator.

www.it-ebooks.info

118 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

You can run character-based programs that would normally run on a terminal or from the console in a terminal emulator window. You can also start graphical programs, such as gnome-calculator, from this window. A graphical program opens its own window. When you are typing in a terminal emulator window, several characters, including *, ?, |, [, and ], have special meanings. Avoid using these characters until you have read “Special Characters” on page 146.
The shell

Once you open a terminal emulator window, you are communicating with the command interpreter called the shell. The shell plays an important part in much of your communication with Linux. When you enter a command at the keyboard in response to the shell prompt on the screen, the shell interprets the command and initiates the appropriate action—for example, executing a program; calling a compiler, a Linux utility, or another standard program; or displaying an error message indicating you entered a command incorrectly. When you are working on a GUI, you bypass the shell and execute a program by clicking an object or a name. Refer to Chapter 7 for more information on the shell

The Window Manager
A window manager—the program that controls the look and feel of the basic GUI—runs under a desktop manager (such as GNOME or KDE) and controls all aspects of the windows in the X Window System environment. The window manager defines the appearance of the windows on the desktop and controls how you operate and position them: open, close, move, resize, minimize, and so on. It might also handle some session management functions, such as how a session is paused, resumed, restarted, or ended (page 110).
Window managers

Mutter, Metacity, and Compiz—the default window managers for GNOME— provide window management and start many components through GNOME panel objects. They also communicate with and facilitate access to other components in the environment. Using the standard X libraries, programmers have created other window managers, including blackbox, fluxbox, and WindowMaker. You can use yum (page 534) to install any of these packages.

Window decorations

A window manager controls window decorations—that is, the titlebar and border of a window. Aside from the aesthetic aspects of changing window decorations, you can alter their functionality by modifying the number and placement of buttons on the titlebar. You can use gnome-tweak-tool (Fedora; page 94) to alter the look and functionality of window decorations. The window manager takes care of window manipulation so client programs do not need to do so. This setup is very different from that of many other operating systems, and the way that GNOME deals with window managers is different from how other desktop environments work. Window managers do more than simply

www.it-ebooks.info

Getting the Most Out of the Desktop 119

Figure 4-20

The Object context menus for a spreadsheet (left) and a text file (right)

manage windows—they provide a useful, good-looking, graphical shell where you can work. Their open design allows users to define their own policies, down to the fine details. Theoretically GNOME is not dependent on any particular window manager and can work with any of several window managers. Because of their flexibility, you would not see major parts of the desktop environment change if you were to switch from one window manager to another. A desktop manager collaborates with the window manager to make your work environment intuitive and easy to use. Although the desktop manager does not control window placement, it does get information from the window manager about window placement.

The Object Context Menu
When you right-click an object or group of objects either on the desktop or in a File Browser window, GNOME displays an Object context menu. Different types of objects display different context menus, but most context menus share common selections. Figure 4-20 shows context menus for an LibreOffice spreadsheet file and for a plain text file. Table 4-1 on the next page lists some common Object context menu selections.

www.it-ebooks.info

120 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Table 4-1 Object context menu selections
Open Open in New Window Open with “App” Runs an executable file. Opens a file with an appropriate application. Opens a folder in a File Browser window. Same as double-clicking the object. (From a File Browser window only.) Opens a folder in a new File Browser window instead of replacing the contents of the current window. Same as holding down SHIFT while double-clicking a folder in a Browser window. Opens the file using the application named App. When this selection appears as the first selection in the menu, App is the default application that GNOME uses to open this type of file. See page 121 for information on changing this default. A triangle appearing to the right of a selection indicates the selection is a menu. Allow the mouse pointer to hover over the selection to display the submenu. Each submenu selection is an Open with “App” selection (above). The last selection in the submenu is Open with Other Application (below). See Figure 4-22 on page 123 for an example. Displays a menu that allows you to select an application to open this type of file. See page 121 for information on changing the default application GNOME uses to open this type of file. Removes the object and places it on the clipboard (page 116). Copies the object to the clipboard (page 116). Extracts the contents of an archive and some other types of files, such as some documents, to a directory with the same name as the original file plus _FILES. If you do not have permission to write to the working directory (page 190), this menu selection appears as Extract To. Extracts the contents of an archive and some other types of files, such as some documents, to a directory you select using the Browse/Save window (page 107). This selection appears only if you do not have permission to write to the working directory (page 190). Otherwise, this menu selection appears as Extract Here. Creates a symbolic link to the object in the same directory as the object. You can then move the link to a different directory where it might be more useful. For more information refer to “Symbolic Links” on page 216. Moves the object to the trash (page 109). Opens a Send To window that allows you to send the object using various techniques including email. Opens the Compress window, which allows you to specify a format and a name for an archive containing one or more objects (page 270). Displays the Object Properties window (next).

Open with ➤

Open with Other Application Cut Copy Extract Here

Extract To

Make Link

Move to Trash Send to Compress Properties

www.it-ebooks.info

Getting the Most Out of the Desktop 121

The Object Properties Window
The Object Properties window displays information about a file, such as its owner, permissions, size, location, MIME type, ways to work with it, and so on. This window is titled filename Properties, where filename is the name of the file you clicked to open the window. To display this window, right-click an object and select Properties from the drop-down list. The Properties window initially displays some basic information. Click the tabs at the top of the window to display additional information. Different sets of tabs appear for different types of files. You can modify the settings in this window only if you have permission to do so. This section describes the three tabs most commonly found in Object Properties windows.
Basic

The Basic tab displays information about the file, including its MIME (page 1176) type, and enables you to select a custom icon for the file and change its name. To change the name of the file, replace the name in the text box labeled Name. If the filename is not listed in a text box, you do not have permission to change it. An easy way to change the icon is to open a File Browser window at /usr/share/icons. Work your way down through the directories until you find an icon you like, and then drag and drop it on the icon to the left of Name in the Basic tab of the Object Properties window. The Permissions tab (Figure 4-21 on the next page) allows the owner of a file to change the file’s permissions (page 202) and to change the group (see /etc/group on page 506) the file is associated with to any group the owner is associated with. When running with root privileges, you can also change the owner of the file. The command su –RETURN followed by the command nautilus, when given from a terminal emulator window, opens a File Browser window running with root privileges (but read the caution on page 90). Nautilus grays out items you are not allowed to change. Using the drop-down lists, you can give the owner (called user elsewhere; see the tip about chmod on page 204), group, and others read or read and write permission for a file. Alternatively, you can prohibit the group and others from accessing the file by specifying permissions as None. Put a tick in the check box labeled Execute to give all users permission to execute the file. This tab does not give you as fine-grained control over assigning permissions as chmod (page 203) does. Permissions for a directory work as explained on page 207. Owner, group, and others can be allowed to list files in a directory, access (read and—with the proper permissions—execute) files, or create and delete files. Group and others permissions can also be set to None. A tick in the check box labeled Execute allows the directory to be searched. Click Apply Permissions to Enclosed Files to apply the permissions in the Permissions tab to all files in the directory.

Permissions

Open With

When you ask GNOME to open a file that is not executable (by double-clicking its icon or right-clicking and selecting the first Open with selection), GNOME determines which application or utility it will use to open the file. GNOME uses several techniques to determine the MIME (page 1176) type of a file and selects the default application based on that determination.

www.it-ebooks.info

122 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-21

The Object Properties window, Permissions tab

The Open With tab (Figure 4-22) enables you to change which applications GNOME can use to open the file and other files of the same MIME type (typically files with the same filename extension). Click the Add button to add to the list of applications. Highlight an application and click Remove to remove an application from the list. You cannot remove the default application. When you add an application, GNOME adds that application to the Open With list but does not change the default application it uses to open that type of file. Highlight the application and click Set as default to cause that application to become the default application GNOME uses to open this type of file. When a file has fewer than four applications in the Open With tab, the Object context menu displays all applications in that menu. With four or more applications, the Object context menu provides an Open With submenu (Figure 4-22).

Updating, Installing, and Removing Software Packages
Fedora/RHEL software comes in packages that include all necessary files, instructions so a program can automatically install and remove the software, and a list of

www.it-ebooks.info

Updating, Installing, and Removing Software Packages

123

Figure 4-22

The Object Properties window, Open With tab, and the Object context menu, Open With submenu for the same file

other packages the package depends on. There are many ways to search for and install software packages. The notification indicator (an envelope icon on the Top panel; page 109) prompts you each time updates are available for software on the system. The Add/Remove Software window (discussed on the next page) is an easy way to install popular software. Chapter 13 explains how to work with software packages from the command line.

Updates
The the Software Update Preferences window (Figure 4-23) allows you to choose how often you want the system to check for updates and which updates you want the system to automatically install (it prompts you to install other updates). Open this window by selecting Main menu: Applications Other Software Updates (Fedora) or Main menu: System Preferences Software Updates (RHEL).

Figure 4-23

The Software Update Preferences window

www.it-ebooks.info

124 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-24

The Add/Remove Software window

Add/Remove Software
The Add/Remove Software window (Figure 4-24) adds and removes applications from the system. Open this window by selecting Main menu: Applications System Tools Add/Remove Software (Fedora), Main menu: System Administration Add/Remove Software (RHEL), or by giving the command gpk-application from a terminal emulator or Run Application window (ALT-F2). Maximizing this window might make it easier to use. Under RHEL it might be easier to use RHN (page 554) to add and remove software. The text box at the upper-left corner of the Add/Remove Software window (adjacent to the grayed-out button labeled Find) is the key to finding the package you want to add or remove. Initially, the icon at the left of this text box is a pencil and paper, indicating you will search for software packages by description. Click this icon to select other types of searches from a drop-down list. Enter the name or part of the name of an application in the text box at the upper-left corner of the window and click Find to search for an application. The Add/Remove Software window displays a list of matching software packages in the frame on the right side of the window. Alternatively, you can select one of the entries from the list on the left side of the window to display a list of packages. An icon and text at the lower-left corner of the window keeps you informed of the utility’s progress. Scroll through the packages displayed in the frame on the right side of the window. When you click/highlight an application, the window displays a summary of the application in the frame at the lower-right corner of the window. Put a tick in the check box next to each application you want to install. Remove ticks from any applications you want to remove. Click Apply to implement the changes you have marked. If a package you want to install depends on other packages that are not installed, the utility will ask for permission to install the dependent packages. Because you need to work with root

www.it-ebooks.info

Where to Find Documentation 125

Figure 4-25

The Desktop Help window

privileges to install and remove software, the utility might ask for the root password. When it is finished, the utility might ask if you want to run the new application. Close the Add/Remove Software window when you are finished. Packages you installed might be available on the Main menu.

Where to Find Documentation
Distributions of Linux, including Fedora/RHEL, typically do not come with hardcopy reference manuals. However, its online documentation has always been one of Linux’s strengths. The man (or manual) and info pages have been available via the man and info utilities since early releases of the operating system. The GNOME desktop provides a graphical Desktop Help browser. Not surprisingly, with the ongoing growth of Linux and the Internet, the sources of documentation have expanded as well. This section discusses some of the places you can look for information on Linux in general and on Fedora/RHEL in particular. See also Appendix B.

GNOME Desktop Help Window
To display the GNOME Desktop Help window (Figure 4-25), select Main menu: Applications Accessories Help (Fedora) or Main menu: System Help (REHL).

www.it-ebooks.info

126 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-26

The man utility displaying information about itself

Click topics in this window until you find the information you are looking for. You can also search for a topic using the text box at the top of the window.

man: Displays the System Manual
The textual man utility displays (man) pages from the system documentation. This documentation is helpful when you know which utility you want to use but have forgotten exactly how to use it. You can also refer to the man pages to get more information about specific topics or to determine which features are available with Linux. Because the descriptions in the system documentation are often terse, they are most helpful if you already understand the basic functions of a utility. Because man is a character-based utility, you need to open a terminal emulator window (page 117) to run it. You can also log in on a virtual terminal (page 138) and run man from there. To find out more about a utility, give the command man, followed by the name of the utility. Figure 4-26 shows man displaying information about itself; the user entered a man man command. less (pager)

The man utility sends its output through a pager—usually less (page 149), which displays one screen of information at a time. When you display a manual page using man, less displays a prompt [e.g., Manual page man(1) line 1] at the bottom of the screen after it displays each screen of text and waits for you to request another screen of text by pressing the SPACE bar. You can also use the PAGE UP, PAGE DOWN, UP ARROW, and DOWN ARROW keys to navigate the text. Pressing h (help) displays a list of less commands. Pressing q (quit) stops less and causes the shell to display a prompt. You can search for topics covered by man pages using the apropos utility (next).

www.it-ebooks.info

Where to Find Documentation 127
Manual sections

Based on the FHS (Filesystem Hierarchy Standard; page 199), the Linux system manual and the man pages are divided into ten sections, where each section describes related tools: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. User Commands System Calls Subroutines Devices File Formats Games Miscellaneous System Administration Kernel New

This layout closely mimics the way the set of UNIX manuals has always been divided. Unless you specify a manual section, man displays the earliest occurrence in the manual of the word you specify on the command line. Most users find the information they need in sections 1, 6, and 7; programmers and system administrators frequently need to consult the other sections. In some cases the manual contains entries for different tools with the same name. For example, the following command displays the man page for the passwd utility from section 1 of the system manual:
$ man passwd

To see the man page for the passwd file from section 5, enter this command:
$ man 5 passwd

The preceding command instructs man to look only in section 5 for the man page. In documentation you might see this man page referred to as passwd(5). Use the –a option (see the adjacent tip) to view all man pages for a given subject (press qRETURN to display each subsequent man page). For example, give the command man –a passwd to view all man pages for passwd.

Options tip An option modifies the way a utility or command works. Options are usually specified as one or more letters that are preceded by one or two hyphens. An option typically appears following the name of the utility you are calling and a SPACE. Other arguments (page 1151) to the command follow the option and a SPACE. For more information refer to “Options” on page 227.

apropos: Searches for a Keyword
When you do not know the name of the command required to carry out a particular task, you can use apropos with a keyword to search for it. This utility searches for the keyword in the short description line of all man pages and displays those that contain

www.it-ebooks.info

128 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

a match. The man utility, when called with the –k (keyword) option, provides the same output as apropos. The database apropos uses, named mandb (Fedora) and makewhatis (RHEL), is not available on Fedora/RHEL systems when they are first installed, but is built automatically by crond (page 611). If apropos does not produce any output, see “Initializing Databases and Updating the System” on page 67. The following example shows the output of apropos when you call it with the who keyword. The output includes the name of each command, the section of the manual that contains it, and the short description from the man page. This list includes the utility you need (who) and identifies other, related tools you might find useful:
$ apropos who at.allow (5) jwhois (1) w (1) who (1) who (1p) whoami (1) whois (1) whois.jwhois (1) whatis determine who can submit jobs via at or batch client for the whois service Show who is logged on and what they are doing. show who is logged on display who is on the system print effective userid client for the whois service client for the whois service

The whatis utility is similar to apropos but finds only complete word matches for the name of the utility:
$ whatis who who (1p) who (1) - display who is on the system - show who is logged on

info: Displays Information About Utilities
The textual info utility (www.gnu.org/software/texinfo/manual/info) is a menu-based hypertext system developed by the GNU project (page 3) and distributed with Fedora/RHEL. It includes a tutorial on itself (give the command info info) and documentation on many Linux shells, utilities, and programs developed by the GNU project. Figure 4-27 shows the screen that info displays when you give the command info coreutils (the coreutils software package holds the Linux core utilities).

man and info display different information tip The info utility displays more complete and up-to-date information on GNU utilities than does man.
When a man page displays abbreviated information on a utility that is covered by info, the man page refers to info. The man utility frequently displays the only information available on non-GNU utilities. When info displays information on non-GNU utilities, it is frequently a copy of the man page.

Because the information on this screen is drawn from an editable file, your display might differ from the screens shown in this section. You can press any of the following keys while the initial info screen is displayed:

www.it-ebooks.info

Where to Find Documentation 129

Figure 4-27

The initial screen displayed by the command info coreutils

• h or ? to list info commands •
SPACE

to scroll through the display

• m followed by the name of the menu you want to display or a SPACE to display a list of menus • q or CONTROL-C to quit The notation info uses to describe keyboard keys might not be familiar to you. The notation C-h is the same as CONTROL-H. Similarly, M-x means hold down the META or ALT key and press x. (On some systems you need to press ESCAPE and then x to duplicate the function of META-X.) After giving the command info coreutils, press the SPACE bar a few times to scroll through the display. Type /sleepRETURN to search for the string sleep. When you type /, the cursor moves to the bottom line of the window and displays Regexp search [string]:, where string is the last string you searched for. Press RETURN to search for string or enter the string you want to search for. Typing sleep displays sleep on that line, and pressing RETURN displays the next occurrence of sleep.

You might find pinfo easier to use than info tip The pinfo utility is similar to info but is more intuitive if you are not familiar with the emacs editor. This utility runs in a textual environment, as does info. When it is available, pinfo uses color to make its interface easier to use. If pinfo is not installed on the system, use the Add/Remove Software window (page 124) to install the pinfo package. Run pinfo from a terminal emulator or Run Application window (ALT-F2) and select Run in terminal.

www.it-ebooks.info

130 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-28

The screen displayed by the command info coreutils after you type /sleepRETURN twice

Now type /RETURN (or /sleepRETURN) to search for the next occurrence of sleep as shown in Figure 4-28. The asterisk at the left end of the line indicates that this entry is a menu item. Following the asterisk is the name of the menu item and a description of the item. Each menu item is a link to the info page that describes the item. To jump to that page, search for or use the ARROW keys to move the cursor to the line containing the menu item and press RETURN. With the cursor positioned as it is in Figure 4-28, press RETURN to display information on sleep. Alternatively, you can type the name of the menu item in a menu command to view the information: To display information on sleep, for example, you can give the command m sleep, followed by RETURN. When you type m (for menu), the cursor moves to the bottom line of the window (as it did when you typed /) and displays Menu item:. Typing sleep displays sleep on that line, and pressing RETURN displays information about the menu item you have specified. Figure 4-29 shows the top node of information on sleep. A node groups a set of information you can scroll through by pressing the SPACE bar. To display the next node, press n. Press p to display the previous node. As you read through this book and learn about new utilities, you can use man or info to find out more about those utilities. If you can print PostScript documents, you can print a manual page by using the man utility with the –t option. For example, man –t cat | lpr prints information about the cat utility. You can also use a Web browser to display the documentation at docs.fedoraproject.org, www.redhat.com/docs, or www.tldp.org, and then print the desired information from the browser.

www.it-ebooks.info

Where to Find Documentation 131

Figure 4-29

The info page on the sleep utility

The ––help Option
Another tool you can use in a textual environment is the ––help option. Most GNU utilities provide a ––help option that displays information about the utility. NonGNU utilities might use a –h or –help option to display help information.
$ cat --help Usage: cat [OPTION] [FILE]... Concatenate FILE(s), or standard input, to standard output. -A, --show-all -b, --number-nonblank -e -E, --show-ends ... equivalent to -vET number nonempty output lines, overrides -n equivalent to -vE display $ at end of each line

If the information that ––help displays runs off the screen, send the output through the less pager (page 126) using a pipe (page 156):
$ ls --help | less

HOWTOs: Finding Out How Things Work
A HOWTO document explains in detail how to do something related to Linux—from setting up a specialized piece of hardware to performing a system administration task to setting up specific networking software. Mini-HOWTOs offer shorter explanations. As with Linux software, one person or a few people generally are responsible for writing and maintaining a HOWTO document, but many people might contribute to it.

www.it-ebooks.info

132 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-30

Google reporting on an error message

The Linux Documentation Project (LDP; page 133) site houses most HOWTO and mini-HOWTO documents. Use a Web browser to visit www.tldp.org, click HOWTOs, and pick the index you want to use to find a HOWTO or mini-HOWTO. You can also use the LDP search feature on its home page to find HOWTOs and other documents.

Getting Help
GNOME provides tooltips (page 112), a context-sensitive Help system, and the Desktop Help window, discussed on page 125.

Finding Help Locally
/usr/share/doc

The /usr/src/linux/Documentation (present only if you install the kernel source code, as explained in Chapter 15) and /usr/share/doc directories often contain more detailed and different information about a utility than either man or info provides. Frequently this information is meant for people who will be compiling and modifying the utility, not just using it. These directories hold thousands of files, each containing information on a separate topic.

Using the Internet to Get Help
The Internet provides many helpful sites related to Linux. Aside from sites that offer various forms of documentation, you can enter an error message from a program you are having a problem with in a search engine such as Google (www.google.com,

www.it-ebooks.info

Where to Find Documentation 133

or its Linux-specific version at www.google.com/linux). Enclose the error message within double quotation marks to improve the quality of the results. The search will likely yield a post concerning your problem and suggestions about how to solve it. See Figure 4-30.
Fedora/Red Hat Web sites

The Red Hat and Fedora Web sites are rich sources of information. The following list identifies locations that may be of interest: • Fedora documentation is available at docs.fedoraproject.org. • Manuals and other documentation for RHEL are available at docs.redhat.com and access.redhat.com. • Various types of support documents and support are available at www.redhat.com/apps/support (requires free registration). • You can query the Red Hat Knowledgebase at kbase.redhat.com (requires free registration). • The home pages for Fedora (fedoraproject.org) and RHEL (www.redhat.com) have a wealth of information. • Fedora/RHEL support forums are online discussions about any Red Hat–related issues that people want to raise. One forum is dedicated to new users; others to Apache, the X Window System, and so on. Visit www.redhat.com/mailman/listinfo to browse the lists. Another (nonauthoritative) site that has similar, useful information is fedoraforum.org. • The Fedora/RHEL bugs database is available at bugzilla.redhat.com. Anyone can search the database. To submit new bugs or append to existing bugs, you need to sign up for a free account. • Fedora weekly news is available at fedoraproject.org/wiki/FWN. • RHEL hardware help is available from the Red Hat hardware catalog at hardware.redhat.com. The hardware that Fedora supports is mostly a superset of that supported by RHEL.

GNU

GNU manuals are available at www.gnu.org/manual. In addition, you can visit the GNU home page (www.gnu.org) to obtain other documentation and GNU resources. Many of the GNU pages and resources are available in a variety of languages. The Linux Documentation Project (www.tldp.org; Figure 4-31 on the next page), which has been around for almost as long as Linux, houses a complete collection of guides, HOWTOs, FAQs, man pages, and Linux magazines. The home page is available in English, Portuguese, Spanish, Italian, Korean, and French. It is easy to use and supports local text searches. It also provides a complete set of links you can use to find almost anything you want related to Linux (click Links in the Search box or go to www.tldp.org/links). The links page includes sections on general information, events, getting started, user groups, mailing lists, and newsgroups, with each section containing many subsections.

The Linux Documentation Project

www.it-ebooks.info

134 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Figure 4-31

The Linux Documentation Project home page

More About Logging In
Refer to “Logging In on the System” on page 91 for information about logging in. This section covers options you can choose from the Login screen and solutions to common login problems. It also describes how to log in from a terminal and from a remote system.

Always use a password security Unless you are the only user of a system; the system is not connected to any other systems, the
Internet, or a modem; and you are the only one with physical access to the system, it is poor practice to maintain a user account without a password.

The Login Screen
The Login screen (Figure 4-1, page 91) presents a list of users who are allowed to log in on the system. At the right end of the panel at the top of the screen is a terminal icon. Click this icon to suspend, restart, or shut down the system. Click your name from the list of users to log in. Once you have clicked your name, the login screen displays a text box labeled Password. Enter your password in the text box and click the button labeled Log In or press RETURN to log in.

www.it-ebooks.info

More About Logging In 135
Sessions

After you click your name, the Login screen displays a drop-down list to the left of the button labeled Cancel. Select the desktop manager you want to use for the upcoming and future sessions from this list, enter your password, and then click Log In.

What to Do if You Cannot Log In
If you enter either your username or your password incorrectly, the system displays an error message after you enter both your username and your password. This message indicates you have entered either the username or the password incorrectly or that they are not valid. It does not differentiate between an unacceptable username and an unacceptable password—a strategy meant to discourage unauthorized people from guessing names and passwords to gain access to the system. Following are some common reasons why logins fail: • The username and password are case sensitive. Make sure the CAPS LOCK key is off and enter your username and password exactly as specified or as you set them up. • You are not logging in on the right machine. The login/password combination might not be valid if you are trying to log in on the wrong machine. On a larger, networked system, you might have to specify the machine you want to connect to before you can log in. • Your username is not valid. The login/password combination might not be valid if you have not been set up as a user. If you are the system administrator, refer to “Configuring User and Group Accounts” on page 602. Otherwise, check with the system administrator. • A filesystem is full. When a filesystem critical to the login process is full, it might appear as though you have logged in successfully, but after a moment the Login screen reappears. You must boot the system in rescue (page 449) and delete some files. • The account is disabled. The root account is disabled from a GUI login by default. An administrator might disable other accounts. Often the root account is not allowed to log in over a network. Use su (page 413) if you need to work with root privileges from a remote system. Refer to “Changing Your Password” on page 137 if you want to change your password.

Logging In Remotely: Terminal Emulators, ssh, and Dial-Up Connections
When you are not using a console, terminal, or other device connected directly to the Linux system you are logging in on, you are probably connected to the Linux system using terminal emulation software on another system. Running on the local system, this software connects to the remote Linux system via a network (Ethernet, asynchronous phone line, PPP, or other type) and allows you to log in.

www.it-ebooks.info

136 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Make sure TERM is set correctly tip No matter how you connect, make sure you have the TERM variable set to the type of terminal your emulator is emulating. For more information refer to “Specifying a Terminal” on page 1122.

When you log in via a dial-up line, the connection is straightforward: You instruct the local emulator program to contact the remote Linux system, it dials the phone, and the remote system displays a login prompt. When you log in via a directly connected network, you either use ssh (secure; page 681) or telnet (not secure; page 383) to connect to the remote system. The ssh program has been implemented on many operating systems, not just Linux. Many user interfaces to ssh include a terminal emulator. From an Apple, Windows, or UNIX machine, open the program that runs ssh and give it the name or IP address (refer to “Host Address” on page 376) of the system you want to log in on. For examples and more details on working with a terminal emulator, refer to “Running Commands from a Terminal Emulator/Shell” on page 117. The next section provides more information about logging in from a terminal emulator.

Logging In from a Terminal (Emulator)
Before you log in on a terminal, terminal emulator, or other textual device, the system displays a message called issue (stored in the /etc/issue file) that identifies the version of Fedora/RHEL running on the system. A sample issue message follows:
Fedora release 15 (Lovelock) Kernel 2.6.38.6-27.fc15.i686.PAE on an i686 (tty2)

This message is followed by a prompt to log in. Enter your username and password in response to the system prompts. If you are using a terminal (page 1192) and the screen does not display the login: prompt, check whether the terminal is plugged in and turned on, and then press the RETURN key a few times. If login: still does not appear, try pressing CONTROL-Q (Xoff). If you are using a workstation (page 1197), run ssh (page 681), telnet (page 383), or whatever communications/emulation software you use to log in on the system.

Did you log in last? security When you log in to a textual environment, after you enter your username and password, the system might display information about the last login on this account, showing when it took place and where it originated. You can use this information to determine whether anyone has accessed the account since you last used it. If someone has, perhaps an unauthorized user has learned your password and logged in as you. In the interest of maintaining security, advise the system administrator of any circumstances that make you suspicious—and change your password.

Once the shell prompt (or just prompt) appears, you have successfully logged in; this prompt shows the system is ready for you to give a command. The first shell prompt line might be preceded by a short message called the message of the day, or motd (page 508), which is stored in the /etc/motd file. Fedora/RHEL establishes a

www.it-ebooks.info

More About Logging In 137

prompt of [user@host directory]$, where user is your username, host is the name of the system, and directory is the name of the directory you are working in. A tilde (~) represents your home directory. For information on how to change the prompt, refer to page 310.

Changing Your Password
If someone else assigned you a password, it is a good idea to give yourself a new one. For security reasons, none of the passwords you enter is displayed by any utility.

Protect your password security Do not allow someone to find out your password: Do not put your password in a file that is not encrypted, allow someone to watch you type your password, or give your password to someone you do not know (a system administrator never needs to know your password). You can always write your password down and keep it in a safe, private place.

Choose a password that is difficult to guess security Do not use phone numbers, names of pets or kids, birthdays, words from a dictionary (not even a foreign language), and so forth. Do not use permutations of these items or a l33t-speak variation of a word: Modern dictionary crackers might also try these permutations.

Include nonalphanumeric characters in your password security Automated password cracking tools first try using alphabetic and numeric characters when they try to guess your password. Including at least one character such as @ or # in a password makes it take longer for one of these tools to crack your password.

Differentiate between important and less important passwords security It is a good idea to differentiate between important and less important passwords. For example,
Web site passwords for blogs or download access are not very important; it is acceptable to use the same password for these types of sites. However, your login, mail server, and bank account Web site passwords are critical: Never use these passwords for an unimportant Web site.

To change your password, select Main menu: Applications System Tools System Settings and select System/User Accounts (Fedora) or Main menu: System Preferences About Me and click Change Password (RHEL). From a command line, give the command passwd. Under Fedora, click the text box labeled Password in the User Accounts window. The first item the system asks for is your current (old) password. This password is verified to ensure that an unauthorized user is not trying to alter your password. Then the system requests a new password. To be relatively secure, a password should contain a combination of numbers, uppercase and lowercase letters, and punctuation characters. It should also meet the following criteria:

www.it-ebooks.info

138 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

• Must be at least six characters long (or longer if the system administrator sets it up that way). Seven or eight characters is a good compromise between length and security. • Should not be a word in a dictionary of any language, no matter how seemingly obscure. • Should not be the name of a person, place, pet, or other thing that might be discovered easily. • Should contain at least two letters and one digit or punctuation character. • Should not be your username, the reverse of your username, or your username shifted by one or more characters. Only the first item is mandatory. Avoid using control characters (such as CONTROL-H) because they might have a special meaning to the system, making it impossible for you to log in. If you are changing your password, the new password should differ from the old one by at least three characters. Changing the case of a character does not make it count as a different character. Refer to “Keeping the System Secure” on page 630 for more information about choosing a password.

pwgen helps you pick a password security The pwgen utility (install the pwgen package) generates a list of almost random passwords. With a little imagination, you can pronounce, and therefore remember, some of these passwords.

After you enter your new password, the system asks you to retype it to ensure you did not make a mistake when you entered it the first time. If the new password is the same both times you enter it, your password is changed. If the passwords differ, you made an error in one of them. In this situation the system displays an error message or does not allow you to click the OK button. If the password you enter is not long enough, the system displays a message similar to The password is too short. When you successfully change your password, you change the way you log in. If you forget your password, a user running with root privileges can change it and tell you the new password.

Using Virtual Consoles
When running Linux on a personal computer, you will frequently work with the display and keyboard attached to the computer. Using this physical console, you can access as many as 63 virtual consoles (also called virtual terminals). Some are set up to allow logins; others act as graphical displays. To switch between virtual consoles, hold the CONTROL and ALT keys down and press the function key that corresponds to the console you want to view. For example, CONTROL-ALT-F5 displays the fifth virtual console. By default, five or six virtual consoles are active and have textual login sessions running. When you want to use both textual and graphical interfaces, you can set up a textual session on one virtual console and a graphical session on another. By default, under Fedora/RHEL a graphical session runs on virtual console number 1.

www.it-ebooks.info

Working from the Command Line 139

Working from the Command Line
Before the introduction of the graphical user interface (GUI), UNIX and then Linux provided only a command-line (textual) interface (CLI). Today, a CLI is available when you log in from a terminal, a terminal emulator, or a textual virtual console, or when you use ssh (secure; page 677) or telnet (not secure; page 383) to log in on a system. This section introduces the Linux CLI. Chapter 5 describes some of the more important utilities you can use from the command line. Most of the examples in Parts IV and V of this book use the CLI, adding examples of graphical tools where available.
Advantages of the CLI

Although the concept might seem antiquated, the CLI has a place in modern computing. In some cases an administrator might use a command-line tool either because a graphical equivalent does not exist or because the graphical tool is not as powerful or flexible as the textual one. Frequently, on a server system, a graphical interface might not even be installed. The first reason for this omission is that a GUI consumes a lot of system resources; on a server, those resources are better dedicated to the main task of the server. Additionally, security considerations mandate that a server system run as few tasks as possible because each additional task can make the system more vulnerable to attack. You can also write scripts using the CLI. Using scripts, you can easily reproduce tasks on multiple systems, enabling you to scale the tasks to larger environments. When you are the administrator of only a single system, using a GUI is often the easiest way to configure the system. When you act as administrator for many systems, all of which need the same configuration installed or updated, a script can make the task go more quickly. Writing a script using command-line tools is frequently easy, whereas the same task can be difficult to impossible using graphical tools.

Pseudographical interface

Before the introduction of GUIs, resourceful programmers created textual interfaces that included graphical elements such as boxes, borders outlining rudimentary windows, highlights, and, more recently, color. These textual interfaces, called pseudographical interfaces, bridge the gap between textual and graphical interfaces.

Correcting Mistakes
This section explains how to correct typographical and other errors you might make while you are logged in on a textual display. Because the shell and most other utilities do not interpret the command line or other text until after you press RETURN, you can readily correct your typing mistakes before you press RETURN. You can correct such mistakes in several ways: erase one character at a time, back up a word at a time, or back up to the beginning of the command line in one step. After you press RETURN, it is too late to correct a mistake: At that point, you must either wait for the command to run to completion or abort execution of the program (next page).

www.it-ebooks.info

140 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

Erasing a Character
While entering characters from the keyboard, you can back up and erase a mistake by pressing the erase key once for each character you want to delete. The erase key backs over as many characters as you wish. It does not, in general, back up past the beginning of the line. The default erase key is BACKSPACE. If this key does not work, try pressing DEL or 1 CONTROL-H. If these keys do not work, give the following stty command to set the erase and line kill (see “Deleting a Line”) keys to their default values:
$ stty ek

Deleting a Word
You can delete a word you entered by pressing CONTROL-W. A word is any sequence of characters that does not contain a SPACE or TAB. When you press CONTROL-W, the cursor moves left to the beginning of the current word (as you are entering a word) or the previous word (when you have just entered a SPACE or TAB), removing the word.

CONTROL-Z suspends a program tip Although it is not a way of correcting a mistake, you might press the suspend key (typically CONTROL-Z) by mistake and wonder what happened. If you see a message containing the word Stopped, you have just stopped your job using job control (page 243). If you give the command fg to continue your job in the foreground, you should return to where you were before you pressed the suspend key. For more information refer to “bg: Sends a Job to the Background” on page 297.

Deleting a Line
Any time before you press RETURN, you can delete the line you are entering by pressing the (line) kill key. When you press this key, the cursor moves to the left, erasing characters as it goes, back to the beginning of the line. The default line kill key is CONTROL-U. If this key does not work, try CONTROL-X. If these keys do not work, give the stty command described under “Erasing a Character.”

Aborting Execution
Sometimes you might want to terminate a running program. For example, you might want to stop a program that is performing a lengthy task such as displaying the contents of a file that is several hundred pages long or copying a large file that is not the one you meant to copy. To terminate a program from a textual display, press the interrupt key (CONTROL-C or sometimes DELETE or DEL). When you press this key, the Linux operating system sends a termination signal to the program you are running and to the shell. Exactly what effect this signal has depends on the program. Some programs stop execution

1. The command stty is an abbreviation for set teletypewriter, the first terminal UNIX ran on. Today stty is commonly thought of as meaning set terminal.

www.it-ebooks.info

Working from the Command Line 141

immediately, some ignore the signal, and some take other actions. When the shell receives a termination signal, it displays a prompt and waits for another command. If these methods do not terminate the program, try sending the program a quit signal (CONTROL-\). If all else fails, try pressing the suspend key (typically CONTROL-Z), giving a jobs command to verify the number of the job running the program, and using kill to abort the job. The job number is the number within the brackets at the left end of the line displayed by jobs ([1]). In the next example, the kill command (page 470) uses –TERM to send a termination signal2 to the job specified by the job number, which is preceded by a percent sign (%1). You can omit –TERM from the command, as kill sends a termination signal by default.
$ bigjob
^Z

[1]+ Stopped $ jobs [1]+ Stopped $ kill -TERM %1 [1]+ Killed

bigjob bigjob bigjob

The kill command returns a prompt; you might need to press RETURN again to see the confirmation message. For more information refer to “Running a Command in the Background” on page 242.

Repeating/Editing Command Lines
To repeat a previous command, press the UP ARROW key. Each time you press this key, the shell displays an earlier command line. To re-execute the displayed command line, press RETURN. Press the DOWN ARROW key to browse through the command lines in the other direction. You can also repeat the previous command using !!. This technique is useful if you forgot to use su (page 413) before a command. In this case, if you type su –c "!!", the shell will run the previous command with root privileges. The command ^old^new^ reruns the previous command, substituting the first occurrence of old with new. Also, on a command line, the shell replaces the characters !$ with the last argument (word) of the previous command. The following example shows the user correcting the filename meno to memo using ^n^m^ and then printing the file named memo by giving the command lpr !$. The shell replaces !$ with memo, the last argument of the previous command.
$ cat meno cat: meno: No such file or directory $ ^n^m^ cat memo This is the memo file. $ lpr !$ lpr memo

2. When the termination signal does not work, use the kill signal (–KILL). A running program cannot ignore a kill signal; it is sure to abort the program (page 470).

www.it-ebooks.info

142 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

The RIGHT and LEFT ARROW keys move the cursor back and forth along the displayed command line. At any point along the command line, you can add characters by typing them. Use the erase key to remove characters from the command line. For information about more complex command-line editing, see page 320.

Chapter Summary
As with many operating systems, your access to a Linux system is authorized when you log in. To do so, you enter your username and password on the Login screen. You can change your password at any time while you are logged in. Choose a password that is difficult to guess and that conforms to the criteria imposed by the utility that changes your password. The system administrator is responsible for maintaining the system. On a single-user system, you are the system administrator. On a small, multiuser system, you or another user might act as the system administrator, or this job might be shared. On a large, multiuser system or a network of systems, there is frequently a full-time system administrator. When extra privileges are required to perform certain system tasks, the system administrator logs in as the root user by entering the username root and the root password; this user is called Superuser or administrator. On a multiuser system, several trusted users might be given the root password. Do not work with root privileges (as Superuser) as a matter of course. When you have to do something that requires root privileges, work with root privileges for only as long as absolutely necessary; revert to working as yourself as soon as possible. Understanding the desktop and its components is essential to getting the most out of the Fedora/RHEL GUI. Its panels offer a convenient way to launch applications, either by clicking objects or by using the Main menu. The Main menu is a multilevel menu you can work with to start many commonly used applications. A window is the graphical manifestation of an application. You can control its size, location, and appearance by clicking buttons on the window’s titlebar. A terminal emulator allows you to use the Linux command-line interface from a graphical environment. You can use a terminal emulator to launch both textual and graphical programs. Panels and menus enable you to select an object (which can be just about anything on the system). On a panel, you generally click an object; on a menu, you typically click text in a list. The GNOME environment provides users with a variety of interests and experience levels—the casual user, the office worker, the power user, and the programmer/system designer—with a space to work in and a set of tools to work with. GNOME also provides off-the-shelf productivity and many ways to customize its look, feel, and response.

www.it-ebooks.info

Exercises

143

Nautilus is GNOME’s simple, yet powerful file manager. It can create, open, display, move, and copy files and directories as well as execute programs and scripts. One of its most basic and important functions is to create and manage the desktop. The man utility provides online documentation for system utilities. This utility is helpful both to new Linux users and to experienced users, who must often delve into system documentation for information on the finer points of a utility’s behavior. The info utility also helps the beginner and the expert alike. It provides a tutorial on its use and documentation on many Linux utilities. The textual or command-line interface (CLI) continues to have a place in modern computing. For example, sometimes a graphical tool does not exist or might not be as powerful or flexible as its textual counterpart. Security concerns on a server system mandate that the system run as few tasks as possible. Because each additional task can make a server more vulnerable to attack, frequently these systems do not have GUIs installed.

Exercises
1. The system displays the following message when you attempt to log in with an incorrect username or an incorrect password:
Login incorrect

a. This message does not indicate whether your username, your password, or both are invalid. Why does it not reveal this information? b. Why does the system wait for a couple of seconds to respond after you supply an incorrect username or password? 2. Give three examples of poor password choices. What is wrong with each? 3. Is fido an acceptable password? Give several reasons why or why not. 4. What is a context menu? How does a context menu differ from other menus? 5. What appears when you right-click the root window? How can you use this object? 6. How would you swap the effects of the right and left buttons on a mouse? What is the drag-and-drop threshold? How would you change it? 7. What are the primary functions of the Main menu? 8. Describe three ways to a. Change the size of a window. b. Delete a window.

www.it-ebooks.info

144 Chapter 4 Introduction to Fedora and Red Hat Enterprise Linux

9. What are the functions of a Window Operations menu? How do you display this menu? 10. What is a panel? Name a few objects on the panels and explain what you can use them for. What do the Workspace Switcher applet and the Window List applet do? 11. What are tooltips? How are they useful?

Advanced Exercises
12. How does the mouse pointer change when you move it to the edge of a window? What happens when you left-click and drag the mouse pointer when it looks like this? Repeat this experiment with the mouse pointer at the corner of a window. 13. Assume you have started a window manager without a desktop manager. What would be missing from the screen? Describe what a window manager does. How does a desktop manager make it easier to work with a GUI? 14. When the characters you type do not appear on the screen, what might be wrong? How can you fix this problem? 15. What happens when you run vi from the Run Application window without specifying that it be run in a terminal? Where does the output go? 16. The example on page 127 shows that the man pages for passwd appear in sections 1 and 5 of the system manual. Explain how you can use man to determine which sections of the system manual contain a manual page with a given name. 17. How many man pages are in the Devices subsection of the system manual? (Hint: Devices is a subsection of Special Files.)

www.it-ebooks.info

5
The Linux Utilities

C 5 hapter5

In This Chapter
Special Characters . . . . . . . . . . . . 146 Basic Utilities . . . . . . . . . . . . . . . . 147 less Is more: Display a Text File One Screen at a Time . . . . . . . . 149 Working with Files. . . . . . . . . . . . . 149 lpr: Prints a File . . . . . . . . . . . . . . . 151 | (Pipe): Communicates Between Processes . . . . . . . . . . . . . . . . . . 156 Compressing and Archiving Files 159 Displaying User and System Information . . . . . . . . . . . . . . . . 166 Tutorial: Using vim to Create and Edit a File . . . . . . . . . . . . . . . . . . 172

Chapter Objectives
After reading this chapter you should be able to: List special characters and methods of preventing interpretation of these characters Use basic utilities to list files and display text files Copy, move, and remove files Search, sort, print, and compare text files String commands together using a pipe Compress, decompress, and archive files Locate utilities on the system Display information about users Communicate with other users
145

www.it-ebooks.info

146 Chapter 5 The Linux Utilities

When Linus Torvalds introduced Linux and for a long time thereafter, Linux did not have a graphical user interface (GUI): It ran on character-based terminals only, using a command-line interface (CLI), also referred to as a textual interface. All the tools ran from a command line. Today the Linux GUI is important, but many people—especially system administrators—run many command-line utilities. Command-line utilities are often faster, more powerful, or more complete than their GUI counterparts. Sometimes there is no GUI counterpart to a textual utility, and some people just prefer the hands-on feeling of the command line. When you work with a command-line interface, you are working with a shell (Chapters 7, 9, and 27). Before you start working with a shell, it is important that you understand something about the characters that are special to the shell, so this chapter starts with a discussion of special characters. The chapter then describes five basic utilities: ls, cat, rm, less, and hostname. It continues by describing several other file manipulation utilities as well as utilities that compress and decompress files, pack and unpack archive files, locate utilities, display system information, communicate with other users, and print files. It concludes with a tutorial on the vim text editor.

Special Characters
Special characters, which have a special meaning to the shell, are discussed in “Filename Generation/Pathname Expansion” on page 244. These characters are mentioned here so you can avoid accidentally using them as regular characters until you understand how the shell interprets them. For example, it is best to avoid using any of the following characters in a filename (even though emacs and some other programs do) because they make the file harder to reference on the command line:
& ; | Whitespace

* ?

'

"



[ ] ( ) $ < > { } # / \ ! ~

Although not considered special characters, RETURN, SPACE, and TAB have special meanings to the shell. RETURN usually ends a command line and initiates execution of a command. The SPACE and TAB characters separate elements on the command line and are collectively known as whitespace or blanks. If you need to use a character that has a special meaning to the shell as a regular character, you can quote (or escape) it. When you quote a special character, you keep the shell from giving it special meaning. The shell treats a quoted special character as a regular character. However, a slash (/) is always a separator in a pathname, even when you quote it. To quote a character, precede it with a backslash (\). When two or more special characters appear together, you must precede each with a backslash (for example, you would enter ** as \*\*). You can quote a backslash just as you would quote any other special character—by preceding it with a backslash (\\). Another way of quoting special characters is to enclose them between single quotation marks: '**'. You can quote many special and regular characters between a pair

Quoting special characters

Backslash

Single quotation marks

www.it-ebooks.info

Basic Utilities 147

of single quotation marks: 'This is a special character: >'. The regular characters are interpreted as usual, and the shell also interprets the special characters as regular characters. The only way to quote the erase character (CONTROL-H), the line kill character (CONTROL-U), and other control characters (try CONTROL-M) is by preceding each with a CONTROL-V. Single quotation marks and backslashes do not work. Try the following:
$ echo 'xxxxxxCONTROL-U' $ echo xxxxxxCONTROL-V CONTROL-U

optional Although you cannot see the CONTROL-U displayed by the second of the preceding pair of commands, it is there. The following command sends the output of echo (page 157) through a pipe (page 156) to od (octal display, see the od man page) to display CONTROL-U as octal 25 (025):
$ echo xxxxxxCONTROL-V CONTROL-U | od -c 0000000 x x x x x x 025 0000010 \n

The \n is the NEWLINE character that echo sends at the end of its output.

Basic Utilities
One of the important advantages of Linux is that it comes with thousands of utilities that perform myriad functions. You will use utilities whenever you work with Linux, whether you use them directly by name from the command line or indirectly from a menu or icon. The following sections discuss some of the most basic and important utilities; these utilities are available from a CLI. Some of the more important utilities are also available from a GUI; others are available only from a GUI.

Run these utilities from a command line tip This chapter describes command-line, or textual, utilities. You can experiment with these utilities from a terminal, a terminal emulator within a GUI (page 117), or a virtual console (page 138).
Folder/directory

The term directory is used extensively in the next sections. A directory is a resource that can hold files. On other operating systems, including Windows and Macintosh, and frequently when speaking about a Linux GUI, a directory is referred to as a folder. That is a good analogy: A traditional manila folder holds files just as a directory does.

In this chapter you work in your home directory tip When you log in on the system, you are working in your home directory. In this chapter that is the only directory you use: All the files you create in this chapter are in your home directory. Chapter 6 goes into more detail about directories.

www.it-ebooks.info

148 Chapter 5 The Linux Utilities

$ ls practice $ cat practice This is a small file that I created with a text editor. $ rm practice $ ls $ cat practice cat: practice: No such file or directory $

Figure 5-1

Using ls, cat, and rm on the file named practice

ls: Lists the Names of Files
Using the editor of your choice, create a small file named practice. (A tutorial on the vim editor appears on page 172.) After exiting from the editor, you can use the ls (list) utility to display a list of the names of the files in your home directory. In the first command in Figure 5-1, ls lists the name of the practice file. (You might also see files that the system or a program created automatically.) Subsequent commands in Figure 5-1 display the contents of the file and remove the file. These commands are described next.

cat: Displays a Text File
The cat utility displays the contents of a text file. The name of the command is derived from catenate, which means to join together, one after the other. (Figure 7-8 on page 235 shows how to use cat to string together the contents of three files.) A convenient way to display the contents of a file on the screen is by giving the command cat, followed by a SPACE and the name of the file. Figure 5-1 shows cat displaying the contents of practice. This figure shows the difference between the ls and cat utilities: The ls utility displays the name of a file, whereas cat displays the contents of a file.

rm: Deletes a File
The rm (remove) utility deletes a file. Figure 5-1 shows rm deleting the file named practice. After rm deletes the file, ls and cat show that practice is no longer in the directory. The ls utility does not list its filename, and cat says that no such file exists. Use rm carefully.

A safer way of removing files tip You can use the interactive form of rm to make sure you delete only the file(s) you intend to delete.
When you follow rm with the –i option (see page 127 for a tip on options) and the name of the file you want to delete, rm prompts with the name of the file and waits for you to respond with y (yes) before it deletes the file. It does not delete the file if you respond with a string that begins with a character other than y. The –i option is set up by default for the root user under Fedora/RHEL:
$ rm -i toollist rm: remove regular file 'toollist'? y

Optional: You can create an alias (page 334) for rm –i and put it in your startup file (page 191) so rm always runs in interactive mode.

www.it-ebooks.info

Working with Files

149

less Is more: Display a Text File One Screen at a Time
Pagers

When you want to view a file that is longer than one screen, you can use either the less utility or the more utility. Each of these utilities pauses after displaying a screen of text; press the SPACE bar to display the next screen of text. Because these utilities show one page at a time, they are called pagers. Although less and more are very similar, they have subtle differences. At the end of the file, for example, less displays an END message and waits for you to press q before returning you to the shell. In contrast, more returns you directly to the shell. While using both utilities you can press h to display a Help screen that lists commands you can use while paging through a file. Give the commands less practice and more practice in place of the cat command in Figure 5-1 to see how these commands work. Use the command less /etc/services instead if you want to experiment with a longer file. Refer to the less and more man pages for more information.

hostname: Displays the System Name
The hostname utility displays the name of the system you are working on. Use this utility if you are not sure that you are logged in on the correct machine.
$ hostname guava

Working with Files
This section describes utilities that copy, move, print, search through, display, sort, compare, and identify files.

Filename completion tip After you enter one or more letters of a filename (following a command) on a command line, press
TAB, and the Bourne Again Shell will complete as much of the filename as it can. When only one filename starts with the characters you entered, the shell completes the filename and places a SPACE after it. You can keep typing or you can press RETURN to execute the command at this point. When

the characters you entered do not uniquely identify a filename, the shell completes what it can and waits for more input. If pressing TAB does not change the display, press TAB again to display a list of possible completions. For more information refer to “Pathname Completion” on page 331.

cp: Copies a File
The cp (copy) utility (Figure 5-2) makes a copy of a file. This utility can copy any file, including text and executable program (binary) files. You can use cp to make a backup copy of a file or a copy to experiment with.
$ ls memo $ cp memo memo.copy $ ls memo memo.copy

Figure 5-2

cp copies a file

www.it-ebooks.info

150 Chapter 5 The Linux Utilities

The cp command line uses the following syntax to specify source and destination files: cp source-file destination-file The source-file is the name of the file that cp will copy. The destination-file is the name cp assigns to the resulting (new) copy of the file. The cp command line in Figure 5-2 copies the file named memo to memo.copy. The period is part of the filename—just another character. The initial ls command shows that memo is the only file in the directory. After the cp command, a second ls shows two files in the directory, memo and memo.copy. Sometimes it is useful to incorporate the date into the name of a copy of a file. The following example includes the date January 30 (0130) in the copied file:
$ cp memo memo.0130

Although it has no significance to Linux, the date can help you find a version of a file you created on a certain date. Including the date can also help you avoid overwriting existing files by providing a unique filename each day. For more information refer to “Filenames” on page 188. Use scp (page 677) or ftp (page 701) when you need to copy a file from one system to another on a common network.

cp can destroy a file caution If the destination-file exists before you give a cp command, cp overwrites it. Because cp overwrites (and destroys the contents of) an existing destination-file without warning, you must take care not to cause cp to overwrite a file that you need. The cp –i (interactive) option prompts you before it overwrites a file. See page 127 for a tip on options. The following example assumes the file named orange.2 exists before you give the cp command. The user answers y to overwrite the file:
$ cp –i orange orange.2 cp: overwrite 'orange.2'? y

mv: Changes the Name of a File
The mv (move) utility can rename a file without making a copy of it. The mv command line specifies an existing file and a new filename using the same syntax as cp: mv existing-filename new-filename The command line in Figure 5-3 changes the name of the file memo to memo.0130. The initial ls command shows that memo is the only file in the directory. After you give the mv command, memo.0130 is the only file in the directory. Compare this result to that of the cp example in Figure 5-2.

www.it-ebooks.info

Working with Files

151

$ ls memo $ mv memo memo.0130 $ ls memo.0130

Figure 5-3

mv renames a file

The mv utility can be used for more than changing the name of a file. Refer to “mv, cp: Move or Copy Files” on page 198. See the mv info page for more information.

mv can destroy a file caution Just as cp can destroy a file, so can mv. Also like cp, mv has a –i (interactive) option. See the caution box labeled “cp can destroy a file.”

lpr: Prints a File
The lpr (line printer) utility places one or more files in a print queue for printing. Linux provides print queues so only one job is printed on a given printer at a time. A queue allows several people or jobs to send output simultaneously to a single printer with the expected results. For systems that have access to more than one printer, you can use lpstat –p to display a list of available printers. Use the –P option to instruct lpr to place the file in the queue for a specific printer—even one that is connected to another system on the network. The following command prints the file named report:
$ lpr report

Because this command does not specify a printer, the output goes to the default printer, which is the printer when you have only one printer. The next command line prints the same file on the printer named mailroom:
$ lpr -P mailroom report

You can see which jobs are in the print queue by giving an lpstat –o command or by using the lpq utility:
$ lpq lp is ready and printing Rank Owner Job Files active max 86 (standard input)

Total Size 954061 bytes

In this example, Max has one job that is being printed; no other jobs are in the queue. You can use the job number (86 in this case) with the lprm utility to remove the job from the print queue and stop it from printing:
$ lprm 86

www.it-ebooks.info

152 Chapter 5 The Linux Utilities

$ cat memo Helen: In our meeting on June 6 we discussed the issue of credit. Have you had any further thoughts about it? Max $ grep 'credit' memo discussed the issue of credit.

Figure 5-4

grep searches for a string

You can send more than one file to the printer with a single command. The following command line prints three files on the printer named laser1:
$ lpr -P laser1 05.txt 108.txt 12.txt

Refer to Chapter 14 for information on setting up a printer and defining the default printer.

grep: Searches for a String
The grep1 utility searches through one or more files to see whether any contain a specified string of characters. This utility does not change the file it searches but simply displays each line that contains the string. The grep command in Figure 5-4 searches through the memo file for lines that contain the string credit and displays the single line that meets this criterion. If memo contained such words as discredit, creditor, or accreditation, grep would have displayed those lines as well because they contain the string it was searching for. The –w (words) option causes grep to match only whole words. Although you do not need to enclose the string you are searching for in single quotation marks, doing so allows you to put SPACEs and special characters in the search string. The grep utility can do much more than search for a simple string in a single file. Refer to the grep info page and Appendix A, “Regular Expressions,” for more information.

head: Displays the Beginning of a File
By default the head utility displays the first ten lines of a file. You can use head to help you remember what a particular file contains. For example, if you have a file

1. Originally the name grep was a play on an ed—an original UNIX editor, available on Fedora/RHEL— command: g/re/p. In this command g stands for global, re is a regular expression delimited by slashes, and p means print.

www.it-ebooks.info

Working with Files

153

$ head months Jan Feb Mar Apr May Jun Jul Aug Sep Oct $ tail -5 months Aug Sep Oct Nov Dec

Figure 5-5

head displays the first ten lines of a file

named months that lists the 12 months of the year in calendar order, one to a line, then head displays Jan through Oct (Figure 5-5). This utility can display any number of lines, so you can use it to look at only the first line of a file, at a full screen, or even more. To specify the number of lines displayed, include a hyphen followed by the number of lines you want head to display. For example, the following command displays only the first line of months:
$ head -1 months Jan

The head utility can also display parts of a file based on a count of blocks or characters rather than lines. Refer to the head info page for more information.

tail: Displays the End of a File
The tail utility is similar to head but by default displays the last ten lines of a file. Depending on how you invoke it, this utility can display fewer or more than ten lines, use a count of blocks or characters rather than lines to display parts of a file, and display lines being added to a file that is changing. The tail command in Figure 5-5 displays the last five lines (Aug through Dec) of the months file. You can monitor lines as they are added to the end of the growing file named logfile with the following command:
$ tail -f logfile

Press the interrupt key (usually CONTROL-C) to stop tail and display the shell prompt. Refer to the tail info page for more information.

www.it-ebooks.info

154 Chapter 5 The Linux Utilities

$ cat days Monday Tuesday Wednesday Thursday Friday Saturday Sunday $ sort days Friday Monday Saturday Sunday Thursday Tuesday Wednesday

Figure 5-6

sort displays the lines of a file in order

sort: Displays a File in Order
The sort utility displays the contents of a file in order by lines; it does not change the original file. Figure 5-6 shows cat displaying the file named days, which contains the name of each day of the week on a separate line in calendar order. The sort utility then displays the file in alphabetical order. The sort utility is useful for putting lists in order. The –u option generates a sorted list in which each line is unique (no duplicates). The –n option puts a list of numbers in numerical order. Refer to the sort info page for more information.

uniq: Removes Duplicate Lines from a File
The uniq (unique) utility displays a file, skipping adjacent duplicate lines; it does not change the original file. If a file contains a list of names and has two successive entries for the same person, uniq skips the extra line (Figure 5-7). If a file is sorted before it is processed by uniq, this utility ensures that no two lines in the file are the same. (Of course, sort can do that all by itself with the –u option.) Refer to the uniq info page for more information.

diff: Compares Two Files
The diff (difference) utility compares two files and displays a list of the differences between them. This utility does not change either file; it is useful when you want to compare two versions of a letter or a report or two versions of the source code for a program. The diff utility with the –u (unified output format) option first displays two lines indicating which of the files you are comparing will be denoted by a plus sign (+)

www.it-ebooks.info

Working with Files

155

$ cat dups Cathy Fred Joe John Mary Mary Paula $ uniq dups Cathy Fred Joe John Mary Paula

Figure 5-7

uniq removes duplicate lines

and which by a minus sign (–). In Figure 5-8, a minus sign indicates the colors.1 file; a plus sign indicates the colors.2 file. The diff –u command breaks long, multiline text into hunks. Each hunk is preceded by a line starting and ending with two at signs (@@). This hunk identifier indicates the starting line number and the number of lines from each file for this hunk. In Figure 5-8, the hunk covers the section of the colors.1 file (indicated by a minus sign) from the first line through the sixth line. The +1,5 then indicates the hunk covers colors.2 from the first line through the fifth line. Following these header lines, diff –u displays each line of text with a leading minus sign, a leading plus sign, or a SPACE. A leading minus sign indicates that the line occurs only in the file denoted by the minus sign. A leading plus sign indicates the line occurs only in the file denoted by the plus sign. A line that begins with a SPACE (neither a plus sign nor a minus sign) occurs in both files in the same location. Refer to the diff info page for more information.
$ diff -u colors.1 colors.2 --- colors.1 2011-04-05 10:12:12.322528610 -0700 +++ colors.2 2011-04-05 10:12:18.420531033 -0700 @@ -1,6 +1,5 @@ red +blue green yellow -pink -purple orange

Figure 5-8

diff displaying the unified output format

www.it-ebooks.info

156 Chapter 5 The Linux Utilities

file: Identifies the Contents of a File
You can use the file utility to learn about the contents of any file on a Linux system without having to open and examine the file yourself. In the following example, file reports that letter_e.bz2 contains data that was compressed by the bzip2 utility (page 160):
$ file letter_e.bz2 letter_e.bz2: bzip2 compressed data, block size = 900k

Next file reports on two more files:
$ file memo zach.jpg memo: ASCII text zach.jpg: JPEG image data, ... resolution (DPI), 72 x 72

Refer to the file man page for more information.

| (Pipe): Communicates Between Processes
Because pipes are integral to the functioning of a Linux system, this chapter introduces them for use in examples. Pipes are covered in detail beginning on page 239. A process is the execution of a command by Linux (page 316). Communication between processes is one of the hallmarks of both UNIX and Linux. A pipe (written as a vertical bar [|] on the command line and appearing as a solid or broken vertical line on a keyboard) provides the simplest form of this kind of communication. A pipe takes the output of one utility and sends that output as input to another utility. More accurately, a pipe takes standard output of one process and redirects it to become standard input of another process. See page 232 for more information on standard output and standard input. Some utilities, such as head, can accept input from a file named on the command line or, via a pipe, from standard input. In the following command line, sort processes the months file (Figure 5-5, page 153); using a pipe, the shell sends the output from sort to the input of head, which displays the first four months of the sorted list:
$ sort months | head -4 Apr Aug Dec Feb

The next command line displays the number of files in a directory. The wc (word count) utility with the –w (words) option displays the number of words in its standard input or in a file you specify on the command line:
$ ls | wc -w 14

www.it-ebooks.info

Four More Utilities

157

$ ls memo memo.0714 practice $ echo Hi Hi $ echo This is a sentence. This is a sentence. $ echo star: * star: memo memo.0714 practice $

Figure 5-9

echo copies the command line (but not the word echo) to the screen

You can also use a pipe to send output of a program to the printer:
$ tail months | lpr

Four More Utilities
The echo and date utilities are two of the most frequently used members of the large collection of Linux utilities. The script utility records part of a session in a file, and unix2dos makes a copy of a text file that can be read on either a Windows or a Macintosh machine.

echo: Displays Text
The echo utility copies the characters you type on the command line after echo to the screen. Figure 5-9 shows some echo commands. The last command shows what the shell does with an unquoted asterisk (*) on the command line: It expands the asterisk into a list of filenames in the directory. The echo utility is a good tool for learning about the shell and other Linux utilities. Some examples on page 246 use echo to illustrate how special characters, such as the asterisk, work. Throughout Chapters 7, 9, and 27, echo helps explain how shell variables work and how you can send messages from shell scripts to the screen. Refer to the coreutils info page, echo section, for more information.

optional You can use echo to create a simple file by redirecting its output to a file:
$ echo 'My new file.' > myfile $ cat myfile My new file.

The greater than (>) sign tells the shell to send the output of echo to the file named myfile instead of to the screen. For more information refer to “Redirecting Standard Output” on page 234.

www.it-ebooks.info

158 Chapter 5 The Linux Utilities

date: Displays the Time and Date
The date utility displays the current date and time:
$ date Tue Apr 5 10:14:41 PDT 2011

The following example shows how you can specify the format and contents of the output of date:
$ date +"%A %B %d" Tuesday April 05

Refer to the date info page for more information.

script: Records a Shell Session
The script utility records all or part of a login session, including your input and the system’s responses. This utility is useful only from character-based devices, such as a terminal or a terminal emulator. It does capture a session with vim; however, because vim uses control characters to position the cursor and display different typefaces, such as bold, the output will be difficult to read and might not be useful. When you cat a file that has captured a vim session, the session quickly passes before your eyes. By default script captures the session in a file named typescript. To specify a different filename, follow the script command with a SPACE and the filename. To append to a file, use the –a option after script but before the filename; otherwise script overwrites an existing file. Following is a session being recorded by script:
$ script Script started, file is typescript $ ls -l /bin | head -5 total 7804 -rwxr-xr-x. 1 root root 123 02-07 -rwxr-xr-x. 1 root root 25948 02-08 lrwxrwxrwx. 1 root root 4 02-25 -rwxr-xr-x. 1 root root 25088 02-08 $ exit exit Script done, file is typescript

17:32 03:46 16:52 03:46

alsaunmute arch awk -> gawk basename

Use the exit command to terminate a script session. You can then view the file you created using cat, less, more, or an editor. Following is the file created by the preceding script command:
$ cat typescript Script started on Tue 05 Apr 2011 10:16:36 AM PDT $ ls -l /bin | head -5 total 7804 -rwxr-xr-x. 1 root root 123 02-07 17:32 alsaunmute -rwxr-xr-x. 1 root root 25948 02-08 03:46 arch lrwxrwxrwx. 1 root root 4 02-25 16:52 awk -> gawk

www.it-ebooks.info

Compressing and Archiving Files 159
-rwxr-xr-x. 1 root root $ exit exit 25088 02-08 03:46 basename

Script done on Tue 05 Apr 2011 10:16:50 AM PDT

If you will be editing the file with vim, emacs, or another editor, you can use dos2unix (next) to eliminate from the typescript file the ^M characters that appear at the ends of the lines. Refer to the script man page for more information.

unix2dos: Converts Linux and Macintosh Files to Windows Format unix2dos, unix2mac

If you want to share a text file you created on a Linux system with someone on a Windows or Macintosh system, you need to convert the file for the person on the other system to read it easily. The unix2dos utility converts a Linux text file so it can be read on a Windows machine; use unix2mac to convert for a Macintosh system. This utility is part of the dos2unix software package; give the command su –c "yum install dos2unix" to install this package. Then enter the following command to convert a file named memo.txt (created with a text editor) to a DOS-format file (use unix2mac to convert to a Macintosh-format file):
$ unix2dos memo.txt

You can now email the file as an attachment to someone on a Windows or Macintosh system. This utility overwrites the original file. dos2unix, dos2mac

The dos2unix utility converts Windows files so they can be read on a Linux system (use dos2mac to convert from a Macintosh system):
$ dos2unix memo.txt

See the dos2unix man page for more information. tr You can also use tr (translate) to change a Windows or Macintosh text file into a Linux text file. In the following example, the –d (delete) option causes tr to remove RETURNs (represented by \r) as it makes a copy of the file:
$ cat memo | tr -d '\r' > memo.txt

The greater than (>) symbol redirects the standard output of tr to the file named memo.txt. For more information refer to “Redirecting Standard Output” on page 234. Converting a file the other way without using unix2dos is not as easy.

Compressing and Archiving Files
Large files use a lot of disk space and take longer than smaller files to transfer from one system to another over a network. If you do not need to look at the contents of a large file often, you might want to save it on a USB flash drive, DVD, or another

www.it-ebooks.info

160 Chapter 5 The Linux Utilities

medium and remove it from the hard disk. If you have a continuing need for the file, retrieving a copy from another medium might be inconvenient. To reduce the amount of disk space you use without removing the file entirely, you can compress the file without losing any of the information it holds. Similarly a single archive of several files packed into a larger file is easier to manipulate, upload, download, and email than multiple files. You might frequently download compressed, archived files from the Internet. The utilities described in this section compress and decompress files and pack and unpack archives.

bzip2: Compresses a File
The bzip2 utility compresses a file by analyzing it and recoding it more efficiently. The new version of the file looks completely different. In fact, because the new file contains many nonprinting characters, you cannot view it directly. The bzip2 utility works particularly well on files that contain a lot of repeated information, such as text and image data, although most image data is already in a compressed format. The following example shows a boring file. Each of the 8,000 lines of the letter_e file contains 72 e’s and a NEWLINE character that marks the end of the line. The file occupies more than half a megabyte of disk storage.
$ ls -l -rw-rw-r--. 1 sam pubs 584000 03-01 22:31 letter_e

The –l (long) option causes ls to display more information about a file. Here it shows that letter_e is 584,000 bytes long. The –v (verbose) option causes bzip2 to report how much it was able to reduce the size of the file. In this case, it shrank the file by 99.99 percent:
$ bzip2 -v letter_e letter_e: 11680.00:1, 0.001 bits/byte, 99.99% saved, 584000 in, 50 out. $ ls -l -rw-rw-r--. 1 sam pubs 50 03-01 22:31 letter_e.bz2 .bz2 filename extension

Now the file is only 50 bytes long. The bzip2 utility also renamed the file, appending .bz2 to its name. This naming convention reminds you that the file is compressed; you would not want to display or print it, for example, without first decompressing it. The bzip2 utility does not change the modification date associated with the file, even though it completely changes the file’s contents.

Keep the original file by using the –k option tip The bzip2 utility (and its counterpart, bunzip2) remove the original file when they compress or decompress a file. Use the –k (keep) option to keep the original file.

In the following, more realistic example, the file zach.jpg contains a computer graphics image:
$ ls -l -rw-r--r--. 1 sam pubs 33287 03-01 22:40 zach.jpg

The bzip2 utility can reduce the size of the file by only 28 percent because the image is already in a compressed format:

www.it-ebooks.info

Compressing and Archiving Files 161
$ bzip2 -v zach.jpg zach.jpg: 1.391:1, 5.749 bits/byte, 28.13% saved, 33287 in, 23922 out. $ ls -l -rw-r--r--. 1 sam pubs 23922 03-01 22:40 zach.jpg.bz2

Refer to the bzip2 man page, www.bzip.org, and the Bzip2 mini-HOWTO (see page 131 for instructions on obtaining this document) for more information.

bzcat and bunzip2: Decompress a File bzcat The bzcat utility displays a file that has been compressed with bzip2. The equivalent of cat for .bz2 files, bzcat decompresses the compressed data and displays the decompressed data. Like cat, bzcat does not change the source file. The pipe in the following example redirects the output of bzcat so instead of being displayed on the screen it becomes the input to head, which displays the first two lines of the file:
$ bzcat letter_e.bz2 | head -2 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

After bzcat is run, the contents of letter_e.bz is unchanged; the file is still stored on the disk in compressed form. bunzip The bunzip2 utility restores a file that has been compressed with bzip2:
$ bunzip2 letter_e.bz2 $ ls -l -rw-rw-r--. 1 sam pubs 584000 03-01 22:31 letter_e $ bunzip2 zach.jpg.bz2 $ ls -l -rw-r--r--. 1 sam pubs 33287 03-01 22:40 zach.jpg

bzip2recover

The bzip2recover utility supports limited data recovery from media errors. Give the command bzip2recover followed by the name of the compressed, corrupted file from which you want to try to recover data.

gzip: Compresses a File gunzip and zcat

The gzip (GNU zip) utility is older and less efficient than bzip2. Its flags and operation are very similar to those of bzip2. A file compressed by gzip is marked with a .gz filename extension. Linux stores manual pages in gzip format to save disk space; likewise, files you download from the Internet are frequently in gzip format. Use gzip, gunzip, and zcat just as you would use bzip2, bunzip2, and bzcat, respectively. Refer to the gzip info page for more information. The compress utility can also compress files, albeit not as well as gzip. This utility marks a file it has compressed by adding .Z to its name.

compress

gzip versus zip tip Do not confuse gzip and gunzip with the zip and unzip utilities. These last two are used to pack and unpack zip archives containing several files compressed into a single file that has been imported from or is being exported to a Windows system. The zip utility constructs a zip archive, whereas unzip unpacks zip archives. The zip and unzip utilities are compatible with PKZIP, a Windows program that compresses and archives files.

www.it-ebooks.info

162 Chapter 5 The Linux Utilities

tar: Packs and Unpacks Archives
The tar utility performs many functions. Its name is short for tape archive, as its original function was to create and read archive and backup tapes. Today it is used to create a single file (called a tar file, archive, or tarball) from multiple files or directory hierarchies and to extract files from a tar file. The cpio utility (page 609) performs a similar function. In the following example, the first ls shows the sizes of the files g, b, and d. Next tar uses the –c (create), –v (verbose), and –f (write to or read from a file) options to create an archive named all.tar from these files. Each line of output displays the name of the file tar is appending to the archive it is creating. The tar utility adds overhead when it creates an archive. The next command shows that the archive file all.tar occupies about 9,700 bytes, whereas the sum of the sizes of the three files is about 6,000 bytes. This overhead is more appreciable on smaller files, such as the ones in this example:
$ ls -l g b -rw-r--r--. -rw-r--r--. -rw-r--r--. d 1 zach other 1178 08-20 14:16 b 1 zach zach 3783 08-20 14:17 d 1 zach zach 1302 08-20 14:16 g

$ tar -cvf all.tar g b d g b d $ ls -l all.tar -rw-r--r--. 1 zach zach 9728 08-20 14:17 all.tar $ tar -tvf -rw-r--r--rw-r--r--rw-r--r-all.tar zach /zach zach /other zach /zach

1302 2011-08-20 14:16 g 1178 2011-08-20 14:16 b 3783 2011-08-20 14:17 d

The final command in the preceding example uses the –t option to display a table of contents for the archive. Use –x instead of –t to extract files from a tar archive. Omit the –v option if you want tar to do its work silently.2 You can use bzip2, compress, or gzip to compress tar files, making them easier to store and handle. Many files you download from the Internet will already be in one of these formats. Files that have been processed by tar and compressed by bzip2 frequently have a filename extension of .tar.bz2 or .tbz. Those processed by tar and gzip have an extension of .tar.gz or .tz, whereas files processed by tar and compress use .tar.Z as the extension.
2. Although the original UNIX tar did not use a leading hyphen to indicate an option on the command line, the GNU/Linux version accepts hyphens but works as well without them. This book precedes tar options with a hyphen for consistency with most other utilities.

www.it-ebooks.info

Compressing and Archiving Files 163

You can unpack a tarred and gzipped file in two steps. (Follow the same procedure if the file was compressed by bzip2, but use bunzip2 instead of gunzip.) The next example shows how to unpack the GNU make utility after it has been downloaded (ftp.gnu.org/pub/gnu/make/make-3.82.tar.gz):
$ ls -l mak* -rw-r--r--. 1 sam pubs 1712747 04-05 10:43 make-3.82.tar.gz $ gunzip mak* $ ls -l mak* -rw-r--r--. 1 sam pubs 6338560 04-05 10:43 make-3.82.tar $ tar -xvf mak* make-3.82/ make-3.82/vmsfunctions.c make-3.82/getopt.h make-3.82/make.1 ... make-3.82/README.OS2 make-3.82/remote-cstms.c

The first command lists the downloaded tarred and gzipped file: make-3.82.tar.gz (about 1.7 megabytes). The asterisk (*) in the filename matches any characters in any filenames (page 246), so ls displays a list of files whose names begin with mak; in this case there is only one. Using an asterisk saves typing and can improve accuracy with long filenames. The gunzip command decompresses the file and yields make-3.82.tar (no .gz extension), which is about 6.3 megabytes. The tar command creates the make-3.82 directory in the working directory and unpacks the files into it.
$ ls -ld mak* drwxr-xr-x. 8 sam pubs 4096 2010-07-27 make-3.82 -rw-r--r--. 1 sam pubs 6338560 04-05 10:43 make-3.82.tar $ ls -l make-3.82 total 2020 -rw-r--r--. 1 sam -rw-r--r--. 1 sam -rw-r--r--. 1 sam -rw-r--r--. 1 sam ... -rw-r--r--. 1 sam -rw-r--r--. 1 sam drwxr-xr-x. 6 sam

pubs pubs pubs pubs pubs pubs pubs

53838 4783 36990 14231

2010-07-27 2010-07-12 2010-07-27 2002-10-14

ABOUT-NLS acinclude.m4 aclocal.m4 alloca.c vmsjobs.c vpath.c w32

18391 2010-07-12 17905 2010-07-19 4096 2010-07-27

After tar extracts the files from the archive, the working directory contains two files whose names start with mak: make-3.82.tar and make-3.82. The –d (directory) option causes ls to display only file and directory names, not the contents of directories as it normally does. The final ls command shows the files and directories in the make-3.82 directory. Refer to the tar info page for more information.

www.it-ebooks.info

164 Chapter 5 The Linux Utilities

tar: the –x option might extract a lot of files caution Some tar archives contain many files. To list the files in the archive without unpacking them, run tar with the –tf options followed by the name of the tar file. In some cases you might want to create a new directory (mkdir [page 194]), move the tar file into that directory, and expand it there. That way the unpacked files will not mingle with existing files, and no confusion will occur. This strategy also makes it easier to delete the extracted files. Depending on how they were created, some tar files automatically create a new directory and put the files into it; the –t option indicates where tar will place the files you extract.

tar: the –x option can overwrite files caution The –x option to tar overwrites a file that has the same filename as a file you are extracting. Follow the suggestion in the preceding caution box to avoid overwriting files.

optional You can combine the gunzip and tar commands on one command line with a pipe
(|), which redirects the output of gunzip so it becomes the input to tar:
$ gunzip -c make-3.82.tar.gz | tar -xvf -

The –c option causes gunzip to send its output through the pipe instead of creating a file. The final hyphen (–) causes tar to read from standard input. Refer to “Pipes” (page 239) and gzip (page 161) for more information about how this command line works. A simpler solution is to use the –z option to tar. This option causes tar to call gunzip (or gzip when you are creating an archive) directly and simplifies the preceding command line to
$ tar -xvzf make-3.82.tar.gz

In a similar manner, the –j option calls bzip2 or bunzip2.

Locating Utilities
The whereis and locate utilities can help you find a command whose name you have forgotten or whose location you do not know. When multiple copies of a utility or program are present, which tells you which copy you will run. The locate utility searches for files on the local system.

which and whereis: Locate a Utility
When you give Linux a command, the shell searches a list of directories for a program with that name. This list of directories is called a search path. For information on how to change the search path, refer to “PATH: Where the Shell Looks for Programs” on page 308. If you do not change the search path, the shell searches only a standard set of directories and then stops searching. However, other directories on the system might also contain useful utilities. which The which utility locates utilities by displaying the full pathname of the file for the utility. (Chapter 6 contains more information on pathnames and the structure of the

www.it-ebooks.info

Locating Utilities 165

Linux filesystem.) The local system might include several utilities that have the same name. When you type the name of a utility, the shell searches for the utility in your search path and runs the first one it finds. You can find out which copy of the utility the shell will run by using which. In the following example, which reports the location of the tar utility:
$ which tar /bin/tar

The which utility can be helpful when a utility seems to be working in unexpected ways. By running which, you might discover that you are running a nonstandard version of a tool or a different one from the one you expected. (“Important Standard Directories and Files” on page 199 provides a list of standard locations for executable files.) For example, if tar is not working properly and you find that you are running /usr/local/bin/tar instead of /bin/tar, you might suspect the local version is broken. whereis The whereis utility searches for files related to a utility by looking in standard locations instead of using your search path. For example, you can find the locations for files related to tar:
$ whereis tar tar: /bin/tar /usr/share/man/man1/tar.1.gz

In this example whereis finds two references to tar: the tar utility file and the tar man page.

which versus whereis tip Given the name of a utility, which looks through the directories in your search path (page 308) in order and locates the utility. If your search path includes more than one utility with the specified name, which displays the name of only the first one (the one you would run). The whereis utility looks through a list of standard directories and works independently of your search path. Use whereis to locate a binary (executable) file, any manual pages, and source code for a program you specify; whereis displays all the files it finds.

which, whereis, and builtin commands caution Both the which and whereis utilities report only the names for utilities as they are found on the disk; they do not report shell builtins (utilities that are built into a shell; page 249). When you use whereis to try to find where the echo command (which exists as both a utility program and a shell builtin) is kept, you get the following result:
$ whereis echo echo: /bin/echo /usr/share/man/man1/echo.1.gz

The whereis utility does not display the echo builtin. Even the which utility reports the wrong information:
$ which echo /bin/echo

Under bash you can use the type builtin (page 1019) to determine whether a command is a builtin:
$ type echo echo is a shell builtin

www.it-ebooks.info

166 Chapter 5 The Linux Utilities

locate: Searches for a File
The locate utility (locate package) searches for files on the local system:
$ locate init /boot/initramfs-2.6.38-0.rc5.git1.1.fc15.i686.img /boot/initrd-plymouth.img /etc/gdbinit /etc/gdbinit.d /etc/init /etc/init.d ...

Before you can use locate, the updatedb utility must build or update the locate database. Typically the database is updated once a day by a cron script (page 611).

If you are not on a network, skip to the vim tutorial tip If you are the only user on a system that is not connected to a network, you might want to skip to the tutorial on the vim editor on page 172. If you are not on a network but are set up to send and receive email, read “Email” on page 171.

Displaying User and System Information
This section covers utilities that provide information about who is using the system, what those users are doing, and how the system is running. To find out who is using the local system, you can employ one of several utilities that vary in the details they provide and the options they support. The oldest utility, who, produces a list of users who are logged in on the local system, the device each person is using, and the time each person logged in. The w and finger utilities show more detail, such as each user’s full name and the command line each user is running. The finger utility can retrieve information about users on remote systems. Table 5-1 on page 169 summarizes the output of these utilities.

who: Lists Users on the System
The who utility displays a list of users who are logged in on the local system. In Figure 5-10 the first column who displays shows that Sam, Max, and Zach are logged in. (Max is logged in from two locations.) The second column shows the device that each user’s terminal, workstation, or terminal emulator is connected to. The third column shows the date and time the user logged in. An optional fourth column shows (in parentheses) the name of the system a remote user logged in from. The information who displays is useful when you want to communicate with a user on the local system. When the user is logged in, you can use write (page 170) to establish communication immediately. If who does not list the user or if you do not need to communicate immediately, you can send email to that person (page 171).

www.it-ebooks.info

Displaying User and System Information 167

$ who sam max zach max

tty4 tty2 tty1 pts/4

2011-07-25 2011-07-25 2011-07-25 2011-07-25

17:18 16:42 16:39 17:27 (guava)

Figure 5-10

who lists who is logged in

If the output of who scrolls off the screen, you can redirect the output through a pipe (|, page 156) so it becomes the input to less, which displays the output one screen at a time. You can also use a pipe to redirect the output through grep to look for a specific name. If you need to find out which terminal you are using or what time you logged in, you can use the command who am i:
$ who am i max pts/4 2011-07-25 17:27 (guava)

finger: Lists Users on the System
The finger utility displays a list of users who are logged in on the local system and in some cases, information about remote systems and users (page 381). In addition to usernames, finger supplies each user’s full name along with information about which device the user’s terminal is connected to, how recently the user typed something on the keyboard, when the user logged in, and what contact information is available. If the user has logged in over the network, the name of the remote system is shown as the user’s office. For example, in Figure 5-11 Max is logged in from the remote system named guava. The asterisks (*) in front of the device names in the Tty column indicate the user has blocked messages sent directly to his terminal (refer to “mesg: Denies or Accepts Messages” on page 170).

finger can be a security risk security On systems where security is a concern, the system administrator might disable finger because it can reveal information that can help a malicious user break into a system.

You can also use finger to learn more about an individual by specifying a username on the command line. In Figure 5-12 on the next page, finger displays detailed information about Max: He is logged in and actively using one of his terminals (tty2), and he has not used his other terminal (pts/4) for 3 minutes and 7 seconds. You also learn from finger that if you want to set up a meeting with Max, you should contact Sam at extension 1693.
$ finger Login max max sam zach

Name Max Wild Max Wild Sam the Great Zach Brill

Tty *tty2 pts/4 *tty4 *tty1

Idle 3 29 1:07

Login Time Office ... Jul 25 16:42 Jul 25 17:27 (guava) Jul 25 17:18 Jul 25 16:39

Figure 5-11

finger I: lists who is logged in

www.it-ebooks.info

168 Chapter 5 The Linux Utilities

$ finger max Login: max Name: Max Wild Directory: /home/max Shell: /bin/bash On since Fri Jul 25 16:42 (PDT) on tty2 (messages off) On since Fri Jul 25 17:27 (PDT) on pts/4 from guava 3 minutes 7 seconds idle New mail received Fri Jul 25 17:16 2010 (PDT) Unread since Fri Jul 25 16:44 2010 (PDT) Plan: I will be at a conference in Hawaii next week. If you need to see me, contact Sam, x1693.

Figure 5-12
.plan and .project

finger II: lists details about one user

Most of the information in Figure 5-12 was collected by finger from system files. The information shown after the heading Plan:, however, was supplied by Max. The finger utility searched for a file named .plan in Max’s home directory and displayed its contents. (Filenames that begin with a period, such as .plan, are not normally listed by ls and are called hidden filenames [page 190].) You might find it helpful to create a .plan file for yourself; it can contain any information you choose, such as your schedule, interests, phone number, or address. In a similar manner, finger displays the contents of the .project and .pgpkey files in your home directory. If Max had not been logged in, finger would have reported only his user information, the last time he logged in, the last time he read his email, and his plan. You can also use finger to display a user’s username. For example, on a system with a user named Helen Simpson, you might know that Helen’s last name is Simpson but might not guess her username is hls. The finger utility, which is not case sensitive, can search for information on Helen using her first or last name. The following commands find the information you seek as well as information on other users whose names are Helen or Simpson:
$ finger HELEN Login: hls ... $ finger simpson Login: hls ... Name: Helen Simpson.

Name: Helen Simpson.

w: Lists Users on the System
The w utility displays a list of the users who are logged in. As discussed in the section on who, the information that w displays is useful when you want to communicate with someone at your installation. The first column in Figure 5-13 shows that Max, Zach, and Sam are logged in. The second column shows the name of the device file each user’s terminal is connected to. The

www.it-ebooks.info

Displaying User and System Information 169

$ w 17:47:35 up 1 day, 8:10, USER TTY FROM sam tty4 max tty2 zach tty1 max pts/4 guava

6 users, load average: 0.34, 0.23, 0.26 LOGIN@ IDLE JCPU PCPU WHAT 17:18 29:14m 0.20s 0.00s vi memo 16:42 0.00s 0.20s 0.07s w 16:39 1:07 0.05s 0.00s run_bdgt 17:27 3:10m 0.24s 0.24s -bash

Figure 5-13

The w utility

third column shows the system that a remote user is logged in from. The fourth column shows the time each user logged in. The fifth column indicates how long each user has been idle (how much time has elapsed since the user pressed a key on the keyboard). The next two columns identify how much computer processor time each user has used during this login session and on the task that user is running. The last column shows the command each user is running. The first line that the w utility displays includes the time of day, the period of time the computer has been running (in days, hours, and minutes), the number of users logged in, and the load average (how busy the system is). The three load average numbers represent the number of jobs waiting to run, averaged over the past 1, 5, and 15 minutes. Use the uptime utility to display just this line. Table 5-1 compares the w, who, and finger utilities.

Table 5-1 Comparison of w, who, and finger
Information displayed
Username Terminal-line identification (tty) Login time (and day for old logins) Login date and time Idle time Program the user is executing Location the user logged in from CPU time used Full name (or other information from /etc/passwd) User-supplied vanity information System uptime and load average x x x x x x x

w x x x

who x x

finger x x

x

x x

www.it-ebooks.info

170 Chapter 5 The Linux Utilities

Communicating with Other Users
The utilities discussed in this section enable you to exchange messages and files with other users either interactively or through email.

write: Sends a Message
The write utility sends a message to another user who is logged in. When you and another user use write to send messages to each other, you establish two-way communication. Initially a write command (Figure 5-14) displays a banner on the other user’s terminal, saying that you are about to send a message. The syntax of a write command line is write username [terminal] The username is the username of the user you want to communicate with. The terminal is an optional device name that is useful if the user is logged in more than once. You can display the usernames and device names of all users who are logged in on the local system by using who, w, or finger. To establish two-way communication with another user, you and the other user must each execute write, specifying the other’s username as the username. The write utility then copies text, line by line, from one keyboard/display to the other (Figure 5-15). Sometimes it helps to establish a convention, such as typing o (for “over”) when you are ready for the other person to type and typing oo (for “over and out”) when you are ready to end the conversation. When you want to stop communicating with the other user, press CONTROL-D at the beginning of a line. Pressing CONTROL-D tells write to quit, displays EOF (end of file) on the other user’s terminal, and returns you to the shell. The other user must do the same. If the Message from banner appears on your screen and obscures something you are working on, press CONTROL- L or CONTROL- R to refresh the screen and remove the banner. Then you can clean up, exit from your work, and respond to the person who is writing to you. You have to remember who is writing to you, however, because the banner will no longer appear on the screen.

mesg: Denies or Accepts Messages
By default, messages to your screen are blocked. Give the following mesg command to allow other users to send you messages:
$ mesg y $ write max Hi Max, are you there? o

Figure 5-14

The write utility I

www.it-ebooks.info

Email 171

$ write max Hi Max, are you there? o Message from max@guava on pts/4 at 16:23 ... Yes Zach, I'm here. o

Figure 5-15

The write utility II

If Max had not given this command before Zach tried to send him a message, Zach might have seen the following message:
$ write max write: max has messages disabled

You can block messages by entering mesg n. Give the command mesg by itself to display is y (for “yes, messages are allowed”) or is n (for “no, messages are not allowed”). If you have messages blocked and you write to another user, write displays the following message because even if you are allowed to write to another user, the user will not be able to respond to you:
$ write max write: you have write permission turned off.

Email
Email enables you to communicate with users on the local system as well as those on the network. If you are connected to the Internet, you can communicate electronically with users around the world. Email utilities differ from write in that they can send a message when the recipient is not logged in. In this case the email is stored until the recipient reads it. These utilities can also send the same message to more than one user at a time. Many email programs are available for Linux, including the original character-based mail program, Mozilla/Thunderbird, pine, mail through emacs, KMail, and evolution. Another popular graphical email program is sylpheed (sylpheed.sraoss.jp/en). Two programs are available that can make any email program easier to use and more secure. The procmail program (www.procmail.org) creates and maintains email servers and mailing lists; preprocesses email by sorting it into appropriate files and directories; starts various programs depending on the characteristics of incoming email; forwards email; and so on. The GNU Privacy Guard (GPG or GNUpg, page 1130) encrypts and decrypts email and makes it almost impossible for an unauthorized person to read. Refer to Chapter 20 for more information on setting email clients and servers.

www.it-ebooks.info

172 Chapter 5 The Linux Utilities
Network addresses

If the local system is part of a LAN, you can generally send email to and receive email from users on other systems on the LAN by using their usernames. Someone sending Max email on the Internet would need to specify his domain name (page 1162) along with his username. Use this address to send email to the author of this book: mgs@sobell.com.

Tutorial: Using vim to Create and Edit a File
This section explains how to start vim, enter text, move the cursor, correct text, save the file to the disk, and exit from vim. The tutorial discusses three of the modes of operation of vim and explains how to switch from one mode to another. vimtutor In addition to working with this tutorial, you might want to try vim’s instructional program, vimtutor. Enter its name as a command to run it.

vimtutor and vim help files are not installed by default tip To run vimtutor and to get help as described on page 176, you must install the vim-enhanced package; give the command su –c 'yum install vim-enhanced' to install this package.
Specifying a terminal

Because vim takes advantage of features that are specific to various kinds of terminals, you must tell it what type of terminal or terminal emulator you are using. On many systems, and usually when you work on a terminal emulator, your terminal type is set automatically. If you need to specify your terminal type explicitly, refer to “Specifying a Terminal” on page 1122.

Starting vim
Start vim with the following command to create and edit a file named practice (use vi in place of vim if you have not installed the vim-enhanced package):
$ vim practice

When you press RETURN, the command line disappears, and the screen looks similar to the one shown in Figure 5-16.

vim is not installed by default: use vi tip The full version of the vim editor is not installed by default. Instead, a small version of vim, named vi, is installed. You can either replace each vim command in this section with vi, or you can install the full vim editor by giving the command su –c 'yum install vim-enhanced' and then use the vim command as shown in this section.

The tildes (~) at the left of the screen indicate the file is empty. They disappear as you add lines of text to the file. If your screen looks like a distorted version of the

www.it-ebooks.info

Tutorial: Using vim to Create and Edit a File 173

Figure 5-16

Starting vim

one shown in Figure 5-16, your terminal type is probably not set correctly (see “Problem,” next). The practice file is new so contains no text. The vim editor displays a message similar to the one shown in Figure 5-16 on the status (bottom) line of the terminal to indicate you are creating and editing a new file. When you edit an existing file, vim displays the first few lines of the file and gives status information about the file on the status line.
Problem

If you start vim with a terminal type that is not in the terminfo database, vim displays an error message and waits for you to press RETURN:
$ vim E437: terminal capability "cm" required Press ENTER or type command to continue

Emergency exit

To reset the terminal type, press ESCAPE and then give the following command to exit from vim and display the shell prompt:
:q!

When you enter the colon (:), vim moves the cursor to the bottom line of the screen. The characters q! tell vim to quit without saving your work. (You will not ordinarily exit from vim this way because you typically want to save your work.) You must press RETURN after you give this command. When the shell displays its prompt, refer to “Specifying a Terminal” on page 1122 and start vim again.

www.it-ebooks.info

174 Chapter 5 The Linux Utilities

Figure 5-17

Starting vim without a filename

If you start vim without a filename, it displays information about itself (Figure 5-17).

Command and Input Modes
Two of vim’s modes of operation are Command mode (also called Normal mode) and Input mode (Figure 5-18). While vim is in Command mode, you can give vim commands. For example, you can delete text or exit from vim. You can also command vim to enter Input mode. In Input mode, vim accepts anything you enter as text and displays it on the screen. Press ESCAPE to return vim to Command mode. By default the vim editor keeps you informed about which mode it is in: It displays INSERT at the lower-left corner of the screen while it is in Insert mode. The following command causes vim to display line numbers next to the text you are editing:
:set number RETURN Last Line mode

The colon (:) in the preceding command puts vim into another mode, Last Line mode. While in this mode, vim keeps the cursor on the bottom line of the screen. When you finish entering the command by pressing RETURN, vim restores the cursor to its place in the text. Give the command :set nonumber RETURN to turn off line numbering. When you give vim a command, remember that the editor is case sensitive. In other words, vim interprets the same letter as two different commands, depending on

vim is case sensitive

www.it-ebooks.info

Tutorial: Using vim to Create and Edit a File 175

Colon (:) Command mode RETURN Insert, Append, Open, Replace, Change Last Line mode

ESCAPE

Input mode

Figure 5-18
LOCK)

Modes in vim

whether you enter an uppercase or lowercase character. Beware of the CAPS LOCK (SHIFTkey. If you set this key to enter uppercase text while you are in Input mode and then exit to Command mode, vim interprets your commands as uppercase letters. It can be confusing when this happens because vim does not appear to be executing the commands you are entering.

Entering Text i/a (Input mode)

When you start vim, you must put it in Input mode before you can enter text. To put vim in Input mode, press the i (insert before cursor) key or the a (append after cursor) key. If you are not sure whether vim is in Input mode, press the ESCAPE key; vim returns to Command mode if it is in Input mode or beeps, flashes, or does nothing if it is already in Command mode. You can put vim back in Input mode by pressing the i or a key again. While vim is in Input mode, you can enter text by typing on the keyboard. If the text does not appear on the screen as you type, vim is not in Input mode. To continue with this tutorial, enter the sample paragraph shown in Figure 5-19 on the next page, pressing the RETURN key at the end of each line. If you do not press RETURN before the cursor reaches the right side of the screen or window, vim wraps the text so that it appears to start a new line. Physical lines will not correspond to programmatic (logical) lines in this situation, so editing will be more difficult. While you are using vim, you can correct typing mistakes. If you notice a mistake on the line you are entering, you can correct it before you continue (page 176). You can correct other mistakes later. When you finish entering the paragraph, press ESCAPE to return vim to Command mode.

www.it-ebooks.info

176 Chapter 5 The Linux Utilities

Figure 5-19

Entering text with vim

Getting Help
You must have the vim-runtime package installed to use vim’s help system; see the tip on page 172. To get help while you are using vim, enter the command :help [feature] followed by RETURN. The editor must be in Command mode when you enter this command. The colon moves the cursor to the last line of the screen. If you type :help, vim displays an introduction to vim Help (Figure 5-20). Each dark band near the bottom of the screen names the file that is displayed above it. (Each area of the screen that displays a file, such as the two areas shown in Figure 5-20, is a vim “window.”) The help.txt file occupies most of the screen (the upper window) in Figure 5-20. The file that is being edited (practice) occupies a few lines in the lower portion of the screen (the lower window). Read through the introduction to Help by scrolling the text as you read. Press j or the DOWN ARROW key to move the cursor down one line at a time; press CONTROL-D or CONTROL-U to scroll the cursor down or up half a window at a time. Give the command :q to close the Help window. You can display information about the insert commands by giving the command :help insert while vim is in Command mode (Figure 5-21).

Correcting Text as You Insert It
The keys that back up and correct a shell command line serve the same functions when vim is in Input mode. These keys include the erase, line kill, and word kill keys (usually CONTROL-H, CONTROL-U, and CONTROL-W, respectively). Although vim might not remove deleted text from the screen as you back up over it using one of these keys, the editor does remove it when you type over the text or press RETURN.

www.it-ebooks.info

Tutorial: Using vim to Create and Edit a File 177

Figure 5-20

The main vim Help screen

Moving the Cursor
You need to be able to move the cursor on the screen so you can delete, insert, and correct text. While vim is in Command mode, the RETURN key, the SPACE bar, and the ARROW keys move the cursor. If you prefer to keep your hand closer to the center of the keyboard, if your terminal does not have ARROW keys, or if the emulator you are using does not support them, you can use the h, j, k, and l (lowercase “l”) keys to move the cursor left, down, up, and right, respectively.

Figure 5-21

Help with insert commands

www.it-ebooks.info

178 Chapter 5 The Linux Utilities

Deleting Text x (Delete character) dw (Delete word) dd (Delete line)

You can delete a single character by moving the cursor until it is over the character you want to delete and then giving the command x. You can delete a word by positioning the cursor on the first letter of the word and then giving the command dw (Delete word). You can delete a line of text by moving the cursor until it is anywhere on the line and then giving the command dd.

Undoing Mistakes u (Undo)

If you delete a character, line, or word by mistake or give any command you want to reverse, give the command u (Undo) immediately after the command you want to undo. The vim editor will restore the text to the way it was before you gave the last command. If you give the u command again, vim will undo the command you gave before the one it just undid. You can use this technique to back up over many of your actions. With the compatible parameter (page 179) set, however, vim can undo only the most recent change. If you undo a command you did not mean to undo, give a Redo command: CONTROL-R or :redo (followed by a RETURN). The vim editor will redo the undone command. As with the Undo command, you can give the Redo command many times in a row.

:redo (Redo)

Entering Additional Text i (Insert) a (Append)

When you want to insert new text within existing text, move the cursor so it is on the character that follows the new text you plan to enter. Then give the i (Insert) command to put vim in Input mode, enter the new text, and press ESCAPE to return vim to Command mode. Alternatively, you can position the cursor on the character that precedes the new text and use the a (Append) command. To enter one or more lines, position the cursor on the line above where you want the new text to go. Give the command o (Open). The vim editor opens a blank line below the line the cursor was on, puts the cursor on the new, empty line, and goes into Input mode. Enter the new text, ending each line with a RETURN. When you are finished entering text, press ESCAPE to return vim to Command mode. The O command works in the same way o works, except it opens a blank line above the line the cursor is on.

o/O (Open)

Correcting Text
To correct text, use dd, dw, or x to remove the incorrect text. Then use i, a, o, or O to insert the correct text. For example, to change the word pressing to hitting in Figure 5-19 on page 176, you might use the ARROW keys to move the cursor until it is on top of the p in pressing. Then give the command dw to delete the word pressing. Put vim in Input mode by giving an i command, enter the word hitting followed by a SPACE, and press ESCAPE. The word is changed, and vim is in Command mode, waiting for another command. A shorthand for the two commands dw followed by the i command is cw (Change word). The command cw puts vim into Input mode.

www.it-ebooks.info

Chapter Summary 179

Page breaks for the printer tip
CONTROL-L tells the printer to skip to the top of the next page. You can enter this character anywhere in a document by pressing CONTROL-L while you are in Input mode. If ^L does not appear, press CONTROL-V before CONTROL-L.

Ending the Editing Session
While you are editing, vim keeps the edited text in an area named the Work buffer. When you finish editing, you must write out the contents of the Work buffer to a disk file so the edited text is saved and available when you next want it. Make sure vim is in Command mode and use the ZZ command (you must use uppercase Zs) to write the newly entered text to the disk and end the editing session. After you give the ZZ command, vim returns control to the shell. You can exit with :q! if you do not want to save your work.

Do not confuse ZZ with CONTROL-Z caution When you exit from vim with ZZ, make sure that you type ZZ and not CONTROL-Z (typically the suspend key). When you press CONTROL-Z, vim disappears from your screen, almost as though you had exited from it. In fact, vim will continue running in the background with your work unsaved. Refer to “Job Control” on page 296. If you try to start editing the same file with a new vim command, vim displays a message about a swap file.

The compatible Parameter
The compatible parameter makes vim more compatible with vi. By default this parameter is not set. While you are running vim, give the command :set compatible RETURN to set the compatible parameter; use nocompatible to unset this parameter. To get started with vim, you can ignore this parameter. Setting the compatible parameter changes many aspects of how vim works. For example, when the compatible parameter is set, the Undo command (page 178) can undo only the most recent change; in contrast, with the compatible parameter unset, you can call Undo repeatedly to undo many changes. To obtain more details on the compatible parameter, give the command :help compatible RETURN. To display a complete list of vim’s differences from the original vi, use :help vi-diff RETURN. See page 176 for a discussion of the help command.

Chapter Summary
The utilities introduced in this chapter are a small but powerful subset of the many utilities available on a Fedora/RHEL system. Because you will use them frequently and because they are integral to the following chapters, it is important that you become comfortable using them.

www.it-ebooks.info

180 Chapter 5 The Linux Utilities

The utilities listed in Table 5-2 manipulate, display, compare, and print files.

Table 5-2 File utilities
Utility
cp diff file grep head lpq lpr lprm mv sort tail uniq

Function
Copies one or more files (page 149) Displays the differences between two files (page 154) Displays information about the contents of a file (page 156) Searches file(s) for a string (page 152) Displays the lines at the beginning of a file (page 152) Displays a list of jobs in the print queue (page 151) Places file(s) in the print queue (page 151) Removes a job from the print queue (page 151) Renames a file or moves file(s) to another directory (page 150) Puts a file in order by lines (page 154) Displays the lines at the end of a file (page 153) Displays the contents of a file, skipping adjacent duplicate lines (page 154)

To reduce the amount of disk space a file occupies, you can compress it using the bzip2 utility. Compression works especially well on files that contain patterns, as do most text files, but reduces the size of almost all files. The inverse of bzip2— bunzip2—restores a file to its original, decompressed form. Table 5-3 lists utilities that compress and decompress files. The bzip2 utility is the most efficient of these.

Table 5-3 (De)compression utilities
Utility
bunzip2 bzcat bzip2 compress gunzip gzip unzip

Function
Returns a file compressed with bzip2 to its original size and format (page 161) Displays a file compressed with bzip2 (page 161) Compresses a file (page 160) Compresses a file (not as well as bzip2 or gzip; page 161) Returns a file compressed with gzip or compress to its original size and format (page 161) Compresses a file (not as well as bzip2; page 161) Unpacks zip archives, which are compatible with Windows PKZIP

www.it-ebooks.info

Chapter Summary 181

Table 5-3 (De)compression utilities (continued) zcat zip
Displays a file compressed with gzip (page 161) Constructs zip archives, which are compatible with Windows PKZIP

An archive is a file, frequently compressed, that contains a group of files. The tar utility (Table 5-4) packs and unpacks archives. The filename extensions .tar.bz2, .tar.gz, and .tgz identify compressed tar archive files and are often seen on software packages obtained over the Internet.

Table 5-4 Archive utility
Utility
tar

Function
Creates or extracts files from an archive file (page 162)

The utilities listed in Table 5-5 determine the location of a utility on the local system. For example, they can display the pathname of a utility or a list of C++ compilers available on the local system.

Table 5-5 Location utilities
Utility
locate whereis which

Function
Searches for files on the local system (page 166) Displays the full pathnames of a utility, source code, or man page (page 164) Displays the full pathname of a command you can run (page 164)

Table 5-6 lists utilities that display information about the local system and other users. You can easily learn a user’s full name, login status, login shell, and other items of information maintained by the system.

Table 5-6 User and system information utilities
Utility
finger hostname w who

Function
Displays detailed information about users, including their full names (page 167) Displays the name of the local system (page 149) Displays detailed information about users who are logged in on the local system (page 168) Displays information about users who are logged in on the local system (page 166)

www.it-ebooks.info

182 Chapter 5 The Linux Utilities

The utilities shown in Table 5-7 can help you stay in touch with other users on the local network.

Table 5-7 User communication utilities
Utility
mesg write

Function
Permits or denies messages sent by write (page 170) Sends a message to another user who is logged in (page 170)

Table 5-8 lists miscellaneous utilities.

Table 5-8 Miscellaneous utilities
Utility
date echo vim

Function
Displays the current date and time (page 158) Copies its arguments (page 1151) to the screen (page 157) Edits text (page 172)

Exercises
1. Which commands can you use to determine who is logged in on a specific terminal? 2. How can you keep other users from using write to communicate with you? Why would you want to? 3. What happens when you give the following commands if the file named done already exists?
$ cp to_do done $ mv to_do done

4. How can you find out which utilities are available on your system for editing files? Which utilities are available for editing on your system? 5. How can you find the phone number for Ace Electronics in a file named phone that contains a list of names and phone numbers? Which command can you use to display the entire file in alphabetical order? How can you display the file without any adjacent duplicate lines? How can you display the file without any duplicate lines? 6. What happens when you use diff to compare two binary files that are not identical? (You can use gzip to create the binary files.) Explain why the diff output for binary files is different from the diff output for ASCII files.

www.it-ebooks.info

Advanced Exercises 183

7. Create a .plan file in your home directory. Does finger display the contents of your .plan file? 8. What is the result of giving the which utility the name of a command that resides in a directory that is not in your search path? 9. Are any of the utilities discussed in this chapter located in more than one directory on the local system? If so, which ones? 10. Experiment by calling the file utility with the names of files in /usr/bin. How many different types of files are there? 11. Which command can you use to look at the first few lines of a file named status.report? Which command can you use to look at the end of the file?

Advanced Exercises
12. Re-create the colors.1 and colors.2 files used in Figure 5-8 on page 155. Test your files by running diff –u on them. Do you get the same results as in the figure? 13. Try giving these two commands:
$ echo cat $ cat echo

Explain the differences between the output of each command. 14. Repeat exercise 5 using the file phone.gz, a compressed version of the list of names and phone numbers. Consider more than one approach to answer each question and explain how you made your choices. 15. Find or create files that a. gzip compresses by more than 80 percent. b. gzip compresses by less than 10 percent. c. Get larger when compressed with gzip. d. Use ls –l to determine the sizes of the files in question. Can you characterize the files in a, b, and c? 16. Older email programs were not able to handle binary files. Suppose you are emailing a file that has been compressed with gzip, which produces a binary file, and the recipient is using an old email program. Refer to the man page on uuencode, which converts a binary file to ASCII. Learn about the utility and how to use it. a. Convert a compressed file to ASCII using uuencode. Is the encoded file larger or smaller than the compressed file? Explain. (If uuencode is not on the local system, you can install it using yum [page 534]; it is part of

www.it-ebooks.info

184 Chapter 5 The Linux Utilities

the sharutils package.) b. Would it ever make sense to use uuencode on a file before compressing it? Explain.

www.it-ebooks.info

6
The Linux Filesystem
C 6 hapter6

In This Chapter
The Hierarchical Filesystem . . . . . 186 Directory Files and Ordinary Files 187 The Working Directory. . . . . . . . . . 190 Your Home Directory . . . . . . . . . . . 191 Pathnames . . . . . . . . . . . . . . . . . . 191 Relative Pathnames . . . . . . . . . . . 193 Working with Directories . . . . . . . 194 Access Permissions . . . . . . . . . . . 202 ACLs: Access Control Lists . . . . . . 208 Hard Links . . . . . . . . . . . . . . . . . . . 214 Symbolic Links . . . . . . . . . . . . . . . 216

Objectives
After reading this chapter you should be able to: Define hierarchical filesystem, ordinary files, directory files, home directory, working directory, and parent directory List best practices for filenames Determine the name of the working directory Explain the difference between absolute and relative pathnames Create and remove directories List files in a directory, remove files from a directory, and copy and move files between directories List and describe the uses of standard Linux directories and files View and interpret file and directory ownership and permissions Modify file and directory permissions Expand access control using ACLs Describe the uses, differences, and methods of creating hard links and symbolic links

185

www.it-ebooks.info

186 Chapter 6 The Linux Filesystem

Grandparent

Aunt

Mom

Uncle

Sister

Brother

Self

Daughter 1

Daughter 2

Grandchild 1

Grandchild 2

Figure 6-1

A family tree

A filesystem is a set of data structures (page 1160) that usually resides on part of a disk and that holds directories of files. Filesystems store user and system data that are the basis of users’ work on the system and the system’s existence. This chapter discusses the organization and terminology of the Linux filesystem, defines ordinary and directory files, and explains the rules for naming them. It also shows how to create and delete directories, move through the filesystem, and use absolute and relative pathnames to access files in various directories. It includes a discussion of important files and directories as well as file access permissions and ACLs (Access Control Lists), which allow you to share selected files with specified users. It concludes with a discussion of hard and symbolic links, which can make a single file appear in more than one directory. In addition to reading this chapter, you can refer to the df info page and to the fsck, mkfs, and tune2fs man pages for more information on filesystems. For information on additional important Linux files, see “Important Files and Directories” on page 502.

The Hierarchical Filesystem
Family tree

A hierarchical (page 1168) structure frequently takes the shape of a pyramid. One example of this type of structure is found by tracing a family’s lineage: A couple has a child, who might in turn have several children, each of whom might have more children. This hierarchical structure is called a family tree (Figure 6-1). Like the family tree it resembles, the Linux filesystem is called a tree. It consists of a set of connected files. This structure allows you to organize files so you can easily find any particular one. On a standard Linux system, each user starts with one

Directory tree

www.it-ebooks.info

Directory Files and Ordinary Files 187

correspond

personal

memos

business

milk_co

cheese_co

letter_1

letter_2

Figure 6-2

A secretary’s directories

directory, to which the user can add subdirectories to any desired level. By creating multiple levels of subdirectories, a user can expand the structure as needed.
Subdirectories

Typically each subdirectory is dedicated to a single subject, such as a person, project, or event. The subject dictates whether a subdirectory should be subdivided further. For example, Figure 6-2 shows a secretary’s subdirectory named correspond. This directory contains three subdirectories: business, memos, and personal. The business directory contains files that store each letter the secretary types. If you expect many letters to go to one client, as is the case with milk_co, you can dedicate a subdirectory to that client. One major strength of the Linux filesystem is its ability to adapt to users’ needs. You can take advantage of this strength by strategically organizing your files so they are most convenient and useful for you.

Directory Files and Ordinary Files
Like a family tree, the tree representing the filesystem is usually pictured upside down with its root at the top. Figures 6-2 and 6-3 (on the next page) show that the tree “grows” downward from the root with paths connecting the root to each of the other files. At the end of each path is either an ordinary file or a directory file. Special files, which can also appear at the ends of paths, are described on page 515. Ordinary files, or simply files, appear at the ends of paths that cannot support other paths. Directory files, also referred to as directories or folders, are the points that other paths can branch off from. (Figures 6-2 and 6-3 show some empty directories.) When you refer to the tree, up is toward the root and down is away from the root. Directories directly connected by a path are called parents (closer to the root) and children (farther from the root). A pathname is a series of names that trace a path along branches from one file to another. See page 191 for more information about pathnames.

www.it-ebooks.info

188 Chapter 6 The Linux Filesystem

Directory

Directory

Directory

Directory

Ordinary File

Ordinary File

Directory

Directory

Ordinary File

Ordinary File

Ordinary File

Figure 6-3

Directories and ordinary files

Filenames
Every file has a filename. The maximum length of a filename varies with the type of filesystem; Linux supports several types of filesystems. Although most of today’s filesystems allow files with names up to 255 characters long, some filesystems restrict filenames to fewer characters. Although you can use almost any character in a filename, you will avoid confusion if you choose characters from the following list: • Uppercase letters (A–Z) • Lowercase letters (a–z) • Numbers (0–9) • Underscore (_) • Period (.) • Comma (,) Like the children of one parent, no two files in the same directory can have the same name. (Parents give their children different names because it makes good sense, but Linux requires it.) Files in different directories, like the children of different parents, can have the same name. The filenames you choose should mean something. Too often a directory is filled with important files with such unhelpful names as hold1, wombat, and junk, not to mention foo and foobar. Such names are poor choices because they do not help you recall what you stored in a file. The following filenames conform to the suggested syntax and convey information about the contents of the file: • correspond • january

www.it-ebooks.info

Directory Files and Ordinary Files 189

• davis • reports • 2001 • acct_payable
Filename length

When you share your files with users on other systems, you might need to make long filenames differ within the first few characters. Systems running DOS or older versions of Windows have an 8-character filename body length limit and a 3-character filename extension length limit. Some UNIX systems have a 14-character limit, and older Macintosh systems have a 31-character limit. If you keep filenames short, they are easy to type; later you can add extensions to them without exceeding the shorter limits imposed by some filesystems. The disadvantage of short filenames is that they are typically less descriptive than long filenames. See “stat: Displays Information About a File or Filesystem” on page 473 for a way to determine the maximum length of a filename on the local system. Long filenames enable you to assign descriptive names to files. To help you select among files without typing entire filenames, shells support filename completion. For more information about this feature, see the “Filename completion” tip on page 149.

Case sensitivity

You can use uppercase and/or lowercase letters within filenames. Linux is case sensitive, so files named JANUARY, January, and january represent three distinct files.

Do not use SPACEs within filenames caution Although Linux allows you to use SPACEs within filenames, it is a poor idea. Because a SPACE is a special character, you must quote it on a command line. Quoting a character on a command line can be difficult for a novice user and cumbersome for an experienced user. Use periods or underscores instead of SPACEs: joe.05.04.26, new_stuff. If you are working with a filename that includes a SPACE, such as a file from another operating system, you must quote the SPACE on the command line by preceding it with a backslash or by placing quotation marks on either side of the filename. The two following commands send the file named my file to the printer.
$ lpr my\ file $ lpr "my file"

Filename Extensions
A filename extension is the part of the filename that follows an embedded period. In the filenames listed in Table 6-1 on the next page, filename extensions help describe the contents of the file. Some programs, such as the C programming language compiler, default to specific filename extensions; in most cases, however, filename extensions are optional. Use extensions freely to make filenames easy to understand. If you like, you can use several periods within the same filename—for example, notes.4.10.54 or files.tar.gz.

www.it-ebooks.info

190 Chapter 6 The Linux Filesystem

Table 6-1 Filename extensions
Filename with extension compute.c compute.o compute memo.0410.txt memo.pdf memo.ps memo.Z memo.gz memo.tgz or memo.tar.gz memo.bz2 memo.html photo.gif, photo.jpg, photo.jpeg, photo.bmp, photo.tif, or photo.tiff

Meaning of extension
A C programming language source file The object code file for compute.c The executable file for compute.c A text file A PDF file; view with xpdf or kpdf under a GUI A PostScript file; view with ghostscript or kpdf under a GUI A file compressed with compress (page 161); use uncompress or gunzip (page 161) to decompress A file compressed with gzip (page 161); view with zcat or decompress with gunzip (both on page 161) A tar (page 162) archive of files compressed with gzip (page 161) A file compressed with bzip2 (page 160); view with bzcat or decompress with bunzip2 (both on page 161) A file meant to be viewed using a Web browser, such as Firefox A file containing graphical information, such as a picture

Hidden Filenames
A filename that begins with a period is called a hidden filename (or a hidden file or sometimes an invisible file) because ls does not normally display it. The command ls –a displays all filenames, even hidden ones. Names of startup files (next page) usually begin with a period so that they are hidden and do not clutter a directory listing. The .plan file (page 168) is also hidden. Two special hidden entries—single and double periods (. and ..)—appear in every directory (page 196).

The Working Directory pwd While you are logged in on a character-based interface to a Linux system, you are always associated with a directory. The directory you are associated with is called the working directory or current directory. Sometimes this association is referred to in a physical sense: “You are in (or working in) the zach directory.” The pwd (print working directory) builtin displays the pathname of the working directory.

www.it-ebooks.info

Pathnames 191

login: max Password: Last login: Wed Oct 20 11:14:21 from 172.16.192.150 $ pwd /home/max

Figure 6-4

Logging in and displaying the pathname of your home directory

Your Home Directory
When you first log in on a Linux system or start a terminal emulator window, the working directory is your home directory. To display the pathname of your home directory, use pwd just after you log in (Figure 6-4). When used without any arguments, the ls utility displays a list of the files in the working directory. Because your home directory has been the only working directory you have used so far, ls has always displayed a list of files in your home directory. (All the files you have created up to this point were created in your home directory.)

Startup Files
Startup files, which appear in your home directory, give the shell and other programs information about you and your preferences. Frequently one of these files tells the shell what kind of terminal you are using (page 1122) and executes the stty (set terminal) utility to establish the erase (page 140) and line kill (page 140) keys. Either you or the system administrator can put a shell startup file containing shell commands in your home directory. The shell executes the commands in this file each time you log in. Because the startup files have hidden filenames (filenames that begin with a period; page 190), you must use the ls –a command to see whether one is in your home directory. A GUI has many startup files. Usually you do not need to work with these files directly but can control startup sequences using icons on the desktop. See page 282 for more information about startup files.

Pathnames
Every file has a pathname, which is a trail from a directory through part of the directory hierarchy to an ordinary file or a directory. Within a pathname, a slash (/) following (to the right of) a filename indicates that the file is a directory file. The file following (to the right of) the slash can be an ordinary file or a directory file. The simplest pathname is a simple filename, which points to a file in the working directory. This section discusses absolute and relative pathnames and explains how to use each.

www.it-ebooks.info

192 Chapter 6 The Linux Filesystem

/ /home home tmp etc /home/hls max /home/zach bin notes /home/hls/bin/log report log zach hls /home/hls/notes /etc

Figure 6-5

Absolute pathnames

Absolute Pathnames
/ (root)

The root directory of the filesystem hierarchy does not have a name; it is referred to as the root directory and is represented by a slash (/) standing alone or at the left end of a pathname. An absolute pathname starts with a slash (/), which represents the root directory. The slash is followed by the name of a file located in the root directory. An absolute pathname can continue, tracing a path through all intermediate directories, to the file identified by the pathname. String all the filenames in the path together, following each directory with a slash (/). This string of filenames is called an absolute pathname because it locates a file absolutely by tracing a path from the root directory to the file. Typically the absolute pathname of a directory does not include the trailing slash, although that format can be used to emphasize that the pathname specifies a directory (e.g., /home/zach/). The part of a pathname following the final slash is called a simple filename, filename, or basename. Figure 6-5 shows the absolute pathnames of directories and ordinary files in part of a filesystem hierarchy. Using an absolute pathname, you can list or otherwise work with any file on the local system, assuming you have permission to do so, regardless of the working directory at the time you give the command. For example, Sam can give the following command while working in his home directory to list the files in the /etc/ssh directory:
$ pwd /home/sam $ ls /etc/ssh moduli ssh_host_dsa_key ssh_config ssh_host_dsa_key.pub sshd_config ssh_host_key

ssh_host_key.pub ssh_host_rsa_key ssh_host_rsa_key.pub

www.it-ebooks.info

Pathnames 193

~ (Tilde) in Pathnames
In another form of absolute pathname, the shell expands the characters ~/ (a tilde followed by a slash) at the start of a pathname into the pathname of your home directory. Using this shortcut, you can display your .bashrc startup file (page 283) with the following command no matter which directory is the working directory:
$ less ~/.bashrc

A tilde quickly references paths that start with your or someone else’s home directory. The shell expands a tilde followed by a username at the beginning of a pathname into the pathname of that user’s home directory. For example, assuming he has permission to do so, Max can examine Sam’s .bashrc file with the following command:
$ less ~sam/.bashrc

Refer to “Tilde Expansion” on page 348 for more information.

Relative Pathnames
A relative pathname traces a path from the working directory to a file. The pathname is relative to the working directory. Any pathname that does not begin with the root directory (represented by /) or a tilde (~) is a relative pathname. Like absolute pathnames, relative pathnames can trace a path through many directories. The simplest relative pathname is a simple filename, which identifies a file in the working directory. The examples in the next sections use absolute and relative pathnames.

Significance of the Working Directory
To access any file in the working directory, you need only a simple filename. To access a file in another directory, you must use a pathname. Typing a long pathname is tedious and increases the chance of making a mistake. This possibility is less likely under a GUI, where you click filenames or icons. You can choose a working directory for any particular task to reduce the need for long pathnames. Your choice of a working directory does not allow you to do anything you could not do otherwise— it just makes some operations easier.

When using a relative pathname, know which directory is the working directory caution The location of the file you are accessing with a relative pathname is dependent on (is relative to) the working directory. Always make sure you know which directory is the working directory before you use a relative pathname. Use pwd to verify the directory. If you are creating a file using vim and you are not where you think you are in the file hierarchy, the new file will end up in an unexpected location. It does not matter which directory is the working directory when you use an absolute pathname. Thus, the following command always edits a file named goals in your home directory:
$ vim ~/goals

www.it-ebooks.info

194 Chapter 6 The Linux Filesystem

/ .. home tmp etc working directory = . max ../zach bin notes bin/log report log zach hls notes

Figure 6-6 Relative pathnames

Refer to Figure 6-6 as you read this paragraph. Files that are children of the working directory can be referenced by simple filenames. Grandchildren of the working directory can be referenced by short relative pathnames: two filenames separated by a slash. When you manipulate files in a large directory structure, using short relative pathnames can save you time and aggravation. If you choose a working directory that contains the files used most often for a particular task, you need use fewer long, cumbersome pathnames.

Working with Directories
This section discusses how to create directories (mkdir), switch between directories (cd), remove directories (rmdir), use pathnames to make your work easier, and move and copy files and directories between directories. It concludes with brief descriptions of important standard directories and files in the Linux filesystem.

mkdir: Creates a Directory
The mkdir utility creates a directory. The argument (page 1151) to mkdir is the pathname of the new directory. The following examples develop the directory structure shown in Figure 6-7. In the figure, the directories that are added appear in a lighter shade than the others and are connected by dashes. In Figure 6-8, pwd shows that Max is working in his home directory (/home/max), and ls shows the names of the files in his home directory: demo, names, and temp. Using mkdir, Max creates a directory named literature as a child of his home directory. He uses a relative pathname (a simple filename) because he wants the literature directory to be a child of the working directory. Max could have used an absolute pathname to create the same directory: mkdir /home/max/literature, mkdir ~max/literature, or mkdir ~/literature.

www.it-ebooks.info

Working with Directories

195

/

home

max

names

temp

literature

demo

promo

Figure 6-7

The file structure developed in the examples

The second ls in Figure 6-8 verifies the presence of the new directory. The –F option to ls displays a slash after the name of each directory and an asterisk after each executable file (shell script, utility, or application). When you call it with an argument that is the name of a directory, ls lists the contents of that directory. The final ls displays nothing because there are no files in the literature directory. The following commands show two ways for Max to create the promo directory as a child of the newly created literature directory. The first way checks that /home/max is the working directory and uses a relative pathname:
$ pwd /home/max $ mkdir literature/promo

The second way uses an absolute pathname:
$ mkdir /home/max/literature/promo

$ pwd /home/max $ ls demo names temp $ mkdir literature $ ls demo literature names temp $ ls -F demo literature/ names temp $ ls literature $

Figure 6-8

The mkdir utility

www.it-ebooks.info

196 Chapter 6 The Linux Filesystem

$ cd /home/max/literature $ pwd /home/max/literature $ cd $ pwd /home/max $ cd literature $ pwd /home/max/literature

Figure 6-9

cd changes the working directory

Use the –p (parents) option to mkdir to create both the literature and promo directories with one command:
$ pwd /home/max $ ls demo names temp $ mkdir -p literature/promo

or
$ mkdir -p /home/max/literature/promo

cd: Changes to Another Working Directory
The cd (change directory) utility makes another directory the working directory; it does not change the contents of the working directory. Figure 6-9 shows two ways to make the /home/max/literature directory the working directory, as verified by pwd. First Max uses cd with an absolute pathname to make literature his working directory—it does not matter which is the working directory when you give a command with an absolute pathname. A pwd command confirms the change Max made. When used without an argument, cd makes your home directory the working directory, as it was when you logged in. The second cd command in Figure 6-9 does not have an argument, so it makes Max’s home directory the working directory. Finally, knowing that he is working in his home directory, Max uses a simple filename to make the literature directory his working directory (cd literature) and confirms the change using pwd.

The . and .. Directory Entries
The mkdir utility automatically puts two entries in each directory it creates: a single period (.) and a double period (..). The . is synonymous with the pathname of the working directory and can be used in its place; the .. is synonymous with the pathname of the parent of the working directory. These entries are hidden because their filenames begin with a period. With the literature directory as the working directory, the following example uses .. three times: first to list the contents of the parent directory (/home/max), second to

www.it-ebooks.info

Working with Directories

197

copy the memoA file to the parent directory, and third to list the contents of the parent directory again.
$ pwd /home/max/literature $ ls .. demo literature names $ cp memoA .. $ ls .. demo literature memoA

temp

names

temp

After using cd to make promo (a subdirectory of literature) his working directory, Max can use a relative pathname to call vim to edit a file in his home directory.
$ cd promo $ vim ../../names

You can use an absolute or relative pathname or a simple filename virtually anywhere a utility or program requires a filename or pathname. This usage holds true for ls, vim, mkdir, rm, and most other Linux utilities.

The working directory versus your home directory tip The working directory is not the same as your home directory. Your home directory remains the same for the duration of your session and usually from session to session. Immediately after you log in, you are always working in the same directory: your home directory. Unlike your home directory, the working directory can change as often as you like. You have no set working directory, which explains why some people refer to it as the current directory. When you log in and until you change directories using cd, your home directory is the working directory. If you were to change directories to Sam’s home directory, then Sam’s home directory would be the working directory.

rmdir: Deletes a Directory
The rmdir (remove directory) utility deletes a directory. You cannot delete the working directory or a directory that contains files other than the . and .. entries. If you need to delete a directory that has files in it, first use rm to delete the files and then delete the directory. You do not have to (nor can you) delete the . and .. entries; rmdir removes them automatically. The following command deletes the promo directory:
$ rmdir /home/max/literature/promo

The rm utility has a –r option (rm –r filename) that recursively deletes files, including directories, within a directory and also deletes the directory itself.

Use rm –r carefully, if at all caution Although rm –r is a handy command, you must use it carefully. Do not use it with an ambiguous file reference such as *. It is frighteningly easy to wipe out your entire home directory with a single short command.

www.it-ebooks.info

198 Chapter 6 The Linux Filesystem

Using Pathnames touch Use a text editor to create a file named letter if you want to experiment with the examples that follow. Alternatively you can use touch to create an empty file:
$ cd $ pwd /home/max $ touch letter

With /home/max as the working directory, the following example uses cp with a relative pathname to copy the file letter to the /home/max/literature/promo directory. (You will need to create promo again if you deleted it earlier.) The copy of the file has the simple filename letter.0210:
$ cp letter literature/promo/letter.0210

If Max does not change to another directory, he can use vim as shown to edit the copy of the file he just made:
$ vim literature/promo/letter.0210

If Max does not want to use a long pathname to specify the file, he can use cd to make promo the working directory before using vim:
$ cd literature/promo $ pwd /home/max/literature/promo $ vim letter.0210

To make the parent of the working directory (named /home/max/literature) the new working directory, Max can give the following command, which takes advantage of the .. directory entry:
$ cd .. $ pwd /home/max/literature

mv, cp: Move or Copy Files
Chapter 5 discussed the use of mv to rename files. However, mv works even more generally: You can use this utility to move files from one directory to another (change the pathname of a file) as well as to change a simple filename. When used to move one or more files to a new directory, the mv command has this syntax: mv existing-file-list directory If the working directory is /home/max, Max can use the following command to move the files names and temp from the working directory to the literature directory:
$ mv names temp literature

This command changes the absolute pathnames of the names and temp files from /home/max/names and /home/max/temp to /home/max/literature/names and

www.it-ebooks.info

Working with Directories

199

/

home

zach

max

sam

names

temp

literature

names

temp

Figure 6-10

Using mv to move names and temp

/home/max/literature/temp, respectively (Figure 6-10). Like most Linux commands, mv accepts either absolute or relative pathnames. As you work with Linux and create more files, you will need to create new directories using mkdir to keep the files organized. The mv utility is a useful tool for moving files from one directory to another as you extend your directory hierarchy. The cp utility works in the same way mv does, except that it makes copies of the existing-file-list in the specified directory.

mv: Moves a Directory
Just as it moves ordinary files from one directory to another, so mv can move directories. The syntax is similar except you specify one or more directories, not ordinary files, to move: mv existing-directory-list new-directory If new-directory does not exist, the existing-directory-list must contain just one directory name, which mv changes to new-directory (mv renames the directory). Although you can rename directories using mv, you cannot copy their contents with cp unless you use the –r (recursive) option. Refer to the tar and cpio man pages for other ways to copy and move directories.

Important Standard Directories and Files
Originally files on a Linux system were not located in standard places within the directory hierarchy. The scattered files made it difficult to document and maintain a Linux system and just about impossible for someone to release a software package that would compile and run on all Linux systems. The first standard for the Linux filesystem, the FSSTND (Linux Filesystem Standard), was released early in 1994. In early 1995 work was started on a broader standard covering many UNIX-like systems: FHS

www.it-ebooks.info

200 Chapter 6 The Linux Filesystem

/ bin sbin var dev usr etc tmp home root

mail

spool

bin

sbin

max

zach

hls

Figure 6-11

A typical FHS-based Linux filesystem structure

(Linux Filesystem Hierarchy Standard; proton.pathname.com/fhs). More recently FHS has been incorporated in LSB (Linux Standard Base; www.linuxfoundation.org/ collaborate/workgroups/lsb ), a workgroup of FSG (Free Standards Group). Finally, FSG combined with Open Source Development Labs (OSDL) to form the Linux Foundation (www.linuxfoundation.org). Figure 6-11 shows the locations of some important directories and files as specified by FHS. The significance of many of these directories will become clear as you continue reading. The following list describes the directories shown in Figure 6-11, some of the directories specified by FHS, and some other directories. Fedora/RHEL, however, does not use all the directories specified by FHS. Be aware that you cannot always determine the function of a directory by its name. For example, although /opt stores add-on software, /etc/opt stores configuration files for the software in /opt. See also “Important Files and Directories” on page 502.
/ Root The root directory, present in all Linux filesystem structures, is the ancestor of

all files in the filesystem. It does not have a name and is represented by a slash (/) standing alone or at the left end of a pathname.
/bin Essential command binaries Holds the files needed to bring the system up and run it

when it first comes up in single-user mode (page 450).
/boot Static files of the boot loader Contains all the files needed to boot the system. /dev Device files Contains all files that represent peripheral devices, such as disk drives,

terminals, and printers. Previously this directory was filled with all possible devices. The udev utility (page 516) provides a dynamic device directory that enables /dev to contain only devices that are present on the system.
/etc Machine–local system configuration files Holds administrative, configuration, and

other system files.
/etc/opt Configuration files for add-on software packages kept in /opt /etc/X11 Machine–local configuration files for the X Window System /home User home directories Each user’s home directory is typically one of many sub-

directories of the /home directory. As an example, assuming that users’ directories are under /home, the absolute pathname of Zach’s home directory is /home/zach. On some systems the users’ directories might not be found under /home but instead might be spread among other directories such as /inhouse and /clients.
/lib Shared libraries

www.it-ebooks.info

Working with Directories /lib/modules Loadable kernel modules /mnt Mount point for temporarily mounting filesystems /opt Add-on (optional) software packages /proc Kernel and process information virtual filesystem /root Home directory for the root account

201

/run Runtime data A tmpfs filesystem (mounted, but stored in RAM) that holds startup

files previously hidden in /dev and other directories. For more information see lists.fedoraproject.org/pipermail/devel/2011-March/150031.html.
/sbin Essential system binaries Utilities used for system administration are stored in /sbin

and /usr/sbin. The /sbin directory includes utilities needed during the booting process, and /usr/sbin holds utilities used after the system is up and running.
/sys Device pseudofilesystem See udev on page 516 for more information. /tmp Temporary files /usr Second major hierarchy Traditionally includes subdirectories that contain information

used by the system. Files in /usr subdirectories do not change often and can be shared by several systems.
/usr/bin Most user commands Contains the standard Linux utility programs—that is, binaries

that are not needed in single-user mode (page 450).
/usr/games Games and educational programs /usr/include Header files included by C programs /usr/lib Libraries /usr/local Local hierarchy Holds locally important files and directories that are added to the

system. Subdirectories can include bin, games, include, lib, sbin, share, and src.
/usr/sbin Nonvital system administration binaries See /sbin. /usr/share Architecture-independent data Subdirectories can include dict, doc, games, info,

locale, man, misc, terminfo, and zoneinfo.
/usr/share/doc Documentation /usr/share/info GNU info system’s primary directory /usr/share/man Online manuals /usr/src Source code /var Variable data Files with contents that vary as the system runs are kept in sub-

directories under /var. The most common examples are temporary files, system log files, spooled files, and user mailbox files. Subdirectories can include cache, lib, lock, log, mail, opt, run, spool, tmp, and yp.
/var/log Log files Contains lastlog (a record of the last login by each user), messages (system

messages from syslogd), and wtmp (a record of all logins/logouts), among other log files.
/var/spool Spooled application data Contains anacron, at, cron, lpd, mail, mqueue, samba,

and other directories. The file /var/mail is typically a link to /var/spool/mail.

www.it-ebooks.info

202 Chapter 6 The Linux Filesystem

Access Permissions
In addition to the controls imposed by SELinux (page 459), Fedora/RHEL supports two methods of controlling who can access a file and how they can access it: traditional Linux access permissions and ACLs (Access Control Lists). This section describes traditional Linux access permissions. See page 208 for a discussion of ACLs, which provide finer-grained control of access permissions than do traditional access permissions. Three types of users can access a file: the owner of the file (owner), a member of a group that the file is associated with (group; see page 506 for more information on groups), and everyone else (other). A user can attempt to access an ordinary file in three ways: by trying to read from, write to, or execute it.

ls –l: Displays Permissions
When you call ls with the –l option and the name of one or more ordinary files, ls displays a line of information about the file(s). See “ls output” on page 20 for information about the format of the display this book uses. The following example displays information for two files. The file letter.0210 contains the text of a letter, and check_spell contains a shell script, a program written in a high-level shell programming language:
$ ls -l check_spell letter.0210 -rwxr-xr-x. 1 sam pubs 766 03-21 14:02 check_spell -rw-r--r--. 1 sam pubs 6193 02-10 14:22 letter.0210

From left to right, the lines that an ls –l command displays contain the following information (refer to Figure 6-12): • The type of file (first character) • The file’s access permissions (the next nine characters) • The ACL flag (present if the file has an ACL, page 208) • The number of links to the file (page 213) • The name of the owner of the file (usually the person who created the file)
Da t of e (an mo d dif tim ica e) tio n

of file Fil ea pe cc rm es iss s i on s AC Lf la g Lin ks Ow ne r

Typ e

Gr ou p

Siz e

-rwxr-xr-x+
Figure 6-12

1 sam pubs

2048 06-10

memo

The columns displayed by the ls –l command

www.it-ebooks.info

Fil e

na me

Access Permissions 203

• The name of the group the file is associated with • The size of the file in characters (bytes) • The date and time the file was created or last modified • The name of the file The type of file (first column) for letter.0210 is a hyphen (–) because it is an ordinary file (directory files have a d in this column). The next three characters specify the access permissions for the owner of the file: r indicates read permission, w indicates write permission, and x indicates execute permission. A – in a column indicates that the owner does not have the permission that could have appeared in that position. In a similar manner the next three characters represent permissions for the group, and the final three characters represent permissions for other (everyone else). In the preceding example, the owner of letter.0210 can read from and write to the file, whereas the group and others can only read from the file, and no one is allowed to execute it. Although execute permission can be allowed for any file, it does not make sense to assign execute permission to a file that contains a document such as a letter. The check_spell file is an executable shell script, so execute permission is appropriate for it. (The owner, group, and others have execute permission.)

chmod: Changes Access Permissions
The Linux file access permission scheme lets you give other users access to the files you want to share yet keep your private files confidential. You can allow other users to read from and write to a file (handy if you are one of several people working on a joint project). You can allow others only to read from a file (perhaps a project specification you are proposing). Or you can allow others only to write to a file (similar to an inbox or mailbox, where you want others to be able to send you mail but do not want them to read your mail). Similarly you can protect entire directories from being scanned (covered shortly).

A user with root privileges can access any file on the system security There is an exception to the access permissions described in this section. Anyone who can gain root privileges has full access to all files, regardless of the file’s owner or access permissions. Of course, if the file is encrypted, read access does not mean the person reading the file can understand what is in the file.

The owner of a file controls which users have permission to access the file and how those users can access it. When you own a file, you can use the chmod (change mode) utility to change access permissions for that file. You can specify symbolic (relative) or numeric (absolute) arguments to chmod.

www.it-ebooks.info

204 Chapter 6 The Linux Filesystem

Symbolic Arguments to chmod
The following example, which uses symbolic arguments to chmod, adds (+) read and write permissions (rw) for all (a) users:
$ ls -l letter.0210 -rw-r-----. 1 sam pubs 6193 02-10 14:22 letter.0210 $ chmod a+rw letter.0210 $ ls -l letter.0210 -rw-rw-rw-. 1 sam pubs 6193 02-10 14:22 letter.0210

You must have read permission to execute a shell script tip Because a shell needs to read a shell script (a text file containing shell commands) before it can execute the commands within that script, you must have read permission for the file containing the script to execute it. You also need execute permission to execute a shell script directly from the command line. In contrast, binary (program) files do not need to be read; they are executed directly. You need only execute permission to run a binary program.

Using symbolic arguments with chmod modifies existing permissions; the change a given argument makes depends on (is relative to) the existing permissions. In the next example, chmod removes (–) read (r) and execute (x) permissions for other (o) users. The owner and group permissions are not affected.
$ ls -l check_spell -rwxr-xr-x. 1 sam pubs 766 03-21 14:02 check_spell $ chmod o-rx check_spell $ ls -l check_spell -rwxr-x---. 1 sam pubs 766 03-21 14:02 check_spell

In addition to a (all) and o (other), you can use g (group) and u (user, although user refers to the owner of the file who might or might not be the user of the file at any given time) in the argument to chmod. For example, chmod a+x adds execute permission for all users (other, group, and owner), and chmod go–rwx removes all permissions for all but the owner of the file.

chmod: o for other, u for owner tip When using chmod, many people assume that the o stands for owner; it does not. The o stands for other, whereas u stands for owner (user). The acronym UGO (user-group-other) might help you remember how permissions are named.

Numeric Arguments to chmod
You can also use numeric arguments to specify permissions with chmod. In place of the letters and symbols specifying permissions used in the previous examples, numeric arguments comprise three octal digits. (A fourth, leading digit controls setuid and setgid permissions and is discussed next.) The first digit specifies permissions for the owner, the second for the group, and the third for other users. A 1 gives the specified user(s) execute permission, a 2 gives write permission, and a 4

www.it-ebooks.info

Access Permissions 205

gives read permission. Construct the digit representing the permissions for the owner, group, or others by ORing (adding) the appropriate values as shown in the following examples. Using numeric arguments sets file permissions absolutely; it does not modify existing permissions as symbolic arguments do. In the following example, chmod changes permissions so only the owner of the file can read from and write to the file, regardless of how permissions were previously set. The 6 in the first position gives the owner read (4) and write (2) permissions. The 0s remove all permissions for the group and other users.
$ chmod 600 letter.0210 $ ls -l letter.0210 -rw-------. 1 sam pubs 6193 02-10 14:22 letter.0210

Next, 7 (4 + 2 + 1) gives the owner read, write, and execute permissions. The 5 (4 + 1) gives the group and other users read and execute permissions:
$ chmod 755 check_spell $ ls -l check_spell -rwxr-xr-x. 1 sam pubs 766 03-21 14:02 check_spell

Refer to Table 6-2 for more examples of numeric permissions.

Table 6-2 Examples of numeric permission specifications
Mode
777 755 711 644 640

Meaning
Owner, group, and others can read, write, and execute file Owner can read, write, and execute file; group and others can read and execute file Owner can read, write, and execute file; group and others can execute file Owner can read and write file; group and others can read file Owner can read and write file, group can read file, and others cannot access file

Refer to page 288 for more information on using chmod to make a file executable and to the chmod man page for more information on absolute arguments and chmod in general. Refer to page 506 for more information on groups.

Setuid and Setgid Permissions
When you execute a file that has setuid (set user ID) permission, the process executing the file takes on the privileges of the file’s owner. For example, if you run a setuid program that removes all files in a directory, you can remove files in any of the file owner’s directories, even if you do not normally have permission to do so. In a similar manner, setgid (set group ID) permission gives the process executing the file the privileges of the group the file is associated with.

www.it-ebooks.info

206 Chapter 6 The Linux Filesystem

Minimize use of setuid and setgid programs owned by root security Executable files that are setuid and owned by root have root privileges when they run, even if they are not run by root. This type of program is very powerful because it can do anything that root can do (and that the program is designed to do). Similarly executable files that are setgid and belong to the group root have extensive privileges. Because of the power they hold and their potential for destruction, it is wise to avoid indiscriminately creating and using setuid programs owned by root and setgid programs belonging to the group root. Because of their inherent dangers, many sites minimize the use of these programs on their systems. One necessary setuid program is passwd. See page 412 for a tip on setuid files owned by root and page 459 for a command that lists setuid files on the local system. Because of this danger, Fedora/RHEL will be removing most setuid files in a future release. See fedoraproject.org/wiki/Features/RemoveSETUID for more information.

The following example shows a user working with root privileges and using symbolic arguments to chmod to give one program setuid privileges and another program setgid privileges. The ls –l output (page 202) shows setuid permission by displaying an s in the owner’s executable position and setgid permission by displaying an s in the group’s executable position:
# ls -l myprog* -rwxr-xr-x. 1 root pubs 362804 03-21 15:38 myprog1 -rwxr-xr-x. 1 root pubs 189960 03-21 15:38 myprog2 # chmod u+s myprog1 # chmod g+s myprog2 # ls -l myprog* -rwsr-xr-x. 1 root pubs 362804 03-21 15:38 myprog1 -rwxr-sr-x. 1 root pubs 189960 03-21 15:38 myprog2

The next example uses numeric arguments to chmod to make the same changes. When you use four digits to specify permissions, setting the first digit to 1 sets the sticky bit (page 1190), setting it to 2 specifies setgid permissions, and setting it to 4 specifies setuid permissions:
# ls -l myprog* -rwxr-xr-x. 1 root pubs 362804 03-21 15:38 myprog1 -rwxr-xr-x. 1 root pubs 189960 03-21 15:38 myprog2 # chmod 4755 myprog1 # chmod 2755 myprog2 # ls -l myprog* -rwsr-xr-x. 1 root pubs 362804 03-21 15:38 myprog1 -rwxr-sr-x. 1 root pubs 189960 03-21 15:38 myprog2

Do not write setuid shell scripts security Never give shell scripts setuid permission. Several techniques for subverting them are well known.

www.it-ebooks.info

Access Permissions 207

Directory Access Permissions
Access permissions have slightly different meanings when they are applied to directories. Although the three types of users can read from or write to a directory, the directory cannot be executed. Execute permission is redefined for a directory: It means that you can cd into the directory and/or examine files that you have permission to read from in the directory. It has nothing to do with executing a file. When you have only execute permission for a directory, you can use ls to list a file in the directory if you know its name. You cannot use ls to list the entire contents of the directory. In the following exchange, Zach first verifies that he is logged in as himself. He then checks the permissions on Max’s info directory. You can view the access permissions associated with a directory by running ls with the –d (directory) and –l (long) options:
$ who am i zach pts/7 Aug 21 10:02 $ ls -ld /home/max/info drwx-----x. 2 max pubs 4096 08-21 09:31 /home/max/info $ ls -l /home/max/info ls: /home/max/info: Permission denied

The d at the left end of the line that ls displays indicates /home/max/info is a directory. Max has read, write, and execute permissions; members of the pubs group have no access permissions; and other users have execute permission only, indicated by the x at the right end of the permissions. Because Zach does not have read permission for the directory, the ls –l command returns an error. When Zach specifies the names of the files he wants information about, he is not reading new directory information but rather searching for specific information, which he is allowed to do with execute access to the directory. He has read permission for notes so he has no problem using cat to display the file. He cannot display financial because he does not have read permission for it:
$ ls -l /home/max/info/financial /home/max/info/notes -rw-------. 1 max pubs 34 08-21 09:31 /home/max/info/financial -rw-r--r--. 1 max pubs 30 08-21 09:32 /home/max/info/notes $ cat /home/max/info/notes This is the file named notes. $ cat /home/max/info/financial cat: /home/max/info/financial: Permission denied

Next Max gives others read access to his info directory:
$ chmod o+r /home/max/info

When Zach checks his access permissions on info, he finds he has both read and execute access to the directory. Now ls –ld displays the contents f the info directory, but he still cannot read financial. (This restriction is an issue of file permissions, not directory permissions.) Finally, Zach tries to create a file named newfile using touch.

www.it-ebooks.info

208 Chapter 6 The Linux Filesystem

If Max were to give him write permission to the info directory, Zach would be able to create new files in it:
$ ls -ld /home/max/info drwx---r-x. 2 max pubs 4096 08-21 09:31 /home/max/info $ ls -l /home/max/info total 8 -rw-------. 1 max pubs 34 08-21 09:31 financial -rw-r--r--. 1 max pubs 30 08-21 09:32 notes $ cat /home/max/info/financial cat: financial: Permission denied $ touch /home/max/info/newfile touch: cannot touch '/home/max/info/newfile': Permission denied

ACLs: Access Control Lists
ACLs (Access Control Lists) provide finer-grained control over which users can access specific directories and files than do traditional Linux permissions (page 202). Using ACLs you can specify the ways in which each of several users can access a directory or file. Because ACLs can reduce performance, do not enable them on filesystems that hold system files, where the traditional Linux permissions are sufficient. Also, be careful when moving, copying, or archiving files: Not all utilities preserve ACLs. In addition, you cannot copy ACLs to filesystems that do not support ACLs. An ACL comprises a set of rules. A rule specifies how a specific user or group can access the file that the ACL is associated with. There are two kinds of rules: access rules and default rules. (The documentation refers to access ACLs and default ACLs, even though there is only one type of ACL: There is one type of list [ACL] and there are two types of rules that an ACL can contain.) An access rule specifies access information for a single file or directory. A default ACL pertains to a directory only; it specifies default access information (an ACL) for any file in the directory that is not given an explicit ACL.

Most utilities do not preserve ACLs caution When used with the –p (preserve) or –a (archive) option, cp preserves ACLs when it copies files.
The mv utility also preserves ACLs. When you use cp with the –p or –a option and it is not able to copy ACLs, and in the case where mv is unable to preserve ACLs, the utility performs the operation and issues an error message:
$ mv report /tmp mv: preserving permissions for '/tmp/report': Operation not supported

Other utilities, such as tar, cpio, and dump, do not support ACLs. You can use cp with the –a option to copy directory hierarchies, including ACLs. You can never copy ACLs to a filesystem that does not support ACLs or to a filesystem that does not have ACL support turned on.

www.it-ebooks.info

ACLs: Access Control Lists 209

Enabling ACLs
The acl package must be installed before you can use ACLs. Fedora/RHEL officially supports ACLs on ext2, ext3, and ext4 filesystems only, although informal support for ACLs is available on other filesystems. To use ACLs on an ext2/ext3/ext4 filesystem, you must mount the device with the acl option (no_acl is the default). For example, if you want to mount the device represented by /home so that you can use ACLs on files in /home, you can add acl to its options list in /etc/fstab:
$ grep home /etc/fstab LABEL=/home /home remount option ext4 defaults,acl 1 2

After changing fstab, you need to remount /home before you can use ACLs. If no one else is using the system, you can unmount it and mount it again (working with root privileges) as long as the working directory is not in the /home hierarchy. Alternatively you can use the remount option to mount to remount /home while the device is in use:
# mount -v -o remount /home /dev/sda3 on /home type ext4 (rw,acl)

See page 524 for information on fstab and page 520 for information on mount.

Working with Access Rules
The setfacl utility modifies a file’s ACL and getfacl displays a file’s ACL. When you use getfacl to obtain information about a file that does not have an ACL, it displays some of the same information as an ls –l command, albeit in a different format:
$ ls -l report -rw-r--r--. 1 max pubs 9537 01-12 23:17 report $ getfacl report # file: report # owner: max # group: pubs user::rwgroup::r-other::r--

The first three lines of the getfacl output comprise the header; they specify the name of the file, the owner of the file, and the group the file is associated with. For more information refer to “ls –l: Displays Permissions” on page 202. The ––omit-header (or just ––omit) option causes getfacl not to display the header:
$ getfacl --omit-header report user::rwgroup::r-other::r--

In the line that starts with user, the two colons (::) with no name between them indicate that the line specifies the permissions for the owner of the file. Similarly, the

www.it-ebooks.info

210 Chapter 6 The Linux Filesystem

two colons in the group line indicate that the line specifies permissions for the group the file is associated with. The two colons following other are there for consistency: No name can be associated with other. The setfacl ––modify (or –m) option adds or modifies one or more rules in a file’s ACL using the following format: setfacl ––modify ugo:name:permissions file-list where ugo can be either u, g, or o to indicate that the command sets file permissions for a user, a group, or all other users, respectively; name is the name of the user or group that permissions are being set for; permissions is the permissions in either symbolic or absolute format; and file-list is the list of files the permissions are to be applied to. You must omit name when you specify permissions for other users (o). Symbolic permissions use letters to represent file permissions (rwx, r–x, and so on), whereas absolute permissions use an octal number. While chmod uses three sets of permissions or three octal numbers (one each for the owner, group, and other users), setfacl uses a single set of permissions or a single octal number to represent the permissions being granted to the user or group represented by ugo and name. See the discussion of chmod on page 203 for more information about symbolic and absolute representations of file permissions. For example, both of the following commands add a rule to the ACL for the report file that gives Sam read and write permission to that file:
$ setfacl --modify u:sam:rw- report

or
$ setfacl --modify u:sam:6 report $ getfacl report # file: report # owner: max # group: pubs user::rwuser:sam:rwgroup::r-mask::rwother::r--

The line containing user:sam:rw– shows that the user named sam has read and write access (rw–) to the file. See page 202 for an explanation of how to read access permissions. See the following optional section for a description of the line that starts with mask. When a file has an ACL, ls –l displays a plus sign (+) following the permissions, even if the ACL is empty:
$ ls -l report -rw-rw-r--+ 1 max pubs 9537 01-12 23:17 report

optional

Effective Rights Mask
The line in the output of getfacl that starts with mask specifies the effective rights mask. This mask limits the effective permissions granted to ACL groups and users. It

www.it-ebooks.info

ACLs: Access Control Lists 211

does not affect the owner of the file or the group the file is associated with. In other words, it does not affect traditional Linux permissions. However, because setfacl always sets the effective rights mask to the least restrictive ACL permissions for the file, the mask has no effect unless you set it explicitly after you set up an ACL for the file. You can set the mask by specifying mask in place of ugo and by not specifying a name in a setfacl command. The following example sets the effective rights mask to read for the report file:
$ setfacl -m mask::r-- report

The mask line in the following getfacl output shows the effective rights mask set to read (r––). The line that displays Sam’s file access permissions shows them still set to read and write. However, the comment at the right end of the line shows that his effective permission is read.
$ getfacl report # file: report # owner: max # group: pubs user::rwuser:sam:rwgroup::r-mask::r-other::r--

#effective:r--

As the next example shows, setfacl can modify ACL rules and can set more than one ACL rule at a time:
$ setfacl -m u:sam:r--,u:zach:rw- report $ getfacl --omit-header report user::rwuser:sam:r-user:zach:rwgroup::r-mask::rwother::r--

The –x option removes ACL rules for a user or a group. It has no effect on permissions for the owner of the file or the group that the file is associated with. The next example shows setfacl removing the rule that gives Sam permission to access the file:
$ setfacl -x u:sam report $ getfacl --omit-header report user::rwuser:zach:rwgroup::r-mask::rwother::r--

You must not specify permissions when you use the –x option. Instead, specify only the ugo and name. The –b option, followed by a filename only, removes all ACL rules and the ACL itself from the file or directory you specify.

www.it-ebooks.info

212 Chapter 6 The Linux Filesystem

Both setfacl and getfacl have many options. Use the ––help option to display brief lists of options or refer to the man pages for details.

Setting Default Rules for a Directory
The following example shows that the dir directory initially has no ACL. The setfacl command uses the –d (default) option to add two default rules to the ACL for dir. These rules apply to all files in the dir directory that do not have explicit ACLs. The rules give members of the pubs group read and execute permissions and give members of the adm group read, write, and execute permissions.
$ ls -ld dir drwx------. 2 max pubs 4096 02-12 23:15 dir $ getfacl dir # file: dir # owner: max # group: pubs user::rwx group::--other::--$ setfacl -d -m g:pubs:r-x,g:adm:rwx dir

The following ls command shows that the dir directory now has an ACL, as indicated by the + to the right of the permissions. Each of the default rules that getfacl displays starts with default:. The first two default rules and the last default rule specify the permissions for the owner of the file, the group that the file is associated with, and all other users. These three rules specify the traditional Linux permissions and take precedence over other ACL rules. The third and fourth rules specify the permissions for the pubs and adm groups. Next is the default effective rights mask.
$ ls -ld dir drwx------+ 2 max pubs 4096 02-12 23:15 dir $ getfacl dir # file: dir # owner: max # group: pubs user::rwx group::--other::--default:user::rwx default:group::--default:group:pubs:r-x default:group:adm:rwx default:mask::rwx default:other::---

Remember that the default rules pertain to files held in the directory that are not assigned ACLs explicitly. You can also specify access rules for the directory itself. When you create a file within a directory that has default rules in its ACL, the effective rights mask for that file is created based on the file’s permissions. In some cases the mask can override default ACL rules.

www.it-ebooks.info

Links 213

In the next example, touch creates a file named new in the dir directory. The ls command shows this file has an ACL. Based on the value of umask (page 473), both the owner and the group that the file is associated with have read and write permissions for the file. The effective rights mask is set to read and write so that the effective permission for pubs is read and the effective permissions for adm are read and write. Neither group has execute permission.
$ cd dir $ touch new $ ls -l new -rw-rw----+ 1 max pubs 0 02-13 00:39 new $ getfacl --omit new user::rwgroup::--group:pubs:r-x #effective:r-group:adm:rwx #effective:rwmask::rwother::---

If you change the file’s traditional permissions to read, write, and execute for the owner and the group, the effective rights mask changes to read, write, and execute, and the groups specified by the default rules gain execute access to the file.
$ chmod 770 new $ ls -l new -rwxrwx---+ 1 max pubs 0 02-13 00:39 new $ getfacl --omit new user::rwx group::--group:pubs:r-x group:adm:rwx mask::rwx other::---

Links
A link is a pointer to a file. Each time you create a file using vim, touch, cp, or by some other means, you are putting a pointer in a directory. This pointer associates a filename with a place on the disk. When you specify a filename in a command, you are indirectly pointing to the place on the disk that holds the information you want. Sharing files can be useful when two or more people are working on the same project and need to share some information. You can make it easy for other users to access one of your files by creating additional links to the file. To share a file with another user, first give the user permission to read from and write to the file (page 203). You might also have to change the access permissions of the parent directory of the file to give the user read, write, or execute permission (page 207). When the permissions are appropriately set, the user can create a link to the file so each of you can access the file from your separate directory hierarchies.

www.it-ebooks.info

214 Chapter 6 The Linux Filesystem

correspond

personal

memos

business

to_do

to_do

to_do

to_do

personal

memos

business business

Links
Figure 6-13

Using links to cross-classify files

A link can also be useful to a single user with a large directory hierarchy. You can create links to cross-classify files in your directory hierarchy, using different classifications for different tasks. For example, if you have the file layout depicted in Figure 6-2 on page 187, a file named to_do might appear in each subdirectory of the correspond directory—that is, in personal, memos, and business. If you find it difficult to keep track of everything you need to do, you can create a separate directory named to_do in the correspond directory. You can then link each subdirectory’s to-do list into that directory. For example, you could link the file named to_do in the memos directory to a file named memos in the to_do directory. This set of links is shown in Figure 6-13. Although it might sound complicated, this technique keeps all your to-do lists conveniently in one place. The appropriate list is easily accessible in the task-related directory when you are busy composing letters, writing memos, or handling personal business.

About the discussion of hard links tip Two kinds of links exist: hard links and symbolic (soft) links. Hard links are older and becoming outdated. The section on hard links is marked as optional; you can skip it, although it discusses inodes and gives you insight into the structure of the filesystem.

optional

Hard Links
A hard link to a file appears as another file. If the file appears in the same directory as the linked-to file, the links must have different filenames because two files in the same directory cannot have the same name. You can create a hard link to a file only from within the filesystem that holds the file.

ln: Creates a Hard Link
The ln (link) utility (without the –s or ––symbolic option) creates a hard link to an existing file using the following syntax: ln existing-file new-link

www.it-ebooks.info

Links 215

/

home

max

zach

memo

planning

/home/max/letter and /home/zach/draft

Figure 6-14

Two links to the same file: /home/max/letter and /home/zach/draft

The next command shows Zach making the link shown in Figure 6-14 by creating a new link named /home/max/letter to an existing file named draft in Zach’s home directory:
$ pwd /home/zach $ ln draft /home/max/letter

The new link appears in the /home/max directory with the filename letter. In practice, Max might need to change directory permissions so Zach will be able to create the link. Even though /home/max/letter appears in Max’s directory, Zach is the owner of the file because he created it. The ln utility creates an additional pointer to an existing file, but it does not make another copy of the file. Because there is only one file, the file status information—such as access permissions, owner, and the time the file was last modified—is the same for all links; only the filenames differ. When Zach modifies /home/zach/draft, for example, Max sees the changes in /home/max/letter.

cp Versus ln
The following commands verify that ln does not make an additional copy of a file. Create a file, use ln to make an additional link to the file, change the contents of the file through one link, and verify the change through the other link:
$ cat file_a This is file A. $ ln file_a file_b $ cat file_b This is file A. $ vim file_b ... $ cat file_b This is file B after the change. $ cat file_a This is file B after the change.

www.it-ebooks.info

216 Chapter 6 The Linux Filesystem

If you try the same experiment using cp instead of ln and change a copy of the file, the difference between the two utilities will become clearer. After you change a copy of a file, the two files are different:
$ cat file_c This is file C. $ cp file_c file_d $ cat file_d This is file C. $ vim file_d ... $ cat file_d This is file D after the change. $ cat file_c This is file C. ls and link counts

You can use ls with the –l option, followed by the names of the files you want to compare, to confirm that the status information is the same for two links to the same file and is different for files that are not linked. In the following example, the 2 in the links field (just to the left of max) shows there are two links to file_a and file_b (from the previous example):
$ ls -l file_a file_b file_c file_d -rw-r--r--. 2 max pubs 33 05-24 10:52 -rw-r--r--. 2 max pubs 33 05-24 10:52 -rw-r--r--. 1 max pubs 16 05-24 10:55 -rw-r--r--. 1 max pubs 33 05-24 10:57 file_a file_b file_c file_d

Although it is easy to guess which files are linked to one another in this example, ls does not explicitly tell you. ls and inodes

Use ls with the –i option to determine without a doubt which files are linked. The –i option lists the inode (page 1169) number for each file. An inode is the control structure for a file. If the two filenames have the same inode number, they share the same control structure and are links to the same file. Conversely, when two filenames have different inode numbers, they are different files. The following example shows that file_a and file_b have the same inode number and that file_c and file_d have different inode numbers:
$ ls -i file_a file_b file_c file_d 3534 file_a 3534 file_b 5800 file_c 7328 file_d

All links to a file are of equal value: The operating system cannot distinguish the order in which multiple links were created. When a file has two links, you can remove either one and still access the file through the remaining link. You can remove the link used to create the file, for example, and as long as one link remains, still access the file through that link.

Symbolic Links
In addition to hard links, Linux supports symbolic links, also called soft links or symlinks. A hard link is a pointer to a file (the directory entry points to the inode),

www.it-ebooks.info

Links 217

whereas a symbolic link is an indirect pointer to a file (the directory entry contains the pathname of the pointed-to file—a pointer to the hard link to the file).
Advantages of symbolic links

Symbolic links were developed because of the limitations inherent in hard links. You cannot create a hard link to a directory, but you can create a symbolic link to a directory. In many cases the Linux file hierarchy encompasses several filesystems. Because each filesystem keeps separate control information (that is, separate inode tables or filesystem structures) for the files it holds, it is not possible to create hard links between files in different filesystems. A symbolic link can point to any file, regardless of where it is located in the file structure, but a hard link to a file must be in the same filesystem as the other hard link(s) to the file. When you create links only among files in your home directory, you will not notice this limitation. A major advantage of a symbolic link is that it can point to a non-existent file. This ability is useful if you need a link to a file that is periodically removed and recreated. A hard link keeps pointing to a “removed” file, which the link keeps alive even after a new file is created. In contrast, a symbolic link always points to the newly created file and does not interfere when you delete the old file. For example, a symbolic link could point to a file that gets checked in and out under a source code control system, a .o file that is re-created by the C compiler each time you run make, or a log file that is repeatedly archived. Although they are more general than hard links, symbolic links have some disadvantages. Whereas all hard links to a file have equal status, symbolic links do not have the same status as hard links. When a file has multiple hard links, it is analogous to a person having multiple full legal names, as many married women do. In contrast, symbolic links are analogous to nicknames. Anyone can have one or more nicknames, but these nicknames have a lesser status than legal names. The following sections describe some of the peculiarities of symbolic links.

ln: Creates Symbolic Links
The ln utility with the ––symbolic (or –s) option creates a symbolic link. The following example creates a symbolic link /tmp/s3 to the file sum in Max’s home directory. When you use an ls –l command to look at the symbolic link, ls displays the name of the link and the name of the file it points to. The first character of the listing is l (for link).
$ ln --symbolic /home/max/sum /tmp/s3 $ ls -l /home/max/sum /tmp/s3 -rw-rw-r--. 1 max pubs 38 06-12 09:51 /home/max/sum lrwxrwxrwx. 1 max pubs 14 06-12 09:52 /tmp/s3 -> /home/max/sum $ cat /tmp/s3 This is sum.

The sizes and times of the last modifications of the two files are different. Unlike a hard link, a symbolic link to a file does not have the same status information as the file itself.

www.it-ebooks.info

218 Chapter 6 The Linux Filesystem

You can also use ln to create a symbolic link to a directory. When you use the ––symbolic option, ln works as expected whether the file you are creating a link to is an ordinary file or a directory.

Use absolute pathnames with symbolic links caution Symbolic links are literal and are not aware of directories. A link that points to a relative pathname, which includes simple filenames, assumes the relative pathname is relative to the directory that the link was created in (not the directory the link was created from). In the following example, the link points to the file named sum in the /tmp directory. Because no such file exists, cat gives an error message:
$ pwd /home/max $ ln --symbolic sum /tmp/s4 $ ls -l sum /tmp/s4 lrwxrwxrwx. 1 max pubs 3 06-12 10:13 /tmp/s4 -> sum -rw-rw-r--. 1 max pubs 38 06-12 09:51 sum $ cat /tmp/s4 cat: /tmp/s4: No such file or directory

optional

cd and Symbolic Links
When you use a symbolic link as an argument to cd to change directories, the results can be confusing, particularly if you did not realize that you were using a symbolic link. If you use cd to change to a directory that is represented by a symbolic link, the pwd shell builtin (page 249) lists the name of the symbolic link. The pwd utility (/bin/pwd) lists the name of the linked-to directory, not the link, regardless of how you got there.
$ ln -s /home/max/grades /tmp/grades.old $ pwd /home/max $ cd /tmp/grades.old $ pwd /tmp/grades.old $ /bin/pwd /home/max/grades

When you change directories back to the parent, you end up in the directory holding the symbolic link:
$ cd .. $ pwd /tmp $ /bin/pwd /tmp

rm: Removes a Link
When you create a file, there is one hard link to it. You can then delete the file or, using Linux terminology, remove the link with the rm utility. When you remove the

www.it-ebooks.info

Chapter Summary 219

last hard link to a file, you can no longer access the information stored there, and the operating system releases the space the file occupied on the disk for use by other files. This space is released even if symbolic links to the file remain. When there is more than one hard link to a file, you can remove a hard link and still access the file from any remaining link. Unlike DOS and Windows, Linux does not provide an easy way to undelete a file once you have removed it. A skilled hacker, however, can sometimes piece the file together with time and effort. When you remove all hard links to a file, you will not be able to access the file through a symbolic link. In the following example, cat reports that the file total does not exist because it is a symbolic link to a file that has been removed:
$ ls -l sum -rw-r--r--. 1 max pubs 981 05-24 11:05 sum $ ln -s sum total $ rm sum $ cat total cat: total: No such file or directory $ ls -l total lrwxrwxrwx. 1 max pubs 6 05-24 11:09 total -> sum

When you remove a file, be sure to remove all symbolic links to it. Remove a symbolic link in the same way you remove other files:
$ rm total

Chapter Summary
Linux has a hierarchical, or treelike, file structure that makes it possible to organize files so you can find them quickly and easily. The file structure contains directory files and ordinary files. Directories contain other files, including other directories; ordinary files generally contain text, programs, or images. The ancestor of all files is the root directory and is represented by / standing alone or at the left end of a pathname. Most Linux filesystems support 255-character filenames. Nonetheless, it is a good idea to keep filenames simple and intuitive. Filename extensions can help make filenames more meaningful. When you are logged in, you are always associated with a working directory. Your home directory is the working directory from the time you log in until you use cd to change directories. An absolute pathname starts with the root directory and contains all the filenames that trace a path to a given file. The pathname starts with a slash, representing the root directory, and contains additional slashes following each of the directories in the path, except for the last directory in the case of a path that points to a directory file. A relative pathname is similar to an absolute pathname but traces the path starting from the working directory. A simple filename is the last element of a pathname and is a form of a relative pathname; it represents a file in the working directory.

www.it-ebooks.info

220 Chapter 6 The Linux Filesystem

A Linux filesystem contains many important directories, including /usr/bin, which stores most of the Linux utilities, and /dev, which stores device files, many of which represent physical pieces of hardware. An important standard file is /etc/passwd; it contains information about users, such as a user’s ID and full name. Among the attributes associated with each file are access permissions. They determine who can access the file and how the file may be accessed. Three groups of users can potentially access the file: the owner, the members of a group, and all other users. An ordinary file can be accessed in three ways: read, write, and execute. The ls utility with the –l option displays these permissions. For directories, execute access is redefined to mean that the directory can be searched. The owner of a file or a user working with root privileges can use the chmod utility to change the access permissions of a file. This utility specifies read, write, and execute permissions for the file’s owner, the group, and all other users on the system. ACLs (Access Control Lists) provide finer-grained control over which users can access specific directories and files than do traditional Linux permissions. Using ACLs you can specify the ways in which each of several users can access a directory or file. Few utilities preserve ACLs when working with files. An ordinary file stores user data, such as textual information, programs, or images. A directory is a standard-format disk file that stores information, including names, about ordinary files and other directory files. An inode is a data structure, stored on disk, that defines a file’s existence and is identified by an inode number. A directory relates each of the filenames it stores to an inode. A link is a pointer to a file. You can have several links to a file so you can share the file with other users or have the file appear in more than one directory. Because only one copy of a file with multiple links exists, changing the file through any one link causes the changes to appear in all the links. Hard links cannot link directories or span filesystems, whereas symbolic links can. Table 6-3 summarizes the utilities introduced in this chapter.

Table 6-3 Utilities introduced in Chapter 6
Utility
cd chmod getfacl ln mkdir pwd rmdir setfacl

Function
Associates you with another working directory (page 196) Changes access permissions on a file (page 203) Displays a file’s ACL (page 209) Makes a link to an existing file (page 214) Creates a directory (page 194) Displays the pathname of the working directory (page 190) Deletes a directory (page 197) Modifies a file’s ACL (page 209)

www.it-ebooks.info

Exercises

221

Exercises
1. Is each of the following an absolute pathname, a relative pathname, or a simple filename? a. milk_co b. correspond/business/milk_co c. /home/max d. /home/max/literature/promo e. .. f. letter.0210 2. List the commands you can use to perform these operations: a. Make your home directory the working directory b. Identify the working directory 3. If the working directory is /home/max with a subdirectory named literature, give three sets of commands you can use to create a subdirectory named classics under literature. Also give several sets of commands you can use to remove the classics directory and its contents. 4. The df utility displays all mounted filesystems along with information about each. Use the df utility with the –h (human-readable) option to answer the following questions. a. How many filesystems are mounted on your Linux system? b. Which filesystem stores your home directory? c. Assuming your answer to exercise 4a is two or more, attempt to create a hard link to a file on another filesystem. What error message do you get? What happens when you attempt to create a symbolic link to the file instead? 5. Suppose you have a file that is linked to a file owned by another user. How can you ensure that changes to the file are no longer shared? 6. You should have read permission for the /etc/passwd file. To answer the following questions, use cat or less to display /etc/passwd. Look at the fields of information in /etc/passwd for the users on the local system. a. Which character is used to separate fields in /etc/passwd? b. How many fields are used to describe each user? c. How many users are on the local system? d. How many different login shells are in use on your system? (Hint: Look at the last field.)

www.it-ebooks.info

222 Chapter 6 The Linux Filesystem

e. The second field of /etc/passwd stores user passwords in encoded form. If the password field contains an x, your system uses shadow passwords and stores the encoded passwords elsewhere. Does your system use shadow passwords? 7. If /home/zach/draft and /home/max/letter are links to the same file and the following sequence of events occurs, what will be the date in the opening of the letter? a. Max gives the command vim letter. b. Zach gives the command vim draft. c. Zach changes the date in the opening of the letter to January 31, writes the file, and exits from vim. d. Max changes the date to February 1, writes the file, and exits from vim. 8. Suppose a user belongs to a group that has all permissions on a file named jobs_list, but the user, as the owner of the file, has no permissions. Describe which operations, if any, the user/owner can perform on jobs_list. Which command can the user/owner give that will grant the user/owner all permissions on the file? 9. Does the root directory have any subdirectories you cannot search as an ordinary user? Does the root directory have any subdirectories you cannot read as a regular user? Explain. 10. Assume you are given the directory structure shown in Figure 6-2 on page 187 and the following directory permissions: d--x--x--drwxr-xr-x 3 zach pubs 512 2010-03-10 15:16 business 2 zach pubs 512 2010-03-10 15:16 business/milk_co

For each category of permissions—owner, group, and other—what happens when you run each of the following commands? Assume the working directory is the parent of correspond and that the file cheese_co is readable by everyone. a. cd correspond/business/milk_co b. ls –l correspond/business c. cat correspond/business/cheese_co

Advanced Exercises
11. What is an inode? What happens to the inode when you move a file within a filesystem? 12. What does the .. entry in a directory point to? What does this entry point to in the root (/) directory?

www.it-ebooks.info

Advanced Exercises 223

13. How can you create a file named –i? Which techniques do not work, and why do they not work? How can you remove the file named –i? 14. Suppose the working directory contains a single file named andor. What error message do you get when you run the following command line?
$ mv andor and\/or

Under what circumstances is it possible to run the command without producing an error? 15. The ls –i command displays a filename preceded by the inode number of the file (page 216). Write a command to output inode/filename pairs for the files in the working directory, sorted by inode number. (Hint: Use a pipe.) 16. Do you think the system administrator has access to a program that can decode user passwords? Why or why not? (See exercise 6.) 17. Is it possible to distinguish a file from a hard link to a file? That is, given a filename, can you tell whether it was created using an ln command? Explain. 18. Explain the error messages displayed in the following sequence of commands:
$ ls -l total 1 drwxrwxr-x. 2 max pubs 1024 03-02 17:57 dirtmp $ ls dirtmp $ rmdir dirtmp rmdir: dirtmp: Directory not empty $ rm dirtmp/* rm: No match.

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

7
The Shell
C 7 hapter7

In This Chapter
The Command Line . . . . . . . . . . . . 226 Standard Input and Standard Output . . . . . . . . . . . . . . . . . . . . 232 Redirection . . . . . . . . . . . . . . . . . . 234 Pipes . . . . . . . . . . . . . . . . . . . . . . . 239 Running a Command in the Background . . . . . . . . . . . . . . . . 242 kill: Aborting a Background Job . . 243 Filename Generation/Pathname Expansion . . . . . . . . . . . . . . . . . 244 Builtins . . . . . . . . . . . . . . . . . . . . . 249

Objectives
After reading this chapter you should be able to: Understand command line syntax and run commands with options and arguments Explain how the shell interprets the command line syntax Understand the purpose of the PATH variable Redirect, overwriting or appending, output of a command to a file Obtain input for a command from a file Connect commands using a pipe Run commands in the background Use special characters as wildcards to generate filenames Explain the difference between an executable file and a shell builtin command
225

www.it-ebooks.info

226 Chapter 7 The Shell

This chapter takes a close look at the shell and explains how to use some of its features. For example, it discusses command-line syntax and describes how the shell processes a command line and initiates execution of a program. This chapter also explains how to redirect input to and output from a command, construct pipes and filters on the command line, and run a command in the background. The final section covers filename expansion and explains how you can use this feature in your everyday work. The exact wording of the shell output differs from shell to shell: What the shell you are using displays might differ slightly from what appears in this book. Refer to Chapter 9 for more information on bash (the default shell under Fedora/RHEL) and to Chapter 27 for information on writing and executing bash shell scripts.

The Command Line
The shell executes a program when you enter a command in response to its prompt. For example, when you give the ls command, the shell executes the utility program named ls. You can cause the shell to execute other types of programs—such as shell scripts, application programs, and programs you have written—in the same way. The line that contains the command, including any arguments, is called the command line.
Command

This book uses the term command to refer to both the characters you type on the command line and the program that action invokes.

Syntax
Command-line syntax dictates the ordering and separation of the elements on a command line. When you press the RETURN key after entering a command, the shell scans the command line for proper syntax. The syntax for a basic command line is command [arg1] [arg2] ... [argn] RETURN One or more SPACEs must separate elements on the command line. The command is the name of the command, arg1 through argn are arguments, and RETURN is the keystroke that terminates all command lines. The brackets in the command-line syntax indicate that the arguments they enclose are optional. Not all commands require arguments: Some commands do not allow arguments; other commands allow a variable number of arguments; and still others require a specific number of arguments. Options, a special kind of argument, are usually preceded by one or two hyphens (also called a dash or minus sign: –).

Command Name
Usage message

Some useful Linux command lines consist of only the name of the command without any arguments. For example, ls by itself lists the contents of the working directory. Commands that require arguments typically give a short error message, called a usage message, when you use them without arguments, with incorrect arguments, or with the wrong number of arguments. For example, the mkdir (make directory) utility requires an argument that specifies the name of the directory you want it to create. Without this argument, it displays a usage message (operand is another term for argument):

www.it-ebooks.info

The Command Line 227

$ ls hold mark names oldstuff temp zach house max office personal test $ ls -r zach temp oldstuff names mark hold test personal office max house $ ls -x hold house mark max names office oldstuff personal temp test zach $ ls -rx zach test temp personal oldstuff office names max mark house hold

Figure 7-1

Using options

$ mkdir mkdir: missing operand Try 'mkdir --help' for more information.

Arguments
Token

On the command line each sequence of nonblank characters is called a token or word. An argument is a token, such as a filename, string of text, number, or other object that a command acts on. For example, the argument to a vim or emacs command is the name of the file you want to edit. The following command line uses cp to copy the file named temp to tempcopy:
$ cp temp tempcopy

Arguments are numbered starting with the command itself, which is argument zero. In this example, cp is argument zero, temp is argument one, and tempcopy is argument two. The cp utility requires at least two arguments on the command line. Argument one is the name of an existing file. Argument two is the name of the file that cp is creating or overwriting. Here the arguments are not optional; both arguments must be present for the command to work. When you do not supply the right number or kind of arguments, cp displays a usage message. Try typing cp and then pressing RETURN.

Options
An option is an argument that modifies the effects of a command. You can frequently specify more than one option, modifying the command in several different ways. Options are specific to and interpreted by the program that the command line calls, not by the shell. By convention options are separate arguments that follow the name of the command and usually precede other arguments, such as filenames. Many utilities require options to be prefixed with a single hyphen. However, this requirement is specific to the utility and not the shell. GNU program options are frequently prefixed with two hyphens. For example, ––help generates a (sometimes extensive) usage message. Figure 7-1 first shows the output of an ls command without any options. By default ls lists the contents of the working directory in alphabetical order, vertically sorted

www.it-ebooks.info

228 Chapter 7 The Shell

in columns. Next the –r (reverse order; because this is a GNU utility, you can also specify ––reverse) option causes the ls utility to display the list of files in reverse alphabetical order, still sorted in columns. The –x option causes ls to display the list of files in horizontally sorted rows.
Combining options

When you need to use several options, you can usually group multiple single-letter options into one argument that starts with a single hyphen; do not put SPACEs between the options. You cannot combine options that are preceded by two hyphens in this way. Specific rules for combining options depend on the program you are running. Figure 7-1 shows both the –r and –x options with the ls utility. Together these options generate a list of filenames in horizontally sorted columns in reverse alphabetical order. Most utilities allow you to list options in any order; thus ls –xr produces the same results as ls –rx. The command ls –x –r also generates the same list. Some utilities have options that themselves require arguments. For example, the gcc utility (C compiler) has a –o (output) option that must be followed by the name you want to give the executable file that gcc generates. Typically an argument to an option is separated from its option letter by a SPACE:
$ gcc -o prog prog.c

Option arguments

Displaying readable file sizes: the –h option tip Most utilities that report on file sizes specify the size of a file in bytes. Bytes work well when you are dealing with smaller files, but the numbers can be difficult to read when you are working with file sizes that are measured in megabytes or gigabytes. Use the –h (or ––human-readable) option to display file sizes in kilo-, mega-, and gigabytes. Experiment with the df –h (disk free) and ls –lh commands.
Arguments that start with a hyphen

Another convention allows utilities to work with arguments, such as filenames, that start with a hyphen. If a file’s name is –l, the following command is ambiguous:
$ ls -l

This command could be a request to display a long listing of all files in the working directory (–l option) or a request for a listing of the file named –l. The ls utility interprets it as the former. Avoid creating files whose names begin with hyphens. If you do create them, many utilities follow the convention that a –– argument (two consecutive hyphens) indicates the end of the options (and the beginning of the arguments). To disambiguate the command, you can type
$ ls -- -l

Using two consecutive hyphens to indicate the end of the options is a convention, not a hard-and-fast rule, and a number of utilities do not follow it (e.g., find). Following this convention makes it easier for users to work with your program. When you write shell programs that require options, follow this convention. For utilities that do not follow this convention, there are other ways to specify a filename that begins with a hyphen. You can use a period to refer to the working directory and a slash to indicate that the following filename refers to a file in the working directory:

www.it-ebooks.info

The Command Line 229
$ ls ./-l

You can also specify the absolute pathname of the file:
$ ls /home/max/-l

Processing the Command Line
As you enter a command line, the Linux tty device driver (part of the Linux kernel) examines each character to see whether it must take immediate action. When you press CONTROL-H (to erase a character) or CONTROL-U (to kill a line), the device driver immediately adjusts the command line as required; the shell never sees the character(s) you erased or the line you killed. Often a similar adjustment occurs when you press CONTROL-W (to erase a word). When the character you entered does not require immediate action, the device driver stores the character in a buffer and waits for additional characters. When you press RETURN, the device driver passes the command line to the shell for processing.

The ––help option tip Many utilities display a (sometimes extensive) help message when you call them with an argument of ––help. All utilities developed by the GNU Project (page 3) accept this option. Following is the help message displayed by the bzip2 compression utility (page 160).
$ bzip2 --help bzip2, a block-sorting file compressor. Version 1.0.6, 6-Sept-2010.

usage: bunzip2 [flags and input files in any order] -h -d -z -k -f ... If --help --decompress --compress --keep --force print this message force decompression force compression keep (don't delete) input files overwrite existing output files

invoked as 'bzip2', default action is to compress. as 'bunzip2', default action is to decompress. as 'bzcat', default action is to decompress to stdout.

...

Parsing the command line

When the shell processes a command line, it looks at the line as a whole and parses (breaks) it into its component parts (Figure 7-2, next page). Next the shell looks for the name of the command. Usually the name of the command is the first item on the command line after the prompt (argument zero). The shell takes the first characters on the command line up to the first blank (TAB or SPACE) and then looks for a command with that name. The command name (the first token) can be specified on the command line either as a simple filename or as a pathname. For example, you can call the ls command in either of the following ways:
$ ls $ /bin/ls

www.it-ebooks.info

230 Chapter 7 The Shell

Get first word and save as command name

NEWLINE

no

Get more of the command line

Execute program

yes

Does program exist?

no

Display not found

Issue prompt

Figure 7-2

Processing the command line

optional The shell does not require the name of the program to appear first on the command line. Thus you can structure a command line as follows:
$ >bb ) instructs the shell to redirect the output of a command to the specified file instead of to the screen (Figure 7-6). The format of a command line that redirects output is command [arguments] > filename where command is any executable program (such as an application program or a utility), arguments are optional arguments, and filename is the name of the ordinary file the shell redirects the output to. Figure 7-7 uses cat to demonstrate output redirection. This figure contrasts with Figure 7-5, where standard input and standard output are associated with the

File Standard input
She ll

Command

Figure 7-6

Redirecting standard output

www.it-ebooks.info

Shell

Standard output

Standard Input and Standard Output

235

$ cat > sample.txt This text is being entered at the keyboard and cat is copying it to a file. Press CONTROL-D to indicate the end of file.
CONTROL-D

$

Figure 7-7

cat with its output redirected

keyboard and screen. The input in Figure 7-7 comes from the keyboard. The redirect output symbol on the command line causes the shell to associate cat’s standard output with the sample.txt file specified following this symbol.

Redirecting output can destroy a file I caution Use caution when you redirect output to a file. If the file exists, the shell will overwrite it and destroy its contents. For more information see the tip “Redirecting output can destroy a file II” on page 237.

After giving the command and typing the text shown in Figure 7-7, the sample.txt file contains the text you entered. You can use cat with an argument of sample.txt to display this file. The next section shows another way to use cat to display the file. Figure 7-7 shows that redirecting standard output from cat is a handy way to create a file without using an editor. The drawback is that once you enter a line and press RETURN, you cannot edit the text. While you are entering a line, the erase and kill keys work to delete text on that line. This procedure is useful for creating short, simple files. Figure 7-8 shows how to run cat and use the redirect output symbol to catenate (join one after the other—the derivation of the name of the cat utility) several files into one larger file. The first three commands display the contents of three files: stationery, tape, and pens. The next command shows cat with three filenames as arguments. When you call it with more than one filename, cat copies the files, one at a time, to
$ cat stationery 2,000 sheets letterhead ordered: $ cat tape 1 box masking tape ordered: 5 boxes filament tape ordered: $ cat pens 12 doz. black pens ordered:

October 7 October 14 October 28 October 4

$ cat stationery tape pens > supply_orders $ cat supply_orders 2,000 sheets letterhead ordered: 1 box masking tape ordered: 5 boxes filament tape ordered: 12 doz. black pens ordered: $

October October October October

7 14 28 4

Figure 7-8

Using cat to catenate files

www.it-ebooks.info

236 Chapter 7 The Shell

File Shell
She ll

Standard input

Standard output

Command

Figure 7-9

Redirecting standard input

standard output. This command redirects standard output to the file supply_orders. The final cat command shows that supply_orders contains the contents of the three of the original files.

Redirecting Standard Input
Just as you can redirect standard output, so you can redirect standard input. The redirect input symbol ( orange cat: orange: input file is output file

Although cat displays an error message, the shell destroys the contents of the existing orange file. The new orange file will have the same contents as pear because the first action the shell takes when it sees the redirection symbol (>) is to remove the contents of the original orange file. If you want to catenate two files into one, use cat to put the two files into a temporary file and then use mv to rename the temp file:
$ cat orange pear > temp $ mv temp orange

What happens in the next example can be even worse. The user giving the command wants to search through files a, b, and c for the word apple and redirect the output from grep (page 152) to the file a.output. Unfortunately the user enters the filename as a output, omitting the period and inserting a SPACE in its place:
$ grep apple a b c > a output grep: output: No such file or directory

The shell obediently removes the contents of a and then calls grep. The error message could take a moment to appear, giving you a sense that the command is running correctly. Even after you see the error message, it might take a while to realize that you have destroyed the contents of a.

noclobber: Avoids Overwriting Files
The shell provides the noclobber feature that prevents overwriting a file using redirection. Enable this feature by setting noclobber using the command set –o noclobber. The same command with +o unsets noclobber. With noclobber set, if you redirect output to an existing file, the shell displays an error message and does not execute the command. The following example creates a file using touch, sets noclobber, attempts to redirect the output from echo to the newly created file, unsets noclobber, and performs the same redirection:
$ touch tmp $ set -o noclobber $ echo "hi there" > tmp -bash: tmp: cannot overwrite existing file $ set +o noclobber $ echo "hi there" > tmp

You can override noclobber by putting a pipe symbol after the redirect symbol (>|). In the following example, the user creates a file by redirecting the output of date. Next the user sets the noclobber variable and redirects output to the same file again. The shell displays an error message. Then the user places a pipe symbol after the redirect symbol, and the shell allows the user to overwrite the file.
$ date > tmp2 $ set -o noclobber $ date > tmp2 -bash: tmp2: cannot overwrite existing file $ date >| tmp2

www.it-ebooks.info

238 Chapter 7 The Shell

Appending Standard Output to a File
The append output symbol (>>) causes the shell to add new information to the end of a file, leaving existing information intact. This symbol provides a convenient way of catenating two files into one. The following commands demonstrate the action of the append output symbol. The second command accomplishes the catenation described in the preceding caution box:
$ cat orange this is orange $ cat pear >> orange $ cat orange this is orange this is pear

The first command displays the contents of the orange file. The second command appends the contents of the pear file to the orange file. The final command displays the result.

Do not trust noclobber caution Appending output is simpler than the two-step procedure described in the preceding caution box, but you must be careful to include both greater than signs. If you accidentally use only one greater than sign and the noclobber feature is not set, the shell will overwrite the orange file. Even if you have the noclobber feature turned on, it is a good idea to keep backup copies of the files you are manipulating in case you make a mistake. Although it protects you from overwriting a file using redirection, noclobber does not stop you from overwriting a file using cp or mv. These utilities include the –i (interactive) option that helps protect you from this type of mistake by verifying your intentions when you try to overwrite a file. For more information see the tip “cp can destroy a file” on page 150.

Figure 7-11 shows how to create a file that contains the date and time (the output from date), followed by a list of who is logged in (the output from who). The first command in the example redirects the output from date to the file named whoson. Then cat displays the file. The next command appends the output from who to the whoson file. Finally cat displays the file containing the output of both utilities.
$ date > whoson $ cat whoson Sun Mar 27 14:31:18 PST 2011 $ who >> whoson $ cat whoson Sun Mar 27 14:31:18 PST 2011 sam tty1 2011-03-27 max pts/4 2011-03-27 max pts/5 2011-03-27 zach pts/7 2011-03-26

05:00(:0) 12:23(:0.0) 12:33(:0.0) 08:45 (172.16.192.1)

Figure 7-11

Redirecting and appending output

www.it-ebooks.info

Standard Input and Standard Output

239

/dev/null: Making Data Disappear
The /dev/null device is a data sink, commonly referred to as a bit bucket. You can redirect output you do not want to keep or see to /dev/null, and the output will disappear without a trace:
$ echo "hi there" > /dev/null $

When you read from /dev/null, you get a null string. The following command truncates the file named messages to zero length while preserving the ownership and permissions of the file:
$ ls -lh messages -rw-rw-r--. 1 sam pubs 125K 03-16 14:30 messages $ cat /dev/null > messages $ ls -lh messages -rw-rw-r--. 1 sam pubs 0 03-16 14:32 messages

See also page 503.

Pipes
The shell uses a pipe to connect standard output of one command to standard input of another command. A pipe (sometimes referred to as a pipeline) has the same effect as redirecting standard output of one command to a file and then using that file as standard input to another command. A pipe does away with separate commands and the intermediate file. The symbol for a pipe is a vertical bar ( |). The syntax of a command line using a pipe is command_a [arguments] | command_b [arguments] The preceding command line uses a pipe on a single command line to generate the same result as the following three command lines: command_a [arguments] > temp command_b [arguments] < temp rm temp In the preceding sequence of commands, the first line redirects standard output from command_a to an intermediate file named temp. The second line redirects standard input for command_b to come from temp. The final line deletes temp. The command using a pipe is not only easier to type but is generally more efficient because it does not create a temporary file. tr You can use a pipe with any of the Linux utilities that accept input either from a file specified on the command line or from standard input. You can also use pipes with commands that accept input only from standard input. For example, the tr (translate) utility takes its input from standard input only. In its simplest usage tr has the following format: tr string1 string2

www.it-ebooks.info

240 Chapter 7 The Shell

$ ls > temp $ lpr temp $ rm temp

or
$ ls | lpr

Figure 7-12

A pipe

The tr utility accepts input from standard input and looks for characters that match one of the characters in string1. Upon finding a match, it translates the matched character in string1 to the corresponding character in string2. (The first character in string1 translates into the first character in string2, and so forth.) The tr utility sends its output to standard output. In both of the following examples, tr displays the contents of the abstract file with the letters a, b, and c translated into A, B, and C, respectively:
$ cat abstract | tr abc ABC $ tr abc ABC < abstract

The tr utility does not change the contents of the original file; it cannot change the original file because it does not “know” the source of its input. lpr The lpr (line printer) utility also accepts input from either a file or standard input. When you type the name of a file following lpr on the command line, it places that file in the print queue. When you do not specify a filename on the command line, lpr takes input from standard input. This feature enables you to use a pipe to redirect input to lpr. The first set of commands in Figure 7-12 shows how you can use ls and lpr with an intermediate file (temp) to send a list of the files in the working directory to the printer. If the temp file exists, the first command overwrites its contents. The second set of commands uses a pipe to send the same list (with the exception of temp) to the printer. The commands in Figure 7-13 redirect the output from the who utility to temp and then display this file in sorted order. The sort utility (page 154) takes its input from the file specified on the command line or, when a file is not specified, from standard input; it sends its output to standard output. The sort command line in Figure 7-13 takes its input from standard input, which is redirected ( temp $ sort < temp max pts/4 max pts/5 sam tty1 zach pts/7 $ rm temp

2011-03-24 2011-03-24 2011-03-24 2011-03-23

12:23 12:33 05:00 08:45

Figure 7-13

Using a temporary file to store intermediate results

The grep utility displays the line containing the string you specify—sam in the following example:
$ who | grep 'sam' sam tty1 less and more 2011-03-24 05:00

Another way of handling output that is too long to fit on the screen, such as a list of files in a crowded directory, is to use a pipe to send the output through less or more (both on page 149).
$ ls | less

The less utility displays text one screen at a time. To view another screen, press the SPACE bar. To view one more line, press RETURN. Press h for help and q to quit. Some utilities change the format of their output when you redirect it. Compare the output of ls by itself and when you send it through a pipe to less.

Filters
A filter is a command that processes an input stream of data to produce an output stream of data. A command line that includes a filter uses a pipe to connect standard output of one command to the filter’s standard input. Another pipe connects the filter’s standard output to standard input of another command. Not all utilities can be used as filters. In the following example, sort is a filter, taking standard input from standard output of who and using a pipe to redirect standard output to standard input of lpr. This command line sends the sorted output of who to the printer:
$ who | sort | lpr

The preceding example demonstrates the power of the shell combined with the versatility of Linux utilities. The three utilities who, sort, and lpr were not specifically
$ who | sort max pts/4 max pts/5 sam tty1 zach pts/7

2011-03-24 2011-03-24 2011-03-24 2011-03-23

12:23 12:33 05:00 08:45

Figure 7-14

A pipe doing the work of a temporary file

www.it-ebooks.info

242 Chapter 7 The Shell

$ who | tee who.out | grep sam sam tty1 2011-03-24 $ cat who.out sam tty1 2011-03-24 max pts/4 2011-03-24 max pts/5 2011-03-24 zach pts/7 2011-03-23

05:00 05:00 12:23 12:33 08:45

Figure 7-15

tee sends its output to a file and to standard output

designed to work with each other, but they all use standard input and standard output in the conventional way. By using the shell to handle input and output, you can piece standard utilities together on the command line to achieve the results you want.

tee: Sends Output in Two Directions
The tee utility copies its standard input both to a file and to standard output. This utility is aptly named: It takes a single stream of input and sends the output in two directions. In Figure 7-15 the output of who is sent via a pipe to standard input of tee. The tee utility saves a copy of standard input in a file named who.out and also sends a copy to standard output. Standard output of tee goes via a pipe to standard input of grep, which displays only those lines containing the string sam. Use the –a (append) option to cause tee to append to a file instead of overwriting it.

Running a Command in the Background
Foreground

All commands up to this point have been run in the foreground. When you run a command in the foreground, the shell waits for it to finish before displaying another prompt and allowing you to continue. When you run a command in the background, you do not have to wait for the command to finish before running another command. A job is a series of one or more commands that can be connected by pipes. You can have only one foreground job in a window or on a screen, but you can have many background jobs. By running more than one job at a time, you are using one of Linux’s important features: multitasking. Running a command in the background can be useful when the command will run for a long time and does not need supervision. It leaves the screen free so you can use it for other work. Alternatively, when you are using a GUI, you can open another window to run another job. To run a command in the background, type an ampersand (&) just before the RETURN that ends the command line. The shell assigns a small number to the job and displays this job number between brackets. Following the job number, the shell displays the process identification (PID) number—a larger number assigned by the operating system. Each of these numbers identifies the command running in the background. The shell then displays another prompt, and you can enter another command. When the

Jobs

Job number, PID number

www.it-ebooks.info

Running a Command in the Background

243

background job finishes, the shell displays a message giving both the job number and the command line used to run the command. The next example runs in the background; it sends the output of ls through a pipe to lpr, which sends it to the printer.
$ ls -l | lpr & [1] 22092 $

The [1] following the command line indicates that the shell has assigned job number 1 to this job. The 22092 is the PID number of the first command in the job. When this background job completes execution, you see the message
[1]+ Done ls -l | lpr

(In place of ls –l, the shell might display something similar to ls ––color=auto –l. This difference is due to the fact that ls is aliased [page 334] to ls ––color=auto.)

Moving a Job from the Foreground to the Background
CONTROL-Z

and bg

You can suspend a foreground job (stop it from running) by pressing the suspend key, usually CONTROL-Z . The shell then stops the process and disconnects standard input from the keyboard. You can put a suspended job in the background and restart it by using the bg command followed by the job number. You do not need to specify the job number when there is only one suspended job. Only the foreground job can take input from the keyboard. To connect the keyboard to a program running in the background, you must bring it to the foreground. To do so, type fg without any arguments when only one job is in the background. When more than one job is in the background, type fg, or a percent sign (%), followed by the number of the job you want to bring into the foreground. The shell displays the command you used to start the job (promptme in the following example), and you can enter any input the program requires to continue: bash $ fg 1 promptme

fg

Redirect the output of a job you run in the background to keep it from interfering with whatever you are working on in the foreground (on the screen). Refer to “Separating and Grouping Commands” on page 292 for more detail about background tasks.

kill: Aborting a Background Job
The interrupt key (usually CONTROL-C ) cannot abort a background process; you must use kill (page 470) for this purpose. Follow kill on the command line with either the PID number of the process you want to abort or a percent sign (%) followed by the job number.

www.it-ebooks.info

244 Chapter 7 The Shell
Determining the PID of a process using ps

If you forget a PID number, you can use the ps (process status) utility (page 317) to display it. The following example runs a tail –f outfile command (the –f [follow] option causes tail to watch outfile and display new lines as they are written to the file) as a background job, uses ps to display the PID number of the process, and aborts the job with kill:
$ tail -f outfile & [1] 18228 $ ps | grep tail 18228 pts/4 00:00:00 tail $ kill 18228 [1]+ Terminated $

tail -f outfile

Determining the number of a job using jobs

If you forget a job number, you can use the jobs command to display a list of job numbers. The next example is similar to the previous one except it uses the job number instead of the PID number to identify the job to be killed. Sometimes the message saying the job is terminated does not appear until you press RETURN after the RETURN that executes the kill command.
$ tail -f outfile & [1] 18236 $ bigjob & [2] 18237 $ jobs [1]- Running [2]+ Running $ kill %1 $ RETURN [1]- Terminated $

tail -f outfile & bigjob &

tail -f outfile

Filename Generation/Pathname Expansion
Wildcards, globbing

When you give the shell abbreviated filenames that contain special characters, also called metacharacters, the shell can generate filenames that match the names of existing files. These special characters are also referred to as wildcards because they act much as the jokers do in a deck of cards. When one of these characters appears in an argument on the command line, the shell expands that argument in sorted order into a list of filenames and passes the list to the program called by the command line. Filenames that contain these special characters are called ambiguous file references because they do not refer to one specific file. The process that the shell performs on these filenames is called pathname expansion or globbing. Ambiguous file references refer to a group of files with similar names quickly, saving the effort of typing the names individually. They can also help find a file whose name you do not remember in its entirety. If no filename matches the ambiguous file reference, the shell generally passes the unexpanded reference—special characters and all—to the command.

www.it-ebooks.info

Filename Generation/Pathname Expansion

245

The ? Special Character
The question mark (?) is a special character that causes the shell to generate filenames. It matches any single character in the name of an existing file. The following command uses this special character in an argument to the lpr utility:
$ lpr memo?

The shell expands the memo? argument and generates a list of files in the working directory that have names composed of memo followed by any single character. The shell then passes this list to lpr. The lpr utility never “knows” the shell generated the filenames it was called with. If no filename matches the ambiguous file reference, the shell passes the string itself (memo?) to lpr or, if it is set up to do so, passes a null string (see nullglob on page 343). The following example uses ls first to display the names of all files in the working directory and then to display the filenames that memo? matches:
$ ls mem memo memo12 memo5 memo9 memoa memomax memos newmemo5

$ ls memo? memo5 memo9

memoa

memos

The memo? ambiguous file reference does not match mem, memo, memo12, memomax, or newmemo5. You can also use a question mark in the middle of an ambiguous file reference:
$ ls 7may4report may14report may4report may4report.79 mayqreport mayreport may_report may.report

$ ls may?report may4report mayqreport echo

may_report

may.report

You can use echo and ls to practice generating filenames. The echo utility displays the arguments that the shell passes to it:
$ echo may?report may4report mayqreport may_report may.report

The shell first expands the ambiguous file reference into a list of all files in the working directory that match the string may?report. It then passes this list to echo, just as though you had entered the list of filenames as arguments to echo. The echo utility displays the list of filenames. A question mark does not match a leading period (one that indicates a hidden filename; see page 190). When you want to match filenames that begin with a period, you must explicitly include the period in the ambiguous file reference.

www.it-ebooks.info

246 Chapter 7 The Shell

The * Special Character

The asterisk (*) performs a function similar to that of the question mark but matches any number of characters, including zero characters, in a filename. The following example first shows all files in the working directory and then shows commands that display all the filenames that begin with the string memo, end with the string mo, and contain the string alx:
$ ls amemo mem memalx memo memo.0612 memoa memoalx.0620 memoalx.keep memorandum memosally sallymemo user.memo

$ echo memo* memo memo.0612 memoa memoalx.0620 memoalx.keep memorandum memosally $ echo *mo amemo memo sallymemo user.memo $ echo *alx* memalx memoalx.0620 memoalx.keep

The ambiguous file reference memo* does not match amemo, mem, sallymemo, or user.memo. Like the question mark, an asterisk does not match a leading period in a filename. The –a option causes ls to display hidden filenames. The command echo * does not display . (the working directory), .. (the parent of the working directory), .aaa, or .profile. In contrast, the command echo .* displays only those four names:
$ ls aaa memo.0612 $ ls -a . aaa .. .aaa memo.sally report sally.0612 saturday thurs

memo.0612 memo.sally

.profile report

sally.0612 saturday

thurs

$ echo * aaa memo.0612 memo.sally report sally.0612 saturday thurs $ echo .* . .. .aaa .profile

In the following example, .p* does not match memo.0612, private, reminder, or report. The ls .* command causes ls to list .private and .profile in addition to the contents of the . directory (the working directory) and the .. directory (the parent of the working directory). When called with the same argument, echo displays the names of files (including directories) in the working directory that begin with a dot (.) but not the contents of directories.
$ ls -a . .. memo.0612 private .private .profile reminder report

www.it-ebooks.info

Filename Generation/Pathname Expansion
$ echo .p* .private .profile $ ls .* .private .profile .: memo.0612 private ..: ...

247

reminder

report

$ echo .* . .. .private .profile

You can plan to take advantage of ambiguous file references when you establish conventions for naming files. For example, when you end the names of all text files with .txt, you can reference that group of files with *.txt. The next command uses this convention to send all text files in the working directory to the printer. The ampersand causes lpr to run in the background.
$ lpr

*.txt &

The [ ] Special Characters
A pair of brackets surrounding a list of characters causes the shell to match filenames containing the individual characters. Whereas memo? matches memo followed by any character, memo[17a] is more restrictive: It matches only memo1, memo7, and memoa. The brackets define a character class that includes all the characters within the brackets. (GNU calls this a character list; a GNU character class is something different.) The shell expands an argument that includes a character-class definition by substituting each member of the character class, one at a time, in place of the brackets and their contents. The shell then passes the list of matching filenames to the program it is calling. Each character-class definition can replace only a single character within a filename. The brackets and their contents are like a question mark that substitutes only the members of the character class. The first of the following commands lists the names of all files in the working directory that begin with a, e, i, o, or u. The second command displays the contents of the files named page2.txt, page4.txt, page6.txt, and page8.txt.
$ echo [aeiou]* ... $ less page[2468].txt ...

A hyphen within brackets defines a range of characters within a character-class definition. For example, [6–9] represents [6789], [a–z] represents all lowercase letters in English, and [a–zA–Z] represents all letters, both uppercase and lowercase, in English.

www.it-ebooks.info

248 Chapter 7 The Shell

The following command lines show three ways to print the files named part0, part1, part2, part3, and part5. Each of these command lines causes the shell to call lpr with five filenames:
$ lpr part0 part1 part2 part3 part5 $ lpr part[01235] $ lpr part[0-35]

The first command line explicitly specifies the five filenames. The second and third command lines use ambiguous file references, incorporating character-class definitions. The shell expands the argument on the second command line to include all files that have names beginning with part and ending with any of the characters in the character class. The character class is explicitly defined as 0, 1, 2, 3, and 5. The third command line also uses a character-class definition but defines the character class to be all characters in the range 0–3 plus 5. The following command line prints 39 files, part0 through part38:
$ lpr part[0-9] part[12][0-9] part3[0-8]

The first of the following commands lists the files in the working directory whose names start with a through m. The second lists files whose names end with x, y, or z.
$ echo [a-m]* ... $ echo ...

*[x-z]

optional When an exclamation point (!) or a caret (^) immediately follows the opening bracket ([) that defines a character class, the string enclosed by the brackets matches any character not between the brackets. Thus [^tsq]* matches any filename that does not begin with t, s, or q. The following examples show that *[^ab] matches filenames that do not end with the letters a or b and that [^b-d]* matches filenames that do not begin with b, c, or d.
$ ls aa ab ac ad ba bb bc bd cc dd

$ ls *[^ab] ac ad bc bd $ ls [^b-d]* aa ab ac ad

cc

dd

You can cause a character class to match a hyphen (–) or a closing bracket (]) by placing it immediately before the final closing bracket.

www.it-ebooks.info

Builtins 249

The next example demonstrates that the ls utility cannot interpret ambiguous file references. First ls is called with an argument of ?old. The shell expands ?old into a matching filename, hold, and passes that name to ls. The second command is the same as the first, except the ? is quoted (by preceding it with a backslash [\]; refer to “Special Characters” on page 146). Because the ? is quoted, the shell does not recognize it as a special character and passes it to ls. The ls utility generates an error message saying that it cannot find a file named ?old (because there is no file named ?old).
$ ls ?old hold $ ls \?old ls: ?old: No such file or directory

Like most utilities and programs, ls cannot interpret ambiguous file references; that work is left to the shell.

The shell expands ambiguous file references tip The shell does the expansion when it processes an ambiguous file reference, not the program that the shell runs. In the examples in this section, the utilities (ls, cat, echo, lpr) never see the ambiguous file references. The shell expands the ambiguous file references and passes a list of ordinary filenames to the utility. In the previous examples, echo demonstrates this fact because it simply displays its arguments; it never displays the ambiguous file reference.

Builtins
A builtin is a utility (also called a command ) that is built into a shell. Each of the shells has its own set of builtins. When it runs a builtin, the shell does not fork a new process. Consequently builtins run more quickly and can affect the environment of the current shell. Because builtins are used in the same way as utilities, you will not typically be aware of whether a utility is built into the shell or is a stand-alone utility. The echo utility, for example, is a shell builtin. The shell always executes a shell builtin before trying to find a command or utility with the same name. See page 1018 for an in-depth discussion of builtin commands and page 1031 for a list of bash builtins.
Listing bash builtins

To display a list of bash builtins, give the command info bash shell builtin. To display a page with information on each builtin, move the cursor to Bash Builtins line and press RETURN. Because bash was written by GNU, the info page has better information than does the man page. If you want to read about builtins in the man page, give the command man bash and search for the section on builtins with the command /^SHELL BUILT (search for a line that begins with SHELL BUILT).

www.it-ebooks.info

250 Chapter 7 The Shell

Chapter Summary
The shell is the Linux command interpreter. It scans the command line for proper syntax, picking out the command name and any arguments. The first argument is argument one, the second is argument two, and so on. The name of the command itself is argument zero. Many programs use options to modify the effects of a command. Most Linux utilities identify an option by its leading one or two hyphens. When you give it a command, the shell tries to find an executable program with the same name as the command. When it does, the shell executes the program. When it does not, the shell tells you it cannot find or execute the program. If the command is a simple filename, the shell searches the directories listed in the PATH variable to locate the command. When it executes a command, the shell assigns one file or device to the command’s standard input and another file to its standard output. By default the shell causes a command’s standard input to come from the keyboard and its standard output to go to the screen. You can instruct the shell to redirect a command’s standard input from or standard output to any file or device. You can also connect standard output of one command to standard input of another command using a pipe. A filter is a command that reads its standard input from standard output of one command and writes its standard output to standard input of another command. When a command runs in the foreground, the shell waits for it to finish before it displays a prompt and allows you to continue. When you put an ampersand (&) at the end of a command line, the shell executes the command in the background and displays another prompt immediately. Run slow commands in the background when you want to enter other commands at the shell prompt. The jobs builtin displays a list of suspended jobs and jobs running in the background and includes the job number of each. The shell interprets special characters on a command line to generate filenames. A question mark represents any single character, and an asterisk represents zero or more characters. A single character might also be represented by a character class: a list of characters within brackets. A reference that uses special characters (wildcards) to abbreviate a list of one or more filenames is called an ambiguous file reference. A builtin is a utility that is built into a shell. Each shell has its own set of builtins. When it runs a builtin, the shell does not fork a new process. Consequently builtins run more quickly and can affect the environment of the current shell.

Utilities and Builtins Introduced in This Chapter
Table 7-1 lists the utilities introduced in this chapter.

www.it-ebooks.info

Exercises

251

Table 7-1 New utilities
Utility
tr tee bg fg jobs

Function
Maps one string of characters to another (page 239) Sends standard input to both a file and standard output (page 242) Moves a process to the background (page 243) Moves a process to the foreground (page 243) Displays a list of suspended jobs and jobs running in the background (page 244)

Exercises
1. What does the shell ordinarily do while a command is executing? What should you do if you do not want to wait for a command to finish before running another command? 2. Using sort as a filter, rewrite the following sequence of commands:
$ sort list > temp $ lpr temp $ rm temp

3. What is a PID number? Why are these numbers useful when you run processes in the background? Which utility displays the PID numbers of the commands you are running? 4. Assume that the following files are in the working directory:
$ ls intro notesa notesb ref1 ref2 ref3 section1 section2 section3 section4a section4b sentrev

Give commands for each of the following, using wildcards to express filenames with as few characters as possible. a. List all files that begin with section. b. List the section1, section2, and section3 files only. c. List the intro file only. d. List the section1, section3, ref1, and ref3 files.

www.it-ebooks.info

252 Chapter 7 The Shell

5. Refer to the info or man pages to determine which command will a. Display the number of lines in the standard input that contain the word a or A. b. Display only the names of the files in the working directory that contain the pattern $(. c. List the files in the working directory in reverse alphabetical order. d. Send a list of files in the working directory to the printer, sorted by size. 6. Give a command to a. Redirect standard output from a sort command to a file named phone_list. Assume the input file is named numbers. b. Translate all occurrences of the characters [ and { to the character (, and all occurrences of the characters ] and } to the character ) in the file permdemos.c. (Hint: Refer to the tr man page.) c. Create a file named book that contains the contents of two other files: part1 and part2. 7. The lpr and sort utilities accept input either from a file named on the command line or from standard input. a. Name two other utilities that function in a similar manner. b. Name a utility that accepts its input only from standard input. 8. Give an example of a command that uses grep a. With both input and output redirected. b. With only input redirected. c. With only output redirected. d. Within a pipe. In which of the preceding cases is grep used as a filter? 9. Explain the following error message. Which filenames would a subsequent ls display?
$ ls abc abd abe abf abg abh $ rm abc ab* rm: cannot remove 'abc': No such file or directory

Advanced Exercises
10. When you use the redirect output symbol (>) with a command, the shell creates the output file immediately, before the command is executed. Demonstrate that this is true.

www.it-ebooks.info

Advanced Exercises 253

11. In experimenting with shell variables, Max accidentally deletes his PATH variable. He decides he does not need the PATH variable. Discuss some of the problems he could soon encounter and explain the reasons for these problems. How could he easily return PATH to its original value? 12. Assume permissions on a file allow you to write to the file but not to delete it. a. Give a command to empty the file without invoking an editor. b. Explain how you might have permission to modify a file that you cannot delete. 13. If you accidentally create a filename that contains a nonprinting character, such as a CONTROL character, how can you remove the file? 14. Why does the noclobber variable not protect you from overwriting an existing file with cp or mv? 15. Why do command names and filenames usually not have embedded SPACEs? How would you create a filename containing a SPACE? How would you remove it? (This is a thought exercise, not recommended practice. If you want to experiment, create and work in a directory that contains only your experimental file.) 16. Create a file named answer and give the following command:
$ > answers.0102 < answer cat

Explain what the command does and why. What is a more conventional way of expressing this command?

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

I

PART III Digging into Fedora/RHEL
CHAPTER 8 Linux GUIs: X and GNOME 257 CHAPTER 9 The Bourne Again Shell 279 359

CHAPTER 10 Networking and the Internet

255

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

8
Linux GUIs: X and GNOME

C 8 hapter8

In This Chapter
X Window System . . . . . . . . . . . . . 258 Starting X from a Character-Based Display . . . . . . . . . . . . . . . . . . . . 260 Remote Computing and Local Displays . . . . . . . . . . . . . . . . . . . 260 Desktop Environments/Managers 265 The Nautilus File Browser Window 266 The Nautilus Spatial View (RHEL) 272 GNOME Utilities . . . . . . . . . . . . . . 273 Run Application Window . . . . . . . 274 Searching for Files . . . . . . . . . . . . 274 GNOME Terminal Emulator/Shell 276

Objectives
After reading this chapter you should be able to: Describe the history of the X Window System Start X from a character-based display Use X remotely across a network Customize the mouse buttons in the X Window System using the command line Explain the similarities, differences, and history of GNOME and KDE desktops Use the Nautilus File Browser Start a terminal emulator and run a graphical program from the emulator Search for files using the Search for Files window

257

www.it-ebooks.info

258 Chapter 8 Linux GUIs: X and GNOME

This chapter covers the Linux graphical user interface (GUI). It continues where Chapter 4 left off, going into more detail about the X Window System, the basis for the Linux GUI. It presents a brief history of GNOME and KDE and discusses some of the problems and benefits of having two major Linux desktop environments. The section on the Nautilus File Browser covers the View pane, Sidebar, Main toolbar, menubar, and Spatial view. The final section explores some GNOME utilities, including Terminal, the GNOME terminal emulator.

X Window System
History of X

The X Window System (www.x.org) was created in 1984 at the Massachusetts Institute of Technology (MIT) by researchers working on a distributed computing project and a campuswide distributed environment, called Project Athena. This system was not the first windowing software to run on a UNIX system, but it was the first to become widely available and accepted. In 1985, MIT released X (version 9) to the public, for use without a license. Three years later, a group of vendors formed the X Consortium to support the continued development of X, under the leadership of MIT. By 1998, the X Consortium had become part of the Open Group. In 2001, the Open Group released X version 11, release 6.6 (X11R6.6). The X Window System was inspired by the ideas and features found in earlier proprietary window systems but is written to be portable and flexible. X is designed to run on a workstation, typically attached to a LAN. The designers built X with the network in mind. If you can communicate with a remote computer over a network, running an X application on that computer and sending the results to a local display is straightforward. Although the X protocol has remained stable for a long time, additions to it in the form of extensions are quite common. One of the most interesting—albeit one that has not yet made its way into production—is the Media Application Server, which aims to provide the same level of network transparency for sound and video that X does for simple windowing applications.

XFree86 and X.org

Many distributions of Linux used the XFree86 X server, which inherited its license from the original MIT X server, through release 4.3. In early 2004, just before the release of XFree86 4.4, the XFree86 license was changed to one that is more restrictive and not compatible with the GPL (page 5). In the wake of this change, a number of distributions abandoned XFree86 and replaced it with an X.org X server that is based on a pre-release version of XFree86 4.4, which predates the change in the XFree86 license. Fedora/RHEL uses the X.org X server, named Xorg; it is functionally equivalent to the one distributed by XFree86 because most of the code is the same. Thus modules designed to work with one server work with the other. The Linux GUI is built in layers (Figure 8-1). The bottom layer is the kernel, which provides the basic interfaces to the hardware. On top of the kernel is the X server, which is responsible for managing windows and drawing basic graphical primitives such as lines and bitmaps. Rather than directly generating X commands, most programs use Xlib, the next layer, which is a standard library for interfacing with an X

The X stack

www.it-ebooks.info

X Window System 259

Graphical applications GTK Qt Tk Xlib X server Linux kernel
Figure 8-1

Motif

Other toolkits

The X stack

server. Xlib is complicated and does not provide high-level abstractions, such as buttons and text boxes. Rather than using Xlib directly, most programs rely on a toolkit that provides high-level abstractions. Using a library not only makes programming easier, but also brings consistency to applications. In recent years, the popularity of X has grown outside the UNIX community and extended beyond the workstation class of computers it was originally conceived for. Today X is available for Macintosh computers as well as for PCs running Windows.
Client/server environment

Computer networks are central to the design of X. It is possible to run an application on one computer and display the results on a screen attached to a different computer; the ease with which this can be done distinguishes X from other window systems available today. Thanks to this capability, a scientist can run and manipulate a program on a powerful supercomputer in another building or another country and view the results on a personal workstation or laptop computer. For more information refer to “Remote Computing and Local Displays” on page 260. When you start an X Window System session, you set up a client/server environment. One process, called the X server, displays a desktop and windows under X. Each application program and utility that makes a request of the X server is a client of that server. Examples of X clients include xterm, Compiz, gnome-calculator, and such general applications as word processing and spreadsheet programs. A typical request from a client is to display an image or open a window.

The roles of X client and server might be counterintuitive tip The terms client and server, when referring to X, have the opposite meanings of how you might think of them intuitively: The server runs the mouse, keyboard, and display; the application program is the client. This disparity becomes even more apparent when you run an application program on a remote system. You might think of the system running the program as the server and the system providing the display as the client, but in fact it is the other way around. With X, the system providing the display is the server, and the system running the program is the client.

www.it-ebooks.info

260 Chapter 8 Linux GUIs: X and GNOME
Events

The server also monitors keyboard and mouse actions (events) and passes them to the appropriate clients. For example, when you click the border of a window, the server sends this event to the window manager (client). Characters you type into a terminal emulation window are sent to that terminal emulator (client). The client takes appropriate action when it receives an event—for example, making a window active or displaying the typed character on the server. Separating the physical control of the display (the server) from the processes needing access to the display (the client) makes it possible to run the server on one computer and the client on another computer. Most of the time, this book discusses running the X server and client applications on a single system. “Remote Computing and Local Displays” describes using X in a distributed environment.

optional You can run xev (X event) by giving the command xev from a terminal emulator window and then watch the information flow from the client to the server and back again. This utility opens the Event Tester window, which has a box in it, and asks the X server to send it events each time anything happens, such as moving the mouse pointer, clicking a mouse button, moving the mouse pointer into the box, typing, or resizing the window. The xev utility displays information about each event in the window you opened it from. You can use xev as an educational tool: Start it and see how much information is processed each time you move the mouse. Close the Event Tester window to exit from xev.

Using X
This section provides basic information about starting and configuring X from the command line. For more information see the Xserver man page and the man pages listed at the bottom of the Xserver man page.

Starting X from a Character-Based Display
Once you have logged in on a virtual console (page 138), you can start an X Window System server by using startx. See “Setting the Persistent Runlevel” on page 432 for instructions on setting Fedora to boot to single-user/rescue mode (specify a target of rescue.target) where it displays a textual interface. See “rcS task and inittab” on page 442 for information on changing the initdefault entry in the /etc/inittab file under RHEL that causes Linux to boot into single-user mode, where it displays a textual interface. When you run startx, the X server displays an X screen, using the first available virtual console. The following command causes startx to run in the background so you can switch back to this virtual console and give other commands:
$ startx &

Remote Computing and Local Displays
Typically the X server and the X client run on the same machine. To identify a remote X server (display) an X client (application) is to use, you can either set a global shell variable or use a command-line option. Before you can connect to a remote X server,

www.it-ebooks.info

X Window System 261

you must turn off two security features: You must turn off the Xorg –nolisten tcp option on the server and you must run xhost on the server to give the client permission to connect to the X server. You also need to disable the firewall or open TCP port 6000 (page 893). Unless you have a reason to leave these features off, turn them back on when you finish with the examples in this section—leaving them off weakens system security. These tasks must be performed on the X server because the features protect the server. You do not have to prepare the client. The examples in this section assume the server is named tiny and the client is named dog.

Security and the Xorg –nolisten tcp option security In a production environment, if you need to place an X server and the clients on different systems, it is best to forward (tunnel) X over ssh. This setup provides a secure, encrypted connection. The method described in this section is useful on local, secure networks and for understanding how X works. See “Forwarding X11” on page 696 for information on setting up ssh so it forwards X.

The X –nolisten tcp Option
As Fedora/RHEL is installed, the X server starts with the –nolisten tcp option, which protects the X server by preventing TCP connections to the X server. To connect to a remote X server, you must turn this option off on the server. To turn it off, while working with root privileges edit the file named /etc/gdm/custom.conf and add the following lines: max@tiny:~$ cat /etc/gdm/custom.conf [security] DisallowTCP=false

Reboot the system to restart the X server and gdm (gdm-binary) to effect this change. Use the command ps –ef | grep Xorg to display the options the X server is running with. See library.gnome.org/admin/gdm/3.0/configuration.html.en#daemonconfig for more information.

xhost Grants Access to a Display
As installed, xhost protects each user’s X server. A user who wants to grant access to his X server needs to run xhost. Assume Max is logged in on the system named tiny and wants to allow a user on dog to use his display (X server). Max runs this command: max@tiny:~$ xhost +dog dog being added to access control list max@tiny:~$ xhost access control enabled, only authorized clients can connect INET:dog ...

Without any arguments, xhost describes its state. In the preceding example, INET indicates an IPv4 connection. If Max wants to allow all systems to access his display, he can give the following command:
$ xhost + access control disabled, clients can connect from any host

www.it-ebooks.info

262 Chapter 8 Linux GUIs: X and GNOME

If you frequently work with other users via a network, you might find it convenient to add an xhost line to your .bash_profile file (page 282)—but see the following tip regarding security and xhost. Be selective in granting access to your X display with xhost; if another system has access to your display, you might find your work frequently interrupted.

Security and xhost security Giving a remote system access to your display using xhost means any user on the remote system can watch everything you type in a terminal emulation window, including passwords. For this reason, some software packages, such as the Tcl/Tk development system (www.tcl.tk), restrict their own capabilities when xhost permits remote access to the X server. If you are concerned about security or want to take full advantage of systems such as Tcl/Tk, you should use a safer means of granting remote access to your X session. See the xauth man page for information about a more secure replacement for xhost.

The DISPLAY Variable
The most common method of identifying a display is to use the DISPLAY shell environment variable to hold the X server ID string. This locally unique identification string is automatically set up when the X server starts. The DISPLAY variable holds the screen number of a display:
$ echo $DISPLAY :0.0

The format of the complete (globally unique) ID string for a display is [hostname]:display-number[.screen-number] where hostname is the name of the system running the X server, display-number is the number of the logical (physical) display (0 unless multiple monitors or graphical terminals are attached to the system, or if you are running X over ssh), and screennumber is the logical number of the (virtual) terminal (0 unless you are running multiple instances of X). When you are working with a single physical screen, you can shorten the identification string. For example, you can use tiny:0.0 or tiny:0 to identify the only physical display on the system named tiny. When the X server and the X clients are running on the same system, you can shorten this identification string even further to :0.0 or :0. An ssh connection shows DISPLAY as localhost:10.0. You might have to use ssh –X to see this value. See “X11 forwarding” on page 675 for information on setting up ssh so it forwards X. If DISPLAY is empty or not set, the screen you are working from is not running X. An application (the X client) uses the value of the DISPLAY variable to determine which display, keyboard, and mouse (collectively, the X server) to use. One way to run an X application, such as gnome-calculator, on the local system but have it use the X display on a remote system is to change the value of the DISPLAY variable on the client system so it identifies the remote X server. sam@dog:~$ export DISPLAY=tiny:0.0 sam@dog:~$ gnome-calculator &

www.it-ebooks.info

X Window System 263

The preceding example shows Sam running gnome-calculator with the default X server running on the system named tiny. After setting the DISPLAY variable to the ID of the tiny server, all X programs (clients) Sam starts use tiny as their server (i.e., output appears on tiny’s display and input comes from tiny’s keyboard and mouse). Try running xterm in place of gnome-calculator and see which keyboard it accepts input from. If this example generates an error, refer to the two preceding sections, which explain how to set up the server to allow a remote system to connect to it.

When you change the value of DISPLAY tip When you change the value of the DISPLAY variable, all X programs send their output to the new display named by DISPLAY.

The –display Option
For a single command, you can usually specify the X server on the command line: sam@dog:~$ gnome-calculator -display tiny:0.0

Many X programs accept the –display option. Those that do not accept this option send their output to the display specified by the DISPLAY variable.

Running Multiple X Servers
You can run multiple X servers on a single system. The most common reason for running a second X server is to use a second display that allocates a different number of bits to each screen pixel (uses a different color depth [page 1157]). The possible values are 8, 16, 24, and 32 bits per pixel. Most X servers available for Linux default to 24 or 32 bits per pixel, permitting the use of millions of colors simultaneously. Starting an X server with 8 bits per pixel permits the use of any combination of 256 colors at the same time. The maximum number of bits per pixel allowed depends on the computer graphics hardware and X server. With fewer bits per pixel, the system has to transfer less data, possibly making it more responsive. In addition, many games work with only 256 colors. When you start multiple X servers, each must have a different ID string. The following command starts a second X server:
$ startx –– :1

The –– option marks the end of the startx options and arguments. The startx script uses the arguments to the left of this option and passes arguments to the right of this option to the X server. When you give the preceding command in a graphical environment, such as from a terminal emulator, you must work with root privileges; you will initiate a privileged X session. The following command starts a second X server running at 16 bits per pixel:
$ startx -- :1 -depth 16 &

“Using Virtual Consoles” on page 138 describes how to switch to a virtual console to start a second server where you do not have to work with root privileges.

www.it-ebooks.info

264 Chapter 8 Linux GUIs: X and GNOME
Switch User

To allow another user to log on while you remain logged in as yourself, click your name at the upper-right corner of the screen and click Switch User from the menu GNOME displays (Fedora) or select Main menu: System Log Out username... and click Switch User (RHEL). GNOME displays a login screen and another user can log on. When appropriate, that user can log off or switch users to allow you to log on and resume your session. See “Tunneling/Port Forwarding” on page 696 for information about running X over an ssh connection.

X over ssh

Stopping the X Server
How you terminate a window manager depends on which window manager is running and how it is configured. If X stops responding, switch to a virtual terminal, log in from another terminal or a remote system, or use ssh to access the system. Then kill (page 470) the process running Xorg.

Remapping Mouse Buttons
Throughout this book, each description of a mouse click refers to the button by its position (left, middle, or right, with left implied when no button is specified) because the position of a mouse button is more intuitive than an arbitrary name or number. X numbers buttons starting at the left and continuing with the mouse wheel. The buttons on a three-button mouse are numbered 1 (left), 2 (middle), and 3 (right). A mouse wheel, if present, is numbered 4 (rolling it up) and 5 (rolling it down). Clicking the wheel is equivalent to clicking the middle mouse button. The buttons on a two-button mouse are 1 (left) and 2 (right). If you are right-handed, you can conveniently press the left mouse button with your index finger; X programs take advantage of this fact by relying on button 1 for the most common operations. If you are left-handed, your index finger rests most conveniently on button 2 or 3 (the right button on a two- or three-button mouse). “Mouse Preferences” on page 101 describes how to use a GUI to change a mouse between right-handed and left-handed. You can also change how X interprets the mouse buttons using xmodmap. If you are left-handed and using a three-button mouse with a wheel, the following command causes X to interpret the right button as button 1 and the left button as button 3:
$ xmodmap -e 'pointer = 3 2 1 4 5'

Omit the 4 and 5 if the mouse does not have a wheel. The following command works for a two-button mouse without a wheel:
$ xmodmap -e 'pointer = 2 1'

If xmodmap displays a message complaining about the number of buttons, use the xmodmap –pp option to display the number of buttons X has defined for the mouse:

www.it-ebooks.info

X Window System 265
$ xmodmap -pp There are 9 pointer buttons defined. Physical Button 1 2 3 4 5 6 7 8 9 Button Code 1 2 3 4 5 6 7 8 9

Then expand the previous command, adding numbers to complete the list. If the –pp option shows nine buttons, give the following command:
$ xmodmap -e 'pointer = 3 2 1 4 5 6 7 8 9'

Changing the order of the first three buttons is critical to making the mouse suitable for a left-handed user. When you remap the mouse buttons, remember to reinterpret the descriptions in this book accordingly. When this book asks you to click the left button or does not specify which button to click, use the right button, and vice versa.

Desktop Environments/Managers
Conceptually X is very simple. As a consequence, it does not provide some of the more common features found in GUIs, such as the ability to drag windows. The UNIX/Linux philosophy is one of modularity: X relies on a window manager, such as Mutter, Metacity, or Compiz, to draw window borders and handle moving and resizing operations. Unlike a window manager, which has a clearly defined task, a desktop environment (manager) does many things. In general, a desktop environment, such as GNOME or KDE, provides a means of launching applications and utilities, such as a file manager, that work with a window manager.

GNOME and KDE
The KDE project began in 1996, with the aim of creating a consistent, user-friendly desktop environment for free UNIX-like operating systems. KDE is based on the Qt toolkit made by Trolltech. When KDE development began, the Qt license was not compatible with the GPL (page 5). For this reason the Free Software Foundation decided to support a different project, the GNU Network Object Model Environment (GNOME). More recently Qt has been released under the terms of the GPL, eliminating part of the rationale for GNOME’s existence.
GNOME

GNOME is the default desktop environment for Fedora/RHEL. It provides a simple, coherent user interface that is suitable for corporate use. GNOME uses GTK for

www.it-ebooks.info

266 Chapter 8 Linux GUIs: X and GNOME

drawing widgets. GTK, developed for the GNU Image Manipulation Program (gimp), is written in C, although bindings for C++ and other languages are available. GNOME does not take much advantage of its component architecture. Instead, it continues to support the traditional UNIX philosophy of relying on many small programs, each of which is good at doing a specific task.
KDE

KDE is written in C++ on top of the Qt framework. KDE tries to use existing technology, if it can be reused, but creates its own if nothing else is available or if a superior solution is needed. For example, KDE implemented an HTML rendering engine long before the Mozilla project was born. Similarly, work on KOffice began a long time before StarOffice became the open-source OpenOffice.org (which is now LibreOffice). In contrast, the GNOME office applications are stand-alone programs that originated outside the GNOME project. KDE’s portability is demonstrated by the use of most of its core components, including Konqueror and KOffice, under Mac OS X. Since the release of version 2, the GNOME project has focused on simplifying the user interface, removing options where they are deemed unnecessary, and aiming for a set of default settings that the end user will not wish to change. Fedora 15 introduced GNOME 3, which is radically different from GNOME 2, following the trend towards simpler, more graphical desktops that have more icons and fewer menus. KDE has moved in the opposite direction, emphasizing configurability. The freedesktop.org group (freedesktop.org), whose members are drawn from the GNOME and KDE projects, is improving interoperability and aims to produce standards that will allow the two environments to work together. One standard released by freedesktop.org allows applications to use the notification area of either the GNOME or KDE panel without being aware of which desktop environment they are running in.

Interoperability

GNUStep
The GNUStep project (www.gnustep.org), which began before both the KDE and GNOME projects, is creating an open-source implementation of the OPENSTEP API and desktop environment. The result is a very clean and fast user interface. The default look of WindowMaker, the GNUStep window manager, is somewhat dated, but it supports themes so you can customize its appearance. The user interface is widely regarded as one of the most intuitive found on a UNIX platform. Because GNUStep has less overhead than GNOME and KDE, it runs better on older hardware. If you are running Linux on hardware that struggles with GNOME and KDE or if you would prefer a user interface that does not attempt to mimic Windows, try GNUStep. WindowMaker is provided in the WindowMaker package.

The Nautilus File Browser Window
“Using Nautilus to Work with Files” on page 102 presented an introduction to using Nautilus. This section discusses the Nautilus File Browser window in more depth.

www.it-ebooks.info

The Nautilus File Browser Window

267

Menubar Main toolbar Sidebar Handle View pane Status bar

Figure 8-2

A Nautilus File Browser window displaying icons

RHEL: turn off Spatial view; turn on File Browser windows tip Under RHEL, to make the Nautilus windows on the desktop you are working on correspond to the figures in this book, you must turn off Spatial view (page 272) and turn on File Browser windows. For more information refer to “The Two Faces of Nautilus” on page 103.

Figure 8-2 shows a File Browser window with a Sidebar, View pane, menubar, Main toolbar, and status bar. To display your home folder in a File Browser window, select Main menu: Places Home Folder.

The View Pane
The View pane displays icons or a list of filenames. Select the view you prefer from the bottom of the File Browser menubar: View menu. Figure 8-2 shows an Icon view and Figure 8-3 on the next page shows a List view. A Compact view is also available. Objects in the View pane behave exactly as objects on the desktop do. See the sections starting on page 95 for information on working with objects. You can cut/copy and paste objects within a single View pane, between View panes, or between a View pane and the desktop. The Object context menu (right-click) has cut, copy, and paste selections. Alternatively, you can use the clipboard (page 116) to cut/copy and paste objects.

The Sidebar
The Sidebar augments the information Nautilus displays in the View pane. You can close or display the Sidebar by pressing F9 or by selecting File Browser menubar: View Sidebar Show Sidebar. To change the horizontal size of the Sidebar, drag the handle (Figure 8-2) on its right side. The File Browser menubar:

www.it-ebooks.info

268 Chapter 8 Linux GUIs: X and GNOME

Main toolbar

Figure 8-3 A Nautilus File Browser window displaying a List view and a textual location in the Main toolbar

View Sidebar menu controls whether the Sidebar displays places or a file tree as described following.
Places

Places lists folders, bookmarks, devices, and network locations. Double-click one of these places to display that place in the View pane. You can open a place in a new File Browser tab or window by right-clicking the directory in the Sidebar and selecting Open in New Tab or Open in New Window, respectively. Add a bookmark by displaying the directory you want to bookmark in the View pane and pressing CONTROL-D or by selecting File Browser menubar: Bookmarks Add Bookmark. Remove a bookmark by selecting File Browser menubar: Bookmarks Edit Bookmarks or by right-clicking the bookmark in the Sidebar and selecting Remove.

Tree

Tree presents an expandable tree view of your home folder and each mounted filesystem. Each directory in the tree has a plus (+) or minus (–) sign to its left. Click a plus sign to expand a directory; click a minus sign to close a directory. Click a directory in the tree to display that directory in the View pane. Double-click a directory to expand or close it in the Sidebar and display it in the View pane.

Nautilus can open a terminal emulator tip When you install the nautilus-open-terminal package (see page 534 for instructions) and log out and log back in, Nautilus presents an Open in Terminal selection in context menus where appropriate. For example, with this package installed, when you right-click a folder (directory) object and select Open in Terminal, Nautilus opens a terminal emulator with that directory as the working directory (page 190).

www.it-ebooks.info

The Nautilus File Browser Window

269

Control Bars
This section discusses the three of the control bars that can appear in a File Browser window: the status bar, menubar, and Main toolbar (Figure 8-2, page 267). From File Browser menubar: View, you can choose which of these bars to display—except for the menubar, which Nautilus always displays.
Menubar

The menubar appears at the top of the File Browser window and displays a menu when you click one of its selections. Which menu selections Nautilus displays depends on what the View pane is displaying and which objects are selected. The next section describes the menubar in detail. The Main toolbar appears below the menubar and holds navigation tool icons: Location, Back, Forward, and Search. The Location buttons display the name of the directory that appears in the View pane. By default, Nautilus displays Location in iconic format. Press CONTROL-L to change the Location to textual format. If the Main toolbar is too short to hold all icons, Nautilus displays a button with a triangle pointing down at the right end of the toolbar. Click this button to display a dropdown list of the remaining icons. Display or remove the Main toolbar by selecting File Browser menubar: View Main toolbar. In iconic format, each button represents a directory in a pathname (page 191). The View pane displays the directory of the depressed (darker) button. Click one of these buttons to display that directory. If the leftmost button holds a triangle that points to the left, Nautilus is not displaying buttons for all the directories in the absolute (full) pathname; click the button with a triangle in it to display more directory buttons. In textual format, the text box displays the absolute pathname of the displayed directory. Nautilus displays another directory when you enter the pathname of the directory and press RETURN.

Main toolbar

Status bar

If no items are selected, the status bar, at the bottom of the window, indicates how many items are displayed in the View pane. If the directory you are viewing is on the local system, it also tells you how much free space is available on the device that holds the directory displayed by the View pane. If an item is selected, the status bar displays the name of the item and its size. Display or remove the status bar by selecting File Browser menubar: View Statusbar.

Menubar
The Nautilus File Browser menubar controls which information the File Browser displays and how it displays that information. Many of the menu selections duplicate controls found elsewhere in the File Browser window. This section highlights some of the selections on the menubar; click Help on the menubar and select Contents for more information. The menus the menubar holds are described next.

www.it-ebooks.info

270 Chapter 8 Linux GUIs: X and GNOME

Figure 8-4
File

The Connect to Server window

The several Open selections and the Property selection of File work with the highlighted object(s) in the View pane. If no objects are highlighted, these selections are grayed out or absent. Selecting Connect to Server (also available from Main menu: Places) displays the Connect to Server window (Figure 8-4). This window presents a Type drop-down list that allows you to select FTP, SSH, Windows, or other types of servers. Enter the URL of the server in the text box labeled Server. For an FTP connection, do not enter the ftp:// part of the URL. Fill in the optional information as appropriate. Click Connect. If the server requires authentication, Nautilus displays a window in which you can enter a username and password. Nautilus opens a window displaying a directory on the server and an object, named for the URL you specified, on the desktop. After you close the window, you can open the object to connect to and display a directory on the server. Edit Many of the Edit selections work with highlighted object(s) in the View pane; if no objects are highlighted, these selections are grayed out or absent. This section discusses two selections from Edit: Compress and Preferences. The Edit Compress selection creates a single archive file comprising the selected objects. This selection opens a Compress window (Figure 8-5) that allows you to specify the name and location of the archive. The drop-down list to the right of the text box labeled Filename allows you to specify a filename extension that determines the type of archive this tool creates. For example, .tar.gz creates a tar (page 162) file compressed by gzip (page 161) and .tar.bz2 creates a tar file compressed by bzip2 (page 160). Click the plus sign to the left of Other Objects to specify a password for and/or to encrypt the archive (available only with certain types of archives). You can also split the archive into several files (volumes).

Figure 8-5

The Compress window

www.it-ebooks.info

The Nautilus File Browser Window

271

Figure 8-6

The File Management Preferences window, Views tab

The Edit Preferences selection displays the File Management Preferences window (Figure 8-6). This window has five tabs that control the appearance and behavior of File Browser windows. The Views tab sets several defaults, including which view the File Browser displays (Icon, List, or Compact view), the arrangement of the objects, the default zoom level, and default settings for the Compact view. The Behavior tab controls how many clicks it takes to open an object and what Nautilus does when it opens an executable text object (script). For more confident users, this tab has an option that includes a Delete selection in addition to the Move to Trash selection on several menus. The Delete selection immediately removes the selected object instead of moving it to the Trash folder. The Display tab specifies which information Nautilus includes in object (icon) captions. The three drop-down lists specify the order in which Nautilus displays information as you increase the zoom level of the View pane. This tab also specifies the date format Nautilus uses. The List Columns tab specifies which columns Nautilus displays, and in what order it displays them, in the View pane when you select List View. The Preview tab controls when Nautilus displays or plays previews of files (by size and Always, Local Files Only, Never).

www.it-ebooks.info

272 Chapter 8 Linux GUIs: X and GNOME
View

Click the Sidebar, Main toolbar, and Statusbar selections in the View submenu to display or remove these elements from the window. The Show Hidden Files selection displays in the View pane those files with hidden filenames (page 190). The Go selections display various folders in the View pane. Bookmarks appear at the bottom of this menu and in the Sidebar under Bookmarks. The Bookmarks selections are explained under “Places” on page 268. The Help selections display local information about Nautilus.

Go Bookmarks

Help

optional

The Nautilus Spatial View (RHEL)
Under RHEL, Nautilus gives you two ways to work with files: the traditional File Browser view described in the previous section and the innovative Spatial view shown in Figure 8-7. By default, RHEL displays the Spatial view. Other than in this section, this book describes the more traditional File Browser window. See “The Two Faces of Nautilus” on page 103 for instructions on how to turn off the Spatial view and turn on the File Browser. The Nautilus Spatial (as in “having the nature of space”) view has many powerful features but might take some getting used to. It always provides one window per folder. By default, when you open a folder, Nautilus displays a new window. To open a Spatial view of your home directory, Select Main menu: Home Folder and experiment as you read this section. If you double-click the Desktop icon in the Spatial view, Nautilus opens a new window that displays the Desktop folder. A Spatial view can display icons, a list of filenames, or a compact view. To select your preferred format, click View on the menubar and choose Icons, List, or Compact. To create files to experiment with, right-click in the window (not on an icon) to display the Nautilus context menu and select Create Folder or Create Document.

Parent-folders button and pop-up menu

Figure 8-7 The Nautilus Spatial view

www.it-ebooks.info

GNOME Utilities 273

Use SHIFT to close the current window as you open another window tip If you hold the SHIFT key down when you double-click to open a new window, Nautilus closes the current window as it opens the new one. This behavior might be more familiar and can help keep the desktop from becoming cluttered. If you do not want to use the keyboard, you can achieve the same result by double-clicking the middle mouse button.
Window memory

Move the window by dragging the titlebar. The Spatial view has window memory— that is, the next time you open that folder, Nautilus opens it at the same size and in the same location. Even the scrollbar will be in the same position. The key to closing the current window and returning to the window of the parent directory is the Parent-folders button (Figure 8-7). Click this button to display the Parent-folders pop-up menu. Select the directory you want to open from this menu. Nautilus then displays in a Spatial view the directory you specified. From a Spatial view, you can open a folder in a traditional view by right-clicking the folder and selecting Browse Folder.

Parent-folders button

GNOME Utilities
GNOME comes with numerous utilities that can make your work with the desktop easier and more productive. This section covers several tools that are integral to the use of GNOME.

Pick a Font Window
The Pick a Font window (Figure 8-8) appears when you select Fonts from the Tweak Tool window (page 94) and click one of the font buttons on the right side of the window. From the Pick a Font window you can select a font family, a style, and a size. A preview of your choice appears in the Preview frame in the lower part of the window. Click OK when you are satisfied with your choice.

Figure 8-8

The Pick a Font window

www.it-ebooks.info

274 Chapter 8 Linux GUIs: X and GNOME

Figure 8-9

The Pick a Color window

Pick a Color Window
The Pick a Color window (Figure 8-9) appears when you need to specify a color, such as when you click the colored button near the lower-right corner of the Background window (Figure 4-8, page 100). When the Pick a Color window opens, the bar below the color circle displays the current color. Click the desired color on the color ring, and click/drag the lightness of that color in the triangle. As you change the color, the right end of the bar below the color circle previews the color you are selecting, while the left end continues to display the current color. You can also use the eyedropper to pick up a color from the workspace: Click the eyedropper and then click the resulting eyedropper mouse pointer on the color you want to select. The color you choose appears in the bar. Click OK when you are satisfied with the color you have specified.

Run Application Window
The Run Application window (Figure 4-6, page 97) enables you to run a program as though you had initiated it from a command line. To display the Run Application window, press ALT-F2. Enter a command in the text box. As soon as GNOME can uniquely identify the command you are entering, it completes the command and might display an object that identifies the application. Keep typing if the displayed command is not the one you want to run. Otherwise, press RETURN to run the command or TAB to accept the command in the text box. You can then continue entering information in the window. Click Run with file to specify a file to use as an argument to the command in the text box. Put a tick in the check box labeled Run in terminal to run a textual application, such as vi, in a terminal emulator window. As explained under “Running textual applications” on page 98, GNOME closes the resulting window when the program terminates.

Searching for Files
The Search for Files window (Figure 8-10) can help you find files whose locations or names you do not know or have forgotten. To open this window, select Main menu: Places Search for Files or enter gnome-search-tool on a command line from

www.it-ebooks.info

GNOME Utilities 275

Figure 8-10

The Search for Files window

a terminal emulator or Run Application window (ALT-F2). To search by filename or partial filename, enter the (partial) filename in the combo box labeled Name contains and then select the folder you want to search in from the drop-down list labeled Look in folder. When GNOME searches in a folder, it searches subfolders to any level (it searches the directory hierarchy). To search all directories in all mounted filesystems, select File System from the drop-down list labeled Look in folder. Select Other to search a folder not included in the drop-down list; GNOME opens a Browse/Save window (page 107). Once you have entered the search criteria, click Find. GNOME displays the list of files matching the criteria in the list box labeled Search results. Double-click a file in this list box to open it. To refine the search, you can enter more search criteria. Click the plus sign to the left of Select more options to expand the window and display more search criteria. GNOME initially displays one search criterion and a line for adding another criterion as shown in Figure 8-11. With this part of the window expanded, GNOME incorporates all visible search criteria when you click Find.

Figure 8-11

The Search for Files window with Select more options expanded

www.it-ebooks.info

276 Chapter 8 Linux GUIs: X and GNOME

The first line below Select more options holds a text box labeled Contains the text. If nothing is entered in this text box, the search matches all files. You can leave this text box as is or remove the line by clicking Remove at the right end of the line. To search for a file that contains a specific string of characters (text), enter the string in this text box. To add search criteria, make a selection from the list box labeled Available options and click Add to the right of the drop-down list. To remove criteria, click Remove at the right end of the line that holds the criterion you want to remove. To select files that were modified fewer than a specified number of days ago, select Date modified less than from the drop-down list labeled Available options and click Add. The Search for Files window adds a line with a spin box labeled Date modified less than. With this spin box showing 0 (zero), as it does initially, no file matches the search criteria. Change this number as desired and click Find to begin the search.

GNOME Terminal Emulator/Shell
The GNOME terminal emulator displays a window that mimics a character-based terminal (page 117). To display a terminal emulator window, select Main menu: Applications System Tools Terminal or enter gnome-terminal on a command line or from a Run Application window (ALT-F2). When the GNOME terminal emulator is already displayed, select Terminal menubar: File Open Terminal or right-click within the Terminal window and select Open Terminal to display a new terminal emulator window. To open an additional terminal session within the same Terminal window, select Terminal menubar: File Open Tab or right-click the window and select Open Tab from the context menu. A row of tabs appears below the menubar as gnome-terminal opens another terminal session on top of the existing one. Add as many terminal sessions as you like; click the tabs to switch between sessions.

GNOME terminal emulator shortcuts tip While using the GNOME terminal emulator, switches to tab number x.
CONTROL-SHIFT-N opens a new window and CONTROL-SHIFT-T opens a new tab. New windows and tabs open to the working directory. In addition, you can use CONTROL-PAGE UP and CONTROL-PAGE DOWN to switch between tabs. In addition, CONTROL-x

A session you add from the context menu uses the same profile as the session you open it from. When you use the menubar to open a session, GNOME gives you a choice of profiles, if more than one is available. You can add and modify profiles, including the Default profile, by selecting Terminal menubar: Edit Profiles. Highlight the profile you want to modify or click New to design a new profile.

www.it-ebooks.info

Exercises

277

Chapter Summary
The X Window System GUI is portable and flexible and makes it easy to write applications that work on many different types of systems without having to know low-level details for the individual systems. This GUI can operate in a networked environment, allowing a user to run a program on a remote system and send the results to a local display. The client/server concept is integral to the operation of the X Window System, in which the X server is responsible for fulfilling requests made of X Window System applications or clients. Hundreds of clients are available that can run under X. Programmers can also write their own clients, using tools such as the GTK+ and GTK+2 GNOME libraries to write GNOME programs and the Qt and KDE libraries to write KDE programs. The window managers, and virtually all X applications, are designed to help users tailor their work environments in simple or complex ways. You can designate applications that start automatically, set such attributes as colors and fonts, and even alter the way keyboard strokes and mouse clicks are interpreted. Built on top of the X Window System, the GNOME desktop manager can be used as is or customized to better suit your needs. It is a graphical user interface to system services (commands), the filesystem, applications, and more. Although not part of GNOME, the Metacity, Mutter, and Compiz window managers work closely with GNOME and are the default window managers for GNOME under Fedora. A window manager controls all aspects of the windows, including placement, decoration, grouping, minimizing and maximizing, sizing, and moving. The Nautilus File Browser window is a critical part of GNOME; the desktop is a modified File Browser window. The File Browser View pane displays icons or a list of filenames you can work with. The Sidebar, which can display two types of information, augments the information Nautilus displays in the View pane. GNOME also provides many graphical utilities you can use to customize and work with the desktop. It supports MIME types; thus, when you double-click an object, GNOME generally knows which tool to use to display the data represented by the object. In sum, GNOME is a powerful desktop manager that can make your job both easier and more fun.

Exercises
1. a. What is Nautilus? b. List four things you can do with Nautilus. c. How do you use Nautilus to search for a file?

www.it-ebooks.info

278 Chapter 8 Linux GUIs: X and GNOME

2. What is a terminal emulator? What does it allow you to do from a GUI that you would not be able to do without one? 3. How would you search the entire filesystem for a file named today.odt? 4. a. List two ways you can open a file using Nautilus. b. How does Nautilus “know” which program to use to open different types of files? c. What are the three common Nautilus control bars? Which kinds of tools do you find on each? d. Discuss the use of the Nautilus location bar in textual mode.

Advanced Exercises
5. Assume you are using a mouse with nine pointer buttons defined. How would you reverse the effects of using the mouse wheel? 6. How would you use Nautilus to connect to the FTP server at mirrors.kernel.org/fedora? 7. Discuss the client/server environment set up by the X Window System. How does the X server work? List three X clients. Where is the client and where is the server when you log in on a local system? What is an advantage of this setup? 8. Run xwininfo from a terminal emulator window and answer these questions: a. What does xwininfo do? b. What does xwininfo give as the name of the window you clicked? Does that agree with the name in the window’s titlebar? c. What is the size of the window? In which units does xwininfo display this size? What is the depth of a window? d. How can you get xwininfo to display the same information without having to click the window? 9. Find and install xeyes (not tuxeyes). Write an xeyes command to display a window that is 600 pixels wide and 400 pixels tall, is located 200 pixels from the right edge of the screen and 300 pixels from the top of the screen, and contains orange eyes outlined in blue with red pupils. (Hint: Refer to the xeyes man page.)

www.it-ebooks.info

9
The Bourne Again Shell
C 9 hapter9

In This Chapter
Startup Files . . . . . . . . . . . . . . . . . 282 Redirecting Standard Error . . . . . . 285 Writing a Simple Shell Script . . . . 288 Job Control. . . . . . . . . . . . . . . . . . . 296 Manipulating the Directory Stack 298 Parameters and Variables . . . . . . 301 Processes . . . . . . . . . . . . . . . . . . . 316 History . . . . . . . . . . . . . . . . . . . . . . 319 Re-executing and Editing Commands. . . . . . . . . . . . . . . . . 320 Functions . . . . . . . . . . . . . . . . . . . . 338 Controlling bash: Features and Options . . . . . . . . . . . . . . . . . . . 340 Processing the Command Line. . . 344

Objectives
After reading this chapter you should be able to: Describe the purpose and history of the Bourne Again Shell List the startup files bash runs Use three different methods to run a shell script Manage multiple processes with job control Redirect error messages to a file Use special characters to separate and group commands Create variables and display the values of variables and parameters List and describe common variables found on the system Reference, repeat, and modify previous commands using history Use control characters to edit the command line Create, display, and remove aliases and functions Customize the bsh environment using set and shopt builtins List the order of command line expansion
279

www.it-ebooks.info

280 Chapter 9 The Bourne Again Shell

This chapter picks up where Chapter 7 left off. Chapter 27 expands on this chapter, exploring control flow commands and more advanced aspects of programming the Bourne Again Shell (bash). The bash home page is at www.gnu.org/software/bash. The bash info page is a complete Bourne Again Shell reference. The Bourne Again Shell is a command interpreter and high-level programming language. As a command interpreter, it processes commands you enter on the command line in response to a prompt. When you use the shell as a programming language, it processes commands stored in files called shell scripts. Like other languages, shells have variables and control flow commands (e.g., for loops and if statements). When you use a shell as a command interpreter, you can customize the environment you work in. You can make your prompt display the name of the working directory, create a function or an alias for cp that keeps it from overwriting certain kinds of files, take advantage of keyword variables to change aspects of how the shell works, and so on. You can also write shell scripts that do your bidding—anything from a one-line script that stores a long, complex command to a longer script that runs a set of reports, prints them, and mails you a reminder when the job is done. More complex shell scripts are themselves programs; they do not just run other programs. Chapter 27 has some examples of these types of scripts. Most system shell scripts are written to run under bash (or dash; next page). If you will ever work in single-user mode—when you boot the system or perform system maintenance, administration, or repair work, for example—it is a good idea to become familiar with this shell. This chapter expands on the interactive features of the shell described in Chapter 7, explains how to create and run simple shell scripts, discusses job control, introduces the basic aspects of shell programming, talks about history and aliases, and describes command-line expansion. Chapter 27 presents some more challenging shell programming problems.

Background
The Bourne Again Shell is based on the Bourne Shell (the early UNIX shell; this book refers to it as the original Bourne Shell to avoid confusion), which was written by Steve Bourne of AT&T’s Bell Laboratories. Over the years the original Bourne Shell has been expanded, but it remains the basic shell provided with many commercial versions of UNIX. sh Shell

Because of its long and successful history, the original Bourne Shell has been used to write many of the shell scripts that help manage UNIX systems. Some of these scripts appear in Linux as Bourne Again Shell scripts. Although the Bourne Again Shell includes many extensions and features not found in the original Bourne Shell, bash maintains compatibility with the original Bourne Shell so you can run Bourne Shell scripts under bash. On UNIX systems the original Bourne Shell is named sh.

www.it-ebooks.info

Shell Basics 281

On Fedora/RHEL systems sh is a symbolic link to bash ensuring that scripts that require the presence of the Bourne Shell still run. When called as sh, bash does its best to emulate the original Bourne Shell. dash Shell

The bash executable file is almost 900 kilobytes, has many features, and is well suited as a user login shell. The dash (Debian Almquist) shell is about 100 kilobytes, offers Bourne Shell compatibility for shell scripts (noninteractive use), and because of its size, can load and execute shell scripts much more quickly than bash. System V UNIX introduced the Korn Shell (ksh), written by David Korn. This shell extended many features of the original Bourne Shell and added many new features. Some features of the Bourne Again Shell, such as command aliases and commandline editing, are based on similar features from the Korn Shell. The POSIX (Portable Operating System Interface) family of related standards is being developed by PASC (IEEE’s Portable Application Standards Committee; www.pasc.org/plato). A comprehensive FAQ on POSIX, including many links, appears at www.opengroup.org/austin/papers/posix_faq.html. POSIX standard 1003.2 describes shell functionality. The Bourne Again Shell provides the features that match the requirements of this standard. Efforts are under way to make the Bourne Again Shell fully comply with the POSIX standard. In the meantime, if you invoke bash with the ––posix option, the behavior of the Bourne Again Shell will closely match the POSIX requirements.

Korn Shell

POSIX

Shell Basics
This section covers writing and using startup files, redirecting standard error, writing and executing simple shell scripts, separating and grouping commands, implementing job control, and manipulating the directory stack.

chsh: changes your login shell tip The person who sets up your account determines which shell you use when you first log in on the system or when you open a terminal emulator window in a GUI environment. Under Fedora/RHEL, bash is the default shell. You can run any shell you like after you are logged in. Enter the name of the shell you want to use (bash, tcsh, or another shell) and press RETURN; the next prompt will be that of the new shell. Give an exit command to return to the previous shell. Because shells you call in this manner are nested (one runs on top of the other), you will be able to log out only from your original shell. When you have nested several shells, keep giving exit commands until you reach your original shell. You will then be able to log out. Use the chsh utility to change your login shell permanently. First give the command chsh. In response to the prompts, enter your password and the absolute pathname of the shell you want to use (/bin/bash, /bin/tcsh, or the pathname of another shell). When you change your login shell in this manner using a terminal emulator (page 117) under a GUI, subsequent terminal emulator windows will not reflect the change until you log out of the system and log back in. See page 469 for an example of how to use chsh.

www.it-ebooks.info

282 Chapter 9 The Bourne Again Shell

Startup Files
When a shell starts, it runs startup files to initialize itself. Which files the shell runs depends on whether it is a login shell, an interactive shell that is not a login shell (such as you get by giving the command bash), or a noninteractive shell (one used to execute a shell script). You must have read access to a startup file to execute the commands in it. Fedora/RHEL puts appropriate commands in some of these files. This section covers bash startup files.

Login Shells
A login shell is the first shell that displays a prompt when you log in on a system from the system console or a virtual console (page 138), remotely using ssh or another program (page 135), or by another means. When you are running a GUI and open a terminal emulator such as gnome-terminal (page 276), you are not logging in on the system (you do not provide your username and password), so the shell the emulator displays is not a login shell; it is an interactive nonlogin shell (next). Login shells are, by their nature, interactive. This section describes the startup files that are executed by login shells and shells that you start with the bash ––login option.
/etc/profile

The shell first executes the commands in /etc/profile, establishing systemwide default characteristics for users running bash. In addition to executing the commands it holds, profile executes the commands within each of the files with a .sh filename extension in the /etc/profile.d directory. This setup allows a user working with root privileges to modify the commands profile runs without changing the profile file itself. Because profile can be replaced when the system is updated, making changes to files in the profile.d directory ensures the changes will remain when the system is updated.

Set global variables for all users in a

tip Setting and exporting a variable in a file with a .sh filename extension in the /etc/profile.d directory makes that variable available to every user’s login shell. Because the variable is exported, it is also available to all interactive and noninteractive subshells of the login shell.
.bash_profile .bash_login .profile

*.sh file in /etc/profile.d

Next the shell looks for ~/.bash_profile, ~/.bash_login, or ~/.profile (~/ is shorthand for your home directory), in that order, executing the commands in the first of these files it finds. You can put commands in one of these files to override the defaults set in /etc/profile. By default, Fedora/RHEL sets up new accounts with ~/.bash_profile and ~/.bashrc files. The default ~/.bash_profile file calls ~/.bashrc, which calls /etc/bashrc (next).

.bash_logout

When you log out, bash executes commands in the ~/.bash_logout file. This file often holds commands that clean up after a session, such as those that remove temporary files.

www.it-ebooks.info

Shell Basics 283

Interactive Nonlogin Shells
The commands in the preceding startup files are not executed by interactive, nonlogin shells. However, these shells inherit values from the login shell variables that are set by these startup files.
.bashrc

An interactive nonlogin shell executes commands in the ~/.bashrc file. The default ~/.bashrc file calls /etc/bashrc. Although not called by bash directly, the Fedora/RHEL ~/.bashrc file calls /etc/bashrc.

/etc/bashrc

Noninteractive Shells
The commands in the previously described startup files are not executed by noninteractive shells, such as those that runs shell scripts. However, these shells inherit login shell variables that are set by these startup files.
BASH_ENV

Noninteractive shells look for the environment variable BASH_ENV (or ENV if the shell is called as sh) and execute commands in the file named by this variable.

Setting Up Startup Files
Although many startup files and types of shells exist, usually all you need are the .bash_profile and .bashrc files in your home directory. Commands similar to the following in .bash_profile run commands from .bashrc for login shells (when .bashrc exists). With this setup, the commands in .bashrc are executed by login and nonlogin shells. if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

The [ –f ~/.bashrc ] tests whether the file named .bashrc in your home directory exists. See pages 971 and 974 for more information on test and its synonym [ ]. See page 284 for information on the . (dot) builtin.

Set PATH in .bash_profile tip Because commands in .bashrc might be executed many times, and because subshells inherit exported variables, it is a good idea to put commands that add to existing variables in the .bash_profile file. For example, the following command adds the bin subdirectory of the home directory to PATH (page 308) and should go in .bash_profile:
PATH=$PATH:$HOME/bin

When you put this command in .bash_profile and not in .bashrc, the string is added to the PATH variable only once, when you log in. Modifying a variable in .bash_profile causes changes you make in an interactive session to propagate to subshells. In contrast, modifying a variable in .bashrc overrides changes inherited from a parent shell.

www.it-ebooks.info

284 Chapter 9 The Bourne Again Shell

Sample .bash_profile and .bashrc files follow. Some commands used in these files are not covered until later in this chapter. In any startup file, you must export variables and functions that you want to be available to child processes. For more information refer to “Locality of Variables” on page 1008.
$ cat ~/.bash_profile if [ -f ~/.bashrc ]; then . ~/.bashrc fi PATH=$PATH:/usr/local/bin export PS1='[\h \W \!]\$ '

# Read local startup file if it exists # Add /usr/local/bin to PATH # Set prompt

The first command in the preceding .bash_profile file executes the commands in the user’s .bashrc file if it exists. The next command adds to the PATH variable (page 308). Typically PATH is set and exported in /etc/profile, so it does not need to be exported in a user’s startup file. The final command sets and exports PS1 (page 310), which controls the user’s prompt. A sample .bashrc file is shown below. The first command executes the commands in the /etc/bashrc file if it exists. Next the file sets and exports the LANG (page 314) and VIMINIT (for vim initialization) variables and defines several aliases. The final command defines a function (page 338) that swaps the names of two files.
$ cat ~/.bashrc if [ -f /etc/bashrc ]; then source /etc/bashrc fi set -o noclobber unset MAILCHECK export LANG=C export VIMINIT='set ai aw' alias df='df -h' alias rm='rm -i' alias lt='ls -ltrh | tail' alias h='history | tail' alias ch='chmod 755 ' function switch() { local tmp=$$switch mv "$1" $tmp mv "$2" "$1" mv $tmp "$2" }

# read global startup file if it exists

# # # # # #

prevent overwriting files turn off "you have new mail" notice set LANG variable set vim options set up aliases always do interactive rm's

# a function to exchange the names # of two files

. (Dot) or source: Runs a Startup File in the Current Shell
After you edit a startup file such as .bashrc, you do not have to log out and log in again to put the changes into effect. Instead, you can run the startup file using the . (dot) or source builtin (they are the same command). As with all other commands,

www.it-ebooks.info

Shell Basics 285

the . must be followed by a SPACE on the command line. Using . or source is similar to running a shell script, except these commands run the script as part of the current process. Consequently, when you use . or source to run a script, changes you make to variables from within the script affect the shell you run the script from. If you ran a startup file as a regular shell script and did not use the . or source builtin, the variables created in the startup file would remain in effect only in the subshell running the script—not in the shell you ran the script from. You can use the . or source command to run any shell script—not just a startup file—but undesirable side effects (such as changes in the values of shell variables you rely on) might occur. For more information refer to “Locality of Variables” on page 1008. In the following example, .bashrc sets several variables and sets PS1, the bash prompt, to the name of the host. The . builtin puts the new values into effect.
$ cat ~/.bashrc export TERM=xterm # set export PS1="$(hostname -f): " # set export CDPATH=:$HOME # add stty kill '^u' # set $ . ~/.bashrc guava: the terminal type the prompt string HOME to CDPATH string kill line to control-u

Commands That Are Symbols
The Bourne Again Shell uses the symbols (, ), [, ], and $ in a variety of ways. To minimize confusion, Table 9-1 lists the most common use of each of these symbols and the page on which it is discussed.

Table 9-1 Builtin commands that are symbols
Symbol
() $( ) (( )) $(( )) [] [[ ]]

Command
Subshell (page 295) Command substitution (page 351) Arithmetic evaluation; a synonym for let (use when the enclosed value contains an equal sign; page 1032) Arithmetic expansion (not for use with an enclosed equal sign; page 349) The test command (pages 971 and 974) Conditional expression; similar to [ ] but adds string comparisons (page 1033)

Redirecting Standard Error
Chapter 7 covered the concept of standard output and explained how to redirect standard output of a command. In addition to standard output, commands can send output to standard error. A command can send error messages to standard

www.it-ebooks.info

286 Chapter 9 The Bourne Again Shell

error to keep them from getting mixed up with the information it sends to standard output. Just as it does with standard output, by default the shell directs standard error to the screen. Unless you redirect one or the other, you might not know the difference between the output a command sends to standard output and the output it sends to standard error. This section describes the syntax used by the Bourne Again Shell to redirect standard error and to distinguish between standard output and standard error.
File descriptors

A file descriptor is the place a program sends its output to and gets its input from. When you execute a program, Linux opens three file descriptors for the program: 0 (standard input), 1 (standard output), and 2 (standard error). The redirect output symbol (> [page 234]) is shorthand for 1>, which tells the shell to redirect standard output. Similarly < (page 236) is short for 0 redirect standard error. For more information refer to “File Descriptors” on page 1003. The following examples demonstrate how to redirect standard output and standard error to different files and to the same file. When you run the cat utility with the name of a file that does not exist and the name of a file that does exist, cat sends an error message to standard error and copies the file that does exist to standard output. Unless you redirect them, both messages appear on the screen.
$ cat y This is y. $ cat x cat: x: No such file or directory $ cat x y cat: x: No such file or directory This is y.

When you redirect standard output of a command, output sent to standard error is not affected and still appears on the screen.
$ cat x y > hold cat: x: No such file or directory $ cat hold This is y.

Similarly, when you send standard output through a pipe, standard error is not affected. The following example sends standard output of cat through a pipe to tr, which in this example converts lowercase characters to uppercase. (See the tr info page for more information.) The text that cat sends to standard error is not translated because it goes directly to the screen rather than through the pipe.
$ cat x y | tr "[a-z]" "[A-Z]" cat: x: No such file or directory THIS IS Y.

The following example redirects standard output and standard error to different files. The filename following 2> tells the shell where to redirect standard error (file

www.it-ebooks.info

Shell Basics 287

descriptor 2). The filename following 1> tells the shell where to redirect standard output (file descriptor 1). You can use > in place of 1>.
$ cat x y 1> hold1 2> hold2 $ cat hold1 This is y. $ cat hold2 cat: x: No such file or directory Combining standard output and standard error

In the next example, the &> token redirects standard output and standard error to a single file:
$ cat x y &> hold $ cat hold cat: x: No such file or directory This is y.

Duplicating a file descriptor

In the next example, first 1> redirects standard output to hold, and then 2>&1 declares file descriptor 2 to be a duplicate of file descriptor 1. As a result, both standard output and standard error are redirected to hold.
$ cat x y 1> hold 2>&1 $ cat hold cat: x: No such file or directory This is y.

In this case, 1> hold precedes 2>&1. If they had been listed in the opposite order, standard error would have been made a duplicate of standard output before standard output was redirected to hold. Only standard output would have been redirected to hold in that case. The next example declares file descriptor 2 to be a duplicate of file descriptor 1 and sends the output for file descriptor 1 through a pipe to the tr command.
$ cat x y 2>&1 | tr "[a-z]" "[A-Z]" CAT: X: NO SUCH FILE OR DIRECTORY THIS IS Y. Sending errors to standard error

You can use 1>&2 to redirect standard output of a command to standard error. Shell scripts use this technique to send the output of echo to standard error. In the following script, standard output of the first echo is redirected to standard error:
$ cat message_demo echo This is an error message. 1>&2 echo This is not an error message.

If you redirect standard output of message_demo, error messages such as the one produced by the first echo appear on the screen because you have not redirected standard error. Because standard output of a shell script is frequently redirected to another file, you can use this technique to display on the screen any error messages generated by the script. The lnks script (page 979) uses this technique. You can also use the exec builtin to create additional file descriptors and to redirect standard input, standard output, and standard error of a shell script from within the script (page 1023).

www.it-ebooks.info

288 Chapter 9 The Bourne Again Shell

The Bourne Again Shell supports the redirection operators shown in Table 9-2.

Table 9-2 Redirection operators
Operator
< filename > filename

Meaning
Redirects standard input from filename. Redirects standard output to filename unless filename exists and noclobber (page 237) is set. If noclobber is not set, this redirection creates filename if it does not exist and overwrites it if it does exist. Redirects standard output to filename, even if the file exists and noclobber (page 237) is set. Redirects and appends standard output to filename; creates filename if it does not exist. Redirects standard output and standard error to filename. Duplicates standard input from file descriptor m (page 1004). Duplicates standard output or file descriptor n if specified from file descriptor m (page 1004). Closes standard input or file descriptor n if specified (page 1004). Closes standard output or file descriptor n if specified.

>| filename >> filename &> filename &m [n]&–

Writing a Simple Shell Script
A shell script is a file that holds commands that the shell can execute. The commands in a shell script can be any commands you can enter in response to a shell prompt. For example, a command in a shell script might run a Linux utility, a compiled program, or another shell script. Like the commands you give on the command line, a command in a shell script can use ambiguous file references and can have its input or output redirected from or to a file or sent through a pipe. You can also use pipes and redirection with the input and output of the script itself. In addition to the commands you would ordinarily use on the command line, control flow commands (also called control structures) find most of their use in shell scripts. This group of commands enables you to alter the order of execution of commands in a script in the same way you would alter the order of execution of statements using a structured programming language. Refer to “Control Structures” on page 971 for specifics. The shell interprets and executes the commands in a shell script, one after another. Thus a shell script enables you to simply and quickly initiate a complex series of tasks or a repetitive procedure.

chmod: Makes a File Executable
To execute a shell script by giving its name as a command, you must have permission to read and execute the file that contains the script (refer to “Access Permissions” on page 202). Read permission enables you to read the file that holds the script. Execute

www.it-ebooks.info

Shell Basics 289

$ ls -l whoson -rw-rw-r--. 1 max pubs 40 05-24 11:30 whoson $ chmod u+x whoson $ ls -l whoson -rwxrw-r--. 1 max pubs 40 05-24 11:30 whoson $ ./whoson Wed May 25 11:40:49 PDT 2011 Users Currently Logged In zach pts/7 2011-05-23 hls pts/1 2011-05-24 sam pts/12 2011-05-24 max pts/4 2011-05-24

18:17 09:59 06:29 (guava) 09:08

Figure 9-1

Using chmod to make a shell script executable

permission tells the shell and the system that the owner, group, and/or public has permission to execute the file; it implies that the content of the file is executable. When you create a shell script using an editor, the file does not typically have its execute permission set. The following example shows a file named whoson that contains a shell script:
$ cat whoson date echo "Users Currently Logged In" who $ ./whoson bash: ./whoson: Permission denied

You cannot execute whoson by giving its name as a command because you do not have execute permission for the file. The shell does not recognize whoson as an executable file and issues the error message Permission denied when you try to execute it. (See the tip on the next page if you get a command not found error message.) When you give the filename as an argument to bash (bash whoson), bash takes the argument to be a shell script and executes it. In this case bash is executable, and whoson is an argument that bash executes, so you do not need execute permission to whoson. You must have read permission. The chmod utility changes the access privileges associated with a file. Figure 9-1 shows ls with the –l option displaying the access privileges of whoson before and after chmod gives execute permission to the file’s owner. The first ls displays a hyphen (–) as the fourth character, indicating the owner does not have permission to execute the file. Next chmod gives the owner execute permission: u+x causes chmod to add (+) execute permission (x) for the owner (u). (The u stands for user, although it means the owner of the file.) The second argument is the name of the file. The second ls shows an x in the fourth position, indicating the owner has execute permission.

www.it-ebooks.info

290 Chapter 9 The Bourne Again Shell

Command not found? tip If you give the name of a shell script as a command without including the leading ./, the shell typically displays the following error message:
$ whoson bash: whoson: command not found

This message indicates the shell is not set up to search for executable files in the working directory. Enter this command instead:
$ ./whoson

The ./ tells the shell explicitly to look for an executable file in the working directory. Although not recommended for security reasons, you can change the environment so the shell searches the working directory automatically; see the section about PATH on page 308.

If other users will execute the file, you must also change group and/or public access permissions for the file. Any user must have execute access to use the file’s name as a command. If the file is a shell script, the user trying to execute the file must have read access to the file as well. You do not need read access to execute a binary executable (compiled program). The final command in Figure 9-1 shows the shell executing the file when its name is given as a command. For more information refer to “Access Permissions” on page 202 as well as the discussions of ls (page 202) and chmod (page 203).

#! Specifies a Shell
You can put a special sequence of characters on the first line of a shell script to tell the operating system which shell (or other program) should execute the file. Because the operating system checks the initial characters of a program before attempting to execute it using exec, these characters save the system from making an unsuccessful attempt. If #! are the first two characters of a script, the system interprets the characters that follow as the absolute pathname of the program that should execute the script. This pathname can point to any program, not just a shell, and can be useful if you have a script you want to run with a shell other than the shell you are running the script from. The following example specifies that bash should run the script:
$ cat bash_script #!/bin/bash echo "This is a Bourne Again Shell script."

The next example runs under Perl and can be run directly from the shell without explicitly calling Perl on the command line:
$ cat ./perl_script.pl #!/usr/bin/perl -w print "This is a Perl script.\n"; $ ./perl_script.pl This is a Perl script.

The next example shows a script that should be executed by tcsh (tcsh package):

www.it-ebooks.info

Shell Basics 291
$ cat tcsh_script #!/bin/tcsh echo "This is a tcsh script." set person = zach echo "person is $person"

Because of the #! line, the operating system ensures that tcsh executes the script no matter which shell you run it from. You can use ps –f within a shell script to display the name of the program that is executing the script. The three lines that ps displays in the following example show the process running the parent bash shell, the process running the tcsh script, and the process running the ps command:
$ cat tcsh_script2 #!/bin/tcsh ps -f $ ./tcsh_script2 UID PID PPID max 3031 3030 max 9358 3031 max 9375 9358

C 0 0 0

STIME Nov16 21:13 21:13

TTY pts/4 pts/4 pts/4

TIME 00:00:00 00:00:00 00:00:00

CMD -bash /bin/tcsh ./tcsh_script2 ps -f

If you do not follow #! with the name of an executable program, the shell reports it cannot find the program you asked it to run. You can optionally follow #! with SPACEs. If you omit the #! line and try to run, for example, a tcsh script from bash, the script will run under bash and might generate error messages or not run properly.

# Begins a Comment
Comments make shell scripts and all code easier to read and maintain by you and others. If a hashmark (#) in the first character position of the first line of a script is not immediately followed by an exclamation point (!) or if a hashmark occurs in any other location in a script, the shell interprets it as the beginning of a comment. The shell then ignores everything between the hashmark and the end of the line (the next NEWLINE character).

Executing a Shell Script fork and exec system calls

As discussed earlier, you can execute commands in a shell script file that you do not have execute permission for by using a bash command to exec a shell that runs the script directly. In the following example, bash creates a new shell that takes its input from the file named whoson:
$ bash whoson

Because the bash command expects to read a file containing commands, you do not need execute permission for whoson. (You do need read permission.) Even though bash reads and executes the commands in whoson, standard input, standard output, and standard error remain directed from/to the terminal.

www.it-ebooks.info

292 Chapter 9 The Bourne Again Shell

Although you can use bash to execute a shell script, this technique causes the script to run more slowly than giving yourself execute permission and directly invoking the script. Users typically prefer to make the file executable and run the script by typing its name on the command line. It is also easier to type the name, and this practice is consistent with the way other kinds of programs are invoked (so you do not need to know whether you are running a shell script or an executable file). However, if bash is not your interactive shell or if you want to see how the script runs with different shells, you might want to run a script as an argument to bash or tcsh.

sh does not call the original Bourne Shell caution The original Bourne Shell was invoked with the command sh. Although you can call bash with an sh command, it is not the original Bourne Shell. The sh command (/bin/sh) is a symbolic link to /bin/bash, so it is simply another name for the bash command. When you call bash using the command sh, bash tries to mimic the behavior of the original Bourne Shell as closely as possible. It does not always succeed.

Separating and Grouping Commands
Whether you give the shell commands interactively or write a shell script, you must separate commands from one another. This section reviews the ways to separate commands that were covered in Chapter 7 and introduces a few new ones.

; and NEWLINE Separate Commands
The NEWLINE character is a unique command separator because it initiates execution of the command preceding it. You have seen this behavior throughout this book each time you press the RETURN key at the end of a command line. The semicolon (;) is a command separator that does not initiate execution of a command and does not change any aspect of how the command functions. You can execute a series of commands sequentially by entering them on a single command line and separating each from the next with a semicolon (;). You initiate execution of the sequence of commands by pressing RETURN:
$ x ; y ; z

If x, y, and z are commands, the preceding command line yields the same results as the next three commands. The difference is that in the next example the shell issues a prompt after each of the commands (x, y, and z) finishes executing, whereas the preceding command line causes the shell to issue a prompt only after z is complete:
$ x $ y $ z Whitespace

Although the whitespace around the semicolons in the earlier example makes the command line easier to read, it is not necessary. None of the command separators needs to be surrounded by SPACEs or TABs.

www.it-ebooks.info

Shell Basics 293

\ Continues a Command
When you enter a long command line and the cursor reaches the right side of the screen, you can use a backslash (\) character to continue the command on the next line. The backslash quotes, or escapes, the NEWLINE character that follows it so the shell does not treat the NEWLINE as a command terminator. Enclosing a backslash within single quotation marks or preceding it with another backslash turns off the power of a backslash to quote special characters such as NEWLINE. Enclosing a backslash within double quotation marks has no effect on the power of the backslash. Although you can break a line in the middle of a word (token), it is typically simpler to break a line immediately before or after whitespace.

optional You can enter a RETURN in the middle of a quoted string on a command line without using a backslash. The NEWLINE (RETURN) you enter will then be part of the string:
$ echo "Please enter the three values > required to complete the transaction." Please enter the three values required to complete the transaction.

In the three examples in this section, the shell does not interpret RETURN as a command terminator because it occurs within a quoted string. The greater than (>) sign is a secondary prompt (PS2; page 311) indicating the shell is waiting for you to continue the unfinished command. In the next example, the first RETURN is quoted (escaped) so the shell treats it as a separator and does not interpret it literally.
$ echo "Please enter the three values \ > required to complete the transaction." Please enter the three values required to complete the transaction.

Single quotation marks cause the shell to interpret a backslash literally:
$ echo 'Please enter the three values \ > required to complete the transaction.' Please enter the three values \ required to complete the transaction.

| and & Separate Commands and Do Something Else
The pipe symbol ( |) and the background task symbol (&) are also command separators. They do not start execution of a command but do change some aspect of how the command functions. The pipe symbol alters the source of standard input or the destination of standard output. The background task symbol causes the shell to execute the task in the background and display a prompt immediately; you can continue working on other tasks. Each of the following command lines initiates a single job comprising three tasks:
$ x | y | z $ ls -l | grep tmp | less

www.it-ebooks.info

294 Chapter 9 The Bourne Again Shell

In the first job, the shell redirects standard output of task x to standard input of task y and redirects y’s standard output to z’s standard input. Because it runs the entire job in the foreground, the shell does not display a prompt until task z runs to completion: Task z does not finish until task y finishes, and task y does not finish until task x finishes. In the second job, task x is an ls –l command, task y is grep tmp, and task z is the pager less. The shell displays a long (wide) listing of the files in the working directory that contain the string tmp, piped through less. The next command line executes tasks d and e in the background and task f in the foreground:
$ d & e & f [1] 14271 [2] 14272

The shell displays the job number between brackets and the PID number for each process running in the background. It displays a prompt as soon as f finishes, which might be before d or e finishes. Before displaying a prompt for a new command, the shell checks whether any background jobs have completed. For each completed job, the shell displays its job number, the word Done, and the command line that invoked the job; the shell then displays a prompt. When the job numbers are listed, the number of the last job started is followed by a + character, and the job number of the previous job is followed by a – character. Other jobs are followed by a SPACE character. After running the last command, the shell displays the following lines before issuing a prompt:
[1][2]+ Done Done d e

The next command line executes all three tasks as background jobs. The shell displays a shell prompt immediately:
$ d [1] [2] [3] & e & f & 14290 14291 14292

You can use a pipe to send the output from one command to the next command and an ampersand (&) to run the entire job in the background. Again the shell displays the prompt immediately. The shell commands joined by a pipe form a single job. That is, it treats all pipes as single jobs, no matter how many commands are connected with the pipe (|) symbol or how complex they are. The Bourne Again Shell reports only one process in the background (although there are three):
$ d | e | f & [1] 14295

www.it-ebooks.info

Shell Basics 295

optional

( ) Groups Commands
You can use parentheses to group commands. The shell creates a copy of itself, called a subshell, for each group. It treats each group of commands as a job and creates a new process to execute each command (refer to “Process Structure” on page 316 for more information on creating subshells). Each subshell (job) has its own environment, meaning it has its own set of variables whose values can differ from those in other subshells. The following command line executes commands a and b sequentially in the background while executing c in the background. The shell displays a prompt immediately.
$ (a ; b) & c & [1] 15520 [2] 15521

The preceding example differs from the earlier example d & e & f & in that tasks a and b are initiated sequentially, not concurrently. Similarly the following command line executes a and b sequentially in the background and, at the same time, executes c and d sequentially in the background. The subshell running a and b and the subshell running c and d run concurrently. The shell displays a prompt immediately.
$ (a ; b) & (c ; d) & [1] 15528 [2] 15529

The next script copies one directory to another. The second pair of parentheses creates a subshell to run the commands following the pipe. Because of these parentheses, the output of the first tar command is available for the second tar command, despite the intervening cd command. Without the parentheses, the output of the first tar command would be sent to cd and lost because cd does not process standard input. The shell variables $1 and $2 hold the first and second command-line arguments (page 1013), respectively. The first pair of parentheses, which creates a subshell to run the first two commands, allows users to call cpdir with relative pathnames. Without them, the first cd command would change the working directory of the script (and consequently the working directory of the second cd command). With them, only the working directory of the subshell is changed.
$ cat cpdir (cd $1 ; tar -cf - . ) | (cd $2 ; tar -xvf - ) $ ./cpdir /home/max/sources /home/max/memo/biblio

The cpdir command line copies the files and directories in the /home/max/sources directory to the directory named /home/max/memo/biblio. This shell script is almost the same as using cp with the –r option. Refer to the cp and tar man pages for more information.

www.it-ebooks.info

296 Chapter 9 The Bourne Again Shell

Job Control
A job is a command pipeline. You run a simple job whenever you give the shell a command. For example, if you type date on the command line and press RETURN, you have run a job. You can also create several jobs with multiple commands on a single command line:
$ find . -print | sort | lpr & grep -l max /tmp/* > maxfiles & [1] 18839 [2] 18876

The portion of the command line up to the first & is one job consisting of three processes connected by pipes: find, sort (page 154), and lpr (page 151). The second job is a single process running grep. The trailing & characters put each job in the background, so bash does not wait for them to complete before displaying a prompt. Using job control you can move commands from the foreground to the background and vice versa, stop commands temporarily, and list all commands that are running in the background or stopped.

jobs: Lists Jobs
The jobs builtin lists all background jobs. Following, the sleep command runs in the background and creates a background job that jobs reports on:
$ sleep 60 & [1] 7809 $ jobs [1] + Running

sleep 60 &

fg: Brings a Job to the Foreground
The shell assigns a job number to each command you run in the background. For each job run in the background, the shell lists the job number and PID number immediately, just before it issues a prompt:
$ gnome-calculator & [1] 1246 $ date & [2] 1247 $ Tue Dec 7 11:44:40 PST 2010 [2]+ Done date $ find /usr -name ace -print > findout & [2] 1269 $ jobs [1]- Running gnome-calculator & [2]+ Running find /usr -name ace -print > findout &

Job numbers, which are discarded when a job is finished, can be reused. When you start or put a job in the background, the shell assigns a job number that is one more than the highest job number in use.

www.it-ebooks.info

Shell Basics 297

In the preceding example, the jobs command lists the first job, gnome-calculator, as job 1. The date command does not appear in the jobs list because it finished before jobs was run. Because the date command was completed before find was run, the find command became job 2. To move a background job to the foreground, use the fg builtin followed by the job number. Alternatively, you can give a percent sign (%) followed by the job number as a command. Either of the following commands moves job 2 to the foreground. When you move a job to the foreground, the shell displays the command it is now executing in the foreground.
$ fg 2 find /usr -name ace -print > findout

or
$ %2 find /usr -name ace -print > findout

You can also refer to a job by following the percent sign with a string that uniquely identifies the beginning of the command line used to start the job. Instead of the preceding command, you could have used either fg %find or fg %f because both uniquely identify job 2. If you follow the percent sign with a question mark and a string, the string can match any part of the command line. In the preceding example, fg %?ace also brings job 2 to the foreground. Often the job you wish to bring to the foreground is the only job running in the background or is the job that jobs lists with a plus (+). In these cases fg without an argument brings the job to the foreground.

Suspending a Job
Pressing the suspend key (usually CONTROL-Z) immediately suspends (temporarily stops) the job in the foreground and displays a message that includes the word Stopped.
CONTROL-Z

[2]+

Stopped

find /usr -name ace -print > findout

For more information refer to “Moving a Job from the Foreground to the Background” on page 243.

bg: Sends a Job to the Background
To move the foreground job to the background, you must first suspend the job (previous). You can then use the bg builtin to resume execution of the job in the background.
$ bg [2]+ find /usr -name ace -print > findout &

www.it-ebooks.info

298 Chapter 9 The Bourne Again Shell

If a background job attempts to read from the terminal, the shell stops the program and displays a message saying the job has been stopped. You must then move the job to the foreground so it can read from the terminal.
$ (sleep 5; cat > mytext) & [1] 1343 $ date Wed Dec 7 11:58:20 PST 2011 [1]+ Stopped $ fg ( sleep 5; cat >mytext ) Remember to let the cat out!
CONTROL-D

( sleep 5; cat >mytext )

$

In the preceding example, the shell displays the job number and PID number of the background job as soon as it starts, followed by a prompt. Demonstrating that you can give a command at this point, the user gives the command date, and its output appears on the screen. The shell waits until just before it issues a prompt (after date has finished) to notify you that job 1 is stopped. When you give an fg command, the shell puts the job in the foreground, and you can enter the data the command is waiting for. In this case the input needs to be terminated with CONTROL-D, which sends an EOF (end of file) signal to cat. The shell then displays another prompt. The shell keeps you informed about changes in the status of a job, notifying you when a background job starts, completes, or stops, perhaps because it is waiting for input from the terminal. The shell also lets you know when a foreground job is suspended. Because notices about a job being run in the background can disrupt your work, the shell delays displaying these notices until just before it displays a prompt. You can set notify (page 343) to cause the shell to display these notices without delay. If you try to exit from a nonlogin shell while jobs are stopped, the shell issues a warning and does not allow you to exit. If you then use jobs to review the list of jobs or you immediately try to exit from the shell again, the shell allows you to exit. If huponexit (page 343) is not set (the default), stopped jobs remain stopped and background jobs keep running in the background. If it is set, the shell terminates these jobs.

Manipulating the Directory Stack
The Bourne Again Shell allows you to store a list of directories you are working with, enabling you to move easily among them. This list is referred to as a stack. It is analogous to a stack of dinner plates: You typically add plates to and remove plates from the top of the stack, so this type of stack is named a LIFO (last in, first out) stack.

dirs: Displays the Stack
The dirs builtin displays the contents of the directory stack. If you call dirs when the directory stack is empty, it displays the name of the working directory:

www.it-ebooks.info

Shell Basics 299

home

sam

demo

names

literature

promo

Figure 9-2
$ dirs ~/literature

The directory structure in the examples

The dirs builtin uses a tilde (~) to represent the name of a user’s home directory. The examples in the next several sections assume you are referring to the directory structure shown in Figure 9-2.

pushd: Pushes a Directory on the Stack
When you supply the pushd (push directory) builtin with one argument, it pushes the directory specified by the argument on the stack, changes directories to the specified directory, and displays the stack. The following example is illustrated in Figure 9-3:
$ pushd ../demo ~/demo ~/literature $ pwd /home/sam/demo $ pushd ../names ~/names ~/demo ~/literature $ pwd /home/sam/names

names 2 pushd 1 demo 1 pushd 2 literature

Figure 9-3 Creating a directory stack

www.it-ebooks.info

300 Chapter 9 The Bourne Again Shell

pushd names demo literature demo names literature

pushd names demo literature

Figure 9-4 Using pushd to change working directories

When you call pushd without an argument, it swaps the top two directories on the stack, makes the new top directory (which was the second directory) the new working directory, and displays the stack (Figure 9-4):
$ pushd ~/demo ~/names ~/literature $ pwd /home/sam/demo

Using pushd in this way, you can easily move back and forth between two directories. You can also use cd – to change to the previous directory, whether or not you have explicitly created a directory stack. To access another directory in the stack, call pushd with a numeric argument preceded by a plus sign. The directories in the stack are numbered starting with the top directory, which is number 0. The following pushd command continues with the previous example, changing the working directory to literature and moving literature to the top of the stack:
$ pushd +2 ~/literature ~/demo ~/names $ pwd /home/sam/literature

popd: Pops a Directory Off the Stack
To remove a directory from the stack, use the popd (pop directory) builtin. As the following example and Figure 9-5 show, without an argument, popd removes the top directory from the stack and changes the working directory to the new top directory:
$ dirs ~/literature ~/demo ~/names $ popd ~/demo ~/names $ pwd /home/sam/demo

To remove a directory other than the top one from the stack, use popd with a numeric argument preceded by a plus sign. The following example removes directory number 1, demo. Removing a directory other than directory number 0 does not change the working directory.

www.it-ebooks.info

Parameters and Variables 301

literature popd demo names

Figure 9-5

Using popd to remove a directory from the stack

$ dirs ~/literature ~/demo ~/names $ popd +1 ~/literature ~/names

Parameters and Variables
Variables

Within a shell, a shell parameter is associated with a value that is accessible to the user. There are several kinds of shell parameters. Parameters whose names consist of letters, digits, and underscores are often referred to as shell variables, or simply variables. A variable name must start with a letter or underscore, not with a number. Thus A76, MY_CAT, and ___X___ are valid variable names, whereas 69TH_STREET (starts with a digit) and MY-NAME (contains a hyphen) are not. Shell variables that you name and assign values to are user-created variables. You can change the values of user-created variables at any time, or you can make them readonly so that their values cannot be changed. You can also make user-created variables global. A global variable (also called an environment variable) is available to all shells and other programs you fork from the shell in which it was created. One naming convention is to use only uppercase letters for global variables and to use mixed-case or lowercase letters for other variables. Refer to “Locality of Variables” on page 1008 for more information on global variables. To assign a value to a variable in the Bourne Again Shell, use the following syntax: VARIABLE=value There can be no whitespace on either side of the equal sign (=). An example assignment follows:
$ myvar=abc

User-created variables

The Bourne Again Shell permits you to put variable assignments on a command line. This type of assignment creates a variable that is local to the command shell—that is, the variable is accessible only from the program the command runs. The my_script shell script displays the value of TEMPDIR. The following command

www.it-ebooks.info

302 Chapter 9 The Bourne Again Shell

runs my_script with TEMPDIR set to /home/sam/temp. The echo builtin shows that the interactive shell has no value for TEMPDIR after running my_script. If TEMPDIR had been set in the interactive shell, running my_script in this manner would have had no effect on its value.
$ cat my_script echo $TEMPDIR $ TEMPDIR=/home/sam/temp ./my_script /home/sam/temp $ echo $TEMPDIR $ Keyword variables

Keyword shell variables (or simply keyword variables) have special meaning to the shell and usually have short, mnemonic names. When you start a shell (by logging in, for example), the shell inherits several keyword variables from the environment. Among these variables are HOME, which identifies your home directory, and PATH, which determines which directories the shell searches and in what order to locate commands that you give the shell. The shell creates and initializes (with default values) other keyword variables when you start it. Still other variables do not exist until you set them. You can change the values of most keyword shell variables. It is usually not necessary to change the values of keyword variables initialized in the /etc/profile or /etc/csh.cshrc systemwide startup files. If you need to change the value of a bash keyword variable, do so in one of your startup files (page 282). Just as you can make user-created variables global, so you can make keyword variables global—a task usually done automatically in startup files. You can also make a keyword variable readonly.

Positional and special parameters

The names of positional and special parameters do not resemble variable names. Most of these parameters have one-character names (for example, 1, ?, and #) and are referenced (as are all variables) by preceding the name with a dollar sign ($1, $?, and $#). The values of these parameters reflect different aspects of your ongoing interaction with the shell. Whenever you run a command, each argument on the command line becomes the value of a positional parameter (page 1012). Positional parameters enable you to access command-line arguments, a capability you will often require when you write shell scripts. The set builtin (page 1014) enables you to assign values to positional parameters. Other frequently needed shell script values, such as the name of the last command executed, the number of command-line arguments, and the status of the most recently executed command, are available as special parameters (page 1010). You cannot assign values to special parameters.

User-Created Variables
The first line in the following example declares the variable named person and initializes it with the value max:

www.it-ebooks.info

Parameters and Variables 303
$ person=max $ echo person person $ echo $person max Parameter substitution

Because the echo builtin copies its arguments to standard output, you can use it to display the values of variables. The second line of the preceding example shows that person does not represent max. Instead, the string person is echoed as person. The shell substitutes the value of a variable only when you precede the name of the variable with a dollar sign ($). Thus the command echo $person displays the value of the variable person; it does not display $person because the shell does not pass $person to echo as an argument. Because of the leading $, the shell recognizes that $person is the name of a variable, substitutes the value of the variable, and passes that value to echo. The echo builtin displays the value of the variable—not its name—never “knowing” you called it with a variable. You can prevent the shell from substituting the value of a variable by quoting the leading $. Double quotation marks do not prevent the substitution; single quotation marks or a backslash (\) do.
$ echo $person max $ echo "$person" max $ echo '$person' $person $ echo \$person $person

Quoting the $

SPACEs

Because they do not prevent variable substitution but do turn off the special meanings of most other characters, double quotation marks are useful when you assign values to variables and when you use those values. To assign a value that contains SPACEs or TABs to a variable, use double quotation marks around the value. Although double quotation marks are not required in all cases, using them is a good habit.
$ person="max and zach" $ echo $person max and zach $ person=max and zach bash: and: command not found

When you reference a variable whose value contains TABs or multiple adjacent SPACEs, you must use quotation marks to preserve the spacing. If you do not quote the variable, the shell collapses each string of blank characters into a single SPACE before passing the variable to the utility:
$ person="max and $ echo $person max and zach $ echo "$person" max and zach zach"

www.it-ebooks.info

304 Chapter 9 The Bourne Again Shell
Pathname expansion in assignments

When you execute a command with a variable as an argument, the shell replaces the name of the variable with the value of the variable and passes that value to the program being executed. If the value of the variable contains a special character, such as * or ?, the shell might expand that variable. The first line in the following sequence of commands assigns the string max* to the variable memo. The Bourne Again Shell does not expand the string because bash does not perform pathname expansion (page 244) when it assigns a value to a variable. All shells process a command line in a specific order. Within this order bash expands variables before it interprets commands. In the following echo command line, the double quotation marks quote the asterisk (*) in the expanded value of $memo and prevent bash from performing pathname expansion on the expanded memo variable before passing its value to the echo command:
$ memo=max* $ echo "$memo" max*

All shells interpret special characters as special when you reference a variable that contains an unquoted special character. In the following example, the shell expands the value of the memo variable because it is not quoted:
$ ls max.report max.summary $ echo $memo max.report max.summary

Here the shell expands the $memo variable to max*, expands max* to max.report and max.summary, and passes these two values to echo.

optional
Braces

The $VARIABLE syntax is a special case of the more general syntax ${VARIABLE}, in which the variable name is enclosed by ${}. The braces insulate the variable name from adjacent characters. Braces are necessary when catenating a variable value with a string:
$ $ $ $ $ PREF=counter WAY=$PREFclockwise FAKE=$PREFfeit echo $WAY $FAKE

The preceding example does not work as expected. Only a blank line is output because although the symbols PREFclockwise and PREFfeit are valid variable names, they are not set. By default bash evaluates an unset variable as an empty (null) string and displays this value. To achieve the intent of these statements, refer to the PREF variable using braces:

www.it-ebooks.info

Parameters and Variables 305
$ PREF=counter $ WAY=${PREF}clockwise $ FAKE=${PREF}feit $ echo $WAY $FAKE counterclockwise counterfeit

The Bourne Again Shell refers to the arguments on its command line by position, using the special variables $1, $2, $3, and so forth up to $9. If you wish to refer to arguments past the ninth argument, you must use braces: ${10}. The name of the command is held in $0 (page 1013).

unset: Removes a Variable
Unless you remove a variable, it exists as long as the shell in which it was created exists. To remove the value of a variable but not the variable itself, assign a null value to the variable:
$ person= $ echo $person $

You can remove a variable using the unset builtin. The following command removes the variable person:
$ unset person

Variable Attributes
This section discusses attributes and explains how to assign them to variables.

readonly: Makes the Value of a Variable Permanent
You can use the readonly builtin to ensure that the value of a variable cannot be changed. The next example declares the variable person to be readonly. You must assign a value to a variable before you declare it to be readonly; you cannot change its value after the declaration. When you attempt to unset or change the value of a readonly variable, the shell displays an error message:
$ person=zach $ echo $person zach $ readonly person $ person=helen bash: person: readonly variable

If you use the readonly builtin without an argument, it displays a list of all readonly shell variables. This list includes keyword variables that are automatically set as readonly as well as keyword or user-created variables that you have declared as readonly. See page 306 for an example (readonly and declare –r produce the same output).

www.it-ebooks.info

306 Chapter 9 The Bourne Again Shell

declare and typeset: Assign Attributes to Variables
The declare and typeset builtins (two names for the same command) set attributes and values for shell variables. Table 9-3 lists five of these attributes.

Table 9-3 Variable attributes (typeset or declare)
Attribute
–a –f –i –r –x

Meaning
Declares a variable as an array (page 1006) Declares a variable to be a function name (page 338) Declares a variable to be of type integer (page 307) Makes a variable readonly; also readonly (page 305) Exports a variable (makes it global); also export (page 1008)

The following commands declare several variables and set some attributes. The first line declares person1 and assigns it a value of max. This command has the same effect with or without the word declare.
$ $ $ $ declare declare declare declare person1=max -r person2=zach -rx person3=helen -x person4

The readonly and export builtins are synonyms for the commands declare –r and declare –x, respectively. You can declare a variable without assigning a value to it, as the preceding declaration of the variable person4 illustrates. This declaration makes person4 available to all subshells (i.e., makes it global). Until an assignment is made to the variable, it has a null value. You can list the options to declare separately in any order. The following is equivalent to the preceding declaration of person3:
$ declare -x -r person3=helen

Use the + character in place of – when you want to remove an attribute from a variable. You cannot remove the readonly attribute. After the following command is given, the variable person3 is no longer exported, but it is still readonly.
$ declare +x person3

You can use typeset in place of declare.
Listing variable attributes

Without any arguments or options, declare lists all shell variables. The same list is output when you run set (page 1014) without any arguments. If you use a declare builtin with options but no variable names as arguments, the command lists all shell variables that have the indicated attributes set. For example, the command declare –r displays a list of all readonly shell variables. This list is the same as that produced by the readonly command without any arguments. After the declarations in the preceding example have been given, the results are as follows:

www.it-ebooks.info

Parameters and Variables 307
$ declare -r declare -r BASHOPTS="checkwinsize:cmdhist:expand_aliases: ... " declare -ir BASHPID declare -ar BASH_VERSINFO='([0]="4" [1]="2" [2]="8" [3]="1" ... ' declare -ir EUID="500" declare -ir PPID="1936" declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand: ... " declare -ir UID="500" declare -r person2="zach" declare -rx person3="helen"

The first seven entries are keyword variables that are automatically declared as readonly. Some of these variables are stored as integers (–i). The –a option indicates that BASH_VERSINFO is an array variable; the value of each element of the array is listed to the right of an equal sign.
Integer

By default the values of variables are stored as strings. When you perform arithmetic on a string variable, the shell converts the variable into a number, manipulates it, and then converts it back to a string. A variable with the integer attribute is stored as an integer. Assign the integer attribute as follows:
$ declare -i COUNT

Keyword Variables
Keyword variables are either inherited or declared and initialized by the shell when it starts. You can assign values to these variables from the command line or from a startup file. Typically you want these variables to apply to all subshells you start as well as to your login shell. For those variables not automatically exported by the shell, you must use export (page 1008) to make them available to child shells.

HOME: Your Home Directory
By default your home directory is the working directory when you log in. Your home directory is established when your account is set up; its name is stored in the /etc/passwd file.
$ grep sam /etc/passwd sam:x:500:500:Sam the Great:/home/sam:/bin/bash

When you log in, the shell inherits the pathname of your home directory and assigns it to the variable HOME. When you give a cd command without an argument, cd makes the directory whose name is stored in HOME the working directory:
$ pwd /home/max/laptop $ echo $HOME /home/max $ cd $ pwd /home/max

www.it-ebooks.info

308 Chapter 9 The Bourne Again Shell

This example shows the value of the HOME variable and the effect of the cd builtin. After you execute cd without an argument, the pathname of the working directory is the same as the value of HOME: your home directory.
Tilde (~)

The shell uses the value of HOME to expand pathnames that use the shorthand tilde (~) notation (page 193) to denote a user’s home directory. The following example uses echo to display the value of this shortcut and then uses ls to list the files in Max’s laptop directory, which is a subdirectory of his home directory:
$ echo ~ /home/max $ ls ~/laptop tester count

lineup

PATH: Where the Shell Looks for Programs
When you give the shell an absolute or relative pathname rather than a simple filename as a command, it looks in the specified directory for an executable file with the specified filename. If the file with the pathname you specified does not exist, the shell reports command not found. If the file exists as specified but you do not have execute permission for it, or in the case of a shell script you do not have read and execute permission for, the shell reports Permission denied. If you give a simple filename as a command, the shell searches through certain directories (your search path) for the program you want to execute. It looks in several directories for a file that has the same name as the command and that you have execute permission for (a compiled program) or read and execute permission for (a shell script). The PATH shell variable controls this search. The default value of PATH is determined when bash is compiled. It is not set in a startup file, although it might be modified there. Normally the default specifies that the shell search several system directories used to hold common commands. These system directories include /bin and /usr/bin and other directories appropriate to the local system. When you give a command, if the shell does not find the executable—and, in the case of a shell script, readable—file named by the command in any of the directories listed in PATH, the shell generates one of the aforementioned error messages.
Working directory

The PATH variable specifies the directories in the order the shell should search them. Each directory must be separated from the next by a colon. The following command sets PATH so a search for an executable file starts with the /usr/local/bin directory. If it does not find the file in this directory, the shell looks next in /bin and then in /usr/bin. If the search fails in those directories, the shell looks in the ~/bin directory, a subdirectory of the user’s home directory. Finally the shell looks in the working directory. Exporting PATH makes its value accessible to subshells:
$ export PATH=/usr/local/bin:/bin:/usr/bin:~/bin:

A null value in the string indicates the working directory. In the preceding example, a null value (nothing between the colon and the end of the line) appears as the last

www.it-ebooks.info

Parameters and Variables 309

element of the string. The working directory is represented by a leading colon (not recommended; see the following security tip), a trailing colon (as in the example), or two colons next to each other anywhere in the string. You can also represent the working directory explicitly with a period (.). Because Linux stores many executable files in directories named bin (binary), users typically put their executable files in their own ~/bin directories. If you put your own bin directory at the end of your PATH, as in the preceding example, the shell looks there for any commands that it cannot find in directories listed earlier in PATH.

PATH and security security Do not put the working directory first in PATH when security is a concern. If you are working as root, you should never put the working directory first in PATH. It is common for root’s PATH to omit the working directory entirely. You can always execute a file in the working directory by prepending ./ to the name: ./myprog. Putting the working directory first in PATH can create a security hole. Most people type ls as the first command when entering a directory. If the owner of a directory places an executable file named ls in the directory, and the working directory appears first in a user’s PATH, the user giving an ls command from the directory executes the ls program in the working directory instead of the system ls utility, possibly with undesirable results.

If you want to add directories to PATH, you can reference the old value of the PATH variable in setting PATH to a new value (but see the preceding security tip). The following command adds /usr/local/bin to the beginning of the current PATH and the bin directory in the user’s home directory (~/bin) to the end:
$ PATH=/usr/local/bin:$PATH:~/bin

MAIL: Where Your Mail Is Kept
The MAIL variable contains the pathname of the file that holds your mail (your mailbox, usually /var/mail/name, where name is your username). If MAIL is set and MAILPATH (next) is not set, the shell informs you when mail arrives in the file specified by MAIL. In a graphical environment you can unset MAIL so the shell does not display mail reminders in a terminal emulator window (assuming you are using a graphical mail program). The MAILPATH variable contains a list of filenames separated by colons. If this variable is set, the shell informs you when any one of the files is modified (for example, when mail arrives). You can follow any of the filenames in the list with a question mark (?) and a message. The message replaces the you have mail message when you receive mail while you are logged in. The MAILCHECK variable specifies how often, in seconds, the shell checks for new mail. The default is 60 seconds. If you set this variable to zero, the shell checks before each prompt.

www.it-ebooks.info

310 Chapter 9 The Bourne Again Shell

PS1: User Prompt (Primary)
The default Bourne Again Shell prompt is a dollar sign ($). When you run bash with root privileges, bash typically displays a hashmark (#) prompt. The PS1 variable holds the prompt string the shell uses to let you know it is waiting for a command. When you change the value of PS1, you change the appearance of your prompt. You can customize the prompt displayed by PS1. For example, the assignment
$ PS1="[\u@\h \W \!]$ "

displays the following prompt: [user@host directory event]$ where user is the username, host is the hostname up to the first period, directory is the basename of the working directory, and event is the event number (page 320) of the current command. If you are working on more than one system, it can be helpful to incorporate the system name into your prompt. For example, you might change the prompt to the name of the system you are using, followed by a colon and a SPACE (a SPACE at the end of the prompt makes the commands you enter after the prompt easier to read). This command uses command substitution (page 351) in the string assigned to PS1:
$ PS1="$(hostname): " guava: echo test test guava:

The first example that follows changes the prompt to the name of the local host, a SPACE, and a dollar sign (or, if the user is running with root privileges, a hashmark). The second example changes the prompt to the time followed by the name of the user. The third example changes the prompt to the one used in this book (a hashmark for a user running with root privileges and a dollar sign otherwise):
$ PS1='\h \$ ' guava $ $ PS1='\@ \u $ ' 09:44 PM max $ $ PS1='\$ ' $

Table 9-4 describes some of the symbols you can use in PS1. For a complete list of special characters you can use in the prompt strings, open the bash man page and search for the third occurrence of PROMPTING (enter the command /PROMPTING and then press n two times).

www.it-ebooks.info

Parameters and Variables 311

Table 9-4 PS1 symbols
Symbol
\$ \w \W \! \d \h \H \u \@ \T \A \t

Display in prompt
# if the user is running with root privileges; otherwise, $ Pathname of the working directory Basename of the working directory Current event (history) number (page 324) Date in Weekday Month Date format Machine hostname, without the domain Full machine hostname, including the domain Username of the current user Current time of day in 12-hour, AM/PM format Current time of day in 12-hour HH:MM:SS format Current time of day in 24-hour HH:MM format Current time of day in 24-hour HH:MM:SS format

PS2: User Prompt (Secondary)
The PS2 variable holds the secondary prompt. On the first line of the next example, an unclosed quoted string follows echo. The shell assumes the command is not finished and on the second line gives the default secondary prompt (>). This prompt indicates the shell is waiting for the user to continue the command line. The shell waits until it receives the quotation mark that closes the string and then executes the command:
$ echo "demonstration of prompt string > 2" demonstration of prompt string 2 $ PS2="secondary prompt: " $ echo "this demonstrates secondary prompt: prompt string 2" this demonstrates prompt string 2

The second command changes the secondary prompt to secondary prompt: followed by a SPACE. A multiline echo demonstrates the new prompt.

PS3: Menu Prompt
The PS3 variable holds the menu prompt for the select control structure (page 1000).

www.it-ebooks.info

312 Chapter 9 The Bourne Again Shell

PS4: Debugging Prompt
The PS4 variable holds the bash debugging symbol (page 982).

IFS: Separates Input Fields (Word Splitting)
The IFS (Internal Field Separator) shell variable specifies the characters you can use to separate arguments on a command line. It has the default value of SPACE TAB NEWLINE. Regardless of the value of IFS, you can always use one or more SPACE or TAB characters to separate arguments on the command line, provided these characters are not quoted or escaped. When you assign IFS character values, these characters can also separate fields—but only if they undergo expansion. This type of interpretation of the command line is called word splitting.

Be careful when changing IFS caution Changing IFS has a variety of side effects, so work cautiously. You might find it useful to save the value of IFS before changing it. Then you can easily restore the original value if you get unexpected results. Alternatively, you can fork a new shell using a bash command before experimenting with IFS; if you get into trouble, you can exit back to the old shell, where IFS is working properly.

The following example demonstrates how setting IFS can affect the interpretation of a command line:
$ a=w:x:y:z $ cat $a cat: w:x:y:z: No such file or directory $ IFS=":" $ cat $a cat: w: No cat: x: No cat: y: No cat: z: No

such such such such

file file file file

or or or or

directory directory directory directory

The first time cat is called, the shell expands the variable a, interpreting the string w:x:y:z as a single word to be used as the argument to cat. The cat utility cannot find a file named w:x:y:z and reports an error for that filename. After IFS is set to a colon (:), the shell expands the variable a into four words, each of which is an argument to cat. Now cat reports errors for four files: w, x, y, and z. Word splitting based on the colon (:) takes place only after the variable a is expanded. The shell splits all expanded words on a command line according to the separating characters found in IFS. When there is no expansion, there is no splitting. Consider the following commands:
$ IFS="p" $ export VAR

Although IFS is set to p, the p on the export command line is not expanded, so the word export is not split.

www.it-ebooks.info

Parameters and Variables 313

The following example uses variable expansion in an attempt to produce an export command:
$ IFS="p" $ aa=export $ echo $aa ex ort

This time expansion occurs, so the p in the token export is interpreted as a separator (as the echo command shows). Now when you try to use the value of the aa variable to export the VAR variable, the shell parses the $aa VAR command line as ex ort VAR. The effect is that the command line starts the ex editor with two filenames: ort and VAR.
$ $aa VAR 2 files to edit "ort" [New File] Entering Ex mode. Type "visual" to go to Normal mode. :q E173: 1 more file to edit :q $

If you unset IFS, only SPACEs and TABs work as field separators.

Multiple separator characters tip Although the shell treats sequences of multiple SPACE or TAB characters as a single separator, it treats each occurrence of another field-separator character as a separator.

CDPATH: Broadens the Scope of cd
The CDPATH variable allows you to use a simple filename as an argument to the cd builtin to change the working directory to a directory other than a child of the working directory. If you have several directories you typically work in, this variable can speed things up and save you the tedium of using cd with longer pathnames to switch among them. When CDPATH is not set and you specify a simple filename as an argument to cd, cd searches the working directory for a subdirectory with the same name as the argument. If the subdirectory does not exist, cd displays an error message. When CDPATH is set, cd searches for an appropriately named subdirectory in the directories in the CDPATH list. If it finds one, that directory becomes the working directory. With CDPATH set, you can use cd and a simple filename to change the working directory to a child of any of the directories listed in CDPATH. The CDPATH variable takes on the value of a colon-separated list of directory pathnames (similar to the PATH variable). It is usually set in the ~/.bash_profile startup file with a command line such as the following: export CDPATH=$HOME:$HOME/literature

www.it-ebooks.info

314 Chapter 9 The Bourne Again Shell

This command causes cd to search your home directory, the literature directory, and then the working directory when you give a cd command. If you do not include the working directory in CDPATH, cd searches the working directory if the search of all the other directories in CDPATH fails. If you want cd to search the working directory first, include a null string, represented by two colons (::), as the first entry in CDPATH: export CDPATH=::$HOME:$HOME/literature

If the argument to the cd builtin is anything other than a simple filename—one that contains a slash (/)—the shell does not consult CDPATH.

Keyword Variables: A Summary
Table 9-5 presents a list of bash keyword variables.

Table 9-5 bash keyword variables
Variable
BASH_ENV CDPATH COLUMNS FCEDIT HISTFILE HISTFILESIZE HISTSIZE HOME IFS INPUTRC LANG LC_*

Value
The pathname of the startup file for noninteractive shells (page 283) The cd search path (page 313) The width of the display used by select (page 999) The name of the editor that fc uses by default (page 322) The pathname of the file that holds the history list (default: ~/.bash_history; page 319) The maximum number of entries saved in HISTFILE (default: 1000; page 319) The maximum number of entries saved in the history list (default: 1000; page 319) The pathname of the user’s home directory (page 307); used as the default argument for cd and in tilde expansion (page 193) Internal Field Separator (page 312); used for word splitting (page 352) The pathname of the Readline startup file (default: ~/.inputrc; page 332) The locale category when that category is not specifically set with an LC_* variable A group of variables that specify locale categories including LC_COLLATE, LC_CTYPE, LC_MESSAGES, and LC_NUMERIC; use the locale builtin to display a complete list with values The height of the display used by select (page 999) The pathname of the file that holds a user’s mail (page 309) How often, in seconds, bash checks for mail (default 60; page 309)

LINES MAIL MAILCHECK

www.it-ebooks.info

Special Characters 315

Table 9-5 bash keyword variables (continued)
Variable
MAILPATH PATH

Value
A colon-separated list of file pathnames that bash checks for mail in (page 309) A colon-separated list of directory pathnames that bash looks for commands in (page 308)

PROMPT_COMMAND A command that bash executes just before it displays the primary prompt PS1 PS2 PS3 PS4 REPLY Prompt String 1; the primary prompt (page 310) Prompt String 2; the secondary prompt (default: > ; page 311) The prompt issued by select (page 999) The bash debugging symbol (page 982) Holds the line that read accepts (page 1020); also used by select (page 999)

Special Characters
Table 9-6 lists most of the characters that are special to the bash shell.

Table 9-6 Shell special characters
Character
NEWLINE

Use
Initiates execution of a command (page 292) Separates commands (page 292) Groups commands (page 295) for execution by a subshell or identifies a function (page 338) Expands an arithmetic expression (page 349) Executes a command in the background (pages 242 and 293) Sends standard output of the preceding command to standard input of the following command (pipe; page 293) Redirects standard output (page 234) Appends standard output (page 238) Redirects standard input (page 236) Here document (page 1001) Any string of zero or more characters in an ambiguous file reference (page 246)

; () (( )) & | > >> < .process.out 2>&1 & }

The next example creates a simple function that displays the date, a header, and a list of the people who are logged in on the system. This function runs the same

www.it-ebooks.info

Functions 339

commands as the whoson script described on page 289. In this example the function is being entered from the keyboard. The greater than (>) signs are secondary shell prompts (PS2); do not enter them.
$ function whoson () > { > date > echo "Users Currently Logged On" > who > } $ whoson Tue Aug 9 15:44:58 PDT 2011 Users Currently Logged On hls console 2011-08-08 08:59 max pts/4 2011-08-08 09:33 zach pts/7 2011-08-08 09:23 Functions in startup files

(:0) (0.0) (guava)

If you want the whoson function to be available without having to enter it each time you log in, put its definition in ~/.bash_profile. Then run .bash_profile, using the . (dot) command to put the changes into effect immediately:
$ cat ~/.bash_profile export TERM=vt100 stty kill '^u' whoson () { date echo "Users Currently Logged On" who } $ . ~/.bash_profile

You can specify arguments when you call a function. Within the function these arguments are available as positional parameters (page 1012). The following example shows the arg1 function entered from the keyboard:
$ arg1 ( ) { > echo "$1" > } $ arg1 first_arg first_arg

See the function switch () on page 284 for another example of a function. “Functions” on page 1009 discusses the use of local and global variables within a function.

www.it-ebooks.info

340 Chapter 9 The Bourne Again Shell

optional The following function allows you to export variables using tcsh syntax. The env builtin lists all environment variables and their values and verifies that setenv worked correctly:
$ cat .bash_profile ... # setenv - keep tcsh users happy function setenv() { if [ $# -eq 2 ] then eval $1=$2 export $1 else echo "Usage: setenv NAME VALUE" 1>&2 fi } $ . ~/.bash_profile $ setenv TCL_LIBRARY /usr/local/lib/tcl $ env | grep TCL_LIBRARY TCL_LIBRARY=/usr/local/lib/tcl eval

The $# special parameter (page 1013) takes on the value of the number of command-line arguments. This function uses the eval builtin to force bash to scan the command $1=$2 twice. Because $1=$2 begins with a dollar sign ($), the shell treats the entire string as a single token—a command. With variable substitution performed, the command name becomes TCL_LIBRARY=/usr/local/lib/tcl, which results in an error. With eval, a second scanning splits the string into the three desired tokens, and the correct assignment occurs.

Controlling bash: Features and Options
This section explains how to control bash features and options using command-line options and the set and shopt builtins.

Command-Line Options
Short and long command-line options are available. Short options consist of a hyphen followed by a letter; long options have two hyphens followed by multiple characters. Long options must appear before short options on a command line that calls bash. Table 9-12 lists some commonly used command-line options.

Table 9-12
Option
Help No edit

Command-line options Explanation
Displays a usage message. Prevents users from using the Readline Library (page 328) to edit command lines in an interactive shell.

Syntax
––help ––noediting

www.it-ebooks.info

Controlling bash: Features and Options 341

Table 9-12
Option
No profile

Command-line options (continued) Explanation
Prevents reading these startup files (page 282): /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile. Prevents reading the ~/.bashrc startup file (page 283). This option is on by default if the shell is called as sh. Runs bash in POSIX mode. Displays bash version information and exits. Causes bash to run as though it were a login shell. Runs a shell with the opt shopt option (page 344). A –O (uppercase “O”) sets the option; +O unsets it. On the command line, signals the end of options. Subsequent tokens are treated as arguments even if they begin with a hyphen (–).

Syntax
––noprofile

No rc

––norc

POSIX Version Login shopt End of options

––posix ––version –l (lowercase “l”) [±]O [opt] ––

Shell Features
You can control the behavior of the Bourne Again Shell by turning features on and off. Different features use different methods to turn themselves on and off. The set builtin controls one group of features, and the shopt builtin controls another group. You can also control many features from the command line you use to call bash.

Features, options, variables? tip To avoid confusing terminology, this book refers to the various shell behaviors that you can control as features. The bash info page refers to them as “options” and “values of variables controlling optional shell behavior.”

set ±o: Turns Shell Features On and Off
The set builtin, when used with the –o or +o option, enables, disables, and lists certain bash features. For example, the following command turns on the noclobber feature (page 237):
$ set -o noclobber

You can turn this feature off (the default) by giving the command
$ set +o noclobber

The command set –o without an option lists each of the features controlled by set, followed by its state (on or off). The command set +o without an option lists the same features in a form you can use as input to the shell. Table 9-13 on the next page lists bash features.

www.it-ebooks.info

342 Chapter 9 The Bourne Again Shell

Table 9-13
Feature
allexport

bash features
Description
Automatically exports all variables and functions you create or modify after giving this command (default is off). Causes bash to perform brace expansion (default is on; page 346). Corrects minor spelling errors in directory names used as arguments to cd (default is off). Saves all lines of a multiline command in the same history entry, adding semicolons as needed (default is on). Causes shell special characters (wildcards; page 244) in an ambiguous file reference to match a leading period in a filename. By default special characters do not match a leading period. You must always specify the filenames . and .. explicitly because no pattern ever matches them (default is off). Specifies emacs editing mode for commandline editing (default is on; page 329). Causes bash to exit when a simple command (not a control structure) fails (default is off). Causes a shell script to continue running when it cannot find the file that is given as an argument to exec. By default a script terminates when exec cannot find the file that is given as its argument (default is off). Causes aliases (page 334) to be expanded (default is on for interactive shells and off for noninteractive shells). Causes bash to remember where commands it has found using PATH (page 308) are located (default is on). Causes bash to append the history list to the file named by HISTFILE (page 319) when the shell exits (default is off [bash overwrites this file]). Turns on the history mechanism (which uses exclamation points by default; page 323). Turn this feature off to turn off history expansion (default is on).

Syntax set –o allexport

Alternate syntax set –a

braceexpand cdspell cmdhist

set –o braceexpand shopt –s cdspell shopt –s cmdhist

set –B

dotglob

shopt –s dotglob

emacs errexit execfail

set –o emacs set –o errexit shopt –s execfail set –e

expand_aliases

shopt –s expand_alias

hashall

set –o hashall

set –h

histappend

shopt –s histappend

histexpand

set –o histexpand

set –H

www.it-ebooks.info

Controlling bash: Features and Options 343

Table 9-13
Feature
history huponexit

bash features (continued)
Description
Enables command history (default is on; page 319). Specifies that bash send a SIGHUP signal to all jobs when an interactive login shell exits (default is off). Specifies that bash must receive ten EOF characters before it exits. Useful on noisy dial-up lines (default is off). Enables job control (default is on; page 296). Causes ambiguous file references (page 244) to match filenames without regard to case (default is off). Helps prevent overwriting files (default is off; page 237). Disables pathname expansion (default is off; page 244). With job control (page 296) enabled, reports the termination status of background jobs immediately (default is off: bash displays the status just before the next prompt). Displays an error and exits from a shell script when you use an unset variable in an interactive shell (default is off: bash displays a null value for an unset variable). Causes bash to expand ambiguous file references (page 244) that do not match a filename to a null string (default is off: bash passes these file references without expanding them). Runs bash in POSIX mode (default is off). Displays command lines as bash reads them (default is off). Specifies vi editing mode for commandline editing (default is off; page 328). Causes the echo builtin to expand backslash escape sequences without the need for the –e option (default is off; page 996). Turns on shell debugging (default is off; page 982).

Syntax set –o history shopt –s huponexit

Alternate syntax

ignoreeof

set –o ignoreeof

monitor nocaseglob

set –o monitor shopt –s nocaseglob

set –m

noclobber noglob notify

set –o noclobber set –o noglob set –o notify

set –C set –f set –b

nounset

set –o nounset

set –u

nullglob

shopt –s nullglob

posix verbose vi xpg_echo

set –o posix set –o verbose set –o vi shopt –s xpg_echo set –v

xtrace

set –o xtrace

set –x

www.it-ebooks.info

344 Chapter 9 The Bourne Again Shell

shopt: Turns Shell Features On and Off
The shopt (shell option) builtin enables, disables, and lists certain bash features that control the behavior of the shell. For example, the following command causes bash to include filenames that begin with a period (.) when it expands ambiguous file references (the –s stands for set):
$ shopt -s dotglob

You can turn this feature off (the default) by giving the following command (the –u stands for unset):
$ shopt -u dotglob

The shell displays how a feature is set if you give the name of the feature as the only argument to shopt:
$ shopt dotglob dotglob off

Without any options or arguments, shopt lists the features it controls and their states. The command shopt –s without an argument lists the features controlled by shopt that are set or on. The command shopt –u lists the features that are unset or off. Table 9-13 on the previous page lists bash features.

Setting set ±o features using shopt tip You can use shopt to set/unset features that are otherwise controlled by set ±o. Use the regular shopt syntax with –s or –u and include the –o option. For example, the following command turns on the noclobber feature:
$ shopt -o -s noclobber

Processing the Command Line
Whether you are working interactively or running a shell script, bash needs to read a command line before it can start processing it—bash always reads at least one line before processing a command. Some bash builtins, such as if and case, as well as functions and quoted strings, span multiple lines. When bash recognizes a command that covers more than one line, it reads the entire command before processing it. In interactive sessions, bash prompts with the secondary prompt (PS2, > by default; page 311) as you type each line of a multiline command until it recognizes the end of the command:
$ echo 'hi > end' hi end

www.it-ebooks.info

Processing the Command Line 345
$ function hello () { > echo hello there > } $

After reading a command line, bash applies history expansion and alias substitution to the line.

History Expansion
“Re-executing and Editing Commands” on page 320 discusses the commands you can give to modify and re-execute command lines from the history list. History expansion is the process bash uses to turn a history command into an executable command line. For example, when you enter the command !!, history expansion changes that command line so it is the same as the previous one. History expansion is turned on by default for interactive shells; set +o histexpand turns it off. History expansion does not apply to noninteractive shells (shell scripts).

Alias Substitution
Aliases (page 334) substitute a string for the first word of a simple command. By default aliases are turned on for interactive shells and off for noninteractive shells. Enter the command shopt –u expand_aliases to turn aliases off.

Parsing and Scanning the Command Line
After processing history commands and aliases, bash does not execute the command immediately. One of the first things the shell does is to parse (isolate strings of characters in) the command line into tokens or words. The shell then scans each token for special characters and patterns that instruct the shell to take certain actions. These actions can involve substituting one word or words for another. When the shell parses the following command line, it breaks it into three tokens (cp, ~/letter, and .):
$ cp ~/letter .

After separating tokens and before executing the command, the shell scans the tokens and performs command-line expansion.

Command-Line Expansion
Both interactive and noninteractive shells transform the command line using commandline expansion before passing the command line to the program being called. You can use a shell without knowing much about command-line expansion, but you can use what a shell has to offer to a better advantage with an understanding of this topic. This section covers Bourne Again Shell command-line expansion. The Bourne Again Shell scans each token for the various types of expansion and substitution in the following order. Most of these processes expand a word into

www.it-ebooks.info

346 Chapter 9 The Bourne Again Shell

a single word. Only brace expansion, word splitting, and pathname expansion can change the number of words in a command (except for the expansion of the variable "$@"—see page 1016). 1. Brace expansion (below) 2. Tilde expansion (page 348) 3. Parameter and variable expansion (page 348) 4. Arithmetic expansion (page 349) 5. Command substitution (page 351) 6. Word splitting (page 352) 7. Pathname expansion (page 352) 8. Process substitution (page 354)
Quote removal

After bash finishes with the preceding list, it removes from the command line single quotation marks, double quotation marks, and backslashes that are not a result of an expansion. This process is called quote removal.

Order of Expansion
The order in which bash carries out these steps affects the interpretation of commands. For example, if you set a variable to a value that looks like the instruction for output redirection and then enter a command that uses the variable’s value to perform redirection, you might expect bash to redirect the output.
$ SENDIT="> /tmp/saveit" $ echo xxx $SENDIT xxx > /tmp/saveit $ cat /tmp/saveit cat: /tmp/saveit: No such file or directory

In fact, the shell does not redirect the output—it recognizes input and output redirection before it evaluates variables. When it executes the command line, the shell checks for redirection and, finding none, evaluates the SENDIT variable. After replacing the variable with > /tmp/saveit, bash passes the arguments to echo, which dutifully copies its arguments to standard output. No /tmp/saveit file is created. The following sections provide more detailed descriptions of the steps involved in command processing. Keep in mind that double and single quotation marks cause the shell to behave differently when performing expansions. Double quotation marks permit parameter and variable expansion but suppress other types of expansion. Single quotation marks suppress all types of expansion.

Brace Expansion
Brace expansion, which originated in the C Shell, provides a convenient way to specify filenames when pathname expansion does not apply. Although brace expansion is almost always used to specify filenames, the mechanism can be

www.it-ebooks.info

Processing the Command Line 347

used to generate arbitrary strings; the shell does not attempt to match the brace notation with the names of existing files. Brace expansion is turned on in interactive and noninteractive shells by default; you can turn it off with set +o braceexpand. The shell also uses braces to isolate variable names (page 304). The following example illustrates how brace expansion works. The ls command does not display any output because there are no files in the working directory. The echo builtin displays the strings the shell generates with brace expansion. In this case the strings do not match filenames (because there are no files in the working directory).
$ ls $ echo chap_{one,two,three}.txt chap_one.txt chap_two.txt chap_three.txt

The shell expands the comma-separated strings inside the braces in the echo command into a SPACE-separated list of strings. Each string from the list is prepended with the string chap_, called the preamble, and appended with the string .txt, called the postscript. Both the preamble and the postscript are optional. The left-to-right order of the strings within the braces is preserved in the expansion. For the shell to treat the left and right braces specially and for brace expansion to occur, at least one comma and no unquoted whitespace characters must be inside the braces. You can nest brace expansions. Brace expansion is useful when there is a long preamble or postscript. The following example copies four files—main.c, f1.c, f2.c, and tmp.c—located in the /usr/local/src/C directory to the working directory:
$ cp /usr/local/src/C/{main,f1,f2,tmp}.c .

You can also use brace expansion to create directories with related names:
$ ls -F file1 file2 file3 $ mkdir vrs{A,B,C,D,E} $ ls -F file1 file2 file3 vrsA/

vrsB/

vrsC/

vrsD/

vrsE/

The –F option causes ls to display a slash (/) after a directory and an asterisk (*) after an executable file. If you tried to use an ambiguous file reference instead of braces to specify the directories, the result would be different (and not what you wanted):
$ rmdir vrs* $ mkdir vrs[A-E] $ ls -F file1 file2 file3

vrs[A-E]/

An ambiguous file reference matches the names of existing files. In the preceding example, because it found no filenames matching vrs[A–E], bash passed the

www.it-ebooks.info

348 Chapter 9 The Bourne Again Shell

ambiguous file reference to mkdir, which created a directory with that name. Brackets in ambiguous file references are discussed on page 247.

Tilde Expansion
Chapter 6 introduced a shorthand notation to specify your home directory or the home directory of another user. This section provides a more detailed explanation of tilde expansion. The tilde (~) is a special character when it appears at the start of a token on a command line. When it sees a tilde in this position, bash looks at the following string of characters—up to the first slash (/) or to the end of the word if there is no slash—as a possible username. If this possible username is null (that is, if the tilde appears as a word by itself or if it is immediately followed by a slash), the shell substitutes the value of the HOME variable for the tilde. The following example demonstrates this expansion, where the last command copies the file named letter from Max’s home directory to the working directory:
$ echo $HOME /home/max $ echo ~ /home/max $ echo ~/letter /home/max/letter $ cp ~/letter .

If the string of characters following the tilde forms a valid username, the shell substitutes the path of the home directory associated with that username for the tilde and name. If the string is not null and not a valid username, the shell does not make any substitution:
$ echo ~zach /home/zach $ echo ~root /root $ echo ~xx ~xx

Tildes are also used in directory stack manipulation (page 298). In addition, ~+ is a synonym for PWD (the name of the working directory), and ~– is a synonym for OLDPWD (the name of the previous working directory).

Parameter and Variable Expansion
On a command line, a dollar sign ($) that is not followed by an open parenthesis introduces parameter or variable expansion. Parameters include both commandline, or positional, parameters (page 1012) and special parameters (page 1010). Variables include both user-created variables (page 302) and keyword variables (page 307). The bash man and info pages do not make this distinction. The shell does not expand parameters and variables that are enclosed within single quotation marks and those in which the leading dollar sign is escaped (i.e., preceded

www.it-ebooks.info

Processing the Command Line 349

with a backslash or enclosed within single quotation marks). The shell does expand parameters and variables enclosed within double quotation marks.

Arithmetic Expansion
The shell performs arithmetic expansion by evaluating an arithmetic expression and replacing it with the result. Under bash the syntax for arithmetic expansion is $((expression)) The shell evaluates expression and replaces $((expression)) with the result of the evaluation. This syntax is similar to the syntax used for command substitution [$(...)] and performs a parallel function. You can use $((expression)) as an argument to a command or in place of any numeric value on a command line. The rules for forming expression are the same as those found in the C programming language; all standard C arithmetic operators are available (see Table 27-8 on page 1035). Arithmetic in bash is done using integers. Unless you use variables of type integer (page 307) or actual integers, however, the shell must convert stringvalued variables to integers for the purpose of the arithmetic evaluation. You do not need to precede variable names within expression with a dollar sign ($). In the following example, after read (page 1019) assigns the user’s response to age, an arithmetic expression determines how many years are left until age 60:
$ cat age_check #!/bin/bash echo -n "How old are you? " read age echo "Wow, in $((60-age)) years, you'll be 60!" $ ./age_check How old are you? 55 Wow, in 5 years, you'll be 60!

You do not need to enclose the expression within quotation marks because bash does not perform filename expansion on it. This feature makes it easier for you to use an asterisk (*) for multiplication, as the following example shows:
$ echo There are $((60*60*24*365)) seconds in a non-leap year. There are 31536000 seconds in a non-leap year.

The next example uses wc, cut, arithmetic expansion, and command substitution (page 351) to estimate the number of pages required to print the contents of the file letter.txt. The output of the wc (word count) utility used with the –l option is the number of lines in the file, in columns (character positions) 1 through 4, followed by a SPACE and the name of the file (the first command following). The cut utility with the –c1–4 option extracts the first four columns.
$ wc -l letter.txt 351 letter.txt $ wc -l letter.txt | cut -c1-4 351

www.it-ebooks.info

350 Chapter 9 The Bourne Again Shell

The dollar sign and single parenthesis instruct the shell to perform command substitution; the dollar sign and double parentheses indicate arithmetic expansion:
$ echo $(( $(wc -l letter.txt | cut -c1-4)/66 + 1)) 6

The preceding example sends standard output from wc to standard input of cut via a pipe. Because of command substitution, the output of both commands replaces the commands between the $( and the matching ) on the command line. Arithmetic expansion then divides this number by 66, the number of lines on a page. A 1 is added because the integer division results in any remainder being discarded.

Fewer dollar signs ($) tip When you use variables within $(( and )), the dollar signs that precede individual variable references are optional:
$ x=23 y=37 $ echo $((2*$x + 3*$y)) 157 $ echo $((2*x + 3*y)) 157

Another way to get the same result without using cut is to redirect the input to wc instead of having wc get its input from a file you name on the command line. When you redirect its input, wc does not display the name of the file:
$ wc -l < letter.txt 351

It is common practice to assign the result of arithmetic expansion to a variable:
$ numpages=$(( $(wc -l < letter.txt)/66 + 1)) let builtin

The let builtin evaluates arithmetic expressions just as the $(( )) syntax does. The following command is equivalent to the preceding one:
$ let "numpages=$(wc -l < letter.txt)/66 + 1"

The double quotation marks keep the SPACEs (both those you can see and those that result from the command substitution) from separating the expression into separate arguments to let. The value of the last expression determines the exit status of let. If the value of the last expression is 0, the exit status of let is 1; otherwise, its exit status is 0. You can supply let with multiple arguments on a single command line:
$ let a=5+3 b=7+2 $ echo $a $b 8 9

When you refer to variables when doing arithmetic expansion with let or $(( )), the shell does not require a variable name to begin with a dollar sign ($). Nevertheless, it is a good practice to do so for consistency, because in most places you must precede a variable name with a dollar sign.

www.it-ebooks.info

Processing the Command Line 351

Command Substitution
Command substitution replaces a command with the output of that command. The preferred syntax for command substitution under bash follows: $(command) Under bash you can also use the following, older syntax:

‘command‘
The shell executes command within a subshell and replaces command, along with the surrounding punctuation, with standard output of command. In the following example, the shell executes pwd and substitutes the output of the command for the command and surrounding punctuation. Then the shell passes the output of the command, which is now an argument, to echo, which displays it.
$ echo $(pwd) /home/max

The next script assigns the output of the pwd builtin to the variable where and displays a message containing the value of this variable:
$ cat where where=$(pwd) echo "You are using the $where directory." $ ./where You are using the /home/zach directory.

Although it illustrates how to assign the output of a command to a variable, this example is not realistic. You can more directly display the output of pwd without using a variable:
$ cat where2 echo "You are using the $(pwd) directory." $ ./where2 You are using the /home/zach directory.

The following command uses find to locate files with the name README in the directory tree rooted at the working directory. This list of files is standard output of find and becomes the list of arguments to ls.
$ ls -l $(find . -name README -print)

The next command line shows the older ‘command‘ syntax:
$ ls -l

‘find

. -name README -print‘

One advantage of the newer syntax is that it avoids the rather arcane rules for token handling, quotation mark handling, and escaped back ticks within the old syntax. Another advantage of the new syntax is that it can be nested, unlike the old syntax. For example, you can produce a long listing of all README files whose size exceeds the size of ./README with the following command:
$ ls -l $(find . -name README -size +$(echo $(cat ./README | wc -c)c ) -print )

www.it-ebooks.info

352 Chapter 9 The Bourne Again Shell

Try giving this command after giving a set –x command (page 982) to see how bash expands it. If there is no README file, the command displays the output of ls –l. For additional scripts that use command substitution, see pages 978, 997, and 1027.

$(( versus $( tip The symbols $(( constitute a single token. They introduce an arithmetic expression, not a command substitution. Thus, if you want to use a parenthesized subshell (page 295) within $(), you must put a SPACE between the $( and the following (.

Word Splitting
The results of parameter and variable expansion, command substitution, and arithmetic expansion are candidates for word splitting. Using each character of IFS (page 312) as a possible delimiter, bash splits these candidates into words or tokens. If IFS is unset, bash uses its default value (SPACE-TAB-NEWLINE). If IFS is null, bash does not split words.

Pathname Expansion
Pathname expansion (page 244), also called filename generation or globbing, is the process of interpreting ambiguous file references and substituting the appropriate list of filenames. Unless noglob (page 343) is set, the shell performs this function when it encounters an ambiguous file reference—a token containing any of the unquoted characters *, ?, [, or ]. If bash cannot locate any files that match the specified pattern, the token with the ambiguous file reference remains unchanged. The shell does not delete the token or replace it with a null string but rather passes it to the program as is (except see nullglob on page 343). In the first echo command in the following example, the shell expands the ambiguous file reference tmp* and passes three tokens (tmp1, tmp2, and tmp3) to echo. The echo builtin displays the three filenames it was passed by the shell. After rm removes the three tmp* files, the shell finds no filenames that match tmp* when it tries to expand it. It then passes the unexpanded string to the echo builtin, which displays the string it was passed.
$ ls tmp1 tmp2 tmp3 $ echo tmp* tmp1 tmp2 tmp3 $ rm tmp* $ echo tmp* tmp*

A period that either starts a pathname or follows a slash (/) in a pathname must be matched explicitly unless you have set dotglob (page 342). The option nocaseglob (page 343) causes ambiguous file references to match filenames without regard to case.

www.it-ebooks.info

Processing the Command Line 353
Quotation marks

Putting double quotation marks around an argument causes the shell to suppress pathname and all other kinds of expansion except parameter and variable expansion. Putting single quotation marks around an argument suppresses all types of expansion. The second echo command in the following example shows the variable $max between double quotation marks, which allow variable expansion. As a result the shell expands the variable to its value: sonar. This expansion does not occur in the third echo command, which uses single quotation marks. Because neither single nor double quotation marks allow pathname expansion, the last two commands display the unexpanded argument tmp* .
$ echo tmp* $max tmp1 tmp2 tmp3 sonar $ echo "tmp* $max" tmp* sonar $ echo 'tmp* $max' tmp* $max

The shell distinguishes between the value of a variable and a reference to the variable and does not expand ambiguous file references if they occur in the value of a variable. As a consequence you can assign to a variable a value that includes special characters, such as an asterisk (*).
Levels of expansion

In the next example, the working directory has three files whose names begin with letter. When you assign the value letter* to the variable var, the shell does not expand the ambiguous file reference because it occurs in the value of a variable (in the assignment statement for the variable). No quotation marks surround the string letter*; context alone prevents the expansion. After the assignment the set builtin (with the help of grep) shows the value of var to be letter*.
$ ls letter* letter1 letter2 letter3 $ var=letter* $ set | grep var var='letter*' $ echo '$var' $var $ echo "$var" letter* $ echo $var letter1 letter2 letter3

The three echo commands demonstrate three levels of expansion. When $var is quoted with single quotation marks, the shell performs no expansion and passes the character string $var to echo, which displays it. With double quotation marks, the shell performs variable expansion only and substitutes the value of the var variable for its name, preceded by a dollar sign. No pathname expansion is performed on this command because double quotation marks suppress it. In the final command, the shell, without the limitations of quotation marks, performs variable substitution and then pathname expansion before passing the arguments to echo.

www.it-ebooks.info

354 Chapter 9 The Bourne Again Shell

Process Substitution
A special feature of the Bourne Again Shell is the ability to replace filename arguments with processes. An argument with the syntax (command) is replaced by the name of a pipe that command reads as standard input. The following example uses sort (page 154) with the –m (merge, which works correctly only if the input files are already sorted) option to combine two word lists into a single list. Each word list is generated by a pipe that extracts words matching a pattern from a file and sorts the words in that list.
$ sort -m -f > $file echo -n "Enter name of person or group: " read name echo "$name" >> $file echo >> $file cat >> $file echo "----------------------------------------------------" >> $file echo >> $file

a. What do you have to do to the script to be able to execute it? b. Why does the script use the read builtin the first time it accepts input from the terminal and the cat utility the second time? 6. Assume the /home/zach/grants/biblios and /home/zach/biblios directories exist. Specify Zach’s working directory after he executes each sequence of commands. Explain what happens in each case. a. $ pwd
/home/zach/grants $ CDPATH=$(pwd) $ cd $ cd biblios

b. $ pwd
/home/zach/grants $ CDPATH=$(pwd) $ cd $HOME/biblios

7. Name two ways you can identify the PID number of the login shell. 8. Enter the following command:
$ sleep 30 | cat /etc/services

Is there any output from sleep? Where does cat get its input from? What has to happen before the shell displays a prompt?

Advanced Exercises
9. Write a sequence of commands or a script that demonstrates variable expansion occurs before pathname expansion. 10. Write a shell script that outputs the name of the shell executing it.

www.it-ebooks.info

358 Chapter 9 The Bourne Again Shell

11. Explain the behavior of the following shell script:
$ cat quote_demo twoliner="This is line 1. This is line 2." echo "$twoliner" echo $twoliner

a. How many arguments does each echo command see in this script? Explain. b. Redefine the IFS shell variable so the output of the second echo is the same as the first. 12. Add the exit status of the previous command to your prompt so it behaves similarly to the following:
$ [0] ls xxx ls: xxx: No such file or directory $ [1]

13. The dirname utility treats its argument as a pathname and writes to standard output the path prefix—that is, everything up to but not including the last component:
$ dirname a/b/c/d a/b/c

If you give dirname a simple filename (no / characters) as an argument, dirname writes a . to standard output:
$ dirname simple .

Implement dirname as a bash function. Make sure it behaves sensibly when given such arguments as /. 14. Implement the basename utility, which writes the last component of its pathname argument to standard output, as a bash function. For example, given the pathname a/b/c/d, basename writes d to standard output:
$ basename a/b/c/d d

15. The Linux basename utility has an optional second argument. If you give the command basename path suffix, basename removes the suffix and the prefix from path:
$ basename src/shellfiles/prog.bash .bash prog $ basename src/shellfiles/prog.bash .c prog.bash

Add this feature to the function you wrote for exercise 14.

www.it-ebooks.info

10
Networking and the Internet

Chapter10 10

In This Chapter
Types of Networks and How They Work. . . . . . . . . . . . . . . . . . 362 Network Protocols. . . . . . . . . . . . . 370 IPv6 . . . . . . . . . . . . . . . . . . . . . . . . 373 Network Utilities . . . . . . . . . . . . . . 382 ping: Tests a Network Connection 386 traceroute: Traces a Route Over the Internet. . . . . . . . . . . . . . . . . . . . 387 host and dig: Query Internet Nameservers . . . . . . . . . . . . . . . 388 whois: Looks Up Information About an Internet Site . . . . . . . . 388 Distributed Computing . . . . . . . . . 390 WWW: World Wide Web . . . . . . . . 400

Objectives
After reading this chapter you should be able to: Discuss a variety of network types Define several network protocols List the software and hardware components of networks Explain the features and advantages of IPv6 List several network utilities Explain how ping works Use dig to determine the nameserver for a Web site Describe distributed computing Explain the role of the World Wide Web
359

www.it-ebooks.info

360 Chapter 10 Networking and the Internet

Introduction to Networking
The communications facilities linking computers are continually improving, allowing faster and more economical connections. The earliest computers were unconnected stand-alone systems. To transfer information from one system to another, you had to store it in some form (usually magnetic tape, paper tape, or punch cards—called IBM or Hollerith cards), carry it to a compatible system, and read it back in. A notable advance occurred when computers began to exchange data over serial lines, although the transfer rate was slow (hundreds of bits per second). People quickly invented new ways to take advantage of this computing power, such as email, news retrieval, and bulletin board services. With the speed and ubiquity of today's networks, a piece of email can cross the country or even travel halfway around the world in a fraction of a second. Today it would be difficult to find a computer facility that does not include a LAN to link its systems. Linux systems are typically attached to an Ethernet (page 1163) network. Wireless networks are also prevalent. Large computer facilities usually maintain several networks, often of different types, and almost certainly have connections to larger networks (companywide or campuswide and beyond).
Internet

The Internet is a loosely administered network of networks (an internetwork) that links computers on diverse LANs around the globe. An internet (small i) is a generic network of networks that might share some parts in common with the public Internet. It is the Internet that makes it possible to send an email message to a colleague thousands of miles away and receive a reply within minutes. A related term, intranet, refers to the networking infrastructure within a company or other institution. Intranets are usually private; access to them from external networks might be limited and carefully controlled, typically using firewalls (page 368). Over the past decade many network services have emerged and become standardized. On Linux and UNIX systems, special processes called daemons (page 1160) support such services by exchanging specialized messages with other systems over the network. Several software systems have been created to allow computers to share filesystems with one another, making it appear as though remote files are stored on local disks. Sharing remote filesystems allows users to share information without knowing where the files physically reside, without making unnecessary copies, and without learning a new set of utilities to manipulate them. Because the files appear to be stored locally, you can use standard utilities (e.g., cat, vim, lpr, mv, or their graphical counterparts) to work with them. Many tools take advantage of networks for more than sharing files. These tools have varied tasks, from executing programs remotely to communicating between computers, programs, and people. The ssh (secure shell, page 673) utility is the standard tool for remote command execution and remote computer access. Years ago it replaced insecure tools such as rsh, telnet, and rlogin. The ssh utility encompasses the features of these tools and includes built-in security features for safe and private access across public networks. Some devices do not implement ssh, but not many.

Network services

www.it-ebooks.info

Introduction to Networking

361

Previously, users communicated using command-line tools such as talk, write, and IRC (Internet Relay Chat). These tools are still around, particularly IRC, but their use has waned in preference to Facebook, Twitter, Google Talk, Skype, and older IM (Instant Messaging) tools like MSN, AIM, ICQ, and Yahoo Messenger. Many people use the Empathy IM client for Linux. Jabber is popular in private company networks.
Intranet

An intranet is a network that connects computing resources at a school, company, or other organization but, unlike the Internet, typically restricts access to internal users. An intranet is usually composed of one or more local area networks (LANs) but could be fairly large for a regional, national, or worldwide company. An intranet can provide database, email, and Web page access to a limited group of people, regardless of their geographic location. The ability of an intranet to connect dissimilar machines is one of its strengths. Think of all the machines you can find on the Internet: Macintosh systems, PCs running different versions of Windows, machines running UNIX and Linux, and so on. Each of these machines can communicate via the IP protocol suite (page 370). The intranet defines the communication boundaries and security trust zones that enable this communication. Another key difference between the Internet and an intranet is that the Internet transmits only one protocol suite natively: IP. In contrast, an intranet can be set up to use a number of protocols, such as IP, AppleTalk, or other protocols developed by vendors over the years. Although these protocols cannot be transmitted directly over the Internet, you can set up gateway boxes at remote sites that tunnel or encapsulate these protocols into IP packets and then use the Internet to pass them. In practice, most of the older protocols have been rewritten to use IP as a transport. Even storage protocols and networks like SCSI and Fibre Channel have IP implementations now (iSCSI, FCoE). You can use an extranet (also called a partner net) or a VPN (virtual private network) to improve security. These terms describe ways to connect remote sites securely to a local site, typically by using the public Internet as a carrier and employing encryption as a means of protecting data in transit. A typical use of an extranet is to link research institutions for collaboration or to link a parts supplier and a manufacturer to obtain direct inventory access. Following are some terms you might want to become familiar with before you read the rest of this chapter: ASP (page 1152) bridge (page 1154) extranet (page 1164) firewall (page 1165) gateway (page 1166) hub (page 1169) internet (page 1170) Internet (page 1170) intranet (page 1170) ISP (page 1171) packet (page 1180) router (page 1186) sneakernet (page 1189) switch (page 1192) VPN (page 1196)

www.it-ebooks.info

362 Chapter 10 Networking and the Internet

Types of Networks and How They Work
Computers communicate over IP networks using unique addresses assigned by system software. An IP packet includes the address of the destination computer and the sender’s return address. Networks can consist of many distinct topologies, or arrangements of computers and networking equipment. The most common topologies are broadcast, point-to-point, and switched. Ethernet is the most common topology used in LANs (local area networks, discussed shortly). Ethernet speeds range from 10 megabits per second to 100 gigabits per second (not formally accepted as of this writing). Other types of LANs include Myrinet, Infiniband, and Quadrics, but they are used mostly in high-performance computing. Speed is critical to the proper functioning of the Internet. Newer specifications (cat 6 and cat 7) are being adopted for 10000BaseT (10 gigabits per second; also called 10GE) and faster networking. Specialized cables are required for higher speeds. SFP+ and QSFP are in common use. Many of the networks that form the backbone of the Internet run at speeds of 38 gigabits per second (OC768) to accommodate the ever-increasing demand for network services. Table 10-1 lists some of the specifications in use today.

Table 10-1
Specification
DS0 ISDN T-1 T-3 OC3 OC12 OC48 OC192 OC768

Network specifications Speed
64 kilobits per second Two DS0 lines plus signaling (16 kilobits per second) or 128 kilobits per second 1.544 megabits per second (24 DS0 lines) 43.232 megabits per second (28 T-1s) 155 megabits per second (100 T-1s) 622 megabits per second (4 OC3s) 2.5 gigabits per second (4 OC12s) 9.6 gigabits per second (4 OC48s) 38.4 gigabits per second (4 OC192s)

Broadcast Networks
On a broadcast network, any of the many systems attached to the network cable can send a message at any time; each system examines the destination address in each message and responds only to messages addressed to it. A problem occurs on a broadcast network when multiple systems send data at the same time, resulting in a collision of the messages on the cable. When messages collide, they can become garbled. The sending

www.it-ebooks.info

Types of Networks and How They Work

363

system notices the garbled message and resends it after waiting a short but random amount of time. Waiting a random amount of time helps prevent those same systems from resending the data at the same moment and experiencing yet another collision. The extra traffic that results from collisions can strain the network; if the collision rate gets too high, retransmissions might result in more collisions. Ultimately the network might become unusable. A special broadcast (page 1154) packet in a network requires all machines to inspect the packet. Certain protocols take advantage of this feature: The ARP protocol, which is used for IP addresses to MAC (Ethernet) addresses resolution, the PXE network boot protocol, DHCP, and some service discovery protocols all use broadcast packets.

Point-to-Point Networks
A point-to-point link does not seem like much of a network because only two endpoints are involved. However, most connections to WANs (wide area networks) go through point-to-point links, using wire cable, radio, or satellite links. The advantage of a pointto-point link is its simplicity: Because only two systems are involved, the traffic on the link is limited and well understood. A disadvantage is that each system can typically be equipped for only a small number of such links; it is impractical and costly to establish point-to-point links that connect each computer to all the rest. For example two hosts require one link, three hosts require three links, four hosts require six links, and five hosts require 10 links to connect a point to point network. The number of required links increases more quickly as you add more hosts. Point-to-point links often use serial links and specialized interfaces. The most common types of point-to-point links are the ones used to connect to the Internet. When you use DSL1 (digital subscriber line), you are using a point-to-point link to connect to the Internet. Serial lines, such as T-1, T-3, ATM links, FIOS, and ISDN, are all point-to-point. Although it might seem like a point-to-point link, a cable modem is based on broadcast technology and in that way is similar to Ethernet.

Switched Networks
With the introduction of switched networks, pure broadcast networks became a thing of the past. A switched network runs in full duplex mode, allowing machines and network equipment to send and receive data at the same time. Support for full duplex is built-in and required in gigabit networks and above, on 100-megabit networks it is optional, and on 10 megabit networks it is rare. A switch is a device that establishes a virtual path between source and destination hosts in such a way that each path appears to be a point-to-point link, much like a old-fashioned telephone switchboard. Instead of the operator using a plug to connect

1. The term DSL incorporates the xDSL suite of technologies, which includes ADSL, IDSL, HDSL, SDSL, and XDSL.

www.it-ebooks.info

364 Chapter 10 Networking and the Internet

your local line to an outgoing line, the switch inspects each packet to determine its destination and directs the packet appropriately. The switch creates and tears down virtual paths as hosts seek to communicate with each other. Each host thinks it has a direct point-to-point path to the host it is talking to. Contrast this approach with a broadcast network, where each host also sees traffic bound for other hosts. The advantage of a switched network over a pure point-topoint network is that each host requires only one connection: the connection to the switch. Using pure point-to-point connections, each host must have a connection to every other host. Scalability is provided by further linking switches. To achieve this reduction in traffic, Ethernet switches learn which hosts are on which ports. When traffic comes in destined for a host, the switch looks up which port that host is connected to in its CAM2 and sends the packet out that port. When a switch has not yet heard from a host matching the destination, or if the packet is a special broadcast packet, the packet is flooded out on all ports just like a packet in a broadcast network. A switched Ethernet network is a technological enhancement over a broadcast network.

LAN: Local Area Network
Local area networks (LANs) are confined to a relatively small area—a single computer facility, building, or campus. Today most LANs run over copper. Fiberoptic (glass or plastic) cable, wireless (Wi-Fi), and sometimes infrared (similar to most television remote control devices) are also common infrastructure. If its destination address is not on the local network, a packet must be passed on to another network by a router (page 367). A router might be a general-purpose computer or a special-purpose device attached to multiple networks to act as a gateway among them.
Switching terminology

You might see references to a layer-2 switch, layer-3 switch, or layer-4 switch. These terms refer to the IP networking model (page 370). A layer-2 switch is what the preceding discussion of switched network describes. A layer-3 switch is equivalent to a router; it combines features of layer-2 switching and routing. A single layer-3 switch can connect multiple independent LANs. Layer-4 switches inspect higher level packets to make decisions. Most Web load balancers are layer-4 switches. They can route traffic to many devices to spread the load on the network.

Ethernet
A Linux system connected to a LAN usually connects to a network using Ethernet. A typical Ethernet connection can support data transfer rates from 10 megabits per second to 100 gigabits per second. Transfer rates of 10 megabits per second, 100 megabits per second, and 1 gigabit per second use the same, older technology. However, 10, 40, and 100 gigabit per second transfer rates require newer technology and

2. A memory table used to dereference (page 1161) Ethernet addresses.

www.it-ebooks.info

Types of Networks and How They Work

365

specialized cables. Ten gigabit per second Ethernet can be compatible with 1 gigabit per second Ethernet by using physical media adapters. Because of the need for extremely tight tolerances, 40 and 100 gigabit per second Ethernet are not backward compatible. The technology required for these higher-speed transfer rates requires very precisely defined physical interfaces. The hardware is quite expensive and common only in the network infrastructure realm and HPC (high performance computing) environments, connecting multiple switches using high speed links.
Cables

As mentioned earlier, a modern Ethernet network transfers data using copper or fiberoptic cable or wireless transmitters and receivers. Originally, each computer was attached to a thick coaxial cable (called thicknet) at tap points spaced at six-foot intervals along the cable. The thick cable was awkward to deal with, so other solutions, including a thinner coaxial cable called thinnet, or 10Base2,3 were developed. Today most Ethernet connections are either wireless or made over UTP (unshielded twisted pair). There are a number of UTP standards, each referred to by a category and a number. Some examples are Category 5 [cat 5], Category 5e [cat 5e], and Category 6 [cat 6]. These categories specify how far the cable can carry a signal. The higher the number, the tighter the tolerances and the more expensive the cable, but the farther it can reliably carry a signal. These standards specify the physical connectors at the ends of the cables, how tightly the wires in the cable are twisted, and various other parameters. The terms 10BaseT, 100BaseT, and 100BaseT refer to Ethernet over cat-3/4/5/5e/6/7 cables. STP (shielded twisted pair) is not very common.

Segment

A network segment is a part of a network in which all systems communicate using the same physical layer (layer 1) of the IP and OSI models (page 371). It is of arbitrary size and can be a part of a WAN, MAN, or another network. In half-duplex mode, packets travel in one direction at a time over the cable. In full-duplex mode, packets travel in both directions. A hub (sometimes called a concentrator) is a device that connects systems so they are all part of one network segment and share the network bandwidth. Hubs work at the physical layer of the IP and OSI models (layer 1, page 371). All packets are sent to all hosts (flooded). A switch connects network segments. A switch inspects each data packet; it learns which devices are connected to which of its ports. The switch sends each packet only to the device it is intended for. Because a switch sends packets only to their destination devices, it can conserve network bandwidth and perform better than a hub. Some switches have buffers that hold and queue packets. Switches work at layers 2 and higher of the IP and OSI models (page 370). A layer-2 switch works at the datalink layer, and a layer-3 switch works at the IP layer and routes packets. Layer-4 switches work at the transport and application layers and are the basis for load balancers and application proxies.

Duplex

Hub

Switch

3. Versions of Ethernet are classified as XBaseY, where X is the data rate in megabits per second, Base means baseband (as opposed to radio frequency), and Y is the category of cabling.

www.it-ebooks.info

366 Chapter 10 Networking and the Internet

All modern Ethernet switches have enough bandwidth to communicate simultaneously, in full-duplex mode, with all connected devices. A nonswitched (hub-based) broadcast network can run in only half-duplex mode. Full-duplex Ethernet further improves efficiency by eliminating collisions. Each host on a full-duplex switched network can transmit and receive simultaneously at the speed of the network (e.g., 100 megabits per second) for an effective bandwidth between hosts of twice the speed of the network (e.g., 200 megabits per second), depending on the capacity of the switch.
Bridge

A network bridge connects multiple network segments at the data link layer (IP layer 2) of the OSI model. A bridge is similar to a repeater or network hub, devices that connect network segments at the physical layer, however a bridge works by forwarding traffic from one network segment to another only if the destination device specified in the packet is known to be on the remote segment. A bridge does not forward traffic between LAN hosts on the same side of the bridge. In Ethernet networks, the term bridge formally means a device that behaves according to the IEEE 802.1D standard. Marketing literature frequently refers to this type of device as a network switch.

Router

A router connects networks at layer 3 of the IP and OSI models (page 371). For example, a router can connect a LAN to a WAN (such as the Internet) or it can connect two LANs. A router determines which path packets should take to travel to a different network and forwards the packets. Routers work at the network layer of the IP and OSI models (layer 3). “Internetworking Through Gateways and Routers” on the next page covers routers in more depth. A VLAN (virtual local area network or virtual LAN) is a logical entity defined in software. It defines a group of hosts that share the same broadcast domain (layer 2). That is, when a host sends out a broadcast packet, such as an ARP packet, it will arrive at all other hosts in the same VLAN. All modern, managed switches, those that have a command line or Web interface, have VLAN capability. A VLAN allows an administrator to group hosts into IP ranges (LANs). VLANs commonly group hosts by department, organization, or security needs. Hosts in the same VLAN communicate with each other using layer 2, and hosts in different VLANs communicate using layer 3. The primary difference between a LAN and a VLAN is that in a LAN (a nonmanaged switch), all hosts are in the same broadcast domain, whereas in a VLAN, there can be multiple broadcast domains on a single switch. Broadcast domains and IP protocol layer 2 segments are effectively the same thing. However, layer 2 can be confused with hosts (network addresses) being in the same IP network. Consider the classic concentrator, a device that sends out all packets on all ports all the time. These ports might include separate networks. For example, some hosts might be in network 10.0.1.0, and others might be in network 192.168.1.0. Ethernet broadcast packets do not know the difference between the networks; these packets go to all hosts regardless of IP address. This setup is sometimes referred to as a collision domain. VLANs enable you to place the hosts in the IP network 10.0.1.0 into one VLAN and the hosts in 192.168.1.0 into another, so the networks do not

VLAN

www.it-ebooks.info

Types of Networks and How They Work

367

share each others’ broadcasts. VLANs also enable you to connect hosts that share the same function across a corporate network.

Wireless
Wireless networks are becoming increasingly common. They are found in offices, homes, and public places, such as universities, coffee shops, and airports. Wireless access points provide functionality similar to an Ethernet hub. They allow multiple users to interact via a common radio frequency spectrum. A wireless, point-to-point connection allows you to wander about your home or office with a laptop, using an antenna to link to a LAN or to the Internet via an in-house base station. Linux includes drivers for many wireless devices. A wireless access point, or base station, connects a wireless network to a wired network so that no special protocol is required for a wireless connection. Refer to the Linux Wireless LAN HOWTO at www.hpl.hp.com/personal/Jean_Tourrilhes/Linux .

WAN: Wide Area Network
A WAN (wide area network) covers a large geographic area. In contrast, the technologies (such as Ethernet) used for LANs were designed to work over limited distances and for a certain number of host connections. A WAN might span long distances over dedicated data lines (leased from a telephone company) or radio or satellite links. Such networks are often used to connect LANs and typically support much lower bandwidth than LANs because of the expense the connection. Major Internet service providers rely on WANs to connect to their customers within a country and around the globe.
MAN

Some networks do not fit into either the LAN or the WAN designation. A metropolitan area network (MAN) is a network that is contained in a smaller geographic area, such as a city. Like WANs, MANs are typically used to interconnect LANs.

Internetworking Through Gateways and Routers
Gateway

A LAN connects to a WAN through a gateway, a generic term for a computer or a special device with multiple network connections that passes data from one network to another. A gateway connects a LAN to other LANs, VLANs, or to a WAN. Data that crosses the country from one Ethernet to another over a WAN, for example, is repackaged from the Ethernet format to a different format that can be processed by the communications equipment that makes up the WAN backbone. When it reaches the end of its journey over the WAN, the data is converted by another gateway to a format appropriate for the receiving network. For the most part, these details are of concern only to the network administrators; the end user does not need to know anything about how the data transfer takes place. The modern, canonical reference to a gateway is to the default gateway, which is the router that connects a LAN or VLAN to other networks. Routers play an important role in internetworking. Just as you might study a map to plan your route when you need to drive to an unfamiliar place, so a computer needs to know how to deliver a

Router

www.it-ebooks.info

368 Chapter 10 Networking and the Internet

message to a system attached to a distant network by passing through intermediary systems and networks along the way. Although you might envision using a giant network road map to choose the route that your data should follow, a static map of computer routes is usually a poor choice for a large network. Computers and networks along the route you choose might be overloaded or down and not provide a detour for your message. Routers instead communicate dynamically, keeping each other informed about which routes are open for use. To extend the analogy, this situation is like heading out on a car trip without consulting a map to find a route to your destination; instead you head for a nearby gas station and ask directions. Throughout the journey you continue to stop at one gas station after another, getting directions at each to find the next one. Although it would take a while to make the stops, the owner of each gas station would advise you of bad traffic, closed roads, alternative routes, and shortcuts. The stops made by the data are much quicker than those you would make in your car, but each message leaves each router on a path chosen based on the most current information. Think of this system as a GPS (global positioning system) setup that automatically gets updates at each intersection and tells you where to go next, based on traffic and highway conditions. Figure 10-1 shows an example of how LANs might be set up at three sites interconnected by a WAN (the Internet). In this type of network diagram, Ethernet LANs are drawn as straight lines, with devices attached at right angles; WANs are represented as clouds, indicating the details have been left out; and wireless connections are drawn as zigzag lines with breaks, indicating the connection might be intermittent. In Figure 10-1, a gateway or a router relays messages between each LAN and the Internet. The figure shows the three routers in the Internet that are closest to each site. Site A has a server, a workstation, a network computer, and a PC sharing a single Ethernet LAN. Site B has an Ethernet LAN that serves a printer and four Linux workstations. A firewall permits only certain traffic to pass between the Internet router and the site’s local router. Site C has three LANs linked by a single router, perhaps to reduce the traffic load that would result if the LANs were combined or to keep workgroups or locations on separate networks. Site C also includes a wireless access point that enables wireless communication with nearby computers.

Firewall
A firewall in a car separates the engine compartment from the passenger compartment, protecting the driver and passengers from engine fires, noise, and fumes. In much the same way, computer firewalls separate computers from malicious and unwanted users. A firewall prevents certain types of traffic from entering or leaving a network. For example, a firewall might prevent traffic from your IP address from leaving the network and prevent anyone except users from selected domains from using FTP

www.it-ebooks.info

Ethernet

Ethernet Printer

PC

W

W W

Site B

GATEWAY/ Router

W W

W

Internet
Firewall Router 3 Router 1

Switch

Router
Printer

W

W Printer

Site C
Router 2
Wireless access point

Types of Networks and How They Work

Gateway
Ethernet

Site A
Legend NC W

W W NC PC Ethernet Network computer Linux workstation Personal computer

PC

Server

NC

W

Figure 10-1

A slice of the Internet

369

www.it-ebooks.info

370 Chapter 10 Networking and the Internet

to retrieve data from the network. The implementations of firewalls vary widely—from Linux machines with two interfaces (page 1170) running custom software to a router (preceding section) with simple access lists to esoteric, vendorsupplied firewall appliances. Most larger installations have at least one kind of firewall in place. A firewall is often accompanied by a proxy server/gateway (page 398) that provides an intermediate point between you and the host you are communicating with. In addition to the firewalls found in multipurpose computers, firewalls are becoming increasingly common in consumer appliances. For example, they are built into cable modems, wireless gateways, routers, and stand-alone devices. Typically a single Linux machine will include a minimal firewall. A small group of Linux systems might have an inexpensive Linux machine with two network interfaces and packet-filtering software functioning as a dedicated firewall. One of the interfaces connects to the Internet, modems, and other outside data sources. The other connects, normally through a hub or switch, to the local network. Refer to Chapter 25 for information on system-config-firewall, iptables, and setting up a firewall and to Appendix C for a discussion of security.

Network Protocols
TCP

To exchange information over a network, computers must communicate using a common language, or protocol (page 1183). The protocol determines the format of message packets. The predominant network protocols used by Linux systems are TCP and IP,4 collectively referred to as TCP/IP (Transmission Control Protocol and Internet Protocol). Network services that require highly reliable connections, such as ssh and scp, tend to use TCP/IP. Network services that do not require guaranteed delivery but require timely delivery, such as video, audio, and time services, operate using the simpler UDP (User Datagram Protocol; UDP/IP). VoIP (voice over IP) and NTP (Network Time Protocol) fall into this category. UDP packets are sent and then forgotten. Voice and video protocols are delay sensitive, not integrity sensitive. The human ear and eye accept and interpolate loss in an audio or video stream but cannot deal with variable delay. The guaranteed delivery that TCP provides can introduce a delay on a busy network when packets are retransmitted. This delay is not acceptable for video and audio transmissions, whereas less than 100 percent integrity is acceptable. In the case of NTP, missing packets are acceptable, but packets that are delayed because of TCP retransmission can result in significantly skewed time settings.

UDP

IP: Internet Protocol
Layering was introduced to facilitate protocol design: Layers distinguish functional differences between adjacent protocols. A grouping of layers can be standardized

4. All references to IP imply IPv4 (page 1171).

www.it-ebooks.info

Types of Networks and How They Work

371

into a protocol model. IP has a model that distinguishes protocol layers and that differs from the ISO seven-layer protocol model (also called the OSI model) often illustrated in networking textbooks. Specifically IP uses the following simplified five-layer model: 1. The first layer of the IP protocol, called the physical layer, describes the physical medium (e.g., copper, fiber, wireless) and the data encoding used to transmit signals on that medium (e.g., pulses of light, electrical waves, or radio waves). 2. The second layer, called the data link layer, covers media access by network devices and describes how to put data into packets, transmit the data, and check it for errors. Ethernet is found at this layer, as is 802.11 (page 1150) wireless. 3. The third layer, called the network layer, frequently uses IP and addresses and routes packets. It allows data to traverse the networks. 4. The fourth layer, called the transport layer, is where TCP and UDP exist. This layer provides a means for applications to communicate with each other. Functions commonly performed by the transport layer include guaranteed delivery, delivery of packets in the order of their transmission, flow control, error detection, and error correction. The transport layer is responsible for dividing data streams into packets. In addition, this layer performs port addressing, which allows it to distinguish among different services using the same transport protocol. Port addressing keeps the data from multiple applications using the same protocol (for example, TCP) separate. 5. Anything above the transport layer is the domain of the application and is part of the fifth layer. Unlike the ISO model, the Internet model does not distinguish among application, presentation, and session layers. All the upper-layer characteristics, such as character encoding, encryption, and GUIs, are part of the application. Applications choose the transport characteristics they require as well as the corresponding transport layer protocol with which to send and receive data.

TCP: Transmission Control Protocol
TCP is most frequently run on top of IP in a combination referred to as TCP/IP. This protocol provides error recovery and guaranteed delivery in packet transmission order; it also works with multiple ports so that it can handle more than one application. TCP is a connection-oriented protocol (page 1158), also known as a stream-based protocol. Once established, a TCP connection looks like a stream of data, not individual IP packets. The connection is assumed to remain up and be uniquely addressable. Every piece of information you write to the connection always goes to the same destination and arrives in the order it was sent. Because TCP is connection oriented and establishes a virtual circuit between two systems,

www.it-ebooks.info

372 Chapter 10 Networking and the Internet

this protocol is not suitable for one-to-many transmissions (see the discussion of UDP, following). TCP has builtin mechanisms for dealing with congestion (or flow) control over busy networks and throttles back (slows the speed of data flow) when it has to retransmit dropped packets. TCP can also deal with acknowledgments, wide area links, high-delay links, and other situations.

UDP: User Datagram Protocol
UDP runs at layer 4 of the IP stack, just as TCP does, but is much simpler. Like TCP, UDP works with multiple ports and multiple applications. It has checksums for error detection but does not automatically retransmit datagrams (page 1160) that fail the checksum test. UDP is a datagram-oriented protocol: Each datagram must carry its own address and port information. Each router along the way examines each datagram to determine the destination, one hop at a time. You can broadcast or multicast UDP datagrams to many destinations at the same time by using special addresses.

PPP: Point-to-Point Protocol
PPP provides serial line point-to-point connections that support IP. It compresses data to make the most of the limited bandwidth available on these connections. PPP acts as a point-to-point layer 2/3 transport that many other types of protocols can ride on. Today it is used mostly in devices such as cable modems. Previously, it was used as a transport for TCP and UDP on dial-up modems that connected a computer to the Internet.

IPv4
Under IPv4, the network address of a machine is an IP address that is represented as one number broken into four octets5 separated by periods (for example, 192.168.184.5). Domain names and IP addresses are assigned through a highly distributed system coordinated by ICANN (Internet Corporation for Assigned Names and Numbers—www.icann.org) via many registrars (see www.internic.net). ICANN is funded by the various domain name registries and registrars and by IP address registries, which supply globally unique identifiers for hosts and services on the Internet. Although you might not deal with any of these agencies directly, your Internet service provider most assuredly does. How a company uses IP addresses is determined by the system or network administrator. For example, the two leftmost sets of numbers in an IP address might represent a large network (campuswide or companywide); the third set, a subnetwork (perhaps a department or a single floor in a building); and the rightmost number, an individual computer. The operating system uses the address in a different, lower-level form, converting it to its binary equivalent, a series of 1s and 0s. Refer to “Private address space” on page 650 for information about addresses you can use on a LAN without registering them.

5. Using binary notation, an eight-bit byte can represent the range of 0–255, thus the term octet.

www.it-ebooks.info

Types of Networks and How They Work

373

IPv6
Getting started with IPv6 tip Even if the ISP you do business with does not provide IPv6 service, it is still easy and free to participate on the IPv6 Internet by using a tunnel broker. For more information on tunnel brokers see en.wikipedia.org/wiki/List_of_IPv6_tunnel_brokers. Setting up a small home or lab network through a tunnel broker is an excellent way to gain experience with IPv6 and experiment with its configurations and capabilities. Most tunnel brokers offer instructions on how to get various platforms working with their service.

IPv6 (Internet Protocol version 6)6 is the next generation of the Internet Protocol. Since the introduction of IPv4 (Internet Protocol version 4) in 1980, the Internet has undergone explosive growth that has caused its needs to exceed the capabilities of IPv4. A proliferation of Internet connected devices, including cellular telephones, tablet computers, electronic book readers, and advanced televisions and set-top boxes, all of which need an IP address in order to communicate on the Internet, has fueled that growth. IPv4 uses a 32-bit address space, which is no longer sufficient to give a unique IP address to every device on the Internet. The lack of IPv4 address space has been mitigated by the use of NAT (page 1177) and by other techniques, but each of these techniques comes with limitations and overhead. IPv6 uses a 128-bit address space, which ensures that all devices can have unique IP addresses without resorting to such mitigations, and provides enormous room to grow as the Internet continues to expand. While the enormous address space in IPv6 is its most compelling feature in light of the shortage of IPv4 addresses, IPv6 has many other desirable features: • IPv6 enables stateless address autoconfiguration. With IPv4, DHCP (page 489) is usually used to automate network configuration. With IPv6, autoconfiguration makes it possible for hosts to configure their IP addresses automatically and autonomously without the need for a central service like DHCP. • In IPv6, multicast (page 1177 and next) is mandatory and fundamental to its operation. • IPv6 provides a simplified packet header that reduces router complexity by improving routing efficiency and cuts router cost by eliminating the need for much hardware acceleration. • IPv6 reserves 20 bits in the header for advanced services, such as resource reservation protocols, better backbone routing, and improved traffic engineering.

6. IPv5 referred to an experimental real-time stream protocol named ST—thus the jump from IPv4 to IPv6.

www.it-ebooks.info

374 Chapter 10 Networking and the Internet

IPv6 can coexist with IPv4. Even though the protocols are very similar in many ways, the network considers them to be different protocols, so one will not interfere with the other. Also, most operating systems support IPv4 and IPv6 simultaneously by using a dual-stack configuration. This configuration is likely to be common for a long time because it allows IPv6-enabled hosts to reach legacy IPv4-only hosts easily.

Multicast Packets
IPv6 does not use broadcast (page 1154) packets and therefore does not have a broadcast address (page 1155) that corresponds to the IPv4 broadcast address. IPv6 mandates the implementation and use of multicast (page 1177). Multicast improves the efficiency of the network by reducing the amount of traffic that each host must process: The system network interface does not ordinarily pass to the host multicast traffic it is not registered to receive. For example, when a host wants to discover a DHCPv6 server, it sends a packet to the “all DHCPv6 servers” well-known link-local multicast address (ff05::1:3). This packet is processed only by hosts running DHCPv6. Under IPv4 the host must send a broadcast packet to discover the DHCP server. This packet is processed by every host on the network. Many tools, such as ssh, work with IPv6 and IPv4 without difficulty. Some tools have special versions that work with IPv6 (e.g., ping6, traceroute6, and ip6tables).

Addresses
Because they are much longer, IPv6 addresses are quite different from IPv4 addresses. For brevity and clarity, IPv6 addresses are usually expressed as eight sets of four hexadecimal digits, each set separated from the next by a colon. As an example, consider the IPv6 address 2001:4860:800a:0000:0000:0000:0000:0067. To shorten the address and allow it to remain unambiguous, you can replace any number of adjacent sets of four zeros and the colons that enclose them with two colons. After making this replacement, you can represent the example address as 2001:4860:800a::0067. With this replacement, you (or a computer) can easily expand the shortened address by adding enough sets of four zeros to fill out the eight sets in the address. You cannot make this replacement more than once in an address. If you did, when you tried to expand the address you would not know how many sets of zeros to replace each double colon with. As an additional way of shortening an address, you can eliminate any leading zeros in any set of four digits. Using this technique, you can write the example address as 2001:4860:800a::67, which is much more manageable than the original address. You can also use CIDR notation (page 380): The example host might be in an allocation such as 2001:4860:800a::/48. In IPv6, the network prefix is always 64 bits, and the host part is always the remaining 64 bits. In other words, all networks are the same size. If an organization were given the network allocation 2001:4860:800a::/48, it would have 16 bits of network

www.it-ebooks.info

Types of Networks and How They Work

375

address space to use, each network being of the form 2001:4860:800a:xxxx::/64, where xxxx is any one of the 65,536 possible numbers in 16 bits. Because of the immense size of the IPv6 address space, this allocation is considered small, although even very large organizations would fit well within it.

Autoconfiguration
A constant host address length of 64 bits allows for stateless address autoconfiguration. Using a multicast ICMPv6 packet, the router sends periodic advertisements to all hosts on the network, telling them of its ability to route for a given network prefix. At the same time, it gives hosts permission to assign themselves addresses using that prefix. A host that hears this advertisement can construct a host address in a deterministic way by incorporating the hardware address of the network adapter (i.e., the MAC address [page 376]). Without resorting to the use of a stateful service (e.g., DHCP) to maintain a list of assigned addresses, this construction guarantees that no other host on the network will have the same address. After constructing a host address, the host finalizes the configuration by adding a default route to the router from which the advertisement came. Alternatively, when a host interface initially comes up, it can solicit router information by sending a request to all routers instead of waiting for the periodic advertisement, speeding up the process. Once it has assigned itself an address, the host expects to hear periodic advertisements from the router. If it does not hear these advertisements, the host will eventually expire its knowledge of this autoconfigured route and address.

Link-local Addresses
A link-local IP address can be used to communicate only with other systems on the network; a router will not forward a packet that has a link-local address. This address is autoconfigured in a manner similar to that explained in the preceding section, except the network prefix is always fe80::/64, marking it as a link-local address. This setup contrasts with how IPv4 DHCP works: Because an IPv4 host does not have an address when it initially communicates with a DHCP server, a hack is needed to use a fake address for the host. Such hacks are not necessary in IPv6 because of autoconfigured link-local addresses. For example, when a host solicits router information, it uses a link-local address to do so.
Multihoming

The IPv6 specification requires multihoming (allowing a single network interface to have multiple addresses). Multihoming allows the link-local address to persist after a global-scope address has been assigned to a system, allowing continued access to the host via the link-local address. The IPv6 loopback address is ::1 and is considered interface-local. Certain link-local addresses are not autoconfigured. These addresses are well-known and are registered with IANA. For a list of these addresses visit IANA at www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xml.

Interface-local Well-known linklocal addresses

www.it-ebooks.info

376 Chapter 10 Networking and the Internet

Servers subscribe to these addresses as needed, allowing them to receive requests. For example, a DHCPv6 server will subscribe to ff05::1:3 so it receives configuration requests from hosts.
DHCPv6

DHCPv6 is a completely new implementation of DHCP (page 489) that covers IPv6. Although autoconfiguration works for most situations, network administrators who wish to have more control over their networks might choose to use DHCPv6. It enables an administrator to specify certain host addresses that can persist even if the host or network interface has to be replaced. (Autoconfigured addresses depend on the interface hardware address [NIC]). It also allows configuration of other operational parameters, such as local DNS resolver address(es), which are not well supported with autoconfiguration. This feature is important on networks that run IPv6 only. DHCPv6 also introduces the notion of a stateless mode, which is a way to enjoy the simplicity of autoconfiguration using router advertisements while also allowing the administrator to manage static configuration elements (e.g., DNS resolver addresses) using a very simple DHCP configuration that is easily made fault tolerant.

DNS

DNS works like in IPv4, except instead of having A records to associate names with addresses, there are AAAA records (sometimes called quad-A records; page 852). Just as a DNS name can have multiple A records, it can have multiple AAAA records and/or a combination of both. DNS does not place any restriction on which record type will be delivered based on which protocol was used by the requestor; DNS returns the record type(s) requested. This setup enables the DNS server to provide AAAA records for services that are available over IPv6, even when DNS itself has not been made available over IPv6. Under IPv6, if a router receives a packet larger than the network can support (the MTU, or maximum transmission unit), it sends a “fragmentation needed” ICMP packet back to the originator. Instead of the router bearing the burden of fragmenting the packet in-transit, the router depends on the originator to keep the packets down to a manageable size. Because the intervening routers do not have to concern themselves with fragmenting packets, they are able to work more efficiently and with less specialized hardware. Each NIC (page 1178) on a system connected to a network has a unique identifier7 called a MAC address (page 1174), sometimes referred to as an Ethernet address. A system attached to more than one network has multiple interfaces—one for each network. Such a system might have one MAC address for the system, or it might have one MAC address for each interface. Both setups work as long as a given MAC address is unique on a given LAN or VLAN.
7. In fact, each identifier might not be unique. Vendors have been known to reuse MAC addresses for physical devices shipped to different regions. In practice, you are unlikely to run into duplicate addresses. The only requirement is that a MAC address must be unique on a given LAN or VLAN segment.

Fragmentation

Host Address
MAC address

www.it-ebooks.info

Types of Networks and How They Work

377

Each system on a network also has a one or more unique IP addresses. At IP layer 3 (the network layer), systems communicate using IP addresses. However, at IP layer 2 (the data link layer), they communicate using MAC addresses.
ARP

For hosts to talk to each other over a network, a map between IP addresses and MAC addresses must exist. ARP (Address Resolution Protocol) is a method for finding a host’s MAC (Ethernet) address from its IP address. Each host builds an ARP cache that holds a map that translates IP addresses into MAC addresses. The arp utility works with this cache; the –a option to display the cache:
$ arp -a plum (172.16.192.151) at 00:0c:29:2d:eb:a9 [ether] on eth0 ...

The preceding output shows the IP and Ethernet addresses of all hosts the local host is aware of. The example maps the IP address of plum (172.16.192.151) to the MAC address of the NIC on plum (00:0c:29:2d:eb:a9). Each packet of information transmitted over a LAN has a destination MAC address. The NIC on each host on the network checks each packet to see if the destination address matches its MAC address. If it matches, the NIC passes the packet to the kernel for processing; if it does not match, and the packet does not have the special FF:FF:FF:FF:FF:FF destination address, the NIC silently drops the packet. All NICs examine all packets that have the special FF:FF:FF:FF:FF:FF destination address. When the local system has a packet to send to another system, but knows only the remote system’s IP address and not its MAC address, it sends an ARP broadcast packet to the special FF:FF:FF:FF:FF:FF destination address. That packet specifies the IP address the local system is trying to contact and requests the system with that IP address respond with its MAC address. When the host with the IP address the packet specifies examines the packet, it responds with its MAC address directly to the host that sent the packet. Once the systems have each other’s IP addresses mapped to their MAC addresses in their ARP caches, they can communicate directly using this information. For more detail, refer to a book such as Internetworking with TCP/IP, Volume 1, 5th edition, by Douglas E. Comer, Prentice Hall (July 2005). When the local system needs to send a packet to a host on another network, it sends the packet to the router (default gateway) on the local network. The local system knows the IP address of the router and uses ARP to determine its MAC address. The router and all hosts use the same process to determine the MAC address of hosts they need to send packets to. Then they can communicate directly with each of these hosts. Each host and router keeps an ARP cache. Entries in the cache expire in about five minutes, balancing the currency of the address resolution table and the frequency of ARP requests.

www.it-ebooks.info

378 Chapter 10 Networking and the Internet

Static Versus Dynamic IP Addresses
A static IP address is one that always remains the same. A server (e.g., mail, Web) usually has a static address so clients can find the server machine on the network. See pages 493 and 652 for information on configuring a static IP address. A dynamic IP address is one that is allocated (leased) to a client and that has a defined expiration time. Typically the client renews the lease before it expires at which time the server provides a new lease on the same IP address. A dynamic IP address can change each time a system connects to the network but frequently remains the same. End-user systems usually work well with dynamic addresses. During a given login session, these systems can function as a client (e.g., Web browser) because they maintain a constant IP address. When you log out and log in again, it does not matter that a system has a different IP address because, acting as a client, it establishes a new connection with a server. The advantage of dynamic addressing is that it allows inactive addresses to be reused, reducing the total number of IP addresses needed. Dynamic addressing is particularly popular on wireless access points.

optional

Subnets
IP addresses are divided into two parts, the network address, also called the subnet (subnetwork address), and the host address, or node address. The network address specifies a set of computers that can communicate without sending packets through a router. The host address specifies an individual computer. IP is an internetwork protocol and was originally intended to connect networks together. When sending a packet, a computer examines the network portion of the destination IP address. If the network address matches that of the sending computer’s network, the computer can send the packet to the destination computer directly. If it does not match, the computer must send the packet to the router. The portion of the IP address used to identify the network is given by the subnet mask (next).

Terminology: subnet mask, network mask, Subnet mask

Although the term subnet mask is in common use, the preferred term is network mask or simply mask. This book uses the term subnet mask to make a clear distinction between the network prefix and the subnet prefix. Otherwise it uses the term network mask. A subnet mask (or network mask) is a bit mask that identifies which parts of an IP address correspond to the network address and the subnet portion of the address. This mask has 1s in positions corresponding to the network and subnet numbers and 0s in the host number positions. When you perform a bitwise AND on an IP address and a subnet mask, the resulting address contains everything except the host address (hostid) portion. There are several ways to represent a subnet mask: A network could have a subnet mask of 255.255.255.0 (decimal), FFFFFF00 (hexadecimal [page 1167]), or /24 (the number of bits used for the subnet mask). A subnet mask of /24 has 8 bits for hosts (32-24). However, the last address in the range is reserved as a broadcast address, and the first address in the range is the network address. The second address is typically reserved for the router, although some setups use the broadcast address – 1 for this purpose. Thus there are 28 – 3 = 253 IP addresses.

www.it-ebooks.info

Types of Networks and How They Work

379

As another example, when you divide the address 192.25.4.0 into eight subnets, you get a subnet mask of 255.255.255.224, FFFFFFE0, or /27 (27 1s). The eight resultant networks are 192.25.4.0, 192.25.4.32, 192.25.4.64, 192.25.4.96, 192.25.4.128, 192.25.4.160, 192.25.4.192, and 192.25.4.224. You can use a Web-based subnet mask calculator to calculate subnet masks (refer to “Network Calculators” on page 1121). To use this calculator to determine the preceding subnet mask, start with an IP network address of 192.25.4.0. Table 10-2 shows some of the computations for the IP address 131.204.027.027. Each address is shown in decimal, hexadecimal, and binary form. Binary is the easiest to work with for bitwise (binary) computations. The first three lines show the IP address. The next three lines show the subnet mask in three bases. Next the IP address and the subnet mask are ANDed together bitwise to yield the subnet number (page 1191), which is shown in three bases. The last three lines show the broadcast address (page 1155), which is computed by taking the subnet number and turning the hostid bits into 1s. The subnet number identifies the local network. The subnet number and the subnet mask determine what range the IP address of the machine must be in. They are also used by routers to segment traffic; see network segment (page 1178). A broadcast on this network goes to all hosts in the range 131.204.27.1 through 131.204.27.254 but will be acted on only by hosts that have a use for it. For more information refer to “Specifying a Subnet” on page 479.

Table 10-2

Computations for IP address 131.204.027.027 ---------------Class B----------131 .204 CC 1100 1100 .255 FF 1111 1111 1100 1100 1111 1111 1100 1100 .204 CC 1100 1100 .204 CC 1100 1100

netid
.027 1B 0001 1011 .255 FF 1111 1111 0001 1011 1111 1111 0001 1011 .027 1B 0001 1011 .27 1B 0001 1011

hostid
.027 decimal 1B hexadecimal 0001 1011 binary .000 decimal 00 hexadecimal 0000 0000 binary 0001 1011 0000 0000 binary 0000 0000 .000 decimal 00 hexadecimal 0000 0000 binary .255 decimal FF hexadecimal 1111 1111 binary

IP address

83 1000 0011 255

Subnet mask

FF 1111 1111

IP address bitwise AND Subnet mask = Subnet number

1000 0011 1111 1111 1000 0011 131

Subnet number

83 1000 0011 131

Broadcast address (set host bits to 1)

83 1000 0011

www.it-ebooks.info

380 Chapter 10 Networking and the Internet

CIDR: Classless Inter-Domain Routing
CIDR (pronounced “cider”) allows groups of addresses to be assigned to an organization or ISP and then further subdivided and parceled out. In addition, it helps prevent routing tables on Internet backbone providers from becoming too large to manage by consolidating arbitrary network ranges along bit boundaries. IPv6 (page 373) is the solution to IPv4 address exhaustion. The trend is to reclaim older, large address blocks, if possible, and recycle them into groups of smaller address blocks. Larger blocks are allocated to ISPs, which in turn subdivide these blocks and allocate them to their customers. When you request an address block, your ISP usually gives you as many addresses as you need—and no more. The ISP aggregates one or more contiguous smaller blocks to satisfy your request. This aggregation is CIDR. Without CIDR, the Internet as we know it would not function. For example, you might be allocated the 192.168.5.0/22 IP address block, which can support 210 hosts (32 – 22 = 10). Your ISP would set its routers so packets going to an address in that block would be sent to your network. Internally, your own routers might further subdivide this block of 1,024 potential hosts into four networks. Four networks require an additional two bits of addressing (22 = 4). You could therefore set up your router to support four networks with this allocation: 192.168.5.0/24, 192.168.6.0/24, 192.168.7.0/24, and 192.168.8.0/24. Each of these networks could then have 254 hosts. CIDR lets you arbitrarily divide networks and subnetworks into increasingly smaller blocks along the way. Each router has enough memory to keep track of the addresses it needs to direct and aggregates the rest. This scheme uses memory and address space efficiently. For example, you could take 192.168.8.0/24 and further divide it into 16 networks with 14 hosts each. The 16 networks require four more bits (24 = 16), so you would have 192.168.8.0/28, 192.168.8.16/28, 192.168.8.32/28, and so on, up through the last subnet of 192.168.8.240/16, which would have the hosts 192.168.8.241 through 192.168.8.254.

Hostnames
People generally find it easier to work with names than with numbers, so Linux provides several ways to associate hostnames with IP addresses. The oldest method is to consult a list of names and addresses stored in the /etc/hosts file:
$ cat /etc/hosts 127.0.0.1 localhost 130.128.52.1 gw–example.example.com 130.128.52.2 bravo.example.com 130.128.52.3 hurrah.example.com 130.128.52.4 kudos.example.com localhost = 127.0.0.1

gw–example bravo hurrah kudos

The address 127.0.0.1 is reserved for the special hostname localhost, which serves as a hook for the system’s networking software to operate on the local machine without going onto a physical network. The names of the other systems are shown in two forms: in a fully qualified domain name (FQDN) format that is unique on the Internet and as a nickname that is locally unique. Use of these names is a convention; the system does not check the contents of the hosts file.

www.it-ebooks.info

Communicate Over a Network 381
NIS

As more hosts joined networks, storing these name-to-address mappings in a text file proved to be inefficient and inconvenient. The hosts file grew bigger and became impossible to keep up-to-date. To solve this problem Linux supports NIS (Network Information Service; Chapter 21), which was developed for use on Sun computers. NIS stores information in a database, making it easier to find a specific address, but it is useful only for host information within a single administrative domain. Hosts outside the domain cannot access the information. Also, by default NIS is not secure and is difficult to secure. The solution to this dilemma is DNS (Domain Name Service; Chapter 24). DNS effectively addresses the efficiency and update issues by arranging the entire network namespace (page 1177) as a hierarchy. Each domain in the DNS manages its own namespace (addressing and name resolution), and each domain can easily query for any host or IP address by following the tree up or down the namespace until it finds the appropriate domain. By providing a hierarchical naming structure, DNS distributes name administration across the entire Internet.

DNS

Communicate Over a Network
Many commands that you can use to communicate with other users on a single computer system have been extended to work over a network. Examples of extended utilities include electronic mail programs, information-gathering utilities (e.g., finger; page 167), and communications utilities (e.g., Empathy). These utilities are examples of the UNIX philosophy: Instead of creating a new, special-purpose tool, modify an existing one. Many utilities understand a convention for the format of network addresses: user@host (spoken as “user at host”). When you use an @ sign in an argument to one of these utilities, the utility interprets the text that follows as the name of a remote host. When you omit the @ sign, a utility assumes you are requesting information from or corresponding with someone on the local system. If you frequently use more than one system over a network, you might find it difficult to keep track of which system you are interacting with at any particular moment. If you set your prompt to include the hostname of the current system, it will always be clear which system you are using. To identify the computer you are using, run hostname or give the command uname –n:
$ hostname kudos

See page 310 for information on how you can change the prompt.

finger: Displays Information About Remote Users
The finger utility displays information about one or more users on a system. This utility was designed for local use, but when networks became popular, it was obvious that finger should be enhanced to reach out and collect information remotely. See page 167 for examples.

www.it-ebooks.info

382 Chapter 10 Networking and the Internet

The in.fingerd daemon security The finger daemon (in.fingerd) gives away system account information that can aid a malicious user. Some sites disable in.fingerd or randomize user account IDs to make a malicious user’s job more difficult. Disable in.fingerd by setting disable = yes in /etc/xinetd.d/finger and restarting xinetd. For more information refer to “The xinetd Superserver” on page 481.

The finger utility (finger package) works by querying a standard network service, the in.fingerd daemon, that runs on the system being queried. Although this service is available in the finger-server package, many sites choose not to install and run it to minimize the load on their systems, reduce security risks, or maintain privacy. When you use finger to obtain information about someone at such a site, you will see an error message or nothing at all. The remote in.fingerd daemon determines how much information to share and in what format.

Mailing List Servers
A mailing list server (listserv8) allows you to create and manage an email list. An electronic mailing list provides a means for people interested in a particular topic to participate in an electronic discussion. One of the most powerful features of most list servers is their ability to archive email postings to the list, create an archive index, and allow users to retrieve postings from the archive based on keywords or discussion threads. Typically you can subscribe and unsubscribe from the list with or without human intervention. The owner of the list can restrict who can subscribe, unsubscribe, and post messages to the list. See page 752 for instructions on configuring the Mailman list server. Other popular list servers include LISTSERV (www.lsoft.com), phplist (www.phplist.com), Lyris (www.lyris.com), and Majordomo (www.greatcircle.com/majordomo). Fedora/RHEL maintains mailing lists and list archives for those mailing lists at www.redhat.com/mailman/listinfo. Use Google to search on linux mailing list to find other lists.

Network Utilities
To realize the full benefits of a networked environment, it made sense to extend certain tools, some of which have already been described. The advent of networks also created a need for new utilities to control and monitor them, spurring the development of new tools that took advantage of network speed and connectivity. This section describes concepts and utilities for systems attached to a network.

Trusted Hosts
Although they are deprecated in favor of ssh (next), some commands, such as rcp and rsh, work only if the remote system trusts the local computer (that is, if the
8. Although the term listserv is sometimes used generically to include many different list server programs, it is a specific product and a registered trademark of L-soft International, Inc.: LISTSERV (for more information visit www.lsoft.com).

www.it-ebooks.info

Network Utilities

383

remote system knows the local computer and believes that it is not pretending to be another system). The /etc/hosts.equiv file lists trusted systems. For reasons of security, the root account does not rely on this file to identify trusted privileged users from other systems. Host-based trust is largely obsolete. Because there are many ways to circumvent trusted host security, including subverting DNS systems and IP spoofing (page 1171), authentication based on IP address is insecure. In a small homogeneous network it might be tempting to use these tools, and it might suffice. However, because ssh is easy to setup, provides privacy and authentication, and does not slow down modern CPUs appreciably, it is a good idea to use it instead of the older tools.

Do not share your login account security It is poor practice to use a ~/.rhosts file to allow another user to log in as you from a remote system without knowing your password. Do not compromise the security of your files or the entire system by sharing your login account. Use ssh and scp instead of rsh and rcp.

OpenSSH Tools
The OpenSSH project provides a set of tools that replace rcp, rsh, and others with secure equivalents. These tools are installed by default in Fedora/RHEL and can be used as drop-in replacements for their insecure counterparts. The OpenSSH tool suite is covered in detail in Chapter 18.

telnet: Logs In on a Remote System
You can use the TELNET protocol to interact with a remote computer. The telnet utility (telnet package), a user interface to this protocol, is older than ssh and is not secure. Nevertheless, it might work where ssh (page 681) is not available (there is more non-UNIX support for TELNET access than for ssh access). In addition, some legacy devices, such as terminal servers, facilities infrastructure, and network devices, still do not support ssh. The following example shows Sam using telnet to log in on a remote system that is running the in.telnetd daemon (controlled by xinetd [page 481]; telnet-server package):
[sam@guava ~]$ telnet plum Trying 172.16.192.151... Connected to plum. Escape character is '^]'. Fedora release 15 (Lovelock) Kernel 2.6.38-1.fc15.i686 on an i686 (1) login: sam Password: Last login: Tue Mar 8 13:20:14 from 172.16.192.1 ... [sam@plum ~]$ logout Connection closed by foreign host. [sam@guava ~]$

www.it-ebooks.info

384 Chapter 10 Networking and the Internet telnet versus ssh

When you connect to a remote UNIX or Linux system, telnet presents a textual login: prompt. Because telnet is designed to work with non-UNIX and non-Linux systems, it does not assume your remote username is the same as your local username (ssh does make this assumption). In some cases, telnet requires no login credentials.

telnet is not secure security Whenever you enter sensitive information, such as your password, while you are using telnet, it is transmitted in cleartext and can be read by someone who is eavesdropping on the session.

In addition, telnet allows you to configure special parameters, such as how RETURNs or interrupts are processed (ssh does not give you this option). When using telnet between UNIX and/or Linux systems, you rarely need to change any parameters. When you do not specify the name of a remote host on the command line, telnet runs in an interactive mode. The following example is equivalent to the previous telnet example:
[sam@guava ~]$ telnet telnet> open plum Trying 172.16.192.151... Connected to plum. Escape character is '^]'. ...

Before connecting to a remote system, telnet tells you what the escape character is; in most cases, it is ^] (where ^ represents the CONTROL key). When you press CONTROL-], you escape to telnet’s interactive mode. Continuing the preceding example:
[sam@guava ~]$ CONTROL-] telnet> ? Commands may be abbreviated.

Commands are:

close close current connection logout forcibly logout remote user and close the connection display display operating parameters mode try to enter line or character mode ('mode ?' for more) ... telnet> close Connection closed. [sam@guava ~]$

When you enter a question mark in response to the telnet> prompt, telnet lists its commands. The close command ends the current telnet session, returning you to the local system. To get out of telnet’s interactive mode and resume communication with the remote system, press RETURN in response to a prompt.

Using telnet to Connect to Other Ports
By default telnet connects to port 23, which is used for remote logins. However, you can use telnet to connect to other services by specifying a port number. In addition to standard services, many of the special remote services available on the Internet

www.it-ebooks.info

Network Utilities

385

use unallocated port numbers. Unlike the port numbers for standard protocols, these port numbers can be picked arbitrarily by the administrator of the service. Although telnet is no longer commonly employed to log in on remote systems, it is still used extensively as a debugging tool by allowing you to communicate directly with a TCP server. Some standard protocols are simple enough that an experienced user can debug problems by connecting to a remote service directly using telnet. If you are having a problem with a network server, a good first step is to try to connect to it using telnet. If you use telnet to connect to port 25 on a host, you can interact with SMTP. In addition, port 110 connects to the POP protocol, port 80 connects with a WWW server, and port 143 connects to IMAP. All these protocols are ASCII protocols and are documented in RFCs (page 1185). You can read the RFCs or search the Web for examples on how to use them interactively. In the following example, a system administrator who is debugging a problem with email delivery uses telnet to connect to the SMTP port (port 25) on the server at example.com to see why it is bouncing mail from the spammer.com domain. The first line of output indicates which IP address telnet is trying to connect to. After telnet displays the Connected to smtpsrv.example.com message, the user emulates an SMTP dialog, following the standard SMTP protocol. The first line, which starts with helo, begins the session and identifies the local system. After the SMTP server identifies itself, the user enters a line that identifies the mail sender as user@spammer.com. The SMTP server’s response explains why the message is bouncing, so the user ends the session with quit.
$ telnet smtpsrv 25 Trying 192.168.1.1... Connected to smtpsrv.example.com. Escape character is '^]'. helo example.com 220 smtpsrv.example.com ESMTP Sendmail 8.13.1/8.13.1; Wed, 4 May 2011 00:13:43 -0500 (CDT) 250 smtpsrv.example.com Hello desktop.example.com [192.168.1.97], pleased to meet you mail from:user@spammer.com 571 5.0.0 Domain banned for spamming quit 221 2.0.0 smtpsrv.example.com closing connection

The telnet utility allows you to use any protocol you want, as long as you know it well enough to type commands manually.

ftp: Transfers Files Over a Network
FTP (File Transfer Protocol) is a method of downloading files from and uploading files to a remote system using TCP/IP over a network. Most Web browsers can download files from FTP servers. Some vendors use anonymous FTP (page 707) to accept uploaded debugging sessions or to allow clients to download firmware. FTP is not a secure protocol; use it only for downloading public information from a public server. See page 702 for more information on FTP security. Use one of the

www.it-ebooks.info

386 Chapter 10 Networking and the Internet

OpenSSH tools described in Chapter 18 for secure communication. Chapter 19 covers FTP clients and servers.

ping: Tests a Network Connection
The ping9 and ping6 utilities (referred to in this section as ping; read the story of ping at http://ftp.arl.mil/~mike/ping.html) send an ECHO_REQUEST packet to a remote computer. This packet causes the remote system to send back a reply. This exchange is a quick way to verify that a remote system is available and to check how well the network is operating, such as how fast it is or whether it is dropping data packets. The ping utility uses the ICMP (Internet Control Message Protocol) protocol. Without any options, ping tests the connection once per second until you abort execution using CONTROL-C.
$ ping www.slashdot.org PING www.slashdot.org (216.34.181.48) 56(84) bytes of data. 64 bytes from star.slashdot.org (216.34.181.48): icmp_seq=1 64 bytes from star.slashdot.org (216.34.181.48): icmp_seq=2 64 bytes from star.slashdot.org (216.34.181.48): icmp_seq=3 64 bytes from star.slashdot.org (216.34.181.48): icmp_seq=4
CONTROL-C

ttl=238 ttl=238 ttl=238 ttl=238

time=70.2 time=72.6 time=57.5 time=71.2

ms ms ms ms

--- www.slashdot.org ping statistics --4 packets transmitted, 4 received, 0% packet loss, time 3024ms rtt min/avg/max/mdev = 57.553/67.899/72.605/6.039 ms

This example shows that a connection to www.slashdot.org is answered by star.slashdot.org (www.slashdot.org is an alias for star.slashdot.org) and that that system is up and available over the network. By default ping sends packets containing 64 bytes (56 data bytes and 8 bytes of protocol header information). In the preceding example, four packets were sent to the system star.slashdot.org before the user interrupted ping by pressing CONTROL-C. The four-part number in parentheses on each line is the remote system’s IP address. A packet sequence number (named icmp_seq) is also given. If a packet is dropped, a gap occurs in the number sequence. The round-trip time is listed last; it represents the time (in milliseconds) that elapsed from when the packet was sent from the local system to the remote system until the reply from the remote system was received by the local system. This time is affected by the distance between the two systems, network traffic, and the load on both computers. Before it terminates, ping summarizes the results, indicating how many packets were sent and received as well as the minimum, average, maximum, and mean deviation round-trip times it measured. Use ping6 to test IPv6 networks.

9. The name ping mimics the sound of a sonar burst used by submarines to identify and communicate with each other. The word ping also expands to packet internet groper.

www.it-ebooks.info

Network Utilities

387

When ping cannot connect tip If it is unable to contact the remote system, ping continues trying until you interrupt it by pressing
CONTROL-C. A system might not answer for any of several reasons: The remote computer might be

down, the network interface or some part of the network between the systems might be broken, a software failure might have occurred, or the remote machine might be set up (for reasons of security) not to return pings (try pinging www.microsoft.com or www.ibm.com).

traceroute: Traces a Route Over the Internet
The traceroute and traceroute6 utilities (referred to in this section as traceroute; traceroute package) trace the route that an IP packet follows, including all intermediary points traversed (called network hops), to its destination (the argument to traceroute—a remote system). They display a numbered list of hostnames, if available, and IP addresses, together with the round-trip time it took for a packet to reach each router along the way and for the local system to receive a response. You can put this information to good use when you are trying to identify the location of a network bottleneck. The traceroute utility has no concept of the path from one host to the next; instead, it simply sends UDP packets with increasing TTL (time to live) values. TTL is an IP header field that indicates how many more hops the packet should be allowed to make before being discarded or returned. Each router along the way inspects the TTL and decrements it by 1. When the TTL reaches 0 the router that has the packet sends back an ICMP TIME EXCEEDED to the local system, where traceroute records the IP address of the router that sent it back. The result is a list of hosts that the packet traveled through to get to its destination. The traceroute utility can help solve routing configuration problems and locate routing path failures. When you cannot reach a host, use traceroute to discover which path the packet follows, how far it gets, and what the delay is. The example on the next page shows the output of traceroute when it follows a route from a local computer to www.linux.org. The first line indicates the IP address of the target, the maximum number of hops that will be traced, and the size of the packets that will be used. Each numbered line contains the name and IP address of the intermediate destination, followed by the time it takes a packet to make a trip to that destination and back again. The traceroute utility sends three packets to each destination; thus three times appear on each line. Line 1 shows the statistics when a packet is sent to the local gateway (less than 3 milliseconds). Lines 4–6 show the packet bouncing around Mountain View (California) before it goes to San Jose. Between hops 13 and 14 the packet travels across the United States (San Francisco to somewhere in the East). By hop 18 the packet has found www.linux.org. The traceroute utility displays asterisks when it does not receive a response. Each asterisk indicates that traceroute has waited three seconds. Use traceroute6 to test IPv6 networks.

www.it-ebooks.info

388 Chapter 10 Networking and the Internet
$ /usr/sbin/traceroute www.linux.org traceroute to www.linux.org (198.182.196.56), 30 hops max, 38 byte packets 1 gw.localco.com. (204.94.139.65) 2.904 ms 2.425 ms 2.783 ms 2 covad-gw2.meer.net (209.157.140.1) 19.727 ms 23.287 ms 24.783 ms 3 gw-mv1.meer.net (140.174.164.1) 18.795 ms 24.973 ms 19.207 ms 4 d1-4-2.a02.mtvwca01.us.ra.verio.net (206.184.210.241) 59.091 ms d1-10-0-0-200.a03. mtvwca01.us.ra.verio.net (206.86.28.5) 54.948 ms 39.485 ms 5 fa-11-0-0.a01.mtvwca01.us.ra.verio.net (206.184.188.1) 40.182 ms 44.405 ms 49.362 ms 6 p1-1-0-0.a09.mtvwca01.us.ra.verio.net (205.149.170.66) 78.688 ms 66.266 ms 28.003 ms 7 p1-12-0-0.a01.snjsca01.us.ra.verio.net (209.157.181.166) 32.424 ms 94.337 ms 54.946 ms 8 f4-1-0.sjc0.verio.net (129.250.31.81) 38.952 ms 63.111 ms 49.083 ms 9 sjc0.nuq0.verio.net (129.250.3.98) 45.031 ms 43.496 ms 44.925 ms 10 mae-west1.US.CRL.NET (198.32.136.10) 48.525 ms 66.296 ms 38.996 ms 11 t3-ames.3.sfo.us.crl.net (165.113.0.249) 138.808 ms 78.579 ms 68.699 ms 12 E0-CRL-SFO-02-E0X0.US.CRL.NET (165.113.55.2) 43.023 ms 51.910 ms 42.967 ms 13 sfo2-vva1.ATM.us.crl.net (165.113.0.254) 135.551 ms 154.606 ms 178.632 ms 14 mae-east-02.ix.ai.net (192.41.177.202) 158.351 ms 201.811 ms 204.560 ms 15 oc12-3-0-0.mae-east.ix.ai.net (205.134.161.2) 202.851 ms 155.667 ms 219.116 ms 16 border-ai.invlogic.com (205.134.175.254) 214.622 ms * 190.423 ms 17 router.invlogic.com (198.182.196.1) 224.378 ms 235.427 ms 228.856 ms 18 www.linux.org (198.182.196.56) 207.964 ms 178.683 ms 179.483 ms

Some firewalls block UDP/traceroute. If traceroute fails, try using the –T (sends TCP SYN packets) or –I (sends ICMP ECHO packets) options. Another useful tool is mtr (mtr package), which provides traceroute functionality plus information about dropped packets. The mtr utility is useful for debugging packet loss along a multi-hop network path.

host and dig: Query Internet Nameservers
Given a name, the host utility looks up an IP address, or vice versa. The following example shows how to use host to look up the domain name of a machine, given an IP address:
$ host 64.13.141.6 6.141.13.64.in-addr.arpa domain name pointer ns.meer.net.

You can also use host to determine the IP address of a domain name:
$ host ns.meer.net ns.meer.net has address 64.13.141.6

The dig (domain information groper) utility queries DNS servers and individual machines for information about a domain. A powerful utility, dig has many features you might never use. It is more complex than host. Chapter 24 on DNS has many examples of the use of host and dig.

whois: Looks Up Information About an Internet Site
The whois utility (whois package) queries a whois server for information about an Internet site. This utility returns site contact and InterNIC or other registry information that can help you track down the person who is responsible for a site: Perhaps

www.it-ebooks.info

Network Utilities

389

that person is sending you or your company spam. Many sites on the Internet are easier to use and faster than whois. Use a browser and search engine to search on whois or go to www.networksolutions.com/whois or www.db.ripe.net/whois to get started. When you do not specify a whois server, whois defaults to whois.internic.net. Use the –h option to whois to specify a different whois server. See the whois info page for more options and setup information. To obtain information on a domain name, specify the complete domain name, as in the following example:
$ whois sobell.com Domain Name: SOBELL.COM Registrar: GODADDY.COM, INC. Whois Server: whois.godaddy.com Referral URL: http://registrar.godaddy.com Name Server: NS1.HUNGERHOST.COM ... Registrant: Sobell Associates Inc 660 Market Street Fifth Floor San Francisco, California 94104 United States Registered through: GoDaddy.com, Inc. (http://www.godaddy.com) Domain Name: SOBELL.COM Created on: 07-Apr-95 Expires on: 08-Apr-13 Last Updated on: 01-Mar-10 Administrative Contact: Sobell, Mark sobell@meer.net Sobell Associates Inc 660 Market Street Fifth Floor SAN FRANCISCO, California 94104 United States 18888446337 Fax -- 18888446337 Technical Contact: W., Tim hostmaster@meer.net meer.net po box 390804 Mountain View, California 94039 United States 18888446337 Fax -- 18888446337 Domain servers in listed order: NS1.HUNGERHOST.COM NS2.HUNGERHOST.COM

www.it-ebooks.info

390 Chapter 10 Networking and the Internet

Several top-level registries serve various regions of the world. You are most likely to use the following ones: North American registry European registry Asia-Pacific registry U.S. military U.S. government whois.arin.net www.ripe.net www.apnic.net whois.nic.mil www.nic.gov

Distributed Computing
When many similar systems are part of a network, it is often desirable to share common files and utilities among them. For example, a system administrator might choose to keep a copy of the system documentation on one computer’s disk and to make those files available to remote systems. In this case, the system administrator configures the files so users who need to access the online documentation are not aware that the files are stored on a remote system. This type of setup, which is an example of distributed computing, not only conserves disk space but also allows you to update one central copy of the documentation rather than tracking down and updating copies scattered throughout the network on many different systems. Figure 10-2 illustrates a fileserver that stores the system manual pages and users’ home directories. With this arrangement, a user’s files are always available to that user no matter which system the user logs in on. Each system’s disk might contain a directory to hold temporary files as well as a copy of the operating system. Chapter 22 contains instructions for setting up NFS clients and servers in networked configurations.

The Client/Server Model
Mainframe model

The client/server model was not the first computational model. First came the mainframe, which follows a one-machine-does-it-all model. That is, all the intelligence resides in one system, including the data and the program that manipulates and reports on the data. Users connect to a mainframe using terminals. With the introduction of PCs, file-sharing networks became available. In this scheme data is downloaded from a shared location to a user’s PC, where a program then manipulates the data. The file-sharing model ran into problems as networks expanded and more users needed access to the data. In the client/server model, a client uses a protocol, such as HTTP or SCP, to request services, and a server provides the services the client requests. Rather than providing data files as the file-sharing model does, the server in a client/server relationship is a database that provides only those pieces of information the client needs or requests. The client/server model dominates UNIX and Linux system networking and underlies most of the network services described in this book. FTP, NFS, DNS, email, and HTTP (the Web browsing protocol) all rely on the client/server model. Some servers, such as

File-sharing model

Client/server model

www.it-ebooks.info

Distributed Computing 391

/usr/man /home Linux Linux

Fileserver
Figure 10-2

A fileserver

Web servers and browser clients, are designed to interact with specific utilities. Other servers, such as those supporting DNS, communicate with one another, in addition to answering queries from a variety of clients. Clients and servers can reside on the same or different systems running the same or different operating systems. The systems can be proximate or thousands of miles apart. A system that is a server to one system can turn around and act as a client to another. A server can reside on a single system or, as is the case with DNS, be distributed among thousands of geographically separated systems running many different operating systems.
Peer-to-peer model

The peer-to-peer (PTP) model, in which either program can initiate a transaction, stands in contrast to the client/server model. PTP protocols are common on small networks. For example, Microsoft’s Network Neighborhood and Apple’s Bonjour both rely on broadcast-based PTP protocols for browsing and automatic configuration. The Zeroconf multicast DNS protocol is a PTP alternative DNS for small networks. The highest-profile PTP networks are those used for file sharing, such as Kazaa and GNUtella. Many of these networks are not pure PTP topologies. Pure PTP networks do not scale well, so networks such as BitTorrent employ a hybrid approach.

DNS: Domain Name Service
DNS is a distributed service: Nameservers on thousands of machines around the world cooperate to keep the DNS database up-to-date. The database itself, which maps hundreds of thousands of alphanumeric hostnames to numeric IP addresses, does not exist in one place. That is, no system has a complete copy of the database. Instead, each system that runs DNS knows which hosts are local to that site and understands how to contact other nameservers to learn about other, nonlocal hosts. Like the Linux filesystem, DNS is organized hierarchically. Each country has an ISO (International Organization for Standardization) country code designation as its domain name. (For example, AU represents Australia, IL is Israel, and JP is Japan; see www.iana.org/domains/root/cctld for a complete list.) Although the United States is represented in the same way (US) and uses the standard two-letter Postal Service state abbreviations to identify the next level of the domain, only governments and a few organizations use these codes. Schools in the US domain are represented by a third(and sometimes second-) level domain: k12. For example, the domain name for Myschool in New York state could be www.myschool.k12.ny.us.

www.it-ebooks.info

392 Chapter 10 Networking and the Internet

gov

com

edu

mil

org

net

example

berkeley

moma

bravo
Figure 10-3

kudos

okeeffe

okeeffe

U.S. top-level domains

Following is a list of the seven original top-level domains. These domains are used extensively within the United States and, to a lesser degree, by users in other countries: COM EDU GOV INT MIL NET ORG Commercial enterprises Educational institutions Nonmilitary government agencies International organizations recognized by treaty Military government agencies Networking organizations Other (often nonprofit) organizations

Recently, the following additional top-level domains have been approved for use. Visit en.wikipedia.org/wiki/List_of_Internet_top-level_domains for a complete, current list. AERO BIZ COOP INFO MUSEUM NAME
FQDN

Air-transport industry Business Cooperatives Unrestricted use Museums Name registries

Like Internet addresses, domain names were once assigned by the Network Information Center (NIC); now they are assigned by several companies. A system’s full name, referred to as its fully qualified domain name (FQDN), is unambiguous in the way that a simple hostname cannot be. The system okeeffe.berkeley.edu at the University of California at Berkeley (Figure 10-3) is not the same as one named okeeffe.moma.org, which might represent a host at the Museum of Modern Art. The domain name not only tells you something about where the system is located but also adds enough diversity to the namespace to avoid confusion when different sites choose similar names for their systems. Unlike the filesystem hierarchy, the top-level domain name appears last (reading from left to right). Also, domain names are not case sensitive, so the names okeeffe.berkeley.edu, okeeffe.Berkeley.edu, and okeeffe.Berkeley.EDU refer to the same computer. Once a domain has been assigned, the local site is free to extend the hierarchy to meet local needs.

www.it-ebooks.info

Distributed Computing 393

With DNS, email addressed to user@example.com can be delivered to the computer named example.com that handles the corporate mail and knows how to forward messages to user mailboxes on individual machines. As the company grows, its site administrator might decide to create organizational or geographical subdomains. The name delta.ca.example.com might refer to a system that supports California offices, for example, while alpha.co.example.com is dedicated to Colorado. Functional subdomains are another choice, with delta.sales.example.com and alpha.dev.example.com representing the sales and development divisions, respectively.
BIND

On Linux systems, the most common interface to the DNS is BIND (Berkeley Internet Name Domain). BIND follows the client/server model. On any given local network, one or more systems might be running a nameserver, supporting all the local hosts as clients. When it wants to send a message to another host, a system queries the nearest nameserver to learn the remote host’s IP address. The client, called a resolver, might be a process running on the same computer as the nameserver, or it might pass the request over the network to reach a server. To reduce network traffic and facilitate name lookups, the local nameserver maintains some knowledge of distant hosts. If the local server must contact a remote server to pick up an address, when the answer comes back, the local server adds that address to its internal table and reuses it for a while. The nameserver deletes the nonlocal information before it can become outdated. Refer to “TTL” on page 1194. The system’s translation of symbolic hostnames into IP addresses is transparent to most users; only the system administrator of a networked system needs to be concerned with the details of name resolution. Systems that use DNS for name resolution are generally capable of communicating with the greatest number of hosts—more than would be practical to maintain in a /etc/hosts file or private NIS database. Chapter 24 covers setting up and running a DNS server. Three common sources are referenced for hostname resolution: NIS, DNS, and system files (such as /etc/hosts). Linux does not ask you to choose among these sources; rather, the nsswitch.conf file (page 494) allows you to choose any of these sources, in any combination, and in any order.

Ports
Ports are logical channels on a network interface and are numbered from 1 to 65,535. Each network connection is uniquely identified by the IP address and port number of each endpoint. In a system that has many network connections open simultaneously, the use of ports keeps packets (page 1180) flowing to and from the appropriate programs. A program that needs to receive data binds to a port and then uses that port for communication.
Privileged ports

Services are associated with specific ports, generally with numbers less than 1024. These ports are called privileged (or reserved) ports. For security reasons, only a process running with root privileges can bind to a privileged port. A service run on a privileged port provides assurance that the service is being provided by someone

www.it-ebooks.info

394 Chapter 10 Networking and the Internet

with authority over the system, with the exception that any user on Windows 98 and earlier Windows systems can bind to any port. Commonly used ports include 22 (SSH), 23 (TELNET), 80 (HTTP), 111 (Sun RPC), and 201–208 (AppleTalk).

Privileged ports do not provide absolute security security Any user who can gain root privileges can easily start a service on any port. Thus, it is necessary to treat privileged ports as a necessary but not sufficient component in a security policy and to be aware of which other systems are on the local network.

NIS: Network Information Service
NIS (Network Information Service) simplifies the maintenance of frequently used administrative files by keeping them in a central database and having clients contact the database server to retrieve information from the database. Just as DNS addresses the problem of keeping multiple copies of hosts files up-to-date, NIS deals with the issue of keeping system-independent configuration files (such as /etc/passwd) current. Although NIS has been functionally replaced by LDAP, LDAP is harder to configure, and so NIS is still in common use. Refer to Chapter 21 for coverage of NIS and LDAP.

NFS: Network Filesystem
The NFS (Network Filesystem) protocol allows a server to share selected local directory hierarchies with client systems on a heterogeneous network. Files on the remote fileserver appear as if they are present on the local system. Chapter 22 covers NFS.

optional

Network Services
Linux Internet services are provided by daemons that run continuously or by a daemon that is started automatically by the xinetd daemon (page 481) or by the systemd init daemon (page 426) when a service request comes in. The /etc/services file lists network services (for example, telnet, ftp, and ssh) and their associated numbers. Any service that uses TCP/IP or UDP/IP has an entry in this file. IANA (Internet Assigned Numbers Authority) maintains a database of all permanent, registered services. The /etc/services file usually lists a small, commonly used subset of services. Most executable daemon files are stored in /usr/sbin. By convention the names of many daemons end with the letter d to distinguish them from utilities (one common daemon whose name does not end in d is sendmail). The prefix in. or rpc. is often used for daemon names. Give the command ls /usr/sbin/*d to see a list of many of the daemon programs on the local system. Refer to “Configuring Daemons (Services)” on page 433 (Fedora) and to “The Upstart Event-Based init Daemon” on page 436 (RHEL)for information about starting and stopping these daemons.

www.it-ebooks.info

Distributed Computing 395

To see how a daemon works, consider what happens when you run ssh. The local system contacts the ssh daemon (sshd) on the remote system to establish a connection. The two systems negotiate the connection according to a fixed protocol. Each system identifies itself to the other, and they then take turns asking each other specific questions and waiting for valid replies. Each network service follows its own protocol.

Common Daemons
In addition to the daemons that support the utilities described in this chapter, many other daemons support system-level services that you will not typically interact with. Table 10-3 lists some of these daemons.

Table 10-3
Daemon
acpid

Common daemons Used for or by
Advanced configuration and power interface

Function
Flexible daemon for delivering ACPI events. Replaces apmd.

anacron

anacrontab

Used for periodic execution of tasks. This daemon looks in the /etc/anacrontab file. When a task comes up for execution, anacron executes it as the user who owns the file that describes the task. See page 611. Reports and takes action on specified changes in system power, including shutdowns. Useful with machines, such as laptops, that run on batteries. Executes a command once at a specific time and date. See crond for periodic execution of a command. See page 615. Automatically mounts filesystems when they are accessed. Automatic mounting is a way of demand-mounting remote directories without having to hard-configure them into /etc/fstab. See page 811. Used for periodic execution of tasks. This daemon looks in the /var/spool/cron directory for files with filenames that correspond to usernames. It also looks at the /etc/crontab file and at files in the /etc/cron.d directory. When a task comes up for execution, crond executes it as the user who owns the file that describes the task. See page 611. Configures local and remote printers and handles printing jobs. See Chapter 14 for more information.

apmd

Advanced power management

atd

at
Automatic mounting

automount

crond

cron

cupsd

Printing daemon

www.it-ebooks.info

396 Chapter 10 Networking and the Internet

Table 10-3
Daemon
dhcpd, dhcpd6

Common daemons (continued) Used for or by
DHCP

Function
Assigns Internet address, network mask, default gateway, DNS, and other information to hosts. This protocol answers DHCP requests and, optionally, BOOTP requests. Refer to “DHCP: Configures Network Interfaces” on page 489. The exim4 daemon came from the University of Cambridge. The exim4 daemon listens on port 25 for incoming mail connections and then calls a local delivery agent, such as /bin/mail. Mail user agents (MUAs), such as KMail and Thunderbird, typically use exim4 to deliver mail messages. See also sendmail. Handles FTP requests. Refer to “ftp: Transfers Files over a Network” on page 385. See also vsftpd (page 701). Can be launched by xinetd.

exim4

Mail programs

ftpd gpm

FTP

General-purpose Allows you to use a mouse to cut and paste text on console mouse or GNU paste applications. manager Hardware Abstraction Layer (HAL) daemon HTTP Collects and maintains system hardware information in real time. Part of the hal package. The Web server daemon (Apache; page 917). Handles requests for user information from the finger utility. Launched by xinetd. See page 381. Listens for service requests on network connections and starts up the appropriate daemon to respond to any particular request. Deprecated in favor of xinetd. Authentication daemon The MySQL daemon DNS Allows processes running on different machines to authenticate each other. Part of the munge package. Supports queries on a MySQL database. It is the M in LAMP (Linux, Apache, MySQL, PHP/Perl/Python). See page 635. Supports DNS. See page 845. These five daemons operate together to handle NFSv3 (page 791) operations. The nfsd daemon handles file and directory requests. The statd and lockd daemons implement network file and record locking. The mountd daemon converts filesystem name requests from the mount utility into NFS handles and checks access permissions. If disk quotas are enabled, rquotad handles those. Synchronizes time on network computers. For more information visit www.ntp.org.

hald

httpd in.fingerd inetd

finger

munged mysqld named

nfsd, statd, lockd, NFS mountd, rquotad

ntpd

NTP

www.it-ebooks.info

Distributed Computing 397

Table 10-3
Daemon
radvd rpcbind

Common daemons (continued) Used for or by
IPv6 RPC

Function
IPv6 Router ADVertisement Daemon. Provides autoconfiguration on the network if the local system is being used as a router. Maps incoming requests for RPC service numbers to TCP or UDP port numbers on the local system. Refer to “RPC Network Services” on page 398. Transcribes important system events and stores them in files and/or forwards them to users or another host running the rsyslogd daemon. See page 623. The sendmail daemon came from Berkeley UNIX and has been available for a long time. The de facto mail transfer program on the Internet, the sendmail daemon always listens on port 25 for incoming mail connections and then calls a local delivery agent, such as /bin/mail. Mail user agents (MUAs), such as KMail and Thunderbird, typically use sendmail to deliver mail messages. See also exim4. See page 729. Monitors the Self-Monitoring, Analysis and Reporting Technology (SMART) system built into many hard drives. SMART tests drive reliability, predicts failure, and performs other self tests. Part of the smartmontools package. See page 81. Allow Windows PCs to share files and printers with UNIX and Linux computers. See page 817. Enables secure logins between remote systems (page 688). Allows you to have a conversation with another user on the same or a remote machine. The talkd daemon handles the connections between the machines. The talk utility on each system contacts the talkd daemon on the other system for a bidirectional conversation. Launched by xinetd. One of the original Internet remote access protocols. Launched by xinetd. See page 383. Used to boot a system or get information from a network. Examples include network computers, routers, and some printers. Launched by xinetd. On a LAN synchronizes time with other computers that are also running timed. Listens for service requests on network connections and starts up the appropriate daemon to respond to any particular request. Because of xinetd, a system does not need the daemons running continually to handle various network requests. See page 481.

rsyslogd

System log

sendmail

Mail programs

smartd

SMART Disk Monitoring Daemon

smbd, nmbd sshd talkd

Samba

ssh, scp talk

telnetd tftpd

TELNET TFTP

timed xinetd

Time server Internet superserver

www.it-ebooks.info

398 Chapter 10 Networking and the Internet

Proxy Servers
A proxy is a network service that is authorized to act for a system while not being part of that system. A proxy server or proxy gateway provides proxy services; it is a transparent intermediary that relays communications back and forth between an application (e.g., a browser) and a server that usually resides outside of a LAN and frequently on the Internet. When more than one process uses the proxy gateway/server, the proxy must keep track of which processes are connecting to which hosts/servers so that it can route the return messages to the proper process. The most commonly encountered proxies are email and Web proxies. A proxy server/gateway insulates the local computer from all other computers or from specified domains by using at least two IP addresses: one to communicate with the local computer and one to communicate with a server. The proxy server/gateway examines and changes the header information on all packets it handles so it can encode, route, and decode them properly. The difference between a proxy gateway and a proxy server is that the proxy server usually includes cache (page 1155) to store frequently used Web pages so the next request for that page is available locally and quickly; a proxy gateway typically does not use cache. The terms “proxy server” and “proxy gateway” are frequently used interchangeably. Proxy servers/gateways are available for such common Internet services as HTTP, HTTPS, FTP, SMTP, and SNMP. When an HTTP proxy sends queries from local systems, it presents a single organizationwide IP address (the external IP address of the proxy server/gateway) to all servers. It funnels all user requests to the appropriate servers and keeps track of them. When the responses come back, the HTTP proxy fans them out to the appropriate applications using each machine’s unique IP address, thereby protecting local addresses from remote/specified servers. Proxy servers/gateways are generally just one part of an overall firewall strategy to prevent intruders from stealing information or damaging an internal network. Other functions, which can be either combined with or kept separate from the proxy server/gateway, include packet filtering, which blocks traffic based on origin and type, and user activity reporting, which helps management learn how the Internet is being used.

RPC Network Services
Much of the client/server interaction over a network is implemented using the RPC (Remote Procedure Call) protocol, which is implemented as a set of library calls that make network access transparent to the client and server. RPC specifies and interprets messages but does not concern itself with transport protocols; it runs on top of TCP/IP and UDP/IP. Services that use RPC include NFS and NIS. RPC was developed by Sun as ONC RPC (Open Network Computing Remote Procedure Calls) and differs from Microsoft RPC. rpcbind In the client/server model, a client contacts a server on a specific port (page 393) to avoid any mixup between services, clients, and servers. RPC has two possible ways

www.it-ebooks.info

Distributed Computing 399

of registering a port. The first works in the manner of a traditional daemon: The server binds to the port it wants to bind to but then registers with the RPC portmapper daemon (rpcbind) on the server. As an example, NFS uses this mechanism to always bind to port 2049. The client's port is irrelevant most of the time, so it picks a random port. More commonly, to avoid maintaining a long list of port numbers and to enable new clients/servers to start up without registering a port number with a central registry, when a server that uses RPC starts, it looks in /etc/rpc for the service number and then registers itself with rpcbind as just described. The rpcbind daemon assigns the server a TCP or UDP port number, and the server binds to that port. RPC servers typically use service numbers (/etc/rpc) that were defined by Sun, but the TCP or UDP port number it uses is random. When a client wants to connect to an RPC server, it first asks rpcbind on the server, which listens on port 111, for the name of the service it is interested in (e.g., mountd). If that service is registered, rpcbind returns the TCP or UDP port number of the server to the client. The client then contacts the RPC server daemon directly to finish the transaction.
Files

The /etc/rpc file (page 511) maps RPC service names to RPC numbers. The /etc/services file (page 511) maps system service names to TCP or UDP ports. The sequence of events for communication between an RPC client and server is as follows: 1. The client system needs to talk to a server application running on a remote system. The client system knows which system the server application is running on but not which port it is using. The client system looks for the service name in /etc/rpc to determine the service number. 2. The client system contacts rpcbind on port 111 of the remote system with the service number to ask which UDP or TCP port the server on the remote system is listening on. 3. The rpcbind daemon on the remote system checks which server applications have registered with it. If the service number the client system asked for is registered, the rpcbind daemon on the remote system returns the UDP or TCP port assigned to that server application. 4. The RPC libraries on the client system encode procedure calls and send them to the server application on the remote system on the specified port. (The client system issues a “read record from a file” request.) 5. The server application running on the remote system receives the call, sends it through the RPC libraries to decode it, and generates results. (The filesystem receives the “read record from file” request.) 6. The server application passes the results back through the RPC libraries, which encode the results, and then sends the encoded results to the client using the client’s registered port. (The read record is returned to the calling program.)

RPC client/server communication

www.it-ebooks.info

400 Chapter 10 Networking and the Internet

7. The client system receives the results, passes them through the RPC libraries for decoding, and presents the decoded results to the user or application that requested them. When RPC servers are started by the xinetd daemon (page 481), the rpcbind daemon must be started before the xinetd daemon is invoked. The init scripts (page 442) make sure rpcbind starts before xinetd. If the rpcbind daemon stops, you must restart all RPC servers on the local system.

WWW: World Wide Web
The World Wide Web (WWW, W3, or the Web) provides a unified, interconnected interface to the vast amount of information stored on computers around the world. The idea that spawned the World Wide Web came from the mind of Tim Berners-Lee (www.w3.org/People/Berners-Lee) of the European Particle Physics Laboratory (CERN) in response to a need to improve communications throughout the high-energy physics community. The first-generation solution consisted of a notebook program named Enquire, short for Enquire Within Upon Everything (the name of a book from Berners-Lee’s childhood), which he created in 1980 on a NeXT computer and which supported links between named nodes. Not until 1989 was the concept proposed as a global hypertext project to be known as the World Wide Web. In 1990, Berners-Lee wrote a proposal for a hypertext project, which eventually produced HTML (Hypertext Markup Language), the common language of the Web. The World Wide Web program became available on the Internet in the summer of 1991. By designing the tools to work with existing protocols, such as FTP and gopher, the researchers who created the Web produced a system that is generally useful for many types of information and across many types of hardware and operating systems. The WWW is another example of the client/server paradigm. You use a WWW client application, or browser, to retrieve and display information stored on a server that might be located anywhere on the local network or the Internet. WWW clients can interact with many types of servers. For example, you can use a WWW client to contact a remote FTP server and display the list of files it offers for anonymous FTP download. Most commonly you use a WWW client to contact a WWW server, which offers support for the special features of the World Wide Web that are described in the remainder of this chapter. The power of the Web derives from its use of hypertext, a way to navigate through information by following cross-references (called links) from one piece of information to another. To use the Web effectively, you need to run interactive network applications. The first GUI for browsing the Web was a tool named Mosaic, which was released in February 1993. Designed at the National Center for Supercomputer Applications at the University of Illinois, its introduction sparked a dramatic increase in the number of users of the World Wide Web. Marc Andreessen, who

www.it-ebooks.info

WWW: World Wide Web

401

participated in the Mosaic project at the University of Illinois, later cofounded Netscape Communications with the founder of Silicon Graphics, Jim Clark. The pair created Netscape Navigator, a Web client program that was designed to perform better and support more features than the Mosaic browser. Netscape Navigator enjoyed immense success and was a popular choice for exploring the World Wide Web. Important for Linux users is the fact that from its inception Netscape has provided versions of its tools that run on Linux.

Browsers
Mozilla (www.mozilla.org) is the open-source counterpart to Netscape and was first released in March 1998, based on Netscape 4 code. Since then, Mozilla has been under continuous development by employees of Netscape (now a division of AOL), Red Hat, and other companies and by contributors from the community. Firefox is the Web browser component of Mozilla. KDE offers Konqueror, an all-purpose file manager and Web browser. Other browsers include Safari (www.apple.com/safari), Chrome (www.google.com/chrome), Epiphany (projects.gnome.org/epiphany) and Opera (www.opera.com). Although each Web browser is unique, all of them allow you to move about the Internet, viewing HTML documents, watching videos, and retrieving files. If you do not use the X Window System, try a text browser, such as lynx or links. The lynx browser works well with Braille terminals.

Search Engines
Search engine is a name that applies to a group of hardware and software tools that help you search for World Wide Web sites that contain specific information. A search engine relies on a database of information collected by a Web crawler, a program that regularly looks through the millions of pages that make up the World Wide Web. A search engine must also have a way of collating the information the Web crawler collects so you can access it quickly, easily, and in a manner that makes it most useful to you. This part of the search engine, called an index, allows you to search for a word, a group of words, or a concept; it returns the URLs of Web pages that pertain to what you are searching for. Many different types of search engines are available on the Internet, each with its own set of strengths and weaknesses.

URL: Uniform Resource Locator
Consider the URL (Uniform Resource Locator) www.w3.org/Consortium/siteindex. The first component in the URL indicates the type of resource, in this case http (HTTP—Hypertext Transfer Protocol). Other valid resource names, such as https (HTTPS—secure HTTP) and ftp (FTP—File Transfer Protocol), represent information available on the Web using other protocols. Next come a colon and double slash (://). Frequently the http:// string is omitted from a URL in print, as you seldom need to enter it to reach the URL. The next element is the full name of the host that acts as the server for the information (www.w3.org/). The rest of the URL consists of a relative pathname to the file that contains the information (Consortium/siteindex). If you

www.it-ebooks.info

402 Chapter 10 Networking and the Internet

enter a URL in the location bar of a Web browser, the Web server returns the page, frequently an HTML (page 1168) file, pointed to by this URL. By convention many sites identify their WWW servers by prefixing a host or domain name with www. For example, you can reach the Web server at the New Jersey Institute of Technology at www.njit.edu. When you use a browser to explore the World Wide Web, you might never need to enter a URL. However, as more information is published in hypertext form, you cannot help but find URLs everywhere—not just online in email messages and blogs, but also in newspapers, in advertisements, and on product labels.
URI

Whereas a URL indicates the location of a resource and a method of retrieving it (e.g., a network address and protocol), a URI (Uniform Resource Identifier) simply indicates something about the resource (e.g., an ISBN number). All URLs are URIs, but not all URIs are URLs. The terms are frequently used interchangeably because very few URIs that are not URLs are in common use.

Chapter Summary
A Linux system attached to a network is probably communicating on an Ethernet network, which might in turn be linked to other local area networks (LANs) and wide area networks (WANs). Communication between LANs and WANs requires the use of gateways and routers. Gateways translate the local data into a format suitable for the WAN, and routers make decisions about the optimal routing of the data along the way. The most widely used network, by far, is the Internet. Basic networking tools allow Linux users to log in and run commands on remote systems (ssh, telnet, rsync) and copy files quickly from one system to another (scp, ftp/sftp). Other features, such as the Network Filesystem (NFS), were created to extend the basic UNIX model and to simplify information sharing. Concern is growing about our ability to protect the security and privacy of machines connected to networks and of data transmitted over networks. Toward this end, many tools and protocols have been created: ssh, scp, HTTPS, IPv6, firewall hardware and software, VPN, and so on. Many of these tools take advantage of newer, more impenetrable encryption techniques. In addition, some weaker concepts (such as that of trusted hosts) and some tools (such as finger and rwho) have been deprecated in the name of security. Computer networks offer two major advantages over other ways of connecting computers: They enable systems to communicate at high speeds, and they require few physical interconnections (typically one per system). The Internet Protocol (IP), the universal language of the Internet, has made it possible for dissimilar computer systems around the world to readily communicate with one another. Technological advances continue to improve the performance of computer systems and the networks that link them.

www.it-ebooks.info

Exercises

403

The rapid increase of network communication speeds in recent years has encouraged the development of many new applications and services. The World Wide Web provides access to vast information stores on the Internet and makes extensive use of hypertext links to promote efficient searching through related documents. It adheres to the client/server model that is so pervasive in networking. Typically the WWW client is local to a site or is made available through an Internet service provider. WWW servers are responsible for providing the information requested by their many clients. Mozilla/Firefox is a WWW client program that has enormous popular appeal. Firefox and other browsers use a GUI to give you access to text, picture, and audio information: Making extensive use of these hypermedia simplifies access to and enhances the presentation of information.

Exercises
1. Describe the similarities and differences between these utilities: a. scp and ftp b. ssh and telnet c. rsh and ssh 2. Describe two ways to find out who is logged in on some of the other machines attached to your network. 3. Explain the client/server model. Give three examples of services on Linux systems that take advantage of this model. 4. A software implementation of chess was developed by GNU and is available for free. How can you use the Internet to find and download this program? 5. What is the difference between the World Wide Web and the Internet? 6. If you have access to the World Wide Web, answer the following questions. a. Which browser do you use? b. What is the URL of the author of this book’s home page? How many links does it have? c. Does your browser allow you to create bookmarks? If so, how do you create a bookmark? How can you delete one? 7. Give one advantage and two disadvantages of using a wireless network. 8. What is the fully abbreviated form of the IPv6 address 2620:0100:e000:0000:0000:0000:0000:8001?

www.it-ebooks.info

404 Chapter 10 Networking and the Internet

9. IPv6 supports 128-bit addresses. What is the size of the host part of the address? How many hosts would that address space theoretically support on one LAN?

Advanced Exercises
10. Suppose the link between routers 1 and 2 is down in the Internet shown in Figure 10-1 on page 369. What happens if someone at site C sends a message to a user on a workstation attached to the Ethernet cable at site A? What happens if the router at site A is down? What does this tell you about designing network configurations? 11. If you have a class B IPv4 network and want to divide it into subnets, each with 126 hosts, which subnet mask should you use? How many networks will be available? What are the four addresses (broadcast and network number) for the network starting at 131.204.18? 12. Suppose you have 300 hosts and want to have no more than 50 hosts per subnet. What size of address block should you request from your ISP? How many /24 addresses would you need? How many subnets would you have left over from your allocation? 13. a. On the local system, find two daemons running that are not listed in this chapter and explain what purpose they serve. b. Review which services/daemons are automatically started on your system and consider which you might turn off. Are there any services/daemons in the list in Table 10-3 on page 395 you would consider adding?
a. PLACE HOLDER: REMOVE /lib/libwrap.so.0 (0x00e7c000)

hosts.allow and hosts.deny

Each line in the hosts.allow and hosts.deny files has the following format: daemon_list : client_list [: command] where daemon_list is a comma-separated list of one or more server daemons (e.g., rpcbind, vsftpd, sshd), client_list is a comma-separated list of one or more clients (see Table 11-5 on page 478), and the optional command is the command that is executed when a client from client_list tries to access a server daemon from daemon_list. When a client requests a connection to a server, the hosts.allow and hosts.deny files on the server system are consulted in the following order until a match is found: 1. If the daemon/client pair matches a line in hosts.allow, access is granted. 2. If the daemon/client pair matches a line in hosts.deny, access is denied. 3. If there is no match in hosts.allow or hosts.deny, access is granted. The first match determines whether the client is allowed to access the server. When either hosts.allow or hosts.deny does not exist, it is as though that file were empty. Although not recommended, you can allow access to all daemons for all clients by removing both files.

www.it-ebooks.info

Setting Up a Server 485
Examples

For a more secure system, put the following line in hosts.deny to block all access:

$ cat /etc/hosts.deny ... ALL : ALL : echo '%c tried to connect to %d and was blocked' >> /var/log/tcpwrappers.log

This line prevents any client from connecting to any service, unless specifically permitted to do so in hosts.allow. When this rule is matched, it adds a line to the file named /var/log/tcpwrappers.log. The %c expands to client information, and the %d expands to the name of the daemon the client attempted to connect to. With the preceding hosts.deny file in place, you can include lines in hosts.allow that explicitly allow access to certain services and systems. For example, the following hosts.allow file allows any client to connect to the OpenSSH daemon (ssh, scp, sftp) but allows telnet connections only from the same network as the local system and users on the 192.168. subnet:
$ cat /etc/hosts.allow sshd: ALL in.telnet: LOCAL in.telnet: 192.168.* 127.0.0.1 ...

The first line allows connection from any system (ALL) to sshd. The second line allows connection from any system in the same domain as the server (LOCAL). The third line matches any system whose IP address starts with 192.168. as well as the local system.

Setting Up a chroot Jail
On early UNIX systems, the root directory was a fixed point in the filesystem. On modern UNIX variants, including Linux, you can define the root directory on a perprocess basis. The chroot utility allows you to run a process with a root directory other than /. The root directory appears at the top of the directory hierarchy and has no parent. Thus a process cannot access files above the root directory because none exists. If, for example, you run a program (process) and specify its root directory as /home/sam/jail, the program would have no concept of any files in /home/sam or above: jail is the program’s root directory and is labeled / (not jail). By creating an artificial root directory, frequently called a (chroot) jail, you prevent a program from accessing, executing, or modifying—possibly maliciously—files outside the directory hierarchy starting at its root. You must set up a chroot jail properly to increase security: If you do not set up the chroot jail correctly, you can make it easier for a malicious user to gain access to a system than if there were no chroot jail.

Using chroot
Creating a chroot jail is simple: Working with root privileges, give the command /usr/sbin/chroot directory. The directory becomes the root directory, and the process

www.it-ebooks.info

486 Chapter 11 System Administration: Core Concepts

attempts to run the default shell. Working with root privileges, the following command sets up a chroot jail in the (existing) /home/sam/jail directory:
# /usr/sbin/chroot /home/sam/jail /usr/sbin/chroot: failed to run command '/bin/bash': No such file or directory

This example sets up a chroot jail, but when the system attempts to run the bash shell, the operation fails. Once the jail is set up, the directory that was named jail takes on the name of the root directory, /. As a consequence, chroot cannot find the file identified by the pathname /bin/bash. In this situation the chroot jail works correctly but is not useful. Getting a chroot jail to work the way you want is more complicated. To have the preceding example run bash in a chroot jail, create a bin directory in jail (/home/sam/jail/bin) and copy /bin/bash to this directory. Because the bash binary is dynamically linked to shared libraries, you need to copy these libraries into jail as well. The libraries go in lib. The next example creates the necessary directories, copies bash, uses ldd to display the shared library dependencies of bash, and copies the necessary libraries to lib. The linux-gate.so.1 file is a dynamically shared object (DSO) provided by the kernel to speed system calls; you do not need to copy it.
$ pwd /home/sam/jail $ mkdir bin lib $ cp /bin/bash bin $ ldd bin/bash linux-gate.so.1 => (0x00988000) libtinfo.so.5 => /lib/libtinfo.so.5 (0x0076b000) libdl.so.2 => /lib/libdl.so.2 (0x00afb000) libc.so.6 => /lib/libc.so.6 (0x00110000) /lib/ld-linux.so.2 (0x00923000) $ cp /lib/{libtinfo.so.5,libdl.so.2,libc.so.6,ld-linux.so.2} lib

Now start the chroot jail again. Although all the setup can be done by an ordinary user, you must be working with root privileges to run chroot:
$ su Password: #/usr/sbin/chroot . bash-4.1# pwd / bash-4.1# ls bash: ls: command not found bash-4.1#

This time chroot finds and starts bash, which displays its default prompt (bash4.1#). The pwd command works because it is a shell builtin (page 249). However, bash cannot find the ls utility because it is not in the chroot jail. You can copy /bin/ls

www.it-ebooks.info

Setting Up a Server 487

and its libraries into the jail if you want users in the jail to be able to use ls. An exit command allows you to escape from the jail. If you provide chroot with a second argument, it takes that argument as the name of the program to run inside the jail. The following command is equivalent to the preceding one:
# /usr/sbin/chroot /home/sam/jail /bin/bash

To set up a useful chroot jail, first determine which utilities the users of the chroot jail need. Then copy the appropriate binaries and their libraries into the jail. Alternatively, you can build static copies of the binaries and put them in the jail without installing separate libraries. (The statically linked binaries are considerably larger than their dynamic counterparts. The size of the base system with bash and the core utilities exceeds 50 megabytes.) You can find the source code for most common utilities in the bash and coreutils SRPMS (source rpm) packages. The chroot utility fails unless you run it with root privileges. The result of running chroot with root privileges is a root shell (a shell with root privileges) running inside a chroot jail. Because a user with root privileges can break out of a chroot jail, it is imperative that you run a program in the chroot jail with reduced privileges (i.e., privileges other than those of root). There are several ways to reduce the privileges of a user. For example, you can put su or sudo in the jail and then start a shell or a daemon inside the jail, using one of these programs to reduce the privileges of the user working in the jail. A command such as the following starts a shell with reduced privileges inside the jail:
# /usr/sbin/chroot jailpath /bin/su user -c /bin/bash

where jailpath is the pathname of the jail directory, and user is the username under whose privileges the shell runs. The problem with this scenario is that sudo and su, as compiled for Fedora/RHEL, call PAM. To run one of these utilities you need to put all of PAM, including its libraries and configuration files, in the jail, along with sudo (or su) and the /etc/passwd file. Alternatively, you can recompile su or sudo. The source code calls PAM, however, so you would need to modify the source so it does not call PAM. Either one of these techniques is time-consuming and introduces complexities that can lead to an unsecure jail. The following C program2 runs a program with reduced privileges in a chroot jail. Because this program obtains the UID and GID of the user you specify on the command line before calling chroot(), you do not need to put /etc/passwd in the jail. The program reduces the privileges of the specified program to those of the specified user. This program is presented as a simple solution to the preceding issues so you can experiment with a chroot jail and better understand how it works.
2. Thanks to David Chisnall and the Étoilé Project (etoileos.com) for the uchroot.c program.

www.it-ebooks.info

488 Chapter 11 System Administration: Core Concepts
$ cat uchroot.c /* See svn.gna.org/viewcvs/etoile/trunk/Etoile/LiveCD/uchroot.c for terms of use. #include #include #include int main(int argc, char * argv[]) { if(argc < 4) { printf("Usage: %s {username} {directory} {program} [arguments]\n", argv[0]); return 1; } /* Parse arguments */ struct passwd * pass = getpwnam(argv[1]); if(pass == NULL) { printf("Unknown user %s\n", argv[1]); return 2; } /* Set the required UID */ chdir(argv[2]); if(chroot(argv[2]) || setgid(pass->pw_gid) || setuid(pass->pw_uid)) { printf("%s must be run as root. Current uid=%d, euid=%d\n", argv[0], (int)getuid(), (int)geteuid() ); return 3; } return execv(argv[3], argv + 3); }

*/

The first of the following commands compiles uchroot.c using cc (gcc package), creating an executable file named uchroot. Subsequent commands move uchroot to /usr/local/bin and give it appropriate ownership.
$ cc -o uchroot uchroot.c $ su password: # mv uchroot /usr/local/bin # chown root:root /usr/local/bin/uchroot # exit $ ls -l /usr/local/bin/uchroot -rwxrwxr-x. 1 root root 5704 12-31 15:00 /usr/local/bin/uchroot

www.it-ebooks.info

DHCP: Configures Network Interfaces 489

Using the setup from earlier in this section, give the following command to run a shell with the privileges of the user sam inside a chroot jail:
# /usr/local/bin/uchroot sam /home/sam/jail /bin/bash

Keeping multiple chroot jails tip If you plan to deploy multiple chroot jails, it is a good idea to keep a clean copy of the bin and lib directories somewhere other than one of the active jails.

Running a Service in a chroot Jail
Running a shell inside a jail has limited usefulness. In reality, you are more likely to want to run a specific service inside the jail. To run a service inside a jail, make sure all files needed by that service are inside the jail. Using uchroot, the format of a command to start a service in a chroot jail is
# /usr/local/bin/uchroot user jailpath daemonname

where jailpath is the pathname of the jail directory, user is the username that runs the daemon, and daemonname is the pathname (inside the jail) of the daemon that provides the service. Some servers are already set up to take advantage of chroot jails. For example, you can set up DNS so that named runs in a jail (page 877), and the vsftpd FTP server can automatically start chroot jails for clients (page 717).

Security Considerations
Some services need to be run by a user or process with root privileges but release their root privileges once started (Apache, Procmail, and vsftpd are examples). If you are running such a service, you do not need to use uchroot or put su or sudo inside the jail. A process run with root privileges can potentially escape from a chroot jail. For this reason, you should reduce privileges before starting a program running inside the jail. Also, be careful about which setuid (page 205) binaries you allow inside a jail—a security hole in one of them could compromise the security of the jail. In addition, make sure the user cannot access executable files that he uploads to the jail.

DHCP: Configures Network Interfaces
Instead of storing network configuration information in local files on each system, DHCP (Dynamic Host Configuration Protocol) enables client systems to retrieve the necessary network configuration information from a DHCP server each time they connect to the network. A DHCP server assigns IP addresses from a pool of addresses to clients as needed. Assigned addresses are typically temporary but need not be.

www.it-ebooks.info

490 Chapter 11 System Administration: Core Concepts

This technique has several advantages over storing network configuration information in local files: • A new user can set up an Internet connection without having to deal with IP addresses, netmasks, DNS addresses, and other technical details. An experienced user can set up a connection more quickly. • DHCP facilitates assignment and management of IP addresses and related network information by centralizing the process on a server. A system administrator can configure new systems, including laptops that connect to the network from different locations, to use DHCP; DHCP then assigns IP addresses only when each system connects to the network. The pool of IP addresses is managed as a group on the DHCP server. • DHCP facilitates the use of IP addresses by more than one system, reducing the total number of IP addresses needed. This conservation of addresses is important because the Internet is quickly running out of IPv4 addresses. Although a particular IP address can be used by only one system at a time, many end-user systems require addresses only occasionally, when they connect to the Internet. By reusing IP addresses, DHCP has lengthened the life of the IPv4 protocol. DHCP is particularly useful for an administrator who is responsible for maintaining a large number of systems because new systems no longer need to be set up with unique configuration information.

More Information
Web

www.dhcp.org DHCP FAQ: www.dhcp-handbook.com/dhcp_faq.html DHCP client: /usr/share/doc/dhclient-* DHCP server: /usr/share/doc/dhcp-* DHCP Mini HOWTO

Local

HOWTO

How DHCP Works
Using dhclient, the client contacts the server daemon, dhcpd, to obtain the IP address, netmask, broadcast address, nameserver address, and other networking parameters. In turn, the server provides a lease on the IP address to the client. The client can request the specific terms of the lease, including its duration; the server can limit these terms. While connected to the network, a client typically requests extensions of its lease as necessary so its IP address remains the same. This lease might expire once the client is disconnected from the network, with the server giving the client a new IP address when it requests a new lease. You can also set up a DHCP server to provide static IP addresses for specific clients (refer to “Static Versus Dynamic IP Addresses” on page 378).

www.it-ebooks.info

DHCP: Configures Network Interfaces 491

When you install Fedora/RHEL, the system runs a DHCP client, connects to a DHCP server if it can find one, and configures its network interface.

DHCP Client
A DHCP client requests network configuration parameters from the DHCP server and uses those parameters to configure its network interface.

Prerequisites
Make sure the following package is installed: • dhclient

dhclient: The DHCP Client
When a DHCP client system connects to the network, dhclient requests a lease from the DHCP server and configures the client’s network interface(s). Once a DHCP client has requested and established a lease, it stores the lease information in a file named dhclient-*-interface.lease, which resides in the /var/lib/dhclient directory. The interface is the name of the interface the client uses, such as eth0. The system uses this information to reestablish a lease when either the server or the client needs to reboot. The DHCP client configuration file, /etc/dhcp/dhclient.conf, is required only for custom configurations. The following dhclient.conf file specifies a single interface, eth0:
$ cat /etc/dhcp3/dhclient.conf interface "eth0" { send dhcp-client-identifier 1:xx:xx:xx:xx:xx:xx; send dhcp-lease-time 86400; }

In the preceding file, the 1 in the dhcp-client-identifier specifies an Ethernet network and xx:xx:xx:xx:xx:xx is the MAC address (page 1174) of the device controlling that interface. See page 493 for instructions on how to determine the MAC address of a device. The dhcp-lease-time is the duration, in seconds, of the lease on the IP address. While the client is connected to the network, dhclient automatically renews the lease each time half of the lease time is up. A lease time of 86,400 seconds (one day) is a reasonable choice for a workstation.

DHCP Server
A DHCP server maintains a list of IP addresses and other configuration parameters. Clients request network configuration parameters from the server.

Prerequisites
Install the following package: • dhcp

www.it-ebooks.info

492 Chapter 11 System Administration: Core Concepts dhcpd init script

Run chkconfig to cause dhcpd to start when the system enters multiuser mode:
# chkconfig dhcpd on

After configuring the DHCP server, start or restart dhcpd:
# service dhcpd start

dhcpd: The DHCP Daemon
A simple DCHP server (dhcpd) allows you to add clients to a network without maintaining a list of assigned IP addresses. A simple network, such as a home-based LAN sharing an Internet connection, can use DHCP to assign a dynamic IP address to almost all nodes. The exceptions are servers and routers, which must be at known network locations if clients are to find them. If servers and routers are configured without DHCP, you can specify a simple DHCP server configuration in /etc/dhcp/dhcpd.conf:
$ cat /etc/dhcp/dhcpd.conf default-lease-time 600; max-lease-time 86400; option option option option option subnet-mask 255.255.255.0; broadcast-address 192.168.1.255; routers 192.168.1.1; domain-name-servers 192.168.1.1; domain-name "example.com";

subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.2 192.168.1.200; }

By default, dhcpd serves requests on all nonbroadcast network interfaces. The preceding configuration file specifies a LAN where both the router and the DNS server are located on 192.168.1.1. The default-lease-time specifies the number of seconds the dynamic IP lease will remain valid if the client does not specify a duration. The max-lease-time is the maximum time allowed for a lease. The information in the option lines is sent to each client when it connects. The names following the word option specify what the following argument represents. For example, the option broadcast-address line specifies the broadcast address of the network. The routers and domain-name-servers options can be followed by multiple values separated by commas. The subnet section includes a range line that specifies the range of IP addresses the DHCP server can assign. In the case of multiple subnets, you can define options, such as subnet-mask, inside the subnet section. Options defined outside all subnet sections are global and apply to all subnets. The preceding configuration file assigns addresses in the range from 192.168.1.2 to 192.168.1.200. The DHCP server starts at the bottom of this range and attempts to assign a new IP address to each new client. Once the DHCP server reaches the top

www.it-ebooks.info

DHCP: Configures Network Interfaces 493

of the range, it starts reassigning IP addresses that have been used in the past but are not currently in use. If you have fewer systems than IP addresses, the IP address of each system should remain fairly constant. Two systems cannot use the same IP address at the same time. Once you have configured a DHCP server, restart it using the dhcpd init script (page 492). When the server is running, clients configured to obtain an IP address from the server using DHCP should be able to do so. See the /usr/share/doc/dhcp*/*sample files for sample dhcpd.conf files.

Static IP Addresses
As mentioned earlier, routers and servers typically require static IP addresses. Although you can manually configure IP addresses for these systems, it might be more convenient to have the DHCP server provide them with static IP addresses. See page 652 if you want to configure a system to use a static IP address without using DHCP. When a system that requires a specific static IP address connects to the network and contacts the DHCP server, the server needs a way to identify the system so it can assign the proper IP address to that system. The DHCP server uses the MAC address (page 1174) of the system’s network interface card (NIC) as an identifier. When you set up the server, you must know the MAC address of each system that requires a static IP address.
Determining a MAC address

The ip utility displays the MAC addresses of the Ethernet cards in a system. In the following example, the MAC address is the colon-separated series of hexadecimal number pairs following link/ether:

$ ip link show eth1 2: eth1: mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000 link/ether 00:0c:29:12:35:6e brd ff:ff:ff:ff:ff:ff

Run ip on each system that requires a static IP address. Once you have determined the MAC addresses of these systems, you can add a host section to the /etc/dhcp/ dhcpd.conf file for each one, instructing the DHCP server to assign a specific address to that system. The following host section assigns the address 192.168.1.1 to the system with the MAC address of BA:DF:00:DF:C0:FF:
$ cat /etc/dhcp/dhcpd.conf ... host router { hardware ethernet BA:DF:00:DF:C0:FF; fixed-address 192.168.1.1; option host-name router; }

The name following host is used internally by dhcpd. The name specified after option host-name is passed to the client and can be a hostname or an FQDN. After making changes to dhcpd.conf, restart dhcpd using the dhcpd init script (page 492).

www.it-ebooks.info

494 Chapter 11 System Administration: Core Concepts

nsswitch.conf: Which Service to Look at First
Once NIS and DNS were introduced, finding user and system information was no longer a simple matter of searching a local file. When once you looked in /etc/passwd to get user information and in /etc/hosts to find system address information, now you can use several methods to obtain this type of information. The /etc/nsswitch.conf (name service switch configuration) file specifies which methods to use and the order in which to use them when looking for a certain type of information. You can also specify which action the system should take based on whether a method succeeds or fails.
Syntax

Each line in nsswitch.conf specifies how to search for a piece of information, such as a user’s password. A line in nsswitch.conf has the following syntax: info: method [[action]] [method [[action]]...]

where info is the type of information the line describes, method is the method used to find the information, and action is the response to the return status of the preceding method. The action is enclosed within square brackets. When called upon to supply information that nsswitch.conf describes, the system examines the line with the appropriate info field. It uses the methods specified on this line, starting with the method on the left. By default, when it finds the desired information, the system stops searching. Without an action specification, when a method fails to return a result, the system tries the next action. It is possible for the search to end without finding the requested information.

Information
The nsswitch.conf file commonly controls searches for usernames, passwords, host IP addresses, and group information. The following list describes most of the types of information (info in the syntax given earlier) that nsswitch.conf controls searches for: automount bootparam ethers group hosts networks passwd protocols publickey rpc services shadow Automount (/etc/auto.master and /etc/auto.misc; page 811) Diskless and other booting options (bootparam man page) MAC address (page 1174) Groups of users (/etc/group; page 506) System information (/etc/hosts; page 507) Network information (/etc/networks) User information (/etc/passwd; page 508) Protocol information (/etc/protocols; page 510) Used for NFS running in secure mode RPC names and numbers (/etc/rpc; page 511) Services information (/etc/services; page 511) Shadow password information (/etc/shadow; page 511)

www.it-ebooks.info

nsswitch.conf: Which Service to Look at First 495

Methods
Following is a list of the types of information that nsswitch.conf controls searches for (method in the syntax shown on the previous page). For each type of information, you can specify one or more of the following methods:3 compat dns files ldap nis ± syntax in passwd, group, and shadow files (page 496) Queries the DNS (hosts queries only) Searches local files such as /etc/passwd and /etc/hosts Queries an LDAP server (page 776) Searches the NIS database; yp is an alias for nis

Search Order
The information provided by two or more methods might overlap: For example, both files and nis might provide password information for the same user. With overlapping information, you need to consider which method you want to be authoritative (take precedence) and then place that method at the left of the list of methods. The default nsswitch.conf file lists methods without actions, assuming no overlap (which is normal). In this case, the order is not critical: When one method fails, the system goes to the next one and all that is lost is a little time. Order becomes critical when you use actions between methods or when overlapping entries differ. The first of the following lines from nsswitch.conf causes the system to search for password information in /etc/passwd and if that fails, to use NIS to find the information. If the user you are looking for is listed in both places, the information in the local file is used and is considered authoritative. The second line uses NIS to find an IP address given a hostname; if that fails, it searches /etc/hosts; if that fails, it checks with DNS to find the information. passwd hosts files nis nis files dns

Action Items
Each method can optionally be followed by an action item that specifies what to do if the method succeeds or fails. An action item has the following format: [[!]STATUS=action] where the opening and closing square brackets are part of the format and do not indicate that the contents are optional; STATUS (uppercase by convention) is the status being tested for; and action is the action to be taken if STATUS matches the status returned by the preceding method. The leading exclamation point (!) is optional and negates the status.
3. Other, less commonly used methods also exist. See the default /etc/nsswitch.conf file and the nsswitch.conf man page for more information. Although NIS+ belongs in this list, it is not implemented as a Linux server and is not discussed in this book.

www.it-ebooks.info

496 Chapter 11 System Administration: Core Concepts
STATUS

STATUS might have any of the following values: NOTFOUND—The method worked, but the value being searched for was not found. The default action is continue. SUCCESS—The method worked, and the value being searched for was found; no error was returned. The default action is return. TRYAGAIN—The method failed because it was temporarily unavailable. For example, a file might be locked or a server overloaded. The default action is continue. UNAVAIL—The method failed because it is permanently unavailable. For example, the required file might not be accessible or the required server might be down. The default action is continue.

action

There are two possible values for action: return—Returns to the calling routine with or without a value. continue—Continues with the next method. Any returned value is overwritten by a value found by a subsequent method.

Example

The following line from nsswitch.conf causes the system first to use DNS to search for the IP address of a given host. The action item following the DNS method tests whether the status returned by the method is not (!) UNAVAIL. hosts dns [!UNAVAIL=return] files

The system takes the action associated with the STATUS (return) if the DNS method does not return UNAVAIL (!UNAVAIL)—that is, if DNS returns SUCCESS, NOTFOUND, or TRYAGAIN. As a consequence, the following method (files) is used only when the DNS server is unavailable. If the DNS server is not unavailable (read the two negatives as “is available”), the search returns the domain name or reports that the domain name was not found. The search uses the files method (checks the local /etc/hosts file) only if the server is not available.

compat Method: ± in passwd, group, and shadow Files
You can put special codes in the /etc/passwd, /etc/group, and /etc/shadow files that cause the system, when you specify the compat method in nsswitch.conf, to combine and modify entries in the local files and the NIS maps. That is, a plus sign (+) at the beginning of a line in one of these files adds NIS information; a minus sign (–) removes information. For example, to use these codes in the passwd file, specify passwd: compat in the nsswitch.conf file. The system then goes through the passwd file in order, adding or removing the appropriate NIS entries when it reaches each line that starts with a + or –. Although you can put a plus sign at the end of the passwd file, specify passwd: compat in nsswitch.conf to search the local passwd file, and then go through the NIS map, it is more efficient to put passwd: file nis in nsswitch.conf and not modify the passwd file.

www.it-ebooks.info

Chapter Summary 497

Getting Help
Fedora/RHEL comes with extensive documentation (page 125). Fedora maintains a page that points to many useful support documents at docs.fedoraproject.org; fedoraproject.org/en/get-help shows how to get to the fedora forums, email lists, and IRC chatrooms; Red Hat maintains a similar page at access.redhat.com. and keeps documentation at access.redhat.com/docs. Although some sections of these Red Hat sites require an account to access, anyone can get an account for free. You can also find help on the System Administrators Guild site (www.sage.org). The Internet is another rich source of information on managing a Linux system; refer to Appendix B (page 1115) and to the author’s home page (www.sobell.com) for pointers to useful sites. You need not act as a Fedora/RHEL system administrator in isolation; a large community of Fedora/RHEL experts is willing to assist you in getting the most out of a Linux system. Of course, you will get better help if you have already tried to solve a problem yourself by reading the available documentation. If you are unable to solve a problem by consulting the documentation, a well-thought-out question posed to the appropriate newsgroup, such as comp.os.linux.misc, or mailing list can often generate useful information. Be sure to describe the problem accurately and identify the system carefully. Include information about the version of Fedora/RHEL running on the system and any software packages and hardware you think relate to the problem. The newsgroup comp.os.linux.answers contains postings of solutions to common problems and periodic postings of the most up-to-date versions of FAQs and HOWTO documents. See www.catb.org/~esr/faqs/smart-questions.html for a helpful paper by Eric S. Raymond and Rick Moen titled “How to Ask Questions the Smart Way.”

Chapter Summary
A system administrator is someone who keeps the system in a useful and convenient state for its users. Much of the work you do as the system administrator will require you to work with root privileges. A user with these privileges (sometimes referred to as Superuser) has extensive systemwide powers that normal users do not have. A user with root privileges can read from and write to any file and can execute programs that ordinary users are not permitted to execute. The system administrator controls system operation, which includes the following tasks: configuring the system; booting up; running init scripts; setting up servers; working in single-user, multiuser, and rescue modes; bringing the system down; and handling system crashes. Fedora/RHEL provides both graphical and textual configuration tools. Many of the graphical tools are named system-config-*.

www.it-ebooks.info

498 Chapter 11 System Administration: Core Concepts

When you bring up the system in single-user mode, only the system console is functional. While working in single-user mode, you can back up files and use fsck to check the integrity of filesystems before you mount them. The telinit utility can bring the system to its default multiuser state. With the system running in multiuser mode, you can still perform many administration tasks, such as adding users and printers. When you install a Fedora/RHEL system, you specify a password for the root account. You use this password to gain root privileges, either by logging in as the user named root or by using su and providing the root password. Alternatively, you can configure sudo which grants root privileges based on your password. A system that does not have a root password and that relies on sudo to escalate permissions can be more secure than one with a root password. The Upstart init daemon, which replaces the traditional System V init daemon (SysVinit), is event based: It can start and stop services upon receiving information that something on the system has changed (an event). Events include adding devices to and removing them from the system as well as bringing the system up and shutting it down. The xinetd superserver starts server daemons as needed and can help secure a system by controlling who can use which services. You can use TCP wrappers to control who can use which system services by editing the hosts.allow and hosts.deny files in the /etc directory. Setting up a chroot jail limits the portion of the filesystem a user sees, so it can help control the damage a malicious user can do. You can set up a DHCP server so you do not have to configure each system on a network manually. DHCP can provide both static and dynamic IP addresses. Whether a system uses NIS, DNS, local files, or a combination (and in what order) as a source of information is determined by /etc/nsswitch.conf. Linux-PAM enables you to maintain fine-grained control over who can access the system, how they can access it, and what they can do.

Exercises
1. How does single-user mode differ from multiuser mode? 2. How would you communicate each of the following messages? a. The system is coming down tomorrow at 6:00 in the evening for periodic maintenance. b. The system is coming down in five minutes. c. Zach’s jobs are slowing the system down drastically, and he should postpone them. d. Zach’s wife just had a baby girl.

www.it-ebooks.info

Advanced Exercises 499

3. What do the letters of the su command stand for? (Hint: It is not Superuser.) What can you do with su besides give yourself root privileges? How would you log in as Zach if you did not know his password but knew the root password? How would you establish the same environment that Zach has when he first logs in? 4. How would you allow a user to execute a specific, privileged command without giving the user the root password? 5. How do you kill process 1648? How do you kill all processes running kmail? In which instances do you need to work with root privileges? 6. How can you disable SELinux? 7. Develop a strategy for coming up with a password that an intruder would not be likely to guess but that you will be able to remember.

Advanced Exercises
8. Give the command
$ /bin/fuser -uv /

What does the output list? Why is it so long? Give the same command while working with root privileges (or ask the system administrator to do so and email you the results). How does this list differ from the first? Why is it different? 9. When it puts files in a lost+found directory, fsck has lost the directory information for the files and thus has lost the names of the files. Each file is given a new name, which is the same as the inode number for the file:
$ ls –l lost+found –rw–r––r–– 1 max pubs 110 2010-06-10 10:55 51262

How can you identify these files and restore them? 10. Take a look at /usr/bin/lesspipe.sh. Explain its purpose and describe six ways it works. 11. Why are setuid shell scripts inherently unsafe? 12. When a user logs in, you would like the system to first check the local /etc/passwd file for a username and then check NIS. How do you implement this strategy? 13. Some older kernels contain a vulnerability that allows a local user to gain root privileges. Explain how this kind of vulnerability negates the value of a chroot jail. New Exercises

www.it-ebooks.info

This page intentionally left blank

www.it-ebooks.info

12
Files, Directories, and Filesystems

Chapter12 12

In This Chapter
Important Files and Directories . . 502 Ordinary Files, Directories, Links, and Inodes . . . . . . . . . . . . . . . . . 515 Device Special Files . . . . . . . . . . . 515 Filesystems . . . . . . . . . . . . . . . . . . 519 mount: Mounts a Filesystem . . . . 520 fstab: Keeps Track of Filesystems 524 fsck: Checks Filesystem Integrity 525

Objectives
After reading this chapter you should be able to: List four important directories and describe their uses List common system startup configuration files and describe their uses List typical network configuration files and describe their uses List common login configuration files and describe their uses List a few device filenames and describe their uses Define the seven types of files found on the system Describe several filesystem types found on a Linux system View, check, tune, mount, and unmount local filesystems
501

www.it-ebooks.info

502 Chapter 12 Files, Directories, and Filesystems

Filesystems hold directories of files. These structures store user data and system data that are the basis of users’ work on the system and the system’s existence. This chapter discusses important files and directories, various types of files and ways to work with them, and the use and maintenance of filesystems.

Important Files and Directories
This section details the files most commonly used to administer the system. For a more general list of files, refer to “Important Standard Directories and Files” on page 199. lost+found Holds preallocated disk blocks of directories that fsck uses to store pointers to

unlinked files (files that have lost their directory [and therefore filename] information). Having these blocks available ensures that fsck does not have to allocate data blocks during recovery, a process that could further damage a corrupted filesystem. See page 525 for more information on fsck. However, fsck will allocate blocks if they do not exist. Each ext2, ext3, and ext4 filesystem contains a lost+found directory in the filesystem’s root directory. If, for example, a filesystem is mounted at /home, there will be a /home/lost+found directory. There is always a /lost+found directory. These directories are normally created by mkfs when it writes an ext2/ext3/ext4 filesystem to a partition. Although rarely necessary, you can create a lost+found directory manually using mklost+found.
~/.bash_profile Contains an individual user’s login shell (page 1174) initialization script. The shell

executes the commands in this file in the same environment as the shell each time a user logs in. (For information about executing a shell script in this manner, refer to the discussion of the . [dot] command on page 284.) The file must be located in a user’s home directory. It is not run from terminal emulator windows because you do not log in in those windows. The default Fedora/RHEL .bash_profile file executes the commands in ~/.bashrc. You can use .bash_profile to specify the editor you want to use, run stty to establish the terminal characteristics, set up aliases, and perform other housekeeping functions when a user logs in. A simple .bash_profile file that sets EDITOR to vi and CONTROL-H as the erase key follows:
$ cat .bash_profile export EDITOR=$(which vi) stty erase '^h'

Using command substitution and which vi sets EDITOR to the absolute pathname of the vi editor. For more information refer to “Startup Files” on page 282.
~/.bashrc Contains an individual user’s interactive, nonlogin shell initialization script. The

shell executes the commands in this file in the same environment as the (new) shell

www.it-ebooks.info

Important Files and Directories 503

each time a user creates a new interactive shell, including when a user opens a terminal emulator window. (For information about executing a shell script in this manner, refer to the discussion of the . [dot] command on page 284.) The .bashrc script differs from .bash_profile in that it is executed each time a new shell is spawned, not just when a user logs in. The default Fedora/RHEL .bash_profile file executes the commands in ~/.bashrc so that these commands are executed when a user logs in. For more information refer to “Startup Files” on page 282.
/dev Contains files representing pseudodevices and physical devices that might be

attached to the system. The /dev/sda device was traditionally the first SCSI disk; now it is the first drive, including IDE, SATA, and USB drives. Other similar drives are named /dev/sdb, /dev/sdc, etc. These names, such as /dev/sda, represent the order of the devices on the bus the devices are connected to, not the devices themselves. For example, if you swap the data cables on the disks referred to as /dev/sda and /dev/sdb, the drive’s designations will change. Similarly, if you remove the device referred to as /dev/sda, the device that was referred to as /dev/sdb will now be referred to as /dev/sda. See /dev/disk/by-uuid (below) for a way to refer to specific devices and not to their order on the bus.
/dev/disk/by-path

Holds symbolic links to local devices. The names of the devices in this directory identify the devices. Each entry points to the device in /dev that it refers to (sr0 is the CD/DVD drive).
$ ls -l /dev/disk/by-path lrwxrwxrwx. 1 root root 9 lrwxrwxrwx. 1 root root 9 lrwxrwxrwx. 1 root root 10 lrwxrwxrwx. 1 root root 10 01-25 01-25 01-25 01-25 04:17 04:17 04:17 04:17 pci-0000:00:07.1-scsi-1:0:0:0 -> ../../sr0 pci-0000:00:10.0-scsi-0:0:0:0 -> ../../sda pci-0000:00:10.0-scsi-0:0:0:0-part1 -> ../../sda1 pci-0000:00:10.0-scsi-0:0:0:0-part2 -> ../../sda2

/dev/disk/by-uuid

Holds symbolic links to local devices. The names of the devices in this directory consist of the UUID (page 1196) numbers of the devices. Each entry points to the device in /dev that it refers to. See page 524 for more information. The dm in the partition names stands for device mapper, which is used by LVM (page 42).
$ ls -l /dev/disk/by-uuid lrwxrwxrwx. 1 root root 10 01-25 04:17 7694f86e-c3ff-4e51-b677-01b3d27927a4 -> ../../sda1 lrwxrwxrwx. 1 root root 10 01-25 12:17 7743cb0d-b961-4b65-b3a2-071536734d8a -> ../../dm-0 lrwxrwxrwx. 1 root root 10 01-25 12:17 800b87ec-b333-4d51-90e5-be359421e999 -> ../../dm-1

/dev/null Also called a bit bucket. Output sent to this file disappears. The /dev/null file is a

device file. Input you redirect to come from this file appears as null values, creating an empty file. You can create an empty file named nothing by giving one of the following commands:
$ cat /dev/null > nothing $ cp /dev/null nothing

or, without explicitly using /dev/null,
$ > nothing

www.it-ebooks.info

504 Chapter 12 Files, Directories, and Filesystems

The last command redirects the output of a null command to the file with the same result as the previous commands. You can use any of these commands to truncate an existing file to zero length without changing its permissions. You can also use /dev/null to get rid of output you do not want:
$ grep portable

* 2> /dev/null

This command displays all lines in all files in the working directory that contain the string portable. Any output to standard error (page 285), such as a permission or directory error, is discarded, while output to standard output appears on the screen. See also page 239.
/dev/pts A hook into the Linux kernel. This pseudofilesystem is part of the pseudoterminal support. Pseudoterminals are used by remote login programs, such as ssh and telnet, as well as xterm and other graphical terminal emulators. The following sequence of commands demonstrates that Sam is logged in on /dev/pts/1. After using tty to verify the pseudoterminal he is logged in on and using ls to show that this pseudoterminal exists, Sam redirects the output of an echo command to /dev/pts/1, whereupon the

output appears on his screen:
$ tty /dev/pts/1 $ ls /dev/pts 0 1 2 ptmx $ echo Hi there > /dev/pts/1 Hi there

/dev/random Interfaces to the kernel’s random number generator. You can use either file with and dd to create a file filled with pseudorandom bytes. However, if there is not /dev/urandom enough entropy, urandom can generate potentially inferior results (although for

most purposes the difference is irrelevant). Following is an example:
$ dd if=/dev/urandom of=randfile bs=1 count=100 100+0 records in 100+0 records out 100 bytes (100 B) copied, 0.000884387 seconds, 113 kB/s

The preceding command reads from /dev/urandom and writes to the file named randfile. The block size is 1, and the count is 100; thus randfile is 100 bytes long. For bytes that are more random, you can read from /dev/random. See the urandom and random man pages for more information.

Using random can cause the system to hang tip Reading from /dev/random can cause the system to hang for a long time if there is not enough entropy. This problem is common on virtual systems. You can create entropy and free the system by moving the mouse, typing on the keyboard, or copying files. Alternatively, you can read from /dev/urandom.

www.it-ebooks.info

Important Files and Directories 505

optional
Wiping a file

You can use a similar technique to wipe data from a file before deleting it, making it almost impossible to recover data from the deleted file. You might want to wipe a file for security reasons. In the following example, ls shows the size of the file named secret. Using a block size of 1 and a count corresponding to the number of bytes in secret, dd wipes the file. The conv=notrunc argument ensures that dd writes over the data in the file and not another (erroneous) place on the disk.
$ ls -l secret -rw-r--r--. 1 sam pubs 5733 01-27 13:12 secret $ dd if=/dev/urandom of=secret bs=1 count=5733 conv=notrunc 5733+0 records in 5733+0 records out 5733 bytes (5.7 kB) copied, 0.0358146 seconds, 160 kB/s $ rm secret

For added security, run sync to flush the disk buffers after running dd and repeat the two commands several times before deleting the file. See wipe.sourceforge.net for more information about wiping files.
/dev/shm Implements shared memory, which provides an efficient way for processes to share

information.
/dev/zero Input you take from this file contains an infinite string of zeros (numerical zeros,

not ASCII zeros). You can fill a file (e.g., a swap file; page 513) or overwrite a file with zeros with a command such as the following:
$ dd if=/dev/zero of=zeros bs=1024 count=10 10+0 records in 10+0 records out 10240 bytes (10 kB) copied, 0.000160263 seconds, 63.9 MB/s $ od -c zeros 0000000 \0 \0

\0

\0

\0

\0

\0

\0

\0

\0

\0

\0

\0

\0

\0

\0

*
0024000

The od utility shows the contents of the new file. If you try to fill a file with zeros using /dev/null, you fill the partition in which you are working:
$ cp /dev/zero bigzero cp: writing 'bigzero': No space left on device $ rm bigzero

/etc/aliases Used by the mail delivery system to hold aliases for users. Edit this file to suit local

needs. For more information refer to /etc/aliases on page 736.

www.it-ebooks.info

506 Chapter 12 Files, Directories, and Filesystems
/etc/alternatives Holds symbolic links so you can call a utility by a name other than that of the file

that holds the utility. For example, when you give the command cancel, the shell calls cancel.cups using the following links:
$ ls -l /usr/bin/cancel lrwxrwxrwx. 1 root root 30 01-25 12:14 /usr/bin/cancel -> /etc/alternatives/print-cancel $ ls -l /etc/alternatives/print-cancel lrwxrwxrwx. 1 root root 20 01-25 12:14 /etc/alternatives/print-cancel -> /usr/bin/cancel.cups

The alternatives directory also allows a utility to appear in more than one directory and can ease an administrator’s learning curve by providing familiar commands to do similar tasks (e.g., mailq can call sendmail or postfix commands depending on which server is being used). In addition, this directory allows you to call one utility by several names. Although the alternatives directory does not allow developers to do anything they could not do without it, it provides an orderly way to keep and update these links. Use whereis (page 165) to find all links to a utility.
/etc/at.allow, /etc/at.deny, /etc/cron.allow, and /etc/cron.deny

By default, users can use the at and crontab utilities. The at.allow and cron.allow files list the users who are allowed to use at and crontab, respectively. The at.deny and cron.deny files specify users who are not permitted to use the corresponding utilities. As Fedora/RHEL is configured, an empty at.deny file and the absence of an at.allow file allows anyone to use at; the absence of cron.allow and an empty cron.deny file allows anyone to use crontab. To prevent anyone except a user running with root privileges from using at, remove the at.deny file. To prevent anyone except a user running with root privileges from using crontab, create a cron.allow file with the single entry root. For more information on crontab, refer to “Scheduling Tasks” on page 611.

/etc/default A directory that holds files that set default values for system services and utilities such as NFS and useradd. Look at the files in this directory for more information. /etc/fstab filesystem (mount) table—Contains a list of mountable devices as specified by the

system administrator. See page 524 for more information.
/etc/group Groups allow users to share files or programs without giving all system users access

to those files or programs. This scheme is useful when several users are working with files that are not public. The /etc/group file associates one or more usernames with each group (number). Refer to “ACLs: Access Control Lists” on page 208 for a finer-grained way to control file access. Each entry in the /etc/group file has four colon-separated fields that describe one group: group-name:password:group-ID:login-name-list The group-name is the name of the group. The password is an optional hashed (page 1167) password. This field frequently contains an x, indicating that group passwords are not used. The group-ID is a number, with 1–499 reserved for system accounts. The login-name-list is a comma-separated list of users who belong to the group. If an entry is too long to fit on one line, end the line with a backslash (\), which quotes the following RETURN, and continue the entry on the next line. A sample

www.it-ebooks.info

Important Files and Directories 507

entry from a group file follows. The group is named pubs, has no password, and has a group ID of 1103: pubs:x:1103:max,sam,zach,mark You can use the groups utility to display the groups to which a user belongs:
$ groups sam sam : sam pubs

Each user has a primary group, which is the group that user is assigned to in the /etc/passwd file. By default, Fedora/RHEL has user private groups: Each user’s primary group has the same name as the user. In addition, a user can belong to other groups, depending on which login-name-lists the user appears on in the /etc/group file. In effect, you simultaneously belong both to your primary group and to any groups you are assigned to in /etc/group. When you attempt to access a file you do not own, Linux checks whether you are a member of the group that has access to the file. If you are, you are subject to the group access permissions for the file. If you are not a member of the group that has access to the file and you do not own the file, you are subject to the public access permissions for the file. When you create a file, Linux assigns it to your primary group. Refer to page 604 for information on using system-config-users to work with groups.
/etc/hosts Stores the names, IP addresses, and optionally aliases of other systems. Typically,

this file holds the hostname and IP address of the local system. It also holds a special entry for localhost. This entry supports the loopback service, which allows the local system to talk to itself (e.g., for RPC services). The IP address of the loopback service is always 127.0.0.1, while 127.0.1.1 names the local system. Following is a simple /etc/hosts file:
$ cat /etc/hosts 127.0.0.1 localhost 127.0.1.1 tiny 192.168.0.9 jam 192.168.0.10 plum 192.168.0.12 dog ...

If you are not using NIS or DNS to look up hostnames (called hostname resolution), you must include in /etc/hosts all systems that the local system should be able to contact by hostname. (A system can always contact another system by using the IP address of the system.) The hosts entry in the /etc/nsswitch.conf file (page 494) controls the order in which hostname resolution services are checked.
/etc/init Holds Upstart job definition files. See page 440 for more information. /etc/init.d A symbolic link to /etc/rc.d/init.d (page 442). /etc/inittab initialization table—Fedora ignores this file. RHEL can use the initdefault entry

in this file to determine which runlevel it boots to. For more information see “rc-default task and inittab” on page 442.

www.it-ebooks.info

508 Chapter 12 Files, Directories, and Filesystems
/etc/motd Contains the message of the day, which can be displayed each time someone logs in

using a textual login. This file typically contains site policy and legal information. Keep this file short because users tend to see the message many times.
/etc/mtab Under Fedora this file is a symbolic link to the kernel mount table at /proc/mounts. Under RHEL it is an ordinary text file. When you call mount without any arguments,

it consults this file and displays a list of mounted devices. Each time you (or an init script) call mount or umount, these utilities make the necessary changes to mtab. Although this is an ASCII text file, you should not edit it. See also /etc/fstab.

Fixing mtab (RHEL) tip The kernel maintains its own internal mount table. You can display this table with the command cat /proc/mounts. Under RHEL, the list of files in /etc/mtab might not be synchronized with the partitions in this table. To bring the mtab file in line with the operating system’s mount table, you can either reboot the system or replace /etc/mtab with a symbolic link to /proc/mounts (although some information might be lost).
# rm /etc/mtab # ln -s /proc/mounts /etc/mtab

/etc/nsswitch.conf

Specifies whether a system uses NIS, DNS, local files, or a combination as the source of certain information and in which order it consults these services (page 494).
/etc/pam.d Files in this directory specify the authentication methods used by PAM (page 463)

applications.
/etc/passwd Describes users to the system. Do not edit this file directly; instead, use one of the

utilities discussed in “Configuring User and Group Accounts” on page 602. Each line in passwd has seven colon-separated fields that describe one user: login-name:password:user-ID:group-ID:info:directory:program The login-name is the user’s username—the name you enter in response to the login: prompt or on a GUI login screen. The value of the password is the character x. The /etc/shadow file (page 511) stores the real password, which is hashed (page 1167). For security reasons, every account should have a password. By convention, disabled accounts have an asterisk (*) in this field. The user-ID is a number, with 0 indicating the root account and 1–499 being reserved for system accounts. The group-ID identifies the user’s primary group. It is a number, with 0–499 being reserved for system accounts; see /etc/group (page 506). You can change these values and set maximum values in /etc/login.defs. The info is information that various programs, such as accounting and email programs, use to identify the user further. Normally it contains at least the first and last names of the user. It is referred to as the GECOS (page 1166) field. The directory is the absolute pathname of the user’s home directory. The program is the program that runs once the user logs in to a textual session. If program is not

www.it-ebooks.info

Important Files and Directories 509

present, a value of /bin/bash is assumed. You can put /bin/tcsh here to log in using the TC Shell or /bin/zsh to log in using the Z Shell, assuming the shell you specify is installed. The chsh utility (page 469) changes this value. The program is usually a shell, but it can be any program. The following line in the passwd file creates a “user” whose only purpose is to execute the who utility: who:x:1000:1000:execute who:/usr:/usr/bin/who

Logging in with who as a username causes the system to log you in, execute the who utility, and log you out. The output of who flashes by quickly because the new login prompt clears the screen immediately after who finishes running. This entry in the passwd file does not provide a shell, so you cannot stay logged in after who finishes executing. This technique is useful for providing special accounts that might do only one thing. The ftp account, for example, enables anonymous FTP (page 701) access to an FTP server. Because no one logs in on this account, the shell is set to /bin/false (which returns a false exit status) or to /sbin/nologin (which does not permit a nonprivileged user to log in). When you put a message in /etc/nologin.txt, nologin displays that message (except it has the same problem as the output of who—it is removed so quickly it is hard to see).

Do not replace a login shell with a shell script security Do not use shell scripts as replacements for shells in /etc/passwd. A user might be able to interrupt a shell script, giving him full shell access when you did not intend to do so. When installing a dummy shell, use a compiled program, not a shell script.
/etc/printcap The printer capability database for LPD/LPR (page 560). It is not used with CUPS

(Chapter 14), the Fedora/RHEL default printing system. This file describes system printers and is derived from 4.3BSD UNIX.
/etc/profile and Contain systemwide interactive shell initialization scripts for environment and startup /etc/profile.d programs. When you log in, the shell immediately executes the commands in

/etc/profile. This script executes all the commands in all the files in the /etc/profile.d directory (next) that have a filename extension of .sh. Both profile and the files in profile.d are executed in the same environment as the shell. (For information on executing a shell script in this manner, refer to the discussion of the . [dot] command on page 284.) As the comments at the beginning of profile say, if you want to make a systemwide change to users’ environments, place a script with a filename extension of .sh in profile.d. Do not modify profile because it might be replaced when the system is updated; a file you create in profile.d will not be replaced. The system administrator can create a file with a filename extension of .sh in profile.d to establish systemwide environment parameters that individual users can override in their ~/.bash_profile (page 502) files. For example, this file can set shell variables, execute utilities, set up aliases, and take care of other housekeeping tasks. Following is an example of a file in /etc/profile.d that displays the message of the day (the

www.it-ebooks.info

510 Chapter 12 Files, Directories, and Filesystems

/etc/motd file), sets the file-creation mask (umask; page 473), and sets the interrupt character to CONTROL-C:
# cat /etc/profile.d/simple.sh cat /etc/motd umask 022 stty intr '^c'

See the /etc/profile file and the files in /etc/profile.d on the local system for more complex examples.
/etc/protocols Provides protocol numbers, aliases, and brief definitions for DARPA Internet

TCP/IP protocols. Do not modify this file.
/etc/rc.d Holds SysVinit initialization scripts and links. See page 442 for more information. /etc/resolv.conf The resolver (page 848) configuration file; provides access to DNS. This file is built

by NetworkManager (page 651) if it is running. The following example shows the resolv.conf file for the example.com domain. A resolv.conf file usually contains at least two lines—a search line (optional) and a nameserver line:
$ cat /etc/resolv.conf search example.com nameserver 10.0.0.50 nameserver 10.0.0.51

The search keyword might be followed by a maximum of six domain names. The first domain is interpreted as the host’s local domain. These names are appended one at a time to all DNS queries, shortening the time needed to query local hosts. The domains are searched in order in the process of resolving hostnames that are not fully qualified. See FQDN on page 1165. When you put search example.com in resolv.conf, any reference to a host within the example.com domain or a subdomain (such as marketing.example.com) can use the abbreviated form of the host. For example, instead of issuing the command ping speedy.marketing.example.com, you can use ping speedy.marketing; however, a trailing period causes DNS to assume the name is complete so it appends no suffix. The following line in resolv.conf causes the marketing subdomain to be searched first, followed by sales, and finally the entire example.com domain: search marketing.example.com sales.example.com example.com

It is a good idea to put the most frequently used domain names first to try to outguess possible conflicts. If both speedy.marketing.example.com and speedy.example.com exist, for example, the order of the search determines which one is selected when you invoke DNS. Do not overuse this feature: The longer the search path, the more network DNS requests generated, and the slower the response. Three or four names are typically sufficient. Up to a maximum of three nameserver lines indicate which systems the local system queries to resolve hostnames to IP addresses, and vice versa. These machines are

www.it-ebooks.info

Important Files and Directories 511

consulted in the order they appear, with a timeout between queries. The first timeout is a few seconds; each subsequent timeout is twice as long as the previous one. The preceding file causes this system to query 10.0.0.50, followed by 10.0.0.51 when the first system does not answer within a few seconds. The resolv.conf file might be automatically updated when a PPP (Point-to-Point Protocol) or DHCP (Dynamic Host Configuration Protocol) controlled interface is activated.
/etc/rpc Maps RPC services to RPC numbers. The three columns in this file show the name

of the server for the RPC program, the RPC program number, and any aliases.
/etc/services Lists system services. The three columns in this file show the informal name of the

service, the port number/protocol the service uses most frequently, and any aliases for the service. This file does not specify which services are running on the local system, nor does it map services to port numbers. The services file is used internally to map port numbers to services for display purposes; editing this file does not change which ports and protocols the local system uses.
/etc/shadow Contains SHA2 (page 1187) or MD5 (page 1175) hashed user passwords,

depending on system configuration. Each entry occupies one line composed of nine fields, separated by colons: login-name:password:last-mod:min:max:warn:inactive:expire:flag The login-name is the user’s username—the name that the user enters in response to the login: prompt or on a GUI login screen. The password is a hashed password that passwd puts in this file. New accounts that are not set up with a password are given a value of !, !!, or * in this field to prevent the user from logging in until you assign a password to that user (page 603). The last-mod field indicates when the password was last modified. The min is the minimum number of days that must elapse before the password can be changed; the max is the maximum number of days before the password must be changed. The warn field specifies how much advance warning (in days) will be given to the user before the password expires. The account will be closed if the number of days between login sessions exceeds the number of days specified in the inactive field. The account will also be closed as of the date in the expire field. The last field in an entry, flag, is reserved for future use. You can use the Password Info tab in systemconfig-users (page 603) or chage to modify these fields. The shadow password file must be owned by root and must not be publicly readable or writable. Setting ownership and permissions in this way makes it more difficult for someone to break into the system by identifying accounts without passwords or by using specialized programs that try to match hashed passwords. A number of conventions exist for creating special shadow entries. An entry of *LK* or NP in the password field indicates locked or no password, respectively. No password is different from an empty password; no password implies that this is an administrative account that no one ever logs in on directly. Occasionally programs will run with the privileges of this account for system maintenance functions. These accounts are set up under the principle of least privilege (page 410).

www.it-ebooks.info

512 Chapter 12 Files, Directories, and Filesystems

Entries in the shadow file must appear in the same order as in the passwd file. There must be exactly one shadow entry for each passwd entry.
/etc/hosts.deny As part of the client/server model, TCP wrappers use these files for access control. and See page 484 for more information. /etc/hosts.allow /etc/sysconfig A directory containing a hierarchy of system configuration files. For more information

refer to the /usr/share/doc/initscripts*/sysconfig.txt file.
/etc/sysconfig/network

Describes the network setup for the local system. Set HOSTNAME in this file to the hostname of the system. Setting this variable changes the hostname of the system the next time it boots. Give the command hostname name to change the hostname of the system to name immediately. Without changing the network file, the hostname will revert the next time the system boots.
/proc Provides a window into the Linux kernel. Through the /proc pseudofilesystem you

can obtain information on any process running on the system, including its current state, memory usage, CPU usage, terminal association, parent, and group. You can extract information directly from the files in /proc. An example follows:
$ sleep 1000 & [1] 3104 $ cd /proc/3104 $ ls -l dr-xr-xr-x. 2 sam pubs 0 04-09 14:00 attr -r-------- 1 sam pubs 0 04-09 14:00 auxv -r--r--r-- 1 sam pubs 0 04-09 14:00 cgroup --w------- 1 sam pubs 0 04-09 14:00 clear_refs -r--r--r-- 1 sam pubs 0 04-09 14:00 cmdline -rw-r--r-- 1 sam pubs 0 04-09 14:00 coredump_filter -r--r--r-- 1 sam pubs 0 04-09 14:00 cpuset lrwxrwxrwx 1 sam pubs 0 04-09 14:00 cwd -> /home/sam -r-------- 1 sam pubs 0 04-09 14:00 environ lrwxrwxrwx 1 sam pubs 0 04-09 14:00 exe -> /bin/sleep dr-x------ 2 sam pubs 0 04-09 14:00 fd ... $ cat status Name: sleep State: S (sleeping) Tgid: 3104 Pid: 3104 PPid: 1503 TracerPid: 0 Uid: 1000 1000 1000 1000 Gid: 1000 1000 1000 1000 FDSize: 256 Groups: 4 20 24 46 105 119 122 1000 VmPeak: 3232 kB VmSize: 3232 kB VmLck: 0 kB ...

www.it-ebooks.info

Important Files and Directories 513

In this example, bash creates a background process (PID 3104) for sleep. Next the user changes directories to the directory in /proc that has the same name as the PID of the background process (cd /proc/3104). This directory holds information about the process it is named for—the sleep process in the example. The ls –l command shows that some entries in this directory are links (cwd is a link to the directory the process was started from, and exe is a link to the executable file that this process is running) and some appear to be ordinary files. All appear to be empty. However, when you use cat to display one of these pseudofiles (status in the example), cat displays output. Obviously it is not an ordinary file. swap Swap space is used by the virtual memory subsystem of the kernel. When it runs low on

real memory (RAM), the kernel writes memory pages from RAM to the swap space. Which pages are written and when they are written are controlled by finely tuned algorithms in the Linux kernel. When needed by running programs, the kernel brings these pages back into RAM—a technique called paging (page 1180). When a system is running very short on memory, an entire process might be paged out to disk. Running an application that requires a large amount of virtual memory might result in the need for additional swap space. Swap space can be added and deleted from the system dynamically: If you run out of swap space, you can use mkswap to create a swap file and swapon to enable it. Normally the kernel uses a disk partition as swap space, but it can also use a file for this purpose. However, a disk partition provides much better performance than a file. If you are creating a file as swap space, first use df to ensure the partition you are creating it in has adequate space for the file. The following commands first use dd and /dev/zero (page 505) to create an empty file (do not use cp because you might create a file with holes, which might not work) in the working directory. Next mkswap takes as an argument the name of the file created in the first step to set up the swap space. For security reasons, change the file so it cannot be read from or written to by anyone except a user with root privileges. Use swapon with the same argument to turn the swap file on; then use swapon –s to confirm the swap space is available. The final two commands turn off the swap file and remove it:
# dd if=/dev/zero of=swapfile bs=1024 count=65536 65536+0 records in 65536+0 records out 67108864 bytes (67 MB) copied, 0.40442 s, 166 MB/s # mkswap swapfile Setting up swapspace version 1, size = 65532 KiB no label, UUID=49ec7f5b-1391-4b24-bd0a-a07e55752666 # chmod 600 swapfile # swapon swapfile # swapon -s Filename Type Priority /dev/dm-1 partition /swapfile file # swapoff swapfile # rm swapfile rm: remove regular file 'swapfile'? y

Size 2031612 0 65532 0

Used -1 -2

www.it-ebooks.info

514 Chapter 12 Files, Directories, and Filesystems
/sys A pseudofilesystem that was added in the Linux 2.6 kernel to make it easy for pro-

grams running in kernelspace, such as device drivers, to exchange information with programs running in userspace. See page 516 for more information.
/usr/share/magic Most files begin with a unique identifier called a magic number. This file is a text database listing all known magic numbers on the system. When you use the file util-

ity, it consults /usr/share/magic to determine the type of a file. Occasionally you might acquire a new tool that creates a new type of file that is unrecognized by the file utility. In this situation you need to update the /usr/share/magic file; refer to the magic man page for details. See also “magic number” on page 1174.
/var/log Holds system log files, many of which are generated by syslogd (page 623). You can use a text display program such as less, tail, or cat, or the graphical program gnomesystem-log (gnome-system-log package) to view the files in this directory. To run gnome-system-log, select Main menu: Applications System Tools Log File Viewer

(if Applications is not visible see “Configuring Fallback Mode” on page 101) or enter gnome-system-log from a terminal emulator or in a Run Application window (ALT-F2).
/var/log/messages

Contains messages from daemons, the Linux kernel, and security programs. For example, you will find filesystem full warning messages, error messages from system daemons (e.g., NFS, ntpd, printer daemons), SCSI and IDE disk error messages, and more in messages. Check /var/log/messages periodically to keep informed about important system events. Much of the information displayed on the system console is also sent to messages. If the system experiences a problem and you cannot access the console, check this file for messages about the problem. See page 623 for information on syslogd, which generates many of these messages.
/var/log/secure

Holds messages from security-related programs such as su and the sshd daemon.

File Types
Linux supports many types of files. This section discusses the following types of files: • Ordinary files, directories, links, and inodes (next) • Symbolic links (page 515) • Device special files (page 515) • FIFO special files (named pipes) (page 517) • Sockets (page 517) • Block and character devices (page 518) • Raw devices (page 518)

www.it-ebooks.info

File Types 515

Ordinary Files, Directories, Links, and Inodes
Ordinary and directory files

An ordinary file stores user data, such as textual information, programs, or images, such as a jpeg or tiff file. A directory is a standard-format disk file that stores information, including names, about ordinary files and other directory files. An inode is a data structure (page 1160), stored on disk, that defines a file’s existence and is identified by an inode number. An inode contains critical information about a file, such as the UID of the owner, where it is physically located on the disk, and how many hard links point to it. In addition, SELinux (page 459) stores extended information about files in inodes. An inode that describes a directory file maps each of the filenames in the directory to the inode that describes that file. This setup allows an inode to be associated with more than one filename and to be pointed to from more than one directory. When you move (mv) a file, including a directory file, within a filesystem, you change the filename portion of the directory entry associated with the inode that describes the file. You do not create a new inode. If you move a file to another filesystem, mv first creates a new inode on the destination filesystem and then deletes the original inode. You can also use mv to move a directory recursively from one filesystem to another. In this case mv copies the directory and all the files in it and deletes the original directory and its contents.

Inodes

Hard links

When you make an additional hard link (ln; page 214) to a file, you add a directory entry that points to the inode that describes the file. You do not create a new inode. It is not possible to create a hard link to a directory. When you remove (rm) a file, you delete the directory entry that describes the file. When you remove the last hard link to a file, the operating system puts all blocks the inode pointed to back in the free list (the list of blocks that are available for use on the disk) and frees the inode to be used again.

The . and .. directory entries

Every directory contains at least two entries (. and ..). The . entry is a link to the directory itself. The .. entry is a link to the parent directory. In the case of the root directory, there is no parent; the .. entry is a link to the root directory itself. Because each filesystem has a separate set of inodes, you can create hard links to a file only from within the filesystem that holds that file. To get around this limitation, Linux provides symbolic links, which are files that point to other files. Files that are linked by a symbolic link do not share an inode. As a consequence, you can create a symbolic link to a file from any filesystem. You can also create a symbolic link to a directory, device, or other special file. For more information refer to “Symbolic Links” on page 216.

Symbolic links

Device Special Files
Device special files (also called device files and special files) represent Linux kernel routines that provide access to an operating system feature. FIFO (first in, first out) special files allow unrelated programs to exchange information. Sockets

www.it-ebooks.info

516 Chapter 12 Files, Directories, and Filesystems

allow unrelated processes on the same or different systems to exchange information. One type of socket, the UNIX domain socket, is a special file. Symbolic links are another type of special file.
Device files

Device files include both block and character special files and represent device drivers that allow the system to communicate with peripheral devices, such as terminals, printers, and hard disks. By convention, device files appear in the /dev directory and its subdirectories. Each device file represents a device; hence, the system reads from and writes to the file to read from and write to the device it represents. The following example shows part of a listing for the /dev directory:
$ ls -l /dev crw-rw----. 1 crw-------. 1 drwxr-xr-x. 2 drwxr-xr-x. 2 drwxr-xr-x. 3 lrwxrwxrwx. 1 drwxr-xr-x. 2 crw-------. 1 lrwxrwxrwx. 1 ... brw-rw----. 1 brw-rw----. 1 brw-rw----. 1 ... root root root root root root root root root video root root root root root root root root 10, 175 01-25 10, 235 01-25 640 01-25 80 01-25 60 01-25 3 01-25 2760 01-25 5, 1 01-25 11 01-25 8, 8, 8, 12:17 12:17 04:17 04:17 04:17 04:17 12:17 12:17 04:17 agpgart autofs block bsg bus cdrom -> sr0 char console core -> /proc/kcore

root disk root disk root disk

0 01-25 04:17 sda 1 01-25 12:17 sda1 2 01-25 04:17 sda2

The first character of each line is always –, b, c, d, l, or p, representing the file type—ordinary (plain), block, character, directory, symbolic link, or named pipe (next), respectively. The next nine characters identify the permissions for the file, followed by the number of hard links and the names of the owner and the group. Where the number of bytes in a file would appear for an ordinary or directory file, a device file shows major and minor device numbers (page 517) separated by a comma. The rest of the line is the same as for any other ls –l listing (page 202). udev The udev utility manages device naming dynamically. It replaces devfs and moves the device-naming functionality from the kernel to userspace. Because devices are added to and removed from a system infrequently, the performance penalty associated with this change is minimal. The benefit of the move is that a bug in udev cannot compromise or crash the kernel. The udev utility is part of the hotplug system (next). When a device is added to or removed from the system, the kernel creates a device name in the /sys pseudofilesystem and notifies hotplug of the event, which is received by udev. The udev utility then creates the device file, usually in the /dev directory, or removes the device file from the system. The udev utility can also rename network interfaces. See the page at www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html for more information. The hotplug system allows you to plug a device into a running system and use it immediately. Although hotplug was available in the Linux 2.4 kernel, the 2.6 kernel integrates hotplug with the unified device driver model framework (the driver

/sys

Hotplug

www.it-ebooks.info

File Types 517

model core) so any bus can report an event when a device is added to or removed from the system. User software can be notified of the event so it can take appropriate action. See linux-hotplug.sourceforge.net for more information.

FIFO Special File (Named Pipe)
A FIFO special file, also called a named pipe, represents a pipe: You read from and write to the file to read from and write to the pipe. The term FIFO stands for first in, first out—the way any pipe works. In other words, the first information you put in one end is the first information that comes out the other end. When you use a pipe on a command line to send the output of a program to the printer, the printer outputs the information in the same order that the program produced it and sent it to the pipe. Unless you are writing sophisticated programs, you will not be working with FIFO special files. However, programs use named pipes for interprocess communication. You can create a pipe using mkfifo:
$ mkfifo AA $ ls -l AA prw-rw-r--. 1 sam pubs 0 01-27 17:55 AA

The p at the left end of the output indicates the file is a pipe. Both UNIX and Linux systems have included pipes for many generations. Without named pipes, only processes that were children of the same ancestor could use pipes to exchange information. Using named pipes, any two processes on a single system can exchange information. When one program writes to a FIFO special file, another program can read from the same file. The programs do not have to run at the same time or be aware of each other’s activity. The operating system handles all buffering and information storage. This type of communication is termed asynchronous (async) because the programs on the opposite ends of the pipe do not have to be synchronized.

Sockets
Like FIFO special files, UNIX/Linux domain sockets allow asynchronous processes that are not children of the same ancestor to exchange information. These sockets are the central mechanism of the interprocess communication that forms the basis of the networking facility. They differ from TCP/IP sockets, which are not represented in the filesystem. When you use networking utilities, pairs of cooperating sockets manage the communication between the processes on the local system and the remote system. Sockets form the basis of such utilities as ssh and scp.

Major and Minor Device Numbers
A major device number points to a driver in the kernel that works with a class of hardware devices: terminal, printer, tape drive, hard disk, and so on. In the listing of the /dev directory on page 516, all the hard disk partitions have a major device number of 8.

www.it-ebooks.info

518 Chapter 12 Files, Directories, and Filesystems

A minor device number identifies a particular piece of hardware within a class. Although all hard disk partitions are grouped together by their major device number, each has a different minor device number (sda1 is 1, sda2 is 2, and so on). This setup allows one piece of software (the device driver) to service all similar hardware yet still be able to distinguish among different physical units.

Block and Character Devices
This section describes typical device drivers. Because device drivers can be changed to suit a particular purpose, the descriptions in this section do not pertain to every system.
Block device

A block device is an I/O (input/output) device that has the following characteristics: • Able to perform random access reads • Has a specific block size • Handles only single blocks of data at a time • Accepts only transactions that involve whole blocks of data • Able to have a filesystem mounted on it • Has the Linux kernel buffer its input and output • Appears to the operating system as a series of blocks numbered from 0 through n – 1, where n is the number of blocks on the device Block devices commonly found on a Linux system include hard disks, floppy diskettes, CDs, and DVDs.

Character device

A character device is any device that is not a block device. Examples of character devices include printers, terminals, tape drives, and modems. The device driver for a character device determines how a program reads from and writes to that device. For example, the device driver for a terminal allows a program to read the information you type on the terminal in two ways. First, a program can read single characters from a terminal in raw mode—that is, without the driver interpreting characters. (This mode has nothing to do with the raw device described next.) Alternatively, a program can read one line at a time. When a program reads one line at a time, the driver handles the erase and kill characters so the program never sees typing mistakes that have been corrected. In this case, the program reads everything from the beginning of a line to the RETURN that ends a line; the number of characters in a line can vary.

Raw Devices
Device driver programs for block devices usually have two entry points so they can be used in two ways: as block devices or as character devices. The character device form of a block device is called a raw device. A raw device is characterized by • Direct I/O (no buffering through the Linux kernel). • One-to-one correspondence between system calls and hardware requests. • Device-dependent restrictions on I/O.

www.it-ebooks.info

Filesystems 519 fsck An example of a utility that uses a raw device is fsck. It is more efficient for fsck to operate on the disk as a raw device rather than being restricted by the fixed size of blocks in the block device interface. Because it has full knowledge of the underlying filesystem structure, fsck can operate on the raw device using the largest possible units. When a filesystem is mounted, processes normally access the disk through the block device interface, which explains why it is important to allow fsck to modify only unmounted filesystems. On a mounted filesystem, there is the danger that while fsck is rearranging the underlying structure through the raw device, another process could change a disk block using the block device, resulting in a corrupted filesystem.

Filesystems
Table 12-1 lists some types of filesystems available under Linux.

Table 12-1
Filesystem
adfs

Filesystems Features
Advanced Disc Filing System. Used on Acorn computers. The word Advanced differentiated this filesystem from its predecessor DFS, which did not support advanced features such as hierarchical filesystems. Amiga Fast Filesystem (FFS). Automounting filesystem (page 811). Common Internet Filesystem (page 1157). Formerly the Samba Filesystem (smbfs). CODA distributed filesystem (developed at Carnegie Mellon). A pseudofilesystem for pseudoterminals (page 504). A standard filesystem for Fedora/RHEL systems, usually with the ext4 extension. A journaling (page 1171) extension to the ext2 filesystem. It greatly improves recovery time from crashes (it takes a lot less time to run fsck), promoting increased availability. As with any filesystem, a journaling filesystem can lose data during a system crash or hardware failure. An extension to the ext3 filesystem. It is backward compatible with ext2/ext3 filesystems and provides improved performance over ext3 the filesystem. Global Filesystem. GFS is a journaling, clustering filesystem. It enables a cluster of Linux servers to share a common storage pool. Hierarchical Filesystem. Used by older Macintosh systems. Newer Macintosh systems use hfs+. High-Performance Filesystem. The native filesystem for IBM’s OS/2.

affs autofs cifs coda devpts ext2 ext3

ext4 GFS hfs hpfs

www.it-ebooks.info

520 Chapter 12 Files, Directories, and Filesystems

Table 12-1
Filesystem
jffs2 iso9660 minix msdos

Filesystems (continued) Features
Journaling Flash Filesystem (jffs). A filesystem for flash memory. The standard filesystem for CDs and DVDs. Very similar to Linux. The filesystem of a small operating system that was written for educational purposes by Andrew S. Tanenbaum (www.minix3.org). Filesystem used by DOS and subsequent Microsoft operating systems. Do not use msdos for mounting Windows filesystems; it does not read VFAT attributes. Novell NetWare NCP Protocol Filesystem. Used to mount remote filesystems under NetWare. Network Filesystem. Developed by Sun Microsystems, this protocol allows a computer to access remote files over a network as if the files were local (page 791). NT Filesystem. The native filesystem of Windows NT. See www.linux-ntfs.org. An interface to several Linux kernel data structures (page 1160) that behaves like a filesystem (page 512). QNX 4 operating system filesystem. A journaling (page 1171) filesystem based on balanced-tree algorithms. See ext4 for more on journaling filesystems. A dumb, readonly filesystem used mainly for RAM disks (page 1184) during installation. Samba Filesystem (deprecated). See cifs. System V UNIX filesystem. Default filesystem under Sun’s Solaris operating system and other UNIXs. A full-feature UNIX-like filesystem that runs on top of a DOS FAT filesystem. Developed by Microsoft, a standard that allows long filenames on FAT partitions. Veritas Extended Filesystem. The first commercial journaling (page 1171) filesystem, popular under HP-UX and Solaris. SGI’s journaling filesystem (ported from Irix).

ncpfs nfs

ntfs proc qnx4 reiserfs romfs smbfs sysv ufs umsdos vfat VxFS xfs

mount: Mounts a Filesystem
The mount utility connects directory hierarchies—typically filesystems—to the Linux directory hierarchy. These directory hierarchies can be on remote and local

www.it-ebooks.info

Filesystems 521

disks, USB flash drives, CDs, DVDs, and floppy diskettes. Linux can also mount virtual filesystems that have been built inside ordinary files, filesystems built for other operating systems, and the special /proc filesystem (page 512), which maps Linux kernel information to a pseudodirectory. This section covers mounting local filesystems; refer to page 791 for information on using NFS to mount remote directory hierarchies. See /dev on page 503 for information on device names.
Mount point

The mount point for the filesystem/directory hierarchy that you are mounting is a directory in the local filesystem. This directory must exist before you can mount a filesystem; its contents disappear as long as a filesystem is mounted on it and reappear when you unmount the filesystem. See page 36 for a discussion of mount points. Without any arguments, mount lists the mounted filesystems, showing the physical device holding each filesystem, the mount point, the type of filesystem, and any options set when each filesystem was mounted. The mount utility gets this information from the /etc/mtab file (page 508).

$ mount /dev/mapper/vg_bee-lv_root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) fusectl on /sys/fs/fuse/connections type fusectl (rw) gvfs-fuse-daemon on /home/sam/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=sam)

The first entry in the preceding example shows an LV mounted by the LVM (page 42) device mapper on / (root). The next entries show the /proc (page 512) and /sys (page 516) pseudofilesystems. The line that starts with devpts shows the interface to pseudoterminal (pty) devices mounted at /dev/pts/* (page 504). The tmpfs device represents a shared-memory filesystem mounted at /dev/shm and stored in RAM. As explained on page 39, the /boot partition cannot reside on an LV; it is mounted on /dev/sda1. binfmt_misc The binfmt_misc line allows the Linux kernel to recognize and execute arbitrary executable file formats and to pass to the resulting processes user space applications, such as emulators and virtual machines. The kernel uses a file’s magic number (page 514) to identify the format of the file. This feature enables you to invoke many programs (e.g., compiled Java, Python) by entering its name in response to a shell prompt. See www.kernel.org/doc/Documentation/binfmt_misc.txt for more information. The fusectl line provides FUSE (Filesystems in Userspace; fuse.sourceforge.net). FUSE is a kernel module that allows a nonprivileged user to create a custom filesystem without editing the kernel. The gvfs-fuse-daemon line uses FUSE to provide a userspace virtual filesystem that support sftp, ftp, smb, etc.

fusectl

gvfs

www.it-ebooks.info

522 Chapter 12 Files, Directories, and Filesystems

Do not mount anything on root (/ ) caution Always mount network directory hierarchies and removable devices at least one level below the root level of the filesystem. The root filesystem is mounted on /; you cannot mount two filesystems in the same place. If you were to try to mount something on /, all files, directories, and filesystems that were under the root directory would no longer be available, and the system would crash.

When you add a line for a filesystem to the /etc/fstab file (page 506), you can mount that filesystem by giving the associated mount point or device name as the argument to mount. For example, the following command mounts a backup volume at /backup:
$ mount /backup

This command works because /etc/fstab contains the additional information needed to mount the file. A nonprivileged user is able to mount the file because of the user option.
/dev/sda7 /backup ext4 user,noauto,rw 0 0

On the local system, the mount point and device name will differ from those in this example. You can specify a filesystem type of auto (in place of ext4) to cause the system to probe the filesystem to determine its type.You can also mount filesystems that do not appear in /etc/fstab. For example, when you insert a floppy diskette that holds a DOS filesystem into the floppy diskette drive, you can mount that filesystem using the following command:
# mount –t msdos /dev/fd0 /media/floppy0

The –t msdos option specifies a filesystem type of msdos. You can mount DOS filesystems only if the Linux kernel is configured to accept DOS filesystems (it is by default). You do not need to mount a DOS filesystem to read from and write to it, such as when you use unix2dos (page 159). However, you do need to mount a DOS filesystem to use Linux commands (other than Mtools commands) on files on the filesystem (which might be on a diskette).

Mount Options
The mount utility takes many options, which you can specify either on the command line or in the /etc/fstab file (page 524). For a complete list of mount options for local filesystems, see the mount man page; for remote directory hierarchies, see page 797 and the nfs man page. The system mounts most filesystems specified in fstab when it boots. You can specify the noauto option to cause Linux not to mount a filesystem automatically.

Mount removable devices with the nosuid option security Always mount removable devices with the nosuid option so a malicious user cannot, for example, put a setuid copy of bash on a disk and have a shell with root privileges. By default, Fedora/RHEL uses the nosuid option when mounting removable media. See page 796 for more information.

www.it-ebooks.info

Filesystems 523

Unless you specify the user, users, or owner option, only a user running with root privileges can mount and unmount a filesystem. The user option allows any user to mount the filesystem, but the filesystem can be unmounted only by the user who mounted it; the users option allows any user to mount and unmount the filesystem. These options are frequently specified for CD, DVD, and floppy drives. The owner option, which is used only under special circumstances, is similar to the user option except that the user mounting the device must own the device. Three options can help ensure the security of the system when you allow nonprivileged users to mount filesystems. The noexec option prevents a user from running an executable file on the mounted filesystem, nosuid forces mounted setuid and setgid executable files to run with regular permissions (no effective user or group ID change) on the mounted filesystem (page 796), and nodev prevents the system from recognizing a device special file on the mounted filesystem (page 797). The user and users option imply all three of these options; owner implies nosuid and nodev.

umount: Unmounts a Filesystem
The umount utility unmounts a filesystem as long as it does not contain any files or directories that are in use (open). For example, a logged-in user’s working directory cannot be on the filesystem you want to unmount. The next command unmounts the CD mounted earlier:
$ umount /media/cdrom

Unmount a floppy or a remote (NFS) directory hierarchy the same way you would unmount a partition of a hard drive. The umount utility consults /etc/fstab to get the necessary information and then unmounts the appropriate filesystem from its server. When a process has a file open on the filesystem you are trying to unmount, umount displays a message similar to the following: umount: /home: device is busy

When you cannot unmount a device because it is in use tip When a process has a file open on a device you need to unmount, use fuser to determine which process has the file open and to kill it. For example, when you want to unmount a CD, give the command fuser –ki /media/cdrom (substitute the mount point for the diskette on the local system for /media/cdrom). After checking with you, this command kills the process(es) using the CD. You can also use lsof (page 629) to find open files.

Use the –a option to umount to unmount all mounted filesystems that are not in use. You can never unmount the filesystem mounted at /. You can combine –a with the –t option to unmount filesystems of a given type (e.g., ext4 or nfs). For example, the following command unmounts all mounted nfs directory hierarchies that are not in use:
# umount -at nfs

www.it-ebooks.info

524 Chapter 12 Files, Directories, and Filesystems

fstab: Keeps Track of Filesystems
The system administrator maintains the /etc/fstab file, which lists local and remote directory hierarchies, most of which the system mounts automatically when it boots. The fstab file has six columns; a hyphen is a placeholder for a column that has no value: 1. Name—The n