Ubuntu Linux 实战手册


Praise for A Practical Guide to Ubuntu Linux “I am so impressed by how Mark Sobell can approach a complex topic in such an understandable manner. His command examples are espe- cially useful in providing a novice (or even 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 his books later.” —Scott Mann Aztek Networks “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 ®“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 con- cepts for computer usage.” —Nathan Eckenrode New York Local Community Team Praise for Other Books by Mark Sobell “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 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 look- ing 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 GonzalezA Practical Guide to Ubuntu Linux ®This page intentionally left blank A Practical Guide to Ubuntu Linux 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 ®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. Ubuntu is a registered trademark of Canonical Ltd. 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: www.prenhallprofessional.com Library of Congress Cataloging-in-Publication Data Sobell, Mark G. A practical guide Ubuntu Linux / Mark G. Sobell. p. cm. Includes index. ISBN-13: 978-0-13-236039-5 (pbk. : alk. paper) 1. Linux. 2. Operating systems (Computers) I. Title. QA76.76.O63S59497 2008 005.4'32—dc22 2007043244 Copyright © 2008 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. For information regarding permissions, write to: Pearson Education, Inc. Rights and Permissions 501 Boylston Street, Suite 900 Boston, MA 02116 Fax (617) 671-3447 ISBN-13: 978-0-13-236039-5 ISBN-10: 0-13-236039-X Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts. First printing, December 2007For my dad, Morton Sobell, who taught me to examine the world very carefully.This page intentionally left blank ixix Brief Contents Contents xi Preface xxxv 1Welcome to Linux1 PART I Installing Ubuntu Linux 21 2 Installation Overview 23 3 Step-by-Step Installation 45 PART II Getting Started with Ubuntu Linux 85 4 Introduction to Ubuntu Linux 87 5 The Linux Utilities 145 6 The Linux Filesystem 183 7The Shell219 PART III Digging into Ubuntu Linux 249 8 Linux GUIs: X and GNOME 251 9 The Bourne Again Shell 275 10 Networking and the Internet 353 11 Programming the Bourne Again Shell 395x Brief Contents PART IV System Administration 483 12 System Administration: Core Concepts 485 13 Files, Directories, and Filesystems 553 14 Downloading and Installing Software 583 15 Printing with CUPS 611 16 Building a Linux Kernel 635 17 Administration Tasks 657 18 Configuring a LAN 693 PART V Using Clients and Setting Up Servers 705 19 OpenSSH: Secure Network Communication 707 20 FTP: Transferring Files Across a Network 729 21 exim4: Setting Up Mail Servers, Clients, and More 755 22 NIS: Network Information Service 781 23 NFS: Sharing Filesystems 799 24 Samba: Linux and Windows File and Printer Sharing 823 25 DNS/BIND: Tracking Domain Names and Addresses 845 26 firestarter and iptables: Setting Up a Firewall 885 27 Apache: Setting Up a Web Server 915 PART VI Appendixes 969 A Regular Expressions 971 B Help 981 C Security 991 D The Free Software Definition 1011 E The Linux 2.6 Kernel 1015 Glossary 1021 Index 1071xixi Contents Preface xxxvi Chapter 1: Welcome to Linux 1 The GNU–Linux Connection 2 The History of GNU–Linux 2 The Code Is Free 4 Have Fun! 5 The Linux 2.6 Kernel 5 The Heritage of Linux: UNIX 5 What Is So Good About Linux? 6 Why Linux Is Popular with Hardware Companies and Developers 7 Linux Is Portable 8 Standards 9 The C Programming Language 9 Ubuntu Linux 10 Overview of Linux 10 Linux Has a Kernel Programming Interface 10 Linux Can Support Many Users 11 Linux Can Run Many Tasks 11 Linux Provides a Secure Hierarchical Filesystem 12 The Shell: Command Interpreter and Programming Language 12 A Large Collection of Useful Utilities 14 Interprocess Communication 14 System Administration 15xii Contents Additional Features of Linux 15 GUIs: Graphical User Interfaces 15 (Inter)Networking Utilities 16 Software Development 17 Conventions Used in This Book 17 Chapter Summary 20 Exercises 20 PART I Installing Ubuntu Linux 21 Chapter 2: Installation Overview 23 The Live/Install Desktop CD/DVD 24 More Information 24 Planning the Installation 25 Considerations 25 Requirements 25 Processor Architecture 26 Interfaces: Installer and Installed System 27 Ubuntu Releases 28 Ubuntu Editions 28 Installing a Fresh Copy or Upgrading an Existing Ubuntu System? 29 Setting Up the Hard Disk 30 RAID 34 LVM: Logical Volume Manager 35 The Installation Process 36 Downloading and Burning a CD/DVD 37 The Easy Way to Download a CD ISO Image File 37 Other Ways to Download a CD/DVD ISO Image File 37 Verifying an ISO Image File 40 Burning the CD/DVD 40 Gathering Information About the System 41 Chapter Summary 42 Exercises 43 Advanced Exercises 43 Chapter 3: Step-by-Step Installation 45 Basic Installation from the Live/Install Desktop CD/DVD 46 Booting the System 46 Checking the CD/DVD for Defects 47 Live Session 47 Contents xiii Graphical Partitioners 53 gparted: the GNOME Partition Editor 53 ubiquity: Setting Up Partitions 56 Upgrading to a New Release 59 Installing KDE 60 Setting Up a Dual-Boot System 61 Creating Free Space on a Windows System 61 Installing Ubuntu Linux as the Second Operating System 61 Advanced Installation 62 The Live/Install Desktop CD: The Initial Install Screen 62 The Alternate CD Initial Install Screen Menu 65 The Server CD Initial Install Screen Menu 66 The DVD 67 The Ubuntu Textual Installer 67 The X Window System 74 displayconfig-gtk: Configures the Display 75 The xorg.conf File 77 gdm: Displays a Graphical Login 82 Chapter Summary 83 Exercises 83 Advanced Exercises 84 PART II Getting Started with Ubuntu Linux 85 Chapter 4: Introduction to Ubuntu Linux 87 Curbing Your Power: root Privileges/sudo 88 A Tour of the Ubuntu Linux Desktop 89 Logging In on the System 89 Introduction 90 Launching Programs from the Desktop 91 Switching Workspaces 93 Setting Personal Preferences 94 Mouse Preferences 95 Working with Windows 96 Using Nautilus to Work with Files 96 The Update Notifier 100 Changing Appearances (Themes) 102 Session Management 104 Getting Help 104 Feel Free to Experiment 105 Logging Out 105xiv Contents Getting the Most out of the Desktop 105 GNOME Desktop Terminology 105 Opening Files 106 Panels 107 The Main Menu 110 Windows 111 The Object Context Menu 115 Updating, Installing, and Removing Software Packages 119 Software Sources Window 119 Add/Remove Applications 120 Synaptic: Finds, Installs, and Removes Software 121 Where to Find Documentation 124 Ubuntu Help Center 124 man: Displays the System Manual 124 info: Displays Information About Utilities 126 The ––help Option 129 HOWTOs: Finding Out How Things Work 129 Getting Help with the System 130 More About Logging In 132 The Login Screen 132 What to Do if You Cannot Log In 133 Logging In Remotely: Terminal Emulators, ssh, and Dial-Up Connections 133 Logging In from a Terminal (Emulator) 134 Changing Your Password 135 Using Virtual Consoles 136 Working from the Command Line 136 Correcting Mistakes 137 Repeating/Editing Command Lines 139 Controlling Windows: Advanced Operations 139 Changing the Input Focus 139 Changing the Resolution of the Display 140 The Window Manager 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 147 cat: Displays a Text File 147 rm: Deletes a File 148 less Is more: Display a Text File One Screen at a Time 148 hostname: Displays the System Name 149 Contents xv 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 151 head: Displays the Beginning of a File 152 tail: Displays the End of a File 152 sort: Displays a File in Order 153 uniq: Removes Duplicate Lines from a File 154 diff: Compares Two Files 154 file: Tests the Contents of a File 155 | (Pipe): Communicates Between Processes 156 Four More Utilities 157 echo: Displays Text 157 date: Displays the Time and Date 157 script: Records a Shell Session 158 unix2dos: Converts Linux and Macintosh Files to Windows Format 159 Compressing and Archiving Files 159 bzip2: Compresses a File 160 bunzip2 and bzcat: Decompress a File 160 gzip: Compresses a File 161 tar: Packs and Unpacks Archives 161 Locating Commands 164 which and whereis: Locate a Utility 164 apropos: Searches for a Keyword 165 slocate: Searches for a File 166 Obtaining User and System Information 166 who: Lists Users on the System 167 finger: Lists Users on the System 167 w: Lists Users on the System 169 Communicating with Other Users 170 write: Sends a Message 170 mesg: Denies or Accepts Messages 171 Email 171 Tutorial: Creating and Editing a File with vim 172 Starting vim 172 Command and Input Modes 174 Entering Text 175 Getting Help 176 Ending the Editing Session 178 The compatible Parameter 179 Chapter Summary 179 Exercises 181 Advanced Exercises 182xvi Contents Chapter 6: The Linux Filesystem 183 The Hierarchical Filesystem 184 Directory Files and Ordinary Files 184 Filenames 185 The Working Directory 188 Your Home Directory 188 Pathnames 189 Absolute Pathnames 189 Relative Pathnames 190 Directory Commands 191 mkdir: Creates a Directory 191 Important Standard Directories and Files 194 Working with Directories 196 rmdir: Deletes a Directory 196 Using Pathnames 197 mv, cp: Move or Copy Files 197 mv: Moves a Directory 198 Access Permissions 199 ls –l: Displays Permissions 199 chmod: Changes Access Permissions 200 Setuid and Setgid Permissions 201 Directory Access Permissions 202 ACLs: Access Control Lists 203 Enabling ACLs 204 Working with Access Rules 204 Setting Default Rules for a Directory 207 Links 209 Hard Links 210 Symbolic Links 212 rm: Removes a Link 214 Chapter Summary 214 Exercises 216 Advanced Exercises 218 Chapter 7: The Shell 219 The Command Line 220 Syntax 220 Processing the Command Line 223 Executing the Command Line 225 Editing the Command Line 225 Standard Input and Standard Output 226 The Screen as a File 226 The Keyboard and Screen as Standard Input and Standard Output 227 Redirection 228 Pipes 234 Contents xvii Running a Program in the Background 237 Filename Generation/Pathname Expansion 239 The ? Special Character 239 The * Special Character 240 The [] Special Characters 241 Builtins 243 Chapter Summary 244 Utilities and Builtins Introduced in This Chapter 245 Exercises 245 Advanced Exercises 247 PART III Digging into Ubuntu Linux 249 Chapter 8: Linux GUIs: X and GNOME 251 X Window System 252 Using X 254 Window Managers 259 The Nautilus File Browser Window 260 The View Pane 261 The Side Pane 261 Control Bars 262 Menubar 263 GNOME Utilities 266 Deskbar Applet 266 Font Preferences 267 Pick a Font Window 268 Pick a Color Window 268 Run Application Window 269 Searching for Files 269 GNOME Terminal Emulator/Shell 270 Chapter Summary 271 Exercises 272 Advanced Exercises 272 Chapter 9: The Bourne Again Shell 275 Background 276 Shell Basics 277 Startup Files 277 Commands That Are Symbols 280 Redirecting Standard Error 280 Writing a Simple Shell Script 282 Separating and Grouping Commands 286 Job Control 290 Manipulating the Directory Stack 292xviii Contents Parameters and Variables 295 User-Created Variables 296 Variable Attributes 299 Keyword Variables 301 Special Characters 309 Processes 310 Process Structure 310 Process Identification 310 Executing a Command 312 History 312 Variables That Control History 312 Reexecuting and Editing Commands 314 The Readline Library 322 Aliases 328 Single Versus Double Quotation Marks in Aliases 329 Examples of Aliases 330 Functions 331 Controlling bash Features and Options 334 Command Line Options 334 Shell Features 334 Processing the Command Line 338 History Expansion 338 Alias Substitution 338 Parsing and Scanning the Command Line 338 Command Line Expansion 339 Chapter Summary 347 Exercises 349 Advanced Exercises 351 Chapter 10: Networking and the Internet 353 Types of Networks and How They Work 355 Broadcast Networks 356 Point-to-Point Networks 356 Switched Networks 356 LAN: Local Area Network 357 WAN: Wide Area Network 358 Internetworking Through Gateways and Routers 358 Network Protocols 361 Host Address 363 CIDR: Classless Inter-Domain Routing 367 Hostnames 368 Communicate Over a Network 370 finger: Displays Information About Remote Users 370 Sending Mail to a Remote User 371 Mailing List Servers 372 Contents xix Network Utilities 372 Trusted Hosts 372 OpenSSH Tools 373 telnet: Logs In on a Remote System 373 ftp: Transfers Files Over a Network 375 ping: Tests a Network Connection 375 traceroute: Traces a Route Over the Internet 376 host and dig: Query Internet Nameservers 378 jwhois: Looks Up Information About an Internet Site 378 Distributed Computing 379 The Client/Server Model 380 DNS: Domain Name Service 381 Ports 383 NIS: Network Information Service 383 NFS: Network Filesystem 383 Internet Services 384 Proxy Servers 387 RPC Network Services 387 Usenet 388 WWW: World Wide Web 390 URL: Uniform Resource Locator 391 Browsers 392 Search Engines 392 Chapter Summary 392 Exercises 393 Advanced Exercises 394 Chapter 11: Programming the Bourne Again Shell 395 Control Structures 396 if...then 396 if...then...else 400 if...then...elif 403 for...in 409 for 410 while 412 until 416 break and continue 418 case 419 select 425 Here Document 427 File Descriptors 429 Parameters and Variables 432 Array Variables 432 Locality of Variables 434 Special Parameters 436 Positional Parameters 438 Expanding Null and Unset Variables 443xx Contents Builtin Commands 444 type: Displays Information About a Command 445 read: Accepts User Input 445 exec: Executes a Command 448 trap: Catches a Signal 451 kill: Aborts a Process 454 getopts: Parses Options 454 A Partial List of Builtins 457 Expressions 458 Arithmetic Evaluation 458 Logical Evaluation (Conditional Expressions) 459 String Pattern Matching 460 Operators 461 Shell Programs 466 A Recursive Shell Script 467 The quiz Shell Script 470 Chapter Summary 476 Exercises 478 Advanced Exercises 480 PART IV System Administration 483 Chapter 12: System Administration: Core Concepts 485 Running Commands with root Privileges 487 sudo: Running a Command with root Privileges 490 sudoers: Configuring sudo 494 Unlocking the root Account (Assigning a Password to root) 499 su: Gives You Another User’s Privileges 499 The Upstart Event-Based init Daemon 500 Software Packages 501 Definitions 501 Jobs 503 SysVinit (rc) Scripts: Start and Stop System Services 507 System Operation 510 Runlevels 510 Booting the System 511 Recovery (Single-User) Mode 512 Going to Multiuser Mode 515 Logging In 516 Contents xxi Logging Out 517 Bringing the System Down 518 Crash 519 Avoiding a Trojan Horse 520 Getting Help 522 Textual System Administration Utilities 522 kill: Sends a Signal to a Process 522 Other Textual Utilities 525 Setting Up a Server 527 Standard Rules in Configuration Files 528 rpcinfo: Displays Information About portmap 530 The inetd and xinetd Superservers 531 Securing a Server 532 DHCP: Configures Network Interfaces 538 nsswitch.conf: Which Service to Look at First 542 How nsswitch.conf Works 542 PAM 545 More Information 546 Configuration Files, Module Types, and Control Flags 546 Example 548 Modifying the PAM Configuration 549 Chapter Summary 550 Exercises 551 Advanced Exercises 551 Chapter 13: Files, Directories, and Filesystems 553 Important Files and Directories 554 File Types 566 Ordinary Files, Directories, Links, and Inodes 566 Special Files 567 Filesystems 570 mount: Mounts a Filesystem 572 umount: Unmounts a Filesystem 575 fstab: Keeps Track of Filesystems 576 fsck: Checks Filesystem Integrity 577 tune2fs: Changes Filesystem Parameters 578 RAID Filesystem 580 Chapter Summary 580 Exercises 580 Advanced Exercises 581xxii Contents Chapter 14: Downloading and Installing Software 583 JumpStart: Installing and Removing Packages Using aptitude 585 Finding the Package That Holds a File You Need 587 APT: Keeps the System Up-to-Date 588 Repositories 588 sources.list: Specifies Repositories for APT to Search 589 The APT Local Package Indexes and the APT Cache 590 The apt cron Script and APT Configuration Files 590 aptitude: Works with Packages and the Local Package Index 592 apt-cache: Displays Package Information 596 apt-get source: Downloads Source Files 598 dpkg: The Debian Package Management System 598 deb Files 599 dpkg: The Foundation of the Debian Package Management System 600 BitTorrent 604 Installing Non-dpkg Software 607 The /opt and /usr/local Directories 607 GNU Configure and Build System 607 wget: Downloads Files Noninteractively 609 Chapter Summary 610 Exercises 610 Advanced Exercises 610 Chapter 15: Printing with CUPS 611 Introduction 612 Prerequisites 612 More Information 613 Notes 613 JumpStart I: Configuring a Local Printer 614 system-config-printer: Configuring a Printer 614 Configuration Tabs 614 Setting Up a Remote Printer 616 JumpStart II: Configuring a Remote Printer Using the CUPS Web Interface 618 Traditional UNIX Printing 622 Configuring Printers 624 The CUPS Web Interface 624 CUPS on the Command Line 626 Sharing CUPS Printers 629 Printing from Windows 630 Printing Using CUPS 631 Printing Using Samba 631 Contents xxiii Printing to Windows 632 Chapter Summary 633 Exercises 633 Advanced Exercises 633 Chapter 16: Building a Linux Kernel 635 Prerequisites 636 Downloading the Kernel Source Code 637 aptitude: Downloading and Installing the Kernel Source Code 637 git: Obtaining the Latest Kernel Source Code 637 Read the Documentation 638 Configuring and Compiling the Linux Kernel 639 .config: Configures the Kernel 639 Customizing a Kernel 640 Cleaning the Source Tree 642 Compiling a Kernel Image File and Loadable Modules 643 Using Loadable Kernel Modules 643 Installing the Kernel, Modules, and Associated Files 646 Rebooting 647 grub: The Linux Boot Loader 647 menu.lst: Configures grub 648 update-grub: Updates the menu.lst file 651 grub-install: Installs the MBR and grub Files 653 dmesg: Displays Kernel Messages 654 Chapter Summary 655 Exercises 656 Advanced Exercises 656 Chapter 17: Administration Tasks 657 Configuring User and Group Accounts 658 users-admin: Manages User Accounts 658 useradd: Adds a User Account 660 userdel: Removes a User Account 661 usermod: Modifies a User Account 661 groupadd: Adds a Group 661 groupdel: Removes a Group 661 Backing Up Files 662 Choosing a Backup Medium 663 Backup Utilities 663 Performing a Simple Backup 665 dump, restore: Back Up and Restore Filesystems 666xxiv Contents Scheduling Tasks 668 cron and anacron: Schedule Routine Tasks 668 at: Runs Occasional Tasks 671 System Reports 671 vmstat: Reports Virtual Memory Statistics 671 top: Lists Processes Using the Most Resources 672 parted: Reports on and Partitions a Hard Disk 673 Keeping Users Informed 677 Creating Problems 678 Solving Problems 679 Helping When a User Cannot Log In 679 Speeding Up the System 680 lsof: Finds Open Files 681 Keeping a Machine Log 681 Keeping the System Secure 682 Log Files and Mail for root 683 Monitoring Disk Usage 683 logrotate: Manages Log Files 684 Removing Unused Space from Directories 686 Disk Quota System 687 syslogd: Logs System Messages 688 Chapter Summary 690 Exercises 690 Advanced Exercises 691 Chapter 18: Configuring a LAN 693 Setting Up the Hardware 694 Connecting the Computers 694 Routers 695 NIC: Network Interface Card 695 Tools 695 Configuring the Systems 697 network-admin: Configures Network Connections 698 nm-applet: Configures Network Connections Automatically 700 iwconfig: Configures a Wireless NIC 700 Setting Up Servers 702 More Information 703 Chapter Summary 703 Exercises 704 Advanced Exercises 704 Contents xxv PART V Using Clients and Setting Up Servers 705 Chapter 19: OpenSSH: Secure Network Communication 707 Introduction 708 About OpenSSH 708 Files 708 How OpenSSH Works 710 More Information 711 OpenSSH Clients 711 Prerequisites 711 JumpStart: Using ssh and scp 711 Setup 712 ssh: Connects to or Executes Commands on a Remote System 714 scp: Copies Files to and from a Remote System 716 sftp: A Secure FTP Client 718 ~/.ssh/config and /etc/ssh/ssh_config Configuration Files 718 sshd: OpenSSH Server 720 Prerequisites 720 Note 720 JumpStart: Starting the sshd Daemon 720 Authorized Keys: Automatic Login 721 Command Line Options 722 /etc/ssh/sshd_config Configuration File 722 Troubleshooting 724 Tunneling/Port Forwarding 725 Chapter Summary 727 Exercises 728 Advanced Exercises 728 Chapter 20: FTP: Transferring Files Across a Network 729 Introduction 730 More Information 731 FTP Client 731 Prerequisites 731 JumpStart I: Downloading Files Using ftp 732 Notes 735 Anonymous FTP 735 Automatic Login 735 Binary Versus ASCII Transfer Mode 736 ftp Specifics 736xxvi Contents FTP Server (vsftpd)740 Prerequisites 740 Notes 740 JumpStart II: Starting a vsftpd Server 741 Testing the Setup 741 vsftpd.conf: The vsftpd Configuration File 742 Chapter Summary 753 Exercises 753 Advanced Exercises 754 Chapter 21: exim4: Setting Up Mail Servers, Clients, and More 755 Introduction to exim4 756 Prerequisites 757 Notes 757 More Information 758 JumpStart I: Configuring exim4 to Use a Smarthost 758 JumpStart II: Configuring exim4 to Send and Receive Email 760 How exim4 Works 761 Mail Logs 762 Working with Messages 763 Aliases and Forwarding 763 Related Programs 765 Configuring exim4 765 Using a Text Editor to Configure exim4 766 The update-exim4.conf.conf Configuration File 766 dpkg-reconfigure: Configures exim4 768 SpamAssassin 768 Prerequisites 769 How SpamAssassin Works 769 Testing SpamAssassin 770 Configuring SpamAssassin 771 Additional Email Tools 772 Webmail 772 Mailing Lists 774 Setting Up an IMAP or POP3 Server 776 Authenticated Relaying 777 Alternatives to exim4 779 Chapter Summary 779 Exercises 780 Advanced Exercises 780 Contents xxvii Chapter 22: NIS: Network Information Service 781 Introduction to NIS 782 How NIS Works 782 More Information 784 Setting Up an NIS Client 784 Prerequisites 785 Notes 785 Step-by-Step Setup 786 Testing the Setup 787 yppasswd: Changes NIS Passwords 788 Setting Up an NIS Server 790 Prerequisites 790 Notes 791 Step-by-Step Setup 791 Testing 796 yppasswdd: The NIS Password Update Daemon 797 Chapter Summary 798 Exercises 798 Advanced Exercises 798 Chapter 23: NFS: Sharing Filesystems 799 Introduction 800 More Information 802 Setting Up an NFS Client 802 Prerequisites 802 JumpStart I: Mounting a Remote Directory Hierarchy 803 mount: Mounts a Directory Hierarchy 804 Improving Performance 806 /etc/fstab: Mounts Directory Hierarchies Automatically 807 Setting Up an NFS Server 808 Prerequisites 808 Notes 808 JumpStart II: Configuring an NFS Server Using shares-admin 809 Manually Exporting a Directory Hierarchy 811 Where the System Keeps NFS Mount Information 815 exportfs: Maintains the List of Exported Directory Hierarchies 817 Testing the Server Setup 818 automount: Mounts Directory Hierarchies on Demand 818 Chapter Summary 821 Exercises 822 Advanced Exercises 822xxviii Contents Chapter 24: Samba: Linux and Windows File and Printer Sharing 823 Introduction 824 About Samba 825 Prerequisites 825 More Information 825 Notes 825 Samba Users, User Maps, and Passwords 826 JumpStart: Configuring a Samba Server Using shares-admin 826 swat: Configures a Samba Server 828 smb.conf: Manually Configuring a Samba Server 832 Parameters in the smbd.conf File 832 The [homes] Share: Sharing Users’ Home Directories 838 Accessing Linux Shares from Windows 838 Browsing Shares 838 Mapping a Share 839 Accessing Windows Shares from Linux 839 smbtree: Displays Windows Shares 839 smbclient: Connects to Windows Shares 840 Browsing Windows Networks 840 Mounting Windows Shares 841 Troubleshooting 841 Chapter Summary 844 Exercises 844 Advanced Exercises 844 Chapter 25: DNS/BIND: Tracking Domain Names and Addresses 845 Introduction to DNS 846 Nodes, Domains, and Subdomains 846 Zones 848 Queries 849 Servers 850 Resource Records 851 DNS Query and Response 854 Reverse Name Resolution 855 About DNS 857 How DNS Works 857 Prerequisites 857 More Information 858 Notes 858 Contents xxix JumpStart I: Setting Up a DNS Cache 858 Setting Up BIND 860 named.conf: The named Configuration File 860 Zone Files 863 A DNS Cache 864 DNS Glue Records 868 TSIGs: Transaction Signatures 868 Running BIND in a chroot Jail 870 Troubleshooting 872 A Full-Functioned Nameserver 873 A Slave Server 877 A Split Horizon Server 878 Chapter Summary 883 Exercises 883 Advanced Exercises 884 Chapter 26: firestarter and iptables: Setting Up a Firewall 885 About firestarter 886 Prerequisites 886 Notes 887 More Information 888 JumpStart: Building a Firewall Using the firestarter Firewall Wizard 888 firestarter: Maintains a Firewall 890 The Status Tab 890 The Events Tab 892 The Policy Tab 894 How iptables Works 896 About iptables 899 More Information 899 Prerequisites 899 Notes 899 Anatomy of an iptables Command 900 Building a Set of Rules 901 Commands 901 Packet Match Criteria 903 Display Criteria 903 Match Extensions 903 Targets 906 Copying Rules to and from the Kernel 907xxx Contents Sharing an Internet Connection Using NAT 908 Connecting Several Clients to a Single Internet Connection 910 Connecting Several Servers to a Single Internet Connection 912 Chapter Summary 912 Exercises 913 Advanced Exercises 913 Chapter 27: Apache: Setting Up a Web Server 915 Introduction 916 About Apache 917 Prerequisites 917 More Information 918 Notes 918 JumpStart: Getting Apache Up and Running 919 Modifying the Configuration Files 919 Testing Apache 920 Putting Your Content in Place 920 Configuring Apache 921 Configuration Tools 921 Include Directives 922 Filesystem Layout 923 Configuration Directives 925 Directives I: Directives You May Want to Modify as You Get Started 926 Contexts and Containers 931 Directives II: Advanced Directives 935 The Ubuntu apache2.conf File 948 The Ubuntu default Configuration File 950 Redirects 951 Content Negotiation 951 Type Maps 951 MultiViews 952 Server-Generated Directory Listings (Indexing) 953 Virtual Hosts 953 Setting Up a Virtual Host 954 Types of Virtual Hosts 954 The default Virtual Host 954 Examples 954 Troubleshooting 956 Modules 957 mod_cgi and CGI Scripts 958 mod_ssl 959 Authentication Modules and .htaccess 961 Scripting Modules 962 Multiprocessing Modules (MPMs) 963 Contents xxxi webalizer: Analyzes Web Traffic 964 MRTG: Monitors Traffic Loads 964 Error Codes 964 Chapter Summary 965 Exercises 966 Advanced Exercises 966 PART VI Appendixes 969 Appendix A: Regular Expressions 971 Characters 972 Delimiters 972 Simple Strings 972 Special Characters 972 Periods 973 Brackets 973 Asterisks 974 Carets and Dollar Signs 974 Quoting Special Characters 975 Rules 975 Longest Match Possible 975 Empty Regular Expressions 976 Bracketing Expressions 976 The Replacement String 976 Ampersand 977 Quoted Digit 977 Extended Regular Expressions 977 Appendix Summary 979 Appendix B: Help 981 Solving a Problem 982 Finding Linux-Related Information 983 Documentation 983 Useful Linux Sites 984 Linux Newsgroups 985 Mailing Lists 985 Words 986 Software 986 Office Suites and Word Processors 988 Specifying a Terminal 988xxxii Contents Appendix C: Security 991 Encryption 992 Public Key Encryption 993 Symmetric Key Encryption 994 Encryption Implementation 995 GnuPG/PGP 995 File Security 997 Email Security 997 MTAs (Mail Transfer Agents) 997 MUAs (Mail User Agents) 998 Network Security 998 Network Security Solutions 999 Network Security Guidelines 999 Host Security 1001 Login Security 1002 Remote Access Security 1003 Viruses and Worms 1004 Physical Security 1004 Security Resources 1006 Appendix Summary 1009 Appendix D: The Free Software Definition 1011 Appendix E: The Linux 2.6 Kernel 1015 Native Posix Thread Library (NPTL) 1016 IPSecurity (IPSec) 1016 Asynchronous I/O (AIO) 1016 O(1) Scheduler 1017 OProfile 1017 kksymoops 1017 Reverse Map Virtual Memory (rmap VM) 1017 HugeTLBFS: Translation Look-Aside Buffer Filesystem 1018 remap_file_pages 1018 2.6 Network Stack Features (IGMPv3, IPv6, and Others) 1018 Internet Protocol Virtual Server (IPVS) 1019 Access Control Lists (ACLs) 1019 4GB-4GB Memory Split: Physical Address Extension (PAE) 1019 Scheduler Support for HyperThreaded CPUs 1019 Contents xxxiii Block I/O (BIO) Block Layer 1019 Support for Filesystems Larger Than 2 Terabytes 1020 New I/O Elevators 1020 Interactive Scheduler Response Tuning 1020 Glossary 1021 Index 1071This page intentionally left blank xxxvxxxv 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 an Ubuntu Linux system. In 27 chapters, this book takes you from installing an Ubuntu system through understanding its inner workings to setting up secure servers that run on the system. The audience 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 cer- tainly 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 • Computer science students who are studying the Linux operating system • Technical executives who want to get a grounding in Linux Benefits A Practical Guide to Ubuntu Linux® gives you a broad understanding of many fac- ets of Linux, from installing Ubuntu Linux 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.xxxvi Preface Overlap If you read A Practical Guide to Linux® Commands, Editors, and Shell Program- ming, 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, and the appendix on regular expressions are very similar in the two books, as are the three chapters on the Bourne Again Shell (bash). Chapters that appear in this book but do not appear in A Practical Guide to Linux® Commands, Editors, and Shell Program- ming include Chapters 2 and 3 (installation), Chapters 4 and 8 (Ubuntu Linux and the GUI), Chapter 10 (networking), all of the chapters in Part IV (system adminis- tration) and Part V (servers), and Appendix C (security). Differences 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, Edi- tors, and Shell Programming works exclusively with the command line. It includes full chapters on the vi and emacs editors, as well as chapters on the gawk pattern processing language and the sed stream editor. In addition, it has a command refer- ence section that provides extensive examples of the use of more than 80 of the most important Linux utilities. You can use these utilities to solve problems without resorting to programming in C. This Book Includes Ubuntu Gutsy Gibbon (7.10) on a Live/Install DVD This book includes a live/install DVD that holds the Gutsy Gibbon (7.10) release of Ubuntu Linux. You can use this DVD to run a live Ubuntu session that displays the GNOME desktop without making any changes to your computer: Boot from the DVD, run an Ubuntu live session, and log off. Your system remains untouched: When you reboot, it is exactly as it was before you ran the Ubuntu live session. Alternatively, you can install Ubuntu from the live session. Chapter 2 helps you get ready to install Ubuntu. Chapter 3 provides step-by-step instructions for installing Ubuntu from this DVD. This book guides you through learning about, using, and administrating an Ubuntu Linux session. DVD features The included DVD incorporates all the features of the live/install Desktop CD as well as the Alternate and Server CDs. It also includes all software packages sup- ported by Ubuntu. You can use it to perform a graphical or textual (command line) installation of either a graphical or a textual Ubuntu system. If you do not have an Internet connection, you can use the DVD as a software repository and install any supported software packages from it. Features of This Book This book is designed and organized so you can get the most out of it in the shortest 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:Features of This Book xxxvii Look up a topic of interest in the table of contents or 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 get additional information: Consider the Internet an extension of this book. A Practical Guide to Ubuntu Linux® is structured with the following features: • 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. • 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. • 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 available at www.sobell.com. •The glossary defines more than 500 common terms. • The chapters that cover 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 explained in the rest of the chapter. • This book provides resources for finding software on the Internet. It also explains how to download and install software using Synaptic, aptitude, the GNOME Add/Remove Applications window, and BitTorrent. It details controlling automatic updates using the Update Notifier and the Update Manager window. • This book describes in detail many important GNU tools, including the GNOME desktop, the Nautilus File Browser, the parted and gparted parti- tion editors, the gzip compression utility, and many command line utilities that come from the GNU project. • Pointers throughout the text provide help in obtaining online documenta- tion from many sources, including the local system, the Ubuntu Web site, and other locations on the Internet. • Many useful URLs point to Web sites where you can obtain software, security programs and information, and more. •The comprehensive index helps you locate topics quickly and easily.xxxviii Preface Key Topics Covered in This Book This book contains a lot of information. This section distills and summarizes its contents. In addition, “Details” (starting on page xli) describes what each chapter covers. Finally, the table of contents provides more detail. This book: Installation • Describes how to download Ubuntu Linux ISO images from the Internet and burn the Ubuntu live/install Desktop CD, the DVD, or the Ubuntu Alternate or Server installation CD. • Helps you plan the layout of the system’s hard disk. It includes a discus- sion of partitions, partition tables, and mount points, and assists you in using the ubiquity or gparted graphical partitioner or the Ubuntu textual partitioner to partition the hard disk. • Explains how to set up a dual-boot system so you can install Ubuntu Linux on a Windows system and boot either operating system. • Describes in detail how to install Ubuntu Linux from a live/install Desktop CD or the live/install DVD using the ubiquity graphical installer. It also explains how to use the textual installer found on the Alternate CD, the Server CD, and the DVD. The graphical installer is fast and easy to use. The textual installer gives you more options and works on systems with less RAM (system memory). • Covers testing an Ubuntu CD/DVD for defects, setting boot command line parameters (boot options), and creating a RAID array. • Covers the details of installing and customizing the X.org version of the X Window System either graphically using the Screen and Graphics Preferences window or manually with a text editor. Working with Ubuntu Linux • Introduces the GNOME desktop (GUI) and explains how to use desktop tools, including the Top and Bottom panels, panel objects, the Main menu, object context menus, the Workspace Switcher, the Nautilus File Browser, and the GNOME Terminal emulator. • Explains how to use the Appearance Preferences window to add and mod- ify themes to customize your desktop to please your senses and help you work more efficiently. • Details how to set up 3D desktop visual effects that take advantage of Compiz Fusion. • Covers the Bourne Again Shell (bash) in three chapters, including an entire chapter on shell programming that 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 pro- gramming language to write shell scripts.Key Topics Covered in This Book xxxix • 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, 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. System administration • Explains how to use the Ubuntu graphical and textual (command line) 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. • Goes into detail about using sudo to allow specific users to work with root privileges (become Superuser) and customizing the way sudo works by editing the sudoers configuration file. It also explains how you can unlock the root account if necessary. • Describes how to use the following tools to download and install software to keep a system up-to-date and to install new software: ◆ The Software Sources window controls which Ubuntu and third-party software repositories Ubuntu downloads software packages from and whether Ubuntu downloads updates automatically. You can also use this window to cause Ubuntu to download and install security updates automatically. ◆ If you do not have an Internet connection, you can use the Software Sources window to set up the DVD included with this book as a soft- ware repository. You can then install any software packages that Ubuntu supports from this repository. ◆ Based on how you set up updates in the Software Sources window, the Update Notifier pops up on the desktop to let you know when soft- ware updates are available. Click the Update Notifier to open the Update Manager window, from which you can download and install updates. ◆ The Add/Remove Applications window provides an easy way to select, download, and install a wide range of software packages. ◆ Synaptic allows you to search for, install, and remove software pack- ages. It gives you more ways to search for packages than does the Add/Remove Applications window.xl Preface ◆ APT downloads and installs software packages from the Internet (or the included DVD), keeping a system up-to-date and resolving dependencies as it processes the packages. You can use APT from a graphical interface (Synaptic) or from several textual interfaces (e.g., aptitude and apt-get). ◆ BitTorrent is a good choice for distributing large amounts of data such as the Ubuntu 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, recov- ery (single-user) and multiuser modes, and steps to take if the system crashes. • Describes how to use and program the new Upstart init daemon, which replaces the System V init daemon. • Describes files, directories, and filesystems, including types of files and file- systems, 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, cpio, dump, and restore. • 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. • Covers using firestarter to share an Internet connection over a LAN, run a DHCP server, and set up a basic firewall to protect the system. • Provides instructions on using iptables to share an Internet connection over a LAN and to build advanced firewalls. • 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. Clients and servers • Explains how to set up and use the most popular Linux servers, providing a chapter on each: Apache, Samba, OpenSSH, exim4, DNS, NFS, FTP, firestarter and iptables, and NIS (all of which are supported by Ubuntu Linux). • Describes how to set up a CUPS printer server. • Describes how to set up and use a DHCP server either by itself or from firestarter. Programming • Provides a full chapter covering shell programming using bash, including many examples.Key Topics Covered in This Book xli 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” (page 17) section details the typefaces and terminology this book uses. Part I Part I, “Installing Ubuntu Linux,” discusses how to install Ubuntu Linux. Chapter 2 presents an overview of the process of installing Ubuntu 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 Ubuntu Linux from a CD or DVD, using the graphical or textual installer. It also shows how to set up the X Window System and customize your desktop (GUI). Part II Part II, “Getting Started with Ubuntu Linux,” familiarizes you with Ubuntu Linux, covering logging in, the GUI, utilities, the filesystem, and the shell. Chapter 4 intro- duces desktop features, including the Top and Bottom 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 use- ful 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 back- ground, and using the shell to generate and expand filenames. Part III Part III, “Digging into Ubuntu Linux,” goes into more detail about working with the system. Chapter 8 discusses the GUI (desktop) and includes a section on how to run a graphical program on a remote system and have the display appear locally. The section on GNOME describes several GNOME utilities, including the new Deskbar applet, 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 net- works, network security, and the Internet and discusses types of networks, subnets, protocols, addresses, hostnames, and various network utilities. The section on dis- tributed computing describes the client/server model and some of the servers you can use on a network. Chapter 11 goes into greater depth about shell programming 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. Part I has two sections that all readers should take a look at: “Conventions Used in This Book” (page 17), which explains the typographic and layout conventions used in this book, and “Where to Find Documentation” (page 124), which points out both local and remote sources of Linux and Ubuntu documentation.xlii Preface using bash, with the discussion enhanced by extensive examples. Details of setting up and using clients and servers are reserved until Part V. Part IV Part IV covers system administration. Chapter 12 discusses core concepts such as the use of sudo, working with root privileges, system operation, chroot jails, TCP wrappers, general information about how to set up a server, DHCP, and PAM. Chapter 13 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 14 explains how to keep a system up-to-date by downloading software from the Inter- net and installing it, including examples of using APT programs such as aptitude, apt-get, and apt-cache. It also covers the dpkg software packaging system and the use of some dpkg utilities. Finally, it explains how to use BitTorrent from the com- mand line to download files. Chapter 15 explains how to set up the CUPS printing system so you can print on both local and remote systems. Chapter 16 details cus- tomizing and building a Linux kernel. Chapter 17 covers additional administration tasks, including setting up user accounts, backing up files, scheduling automated tasks, tracking disk usage, and solving general problems. Chapter 18 explains how to set up a local area network (LAN), including both hardware (including wireless) and software configuration. Part V Part V goes into detail about setting up and running servers and connecting to them with clients. Where appropriate, these chapters include JumpStart sections that 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 com- municate 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. • Mail Configure exim4 and use Webmail, POP3, or IMAP to retrieve email; use SpamAssassin to combat spam. • NIS Set up NIS to facilitate system administration of a LAN. • 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. • firestarter and iptables Share a single Internet connection between systems on a LAN, run a DHCP server, and set up a firewall to protect local systems. • Apache Set up an HTTP server that serves Web pages that browsers can display. This chapter includes many suggestions for increasing Apache security.Thanks xliii Part VI Part VI includes appendixes on regular expressions, helpful Web sites, system secu- rity, and free software. This part also includes an extensive glossary with more than 500 entries plus a comprehensive index. 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 25 years of book writing experience: an editor who works with the tools I write about. Because Mark runs Ubuntu 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. Molly Sharp of ContentWorks worked with me day-by-day during production of this book providing help, listening to my rants, and keeping everything on track. Thanks to Jill Hobbs, Copyeditor, who made the book readable, understandable, and consistent; and Linda Seifert, Proofreader, who made each page sparkle. Thanks also to the folks at Prentice Hall who helped bring this book to life, espe- cially Julie Nahil, Full-Service Production Manager, who oversaw production of the book; John Fuller, Managing Editor, who kept the large view in check; Marie McKinley, Marketing Manager; Noreen Regina, Editorial Assistant, who attended to the many details involved in publishing this book, including keeping the review team on schedule (no small task); Heather Fox, Publicist; Dan Scherf, Media Developer; Sandra Schroeder, Design Manager; Chuti Prasertsith, Cover Designer; and everyone else who worked behind the scenes to make this book come into being. I am also indebted to Denis Howe, Editor of The Free On-line Dictionary of Com- puting (FOLDOC). Denis has graciously permitted me to use entries from his compilation. Be sure to look at this dictionary (www.foldoc.org). 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: David Chisnall, Swansea University; Scott Mann, Aztek Networks; Matthew Miller, Senior Systems Analyst/Administrator, BU Linux Project, Boston University Office of Information Technology; Georgexliv Preface Vish II, Senior Education Consultant, Hewlett-Packard; Thomas Achtemichuk, Mansueto Ventures; John Dong, Ubuntu Forum Council Member/Backports Team Leader; Scott James Remnant, Ubuntu Development Manager and Desktop Team Leader; 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 Univer- sity; Valerie Chau, Palomar College and Programmers Guild; James Kratzer; Sean McAllister; Nathan Eckenrode, New York Ubuntu Local Community Team; Christer Edwards; Nicolas Merline; and Michael Price. Thanks also to the following people who helped with my previous Linux books, which provided a foundation for this book: Chris Karr, Northwestern University; Jesse Keating, Fedora Project; Carsten Pfeiffer, Software Engineer and KDE Devel- oper; 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, Priva- teer 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, Look- smart; Matthew Whitworth; Dameon D. Welch-Abernathy, Nokia Systems; Josh Simon, Consultant; Stan Isaacs; and Dr. Eric H. Herrin II, Vice President, Herrin Software Development. And thanks to Doug Hughes, long-time system designer and administrator, who gave me a big hand with the sections on system administra- tion, networks, the Internet, and programming. More thanks go to consultants Lorraine Callahan and Steve Wampler; Ronald Hiller, Graburn Technology; Charles A. Plater, Wayne State University; Bob Palowoda; Tom Bialaski, Sun Microsystems; Roger Hartmuller, TIS Labs at Net- work 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 Ubuntu Linux® is based in part on two of my previous UNIX books: UNIX System V: A Practical Guide and A Practical Guide to the UNIX Sys- tem. 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, IntelThanks xlv 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, Vir- ginia 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. Finally, thanks to Peter and his family for providing nourishment and a very com- fortable place to work. I spent many hours reading the manuscript at JumpStart, Peter’s neighborhood coffee and sandwich shop. If you are in the neighborhood (24th & Guerrero in San Francisco), stop by and say “Hi.” 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 point- ers to interesting Linux pages on the Internet. Mark G. Sobell San Francisco, CaliforniaThis page intentionally left blank 11 1Chapter1The 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 Linux operating system, which was developed through the cooperation of many, many 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, documenta- tion, or support (via newsgroups, mailing lists, and other In This Chapter The GNU–Linux Connection . . . . . . . 2 The Linux 2.6 Kernel . . . . . . . . . . . . . 5 The Heritage of Linux: UNIX . . . . . . . 5 What Is So Good About Linux?. . . . . 6 Overview of Linux . . . . . . . . . . . . . . 10 Additional Features of Linux. . . . . . 15 Conventions Used in This Book . . . 17 1 Welcome to Linux2 Chapter 1 Welcome to Linux 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 GNU–Linux Connection 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 mem- ory, disk space, and CPU (page 1031) cycles—to all other programs that run on the computer. The system programs perform higher-level housekeeping tasks, often act- ing as servers in a client/server relationship. Linux is the name of the kernel that Linus Torvalds presented to the world in 1991 and that many others have worked on since then to enhance, stabilize, expand, and make more secure. The History of GNU–Linux This section presents some background on the relationship between GNU and Linux. 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 Mani- festo,2 which begins as follows: GNU, which stands for Gnu’s Not UNIX, is the name for the com- plete 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 men- tions the possibility of companies providing the service of distribu- tion for a profit. Subsequently I have learned to distinguish 1. www.gnu.org/gnu/initial-announcement.html 2. www.gnu.org/gnu/manifesto.htmlThe GNU–Linux Connection 3 carefully between “free” in the sense of freedom and “free” in the sense of price. Free software is software that users have the free- dom 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. 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 agree- ment 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 operat- ing 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 ker- nel, 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. 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.html4 Chapter 1 Welcome to Linux 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 GNU sys- tem. 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 died as UNIX was commercialized and manufacturers regarded 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, it 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 goes in the opposite direction. You can obtain Linux at no cost over the Internet (page 37). 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 Ubuntu Linux, that include installa- tion instructions, software, and support. GPL Linux and GNU software are distributed under the terms of the GNU General Pub- lic 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 6. See Appendix D or www.gnu.org/philosophy/free-sw.html. 7. www.gnu.org/gnu/linux-and-gnu.htmlThe Heritage of Linux: UNIX 5 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.) Have Fun! Two key words for Linux are “Have Fun!” These words pop up in prompts and doc- umentation. 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. The Linux 2.6 Kernel The Linux 2.6 kernel was released on December 17, 2003. This kernel has many features that offer increased security and speed. Some of these features benefit end users directly; others help developers produce better code and find problems more quickly. See Appendix E for a description of the features introduced in the Linux 2.6 kernel. The Heritage of Linux: UNIX The UNIX system was developed by researchers who needed a set of modern com- puting 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 became widely available in 1975, Bell Labs offered it to educational institu- tions at nominal cost. The schools, in turn, used it in their computer science pro- grams, 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 operat- ing system found its way into industry.6 Chapter 1 Welcome to Linux 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. What Is So Good About Linux? In recent years Linux has emerged as a powerful and innovative UNIX work-alike. Its popularity is surpassing 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. Applications 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 sup- porter. Linux conforms increasingly more closely to POSIX standards, and some distributions and parts of others meet this standard. (See “Standards” on page 9.) These developments indicate that Linux is becoming more mainstream and is respected as an attractive alternative to other popular operating systems. Peripherals 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. Software 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 include more than free software. Netscape, for example, has been available for Linux from the start and included Java support before it was available from many commercial vendors. Now its sibling Mozilla/Thunderbird/ Firefox is also a viable browser, mail client, and newsreader, performing many other functions as well.What Is So Good About Linux? 7 Platforms Linux is not just for Intel-based platforms: It has been ported to and runs on the Power PC—including Apple computers (ppclinux), Compaq’s (née Digital Equipment Corpo- ration) 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). It also includes an O(1) scheduler, which dramatically increases scalability on SMP systems. Emulators Linux supports programs, called emulators, that run code intended for other operat- ing 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 on top of the X Window System and UNIX/Linux; QEMU (fabrice.bellard.free.fr/qemu) is a CPU-only emulator that executes x86 Linux binaries on non-x86 Linux systems. 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 the others. In addition, Xen introduces minimal performance overhead when compared with running each of the operating systems natively. 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. You can also set up and test networks of systems on a single machine. Xen 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. The Gutsy release of Ubuntu supports Xen 3.1. This book does not cover the installation or use of Xen. See help.ubuntu.com/community/Xen for information on running Xen under Ubuntu. For more information on Xen, refer to the wiki at wiki.xensource.com/xenwiki and the Xen home page at www.cl.cam.ac.uk/research/srg/netos/xen. KVM and VirtualBox If you want to run a virtual instance of Windows, you may want to investigate KVM (Kernel Virtual Machine, help.ubuntu.com/community/KVM) and VirtualBox (www.virtualbox.org). 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 inex- pensive memory, and lower-priced hard disk storage have allowed hardware com- panies to install multiuser operating systems on desktop computers.8 Chapter 1 Welcome to Linux 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 that they can easily adapt to their machines. Generic operating systems 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 (as they have to pay to Microsoft for each copy of Windows they sell), manufacturers are much better off. In turn, software developers need to keep the prices of their products down; they cannot afford to convert 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 propri- etary as manufacturers added support for their own specialized features and intro- duced new software libraries and utilities. Linux emerged to serve both needs: It is a generic operating system that takes advantage of available hardware power. 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 lan- guage, 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 impor- tant, 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 cen- tral 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 get- ting faster all the time, at about the same price point. Linux on a fast microcom- puter has become good enough to displace workstations on many desktops. Linux 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 soft- ware environment.What Is So Good About Linux? 9 The advent of a standard operating system has aided the development of the soft- ware industry. Now software manufacturers can afford to make one version of a product available on machines from different manufacturers. Standards Individuals from companies throughout the computer industry have joined together to develop the POSIX (Portable Operating System Interface for Computer Environ- ments) standard, which is based largely on the UNIX System V Interface Definition (SVID) and other earlier standardization efforts. These efforts have been spurred by the U.S. government, which needs a standard computing environment to minimize its training and procurement costs. Now that these standards are gaining accep- tance, software developers are able to develop applications that run on all conform- ing versions of UNIX, Linux, and other operating systems. The C Programming Language Ken Thompson wrote the UNIX operating system in 1969 in PDP-7 assembly lan- guage. Assembly language is machine dependent: Programs written in assembly language work on only one machine or, at best, on one family of machines. The original UNIX operating system therefore 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. 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 com- puter 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 get code that would run well (that is, quickly—although an assembler will always generate more efficient code than a high-level language). C is a good systems language. You can write a compiler or an operating system in C. It is highly structured but is not necessarily a high-level language. C allows a program- mer to manipulate bits and bytes, as is necessary when writing an operating system. But it also has high-level constructs that allow for efficient, modular programming. In the late 1980s the American National Standards Institute (ANSI) defined a stan- dard 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.10 Chapter 1 Welcome to Linux Another researcher at Bell Labs, Bjarne Stroustrup, created an object-oriented pro- gramming 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. Ubuntu Linux From its first release in October 2004, Ubuntu has been a community-oriented project. Ubuntu maintains several structures to keep it functioning effectively, with community members invited to participate in all structures. For more information about Ubuntu governance, see www.ubuntu.com/community/processes/governance. Ubuntu Linux, which is sponsored by Canonical Ltd. (www.canonical.com), is based on the Debian Linux and focuses on enhancing usability, accessibility, and internationalization. Although Ubuntu initially targeted the desktop user, recent releases have put increasing emphasis on the server market. With a new release scheduled every six months, Ubuntu provides cutting-edge software. An Ubuntu system uses the GNOME desktop manager (www.gnome.org) and includes the OpenOffice.org suite of productivity tools, the Firefox Web browser, the Pidgin (formerly Gaim) IM client, and an assortment of tools and games. To keep software on a system up-to-date, Ubuntu uses Debian’s deb package format and various APT-based tools. Ubuntu distributes and supports many versions of its Linux distribution. For example, Kubuntu (www.kubuntu.org) runs the KDE desk- top manager, Edubuntu (www.edubuntu.org) includes many school-related applica- tions, and Xubuntu (www.xubuntu.org) runs the lightweight Xfce desktop, which makes it ideal for older, slower machines. For more information about Ubuntu, see www.ubuntu.com/aboutus/faq. 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 allo- cating the computer’s resources and scheduling user jobs so that each one gets its fair share of system resources, including access to the CPU; peripheral devices, such as hard disk, DVD, and CD-ROM storage; printers; and tape drives. 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 ofOverview of Linux 11 devices. For example, there is one write() system call, not 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 rewrit- ing them (provided that the new version recognizes the same system calls). See page 1015 for information on the Linux 2.6 kernel. Linux Can Support Many Users Depending on the hardware and the types of tasks that the computer performs, a Linux system can support from 1 to more than 1,000 users, each concurrently run- ning 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 ter- minals 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 pri- mary 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 15), you can run different programs in different windows on the same screen and watch all of them. This capability ensures that users can be more productive. Figure 1-1 A layered view of the Linux operating system Compilers Database Management Systems Word Mail and Message Facilities ShellsProcessors Hardware Linux Kernel12 Chapter 1 Welcome to Linux Linux Provides a Secure Hierarchical Filesystem A file is a collection of information, such as text for a memo or report, an accumu- lation 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 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 develop- ers, a group got together over the Internet and developed the Linux Filesystem Stan- dard (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 know where to expect to find any given standard program (page 194). Links A link allows a given file to be accessed by means of two or more names. The alter- native 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 328). Security 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 cer- tain other users by means of a simple but effective protection scheme. This level of secu- rity is provided by file access permissions, which limit which users can read from, write to, or execute a file. More recently, Linux has implemented Access Control Lists (ACLs), which give users and administrators finer-grained control over file access permissions. 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 three most popular shells are • The Bourne Again Shell (bash), an enhanced version of the original Bourne Shell (the original UNIX shell). • 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.Overview of Linux 13 Because different users may prefer different shells, multiuser systems can have sev- eral 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 send- ing 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 with rather short commands, or to build with surprisingly little effort elaborate programs that perform highly com- plex 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 refer- ences, are a kind of shorthand: Rather than typing in complete filenames, users 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. Device-Independent Input and Output Redirection Devices (such as a printer or a terminal) and disk files appear as files to Linux pro- grams. 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. Figure 1-2 The Linux filesystem structure / etctmphome hlsjennyalex notesbin report log14 Chapter 1 Welcome to Linux Device independence In a similar manner, a program’s input that 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. 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, include shell functions that the shell holds in memory so it does not have to read them from the disk each time you exe- cute them. The shell also keeps functions in an internal format so that 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 fre- quently run in the foreground so it is connected to the terminal. Using job control, you can move the job you are working with into the background and continue run- ning it there while working on or observing another job in the foreground. If a background job then needs your attention, you can move it into the foreground so that 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 numeri- cal order and can be used to sort lists by part number, last name, city, ZIP code, tele- phone number, age, size, cost, and so forth. The sort utility is an important programming tool and 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 documenta- tion for Linux itself. Interprocess Communication Pipes and filters Linux allows 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 specialAdditional Features of Linux 15 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 cat utility to sort (a filter) and then use another pipe to send the output of sort 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 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 may be to set up the system and install the software. 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, and taking care of any problems that arise. Additional Features of Linux The developers of Linux included features from BSD, System V, and Sun Microsys- tems’ 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, dis- play graphical information, or use special-purpose applications to draw pictures, monitor processes, or preview formatted output. X is an across-the-network proto- col 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 under X: a desktop manager and a window manager. A desktop manager is a picture-oriented user interface that enables you to interact with system programs by manipulating icons instead of typing the corresponding16 Chapter 1 Welcome to Linux commands to a shell. Ubuntu runs GNOME (Figure 1-3; 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. 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 desk- tops, create menus, and more. Several popular window managers run under X and Linux. Ubuntu Linux provides both Metacity (the default under GNOME) and kwin (the default under KDE). 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 Figure 1-3 A GNOME workspaceConventions Used in This Book 17 manner, copy files back and forth, run programs on remote systems while display- ing 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 dis- cusses networks, the Internet, and the Linux network facilities. Software Development One of Linux’s most impressive strengths is its rich software development environ- ment. You can find 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 infor- mation). 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 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 pro- gram’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 B-4 on page 987 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 fol- lowing 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 (click the box to put a tick in it; click again to remove the tick).” See the glossary for definitions of various widgets. Tabs and frames Tabs allow windows to display sets of related information, one set at a time. For example, Figure 4-11 on page 102 shows the Appearance Preferences window, which has five tabs; the Background tab is highlighted. A frame isolates a set of information within a window. Figure 4-11 shows the Wallpaper frame, which allows you to select one of several wallpapers. Menu selection path The menu selection path is the name of the menu or the location of the menu, fol- lowed by a colon, a SPACE, and the menu selections separated by D markers. The entire18 Chapter 1 Welcome to Linux menu selection path is in bold type. You can read Main menu: SystemDPreferencesD Appearance as “From the Main menu, select System; from System, select Preferences; and then select Appearance.” Text and examples 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. 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 pre- vious 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. Utility names Names of utilities are printed in this bold 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 give on the command line to invoke the utilities. Filenames Filenames appear in a bold typeface. Examples are memo5, letter.1283, and reports. Filenames may include uppercase and lowercase letters; however, Linux is case sen- sitive (page 1027), so memo5, MEMO5, and Memo5 name three different files. 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 delim- iters before and after a string. An example is the following string, which is displayed by the passwd utility: Sorry, passwords do not match. Buttons and labels Words appear in a bold typeface in the sections of the book that describe a GUI. This font indicates that 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. Keys and characters 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.) 8. Different keyboards use different keys to move the cursor (page 1032) to the beginning of the next line. This book always refers to the key that ends a line as the RETURN key. Your keyboard may 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.Conventions Used in This Book 19 Prompts and RETURNs Most examples include the shell prompt—the signal that Linux is waiting for a command—as a dollar sign ($), a pound sign (#), or sometimes a percent sign (%). The prompt is not in a bold typeface 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, give 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 exam- ples 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 chap- ter. This is an optional paragraph. URLs (Web addresses) Web addresses, or URLs, have an implicit http:// prefix, unless ftp:// or https:// 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. Tip, caution, and security boxes The following boxes highlight information that may be helpful while you are using or administrating a Linux system. This is a tip box tip A tip box may help you avoid repeating a common mistake or may 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 for system adminis- trators, but some apply to all users.20 Chapter 1 Welcome to Linux 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 pro- grammers 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. 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?21 I PART I Installing Ubuntu Linux CHAPTER 2 Installation Overview 23 CHAPTER 3 Step-by-Step Installation 45This page intentionally left blank 2323 2Chapter2Installing Ubuntu Linux is the process of copying operating sys- tem files from a CD or DVD to hard drive(s) on a system and setting up configuration files so that Linux runs properly on the hardware. Several types of installations are possible, including fresh installations, upgrades from older releases of Ubuntu Linux, 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 may be helpful for installation and administration. Chapter 3 covers the process of installing Ubuntu. The ubiquity utility is a user-friendly, graphical tool that installs Ubuntu. To install Ubuntu Linux on standard hardware, you can typically insert the live/install Desktop CD or DVD, boot the sys- tem, and double-click Install. After you answer a few questions, you are done. However, you may want to customize the system or you may be installing on nonstandard hardware: the installer gives you choices as the installation process unfolds. Ubuntu also provides a textual installer that gives you more control over the In This Chapter More Information . . . . . . . . . . . . . . 24 Planning the Installation . . . . . . . . 25 Setting Up the Hard Disk . . . . . . . . 30 LVM: Logical Volume Manager . . . . 35 The Installation Process . . . . . . . . . 36 Downloading and Burning a CD/DVD . . . . . . . . . . . . . . . . . . . 37 Using BitTorrent . . . . . . . . . . . . . . . 39 Gathering Information About the System . . . . . . . . . . . . . . . . . . 41 2 Installation Overview24 Chapter 2 Installation Overview installation. Refer to “Basic Installation from the Live/Install Desktop CD/DVD” (page 46) and “Advanced Installation” (page 62) for information about installing and customizing Ubuntu Linux. The Live/Install Desktop CD/DVD A live/install Desktop CD/DVD runs Ubuntu without installing it on the system. When you boot a live/install Desktop CD/DVD, it brings up a GNOME desktop: You are running a live session. When you exit from the live session, the system is as it was before you booted from the CD/DVD. If the system has a swap partition (most Linux systems have one; see page 32), the live session uses it to improve its performance but does not otherwise write to the hard disk. You can also install Ubuntu from a live session. Booting a live/install Desktop CD/DVD 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 Ubuntu or Linux and want to experiment with Ubuntu but are not ready to install Ubuntu on their system. More Information In addition to the following references, see “Where to Find Documentation” on page 124 and refer to Appendix B for additional resources. Web memtest86+ www.memtest.org gparted (GNOME Partition Editor) gparted.sourceforge.net Hardware compatibility wiki.ubuntu.com/HardwareSupport Swap space help.ubuntu.com/community/SwapFaq Partition HOWTO tldp.org/HOWTO/Partition Upgrading www.ubuntu.com/getubuntu/upgrading Boot command line parameters help.ubuntu.com/community/BootOptions and The Linux BootPrompt-HowTo RAID en.wikipedia.org/wiki/RAID LVM Resource Page (includes many links) sourceware.org/lvm2 LVM HOWTO www.tldp.org/HOWTO/LVM-HOWTO BitTorrent help.ubuntu.com/community/BitTorrent BitTorrent azureus.sourceforge.net X.org release information wiki.x.org Download Ubuntu Easiest download www.ubuntu.com/getubuntu Released versions releases.ubuntu.com Older versions old-releases.ubuntu.com/releases Development images and unsupported releases cdimage.ubuntu.com Mac (PowerPC) wiki.ubuntu.com/PowerPCDownloads BitTorrent torrent files torrent.ubuntu.com/releasesPlanning the Installation 25 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 Ubuntu, 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 Ubuntu Linux and fresh installations versus upgrades. Considerations GUI On most systems, except for servers, you probably want to install a graphical user interface (a desktop). Ubuntu installs GNOME by default. See page 60 for informa- tion about installing KDE. Software and services 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. Additional services can also slow the system down. For a system to learn on, or for a development system, additional packages and services may be useful. However, for a more secure production system, it is best to install and maintain the minimum number of packages required and enable only needed services. See page 507 for information on starting and stopping system services. Requirements Hardware This chapter and Chapter 3 cover installing Ubuntu on 32-bit Intel and compatible processor architectures such as AMD as well as 64-bit processor architectures such as AMD64 processors and Intel processors with Intel EM64T technology. Within these processor architectures, Ubuntu Linux runs on much of the available hard- ware. You can view Ubuntu’s list of compatible and supported hardware at wiki.ubuntu.com/HardwareSupport. Many Internet sites discuss Linux hardware; use Google (www.google.com/linux) to search for linux hardware, ubuntu hard- ware, or linux and the specific hardware you want more information on (for exam- ple, linux sata or linux a8n). In addition, many HOWTOs cover specific hardware. There is also a Linux Hardware Compatibility HOWTO, although it becomes dated rather quickly. Ubuntu Linux usually runs on systems that Windows runs on, unless the system includes a very new or unusual component. The hardware required to run Ubuntu depends on what 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, has many installed packages, and supports visual effects (page 103). Use the Alternate CD (page 28) if you are installing Ubuntu on a system with less than 320 megabytes of RAM. If you want to run visual effects on the system, see gentoo-wiki.com/HARDWARE_Video_Card_Support_Under_XGL for a list of sup- ported graphics cards.26 Chapter 2 Installation Overview A network connection is invaluable for keeping Ubuntu up-to-date. A sound card is nice to have for multimedia applications. If you are installing Ubuntu on old or minimal hardware and want to run a GUI, consider installing Xubuntu (www.xubuntu.org), as it provides a lightweight desktop and uses system resources more efficiently than Ubuntu does. RAM (memory) An extremely minimal textual (command line) system requires 32 megabytes of RAM. A standard desktop system requires 320 megabytes, although you may be able to use less if you install Xubuntu. Installing Ubuntu from a live session requires 320 megabytes. Use the textual installer (page 67) if the system has less than 320 megabytes 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. CPU Ubuntu Linux requires a minimum of a 200-megahertz Pentium-class processor or the equivalent AMD or other processor for textual mode and at least a 400-megahertz Pentium II processor or the equivalent for graphical mode. Hard disk space The amount of hard disk space Ubuntu requires depends on which edition of Ubuntu Linux you install, which packages you install, how many languages you install, and how much space you need for user data (your files). The operating system typically requires 2–8 gigabytes, although a minimal system can make due with much less space. Installing Ubuntu from a live session requires 4 gigabytes of space on a hard disk. BIOS setup Modern computers can be set to boot from a CD/DVD or hard disk. The BIOS determines the order in which the system tries to boot from each device. You may 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. CMOS 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 move the cursor to the screen and line that deal with boot- ing the system. Generally there is a list of three or four devices that the system tries to boot from; if the first attempt fails, the system tries the second device, and so on. Manipulate the list so that the CD/DVD is the first choice, save the list, and reboot. Refer to the hardware/BIOS manual for more information. Processor Architecture Ubuntu 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 Ubuntu is compiled for. See help.ubuntu.com/community/ProcessorArch for a detailed list of processors in each architecture. Because Linux source code is available to everyone, a knowledgeable user can compile Ubuntu Linux to run on other processor architectures.Planning the Installation 27 PC (Intel x86) Software on an Ubuntu PC (Intel x86) CD/DVD is compiled to run on Intel x86- compatible processors, including most machines with Intel and AMD processors, almost all machines that run MS Windows, and newer Apple Macintosh machines that use Intel processors. If you are not sure which type of processor a machine has, assume it has this type of processor. 64-bit PC (AMD64) Software on an Ubuntu 64-bit PC (AMD64) 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. Because some fea- tures of proprietary third-party applications are not available for 64-bit architec- ture, you may want to run Ubuntu compiled for a 32-bit (Intel x86) processor on a system with a 64-bit processor. SPARC Software on an Ubuntu SPARC CD (there is no DVD for this architecture) is com- piled to run on UltraSPARC machines, including those based on the multicore UltraSPARC T1 (Niagara) processors. Mac (PowerPC) Ubuntu does not officially support the PowerPC, but there is extensive community support for this processor architecture. See wiki.ubuntu.com/PowerPCFAQ for more information about running Ubuntu on a PowerPC. You can download PowerPC ver- sions of Ubuntu from wiki.ubuntu.com/PowerPCDownloads. Interfaces: Installer and Installed System When you install Ubuntu, you have a choice of interfaces to use while you install it (to work with the installer) and a choice of interfaces to use to work with the installed system. This section describes the two basic interfaces: textual and graphical. Textual (CLI) A textual interface, also called a command line interface (CLI) or character-based interface, displays characters and some simple graphical symbols. It is line oriented; you give it instructions using a keyboard only. Graphical (GUI) 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 114). A GUI uses more computer resources (CPU time and memory) than a textual interface does. Pseudographical A pseudographical interface is a textual interface that takes advantage of graphical ele- ments on a text-based display device such as a terminal. It may also use color. This interface uses text elements, including simple graphical symbols, to draw rudimentary boxes that emulate GUI windows and buttons. The TAB key frequently moves the cursor from one element to the next and the RETURN key selects the element the cursor is on. Advantages 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 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.28 Chapter 2 Installation Overview Installer interfaces Ubuntu provides a user-friendly, graphical installer (ubiquity) and an efficient, pseudographical installer that 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 Ubuntu. Ubuntu Releases Ubuntu distributes a new release about every six months. Each release has both a number and a name. In sequence, recent releases are 6.06 (Dapper Drake), 6.10 (Edgy Eft), 7.04 (Feisty Fawn), and 7.10 (Gutsy Gibbon). Ubuntu supports (i.e., pro- vides updates for, including security updates) each release for at least 18 months. LTS releases Ubuntu supports releases of its operating system marked LTS (long-term support— Dapper is an LTS release) for three years for a desktop system and for five years for a server system. LTS releases are designed for people who are more interested in having a stable, unchanging operating system rather than the latest, fastest version. Large and corporate installations frequently fall into this category. You can install and upgrade an LTS release just as you would any other release. Ubuntu Editions Desktop CD The Desktop CD is a live/install CD (page 24); you can use it to boot into a live ses- sion. You can install Ubuntu from a live session (page 48). This CD is available for PC and 64-bit PC architectures (page 26), uses the graphical installer, and installs a graphical (desktop) Ubuntu system. Alternate CD The Alternate Install CD is not a live CD; it is for special installations only. It pre- sents more advanced installation options than the Desktop CD does. This CD is available for PC and 64-bit PC architectures (page 26), uses the textual installer, and installs an Ubuntu system that displays either a graphical or a textual interface. You can use this CD to Figure 2-1 Graphical (left) and textual (pseudographical, right) installersPlanning the Installation 29 • Upgrade from older releases of Ubuntu on systems without an Internet connection. • Rescue a broken system (page 67). • Install Ubuntu on systems with less than 256 megabytes of RAM. These systems may work best from a textual interface; they may not be able to run a graphical interface fast enough to be usable. • Set up RAID (page 34) and/or LVM (page 35) partitions. • Create preconfigured OEM systems. • Set up automated deployments (having the installer answer installation questions automatically; also called preseeding). Server CD The Server CD is not a live CD; it is for installation only. This CD is available for PC, 64-bit PC, and SPARC architectures (page 26). It uses the textual installer and installs an Ubuntu system that displays a textual interface (no desktop). During installation, the Server CD gives you the option of installing DNS and/or LAMP (Linux, Apache, MySQL, and PHP). A system installed using this CD has no open ports (page 383) and includes only software essential to a server. DVD The DVD is a live/install DVD (page 24); you can use it to boot into a live session. You can install Ubuntu from a live session (page 48). The DVD is available for PC and 64-bit PC architectures (page 26), uses the graphical or textual installer, and installs an Ubuntu system that displays either a graphical or a textual interface. The DVD includes all software packages supported by Ubuntu, not just those installed by default. It is an excellent resource for someone with a system that has no Internet connection. Installing a Fresh Copy or Upgrading an Existing Ubuntu 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 disk. Alternatively, you can perform a clean install on an existing system without overwriting data by setting up a dual-boot system (page 61). Upgrade An upgrade replaces the Linux kernel and utilities on an installed release of Ubuntu Linux with a newer 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. Because an upgrade preserves the desktop, an upgraded system may not display or take advantage of new features that a clean install would display. See page 59 for instructions on upgrading an Ubuntu system to a new release.30 Chapter 2 Installation Overview Setting Up the Hard Disk Formatting and free space Hard disks must be prepared in several ways so an operating system can write to and read from them. Low-level formatting is the first step in preparing a disk for use. Normally you do not need to low-level format a hard disk, as this task is done at the factory. 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. The area of the disk not occupied by partitions is called free space. A new disk has no partition table and no partitions. Under DOS/Windows, the term formatting means creating a filesystem on a parti- tion; see “Filesystems” below. Partitions A partition, or slice, is a logical section of a hard disk that has a device name, such as /dev/sda1, so you can address it separately from other sections. From a live ses- sion, and after you install Ubuntu, you can use the GNOME Partition Editor (page 53) to view and resize partitions on an existing system. During installation, you can use the ubiquity partitioner (pages 50 and 56) to create partitions. After installation, you can use parted (page 673) to manipulate partitions. See /dev on page 554 for more information on device names. Partition table 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 a partition table, you can no longer access information on the disk except by extraordinary means. Filesystems Before most programs can write to a partition, a data structure (page 1032), called a filesystem, needs to be written to the partition. When the Ubuntu installer creates a partition, it writes a filesystem to the partition. You can use the mkfs (make file- system; page 525) utility, which is similar to the DOS/Windows format utility, to manually create a filesystem on a partition. Table 13-1 on page 570 lists some common types of filesystems. Ubuntu Linux typically creates ext3 filesystems for data, whereas Windows uses FAT16, FAT32, and NTFS filesystems. Apple uses HFS (Hierarchical Filesystem) and HFS+. OS X uses either HFS+ or UFS. Under Linux, typical filesystem names are / (root), /boot, /var, /home, and /usr. Under DOS/Windows, filesystems are labeled C:, D:, and so on (sometimes a whole disk is a single partition). Different types of partitions can coexist on the same hard disk, including both Windows and Linux partitions. Under Linux, the fsck (file- system check; page 577) utility checks the integrity of filesystem data structures. Mount point A partition holds no information about where it will reside in a system’s directory structure. When you use the installer to create most partitions, you specify the name of a directory that Ubuntu associates with the partition. For example, you might create a partition and associate it with the /var directory. The location you specify is the mount point for the partition. As part of the boot process, Ubuntu consults the fstab (filesystem table; page 576) file which associates each partition with its mount point. This association, called mounting, enables you to access the filesystem on a partition using the name of the directory it is mounted on.Planning the Installation 31 For example, the second partition on the first hard disk, with the device name /dev/sda2, might hold the filesystem that normally is mounted on the /home direc- tory. This association is normal, but not mandatory. When you work in recovery mode, you may mount this filesystem on the /target directory so you can repair the filesystem. A partition is frequently referred to by the name of its normal mount point: Thus “the /home partition” refers to the partition that holds the filesystem normally mounted on the /home directory. See page 572 for more information on mount points. Filesystem independence The state of one filesystem does not affect other filesystems: One filesystem on a drive may be corrupt and unreadable while other filesystems function normally. One filesystem may be full so you cannot write to it while others have plenty of room for more data. 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. 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 par- titions are the sizes you want the final partitions to be. The extended partition occu- pies the rest of the disk. Once you establish the extended partition, you can subdivide it into additional logical partitions (numbered 5 or greater) that are each the size you want. You cannot use the extended partition (number 4, above), only the logical partitions it holds. Figure 17-5 on page 674 illustrates the disk described in this paragraph. Partitioning a Disk During installation, the installer calls a partitioner to set up disk partitions. This sec- tion discusses how to plan partition sizes. Although this section uses the term parti- tion, planning and sizing LVs (logical volumes; page 35) works the same way. For more information refer to pages 53 and 56 and to the Linux Partition HOWTO at www.tldp.org/HOWTO/Partition. Planning Partitions Guided partitioning It can be difficult to plan partition sizes appropriately if you are not familiar with Linux. For this reason Ubuntu provides guided partitioning. Without asking any questions, guided partitioning divides the portion of the disk allotted to Ubuntu32 Chapter 2 Installation Overview into two partitions. One partition is the swap partition, which can be any size from 512 megabytes to 2 or more gigabytes. The other partition is designated as / (root) and contains the remainder of the disk space. Having only two partitions makes managing disk space quite easy. But if a program runs amok or if the system is sub- jected to a DoS attack (page 1034), the entire disk can fill up. System accounting and logging information, which may contain data that can tell you what went wrong, may be lost. Partition Suggestions A Linux system must have a / (root) partition. It is advisable to set up a swap par- tition as well. You can create additional partition/mount point pairs; this section lists some of the more common ones. Any standard directories you do not create partitions/mount points for automatically become subdirectories of the / (root) directory and reside on the / (root) partition. For example, if you do not create a partition to hold the /home filesystem, the installer creates home as a subdirectory of / and the home directory resides on the / (root) partition. (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 size of the swap partition should be between one and two times the size of the RAM in the system, with a minimum size of 256 megabytes. For example, a system with 1 gigabyte of RAM should have a 1- to 2-gigabyte swap partition. Although a swap partition is not required, most systems perform better with one. A swap partition is not mounted so it is not associated with a mount point. See swap on page 564 for more information. /boot This partition holds the kernel and other data the system needs when it boots. The /boot partition is typically about 100 megabytes, although the amount of space required depends on how many kernel images you want to keep on hand. This partition can be as small as 50 megabytes. Although you can omit the /boot parti- tion, 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 added overhead of the ext3 journal. Some older BIOSs require the /boot partition [or the / (root) partition if there is no /boot partition] to appear near the begin- ning of the disk. Where to put the /boot partition tip On older systems, the /boot partition must reside completely below cylinder 1023 of the hard disk. When a system has more than one hard disk, the /boot partition must also reside on a drive on: • 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 0Planning the Installation 33 /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. In this way, if a user runs a job that consumes all of the user’s disk space, system logs will not be affected. The /var par- tition can occupy from 500 megabytes up to several gigabytes for extremely active systems with many verbose daemons and a lot of printer activity (files in the print queue are stored on /var). Systems that are license servers for licensed software often qualify as extremely active systems. By default, Apache content (Web pages it serves) is stored on /var under Ubuntu. /home It is a common strategy to put user home directories on their own disk or partition. This partition is usually named /home. Having /home in a separate partition allows you to perform a clean install without overwriting user files. / (root) Some administrators choose to separate the / (root), /boot, and /usr partitions. When you have Ubuntu decide how to partition the disk (guided partitioning), it puts all directories in the root partition. By itself, the root partition usually con- sumes less than 30 megabytes of disk space. However, /lib, which can consume more than 300 megabytes, is part of the root partition. On occasion, you may install a special program that has many kernel drivers that consume a lot of space in the root partition. Allot 1 gigabyte to the root partition at a minimum. /usr Separating the /usr partition can be useful if you plan to export /usr to another sys- tem 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 added overhead of the ext3 journal. The size of /usr depends on the number of packages you install. On a default system, it is typically 2–4 gigabytes. /usr/local and /opt Both /usr/local and /opt are candidates for separation. If you plan to install many packages in addition to Ubuntu Linux, you may 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 may encroach on the users’ disk space. Many sites keep all /usr/local or /opt software on one server and export it 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 (next page) gives guidelines for minimum sizes for partitions used by Linux. Set the sizes of other partitions, such as /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) or wait until later to divide the space into partitions. Set up partitions to aid in making backups tip Plan partitions around 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.34 Chapter 2 Installation Overview 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 per- formance. Applications and utilities see these multiple drives/partitions as a single logical device. RAID, which can be implemented in hardware or software (Ubuntu 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 hard- ware failure. Although it can improve disk performance by increasing read/write speed, software RAID uses quite a bit of CPU time, which may 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 can be an effective addition to a backup. Ubuntu offers RAID software that you can install either when you install an Ubuntu 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 con- trollers, but it also works with the less expensive ATA disks as well as SCSI disks. Table 2-1 Example minimum partition sizesa Partition Example size /boot 50–100 megabytes / (root) 1 gigabyte (swap) One to two times the amount of RAM in the system with a minimum of 256 megabytes /home As large as necessary; depends on the number of users and the type of work they do /tmp Minimum of 500 megabytes /usr Minimum of 2–16 gigabytes, depending on which and how many software packages you install /var Minimum of 500 megabytes 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 the system programs. Do not replace backups with RAID caution Do not use RAID as a replacement for regular backups. If the system undergoes a catastrophic failure, RAID is useless. Earthquake, fire, theft, and other disasters may leave the entire system inaccessible (if the hard disks are destroyed or missing). RAID also does not take care of the simple case of replac- ing 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.Planning the Installation 35 Fake RAID Ubuntu provides support for motherboard-based RAID (known as fake RAID) through the dmraid driver set. Linux software RAID is almost always better than fake RAID. For more information see help.ubuntu.com/community/FakeRaidHowto. The partitioner on the Alternate CD gives you the choice of implementing RAID level 0, 1, or 5: • 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 par- titions or disks. • RAID level 5 (disk striping with parity) Provides redundancy and improves (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). For more information refer to the Software-RAID HOWTO. 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 your needs change, you can make LVs 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 retroac- tively 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. Figure 2-2 LVM: Logical Volume Manager Disk A 40 GB Disk B 80 GB Disk C 40 GB Disk D 20 GB / (root) home var usr (swap) boot 40 + 80 + 40 + 20 GB (– boot partition) Physical volumes (PVs) Boot partition Volume group (VG) Logical volumes (LVs)36 Chapter 2 Installation Overview 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 parti- tions: 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 file- system to a larger 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 or need to upgrade PVs, LVM can move data to new PVs. To read more about LVM, refer to the resources listed on page 24. The Installation Process The following steps outline the process of installing Ubuntu Linux from a CD/DVD. See Chapter 3 for installation specifics. 1. Insert the installation CD/DVD in and reset the computer. The computer boots from the CD/DVD and displays the initial install screen (Figure 3-1, page 46). 2. You can press function keys to display options, select an item from the ini- tial install screen menu, and begin bringing up a live session or installing Ubuntu when you are ready. Or you can do nothing. A live/install Desktop CD/DVD starts to bring up the system after 30 seconds; an installation- only CD waits for you to select an item from the menu. One of the menu items checks the installation medium. 3. As part of the process of bringing up a live session or installing Ubuntu, Ubuntu Linux creates RAM disks (page 1056) that it uses in place of a 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/install Desktop CD/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 installa- tion at any point before the system warns you it is about to write to the hard disk (or you complete the installation). If you opt out before this point, the system is left in its original state. The RAM disks also allow a system booted from a live/install Desktop CD to leave the hard disk untouched. 4. The installer prompts you with questions about how you want to configure Ubuntu Linux. 5. When the installer is finished collecting information, it displays the Ready to install screen (Figure 3-7, page 53). When you click Install, it writes the operating system files to the hard disk.Downloading and Burning a CD/DVD 37 6. The installer prompts you to remove the CD/DVD and press RETURN; it then reboots the system. 7. The Ubuntu Linux system is ready for you to log in and use. Downloading and Burning a CD/DVD There are several ways to obtain an Ubuntu CD/DVD. Ubuntu makes available releases of Linux as CD and DVD ISO image files (named after the ISO9660 standard that defines the CD filesystem). This section describes how to download one of these images and burn a CD/DVD. You can also point a browser at shipit.ubuntu.com to display a Web page with links that enable you to request a free CD from Ubuntu or purchase a CD/DVD from a Web site. The Easy Way to Download a CD ISO Image File This section explains the easiest way to download a CD ISO image file. This tech- nique works in most situations; it is straightforward but limited. For example, it does not allow you to use BitTorrent to download the file nor does it download a DVD image. To begin, point a browser at www.ubuntu.com and click Download Now or Get Ubuntu. Select the release (page 28) and edition (page 28) you want to download. Then select the type of system you want to install it on (see “Processor Architec- ture” on page 26). Finally select a location from the drop-down list labeled Choose a location near you and click Start Download. 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 40. Other Ways to Download a CD/DVD ISO Image File This section explains how to download a release that is not listed on the Ubuntu download page or a DVD image, and how to download a torrent that enables you to use BitTorrent to download the ISO image file. See “Download Ubuntu” on page 24 for other locations you can download Ubuntu 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 down- load 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 the38 Chapter 2 Installation Overview 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 Ubuntu ISO image files and BitTorrent tor- rents. Some mirrors use HTTP while others use FTP; you can use a browser to download files from either. FTP and HTTP appear slightly different. Point a browser at www.ubuntu.com/getubuntu/downloadmirrors to locate a mirror site. Scroll through the list of mirror sites, find a site near you, and click that site’s URL. The browser displays a page similar to the one shown in Figure 2-3. Click any link on the page that includes the name or release number of the version of Ubuntu you want to install. The browser displays a page similar to the one shown in Figure 2-4. Downloading an ISO image file You can click the links at the top of the page, although there is usually a better selection of versions in the list of files at the bottom of the page. Click the number or name of the release you want to download (e.g., gutsy or 7.10). At this point, some sites display a page with two links: Parent Directory and release. If the browser displays this page, click release. The browser displays a page with the name and number of the release at the top, followed by a description of the different types of CDs. At the bottom of the page is a list of files, with each line showing the name of the file, the date it was created, its size, and a short description. Each filename is a link. The following two lines describe the Intel x86 desktop ISO image file for Gutsy (7.10) and the torrent file that enables you to use BitTorrent to download the same ISO image file. The ISO image file is almost 700 megabytes; the torrent file is 27 kilobytes. ubuntu-7.10-desktop-i386.iso ... 687M Desktop CD for PC (Intel x86) computers (standard download) ubuntu-7.10-desktop-i386.iso.torrent..27k Desktop CD for PC (Intel x86) computers (BitTorrent download) Figure 2-3 An Ubuntu mirror IDownloading and Burning a CD/DVD 39 Click the filename/link that specifies the release, edition, and architecture you want. For example, clicking gutsy-desktop-i386.iso downloads the CD ISO image for Gutsy (release 7.10) desktop (edition) for the i386 architecture. Save the file to the hard disk. Next, download the file named MD5SUMS.htm (at the top of the list) to the same directory. An easy way to save a file is to right-click it, select Save Link/Target As, and save the file to a directory with enough space for the file. See page 40 for an explanation of how to use the MD5SUMS.htm file to verify the ISO image file you download. Downloading a DVD To download a DVD ISO image file, go to cdimage.ubuntu.com/releases and follow the instructions under “Downloading an ISO image file.” You can identify DVD ISO image files by the string dvd in their names. Make sure you have room for the file on the hard disk: A DVD ISO image file occupies about 4 gigabytes. Using BitTorrent You can use BitTorrent to obtain an ISO image file. Because BitTorrent is avail- able for both Windows and Mac OS X (www.bittorrent.com), you can download and burn the Ubuntu CD/DVD under either of these operating systems. To Figure 2-4 An Ubuntu mirror II40 Chapter 2 Installation Overview download a torrent, point a browser at releases.ubuntu.com and click the file- name 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; follow the instructions under “Downloading an ISO image file” on page 38. You can then start downloading the file from the command line (page 604) or by clicking it in a File Browser such as Nautilus (page 96). Verifying an ISO Image File This section assumes you have an ISO image file and a MD5SUMS.htm file saved on the hard disk and explains how to verify that the ISO IMAGE file is correct. The MD5SUMS.htm file contains the MD5 (page 1047) sums for each of the available ISO image files. When you process a file using the md5sum utility, md5sum generates a number based on the file. If that number matches the corresponding number in the MD5SUMS.htm file, the downloaded file is correct. You can run the following commands from a terminal emulator: $ grep desktop-i386 MD5SUMS.htm;md5sum gutsy-desktop-i386.iso 198fc031e7e482514eb57a2a7890dcac *gutsy-desktop-i386.iso 198fc031e7e482514eb57a2a7890dcac gutsy-desktop-i386.iso Computing an MD5 sum for a large file takes a while. The two long strings that the preceding command displays must be identical: If they are not, you must download the file again. 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. The CD/DVD burning software you use has a special selection for burn- ing an ISO image. It has 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. 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 frequently use ftp (page 732) or, on a Linux system, Nautilus menubar: FileDPlacesDConnect to Server (page 263) in place of a browser to download the file. 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 burn- ing 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 Ubuntu Linux.Gathering Information About the System 41 Gathering Information About the System It is not difficult to install and bring up an Ubuntu Linux system. Nevertheless, the more you know about the process before you start, the easier it will be. The installa- tion software collects information about the system and can help you make deci- sions during the installation process. However, the system will work better when you know how you want your disk partitioned 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 that 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 you have. Newer equipment is more likely to report on itself than older equip- ment is. 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 may need information about. Get as much informa- tion on 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, may be built into the motherboard. •Hard disks. • Memory. You don’t need it for installation, but it is good to know. • SCSI interface card. • Network interface card (NIC). You must use 700-megabyte CD-ROM blanks tip When you burn an Ubuntu Linux CD from an ISO image, you must use a 700-megabyte blank. A 650-megabyte blank will not work because there is too much data to fit on it. Test the ISO 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 Ubuntu Linux. When you boot the system from the CD/DVD, Ubuntu gives you the option of checking the CD/DVD for defects (page 47). A bad file on a CD may not show up until you finish installing Ubuntu Linux and have it running. At that point, it may 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 want to perform one test only, test the CD/DVD.42 Chapter 2 Installation Overview • 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 538), 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/install Desktop CD runs a live Ubuntu session without installing Ubuntu on the system. You can install Ubuntu from a live session. Booting a live/install Desk- top CD is a good way to test hardware and fix a system that will not boot from the hard disk. Before you download or otherwise obtain an Ubuntu CD or DVD, make sure you are using medium that is appropriate to the hardware you are installing it on and to what the system will be used for. Ubuntu has three editions: Desktop (the most com- mon), Alternate (for special cases), and Server. The Ubuntu live DVD combines fea- tures of all three of these editions. When you install Ubuntu Linux, you copy operating system files from a CD or DVD to hard disk(s) on a system and set up configuration files so Linux runs prop- erly on the hardware. Operating system files are stored as CD or DVD ISO image files. You can use a Web browser 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 Ubuntu Linux. When you install Ubuntu, you can let the installer decide how to partition the hard disk (guided partitioning) or you can manually specify how you want to par- tition it.Advanced Exercises 43 Exercises 1. Briefly, what does the process of installing an operating system such as Ubuntu Linux 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. List two reasons why RAID cannot replace backups. 8. What are RAM disks? How are they used during installation? 9. What is MD5? How does it work to ensure that an ISO image file you download is correct?This page intentionally left blank 4545 3Chapter3Chapter 2 covered planning the installation of Ubuntu Linux: 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 install- ing Ubuntu. Frequently the installation is quite simple, espe- cially if you have done a good job of planning. Sometimes you may 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 Ubuntu, continue with Chapter 4, which covers getting started using the Ubuntu desktop. If you install a textual (command line) system, continue with Chapter 5. In This Chapter Basic Installation from the Live/Install Desktop CD/DVD . . . 46 The Ubuntu Graphical Installer . . . 48 Graphical Partitioners. . . . . . . . . . . 53 Setting Up a Dual-Boot System . . . 61 The live/Install Desktop CD: The Initial Install Screen . . . . . . . 62 The Alternate CD Initial Install Screen Menu . . . . . . . . . . . . . . . . 65 The Server CD Initial Install Screen Menu . . . . . . . . . . . . . . . . 66 The Ubuntu Textual Installer. . . . . . 67 Manual Partitioning . . . . . . . . . . . . 70 Setting Up a RAID Array . . . . . . . . . 73 The xorg.conf File . . . . . . . . . . . . . . 77 3 Step-by-Step Installation46 Chapter 3 Step-by-Step Installation Basic Installation from the Live/Install Desktop CD/DVD To begin installing Ubuntu from a live/install Desktop CD/DVD, insert the disk in the computer and boot the system. The system displays the initial install screen (Figure 3-1). Refer to “BIOS setup” on page 26 if the system does not boot from the CD/DVD. See “The Function Keys” on page 62 for information about changing the language, keyboard, and accessibility features the live session uses. The menu on the initial install screen differs depending on which edition of Ubuntu (page 28) you are installing; along the bottom of the screen, the labels for the func- tion keys remain the same. To the left of the menu, the live (desktop) CD/DVD dis- plays a counter that counts down from 30 to 1; when the counter reaches 0, Ubuntu boots the system. When you press a key (other than RETURN) before the counter reaches 0, the counter stops and the system waits for you to make a selection from the menu. The installation-only (Alternate and Server) CDs do not have a counter, but rather wait for you to make a selection. This section describes how to boot into a live session and how to install Ubuntu from that session. Booting the System Before Ubuntu can display a desktop from a live/install Desktop CD/DVD or install itself on a hard disk, the Ubuntu operating system must be read into memory Figure 3-1 The initial install screen for the live/install Desktop CDBasic Installation from the Live/Install Desktop CD/DVD 47 (booted). This process can take a few minutes on older, slower systems or systems with minimal RAM (memory). Each of the menu selections on the initial install screen, except the memory test, boots the system. Checking the CD/DVD for Defects Whether you burned your own CD/DVD, purchased it, or are using the one included with this book, it is a good idea to verify that the contents of the CD/DVD is correct. On the initial install screen, use the ARROW keys to highlight Check the CD for Defects (this selection checks DVDs too) and press RETURN. Checking the CD/DVD takes a few minutes—Ubuntu keeps you apprised of its progress. When Ubuntu finishes checking the CD/DVD, it displays the result of its testing. Press RETURN to redisplay the initial install screen. Live Session In most cases, you can boot Ubuntu to run a live session that displays a desktop with- out doing anything after you boot from the live/install Desktop CD/DVD: Ubuntu displays the initial install screen, counts down from 30, boots the system, displays the Ubuntu logo while an orange cursor moves back and forth on the progress bar, and finally displays the GNOME desktop (Figure 3-2). To speed up this process, you can Test the CD/DVD tip Testing the CD/DVD takes a few minutes but can save you much aggravation if the installation fails or you run into problems after installing Ubuntu owing to bad media. Figure 3-2 The GNOME desktop displayed by a live session Install object48 Chapter 3 Step-by-Step Installation press RETURN when Ubuntu displays the initial install screen. The first time you use a CD/DVD, it is a good idea to check it for defects (see the previous page). If you encounter problems with the display while you are bringing up the desktop from a live/install Desktop CD/DVD or during installation, reboot the system and select Start Ubuntu in safe graphics mode (page 62) from the initial install screen. If that does not work, install Ubuntu using the textual installer on the Alternate CD (page 65) or the DVD. The live/install Desktop CD/DVD gives you a chance to preview Ubuntu without installing it. Boot the live/install Desktop CD/DVD to begin a live session and work with Ubuntu as explained in Chapter 4. When you are finished, remove the CD/DVD and reboot the system. The system boots as it did before the live session. Because a live session does not write to the hard disk (other than using a swap parti- tion if one is available), none of the work you save will be available once you reboot. You can use Webmail or another method to transfer files you want to preserve to another system. optional Seeing What Is Going On If you are curious and want to see what Ubuntu is doing as it boots, remove quiet and splash from the boot command line (Figure 3-16, page 64): With the initial install screen displayed, press F6 to display the boot command line. Press BACKSPACE or DEL to back up and erase quiet and splash from the boot command line. If you have not added anything to this line, you can remove the two hyphens at the end of the line. If you have added to this line, use the BACK ARROW key to back up over—but not remove—whatever you added, the hyphens, and the SPACE on each side of them. Then remove quiet and splash. As Ubuntu boots, it displays information about what it is doing. Text scrolls on the screen, although sometimes too rapidly to read. The Ubuntu Graphical Installer ubiquity The ubiquity utility is a graphical installer, written mostly in Python, which installs Ubuntu from a live session. You can use the Alternate or Server CD or the DVD to install Ubuntu using the textual installer (page 67). Welcome screen To install Ubuntu from the live/install Desktop CD/DVD, start a live session and double-click (use the left mouse button) the object on the desktop labeled Install (Figure 3-2, page 47). After a few moments Ubuntu displays the Welcome screen of the Install window (Figure 3-3). This screen displays a welcome message and a query about which lan- guage you would like ubiquity to use. The language you choose will be the default Before you start, see what is on the hard disk tip Unless you are certain you are working with a new disk, or you are sure the data on the disk is of no value, it is a good idea to see what is on the disk before you start installing Ubuntu. You can use the GNOME Partition Editor to examine the contents of a hard disk. See page 53 for more information.Basic Installation from the Live/Install Desktop CD/DVD 49 language for the installed system; you can change this default once the system is installed (page 132). Using the Mouse to Work with the Install Window You can use the mouse or keyboard to make selections from the Install window screens. To select a language from the Welcome screen using the mouse, left-click the language you want to use in the list box at the left. If the language you want does not appear on the displayed portion of the list, click or drag the scrollbar (Figure 3-3) to display more languages; then click the language of your choice. Ubuntu highlights the language you click. Once you select a language, you are fin- ished working with the Welcome screen; click the Forward button to display the next screen. Using the Keyboard to Work with the Install Window To use the keyboard to make selections, first use the TAB key to move the highlight to the object you want to work with. On the Welcome screen, the objects are the selected item in the list box and the buttons labeled Release Notes, Cancel, and For- ward. With a language in the list box highlighted, use the UP ARROW and DOWN ARROW keys to move the highlight to the language you want to use. The list scrolls auto- matically when you move the highlight to the next, undisplayed entry in the list. See “F3 Keymap” on page 63 to change the layout of the keyboard ubiquity uses during installation. Once you select a language, you are finished working with the Welcome screen; use the TAB key to highlight the Forward button. The border of a button becomes thicker and darker when it is highlighted. With the Forward button highlighted, press RETURN to display the next screen. Figure 3-3 The Install window, Welcome screen Scrollbar Forward button List box50 Chapter 3 Step-by-Step Installation This book describes using the mouse to make selections from a graphical interface; you can use the keyboard if you prefer. Where are you? Next, ubiquity displays the Where are you? screen. This screen allows you to specify the time zone the computer is in. You can use the map or the drop-down list labeled Selected city to specify the time zone. Initially, when it is over the map, the mouse pointer appears a magnifying glass with a plus sign in it. Left-click the map near a city that is in the same time zone as the computer; the map zooms in on that area. Left-click again to choose a city; the name of the city appears in the box labeled Selected city. To use the drop-down list, click anywhere in the box labeled Selected city; ubiquity expands the box into a list of cities. Use the mouse or ARROW keys to select a city and then either click the city or press RETURN. Click Forward. Keyboard layout The Keyboard layout screen allows you to specify the type of keyboard to be used by the installed system. (See “F3 Keymap” on page 63 to change the layout of the keyboard ubiquity uses during installation.) Select the country you are in or the lan- guage you will be using with the installed system from the list box on the left. Then select the type of keyboard you will be using from the list box on the right. Click the empty text box near the bottom of the window and enter some characters to test your selection. Click Forward. The installer displays the Setting up the partitioner window while it gets ready for the next step. Prepare disk space The Prepare disk space screen controls how ubiquity partitions the hard disk. See page 31 for a discussion of some of the issues involved in partitioning a hard disk. With a single, clean hard disk—a hard disk with nothing installed on it, as it comes from the factory (i.e., no partition table)—the ubiquity partitioner displays a Prepare disk space screen similar to the one shown in Figure 3-4. In this case, the simplest way to partition the disk is to have ubiquity do it for you. By default, the radio button labeled Guided – use entire disk and the radio button next to the name of the only hard disk in the system are selected. If the system has two or more clean hard disks, the ubiquity partitioner displays a line for each hard disk; click the radio button next to the one you want to install Ubuntu on. Click Forward and ubiquity creates two partitions on the hard disk: a small swap partition (page 32) and a root partition (/, page 33) that occupies the rest of the disk. The installer displays the Starting Up the Partitioner window while it gets ready for the next step. If the Prepare disk space screen includes Guided - resize... and Guided - use the larg- est continuous free space selections, there is probably at least one partition on the Figure 3-4 The ubiquity partitioner showing one empty hard diskBasic Installation from the Live/Install Desktop CD/DVD 51 hard disk (there could just be an empty partition table). If you are sure you do not want to keep any of the information on the hard disk, you can select Guided - use entire disk. To find out more about what is on the disk, see the section on the Parti- tion Editor on page 53. For more information on guided partitioning, see page 56. For information on manual partitioning, see page 57. The ubiquity partitioner displays a warning window (Figure 3-5) if it is going to write to the hard disk before it displays the Ready to install screen (Figure 3-7, page 53). If it does not display this window, ubiquity will not make changes to the hard disk until you click Install on the Ready to install screen. Migrate documents and settings If you are installing Ubuntu on a system that already has one or more operating systems installed on it, and you are not overwriting those operating systems, the Migrate documents and settings screen displays a list of accounts and settings from the existing operating systems. For example, if you are creating a dual-boot system on a system that already has Windows installed on it, this screen shows the accounts from the Windows system and a list of programs and settings. It might show your name from the Windows system and, under that, Internet Explorer and My Documents. Put ticks in the check boxes adjacent to the items you want to migrate to the Ubuntu system. On the lower portion of the screen, enter the information necessary to create an Ubuntu user to receive the migrated information. Who are you? The Who are you? screen (Figure 3-6 on the next page) sets up the first Ubuntu user. This user can use sudo (page 88) to administer the system, including setting up addi- tional users (page 658). Enter the full name of the user in the text box labeled What is your name?. When you press RETURN, ubiquity enters the first name from the name you just entered in the box labeled What name do you want to use to log in?. Press RETURN to accept this name or backspace (page 137) over it and enter a different name. Enter the same password in the next two (adjacent) boxes. Although ubiquity accepts any password, it is a good idea to choose a more secure password if the sys- tem is connected to the Internet. See “Changing Your Password” on page 135 for a discussion of password security. The final text box specifies the name of the computer. For use on a local network and to connect to the Internet with a Web browser or other client, you can use a simple name such as tiny. If you are setting up a system that is to function as a server, see “FQDN” on page 847 for information on names that are valid on the Internet. Click Forward. Figure 3-5 Write to disk warning window52 Chapter 3 Step-by-Step Installation Ready to install The final screen ubiquity displays is the Ready to install screen (Figure 3-7). Unless ubiquity asked your permission to write to the hard disk during the partitioning phase of the installation, it has not written to the disk yet. If you click Cancel at this point, the hard disk will remain untouched. This screen summarizes your answers to the questions ubiquity asked in the previous screens. Click Advanced to display the Advanced Options window, which allows you to choose whether to install a boot loader (normally you want to) and whether you want the system to participate in an automatic, informal package usage survey. Click OK to close the Advanced Options window. If everything looks right in the summary in the Ready to install screen, click Install. The installer begins installing Ubuntu on the hard disk. The ubiquity installer displays a series of windows to keep you informed of its progress. When the new system is installed, Ubuntu displays the Installation Complete window, which gives you the choice of continuing the live session or rebooting the system so you can use the newly installed copy of Ubuntu. Click Restart now to reboot the system. Ubuntu displays its logo and a progress bar. When it has finished shutting down the system, it asks you to remove the disk (so you do not reboot from the live/install Desktop CD/DVD) and press RETURN. When you do so, it reboots the system and dis- plays the Ubuntu GNOME login screen (Figure 4-1, page 90). Figure 3-6 The Install window, Who are you? screen This is when ubiquity writes to the hard disk caution You can abort the installation by clicking the Cancel button at any point up to and including the Ready to install screen (Figure 3-7) without making any changes to the system. Once you click Next in this screen, ubiquity writes to the hard disk. However, if ubiquity displayed the warning window shown in Figure 3-5 on page 51 and you clicked Continue, it wrote to the hard disk at that time.Graphical Partitioners 53 Graphical Partitioners A partitioner displays and can add, delete, and modify partitions on a hard disk. This section describes two graphical partitioners you can use to configure a hard disk so you can install Ubuntu Linux. One partitioner, gparted, is available on the live/install Desktop CD desktop. The other partitioner is part of the ubiquity installer and is not available by itself. See page 57 for information on using the textual parti- tioner, which is available when you use the textual installer. After you install Ubuntu Linux, you can use parted (page 673) to manipulate partitions. If you want a basic set of partitions, you can allow ubiquity to partition the hard disk automati- cally using guided partitioning. See “Setting Up the Hard Disk” on page 30 for a discussion of free space, parti- tions, partition tables, and filesystems. “Partition Suggestions” on page 32 discusses some of the filesystems you may want to set up partitions for. gparted: the GNOME Partition Editor Unless you know the hard disk you are installing Ubuntu Linux 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. The GNOME Partition Editor (gparted), which is available from a live session, is a good Figure 3-7 The Install window, Ready to install screen54 Chapter 3 Step-by-Step Installation tool for this job. Open the Partition Editor window by selecting Main menu: Admin- istrationDPartition Editor as shown in Figure 3-8. The Partition Editor displays the layout of a hard disk and can resize partitions, such as when you are setting up a dual-boot system by adding Ubuntu to a Windows sys- tem (page 61). Although you can create partitions using the Partition Editor, you cannot specify the mount point (page 30) for a partition—this step must wait until you are installing Ubuntu and using the ubiquity partitioner. You can save time if you use the Partition Manager to examine a hard disk and the ubiquity partitioner to set up the partitions that you install Ubuntu on. An Empty Hard Disk The Partition Editor shows one large unallocated space for a new hard disk (empty, with no partition table). If you have more than one hard disk, use the list box in the upper-right corner of the screen to select which disk the Partition Editor displays information about. Figure 3-9 shows an empty 200-gigabyte hard disk on the device named /dev/sda. Figure 3-4 on page 50 shows the ubiquity partitioner ready to partition an empty drive similar to the one shown in Figure 3-9. Figure 3-8 Selecting the Partition Editor from the Main menu Figure 3-9 The Partition Editor displaying an empty disk driveGraphical Partitioners 55 Deleting a Partition Before deleting a partition, make sure it does not contain any data you need. To use the Partition Editor to delete a partition, highlight the partition you want to delete and click Delete and then Apply on the toolbar. Resizing a Partition Figure 3-10 shows the Partition Editor displaying information about a hard disk with a single partition that occupies the entire disk. It shows a single 200-gigabyte NTFS filesystem. 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 Ubuntu on this system, you must resize (shrink) the partition to make room for Ubuntu. Before you resize a Windows partition, you must boot Windows and defragment the partition using the Windows defragmenter; see the tip on page 61. Although you can resize a partition with the ubiquity partitioner while you are install- ing Ubuntu, it is easier to see what you are doing when you use the Partition Editor Figure 3-10 The Partition Editor displaying a disk drive holding a Windows system Always back up the data on a hard disk caution If you are installing Ubuntu on a disk that holds data that is important to you, always back up the data before you start the installation. Things can and do go wrong. The power may go out in the middle of an installation, corrupting the data on the hard disk. There may be a bug in the partition- ing software that destroys a filesystem. Although it is unlikely, you might make a mistake and for- mat a partition holding data you want to keep.56 Chapter 3 Step-by-Step Installation for this task. Also, the ubiquity partitioner will not resize a partition on a hard disk with more than one partition. To resize the partition, right-click to highlight the line that describes the partition and select Resize/Move on the toolbar. The Partition Edi- tor opens a small Resize/Move window, as shown in Figure 3-10 on page 55. At the top of the Resize/Move window is a graphical representation of the partition. Initially, the partition occupies the whole disk. The spin box labeled New Size (MiB) shows the number of megabytes occupied by the partition—in this case the whole disk. The two spin boxes labeled Free Space show no free space. Back up the partition before you begin resizing. You can specify how you want to resize the partition by (right-clicking and) dragging one of the triangles at the ends of the graphical representation of the partition or by entering the number of mega- bytes you want to shrink the Windows partition to in the spin box labeled New Size. The value in one of the spin boxes labeled Free Space increases. Click Resize/Move to add the resize operation to the list of pending operations at the bot- tom of the window. Click Apply on the toolbar to resize the partition. Although you can use the Partition Editor to create partitions to install Ubuntu on, it may be easier to create partitions using ubiquity while you are installing Ubuntu. The ubiquity partitioner allows you to specify mount points for the partitions; the Partition Editor does not. ubiquity: Setting Up Partitions While you are installing Ubuntu, ubiquity offers two ways to partition a disk: guided and manual. Guided partitioning sets up two partitions, one for swap space (page 32) and one for / (root, where the entire Ubuntu filesystem gets mounted; page 33). The amount of space occupied by root depends on which guided option you select. Manual partitioning enables you to set up partitions of any type and size, and to specify the mount point for each partition. Advanced Guided Partitioning “Prepare disk space” on page 50 explained how to use guided partitioning to parti- tion an empty disk. This section explains how guided partitioning works on a disk that is already partitioned. Installing Ubuntu on a partitioned disk gives you a few options: • You can install Ubuntu on the entire hard disk; by doing so you delete any information that was on the hard disk. • If the disk has enough free space, you can install Ubuntu in the free space, typically creating a dual-boot system. • If one partition has enough unused space, you can resize (shrink) the parti- tion, creating free space in the process. You can then install Ubuntu in the Graphical Partitioners 57 free space. It it easier to see what you are doing if you use the Partiton Editor to resize a partition before you begin installing Ubuntu. Also, the ubiquity partitioner will not resize a partition on a hard disk with more than one par- tition. For more information refer to “Resizing a Partition” on page 55. Figure 3-11 shows the Prepare disk space screen for a hard disk with one partition and some free space. If the disk had no free space, the partitioner would not display Guided - use the largest continuous free space. If the disk had two or more parti- tions, the partitioner would not give you the option of resizing a partition—you must use the Partition Editor to resize a partition on a hard disk with more than one partition. Using the whole disk for Ubuntu is easy. Before you start, make certain the disk does not contain any information you need. Once you rewrite the partition table, the data is gone for good. If you are not sure what is on the disk, use the Nautilus File Browser to take a look. (See page 96; select Main menu: PlacesDComputer and double-click one or more of the Filesystem objects.) To use guided partitioning to partition the whole disk, click the radio button labeled Guided - use entire disk and click Forward. To use the free space, select Guided - use the largest continuous free space and click Forward. If you want to use the ubiquity partitioner to resize a partition, click the radio button labeled Guided - resize ... and drag the slider until the numbers above it show the size you would like the new partition to be. Click Forward. Manual Partitioning This section explains how to use the ubiquity partitioner to create a partition on an empty hard disk. Figure 3-4 on page 50 shows the Prepare disk space screen for an empty hard disk. To create partitions manually, click the radio button labeled Man- ual and click Forward. The ubiquity partitioner displays a Prepare partitions screen that shows a device without any partitions. Before you can create partitions, you Figure 3-11 The Prepare disk space screen showing a hard disk with one partition and some free space58 Chapter 3 Step-by-Step Installation must set up a partition table (page 30): Highlight the device name (e.g., /dev/sda) and click New partition table. The partitioner asks you to confirm you want to cre- ate a new empty partition table. Click Continue to create a partition table that con- tains only free space. Now ubiquity displays a screen that looks similar to the one in Figure 3-12. The device (hard disk) at /dev/sda has a partition table without any partitions—it contains only free space. To create a partition, highlight the line with free space in the Device column and click New partition. The ubiquity partitioner displays a Create Partition window (Figure 3-13), which asks you to specify whether you want to create a primary or a logical partition (page 31), what size you want to make the partition (in mega- bytes), whether you want the partition to appear at the beginning or end of the free space, what type you want to make the partition (Use as), and the name of the mount point (page 30) for the partition. Because Linux does not mount a swap par- tition, you cannot specify a mount point for a type swap partition. If you are unsure of which type a partition should be, choose ext3 (page 571). Click OK. After a few moments the Prepare partitions screen displays the new partition (Figure 3-14). To create another partition, highlight free space and repeat the preced- ing steps. Remember to create a swap partition (page 32). When you have finished creating partitions, click Forward. The Prepare partitions screen displays two or three buttons immediately below the frame that lists the disks and partitions. The labels on these buttons change depend- ing on what is highlighted. This screen always displays a button labeled Undo Figure 3-12 An empty hard disk with a partition table Figure 3-13 The Create Partition windowUpgrading to a New Release 59 changes to partitions. When the device is highlighted, the Prepare partitions screen displays a New partition table button. Clicking this button creates a new partition table, destroying any existing partition table. Highlighting a partition gives you the choice of editing or deleting the partition. Editing a partition you just created allows you to change only its type and mount point. You must delete and recreate a parti- tion to change any of its other attributes. As mentioned earlier, highlighting free space allows you to create a new partition. Upgrading to a New Release Upgrading a system is the process of installing a new release of Ubuntu over an older one. All user and configuration files are preserved and all software is upgraded to the most recent version consistent with the new release of Ubuntu. Ubuntu cautions that you must not skip releases when you upgrade; doing so can cause irreparable damage to the system, requiring a clean install and potentially los- ing data. For example, you can upgrade from Feisty Fawn (7.04) to Gutsy Gibbon (7.10), but not from Edgy Eft (6.10) to Gutsy Gibbon. Ubuntu also advises against upgrading systems that have had packages installed from repositories (page 588) that it does not control. These packages may corrupt the software package data- base, causing the upgrade to fail. Before you upgrade a system, it is a good idea to back up all user files on the system. The following procedure assumes that you have a desktop system that is connected to the Internet. Even with a fast Internet connection, this process takes a long time. Follow these steps to upgrade a system: 1. Open the Update Manager window (Figure 4-10, page 101) by selecting Main menu: SystemDAdministrationDUpdate Manager. 2. Regardless of whether the window says You can install nn updates or not, click Check. This step ensures the software package database is up-to-date. 3. If the window displays You can install nn updates, click Install Updates. This step ensures all software packages on the system are up-to-date. Figure 3-14 The Prepare partitions screen displays a new partition60 Chapter 3 Step-by-Step Installation 4. At this point, if a new release is available, the window displays the message New distribution release 'X.XX' is available. Click Upgrade. 5. The utility displays the Release Notes window. Read the release notes and then click Upgrade. 6. The utility downloads the upgrade tool and updates some files. 7. You are asked if you want to start the upgrade. Click Start Upgrade. 8. When the upgrade is complete, reboot the system. See www.ubuntu.com/getubuntu/upgrading for instructions on upgrading other edi- tions of Ubuntu. Installing KDE You can install KDE in one of two ways. The first approach installs KDE only: Follow the instructions in Chapter 2 and this chapter but instead of downloading and burn- ing an Ubuntu CD/DVD, download a Kubuntu CD/DVD from www.kubuntu.org, burn it, and use that disk to install Linux. The second approach requires the system to be connected to the Internet and installs KDE in addition to GNOME. After you install Ubuntu as explained in this chapter, use Synaptic (page 121) or aptitude (page 592) to perform the following steps. This process takes a while; you will be downloading and installing more than 200 software packages. 1. Ensure the software package database is up-to-date: From Synaptic, click Reload. To use aptitude, give the command sudo aptitude update from a command line, terminal emulator, or Run Application window (ALT-F2). 2. Ensure all software packages on the system are up-to-date: From Synaptic, click Mark All Upgrades and then click Apply. To use aptitude, give the command sudo aptitude safe-upgrade from a command line, terminal emulator, or Run Application window (ALT-F2). 3. Install the KDE software: From Synaptic, search for and install the kubuntu-desktop virtual package (page 592). To use aptitude, give the command sudo aptitude install kubuntu-desktop from a command line, terminal emulator, or Run Application window (ALT-F2). After the software is downloaded, while it is being installed, debconf asks if you want to use the gdm (page 82, GNOME) or kdm (KDE) display manager. Either one works with either desktop. One way to choose which display manager to use is to select the one associated with the desktop you will be using most often. Once KDE is installed, reboot the system. From the Login screen, follow the instructions on page 132 to display the Action menu and select the session you want to run (GNOME or KDE).Setting Up a Dual-Boot System 61 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 Ubuntu to a system that can boot Windows, cre- ating a system that can boot Windows or Linux. You can use the same technique for adding Ubuntu to a system that runs a different version or distribution of Linux. One issue in setting up a dual-boot system is finding disk space for the new Ubuntu system. The next section discusses several ways to create the needed space. Creating Free Space on a Windows System Typically you install Ubuntu Linux in free space on a hard disk. To add Ubuntu Linux 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 fol- lowing paragraphs discuss these options in order from easiest to most difficult: Add a new disk drive Add another disk drive 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 disk drive. 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. Resize Windows partitions Windows partitions typically occupy the entire disk, making resizing a Windows partition the most common technique to free up space. Windows systems typically use NTFS, FAT32, and/or FAT16 filesystems. You can use the Ubuntu Partition Edi- tor to examine and resize an existing Windows partition to open up free space in which to install Linux (page 55). 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 move the data from the partition you will delete to another Windows partition. Once you are sure a partition contains no useful information, you can use the Parti- tion Editor to delete it (page 55). After deleting the partition, you can install Ubuntu Linux in the free space left by the partition you removed. Installing Ubuntu Linux as the Second Operating System After creating enough free space on a Windows system (see the previous section), you can install Ubuntu Linux. On the ubiquity Prepare disk space screen, select Always defragment before resizing caution You must boot Windows and defragment a Windows partition before you resize it. Sometimes you may 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 may also keep the process of setting up a dual-boot system from failing.62 Chapter 3 Step-by-Step Installation Guided - use the largest continuous free space. Or, if you are installing Ubuntu on its own hard disk, select Guided - use entire disk and click the radio button next to the disk you want to install Ubuntu on. Click Forward. When you boot from the hard disk, you will be able to choose which operating system you want to run. Advanced Installation This section discusses the live/install Desktop CD initial install screen, using the Alternate and Server CDs as well as the live/install DVD to install Ubuntu, and the Ubuntu textual installer. The Live/Install Desktop CD: The Initial Install Screen This section covers some of the things you can do from the initial install screen (Figure 3-1, page 46) other than simply booting to a live session. Menu Selections Start or install Ubuntu Boots to a live session (page 47). Start Ubuntu in safe graphics mode If you encounter problems with the display while you are bringing up a live session or during installation, choose this menu selection. It adds xforcevesa to the boot parameters, causing Ubuntu to use the generic vesa driver in place of the driver for the graphics chip in the system. The vesa driver is slow and does not support high resolutions, but it works with almost any graphics chip. Install with driver update CD Installs Ubuntu with an updated driver. OEM install (for manufacturers) Allows a manufacturer or reseller to preinstall Ubuntu but leave some configuration details, such as creating a user account, to the purchaser. Check CD for defects Verifies the contents of the CD/DVD you are booting from (page 47). Memory test 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. Boot from first hard disk Boots the system from the first hard disk. This selection frequently has the same effect as booting the system without the CD/DVD (depending on how the BIOS [page 26] is set up). The Function Keys Along the bottom of the initial install screen is a row of labeled function key names (Figure 3-1, page 46). Pressing these function keys displays information that may be helpful if you are having a problem booting Ubuntu or working in a live session. Some of the keys allow you to change boot parameters.Advanced Installation 63 F1 Help The F1 key displays the help window shown in Figure 3-15. Pressing a function key while this window is displayed displays yet another help window. Pressing a func- tion key when this window is not displayed has the effect described in the following paragraphs. Press ESCAPE to close the help window. F2 Language The F2 key displays a menu of languages. Use the ARROW keys to highlight the lan- guage you want the live session to use and then press RETURN. This language is not necessarily the language the installed system displays. F3 Keymap The F3 key displays a menu of countries and languages. Use the ARROW keys to high- light the country/language of the keyboard you want the live session to use; press RETURN. This keyboard is not necessarily the keyboard the installed system uses. F4 VGA The ubiquity installer normally detects the characteristics of the monitor attached to the system. The F4 key displays a list of monitor resolutions and color depths (page 1029). For example, 640 x 480 x 16 specifies a resolution of 640 x 480 dots per inch (dpi) and a color depth of 16. Use the ARROW keys to highlight the resolution and color depth you want the live session to use and then press RETURN. This selection is not necessarily the resolution and color depth the installed system uses. F5 Accessibility The F5 key displays a list of features, such as a high-contrast display and a Braille terminal, that can make Ubuntu more accessible for some people. Use the ARROW keys to highlight the feature you want the live session to use and then press RETURN. F6 Other Options The F6 key displays the boot command line (Figure 3-16 on the next page). Type the parameters you want to add to the boot command line (discussed in the next section) after the double hyphen and press RETURN to boot the system. If you remove quiet and splash from this line, Ubuntu displays what it is doing while it boots (page 48). Boot Command Line Parameters (Boot Options) Following are some of the parameters you can add to the boot command line (see “F6 Other Options” above). You can specify multiple parameters separated by SPACEs. Figure 3-15 Initial install screen, F1 help window64 Chapter 3 Step-by-Step Installation See help.ubuntu.com/community/BootOptions and The Linux BootPrompt-HowTo for more information. noacpi Disables ACPI (Advanced Configuration and Power Interface). Useful for systems that do not support ACPI or that have problems with their ACPI implementation. Also acpi=off. The default is to enable ACPI. noapic Disables APIC (Advanced Programmable Interrupt Controller). The default is to enable APIC. noapm Disables APM (Advanced Power Management). Also apm=off. The default is to enable APM. irqpoll Changes the way the kernel handles interrupts. nolapic Disables local APIC (above). The default is to enable local APIC. Virtual Consoles While it is running, ubiquity opens a shell on each of the six virtual consoles (also called virtual terminals; page 136). 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. At any time during the installation, you can switch to a virtual console and give shell commands to display information about processes and files. Do not give commands Figure 3-16 Initial install screen, F6 boot command line Boot command lineAdvanced Installation 65 that change any part of the installation process. To switch back to the graphical installation screen, press CONTROL-ALT-F7. To switch back to the textual (pseudographi- cal) installation screen, press CONTROL-ALT-F1. The Alternate CD Initial Install Screen Menu The Alternate CD uses the textual installer (page 67) to install a system that uses a graphical interface or one that uses a textual interface. It is not a live CD (does not bring up a desktop to install from), does not require as much RAM to install Ubuntu, and presents more installation options. The Alternate CD initial install screen takes advantage of the functions keys described on page 62 and accepts the boot parameters described on page 63. This screen has the following selections that are not present on the live/install Desktop CD initial install screen (page 62): Install in text mode Installs a graphical Ubuntu system using the textual installer. For more information refer to “The Ubuntu Textual Installer” on page 67. Install a command- line system Installs a textual Ubuntu system (no graphical interface, no desktop, only a com- mand line interface) using the textual installer. For more information refer to “The Ubuntu Textual Installer” on page 67. Rescue a broken system Brings up Ubuntu but does not install it. After detecting the system’s disks and parti- tions, the system enters recovery mode and allows you to select the device you want to mount as the root filesystem. Once you select a device, recovery mode displays a list of rescue operations (Figure 3-17): • Execute a shell in /dev/xxx Mounts the device you selected (/dev/xxx) as / (root) and spawns a root shell (e.g., dash or bash; Chapter 7) if a shell is available on the mounted device. You are working with root privileges (page 88) and can make changes to the filesystem on the device you selected. You have access only to the shell and utilities on the mounted file- system. You may be able to mount other filesystems. If the mounted file- system does not include a shell, you must use the next selection. Give an exit command to return to the list of rescue operations. • Execute a shell in the installer environment Mounts the device you selected as /target and spawns a root dash shell (Chapter 7). You are Figure 3-17 The list of rescue operations66 Chapter 3 Step-by-Step Installation working in the installer environment with root privileges (page 88). You have access to dash and the many utilities available in the installer environment. The root filesystem is on a RAM disk (page 1056) and you can use nano to edit files. You can make changes to the filesystem on the device you selected, which is mounted on /target. You can mount other filesystems. Give an exit command to return to the list of rescue operations. • Choose a different root file system Returns to the previous step where you can select a filesystem to work with. • Reboot the system Reboots the system. Remove the CD if you want to boot from the hard disk. The Server CD Initial Install Screen Menu The Server CD uses the textual installer (page 67) to install a minimal system with a textual interface and no open ports. The installed system is appropriate for a server. The initial install screen takes advantage of the functions keys described on page 62 and accepts the boot parameters described on page 63. The Server CD initial install Figure 3-18 The DVD initial install screenAdvanced Installation 67 screen has the following selections that are not present on the live/install Desktop CD initial install screen (page 62): Install to the hard disk Installs a textual Ubuntu server system using the textual installer. For more informa- tion refer to “The Ubuntu Textual Installer” on page 67. At the end of the normal installation, the installer displays the Software selection screen, which asks if you want to install a DNS server (Chapter 25) and/or a LAMP server. LAMP stands for Linux (which you are installing), Apache (Chapter 27), MySQL, and PHP. Use the ARROW keys to move the highlight to the space between the brackets ([ ]) and press the SPACE BAR to select either or both of the choices. Rescue a broken system Brings Ubuntu up in recovery mode (page 65). The DVD The Ubuntu DVD does everything each of the CDs does and includes all software packages supported by Ubuntu, not just those installed by default. If the system you are installing is not connected to the Internet, you can install software packages from the DVD but you will have no way to update the system. The initial install screen takes advantage of the functions keys described on page 62 and accepts the boot parameters described on page 63. Figure 3-18 shows the DVD initial install screen. See the descriptions of the CD initial install screens on pages 62, 65, and 66 for information on the selections on this screen. The Ubuntu Textual Installer The Ubuntu textual installer gives you more control over the installation process than the Ubuntu graphical installer does (page 48). The textual installer displays a pseudographical interface and uses fewer system resources, including less RAM than the graphical installer does. You can install either a graphical (desktop) or textual (command line) system using the textual installer, depending on which CD/DVD you use and which selection you make from the initial install screen: Install in text mode installs a graphical system; Install a server and Install a command-line system install textual systems. Many of the screens that the textual installer displays parallel the screens displayed by the graphical installer. Within the textual installer’s screens, TAB moves between items, ARROW keys move between selections on a list, and RETURN activates the high- lighted selection and causes the installer to display the next screen. A few screens include brackets ([ ]) that function similarly to check boxes; they use an asterisk in place of a tick. Use the ARROW keys to move the highlight to the space between the brackets and press the SPACE BAR to place an asterisk between the brackets and select the adjacent choice. Press the SPACE bar again to remove the asterisk.68 Chapter 3 Step-by-Step Installation The Ubuntu installer main menu (the contents of this menu varies—Figure 3-19 shows an example) allows you to go directly to any step of the installation process or enter recovery mode (see “Rescue a broken system” on page 65). At the lower left of most textual installer screens is . See Figure 3-20 for an example. Use the TAB key to highlight this item and press RETURN to display the Ubuntu installer main menu. You may have to back up through several screens to display this menu. The first screen the textual installer displays is Choose a language (Figure 3-20). Use the UP and DOWN arrow keys to select the language you want the installer to use. You can type the first letter of the language to move the highlight to the vicinity of the language you want to choose. This language will be the default language for the installed system; you can change the default once the system is installed (page 132). Press RETURN to select the highlighted language and display the next screen. The installer steps through a series of screens, each of which has an explanation and asks a question. Use the ARROW keys and/or TAB key to highlight an answer or selection and press RETURN on each of the screens. After a few screens, the installer detects and installs programs from the CD/DVD, detects the network hardware, and configures it with DHCP (if available). As it is configuring the network, the installer asks you for the hostname of the system you are installing. For use on a local network and to connect to the Internet with a Web browser or other client, you can make up a simple name. If you are setting up a server, see “FQDN” on page 847 for information on names that are valid on the Internet. Figure 3-19 The Ubuntu installer main menuAdvanced Installation 69 After this step, the installer continues detecting hardware, starts the partitioner, and displays the Partitioning method screen (Figure 3-21). Many of the selections avail- able from the textual partitioner parallel those available from the graphical parti- tioner. This section describes how to use the textual partitioner to partition a hard disk manually. See page 56 for a description of guided partitioning. Figure 3-20 The Choose a language screen Figure 3-21 The Partitioning method screen70 Chapter 3 Step-by-Step Installation Manual Partitioning When you select Manual from the Partitioning method screen (Figure 3-21), the textual partitioner displays the Partition overview screen, which lists the hard disks in the sys- tem and partitions on those disks. If a hard disk has no partitions, the partitioner dis- plays only information about the hard disk. Figure 3-22 shows a single 200+-megabyte hard disk (highlighted) that has no partition table (and no partitions). If you want to set up RAID, see page 73 before continuing. Creating a partition table If the Partition overview screen shows no partitions and no free space on a hard disk, as it does in Figure 3-22, the hard disk does not have a partition table: You need to create one. If this screen shows at least one partition or some free space, the disk has a parti- tion table and you can skip this step and continue with “Creating a partition” below. To create a partition table, highlight the disk you want to create a partition table on and press RETURN. The installer asks if you want to create a new partition table on the device and warns that doing so will destroy all data on the disk. Highlight Yes and press RETURN. The installer displays the Partition disks screen showing the disk with a single block of free space as large as the disk (Figure 3-23). Creating a partition To create a partition, highlight the line with FREE SPACE on it and press RETURN. The partitioner asks how you want to use the free space; highlight Create a new partition and press RETURN. Next the partitioner asks you to specify the size of the new parti- tion. You can enter either a percentage (e.g., 50%) or a number of gigabytes fol- lowed by GB (e.g., 30 GB). Press RETURN. The partitioner then asks you to specify the type of the new partition (primary or logical; page 31) and asks whether you want to create the partition at the beginning or the end of the free space. It does not usually matter where you create the partition. After answering each of these questions, press RETURN. The partitioner then displays the Partition settings screen (Figure 3-24). To change a setting on the Partition settings screen, use the ARROW keys to move the highlight to the setting you want to change and press RETURN. The partitioner displays a screen that allows you to change the setting. Figure 3-22 The Partition overview screen IAdvanced Installation 71 Specifying a partition type (Use as) The first line, labeled Use as, allows you to specify the type of filesystem the installer creates on the partition. This setting defaults to ext3, which is a good choice for most normal filesystems. If you want to change the filesystem type, move the highlight to this line and press RETURN; the installer displays the How to use this partition screen (Figure 3-25 on the next page). You can select swap area (page 32), RAID (next), LVM (page 35), or another type of filesystem. Table 13-1 on page 570 lists some common types of filesystems. Move the highlight to the selection you want and press RETURN. The partitioner returns to the Partition settings screen, which now reflects the selection you made. For a swap area, there is nothing else to set up; skip to “Done setting up the partition” on the next page. Specifying a mount point The mount point defaults to / (root). To change the mount point for the filesystem, highlight the line labeled Mount point and press RETURN. The partitioner displays a Figure 3-23 The Partition overview screen II Figure 3-24 The Partitions settings screen72 Chapter 3 Step-by-Step Installation screen that allows you to specify a mount point (Figure 3-26). Select a mount point; if the mount point you want to use is not listed, select Enter manually. Press RETURN. The bootable flag Typically the only other setting you need to change is the bootable flag. Turn this flag on for the /boot partition if the system has one; otherwise, turn it on for the / (root) partition. To change the state of the bootable flag, highlight the line labeled Bootable flag on the Partition settings screen and press RETURN. After a moment, the partitioner redisplays the screen showing the changed state of this flag. Done setting up the partition Once the partition settings are satisfactory, highlight Done setting up the partition and press RETURN. The partitioner displays the Partition overview screen showing the new partition setup. To create another partition, repeat the steps starting with “Creating a partition” on page 70. To modify a partition, highlight the partition and press RETURN. Write the partitions to disk When you are satisfied with the design of the partition table(s), highlight Finish par- titioning and write changes to disk and press RETURN. After giving you another chance to back out, the partitioner writes the partitions to the hard disk. Figure 3-25 How to use this partition screen Figure 3-26 Mount point screenAdvanced Installation 73 Time zone, user account, and video modes The installer continues by asking which time zone the computer is in and if the sys- tem clock is set to UTC (page 1067). It then asks you to set up a user account and continues installing the system. Finally the installer asks you to select which video modes the X server will use. If you leave all entries unmarked, the X server uses the highest resolution the monitor is capable of displaying. The installer continues to install software and then finishes the installation. When the installer displays the Installation Complete window, remove the CD/DVD and click Restart now to reboot the system. Setting Up a RAID Array To set up a RAID array (page 34), first create two or more partitions of the same size. Usually, these partitions will be on different hard disks. You create RAID parti- tions as explained in the preceding section, except instead of making the partitions type ext3 or swap, you declare each to be a RAID volume. (RAID partitions are referred to as volumes.) Once you have two or more RAID volumes, the partitioner allows you to combine these volumes into a RAID array that looks and acts like a single partition. The following example uses 100 gigabytes from each of two new hard disks to set up a 100-gigabyte RAID 1 array that is mounted on /home. Follow the instructions on page 70 to create a new partition table on each hard disk. Then create two 100- gigabyte partitions, one on each disk. When the partitioner displays the How to use this partition screen (Figure 3-25, page 72), follow the instructions on page 71 and specify a partition type of physical volume for RAID. Figure 3-27 shows the partitioner screen after setting up the RAID volumes. Once you have at least two RAID volumes, the partitioner adds the Configure software RAID selection as the top line of its menu (highlighted in Figure 3-27). Figure 3-27 The partitioner ready to set up RAID74 Chapter 3 Step-by-Step Installation Highlight Configure software RAID, press RETURN, and confirm you want to write changes to the hard disk. From the next screen, select Create MD device (MD stands for multidisk) and press RETURN. Then select RAID 0, 1, or 5 and press RETURN. The dif- ferent types of RAID arrays are described on page 35. The partitioner then asks you to specify the number of active devices (2) and the number of spares (0) in the RAID array. The values the partitioner enters in these fields are based on your previous input and are usually correct. Next select the active devices for the RAID array (use the SPACE BAR to put an asterisk before each device; Figure 3-28) and press RETURN. Select Finish from the next screen (the one that asks if you want to create an MD device again) and press RETURN. Now you need to tell the installer where to mount the RAID array. Highlight the RAID array. In the example, this line contains #1 100.0 GB FREE SPACE (this line is highlighted in Figure 3-29, but shown after the parti- tion is created). Press RETURN. (If the RAID array line does not include the words FREE SPACE and does not show a partition, highlight the RAID array line, press RETURN, highlight Delete the Partition, and press RETURN. The RAID array line should now include FREE SPACE.) Set up this partition as you would any other by follow- ing the instructions under “Creating a partition” on page 70. In the example, the full 100 gigabytes is used for an ext3 filesystem mounted on /home. To complete this example, create a bootable / (root) partition using the rest of the free space on the first drive and a 4-gigabyte swap partition on the second drive. Figure 3-29 shows the Partition overview screen that includes these changes. High- light Finish partitioning and write changes to disk and press RETURN. The X Window System If you are installing a graphical desktop environment such as GNOME, you installed the X.org (x.org and freedesktop.org) implementation of the X Window System when you installed Ubuntu. The X Window System release X11R7.2 comprises almost 50 software packages. The X configuration files are kept in /etc/X11; the configuration file that guides the initial setup is /etc/X11/xorg.conf. You can edit Figure 3-28 Specifying the active devices in the RAID arrayThe X Window System 75 xorg.conf graphically using the Screen and Graphics Preferences window (discussed next) or you can edit it manually using a text editor (page 77). displayconfig-gtk: Configures the Display Once the (graphical) system is installed, the Screen and Graphics Preferences win- dow (Figure 3-30 on the next page) enables you to configure X.org, including spec- ifying a monitor type and video (graphics) card. To display this window, select Main menu: SystemDAdministrationDScreens and Graphics or give the command gksudo displayconfig-gtk from a terminal emulator or Run Application window (ALT-F2). This utility modifies the xorg.conf file. Specifying monitors The Screen tab of the Screen and Graphics Preferences window allows you to spec- ify the model, resolution, and frequency of one or two monitors. If you specify two monitors, it enables you to specify one as the primary monitor and to describe the spacial relationship of the two monitors. Click Screen 1 on the left of the window to specify the characteristics of one monitor; click Screen 2 to specify the other. If there is only one monitor, click the radio button labeled Disabled for one of the monitors. Figure 3-29 The finished partition tables Back up xorg.conf tip Before you make any changes to /etc/X11/xorg.conf, whether graphically or manually, make a backup copy of this file. If you run into problems with the graphical interface, you can bring the system up in recovery mode (page 512), copy the backup over the new xorg.conf, and reboot the system.76 Chapter 3 Step-by-Step Installation Normally the system probes the monitors and fills in the values in this window. If these values are missing, click the box labeled Model. The displayconfig-gtk utility dis- plays the Choose window. Specify the manufacturer and model of the monitor in this window. If an appropriate model is not listed, scroll to the top of the Manufacturer list and click Generic. From the Model list, select an LCD or conventional monitor with the maximum resolution of the monitor attached to the system. Click OK. You can specify a lower default resolution and the frequency of the monitor from the two spin boxes labeled Resolution and at. No harm is done if you specify a lower resolu- tion than the monitor is capable of displaying, but you can damage an older monitor by specifying a resolution higher than the monitor is capable of displaying. When you are done, click Test at the bottom of the window to check whether the new settings work. The displayconfig-gtk utility displays the desktop using the new settings with a dialog box asking Do you want to keep the current configuration? Click Cancel or wait 15 seconds to keep the old configuration; click Keep configuration to keep the new configuration. Specifying a graphics card Next click the Graphics Card tab (Figure 3-31). The system normally probes for the model of video card and enters an appropriate selection in the box labeled Driver. You can manually select a graphics card by clicking the box labeled Driver; displayconfig- gtk opens Choose Graphics Card Driver window (also in Figure 3-31). Near the top of this window are two radio buttons that enable you to select a driver by specifying the manufacturer and model of the graphics card in the system or by specifying the name of the driver you want to use. Click OK. The displayconfig-gtk utility generates an xorg.conf file with the information from the window. Figure 3-30 The Screen and Graphics Preferences window, Screen tabThe X Window System 77 The xorg.conf File If you choose to edit /etc/X11/xorg.conf manually, read the notes at the top of the file. You may also want to refer to the xorg.conf man page. The xorg.conf file com- prises sections that can appear in any order. Each section is formatted as follows: Section "name" entry ... EndSection where name is the name of the section. A typical entry occupies multiple physical lines but is one logical line, consisting of multiple sets of a keyword followed by zero or more integer, real, or string arguments. Keywords in these files are not case sensi- tive; underscores (_) within keywords are ignored. Most strings are not case sensitive, and SPACEs and underscores in most strings are ignored. All strings must appear within double quotation marks. The Option keyword provides free-form data to server components and is followed by the name of the option and optionally a value. All Option values must be enclosed within double quotation marks. Boolean Options take a value of TRUE (1, on, true, yes) or FALSE (0, off, false, no); no value is the same as TRUE. You can prepend No to the name of a Boolean Option to reverse the sense of the Option. Figure 3-31 The Screen and Graphics Preferences window, Graphics Card tab78 Chapter 3 Step-by-Step Installation The following sections can appear in an xorg.conf file: ServerFlags Global Options (optional) ServerLayout Binds Screen(s) and InputDevice(s) Files Locations of configuration files Module Modules to be loaded (optional) InputDevice Keyboard(s) and pointer(s) Monitor Monitor(s) Device Video card(s) Screen Binds device(s) and monitor(s) VideoAdaptor Configures the Xv extension (optional) Modes Video modes (optional) DRI Direct Rendering Infrastructure (optional) Vendor Vendor-specific information (optional) This chapter covers the sections you most likely need to work with: ServerLayout, InputDevice, Monitor, Device, and Screen. ServerLayout Section The ServerLayout section appears first in some xorg.conf files because it summa- rizes the other sections that specify the server. The following ServerLayout section names the server single head configuration and specifies that the server comprises the sections named Screen0, Mouse0, Keyboard0, and DevInputMice. The term core in this file means primary; there must be exactly one CoreKeyboard and one CorePointer. The AlwaysCore argument indicates the device reports core events and is used here to allow a non-USB mouse and a USB mouse to work at the same time. As a result, you can use either type of mouse interchangeably without modifying the xorg.conf file: Section "ServerLayout" Identifier "single head configuration" Screen 0 "Screen0" 0 0 InputDevice "Mouse0" "CorePointer" InputDevice "Keyboard0" "CoreKeyboard" InputDevice "DevInputMice" "AlwaysCore" EndSection Refer to the following sections for explanations of the sections specified in ServerLayout. InputDevice Section There must be at least two InputDevice sections: one specifying the keyboard and one specifying the pointer (usually a mouse). An InputDevice section has the following format: Section "InputDevice" Identifier "id_name" Driver "drv_name" options ... EndSectionThe X Window System 79 where id_name is a unique name for the device and drv_name is the driver to use for the device. The displayconfig-gtk utility typically creates several InputDevice sections. The following section defines a keyboard device named Keyboard0 that uses the kbd driver. The keyboard model is a 105-key PC keyboard. You can change pc105 to microsoft if you are using a U.S. Microsoft Natural keyboard, although the dif- ferences are minimal. Section "InputDevice" Identifier "Keyboard0" Driver "kbd" Option "XkbModel" "pc105" Option "XkbLayout" "us" EndSection To change the language supported by the keyboard, change the argument to the XkbLayout Option to, for example, fr for French. The next InputDevice section defines a mouse named Mouse0 that uses the mouse driver. The Device Option specifies a PS2 device. The ZAxisMapping Option maps the Z axis, the mouse wheel, to virtual mouse buttons 4 and 5, which are used to scroll a window. For more information refer to “Remapping Mouse Buttons” on page 258. When set to YES, the Emulate3Buttons Option enables the user of a two-button mouse to emulate a three-button mouse by pressing the two buttons simultaneously. Section "InputDevice" Identifier "Mouse0" Driver "mouse" Option "Protocol" "IMPS/2" Option "Device" "/dev/psaux" Option "ZAxisMapping" "4 5" Option "Emulate3Buttons" "no" EndSection The next InputDevice section is similar to the previous one except that the Device Option specifies a USB mouse. See “ServerLayout Section” on page 78 for a discus- sion of this option. Section "InputDevice" # If the normal CorePointer mouse is not a USB mouse then # this input device can be used in AlwaysCore mode to let you # also use USB mice at the same time. Identifier "DevInputMice" Driver "mouse" Option "Protocol" "IMPS/2" Option "Device" "/dev/input/mice" Option "ZAxisMapping" "4 5" Option "Emulate3Buttons" "no" EndSection80 Chapter 3 Step-by-Step Installation Monitor Section The xorg.conf file must have at least one Monitor section. The easiest way to set up this section is to use the displayconfig-gtk utility, which either determines the type of monitor automatically by probing or allows you to select the type from a list of monitors. The following section defines a monitor named Monitor0. The VendorName and ModelName are for reference only and do not affect the way the system works. The optional DisplaySize specifies the height and width of the screen in millime- ters, allowing X to calculate the dpi (dots per inch) of the monitor. HorizSync and VertRefresh specify ranges of vertical refresh frequencies and horizontal sync frequencies for the monitor; these values are available from the manufacturer. The dpms Option specifies that the monitor is DPMS (page 1034) compliant (i.e., has built-in energy-saving features). Section "Monitor" Identifier "Monitor0" VendorName "Monitor Vendor" ModelName "Dell D1028L" DisplaySize 360 290 HorizSync 31.0 - 70.0 VertRefresh 50.0 - 120.0 Option "dpms" EndSection A Monitor section may mention DDC (Display Data Channel); a monitor can use DDC to inform a video card about its properties. If you omit or comment out the HorizSync and VertRefresh lines, X uses DDC probing to determine the proper values. Device Section The xorg.conf file must have at least one Device section to specify the type of video card in the system. The VendorName and BoardName are for reference only and do not affect the way the system works. The easiest way to set up this section is to use the displayconfig-gtk utility, which usually determines the type of video card by probing. The following Device section specifies that Videocard0 uses the nv driver and locates it on the PCI bus: Section "Device" Identifier "Videocard0" Driver "nv" VendorName "Videocard vendor" BoardName "NVIDIA GeForce4 (generic)" BusID "PCI:3:0:0" EndSection Do not guess at values for HorizSync or VertRefresh caution If you configure the Monitor section manually, do not guess at the scan rates (HorizSync and Vert- Refresh). On older monitors, you can destroy the hardware by choosing scan rates that are too high.The X Window System 81 Screen Section The xorg.conf file must contain at least one Screen section. This section binds a video card specified in the Device section to a display specified in the Monitor section. The following Screen section specifies that Screen0 comprises Videocard0 and Monitor0, both of which are defined elsewhere in the file. The DefaultDepth entry specifies the default color depth (page 1029), which the Display subsection can override. Each Screen section must have at least one Display subsection. The subsection in the following example specifies a color Depth and three Modes. The Modes specify the screen resolutions in units of dots per inch (dpi). The first Mode is the default; you can switch between Modes while X is running by pressing CONTROL-ALT-KEYPAD+ or CONTROL-ALT-KEYPAD–. You must use the plus or minus on the numeric keypad when giv- ing these commands. X ignores invalid Modes. Section "Screen" Identifier "Screen0" Device "Videocard0" Monitor "Monitor0" DefaultDepth 24 SubSection "Display" Depth 24 Modes "1024x768" "800x600" "640x480" EndSubSection EndSection If you omit or comment out the Depth and Modes lines, X uses DDC probing to determine the optimal values. Multiple Monitors X has supported multiple screens for a long time. X.org supports multimonitor con- figurations using either two graphics cards or a dual-head card. Both setups are usu- ally configured the same way because the drivers for dual-head cards provide a secondary virtual device. Traditionally each screen in X was treated as a single entity. That is, each window had to be on one screen or another. More recently the Xinerama extension has allowed windows to be split across two or more monitors. This extension is sup- ported by X.org and works with most video drivers. When using Xinerama, you must set all screens to the same color depth. For each screen, you must define a Device, Monitor, and Screen section in the xorg.conf file. These sections are exactly the same as for a single-screen configura- tion; each screen must have a unique identifier. If you are using a dual-head card, the Device section for the second head is likely to require a BusID value to enable the driver to determine that you are not referring to the primary display.82 Chapter 3 Step-by-Step Installation The following section identifies the two heads on an ATI Radeon 8500 card. For other dual-head cards, consult the documentation provided with the driver (for example, give the command man mga to display information on the mga driver). Section "Device" Identifier "Videocard0" Driver "radeon" VendorName "ATI" BoardName "Radeon 8500" EndSection Section "Device" Identifier "Videocard1" Driver "radeon" VendorName "ATI" BoardName "Radeon 8500" BusID "PCI:1:5:0" EndSection Once you have defined the screens, use the ServerLayout section to tell X where the screens are in relation to each other. Each screen is defined in the following form: Screen [ScreenNumber] "Identifier" Position The ScreenNumber is optional. If omitted, X numbers screens in the order they are specified, starting with 0. The Identifier is the same Identifier used in the Screen sec- tions. The Position can be either absolute or relative. The easiest way to define screen positions is to give one screen an absolute position, usually with the coordi- nates of the origin, and then use the LeftOf, RightOf, Above, and Below keywords to indicate the positions of the other screens: Section "ServerLayout" Identifier "Multihead layout" Screen 0 "Screen0" LeftOf "Screen1" Screen 1 "Screen1" 0 0 InputDevice "Mouse0" "CorePointer" InputDevice "Keyboard0" "CoreKeyboard" InputDevice "DevInputMice" "AlwaysCore" Option "Xinerama" "on" Option "Clone" "off" EndSection By default, Xinerama causes multiple screens to act as if they were a single screen. Clone causes each of the screens to display the same image. gdm: Displays a Graphical Login Traditionally users logged in on a textual terminal and then started the X server. Ubuntu Linux uses the GNOME display manager (gdm) to provide a graphical login. The Login Window Preferences window enables you to configure the login presented by gdm. To display this window, select Main menu: SystemDAdministrationDLogin Window or give the command gksudo gdmsetup from a terminal emulator or Run ApplicationExercises 83 window (ALT-F2). The gdmsetup utility edits /etc/gdm/gdm.conf-custom, which aug- ments the heavily commented /etc/gdm/gdm.conf file. You can make many changes to the system from the Login Window Preferences window. For example, you can alter the appearance and functionality of the login screen, control whether remote logins are allowed (they are not by default), set up an automatic login, and control TCP connections to the X server (they are denied by default; page 255). Chapter Summary Most installations of Ubuntu Linux begin by booting from the live/install Desktop CD/DVD and running a live session that displays a GNOME desktop. To start the installation, double-click the object on the desktop labeled Install. Ubuntu provides a graphical installer (ubiquity) on the live/install Desktop CD/DVD; it offers a textual installer on the Alternate and Server CDs and the DVD. Both installers identify the hardware, build the filesystems, and install the Ubuntu Linux operating system. The ubiquity installer does not write to the hard disk until it dis- plays the Ready to install screen or warns you it is about to write to the disk. Until that point, you can back out of the installation without making any changes to the hard disk. A dual-boot system can boot one of two operating systems—frequently either Win- dows or Linux. You can use the GNOME Partition Editor from a live session to examine the contents of a hard disk and to resize partitions to make room for Ubuntu when setting up a dual-boot system. During installation from a live session, you can use the ubiquity partitioner to add, delete, and modify partitions. Ubuntu uses the X.org X Window System version X11R7.2. The /etc/X11/xorg.conf file configures the X server, setting up the monitor, mouse, and graphics card. The displayconfig-gtk utility is a graphical editor for this file. The GNOME display manager (gdm) provides a graphical login. The gdmsetup util- ity configures the login presented by gdm by editing /etc/gdm/gdm.conf-custom, which augments the heavily commented /etc/gdm/gdm.conf file. Exercises 1. How do you start a live session? List two problems you could encounter and explain what you would do to fix them. 2. What should you do before the first time you start a live session or install Ubuntu with a new CD/DVD? How would you do it?84 Chapter 3 Step-by-Step Installation 3. What is guided partitioning? 4. Without asking any questions, guided partitioning divides the portion of the disk allotted to Ubuntu into two partitions. One partition is the swap partition, which can be any size from 512 megabytes to 2 or more gigabytes. The other partition is designated as / (root) and contains the remainder of the disk space.What is ubiquity? 5. Describe the ubiquity partitioner. How does it differ from the partitioner on the Alternate and Server CDs? 6. When is it beneficial to use an ext2 filesystem instead of an ext3 filesystem? Advanced Exercises 7. What is a virtual console? During installation, what can you use a virtual console for? If the system is displaying a virtual console, how do you dis- play the graphical installation screen? 8. What would you do to have the system display all the things it is doing as it boots from a live/install Desktop CD/DVD? 9. Assume you have configured four screens, screen0 through screen3. How would you specify in xorg.conf that the screen layout is a T shape with the first screen at the bottom and the other three screens in a row above it?85 I PART II Getting Started with Ubuntu Linux CHAPTER 4 Introduction to Ubuntu Linux 87 CHAPTER 5 The Linux Utilities 145 CHAPTER 6 The Linux Filesystem 183 CHAPTER 7 The Shell 219This page intentionally left blank 8787 4Chapter4One way or another you are sitting in front of a computer that is running Ubuntu Linux. After describing root 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. 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 are sections that describe where to find Linux documentation (page 124) and offer more about logging in on the system, including information about passwords (page 132). The chapter concludes with a more advanced, optional section about working with Linux windows (page 139). Be sure to read the warning about the dangers of misusing the powers of root (sudo) in the next section. Heed that warning, but 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. In This Chapter Curbing Your Power: root Privileges/sudo . . . . . . . . . . . . . . 88 A Tour of the Ubuntu Linux Desktop . . . . . . . . . . . . . . . . . . . . 89 The Update Notifier. . . . . . . . . . . . 100 Updating, Installing, and Removing Software Packages . . 119 Where to Find Documentation . . . 124 More About Logging In . . . . . . . . . 132 What to Do If You Cannot Log In. . 133 Working from the Command Line. . . . . . . . . . . . . . 136 Controlling Windows: Advanced Operations . . . . . . . . . . . . . . . . . 139 4 Introduction to Ubuntu Linux88 Chapter 4 Introduction to Ubuntu Linux Curbing Your Power: root Privileges/sudo When you enter your password to run a program (not when you log in on the system), or when you use sudo from the command line, you are working with root privileges and have extraordinary systemwide powers. A person working with root privileges is sometimes referred to as Superuser or administrator. When working with root privileges, you can read from or write to any file on the sys- tem, execute programs that ordinary users cannot, and more. On a multiuser system you may not be permitted to run certain programs, but someone—the system administrator—can and that person maintains the system. When you are running Linux on your own computer, the first user you set up, usually when you install Ubuntu, is able to use sudo and its graphical counterpart, gksudo, to run programs with root privileges. There are two primary ways to gain root privileges. First, when you start a program that requires root privileges, a dialog box pops up asking you to Enter your pass- word to perform administrative tasks. After you enter your password, the program runs with root privileges. Second, if you use the sudo utility (for textual applica- tions; page 490) or gksudo utility (for graphical applications; page 491) from the command line (such as from a terminal emulator; page 114) and provide your pass- word, the command you enter runs with root privileges. In both cases you cease working with root privileges when the command finishes or when you exit from the program you started with root privileges. For more information refer to “Running Commands with root Privileges” on page 487. root terminology tip Most Linux systems include an account for a user named root. This user has special privileges and is sometimes referred to as Superuser. On a classic Linux system a user can log in and work as root by providing the root password. As installed, Ubuntu has a root account but no password for the account: The root account is locked. The next section explains how you can use sudo and provide your password to run a com- mand with root privileges. This book uses the phrase “working with root privileges” to distinguish this temporary escalation of privileges from the classic scenario wherein a user can work with root privileges for an entire session. See page 487 for more information on root privileges. Who is allowed to run sudo? security The first user you set up when you install Ubuntu can administer the system: This user can use sudo to execute any command. When you add user accounts, you can specify whether they are allowed to administer the system. See page 658 and Figure 17-2 on page 659 for more information. In this chapter and in Chapter 8, when this book says you have to enter your password, it assumes you have permission to administer the system. If not, you must get an administrator to perform the task.A Tour of the Ubuntu Linux Desktop 89 A Tour of the Ubuntu Linux Desktop This section presents new words (for some readers) in a context that explains the terms well enough to get you started with the Ubuntu desktop. If you would like exact definitions as you read this section, refer to “GNOME Desktop Terminology” on page 105 and to the Glossary. The Glossary also describes the data entry widgets (page 1068), such as the combo box (page 1029), drop-down list (page 1035), list box (page 1045), and text box (page 1064). GNOME GNOME (www.gnome.org), a product of the GNU project (page 4), is the user- friendly default desktop manager under Ubuntu Linux. KDE, the K Desktop Envi- ronment, is a powerful desktop manager and complete set of tools you can use in place of GNOME. The version of Ubuntu that runs KDE is named Kubuntu. See page 60 for instructions on installing KDE. This tour describes GNOME, a full-featured, mature desktop environment that boasts a rich assortment of configurable tools. After discussing logging in, this sec- tion 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 con- cludes with a discussion of the Update Notifier, the object 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 Ubuntu Linux system, it displays a Login screen (Figure 4-1) on the system console. At the lower-left corner of the screen is a small 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 Ubuntu Linux to get it working again. If you bought your system with Ubuntu installed at the factory security When a manufacturer installs Ubuntu, it cannot set up an account for you (it does not know your name). Typically, these systems come with the root account unlocked. Ubuntu suggests you not unlock the root account. To set the system up the way Ubuntu suggests, use users-admin as explained on page 658 to add a user who will be the system administrator. Make sure to put a tick in the check box labeled Administer the system in the User Privileges tab (page 658). Then relock the root account (page 499).90 Chapter 4 Introduction to Ubuntu Linux object labeled Options. Click this object or press F10 to display the Actions menu. The selections on this menu allow you to work in a different language (Select Lan- guage), specify a desktop manager (Session), log in remotely, reboot the system (Restart), turn the system off (Shut Down), suspend the system, or have the system hibernate. For more information refer to “The Login Screen” on page 132. To log in, enter your username in the text box labeled Username and press RETURN. The label changes to Password. Enter your password and press RETURN. If Ubuntu dis- plays an error message, try entering your username and password again. Make sure the CAPS LOCK key is not on (Ubuntu displays a message if it is); the routine that veri- fies your entries is case sensitive. See page 133 if you need help with logging in and page 135 if you want to change your password. The system takes a moment to set things up and then displays a workspace (Figure 4-2). Introduction 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 may want to add a user 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 desk- top 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 (Figure 4-2). Each of the bars holds several icons and words called objects. (Buttons, applets, and menus are all types of objects.) When you click an object, something happens. Figure 4-1 The Ubuntu GNOME login screenA Tour of the Ubuntu Linux Desktop 91 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. Because the panels are easy to configure, you can set them up to hold tools you use fre- quently. You can create additional panels to hold different groups of tools. Workspaces and the Desktop What you see displayed on the screen is a workspace. Initially Ubuntu configures GNOME with two workspaces. The desktop, which is not displayed all at once, is the collection of all workspaces. “Switching Workspaces” on page 93 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. Click- ing an object may, for example, start a program; display a menu or a folder; or open a file, a window, or a dialog box. Figure 4-2 The initial workspace Firefox Web Browser Main menu Clock Log out Workplace switcher Trash Tooltip Do not remove objects or panels yet caution You can add and remove panels and objects as you please. Until you are comfortable working with the desktop and have finished reading this section, however, it is best not to remove any panels or objects from the desktop. Click and right-click tip This book uses the term click when you need to click the left mouse button and right-click when you need to click the right mouse button. See page 95 to adapt the mouse for left-handed use.92 Chapter 4 Introduction to Ubuntu Linux For example, to start the Firefox Web browser, (left) click the Firefox object (the blue and orange globe on the Top panel; see Figure 4-2). GNOME opens a window running Firefox. When you are done using Firefox, click the small x at the right end of the titlebar at the top of the window. GNOME closes the window. When you (left) click the date and time near the right end of the Top panel, the Clock applet displays a calendar for the current month. (If you double-click a date on the calendar, the object opens the Evolution calendar to the date you clicked—but first you have to set up Evolution.) Click the date and time again to close the calendar. 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, Places, and System. Click one of these words to display the corresponding menu. Each menu selection that holds a submenu displays a small triangle (pointing to the right) to the right of the name of the menu (Figure 4-3). 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 106). Experiment with the Main menu. Start Solitaire (Main menu: ApplicationsDGamesD Free Cell Solitaire), a terminal emulator (Main menu: ApplicationsDAccessoriesD Terminal), and other programs from the Applications menu. The Places and System menus are discussed on page 110. Use the Run Application window Finally, you can start a program by pressing ALT-F2 to display the Run Application window (Figure 4-4). As you start to type firefox in the text box at the top of the window, the window recognizes what you are typing and displays the Firefox logo and the rest of the word firefox. Click Run to start Firefox. Figure 4-3 The Applications menu, Accessories, TerminalA Tour of the Ubuntu Linux Desktop 93 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 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 vim (a text-based editor) in the text box, put a tick in the box labeled Run in terminal, and click Run. GNOME opens a Terminal (emulator) win- dow and runs the vim text editor in that window. When you exit from vim (press ESCAPE:q! 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 800) utility and keep the win- dow open until you press RETURN: bash -c "df -h ; read" This command starts a bash shell (Chapter 7) that executes the command line fol- lowing the –c option. The command line holds two commands separated by a semi- colon. The second command, read (page 445), 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 Workplace Switcher Each rectangle in the Workplace Switcher applet (or just Switcher)—the group of rectangles near the right end of the Bottom panel—represents a workspace (Figure 4-2, page 91). When you click a rectangle, the Switcher displays the corre- sponding workspace and highlights the rectangle to indicate which workspace is displayed. Figure 4-4 Run Application window94 Chapter 4 Introduction to Ubuntu Linux Click the rightmost rectangle in the Switcher (not the Trash applet to its right). Next, select Main menu: PreferencesDMouse. GNOME opens the Mouse Prefer- ences window. The Switcher rectangle that corresponds to the workspace you are working in displays a small colored rectangle. This small rectangle corresponds in size and location within the Switcher rectangle to the window within the work- space. 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. When you release the mouse button, the small rectangle within the Switcher moves to the corresponding location within the Switcher rectangle. Now click a different rectangle in the Switcher and open another application—for example, the Ubuntu Help Center (click the blue question mark on the Top panel). With the Ubuntu Help Center 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). Setting Personal Preferences You can set preferences for many objects on the desktop, including those on the panels. Workspace Switcher To display the Workspace Switcher Preferences window (Figure 4-5), first right-click anywhere on the Switcher to display the Switcher menu and then select Preferences. Figure 4-5 The Workspace Switcher Preferences window Right-click to display a 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 selec- tion. See the adjacent section, “Setting Personal Preferences,” and page 115 for more information on Object Context menus.A Tour of the Ubuntu Linux Desktop 95 Specify the number of workspaces you want in the spin box labeled Number of workspaces or Columns. (The window looks different if you have Visual Effects [page 103] enabled; in this case change the value in the spin box labeled Columns.) 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 Prefer- ences 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-2, page 91) and select Preferences. The resulting window enables you to customize the date and time the Clock applet displays on the Top panel. The clock immediately reflects the changes you make in this window. Different objects display different Preferences windows. Objects that launch pro- grams display Properties windows and do not have Preferences windows. Experi- ment with different Preferences and Properties windows and see what happens. Mouse Preferences The Mouse Preferences window (Figure 4-6) enables you to change the characteristics of the mouse to suit your needs. To display this window, select Main menu: SystemD PreferencesDMouse or give the command gnome-mouse-properties from a terminal emulator or Run Application window (ALT-F2). The Mouse Preferences window has two tabs: Buttons and Motion (and a third, Touchpad, on a laptop). Left-handed mouse To change the orientation of the mouse buttons for use by a left-handed person, put a tick in the check box labeled Left-handed mouse. If you change the setup of 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 a button to click, click the right button, and vice versa. See “Remapping Mouse Buttons” on page 258 if you want to change the orientation of the mouse buttons from the command line. Figure 4-6 The Mouse Preferences window, Buttons tab96 Chapter 4 Introduction to Ubuntu Linux Double-click timeout Use the Double-Click Timeout slider in the Buttons tab to change the speed with which you must double-click a mouse button to have the system recognize your action as a double-click rather than as two single clicks. In the Motion tab you can control the acceleration and sensitivity of the mouse. The Drag and Drop Threshold specifies how far you must drag an object before the sys- tem considers the action the drag part of a drag and drop. Working with Windows To resize a window, move the mouse pointer over an edge of the window; the pointer turns into an arrow. When the pointer is an arrow, 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 at the same time. 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). For fun, try moving the window past either side of the workspace. What happens? The result depends on how Visual Effects (page 103) is set. Titlebar At the right of the titlebar are three icons that control the window (Figure 4-16, page 112). Clicking the underscore, usually at the left 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 109). Click this object to toggle the window between visible and minimized. Clicking the box icon, usually the middle of the three icons, toggles the window between its maxi- mum size (maximizes the window) and its normal size. Double-clicking the titlebar does the same thing. Terminating a program Clicking the x terminates the program running in the window and closes the win- dow. In some cases you may need to click several times. 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 260 for more detailed information on Nautilus. 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” may be used in textual or command line contexts. This book uses these terms interchangeably. Term: File Browser This book sometimes uses the terms File Browser window and File Browser when referring to the Nautilus File Browser window. Double-clicking an object in a File Browser window has the same effect as double- clicking an object on the desktop: Nautilus takes an action appropriate to the object.A Tour of the Ubuntu Linux Desktop 97 For example, when you double-click a text file, Nautilus opens the file with a text editor. When you double-click an OpenOffice.org document, Nautilus opens the file with OpenOffice.org. 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 been in the window. The Desktop Directory The files on the desktop are held in a directory that has a pathname (page 189) of /home/username/Desktop, where username is your login name. The simple direc- tory name is Desktop. When you select Main menu: PlacesDDesktop, GNOME opens a File Browser window showing the files on the desktop (Figure 4-7). Initially there are no files. If you click the pencil and paper object at the left edge near the top of the File Browser window, Nautilus displays in the Location text box the pathname of the directory it is displaying. To see the correspondence between the graphical desktop and the Desktop direc- tory, right-click anywhere within the large clear area of the Desktop File Browser window. Select Create DocumentDEmpty File. Nautilus creates a new file on the desktop and displays its object in this window. When you create this file, GNOME highlights the name new file under the file: You can type any name you like at this point. Press RETURN when you are finished entering a 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 from the File menu or by clicking the x at the right end of the titlebar. You have created a text document on the desktop. You can 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 Folder. You can name this folder in the same way that you named the file you created. 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. Figure 4-7 The desktop with a Nautilus File Browser window98 Chapter 4 Introduction to Ubuntu Linux 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 you clicked on. 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 win- dow. The file is back on the desktop, although it may be hidden by one of the File Browser windows. Next, open a word processing document by selecting Main menu: ApplicationsD OfficeDOpenOffice.org Word Processor. Type some text and click the floppy disk icon or select menubar: FileDSave to save the document. OpenOffice.org displays a Save window (Figure 4-8). 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 and close this sec- tion of the window. Figure 4-8 shows the Save window with this 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 lim- ited 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, click Desktop in this drop-down list and then click Save. OpenOffice.org saves the document with a filename extension of .odt, which indicates it is an OpenOffice.org word processing document. The object for this type of file has some text and a pic- ture in it. optional Browse/Save window With the Browse for other folders section opened, the Save window grays out the drop-down list labeled Save in folder and expands the Browse for other folders sec- tion, as shown in Figure 4-9. This expanded section holds two large side-by-side list boxes: Places and Name. The list box labeled Places displays directories and loca- tions on the system, including File System. The list box labeled Name lists the files within the directory highlighted in Places. Figure 4-8 The Save windowA Tour of the Ubuntu Linux Desktop 99 The Browse for other folders section of the Browse/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 win- dow. In this example, OpenOffice.org calls it a Save window and uses it to locate the directory to save a document in. 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 (/; see “Absolute Pathnames” on page 189 for more information). Next, double-click tmp in the list box on the right. The button(s) above the list box on the left change to reflect the directory the list box on the right is displaying. Click Save. The buttons above the list box on the left represent directories. The list box on the right displays the directories found within the directory named in the highlighted (darker) button. This directory is the one you would save the file to if you clicked Save. 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 any changes since you last saved it, OpenOffice.org asks if you want to save the document. If you choose to save it, OpenOffice.org 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 in OpenOffice.org. The Desktop directory is special In summary, the Desktop directory is like any other directory, except that GNOME displays its contents on the desktop (in every workspace). It is as though the desk- top is a large, plain Desktop File Browser window. You can work with the Desktop Figure 4-9 A Save window with Browse for other folders open100 Chapter 4 Introduction to Ubuntu Linux directory because it is always displayed. Within the GUI, you must use a utility, such as Nautilus, to display and work with the contents of any other directory. Selecting Objects The same techniques 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. You can select a group of adjacent objects by highlighting the first object and then, while holding down the SHIFT key, clicking the last object; GNOME highlights all objects between the two objects you clicked. Or, 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. Display the Examples folder by double-clicking it. 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 the Examples folder to the desktop. You can drag and drop objects to move them, although you do not have permission to move the objects from the Examples folder. Emptying the Trash Selecting File Browser menubar: FileDMove to Trash moves the selected (high- lighted) object to the .Trash directory. Like the Desktop directory, .Trash is a direc- tory in /home/username. Because its name starts with a period however, it is not usually displayed. Press CONTROL-H or select File Browser menubar: ViewDShow Hid- den Files to display hidden files. For more information refer to “Hidden Filenames” on page 188. 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. All File Browser windows allow you to permanently delete all files in the .Trash directory by selecting File Browser menubar: FileDEmpty Trash. To view the files in the trash, click the Trash applet (Figure 4-2, page 91). Nauti- lus displays the Trash File Browser window. Select Empty Trash from the Trash applet context menu to permanently remove all files from the trash. (This selection does not appear if there are no files in the trash.) Or you can open the .Trash directory, right- click an object, and select Delete from Trash to remove only that object (file). You can drag and drop files to and from the trash just as you can with any other folder. The Update Notifier On systems connected to the Internet, Ubuntu is initially set up to automatically search for and notify you when software updates are available. GNOME displays the message Software updates available in a bright dialog box and places the Update Notifier (Figure 4-10) toward the right end of the Top panel when updates are available. Clicking this object opens the Update Manager window (Figure 4-10).A Tour of the Ubuntu Linux Desktop 101 You can also open this window by selecting Main menu: SystemDAdministrationD Update Manager or by giving the command update-manager from a terminal emu- lator or Run Application window (ALT-F2). Update Manager window When the Update Manager window opens, it displays the message Starting Update Manager; after a moment it tells you how many updates are available. If no updates are available, this window displays the message Your system is up-to-date. If you have rea- son to believe the system is not aware of available updates, click Check. The update- manager asks for your password, reloads its database, and checks for updates again. If updates are available, click Install Updates. The Update Manager asks for your password, displays the Downloading Package Files window, and counts the pack- ages as it downloads them. Next the Update Manager displays the Applying Changes window with the message Installing software and describes the steps it is taking to install the packages. When it is finished, the Update Manager displays the Changes Applied window, which displays the message Update is complete. After you click Close, the Update Manager again checks for updates and usually displays the message Your system is up-to-date. Click Close. If the updates require you to reboot the system or restart a program, an object appears on the Top panel. Click this object and take the required action as soon as you are ready. For more informa- tion refer to “Updating, Installing, and Removing Software Packages” on page 119. Figure 4-10 The Update Notifier and the Update Manager window Update Notifier102 Chapter 4 Introduction to Ubuntu Linux Changing Appearances (Themes) One of the most exciting aspects of a Linux desktop is the ability it gives you to change its appearance. You can change not only the backgrounds, but also window borders (including the titlebar), icons, the buttons that applications use, and more. To see some examples of what you can do, visit art.gnome.org. Themes In a GUI, a theme is a recurring pattern and overall look that (ideally) pleases the eye and is easy to interpret and use. You can work with desktop themes at several levels. First and easiest is to leave well enough alone. Ubuntu comes with a good- looking theme named Human. If you are not interested in changing the way the desktop looks, continue with the next section. The next choice, which is almost as easy, is to select one of the alternative themes that comes with Ubuntu. You can also modify one of these themes, changing the background, fonts, or interface. In addition, you can download themes from many sites on the Internet and change them in the same ways. The next level is customizing a theme, which changes the way the theme looks—for example, changing the icons a theme uses. At an even higher level, you can design and code your own theme. For more information see the tutorials at art.gnome.org. Appearance Preferences window The key to changing the appearance of your desktop is the Appearance Preferences window. Display this window by choosing Main Menu: SystemsDPreferencesD Appearances or by right-clicking the root window (any empty space on a workspace) Figure 4-11 The Appearance Preferences window, Theme tabA Tour of the Ubuntu Linux Desktop 103 and selecting Change Desktop Background. The Appearance Preferences window has five tabs: •The Theme tab (Figure 4-11) enables you to select one of several themes. Click a theme and the workspace immediately reflects the use of that theme. The Human theme is the default Ubuntu theme; select this theme to make the workspace appear as it did when you installed the system. Once you select a theme, you can click Close or you can click the other tabs to modify the theme. •The Background tab enables you to specify a wallpaper or color for the desktop background. To specify a wallpaper, click one of the samples in the Wallpaper frame or click Add and choose a file—perhaps a picture— you want to use as wallpaper. (Clicking Add displays the Add Wallpaper window; see “Browse/Save window” on page 98 for instructions on select- ing a file using this window.) Then choose the style you want GNOME to use to apply the wallpaper. For example, Zoom makes the picture you chose fit the workspace. You can also specify a color for the background: either solid or a gradient between two colors. To use a color, you must first select No Wallpaper from the Wallpaper frame: Allow the mouse pointer to hover over each of the wallpapers displayed in the Wallpaper frame until you find one that displays the tooltip No Wallpaper. Select that (non)wallpaper. Next, select Solid color from the drop-down list labeled Colors and click the colored box to the right of this list. GNOME displays the Pick a Color window. Click a color you like from the ring and adjust the color by moving the lit- tle circle within the triangle. Click OK when you are done. The color you chose becomes the background color of the desktop. See page 268 for more information on the Pick a Color window. •The Fonts tab (Figure 8-8, page 267) enables you to specify which fonts you want GNOME to use in different places on the desktop. You can also change how GNOME renders the fonts (page 267). •The Interface tab enables you to modify the appearance of window menus and toolbars and presents a Preview pane that shows what your choices will look like. Visual effects •The Visual Effects tab enables you to select one of three levels of visual effects: None, Normal, and Extra. Normal and Extra effects replace the Metacity window manager with Compiz Fusion (compiz.org), which implements 3D desktop visual effects. (Compiz is the name of the core; the plugins are called Compiz Fusion.) When you install Ubuntu, Ubuntu determines what the hardware is capable of running and sets the proper level of effects. One of the most dramatic visual effects is wiggly windows: To see this effect, select Normal or Extra and drag a window around using its titlebar. If you experience problems with the system, select None.104 Chapter 4 Introduction to Ubuntu Linux The changes you make in the Background, Fonts, Interface, and Visual Effects tabs are used by any theme you select, including ones you customize. When you are through making changes in the Appearance Preferences window tabs, you can click Close to use the theme as you have modified it or return to the Theme tab to cus- tomize the theme. Customizing a theme From the Theme tab of the Appearance Preferences window, select the theme you want to customize or continue with the theme you modified in the preceding sec- tion. Click Customize to open the Customize Theme window. Go through each tab in this window; choose entries and watch the change each choice makes in the workspace. Not all tabs work with all themes. When you are satisfied with the result, click Close. After you customize a theme, it is named Custom. When you customize another theme, those changes overwrite the Custom theme. For this reason it is best to save a customized theme by clicking Save As and specifying a name for the theme. After saving a theme, it appears among the themes in the Theme tab. 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 saved a session or logged out: The same windows will be positioned as they were on the same work- spaces and programs will be as you left them. To save a session, first make sure you have only the windows open that you want to appear the next time you log in. Then select Main Menu: SystemDPreferencesD Sessions to display the Sessions window. Click the Session Options tab and then click Remember currently running applications. The window displays Your session has been saved. Each time you log in, the same windows will appear. If you want GNOME to remember what you were doing each time you log off, put a tick in the check box labeled Automatically remember running applications when logging out. Getting Help Ubuntu provides help in many forms. Clicking the question mark object on the Top panel displays the Ubuntu Help Center window, which provides information on the desktop. To display other information, click a topic in the list on the left side of this window. You can also enter text to search for in the text box labeled Search and press RETURN. In addition, most windows provide a Help object or menu. See “Where to Find Documentation” on page 124 for more resources. Visual effects can cause problems caution Setting Visual Effects to Normal or Extra can cause unexpected graphical artifacts, shorten battery life, and reduce performance in 3D applications and video playback. If you are having problems with an Ubuntu system, try setting Visual Effects to None and see if the problem goes away.Getting the Most out of the Desktop 105 Feel Free to Experiment Try selecting different items from the Main menu and see what you discover. Fol- lowing are some applications you may want to explore: • OpenOffice.org’s Writer is a full-featured word processor that can import and export MS Word documents. Select Main menu: OfficeDOpen-Office.org Writer. The Office menu also offers a database, presentation manager, and spreadsheet. • Firefox is a powerful, full-featured Web browser. Click the blue and orange globe object on the Top panel to start Firefox. You can also select Main menu: ApplicationsDInternetDFirefox Web Browser. • Pidgin is a graphical IM (instant messenger) client, formerly called Gaim, that allows you to chat on the Internet with people who are using IM cli- ents such as AOL, MSN, and Yahoo! To start Pidgin, select Main menu: ApplicationsDInternetDPidgin Internet Messenger. The first time you start Pidgin, it opens the Accounts window; click Add to open the Add Account window. In the Add Account window, select a proto- col (such as AIM or MSN), enter your screen name and password, and put a tick in the check box labeled Remember password if you want Pidgin to remember your password. Click Save. Visit pidgin.im for more information, including Pidgin documentation and plugins that add features to Pidgin. Logging Out To log out, click the Logout button (Figure 4-2, page 91) at the upper-right corner of the workspace. GNOME displays the Logout window. This window looks differ- ent from other windows because it has no decorations (page 141). Or, you can select Main Menu: SystemDQuit and click Logout. You can also choose to 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 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, from the GNOME Users Guide, establishes a founda- tion for discussing the GNOME desktop. Figure 4-2 on page 91 shows the initial Ubuntu GNOME desktop.106 Chapter 4 Introduction to Ubuntu Linux 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 Panels are bars that appear on the desktop and hold (Panel) objects. Initially there are two gray 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). You can add and remove panels. You can place panels at the top, bottom, and both sides of the desktop, and you can stack more than one panel at any of these loca- tions. The desktop can have no panels, one panel, or several panels. See page 107 for more information on panels. Panel objects Panel objects appear as words or icons on panels. You can click these objects to dis- play menus, run applets, or launch programs. The five types of panel objects are applets, launchers, buttons, menus, and drawers. See page 109 for more informa- tion on Panel objects. Windows A graphical application typically runs within and displays a window. At the top of most windows is a titlebar that 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. Most windows have decorations (page 141) but some, such as the Logout window, do not. Workspaces 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 93) enables you to display any one of several workspaces. Tooltips Tooltips (Figure 4-2, page 91) 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. Opening Files By default, you double-click an object to open it; or you can right-click the object and select Open from the drop-down menu. When you open a file, GNOME figures out the appropriate tool to use by determining the file’s MIME (page 1048) 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 deter- mine a file’s MIME type. If GNOME does not recognize the filename extension, it examines the file’s magic number (page 1046). 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 theGetting the Most out of the Desktop 107 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 exe- cutable. When GNOME uses the wrong tool to open a file, the tool generally issues an error message. See “Open With” on page 118 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 and therefore are consistent across workspaces. The Panel Menu Right-clicking an empty part of a panel displays the Panel (Context) menu. Aside from help and informational selections, this menu has four selections. Add to Panel Selecting Add to Panel displays the Add to Panel window (Figure 4-12). You can drag an object from this window to a panel, giving you the choice of which panel the object appears on. You can also highlight an object and click Add to add the object to the panel whose menu you used to display this window. Many objects in this window are whimsical: Try Geyes and select Bloodshot from its preferences window, or try Fish. One of the more useful objects is Search for Files. When you click this object on the panel, it displays the Search for Files window (page 269). Figure 4-12 The Add to Panel window108 Chapter 4 Introduction to Ubuntu Linux Properties Selecting Properties displays the Panel Properties window (Figure 4-13). This win- dow has two tabs: General and Background. In the General tab, Orientation selects which side of the desktop the panel appears on; Size adjusts the width of the panel. Expand causes the panel to span the width or height of the workspace; without a tick in this check box the panel is centered and just wide enough to hold its objects. Autohide causes the panel to disappear until you bump the mouse pointer against the side of the workspace. Hide buttons work differently from autohide: Show hide buttons displays buttons at each end of the panel. When you click one of these buttons, the panel slides out of view, leaving only a button remaining. When you click that button, the panel slides back into place. The Background tab of the Panel Properties window enables you to specify a color and transparency or an image for the panel. If you want to see what stacked panels look like, use the Orientation drop-down list to change the location of the panel you are working with. If you are working with the Top panel, select Bottom and vice versa. As with Preferences windows, Properties windows have no Apply and Cancel buttons; they implement changes immediately. Use the same procedure to put the panel back where it was. See “Pick a Color Window” on page 268 for instructions on how to change the color of the panel. Once you have changed the color, move the slider labeled Style to make the color of the panel more or less transparent. If you do not like the effect, click the radio button labeled None (use system scheme) to return the panel to its default appearance. Click Close. Figure 4-13 The Panel Properties window, General tabGetting the Most out of the Desktop 109 Delete This Panel Selecting Delete This Panel does what you might expect it. Be careful with this selec- tion: When it removes a panel, it removes all the objects on the panel and you will need to reconstruct the panel if you want it back as it was. New Panel Selecting New Panel adds a new panel to the desktop. GNOME decides where it goes; you can move the panel if you want it somewhere else. Moving a Panel You can drag any panel to any of the four sides of the desktop: Left-click any empty space on a panel; the mouse pointer turns into a small hand. Drag the panel to the side you want to move it to. Unlike dragging an object across a workspace, the panel does not move until you have dragged the mouse pointer all the way to the new location of the panel; it then snaps into place. If you have stacked panels and are having trouble restacking them in the order you want, try dragging a panel first to an empty side of the workspace and then to its final location. Panel Objects The icons and words on a panel, called panel objects, display menus, launch pro- grams, and present information. The panel object with the blue and orange globe starts Firefox. The email button (the open envelope icon) starts Evolution, an email and calendaring application (www.gnome.org/projects/evolution). You can start almost any utility or program on the system using a Panel object. This section describes the different 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 Mixer (vol- ume control), Clock (date and time; Figure 4-2, page 91), and Workspace Switcher (Figure 4-2, page 91) are applets. Window List applet Although not a distinct type of object, the Window List applet is a unique and important tool. One Window List applet (Figure 4-14) 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 to display the Window Operations menu (page 112). If a window is berried 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. The Firefox object is a launcher: It starts the Firefox application. Under Main menu: Applications, you can find launchers that start applications. Under Main menu: Places, the Home Folder, Figure 4-14 Window List applets110 Chapter 4 Introduction to Ubuntu Linux Documents, Desktop, and Computer objects are launchers that open File Browser windows to display folders. Buttons A button performs a single, simple action. The Logout button (Figure 4-2, page 91) dis- plays a window that enables you to log off, shut down, or reboot the system. The Show Desktop button at the left of the Bottom panel minimizes all windows on the workspace. Menus 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 sec- tion discusses the Main menu. Drawers A drawer is an extension of a panel. You can put the same objects in a drawer that you can put on a panel, including another drawer. When you click a Drawer object, the drawer opens; you can then click an object in the drawer the same way you click an object on a panel. The Panel Object Context Menus Three selections are unique to Panel Object Context menus (right-click a Panel object). The Remove from Panel selection does just that. The Move selection allows you to move the object within the panel and to other panels; you can also move an object by dragging it with the middle mouse button. The Lock to Panel selection locks the object in position so it cannot be moved. When you move an object on a panel, it can move through other objects. If the other object is not locked, it can displace the object if necessary. The Move selection is grayed out when the object is locked. The Main Menu The Main menu appears at the left of the Top panel and includes Applications, Places, and System. Click one of these words to display the corresponding menu. Applications The Applications menu holds several submenus, each named for a category of appli- cations (e.g., Games, Graphics, Internet, Office). The last selection, Add/Remove, is discussed on page 120. Selections from the submenus launch applications—peruse these selections, hovering over those you are unsure of to display tooltips. Places The Places menu holds a variety of launchers, most of which open a File Browser window. The Home Folder, Documents, and Desktop objects display your directo- ries with corresponding names. The Computer, CD/DVD Creator, and Network objects display special locations. Each of these locations enables you to access file manager functions. A special URI (page 1067) specifies each of these locations. For example, the CD/DVD Creator selection displays the burn:///, URI which enables you to create a CD or DVD. The Connect to Server selection opens a window that allows you to connect to various type of servers, including SSH and FTP (see “File” on page 263). Below these selections are mounted filesystems; click one of these toGetting the Most out of the Desktop 111 display the top-level directory of that filesystem. The Search for Files selection enables you to search for files (page 269). System The System menu holds two submenus, selections that can provide support, and the Quit selection. The two submenus are key to configuring your account and setting up and maintaining the system. The Preferences submenu establishes the characteristics of your account; each user can establish her own preferences. Click some of these selections to become familiar with the ways you can customize your account on an Ubuntu system. The Administration submenu controls the way the system works. For example, AdministrationDFolder Sharing enables Ubuntu to use NFS (Chapter 23) or Samba (Chapter 24) to share folders with other systems. AdministrationDNetwork config- ures the system’s network connections (page 698). Most of these selections require you to be a system administrator and enter your password to make changes. These menu selections are discussed throughout this book. Copying launchers to a panel You can copy any launcher from the Main menu to the Top panel or the desktop. Instead of left-clicking the menu selection, right-click it. GNOME displays a small menu that can add the launcher to the Top panel or desktop. Windows In a workspace, a window is a region that runs, or is controlled by, a particular pro- gram (Figure 4-15). Because you can control the look and feel of windows—even the buttons they display—your windows may not look like the ones shown in this book. Each window in a workspace has a Window List applet (page 109) on the Bottom panel. Figure 4-15 A typical window Titlebar Toolbar Vertical scrollbar Menubar Window contents Buttons112 Chapter 4 Introduction to Ubuntu Linux Titlebar A titlebar (Figures 4-15 and 4-16) appears at the top of most windows and con- trols 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-16. The minimize (iconify) button collapses the window so that the only indication of the window 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 on the titlebar of a maximized window to restore the window to its former size. You can also double-click the title- bar to maximize and restore a window. Clicking the maximize button with the mid- dle mouse button expands the window vertically; using the left button expands it horizontally. Use the same or a different mouse button to click the maximize button again and see what happens. Clicking the close button closes the window and termi- nates the program that is running in it. Left-click the titlebar and drag the window to reposition it. Window Operations menu The Window Operations menu contains most common operations you need to per- form on any window. Click the Window Operations menu button or right-click either the titlebar or the Window List applet (page 109) to display this menu. Toolbar A toolbar (Figure 4-15) 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 win- dow manager (page 141). 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 109), you can change which window on the current 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 the titlebar information from 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 Figure 4-16 A window titlebar Minimize Maximize Close Operations menu Window title WindowGetting the Most out of the Desktop 113 repeatedly pressing TAB cycles in the other direction. See page 139 for more infor- mation on input focus. 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. First you can use the clipboard, technically called the copy buffer, to copy or move objects or text: You explicitly copy an object or text to the buffer and then paste it somewhere else. Applications that fol- low the user interface guidelines use CONTROL-X to cut, CONTROL-C to copy, and CONTROL-V to paste. Application context menus frequently have these same selections. You may be less familiar with the second method—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 the 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. Next, to use the clipboard, explicitly copy (CONTROL-C) or cut (CONTROL-X) the objects or text.1 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). 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. Using the selection buffer, you can give other commands after selecting text and before past- ing 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. To view the root window when it is obscured, click the Show Desktop button at the left end of the Bottom panel to minimize the windows in the workspace. 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 Appearance Preferences window (page 102) to the Background tab. 1. These control 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. You must either 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).114 Chapter 4 Introduction to Ubuntu Linux 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-17), select Main menu: ApplicationsDAccessoriesDTerminal or enter the command gnome- terminal 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 124), which displays Linux manual pages. Chapter 5 describes utilities that you can run from a terminal emulator. 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 xeyes, 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 com- mand 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 com- piler, a Linux utility, or another standard program; or displaying an error message indicating that 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 name. Refer to Chapter 7 for more information on the shell. Figure 4-17 A Terminal terminal emulator windowGetting the Most out of the Desktop 115 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 selec- tions. Figure 4-18 shows context menus for a OpenOffice.org spreadsheet file and for a plain text file. Table 4-1 lists some common Object Context menu selections. Figure 4-18 The Object Context menus for a spreadsheet (left) and a text file (right) Table 4-1 Common Object Context menu selections Open 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. Open in New Window (From a File Browser window only.) Opens a folder in a new File Browser win- dow instead of replacing the contents of the current window. Same as holding SHIFT while double-clicking a folder in a Browser window. Open with "App" 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 118 for information on changing this default. Open with ➤ 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 sub- menu. Each submenu selection is an Open with "App" selection (above). The last selection in the submenu is Open with Other Application (below). Browse Folder (On the desktop only.) Opens a folder in a File Browser window. Same as double- clicking a folder on the desktop.116 Chapter 4 Introduction to Ubuntu Linux The Object Properties Window The Object Properties window displays information about a file, such as who owns it, 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. Display this window by right-clicking an object and selecting Properties from the drop-down menu. The Properties window initially dis- plays some basic information. Click the tabs at the top of the window to display additional information. Different types of files display different sets of tabs. You can modify the settings in this window only if you have permission to do so. This section describes the five tabs common to most Object Properties windows. Basic The Basic tab displays information about the file, including its MIME type, and enables you to select a custom icon for the file and change its name. Change the name of the file 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 with Other Application Displays the Open With menu. This menu allows you to select an application to open this type of file; the next time you use the Object Context menu to open this type of file, the application you selected appears as an Open with "App" selection (above). Does not change the default application for this type of file. See page 118 for information on changing the default application. Cut Removes the object and places it on the clipboard (page 113). Copy Copies the object to the clipboard (page 113). Extract Here 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. Make Link Creates a link to the object in the same directory as the object. You can then move the link to different directory where it may be more useful. Move to Trash Moves the object to the trash (page 100). Send to Opens a Send To window that allows you to email the object. Create Archive Opens the Create Archive window which allows you to specify a format and a name for an archive of one or more objects (page 264). Share folder Opens the Share Folder window, which allows you to share a folder using NFS (Chapter 23) or Samba (Chapter 24), depending on which is installed on the local system. Select Main Menu: SystemDAdministrationDShared Folders to display the Shared Folders window, which lists folders that are shared from the local system. Requires root privileges. Properties Displays the Object Properties window. Table 4-1 Common Object Context menu selections (continued)Getting the Most out of the Desktop 117 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. This technique does not work for files that are links (indicated by the arrow emblem at the upper right of the object). Emblems The Emblems tab (Figure 4-19, left) allows you to add and remove emblems associ- ated with the file by placing (removing) a tick in the check box next to an emblem. Figure 4-15 on page 111 shows some emblems on file objects. Nautilus displays emblems in both its Icon and List views, although there may not be room for more than one emblem in the List view. Emblems are displayed on the desktop as well. You can also place an emblem on an object by dragging the emblem from the Side pane/Emblems and dropping it on an object in the View pane (page 261) of a File Browser window. Drag the Erase emblem to an object to remove most emblems from the object. Permissions The Permissions tab (Figure 4-19, right) allows the owner of a file to change the file’s permissions (page 199) and to change the group (see /etc/group on page 558) that 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 gksudo nautilus opens a File Browser window running with root privileges (but read the caution on page 88). 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 201), group, and others read or read and write permission for a file. 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 200) does. Figure 4-19 The Object Properties window: Emblems tab (left); Permissions tab (right)118 Chapter 4 Introduction to Ubuntu Linux Permissions for a directory work as explained on page 202. 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 be set to None. The tri-state check box labeled Execute does not apply to the directory; it applies to the files in the directory. A tick in this check box gives everyone execute access to these files; a hyphen does not change exe- cute permissions of the files; and an empty check box removes execute access for everyone from these files. 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 deter- mines which application or utility it will use to open the file. GNOME uses several techniques to determine the MIME (page 1048) type of a file and selects the default application based on that determination. The Open With tab (Figure 4-20) 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. Click the radio button next to an application to cause that application to become the default application that GNOME uses to open this type of file. When a file has fewer than four applications in the Open With tab, the Object Con- text menu displays all applications in that menu. With four or more applications, the Object Context menu uses an Open With submenu (Figure 4-20). Notes The Notes tab provides a place to keep notes about the file. Figure 4-20 The Object Properties window, Open With tab, and the Object Context menu, Open With submenu for the same fileUpdating, Installing, and Removing Software Packages 119 Updating, Installing, and Removing Software Packages Ubuntu software comes in packages that include all necessary files, instructions so that a program can automatically install and remove the software, and a list of other packages that the package depends on. There are many ways to search for and install software packages. The Update Notifier (page 100) prompts you each time updates are available for software on the system. The Software Sources window (discussed next) is an easy way to install popular software. Synaptic (page 121) is more complex and gives you a wider selection of soft- ware. Chapter 14 explains how to work with software packages from the com- mand line. Software Sources Window Repositories Repositories hold collections of software packages and related information. The Software Sources window controls which categories of packages Ubuntu installs, which repositories it downloads the packages from, how automatic updating works, and more. Open this window by selecting Main menu: SystemD AdministrationDSoftware Sources (you will need to supply your password) or by giving the command gksudo software-properties-gtk from a terminal emulator or Run Application window (ALT-F2). The Software Source window has five tabs, which are discussed next. Ubuntu Software The Ubuntu Software tab controls which categories of packages (page 588) APT (page 588) and Synaptic install and the Update Manager updates automatically. Typically all categories have ticks in their check boxes except for Source code. Put a tick in this check box if you want to download source code. If the drop- down list labeled Download from does not specify a server near you, use the list to specify one. If the system does not have an Internet connection, put a tick in one of the check boxes in the drop-down list labeled Installable from CD-ROM/DVD; APT will then install software from that source. If you do have an Internet connection, remove the tick from that check box. You can specify a new CD/DVD in the Third- Party Software tab. Add only repositories you know to be trustworthy security Adding software from other than the Ubuntu repositories can cause the system to not work prop- erly and cause updates to fail. Even worse, it can make the system vulnerable to attack. Do not add a third-party repository unless you trust it implicitly.120 Chapter 4 Introduction to Ubuntu Linux Third-Party Software You can add, edit, and remove repositories from the Third-Party Software tab. (See the adjacent security box concerning adding repositories.) Unless you are working with software that is not distributed by Ubuntu, you do not need to add any reposi- tories. To add a CD/DVD as a repository, click Add CD-ROM. Updates The top part of the Updates tab (Figure 4-21) specifies which types of updates you want the Update Manager to download. Typically you will want to download important security updates and recommended updates. In the bottom part of this tab you can specify if and how often the Update Manager will check for updates and what to do when it finds updates. Authentication The Authentication tab holds keys for trusted software providers. Ubuntu uses keys to authenticate software, which protects the system against malicious software. Typically Ubuntu provides these keys automatically. Statistics The Statistics tab allows you to participate in a software popularity contest. Add/Remove Applications The Add/Remove Applications window (Figure 4-22) adds and removes applica- tions from the system. It is simpler and has fewer selections than Synaptic (described next). Open this window by selecting Main menu: ApplicationsD Add/Remove or by giving the command gnome-app-install from a terminal emula- tor or Run Application window (ALT-F2). Maximizing this window may make it easier to use. Figure 4-21 The Software Sources window, Updates tabUpdating, Installing, and Removing Software Packages 121 Enter the name or part of the name of an application in the text box labeled Search at the top of the window and press RETURN to search for an application. Unless you want to limit selections, select All available applications in the drop-down list labeled Show. You can select a category of applications from the list at the left of the window. Scroll through the applications displayed at the right 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 tick marks from applications you want to remove. Click Apply Changes to implement the changes you have marked. This utility summarizes the changes you have requested and asks if you want to apply them. Click Apply. Because you need to work with root privileges to install and remove software, the utility may ask for your password. When it is finished it tells you it has been successful. Click Close. Packages you installed should be avail- able on the Main menu. optional Synaptic: Finds, Installs, and Removes Software This section describes how to use Synaptic to find, download, install, and remove software packages. Open the Synaptic Package Manager window by selecting Figure 4-22 The Add/Remove Applications window122 Chapter 4 Introduction to Ubuntu Linux System: AdministrationDSynaptic Package Manager from the Main menu or by giving the command gksudo synaptic from a terminal emulator or Run Applica- tion window (ALT-F2). Figure 4-23 shows the initial window. The first time you run Synaptic, it reminds you to reload package information regularly. You can do so by clicking Reload on the toolbar. The Synaptic Package Manager window displays a lot of information. Maximizing this window and widening the left column (by dragging the handle) may make it easier to use. When the Sections button is highlighted in the left column, the top of the left column holds a list box containing categories of software. Initially All is selected in this list box, causing the window to display all software packages in the list box at the top of the right column. You can shorten the list of packages in the list box by selecting a category in the cate- gory list box or by searching for a package. To search for a package, display the Find window by clicking Search on the toolbar. Enter the name or part of the name of the package you are looking for. For example, to display all packages related to exim4, enter exim4 in the text box labeled Search and select Description and Name from the Figure 4-23 The Synaptic Package Manager window Figure 4-24 The Find window Category list box Handle Sections buttonUpdating, Installing, and Removing Software Packages 123 drop-down list labeled Look in (Figure 4-24). Click Search. The Synaptic Package Manager window displays the list of packages meeting the search criteria specified in the list box at the top of the right column. When you click a package name in this list, Synaptic displays a description of the package in the frame below the list. The following example explains how to use Synaptic to locate, download, and install a chess program. With the Synaptic Package Manager window open, search for chess. Synaptic displays a list of chess-related packages in the righthand list box. Click several packages, one at a time, reading the descriptions in the frame at the lower right of the window. Assume you decide to install Dream Chess (the dreamchess package, www.dreamchess.org). When you click the check box to the left of dreamchess, Synaptic displays a list of options. Because this package is not installed, all selections except Mark for Installation are grayed out (Figure 4-25). Click this selection. Because the dreamchess package is dependent on other pack- ages that are not installed, Synaptic displays a window asking if you want to mark additional required changes (Figure 4-26 on the next page). This window lists additional packages Synaptic needs to install so that Dream Chess will run. Click Mark to mark the additional packages. All packages marked for installation are highlighted in green. To apply the changes you have marked, click Apply on the toolbar. Synaptic dis- plays a Summary window. If you were installing and/or removing several packages, this summary would be longer. Click Apply. Synaptic keeps you informed of its progress. When it is done, it displays the Changes Applied window. Click Close and then close the Synaptic Package Manager window. Now Dream Chess appears on the Main menu: ApplicationsDGames menu. Figure 4-25 The Synaptic Package Manager window displaying chess programs124 Chapter 4 Introduction to Ubuntu Linux Where to Find Documentation Distributions of Linux, including Ubuntu, 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. Ubuntu provides a graphical help center. Not surprisingly, with the 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 infor- mation on Linux in general and Ubuntu Linux in particular. See also Appendix B. Ubuntu Help Center To display the Ubuntu Help Center window (Figure 4-27), click the blue object with a question mark in it on the Top panel or select Main menu: SystemDHelp and Sup- port. 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 labeled Search. man: Displays the System Manual In addition to the Graphical Ubuntu Help Center, 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 win- dow (page 114) to run it. You can also log in on a virtual terminal (page 136) and run man from there. Figure 4-26 Mark additional required changes screenWhere to Find Documentation 125 To find out more about a utility, give the command man, followed by the name of the utility. Figure 4-28 shows man displaying information about itself; the user entered a man man command. less (pager) The man utility automatically sends its output through a pager—usually less (page 148), which displays one screen at a time. When you access a manual page in this manner, 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 Figure 4-27 The Ubuntu Help Center window Figure 4-28 The man utility displaying information about itself126 Chapter 4 Introduction to Ubuntu Linux another screen of text by pressing the SPACE bar. 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 (page 165). Based on the FHS (Filesystem Hierarchy Standard, page 194), the Linux system manual and the man pages are divided into ten sections, where each section describes related tools: 1. User Commands 2. System Calls 3. Subroutines 4. Devices 5. File Formats 6. Games 7. Miscellaneous 8. System Administration 9. Kernel 10. 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 infor- mation 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 $ man 5 passwd The preceding command instructs man to look only in section 5 for the man page. In documentation you may 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 the next man page). For example, give the command man –a passwd to view all man pages for passwd. info: Displays Information About Utilities The textual info utility is a menu-based hypertext system developed by the GNU project (page 2) and distributed with Ubuntu Linux. The info utility includes a 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 1023) to the command fol- low the option and a SPACE. For more information refer to “Options” on page 221.Where to Find Documentation 127 tutorial on itself (go to www.gnu.org/software/texinfo/manual/info) and documen- tation on many Linux shells, utilities, and programs developed by the GNU project. Figure 4-29 shows the screen that info displays when you give the com- mand info coreutils (the coreutils software package holds the Linux core utilities). Because the information on this screen is drawn from an editable file, your display may differ from the screens shown in this section. When you see the initial info screen, you can press any of the following keys or key combinations: • h to go through an interactive tutorial on info • ? to list info commands • SPACE to scroll through the menu of items for which information is available • m followed by the name of the menu you want to display or a SPACE to dis- play a list of menus • q or CONTROL-C to quit The notation info uses to describe keyboard keys may 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.) Figure 4-29 The screen info coreutils displays 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.128 Chapter 4 Introduction to Ubuntu Linux After giving the command info, press the SPACE bar a few times to scroll through the display. Figure 4-30 shows the entry for sleep. 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, use the ARROW keys to move the cursor to the line containing the menu item and press RETURN. Alternatively, you can type the name of the menu item in a menu com- mand 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 and displays Menu item:. Typing sleep displays sleep on that line, and pressing RETURN displays information about the menu item you have chosen. Figure 4-31 shows the top node of information on sleep. A node groups a set of information you can scroll through with 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 with the man utility using 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 www.tldp.org, help.ubuntu.com, help.ubuntu.com/community, or answers.launchpad.net/ubuntu and print the desired information from the browser. Figure 4-30 The screen info displays after you type /sleepRETURN You may 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. Use Synaptic to install the pinfo package if you want to experiment with it. Run pinfo from a terminal emulator or Run Application window (ALT-F2) and select Run in terminal).Where to Find Documentation 129 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. Non- GNU utilities may 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 equivalent to -vET -b, --number-nonblank number nonblank output lines -e equivalent to -vE -E, --show-ends display $ at end of each line ... If the information that ––help displays runs off the screen, send the output through the less pager (page 125) 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 gen- erally are responsible for writing and maintaining a HOWTO document, but many people may contribute to it. The Linux Documentation Project (LDP, page 131) site houses most HOWTO and mini-HOWTO documents. Use a Web browser to visit www.tldp.org, click HOWTOs, Figure 4-31 The info page on the sleep utility130 Chapter 4 Introduction to Ubuntu Linux 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 with the System GNOME provides tooltips (page 106), a context-sensitive Help system, and Ubuntu provides the help center discussed on page 124. Finding Help Locally /usr/share/doc The /usr/src/linux/Documentation (present only if you installed the kernel source code as explained in Chapter 16) and /usr/share/doc directories often contain more detailed and different information about a utility than man or info provides. Frequently this information is meant for people who will be compiling and modify- ing the utility, not just using it. These directories hold thousands of files, each con- taining information on a separate topic. Using the Internet to Get Help The Internet provides many helpful sites related to Linux. Aside from sites that carry 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, 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-32. Ubuntu Web sites The Ubuntu Web site is a rich source of information. The following list identifies some locations that may be of interest: Figure 4-32 Google reporting on an error messageWhere to Find Documentation 131 • Ubuntu documentation is available at help.ubuntu.com. • Ubuntu community documentation is available at help.ubuntu.com/community. • You can find answers to many questions at answers.launchpad.net/ubuntu. • The Ubuntu forums (ubuntuforums.org) is a good place to find answers to questions. • You can talk with other Ubuntu users using IRC (Internet relay chat). See help.ubuntu.com/community/InternetRelayChat for a list of Ubuntu IRC channels available via the Freenode IRC service. • You can subscribe to Ubuntu mailing lists. See lists.ubuntu.com. • You can search for information about packages and find out which pack- age contains a specific file at packages.ubuntu.com. GNU GNU manuals are available at www.gnu.org/manual. In addition, you can visit the GNU home page (www.gnu.org) for more documentation and other GNU resources. Many of the GNU pages and resources are available in a variety of languages. The Linux Documentation Project The Linux Documentation Project (www.tldp.org), 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 (Figure 4-33) 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. Figure 4-33 The Linux Documentation Project home page132 Chapter 4 Introduction to Ubuntu Linux More About Logging In Refer to “Logging In on the System” on page 89 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. The Login Screen At the lower-left corner of the Login screen is a small object labeled Options (Figure 4-1, page 90). Click this object or press F10 to display the Actions menu, which has the following selections: • Select Language Displays a window from which you can select the lan- guage for the session you are about to start. This change affects window titles, prompts, error messages, and other textual items displayed by GNOME and many applications. Just after you log in, the system asks whether you want to make the language you specified the default language or just use it for this session. • Select Session Displays the Sessions dialog box, which presents several choices concerning the session you are about to start. Choose one of the following, click Change Session, and continue logging in: ◆ Last Session Brings up the same desktop environment you used the last time you logged in. This choice is the default. ◆ Run Xclient script Brings up the default desktop environment. ◆ GNOME Brings up the GNOME desktop environment. ◆ KDE Brings up the KDE desktop environment (if you have installed Kubuntu or KDE, see page 60). ◆ Failsafe GNOME Brings up a default GNOME session without run- ning any startup scripts. Use this choice to fix problems that prevent you from logging in normally. ◆ Failsafe Terminal Brings up an xterm terminal emulator window without a desktop manager and without running any startup scripts. This setup allows you to log in on a minimal desktop when your stan- dard login does not work well enough to allow you to log in to fix a problem. Give the command exit from the xterm window to log out and display the Login screen. 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 prac- tice to maintain a user account without a password.More About Logging In 133 Just after you log in, the system asks whether to use your selection from the Sessions dialog box just for this session or permanently. The failsafe logins do not ask this question. • Restart Shuts down and reboots the system. • Shut Down Shuts down the system and turns off the power. • Suspend Puts the system in power-saving mode. Exactly what this selec- tion does depends on the hardware. What to Do If You Cannot Log In If you enter either your username or 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 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 combina- tion may not be valid if you are trying to log in on the wrong machine. On a larger, networked system, you may have to specify the machine you want to connect to before you can log in. • Your username is not valid. The login/password combination may not be valid if you have not been set up as a user. If you are the system adminis- trator, refer to “Configuring User and Group Accounts” on page 658. Otherwise, check with the system administrator. • A filesystem is full. When a filesystem critical to the login process is full, it may appear as though you have logged in successfully, but after a moment the login screen reappears. You must log in using one of the failsafe logins and delete some files. Refer to “Changing Your Password” on page 135 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.134 Chapter 4 Introduction to Ubuntu Linux 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 con- nected network, you use ssh (secure, page 714) or telnet (not secure, page 373) 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, PC, or UNIX machine, open the program that runs ssh and give it the name or IP address (refer to “Host Address” on page 363) 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 114. 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 sys- tem displays a message called issue (stored in the /etc/issue file) that identifies the version of Ubuntu Linux running on the system. A sample issue message follows: Ubuntu 7.10 tiny tty1 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 1064) 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. If you are using a workstation (page 1069), run ssh (page 714), telnet (page 373), or whatever communications/emulation software you use to log in on the system. Log in. Next the shell prompt (or just prompt) appears, indicating you have successfully logged in; it indicates the system is ready for you to give a command. The first shell prompt line may be preceded by a short message called the message of the day, or motd (page 559), which is stored in the /etc/motd file. Ubuntu Linux establishes a prompt of [user@host: directory]$, where user is your username, host is the name of 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 988. Did you log in last? security As you are logging in to a textual environment, after you enter your username and password, the system displays 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 else has accessed the account since you last used it. If someone has, perhaps an unauthorized user has learned your password and logged on as you. In the interest of maintaining security, advise the system admin- istrator of any circumstances that make you suspicious and change your password.More About Logging In 135 the local 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 303. 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. To change your password, select Main menu: SystemDPreferencesDAbout Me and click Change Password. From a command line, give the command passwd. 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. A password should contain a combination of numbers, uppercase and lowercase let- ters, and punctuation characters and meet the following criteria to be relatively secure: • Must be at least four 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 user- name shifted by one or more characters. 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 as modern dictionary crackers may also try these permutations. 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.136 Chapter 4 Introduction to Ubuntu Linux Only the first item is mandatory. Avoid using control characters (such as CONTROL-H) because they may 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 682 for more information about choosing a password. After you enter your new password, the system asks you to retype it to make sure 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 mes- sage 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 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. This book refers to the console you see when you press CONTROL-ALT-F1 as the system console, or just console. By default, 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 tex- tual session on one virtual console and a graphical session on another. No matter which virtual console you start a graphical session from, the graphical session runs on the first unused virtual console (number seven by default). 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, a textual virtual console, or when you use ssh (page 711) or telnet (insecure, page 373) 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.Working from the Command Line 137 Advantages of the CLI Although the concept may seem antiquated, the CLI has a place in modern comput- ing. In some cases an administrator may 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 may 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 mandates that a server system run as few tasks as possible because each additional task can make the system more vulnerable to attack. Pseudographical interface Before the introduction of GUIs, resourceful programmers created textual interfaces that included graphical elements such as boxes, borders outlining rudimentary win- dows, highlights, and, more recently, color. These textual interfaces, called pseudo- graphical interfaces, bridge the gap between textual and graphical interfaces. One example of a modern utility that uses a pseudographical interface is the dpkg- reconfigure utility, which reconfigures an installed software package. Correcting Mistakes This section explains how to correct typographical and other errors you may make while you are logged in on a textual display. Because the shell and most other utili- ties do not interpret the command line or other text until after you press RETURN, you can readily correct typing mistakes before you press RETURN. You can correct typing 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: You must either wait for the command to run to completion or abort execution of the program (page 138). 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 DELETE or CONTROL-H. If these keys do not work, give the following stty2 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 2. The command stty is an abbreviation for set teletypewriter, the first terminal that UNIX was run on. Today stty is commonly thought of as set terminal.138 Chapter 4 Introduction to Ubuntu Linux 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. Deleting a Line Any time before you press RETURN, you can delete the line you are entering by press- ing 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 may want to terminate a running program. For example, you may want to stop a program that is performing a lengthy task such as displaying the con- tents of a file that is several hundred pages long or copying a 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 terminal interrupt 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 immediately, some ignore the signal, and some take other actions. When it receives a terminal interrupt signal, the shell displays a prompt and waits for another command. If these methods do not terminate the program, try stopping the program with 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 num- ber within the brackets at the left end of the line that jobs displays ([1]). The kill command (page 522) uses –TERM to send a termination signal3 to the job specified by the job number, which is preceded by a percent sign (%1): $ bigjob ^Z [1]+ Stopped bigjob $ jobs [1]+ Stopped bigjob CONTROL-Z suspends a program tip Although it is not a way of correcting a mistake, you may press the suspend key (typically CONTROL-Z) by mistake and wonder what happened (you will see a message containing the word Stopped). You have just stopped your job, using job control (page 290). Give the command fg to continue your job in the foreground, and 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 291. 3. When the terminal interrupt signal does not work, use the kill (–KILL) signal. A running program can- not ignore a kill signal; it is sure to abort the program (page 522).Controlling Windows: Advanced Operations 139 $ kill -TERM %1 $ RETURN [1]+ Killed bigjob The kill command returns a prompt; press RETURN again to see the confirmation mes- sage. For more information refer to “Running a Program in the Background” on page 237. 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 reexecute the displayed command line, press RETURN. Press the DOWN ARROW key to browse through the command lines in the other direction. 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 314. optional Controlling Windows: Advanced Operations Refer to “Windows” on page 111 for an introduction to working with windows under Ubuntu Linux. This section explores changing the input focus on the work- space, changing the resolution of the display, and understanding more about the window manager. Changing the Input Focus When you type on the keyboard, the window manager (page 141) directs the char- acters you type somewhere, usually to a window. The active window (the window accepting input from the keyboard) is said to have the input focus. Depending on how you set up your account, you can use the mouse in one of three ways to change the input focus (you can also use the keyboard; see page 112): • Click-to-focus (explicit focus) Gives the input focus to a window when you click the window. That window continues to accept input from the keyboard regardless of the location of the mouse pointer. The window loses the focus when you click another window. Although clicking the middle or the right mouse button also activates a window, use only the left mouse button for this purpose; other buttons may have unexpected effects when you use them to activate a window. • Focus-follows-mouse (sloppy focus, enter-only, or focus-under-mouse) Gives the input focus to a window when you move the mouse pointer onto the window. That window maintains the input focus until you move the 140 Chapter 4 Introduction to Ubuntu Linux mouse pointer onto another window, at which point the new window gets the focus. Specifically, when you move the mouse pointer off a window and onto the root window, the window that had the input focus does not lose it. • Focus-strictly-under-mouse (enter-exit) Gives the input focus to a win- dow when you move the mouse pointer onto the window. That window maintains the input focus until you move the mouse pointer off if it, at which point no window has the focus. Specifically, when you move the mouse pointer off a window and onto the root window, the window that had the input focus loses it, and input from the keyboard is lost. GNOME The Window Preferences window changes the focus policy. To display this window, select Main menu: SystemDPreferencesDWindows or give the command gnome- window-properties from a terminal emulator or Run Application window (ALT-F2). Put a tick in the check box next to Select windows when the mouse moves over them to select the focus-follows-mouse policy. When there is no tick in this check box, click-to-focus is in effect. Click Close. Focus-strictly-under-mouse is not avail- able from this window. To determine which window has the input focus, compare the window borders. The border color of the active window is different from the others or, on a monochrome display, is darker. Another indication that a window is active is that the keyboard cursor is a solid rectangle; in windows that are not active, the cursor is an outline of a rectangle. Use the following tests to determine which keyboard focus method you are using. If you position the mouse pointer in a window and that window does not get the input focus, your window manager is configured to use the click-to-focus method. If the border of the window changes, you are using the focus-follows-mouse or focus- strictly-under-mouse method. To determine which of the latter methods you are using, start typing something, with the mouse pointer positioned on the active win- dow. Then move the mouse pointer over the root window and continue typing. If characters continue to appear within the window, you are using focus-follows- mouse. Otherwise, you are using focus-strictly-under-mouse. Changing the Resolution of the Display The X server (the basis for the Linux graphical interface; see page 252) starts at a specific display resolution and color depth (page 1029). Although you can change the color depth only when you start an X server, you can change the resolution while the X server is running. The number of resolutions available depends both on the display hardware and on the configuration of the X server (see page 75 for details). Many users prefer to do most of their work at a higher resolution but might want to switch to a lower resolution for some tasks, such as playing games. You can switch between display resolutions by pressing either CONTROL-ALT-KEYPAD- + or CONTROL-ALT-KEYPAD- –, using the + and – on the keyboard’s numeric keypad. You canControlling Windows: Advanced Operations 141 also use the Screen and Graphics Preferences window to change the resolution of the display (page 75). Changing to a lower resolution has the effect of zooming in on the display; as a result, you may no longer be able to view the entire workspace at once. You can scroll the display by pushing the mouse pointer against the edge of the screen. 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 man- ager 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 may also handle some session management functions, such as how a session is paused, resumed, restarted, or ended (page 104). 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. The window manager takes care of window manipulation so client programs do not need to. 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 manage win- dows—they provide a useful, good-looking, graphical shell to work from. Their open design allows users to define their own policy 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. Ubuntu Linux Window Managers Metacity and Compiz—the default window managers for Ubuntu GNOME—provide window management and start many components through GNOME panel objects. They also communicate with and facilitate access to other components in the environ- ment. The Visual Effects tab of the Appearance Preferences window (page 103) allows you to switch between Metacity and Compiz. Using the standard X libraries, programmers have created other window managers, including blackbox, fluxbox, and WindowMaker. You can use Synaptic (page 121) to install any of these packages.142 Chapter 4 Introduction to Ubuntu Linux Using a Window Manager Without a Desktop Manager It is interesting to see exactly where the line that separates the window manager and the desktop manager falls. Toward this end, you can run the Failsafe Terminal from the Login screen: Specify Options: Select SessionDFailsafe Terminal and log in. You should see a clean screen with an undecorated window running xterm. You can give commands from this window to open other windows. Try xeyes, xterm, and xclock. Give the command exit to return to the Login screen. Chapter Summary As with many operating systems, your access to a Linux system is authorized when you log in. 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 may act as the system administrator, or this job may 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 uses sudo to obtain extra privileges, called root privileges. An administrator working with root privileges is sometimes referred to as Superuser. Do not work with root privileges as a matter of course. When you have to do some- thing 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 Ubuntu GUI. The 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 use to customize and maintain the system and to start many common appli- cations. 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 the casual user, the office worker, the power user, and the programmer/system designer a space to work in and a set of tools toExercises 143 work with. GNOME also provides off-the-shelf productivity and many ways to customize its look, feel, and response. Nautilus is GNOME’s simple, yet powerful file manager. It can create, open, dis- play, 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 may 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: Incorrect username or password. Letters must be typed in the correct case. This message does not indicate whether your username, your password, or both are invalid. Why does it not reveal this information? 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. What is the input focus? When no window has the input focus, what hap- pens to the letters you type on the keyboard? Which type of input focus would you prefer to work with? Why?144 Chapter 4 Introduction to Ubuntu Linux 9. What are the functions of a Window Operations menu? How do you dis- play 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 Win- dow List applets do? 11. What are tooltips? How are they useful? Advanced Exercises 12. What change does the mouse pointer undergo when you move it to the edge of a window? What happens when you right-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. Try the experiment described in “Using a Window Manager Without a Desktop Manager” on page 142. What is missing from the screen? Based only on what you see, 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 vim from the Run Command window with- out specifying that it be run in a terminal? Where does the output go? 16. The example on page 126 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.)145145 5Chapter5When 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. All the tools ran from a command line. Today the Linux GUI is important but many people—especially system administrators—run many command line programs. Command line utilities are often faster, more powerful, or more complete than their GUI counterparts. Some- times there is no GUI counterpart to a textual utility; some peo- ple just prefer the hands-on feeling of the command line. When you work with a command line interface, you are work- ing with a shell (Chapters 7, 9, and 11). 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 manipu- lation utilities as well as utilities that find out who is logged in; that communicate with other users; that print, compress, and decompress files; and that pack and unpack archive files. In This Chapter Special Characters . . . . . . . . . . . . 146 Basic Utilities . . . . . . . . . . . . . . . . 147 less Is more: Display a Text File One Screen at a Time . . . . . . . . 148 Working with Files. . . . . . . . . . . . . 149 lpr: Prints a File . . . . . . . . . . . . . . . 151 | (Pipe): Communicates Between Processes. . . . . . . . . . . . . . . . . . 156 Compressing and Archiving Files . . . . . . . . . . . . . . . . . . . . . . 159 Obtaining User and System Information . . . . . . . . . . . . . . . . 166 Tutorial: Creating and Editing a File with vim . . . . . . . . . . . . . . . . . . . 172 5 The Linux Utilities146 Chapter 5 The Linux Utilities Special Characters Special characters, which have a special meaning to the shell, are discussed in “File- name Generation/Pathname Expansion” on page 239. These characters are men- tioned here so that 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 also 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. Quoting special characters 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 char- acter as a regular character. However, a slash (/) is always a separator in a path- name, even when you quote it. Backslash 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 (\\). Single quotation marks Another way of quoting special characters is to enclose them between single quota- tion marks: '**'. You can quote many special and regular characters between a pair of single quotation marks: 'This is a special character: >'. The regular charac- ters 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 (see the od man page) to display CONTROL-U as octal 25 (025): 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 114), or a virtual console (page 136).Basic Utilities 147 $ echo xxxxxxCONTROL-V CONTROL-U | od -c 0000000 x x x x x x 025 \n 0000010 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 utili- ties 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 character-based interface. Some of the more important utilities are also available from a GUI; others are avail- able only from a GUI. Folder 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 direc- tory does. 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 (next page), ls lists the name of the practice file. (You may also see files the system or a program created automatically.) Subsequent com- mands in Figure 5-1 display the contents of the file and remove the file. These com- mands 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 230 shows how to use cat to string together the contents of three files.) A convenient way to display the contents of a file to the screen is by giving the com- mand cat, followed by a SPACE and the filename. 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. 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.148 Chapter 5 The Linux Utilities 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. 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. 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 EOF (end of file) message and waits for you to press q before returning you to the shell. In contrast, more returns you directly to the shell. In both utilities you can press h to display a Help screen that lists com- mands 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 com- mands work. Use the command less /etc/termcap instead if you want to experiment with a longer file. Refer to the less man page for more information. $ 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 A safer way of removing files tip You can use the interactive form of rm to make sure that you delete only the file(s) you intend to delete. When you follow rm with the –i option (see page 126 for a tip on options) and the name of the file you want to delete, rm displays the name of the file and then 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 does not begin with y. $ rm -i toollist rm: remove regular file 'toollist'? y Optional: You can create an alias (page 328) for rm –i and put it in your startup file (page 188) so that rm always runs in interactive mode.Working with Files 149 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 right machine. $ hostname bravo.example.com Working with Files This section describes utilities that copy, move, print, search through, display, sort, and compare files. 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. 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 that 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. 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. When 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 324. $ ls memo $ cp memo memo.copy $ ls memo memo.copy Figure 5-2 cp copies a file150 Chapter 5 The Linux Utilities Sometimes it is useful to incorporate the date in 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 that 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 infor- mation refer to “Filenames” on page 185. Use scp (page 711) or ftp (page 729) when you need to copy a file from one system to another on a common network. mv: Changes the Name of a File The mv (move) utility can rename a file without making a copy of it. The mv com- mand 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 earlier cp example. 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 197. See the mv info page for more information. cp can destroy a file caution If the destination-file exists before you give a cp command, cp overwrites it. Because cp over- writes (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 still need. The cp –i (interactive) option prompts you before it overwrites a file. See page 126 for a tip on options. The following example assumes that the file named orange.2 exists before you give the cp com- mand. The user answers y to overwrite the file: $ cp –i orange orange.2 cp: overwrite 'orange.2'? y 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.” $ ls memo $ mv memo memo.0130 $ ls memo.0130 Figure 5-3 mv renames a fileWorking with Files 151 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 that 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 sin- gle printer with the expected results. On 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 Total Size active alex 86 (standard input) 954061 bytes In this example, Alex 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 You can send more than one file to the printer with a single command. The follow- ing command line prints three files on the printer named laser1: $ lpr -P laser1 05.txt 108.txt 12.txt Refer to Chapter 15 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. 1. Originally the name grep was a play on an ed—an original UNIX editor, available on Ubuntu Linux—command: g/re/p. In this command g stands for global, re is a regular expression delimited by slashes, and p means print.152 Chapter 5 The Linux Utilities The grep command in Figure 5-4 searches through the file memo for lines that con- tain the string credit and displays a single line that meets this criterion. If memo contained such words as discredit, creditor, or accreditation, grep would have dis- played those lines as well because they contain the string it was searching for. The –w 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 infor- mation. 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 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 dis- played, include a hyphen followed by the number of lines in the head command. 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 charac- ters 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, $ cat memo Helen: In our meeting on June 6 we discussed the issue of credit. Have you had any further thoughts about it? Alex $ grep 'credit' memo discussed the issue of credit. Figure 5-4 grep searches for a stringWorking with Files 153 and display lines being added to a file that is changing. The following command causes tail to display the last five lines, Aug through Dec, of the months file shown in Figure 5-5: $ tail -5 months Aug Sep Oct Nov Dec 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. sort: Displays a File in Order The sort utility displays the contents of a file in order by lines but does not change the original file. $ cat months Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec $ head months Jan Feb Mar Apr May Jun Jul Aug Sep Oct Figure 5-5 head displays the first ten lines of a file154 Chapter 5 The Linux Utilities For example, if a file named days contains the name of each day of the week in cal- endar order, each on a separate line, then sort displays the file in alphabetical order (Figure 5-6). 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 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, but 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, so 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 (+) 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 $ 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 orderWorking with Files 155 sign) from the first line through the sixth line. The +1,5 then indicates that 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 nothing. A leading minus sign indicates that the line occurs only in the file denoted by the minus sign. A leading plus sign indicates that the line comes from the file denoted by the plus sign. A line that begins with 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. file: Tests 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 $ cat dups Cathy Fred Joe John Mary Mary Paula $ uniq dups Cathy Fred Joe John Mary Paula Figure 5-7 uniq removes duplicate lines $ diff -u colors.1 colors.2 --- colors.1 Fri Nov 28 15:45:32 2007 +++ colors.2 Fri Nov 28 15:24:46 2007 @@ -1,6 +1,5 @@ red +blue green yellow -pink -purple orange Figure 5-8 diff displaying the unified output format156 Chapter 5 The Linux Utilities 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, they are introduced here for use in examples. Pipes are covered in detail beginning on page 234. A process is the execution of a command by Linux (page 310). 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 keyboards) provides the simplest form of this kind of communication. Sim- ply put, a pipe takes the output of one utility and sends that output as input to another utility. Using UNIX/Linux terminology, a pipe takes standard output of one process and redirects it to become standard input of another process. (For more information refer to “Standard Input and Standard Output” on page 226.) Most of what a process displays on the screen is sent to standard output. If you do not redi- rect it, this output appears on the screen. Using a pipe, you can redirect the output so that it becomes instead standard input of another utility. For example, a utility such as head can take its input from a file whose name you specify on the command line following the word head, or it can take its input from standard input. Thus, you can give the command shown in Figure 5-5 on page 153 as follows: $ cat months | head Jan Feb Mar Apr May Jun Jul Aug Sep Oct The next command displays the number of files in a directory. The wc (word count) utility with the –w option displays the number of words in its standard input or in a file you specify on the command line: $ ls | wc -w 14 You can use a pipe to send output of a program to the printer: $ tail months | lprFour More Utilities 157 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 anything you put on the command line after echo to the screen. Some examples appear in Figure 5-9. The last example 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 pro- grams. Some examples on page 240 use echo to illustrate how special characters, such as the asterisk, work. Throughout Chapters 7, 9, and 11, echo helps explain how shell variables work and how you can send messages from shell scripts to the screen. Refer to the echo info page for more information. date: Displays the Time and Date The date utility displays the current date and time: $ date Thu Jan 24 10:24:00 PST 2008 The following example shows how you can choose the format and select the con- tents of the output of date: $ date +"%A %B %d" Thursday January 24 Refer to the date info page for more information. $ 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 screen158 Chapter 5 The Linux Utilities 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 may 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 use a different filename, follow the script command with a SPACE and the new filename. To append to a file, use the –a option after script but before the filename; otherwise script over- writes an existing file. Following is a session being recorded by script: $ script Script started, file is typescript mark@plum:~$ whoami sam $ ls -l /bin | head -5 total 5024 -rwxr-xr-x 1 root root 2928 Sep 21 21:42 archdetect -rwxr-xr-x 1 root root 1054 Apr 26 15:37 autopartition -rwxr-xr-x 1 root root 7168 Sep 21 19:18 autopartition-loop -rwxr-xr-x 1 root root 701008 Aug 27 02:41 bash $ exit exit Script done, file is typescript $ Use the exit command to terminate a script session. You can then view the file you created with cat, less, more, or an editor. Following is the file that was created by the preceding script command: $ cat typescript Script started on Mon Sep 24 20:54:59 2007 $ whoami sam $ ls -l /bin | head -4 total 5024 -rwxr-xr-x 1 root root 2928 Sep 21 21:42 archdetect -rwxr-xr-x 1 root root 1054 Apr 26 15:37 autopartition -rwxr-xr-x 1 root root 7168 Sep 21 19:18 autopartition-loop $ ls -l /bin | head -5 total 5024 -rwxr-xr-x 1 root root 2928 Sep 21 21:42 archdetect -rwxr-xr-x 1 root root 1054 Apr 26 15:37 autopartition -rwxr-xr-x 1 root root 7168 Sep 21 19:18 autopartition-loop -rwxr-xr-x 1 root root 701008 Aug 27 02:41 bash $ exit exit Script done on Mon Sep 24 20:55:29 2007Compressing and Archiving Files 159 If you will be editing the file with vim, emacs, or another editor, you can use dos2unix (below) 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 If you want to share a text file that you created on a Linux system with someone on a Windows or Macintosh system, you need to convert the file before the person on the other system can read it easily. The unix2dos utility converts a Linux text file so that it can be read on a Windows or Macintosh system. This utility is part of the tofrodos software package; give the command sudo aptitude install tofrodos to install this package. Give the following command to convert a file named memo.txt (created with a text editor) to a DOS-format file: $ unix2dos memo.txt Without any options unix2dos overwrites the original file. You can now email the file as an attachment to someone on a Windows or Macintosh system. dos2unix You can use the dos2unix utility to convert Windows or Macintosh files so they can be read on a Linux system: $ dos2unix memo.txt See the unix2dos and dos2unix man pages for more information. You can also use tr to change a Windows or Macintosh text file into a Linux text file. In the following example, the –d 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 228. 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 very often, you may want to save it on a CD, DVD, or another medium and remove it from the hard disk. If you have a continuing need for the file, retrieving a copy from a CD may 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 may fre- quently download compressed, archived files from the Internet. The utilities described in this section compress and decompress files and pack and unpack archives.160 Chapter 5 The Linux Utilities 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 sam 584000 Mar 1 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 ––verbose (or –v) 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 sam 50 Mar 1 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. In the following, more realistic example, the file zach.jpg contains a computer graphics image: $ ls -l -rw-r--r-- 1 sam sam 33287 Mar 1 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: $ 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 sam 23922 Mar 1 22:40 zach.jpg.bz2 Refer to the bzip2 man page, www.bzip.org, and the Bzip2 mini-HOWTO (see page 129) for more information. bunzip2 and bzcat: Decompress a File You can use the bunzip2 utility to restore a file that has been compressed with bzip2: $ bunzip2 letter_e.bz2 $ ls -l -rw-rw-r-- 1 sam sam 584000 Mar 1 22:31 letter_eCompressing and Archiving Files 161 $ bunzip2 zach.jpg.bz2 $ ls -l -rw-r--r-- 1 sam sam 33287 Mar 1 22:40 zach.jpg 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 con- tents of the decompressed file. Like cat, bzcat does not change the source file. The pipe in the following example redirects the output of bzcat so that 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. 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 opera- tion are very similar to those of bzip2. A file compressed by gzip is marked by 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. compress 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. tar: Packs and Unpacks Archives The tar utility performs many functions. Its name is short for tape archive, as its origi- nal 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 hierar- chies and to extract files from a tar file. The cpio utility performs a similar function. In the following example, the first ls shows the existence and 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 output dis- plays the name of the file tar is appending to the archive it is creating. 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 system running Windows. 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.162 Chapter 5 The Linux Utilities 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 d -rw-r--r-- 1 jenny jenny 1302 Aug 20 14:16 g -rw-r--r-- 1 jenny other 1178 Aug 20 14:16 b -rw-r--r-- 1 jenny jenny 3783 Aug 20 14:17 d $ tar -cvf all.tar g b d g b d $ ls -l all.tar -rw-r--r-- 1 jenny jenny 9728 Aug 20 14:17 all.tar $ tar -tvf all.tar -rw-r--r-- jenny/jenny 1302 2007-08-20 14:16 g -rw-r--r-- jenny/other 1178 2007-08-20 14:16 b -rw-r--r-- jenny/jenny 3783 2007-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 fre- quently 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. 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 exam- ple shows how to unpack the GNU make utility after it has been downloaded (ftp.gnu.org/pub/gnu/make/make-3.80.tar.gz): $ ls -l mak* -rw-rw-r-- 1 sam sam 1211924 Jan 20 11:49 make-3.80.tar.gz $ gunzip mak* $ ls -l mak* -rw-rw-r-- 1 sam sam 4823040 Jan 20 11:49 make-3.80.tar $ tar -xvf mak* make-3.80/ make-3.80/po/ make-3.80/po/Makefile.in.in ... make-3.80/tests/run_make_tests.pl make-3.80/tests/test_driver.pl 2. Although the original UNIX tar did not use a leading hyphen to indicate an option on the command line, it now accepts hyphens. The GNU tar described here will accept tar commands with or without a lead- ing hyphen. This book uses the hyphen for consistency with most other utilities.Compressing and Archiving Files 163 The first command lists the downloaded tarred and gzipped file: make-3.80.tar.gz (about 1.2 megabytes). The asterisk (*) in the filename matches any characters in any filenames (page 240), so you end up with 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.80.tar (no .gz extension), which is about 4.8 megabytes. The tar command creates the make-3.80 directory in the working directory and unpacks the files into it. $ ls -ld mak* drwxrwxr-x 8 sam sam 4096 Oct 3 2002 make-3.80 -rw-rw-r-- 1 sam sam 4823040 Jan 20 11:49 make-3.80.tar $ ls -l make-3.80 total 1816 -rw-r--r-- 1 sam sam 24687 Oct 3 2002 ABOUT-NLS -rw-r--r-- 1 sam sam 1554 Jul 8 2002 AUTHORS -rw-r--r-- 1 sam sam 18043 Dec 10 1996 COPYING -rw-r--r-- 1 sam sam 32922 Oct 3 2002 ChangeLog ... -rw-r--r-- 1 sam sam 16520 Jan 21 2000 vmsify.c -rw-r--r-- 1 sam sam 16409 Aug 9 2002 vpath.c drwxrwxr-x 5 sam sam 4096 Oct 3 2002 w32 After tar extracts the files from the archive, the working directory contains two files whose names start with mak: make-3.80.tar and make-3.80. The –d (directory) option causes ls to display only file and directory names, not the contents of directo- ries as it normally does. The final ls command shows the files and directories in the make-3.80 directory. Refer to the tar info page for more information. optional You can combine the gunzip and tar commands on one command line with a pipe (|), which redirects the output of gunzip so that it becomes the input to tar: $ gunzip -c make-3.80.tar.gz | tar -xvf - The –c option causes gunzip to send its output through the pipe instead of creating a file. Refer to “Pipes” (page 234) and gzip (page 161) for more information about how this command line works. tar: the –x option may 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 –t option and the name of the tar file. In some cases you may want to create a new directory (mkdir [page 191]), move the tar file into that directory, and expand it there. That way the unpacked files will not mingle with your existing files, and no confusion will occur. This strat- egy also makes it easier to delete the extracted files. Some tar files automatically create a new directory and put the files into it. Refer to the preceding example. 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.164 Chapter 5 The Linux Utilities 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 com- mand line to $ tar -xvzf make-3.80.tar.gz In a similar manner, the –j option calls bzip2 or bunzip2. Locating Commands The whereis and apropos utilities can help you find a command whose name you have forgotten or whose location you do not know. When multiple copies of a util- ity or program are present, which tells you which copy you will run. The locate util- ity 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 pro- gram with that name and runs the first one it finds. 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 302. If you do not change the search path, the shell searches only a standard set of directories and then stops searching. Other directories on the system may also contain useful utilities, however. 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 Linux filesystem.) The local system may include several commands that have the same name. When you type the name of a command, the shell searches for the com- mand in your search path and runs the first one it finds. You can find out which copy of the program the shell will run by using which. In the following example, which reports the location of the tar command: $ which tar /bin/tar The which utility can be helpful when a command seems to be working in unexpected ways. By running which, you may discover that you are running a nonstandard version of a tool or a different one than you expected. (“Important Standard Directories and Files” on page 194 provides a list of standard locations for executable files.) For exam- ple, if tar is not working properly and you find that you are running /usr/local/bin/tar instead of /bin/tar, you might suspect that the local version is broken. whereis The whereis utility searches for files related to a utility by looking in standard loca- tions instead of using your search path. For example, you can find the locations for files related to tar: $ whereis tar tar: /bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz In this example whereis finds three references to tar: the tar utility file, a tar header file, and the tar man page.Locating Commands 165 apropos: Searches for a Keyword When you do not know the name of the command you need to carry out a particu- lar task, you can use apropos with a keyword to search for it. This utility searches for the keyword in the short description line (the top line) of all man pages and dis- plays those that contain a match. The man utility, when called with the –k (key- word) option, gives you the same output as apropos (it is the same command). The database apropos uses, named whatis, is not on Ubuntu Linux systems when they are first installed, but is built automatically by cron (page 668) using mandb. If apro- pos does not produce any output, run the command sudo mandb. 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 man- ual that contains it, and the brief description from the top of the man page. This list includes the utility that you need (who) and identifies other, related tools that you might find useful: $ apropos who at.allow (5) - determine who can submit jobs via at or batch at.deny (5) - determine who can submit jobs via at or batch from (1) - print names of those who have sent mail w (1) - Show who is logged on and what they are doing. w.procps (1) - Show who is logged on and what they are doing. who (1) - show who is logged on ... which versus whereis tip Given the name of a program, which looks through the directories in your search path, in order, and locates the program. If the search path includes more than one program 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 commands as they are found on the disk; they do not report shell builtins (utilities that are built into a shell; see page 243). 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 445) to determine whether a command is a builtin: $ type echo echo is a shell builtin166 Chapter 5 The Linux Utilities whatis The whatis utility is similar to apropos but finds only complete word matches for the name of the utility: $ whatis who who (1) - show who is logged on slocate: Searches for a File The slocate utility searches for files on the local system: $ slocate motd /usr/share/app-install/icons/xmotd.xpm /usr/share/app-install/desktop/motd-editor.desktop /usr/share/app-install/desktop/xmotd.desktop /usr/share/base-files/motd.md5sums /usr/share/base-files/motd ... Before you can use slocate the updatedb utility must build or update the slocate data- base. Typically the database is updated once a day by a cron script (page 668). Obtaining 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. You can use the finger utility to retrieve infor- mation about users on remote systems if your computer is attached to a network. Table 5-1 on page 169 summarizes the output of these utilities. If you are not on a network, skip the rest of this chapter tip If you are the only user on a system that is not connected to a network, you may want to skip the rest of this chapter. If you are not on a network but are set up to send and receive email, read “Email” on page 171. $ who sam console Mar 27 05:00 alex pts/4 Mar 27 12:23 alex pts/5 Mar 27 12:33 jenny pts/7 Mar 26 08:45 Figure 5-10 who lists who is logged inObtaining User and System Information 167 who: Lists Users on the System The who utility displays a list of users who are logged in. In Figure 5-10 the first col- umn that who displays shows that Sam, Alex, and Jenny are logged in. (Alex 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 that a remote user logged in from; this column does not appear in Figure 5-10. The information that who displays is useful when you want to communicate with a user at your installation. 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). If the output of who scrolls off the screen, you can redirect the output through a pipe (|, page 156) so that it becomes the input to less, which displays the output one page 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 alex pts/5 Mar 27 12:33 finger: Lists Users on the System You can use finger to display a list of the users who are logged in on the system. 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 sys- tem is shown as the user’s location. For example, in Figure 5-11 jenny and hls are logged in from the remote system named bravo. The asterisk (*) in front of the name of Helen’s device (TTY) indicates that she has blocked others from sending messages directly to her terminal (refer to “mesg: Denies or Accepts Messages” on page 171). $ finger Login Name Tty Idle Login Time Office Office Phone sam root 1 1:35 May 24 08:38 alex Alex Watson /0 Jun 7 12:46 (:0) alex Alex Watson /1 19 Jun 7 12:47 (:0) jenn Jenny Chen /2 2:24 Jun 2 05:33 (bravo.example.com) hls Helen Simpson */2 2 Jun 2 05:33 (bravo.example.com) Figure 5-11 finger I: lists who is logged in168 Chapter 5 The Linux Utilities You can also use finger to learn more about an individual by specifying the name of that user on the command line. In Figure 5-12, finger displays detailed information about the user named Alex. Alex is logged in and actively using one of his terminals (pts/1); he has not used his other terminal (pts/0) for 5 minutes and 52 seconds. You also learn from finger that if you want to set up a meeting with Alex, you should contact Jenny at extension 1693. .plan and .project 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 Alex. The finger utility searched for a file named .plan in Alex’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 188].) You may 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 Alex 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. $ finger alex Login: alex Name: Alex Watson Directory: /home/alex Shell: /bin/bash On since Wed Jun 6 12:46 (PDT) on pts/0 from :0 5 minutes 52 seconds idle On since Wed Jun 6 12:47 (PDT) on pts/1 from bravo Last login Wed Jun 6 12:47 (PDT) on 1 from bravo New mail received Wed Jun 6 13:16 2007 (PDT) Unread since Fri May 25 15:32 2007 (PDT) Plan: I will be at a conference in Hawaii all next week. If you need to see me, contact Jenny Chen, x1693. Figure 5-12 finger II: lists details about one user finger can be a security risk security On systems where security is a concern, the system administrator may disable finger. This utility can reveal information that can help a malicious user break into a system. $ w 8:20am up 4 days, 2:28, 3 users, load average: 0.04, 0.04, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT alex pts/4 :0 5:55am 13:45 0.15s 0.07s w alex pts/5 :0 5:55am 27 2:55 1:01 bash jenny pts/7 bravo 5:56am 13:44 0.51s 30s vim 3.txt scott pts/12 bravo 7:17pm 1.00s 0:14s run_bdgt Figure 5-13 The w utilityObtaining User and System Information 169 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 that 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 fol- lowing commands find the information you seek as well as information on other users whose names are Helen or Simpson: $ finger HELEN Login: hls Name: Helen Simpson. ... $ finger simpson Login: hls Name: Helen Simpson. ... See page 370 for information about using finger over a network. w: Lists Users on the System The w utility displays a list of the users who are logged in. As discussed in the sec- tion on who, the information that w displays is useful when you want to communi- cate with someone at your installation. The first column in Figure 5-13 shows that Alex, Jenny, and Scott are logged in. The second column shows the designation of the device that each user’s terminal is con- nected to. The third column shows the system that a remote user is logged in from. The fourth column shows the time when each user logged in. The fifth column indi- cates 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 com- puter processor time each user has used during this login session and on the task that 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 w who finger Username x x x Terminal-line identification (tty) x x x Login day and time x Login date and time x x Idle time x x170 Chapter 5 The Linux Utilities Communicating with Other Users The utilities discussed in this section 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 com- munication. 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 ter- minal 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. Press- ing 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. Information displayed w who finger Program the user is executing x Location the user logged in from x CPU time used x Full name (or other information from /etc/passwd)x User-supplied vanity information x System uptime and load average x Table 5-1 Comparison of w, who, and finger (continued) $ write alex Hi Alex, are you there? o Figure 5-14 The write utility IEmail 171 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 Give the following command when you do not wish to receive messages from another user: $ mesg n If Alex had given this command before Jenny tried to send him a message, Jenny would have seen the following message: $ write alex Permission denied You can allow messages again by entering mesg y. Give the command mesg by itself to display is y (for “yes, messages are allowed”) or is n (for “no, messages are not allowed”). Email Email enables you to communicate with users on the local system and, if the instal- lation is part of a network, with other users 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 email utilities can send a message when the recipient is not logged in. 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.good-day.net). 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 $ write alex Hi Alex, are you there? o Message from alex@bravo.example.com on pts/0 at 16:23 ... Yes Jenny, I'm here. o Figure 5-15 The write utility II172 Chapter 5 The Linux Utilities incoming email; forwards email; and so on. The GNU Privacy Guard (GPG or GNUpg, page 995) encrypts and decrypts email and makes it almost impossible for an unauthorized person to read. Refer to Chapter 21 for more information on setting email clients and servers. Network addresses If your 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 send- ing Alex email on the Internet would need to specify his domain name (page 1034) along with his username. Use this address to send email to the author of this book: mgs@sobell.com. Tutorial: Creating and Editing a File with vim 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 may want to try vim’s tutor, named vimtutor: Give its name as a command to run it. Specifying a terminal Because vim takes advantage of features that are specific to various kinds of termi- nals, 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 988. Starting vim Start vim with the following command line to create and edit a file named practice: $ vim practice When you press RETURN, the command line disappears, and the screen looks similar to the one shown in Figure 5-16. The tildes (~) at the left of the screen indicate that 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 one shown in Figure 5-16, your terminal type is probably not set correctly. If you start vim with a terminal type that is not in the terminfo database, vim dis- plays an error message and the terminal type defaults to ansi, which works on many terminals. In the following example, the user mistyped vt100 and set the terminal type to vg100: The vi command runs vim tip On Ubuntu Linux systems the command vi runs vim. See “The compatible Parameter” on page 179 for information on running vim in vi-compatible mode.Tutorial: Creating and Editing a File with vim 173 E558: Terminal entry not found in terminfo 'vg100' not known. Available builtin terminals are: builtin_riscos builtin_amiga builtin_beos-ansi builtin_ansi builtin_pcansi builtin_win32 builtin_vt320 builtin_vt52 builtin_xterm builtin_debug builtin_dumb defaulting to 'ansi' To reset the terminal type, press ESCAPE and then give the following command to exit from vim and get the shell prompt back: :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. Once you get the shell prompt back, refer to “Specifying a Terminal” on page 988, and then start vim again. If you start this editor without a filename, vim assumes that you are a novice and tells you how to get started (Figure 5-17, next page). The practice file is new so it does not contain any 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 that 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. Figure 5-16 Starting vim174 Chapter 5 The Linux Utilities 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 com- mand 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 Figure 5-17 Starting vim without a filename Figure 5-18 Modes in vim Insert, Append, Open, Replace, Change RETURN ESCAPE Colon (:) Input mode Last Line mode Command mode Input mode Last Line modeTutorial: Creating and Editing a File with vim 175 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 numbers. vim is case sensitive 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 whether you enter an uppercase or lowercase character. Beware of the CAPS LOCK (SHIFT- LOCK) key. 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 key (insert before the cursor) or the a key (append after the cursor). If you are not sure whether vim is currently in Input mode, press the ESCAPE key; vim returns to Command mode if it was in Input mode or beeps, flashes, or does noth- ing if it is already in Command mode. You can put vim back in Input mode by press- ing 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, 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 will wrap 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 always correct any typing mistakes you make. If you notice a mistake on the line you are entering, you can correct it before you continue (page 176). You Figure 5-19 Entering text with vim176 Chapter 5 The Linux Utilities can correct other mistakes later. When you finish entering the paragraph, press ESCAPE to return vim to Command mode. Getting Help To get help while you are using vim, give the command :help [feature] followed by RETURN (you must be in Command mode when you give 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 dis- plays 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 get help with 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 may 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. Figure 5-20 The main vim Help screenTutorial: Creating and Editing a File with vim 177 Moving the Cursor You need to be able to move the cursor on the screen so that you can delete, insert, and correct text. While vim is in Command mode, you can use the RETURN key, the SPACE bar, and the ARROW keys to 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. 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 posi- tioning 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 any- where 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. :redo (Redo) 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. Figure 5-21 Help with insert commands178 Chapter 5 The Linux Utilities 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. o/O (Open) 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, puts the cursor on it, 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 Com- mand mode. The O command works in the same way o works, except that it opens a blank line above the line the cursor is on. 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 175, you might use the ARROW keys to move the cursor until it is on top of the p in press- ing. 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. 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 that the edited text is saved and available when you next want it. Make sure that vim is in Command mode, and then use the ZZ command (you must use uppercase Zs) to write your 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. 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. 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 sus- pend 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 290. If you try to start editing the same file with a new vim com- mand, vim displays a message about a swap file.Chapter Summary 179 The compatible Parameter The compatible parameter makes vim more compatible with vi. By default this parameter is not set. From the command line use the –C option to set the compati- ble parameter and use the –N option to unset it. 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 177) can undo only your 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 an Ubuntu Linux 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. The utilities listed in Table 5-2 manipulate, display, compare, and print files. Table 5-2 File utilities Utility Function cp Copies one or more files (page 149) diff Displays the differences between two files (page 154) file Displays information about the contents of a file (page 155) grep Searches file(s) for a string (page 151) head Displays the lines at the beginning of a file (page 152) lpq Displays a list of jobs in the print queue (page 151) lpr Places file(s) in the print queue (page 151) lprm Removes a job from the print queue (page 151) mv Renames a file or moves file(s) to another directory (page 150) sort Puts a file in order by lines (page 153) tail Displays the lines at the end of a file (page 152) uniq Displays the contents of a file, skipping successive duplicate lines (page 154)180 Chapter 5 The Linux Utilities To reduce the amount of disk space a file occupies, you can compress it with 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. 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. The utilities listed in Table 5-5 determine the location of a utility on the local sys- tem. For example, they can display the pathname of a utility or a list of C++ compil- ers available on the local system. Table 5-6 lists utilities that display information about other users. You can easily learn a user’s full name, the user’s login status, the login shell of the user, and other items of information maintained by the system. Table 5-3 (De)compression utilities Utility Function bunzip2 Returns a file compressed with bzip2 to its original size and format (page 160) bzcat Displays a file compressed with bzip2 (page 160) bzip2 Compresses a file (page 160) compress Compresses a file (not as well as gzip) (page 161) gunzip Returns a file compressed with gzip or compress to its original size and for- mat (page 161) gzip Compresses a file (page 161) zcat Displays a file compressed with gzip (page 161) Table 5-4 Archive utility Utility Function tar Creates or extracts files from an archive file (page 161) Table 5-5 Location utilities Utility Function apropos Searches the man page one-line descriptions for a keyword (page 165) locate Searches for files on the local system (page 166) whereis Displays the full pathnames of a utility, source code, or man page (page 164) which Displays the full pathname of a command you can run (page 164)Exercises 181 The utilities shown in Table 5-7 can help you stay in touch with other users on the local network. Table 5-8 lists miscellaneous utilities. 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 edit- ing 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 remove adja- cent duplicate lines from the file? How can you remove all duplicates? Table 5-6 User and system information utilities Utility Function finger Displays detailed information about users, including their full names (page 167) hostname Displays the name of the local system (page 149) w Displays detailed information about users who are logged in (page 169) who Displays information about users who are logged in (page 167) Table 5-7 User communication utilities Utility Function mesg Permits or denies messages sent by write (page 171) write Sends a message to another user who is logged in (page 170) Table 5-8 Miscellaneous utilities Utility Function date Displays the current date and time (page 157) echo Copies its arguments (page 1023) to the screen (page 157)182 Chapter 5 The Linux Utilities 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. 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 your 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 them. 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 existing files 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 charac- terize the files in a, b, and c? 16. Older email programs were not able to handle binary files. Suppose that 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 aptitude [page 585]; it is part of the sharutils package.) b. Would it ever make sense to use uuencode on a file before compressing it? Explain.183183 6Chapter6A filesystem is a set of data structures (page 1032) that usually resides on part of a disk and that holds directories of files. File- systems 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 file- system, 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 Access Control Lists (ACLs), which allow you to share selected files with other users. It con- cludes 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 may want to refer to the df info page and to the fsck, mkfs, and tune2fs man pages for more information on filesystems. In This Chapter The Hierarchical Filesystem . . . . . 184 Directory Files and Ordinary Files . . . . . . . . . . . . . . . . . . . . . . 184 The Working Directory. . . . . . . . . . 188 Your Home Directory . . . . . . . . . . . 188 Pathnames . . . . . . . . . . . . . . . . . . 189 Relative Pathnames . . . . . . . . . . . 190 Working with Directories . . . . . . . 196 Access Permissions . . . . . . . . . . . 199 ACLs: Access Control Lists . . . . . . 203 Hard Links . . . . . . . . . . . . . . . . . . . 210 Symbolic Links . . . . . . . . . . . . . . . 212 6 The Linux Filesystem184 Chapter 6 The Linux Filesystem The Hierarchical Filesystem Family tree A hierarchical structure (page 1040) 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 may in turn have several children, each of whom may have more chil- dren. This hierarchical structure is called a family tree (Figure 6-1). Directory tree 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, 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 corre- spond. 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 show that the tree “grows” Figure 6-1 A family tree Grandparent UncleMomAunt SelfBrotherSister Daughter 2Daughter 1 Grandchild 2Grandchild 1Directory Files and Ordinary Files 185 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 be at the ends of paths, are described on page 567. Ordinary files, or sim- ply 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. More information about pathnames appears on page 189. 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 filesys- tems allow you to create files with names up to 255 characters long, some filesystems Figure 6-2 A secretary’s directories correspond personal milk_co businessmemos cheese_co letter_2letter_1 Figure 6-3 Directories and ordinary files DirectoryDirectory Ordinary FileOrdinary File Ordinary File Ordinary File Ordinary File Directory Directory Directory Directory186 Chapter 6 The Linux Filesystem restrict you to shorter names. While 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 (,) / or root The root directory is always named / (slash) and referred to by this single character. No other file can use this name or have a / in its name. However, in a pathname, which is a string of filenames including directory names, the slash separates file- names (page 189). 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 • davis • reports • 2001 • acct_payable Filename length When you share your files with users on other systems, you may need to make long filenames differ within the first few characters. Systems running DOS or older ver- sions of Windows have an 8-character filename body length and a 3-character file- name extension length limit. Some UNIX systems have a 14-character limit and older Macintosh systems have a 31-character limit. If you keep the 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 on page 526 for a way to determine the maximum length of a filename on the local system.Directory Files and Ordinary Files 187 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 sen- sitive, so files named JANUARY, January, and january represent three distinct files. Filename Extensions A filename extension is the part of the filename following an embedded period. In the filenames listed in Table 6-1, 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.01 or files.tar.gz. Do not use SPACEs within filenames caution Although you can use SPACEs within filenames, it is a poor idea. Because a SPACE is a special char- acter, you must quote it on a command line. Quoting a character on a command line can be diffi- cult 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 sys- tem, 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" Table 6-1 Filename extensions Filename with extension Meaning of extension compute.c A C programming language source file compute.o The object code for the program compute The same program as an executable file memo.0410.txt A text file memo.pdf A PDF file; view with xpdf under a GUI memo.ps A PostScript file; view with gs under a GUI memo.Z A file compressed with compress (page 161); use uncompress or gunzip (page 161) to decompress memo.tgz or memo.tar.gz A tar (page 161) archive of files compressed with gzip (page 161)188 Chapter 6 The Linux Filesystem 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 (page 188) 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—a sin- gle and double period (. and ..)—appear in every directory (page 193). 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 jenny directory.” The pwd (print working directory) utility displays the pathname of the working directory. Your Home Directory When you first log in on a Linux system or start a terminal emulator window, your 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 pro- grams information about you and your preferences. Frequently one of these files tells the shell what kind of terminal you are using (page 988) and executes the stty (set terminal) utility to establish the erase (page 137) and line kill (page 138) 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, you must use memo.gz A file compressed with gzip (page 161); view with zcat or decompress with gunzip (both on page 161) memo.bz2 A file compressed with bzip2 (page 160); view with bzcat or decompress with bunzip2 (both on page 160) memo.html A file meant to be viewed using a Web browser, such as Firefox photo.gif, photo.jpg, photo.jpeg, photo.bmp, photo.tif, or photo.tiff A file containing graphical information, such as a picture Table 6-1 Filename extensions (continued)Pathnames 189 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 con- trol startup sequences using icons on the desktop. See page 277 for more informa- tion about startup files. Pathnames This section discusses absolute and relative pathnames and explains how to use them to your advantage. Absolute Pathnames Every file has a pathname. Figure 6-5 shows the pathnames of directories and ordi- nary files in part of a filesystem hierarchy. An absolute pathname always starts with a slash (/), the name of the root directory. You can then build the absolute pathname of a file by tracing a path from the root directory through all the intermediate direc- tories to the file. String all the filenames in the path together, separating each from the next with a slash (/) and preceding the entire group of filenames with a slash (/). This path of filenames is called an absolute pathname because it locates a file abso- lutely by tracing a path from the root directory to the file. The part of a pathname following the final slash is called a simple filename, filename, or basename. login: alex Password: Last login: Wed Oct 20 11:14:21 from bravo $ pwd /home/alex Figure 6-4 Logging in and displaying the pathname of your home directory Figure 6-5 Absolute pathnames / etctmphome hlsjennyalex notesbin report log /home /etc /home/hls /home/hls/notes /home/hls/bin/log /home/jenny190 Chapter 6 The Linux Filesystem ~ (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 277) with the following command, no matter which directory is your 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, Alex can examine Scott’s .bashrc file with the following command: $ less ~scott/.bashrc Refer to “Tilde Expansion” on page 341 for more information. Relative Pathnames A relative pathname traces a path from the working directory to a file. The path- name is relative to the working directory. Any pathname that does not begin with the root directory (/) 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 direc- tory 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. Refer to Figure 6-6 as you read this paragraph. Files that are children of the work- ing 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 to use fewer long, cumbersome pathnames. When using a relative pathname, know which directory is the working directory caution The location of the file that 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 using mkdir and you are not where you think you are in the file hierarchy, the new directory will end up in an unexpected location. It does not matter which directory is the working directory when you use an absolute pathname.Directory Commands 191 Directory Commands 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. mkdir: Creates a Directory The mkdir utility creates a directory. The argument (page 1023) to mkdir becomes the pathname of the new directory. The following examples develop the directory struc- ture 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. Figure 6-6 Relative pathnames / etctmphome hlsjennyalex notesbin report log .. working directory = . notes bin/log ../jenny Figure 6-7 The file structure developed in the examples / home alex literature promo demonames temp192 Chapter 6 The Linux Filesystem In Figure 6-8, pwd shows that Alex is working in his home directory (/home/alex) and ls shows the names of the files in his home directory: demo, names, and temp. Using mkdir, Alex 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. Of course, Alex could have used an absolute pathname to create the same directory: mkdir /home/alex/literature. 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 exe- cutable 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 does not display anything because there are no files in the literature directory. The following commands show two ways to create the promo directory as a child of the newly created literature directory. The first way checks that /home/alex is the working directory and uses a relative pathname: $ pwd /home/alex $ mkdir literature/promo The second way uses an absolute pathname: $ mkdir /home/alex/literature/promo Use the –p (parents) option to mkdir to create both the literature and promo directo- ries with one command: $ pwd /home/alex $ ls demo names temp $ mkdir -p literature/promo or $ mkdir -p /home/alex/literature/promo $ pwd /home/alex $ ls demo names temp $ mkdir literature $ ls demo literature names temp $ ls -F demo literature/ names temp $ ls literature $ Figure 6-8 The mkdir utilityDirectory Commands 193 cd: Changes to Another Working Directory The cd (change directory) utility makes another directory the working directory but does not change the contents of the working directory. Figure 6-9 shows two ways to make the /home/alex/literature directory the working directory, as verified by pwd. First Alex uses cd with an absolute pathname to make literature his working directory—it does not matter which is your working directory when you give a command with an absolute pathname. A pwd command confirms the change made by Alex. When used without an argu- ment, 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 Alex’s home directory the working directory. Finally, knowing that he is working in his home directory, Alex uses a simple filename to make the literature directory his working directory (cd literature) and confirms the change with pwd. The . and .. Directory Entries The mkdir utility automatically puts two entries in each directory you create: 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 path- name 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/alex), second to $ cd /home/alex/literature $ pwd /home/alex/literature $ cd $ pwd /home/alex $ cd literature $ pwd /home/alex/literature Figure 6-9 cd changes your working directory 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 by using cd, your home directory is your working directory. If you were to change directories to Scott’s home directory, then Scott’s home directory would be your working directory.194 Chapter 6 The Linux Filesystem copy the memoA file to the parent directory, and third to list the contents of the parent directory again. $ pwd /home/alex/literature $ ls .. demo literature names temp $ cp memoA .. $ ls .. demo literature memoA names temp After using cd to make promo (a subdirectory of literature) his working directory, Alex 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 any- where that a utility or program requires a filename or pathname. This usage holds true for ls, vim, mkdir, rm, and most other Linux utilities. Important Standard Directories and Files Originally files on a Linux system were not located in standard places. The scat- tered 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 on February 14, 1994. In early 1995 work was started on a broader standard covering many UNIX-like systems: FHS (Linux Filesystem Hierarchy Standard, www.pathname.com/fhs). More recently FHS has been incorporated in LSB (Linux Standard Base, www.linuxbase.org), a workgroup of FSG (Free Standards Group, www.freestandards.org). Figure 6-10 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-10, some of the direc- tories specified by FHS, and some other directories. Ubuntu Linux, however, does not use all the directories specified by FHS. Be aware that you cannot always deter- mine 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 554. / Root The root directory, present in all Linux filesystem structures, is the ancestor of all files in the filesystem. /bin Essential command binaries Holds the files needed to bring the system up and run it when it first comes up in recovery mode (page 512). /boot Static files of the boot loader Contains all of 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.Directory Commands 195 The udev utility (page 568) 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. One of the most important is /etc/passwd, which contains a list of all users who have permission to use the system. /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 Jenny’s home directory is /home/jenny. On some systems the users’ directories may not be found under /home but instead might be spread among other directories such as /inhouse and /clients. /lib Shared libraries /lib/modules Loadable kernel modules /mnt Mount point for temporarily mounting filesystems /opt Add-on software packages (optional packages) /proc Kernel and process information virtual filesystem /root Home directory for the root account /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. In older versions of Linux, many system administration utilities were scattered through several directories that often included other system files (/etc, /usr/bin, /usr/adm, /usr/include). /sys Device pseudofilesystem See udev on page 568 for more information. /tmp Temporary files /usr Second major hierarchy Traditionally includes subdirectories that contain infor- mation used by the system. Files in /usr subdirectories do not change often and may be shared by several systems. /usr/bin Most user commands Contains the standard Linux utility programs—that is, binaries that are not needed in recovery mode (page 512). Figure 6-10 A typical FHS-based Linux filesystem structure mail spool bin sbin alex jenny hls sbin var dev usr etc tmp home root / bin196 Chapter 6 The Linux Filesystem /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/man Online manuals /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/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, opt, run, spool, tmp, and yp. Older versions of Linux scattered such files through several subdirectories of /usr (/usr/adm, /usr/mail, /usr/spool, /usr/tmp). /var/log Log files Contains lastlog (a record of the last login by each user), messages (sys- tem messages from syslogd), and wtmp (a record of all logins/logouts). /var/spool Spooled application data Contains anacron, at, cron, lpd, mail, mqueue, samba, and other directories. The file /var/spool/mail typically has a symbolic link in /var. Working with Directories This section covers deleting directories, copying and moving files between directo- ries, and moving directories. It also describes how to use pathnames to make your work with Linux easier. rmdir: Deletes a Directory The rmdir (remove directory) utility deletes a directory. You cannot delete the work- ing 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/alex/literature/promo The rm utility has a –r option (rm –r filename) that recursively deletes files, includ- ing directories, within a directory and also deletes the directory itself.Working with Directories 197 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/alex $ touch letter With /home/alex as the working directory, the following example uses cp with a rel- ative pathname to copy the file letter to the /home/alex/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.0610: $ cp letter literature/promo/letter.0610 If Alex 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.0610 If Alex 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/alex/literature/promo $ vim letter.0610 To make the parent of the working directory (named /home/alex/literature) the new working directory, Alex can give the following command, which takes advantage of the .. directory entry: $ cd .. $ pwd /home/alex/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 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.198 Chapter 6 The Linux Filesystem If the working directory is /home/alex, Alex 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/alex/names and /home/alex/temp to /home/alex/literature/names and /home/alex/literature/temp, respectively (Figure 6-11). Like most Linux com- mands, mv accepts either absolute or relative pathnames. As you work with Linux and create more files, you will need to create new directo- ries 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 as 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 direc- tories. The syntax is similar except that 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 option. Refer to the tar and cpio man pages for other ways to copy and move directories. Figure 6-11 Using mv to move names and temp home scottalexjenny literature names temp names temp /Access Permissions 199 Access Permissions Ubuntu Linux supports two methods of controlling who can access a file and how they can access it: traditional Linux access permissions and Access Control Lists (ACLs, page 203). ACLs provide finer-grained control of access privileges. This sec- tion describes traditional Linux 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 558 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. The following example displays infor- mation for two files. The file letter.0610 contains the text of a letter, and check_spell contains a shell script, a program written in a high-level shell program- ming language: $ ls -l letter.0610 check_spell -rw-r--r-- 1 alex pubs 3355 May 2 10:52 letter.0610 -rwxr-xr-x 2 alex pubs 852 May 5 14:03 check_spell 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 203) • The number of links to the file (page 209) • The name of the owner of the file (usually the person who created the file) • The name of the group that the file is associated with • The size of the file in characters (bytes) Figure 6-12 The columns displayed by the ls –l command -rwxrwxr-x+..3.alex.....pubs.........2048.Aug.12.13:15.memo T y p e o f file F ile a c c e s s L in k s O w n e r S iz e o f m o d ific a tio n D a te a n d tim e F ile n a m e G ro u p A C L fla g p e rm is s io n s200 Chapter 6 The Linux Filesystem • The date and time the file was created or last modified • The name of the file The type of file (first column) for letter.0610 is a hyphen (–) because it is an ordi- nary 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 per- mission. A – in a column indicates that the owner does not have the permission that would 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.0610 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 access permission.) chmod: Changes Access Permissions The owner of a file controls which users have permission to access the file and how they can access it. When you own a file, you can use the chmod (change mode) util- ity to change access permissions for that file. In the following example, chmod adds (+) read and write permissions (rw) for all (a) users: $ chmod a+rw letter.0610 $ ls -l letter.0610 -rw-rw-rw- 1 alex pubs 3355 May 2 10:52 letter.0610 In the next example, chmod removes (–) read (r) and execute (x) permissions for users other (o) than the owner of the file (alex) and members of the group the file is associated with (pubs): $ chmod o-rx check_spell $ ls -l check_spell -rwxr-x--- 2 alex pubs 852 May 5 14:03 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 may or may not be the user of the file at any given time) in the argument to chmod. You can also use absolute, or numeric, argu- ments with chmod. Refer to page 283 for more information on using chmod to make a file executable and to the chmod man page for information on absolute arguments and chmod in general. Refer to page 558 for more information on groups. 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 on 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 (nonshell) program.Access Permissions 201 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 directo- ries from being scanned (covered shortly). There is an exception to the access permissions just described. Anyone who can gain root privileges (using sudo (page 490) or su) has full access to all files, regardless of the file’s owner or access permissions. Setuid and Setgid Permissions When you execute a file that has setuid (set user ID) permission, the process execut- ing the file takes on the privileges of the file’s owner. For example, if you run a set- uid 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 means that the process execut- ing the file takes on the privileges of the group the file is associated with. The ls util- ity shows setuid permission by placing an s in the owner’s executable position and setgid permission by placing an s in the group’s executable position: $ ls -l program1 -rwxr-xr-x 1 alex pubs 15828 Nov 5 06:28 program1 $ chmod u+s program1 $ ls -l program1 -rwsr-xr-x 1 alex pubs 15828 Nov 5 06:28 program1 $ chmod g+s program1 $ ls -l program1 -rwsr-sr-x 1 alex pubs 15828 Nov 5 06:28 program1 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) can help you remember how permissions are named. 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 are 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 indiscrimi- nately creating and using setuid and setgid programs owned by or belonging to the group root. Because of their inherent dangers, many sites minimize the use of these programs on their sys- tems. One necessary setuid program is passwd. See page 489 for a tip on setuid files owned by root and page 521 for a command that lists setuid files on the local system.202 Chapter 6 The Linux Filesystem The following example shows two ways for a user working with root privileges to give a program setuid privileges: $ ls -l my* –rwxr–xr–x 1 root other 24152 Apr 29 16:30 myprog –rwxr–xr–x 1 root other 24152 Apr 29 16:31 myprog2 $ sudo chmod 4755 myprog $ sudo chmod u+s myprog2 $ ls -l my* –rwsr–xr–x 1 root other 24152 Apr 29 16:30 myprog –rwsr–xr–x 1 root other 24152 Apr 29 16:31 myprog2 The s in the owner execute position of the ls –l output (page 199) indicates that the file has setuid permission. Directory Access Permissions Access permissions have slightly different meanings when they are used with direc- tories. Although the three types of users can read from or write to a directory, the directory cannot be executed. Execute access permission is redefined for a directory: It means that you can cd into the directory and/or examine files that you have per- mission 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 without an argument to list the entire contents of the directory. In the following exchange, Jenny first verifies that she is logged in as herself. Then she checks the permissions on Alex’s info direc- tory. You can view the access permissions associated with a directory by running ls with the –d (directory) and –l (long) options: $ who am i jenny pts/7 Aug 21 10:02 $ ls -ld /home/alex/info drwx-----x 2 alex pubs 512 Aug 21 09:31 /home/alex/info $ ls -l /home/alex/info ls: /home/alex/info: Permission denied The d at the left end of the line that ls displays indicates that /home/alex/info is a directory. Alex has read, write, and execute permissions; members of the pubs group have no access permissions; and other users have execute permission only, as indicated by the x at the right end of the permissions. Because Jenny does not have read permission for the directory, the ls –l command returns an error. When Jenny specifies the names of the files she wants information about, she is not reading new directory information but rather searching for specific information, which she is allowed to do with execute access to the directory. She has read permis- sion for notes so she has no problem using cat to display the file. She cannot display financial because she does not have read permission for it: Do not write setuid shell scripts security Never give shell scripts setuid permission. Several techniques for subverting them are well known.ACLs: Access Control Lists 203 $ ls -l /home/alex/info/financial /home/alex/info/notes -rw------- 1 alex pubs 34 Aug 21 09:31 /home/alex/info/financial -rw-r--r-- 1 alex pubs 30 Aug 21 09:32 /home/alex/info/notes $ cat /home/alex/info/notes This is the file named notes. $ cat /home/alex/info/financial cat: /home/alex/info/financial: Permission denied Next Alex gives others read access to his info directory: $ chmod o+r /home/alex/info When Jenny checks her access permissions on info, she finds that she has both read and execute access to the directory. Now ls –l works just fine without arguments, but she still cannot read financial. (This restriction is an issue of file permissions, not directory permissions.) Finally, Jenny tries to create a file named newfile by using touch. If Alex were to give her write permission to the info directory, Jenny would be able to create new files in it: $ ls -ld /home/alex/info drwx---r-x 2 alex pubs 512 Aug 21 09:31 /home/alex/info $ ls -l /home/alex/info total 8 -rw------- 1 alex pubs 34 Aug 21 09:31 financial -rw-r--r-- 1 alex pubs 30 Aug 21 09:32 notes $ cat /home/alex/info/financial cat: financial: Permission denied $ touch /home/alex/info/newfile touch: cannot touch '/home/alex/info/newfile': Permission denied ACLs: Access Control Lists Access Control Lists (ACLs) provide finer-grained control over which users can access specific directories and files than do traditional Linux permissions (page 199). 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 file- systems 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.204 Chapter 6 The Linux Filesystem Enabling ACLs Before you can use ACLs you must install the acl software package: $ sudo aptitude install acl Ubuntu Linux officially supports ACLs on ext2 and ext3 filesystems only, although informal support for ACLs is available on other filesystems. To use ACLs on an ext2 or ext3 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 ext3 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 your 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/hda3 on /home type ext3 (rw,acl) See page 576 for information on fstab and page 572 for information on mount. Working with Access Rules The setfacl utility modifies a file’s ACL and the getfacl utility displays a file’s ACL. When you use getfacl to obtain information about a file that does not have an ACL, it displays the same information as an ls –l command, albeit in a different format: $ ls -l report -rw-r--r-- 1 max max 9537 Jan 12 23:17 report $ getfacl report # file: report # owner: max # group: max Most utilities do not preserve ACLs caution When used with the –p (preserve) or –a (archive) option, cp preserves ACLs when it copies files. Another utility that is supplied with Ubuntu Linux that preserves ACLs is mv. 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.ACLs: Access Control Lists 205 user::rw- group::r-- other::r-- The first three lines of the getfacl output are called 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 199. The ––omit-header (or just ––omit) option causes getfacl not to display the header: $ getfacl --omit-header report user::rw- group::r-- other::r-- In the line that starts with user, the two colons (::) with no name between them indi- cate that the line specifies the permissions for the owner of the file. Similarly, the 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 that 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. 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: max user::rw- user:sam:rw- group::r-- mask::rw- other::r--206 Chapter 6 The Linux Filesystem The line containing user:sam:rw– shows that the user named sam has read and write access (rw–) to the file. See page 199 for an explanation of how to read sym- bolic 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 max 9537 Jan 12 23:17 report optional Effective Rights Mask The line that starts with mask specifies the effective rights mask. This mask limits the effective permissions granted to ACL groups and users. It 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 getfacl always sets the effec- tive 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: max user::rw- user:sam:rw- #effective:r-- group::r-- mask::r-- other::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::rw- user:sam:r-- user:zach:rw- group::r--ACLs: Access Control Lists 207 mask::rw- other::r-- The –x option removes ACL rules for a user or a group. It has no effect on permis- sions 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::rw- user:zach:rw- group::r-- mask::rw- other::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. 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 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 admin group read, write, and execute permissions. $ ls -ld dir drwx------ 2 max max 4096 Feb 12 23:15 dir $ getfacl dir # file: dir # owner: max # group: max user::rwx group::--- other::--- $ setfacl -d -m g:pubs:r-x,g:admin:rwx dir The following ls command shows that the dir directory now has an ACL, as indi- cated 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 spec- ify 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 permis- sions for the pubs and admin groups. Next is the default effective rights mask.208 Chapter 6 The Linux Filesystem $ ls -ld dir drwx------+ 2 max max 4096 Feb 12 23:15 dir $ getfacl dir # file: dir # owner: max # group: max user::rwx group::--- other::--- default:user::rwx default:group::--- default:group:pubs:r-x default:group:admin: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 effec- tive rights mask for that file is created based on the file’s permissions. In some cases the mask may override default ACL rules. In the next example, touch creates a file named new in the dir directory. The ls com- mand shows that this file has an ACL. Based on the value of umask (page 526), both the owner and the group that the file is associated with have read and write permis- sions for the file. The effective rights mask is set to read and write so that the effec- tive permission for pubs is read and the effective permissions for admin are read and write. Neither group has execute permission. $ cd dir $ touch new $ ls -l new -rw-rw----+ 1 max max 0 Feb 13 00:39 new $ getfacl --omit new user::rw- group::--- group:pubs:r-x #effective:r-- group:admin:rwx #effective:rw- mask::rw- other::--- 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 max 0 Feb 13 00:39 new $ getfacl --omit new user::rwx group::--- group:pubs:r-x group:admin:rwx mask::rwx other::---Links 209 Links A link is a pointer to a file. Every time you create a file by using vim, touch, cp, or any 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 200). You may also have to change the access permissions of the parent directory of the file to give the user read, write, or execute permis- sion (page 202). Once the permissions are appropriately set, the user can create a link to the file so that each of you can access the file from your separate directory hierarchies. 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 classifi- cations for different tasks. For example, if you have the file layout depicted in Figure 6-2 on page 185, 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 direc- tory 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 may sound complicated, this technique keeps all your to-do lists con- veniently in one place. The appropriate list is easily accessible in the task-related directory when you are busy composing letters, writing memos, or handling per- sonal business. Figure 6-13 Using links to cross-classify files correspond to_do businessmemospersonalto_doto_do to_dobusinessmemospersonal Links business210 Chapter 6 The Linux 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 The next command makes the link shown in Figure 6-14 by creating a new link named /home/alex/letter to an existing file named draft in Jenny’s home directory: $ pwd /home/jenny $ ln draft /home/alex/letter The new link appears in the /home/alex directory with the filename letter. In prac- tice, Alex may need to change the directory and file permissions so that Jenny will be able to access the file. Even though /home/alex/letter appears in Alex’s directory, Jenny is the owner of the file because she created it. 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. Figure 6-14 Two links to the same file: /home/alex/letter and /home/jenny/draft home / alex jenny memo planning /home/alex/letter and /home/jenny/draftLinks 211 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 informa- tion—such as access permissions, owner, and the time the file was last modified—is the same for all links; only the filenames differ. When Jenny modifies /home/jenny/draft, for example, Alex sees the changes in /home/alex/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. 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. Once 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 alex) shows there are two links to file_a and file_b: $ ls -l file_a file_b file_c file_d -rw-r--r-- 2 alex pubs 33 May 24 10:52 file_a -rw-r--r-- 2 alex pubs 33 May 24 10:52 file_b -rw-r--r-- 1 alex pubs 16 May 24 10:55 file_c -rw-r--r-- 1 alex pubs 33 May 24 10:57 file_d Although it is easy to guess which files are linked to one another in this example, ls does not explicitly tell you.212 Chapter 6 The Linux Filesystem 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 1041) 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 file- names 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), 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, regard- less 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 nonexistent file. This ability is useful if you need a link to a file that is periodically removed and re- created. 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 disad- vantages. 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 analo- gous to a person having multiple full legal names, as many married women do. InLinks 213 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 follow- ing sections describe some of the peculiarities of symbolic links. ln: Creates a Symbolic Link You use ln with the ––symbolic (or –s) option to create a symbolic link. The follow- ing example creates a symbolic link /tmp/s3 to the file sum in Alex’s home direc- tory. 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 list- ing is l (for link). $ ln --symbolic /home/alex/sum /tmp/s3 $ ls -l /home/alex/sum /tmp/s3 -rw-rw-r-- 1 alex alex 38 Jun 12 09:51 /home/alex/sum lrwxrwxrwx 1 alex alex 14 Jun 12 09:52 /tmp/s3 -> /home/alex/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. You can also use ln to create a symbolic link to a directory. When you use the ––symbolic option, ln does not care whether the file you are creating a link to is an ordinary file or a 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 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. Use absolute pathnames with symbolic links tip Symbolic links are literal and are not aware of directories. A link that points to a relative pathname, which includes simple filenames, assumes that 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/alex $ ln --symbolic sum /tmp/s4 $ ls -l sum /tmp/s4 lrwxrwxrwx 1 alex alex 3 Jun 12 10:13 /tmp/s4 -> sum -rw-rw-r-- 1 alex alex 38 Jun 12 09:51 sum $ cat /tmp/s4 cat: /tmp/s4: No such file or directory214 Chapter 6 The Linux Filesystem $ ln -s /home/alex/grades /tmp/grades.old $ pwd /home/alex $ cd /tmp/grades.old $ pwd /tmp/grades.old $ /bin/pwd /home/alex/grades When you change directories back to the parent, you end up in the directory hold- ing 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 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 subsequent 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 alex pubs 981 May 24 11:05 sum $ ln -s sum total $ rm sum $ cat total cat: total: No such file or directory $ ls -l total lrwxrwxrwx 1 alex pubs 6 May 24 11:09 total -> sum When you remove a file, be sure to remove all symbolic links to it. Remove a sym- bolic 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 orga- nize files so that you can find them quickly and easily. The file structure containsChapter Summary 215 directory files and ordinary files. Directories contain other files, including other directories; ordinary files generally contain text, programs, or images. The ances- tor of all files is the root directory named /. Most Linux filesystems support 255-character filenames. Nonetheless, it is a good idea to keep filenames simple and intuitive. Filename extensions can help make file- names more meaningful. When you are logged in, you are always associated with a working directory. Your home directory is your 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 between the other filenames in the path. 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. A Linux filesystem contains many important directories, including /usr/bin, which stores most of the Linux utility commands, 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 each user’s ID and full name. Among the attributes associated with each file are access permissions. They deter- mine 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 exe- cute permissions for the file’s owner, the group, and all other users on the system. Access Control Lists (ACLs) 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 these 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 a specific inode. A link is a pointer to a file. You can have several links to a single file so that 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.216 Chapter 6 The Linux Filesystem Table 6-2 summarizes the utilities introduced in this chapter. 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/alex d. /home/alex/literature/promo e. .. f. letter.0610 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 your working directory is /home/alex with a subdirectory named litera- ture, give three sets of commands that 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? Table 6-2 Utilities introduced in Chapter 6 Utility Function cd Associates you with another working directory (page 193) chmod Changes the access permissions on a file (page 200) getfacl Displays a file’s ACL (page 204) ln Makes a link to an existing file (page 210) mkdir Creates a directory (page 191) pwd Displays the pathname of the working directory (page 188) rmdir Deletes a directory (page 196) setfacl Modifies a file’s ACL (page 204)Exercises 217 c. Assuming that 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 that 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 your 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 your system? d. How many different login shells are in use on your system? (Hint: Look at the last field.) 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/jenny/draft and /home/alex/letter are links to the same file and the following sequence of events occurs, what will be the date in the open- ing of the letter? a. Alex gives the command vim letter. b. Jenny gives the command vim draft. c. Jenny changes the date in the opening of the letter to January 31, 2008, writes the file, and exits from vim. d. Alex changes the date to February 1, 2008, writes the file, and exits from vim. 8. Suppose that 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 that you cannot search as a regular user? Does the root directory have any subdirectories that you cannot read as a regular user? Explain. 10. Assume that you are given the directory structure shown in Figure 6-2 on page 185 and the following directory permissions: d--x--x--- 3 jenny pubs 512 Mar 10 15:16 business drwxr-xr-x 2 jenny pubs 512 Mar 10 15:16 business/milk_co218 Chapter 6 The Linux Filesystem For each category of permissions—owner, group, and other—what hap- pens when you run each of the following commands? Assume that 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? 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 that 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 pro- ducing an error? 15. The ls –i command displays a filename preceded by the inode number of the file (page 212). 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 that 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 alex pubs 1024 Mar 2 17:57 dirtmp $ ls dirtmp $ rmdir dirtmp rmdir: dirtmp: Directory not empty $ rm dirtmp/* rm: No match.219219 7Chapter7This 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 also describes how the shell processes a command line and initiates execution of a program. The chapter also explains how to redirect input to and output from a command, construct pipes and filters on the command line, and run a com- mand 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 your shell displays may differ slightly from what appears in this book. Refer to Chapter 9 for more information on bash and to Chapter 11 for information on writing and executing bash shell scripts. In This Chapter The Command Line . . . . . . . . . . . . 220 Standard Input and Standard Output . . . . . . . . . . . . . . . . . . . . 226 Pipes . . . . . . . . . . . . . . . . . . . . . . . 234 Running a Program in the Background . . . . . . . . . . . . . . . . 237 kill: Aborting a Background Job . . 238 Filename Generation/Pathname Expansion . . . . . . . . . . . . . . . . . 239 Builtins . . . . . . . . . . . . . . . . . . . . . 243 7 The Shell220 Chapter 7 The Shell The Command Line The shell executes a program when you give it 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. In this book the term command refers to the characters you type on the command line as well as to 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 key- stroke 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 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 with- out any arguments. For example, ls by itself lists the contents of the working direc- tory. Most commands accept one or more arguments. 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. Arguments 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 com- mand is the name of the file you want to edit. The following command line shows cp copying the file named temp to tempcopy: $ cp temp tempcopyThe Command Line 221 Arguments are numbered starting with the command itself as argument zero. In this example cp is argument zero, temp is argument one, and tempcopy is argument two. The cp utility requires two arguments on the command line. (The utility can take more arguments but not fewer.) 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 fre- quently 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 the shell. By convention options are separate arguments that follow the name of the com- mand and usually precede other arguments, such as filenames. Most utilities require you to prefix options with a single hyphen. However, this requirement is specific to the utility and not the shell. GNU program options are frequently preceded by two hyphens in a row. For example, ––help generates a (sometimes extensive) usage message. Figure 7-1 first shows what happens when you give an ls command without any options. By default ls lists the contents of the working directory in alphabetical order, vertically sorted in columns. Next the –r (reverse order; because this is a GNU utility, you can also use ––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 $ ls alex house mark office personal test hold jenny names oldstuff temp $ ls -r test personal office mark house alex temp oldstuff names jenny hold $ ls -x alex hold house jenny mark names office oldstuff personal temp test $ ls -rx test temp personal oldstuff office names mark jenny house hold alex Figure 7-1 Using options222 Chapter 7 The Shell hyphens in this way, however. 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 col- umns, 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. Option arguments Some utilities have options that themselves require arguments. For example, the gcc utility has a –o option that must be followed by the name you want to give the exe- cutable file that gcc generates. Typically an argument to an option is separated from its option letter by a SPACE: $ gcc -o prog prog.c 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 mean a long listing of all files in the working directory or a listing of the file named –l. It is interpreted as the former. You should 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 You can use an alternative format in which the period refers to the working direc- tory and the slash indicates that the name refers to a file in the working directory: $ ls ./-l Assuming that you are working in the /home/alex directory, the preceding com- mand is functionally equivalent to $ ls /home/alex/-l You can give the following command to get a long listing of this file: $ ls -l -- -l 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 df –h (disk free) and ls –lh commands.The Command Line 223 These are conventions, not hard-and-fast rules, and a number of utilities do not fol- low them (e.g., find). Following such conventions is a good idea; it makes it much easier for users to work with your program. When you write shell programs that require options, follow the Linux option conventions. Processing the Command Line As you enter a command line, the Linux tty device driver (part of the Linux operating system 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 com- mand line to the shell for processing. 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 the shell looks for the name of the command. Usually the name of the command is the first item on the com- mand 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 com- mand 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 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 2) accept this option. An example follows. $ bzip2 --help bzip2, a block-sorting file compressor. Version 1.0.2, 30-Dec-2001. usage: bzip2 [flags and input files in any order] -h --help print this message -d --decompress force decompression -z --compress force compression -k --keep keep (don't delete) input files -f --force overwrite existing output files -t --test test compressed file integrity -c --stdout output to standard out -q --quiet suppress noncritical error messages -v --verbose be verbose (a 2nd -v gives more) ...224 Chapter 7 The Shell optional The shell does not require that the name of the program appear first on the com- mand line. Thus you can structure a command line as follows: $ >bb ) instructs the shell to redirect the output of a com- mand 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 $ cat This is a line of text. This is a line of text. Cat keeps copying lines of text Cat keeps copying lines of text until you press CONTROL-D at the beginning until you press CONTROL-D at the beginning of a line. of a line. CONTROL-D $ Figure 7-5 The cat utility copies standard input to standard outputStandard Input and Standard Output 229 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 ordi- nary file the shell redirects the output to. Figure 7-7 uses cat to demonstrate output redirection. This figure contrasts with Figure 7-3 on page 226, where both standard input and standard output are associ- ated with the keyboard and the screen. The input in Figure 7-7 comes from the key- board. The redirect output symbol on the command line causes the shell to associate cat’s standard output with the sample.txt file specified on the command line. 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 the 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. This procedure is useful for making short, simple files. Figure 7-6 Redirecting standard output Command Standard input Shell Standard outputS h e ll File 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 232. $ 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 redirected230 Chapter 7 The Shell Figure 7-8 shows how to use cat and 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 standard output. In this case standard output is redirected to the file supply_orders. The final cat command shows that supply_orders contains the con- tents of all three files. Redirecting Standard Input Just as you can redirect standard output, so you can redirect standard input. The redirect input symbol (<) instructs the shell to redirect a command’s input to come from the specified file instead of from the keyboard (Figure 7-9). The format of a command line that redirects input 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 ordi- nary file the shell redirects the input from. Figure 7-10 shows cat with its input redirected from the supply_orders file that was created in Figure 7-8 and standard output going to the screen. This setup causes cat to display the sample file on the screen. The system automatically supplies an EOF (end of file) signal at the end of an ordinary file. Utilities that take input from a file or standard input Giving a cat command with input redirected from a file yields the same result as giv- ing a cat command with the filename as an argument. The cat utility is a member of a class of Linux utilities that function in this manner. Other members of this class of utilities include lpr, sort, and grep. These utilities first examine the command line $ cat stationery 2,000 sheets letterhead ordered: 10/7/05 $ cat tape 1 box masking tape ordered: 10/14/05 5 boxes filament tape ordered: 10/28/05 $ cat pens 12 doz. black pens ordered: 10/4/05 $ cat stationery tape pens > supply_orders $ cat supply_orders 2,000 sheets letterhead ordered: 10/7/05 1 box masking tape ordered: 10/14/05 5 boxes filament tape ordered: 10/28/05 12 doz. black pens ordered: 10/4/05 $ Figure 7-8 Using cat to catenate filesStandard Input and Standard Output 231 that you use to call them. If you include a filename on the command line, the utility takes its input from the file you specify. If you do not specify a filename, the utility takes its input from standard input. It is the utility or program—not the shell or operating system—that functions in this manner. noclobber: Avoids Overwriting Files The shell provides a feature called noclobber that stops you from inadvertently overwriting an existing file using redirection. When you enable this feature by set- ting the noclobber variable and then attempt to redirect output to an existing file, the shell displays an error message and does not execute the command. If the pre- ceding examples result in one of the following messages, the noclobber feature has been set. The following examples set noclobber, attempt to redirect the output from echo into an existing file, and then unset noclobber: $ 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 symbol you use for redirecting output (>|). In the following example, the user first creates a file named a by redirecting the out- put of date to the file. Next the user sets the noclobber variable and tries redirecting Figure 7-9 Redirecting standard input $ cat < supply_orders 2,000 sheets letterhead ordered: 10/7/05 1 box masking tape ordered: 10/14/05 5 boxes filament tape ordered: 10/28/05 12 doz. black pens ordered: 10/4/05 Figure 7-10 cat with its input redirected Command Standard outputShell Standard input File S h e ll232 Chapter 7 The Shell output to a again. The shell returns an error message. Then the user tries the same thing but using a pipe symbol after the redirect symbol. This time the shell allows the user to overwrite the file. Finally, the user unsets noclobber (using a plus sign in place of the hyphen) and verifies that it is no longer set. $ date > a $ set -o noclobber $ date > a bash: a: Cannot overwrite existing file $ date >| a $ set +o noclobber $ date > a Appending Standard Output to a File The append output symbol (>>) causes the shell to add new information to the end of a file, leaving any 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 cate- nation described in the preceding caution box: $ cat orange this is orange $ cat pear >> orange $ cat orange this is orange this is pear Redirecting output can destroy a file II caution Depending on which shell you are using and how your environment has been set up, a command such as the following may give you undesired results: $ cat orange pear > orange cat: orange: input file is output file Although cat displays an error message, the shell goes ahead and 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 orig- inal orange file. If you want to catenate two files into one, use cat to put the two files into a tem- porary file and then use mv to rename this third 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 151) 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 may take a moment to appear, giving you a sense that the command is running correctly. Even after you see the error message, it may take a while to realize that you destroyed the contents of a.Standard Input and Standard Output 233 You first see the contents of the orange file. Next the contents of the pear file is added to the end of (catenated with) the orange file. The final cat shows the result. The next example 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 line in Figure 7-11 redirects the output from date to the file named whoson. Then cat displays the file. Next the example appends the output from who to the whoson file. Finally cat displays the file containing the output of both utilities. /dev/null: Making Data Disappear The /dev/null device is a data sink, commonly referred to as a bit bucket. You can redirect output that you do not want to keep or see to /dev/null. The output disap- pears without a trace: $ echo "hi there" > /dev/null $ When you read from /dev/null, you get a null string. Give the following cat command to truncate a file named messages to zero length while preserving the ownership and permissions of the file: $ ls -l messages -rw-r--r-- 1 alex pubs 25315 Oct 24 10:55 messages $ cat /dev/null > messages $ ls -l messages -rw-r--r-- 1 alex pubs 0 Oct 24 11:02 messages 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 and the noclobber feature is not on, you will overwrite the orange file. Even if you have the noclobber feature turned on, it is a good idea to keep backup copies of files you are manipulating in these ways in case you make a mistake. Although it protects you from making an erroneous redirection, noclobber does not stop you from overwriting an existing 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. $ date > whoson $ cat whoson Thu Mar 27 14:31:18 PST 2008 $ who >> whoson $ cat whoson Thu Mar 27 14:31:18 PST 2008 sam console Mar 27 05:00(:0) alex pts/4 Mar 27 12:23(:0.0) alex pts/5 Mar 27 12:33(:0.0) jenny pts/7 Mar 26 08:45 (bravo.example.com) Figure 7-11 Redirecting and appending output234 Chapter 7 The Shell Pipes The shell uses a pipe to connect standard output of one command directly to stan- dard 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 sepa- rate 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 to generate the same result as the following group of 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 (trans- late) utility takes its input from standard input only. In its simplest usage tr has the following format: tr string1 string2 The tr utility accepts input from standard input and looks for characters that match one of the characters in string1. Upon finding a match, tr 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 $ ls > temp $ lpr temp $ rm temp or $ ls | lpr Figure 7-12 A pipeStandard Input and Standard Output 235 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 sends the same list (with the exception of temp) to the printer using a pipe. 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 153) takes its input from the file specified on the command line or, when a file is not specified, from standard input and sends its output to standard output. The sort command line in Figure 7-13 takes its input from standard input, which is redirected (<) to come from temp. The output that sort sends to the screen lists the users in sorted (alpha- betical) order. Because sort can take its input from standard input or from a filename on the com- mand line, omitting the < symbol from Figure 7-13 yields the same result. Figure 7-14 achieves the same result without creating the temp file. Using a pipe the shell redirects the output from who to the input of sort. The sort utility takes input from standard input because no filename follows it on the command line. When many people are using the system and you want information about only one of them, you can send the output from who to grep (page 151) using a pipe. The grep util- ity displays the line containing the string you specify—sam in the following example: $ who | grep 'sam' sam console Mar 24 05:00 $ who > temp $ sort < temp alex pts/4 Mar 24 12:23 alex pts/5 Mar 24 12:33 jenny pts/7 Mar 23 08:45 sam console Mar 24 05:00 $ rm temp Figure 7-13 Using a temporary file to store intermediate results $ who | sort alex pts/4 Mar 24 12:23 alex pts/5 Mar 24 12:33 jenny pts/7 Mar 23 08:45 sam console Mar 24 05:00 Figure 7-14 A pipe doing the work of a temporary file236 Chapter 7 The Shell 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 148). $ 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 stan- dard 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 ver- satility of Linux utilities. The three utilities who, sort, and lpr were not specifically designed to work with each other, but they all use standard input and standard out- put 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. The utility is aptly named: It takes a single 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 lines containing the string sam. $ who | tee who.out | grep sam sam console Mar 24 05:00 $ cat who.out sam console Mar 24 05:00 alex pts/4 Mar 24 12:23 alex pts/5 Mar 24 12:33 jenny pts/7 Mar 23 08:45 Figure 7-15 Using teeRunning a Program in the Background 237 Running a Program in the Background Foreground In all the examples so far in this book, commands were run in the foreground. When you run a command in the foreground, the shell waits for it to finish before giving you another prompt and allowing you to continue. When you run a com- mand in the background, you do not have to wait for the command to finish before you start running another command. Jobs 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 super- vision. It leaves the screen free so that you can use it for other work. Of course, when you are using a GUI, you can open another window to run another job. Job number, PID number 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. Then the shell displays another prompt and you can enter another command. When the background job finishes running, 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 and sends its output 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 may display something similar to ls ––color=tty –l. This difference is due to the fact that ls is aliased [page 328] to ls ––color=tty.) Moving a Job from the Foreground to the Background CONTROL-Z 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 use the job number when there is only one stopped job.238 Chapter 7 The Shell Only the foreground job can take input from the keyboard. To connect the key- board to a program running in the background, you must bring it into the fore- ground. 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 (%), fol- lowed 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 exam- ple), and you can enter any input the program requires to continue: bash $ fg 1 promptme Redirect the output of a job you run in the background to keep it from interfering with whatever you are doing on the screen. Refer to “Separating and Grouping Commands” on page 286 for more detail about background tasks. kill: Aborting a Background Job The interrupt key (usually CONTROL-C) cannot abort a process you are running in the background; you must use kill (page 522) for this purpose. Follow kill on the com- mand line with either the PID number of the process you want to abort or a percent sign (%) followed by the job number. Determining a PID number with ps If you forget the PID number, you can use the ps (process status) utility (page 310) to display it. The following example runs a tail –f outfile command (the –f 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 $ If you forget the job number, you can use the jobs command to display a list of job numbers. The next example is similar to the previous one but uses the job number instead of the PID number to kill the job. Sometimes the message saying that the job is terminated does not appear until you press RETURN after the RETURN that ends the kill command: $ tail -f outfile & [1] 18236 $ bigjob & [2] 18237 $ jobs [1]- Running tail -f outfile & [2]+ Running bigjob & $ kill %1 $ RETURN [1]- Terminated tail -f outfile $Filename Generation/Pathname Expansion 239 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 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 that the command line calls. Filenames that contain these special characters are called ambiguous file refer- ences because they do not refer to any 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, sav- ing you the effort of typing the names individually. They can also help you 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. The ? Special Character The question mark (?) is a special character that causes the shell to generate file- names. 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” that the shell generated the filenames it was called with. If no filename matches the ambiguous file refer- ence, 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 337). 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 memo12 memo9 memoalex newmemo5 memo memo5 memoa memos $ ls memo? memo5 memo9 memoa memos The memo? ambiguous file reference does not match mem, memo, memo12, memoalex, or newmemo5. You can also use a question mark in the middle of an ambiguous file reference: $ ls 7may4report may4report mayqreport may_report may14report may4report.79 mayreport may.report $ ls may?report may.report may4report may_report mayqreport240 Chapter 7 The Shell To practice generating filenames, you can use echo and ls. The echo utility displays the arguments that the shell passes to it: $ echo may?report may.report may4report may_report mayqreport The shell first expands the ambiguous file reference into a list of all files in the working directory that match the string may?report and then passes this list to echo, as though you had entered the list of filenames as arguments to echo. Next echo dis- plays the list of filenames. A question mark does not match a leading period (one that indicates a hidden file- name; see page 188). When you want to match filenames that begin with a period, you must explicitly include the period in the ambiguous file reference. 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 fol- lowing example shows all of the files in the working directory and then shows three commands that display all the filenames that begin with the string memo, end with the string mo, and contain the string alx: $ ls amemo memo memoalx.0620 memosally user.memo mem memo.0612 memoalx.keep sallymemo memalx memoa memorandum typescript $ 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.sally sally.0612 thurs memo.0612 report saturday $ ls -a . .aaa aaa memo.sally sally.0612 thurs .. .profile memo.0612 report saturday $ echo * aaa memo.0612 memo.sally report sally.0612 saturday thurs $ echo .* . .. .aaa .profileFilename Generation/Pathname Expansion 241 In the following example .p* does not match memo.0612, private, reminder, or report. Next 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 par- ent 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 . .private memo.0612 reminder .. .profile private report $ echo .p* .private .profile $ ls .* .private .profile .: memo.0612 private reminder report ..: . . $ echo .* . .. .private .profile You can take advantage of ambiguous file references when you establish conven- tions for naming files. For example, when you end all text filenames with .txt, you can reference that group of files with *.txt. The next command uses this convention to send all the 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 file- names containing the individual characters. Whereas memo? matches memo fol- lowed by any character, memo[17a] is more restrictive, and 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 charac- ter-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 the 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.242 Chapter 7 The Shell $ echo [aeiou]* ... $ less page[2468].txt ... A hyphen within brackets defines a range of characters within a character-class def- inition. 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. 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 defini- tions. 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 next two examples list the names of some of the files in the working directory. The first lists the files 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 [^ab]* matches any filename that does not begin with a or b. 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 begin with b, c, or d. $ ls aa ab ac ad ba bb bc bd cc ddBuiltins 243 $ ls *[^ab] ac ad bc bd cc ddcc dd $ ls [b-d]* ba bb bc bd cc dd You can match a hyphen (–) or a closing bracket (]) by placing it immediately before the final closing bracket. 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 (refer to “Special Characters” on page 146). The shell does not recognize this question mark as a special character and passes it on 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. 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 environ- ment 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 is a shell builtin. The shell always executes a shell builtin before try- ing to find a command or utility with the same name. See page 444 for an in-depth discussion of builtin commands and page 457 for a list of bash builtins. Listing bash builtins To get a complete list of bash builtins, give the command info bash builtin. To dis- play a page with more information on each builtin, move the cursor to one of the lines listing a builtin command and press RETURN. Alternatively, after typing info 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 ambig- uous file references. The shell expands the ambiguous file references and passes a list of ordinary filenames to the utility. In the previous examples, echo shows this to be true because it simply displays its arguments; it never displays the ambiguous file reference.244 Chapter 7 The Shell bash, give the command /builtin to search the bash documentation for the string builtin. The cursor will rest on the word Builtin in a menu; press RETURN to display the builtins menu. 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 then search for the section on builtins with the command /^SHELL BUILTIN COMMANDS (search for a line that begins with 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 com- mand. 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 that it cannot find or execute the program. If the com- mand is a simple filename, the shell searches the directories given in the variable PATH in an attempt to locate the command. When it executes a command, the shell assigns one file to the command’s standard input and another file to its standard output. By default the shell causes a com- mand’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 dis- plays a list of jobs 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 may 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.Exercises 245 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. 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 pro- cesses 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 notesb ref2 section1 section3 section4b notesa ref1 ref3 section2 section4a sentrev Give commands for each of the following, using wildcards to express file- names with as few characters as possible. a. List all files that begin with section. b. List the section1, section2, and section3 files only. Table 7-1 New utilities Utility Function tr Maps one string of characters into another (page 234) tee Sends standard input to both a file and standard output (page 236) bg Moves a process into the background (page 237) fg Moves a process into the foreground (page 238) jobs Displays a list of currently running jobs (page 238)246 Chapter 7 The Shell c. List the intro file only. d. List the section1, section3, ref1, and ref3 files. 5. Refer to the documentation of utilities in the man pages to determine which commands will a. Output the number of lines in the standard input that contain the word a or A. b. Output only the names of the files in the working directory that contain the pattern $(. c. List the files in the working directory in their 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 the standard output from a sort command into a file named phone_list. Assume that 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 com- mand 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 is grep used as a filter? 9. Explain the following error message. What filenames would a subsequent ls display? $ ls abc abd abe abf abg abh $ rm abc ab* rm: cannot remove 'abc': No such file or directoryAdvanced Exercises 247 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. Dem- onstrate that this is true. 11. In experimenting with shell variables, Alex accidentally deletes his PATH variable. He decides that he does not need the PATH variable. Discuss some of the problems he may soon encounter and explain the reasons for these problems. How could he easily return PATH to its original value? 12. Assume that your permissions allow you to write to a 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 can- not delete. 13. If you accidentally create a filename that contains a nonprinting character, such as a CONTROL character, how can you rename 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?This page intentionally left blank 249 I PART III Digging into Ubuntu Linux CHAPTER 8 Linux GUIs: X and GNOME 251 CHAPTER 9 The Bourne Again Shell 275 CHAPTER 10 Networking and the Internet 353 CHAPTER 11 Programming the Bourne Again Shell 395This page intentionally left blank 251251 8Chapter8This 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 and Side panes, the control bars, and the menubar. The final section explores some GNOME utilities, including the new Deskbar applet and Terminal, the GNOME terminal emulator. In This Chapter X Window System . . . . . . . . . . . . . 252 Starting X from a Character- Based Display . . . . . . . . . . . . . . 254 Remote Computing and Local Displays . . . . . . . . . . . . . . . . . . . 254 Window Managers . . . . . . . . . . . . 259 The Nautilus File Browser Window . . . . . . . . . . . . . . . . . . . 260 GNOME Utilities . . . . . . . . . . . . . . 266 Run Application Window . . . . . . . 269 GNOME Terminal Emulator/Shell . . . . . . . . . . . . . 270 8 Linux GUIs: X and GNOME252 Chapter 8 Linux GUIs: X and GNOME 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 sys- tem 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 propri- etary 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 4). 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. Ubuntu uses the X.org X server, named X; it is func- tionally equivalent to the one distributed by XFree86 because most of the code is the same so modules designed to work with one server work with the other. The X stack 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 pro- grams use Xlib, the next layer, which is a standard library for interfacing with an X 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 pro- gramming 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.X Window System 253 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 pro- gram 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 254. When you start an X Window System session, you set up a client/server environ- ment. 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, xclock, 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. 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. Figure 8-1 The X stack Graphical applications GTK Tk Motif OtherQt toolkits Xlib X server Linux kernel The roles of X client and server may 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 pro- gram 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 provid- ing 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.254 Chapter 8 Linux GUIs: X and GNOME Separating the physical control of the display (the server) from the processes need- ing access to the display (the client) makes it possible to run the server on one com- puter and the client on another computer. In general, 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 136), you can start an X Win- dow System server by using startx. See page 506 for information on creating a /etc/inittab file that causes Linux to boot into recovery (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 application (client) is to use, you can either set a glo- bal shell variable or use a command line option. Before you can connect to a remote X server, you must turn off two security features: You must run xhost on the server to give the client permission to connect to the X server and you must turn off the X –nolisten tcp option on the server. 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 lessens system security. Both of 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 a server named tiny and a client named dog.X Window System 255 The X –nolisten tcp Option As Ubuntu is installed, the X server starts with the –nolisten tcp option, which pro- tects 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, select Main menu: SystemDAdministrationDLogin Window, Security tab, and remove the tick from the check box labeled Deny TCP connections to Xserver. xhost Grants Access to a Display As Ubuntu is 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 sys- tem named tiny and wants to allow a user on dog to use his display (X server). Max runs the following 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 dis- play, he can give the following command: $ xhost + access control disabled, clients can connect from any host If you frequently work with other users via a network, you may find it convenient to add an xhost line to your .bash_profile file (page 277), but see the adjacent tip regarding security and xhost. Be selective in granting access to your X display with xhost, however; if another system has access to your display, you may find your work frequently interrupted. Security and the X –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 “X11 forwarding” on page 708 for information on setting up ssh so it forwards X. 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 rea- son, 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.256 Chapter 8 Linux GUIs: X and GNOME The DISPLAY Variable The most common method of identifying a display is to use the DISPLAY shell envi- ronment 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 screen- number 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 local- host:10.0. See “X11 forwarding” on page 708 for information on setting up ssh so that 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 xclock, on the local system but have it use the X dis- play on a remote system is to change the value of the DISPLAY variable on the client system so that it identifies the remote X server. sam@dog:~$ export DISPLAY=tiny:0.0 sam@dog:~$ xclock & The preceding example shows Sam running xclock 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 xclock and see which keyboard it accepts input from. If this example generates an error, refer back to the two preceding sections, which explain how to set up the server to allow a remote system to connect to it. The –display Option For a single command, you can specify the X server on the command line: 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 dis- play named by DISPLAY.X Window System 257 sam@dog:~$ xclock -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 1029]). 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 envi- ronment, such as from a terminal emulator, you must work with root privileges; you will initiate a privileged X session. The following command starts an X server running at 16 bits per pixel: $ startx -- -depth 16 & Refer to “Using Virtual Consoles” on page 136 for information on how to switch to a vir- tual console to start a second server where you do not have to work with root privileges. Switch User When you click the Logout object (Figure 4-2, page 91), select Switch User, and log in as a different user, Ubuntu starts a second X server to accommodate that user. When the second user logs off, the original X server displays the first user’s desktop. You can switch between the X servers (and users) by selecting the virtual console (page 136) that displays the X server you want to work with. X over ssh See “Tunneling/Port Forwarding” on page 725 for information about running X over an ssh connection. Stopping the X Server How you terminate a window manager depends on which window manager you are 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 522) the process running X. You can also press CONTROL-ALT-BACKSPACE to quit the X server. This method may not shut down the X session cleanly; use it only as a last resort.258 Chapter 8 Linux GUIs: X and GNOME 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 con- veniently on button 2 or 3 (the right button on a two- or three-button mouse). “Mouse Preferences” on page 95 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: $ xmodmap -pp There are 9 pointer buttons defined. Physical Button Button Code 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 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'X Window System 259 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. Window Managers Conceptually X is very simple and does not provide some of the more common fea- tures found in GUIs, such as the ability to drag windows. The UNIX/Linux philoso- phy is one of modularity: X relies on a window manager, such as 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 KDE or GNOME, provides a means of launching applications and utilities, such as a file manager, that work with a window manager. KDE and GNOME 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 4). For this reason the Free Software Foundation decided to support a different project, the GNU Network Object Model Environ- ment (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 Ubuntu Linux. It provides a sim- ple, coherent user interface that is suitable for corporate use. GNOME uses GTK for 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 tech- nology, if it can be reused, but creates its own if nothing else is available or a supe- rior 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. 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. Interoperability Since version 2, GNOME has focused on simplifying its 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. KDE has moved in the opposite direction, emphasizing configurability.260 Chapter 8 Linux GUIs: X and GNOME 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. 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 inter- face is widely regarded as one of the most intuitive found on a UNIX platform. GNUStep has less overhead than KDE and GNOME, so it runs better on older hardware. If you are running Linux on hardware that struggles with GNOME and KDE or you would prefer a user interface that does not attempt to mimic Windows, try GNUStep. WindowMaker is provided in the wmaker package. The Nautilus File Browser Window “Using Nautilus to Work with Files” on page 96 presented an introduction to using Nautilus. This section discusses the Nautilus File Browser window in more depth. Figure 8-2 shows a File Browser window with a Side pane (sometimes called a sidebar), View pane, menubar, toolbar, location bar, and status bar. To Figure 8-2 Nautilus File Browser window displaying icons Menubar Main toolbar Location bar Side pane button Side pane View pane Status bar HandleThe Nautilus File Browser Window 261 display your home folder in a File Browser window, select Main menu: PlacesD Home Folder. The View Pane The View pane displays icons or a list of filenames. Select the view you prefer from the drop-down list at the right end of the location bar. Figure 8-2 shows View as Icons and Figure 8-3 shows View as List. Objects in the View pane behave exactly as objects on the desktop do. See the sections starting on page 90 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. Or, you can use the clipboard (page 113) to cut/copy and paste objects. The Side Pane The Side pane augments the information Nautilus displays in the View pane. Press F9 or click the small x at the top of the Side pane to close it. You can display the Side pane by pressing F9 or selecting File Browser menu: ViewDSide Pane. To change the horizontal size of the Side pane and its contents, drag the handle (Figure 8-2) on its right side. Figure 8-3 Nautilus File Browser window displaying a List view and a textual location bar Location bar Nautilus can open a terminal emulator tip When you install the nautilus-open-terminal package and reboot the system, 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 188).262 Chapter 8 Linux GUIs: X and GNOME The Side pane can display six types of information. The button at its top controls which type it displays. This button is initially labeled Places; click it to display the Side pane drop-down list, which has the following selections: Places Places lists folders. Double-click one of these folders to display that folder in the View pane. You can open a directory in a new File Browser window by right- clicking the directory in Places and selecting Open in New Window. Places contains two parts: The list above the divider is static and holds your home directory, your desktop, the filesystem, unmounted filesystems, and the trash. The list below the divider holds bookmarks. Add a bookmark by display- ing the directory you want to bookmark in the View pane and pressing CONTROL-D or by selecting Browser menu: BookmarksDAdd Bookmark. Remove a bookmark by selecting Browser menu: BookmarksDEdit Bookmarks or by right-clicking the bookmark and selecting Remove. You can also use Edit Bookmarks to reorder bookmarks. Information Information presents information about the folder displayed by the View pane. Tree Tree presents an expandable tree view of your home folder, and each mounted file- system. Each directory in the tree has a triangle to its left. Click a triangle that points right to expand a directory; click a triangle that points down to close a directory. Click a directory in the tree to display that directory in the View pane. Double-click a directory to expand it in the Side pane and display it in the View pane. History History displays a chronological list of the folders that have been displayed in the View pane, with the most recently displayed folder at the top. Double-click a folder in this list to display it in the View pane. Notes Notes provides a place to keep notes about the folder displayed in the View pane. Emblems Similar to the Emblems tab in the Object Properties window (page 117), Emblems allows you to drag emblems from the Side pane and drop them on objects in the View pane. Drag and drop the Erase emblem to erase emblems associated with an object. You cannot erase emblems that Ubuntu places on objects, such as locked and link emblems. Control Bars This section discusses the four control bars that initially appear in a File Browser window: the Status bar, menubar, Main toolbar, and location bar (Figure 8-2). 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 object(s) are selected. The next section describes the menubar in detail.The Nautilus File Browser Window 263 Main toolbar The Main toolbar appears below the menubar and holds navigation tool icons: Back, Forward, Up, Stop, Reload, Home, Computer, and Search. If the Main tool- bar 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 drop-down list of the remaining icons. Location bar Below the Main toolbar is the location bar, which displays the name of the directory that appears in the View pane. It can display this name in two formats: iconic (using buttons) and textual (using a text box). Press CONTROL-L to switch to textual format, click the pencil and paper icon at the left of this bar to switch between iconic and textual formats. In iconic format, each button represents a directory in a pathname (page 189). 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 direc- tory buttons. In textual format, the text box displays the absolute pathname of the displayed directory. To have Nautilus display another directory, enter the pathname of the directory and press RETURN. The location bar also holds the magnification selector and the View as drop-down list. To change the magnification of the display in the View pane, click the plus or minus sign in a magnifying glass on either side of the magnification percentage. Right-click the magnification percentage itself to return to the default magnification. Left-click the magnification percentage to display a drop-down list of magnifica- tions. Click View as (to the right of the right-hand magnifying glass) to choose whether to view files as icons or as a list. Status bar 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 on the device that holds the directory displayed by the View pane. Menubar The Nautilus File Browser menubar controls what information the File Browser dis- plays 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 or Get Help Online for more information. This section describes the six parts of the menubar. File The several Open selections and the Property selection of File work with the high- lighted object(s) in the View pane. If no objects are highlighted, these selections are264 Chapter 8 Linux GUIs: X and GNOME grayed out. Selecting Connect to Server displays the Connect to Server window (Figure 8-4). This window presents a Service 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 so you can enter your username and password. Nautilus does not open a window but instead places an object, named for the URL you specified, on the desktop. Open the object to con- nect to and display the top-level 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 not displayed. This section discusses three selections from Edit: Create Archive, Backgrounds and Emblems, and Preferences. The EditDCreate Archive selection creates a single archive file comprising the selected objects. This selection opens a Create Archive 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 Archive text box allows you to specify a filename extension that determines the type of archive this tool creates. For example, .tar.gz creates a tar (page 161) file compressed by gzip (page 161) and .tar.bz2 creates a tar file com- pressed by bzip2 (page 160). The EditDBackgrounds and Emblems selection has three buttons on the left: Pat- terns, Colors, and Emblems. Click Patterns to display many pattern objects on the right side of the window. Drag and drop one of these objects on the View pane of a Figure 8-4 Connect to Server window Figure 8-5 Create Archive windowThe Nautilus File Browser Window 265 File Browser window to change the background of all File Browser View panes. Drag and drop the Reset object to reset the background to its default color and pattern (usually white). The Colors button works the same way as the Patterns button. The Emblems button works the same way as the Emblems tab in the Side pane (page 262). The EditDPreferences 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 EditDPreferencesDViews tab sets several defaults, including which view the File Browser displays (Icon or List view), the arrangement of the objects, and the default zoom level. Delete Versus Move to Trash The EditDPreferencesDBehavior 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 EditDPreferencesDDisplay tab specifies which information Nautilus includes in object (icon) captions. The three drop-down lists specify the order in which Nau- tilus displays information as you increase the zoom level of the View pane. This tab also specifies the date format Nautilus uses. The EditDPreferencesDList Columns tab specifies which columns Nautilus displays, and in what order it displays them, in the View pane when you select View as List. Figure 8-6 File Management Preferences window, Views tab266 Chapter 8 Linux GUIs: X and GNOME The EditDPreferencesDPreview tab controls when Nautilus displays or plays pre- views of files (local files, all files, no files). View Click the Main Toolbar, Side Pane, Location Bar, and Statusbar selections in View to display or remove these elements from the window. The Show Hidden Files selec- tion displays in the View pane files with hidden filenames (page 188). Go The Go selections display various folders in the View pane. Bookmark Bookmarks appear at the bottom of this menu and in the Side pane under Places. The Bookmark selections are explained under “Places” on page 262. Help The Help selections display local and online information about Nautilus. 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. Deskbar Applet Clicking the Deskbar applet (Figure 8-7) or pressing ALT-F3 opens the Deskbar Applet window (also in Figure 8-7). As you type in the text box labeled Search, this tool searches for the string you are entering. In Figure 8-7, the user has entered the string desk. Below the list box labeled History, the Deskbar Applet window displays matches it has found for desk. At the top of the list are actions that match or whose descriptions match the string. For example, clicking Launch: Take Screenshot displays a window that includes a radio button labeled Grab the whole desktop. Below the actions are places: When you click Desktop, the Desktop Applet opens the Nautilus File Browser Figure 8-7 Deskbar applet and Deskbar Applet window Deskbar appletGNOME Utilities 267 displaying the desktop. Finally, the window displays a list of Web searches. Click one of these to open Firefox and perform the search. You can also use the Search for Files win- dow (page 269) to search for files. The Deskbar Applet displays matches based on extensions. Right-click the Deskbar applet and select Preferences to open the Deskbar Preferences window. In the Searches tab of this window, you can select the extensions you want the applet to use and change the order in which it presents information generated by the extensions. For example, remove the tick from the check box labeled Programs to cause the Deskbar Applet window not to display programs in the Action section of its window. Experiment with enabling and disabling extensions and changing their order. The Extensions with Errors tab lists extensions the Deskbar applet cannot use. When you highlight one of the lines in the Extensions with Errors frame, the Desk- top Preferences window displays the reason the extension cannot be used. For example, when you click Beagle, the message Beagle does not seem to be installed appears below the frame. Install the beagle software package and, after the beagle-crawl-system cron script runs (it runs each night) and you reboot the system, this extension appears in the Searches tab. You can enable it and change its position in the list of extensions. Font Preferences The Fonts tab of the Appearance Preferences window (Figure 8-8) enables you to change the font that GNOME uses for applications, documents, the desktop, window titles, and terminal emulators (fixed width). To display this window, select Main menu: SystemDPreferencesDAppearance or enter gnome-appearance-properties on a command line. Click the Fonts tab. Click one of the five font bars in the upper part of the window to display the Pick a Font window (discussed next). Figure 8-8 Appearance Preferences window, Fonts tab268 Chapter 8 Linux GUIs: X and GNOME Examine the four sample boxes in the lower part of the window and select the one in which the letters look the best. Subpixel smoothing is usually best for LCD mon- itors. Click Details to refine the font rendering further, again picking the box in each frame in which the letters look the best. Pick a Font Window The Pick a Font window (Figure 8-9) appears when you need to choose a font (see “Font Preferences” on the previous page). From this window you can select a font family, a style, and a size. A preview of your choice appears in the Preview box in the lower part of the window. Click OK when you are satisfied with your choice. Pick a Color Window The Pick a Color window (Figure 8-10) appears when you need to specify a color, such as when you specify a solid color for the desktop background (page 103) or a panel. To specify a color for a panel, right-click the panel to display its context menu, click the Background tab, click the radio button labeled Solid color, and click within the box labeled Color. GNOME displays the Pick a Color window. 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. Figure 8-9 The Pick a Font windowGNOME Utilities 269 Run Application Window The Run Application window (Figure 4-4, page 93) enables you to run a program as though you had initiated it from a command line. To display the Run Application win- dow, 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 may 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 vim, in a terminal emulator window. Searching for Files The Search for Files window (Figure 8-11 on the next page) can help you find files whose locations or names you do not know or have forgotten. You can also use the Deskbar applet (page 266) to search for files. Open this window by selecting Main menu: PlacesDSearch for Files or enter gnome-search-tool on a command line from 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 con- tains 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 the Browse window (page 98). 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. You can refine the search by entering more search criteria. Click the triangle to the left of Select more options to expand the window and display more search criteria. GNOME initially displays two search criteria and a line for adding criteria as shown Figure 8-10 Pick a Color window270 Chapter 8 Linux GUIs: X and GNOME in Figure 8-12. With this part of the window expanded, GNOME incorporates all visible search criteria when you click Find. The first line below Select more options holds a text box labeled Contains the text. Absence of an entry in this text box 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. Add criteria by making a selection from the list box labeled Available options and clicking Add to the right of the drop-down list. Remove criteria by clicking 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 list box and click Add. The Search for Files win- dow 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 (Figure 4-17, page 114) displays a window that mimics a character-based terminal (page 114). To display a terminal emulator win- dow, select Main menu: ApplicationsDAccessoriesDTerminal or enter gnome- terminal on a command line from a Run Application window (ALT-F2). When the GNOME terminal emulator is already displayed, select Terminal menubar: FileDOpen 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, right- click the window and select Open Tab from the context menu or select Terminal menubar: FileDOpen Tab. A row of tabs appears below the menubar as gnome- terminal opens another terminal session on top of the existing one. Add as many ter- minal sessions as you like; click the tabs to switch between sessions. Figure 8-11 The Search for Files windowChapter Summary 271 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: EditDProfiles. High- light the profile you want to modify or click New to design a new profile. 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 Qt and KDE libraries to write KDE programs and the GTK+ and GTK+2 GNOME libraries to write GNOME 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 appli- cations 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, you can use the GNOME desktop manager as is or customize it to better suit your needs. It is a graphical user interface to sys- tem services (commands), the filesystem, applications, and more. Although not part of GNOME, the Metacity and Compiz window managers work closely with GNOME and are the default window managers for GNOME under Ubuntu Linux. Figure 8-12 The Search for Files window with Select more options expanded272 Chapter 8 Linux GUIs: X and GNOME A window manager controls all aspects of the windows, including placement, deco- ration, 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 that you can work with. The Side pane, which can display six 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 so 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? a. b. List four things you can do with Nautilus. c. How do you use Nautilus to search for a file? 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. a. b. How does Nautilus “know” which program to use to open different types of files? c. Which are the three common Nautilus control bars? What kinds of tools do you find on each? d. Discuss the use of the Nautilus location bar in textual mode. Advanced Exercises 5. Assume a mouse with nine pointer buttons defined. How would you reverse the effects of using the mouse wheel? 6. a. How would you use Nautilus to connect to the FTP server at ftp.ubuntu.com? a. b. Open the following folders: ubuntu, dists, and gutsy. How would you copy the file named Contents-i386.gz to the desktop? What type of file is Contents-i386.gz?Advanced Exercises 273 c. How would you open the Contents-i386.gz file on the desktop? How would you open the Contents-i386.gz file on the FTP server? Which file opens more quickly? Why? Which file can you modify? 7. Discuss the client/server environment the X Window System sets up. 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? What units does xwininfo display? What is the depth of a window? d. How can you get xwininfo to display the same information without hav- ing to click the window? 9. 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.)This page intentionally left blank 275275 9Chapter9This chapter picks up where Chapter 7 left off. Chapter 11 expands on this chapter, exploring control flow commands and more advanced aspects of programming the Bourne Again Shell. The bash home page is 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 pro- cesses 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 com- mands (for example, for loops and if statements). When you use a shell as a command interpreter, you can cus- tomize the environment you work in. You can make your prompt display the name of the working directory, create a function or 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, from a one-line script that stores a long, complex command to a longer script that runs a In This Chapter Startup Files . . . . . . . . . . . . . . . . . 277 Redirecting Standard Error . . . . . . 280 Writing a Simple Shell Script . . . . 282 Job Control. . . . . . . . . . . . . . . . . . . 290 Manipulating the Directory Stack . . . . . . . . . . . . . . . . . . . . . 292 Parameters and Variables . . . . . . 295 Processes . . . . . . . . . . . . . . . . . . . 310 History . . . . . . . . . . . . . . . . . . . . . . 312 Reexecuting and Editing Commands. . . . . . . . . . . . . . . . . 314 Functions. . . . . . . . . . . . . . . . . . . . 331 Controlling bash Features and Options . . . . . . . . . . . . . . . . 334 Processing the Command Line. . . 338 9 The Bourne Again Shell276 Chapter 9 The Bourne Again Shell 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 11 has some examples of these types of scripts. Most system shell scripts are written to run under the Bourne Again Shell. If you will ever work in recovery mode—as when you boot your system or do 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 11 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 commer- cial 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. dash Shell The bash executable file is about 700 kilobytes, has many features, and is well suited as a user login shell. The dash shell is about 80 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. Most system scripts are set up to run sh which, under Ubuntu, is a symbolic link to dash. This setup allows the system to boot and run system shell scripts quickly. On Linux 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 emu- late the original Bourne Shell. Korn Shell 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 command line editing, are based on similar features from the Korn Shell. POSIX standards The POSIX (the Portable Operating System Interface) family of related standards is being developed by PASC (IEEE’s Portable Application Standards Committee, www.pasc.org). A comprehensive FAQ on POSIX, including many links, appears at www.opengroup.org/austin/papers/posix_faq.html.Shell Basics 277 POSIX standard 1003.2 describes shell functionality. The Bourne Again Shell pro- vides the features that match the requirements of this POSIX 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 more closely match the POSIX requirements. Shell Basics This section covers writing and using startup files, redirecting standard error, writing and executing simple shell scripts, separating and grouping commands, implement- ing job control, and manipulating the directory stack. 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. Ubuntu Linux puts appropriate commands in some of these files. This section covers bash startup files. Login Shells The files covered in this section are executed by login shells and shells that you start with the ––login option. Login shells are, by their nature, interactive. /etc/profile The shell first executes the commands in /etc/profile. A user working with root privi- leges can set up this file to establish systemwide default characteristics for bash users. .bash_profile .bash_login .profile Next the shell looks for ~/.bash_profile, ~/.bash_login, and ~/.profile (~/ is short- hand 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. A shell running on a virtual terminal does not execute commands in these files. .bash_logout When you log out, bash executes commands in the ~/.bash_logout file. Frequently commands that clean up after a session, such as those that remove temporary files, go in this file. Interactive Nonlogin Shells The commands in the preceding startup files are not executed by interactive, non- login shells. However, these shells inherit from the login shell variables that are set by these startup files. /etc/bashrc Although not called by bash directly, many ~/.bashrc files call /etc/bashrc. This setup allows a user working with root privileges to establish systemwide default characteristics for nonlogin bash shells. .bashrc An interactive nonlogin shell executes commands in the ~/.bashrc file. Typically a startup file for a login shell, such as .bash_profile, runs this file, so that both login and nonlogin shells benefit from the commands in .bashrc.278 Chapter 9 The Bourne Again Shell Noninteractive Shells The commands in the previously described startup files are not executed by nonin- teractive shells, such as those that runs shell scripts. However, these shells inherit from the 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 fol- lowing 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 non- login shells. if [ -f ~/.bashrc ]; then source ~/.bashrc; fi The [ –f ~/.bashrc ] tests whether the file named .bashrc in your home directory exists. See pages 397 and 399 for more information on test and its synonym [ ]. Sample .bash_profile and .bashrc files follow. Some of the 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 434. $ cat ~/.bash_profile if [ -f ~/.bashrc ]; then source ~/.bashrc # read local startup file if it exists fi PATH=$PATH:. # add the working directory to PATH export PS1='[\h \W \!]\$ ' # 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 302). 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 303), which controls the user’s prompt. Use .bash_profile to set PATH tip Because commands in .bashrc may 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 302) 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 allows changes you make in an interactive session to prop- agate to subshells. In contrast, modifying a variable in .bashrc overrides changes inherited from a parent shell.Shell Basics 279 Next is a sample .bashrc file. The first command executes the commands in the /etc/bashrc file if it exists. Next the LANG (page 308) and VIMINIT (for vim initial- ization) variables are set and exported and several aliases (page 328) are established. The final command defines a function (page 331) that swaps the names of two files. $ cat ~/.bashrc if [ -f /etc/bashrc ]; then source /etc/bashrc # read global startup file if it exists fi set -o noclobber # prevent overwriting files unset MAILCHECK # turn off "you have new mail" notice export LANG=C # set LANG variable export VIMINIT='set ai aw' # set vim options alias df='df -h' # set up aliases alias rm='rm -i' # always do interactive rm's alias lt='ls -ltrh | tail' alias h='history | tail' function switch() # a function to exchange the names { # of two files local tmp=$$switch mv "$1" $tmp mv "$2" "$1" mv $tmp "$2" } . (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. You can run the startup file using the . (dot) or source builtin (they are the same command). As with all other commands, the . must be followed by a SPACE on the command line. Using the . or source builtin is similar to running a shell script, except that 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 that you run 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 vari- ables you rely on) may occur. 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. For more information refer to “Locality of Variables” on page 434. In the following example, .bashrc sets several variables and sets PS1, the prompt, to the name of the host. The . builtin puts the new values into effect. $ cat ~/.bashrc export TERM=vt100 # set the terminal type export PS1="$(hostname -f): " # set the prompt string export CDPATH=:$HOME # add HOME to CDPATH string stty kill '^u' # set kill line to control-u$ . ~/.bashrc bravo.example.com:280 Chapter 9 The Bourne Again Shell 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, even though some of them are not introduced until later. 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 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 sends a command’s standard error to the screen. Unless you redirect one or the other, you may not know the differ- ence between the output a command sends to standard output and the output it sends to standard error. This section covers the syntax used by the Bourne Again Shell. File descriptors A file descriptor is the place a program sends its output to and gets its input from. When you execute a program, the process running the program opens three file descriptors: 0 (standard input), 1 (standard output), and 2 (standard error). The redirect output symbol (> [page 228]) is shorthand for 1>, which tells the shell to redirect standard output. Similarly < (page 230) is short for 0<, which redirects standard input. The symbols 2> redirect standard error. For more information refer to “File Descriptors” on page 429. 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 out- put. Unless you redirect them, both messages appear on the screen. $ cat y This is y. $ cat x cat: x: No such file or directory Table 9-1 Builtin commands that are symbols Symbol Command ( ) Subshell (page 289) $( ) Command substitution (page 344) (( )) Arithmetic evaluation; a synonym for let (use when the enclosed value con- tains an equal sign) (page 458) $(( )) Arithmetic expansion (not for use with an enclosed equal sign) (page 342) [ ] The test command (pages 397, 399, and 412) [[ ]] Conditional expression; similar to [ ] but adds string comparisons (page 459)Shell Basics 281 $ 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 trans- lated 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 notation 2> tells the shell where to redirect standard error (file descriptor 2). The 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 Duplicating a file descriptor In the next example, 1> redirects standard output to hold. 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 the preceding example, 1> hold precedes 2>&1. If they had been listed in the opposite order, standard error would have been made a duplicate of standard out- put before standard output was redirected to hold. In that case only standard output would have been redirected to hold. 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.282 Chapter 9 The Bourne Again Shell Sending errors to standard error You can also use 1>&2 to redirect standard output of a command to standard error. This technique is used in shell scripts 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 pro- duced by the first echo will still go to the screen because you have not redirected stan- dard error. Because standard output of a shell script is frequently redirected to another file, you can use this technique to display on the screen error messages generated by the script. The lnks script (page 404) 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 448). The Bourne Again Shell supports the redirection operators shown in Table 9-2. Writing a Simple Shell Script A shell script is a file that contains commands that the shell can execute. The com- mands 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 com- piled 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 (page 234). You can also use pipes and redirection with the input and output of the script itself. Table 9-2 Redirection operators Operator Meaning < filename Redirects standard input from filename. > filename Redirects standard output to filename unless filename exists and noclobber (page 231) is set. If noclobber is not set, this redirection creates filename if it does not exist. >| filename Redirects standard output to filename, even if the file exists and noclobber (page 231) is set. >> filename Redirects and appends standard output to filename unless filename exists and noclobber (page 231) is set. If noclobber is not set, this redirection creates filename if it does not exist. <&m Duplicates standard input from file descriptor m (page 430). [n] >&m Duplicates standard output or file descriptor n if specified from file descriptor m (page 430). [n]<&– Closes standard input or file descriptor n if specified (page 430). [n] >&– Closes standard output or file descriptor n if specified.Shell Basics 283 In addition to the commands you would ordinarily use on the command line, con- trol 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 just as you would alter the order of execution of statements using a structured programming language. Refer to “Control Structures” on page 396 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 permis- sion to read and execute the file that contains the script (refer to “Access Permis- sions” on page 199). Read permission enables you to read the file that holds the script. Execute 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 exe- cutable file and issues an error message when you try to execute it. 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 argu- ment that bash executes so you do not need to have permission to execute whoson. Command not found? tip If you get the message $ whoson bash: whoson: command not found the shell is not set up to search for executable files in the working directory. Give this command instead: $ ./whoson The ./ tells the shell explicitly to look for an executable file in the working directory. To change the environment so that the shell searches the working directory automatically, see page 302.284 Chapter 9 The Bourne Again Shell 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 that the owner does not have permission to execute the file. Next chmod gives the owner execute permission: The 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 who may be the user of the file at any given time.) The second argument is the name of the file. The second ls shows an x in the fourth position, indicating that the owner now has exe- cute permission. 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 also have read access to the file. You do not need read access to execute a binary execut- able (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 199, ls (page 199), and chmod (page 200). #! Specifies a Shell You can put a special sequence of characters on the first line of a file to tell the oper- ating system which shell should execute the file. Because the operating system checks the initial characters of a program before attempting to exec it, 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 utility that should execute the script. This can be the pathname of any program, not just a shell. The following example specifies that bash should run the script: Figure 9-1 Using chmod to make a shell script executable $ ls -l whoson -rw-rw-r-- 1 alex group 40 May 24 11:30 whoson $ chmod u+x whoson $ ls -l whoson -rwxrw-r-- 1 alex group 40 May 24 11:30 whoson $ whoson Tue May 22 11:40:49 PDT 2007 Users Currently Logged In jenny pts/7 May 21 18:17 hls pts/1 May 22 09:59 scott pts/12 May 22 06:29 (bravo.example.com) alex pts/4 May 22 09:08Shell Basics 285 $ cat bash_script #!/bin/bash echo "This is a Bourne Again Shell script." The #! characters are useful if you have a script that you want to run with a shell other than the shell you are running the script from. The following example shows a script that should be executed by tcsh: $ cat tcsh_script #!/bin/tcsh echo "This is a tcsh script." set person = jenny 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 shell that is execut- ing 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 C STIME TTY TIME CMD alex 3031 3030 0 Nov16 pts/4 00:00:00 -bash alex 9358 3031 0 21:13 pts/4 00:00:00 /bin/tcsh ./tcsh_script2 alex 9375 9358 0 21:13 pts/4 00:00:00 ps -f If you do not follow #! with the name of an executable program, the shell reports that it cannot find the command that 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 shell may generate error messages or the script may not run properly. # Begins a Comment Comments make shell scripts and all code easier to read and maintain by you and others. If a pound sign (#) in the first character position of the first line of a script is not immediately followed by an exclamation point (!) or if a pound sign 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 pound sign and the end of the line (the next NEWLINE character). Running a Shell Script fork and exec system calls A command on the command line causes the shell to fork a new process, creating a duplicate of the shell process (a subshell). The new process attempts to exec (exe- cute) the command. Like fork, the exec routine is executed by the operating system (a system call). If the command is a binary executable program, such as a compiled286 Chapter 9 The Bourne Again Shell C program, exec succeeds and the system overlays the newly created subshell with the executable program. If the command is a shell script, exec fails. When exec fails, the command is assumed to be a shell script, and the subshell runs the commands in the script. Unlike a login shell, which expects input from the command line, the sub- shell takes its input from a file: the shell script. As discussed earlier, if you have a shell script in a file that you do not have execute permission for, you can run the commands in the script by using a bash command to exec a shell to run 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 connected to the terminal. 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 another kind of program). However, if bash is not your interactive shell or if you want to see how the script runs with differ- ent shells, you may want to run a script as an argument to bash or tcsh. 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 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 com- mand and does not change any aspect of how the command functions. You can exe- cute 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: 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.Shell Basics 287 $ 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. \ 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 that the shell does not treat the NEWLINE as a command terminator. Enclosing a backslash within single quotation marks 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 easier to break a line just 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) that 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 com- mand terminator because it occurs within a quoted string. The > is a secondary prompt indicating that the shell is waiting for you to continue the unfinished com- mand. 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.288 Chapter 9 The Bourne Again Shell | and & Separate Commands and Do Something Else The pipe symbol (|) and the background task symbol (&) are also command separa- tors. 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 exe- cute the task in the background so you get a prompt immediately and 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 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 (process identifica- tion) number for each process running in the background. You get a prompt as soon as f finishes, which may be before d or e finishes. Before displaying a prompt for a new command, the shell checks whether any back- ground jobs have completed. For each job that has completed, the shell displays its job number, the word Done, and the command line that invoked the job; then the shell 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 fol- lowed by a – character. Any other jobs listed show a SPACE character. After running the last command, the shell displays the following before issuing a prompt: [1]- Done d [2]+ Done e The next command line executes all three tasks as background jobs. You get a shell prompt immediately: $ d & e & f & [1] 14290 [2] 14291 [3] 14292 You can use pipes to send the output from one task to the next task and an amper- sand (&) to run the entire job as a background task. Again the prompt comes back immediately. The shell regards the commands joined by a pipe as being a single job. That is, it treats all pipes as single jobs, no matter how many tasks are connectedShell Basics 289 with the pipe (|) symbol or how complex they are. The Bourne Again Shell shows only one process placed in the background: $ d | e | f & [1] 14295 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 cre- ates a new process to execute each command (refer to “Process Structure” on page 310 for more information on creating subshells). Each subshell (job) has its own environment, meaning that it has its own set of variables with values that can differ from those of other subshells. The following command line executes commands a and b sequentially in the back- ground while executing c in the background. The shell prompt returns 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 back- ground 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 prompt returns immediately. $ (a ; b) & (c ; d) & [1] 15528 [2] 15529 The next script copies one directory to another. The second pair of parentheses cre- ates a subshell to run the commands following the pipe. Because of these parenthe- ses, 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 input from standard input. The shell variables $1 and $2 represent the first and second command line arguments (page 439), 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 sec- ond 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/alex/sources /home/alex/memo/biblio The cpdir command line copies the files and directories in the /home/alex/sources directory to the directory named /home/alex/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.290 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, 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 alex /tmp/* > alexfiles & [1] 18839 [2] 18876 The portion of the command line up to the first & is one job consisting of three pro- cesses connected by pipes: find, sort (page 153), and lpr (page 151). The second job is a single process running grep. Both jobs have been put into the background by the trailing & characters, 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 the commands that are running in the background or stopped. jobs: Lists Jobs The jobs builtin lists all background jobs. The following sequence demonstrates what happens when you give a jobs command. Here 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 job numbers to commands you run in the background (page 288). Several jobs are started in the background in the next example. For each job the shell lists the job number and PID number immediately, just before it issues a prompt. $ xclock & [1] 1246 $ date & [2] 1247 $ Sun Dec 2 11:44:40 PST 2007 [2]+ Done date $ find /usr -name ace -print > findout & [2] 1269 $ jobs [1]- Running xclock & [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.Shell Basics 291 In the preceding example, the jobs command lists the first job, xclock, 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 com- mand became job 2. To move a background job into 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 into the foreground: $ fg 2 or $ %2 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 exam- ple, fg %?ace also brings job 2 into the foreground. Often the job you wish to bring into the foreground is the only job running in the background or is the job that jobs lists with a plus (+). In these cases you can use fg without an argument. bg: Sends a Job to the Background To move the foreground job to the background, you must first suspend (temporarily stop) the job by pressing the suspend key (usually CONTROL-Z). Pressing the suspend key immediately suspends the job in the foreground. You can then use the bg builtin to resume execution of the job in the background. $ bg If a background job attempts to read from the terminal, the shell stops it and noti- fies you that the job has been stopped and is waiting for input. You must then move the job into the foreground so that it can read from the terminal. The shell displays the command line when it moves the job into the foreground. $ (sleep 5; cat > mytext) & [1] 1343 $ date Sun Dec 2 11:58:20 PST 2007 [1]+ Stopped ( sleep 5; cat >mytext ) $ fg ( sleep 5; cat >mytext ) Remember to let the cat out! CONTROL-D $ 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 output292 Chapter 9 The Bourne Again Shell 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 input that the command is waiting for. In this case the input needs to be terminated with a CONTROL-D to signify EOF (end of file). 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 is stopped, perhaps waiting for input from the terminal. The shell also lets you know when a foreground job is sus- pended. 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 337) to make the shell display these notices with- out delay. If you try to exit from a 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 leave the shell again, the shell allows you to leave and terminates the stopped jobs. Jobs that are running (not stopped) in the background continue to run. In the following example, find (job 1) continues to run after the second exit ter- minates the shell, but cat (job 2) is terminated: $ find / -size +100k > $HOME/bigfiles 2>&1 & [1] 1426 $ cat > mytest & [2] 1428 [2]+ Stopped cat >mytest $ exit exit There are stopped jobs. $ exit exit login: 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, creating a last-in first-out, (LIFO) 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: $ dirs ~/literatureShell Basics 293 The dirs builtin uses a tilde (~) to represent the name of the home directory. The examples in the next several sections assume that you are referring to the directory structure shown in Figure 9-2. pushd: Pushes a Directory on the Stack To change directories and at the same time add a new directory to the top of the stack, use the pushd (push directory) builtin. In addition to changing directories, the pushd builtin displays the contents of 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 Figure 9-2 The directory structure in the examples Figure 9-3 Creating a directory stack home sam demo promo literaturenames names demo literature 2 pushd 1 pushd 1 2294 Chapter 9 The Bourne Again Shell When you use pushd without an argument, it swaps the top two directories on the stack and makes the new top directory (which was the second directory) become the new working directory (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 directo- ries. 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 follow- ing 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 fol- lowing example and Figure 9-5 show, popd used without an argument 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 direc- tory number 1, demo: $ dirs ~/literature ~/demo ~/names $ popd +1 ~/literature ~/names Figure 9-4 Using pushd to change working directories names demo names demonamesdemo literature literature literature pushd pushdpushd pushdParameters and Variables 295 Removing a directory other than directory number 0 does not change the working directory. 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 num- ber. 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. User-created variables 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 original shell. One naming con- vention 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 434 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 assign- ment follows: $ myvar=abc The Bourne Again Shell permits you to put variable assignments on a command line. These assignments are local to the command shell—that is, they apply to the command only. The my_script shell script displays the value of TEMPDIR. The fol- lowing command 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. Figure 9-5 Using popd to remove a directory from the stack literature demo names popd296 Chapter 9 The Bourne Again Shell $ 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 of the keyword shell variables at any time but 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 277). Just as you can make user-created variables global, so you can make keyword variables global; this is usually done automatically in the 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 give a command, each argument on the command line becomes the value of a positional parameter. Positional parameters (page 438) enable you to access command line arguments, a capability that you will often require when you write shell scripts. The set builtin (page 442) enables you to assign values to posi- tional 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. You cannot assign values to special parameters. User-Created Variables The first line in the following example declares the variable named person and ini- tializes it with the value alex: $ person=alex $ echo person person $ echo $person alexParameters and Variables 297 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 alex. Instead, the string person is echoed as person. The shell substitutes the value of a variable only when you precede the name of the vari- able with a dollar sign ($). 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 that you called it with a variable. Quoting the $ 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 alex $ echo "$person" alex $ echo '$person' $person $ echo \$person $person SPACEs Because they do not prevent variable substitution but do turn off the special mean- ings 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="alex and jenny" $ echo $person alex and jenny $ person=alex and jenny bash: and: command not found When you reference a variable that contains TABs or multiple adjacent SPACEs, you need to use quotation marks to preserve the spacing. If you do not quote the vari- able, the shell collapses each string of blank characters into a single SPACE before passing the variable to the utility: $ person="alex and jenny" $ echo $person alex and jenny $ echo "$person" alex and jenny 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 pro- gram being executed. If the value of the variable contains a special character, such as * or ?, the shell may expand that variable.298 Chapter 9 The Bourne Again Shell Pathname expansion in assignments The first line in the following sequence of commands assigns the string alex* to the variable memo. The Bourne Again Shell does not expand the string because bash does not perform pathname expansion (page 239) when assigning a value to a vari- able. 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=alex* $ echo "$memo" alex* 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 alex.report alex.summary $ echo $memo alex.report alex.summary Here the shell expands the $memo variable to alex*, expands alex* to alex.report and alex.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. 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 planned. 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: $ PREF=counter $ WAY=${PREF}clockwise $ FAKE=${PREF}feit $ echo $WAY $FAKE counterclockwise counterfeitParameters and Variables 299 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 439). 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, set the value to null: $ person= $ echo $person $ You can remove a variable with the unset builtin. To remove the variable person, give the following command: $ 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=jenny $ echo $person jenny $ 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 “Listing variable attributes” on page 300 for an example (readonly and declare –r produce the same output). 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.300 Chapter 9 The Bourne Again Shell The following commands declare several variables and set some attributes. The first line declares person1 and assigns it a value of alex. This command has the same effect with or without the word declare. $ declare person1=alex $ declare -r person2=jenny $ declare -rx person3=helen $ declare -x person4 The readonly and export builtins are synonyms for the commands declare –r and declare –x, respectively. It is legal to 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 (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 equiva- lent 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 vari- able. You cannot remove a readonly attribute however. After the following com- mand is given, the variable person3 is no longer exported but it is still readonly. $ declare +x person3 You can also use typeset instead of declare. Listing variable attributes Without any arguments or options, the declare builtin lists all shell variables. The same list is output when you run set (page 442) 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 option –r with declare gives a list of all readonly shell variables. This list is the same as that produced by a readonly command without any arguments. After the declarations in the preceding example have been given, the results are as follows: $ declare -r declare -ar BASH_VERSINFO='([0]="2" [1]="05b" [2]="0" [3]="1" ... )' declare -ir EUID="500" declare -ir PPID="936" Table 9-3 Variable attributes (typeset or declare) Attribute Meaning –a Declares a variable as an array (page 432) –f Declares a variable to be a function name (page 331) –i Declares a variable to be of type integer (page 301) –r Makes a variable readonly; also readonly (page 299) –x Exports a variable (makes it global); also export (page 434)Parameters and Variables 301 declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:..." declare -ir UID="500" declare -r person2="jenny" declare -rx person3="helen" The first five entries are keyword variables that are automatically declared as read- only. 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 arith- metic 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: $ typeset -i COUNT Keyword Variables Keyword variables either are inherited or are 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 434) to make them available to child shells. HOME: Your Home Directory By default your home directory is your working directory when you log in. Your home directory is determined when you establish your account; its name is stored in the /etc/passwd file. $ grep sam /etc/passwd sam:x:501:501:Sam S. x301:/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/alex/laptop $ echo $HOME /home/alex $ cd $ pwd /home/alex This example shows the value of the HOME variable and the effect of the cd buil- tin. After you execute cd without an argument, the pathname of the working direc- tory 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 190) to denote a user’s home directory. The following example302 Chapter 9 The Bourne Again Shell uses echo to display the value of this shortcut and then uses ls to list the files in Alex’s laptop directory, which is a subdirectory of his home directory: $ echo ~ /home/alex $ 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 file- name 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 it, the shell reports Permission denied. If you give a simple filename as a command, the shell searches through certain directories 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 may be modified there. Normally the default specifies that the shell search several system directories used to hold common commands and then search the working directory. 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 that a search for an executable file starts with the /usr/local/bin direc- tory. If it does not find the file in this directory, the shell first looks 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 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 own executable files in their own ~/bin directories. If you putParameters and Variables 303 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. If you want to add directories to PATH, you can reference the old value of the PATH variable while you are 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/spool/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 that 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 exam- ple, when mail arrives). You can follow any of the filenames in the list with a ques- tion mark (?), followed by a message. The message replaces the you have mail message when you get 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. PS1: User Prompt (Primary) The default Bourne Again Shell prompt is a dollar sign ($). When you run bash with root privileges, you may have a pound sign (#) prompt. The PS1 variable holds the prompt string that the shell uses to let you know that it is waiting for a command. When you change the value of PS1, you change the appearance of your prompt. 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: ./ls. 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.304 Chapter 9 The Bourne Again Shell 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 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 that you enter after the prompt easier to read): $ PS1="$(hostname): " bravo.example.com: echo test test bravo.example.com: 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 pound sign). 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 pound sign for root and a dollar sign otherwise): $ PS1='\h \$ ' bravo $ $ PS1='\@ \u $ ' 09:44 PM alex $ $ PS1='\$ ' $ Table 9-4 describes some of the symbols you can use in PS1. For a complete list of spe- cial characters you can use in the prompt strings, open the bash man page and search for the second occurrence of PROMPTING (give the command /PROMPTING and then press n). Table 9-4 PS1 symbols Symbol Display in prompt \$ # if the user is running with root privileges; otherwise, $ \w Pathname of the working directory \W Basename of the working directory \! Current event (history) number (page 317) \d Date in Weekday Month Date formatParameters and Variables 305 PS2: User Prompt (Secondary) Prompt String 2 is a secondary prompt that the shell stores in PS2. On the first line of the next example, an unclosed quoted string follows echo. The shell assumes that the command is not finished and, on the second line, gives the default secondary prompt (>). This prompt indicates that 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: fol- lowed by a SPACE. A multiline echo demonstrates the new prompt. PS3: Menu Prompt PS3 holds the menu prompt for the select control structure (page 426). PS4: Debugging Prompt PS4 holds the bash debugging symbol (page 408). IFS: Separates Input Fields (Word Splitting) The IFS (Internal Field Separator) shell variable specifies the characters that you can use to separate arguments on a command line and 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 that these characters are not quoted or escaped. When you assign IFS character val- ues, these characters can also separate fields but only if they undergo expansion. This type of interpretation of the command line is called word splitting. \h Machine hostname, without the domain \H Full machine hostname, including the domain \u Username of the current user \@ Current time of day in 12-hour, AM/PM format \T Current time of day in 12-hour HH:MM:SS format \A Current time of day in 24-hour HH:MM format \t Current time of day in 24-hour HH:MM:SS format Table 9-4 PS1 symbols (continued)306 Chapter 9 The Bourne Again Shell 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 such file or directory cat: x: No such file or directory cat: y: No such file or directory cat: z: No such file or 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 argu- ment to cat. Now cat reports an error for four separate 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. 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 that the character p in the token export is interpreted as a separator as the preceding 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 edi- tor with two filenames: ort and VA R. Be careful when changing IFS caution Changing IFS has a variety of side effects so work cautiously. You may find it useful to first save the value of IFS before changing it; you can easily then restore the original value if you get unex- pected results. Alternatively, you can fork a new shell with 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. You can also set IFS to its default value with the following command: $ IFS=' \t\n'Parameters and Variables 307 $ $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. 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 like to work out of, this vari- able 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 directo- ries in the CDPATH list. If cd finds one, that directory becomes the working direc- tory. 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 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 (which you should never do when you are working with root privi- leges—refer to the tip on page 303), 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 an absolute pathname—one starting with a slash (/)—the shell does not consult CDPATH. Multiple separator characters tip Although sequences of multiple SPACE or TAB characters are treated as single separators, each occurrence of another field-separator character acts as a separator.308 Chapter 9 The Bourne Again Shell Keyword Variables: A Summary Table 9-5 lists the bash keyword variables. Table 9-5 bash keyword variables Variable Value BASH_ENV The pathname of the startup file for noninteractive shells (page 278) CDPATH The cd search path (page 307) COLUMNS The width of the display used by select (page 425) FCEDIT The name of the editor that fc uses by default (page 315) HISTFILE The pathname of the file that holds the history list (default: ~/.bash_history; page 312) HISTFILESIZE The maximum number of entries saved in HISTFILE (default: 500; page 312) HISTSIZE The maximum number of entries saved in the history list (default: 500; page 312) HOME The pathname of the user’s home directory (page 301); used as the default argument for cd and in tilde expansion (page 190) IFS Internal Field Separator (page 305); used for word splitting (page 345) INPUTRC The pathname of the Readline startup file (default: ~/.inputrc; page 325) LANG The locale category when that category is not specifically set with an LC_* variable LC_* 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 LINES The height of the display used by select (page 425) MAIL The pathname of the file that holds a user’s mail (page 303) MAILCHECK How often, in seconds, bash checks for mail (page 303) MAILPATH A colon-separated list of file pathnames that bash checks for mail in (page 303) PATH A colon-separated list of directory pathnames that bash looks for com- mands in (page 302) PROMPT_COMMAND A command that bash executes just before it displays the primary prompt PS1 Prompt String 1; the primary prompt (default: '\s–\v\$ '; page 303) PS2 Prompt String 2; the secondary prompt (default: '> '; page 305) PS3 The prompt issued by select (page 425) PS4 The bash debugging symbol (page 408) REPLY Holds the line that read accepts (page 446); also used by select (page 425)Special Characters 309 Special Characters Table 9-6 lists most of the characters that are special to the bash shell. Table 9-6 Shell special characters Character Use NEWLINE Initiates execution of a command (page 286) ; Separates commands (page 286) ( ) Groups commands (page 289) for execution by a subshell or identifies a func- tion (page 331) & Executes a command in the background (pages 237 and 288) | Sends standard output of preceding command to standard input of following command (pipe; page 288) > Redirects standard output (page 228) >> Appends standard output (page 232) < Redirects standard input (page 230) << Here document (page 427) * Any string of zero or more characters in an ambiguous file reference (page 240) ? Any single character in an ambiguous file reference (page 239) \ Quotes the following character (page 146) ' Quotes a string, preventing all substitution (page 146) " Quotes a string, allowing only variable and command substitution (pages 146 and 297) ‘...‘ Performs command substitution (page 344) [ ] Character class in an ambiguous file reference (page 241) $ References a variable (page 295) . (dot builtin) Executes a command (only at the beginning of a line, page 279) # Begins a comment (page 285) { } Used to surround the contents of a function (page 331) : (null builtin) Returns true (page 453) && (Boolean AND) Executes command on right only if command on left succeeds (returns a zero exit status, page 464) || (Boolean OR) Executes command on right only if command on left fails (returns a nonzero exit status; page 464) ! (Boolean NOT) Reverses exit status of a command $() Performs command substitution (preferred form; page 344) [] Evaluates an arithmetic expression (page 342)310 Chapter 9 The Bourne Again Shell Processes A process is the execution of a command by Linux. The shell that starts when you log in is a command, or a process, like any other. When you give the name of a Linux utility on the command line, you initiate a process. When you run a shell script, another shell process is started and additional processes are created for each command in the script. Depending on how you invoke the shell script, the script is run either by the current shell or, more typically, by a subshell (child) of the current shell. A process is not started when you run a shell builtin, such as cd. Process Structure fork system call Like the file structure, the process structure is hierarchical, with parents, children, and even a root. A parent process forks a child process, which in turn can fork other processes. (The term fork indicates that, as with a fork in the road, one process turns into two. Initially the two forks are identical except that one is identified as the parent and one as the child. You can also use the term spawn; the words are interchangeable.) The operating system routine, or system call, that creates a new process is named fork. When Linux begins execution when a system is started, it starts init, a single process called a spontaneous process, with PID number 1. This process holds the same posi- tion in the process structure as the root directory does in the file structure: It is the ancestor of all processes that the system and users work with. When the system is in multiuser mode, init runs getty or mingetty processes, which display login: prompts on terminals and virtual consoles. When someone responds to the prompt and presses RETURN, getty hands control over to a utility named login, which checks the username and password combination. After the user logs in, the login process becomes the user’s shell process. Process Identification PID number Linux assigns a unique PID (process identification) number at the inception of each process. As long as a process exists, it keeps the same PID number. During one ses- sion the same process is always executing the login shell. When you fork a new pro- cess—for example, when you use an editor—the PID number of the new (child) process is different from that of its parent process. When you return to the login shell, it is still being executed by the same process and has the same PID number as when you logged in. The following example shows that the process running the shell forked (is the parent of) the process running ps (page 238). When you call it with the –f option, ps dis- plays a full listing of information about each process. The line of the ps display with bash in the CMD column refers to the process running the shell. The column headed by PID identifies the PID number. The column headed PPID identifies the PID num- ber of the parent of the process. From the PID and PPID columns you can see that the process running the shell (PID 21341) is the parent of the process running sleepProcesses 311 (PID 22789). The parent PID number of sleep is the same as the PID number of the shell (21341). $ sleep 10 & [1] 22789 $ ps -f UID PID PPID C STIME TTY TIME CMD alex 21341 21340 0 10:42 pts/16 00:00:00 bash alex 22789 21341 0 17:30 pts/16 00:00:00 sleep 10 alex 22790 21341 0 17:30 pts/16 00:00:00 ps -f Refer to the ps man page for more information on ps and the columns it displays with the –f option. A second pair of sleep and ps –f commands shows that the shell is still being run by the same process but that it forked another process to run sleep: $ sleep 10 & [1] 22791 $ ps -f UID PID PPID C STIME TTY TIME CMD alex 21341 21340 0 10:42 pts/16 00:00:00 bash alex 22791 21341 0 17:31 pts/16 00:00:00 sleep 10 alex 22792 21341 0 17:31 pts/16 00:00:00 ps -f You can also use pstree (or ps ––forest, with or without the –e option) to see the parent–child relationship of processes. The next example shows the –p option to pstree, which causes it to display PID numbers: $ pstree -p init(1)-+-acpid(1395) |-atd(1758) |-crond(1702) ... |-kdeinit(2223)-+-firefox(8914)---run-mozilla.sh(8920)---firefox-bin(8925) | |-gaim(2306) | |-gqview(14062) | |-kdeinit(2228) | |-kdeinit(2294) | |-kdeinit(2314)-+-bash(2329)---ssh(2561) | | |-bash(2339) | | '-bash(15821)---bash(16778) | |-kdeinit(16448) | |-kdeinit(20888) | |-oclock(2317) | '-pam-panel-icon(2305)---pam_timestamp_c(2307) ... |-login(1823)---bash(20986)-+-pstree(21028) | '-sleep(21026) ... The preceding output is abbreviated. The line that starts with –kdeinit shows a graphical user running many processes, including firefox, gaim, and oclock. The line that starts with –login shows a textual user running sleep in the background while running pstree in the foreground. Refer to “$$: PID Number” on page 437 for a description of how to instruct the shell to report on PID numbers.312 Chapter 9 The Bourne Again Shell Executing a Command fork and sleep When you give the shell a command, it usually forks (spawns) a child process to execute the command. While the child process is executing the command, the par- ent process sleeps. While a process is sleeping, it does not use any computer time but remains inactive, waiting to wake up. When the child process finishes executing the command, it tells its parent of its success or failure via its exit status and then dies. The parent process (which is running the shell) wakes up and prompts for another command. Background process When you run a process in the background by ending a command with an amper- sand (&), the shell forks a child process without going to sleep and without waiting for the child process to run to completion. The parent process, which is executing the shell, reports the job number and PID number of the child and prompts for another command. The child process runs in the background, independent of its parent. Builtins Although the shell forks a process to run most of the commands you give it, some commands are built into the shell. The shell does not need to fork a process to run builtins. For more information refer to “Builtins” on page 243. Variables Within a given process, such as your login shell or a subshell, you can declare, initial- ize, read, and change variables. By default however, a variable is local to a process. When a process forks a child process, the parent does not pass the value of a variable to the child. You can make the value of a variable available to child processes (global) by using the export builtin (page 434). History The history mechanism, a feature adapted from the C Shell, maintains a list of recently issued command lines, also called events, providing a quick way to reexe- cute any of the events in the list. This mechanism also enables you to execute varia- tions of previous commands and to reuse arguments from them. You can replicate complicated commands and arguments that you used earlier in this login session or in a previous one and enter a series of commands that differ from one another in minor ways. The history list also serves as a record of what you have done. It can prove helpful when you have made a mistake and are not sure what you did or when you want to keep a record of a procedure that involved a series of commands. The history builtin displays the history list. If it does not, read on—you need to set some variables. Variables That Control History The value of the HISTSIZE variable determines the number of events preserved in the history list during a session. A value in the range of 100 to 1,000 is normal. When you exit from the shell, the most recently executed commands are saved in the file given by the HISTFILE variable (the default is ~/.bash_history). The next time you start the shell, this file initializes the history list. The value of the HISTFILESIZEHistory 313 variable determines the number of lines of history saved in HISTFILE (not necessar- ily the same as HISTSIZE). HISTSIZE holds the number of events remembered dur- ing a session, HISTFILESIZE holds the number remembered between sessions, and the file designated by HISTFILE holds the history list. See Table 9-7. Event number The Bourne Again Shell assigns a sequential event number to each command line. You can display this event number as part of the bash prompt by including \! in PS1 (page 303). Examples in this section show numbered prompts when they help to illustrate the behavior of a command. Give the following command manually or place it in ~/.bash_profile (to affect future sessions) to establish a history list of the 100 most recent events: $ HISTSIZE=100 The following command causes bash to save the 100 most recent events across login sessions: $ HISTFILESIZE=100 After you set HISTFILESIZE, you can log out and log in again, and the 100 most recent events from the previous login session will appear in your history list. Give the command history to display the events in the history list. The list of events is ordered with oldest events at the top of the list. The following history list includes a command to modify the bash prompt so that it displays the history event number. The last event in the history list is the history command that displayed the list. 32 $ history | tail 23 PS1="\! bash$ " 24 ls -l 25 cat temp 26 rm temp 27 vim memo 28 lpr memo 29 vim memo 30 lpr memo 31 rm memo 32 history | tail Table 9-7 History variables Variable Default Function HISTSIZE 500 events Maximum number of events saved during a session HISTFILE ~/.bash_history Location of the history file HISTFILESIZE 500 events Maximum number of events saved between sessions history can help track down mistakes tip When you have made a command line mistake (not an error within a script or program) and are not sure what you did wrong, look at the history list to review your recent commands. Sometimes this list can help you figure out what went wrong and how to fix things.314 Chapter 9 The Bourne Again Shell As you run commands and your history list becomes longer, it may run off the top of the screen when you use the history builtin. Pipe the output of history through less to browse through it, or give the command history 10 to look at the ten most recent commands. Reexecuting and Editing Commands You can reexecute any event in the history list. This feature can save you time, effort, and aggravation. Not having to reenter long command lines allows you to reexecute events more easily, quickly, and accurately than you could if you had to retype the entire command line. You can recall, modify, and reexecute previously executed events in three ways: You can use the fc builtin (covered next); the excla- mation point commands (page 316); or the Readline Library, which uses a one-line vi- or emacs-like editor to edit and execute events (page 322). fc: Displays, Edits, and Reexecutes Commands The fc (fix command) builtin enables you to display the history list and to edit and reexecute previous commands. It provides many of the same capabilities as the com- mand line editors. Viewing the History List When you call fc with the –l option, it displays commands from the history list. Without any arguments, fc –l lists the 16 most recent commands in a numbered list, with the oldest appearing first: $ fc -l 1024 cd 1025 view calendar 1026 vim letter.adams01 1027 aspell -c letter.adams01 1028 vim letter.adams01 1029 lpr letter.adams01 1030 cd ../memos 1031 ls 1032 rm *0405 1033 fc -l 1034 cd 1035 whereis aspell 1036 man aspell 1037 cd /usr/share/doc/*aspell* 1038 pwd 1039 ls 1040 ls man-html Which method to use? tip If you are more familiar with vi or emacs and less familiar with the C or TC Shell, use fc or the Readline Library. If you are more familiar with the C or TC Shell and less familiar with vi and emacs, use the exclamation point commands. If it is a toss-up, try the Readline Library; it will benefit you in other areas of Linux more than learning the exclamation point commands will.History 315 The fc builtin can take zero, one, or two arguments with the –l option. The argu- ments specify the part of the history list to be displayed: fc –l [first [last]] The fc builtin lists commands beginning with the most recent event that matches first. The argument can be an event number, the first few characters of the com- mand line, or a negative number, which is taken to be the nth previous command. If you provide last, fc displays commands from the most recent event that matches first through the most recent event that matches last. The next command displays the history list from event 1030 through event 1035: $ fc -l 1030 1035 1030 cd ../memos 1031 ls 1032 rm *0405 1033 fc -l 1034 cd 1035 whereis aspell The following command lists the most recent event that begins with view through the most recent command line that begins with whereis: $ fc -l view whereis 1025 view calendar 1026 vim letter.adams01 1027 aspell -c letter.adams01 1028 vim letter.adams01 1029 lpr letter.adams01 1030 cd ../memos 1031 ls 1032 rm *0405 1033 fc -l 1034 cd 1035 whereis aspell To list a single command from the history list, use the same identifier for the first and second arguments. The following command lists event 1027: $ fc -l 1027 1027 1027 aspell -c letter.adams01 Editing and Reexecuting Previous Commands You can use fc to edit and reexecute previous commands. fc [–e editor] [first [last]] When you call fc with the –e option followed by the name of an editor, fc calls the editor with event(s) in the Work buffer. Without first and last, fc defaults to the most recent command. The next example invokes the vi(m) editor to edit the most recent command: $ fc -e vi316 Chapter 9 The Bourne Again Shell The fc builtin uses the stand-alone vi(m) editor. If you set the FCEDIT variable, you do not need to use the –e option to specify an editor on the command line. Because the value of FCEDIT has been changed to /usr/bin/emacs and fc has no arguments, the following command edits the most recent command with the emacs editor: $ export FCEDIT=/usr/bin/emacs $ fc If you call it with a single argument, fc invokes the editor on the specified command. The following example starts the editor with event 21 in the Work buffer. When you exit from the editor, the shell executes the command: $ fc 21 Again you can identify commands with numbers or by specifying the first few characters of the command name. The following example calls the editor to work on events from the most recent event that begins with the letters vim through event 206: $ fc vim 206 Reexecuting Commands Without Calling the Editor You can reexecute previous commands without going into an editor. If you call fc with the –s option, it skips the editing phase and reexecutes the command. The fol- lowing example reexecutes event 1029: $ fc -s 1029 lpr letter.adams01 The next example reexecutes the previous command: $ fc -s When you reexecute a command you can tell fc to substitute one string for another. The next example substitutes the string john for the string adams in event 1029 and executes the modified event: $ fc -s adams=john 1029 lpr letter.john01 Using an Exclamation Point (!) to Reference Events The C Shell history mechanism uses an exclamation point to reference events and is available under bash. It is frequently more cumbersome to use than fc but nevertheless Clean up the fc buffer caution When you execute an fc command, the shell executes whatever you leave in the editor buffer, possibly with unwanted results. If you decide you do not want to execute a command, delete everything from the buffer before you exit from the editor.History 317 has some useful features. For example, the !! command reexecutes the previous event, and the !$ token represents the last word on the previous command line. You can reference an event by using its absolute event number, its relative event number, or the text it contains. All references to events, called event designators, begin with an exclamation point (!). One or more characters follow the exclama- tion point to specify an event. You can put history events anywhere on a command line. To escape an exclamation point so that it is treated literally instead of as the start of a history event, precede it with a backslash (\) or enclose it within single quotation marks. Event Designators An event designator specifies a command in the history list. See Table 9-8 on page 318 for a list of event designators. !! reexecutes the previous event You can always reexecute the previous event by giving a !! command. In the follow- ing example, event 45 reexecutes event 44: 44 $ ls -l text -rw-rw-r-- 1 alex group 45 Apr 30 14:53 text 45 $ !! ls -l text -rw-rw-r-- 1 alex group 45 Apr 30 14:53 text The !! command works whether or not your prompt displays an event number. As this example shows, when you use the history mechanism to reexecute an event, the shell displays the command it is reexecuting. !n event number A number following an exclamation point refers to an event. If that event is in the history list, the shell executes it. Otherwise, the shell displays an error message. A negative number following an exclamation point references an event relative to the current event. For example, the command !–3 refers to the third preceding event. After you issue a command, the relative event number of a given event changes (event –3 becomes event –4). Both of the following commands reexecute event 44: 51 $ !44 ls -l text -rw-rw-r-- 1 alex group 45 Nov 30 14:53 text 52 $ !-8 ls -l text -rw-rw-r-- 1 alex group 45 Nov 30 14:53 text !string event text When a string of text follows an exclamation point, the shell searches for and executes the most recent event that began with that string. If you enclose the string between question marks, the shell executes the most recent event that con- tained that string. The final question mark is optional if a RETURN would immedi- ately follow it.318 Chapter 9 The Bourne Again Shell 68 $ history 10 59 ls -l text* 60 tail text5 61 cat text1 text5 > letter 62 vim letter 63 cat letter 64 cat memo 65 lpr memo 66 pine jenny 67 ls -l 68 history 69 $ !l ls -l ... 70 $ !lpr lpr memo 71 $ !?letter? cat letter ... optional Word Designators A word designator specifies a word or series of words from an event. Table 9-9 on page 320 lists word designators. The words are numbered starting with 0 (the first word on the line—usually the command), continuing with 1 (the first word following the command), and going through n (the last word on the line). To specify a particular word from a previous event, follow the event designator (such as !14) with a colon and the number of the word in the previous event. For Table 9-8 Event designators Designator Meaning ! Starts a history event unless followed immediately by SPACE, NEWLINE, =, or (. !! The previous command. !n Command number n in the history list. !–n The nth preceding command. !string The most recent command line that started with string. !?string[?] The most recent command that contained string. The last ? is optional. !# The current command (as you have it typed so far). !{event } The event is an event designator. The braces isolate event from the surrounding text. For example, !{–3}3 is the third most recently executed command followed by a 3.History 319 example, !14:3 specifies the third word following the command from event 14. You can specify the first word following the command (word number 1) by using a caret (^) and the last word by using a dollar sign ($). You can specify a range of words by separating two word designators with a hyphen. 72 $ echo apple grape orange pear apple grape orange pear 73 $ echo !72:2 echo grape grape 74 $ echo !72:^ echo apple apple 75 $ !72:0 !72:$ echo pear pear 76 $ echo !72:2-4 echo grape orange pear grape orange pear 77 $ !72:0-$ echo apple grape orange pear apple grape orange pear As the next example shows, !$ refers to the last word of the previous event. You can use this shorthand to edit, for example, a file you just displayed with cat: $ cat report.718 ... $ vim !$ vim report.718 ... If an event contains a single command, the word numbers correspond to the argu- ment numbers. If an event contains more than one command, this correspondence does not hold true for commands after the first. In the following example event 78 contains two commands separated by a semicolon so that the shell executes them sequentially; the semicolon is word number 5. 78 $ !72 ; echo helen jenny barbara echo apple grape orange pear ; echo helen jenny barbara apple grape orange pear helen jenny barbara 79 $ echo !78:7 echo helen helen 80 $ echo !78:4-7 echo pear ; echo helen pear helen320 Chapter 9 The Bourne Again Shell Modifiers On occasion you may want to change an aspect of an event you are reexecuting. Perhaps you entered a complex command line with a typo or incorrect pathname or you want to specify a different argument. You can modify an event or a word of an event by putting one or more modifiers after the word designator, or after the event designator if there is no word designator. Each modifier must be preceded by a colon (:). Substitute modifier The substitute modifier is more complex than the other modifiers. The following example shows the substitute modifier correcting a typo in the previous event: $ car /home/jenny/memo.0507 /home/alex/letter.0507 bash: car: command not found $ !!:s/car/cat cat /home/jenny/memo.0507 /home/alex/letter.0507 ... The substitute modifier has the following syntax: [g]s/old/new/ where old is the original string (not a regular expression), and new is the string that replaces old. The substitute modifier substitutes the first occurrence of old with new. Placing a g before the s (as in gs/old/new/) causes a global substitution, replac- ing all occurrences of old. The / is the delimiter in the examples but you can use any character that is not in either old or new. The final delimiter is optional if a RETURN would immediately follow it. As with the vim Substitute command, the history mechanism replaces an ampersand (&) in new with old. The shell replaces a null old string (s//new/) with the previous old string or string within a command that you searched for with ?string?. Table 9-9 Word designators Designator Meaning n The nth word. Word 0 is normally the command name. ^ The first word (after the command name). $ The last word. m–n All words from word number m through word number n; m defaults to 0 if you omit it (0–n). n* All words from word number n through the last word. * All words except the command name. The same as 1*. % The word matched by the most recent ?string ? search.History 321 Quick substitution An abbreviated form of the substitute modifier is quick substitution. Use it to reexe- cute the most recent event while changing some of the event text. The quick substi- tution character is the caret (^). For example, the command $ ^old^new^ produces the same results as $ !!:s/old/new/ Thus substituting cat for car in the previous event could have been entered as $ ^car^cat cat /home/jenny/memo.0507 /home/alex/letter.0507 ... You can omit the final caret if it would be followed immediately by a RETURN. As with other command line substitutions, the shell displays the command line as it appears after the substitution. Other modifiers Modifiers (other than the substitute modifier) perform simple edits on the part of the event that has been selected by the event designator and the optional word des- ignators. You can use multiple modifiers, each preceded by a colon (:). The following series of commands uses ls to list the name of a file, repeats the com- mand without executing it (p modifier), and repeats the last command, removing the last part of the pathname (h modifier) again without executing it: $ ls /etc/sysconfig/harddisks /etc/sysconfig/harddisks $ !!:p ls /etc/sysconfig/harddisks $ !!:h:p ls /etc/sysconfig $ Table 9-10 lists event modifiers other than the substitute modifier. Table 9-10 Modifiers Modifier Function e (extension) Removes all but the filename extension h (head) Removes the last part of a pathname p (print-not) Displays the command, but does not execute it q (quote) Quotes the substitution to prevent further substitutions on it r (root) Removes the filename extension t (tail) Removes all elements of a pathname except the last x Like q but quotes each word in the substitution individually322 Chapter 9 The Bourne Again Shell The Readline Library Command line editing under the Bourne Again Shell is implemented through the Readline Library, which is available to any application written in C. Any applica- tion that uses the Readline Library supports line editing that is consistent with that provided by bash. Programs that use the Readline Library, including bash, read ~/.inputrc (page 325) for key binding information and configuration settings. The ––noediting command line option turns off command line editing in bash. vi mode You can choose one of two editing modes when using the Readline Library in bash: emacs or vi(m). Both modes provide many of the commands available in the stand- alone versions of the vi(m) and emacs editors. You can also use the ARROW keys to move around. Up and down movements move you backward and forward through the history list. In addition, Readline provides several types of interactive word completion (page 324). The default mode is emacs; you can switch to vi mode with the following command: $ set -o vi emacs mode The next command switches back to emacs mode: $ set -o emacs vi Editing Mode Before you start make sure you are in vi mode. When you enter bash commands while in vi editing mode, you are in Input mode (page 174). As you enter a command, if you discover an error before you press RETURN, you can press ESCAPE to switch to vi Command mode. This setup is different from the stand-alone vi(m) editor’s initial mode. While in Command mode you can use many vi(m) commands to edit the command line. It is as though you were using vi(m) to edit a copy of the history file with a screen that has room for only one com- mand. When you use the k command or the UP ARROW to move up a line, you access the previous command. If you then use the j command or the DOWN ARROW to move down a line, you will return to the original command. To use the k and j keys to move between commands you must be in Command mode; you can use the ARROW keys in both Command and Input modes. In addition to cursor-positioning commands, you can use the search-backward (?) command followed by a search string to look back through your history list for the most recent command containing that string. If you have moved back in your history list, use a forward slash (/) to search forward toward your most recent command. Unlike the search strings in the stand-alone vi(m) editor, these search strings cannot The stand-alone editor starts in Command mode tip The stand-alone vim editor starts in Command mode, whereas the command line vi(m) editor starts in Input mode. If commands display characters and do not work properly, you are in Input mode. Press ESCAPE and enter the command again.History 323 contain regular expressions. You can, however, start the search string with a caret (^) to force the shell to locate commands that start with the search string. As in vi(m), pressing n after a successful search looks for the next occurrence of the same string. You can also access events in the history list by using event numbers. While you are in Command mode (press ESCAPE), enter the event number followed by a G to go to the command with that event number. When you use /, ?, or G to move to a command line, you are in Command mode, not Input mode. Now you can edit the command as you like or press RETURN to execute it. Once the command you want to edit is displayed, you can modify the command line using vi(m) Command mode editing commands such as x (delete character), r (replace character), ~ (change case), and . (repeat last change). To change to Input mode, use an Insert (i, I), Append (a, A), Replace (R), or Change (c, C) command. You do not have to return to Command mode to run a command; simply press RETURN, even if the cursor is in the middle of the command line. emacs Editing Mode Unlike the vi(m) editor, emacs is modeless. You need not switch between Command mode and Input mode because most emacs commands are control characters, allowing emacs to distinguish between input and commands. Like vi(m), the emacs command line editor provides commands for moving the cursor on the command line and through the command history list and for modifying part or all of a com- mand. The emacs command line editor commands differ in a few cases from the commands in the stand-alone emacs editor. In emacs you perform cursor movement by using both CONTROL and ESCAPE commands. To move the cursor one character backward on the command line, press CONTROL-B. Press CONTROL-F to move one character forward. As in vi, you may precede these move- ments with counts. To use a count you must first press ESCAPE; otherwise, the num- bers you type will appear on the command line. Like vi(m), emacs provides word and line movement commands. To move backward or forward one word on the command line, press ESCAPE b or ESCAPE f. To move several words by using a count, press ESCAPE followed by the number and the appropriate escape sequence. To get to the beginning of the line, press CONTROL-A; to the end of the line, press CONTROL-E; and to the next instance of the character c, press CONTROL-X CONTROL-F followed by c. You can add text to the command line by moving the cursor to the correct place and typing the desired text. To delete text, move the cursor just to the right of the char- acters that you want to delete and press the erase key (page 137) once for each char- acter you want to delete. CONTROL-D can terminate your screen session tip If you want to delete the character directly under the cursor, press CONTROL-D. If you enter CONTROL-D at the beginning of the line, it may terminate your shell session.324 Chapter 9 The Bourne Again Shell If you want to delete the entire command line, type the line kill character (page 138). You can type this character while the cursor is anywhere in the com- mand line. If you want to delete from the cursor to the end of the line, use CONTROL-K. Readline Completion Commands You can use the TAB key to complete words you are entering on the command line. This facility, called completion, works in both vi and emacs editing modes. Several types of completion are possible, and which one you use depends on which part of a command line you are typing when you press TAB. Command Completion If you are typing the name of a command (the first word on the command line), pressing TAB results in command completion. That is, bash looks for a command whose name starts with the part of the word you have typed. If no command starts with what you have entered, bash beeps. If there is one such command, bash com- pletes the command name for you. If there is more than one choice, bash does noth- ing in vi mode and beeps in emacs mode. Pressing TAB a second time causes bash to display a list of commands whose names start with the prefix you typed and allows you to finish typing the command name. In the following example, the user types bz and presses TAB. The shell beeps (the user is in emacs mode) to indicate that several commands start with the letters bz. The user enters another TAB to cause the shell to display a list of commands that start with bz followed by the command line as the user had entered it so far: $ bz → TAB (beep) → TAB bzcat bzdiff bzip2 bzless bzcmp bzgrep bzip2recover bzmore $ bz■ Next the user types c and presses TAB twice. The shell displays the two commands that start with bzc. The user types a followed by TAB and the shell then completes the command because only one command starts with bzca. $ bzc → TAB (beep) → TAB bzcat bzcmp $ bzca → TAB → t ■ Pathname Completion Pathname completion, which also uses TABs, allows you to type a portion of a path- name and have bash supply the rest. If the portion of the pathname that you have typed is sufficient to determine a unique pathname, bash displays that pathname. If more than one pathname would match it, bash completes the pathname up to the point where there are choices so that you can type more. When you are entering a pathname, including a simple filename, and press TAB, the shell beeps (if the shell is in emacs mode—in vi mode there is no beep). It then extends the command line as far as it can.History 325 $ cat films/dar → TAB (beep) cat films/dark_■ In the films directory every file that starts with dar has k_ as the next characters, so bash cannot extend the line further without making a choice among files. You are left with the cursor just past the _ character. At this point you can continue typing the pathname or press TAB twice. In the latter case bash beeps, displays your choices, redisplays the command line, and again leaves the cursor just after the _ character. $ cat films/dark_ → TAB (beep) → TAB dark_passage dark_victory $ cat films/dark_■ When you add enough information to distinguish between the two possible files and press TAB, bash displays the unique pathname. If you enter p followed by TAB after the _ character, the shell completes the command line: $ cat films/dark_p → TAB → assage Because there is no further ambiguity, the shell appends a SPACE so you can finish typ- ing the command line or just press RETURN to execute the command. If the complete pathname is that of a directory, bash appends a slash (/) in place of a SPACE. Variable Completion When typing a variable name, pressing TAB results in variable completion, where bash tries to complete the name of the variable. In case of an ambiguity, pressing TAB twice displays a list of choices: $ echo $HO → TAB → TAB $HOME $HOSTNAME $HOSTTYPE $ echo $HOM → TAB → E .inputrc: Configuring Readline The Bourne Again Shell and other programs that use the Readline Library read the file specified by the INPUTRC environment variable to obtain initialization infor- mation. If INPUTRC is not set, these programs read the ~/.inputrc file. They ignore lines of .inputrc that are blank or that start with a pound sign (#). Variables You can set variables in .inputrc to control the behavior of the Readline Library using the following syntax: set variable value Table 9-11 lists some variables and values you can use. See Readline Variables in the bash man or info page for a complete list. Pressing RETURN executes the command caution Pressing RETURN causes the shell to execute the command regardless of where the cursor is on the command line.326 Chapter 9 The Bourne Again Shell Key Bindings You can specify bindings that map keystroke sequences to Readline commands, allowing you to change or extend the default bindings. As in emacs, the Readline Library includes many commands that are not bound to a keystroke sequence. To use an unbound command, you must map it using one of the following forms: keyname: command_name "keystroke_sequence": command_name In the first form, you spell out the name for a single key. For example, CONTROL-U would be written as control-u. This form is useful for binding commands to single keys. In the second form, you specify a string that describes a sequence of keys that will be bound to the command. You can use the emacs-style backslash escape sequences to represent the special keys CONTROL (\C), META (\M), and ESCAPE (\e). Specify a back- slash by escaping it with another backslash: \\. Similarly, a double or single quota- tion mark can be escaped with a backslash: \" or \'. The kill-whole-line command, available in emacs mode only, deletes the current line. Put the following command in .inputrc to bind the kill-whole-line command (which is unbound by default) to the keystroke sequence CONTROL-R. control-r: kill-whole-line bind Give the command bind –P to display a list of all Readline commands. If a com- mand is bound to a key sequence, that sequence is shown. Commands you can use in vi mode start with vi. For example, vi-next-word and vi-prev-word move the cur- sor to the beginning of the next and previous words, respectively. Commands that do not begin with vi are generally available in emacs mode. Use bind –q to determine which key sequence is bound to a command: Table 9-11 Readline variables Variable Effect editing-mode Set to vi to start Readline in vi mode. Set to emacs to start Readline in emacs mode (the default). Similar to the set –o vi and set –o emacs shell commands (page 322). horizontal-scroll-mode Set to on to cause long lines to extend off the right edge of the display area. Moving the cursor to the right when it is at the right edge of the display area shifts the line to the left so you can see more of the line. You can shift the line back by moving the cursor back past the left edge. The default value is off, which causes long lines to wrap onto multiple lines of the display. mark-directories Set to off to cause Readline not to place a slash (/) at the end of directory names it completes. Normally it is on. mark-modified-lines Set to on to cause Readline to precede modified history lines with an asterisk. The default value is off.History 327 $ bind -q kill-whole-line kill-whole-line can be invoked via "\C-r". You can also bind text by enclosing it within double quotation marks (emacs mode only): "QQ": "The Linux Operating System" This command causes bash to insert the string The Linux Operating System when you type QQ. Conditional Constructs You can conditionally select parts of the .inputrc file using the $if directive. The syntax of the conditional construct is $if test[=value] commands [$else commands] $endif where test is mode, term, or bash. If test equals value or if test is true, this structure executes the first set of commands. If test does not equal value or if test is false, it executes the second set of commands if they are present or exits from the structure if they are not present. The power of the $if directive lies in the three types of tests it can perform. 1. You can test to see which mode is currently set. $if mode=vi The preceding test is true if the current Readline mode is vi and false other- wise. You can test for vi or emacs. 2. You can test the type of terminal. $if term=xterm The preceding test is true if the TERM variable is set to xterm. You can test for any value of TERM. 3. You can test the application name. $if bash The preceding test is true when you are running bash and not another pro- gram that uses the Readline Library. You can test for any application name. These tests can customize the Readline Library based on the current mode, the type of terminal, and the application you are using. They give you a great deal of power and flexibility when using the Readline Library with bash and other programs.328 Chapter 9 The Bourne Again Shell The following commands in .inputrc cause CONTROL-Y to move the cursor to the begin- ning of the next word regardless of whether bash is in vi or emacs mode: $ cat ~/.inputrc set editing-mode vi $if mode=vi "\C-y": vi-next-word $else "\C-y": forward-word $endif Because bash reads the preceding conditional construct when it is started, you must set the editing mode in .inputrc. Changing modes interactively using set will not change the binding of CONTROL-Y. For more information on the Readline Library, open the bash man page and give the command /^READLINE, which searches for the word READLINE at the beginning of a line. Aliases An alias is a (usually short) name that the shell translates into another (usually longer) name or (complex) command. Aliases allow you to define new commands by substituting a string for the first token of a simple command. They are typically placed in the ~/.bashrc startup files so that they are available to interactive sub- shells. The syntax of the alias builtin is alias [name[=value]] No SPACEs are permitted around the equal sign. If value contains SPACEs or TABs, you must enclose value between quotation marks. An alias does not accept an argument from the command line in value. Use a function (page 331) when you need to use an argument. An alias does not replace itself, which avoids the possibility of infinite recursion in handling an alias such as the following: $ alias ls='ls -F' You can nest aliases. Aliases are disabled for noninteractive shells (that is, shell scripts). To see a list of the current aliases, give the command alias. To view the alias for a particular name, use alias followed by the name and nothing else. You can use the unalias builtin to remove an alias. If Readline commands do not work, log out and log in again tip The Bourne Again Shell reads ~/.inputrc when you log in. After you make changes to this file, you must log out and log in again before the changes take effect.Aliases 329 When you give an alias builtin command without any arguments, the shell displays a list of all defined aliases: $ alias alias ll='ls -l' alias l='ls -ltr' alias ls='ls -F' alias zap='rm -i' Ubuntu Linux defines some aliases. Give an alias command to see which aliases are in effect. You can delete the aliases you do not want from the appropriate startup file. Single Versus Double Quotation Marks in Aliases The choice of single or double quotation marks is significant in the alias syntax when the alias includes variables. If you enclose value within double quotation marks, any variables that appear in value are expanded when the alias is created. If you enclose value within single quotation marks, variables are not expanded until the alias is used. The following example illustrates the difference. The PWD keyword variable holds the pathname of the working directory. Alex cre- ates two aliases while he is working in his home directory. Because he uses double quotation marks when he creates the dirA alias, the shell substitutes the value of the working directory when he creates this alias. The alias dirA command displays the dirA alias and shows that the substitution has already taken place: $ echo $PWD /home/alex $ alias dirA="echo Working directory is $PWD" $ alias dirA alias dirA='echo Working directory is /home/alex' When Alex creates the dirB alias, he uses single quotation marks, which prevent the shell from expanding the $PWD variable. The alias dirB command shows that the dirB alias still holds the unexpanded $PWD variable: $ alias dirB='echo Working directory is $PWD' $ alias dirB alias dirB='echo Working directory is $PWD' After creating the dirA and dirB aliases, Alex uses cd to make cars his working directory and gives each of the aliases as commands. The alias that he created with double quotation marks displays the name of the directory that he created the alias in as the working directory (which is wrong) and the dirB alias displays the proper name of the working directory: $ cd cars $ dirA Working directory is /home/alex $ dirB Working directory is /home/alex/cars330 Chapter 9 The Bourne Again Shell Examples of Aliases The following alias allows you to type r to repeat the previous command or r abc to repeat the last command line that began with abc: $ alias r='fc -s' If you use the command ls –ltr frequently, you can create an alias that substitutes ls –ltr when you give the command l: $ alias l='ls -ltr' $ l total 41 -rw-r--r-- 1 alex group 30015 Mar 1 2007 flute.ps -rw-r----- 1 alex group 3089 Feb 11 2008 XTerm.ad -rw-r--r-- 1 alex group 641 Apr 1 2008 fixtax.icn -rw-r--r-- 1 alex group 484 Apr 9 2008 maptax.icn drwxrwxr-x 2 alex group 1024 Aug 9 17:41 Tiger drwxrwxr-x 2 alex group 1024 Sep 10 11:32 testdir -rwxr-xr-x 1 alex group 485 Oct 21 08:03 floor drwxrwxr-x 2 alex group 1024 Oct 27 20:19 Test_Emacs Another common use of aliases is to protect yourself from mistakes. The following exam- ple substitutes the interactive version of the rm utility when you give the command zap: $ alias zap='rm -i' $ zap f* rm: remove 'fixtax.icn'? n rm: remove 'flute.ps'? n rm: remove 'floor'? n The –i option causes rm to ask you to verify each file that would be deleted, to help you avoid accidentally deleting the wrong file. You can also alias rm with the rm –i command: alias rm='rm –i'. The aliases in the next example cause the shell to substitute ls –l each time you give an ll command and ls –F when you use ls: $ alias ls='ls -F' $ alias ll='ls -l' $ ll total 41 drwxrwxr-x 2 alex group 1024 Oct 27 20:19 Test_Emacs/ drwxrwxr-x 2 alex group 1024 Aug 9 17:41 Tiger/ -rw-r----- 1 alex group 3089 Feb 11 2008 XTerm.ad -rw-r--r-- 1 alex group 641 Apr 1 2008 fixtax.icn -rw-r--r-- 1 alex group 30015 Mar 1 2007 flute.ps -rwxr-xr-x 1 alex group 485 Oct 21 08:03 floor* -rw-r--r-- 1 alex group 484 Apr 9 2008 maptax.icn drwxrwxr-x 2 alex group 1024 Sep 10 11:32 testdir/ How to prevent the shell from invoking an alias tip The shell checks only simple, unquoted commands to see if they are aliases. Commands given as relative or absolute pathnames and quoted commands are not checked. When you want to give a command that has an alias but do not want to use the alias, precede the command with a back- slash, specify the command’s absolute pathname, or give the command as ./command.Functions 331 The –F option causes ls to print a slash (/) at the end of directory names and an asterisk (*) at the end of the names of executable files. In this example, the string that replaces the alias ll (ls –l) itself contains an alias (ls). When it replaces an alias with its value, the shell looks at the first word of the replacement string to see whether it is an alias. In the preceding example, the replacement string contains the alias ls, so a second substitution occurs to produce the final command ls –F –l. (To avoid a recursive plunge, the ls in the replacement text, although an alias, is not expanded a second time.) When given a list of aliases without the =value or value field, the alias builtin responds by displaying the value of each defined alias. The alias builtin reports an error if an alias has not been defined: $ alias ll l ls zap wx alias ll='ls -l' alias l='ls -ltr' alias ls='ls -F' alias zap='rm -i' bash: alias: wx: not found You can avoid alias substitution by preceding the aliased command with a backslash (\): $ \ls Test_Emacs XTerm.ad flute.ps maptax.icn Tiger fixtax.icn floor testdir Because the replacement of an alias name with the alias value does not change the rest of the command line, any arguments are still received by the command that gets executed: $ ll f* -rw-r--r-- 1 alex group 641 Apr 1 2008 fixtax.icn -rw-r--r-- 1 alex group 30015 Mar 1 2007 flute.ps -rwxr-xr-x 1 alex group 485 Oct 21 08:03 floor* You can remove an alias with the unalias builtin. When the zap alias is removed, it is no longer displayed with the alias builtin and its subsequent use results in an error message: $ unalias zap $ alias alias ll='ls -l' alias l='ls -ltr' alias ls='ls -F' $ zap maptax.icn bash: zap: command not found Functions A shell function is similar to a shell script in that it stores a series of commands for execution at a later time. However, because the shell stores a function in the com- puter’s main memory (RAM) instead of in a file on the disk, the shell can access it more quickly than the shell can access a script. The shell also preprocesses (parses) a function so that it starts up more quickly than a script. Finally the shell executes a332 Chapter 9 The Bourne Again Shell shell function in the same shell that called it. If you define too many functions, the overhead of starting a subshell (as when you run a script) can become unacceptable. You can declare a shell function in the ~/.bash_profile startup file, in the script that uses it, or directly from the command line. You can remove functions with the unset builtin. The shell does not keep functions once you log out. The syntax that declares a shell function is [function] function-name () { commands } where the word function is optional, function-name is the name you use to call the function, and commands comprise the list of commands the function executes when you call it. The commands can be anything you would include in a shell script, including calls to other functions. The first brace ({) can appear on the same line as the function name. Aliases and variables are expanded when a function is read, not when it is executed. You can use the break statement (page 418) within a function to terminate its execution. Shell functions are useful as a shorthand as well as to define special commands. The following function starts a process named process in the background, with the out- put normally displayed by process being saved in .process.out: start_process() { process > .process.out 2>&1 & } The next example shows how to create a simple function that displays the date, a header, and a list of the people who are using the system. This function runs the same commands as the whoson script described on page 283. In this example the function is being entered from the keyboard. The greater-than (>) signs are second- ary shell prompts (PS2); do not enter them. $ function whoson () > { > date > echo "Users Currently Logged On" > who > } $ whoson Sun Aug 5 15:44:58 PDT 2007 Users Currently Logged On hls console Aug 4 08:59 (:0) alex pts/4 Aug 4 09:33 (0.0) jenny pts/7 Aug 4 09:23 (bravo.example.com) Removing variables and functions tip If you have a shell variable and a function with the same name, using unset removes the shell variable. If you then use unset again with the same name, it removes the function.Functions 333 Functions in startup files If you want to have the whoson function always 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 438). The following exam- ple shows the arg1 function entered from the keyboard. $ arg1 ( ) { > echo "$1" > } $ arg1 first_arg first_arg See the function switch () on page 279 for another example of a function. “Func- tions” on page 435 discusses the use of local and global variables within a function. 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 439) takes on the value of the number of command line arguments. This function uses the eval builtin to force bash to scan the com- mand $1=$2 twice. Because $1=$2 begins with a dollar sign ($), the shell treats the334 Chapter 9 The Bourne Again Shell 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. Using 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 Two kinds of command line options are available: short and long. 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. 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 features on and off. The set Table 9-12 Command line options Option Explanation Syntax Help Displays a usage message. ––help No edit Prevents users from using the Readline Library (page 322) to edit command lines in an interactive shell. ––noediting No profile Prevents reading these startup files (page 277): /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile. ––noprofile No rc Prevents reading the ~/.bashrc startup file (page 277). This option is on by default if the shell is called as sh. ––norc POSIX Runs bash in POSIX mode. ––posix Version Displays bash version information and exits. ––version Login Causes bash to run as though it were a login shell. –l (lowercase “l”) shopt Runs a shell with the opt shopt option (page 335). A –O (uppercase “O”) sets the option; +O unsets it. [±]O [opt] End of options On the command line, signals the end of options. Subsequent tokens are treated as arguments even if they begin with a hyphen (–). ––Controlling bash Features and Options 335 builtin controls one group of features, while the shopt builtin controls another group. You can also control many features from the command line you use to call bash. set ±o: Turns Shell Features On and Off The set builtin, when used with the –o or +o option, enables, disables, and lists cer- tain bash features. For example, the following command turns on the noclobber fea- ture (page 231): $ 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 fol- lowed by its state (on or off). The command set +o without an option lists the same features in a form that you can use as input to the shell. Table 9-13 lists bash features. 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 ref- erences (the –s stands for set): $ shopt -s dotglob You can turn this feature off (the default) by giving the 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 The command shopt without any options or arguments lists the features controlled by shopt and their state. The command shopt –s without an argument lists the fea- tures controlled by shopt that are set or on. The command shopt –u lists the features that are unset or off. Table 9-13 lists bash features. 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.” 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 noclobber336 Chapter 9 The Bourne Again Shell Table 9-13 bash features Feature Description Syntax Alternate syntax allexport Automatically exports all variables and functions that you create or modify after giving this command. set –o allexport set –a braceexpand Causes bash to perform brace expansion (the default; page 340). set –o braceexpand set –B cdspell Corrects minor spelling errors in directory names used as arguments to cd. shopt –s cdspell cmdhist Saves all lines of a multiline command in the same history entry, adding semicolons as needed. shopt –s cmdhist dotglob Causes shell special characters (wildcards; page 239) in an ambiguous file reference to match a leading period in a filename. By default special characters do not to match a leading period. You must always specify the filenames . and .. explicitly because no pattern ever matches them. shopt –s dotglob emacs Specifies emacs editing mode for com- mand line editing (the default; page 323). set –o emacs errexit Causes bash to exit when a simple com- mand (not a control structure) fails. set –o errexit set –e execfail 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. shopt –s execfail expand_aliases Causes aliases (page 328) to be expanded (by default it is on for interactive shells and off for noninteractive shells). shopt –s expand_alias hashall Causes bash to remember where com- mands it has found using PATH (page 302) are located (default). set –o hashall set –h histappend Causes bash to append the history list to the file named by HISTFILE (page 312) when the shell exits. By default bash over- writes this file. shopt –s histappend histexpand Causes the history mechanism (which uses exclamation points; page 316) to work (default). Turn this feature off to turn off history expansion. set –o histexpand set –HControlling bash Features and Options 337 history Enable command history (on by default; page 312). set –o history ignoreeof Specifies that bash must receive ten EOF characters before it exits. Useful on noisy dial-up lines. set –o ignoreeof monitor Enables job control (on by default, page 290). set –o monitor set –m nocaseglob Causes ambiguous file references (page 239) to match filenames without regard to case (off by default). shopt –s nocaseglob noclobber Helps prevent overwriting files (off by default; page 231). set –o noclobber set –C noglob Disables pathname expansion (off by default; page 239). set –o noglob set –f notify With job control (page 290) enabled, reports the termination status of back- ground jobs immediately. The default behavior is to display the status just before the next prompt. set –o notify set –b nounset Displays an error and exits from a shell script when you use an unset variable in an interactive shell. The default is to display a null value for an unset variable. set –o nounset set –u nullglob Causes bash to expand ambiguous file references (page 239) that do not match a filename to a null string. By default bash passes these file references without expanding them. shopt –s nullglob posix Runs bash in POSIX mode. set –o posix verbose Displays command lines as bash reads them. set –o verbose set –v vi Specifies vi editing mode for command line editing (page 322). set –o vi xpg_echo Causes the echo builtin to expand back- slash escape sequences without the need for the –e option (page 422). shopt –s xpg_echo xtrace Turns on shell debugging (page 408). set –o xtrace set –x Table 9-13 bash features (continued)338 Chapter 9 The Bourne Again Shell 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 you with the secondary prompt (PS2, > by default; page 305) as you type each line of a multiline command until it recognizes the end of the command: $ echo 'hi > end' hi end $ function hello () { > echo hello there > } $ After reading a command line, bash applies history expansion and alias substitution to the line. History Expansion “Reexecuting and Editing Commands” on page 314 discusses the commands you can give to modify and reexecute command lines from the history list. History expansion is the process that bash uses to turn a history command into an execut- able command line. For example, when you give 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 328) 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. Give 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 charac- ters 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 .Processing the Command Line 339 After separating tokens and before executing the command, the shell scans the tokens and performs command line expansion. Command Line Expansion In both interactive and noninteractive use, the shell transforms the command line using command line 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 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 vari- able "$@"—page 440). 1. Brace expansion (page 340) 2. Tilde expansion (page 341) 3. Parameter and variable expansion (page 342) 4. Arithmetic expansion (page 342) 5. Command substitution (page 344) 6. Word splitting (page 345) 7. Pathname expansion (page 345) 8. Process substitution (page 347) 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 com- mands. 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 redi- rection before it evaluates variables. When it executes the command line, the shell checks for redirection and, finding none, evaluates the SENDIT variable. After340 Chapter 9 The Bourne Again Shell 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 expan- sion. 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 expan- sion is almost always used to specify filenames, the mechanism can be used to gen- erate 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 298). 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 that the shell generates with brace expansion. In this case the strings do not match filenames (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 com- mand 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 follow- ing example copies the 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/Processing the Command Line 341 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 direc- tories, 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. Because it found no filenames matching vrs[A–E], bash passed the ambiguous file reference to mkdir, which created a directory with that name. Page 241 has a discussion of brackets in ambiguous file references. Tilde Expansion Chapter 6 showed 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 com- mand 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 Alex’s home directory to the working directory: $ echo $HOME /home/alex $ echo ~ /home/alex $ echo ~/letter /home/alex/letter $ cp ~/letter . If the string of characters following the tilde forms a valid username, the shell sub- stitutes the path of the home directory associated with that username for the tilde and name. If it is not null and not a valid username, the shell does not make any substitution: $ echo ~jenny /home/jenny $ echo ~root /root $ echo ~xx ~xx342 Chapter 9 The Bourne Again Shell Tildes are also used in directory stack manipulation (page 292). 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 command line, or positional, parameters (page 438) and special parameters (page 436). Variables include user-created variables (page 296) and keyword variables (page 301). The bash man and info pages do not make this distinction, however. Parameters and variables are not expanded if they are enclosed within single quotation marks or if the leading dollar sign is escaped (preceded with a backslash). If they are enclosed within double quotation marks, the shell expands parameters and variables. 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 11-8 on page 461). Arithmetic in bash is done using integers. Unless you use variables of type integer (page 301) or actual integers, however, the shell must convert string- valued 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, 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:Processing the Command Line 343 $ 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 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 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 The dollar sign and single parenthesis instruct the shell to perform command substi- tution; 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. Arith- metic expansion then divides this number by 66, the number of lines on a page. A 1 is added at the end because the integer division results in any remainder being discarded. 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" Fewer dollar signs ($) tip When you use variables within $(( and )), the dollar signs that precede individual variable refer- ences are optional: $ x=23 y=37 $ echo $((2*$x + 3*$y)) 157 $ echo $((2*x + 3*y)) 157344 Chapter 9 The Bourne Again Shell 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, the exit sta- tus is 0. You can give multiple arguments to let 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 you to begin the variable name with a dollar sign ($). Never- theless, it is a good practice to do so, as in most places you must include this symbol. 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 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/alex The next script assigns the output of the pwd builtin to the variable where and dis- plays 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/jenny 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/jenny directory.Processing the Command Line 345 The following command uses find to locate files with the name README in the directory tree with its root at the working directory. This list of files is standard out- put 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 ) Try giving this command after giving a set –x command (page 408) to see how bash expands it. If there is no README file, you just get the output of ls –l. For additional scripts that use command substitution, see pages 404, 423, and 453. Word Splitting The results of parameter and variable expansion, command substitution, and arith- metic expansion are candidates for word splitting. Using each character of IFS (page 305) 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 239), also called filename generation or globbing, is the process of interpreting ambiguous file references and substituting the appropriate list of filenames. Unless noglob (page 337) 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 spec- ified pattern, the token with the ambiguous file reference is left alone. The shell does not delete the token or replace it with a null string but rather passes it to the pro- gram as is (except see nullglob on page 337). In the first echo command in the following example, the shell expands the ambigu- ous 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 $(( Versus $( tip The symbols $(( constitute a separate token. They introduce an arithmetic expression, not a com- mand substitution. Thus, if you want to use a parenthesized subshell (page 289) within $(), you must insert a SPACE between the $( and the next (.346 Chapter 9 The Bourne Again Shell removes the three tmp* files, the shell finds no filenames that match tmp* when it tries to expand it. Thus it 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 336). The option nocaseglob (page 337) causes ambiguous file references to match filenames without regard to case. Quotation marks Putting double quotation marks around an argument causes the shell to suppress pathname and all other expansion except parameter and variable expansion. Put- ting single quotation marks around an argument suppresses all types of expansion. The second echo command in the following example shows the variable $alex 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* $alex tmp1 tmp2 tmp3 sonar $ echo "tmp* $alex" tmp* sonar $ echo 'tmp* $alex' tmp* $alex The shell distinguishes between the value of a variable and a reference to the vari- able 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*. 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. When you use 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 performedChapter Summary 347 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. $ ls letter* letter1 letter2 letter3 $ var=letter* $ set | grep var var='letter*' $ echo '$var' $var $ echo "$var" letter* $ echo $var letter1 letter2 letter3 Process Substitution A special feature of the Bourne Again Shell is the ability to replace filename argu- ments with processes. An argument with the syntax <(command) causes command to be executed and the output written to a named pipe (FIFO). The shell replaces that argument with the name of the pipe. If that argument is then used as the name of an input file during processing, the output of command is read. Similarly an argument with the syntax >(command) is replaced by the name of a pipe that com- mand reads as standard input. The following example uses sort (page 153) with the –m (merge, which works cor- rectly 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 <(grep "[^A-Z]..$" memo1 | sort) <(grep ".*aba.*" memo2 |sort) Chapter Summary The shell is both a command interpreter and a programming language. As a com- mand interpreter, the shell executes commands you enter in response to its prompt. As a programming language, the shell executes commands from files called shell scripts. When you start a shell, it typically runs one or more startup files. Running a shell script Assuming that the file holding a shell script is in the working directory, there are three basic ways to execute the shell script from the command line. 1. Type the simple filename of the file that holds the script. 2. Type a relative pathname, including the simple filename preceded by ./. 3. Type bash followed by the name of the file.348 Chapter 9 The Bourne Again Shell Technique 1 requires that the working directory be in the PATH variable. Tech- niques 1 and 2 require that you have execute and read permission for the file hold- ing the script. Technique 3 requires that you have read permission for the file holding the script. Job control A job is one or more commands connected by pipes. You can bring a job running in the background into the foreground by using the fg builtin. You can put a fore- ground job into the background by using the bg builtin, provided that you first sus- pend the job by pressing the suspend key (typically CONTROL-Z). Use the jobs builtin to see which jobs are running or suspended. Variables The shell allows you to define variables. You can declare and initialize a variable by assigning a value to it; you can remove a variable declaration by using unset. Vari- ables are local to a process unless they are exported using the export builtin to make them available to child processes. Variables you declare are called user-created vari- ables. The shell also defines called keyword variables. Within a shell script you can work with the command line (positional) parameters the script was called with. Process Each process has a unique identification (PID) number and is the execution of a single Linux command. When you give it a command, the shell forks a new (child) process to execute the command, unless the command is built into the shell (page 243). While the child process is running, the shell is in a state called sleep. By ending a command line with an ampersand (&), you can run a child process in the background and bypass the sleep state so that the shell prompt returns immediately after you press RETURN. Each command in a shell script forks a separate process, each of which may in turn fork other processes. When a process terminates, it returns its exit status to its parent process. An exit status of zero signifies success and nonzero signifies failure. History The history mechanism, a feature adapted from the C Shell, maintains a list of recently issued command lines, also called events, that provides a way to reexecute previous commands quickly. There are several ways to work with the history list; one of the easiest is to use a command line editor. Command line editors When using an interactive Bourne Again Shell, you can edit your command line and commands from the history file, using either of the Bourne Again Shell’s command line editors (vi[m] or emacs). When you use the vi(m) command line editor, you start in Input mode, unlike the way you normally enter vi(m). You can switch between Command and Input modes. The emacs editor is modeless and distinguishes com- mands from editor input by recognizing control characters as commands. Aliases An alias is a name that the shell translates into another name or (complex) com- mand. Aliases allow you to define new commands by substituting a string for the first token of a simple command. Functions A shell function is a series of commands that, unlike a shell script, are parsed prior to being stored in memory so that they run faster than shell scripts. Shell scripts are parsed at runtime and are stored on disk. A function can be defined on the com- mand line or within a shell script. If you want the function definition to remain in effect across login sessions, you can define it in a startup file. Like the functions of a programming language, a shell function is called by giving its name followed by any arguments.Exercises 349 Shell features There are several ways to customize the shell’s behavior. You can use options on the command line when you call bash and you can use the bash set and shopt builtins to turn features on and off. Command line expansion When it processes a command line, the Bourne Again Shell may replace some words with expanded text. Most types of command line expansion are invoked by the appearance of a special character within a word (for example, a leading dollar sign denotes a variable). See Table 9-6 on page 309 for a list of special characters. The expansions take place in a specific order. Following the history and alias expan- sions, the common expansions are parameter and variable expansion, command substitution, and pathname expansion. Surrounding a word with double quotation marks suppresses all types of expansion except parameter and variable expansion. Single quotation marks suppress all types of expansion, as does quoting (escaping) a special character by preceding it with a backslash. Exercises 1. Explain the following unexpected result: $ whereis date date: /bin/date ... $ echo $PATH .:/usr/local/bin:/usr/bin:/bin $ cat > date echo "This is my own version of date." $ date Tue May 22 11:45:49 PDT 2007 2. What are two ways you can execute a shell script when you do not have execute access permission for the file containing the script? Can you exe- cute a shell script if you do not have read access permission for the file containing the script? 3. What is the purpose of the PATH variable? a. Set the PATH variable so that it causes the shell to search the following directories in order: • /usr/local/bin • /usr/bin • /bin • /usr/kerberos/bin •The bin directory in your home directory • The working directory b. If there is a file named doit in /usr/bin and another file with the same name in your ~/bin, which one will be executed? (Assume that you have execute permission for both files.)350 Chapter 9 The Bourne Again Shell c. If your PATH variable is not set to search the working directory, how can you execute a program located there? d. Which command can you use to add the directory /usr/games to the end of the list of directories in PATH? 4. Assume that you have made the following assignment: $ person=jenny Give the output of each of the following commands: a. echo $person b. echo '$person' c. echo "$person" 5. The following shell script adds entries to a file named journal-file in your home directory. This script helps you keep track of phone conversations and meetings. $ cat journal # journal: add journal entries to the file # $HOME/journal-file file=$HOME/journal-file date >> $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 (page 445) the first time it accepts input from the terminal and the cat utility the second time? 6. Assume that the /home/jenny/grants/biblios and /home/jenny/biblios directories exist. Give Jenny’s working directory after she executes each sequence of commands given. Explain what happens in each case. a. $ pwd /home/jenny/grants $ CDPATH=$(pwd) $ cd $ cd biblios b. $ pwd /home/jenny/grants $ CDPATH=$(pwd) $ cd $HOME/bibliosAdvanced Exercises 351 7. Name two ways you can identify the PID number of your login shell. 8. Give the following command: $ sleep 30 | cat /etc/inittab Is there any output from sleep? Where does cat get its input from? What has to happen before the shell displays another prompt? Advanced Exercises 9. Write a sequence of commands or a script that demonstrates that variable expansion occurs before pathname expansion. 10. Write a shell script that outputs the name of the shell that is executing it. 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 that 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 that 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 stan- dard 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 that it behaves sensibly when given such arguments as /.352 Chapter 9 The Bourne Again Shell 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.353353 10Chapter10The 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 com- patible 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 sec- ond). People quickly invented new ways to take advantage of this computing power, such as email, news retrieval, and bulletin board services. With the speed of today’s networks, a piece of email can cross the country or even travel halfway around the world in a few seconds. Today it would be difficult to find a computer facility that does not include a LAN to link its systems. Linux systems are typi- cally attached to an Ethernet (page 1035) 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). In This Chapter Types of Networks and How They Work. . . . . . . . . . . . . . . . . . 355 Network Protocols. . . . . . . . . . . . . 361 Network Utilities . . . . . . . . . . . . . . 372 ping: Tests a Network Connection. . . . . . . . . . . . . . . . . 375 traceroute: Traces a Route Over the Internet . . . . . . . . . . . . . . . . 376 host and dig: Query Internet Nameservers . . . . . . . . . . . . . . . 378 Distributed Computing . . . . . . . . . 379 Usenet . . . . . . . . . . . . . . . . . . . . . . 388 WWW: World Wide Web . . . . . . . . 390 10 Networking and the Internet354 Chapter 10 Networking and the Internet 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 may 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 thou- sands of miles away and receive a reply within minutes. A related term, intranet, refers to the networking infrastructure within a company or other institution. Intra- nets are usually private; access to them from external networks may be limited and carefully controlled, typically using firewalls (page 359). Network services Over the past decade many network services have emerged and become standard- ized. On Linux and UNIX systems, special processes called daemons (page 1032) 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 (such as cat, vim, lpr, mv, or their graphical counterparts) to work with them. Developers have created new tools and extended existing ones to take advantage of higher network speeds and to work within more crowded networks. The rlogin, rsh, and telnet utilities, which were designed long ago, have largely been supplanted by ssh (secure shell, page 707) in recent years. The ssh utility allows a user to log in on or execute commands securely on a remote computer. Users rely on such utilities as scp and ftp to transfer files from one system to another across the network. Commu- nication utilities, including email utilities and chat programs (e.g., talk, Internet Relay Chat [IRC], ICQ, and instant messenger [IM] programs, such as AOL’s AIM and gaim) have become so prevalent that many people with very little computer expertise use them on a daily basis to keep in touch with friends, family, and colleagues. 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 very similar to a LAN (local area network) but is based on Internet technology. 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 run- ning different versions of Windows, machines running UNIX and Linux, and so on. Each of these machines can communicate via IP (page 361), a common protocol. So it is with an intranet: Dissimilar machines can all talk to one another. Another key difference between the Internet and an intranet is that the Internet trans- mits only one protocol suite: IP. In contrast, an intranet can be set up to use a number of protocols, such as IP, IPX, AppleTalk, DECnet, XNS, or other protocols developed by vendors over the years. Although these protocols cannot be transmitted directly over the Internet, you can set up special gateway boxes at remote sites that tunnel or encapsulate these protocols into IP packets and then use the Internet to pass them.Types of Networks and How They Work 355 You can use an extranet (also called a partner net) or a virtual private network (VPN) 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. Following are some terms you may want to become familiar with before you read the rest of this chapter: ASP (page 1024) hub (page 1041) packet (page 1051) bridge (page 1026) internet (page 1042) router (page 1058) extranet (page 1036) Internet (page 1042) sneakernet (page 1060) firewall (page 1037) intranet (page 1042) switch (page 1063) gateway (page 1038) ISP (page 1043) VPN (page 1068) Types of Networks and How They Work Computers communicate over networks using unique addresses assigned by system software. A computer message, called a packet, frame, or datagram, includes the address of the destination computer and the sender’s return address. The three most common types of networks are broadcast, point-to-point, and switched. Once pop- ular, token-based networks (such as FDDI and token ring) are rarely seen anymore. Speed is critical to the proper functioning of the Internet. Newer specifications (cat 6 and cat 7) are being standardized for 1000BaseT (1 gigabit per second, called gigabit Ethernet, or GIG-E) and faster networking. Some of the networks that form the backbone of the Internet run at speeds of almost 10 gigabits per second (OC192) to accommodate the ever-increasing demand for network services. Table 10-1 lists some of the specifications in use today. Table 10-1 Network specifications Specification Speed DS0 64 kilobits per second ISDN Two DS0 lines plus signaling (16 kilobits per second) or 128 kilobits per second T-1 1.544 megabits per second (24 DS0 lines) T-3 43.232 megabits per second (28 T-1s) OC3 155 megabits per second (100 T-1s) OC12 622 megabits per second (4 OC3s) OC48 2.5 gigabits per seconds (4 OC12s) OC192 9.6 gigabits per second (4 OC48s) 356 Chapter 10 Networking and the Internet Broadcast Networks On a broadcast network, such as Ethernet, any of the many systems attached to the network cable can send a message at any time; each system examines the 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 gar- bled. The sending 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 net- work; if the collision rate gets too high, retransmissions may result in more colli- sions. Ultimately the network may become unusable. Point-to-Point Networks A point-to-point link does not seem like much of a network because only two end- points are involved. However, most connections to WANs (wide area networks) go through point-to-point links, using wire cable, radio, or satellite links. The advan- tage of a point-to-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 sys- tem 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. Point-to-point links often use serial lines and modems. The combination of a modem with a point-to-point link allows an isolated system to connect inexpen- sively to a larger network. 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, 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 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 railroad roundhouse. 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-to-point 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. 1. The term DSL incorporates the xDSL suite of technologies, which includes ADSL, XDSL, SDSL, and HDSL.Types of Networks and How They Work 357 LAN: Local Area Network Local area networks (LANs) are confined to a relatively small area—a single com- puter facility, building, or campus. Today most LANs run over copper or fiberoptic (glass or plastic) cable, but other wireless technologies, such as infrared (similar to most television remote control devices) and radio wave (wireless, or Wi-Fi), are becoming more popular. If its destination address is not on the local network, a packet must be passed on to another network by a router (page 358). A router may be a general-purpose com- puter or a special-purpose device attached to multiple networks to act as a gateway among them. 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 1 gigabit per second, with further speed enhancements planned for the future. As a result of computer load, competing network traffic, and network over- head, file transfer rates on an Ethernet are always slower than the maximum, theo- retical transfer rate. Cables An Ethernet network transfers data using copper or fiberoptic cable or wireless trans- mitters 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,2 were developed. Today most Ethernet connections are either wireless or made over unshielded twisted pair (referred to as UTP, Category 5 [cat 5], Category 5e [cat 5e], Category 6 [cat 6], 10BaseT, or 100BaseT) wire—similar to the type of wire used for telephone lines and serial data communications. 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 361). Duplex In half-duplex mode, packets travel in one direction at a time over the cable. In full- duplex mode, packets travel in both directions. Hub 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 361). Switch A switch connects network segments. A switch inspects each data packet and learns which devices are connected to which of its ports. The switch sorts packets and sends each packet only to the device it is intended for. Because a switch sends pack- ets only to their destination devices, it can conserve network bandwidth and per- form better than a hub. A switch may have buffers for holding and queuing packets. Switches work at the data link layer of the IP and OSI models (layer 2, page 362). 2. 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.358 Chapter 10 Networking and the Internet Some 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 things by eliminating collisions. Theoretically, each host on a switched network can transmit and receive simultaneously at 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. Router A router connects networks. For example, a router can connect a LAN to a WAN (such as the Internet). 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, page 362). The next page covers routers in more depth. 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 of the common wireless boards. 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 page 700 and to the Linux Wireless LAN HOWTO at www.hpl.hp.com/personal/Jean_Tourrilhes/Linux. WAN: Wide Area Network A wide area network (WAN) covers a large geographic area. In contrast, the technol- ogies (such as Ethernet) used for LANs were designed to work over limited distances and for a certain number of host connections. A WAN may span long distances over dedicated data lines (leased from a telephone company) or radio or satellite links. Such networks are often used to interconnect LANs. 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 metropol- itan 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 converts the data traffic from the format used on the LAN to that used on the 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 for- mat 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.Types of Networks and How They Work 359 Router A router (page 1058) is the most popular form of gateway. Routers play an impor- tant 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 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. Comput- ers and networks along the route you choose may be overloaded or down, without providing 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 would be like heading out on a car trip without consulting a map to find a route to your destina- tion; 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 (next page) 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 that the details have been left out; and wireless connections are drawn as zigzag lines with breaks, indicating that the con- nection may be intermittent. In Figure 10-1, a gateway or a router relays messages between each LAN and the Internet. Three of the routers in the Internet are shown (for example, the one closest to each site). Site A has a server, a workstation, a network computer, and a PC shar- ing 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 com- bined 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 network360 Chapter 10 Networking and the Internet Figure 10-1 A Slice of the Internet NC Network Linux PC Personal W computer Ethernet Firewall Router Router 3 Router 1 Router 2 W W W Gateway PC W Printer Ethernet Ethernet W W Internet GATEWAY/ W W W Legend PC W NC Site B Site A Site C Ethernet Printer Printer Wireless access NC W workstation Router Switch Server computer point Figure 10-1 A slice of the InternetTypes of Networks and How They Work 361 and prevent anyone except users from selected domains from using FTP to retrieve data from the network. The implementations of firewalls vary widely—from Linux machines with two interfaces (page 1042) running custom software to a router (preceding sec- tion) with simple access lists to esoteric, vendor-supplied firewall appliances. Most larger installations have at least one kind of firewall in place. A firewall is often accom- panied by a proxy server/gateway (page 387) 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 becom- ing 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 may have an inexpensive Linux machine with two network interfaces and packet-filtering software functioning as a dedicated firewall. One of the inter- faces 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 26 for information on firestarter, iptables, and setting up a firewall and to Appendix C for a discussion of security. Network Protocols To exchange information over a network, computers must communicate using a com- mon language, or protocol (page 1054). The protocol determines the format of mes- sage packets. The predominant network protocols used by Linux systems are TCP and IP,3 collectively referred to as TCP/IP (Transmission Control Protocol and Inter- net Protocol). Network services that need highly reliable connections, such as ssh and scp, tend to use TCP/IP. Another protocol used for some system services is UDP (User Datagram Protocol). Network services that do not require guaranteed delivery, such as RealAudio and RealVideo, operate satisfactorily with the simpler UDP.4 IP: Internet Protocol Layering was introduced to facilitate protocol design: Layers distinguish functional differences between adjacent protocols. A grouping of layers can be standardized into a protocol model. IP has a model that distinguishes protocol layers. The IP model differs from the ISO seven-layer protocol model (also called the OSI model) that is 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 (copper, fiber, wireless) and the data encoding used to 3. All references to IP imply IPv4 (page 1043). 4. 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 introduces a delay on a busy network when packets get retransmitted. This delay is not acceptable for video and audio transmissions, whereas less than 100 percent integrity is acceptable.362 Chapter 10 Networking and the Internet transmit signals on that medium (pulses of light, electrical waves, or radio waves, for instance). 2. The second layer, called the data link layer, covers media access by net- work 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 1022) wireless. 3. The third layer, called the network layer, frequently uses IP and addresses and routes packets. 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 guar- anteed delivery, delivery of packets in the order of their transmission, flow control, error detection, and error correction. The transport layer is respon- sible 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 of the upper-layer characteristics, such as character encoding, encryption, and GUIs, are part of the application. Applications choose the transport char- acteristics they require as well as the corresponding transport layer proto- col 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 transmis- sion order; it also works with multiple ports so that it can handle more than one application. TCP is a connection-oriented protocol (page 1030), 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, 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 errorTypes of Networks and How They Work 363 detection but does not automatically retransmit datagrams (page 1032) 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 data- gram 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. This protocol compresses data to make the most of the limited bandwidth available on serial con- nections. PPP, which replaces SLIP5 (Serial Line IP), acts as a point-to-point layer 2/3 transport that many other types of protocols can ride on. It is used mostly for IP-based services and connections, such as TCP or UDP. Xremote and LBX Two protocols that speed up data transfer over serial lines are Xremote and LBX. Xremote compresses the X Window System protocol so that it is more efficient over slower serial lines. LBX (low-bandwidth X) is based on the Xremote technology and is part of X Window System release X11R6 and higher. Host Address Each computer interface is identified by a unique address, or host number, on its network. A system attached to more than one network has multiple interfaces—one for each network, each with a unique address. Each packet of information that is broadcast over the network has a destination address. All hosts on the network must process each broadcast packet to see whether it is addressed to that host.6 If the packet is addressed to a given host, that host continues to process it. If not, the host ignores the packet. The network address of a machine is an IP address, which, under IPv4, is repre- sented as one number broken into four segments separated by periods (for example, 192.168.184.5). Domain names and IP addresses are assigned through a highly dis- tributed 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 may not deal with any of these agencies directly, your Inter- net service provider most assuredly does. How a company uses IP addresses is determined by the system or network adminis- trator. For example, the leftmost two sets of numbers in an IP address might represent 5. SLIP was one of the first serial line implementations of IP and has slightly less overhead than PPP. PPP supports multiple protocols (such as AppleTalk and IPX), whereas SLIP supports only IP. 6. Contrast broadcast packets with unicast packets: Ethernet hardware on a computer filters out unicast pack- ets that are not addressed to that machine; the operating system on that machine never sees these packets.364 Chapter 10 Networking and the Internet 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, con- verting it to its binary equivalent, a series of 1s and 0s. See the following optional sec- tion for more information. Refer to “Private address space” on page 697 for information about addresses you can use on a LAN without registering them. Static Versus Dynamic IP Addresses A static IP address is one that always remains the same. A dynamic IP address is one that can change each time you connect to the network. A dynamic address remains the same during a single login session. Any server (mail, Web, and so on) must have a static address so clients can find the machine that is acting as the server. End-user systems usually work well with dynamic addresses. During a given login session, they can function as a client (your Web browser, for example) because they main- tain a constant IP address. When you log out and log in again, it does not matter that you have a different IP address because your computer, acting as a client, estab- lishes 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. optional IP Classes To facilitate routing on the Internet, IP addresses are divided into classes. These classes, which are labeled class A through class E, allow the Internet address space to be broken into blocks of small, medium, and large networks that are designed to be assigned based on the number of hosts within a network. When you need to send a message to an address outside the local network, your sys- tem looks up the address block/class in its routing table and sends the message to the next router on the way to the final destination. Every router along the way does a similar lookup and forwards the message accordingly. At the destination, local routers direct the message to the specific address. Without classes and blocks, your host would have to know every network and subnetwork address on the Internet before it could send a message. This setup would be impractical because of the huge number of addresses on the Internet. Each of the four numbers in the IP address is in the range 0–255 because each seg- ment of the IP address is represented by 8 bits (an octet), with each bit being capa- ble of taking on two values; the total number of values is therefore 28 = 256. When you start counting at 0, the range 1–256 becomes 0–255.7 Each IP address is divided into a net address (netid) portion, which is part of the class, and a host address (hostid) portion. See Table 10-2. 7. Internally, the IP address is represented as a set of four unsigned 8-bit fields or a 32-bit unsigned num- ber, depending on how programs are using it. The most common format in C is to represent it as a union of an unsigned 32-bit long integer, four unsigned chars, and two unsigned short integers.Types of Networks and How They Work 365 The first set of addresses, defining class A networks, is reserved for extremely large corporations, such as General Electric (3.0.0.0) and Hewlett-Packard (15.0.0.0), and for ISPs. One start bit (0) in the first position designates a class A network, 7 bits holds the network portion of the address (netid), and 24 bits holds the host portion of the address (hostid; see Table 10-2). This setup means that GE can have 224, or approximately 16 million, hosts on its network. Unused address space and subnets (page 1062) lower this number quite a bit. The 127.0.0.0 subnet (page 369) is reserved, as are several others (see private address space on page 1054). Two start bits (10) in the first two positions designates a class B network, 14 bits holds the network portion of the address (netid), and 16 bits holds the host portion of the address, for a potential total of 65,534 hosts.8 A class C network uses 3 start bits (100), 21 netid bits (2 million networks), and 8 hostid bits (254 hosts). Today a new large customer will not receive a class A or B network but is likely to receive a class C or several (usually contiguous) class C networks, if merited. Several other classes of networks exist. Class D networks are reserved for multicast (page 1049) networks. When you run netstat –nr on a Linux system, you can see whether the machine is a member of a multicast network. A 224.0.0.0 in the Desti- nation column that netstat displays indicates a class D, multicast address (Table 10-2). A multicast is like a broadcast, but only hosts that subscribe to the Table 10-2 IP classes Class Start bits Address range All bits (including start bits) 0–7 8–15 16–23 24–31 Class A 0 001.000.000.000–126.000.000.000 0-netid ========hostid========= Class B 10 129.000.000.000–191.255.000.000 10-----netid------ =====hostid===== Class C 110 192.000.000.000–223.255.255.000 110----------netid----------- =hostid= Class D (multicast) 1110 224.000.000.000–239.255.255.000 1110 Class E (reserved) 11110 240.000.000.000–255.255.255.000 11110 8. A 16-bit (class B) address can address 216 = 65,536 hosts, yet the potential number of hosts is two fewer than that because the first and last addresses on any network are reserved. In a similar manner, an 8-bit (class C) address can address only 254 hosts (28 – 2 = 254). The 0 host address (for example, 194.16.100.0 for a class C network or 131.204.0.0 for a class B network) is reserved as a designator for the network itself. Several older operating systems use this as a broadcast address. The 255 host address (for example, 194.16.100.255 for a class C network or 131.204.255.255 for a class B network) is reserved as the IP broadcast address. An IP packet (datagram) that is sent to this address is broadcast to all hosts on the network. The netid portion of a subnet does not have the same limitations. Often you are given the choice of re- serving the first and last networks in a range as you would a hostid, but this is rarely done in practice. More often the first and last networks in the netid range provide more usable address space. Refer to “Subnets” on page 367.366 Chapter 10 Networking and the Internet multicast group receive the message. To use Web terminology, a broadcast is like a “push.” A host pushes a broadcast on the network, and every host on the network must check each packet to see whether it contains relevant data. A multicast is like a “pull.” A host will see a multicast only if it registers itself as subscribed to a multi- cast group or service and pulls the appropriate packets from the network. Table 10-3 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 easi- est to work with for bitwise (binary) computations. The first three lines show the IP address. The next three lines show the subnet mask (page 1063) in three bases. Next the IP address and the subnet mask are ANDed together bitwise to yield the subnet number (page 1063), which is shown in three bases. The last three lines show the broadcast address (page 1026), which is computed by taking the subnet number and turning the hostid bits to 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 1050). 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. Table 10-3 Computations for IP address 131.204.027.027 ---------------Class B----------- netid hostid IP address 131 .204 .027 .027 decimal 83 CC 1B 1B hexadecimal 1000 0011 1100 1100 0001 1011 0001 1011 binary Subnet mask 255 .255 .255 .000 decimal FF FF FF 00 hexadecimal 1111 1111 1111 1111 1111 1111 0000 0000 binary IP address bitwise AND 1000 0011 1100 1100 0001 1011 0001 1011 binarySubnet mask 1111 1111 1111 1111 1111 1111 0000 0000 = Subnet number 1000 0011 1100 1100 0001 1011 0000 0000 Subnet number 131 .204 .027 .000 decimal 83 CC 1B 00 hexadecimal 1000 0011 1100 1100 0001 1011 0000 0000 binary Broadcast address (set host bits to 1) 131 .204 .27 .255 decimal 83 CC 1B FF hexadecimal 1000 0011 1100 1100 0001 1011 1111 1111 binaryTypes of Networks and How They Work 367 Subnets Each host on a network must process each broadcast packet to determine whether the information in the packet is useful to that host. If the network includes numer- ous hosts, each host must process many packets. To maintain efficiency—most net- works, and particularly shared media networks such as Ethernet—need to be split into subnetworks, or subnets.9 The more hosts on a network, the more dramatically network performance is affected. Organizations use router and switch technology called VLANs (virtual local area networks) to group similar hosts into broadcast domains (subnets) based on function. For example, it is not uncommon to see a switch with different ports being part of different subnets. See page 529 for infor- mation on how to specify a subnet. Subnet mask A subnet mask (or address 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 (Table 10-3), 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), or /24 (the number of bits used for the subnet mask). If it were a class B network (of which 16 bits are already fixed), this yields 28 (24 total bits – 16 fixed bits = 8 bits, 28 = 256) net- works10 with 28 – 2 (256 – 2 = 254) hosts11 on each network. For example, when you divide the class C 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 Calcu- lators” on page 987). To use this calculator to determine the preceding subnet mask, start with an IP host address of 192.25.4.0. For more information refer to “Specifying a Subnet” on page 529. CIDR: Classless Inter-Domain Routing CIDR (pronounced “cider”) allows groups of addresses that are smaller than a class C block to be assigned to an organization or ISP and then further subdivided and parceled out. In addition, it helps to alleviate the potential problem of routing tables on major Internet backbone and peering devices becoming too large to manage. 9. Splitting a network is also an issue with other protocols, particularly AppleTalk. 10. The first and last networks are reserved in a manner similar to the first and last hosts, although the standard is flexible. You can configure routers to reclaim the first and last networks in a subnet. Different routers have different techniques for reclaiming these networks. 11. Subtract 2 because the first and last host addresses on every network are reserved.368 Chapter 10 Networking and the Internet The pool of available IPv4 addresses has been depleted to the point that no one gets a class A address anymore. The trend is to reclaim these huge address blocks, if pos- sible, and recycle them into groups of smaller addresses. Also, as more class C addresses are assigned, routing tables on the Internet are filling up and causing memory overflows. The solution is to aggregate12 groups of addresses into blocks and allocate them to ISPs, which in turn subdivide these blocks and allocate them to their customers. The address class designations (A, B, and C) described in the previ- ous section are used less often today, although you may still encounter subnets. When you request an address block, your ISP usually gives you as many addresses as you need—and no more. The ISP aggregates several contiguous smaller blocks and routes them to your location. 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 could support 210 hosts (32 – 22 = 10). Your ISP would set its routers so that any 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 subnets, perhaps into four networks. Four networks require an additional two bits of addressing (22 = 4). You could therefore set up your router to support four net- works 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 net- works 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 that are stored in the /etc/hosts file: $ cat /etc/hosts 127.0.0.1 localhost 130.128.52.1 gw–example.example.com gw–example 130.128.52.2 bravo.example.com bravo 130.128.52.3 hurrah.example.com hurrah 130.128.52.4 kudos.example.com kudos 12. Aggregate means to join. In CIDR, the aggregate of 208.178.99.124 and 208.178.99.125 is 208.178.99.124/23 (the aggregation of two class C blocks).Types of Networks and How They Work 369 localhost = 127.0.0.1 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. 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 increasingly larger and became impossible to keep up-to-date. To solve this problem Linux supports NIS (Network Information Service, page 383), 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. DNS The solution to this dilemma is DNS (Domain Name Service, page 381). DNS effec- tively addresses the efficiency and update issues by arranging the entire network namespace (page 1049) 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 distrib- utes name administration across the entire Internet. IPv6 The explosive growth of the Internet has uncovered deficiencies in the design of the current address plan—most notably the shortage of addresses. Over the next few years, a revised protocol, named IPng (IP Next Generation), also known as IPv6 (IP version 6),13 will be phased in. (It may take longer—the phase-in is going quite slowly.) This new scheme is designed to overcome the major limitations of the cur- rent approach and can be implemented gradually because it is compatible with the existing address usage. IPv6 makes it possible to assign many more unique Internet addresses (2128, or 340 undecillion [1036]). It also supports more advanced security and performance control features: • IPv6 enables autoconfiguration. With IPv4, autoconfiguration is available using optional DHCP (page 538). With IPv6, autoconfiguration is manda- tory, making it easy for hosts to configure their IP addresses automatically. • IPv6 reserves 24 bits in the header for advanced services, such as resource reservation protocols, better backbone routing, and improved traffic engineering. • IPv6 makes multicast protocols mandatory and uses them extensively. In IPv4, multicast, which improves scalability, is optional. 13. IPv5 referred to an experimental real-time stream protocol named ST—thus the jump from IPv4 to IPv6.370 Chapter 10 Networking and the Internet • IPv6 aggregates address blocks more efficiently because of the huge address space. This aggregation makes obsolete NAT (page 1049), which decreased scalability and introduced protocol issues. • IPv6 provides a simplified packet header that allows hardware accelerators to work better. A sample IPv6 address is fe80::a00:20ff:feff:5be2/10. Each group of four hexadeci- mal digits is equivalent to a number between 0 and 65,536 (164). A pair of adjacent colons indicates a hex value of 0x0000; leading 0s need not be shown. With eight sets of hexadecimal groupings, 65,5368 = 2128 addresses are possible. In an IPv6 address on a host with the default autoconfiguration, the first characters in the address are always fe80. The last 64 bits hold an interface ID designation, which is often the MAC address (page 1046) of the system’s Ethernet controller. 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 (such as finger, page 167), and communications utilities (such as talk). 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 that you are requesting information from or corresponding with someone on the local system. The prompts shown in the examples in this chapter include the hostname of the sys- tem you are using. If you frequently use more than one system over a network, you may 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 uname –n: $ hostname kudos See page 303 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 obvi- ous that finger should be enhanced to reach out and collect information remotely. In the following examples, finger displays information about all users logged in on the system named bravo:Communicate Over a Network 371 [kudos]$ finger @bravo [bravo.example.com] Login Name Tty Idle Login Time Office Office Phone sam Sam the Great *1 1:35 Oct 22 5:00 alex Alex Watson 4 Oct 22 12:23 (kudos) alex Alex Watson 5 19 Oct 22 12:33 (:0) jenny Jenny Chen 7 2:24 Oct 22 8:45 (:0) hls Helen Simpson 11 2d Oct 20 12:23 (:0) A user’s username in front of the @ sign causes finger to display information from the remote system for the specified user only. If the remote system has multiple matches for that name, finger displays the results for all of them: [kudos]$ finger alex@bravo [bravo.example.com] Login Name Tty Idle Login Time Office Office Phone alex Alex Watson 4 Oct 22 12:23 (kudos) alex Alex Watson 5 19 Oct 22 12:33 (:0) The finger utility works by querying a standard network service, the in.fingerd dae- mon, that runs on the system being queried. Although this service is available in the fingerd package for Ubuntu Linux, some sites choose not to 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. As a result, the report displayed for any given system may differ from that shown in the preceding examples. The information for remote finger looks much the same as it does when finger runs on the local system, with one difference: Before displaying the results, finger reports the name of the remote system that answered the query (bravo, as shown in brackets in the preceding example). The name of the host that answers may be different from the system name you specified on the command line, depending on how the finger dae- mon service is configured on the remote system. In some cases, several hostnames may be listed if one finger daemon contacts another to retrieve the information. Sending Mail to a Remote User Given a user’s username on a remote system and the name of the remote system or its domain, you can use an email program to send a message over the network or the Internet, using the @ form of an address: jenny@bravo or jenny@example.com The in.fingerd daemon security The finger daemon (in.fingerd) gives away system account information that can aid a malicious user. Some sites disable finger or randomize user account IDs to make a malicious user’s job more difficult. Do not install the fingerd package if you do not want to run the finger daemon.372 Chapter 10 Networking and the Internet Although many Linux utilities recognize the @ form of a network address, you may find that you can reach more remote computers with email than with the other net- working utilities described in this chapter. This disparity arises because the email system can deliver a message to a host that does not run IP, even though it appears to have an Internet address. The message may be routed over the network, for example, until it reaches a remote system that has a point-to-point, dial-up connec- tion to the destination system. Other utilities, such as talk, rely on IP and operate only between networked hosts. Mailing List Servers A mailing list server (listserv14) 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 and for a person to disseminate information periodically to a potentially large mailing list. 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. Popular list servers include LISTSERV (www.lsoft.com), Lyris (www.lyris.com), Majordomo (www.greatcircle.com/ majordomo), Mailman (www.list.org, page 775), and ListProc (www.listproc.net). Ubuntu maintains quite a few mailing lists and list archives for those mailing lists at lists.ubuntu.com. 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 Some commands, such as rcp and rsh, work only if the remote system trusts your local computer (that is, if the remote system knows your 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. 14. 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 infor- mation go to www.lsoft.com).Network Utilities 373 Host-based trust is largely obsolete. Because there are many ways to circumvent trusted host security, including subverting DNS systems and IP spoofing (page 1043), authentication based on IP address is widely regarded as insecure and obsolete. In a small homogeneous network of machines with local DNS control, it can be “good enough.” Its greater ease of use in these situations may outweigh the security concerns. 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 Ubuntu Linux and can be used as drop-in replacements for their insecure counterparts. The OpenSSH tool suite is covered in detail in Chapter 19. telnet: Logs In on a Remote System You can use the TELNET protocol to interact with a remote computer. The telnet utility, a user interface to this protocol, is older than ssh and is not secure. Never- theless, it may work where ssh (page 714) is not available (there is more non-UNIX support for TELNET access than for ssh access). In addition, many legacy devices, such as terminal servers and network devices, do not support ssh. [bravo]$ telnet kudos Trying 172.19.52.2... Connected to kudos.example.com Escape character is '^]'. Welcome to SuSE Linux 7.3 (i386) - Kernel 2.4.10-4GB (2). kudos login: watson Password: You have old mail in /var/mail/watson. Last login: Mon Feb 27 14:46:55 from bravo.example.com watson@kudos:~> ... watson@kudos:~> logout Connection closed by foreign host. [bravo]$ telnet versus ssh When you connect to a remote UNIX or Linux system using telnet, you are pre- sented with a regular, textual login: prompt. Unless you specify differently, the ssh utility assumes that your username on the remote system matches that on the local system. Because telnet is designed to work with non-UNIX and non-Linux systems, it makes no such assumptions. Do not share your login account security You can use a .rhosts file to allow another user to log in as you from a remote system without knowing your password. This setup is not recommended. 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 whenever possible.374 Chapter 10 Networking and the Internet Another difference between these two utilities is that telnet allows you to configure many special parameters, such as how RETURNs or interrupts are processed. 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: [bravo]$ telnet telnet> open kudos Trying 172.19.52.2... Connected to kudos.example.com Escape character is '^]'. ... Before connecting you 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: [kudos]$ CONTROL-] telnet> ? (displays help information) telnet> close Connection closed. [bravo]$ 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. You can use telnet to access special remote services at sites that have chosen to make such services available. However, many of these services, such as the U.S. Library of Congress Information System (LOCIS), have moved to the Web. As a consequence, you can now obtain the same information using a Web browser. 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 use unallocated port numbers. For example, you can access some multiplayer text games, called MUDs (Multi-User Dungeons, or Dimensions), using telnet to connect to a spec- ified port, such as 4000 or 8888. Unlike the port numbers for standard protocols, these port numbers can be picked arbitrarily by the administrator of the game. 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 listening in on the session.Network Utilities 375 While telnet is no longer commonly employed to log in on remote systems, it is still used extensively as a debugging tool. This utility allows 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. 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 a 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 dis- plays 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 responds, 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 2005 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 The File Transfer Protocol (FTP) is a method of downloading files from and upload- ing files to another system using TCP/IP over a network. FTP is not a secure proto- col; use it only for downloading public information from a public server. Most Web browsers can download files from FTP servers. Chapter 20 covers FTP clients and servers. ping: Tests a Network Connection The ping15 utility (http://ftp.arl.mil/~mike/ping.html) sends 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 15. 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.376 Chapter 10 Networking and the Internet 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 with CONTROL-C. $ ping tsx-11.mit.edu PING tsx-11.mit.edu (18.7.14.121) 56(84) bytes of data. 64 bytes from TSX-11.MIT.EDU (18.7.14.121): icmp_seq=0 ttl=45 time=97.2 ms 64 bytes from TSX-11.MIT.EDU (18.7.14.121): icmp_seq=1 ttl=45 time=96.1 ms 64 bytes from TSX-11.MIT.EDU (18.7.14.121): icmp_seq=2 ttl=45 time=95.7 ms 64 bytes from TSX-11.MIT.EDU (18.7.14.121): icmp_seq=3 ttl=45 time=96.3 ms CONTROL-C --- tsx-11.mit.edu ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3001ms rtt min/avg/max/mdev = 95.755/96.361/97.202/0.653 ms This example shows that the remote system named tsx-11.mit.edu is up and avail- able over the network. By default ping sends packets containing 64 bytes (56 data bytes and 8 bytes of pro- tocol header information). In the preceding example, four packets were sent to the system tsx-11.mit.edu 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 (called icmp_seq) is also given. If a packet is dropped, a gap occurs in the sequence numbers. 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, net- work 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 mini- mum, average, maximum, and mean deviation round-trip times it measured. Use ping6 to test IPv6 networks. traceroute: Traces a Route Over the Internet The traceroute utility traces the route that an IP packet follows, including all inter- mediary points traversed (called network hops), to its destination (the argument to traceroute—an Internet host). It displays a numbered list of hostnames, if avail- able, and IP addresses, together with the round-trip time it took for a packet to reach each router along the way and an acknowledgment to get back. You can put When ping cannot connect tip If it is unable to contact the remote system, ping continues trying until you interrupt it with CONTROL-C. A system may not answer for any of several reasons: The remote computer may be down, the network interface or some part of the network between the systems may be broken, a software failure may have occurred, or the remote machine may be set up, for reasons of security, not to return pings (try pinging www.microsoft.com or www.ibm.com).Network Utilities 377 this information to good use when you are trying to identify the location of a net- work bottleneck. The traceroute utility has no concept of the path from one host to the next; instead, it simply sends out 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. In the case of a traceroute packet, the packet is returned by the host that has the packet when the TTL value is zero. The result is a list of hosts that the packet traveled through to get to its destination. The traceroute utility can help you solve routing configuration problems and locate routing path failures. When you cannot reach a host, use traceroute to discover what path the packet follows, how far it gets, and what the delay is. The next example 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 statis- tics 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. $ /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 ms378 Chapter 10 Networking and the Internet host and dig: Query Internet Nameservers The host utility looks up an IP address given a name, 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 140.174.164.2 2.164.174.140.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 140.174.164.2 The dig (domain information groper) utility queries DNS servers and individual machines for information about a domain. A powerful utility, dig has many features that you may never use. It is more complex than host. Chapter 25 on DNS has many examples of the use of host and dig. jwhois: Looks Up Information About an Internet Site The jwhois utility (jwhois package) replaces whois and 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 responsi- ble for a site: Perhaps that person is sending you or your company spam (page 1061). Many sites on the Internet are easier to use and faster than jwhois. Use a browser and search engine to search on whois or go to www.networksolu- tions.com/whois or www.ripe.net/perl/whois to get started. When you do not specify a whois server, jwhois defaults to whois.internic.net. Use the –h option to jwhois to specify a different whois server. See the jwhois 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: $ jwhois sobell.com [Querying whois.internic.net] [Redirected to whois.godaddy.com] [Querying whois.godaddy.com] [whois.godaddy.com] The data contained in Go Daddy Software, Inc.'s WhoIs database, ... Registrant: Sobell Associates Inc POBox 460068 San Francisco, California 94146-0068 United StatesDistributed Computing 379 Registered through: GoDaddy.com Domain Name: SOBELL.COM Created on: 07-Apr-95 Expires on: 08-Apr-13 Last Updated on: 16-Jan-04 Administrative Contact: Sobell, Mark sobell@meer.net Sobell Associates Inc PO BOX 460068 SAN FRANCISCO, California 94146-0068 United States 9999999999 Fax -- 9999999999 Technical Contact: , hostmaster@meer.net meer.net po box 390804 Mountain View, California 94039 United States 18888446337 Fax -- 18888446337 Domain servers in listed order: NS.MEER.NET NS2.MEER.NET Several top-level registries serve various regions of the world. You are most likely to use the following ones: North American registry whois.arin.net European registry www.ripe.net Asia-Pacific registry www.apnic.net U.S. military whois.nic.mil U.S. government www.nic.gov Distributed Computing When many similar systems are found on the same 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 admin- istrator 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 track- ing down and updating copies scattered throughout the network on many different systems.380 Chapter 10 Networking and the Internet 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 23 contains instructions for setting up NFS clients and servers in net- worked configurations. The Client/Server Model Mainframe model The client/server model was not the first computational model. First came the main- frame, 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. File-sharing model 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. Client/server model In the client/server model, a client uses a protocol, such as FTP, to request services, and a server provides the services that the client requests. Rather than providing data files as the file-sharing model does, the server in a client/server relationship is a data- base that provides only those pieces of information that the client needs or requests. The client/server model dominates UNIX and Linux system networking and under- lies 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 Web servers and browser clients, are designed to interact with specific utili- ties. 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 geo- graphically 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 Figure 10-2 A fileserver /usr/man /home Fileserver Linux LinuxDistributed Computing 381 networks. For example, Microsoft’s Network Neighborhood and Apple’s AppleTalk both rely on broadcast-based PTP protocols for browsing and automatic configura- tion. The Zeroconf multicast DNS protocol is a PTP alternative DNS for small net- works. 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 Napster and Kazaa 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 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/cctld/cctld.htm for a complete list.) Although the United States is represented in the same way (US) and uses the standard two-letter Postal Service 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. Following is a list of the six original top-level domains. These domains are used extensively within the United States and, to a lesser degree, by users in other countries: COM Commercial enterprises EDU Educational institutions GOV Nonmilitary government agencies MIL Military government agencies NET Networking organizations ORG Other (often nonprofit) organizations Recently, the following additional top-level domains have been approved for use: AERO Air-transport industry BIZ Business COOP Cooperatives INFO Unrestricted use MUSEUM Museums NAME Name registries382 Chapter 10 Networking and the Internet Like Internet addresses, domain names were once assigned by the Network Infor- mation Center (NIC, page 363); now they are assigned by several companies. A sys- tem’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. With DNS, email addressed to user@example.com can be delivered to the com- puter named example.com that handles the corporate mail and knows how to for- ward messages to user mailboxes on individual machines. As the company grows, its site administrator might decide to create organizational or geographical subdo- mains. The name delta.ca.example.com might refer to a system that supports Cali- fornia offices, for example, while alpha.co.example.com is dedicated to Colorado. Functional subdomains might be 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 Inter- net Name Domain). BIND follows the client/server model. On any given local net- work, one or more systems may 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, may be a process running on the same computer as the nameserver, or it may 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 dis- tant 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 Figure 10-3 U.S. top-level domains com edu org bravo kudos okeeffe okeeffe mil net momaberkeley gov exampleDistributed Computing 383 and reuses it for a while. The nameserver deletes the nonlocal information before it can become outdated. Refer to “TTL” on page 1066. The system’s translation of symbolic hostnames into addresses is transparent to most users; only the system administrator of a networked system needs to be con- cerned with the details of name resolution. Systems that use DNS for name resolu- tion 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 25 covers setting up and running a DNS server. Three common sources are referenced for hostname resolution: NIS, DNS, and sys- tem files (such as /etc/hosts). Linux does not ask you to choose among these sources; rather, the nsswitch.conf file (page 542) 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 1051) flowing to and from the appropriate programs. A program that needs to receive data binds to a port and then uses that port for com- munication. 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 privileged ports. A service run on a privileged port provides assurance that the service is being provided by someone 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). 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. Refer to Chapter 22 for coverage of NIS. 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. NFS is covered in Chapter 23.384 Chapter 10 Networking and the Internet optional Internet Services Linux Internet services are provided by daemons that run continuously or by a dae- mon that is started automatically by the inetd or xinetd daemon (page 531) 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 usu- ally lists a small, commonly used subset of services. Visit www.rfc.net/rfc1700.html for more information and a complete list of registered services. Most of the daemons (the executable 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. Look at /usr/sbin/*d to see a list of many of the dae- mon programs on the local system. Refer to “SysVinit (rc) Scripts: Start and Stop Sys- tem Services” on page 507 for information about starting and stopping these daemons. To see how a daemon works, consider what happens when you run ssh. The local sys- tem 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 then they take turns asking each other specific ques- tions and waiting for valid replies. Each network service follows its own protocol. In addition to the daemons that support the utilities described up to this point, many other daemons support system-level network services that you will not typi- cally interact with. Table 10-4 lists some of these daemons. Table 10-4 Common daemons Daemon Used for or by Function acpid Advanced configuration and power interface 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. apache2 HTTP The Web server daemon (Apache, page 915). apmd Advanced power management Reports and takes action on specified changes in system power, including shutdowns. Useful with machines, such as laptops, that run on batteries. atd at Executes a command once at a specific time and date. See crond for periodic execution of a command. automount Automatic mounting Automatically mounts filesystems when they are accessed. Auto- matic mounting is a way of demand-mounting remote directories without having to hard-configure them into /etc/fstab.Distributed Computing 385 cron crontab Used for periodic execution of tasks. This daemon looks in the /var/spool/cron/crontabs directory for files with filenames that cor- respond to users’ 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. dhcpcd DHCP DHCP client daemon (page 539). dhcpd DHCP Assigns Internet address, subnet mask, default gateway, DNS, and other information to hosts. This protocol answers DHCP requests and, optionally, BOOTP requests. Refer to “DHCP: Configures Net- work Interfaces” on page 538. exim4 Mail programs The exim4 daemon came from the University of Cambridge. The 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. ftpd FTP Handles FTP requests. Refer to “ftp: Transfers Files over a Network” on page 375. See also vsftpd (page 729). gpm General-purpose mouse or GNU paste manager Allows you to use a mouse to cut and paste text on console applications. in.fingerd finger Handles requests for user information from the finger utility. inetd Listens for service requests on network connections and starts up the appropriate daemon to respond to any particular request. Because of inetd, a system does not need the daemons running con- tinually to handle various network requests. For more information refer to page 531. lpd Line printer spooler daemon Launched by xinetd when printing requests come to the machine. Not used with CUPS. named DNS Supports DNS (page 845). nfsd, statd, lockd, mountd, rquotad NFS These five daemons operate together to handle NFS (page 799) 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 permis- sions. If disk quotas are enabled, rquotad handles those. ntpd NTP Synchronizes time on network computers. Requires a /etc/ntp.conf file. For more information go to www.ntp.org. Table 10-4 Common daemons (continued)386 Chapter 10 Networking and the Internet portmap RPC Maps incoming requests for RPC service numbers to TCP or UDP port numbers on the local system. Refer to “RPC Network Services” on page 387. pppd PPP For a modem, this protocol controls the pseudointerface represented by the IP connection between the local computer and a remote com- puter. Refer to “PPP: Point-to-Point Protocol” on page 363. rexecd rexec Allows a remote user with a valid username and password to run programs on a system. Its use is generally deprecated for security reasons; certain programs, such as PC-based X servers, may still have it as an option. routed Routing tables Manages the routing tables so your system knows where to send messages that are destined for remote networks. If your system does not have a /etc/defaultrouter file, routed is started automatically to listen to incoming routing messages and to advertise outgoing routes to other systems on the local network. A newer daemon, the gateway daemon (gated), offers enhanced configurability and support for more routing protocols and is proportionally more complex. sendmail Mail programs The sendmail daemon came from Berkeley UNIX and has been avail- able for a long time. The de facto mail transfer program on the Inter- net, 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. smbd, nmbd Samba Allow Windows PCs to share files and printers with UNIX and Linux computers (page 823). sshd ssh, scp Enables secure logins between remote systems (page 720). syslogd System log Transcribes important system events and stores them in files and/or forwards them to users or another host running the syslogd dae- mon. This daemon is configured with /etc/syslog.conf and used with the syslog utility. See page 688. talkd talk 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. telnetd TELNET One of the original Internet remote access protocols (page 373). tftpd TFTP Used to boot a system or get information from a network. Examples include network computers, routers, and some printers. timed Time server On a LAN synchronizes time with other computers that are also run- ning timed. xinetd Internet superserver 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. For more informa- tion refer to page 531. Table 10-4 Common daemons (continued)Distributed Computing 387 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, relaying communications back and forth between an application, such as a browser and a server, usually 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 that 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 1027) to store frequently used Web pages so that 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 appropri- ate 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 Inter- net 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. In the client/server model, a client contacts a server on a specific port (page 383) to avoid any mixup between services, clients, and servers. 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 specifies388 Chapter 10 Networking and the Internet the port it expects to be contacted on. RPC servers typically use port numbers that have been defined by Sun. If a server does not use a predefined port number, it picks an arbitrary number. portmap The server then registers this port with the RPC portmapper (the portmap daemon) on the local system. The server tells the daemon which port number it is listening on and which RPC program numbers it serves. Through these exchanges, the portmap daemon learns the location of every registered port on the host and the programs that are available on each port. The portmap daemon, which always listens on port 111 for both TCP and UDP, must be running to make RPC calls. Files The /etc/rpc file (page 562) maps RPC services to RPC numbers. The /etc/services file (page 562) lists system services. RPC client/server communication The sequence of events for communication between an RPC client and server occurs as follows: 1. The client program on the client system makes an RPC call to obtain data from a (remote) server system. (The client issues a “read record from a file” request.) 2. If RPC has not yet established a connection with the server system for the client program, it contacts portmap on port 111 of the server and asks which port the desired RPC server is listening on (for example, rpc.nfsd). 3. The portmap daemon on the remote server looks in its tables and returns a UDP or TCP port number to the local system, the client (typically 2049 for nfs). 4. The RPC libraries on the server system receive the call from the client and pass the request to the appropriate server program. The origin of the request is transparent to the server program. (The filesystem receives the “read record from file” request.) 5. The server responds to the request. (The filesystem reads the record.) 6. The RPC libraries on the remote server return the result over the network to the client program. (The read record is returned to the calling program.) Under Ubuntu Linux most servers start and run their own daemons. When RPC servers are started by the xinetd daemon (page 531), the portmap daemon must be started before the xinetd daemon is invoked. The init scripts (page 507) make sure portmap starts before xinetd. You can confirm this sequence by looking at the numbers associated with /etc/rc.d/*/S*portmap and /etc/rc.d/*/S*/xinetd. If the portmap daemon stops, you must restart all RPC servers on the local system. Usenet One of the earliest information services available on the Internet, Usenet is an elec- tronic bulletin board that allows users with common interests to exchange informa- tion. Usenet comprises an informal, loosely connected network of systems that exchange email and news items (commonly referred to as netnews). It was formed in 1979 when a few sites decided to share some software and information on topicsUsenet 389 of common interest. They agreed to contact one another and to pass the informa- tion along over dial-up telephone lines (at that time running at 1,200 baud at best), using UNIX’s uucp utility (UNIX-to-UNIX copy program). The popularity of Usenet led to major changes in uucp to handle the escalating vol- ume of messages and sites. Today much of the news flows over network links using a sophisticated protocol designed especially for this purpose: NNTP (Network News Transfer Protocol). The news messages are stored in a standard format, and the many public domain programs available let you read them. An old, simple inter- face is named readnews. Other interfaces, such as rn, its X Window System cousin xrn, tin, nn, and xvnews, have many features that help you browse through and reply to the articles that are available or create articles of your own. In addition, Netscape and Mozilla include an interface that you can use to read news (Netscape/Mozilla News) as part of their Web browsers. One of the easiest ways to read netnews is to go to groups.google.com. The program you select to read netnews is largely a mat- ter of personal taste. As programs to read netnews articles have been ported to non-UNIX and non- Linux systems, the community of netnews users has become highly diversified. In the UNIX tradition, categories of netnews groups are structured hierarchically. The top level includes such designations as comp (computer-related), misc (miscella- neous), rec (recreation), sci (science), soc (social issues), and talk (ongoing discus- sions). Usually at least one regional category is at the top level, such as ba (San Francisco Bay Area), and includes information about local events. New categories are continually being added to the more than 30,000 newsgroups. The names of newsgroups resemble domain names but are read from left to right (like Linux file- names): comp.os.unix.misc, comp.lang.c, misc.jobs.offered, rec.skiing, sci.med, soc.singles, and talk.politics are but a few examples. A great deal of useful information is available on Usenet, but you need patience and perseverance to find what you are looking for. You can ask a question, and someone from halfway around the world might answer it. Before posing such a simple question and causing it to appear on thousands of systems around the world, however, first ask yourself whether you can get help in a less invasive way. Try the following: • Refer to the man pages and info. • Look through the files in /usr/share/doc. • Ask the system administrator or another user for help. • All of the popular newsgroups have FAQs (lists of frequently asked ques- tions). Consult these lists and see whether your question has been answered. FAQs are periodically posted to the newsgroups; in addition, all the FAQs are archived at sites around the Internet, including Google groups (groups.google.com). • Because someone has probably asked the same question earlier, search the netnews archives for an answer. Try looking at groups.google.com, which has a complete netnews archive.390 Chapter 10 Networking and the Internet • Use a search engine to find an answer. One good way to get help is to search on an error message. • Review support documents at help.ubuntu.com. • Contact a Ubuntu Linux users’ group. Post a query to the worldwide Usenet community as a last resort. If you are stuck on a Linux question and cannot find any other help, try submitting it to one of these newsgroups: • comp.os.linux.misc • alt.os.linux • comp.os.linux.networking • comp.os.linux.security • comp.os.linux.setup One way to find out about new tools and services is to read Usenet news. The comp.os.linux hierarchy is of particular interest to Linux users; for example, news about newly released software for Linux is posted to comp.os.linux.announce. Peo- ple often announce the availability of free software there, along with instructions on how to get a copy for your own use using anonymous FTP (page 735). Other tools to help you find resources, both old and new, exist on the network; see Appendix B. 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 pro- gram 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 may be located anywhere on your 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 remoteWWW: World Wide Web 391 FTP server and display the list of files it offers for anonymous FTP. Most commonly you use a WWW client to contact a WWW server, which offers support for the special fea- tures 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 informa- tion 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 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 per- form better and support more features than the Mosaic browser. Netscape Naviga- tor has enjoyed immense success and has become 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. Also, Netscape cre- ated Mozilla (mozilla.org) as an open-source browser project. These browsers provide GUIs that allow you to listen to sounds, watch Web events or live news reports, and display pictures as well as text, giving you access to hyper- media. A picture on your screen may be a link to more detailed, nonverbal informa- tion, such as a copy of the same picture at a higher resolution or a short animation. If your system can produce audio output, you can listen to audio clips that have been linked to a document. URL: Uniform Resource Locator Consider the URL http://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 (://). Fre- quently 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 rel- ative pathname to the file that contains the information (Consortium/siteindex). If you enter a URL in the location bar of a Web browser, the Web server returns the page, frequently an HTML (page 1040) 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 may never need to enter a URL. However, as more infor- mation is published in hypertext form, you cannot help but find URLs every- where—not just online in email messages and Usenet articles, but also in newspapers, in advertisements, and on product labels.392 Chapter 10 Networking and the Internet Browsers Mozilla (www.mozilla.org) is the open-source counterpart to Netscape. Mozilla, which was first released in March 1998, was based on Netscape 4 code. Since then, Mozilla has been under continuous development by employees of Netscape (now a division of AOL) 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 Epiphany (www.gnome.org/projects/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, listening to sounds, 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 pro- gram 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 that 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. Chapter Summary A Linux system attached to a network is probably communicating on an Ethernet, which may in turn be linked to other local area networks (LANs) and wide area net- works (WANs). Communication between LANs and WANs requires the use of gate- ways 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) and copy files quickly from one system to another (scp, ftp/sftp). Many tools that were originally designed to support communication on a single- host computer (for example, finger and talk) have since been extended to recognize network addresses, thus allowing users on different systems to interact with one another. Other features, such as the Network Filesystem (NFS), were created to extend the basic UNIX model and to simplify information sharing.Exercises 393 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 new tools and protocols have been created: ssh, scp, HTTPS, IPv6, firewall hardware and software, VPN, and so on. Many of these tools take advan- tage 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) are being discarded 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, often on a shared cable). The Internet Protocol (IP), the universal language of the Internet, has made it possi- ble for dissimilar computer systems around the world to readily communicate with one another. Technological advances continue to improve the performance of com- puter systems and the networks that link them. One way to gather information on the Internet is via Usenet. Many Linux users rou- tinely peruse Usenet news (netnews) to learn about the latest resources available for their systems. Usenet news is organized into newsgroups that cover a wide range of topics, computer-related and otherwise. To read Usenet news, you need to have access to a news server and the appropriate client software. Many modern email programs, such as Mozilla and Netscape, can display netnews. The rapid increase of network communication speeds in recent years has encour- aged 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 pro- vider. 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. Fire- fox 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 ssh394 Chapter 10 Networking and the Internet 2. Assuming rwho is disabled on the systems on your LAN, 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 a copy and download it? 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. Advanced Exercises 8. Suppose the link between routers 1 and 2 is down in the Internet shown in Figure 10-1 on page 360. What happens if someone at site C sends a mes- sage 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? 9. If you have a class B 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 num- ber) for the network starting at 131.204.18? 10. 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 class C–equivalent addresses would you need? How many sub- nets would you have left over from your allocation? 11. a. On your system, find two daemons running that are not listed in this chapter and explain what purpose they serve. a.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-4 on page 384 that you would consider adding?395395 11Chapter11Chapter 7 introduced the shells and Chapter 9 went into detail about the Bourne Again Shell. This chapter introduces additional Bourne Again Shell commands, builtins, and concepts that carry shell programming to a point where it can be useful. The first part of this chapter covers programming control structures, which are also known as control flow constructs. These struc- tures allow you to write scripts that can loop over command line arguments, make decisions based on the value of a variable, set up menus, and more. The Bourne Again Shell uses the same con- structs found in such high-level programming languages as C. Although you may make use of shell programming as a system administrator, reading this chapter is not required to perform system administration tasks. Feel free to skip this chapter and come back to it when you will find it most useful. The next part of this chapter discusses parameters and vari- ables, going into detail about array variables, local versus glo- bal variables, special parameters, and positional parameters. The exploration of builtin commands covers type, which dis- plays information about a command, and read, which allows In This Chapter Control Structures . . . . . . . . . . . . . 396 File Descriptors . . . . . . . . . . . . . . . 429 Parameters and Variables . . . . . . 432 Array Variables . . . . . . . . . . . . . . . 432 Locality of Variables . . . . . . . . . . . 434 Special Parameters. . . . . . . . . . . . 436 Positional Parameters. . . . . . . . . . 438 Builtin Commands . . . . . . . . . . . . 444 Expressions . . . . . . . . . . . . . . . . . . 458 Shell Programs . . . . . . . . . . . . . . . 466 A Recursive Shell Script . . . . . . . . 467 The quiz Shell Script. . . . . . . . . . . 470 11 Programming the Bourne Again Shell396 Chapter 11 Programming the Bourne Again Shell you to accept user input in a shell script. The section on the exec builtin demon- strates how exec provides an efficient way to execute a command by replacing a process and explains how you can use it to redirect input and output from within a script. The next section covers the trap builtin, which provides a way to detect and respond to operating system signals (such as that which is generated when you press CONTROL-C). The discussion of builtins concludes with a discussion of kill, which can abort a process, and getopts, which makes it easy to parse options for a shell script. (Table 11-6 on page 457 lists some of the more commonly used builtins.) Next the chapter examines arithmetic and logical expressions and the operators that work with them. The final section walks through the design and implementa- tion of two major shell scripts. This chapter contains many examples of shell programs. Although they illustrate certain concepts, most use information from earlier examples as well. This overlap not only reinforces your overall knowledge of shell programming but also demon- strates how you can combine commands to solve complex tasks. Running, modify- ing, and experimenting with the examples in this book is a good way to become comfortable with the underlying concepts. This chapter illustrates concepts with simple examples, which are followed by more complex ones in sections marked “Optional.” The more complex scripts illustrate tra- ditional shell programming practices and introduce some Linux utilities often used in scripts. You can skip these sections without loss of continuity the first time you read the chapter. Return to them later when you feel comfortable with the basic concepts. Control Structures The control flow commands alter the order of execution of commands within a shell script. Control structures include the if...then, for...in, while, until, and case state- ments. In addition, the break and continue statements work in conjunction with the control structures to alter the order of execution of commands within a script. if...then The if...then control structure has the following syntax: if test-command then commands fi Do not name a shell script test tip You can unwittingly create a problem if you give a shell script the name test because a Linux utility has the same name. Depending on how the PATH variable is set up and how you call the program, you may run your script or the utility, leading to confusing results.Control Structures 397 The bold words in the syntax description are the items you supply to cause the structure to have the desired effect. The nonbold words are the keywords the shell uses to identify the control structure. test builtin Figure 11-1 shows that the if statement tests the status returned by the test-command and transfers control based on this status. The end of the if structure is marked by a fi statement, (if spelled backward). The following script prompts for two words, reads them, and then uses an if structure to execute commands based on the result returned by the test builtin when it compares the two words. (See the test info page for information on the test utility, which is similar to the test builtin.) The test buil- tin returns a status of true if the two words are the same and false if they are not. Double quotation marks around $word1 and $word2 make sure that test works properly if you enter a string that contains a SPACE or other special character: $ cat if1 echo -n "word 1: " read word1 echo -n "word 2: " read word2 if test "$word1" = "$word2" then echo "Match" fi echo "End of program." Figure 11-1 An if...then flowchart then commands fi if test-command True False398 Chapter 11 Programming the Bourne Again Shell $ if1 word 1: peach word 2: peach Match End of program. In the preceding example the test-command is test "$word1" = "$word2". The test builtin returns a true status if its first and third arguments have the relationship specified by its second argument. If this command returns a true status (= 0), the shell executes the commands between the then and fi statements. If the command returns a false status (not = 0), the shell passes control to the statement following fi without executing the statements between then and fi. The effect of this if statement is to display Match if the two words are the same. The script always displays End of program. Builtins In the Bourne Again Shell, test is a builtin—part of the shell. It is also a stand-alone utility kept in /usr/bin/test. This chapter discusses and demonstrates many Bourne Again Shell builtins. You usually use the builtin version if it is available and the util- ity if it is not. Each version of a command may vary slightly from one shell to the next and from the utility to any of the shell builtins. See page 444 for more informa- tion on shell builtins. Checking arguments The next program uses an if structure at the beginning of a script to check that you have supplied at least one argument on the command line. The –eq test operator com- pares two integers, where the $# special parameter (page 439) takes on the value of the number of command line arguments. This structure displays a message and exits from the script with an exit status of 1 if you do not supply at least one argument: $ cat chkargs if test $# -eq 0 then echo "You must supply at least one argument." exit 1 fi echo "Program running." $ chkargs You must supply at least one argument. $ chkargs abc Program running. A test like the one shown in chkargs is a key component of any script that requires arguments. To prevent the user from receiving meaningless or confusing information from the script, the script needs to check whether the user has sup- plied the appropriate arguments. Sometimes the script simply tests whether argu- ments exist (as in chkargs). Other scripts test for a specific number or specific kinds of arguments. You can use test to ask a question about the status of a file argument or the relation- ship between two file arguments. After verifying that at least one argument has been given on the command line, the following script tests whether the argument is theControl Structures 399 name of an ordinary file (not a directory or other type of file) in the working direc- tory. The test builtin with the –f option and the first command line argument ($1) check the file: $ cat is_ordfile if test $# -eq 0 then echo "You must supply at least one argument." exit 1 fi if test -f "$1" then echo "$1 is an ordinary file in the working directory" else echo "$1 is NOT an ordinary file in the working directory" fi You can test many other characteristics of a file with test and various options. Table 11-1 lists some of these options. Other test options provide ways to test relationships between two files, such as whether one file is newer than another. Refer to later examples in this chapter for more detailed information. [] is a synonym for test The following example—another version of chkargs—checks for arguments in a way that is more traditional for Linux shell scripts. The example uses the bracket ([]) synonym for test. Rather than using the word test in scripts, you can surround the arguments to test with brackets. The brackets must be surrounded by white- space (SPACEs or TABs). Table 11-1 Options to the test builtin Option Tests file to see if it –d Exists and is a directory file –e Exists –f Exists and is an ordinary file (not a directory) –r Exists and is readable –s Exists and has a size greater than 0 bytes –w Exists and is writable –x Exists and is executable Always test the arguments tip To keep the examples in this book short and focused on specific concepts, the code to verify argu- ments is often omitted or abbreviated. It is a good practice to test arguments in shell programs that other people will use. Doing so results in scripts that are easier to run and debug.400 Chapter 11 Programming the Bourne Again Shell $ cat chkargs2 if [ $# -eq 0 ] then echo "Usage: chkargs2 argument..." 1>&2 exit 1 fi echo "Program running." exit 0 $ chkargs2 Usage: chkargs2 arguments $ chkargs2 abc Program running. Usage message The error message that chkargs2 displays is called a usage message and uses the 1>&2 notation to redirect its output to standard error (page 280). After issuing the usage message, chkargs2 exits with an exit status of 1, indicating that an error has occurred. The exit 0 command at the end of the script causes chkargs2 to exit with a 0 status after the program runs without an error. The Bourne Again Shell returns a 0 status if you omit the status code. The usage message is commonly employed to specify the type and number of argu- ments the script takes. Many Linux utilities provide usage messages similar to the one in chkargs2. If you call a utility or other program with the wrong number or kind of arguments, you will often see a usage message. Following is the usage mes- sage that cp displays when you call it without any arguments: $ cp cp: missing file argument Try 'cp --help' for more information. if...then...else The introduction of an else statement turns the if structure into the two-way branch shown in Figure 11-2. The if...then...else control structure has the following syntax: if test-command then commands else commands fi Because a semicolon (;) ends a command just as a NEWLINE does, you can place then on the same line as if by preceding it with a semicolon. (Because if and then are sepa- rate builtins, they require a command separator between them; a semicolon and NEW- LINE work equally well.) Some people prefer this notation for aesthetic reasons, while others like it because it saves space: if test-command; then commands else commands fiControl Structures 401 If the test-command returns a true status, the if structure executes the commands between the then and else statements and then diverts control to the statement fol- lowing fi. If the test-command returns a false status, the if structure executes the commands following the else statement. When you run the next script, named out, with arguments that are filenames, it dis- plays the files on the terminal. If the first argument is –v (called an option in this case), out uses less (page 148) to display the files one page at a time. After determin- ing that it was called with at least one argument, out tests its first argument to see whether it is –v. If the result of the test is true (if the first argument is –v), out uses the shift builtin to shift the arguments to get rid of the –v and displays the files using less. If the result of the test is false (if the first argument is not –v), the script uses cat to display the files: $ cat out if [ $# -eq 0 ] then echo "Usage: out [-v] filenames..." 1>&2 exit 1 fi if [ "$1" = "-v" ] then shift less -- "$@" else cat -- "$@" fi Figure 11-2 An if...then...else flowchart fi if test-commandTr ue False else commands then commands402 Chapter 11 Programming the Bourne Again Shell optional In out the –– argument to cat and less tells these utilities that no more options fol- low on the command line and not to consider leading hyphens (–) in the following list as indicating options. Thus –– allows you to view a file with a name that starts with a hyphen. Although not common, filenames beginning with a hyphen do occa- sionally occur. (You can create such a file by using the command cat > –fname.) The –– argument works with all Linux utilities that use the getopts builtin (page 454) to parse their options; it does not work with more and a few other utilities. This argu- ment is particularly useful when used in conjunction with rm to remove a file whose name starts with a hyphen (rm –– –fname), including any that you create while experimenting with the –– argument. Figure 11-3 An if...then...elif flowchart fi if test-commandTr ue Fa ls e else commands then commands elif test-commandTr u e Fa l se then commandsControl Structures 403 if...then...elif The if...then...elif control structure (Figure 11-3) has the following syntax: if test-command then commands elif test-command then commands . . . else commands fi The elif statement combines the else statement and the if statement and allows you to construct a nested set of if...then...else structures (Figure 11-3). The difference between the else statement and the elif statement is that each else statement must be paired with a fi statement, whereas multiple nested elif statements require only a sin- gle closing fi statement. The following example shows an if...then...elif control structure. This shell script com- pares three words that the user enters. The first if statement uses the Boolean operator AND (–a) as an argument to test. The test builtin returns a true status only if the first and second logical comparisons are true (that is, if word1 matches word2 and word2 matches word3). If test returns a true status, the script executes the command following the next then statement, passes control to the statement following fi, and terminates: $ cat if3 echo -n "word 1: " read word1 echo -n "word 2: " read word2 echo -n "word 3: " read word3 if [ "$word1" = "$word2" -a "$word2" = "$word3" ] then echo "Match: words 1, 2, & 3" elif [ "$word1" = "$word2" ] then echo "Match: words 1 & 2" elif [ "$word1" = "$word3" ] then echo "Match: words 1 & 3" elif [ "$word2" = "$word3" ] then echo "Match: words 2 & 3" else echo "No match" fi404 Chapter 11 Programming the Bourne Again Shell $ if3 word 1: apple word 2: orange word 3: pear No match $ if3 word 1: apple word 2: orange word 3: apple Match: words 1 & 3 $ if3 word 1: apple word 2: apple word 3: apple Match: words 1, 2, & 3 If the three words are not the same, the structure passes control to the first elif, which begins a series of tests to see if any pair of words is the same. As the nesting continues, if any one of the if statements is satisfied, the structure passes control to the next then statement and subsequently to the statement following fi. Each time an elif statement is not satisfied, the structure passes control to the next elif statement. The double quotation marks around the arguments to echo that con- tain ampersands (&) prevent the shell from interpreting the ampersands as special characters. optional The lnks Script The following script, named lnks, demonstrates the if...then and if...then...elif con- trol structures. This script finds hard links to its first argument, a filename. If you provide the name of a directory as the second argument, lnks searches for links in that directory and all subdirectories. If you do not specify a directory, lnks searches the working directory and its subdirectories. This script does not locate symbolic links. $ cat lnks #!/bin/bash # Identify links to a file # Usage: lnks file [directory] if [ $# -eq 0 -o $# -gt 2 ]; then echo "Usage: lnks file [directory]" 1>&2 exit 1 fi if [ -d "$1" ]; then echo "First argument cannot be a directory." 1>&2 echo "Usage: lnks file [directory]" 1>&2 exit 1 else file="$1" fiControl Structures 405 if [ $# -eq 1 ]; then directory="." elif [ -d "$2" ]; then directory="$2" else echo "Optional second argument must be a directory." 1>&2 echo "Usage: lnks file [directory]" 1>&2 exit 1 fi # Check that file exists and is an ordinary file: if [ ! -f "$file" ]; then echo "lnks: $file not found or special file" 1>&2 exit 1 fi # Check link count on file set -- $(ls -l "$file") linkcnt=$2 if [ "$linkcnt" -eq 1 ]; then echo "lnks: no other hard links to $file" 1>&2 exit 0 fi # Get the inode of the given file set $(ls -i "$file") inode=$1 # Find and print the files with that inode number echo "lnks: using find to search for links..." 1>&2 find "$directory" -xdev -inum $inode -print Alex has a file named letter in his home directory. He wants to find links to this file in his and other users’ home directory file trees. In the following example, Alex calls lnks from his home directory to perform the search. The second argument to lnks, /home, is the pathname of the directory he wants to start the search in. The lnks script reports that /home/alex/letter and /home/jenny/draft are links to the same file: $ lnks letter /home lnks: using find to search for links... /home/alex/letter /home/jenny/draft In addition to the if...then...elif control structure, lnks introduces other features that are commonly used in shell programs. The following discussion describes lnks sec- tion by section. Specify the shell The first line of the lnks script uses #! (page 284) to specify the shell that will exe- cute the script: #!/bin/bash406 Chapter 11 Programming the Bourne Again Shell In this chapter the #! notation appears only in more complex examples. It ensures that the proper shell executes the script, even when the user is running a different shell or the script is called from another shell script. Comments The second and third lines of lnks are comments; the shell ignores the text that fol- lows a pound sign up to the next NEWLINE character. These comments in lnks briefly identify what the file does and how to use it: # Identify links to a file # Usage: lnks file [directory] Usage messages The first if statement tests whether lnks was called with zero arguments or more than two arguments: if [ $# -eq 0 -o $# -gt 2 ]; then echo "Usage: lnks file [directory]" 1>&2 exit 1 fi If either of these conditions is true, lnks sends a usage message to standard error and exits with a status of 1. The double quotation marks around the usage message prevent the shell from interpreting the brackets as special characters. The brackets in the usage message indicate that the directory argument is optional. The second if statement tests whether the first command line argument ($1) is a directory (the –d argument to test returns a true value if the file exists and is a directory): if [ -d "$1" ]; then echo "First argument cannot be a directory." 1>&2 echo "Usage: lnks file [directory]" 1>&2 exit 1 else file="$1" fi If the first argument is a directory, lnks displays a usage message and exits. If it is not a directory, lnks saves the value of $1 in the file variable because later in the script set resets the command line arguments. If the value of $1 is not saved before the set command is issued, its value will be lost. Test the arguments The next section of lnks is an if...then...elif statement: if [ $# -eq 1 ]; then directory="." elif [ -d "$2" ]; then directory="$2" else echo "Optional second argument must be a directory." 1>&2 echo "Usage: lnks file [directory]" 1>&2 exit 1 fiControl Structures 407 The first test-command determines whether the user specified a single argument on the command line. If the test-command returns 0 (true), the user-created vari- able named directory is assigned the value of the working directory (.). If the test- command returns false, the elif statement tests whether the second argument is a directory. If it is a directory, the directory variable is set equal to the second com- mand line argument, $2. If $2 is not a directory, lnks sends a usage message to standard error and exits with a status of 1. The next if statement in lnks tests whether $file does not exist. This test keeps lnks from wasting time looking for links to a nonexistent file. The test builtin with the three arguments !, –f, and $file evaluates to true if the file $file does not exist: [ ! -f "$file" ] The ! operator preceding the –f argument to test negates its result, yielding false if the file $file does exist and is an ordinary file. Next lnks uses set and ls –l to check the number of links $file has: # Check link count on file set -- $(ls -l "$file") linkcnt=$2 if [ "$linkcnt" -eq 1 ]; then echo "lnks: no other hard links to $file" 1>&2 exit 0 fi The set builtin uses command substitution (page 344) to set the positional parame- ters to the output of ls –l. The second field in this output is the link count, so the user-created variable linkcnt is set equal to $2. The –– used with set prevents set from interpreting as an option the first argument produced by ls –l (the first argu- ment is the access permissions for the file and typically begins with –). The if state- ment checks whether $linkcnt is equal to 1; if it is, lnks displays a message and exits. Although this message is not truly an error message, it is redirected to stan- dard error. The way lnks has been written, all informational messages are sent to standard error. Only the final product of lnks—the pathnames of links to the speci- fied file—is sent to standard output, so you can redirect the output as you please. If the link count is greater than one, lnks goes on to identify the inode (page 1041) for $file. As explained on page 212, comparing the inodes associated with filenames is a good way to determine whether the filenames are links to the same file. The lnks script uses set to set the positional parameters to the output of ls –i. The first argu- ment to set is the inode number for the file, so the user-created variable named inode is assigned the value of $1: # Get the inode of the given file set $(ls -i "$file") inode=$1408 Chapter 11 Programming the Bourne Again Shell Finally lnks uses the find utility to search for files having inode numbers that match $inode: # Find and print the files with that inode number echo "lnks: using find to search for links..." 1>&2 find "$directory" -xdev -inum $inode -print The find utility searches for files that meet the criteria specified by its arguments, beginning its search with the directory specified by its first argument ($directory) and searching all subdirectories. The remaining arguments specify that the file- names of files having inodes matching $inode should be sent to standard output. Because files in different filesystems can have the same inode number and not be linked, find must search only directories in the same filesystem as $directory. The –xdev argument prevents find from searching directories on other filesystems. Refer to page 209 for more information about filesystems and links. The echo command preceding the find command in lnks, which tells the user that find is running, is included because find frequently takes a long time to run. Because lnks does not include a final exit statement, the exit status of lnks is that of the last command it runs, find. Debugging Shell Scripts When you are writing a script such as lnks, it is easy to make mistakes. You can use the shell’s –x option to help debug a script. This option causes the shell to display each command before it runs the command. Tracing a script’s execution in this way can give you information about where a problem lies. You can run lnks as in the previous example and cause the shell to display each command before it is executed. Either set the –x option for the current shell (set –x) so that all scripts display commands as they are run or use the –x option to affect only the shell that is running the script called by the command line. $ bash -x lnks letter /home + '[' 2 -eq 0 -o 2 -gt 2 ']' + '[' -d letter ']' + file=letter + '[' 2 -eq 1 ']' + '[' -d /home ']' + directory=/home + '[' '!' -f letter ']' ... PS4 Each command that the script executes is preceded by the value of the PS4 vari- able—a plus sign (+) by default, so you can distinguish debugging output from script-produced output. You must export PS4 if you set it in the shell that calls the script. The next command sets PS4 to >>>> followed by a SPACE and exports it: $ export PS4='>>>> 'Control Structures 409 You can also set the –x option of the shell running the script by putting the follow- ing set command at the top of the script: set -x Put set –x anywhere in the script you want to turn debugging on. Turn the debug- ging option off with a plus sign. set +x The set –o xtrace and set +o xtrace commands do the same things as set –x and set +x, respectively. for...in The for...in control structure has the following syntax: for loop-index in argument-list do commands done Figure 11-4 A for...in flowchart Assign next argument in argument-list to loop-index do commands Another argument in argument-list done Ye s No ?410 Chapter 11 Programming the Bourne Again Shell The for...in structure (Figure 11-4, previous page) assigns the value of the first argu- ment in the argument-list to the loop-index and executes the commands between the do and done statements. The do and done statements mark the beginning and end of the for loop. After it passes control to the done statement, the structure assigns the value of the second argument in the argument-list to the loop-index and repeats the commands. The structure repeats the commands between the do and done statements one time for each argument in the argument-list. When the structure exhausts the argument- list, it passes control to the statement following done. The following for...in structure assigns apples to the user-created variable fruit and then displays the value of fruit, which is apples. Next the structure assigns oranges to fruit and repeats the process. When it exhausts the argument list, the structure transfers control to the statement following done, which displays a message. $ cat fruit for fruit in apples oranges pears bananas do echo "$fruit" done echo "Task complete." $ fruit apples oranges pears bananas Task complete. The next script lists the names of the directory files in the working directory by looping over all the files, using test to determine which files are directories: $ cat dirfiles for i in * do if [ -d "$i" ] then echo "$i" fi done The ambiguous file reference character * matches the names of all files (except hid- den files) in the working directory. Prior to executing the for loop, the shell expands the * and uses the resulting list to assign successive values to the index variable i. for The for control structure has the following syntax: for loop-index do commands doneControl Structures 411 In the for structure the loop-index takes on the value of each of the command line arguments, one at a time. It is the same as the for...in structure (Figure 11-4) except for where it gets values for the loop-index. The for structure performs a sequence of commands, usually involving each argument in turn. The following shell script shows a for structure displaying each command line argu- ment. The first line of the script, for arg, implies for arg in "$@", where the shell expands "$@" into a list of quoted command line arguments "$1" "$2" "$3" and so on. The balance of the script corresponds to the for...in structure. $ cat for_test for arg do echo "$arg" done $ for_test candy gum chocolate candy gum chocolate optional The whos Script The following script, named whos, demonstrates the usefulness of the implied "$@" in the for structure. You give whos one or more users’ full names or usernames as arguments, and whos displays information about the users. The whos script gets the information it displays from the first and fifth fields in the /etc/passwd file. The first field always contains a username, and the fifth field typically contains the user’s full name. You can provide a username as an argument to whos to identify the user’s name or provide a name as an argument to identify the username. The whos script is similar to the finger utility, although whos delivers less information. $ cat whos #!/bin/bash # adapted from finger.sh by Lee Sailer # UNIX/WORLD, III:11, p. 67, Fig. 2 if [ $# -eq 0 ] then echo "Usage: whos id..." 1>&2 exit 1 fi for id do gawk -F: '{print $1, $5}' /etc/passwd | grep -i "$id" done Below whos identifies the user whose username is chas and the user whose name is Marilou Smith: $ whos chas "Marilou Smith" chas Charles Casey msmith Marilou Smith412 Chapter 11 Programming the Bourne Again Shell Use of "$@" The whos script uses a for statement to loop through the command line arguments. In this script the implied use of "$@" in the for loop is particularly beneficial because it causes the for loop to treat an argument that contains a SPACE as a single argument. This example quotes Marilou Smith, which causes the shell to pass it to the script as a single argument. Then the implied "$@" in the for statement causes the shell to regenerate the quoted argument Marilou Smith so that it is again treated as a single argument. gawk For each command line argument, whos searches the /etc/passwd file. Inside the for loop the gawk utility extracts the first ($1) and fifth ($5) fields from the lines in /etc/passwd. The –F: option causes gawk to use a colon (:) as a field separator when it reads /etc/passwd, allowing it to break each line into fields. The gawk command sets and uses the $1 and $5 arguments; they are included within single quotation marks and are not interpreted by the shell. Do not confuse these arguments with positional parameters, which correspond to command line arguments. The first and fifth fields are sent to grep (page 151) via a pipe. The grep utility searches for $id (which has taken on the value of a command line argument) in its input. The –i option causes grep to ignore case as it searches; grep displays each line in its input that contains $id. | at the end of a line An interesting syntactical exception that bash gives the pipe symbol (|) appears on the line with the gawk command: You do not have to quote a NEWLINE that immedi- ately follows a pipe symbol (that is, a pipe symbol that is the last thing on a line) to keep the NEWLINE from executing a command. Try giving the command who | and pressing RETURN. The shell displays a secondary prompt. If you then enter sort fol- lowed by another RETURN, you see a sorted who list. The pipe works even though a NEWLINE follows the pipe symbol. while The while control structure has the following syntax: while test-command do commands done As long as the test-command (Figure 11-5) returns a true exit status, the while structure continues to execute the series of commands delimited by the do and done statements. Before each loop through the commands, the structure executes the test- command. When the exit status of the test-command is false, the structure passes control to the statement after the done statement. test builtin The following shell script first initializes the number variable to zero. The test builtin then determines whether number is less than 10. The script uses test with the –lt argument to perform a numerical test. For numerical comparisons, you must use –ne (not equal), –eq (equal), –gt (greater than), –ge (greater than or equal to), –lt (less than), or –le (less than or equal to). For string comparisons use = (equal) or != (not equal) when you are working with test. In this example, test has an exit status of 0 (true) as long as number is less than 10. As long as test returns true, the structureControl Structures 413 executes the commands between the do and done statements. See page 397 for infor- mation on the test utility, which is very similar to the test builtin. $ cat count #!/bin/bash number=0 while [ "$number" -lt 10 ] do echo -n "$number" ((number +=1)) done echo $ count 0123456789 $ The echo command following do displays number. The –n prevents echo from issu- ing a NEWLINE following its output. The next command uses arithmetic evaluation [((...)); page 458] to increment the value of number by 1. The done statement termi- nates the loop and returns control to the while statement to start the loop over again. The final echo causes count to send a NEWLINE character to standard output, so that the next prompt occurs in the leftmost column on the display (rather than immediately following 9). optional The spell_check Script The aspell utility checks the words in a file against a dictionary of correctly spelled words. With the –l option, aspell runs in list mode: Input comes from standard input and aspell sends each potentially misspelled word to standard output. The following command produces a list of possible misspellings in the file letter.txt: $ aspell -l < letter.txt quikly portible frendly Figure 11-5 A while flowchart while test-command do commands doneFalse Tr ue414 Chapter 11 Programming the Bourne Again Shell The next shell script, named spell_check, shows another use of a while structure. To find the incorrect spellings in a file, you can use spell_check, which calls aspell to check a file against a system dictionary but goes a step further: It enables you to specify a list of correctly spelled words and removes these words from the output of aspell. This script is useful for removing words that you use frequently, such as names and technical terms, that are not in a standard dictionary. Although you can duplicate the functionality of spell_check by using additional aspell dictionaries, the script is included here for its instructive value. The spell_check script requires two filename arguments: a file containing the list of correctly spelled words and a file that you want to check. The first if statement ver- ifies that the user specified two arguments. The next two if statements verify that both arguments are readable files. (The exclamation point negates the sense of the following operator; the –r operator causes test to determine whether a file is read- able. The result is a test that determines whether a file is not readable.) $ cat spell_check #!/bin/bash # remove correct spellings from aspell output if [ $# -ne 2 ] then echo "Usage: spell_check file1 file2" 1>&2 echo "file1: list of correct spellings" 1>&2 echo "file2: file to be checked" 1>&2 exit 1 fi if [ ! -r "$1" ] then echo "spell_check: $1 is not readable" 1>&2 exit 1 fi if [ ! -r "$2" ] then echo "spell_check: $2 is not readable" 1>&2 exit 1 fi aspell -l < "$2" | while read line do if ! grep "^$line$" "$1" > /dev/null then echo $line fi done The spell_check script sends the output from aspell (with the –l option so that it produces a list of misspelled words on standard output) through a pipe to standard input of a while structure, which reads one line at a time (each line has one word onControl Structures 415 it) from standard input. The test-command (that is, read line) returns a true exit sta- tus as long as it receives a line from standard input. Inside the while loop an if statement1 monitors the return value of grep, which deter- mines whether the line that was read is in the user’s list of correctly spelled words. The pattern that grep searches for (the value of $line) is preceded and followed by special characters that specify the beginning and end of a line (^ and $, respectively). These special characters ensure that grep finds a match only if the $line variable matches an entire line in the file of correctly spelled words. (Otherwise, grep would match a string, such as paul, in the output of aspell if the file of correctly spelled words contained the word paulson.) These special characters, together with the value of the $line variable, form a regular expression (Appendix A). The output of grep is redirected to /dev/null (page 233) because the output is not needed; only the exit code is important. The if statement checks the negated exit sta- tus of grep (the leading exclamation point negates or changes the sense of the exit status—true becomes false, and vice versa), which is 0 or true (false when negated) when a matching line is found. If the exit status is not 0 or false (true when negated), the word was not in the file of correctly spelled words. The echo builtin sends a list of words that are not in the file of correctly spelled words to standard output. Once it detects the EOF (end of file), the read builtin returns a false exit status. Con- trol then passes out of the while structure, and the script terminates. Before you use spell_check, create a file of correct spellings containing words that you use frequently but that are not in a standard dictionary. For example, if you work for a company named Blinkenship and Klimowski, Attorneys, you would put Blinkenship and Klimowski into the file. The following example shows how spell_check checks the spelling in a file named memo and removes Blinkenship and Klimowski from the output list of incorrectly spelled words: $ aspell -l < memo Blinkenship Klimowski targat hte $ cat word_list Blinkenship Klimowski $ spell_check word_list memo targat hte Refer to the aspell manual (in the /usr/share/doc/aspell directory or at aspell.net) for more information. 1. This if statement can also be written as if ! grep -qw "$line" "$1" The –q option suppresses the output from grep so that only an exit code is returned. The –w option causes grep to match only a whole word.416 Chapter 11 Programming the Bourne Again Shell until The until and while structures are very similar, differing only in the sense of the test performed at the top of the loop. Figure 11-6 shows that until continues to loop until the test-command returns a true exit status. The while structure loops while the test-command continues to return a true or nonerror condition. The until con- trol structure has the following syntax: until test-command do commands done The following script demonstrates an until structure that includes read. When the user enters the correct string of characters, the test-command is satisfied and the structure passes control out of the loop. $ cat until1 secretname=jenny name=noname echo "Try to guess the secret name!" echo until [ "$name" = "$secretname" ] do echo -n "Your guess: " read name done echo "Very good." $ until1 Try to guess the secret name! Your guess: helen Your guess: barbara Your guess: rachael Your guess: jenny Very good Figure 11-6 An until flowchart until test-command do commands done False Tr u eControl Structures 417 The following locktty script is similar to the lock command on Berkeley UNIX and the Lock Screen menu selection in GNOME. The script prompts you for a key (password) and uses an until control structure to lock the terminal. The until state- ment causes the system to ignore any characters typed at the keyboard until the user types in the key on a line by itself, which unlocks the terminal. The locktty script can keep people from using your terminal while you are away from it for short peri- ods of time. It saves you from having to log out if you are concerned about other users using your login. $ cat locktty #! /bin/bash # UNIX/WORLD, III:4 trap '' 1 2 3 18 stty -echo echo -n "Key: " read key_1 echo echo -n "Again: " read key_2 echo key_3= if [ "$key_1" = "$key_2" ] then tput clear until [ "$key_3" = "$key_2" ] do read key_3 done else echo "locktty: keys do not match" 1>&2 fi stty echo trap builtin The trap builtin (page 451) at the beginning of the locktty script stops a user from being able to terminate the script by sending it a signal (for example, by pressing the interrupt key). Trapping signal 18 means that no one can use CONTROL-Z (job control, a stop from a tty) to defeat the lock. (See Table 11-5 on page 451 for a list of signals.) The stty –echo command causes the terminal not to display characters typed at the keyboard, thereby preventing the key that the user enters from appearing on the screen. After turning off keyboard echo, the script prompts the user for a key, reads it into the user-created variable key_1, prompts the user to enter the same key again, and saves it in key_2. The statement key_3= creates a variable with a NULL value. If key_1 and key_2 match, locktty clears the screen (with the tput command) and starts an until loop. The until loop keeps attempting to read from the terminal and Forget your password for locktty? tip If you forget your key (password), you will need to log in from another (virtual) terminal and kill the process running locktty.418 Chapter 11 Programming the Bourne Again Shell assigning the input to the key_3 variable. Once the user types in a string that matches one of the original keys (key_2), the until loop terminates and keyboard echo is turned on again. break and continue You can interrupt a for, while, or until loop by using a break or continue statement. The break statement transfers control to the statement after the done statement, which terminates execution of the loop. The continue command transfers control to the done statement, which continues execution of the loop. The following script demonstrates the use of these two statements. The for...in structure loops through the values 1–10. The first if statement executes its com- mands when the value of the index is less than or equal to 3 ($index –le 3). The second if statement executes its commands when the value of the index is greater than or equal to 8 ($index –ge 8). In between the two ifs, echo displays the value of the index. For all values up to and including 3, the first if statement displays continue and executes a continue statement that skips echo $index and the second if statement and continues with the next for statement. For the value of 8, the sec- ond if statement displays break and executes a break statement that exits from the for loop: $ cat brk for index in 1 2 3 4 5 6 7 8 9 10 do if [ $index -le 3 ] ; then echo "continue" continue fi # echo $index # if [ $index -ge 8 ] ; then echo "break" break fi done $ brk continue continue continue 4 5 6 7 8 breakControl Structures 419 case The case structure (Figure 11-7, page 420) is a multiple-branch decision mecha- nism. The path taken through the structure depends on a match or lack of a match between the test-string and one of the patterns. The case control structure has the following syntax: case test-string in pattern-1) commands-1 ;; pattern-2) commands-2 ;; pattern-3) commands-3 ;; . . . esac The following case structure examines the character that the user enters as the test- string. This value is held in the variable letter. If the test-string has a value of A, the structure executes the command following the pattern A. The right parenthesis is part of the case control structure, not part of the pattern. If the test-string has a value of B or C, the structure executes the command following the matching pat- tern. The asterisk (*) indicates any string of characters and serves as a catchall in case there is no match. If no pattern matches the test-string and if there is no catch- all (*) pattern, control passes to the command following the esac statement, with- out the case structure taking any action. $ cat case1 echo -n "Enter A, B, or C: " read letter case "$letter" in A) echo "You entered A" ;; B) echo "You entered B" ;; C) echo "You entered C" ;; *) echo "You did not enter A, B, or C" ;; esac $ case1 Enter A, B, or C: B You entered B420 Chapter 11 Programming the Bourne Again Shell The next execution of case1 shows the user entering a lowercase b. Because the test- string b does not match the uppercase B pattern (or any other pattern in the case statement), the program executes the commands following the catchall pattern and displays a message: $ case1 Enter A, B, or C: b You did not enter A, B, or C The pattern in the case structure is analogous to an ambiguous file reference. It can include any of the special characters and strings shown in Table 11-2. The next script accepts both uppercase and lowercase letters: Figure 11-7 A case flowchart case esac test-string = pattern-1 ? test-string = pattern-2 ? test-string = pattern-3 ? commands-1 commands-2 commands-3Control Structures 421 $ cat case2 echo -n "Enter A, B, or C: " read letter case "$letter" in a|A) echo "You entered A" ;; b|B) echo "You entered B" ;; c|C) echo "You entered C" ;; *) echo "You did not enter A, B, or C" ;; esac $ case2 Enter A, B, or C: b You entered B optional The following example shows how you can use the case structure to create a simple menu. The command_menu script uses echo to present menu items and prompt the user for a selection. (The select control structure [page 425] makes it much easier to code a menu.) The case structure then executes the appropriate utility depending on the user’s selection. $ cat command_menu #!/bin/bash # menu interface to simple commands echo -e "\n COMMAND MENU\n" echo " a. Current date and time" echo " b. Users currently logged in" echo " c. Name of the working directory" echo -e " d. Contents of the working directory\n" echo -n "Enter a, b, c, or d: " read answer echo Table 11-2 Patterns Pattern Function * Matches any string of characters. Use for the default case. ? Matches any single character. [...] Defines a character class. Any characters enclosed within brackets are tried, one at a time, in an attempt to match a single character. A hyphen between two characters specifies a range of characters. | Separates alternative choices that satisfy a particular branch of the case structure.422 Chapter 11 Programming the Bourne Again Shell # case "$answer" in a) date ;; b) who ;; c) pwd ;; d) ls ;; *) echo "There is no selection: $answer" ;; esac $ command_menu COMMAND MENU a. Current date and time b. Users currently logged in c. Name of the working directory d. Contents of the working directory Enter a, b, c, or d: a Wed Jan 2 12:31:12 PST 2008 echo –e The –e option causes echo to interpret \n as a NEWLINE character. If you do not include this option, echo does not output the extra blank lines that make the menu easy to read but instead outputs the (literal) two-character sequence \n. The –e option causes echo to interpret several other backslash-quoted characters (Table 11-3). Remember to quote (i.e., place double quotation marks around the string) the backslash-quoted character so that the shell does not interpret it but passes the backslash and the char- acter to echo. See xpg_echo (page 337) for a way to avoid using the –e option. Table 11-3 Special characters in echo (must use –e) Quoted character echo displays \a Alert (bell) \b BACKSPACE \c Suppress trailing NEWLINE \f FORMFEED \n NEWLINEControl Structures 423 You can also use the case control structure to take various actions in a script, depending on how many arguments the script is called with. The following script, named safedit, uses a case structure that branches based on the number of com- mand line arguments ($#). It saves a backup copy of a file you are editing with vim. $ cat safedit #!/bin/bash # UNIX/WORLD, IV:11 PATH=/bin:/usr/bin script=$(basename $0) case $# in 0) vim exit 0 ;; 1) if [ ! -f "$1" ] then vim "$1" exit 0 fi if [ ! -r "$1" -o ! -w "$1" ] then echo "$script: check permissions on $1" 1>&2 exit 1 else editfile=$1 fi if [ ! -w "." ] then echo "$script: backup cannot be " \ "created in the working directory" 1>&2 exit 1 fi ;; *) echo "Usage: $script [file-to-edit]" 1>&2 exit 1 ;; esac \r RETURN \t Horizontal TAB \v Vertical TAB \\ Backslash \nnn The character with the ASCII octal code nnn; if nnn is not valid, echo displays the string literally Table 11-3 Special characters in echo (must use –e) (continued)424 Chapter 11 Programming the Bourne Again Shell tempfile=/tmp/$$.$script cp $editfile $tempfile if vim $editfile then mv $tempfile bak.$(basename $editfile) echo "$script: backup file created" else mv $tempfile editerr echo "$script: edit error--copy of " \ "original file is in editerr" 1>&2 fi If you call safedit without any arguments, the case structure executes its first branch and calls vim without a filename argument. Because an existing file is not being edited, safedit does not create a backup file. If you call safedit with one argument, it runs the commands in the second branch of the case structure and verifies that the file specified by $1 does not yet exist or is the name of a file for which the user has read and write permission. The safedit script also verifies that the user has write permission for the working directory. If the user calls safedit with more than one argument, the third branch of the case structure presents a usage message and exits with a status of 1. Set PATH In addition to using a case structure for branching based on the number of com- mand line arguments, the safedit script introduces several other features. First, at the beginning of the script, the PATH variable is set to search /bin and /usr/bin. Set- ting PATH in this way ensures that the commands executed by the script are stan- dard utilities, which are kept in those directories. By setting PATH inside a script, you can avoid the problems that might occur if users have set PATH to search their own directories first and have scripts or programs with the same names as the utili- ties the script calls. You can also include absolute pathnames within a script to achieve this end, but this practice can make a script less portable. Name of the program In a second safedit feature, the following line creates a variable named script and assigns the simple filename of the script to it: script=$(basename $0) The basename utility sends the simple filename component of its argument to stan- dard output, which is assigned to the script variable, using command substitution. The $0 holds the command the script was called with (page 439). No matter which of the following commands the user calls the script with, the output of basename is the simple filename safedit: $ /home/alex/bin/safedit memo $ ./safedit memo $ safedit memo After the script variable is set, it replaces the filename of the script in usage and error messages. By using a variable that is derived from the command that invoked the script rather than a filename that is hardcoded into the script, you can createControl Structures 425 links to the script or rename it, and the usage and error messages will still provide accurate information. Naming temporary files A third significant feature of safedit relates to the use of the $$ variable in the name of a temporary file. The statement following the esac statement creates and assigns a value to the tempfile variable. This variable contains the name of a temporary file that is stored in the /tmp directory, as are many temporary files. The temporary file- name begins with the PID number of the shell and ends with the name of the script. Use of the PID number ensures that the filename is unique, and safedit will not attempt to overwrite an existing file, as might happen if two people were using safedit at the same time. The name of the script is appended so that, should the file be left in /tmp for some reason, you can figure out where it came from. The PID number is used in front of—rather than after—$script in the filename because of the 14-character limit placed on filenames by some older versions of UNIX. Linux systems do not have this limitation. Because the PID number ensures the uniqueness of the filename, it is placed first so that it cannot be truncated. (If the $script component is truncated, the filename is still unique.) For the same reason, when a backup file is created inside the if control structure a few lines down in the script, the filename is composed of the string bak. followed by the name of the file being edited. On an older system, if bak were used as a suffix rather than a prefix and the original filename were 14 characters long, .bak might be lost and the origi- nal file would be overwritten. The basename utility extracts the simple filename of $editfile before it is prefixed with bak. Fourth, safedit uses an unusual test-command in the if structure: vim $editfile. The test-command calls vim to edit $editfile. When you finish editing the file and exit from vim, vim returns an exit code. The if control structure uses that exit code to determine which branch to take. If the editing session completed successfully, vim returns 0 and the statements following the then statement are executed. If vim does not terminate normally (as would occur if the user killed [page 522] the vim pro- cess), vim returns a nonzero exit status and the script executes the statements fol- lowing else. select The select control structure is based on the one found in the Korn Shell. It displays a menu, assigns a value to a variable based on the user’s choice of items, and executes a series of commands. The select control structure has the following syntax: select varname [in arg . . . ] do commands done The select structure displays a menu of the arg items. If you omit the keyword in and the list of arguments, select uses the positional parameters in place of the arg426 Chapter 11 Programming the Bourne Again Shell items. The menu is formatted with numbers before each item. For example, a select structure that begins with select fruit in apple banana blueberry kiwi orange watermelon STOP displays the following menu: 1) apple 3) blueberry 5) orange 7) STOP 2) banana 4) kiwi 6) watermelon The select structure uses the values of the LINES and COLUMNS variables to determine the size of the display. (LINES has a default value of 24; COLUMNS has a default value of 80.) With COLUMNS set to 20, the menu looks like this: 1) apple 2) banana 3) blueberry 4) kiwi 5) orange 6) watermelon 7) STOP PS3 After displaying the menu select displays the value of PS3, the special select prompt. The default value of PS3 is ?# but you typically set PS3 to a more meaningful value. When you enter a valid number (one in the menu range) in response to the PS3 prompt, select sets varname to the argument corresponding to the number you entered. If you make an invalid entry, varname is set to null. Either way select stores your response in the keyword variable REPLY and then executes the commands between do and done. If you press RETURN without entering a choice, the shell redis- plays the menu and the PS3 prompt. The select structure continues to issue the PS3 prompt and execute the commands until something causes it to exit—typically a break or exit statement. A break state- ment exits from the loop and an exit statement exits from the script. The following script illustrates the use of select: $ cat fruit2 #!/bin/bash PS3="Choose your favorite fruit from these possibilities: " select FRUIT in apple banana blueberry kiwi orange watermelon STOP do if [ "$FRUIT" == "" ]; then echo -e "Invalid entry.\n" continue elif [ $FRUIT = STOP ]; then echo "Thanks for playing!" break fi echo "You chose $FRUIT as your favorite." echo -e "That is choice number $REPLY.\n" doneControl Structures 427 $ fruit2 1) apple 3) blueberry 5) orange 7) STOP 2) banana 4) kiwi 6) watermelon Choose your favorite fruit from these possibilities: 3 You chose blueberry as your favorite. That is choice number 3. Choose your favorite fruit from these possibilities: 99 Invalid entry. Choose your favorite fruit from these possibilities: 7 Thanks for playing! After setting the PS3 prompt and establishing the menu with the select statement, fruit2 executes the commands between do and done. If the user makes an invalid entry, the shell sets varname ($FRUIT) to a null value, so fruit2 first tests whether $FRUIT is null. If it is, echo displays an error and continue causes the shell to redis- play the PS3 prompt. If the entry is valid, the script tests whether the user wants to stop. If so, echo displays a message and break exits from the select structure (and from the script). If the user entered a valid response and does not want to stop, the script displays the name and number of the user’s response. (See page 422 for infor- mation about the –e option to echo.) Here Document A Here document allows you to redirect input to a shell script from within the shell script itself. A Here document is so called because it is here—immediately accessible in the shell script—instead of there, perhaps in another file. The following script, named birthday, contains a Here document. The two less than (<<) symbols in the first line indicate that a Here document follows. One or more characters that delimit the Here document follow the less than symbols—this exam- ple uses a plus sign. Whereas the opening delimiter must appear adjacent to the less than symbols, the closing delimiter must be on a line by itself. The shell sends every- thing between the two delimiters to the process as standard input. In the example it is as though you had redirected standard input to grep from a file, except that the file is embedded in the shell script: $ cat birthday grep -i "$1" <<+ Alex June 22 Barbara February 3 Darlene May 8 Helen March 13 Jenny January 23 Nancy June 26 + $ birthday Jenny Jenny January 23 $ birthday june Alex June 22 Nancy June 26428 Chapter 11 Programming the Bourne Again Shell When you run birthday, it lists all the Here document lines that contain the argu- ment you called it with. In this case the first time birthday is run, it displays Jenny’s birthday because it is called with an argument of Jenny. The second run displays all the birthdays in June. The –i argument causes grep’s search not to be case sensitive. optional The next script, named bundle,2 includes a clever use of a Here document. The bundle script is an elegant example of a script that creates a shell archive (shar) file. The script, shown following, creates a file that is itself a shell script containing sev- eral other files as well as the code to re-create the original files. $ cat bundle #!/bin/bash # bundle: group files into distribution package echo "# To unbundle, bash this file" for i do echo "echo $i 1>&2" echo "cat >$i <<'End of $i'" cat $i echo "End of $i" done Just as the shell does not treat special characters that occur in standard input of a shell script as special, so the shell does not treat the special characters that occur between the delimiters in a Here document as special. As the following example shows, the output of bundle is a shell script, which is redi- rected to a file named bothfiles. It contains the contents of each file given as an argument to bundle (file1 and file2 in this case) inside a Here document. To extract the original files from bothfiles, you simply run it as an argument to a bash com- mand. Before each Here document is a cat command that causes the Here document to be written to a new file when bothfiles is run: $ cat file1 This is a file. It contains two lines. $ cat file2 This is another file. It contains three lines. $ bundle file1 file2 > bothfiles $ cat bothfiles # To unbundle, bash this file echo file1 1>&2 cat >file1 <<'End of file1' 2. Thanks to Brian W. Kernighan and Rob Pike, The Unix Programming Environment (Englewood Cliffs, N.J.: Prentice-Hall, 1984), 98. Reprinted with permission.File Descriptors 429 This is a file. It contains two lines. End of file1 echo file2 1>&2 cat >file2 <<'End of file2' This is another file. It contains three lines. End of file2 In the next example, file1 and file2 are removed before bothfiles is run. The both- files script echoes the names of the files it creates as it creates them. The ls command then shows that bothfiles has re-created file1 and file2: $ rm file1 file2 $ bash bothfiles file1 file2 $ ls bothfiles file1 file2 File Descriptors As discussed on page 280, before a process can read from or write to a file it must open that file. When a process opens a file, Linux associates a number (called a file descriptor) with the file. Each process has its own set of open files and its own file descriptors. After opening a file, a process reads from and writes to that file by referring to its file descriptor. When it no longer needs the file, the process closes the file, freeing the file descriptor. A typical Linux process starts with three open files: standard input (file descriptor 0), standard output (file descriptor 1), and standard error (file descriptor 2). Often those are the only files the process needs. Recall that you redirect standard output with the symbol > or the symbol 1> and that you redirect standard error with the symbol 2>. Although you can redirect other file descriptors, because file descriptors other than 0, 1, and 2 do not have any special conventional meaning, it is rarely useful to do so. The exception is in programs that you write yourself, in which case you control the meaning of the file descriptors and can take advantage of redirection. Opening a file descriptor The Bourne Again Shell opens files using the exec builtin as follows: exec n> outfile exec m< infile The first line opens outfile for output and holds it open, associating it with file descriptor n. The second line opens infile for input and holds it open, associating it with file descriptor m.430 Chapter 11 Programming the Bourne Again Shell Duplicating a file descriptor The <& token duplicates an input file descriptor; use >& to duplicate an output file descriptor. You can duplicate a file descriptor by making it refer to the same file as another open file descriptor, such as standard input or output. Use the following format to open or redirect file descriptor n as a duplicate of file descriptor m: exec n<&m Once you have opened a file, you can use it for input and output in two different ways. First, you can use I/O redirection on any command line, redirecting standard output to a file descriptor with >&n or redirecting standard input from a file descrip- tor with <&n. Second, you can use the read (page 445) and echo builtins. If you invoke other commands, including functions (page 331), they inherit these open files and file descriptors. When you have finished using a file, you can close it with exec n<&– When you invoke the shell function in the next example, named mycp, with two arguments, it copies the file named by the first argument to the file named by the second argument. If you supply only one argument, the script copies the file named by the argument to standard output. If you invoke mycp with no arguments, it cop- ies standard input to standard output. function mycp () { case $# in 0) # zero arguments # file descriptor 3 duplicates standard input # file descriptor 4 duplicates standard output exec 3<&0 4<&1 ;; 1) # one argument # open the file named by the argument for input # and associate it with file descriptor 3 # file descriptor 4 duplicates standard output exec 3< $1 4<&1 ;; 2) # two arguments # open the file named by the first argument for input # and associate it with file descriptor 3 # open the file named by the second argument for output # and associate it with file descriptor 4 exec 3< $1 4> $2 ;; A function is not a shell script tip The mycp example is a shell function; it will not work as you expect if you execute it as a shell script. (It will work: The function will be created in a very short-lived subshell, which is probably of little use.) You can enter this function from the keyboard. If you put the function in a file, you can run it as an argument to the . (dot) builtin (page 279). You can also put the function in a startup file if you want it to be always available (page 333).File Descriptors 431 *) echo "Usage: mycp [source [dest]]" return 1 ;; esac # call cat with input coming from file descriptor 3 # and output going to file descriptor 4 cat <&3 >&4 # close file descriptors 3 and 4 exec 3<&- 4<&- } The real work of this function is done in the line that begins with cat. The rest of the script arranges for file descriptors 3 and 4, which are the input and output of the cat command, to be associated with the appropriate files. optional The next program takes two filenames on the command line, sorts both, and sends the output to temporary files. The program then merges the sorted files to standard output, preceding each line by a number that indicates which file it came from. $ cat sortmerg #!/bin/bash usage () { if [ $# -ne 2 ]; then echo "Usage: $0 file1 file2" 2>&1 exit 1 fi } # Default temporary directory : ${TEMPDIR:=/tmp} # Check argument count usage "$@" # Set up temporary files for sorting file1=$TEMPDIR/$$.file1 file2=$TEMPDIR/$$.file2 # Sort sort $1 > $file1 sort $2 > $file2 # Open $file1 and $file2 for reading. Use file descriptors 3 and 4. exec 3<$file1 exec 4<$file2 # Read the first line from each file to figure out how to start. read Line1 <&3 status1=$? read Line2 <&4 status2=$?432 Chapter 11 Programming the Bourne Again Shell # Strategy: while there is still input left in both files: # Output the line that should come first. # Read a new line from the file that line came from. while [ $status1 -eq 0 -a $status2 -eq 0 ] do if [[ "$Line2" > "$Line1" ]]; then echo -e "1.\t$Line1" read -u3 Line1 status1=$? else echo -e "2.\t$Line2" read -u4 Line2 status2=$? fi done # Now one of the files is at end-of-file. # Read from each file until the end. # First file1: while [ $status1 -eq 0 ] do echo -e "1.\t$Line1" read Line1 <&3 status1=$? done # Next file2: while [[ $status2 -eq 0 ]] do echo -e "2.\t$Line2" read Line2 <&4 status2=$? done # Close and remove both input files exec 3<&- 4<&- rm -f $file1 $file2 exit 0 Parameters and Variables Shell parameters and variables were introduced on page 295. This section adds to the previous coverage with a discussion of array variables, global versus local vari- ables, special and positional parameters, and expanding null and unset variables. Array Variables The Bourne Again Shell supports one-dimensional array variables. The subscripts are integers with zero-based indexing (i.e., the first element of the array has the sub- script 0). The following format declares and assigns values to an array:Parameters and Variables 433 name=(element1 element2 ...) The following example assigns four values to the array NAMES: $ NAMES=(max helen sam zach) You reference a single element of an array as follows: $ echo ${NAMES[2]} sam The subscripts [*] and [@] both extract the entire array but work differently when used within double quotation marks. An @ produces an array that is a duplicate of the original array; an * produces a single element of an array (or a plain variable) that holds all the elements of the array separated by the first character in IFS (nor- mally a SPACE). In the following example, the array A is filled with the elements of the NAMES variable using an *, and B is filled using an @. The declare builtin with the –a option displays the values of the arrays (and reminds you that bash uses zero- based indexing for arrays): $ A=("${NAMES[*]}") $ B=("${NAMES[@]}") $ declare -a declare -a A='([0]="max helen sam zach")' declare -a B='([0]="max" [1]="helen" [2]="sam" [3]="zach")' ... declare -a NAMES='([0]="max" [1]="helen" [2]="sam" [3]="zach")' From the output of declare, you can see that NAMES and B have multiple elements. In contrast, A, which was assigned its value with an * within double quotation marks, has only one element: A has all its elements enclosed between double quota- tion marks. In the next example, echo attempts to display element 1 of array A. Nothing is dis- played because A has only one element and that element has an index of 0. Element 0 of array A holds all four names. Element 1 of B holds the second item in the array and element 0 holds the first item. $ echo ${A[1]} $ echo ${A[0]} max helen sam zach $ echo ${B[1]} helen $ echo ${B[0]} max You can apply the ${#name[*]} operator to array variables, returning the number of elements in the array: $ echo ${#NAMES[*]} 4434 Chapter 11 Programming the Bourne Again Shell The same operator, when given the index of an element of an array in place of *, returns the length of the element: $ echo ${#NAMES[1]} 5 You can use subscripts on the left side of an assignment statement to replace selected elements of the array: $ NAMES[1]=alex $ echo ${NAMES[*]} max alex sam zach Locality of Variables By default variables are local to the process in which they are declared. Thus a shell script does not have access to variables declared in your login shell unless you explicitly make the variables available (global). Under bash, export makes a variable available to child processes. Once you use the export builtin with a variable name as an argument, the shell places the value of the variable in the calling environment of child processes. This call by value gives each child process a copy of the variable for its own use. The following extest1 shell script assigns a value of american to the variable named cheese and then displays its filename (extest1) and the value of cheese. The extest1 script then calls subtest, which attempts to display the same information. Next sub- test declares a cheese variable and displays its value. When subtest finishes, it returns control to the parent process, which is executing extest1. At this point extest1 again displays the value of the original cheese variable. $ cat extest1 cheese=american echo "extest1 1: $cheese" subtest echo "extest1 2: $cheese" $ cat subtest echo "subtest 1: $cheese" cheese=swiss echo "subtest 2: $cheese" $ extest1 extest1 1: american subtest 1: subtest 2: swiss extest1 2: american The subtest script never receives the value of cheese from extest1, and extest1 never loses the value. Unlike in the real world, a child can never affect its parent’s attributes. When a process attempts to display the value of a variable that has not been declared, as is the case with subtest, the process displays nothing; the value of an undeclared variable is that of a null string.Parameters and Variables 435 export The following extest2 script is the same as extest1 except that it uses export to make cheese available to the subtest script: $ cat extest2 export cheese=american echo "extest2 1: $cheese" subtest echo "extest2 2: $cheese" $ extest2 extest2 1: american subtest 1: american subtest 2: swiss extest2 2: american Here the child process inherits the value of cheese as american and, after displaying this value, changes its copy to swiss. When control is returned to the parent, the parent’s copy of cheese retains its original value: american. An export builtin can optionally include an assignment: export cheese=american The preceding statement is equivalent to the following two statements: cheese=american export cheese Although it is rarely done, you can export a variable before you assign a value to it. You do not need to export an already-exported variable a second time after you change its value. For example, you do not usually need to export PATH when you assign a value to it in ~/.bash_profile because it is typically exported in the /etc/profile global startup file. Functions Because functions run in the same environment as the shell that calls them, variables are implicitly shared by a shell and a function it calls. $ function nam () { > echo $myname > myname=zach > } $ myname=sam $ nam sam $ echo $myname zach In the preceding example, the myname variable is set to sam in the interactive shell. Then the nam function is called. It displays the value of myname it has (sam) and sets myname to zach. The final echo shows that, in the interactive shell, the value of myname has been changed to zach.436 Chapter 11 Programming the Bourne Again Shell Function local variables Local variables are helpful in a function written for general use. Because the func- tion is called by many scripts that may be written by different programmers, you need to make sure that the names of the variables used within the function do not interact with variables of the same name in the programs that call the function. Local variables eliminate this problem. When used within a function, the typeset builtin declares a variable to be local to the function it is defined in. The next example shows the use of a local variable in a function. It uses two vari- ables named count. The first is declared and assigned a value of 10 in the interactive shell. Its value never changes, as echo verifies after count_down is run. The other count is declared, using typeset, to be local to the function. Its value, which is unknown outside the function, ranges from 4 to 1, as the echo command within the function confirms. The example shows the function being entered from the keyboard; it is not a shell script. (See the tip “A function is not a shell script” on page 430). $ function count_down () { > typeset count > count=$1 > while [ $count -gt 0 ] > do > echo "$count..." > ((count=count-1)) > sleep 1 > done > echo "Blast Off." > } $ count=10 $ count_down 4 4... 3... 2... 1... Blast Off\! $ echo $count 10 The ((count=count–1)) assignment is enclosed between double parentheses, which cause the shell to perform an arithmetic evaluation (page 458). Within the double parentheses you can reference shell variables without the leading dollar sign ($). Special Parameters Special parameters enable you to access useful values pertaining to command line arguments and the execution of shell commands. You reference a shell special parameter by preceding a special character with a dollar sign ($). As with posi- tional parameters, it is not possible to modify the value of a special parameter by assignment.Parameters and Variables 437 $$: PID Number The shell stores in the $$ parameter the PID number of the process that is execut- ing it. In the following interaction, echo displays the value of this variable and the ps utility confirms its value. Both commands show that the shell has a PID number of 5209: $ echo $$ 5209 $ ps PID TTY TIME CMD 5209 pts/1 00:00:00 bash 6015 pts/1 00:00:00 ps Because echo is built into the shell, the shell does not have to create another process when you give an echo command. However, the results are the same whether echo is a builtin or not, because the shell substitutes the value of $$ before it forks a new process to run a command. Try using the echo utility (/bin/echo), which is run by another process, and see what happens. In the following example, the shell substi- tutes the value of $$ and passes that value to cp as a prefix for a filename: $ echo $$ 8232 $ cp memo $$.memo $ ls 8232.memo memo Incorporating a PID number in a filename is useful for creating unique filenames when the meanings of the names do not matter; it is often used in shell scripts for creating names of temporary files. When two people are running the same shell script, these unique filenames keep them from inadvertently sharing the same tem- porary file. The following example demonstrates that the shell creates a new shell process when it runs a shell script. The id2 script displays the PID number of the process running it (not the process that called it—the substitution for $$ is performed by the shell that is forked to run id2): $ cat id2 echo "$0 PID= $$" $ echo $$ 8232 $ id2 ./id2 PID= 8362 $ echo $$ 8232 The first echo displays the PID number of the interactive shell. Then id2 displays its name ($0) and the PID of the subshell that it is running in. The last echo shows that the PID number of the interactive shell has not changed.438 Chapter 11 Programming the Bourne Again Shell $! The value of the PID number of the last process that you ran in the background is stored in $!. The following example executes sleep as a background task and uses echo to display the value of $!: $ sleep 60 & [1] 8376 $ echo $! 8376 $?: Exit Status When a process stops executing for any reason, it returns an exit status to the par- ent process. The exit status is also referred to as a condition code or a return code. The $? variable stores the exit status of the last command. By convention a nonzero exit status represents a false value and means that the command failed. A zero is true and indicates that the command was successful. In the following example, the first ls command succeeds and the second fails: $ ls es es $ echo $? 0 $ ls xxx ls: xxx: No such file or directory $ echo $? 1 You can specify the exit status that a shell script returns by using the exit builtin, followed by a number, to terminate the script. If you do not use exit with a number to terminate a script, the exit status of the script is that of the last command the script ran. $ cat es echo This program returns an exit status of 7. exit 7 $ es This program returns an exit status of 7. $ echo $? 7 $ echo $? 0 The es shell script displays a message and terminates execution with an exit com- mand that returns an exit status of 7, the user-defined exit status in this script. The first echo then displays the value of the exit status of es. The second echo displays the value of the exit status of the first echo. The value is 0 because the first echo was successful. Positional Parameters The positional parameters comprise the command name and command line argu- ments. They are called positional because within a shell script, you refer to them byParameters and Variables 439 their position on the command line. Only the set builtin (page 442) allows you to change the values of positional parameters with one exception: You cannot change the value of the command name from within a script. $#: Number of Command Line Arguments The $# parameter holds the number of arguments on the command line (positional parameters), not counting the command itself: $ cat num_args echo "This script was called with $# arguments." $ num_args sam max zach This script was called with 3 arguments. $0: Name of the Calling Program The shell stores the name of the command you used to call a program in parameter $0. This parameter is numbered zero because it appears before the first argument on the command line: $ cat abc echo "The command used to run this script is $0" $ abc The command used to run this script is ./abc $ /home/sam/abc The command used to run this script is /home/sam/abc The preceding shell script uses echo to verify the name of the script you are executing. You can use the basename utility and command substitution to extract and display the simple filename of the command: $ cat abc2 echo "The command used to run this script is $(basename $0)" $ /home/sam/abc2 The command used to run this script is abc2 $1–$n: Command Line Arguments The first argument on the command line is represented by parameter $1, the second argument by $2, and so on up to $n. For values of n over 9, the number must be enclosed within braces. For example, the twelfth command line argument is repre- sented by ${12}. The following script displays positional parameters that hold com- mand line arguments: $ cat display_5args echo First 5 arguments are $1 $2 $3 $4 $5 $ display_5args jenny alex helen First 5 arguments are jenny alex helen The display_5args script displays the first five command line arguments. The shell assigns a null value to each parameter that represents an argument that is not440 Chapter 11 Programming the Bourne Again Shell present on the command line. Thus the $4 and $5 variables have null values in this example. $* The $* variable represents all the command line arguments, as the display_all pro- gram demonstrates: $ cat display_all echo All arguments are $* $ display_all a b c d e f g h i j k l m n o p All arguments are a b c d e f g h i j k l m n o p Enclose references to positional parameters between double quotation marks. The quotation marks are particularly important when you are using positional parame- ters as arguments to commands. Without double quotation marks, a positional parameter that is not set or that has a null value disappears: $ cat showargs echo "$0 was called with $# arguments, the first is :$1:." $ showargs a b c ./showargs was called with 3 arguments, the first is :a:. $ echo $xx $ showargs $xx a b c ./showargs was called with 3 arguments, the first is :a:. $ showargs "$xx" a b c ./showargs was called with 4 arguments, the first is ::. The showargs script displays the number of arguments ($#) followed by the value of the first argument enclosed between colons. The preceding example first calls showargs with three simple arguments. Next the echo command demonstrates that the $xx variable, which is not set, has a null value. In the final two calls to showargs, the first argument is $xx. In the first case the command line becomes showargs a b c; the shell passes showargs three arguments. In the second case the command line becomes showargs "" a b c, which results in calling showargs with four arguments. The difference in the two calls to showargs illustrates a subtle potential problem that you should keep in mind when using positional parameters that may not be set or that may have a null value. "$*" versus "$@" The $* and $@ parameters work the same way except when they are enclosed within double quotation marks. Using "$*" yields a single argument (with SPACEs or the value of IFS [page 305] between the positional parameters), whereas "$@" pro- duces a list wherein each positional parameter is a separate argument. This differ- ence typically makes "$@" more useful than "$*" in shell scripts. The following scripts help to explain the difference between these two special parameters. In the second line of both scripts, the single quotation marks keep the shell from interpreting the enclosed special characters so they can be displayed as themselves. The bb1 script shows that set "$*" assigns multiple arguments to the first command line parameter:Parameters and Variables 441 $ cat bb1 set "$*" echo $# parameters with '"$*"' echo 1: $1 echo 2: $2 echo 3: $3 $ bb1 a b c 1 parameters with "$*" 1: a b c 2: 3: The bb2 script shows that set "$@" assigns each argument to a different command line parameter: $ cat bb2 set "$@" echo $# parameters with '"$@"' echo 1: $1 echo 2: $2 echo 3: $3 $ bb2 a b c 3 parameters with "$@" 1: a 2: b 3: c shift: Promotes Command Line Arguments The shift builtin promotes each command line argument. The first argument (which was $1) is discarded. The second argument (which was $2) becomes the first argu- ment (now $1), the third becomes the second, and so on. Because no “unshift” com- mand exists, you cannot bring back arguments that have been discarded. An optional argument to shift specifies the number of positions to shift (and the number of arguments to discard); the default is 1. The following demo_shift script is called with three arguments. Double quotation marks around the arguments to echo preserve the spacing of the output. The pro- gram displays the arguments and shifts them repeatedly until there are no more arguments left to shift: $ cat demo_shift echo "arg1= $1 arg2= $2 arg3= $3" shift echo "arg1= $1 arg2= $2 arg3= $3" shift echo "arg1= $1 arg2= $2 arg3= $3" shift echo "arg1= $1 arg2= $2 arg3= $3" shift442 Chapter 11 Programming the Bourne Again Shell $ demo_shift alice helen jenny arg1= alice arg2= helen arg3= jenny arg1= helen arg2= jenny arg3= arg1= jenny arg2= arg3= arg1= arg2= arg3= Repeatedly using shift is a convenient way to loop over all the command line argu- ments in shell scripts that expect an arbitrary number of arguments. See page 401 for a shell script that uses shift. set: Initializes Command Line Arguments When you call the set builtin with one or more arguments, it assigns the values of the arguments to the positional parameters, starting with $1. The following script uses set to assign values to the positional parameters $1, $2, and $3: $ cat set_it set this is it echo $3 $2 $1 $ set_it it is this Combining command substitution (page 344) with the set builtin is a convenient way to get standard output of a command in a form that can be easily manipulated in a shell script. The following script shows how to use date and set to provide the date in a useful format. The first command shows the output of date. Then cat dis- plays the contents of the dateset script. The first command in this script uses com- mand substitution to set the positional parameters to the output of the date utility. The next command, echo $*, displays all positional parameters resulting from the previous set. Subsequent commands display the values of parameters $1, $2, $3, and $6. The final command displays the date in a format you can use in a letter or report: $ date Wed Jan 2 23:39:18 PST 2008 $ cat dateset set $(date) echo $* echo echo "Argument 1: $1" echo "Argument 2: $2" echo "Argument 3: $3" echo "Argument 6: $6" echo echo "$2 $3, $6" $ dateset Wed Jan 2 23:39:25 PST 2008 Argument 1: Wed Argument 2: Jan Argument 3: 2 Argument 6: 2008 Jan 2, 2008Parameters and Variables 443 You can also use the +format argument to date to modify the format of its output. When used without any arguments, set displays a list of the shell variables that are set, including user-created variables and keyword variables. Under bash, this list is the same as that displayed by declare and typeset when they are called without any arguments. The set builtin also accepts options that let you customize the behavior of the shell. For more information refer to “set ±o: Turns Shell Features On and Off” on page 335. Expanding Null and Unset Variables The expression ${name} (or just $name if it is not ambiguous) expands to the value of the name variable. If name is null or not set, bash expands ${name} to a null string. The Bourne Again Shell provides the following alternatives to accepting the expanded null string as the value of the variable: • Use a default value for the variable. • Use a default value and assign that value to the variable. • Display an error. You can choose one of these alternatives by using a modifier with the variable name. In addition, you can use set –o nounset (page 337) to cause bash to display an error and exit from a script whenever an unset variable is referenced. :– Uses a Default Value The :– modifier uses a default value in place of a null or unset variable while allow- ing a nonnull variable to represent itself: ${name:–default} The shell interprets :– as “If name is null or unset, expand default and use the expanded value in place of name; else use name.” The following command lists the contents of the directory named by the LIT variable. If LIT is null or unset, it lists the contents of /home/alex/literature: $ ls ${LIT:-/home/alex/literature} The default can itself have variable references that are expanded: $ ls ${LIT:-$HOME/literature} := Assigns a Default Value The :– modifier does not change the value of a variable. You may want to change the value of a null or unset variable to its default in a script, however. You can do so with the := modifier: ${name:=default}444 Chapter 11 Programming the Bourne Again Shell The shell expands the expression ${name:=default} in the same manner as it expands ${name:–default} but also sets the value of name to the expanded value of default. If a script contains a line such as the following and LIT is unset or null at the time this line is executed, LIT is assigned the value /home/alex/literature: $ ls ${LIT:=/home/alex/literature} : builtin Shell scripts frequently start with the : (colon) builtin followed on the same line by the := expansion modifier to set any variables that may be null or unset. The : buil- tin evaluates each token in the remainder of the command line but does not execute any commands. Without the leading colon (:), the shell evaluates and attempts to execute the “command” that results from the evaluation. Use the following syntax to set a default for a null or unset variable in a shell script (there is a SPACE following the first colon): : ${name:=default} When a script needs a directory for temporary files and uses the value of TEMPDIR for the name of this directory, the following line makes TEMPDIR default to /tmp: : ${TEMPDIR:=/tmp} :? Displays an Error Message Sometimes a script needs the value of a variable but you cannot supply a reasonable default at the time you write the script. If the variable is null or unset, the :? modi- fier causes the script to display an error message and terminate with an exit status of 1: ${name:?message} You must quote message if it contains SPACEs. If you omit message, the shell displays the default error message (parameter null or not set). Interactive shells do not exit when you use :?. In the following command, TESTDIR is not set so the shell dis- plays on standard error the expanded value of the string following :?. In this case the string includes command substitution for date, with the %T format being fol- lowed by the string error, variable not set. cd ${TESTDIR:?$(date +%T) error, variable not set.} bash: TESTDIR: 16:16:14 error, variable not set. Builtin Commands Builtin commands were introduced in Chapter 7. Commands that are built into a shell do not fork a new process when you execute them. This section discusses the type, read, exec, trap, kill, and getopts builtins and concludes with Table 11-6 on page 457, which lists many bash builtins.Builtin Commands 445 type: Displays Information About a Command The type builtin provides information about a command: $ type cat echo who if lt cat is hashed (/bin/cat) echo is a shell builtin who is /usr/bin/who if is a shell keyword lt is aliased to 'ls -ltrh | tail' The preceding output shows the files that would be executed if you gave cat or who as a command. Because cat has already been called from the current shell, it is in the hash table (page 1039) and type reports that cat is hashed. The output also shows that a call to echo runs the echo builtin, if is a keyword, and lt is an alias. read: Accepts User Input When you begin writing shell scripts, you soon realize that one of the most common tasks for user-created variables is storing information a user enters in response to a prompt. Using read, scripts can accept input from the user and store that input in variables. The read builtin reads one line from standard input and assigns the words on the line to one or more variables: $ cat read1 echo -n "Go ahead: " read firstline echo "You entered: $firstline" $ read1 Go ahead: This is a line. You entered: This is a line. The first line of the read1 script uses echo to prompt you to enter a line of text. The –n option suppresses the following NEWLINE, allowing you to enter a line of text on the same line as the prompt. The second line reads the text into the variable firstline. The third line verifies the action of read by displaying the value of firstline. The vari- able is quoted (along with the text string) in this example because you, as the script writer, cannot anticipate which characters the user might enter in response to the prompt. Consider what would happen if the variable were not quoted and the user entered * in response to the prompt: $ cat read1_no_quote echo -n "Go ahead: " read firstline echo You entered: $firstline $ read1_no_quote Go ahead: * You entered: read1 read1_no_quote script.1 $ ls read1 read1_no_quote script.1446 Chapter 11 Programming the Bourne Again Shell The ls command lists the same words as the script, demonstrating that the shell expands the asterisk into a list of files in the working directory. When the variable $firstline is surrounded by double quotation marks, the shell does not expand the asterisk. Thus the read1 script behaves correctly: $ read1 Go ahead: * You entered: * If you want the shell to interpret the special meanings of special characters, do not use quotation marks. REPLY The read builtin has features that can make it easier to use. When you do not specify a variable to receive read’s input, bash puts the input into the variable named REPLY. You can use the –p option to prompt the user instead of using a separate echo command. The following read1a script performs exactly the same task as read1: $ cat read1a read -p "Go ahead: " echo "You entered: $REPLY" The read2 script prompts for a command line and reads the user’s response into the variable cmd. The script then attempts to execute the command line that results from the expansion of the cmd variable: $ cat read2 read -p "Enter a command: " cmd $cmd echo "Thanks" In the following example, read2 reads a command line that calls the echo builtin. The shell executes the command and then displays Thanks. Next read2 reads a command line that executes the who utility: $ read2 Enter a command: echo Please display this message. Please display this message. Thanks $ read2 Enter a command: who alex pts/4 Jun 17 07:50 (:0.0) scott pts/12 Jun 17 11:54 (bravo.example.com) Thanks If cmd does not expand into a valid command line, the shell issues an error message: $ read2 Enter a command: xxx ./read2: line 2: xxx: command not found Thanks The read3 script reads values into three variables. The read builtin assigns one word (a sequence of nonblank characters) to each variable:Builtin Commands 447 $ cat read3 read -p "Enter something: " word1 word2 word3 echo "Word 1 is: $word1" echo "Word 2 is: $word2" echo "Word 3 is: $word3" $ read3 Enter something: this is something Word 1 is: this Word 2 is: is Word 3 is: something When you enter more words than read has variables, read assigns one word to each variable, with all leftover words going to the last variable. Both read1 and read2 assigned the first word and all leftover words to the one variable they each had to work with. In the following example, read accepts five words into three variables, assigning the first word to the first variable, the second word to the second variable, and the third through fifth words to the third variable: $ read3 Enter something: this is something else, really. Word 1 is: this Word 2 is: is Word 3 is: something else, really. Table 11-4 lists some of the options supported by the read builtin. Table 11-4 read options Option Function –a aname (array) Assigns each word of input to an element of array aname. –d delim (delimiter) Uses delim to terminate the input instead of NEWLINE. –e (Readline) If input is coming from a keyboard, use the Readline Library (page 322) to get input. –n num (number of characters) Reads num characters and