A Practical Guide To Ubuntu Linux (3rd Edition) Mark G. Sobell

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 1292

DownloadA Practical Guide To Ubuntu Linux (3rd Edition) - Mark G. Sobell
Open PDF In BrowserView PDF
A PRACTICAL GUIDE TO UBUNTU LINUX
THIRD EDITION

MARKG. SOBELL

PRENTICE
HALL

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

M a n y of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. W h e r e
those designations appear in this b o o k , and the publisher w a s aware o f a trademark claim, the designations have been printed
with initial capital letters or in all capitals.
Ubuntu® is a trademark o f Canonical Ltd and is used under license from Canonical Ltd. Points of view or opinions in this
publication do not necessarily represent the policies or positions o f Canonical Ltd or imply affiliation with Ubuntu,
www.ubuntu.com.
The author and publisher have taken care in the preparation o f this b o o k , but make no expressed or implied warranty of any
kind and assume no responsibility for errors or omissions. N o 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 b o o k 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. F o r more information, please contact:
U.S. Corporate and Government Sales
(800) 3 8 2 - 3 4 1 9
corpsales@pearsontechgroup.com
For sales outside the United States, please contact:
International Sales
international@pearsoned .com
Visit us on the W e b : informit.com/ph

Library of Congress Cataloging-in-Publication

Data

Sobell, M a r k G.
A practical guide to Ubuntu Linux / M a r k G. S o b e l l . — 3 r d ed.
p. cm.
Includes index.
I S B N 9 7 8 - 0 - 1 3 - 2 5 4 2 4 8 - 7 (pbk. : alk. paper)
1. Ubuntu (Electronic resource) 2 . Linux. 3 . Operating systems (Computers) I. Title.
QA76.76.063S59497 2010
005.4'32—dc22
2010024353
Copyright © 2 0 1 1 M a r k G. Sobell
All rights reserved. Printed in the United States o f America. T h i s 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 Contracts Department
5 0 1 Boylston Street, Suite 9 0 0
Boston, M A 0 2 1 1 6
Fax: (617) 6 7 1 - 3 4 4 7
ISBN-13: 978-0-13-254248-7
ISBN-10:

0-13-254248-X

Printed in the United States o f America on recycled paper at Edwards Brothers in Ann Arbor, Michigan.
First printing, August 2 0 1 0

CONTENTS
LIST OFJUMPSTARTS
PREFACE

xxxvii

xxxix

CHAPTER 1: WELCOME TO LINUX
Ubuntu Linux

1

2

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

10

xiv

CONTENTS

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

PART I

INSTALLING UBUNTU LINUX

CHAPTER 2: INSTALLATION OVERVIEW

23

25

The Live/Install Desktop CD and the Live/Install DVD 26
More Information 26
Planning the Installation 27
Considerations 27
Requirements 27
Processor Architecture 29
Interfaces: Installer and Installed System 30
Ubuntu Releases 31
Ubuntu Editions 32
Installing a Fresh Copy or Upgrading an Existing Ubuntu System?
Setting Up the Hard Disk 33
RAID 40
LVM: Logical Volume Manager 41
The Installation Process 42
Downloading and Burning a CD/DVD 43
The Easy Way to Download a CD ISO Image File 43
Other Ways to Download a CD/DVD ISO Image File 44
Verifying an ISO Image File 46
Burning the CD/DVD 47
Gathering Information About the System 47
Chapter Summary 49
Exercises 49
Advanced Exercises 49

CHAPTER 3: STEP-BY-STEP INSTALLATION

51

Booting from a Live/Install Desktop CD or a Live/Install DVD
Live Session 52
Basic Instructions 53
Detailed Instructions 53
The Live/Install DVD 55
The Live/Install Desktop CD 56

52

32

CONTENTS

The Welcome Screen 57
ubiquity: Installing Ubuntu Graphically 57
Graphical Partition Editors 63
gparted: The GNOME Partition Editor 64
palimpsest: The GNOME Disk Utility 66
ubiquity: Setting Up Partitions 70
Upgrading to a New Release 74
Installing KDE 75
Setting Up a Dual-Boot System 76
Creating Free Space on a Windows System 76
Installing Ubuntu Linux as the Second Operating System
Advanced Installation 77
The Disk Menu Screens 78
The Ubuntu Textual Installer 85
Chapter Summary 93
Exercises 94
Advanced Exercises 94

PART II

77

GETTING STARTED WITH UBUNTU LINUX

CHAPTER 4: INTRODUCTION TO UBUNTU LINUX
Curbing Your Power: root Privileges/sudo 98
A Tour of the Ubuntu Desktop 99
Logging In on the System 100
Introduction 100
Launching Programs from the Desktop 102
Switching Workspaces 104
Setting Personal Preferences 104
Mouse Preferences 105
Working with Windows 106
Using Nautilus to Work with Files 107
The Update Manager 112
Changing Appearance (Themes) 113
Session Management 116
Getting Help 116
Feel Free to Experiment 116
Logging Out 117
Getting the Most Out of the Desktop 117
GNOME Desktop Terminology 117
Opening Files 118
Panels 118
The Main Menu 122
Windows 123
The Object Context Menu 126

XV

97

95

xvi

CONTENTS

Updating, Installing, and Removing Software Packages 131
Software Sources Window 131
The Ubuntu Software Center 132
synaptic: Finds, Installs, and Removes Software 133
Where to Find Documentation 136
Ubuntu Help Center 136
man: Displays the System Manual 136
apropos: Searches for a Keyword 139
info: Displays Information About Utilities 139
The —help Option 142
HOWTOs: Finding Out How Things Work 142
Getting Help 143
More About Logging In 144
The Login Screen 145
What to Do If You Cannot Log In 146
Logging In Remotely: Terminal Emulators, ssh, and Dial-Up Connections
Logging In from a Terminal (Emulator) 147
Changing Your Password 148
Using Virtual Consoles 149
Working from the Command Line 150
Correcting Mistakes 150
Repeating/Editing Command Lines 152
Controlling Windows: Advanced Operations 153
Changing the Input Focus 153
Changing the Resolution of the Display 154
The Window Manager 155
Chapter Summary 156
Exercises 157
Advanced Exercises 158

CHAPTER 5: THE LINUX UTILITIES

159

Special Characters 160
Basic Utilities 161
Is: Lists the Names of Files 161
cat: Displays a Text File 162
rm: Deletes a File 162
less Is more: Display a Text File One Screen at a Time
hostname: Displays the System Name 163
Working with Files 163
cp: Copies a File 163
mv: Changes the Name of a File 164
Ipr: Prints a File 165
grep: Searches for a String 166
head: Displays the Beginning of a File 166

162

147

CONTENTS

tail: Displays the End of a File 167
sort: Displays a File in Order 168
uniq: Removes Duplicate Lines from a File 168
diff: Compares Two Files 168
file: Identifies the Contents of a File 170
I (Pipe): Communicates Between Processes 170
Four More Utilities 171
echo: Displays Text 171
date: Displays the Time and Date 172
script: Records a Shell Session 172
todos: Converts Linux and Macintosh Files to Windows Format
Compressing and Archiving Files 174
bzip2: Compresses a File 174
bunzip2 and bzcat: Decompress a File 175
gzip: Compresses a File 175
tar: Packs and Unpacks Archives 176
Locating Commands 178
which and whereis: Locate a Utility 178
mlocate: Searches for a File 180
Obtaining User and System Information 180
who: Lists Users on the System 180
finger: Lists Users on the System 181
w: Lists Users on the System 183
Communicating with Other Users 184
write: Sends a Message 184
mesg: Denies or Accepts Messages 185
Email 185
Tutorial: Using vim to Create and Edit a File
Starting vim 186
Command and Input Modes 188
Entering Text 189
Getting Help 190
Ending the Editing Session 193
The compatible Parameter 193
Chapter Summary 193
Exercises 196
Advanced Exercises 197

CHAPTER 6: THE LINUX FILESYSTEM
The Hierarchical Filesystem 200
Directory Files and Ordinary Files
Filenames 201
The Working Directory 204
Your Home Directory 204

200

186

199

173

xvii

x v i i i BRIEF CONTENTS

Pathnames 205
Absolute Pathnames 205
Relative Pathnames 206
Working with Directories 207
mkdir: Creates a Directory 208
cd: Changes to Another Working Directory 209
rmdir: Deletes a Directory 210
Using Pathnames 211
mv, cp: Move or Copy Files 212
mv: Moves a Directory 212
Important Standard Directories and Files 213
Access Permissions 215
Is -1: Displays Permissions 215
chmod: Changes Access Permissions 216
Setuid and Setgid Permissions 218
Directory Access Permissions 220
ACLs: Access Control Lists 221
Enabling ACLs 222
Working with Access Rules 222
Setting Default Rules for a Directory 225
Links 226
Hard Links 228
Symbolic Links 230
rm: Removes a Link 232
Chapter Summary 232
Exercises 234
Advanced Exercises 236

CHAPTER 7: THE S H E L L

237

The Command Line 238
Syntax 238
Processing the Command Line 240
Executing the Command Line 243
Editing the Command Line 243
Standard Input and Standard Output 243
The Screen as a File 244
The Keyboard and Screen as Standard Input and Standard Output
Redirection 245
Pipes 251
Running a Command in the Background 254
Filename Generation/Pathname Expansion 256
The ? Special Character 256
The
Special Character 257
The [ ] Special Characters 259

244

CONTENTS

Builtins 261
Chapter Summary 261
Utilities and Builtins Introduced in This Chapter
Exercises 262
Advanced Exercises 264

PART III

262

DIGGING INTO UBUNTU LINUX

CHAPTER 8: LINUX GUIs: X AND GNOME

267

X Window System 268
Using X 270
Desktop Environments/Managers 275
The Nautilus File Browser Window 276
The View Pane 277
The Side Pane 277
Control Bars 278
Menubar 279
The Nautilus Spatial View 282
GNOME Utilities 284
Font Preferences 284
Pick a Font Window 284
Pick a Color Window 285
Run Application Window 286
Searching for Files 286
GNOME Terminal Emulator/Shell 287
Chapter Summary 288
Exercises 289
Advanced Exercises 289

CHAPTER 9: THE BOURNE AGAIN S H E L L
Background 292
Shell Basics 293
Startup Files 293
Commands That Are Symbols 297
Redirecting Standard Error 297
Writing a Simple Shell Script 300
Separating and Grouping Commands 303
Job Control 307
Manipulating the Directory Stack 310

291

265

xix

x x BRIEF CONTENTS

Parameters and Variables 312
User-Created Variables 314
Variable Attributes 317
Keyword Variables 318
Special Characters 326
Processes 328
Process Structure 328
Process Identification 328
Executing a Command 330
History 330
Variables That Control History 330
Reexecuting and Editing Commands 332
The Readline Library 340
Aliases 346
Single Versus Double Quotation Marks in Aliases
Examples of Aliases 348
Functions 349
Controlling bash: Features and Options 352
Command-Line Options 352
Shell Features 352
Processing the Command Line 356
History Expansion 356
Alias Substitution 356
Parsing and Scanning the Command Line
Command-Line Expansion 357
Chapter Summary 365
Exercises 367
Advanced Exercises 369

347

356

CHAPTER 10: NETWORKING AND THE INTERNET
Types of Networks and How They Work 373
Broadcast Networks 374
Point-to-Point Networks 374
Switched Networks 374
LAN: Local Area Network 375
WAN: Wide Area Network 376
Internetworking Through Gateways and Routers
Network Protocols 379
Host Address 381
CIDR: Classless Inter-Domain Routing 386
Hostnames 386

376

371

CONTENTS

xxi

Communicate over a Network 388
finger: Displays Information About Remote Users 389
Sending Mail to a Remote User 390
Mailing List Servers 390
Network Utilities 390
Trusted Hosts 391
OpenSSH Tools 391
telnet: Logs In on a Remote System 391
ftp: Transfers Files over a Network 393
ping: Tests a Network Connection 393
traceroute: Traces a Route over the Internet 394
host and dig: Query Internet Nameservers 396
jwhois: Looks Up Information About an Internet Site 396
Distributed Computing 397
The Client/Server Model 398
DNS: Domain Name Service 399
Ports 401
NIS: Network Information Service
NFS: Network Filesystem 401
Network Services 402
Common Daemons 402
Proxy Servers 405
RPC Network Services 406
Usenet 407
WWW: World Wide Web 409
URL: Uniform Resource Locator
Browsers 410
Search Engines 411
Chapter Summary 411
Exercises 412
Advanced Exercises 413

PART IV

401

410

SYSTEM ADMINISTRATION

415

CHAPTER 11: SYSTEM ADMINISTRATION: CORE CONCEPTS
Running Commands with root Privileges 419
sudo: Running a Command with root Privileges 421
sudoers: Configuring sudo 426
Unlocking the root Account (Assigning a Password to root)
su: Gives You Another User's Privileges 431

431

417

x x i i BRIEF CONTENTS

The Upstart Event-Based init Daemon 432
Software Package 433
Definitions 433
Jobs 435
SysVinit (rc) Scripts: Start and Stop System Services
System Operation 443
Runlevels 443
Booting the System 444
Recovery (Single-User) Mode 445
Going to Multiuser Mode 448
Logging In 448
Logging Out 450
Bringing the System Down 450
Crash 452
Avoiding a Trojan Horse 453
Getting Help 454

440

Textual System Administration Utilities 455
kill: Sends a Signal to a Process 455
Other Textual Utilities 457
Setting Up a Server 460
Standard Rules in Configuration Files 460
rpcinfo: Displays Information About portmap 462
The inetd and xinetd Superservers 464
Securing a Server 465
DHCP: Configures Network Interfaces 470
nsswitch.conf: Which Service to Look at First 475
How nsswitch.conf Works 475
PAM 478
More Information 479
Configuration Files, Module Types, and Control Flags
Example 481
Modifying the PAM Configuration 482
Chapter Summary 483
Exercises 484
Advanced Exercises 484

479

CHAPTER 12: FILES, DIRECTORIES, AND FILESYSTEMS
Important Files and Directories 488
File Types 500
Ordinary Files, Directories, Links, and Inodes
Device Special Files 501

500

487

CONTENTS

Filesystems 505
mount: Mounts a Filesystem 506
umount: Unmounts a Filesystem 509
fstab: Keeps Track of Filesystems 510
fsck: Checks Filesystem Integrity 512
tune2fs: Changes Filesystem Parameters
RAID Filesystem 514
Chapter Summary 514
Exercises 515
Advanced Exercises 515

512

CHAPTER 13: DOWNLOADING AND INSTALLING SOFTWARE
JumpStart: Installing and Removing Packages Using aptitude 519
Finding the Package That Holds a File You Need 521
APT: Keeps the System Up-to-Date 522
Repositories 522
sources.list: Specifies Repositories for APT to Search 523
The APT Local Package Indexes and the APT Cache 524
The apt cron Script and APT Configuration Files 524
aptitude: Works with Packages and the Local Package Index 526
apt-cache: Displays Package Information 530
apt-get source: Downloads Source Files 532
dpkg: The Debian Package Management System 532
deb Files 533
dpkg: The Foundation of the Debian Package Management System
BitTorrent 539
Installing Non-dpkg Software 541
The /opt and /usr/local Directories 541
GNU Configure and Build System 542
wget: Downloads Files Noninteractively 543
Chapter Summary 544
Exercises 545
Advanced Exercises 545

CHAPTER 14: PRINTING WITH C U P S
Introduction 548
Prerequisites 548
More Information 549
Notes 549
JumpStart I: Configuring a Local Printer

xxiii

549

547

534

517

x x i v BRIEF CONTENTS

system-config-printer: Configuring a Printer 550
Configuration Selections 550
Setting Up a Remote Printer 552
JumpStart II: Setting Up a Local or Remote Printer Using the CUPS Web
Interface 555
Traditional UNIX Printing

558

Configuring Printers 560
The CUPS Web Interface 560
CUPS on the Command Line 561
Sharing CUPS Printers 565
Printing from Windows 566
Printing Using CUPS 566
Printing Using Samba 567
Printing to Windows 568
Chapter Summary 568
Exercises 569
Advanced Exercises 569

CHAPTER 15: BUILDING A LINUX KERNEL
Prerequisites

571

572

Downloading the Kernel Source Code 573
aptitude: Downloading and Installing the Kernel Source Code
git: Obtaining the Latest Kernel Source Code 574
/usr/src/linux: The Working Directory 575
Read the Documentation 575
Configuring and Compiling the Linux Kernel 575
.config: Configures the Kernel 575
Customizing a Kernel 577
Cleaning the Source Tree 579
Compiling a Kernel Image File and Loadable Modules 579
Using Loadable Kernel Modules 580
Installing the Kernel, Modules, and Associated Files 582
Rebooting 583
GRUB: The Linux Boot Loader 583
Configuring GRUB 584
update-grub: Updates the grub.cfg File 587
grub-install: Installs the MBR and GRUB Files 589
dmesg: Displays Kernel Messages 589
Chapter Summary 590
Exercises 590
Advanced Exercises 591

573

CONTENTS

CHAPTER 16: ADMINISTRATION TASKS

593

Configuring User and Group Accounts 594
users-admin: Manages User Accounts 594
useradd: Adds a User Account 597
userdel: Removes a User Account 598
usermod: Modifies a User Account 598
groupadd: Adds a Group 598
groupdel: Removes a Group 598
Backing Up Files 599
Choosing a Backup Medium 600
Backup Utilities 600
Performing a Simple Backup 602
dump, restore: Back Up and Restore Filesystems 603
Scheduling Tasks 605
cron and anacron: Schedule Routine Tasks 605
at: Runs Occasional Tasks 608
System Reports 608
vmstat: Reports Virtual Memory Statistics 609
top: Lists Processes Using the Most Resources 610
parted: Reports on and Partitions a Hard Disk 611
Keeping Users Informed 614
Creating Problems 615
Solving Problems 616
Helping When a User Cannot Log In 616
Speeding Up the System 617
Isof: Finds Open Files 618
Keeping a Machine Log 618
Keeping the System Secure 619
Log Files and Mail for root 620
Monitoring Disk Usage 620
logrotate: Manages Log Files 622
Removing Unused Space from Directories 624
Disk Quota System 625
rsyslogd: Logs System Messages 625
MySQL 628
More Information 628
Terminology 628
Syntax and Conventions 628
Prerequisites 629
Notes 629
JumpStart: Setting Up MySQL 629
Options 630
The .my.cnf Configuration File 630
Working with MySQL 630

XXV

x x v i BRIEF CONTENTS

Chapter Summary 635
Exercises 636
Advanced Exercises 636

CHAPTER 17: CONFIGURING AND MONITORING A LAN
Setting Up the Hardware 638
Connecting the Computers 638
Routers 638
NIC: Network Interface Card 639
Tools 640
Configuring the Systems 641
NetworkManager: Configures Network Connections
The NetworkManager Applet 642
Setting Up Servers 646
Introduction to Cacti 647
Configuring SNMP 648
Setting Up LAMP 648
The Cacti Poller 652
Configuring Cacti 652
Basic Cacti Administration 652
Setting Up a Remote Data Source 654
More Information 658
Chapter Summary 659
Exercises 660
Advanced Exercises 660

637

642

PART V USING CLIENTS AND SETTING UP
SERVERS
661
CHAPTER 18: O P E N S S H : SECURE NETWORK
COMMUNICATION

663

Introduction to OpenSSH 664
How OpenSSH Works 664
Files 665
More Information 666
Running the ssh, scp, and sftp OpenSSH Clients 667
Prerequisites 667
JumpStart: Using ssh and scp to Connect to an OpenSSH Server

667

CONTENTS

Configuring OpenSSH Clients 668
ssh: Connects to or Executes Commands on a Remote System 670
scp: Copies Files to and from a Remote System 672
sftp: A Secure FTP Client 674
~/.ssh/config and /etc/ssh/ssh_config Configuration Files 674
Setting Up an OpenSSH Server (sshd) 676
Prerequisites 676
Note 676
JumpStart: Starting an OpenSSH Server 677
Authorized Keys: Automatic Login 677
Command-Line Options 678
/etc/ssh/sshd_config Configuration File 679
Troubleshooting 680
Tunneling/Port Forwarding 681
Chapter Summary 684
Exercises 684
Advanced Exercises 685

CHAPTER 19: FTP: TRANSFERRING FILES A C R O S S
A NETWORK

687

Introduction to FTP 688
Security 688
FTP Connections 688
FTP Clients 689
More Information 689
Running the ftp and sftp FTP Clients 690
Prerequisites 690
JumpStart I: Downloading Files Using ftp 690
Anonymous FTP 694
Automatic Login 694
Binary Versus ASCII Transfer Mode 694
ftp Specifics 695
Setting Up an FTP Server (vsftpd) 699
Prerequisites 699
Notes 699
JumpStart II: Starting a vsftpd FTP Server 700
Testing the Setup 700
Configuring a vsftpd Server 701
Chapter Summary 711
Exercises 712
Advanced Exercises 712

xxvii

x x v i i i BRIEF C O N T E N T S

CHAPTER 20: e x i m 4 : SETTING UP MAIL SERVERS, CLIENTS,
AND MORE

713

Introduction to exim4 714
Alternatives to exim4 715
More Information 715
Setting Up a Mail Server (exim4) 715
Prerequisites 715
Notes 716
JumpStart I: Configuring exim4 to Use a Smarthost 716
JumpStart II: Configuring exim4 to Send and Receive Mail
Working with exim4 Messages 720
Mail Logs 720
Working with Messages 721
Aliases and Forwarding 722
Related Programs 723
Configuring an exim4 Mail Server 724
Using a Text Editor to Configure exim4 724
The update-exim4.conf.conf Configuration File 724
dpkg-reconfigure: Configures exim4 726
SpamAssassin 727
How SpamAssassin Works 727
Prerequisites 728
Testing SpamAssassin 728
Configuring SpamAssassin 730
Additional Email Tools 731
Webmail 731
Mailing Lists 733
Setting Up an IMAP or POP3 Mail Server 735
Authenticated Relaying 736
Chapter Summary 738
Exercises 738
Advanced Exercises 739

CHAPTER 21: N I S AND L D A P

741

Introduction to NIS 742
How NIS Works 742
More Information 744
Running an NIS Client 744
Prerequisites 745
Notes 745
Configuring an NIS Client 746
Testing the Setup 747
yppasswd: Changes NIS Passwords

748

718

CONTENTS

Setting Up an NIS Server 750
Prerequisites 750
Notes 751
Configuring the Server 751
Testing the Server 756
yppasswdd: The NIS Password Update Daemon

757

LDAP 758
More Information 760
Setting Up an LDAP Server 760
Prerequisites 760
Notes 760
Set up the Server 761
Add Entries to the Directory 764
Other Tools for Working with LDAP 767
Evolution Mail 767
Konqueror 770
Chapter Summary 770
Exercises 771
Advanced Exercises 771

CHAPTER 22: NFS: SHARING FILESYSTEMS

773

Introduction to NFS 774
More Information 776
Running an NFS Client 776
Prerequisites 776
JumpStart I: Mounting a Remote Directory Hierarchy 777
mount: Mounts a Directory Hierarchy 778
Improving Performance 780
/etc/fstab: Mounts Directory Hierarchies Automatically 781
Setting Up an NFS Server 782
Prerequisites 782
Notes 782
JumpStart II: Configuring an NFS Server Using shares-admin 783
Manually Exporting a Directory Hierarchy 785
Where the System Keeps NFS Mount Information 789
exportfs: Maintains the List of Exported Directory Hierarchies 791
Testing the Server Setup 792
automount: Mounts Directory Hierarchies on Demand 792
Chapter Summary 795
Exercises 795
Advanced Exercises 795

xxix

x x x BRIEF C O N T E N T S

CHAPTER 23: SAMBA: LINUX AND WINDOWS FILE AND
PRINTER SHARING

797

Introduction to Samba 798
More Information 799
Notes 799
Samba Users, User Maps, and Passwords 799
Setting Up a Samba Server 800
Prerequisites 800
JumpStart: Configuring a Samba Server Using system-config-samba
swat: Configures a Samba Server
804
smb.conf: Manually Configuring a Samba Server 807
Working with Linux Shares from Windows 814
Browsing Shares 814
Mapping a Share 814
Working with Windows Shares from Linux 815
smbtree: Displays Windows Shares 815
smbclient: Connects to Windows Shares 815
Browsing Windows Networks 816
Mounting Windows Shares 816
Troubleshooting 817
Chapter Summary 819
Exercises 820
Advanced Exercises 820

CHAPTER 24: D N S / B I N D : TRACKING DOMAIN NAMES
AND A D D R E S S E S

821

Introduction to DNS 822
Nodes, Domains, and Subdomains
Zones 824
Queries 825
Servers 826
Resource Records 827
DNS Queries and Responses 830
Reverse Name Resolution 831
How DNS Works 833
More Information 833
Notes 833
Setting Up a DNS Server 834
Prerequisites 834
JumpStart: Setting Up a DNS Cache
Configuring a DNS Server 836

822

834

800

CONTENTS

Setting Up Different Types of DNS Servers
A Full-Functioned Nameserver 850
A Slave Server 854
A Split Horizon Server 855
Chapter Summary 860
Exercises 860
Advanced Exercises 861

xxxi

850

CHAPTER 25: firestarter, gufw, AND iptables:
SETTING UP A FIREWALL

863

Introduction to firestarter 864
Notes 864
More Information 866
firestarter: Setting Up and Maintaining a Firewall 866
Prerequisites 866
JumpStart: Configuring a Firewall Using the firestarter Firewall Wizard
Maintaining a Firewall using firestarter 868
ufw: The Uncomplicated Firewall 874
gufw: The Graphical Interface to ufw 876
The Firewall Window 876
Adding Rules 877
Introduction to iptables 880
More Information 883
Prerequisites 883
Notes 883
Anatomy of an iptables Command 884
Building a Set of Rules Using iptables 885
Commands 885
Packet Match Criteria 887
Display Criteria 887
Match Extensions 887
Targets 890
Copying Rules to and from the Kernel 891
Sharing an Internet Connection Using NAT 892
Connecting Several Clients to a Single Internet Connection
Connecting Several Servers to a Single Internet Connection
Chapter Summary 896
Exercises 897
Advanced Exercises 897

893
896

867

x x x i i BRIEF C O N T E N T S

CHAPTER 26: APACHE: SETTING UP A WEB SERVER

899

Introduction 900
More Information 901
Notes 901
Running a Web Server (Apache) 902
Prerequisites 902
JumpStart: Getting Apache Up and Running 903
Configuring Apache 905
Configuration Directives 909
Directives I: Directives You May Want to Modify as You Get Started
Contexts and Containers 915
Directives II: Advanced Directives 919
Configuration Files 932
The Ubuntu apache2.conf File 932
The Ubuntu default Configuration File 934
Advanced Configuration 935
Redirects 935
Content Negotiation 935
Server-Generated Directory Listings (Indexing) 937
Virtual Hosts 937
Troubleshooting 940
Modules 941

910

mod_cgi and CGI Scripts 942
mod_ssl 943
Authentication Modules and .htaccess 945
Scripting Modules 946
Multiprocessing Modules (MPMs) 947
webalizer: Analyzes Web Traffic 948
MRTG: Monitors Traffic Loads 948
Error Codes 948
Chapter Summary 949
Exercises 950
Advanced Exercises 950

PART VI

PROGRAMMING TOOLS

951

CHAPTER 27: PROGRAMMING THE BOURNE AGAIN S H E L L
Control Structures 954
if...then 954
if...then...else 958
if...then...elif 961

953

CONTENTS

for... in 967
for 968
while 970
until 974
break and continue 976
case 977
select 983
Here Document 985
File Descriptors 987
Parameters and Variables 990
Array Variables 990
Locality of Variables 992
Special Parameters 994
Positional Parameters 996
Expanding Null and Unset Variables
Builtin Commands 1002

1001

type: Displays Information About a Command 1003
read: Accepts User Input 1003
exec: Executes a Command or Redirects File Descriptors
trap: Catches a Signal 1009
kill: Aborts a Process 1012
getopts: Parses Options 1012
A Partial List of Builtins 1015
Expressions 1016
Arithmetic Evaluation 1016
Logical Evaluation (Conditional Expressions) 1017
String Pattern Matching 1018
Operators 1019
Shell Programs 1024
A Recursive Shell Script 1025
The quiz Shell Script 1028
Chapter Summary 1034
Exercises 1036
Advanced Exercises 1038

CHAPTER 28: THE PERL SCRIPTING LANGUAGE
Introduction to Perl 1042
More Information 1042
Help 1043
perldoc 1043
Terminology 1045
Running a Perl Program 1046
Syntax 1047

1006

1041

xxxiii

x x x i v BRIEF CONTENTS

Variables 1049
Scalar Variables 1051
Array Variables 1053
Hash Variables 1056
Control Structures 1057
if/unless 1057
if...else 1059
if...elsif...else 1060
foreach/for 1061
last and next 1062
while/until 1064
Working with Files 1066
Sort 1069
Subroutines 1071
Regular Expressions 1073
Syntax and the =~ Operator
CPAN Modules 1079
Examples 1081
Chapter Summary 1085
Exercises 1085
Advanced Exercises 1086

PART VII

1074

APPENDIXES

1087

APPENDIX A: REGULAR EXPRESSIONS
Characters 1090
Delimiters 1090
Simple Strings 1090
Special Characters 1090
Periods 1091
Brackets 1091
Asterisks 1092
Carets and Dollar Signs 1092
Quoting Special Characters 1093
Rules 1093
Longest Match Possible 1093
Empty Regular Expressions 1094
Bracketing Expressions 1094
The Replacement String 1094
Ampersand 1095
Quoted Digit 1095
Extended Regular Expressions 1095
Appendix Summary 1097

1089

CONTENTS

APPENDIX B: HELP

1099

Solving a Problem 1100
Finding Linux-Related Information 1101
Documentation 1101
Useful Linux Sites 1102
Linux Newsgroups 1103
Mailing Lists 1103
Words 1104
Software 1104
Office Suites and Word Processors 1106
Specifying a Terminal 1106

APPENDIX C: SECURITY

1109

Encryption 1110
Public Key Encryption 1111
Symmetric Key Encryption 1112
Encryption Implementation 1113
GnuPG/PGP 1113
File Security 1115
Email Security 1115
MTAs (Mail Transfer Agents) 1115
MUAs (Mail User Agents) 1116
Network Security 1116
Network Security Solutions 1117
Network Security Guidelines 1117
Host Security 1119
Login Security 1120
Remote Access Security 1121
Viruses and Worms 1122
Physical Security 1122
Security Resources 1124
Appendix Summary 1127

APPENDIX D: THE FREE SOFTWARE DEFINITION
GLOSSARY

1133

JUMPSTA RT INDEX
FILE TREE INDEX
UTILITY INDEX
MAIN INDEX

1183
1185
1189

1195

1129

XXXV

This page intentionally left blank

JUMPSTARTS
JumpStarts

get you off to a quick start when you need to use a client or set up a server.

Once you have the client or server up and running, you can refine its
using the information

presented

in the sections

following

each

configuration

JumpStart.

A P T (SOFTWARE PACKAGES)
Installing and Removing Packages Using aptitude

519

C U P S (PRINTING)
Configuring a Local Printer
549
Setting Up a Local or Remote Printer Using the CUPS Web Interface

555

M Y S Q L (DATABASE)
Setting Up MySQL

629

O P E N S S H (SECURE COMMUNICATION)
Using ssh and scp to Connect to an OpenSSH Server
Starting an OpenSSH Server 677

667

FTP (DOWNLOAD AND UPLOAD FILES)
Downloading Files Using ftp
Starting a vsftpd FTP Server

690
700
xxxvii

xxxviii

JUMPSTARTS

EMAIL
Configuring exim4 to Use a Smarthost
716
Configuring exim4 to Send and Receive Mail

718

N F S (NETWORK FILESYSTEM)
Mounting a Remote Directory Hierarchy
777
Configuring an NFS Server Using shares-admin 783

SAMBA (LINUX/WINDOWS FILE SHARING)
Configuring a Samba Server Using system-config-samba

800

D N S (DOMAIN NAME SERVICE)
Setting Up a DNS Cache

834

firestarter (FIREWALL)
Configuring a Firewall Using the firestarter Firewall Wizard

APACHE (HTTP)
Getting Apache Up and Running

903

867

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 28 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 certainly helpful. This book is appropriate for
• Students who are taking a class in which they use Linux
• Home users who want to set up and/or run Linux
• Professionals who use Linux at work
• System administrators who need an understanding of Linux and the tools
that are available to them including the bash and Perl scripting languages
• Computer science students who are studying the Linux operating system
• Technical executives who want to get a grounding in Linux
Benefits A Practical Guide to Ubuntu LinuxThird
Edition, gives you a broad understanding of many facets 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.
xxxix

xl

PREFACE

New in this edition This edition includes many updates to the previous edition:
• Coverage of the MySQL relational database, which has been added to
Chapter 16 (page 628).
• An all-new section on the Cacti network monitoring tool, which has been
added to Chapter 17 (page 647).
• Coverage of the gufw firewall, which has been added to Chapter 25
(page 874).
• Updated chapters to reflect the Ubuntu 10.04 LTS (Lucid Lynx; maintained until 2013).
• Four indexes to make it easier to find what you are looking for quickly.
These indexes locate tables (page numbers followed by the letter t, definitions (italic page numbers), and differentiate between light and comprehensive coverage (light and standard fonts).
• The JumpStart index (page 1183) lists all the JumpStart sections in
this book. These sections help you set up servers and clients as quickly
as possible.
• The File Tree index (page 1185) lists, in hierarchical fashion, most
files mentioned in this book. These files are also listed in the main
index.
• The Utility index (page 1189) locates all utilities mentioned in this
book. A page number in a ight fon indicates a brief mention of the
utility while the regular font indicates more substantial coverage.
• The revised Main index (page 1195) is designed for ease of use.
Overlap If you have read the second edition of A Practical Guide to Linux® Commands, Editors, and Shell Programming, you will notice some overlap between that book and the
one you are reading now. The first chapter; the chapters on the utilities and the filesystem; the appendix on regular expressions; and the Glossary are very similar in the
two books, as are the three chapters on the Bourne Again Shell (bash). Chapters that
appear in this book but do not appear in A Practical Guide to Linux® Commands,
Editors, and Shell Programming 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 administration) 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, Editors, and Shell Programming, Second Edition works exclusively with the command
line and covers Mac OS X in addition to Linux. It includes full chapters on the vim
and emacs editors, as well as chapters on the gawk pattern processing language, the
sed stream editor, and the rsync secure copy utility. In addition, it has a command
reference section that provides extensive examples of the use of 100 of the most

FEATURES OF T H I S B O O K

xli

important Linux and Mac OS X utilities. You can use these utilities to solve problems without resorting to programming in C.

THIS BOOK INCLUDES UBUNTU LUCID LYNX ( 1 0 . 0 4 LTS)
ON A LIVE/INSTALL DVD
This book includes a live/install DVD that holds the Lucid Lynx (10.04) 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 system.
DVD features The included DVD incorporates all the features of the live/install Desktop CD as
well as many of the features of the Alternate and Server CDs. It also includes all
software packages supported 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 least
amount of time. You do not have to read this book straight through in page order.
Instead, once you are comfortable using Linux, you can use this book as a reference:
Look up a topic of interest in the table of contents or in an index and read about it.
Or think of the book as a catalog of Linux topics: Flip through the pages until a
topic catches your eye. The book includes many pointers to Web sites where you
can obtain additional information: Consider the Internet to be an extension of this
book.
A Practical Guide to Ubuntu LinuxThird
features:

Edition, is structured with the following

• 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.

xlii

PREFACE

• Tip boxes highlight ways you can save time by doing something differently
or situations when it may be useful or just interesting to have additional
information.
• Security boxes point out places where you can make a system more secure.
The security appendix presents a quick background in system security issues.
• Concepts are illustrated by practical examples throughout the book.
• 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 can
be found at www.sobell.com.
• The glossary defines more than 500 commonly encountered 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 each of these chapters.
• This book provides resources for finding software on the Internet. It also
explains how to download and install software using Synaptic, aptitude,
the Ubuntu Software Center window, and BitTorrent. It details controlling
automatic updates using the Update Manager window.
• This book describes in detail many important GNU tools, including the
GNOME desktop, the Nautilus File Browser, the parted, palimpsest, and
gparted partition editors, the gzip compression utility, and many command-line utilities that come from the GNU project.
• Pointers throughout the text provide help in obtaining online documentation from many sources, including the local system, the 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 multiple comprehensive indexes help you locate topics quickly and
easily.

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 xlvi) describes what each chapter
covers. Finally, the table of contents provides more detail. This book:

KEY T O P I C S COVERED IN T H I S B O O K

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 discussion of partitions, partition tables, and mount points, and assists you in
using the ubiquity, palimpsest, or gparted graphical partition editor or the
Ubuntu textual partition editor to examine and 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.

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 modify 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 programming language in which to write shell scripts.
• Explains the textual (command-line) interface and introduces more than
30 command-line utilities.
• Presents a tutorial on the vim textual editor.
• Covers types of networks, network protocols, 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.

xliii

xliv

PREFACE

• 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)
t 0 0 l s t 0 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 software 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 Manager window appears on the desktop to let you know
when software updates are available. You can download and install
updates from the Update Manager window.
• The Ubuntu Software Center 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 packages. It gives you more ways to search for packages than does the
Ubuntu Software Center window.
• 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.

KEY T O P I C S COVERED IN T H I S B O O K

• Explains system operation, including the boot process, init scripts, recovery (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.
• Explains how to set up and use the Cacti network monitor tool to graph
system and network information over time, including installing and setting
up the LAMP (Linux, Apache, MySQL, and PHP) stack.
• Provides instructions on installing and setting up a MySQL relational
database.
• Describes files, directories, and filesystems, including types of files and filesystems, fstab (the filesystem table), and automatically mounted filesystems, and explains how to fine-tune and check the integrity of filesystems.
• Covers backup utilities, including tar, 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.
• Describes how to use the textual uncomplicated firewall (ufw) and its
graphical interface (gufw) to protect the system.
• Covers the use of 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,
gufw and iptables, and NIS/LDAP (all of which are supported by Ubuntu
Linux).
• Describes how to set up a CUPS printer server.
• Explains how to set up and use a MySQL relational database.
• Describes how to set up and use a DHCP server either by itself or from
firestarter.

Programming

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

xlv

xlvi

PREFACE

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 19) section details the typefaces and terminology used in this book.
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.
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 introduces 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 useful utilities, and presents a tutorial on the vim text editor. Chapter 6 discusses the
Linux hierarchical filesystem, covering files, filenames, pathnames, working with
directories, access permissions, and hard and symbolic links. Chapter 7 introduces
the Bourne Again Shell (bash) and discusses command-line arguments and options,
redirecting input to and output from commands, running programs in the background, and using the shell to generate and expand filenames.

Experienced users may want to skim Part II
tip If you have used a UNIX or Linux system before, you may want to skim or skip some or all of the
chapters in Part II. Two sections that all readers should take a look at are: "Conventions Used in
This Book" (page 19), which explains the typographic and layout conventions used in this book,
and "Where to Find Documentation" (page 136), which points out both local and remote sources
of Linux and Ubuntu documentation.
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 and goes into more
depth about the Nautilus File Browser. Chapter 9 extends the bash coverage from
Chapter 7, explaining how to redirect error output, avoid overwriting files, and
work with job control, processes, startup files, important shell builtin commands,
parameters, shell variables, and aliases. Chapter 10 explains networks, network
security, and the Internet and discusses types of networks, subnets, protocols,
addresses, hostnames, and various network utilities. The section on distributed
computing describes the client/server model and some of the servers you can use on a
network. Details of setting up and using clients and servers are reserved until Part V.

KEY T O P I C S COVERED IN T H I S B O O K

xlvii

Part IV Part IV covers system administration. Chapter 11 discusses core concepts such as
the use of sudo, working with root privileges, system operation including a discussion of the Upstart init daemon, chroot jails, TCP wrappers, general information
about how to set up a server, DHCP, and PAM. Chapter 12 explains the Linux filesystem, going into detail about types of files, including special and device files; the
use of fsck to verify the integrity of and repair filesystems; and the use of tune2fs to
change filesystem parameters. Chapter 13 explains how to keep a system up-to-date
by downloading software from the Internet and installing it, including examples
that use APT programs such as aptitude, apt-get, and apt-cache to perform these
tasks. It also covers the dpkg software packaging system and the use of some dpkg
utilities. Finally, it explains how to use BitTorrent from the command line to download files. Chapter 14 explains how to set up the CUPS printing system so you can
print on both local and remote systems. Chapter 15 details customizing and building a Linux kernel. Chapter 16 covers additional administration tasks, including
setting up user accounts, backing up files, scheduling automated tasks, tracking disk
usage, solving general problems, and setting up a MySQL relational database.
Chapter 17 explains how to set up a local area network (LAN), including both
hardware (including wireless) and software configuration, and how to set up Cacti
to monitor the network.
Part V Part V goes into detail about setting up and running servers and connecting to them
using clients. Where appropriate, these chapters include JumpStart sections 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 communicate securely over the Internet.
• FTP—Set up a vsftpd secure FTP server and use any of several FTP clients
to exchange files with the server.
• Email—Configure exim4 and use Webmail, POP3, or IMAP to retrieve
email; use SpamAssassin to combat spam.
• NIS and LDAP—Set up NIS to facilitate system administration of a LAN
and LDAP to maintain databases.
• NFS—Share filesystems between systems on a network.
• Samba—Share filesystems and printers between Windows and Linux
systems.
• DNS/BIND—Set up a domain nameserver to let other systems on the
Internet know the names and IP addresses of local systems they may need
to contact.
• firestarter, ufw, gufw, and iptables—Set up a firewall to protect local systems,
share a single Internet connection between systems on a LAN, and run a
DHCP server.

xlviii

PREFACE

• Apache—Set up an HTTP server that serves Web pages, which browsers
can then display. This chapter includes many suggestions for increasing
Apache security.
Part VI Part VI covers two important programming tools that are used extensively in
Ubuntu system administration and general-purpose programming. Chapter 27 continues where Chapter 9 left off, going into greater depth about shell programming
using bash, with the discussion enhanced by extensive examples. Chapter 28 introduces the popular, feature-rich Perl programming language, including coverage of
regular expressions and file handling.
Part VII Part VII includes appendixes on regular expressions, helpful Web sites, system security, and free software. This part also includes an extensive glossary with more than
500 entries plus the JumpStart index, the File Tree index, the Utility index, and a
comprehensive traditional 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 evennumbered 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 28 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 Bob Campbell, Proofreader, who made each page sparkle and
found the mistakes that the author left behind.
Thanks also to the folks at Prentice Hall who helped bring this book to life, especially Julie Nahil, Full-Service Production Manager, who oversaw production of the
book; John Fuller, Managing Editor, who kept the large view in check; Stephane
Nakib, Marketing Manager; Kim Boedigheimer, Editorial Assistant, who attended
to the many details involved in publishing this book; Heather Fox, Publicist; Dan
Scherf, Media Developer; Cheryl Lenser, Senior Indexer; Sandra Schroeder, Design
Manager; Chuti Prasertsith, Cover Designer; and everyone else who worked behind
the scenes to make this book come into being.

THANKS

xlix

I am also indebted to Denis Howe, Editor of The Free On-Line Dictionary of Computing (FOLDOC). Denis has graciously permitted me to use entries from his compilation. Be sure to 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: John Dong, Ubuntu Developer, Forums
Council Member; Nathan Handler; Andy Lester, author of Land the Tech Job
You Love: Why Skill and Luck Are Not Enough; Max Sobell, New York University; Scott James Remnant, Ubuntu Development Manager and Desktop Team Leader;
and Susan Lauber, Lauber System Solutions, Inc.
Thanks also to the people who helped with the first and second editions of this book:
David Chisnall, Swansea University; Scott Mann, Aztek Networks; Thomas Achtemichuk, Mansueto Ventures; Daniel R. Arfsten, Pro/Engineer Drafter/Designer; Chris Cooper, Senior Education Consultant, Hewlett-Packard Education Services; Sameer Verma,
Associate Professor of Information Systems, San Francisco State University; Valerie
Chau, Palomar College and Programmers Guild; James Kratzer; Sean McAllister;
Nathan Eckenrode, New York Ubuntu Local Community Team; Christer Edwards;
Nicolas Merline; Michael Price; Mike Basinger, Ubuntu Community and Forums Council Member; Joe Barker, Ubuntu Forums Staff Member; Matthew Miller, Senior Systems
Analyst/Administrator, BU Linux Project, Boston University Office of Information Technology; George Vish II, Senior Education Consultant, Hewlett-Packard; James Stockford, Systemateka, Inc.; Stephanie Troeth, Book Oven; Doug Sheppard; Bryan Helvey, IT
Director, OpenGeoSolutions; and Vann Scott, Baker College of Flint.
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 Developer; Aaron Weber, Ximian; Cristof Falk, Software Developer at CritterDesign;
Steve Elgersma, Computer Science Department, Princeton University; Scott Dier,
University of Minnesota; Robert Haskins, Computer Net Works; Lars KelloggStedman, Harvard University; Jim A. Lola, Principal Systems Consultant, Privateer
Systems; Eric S. Raymond, Cofounder, Open Source Initiative; Scott Mann; Randall
Lechlitner, Independent Computer Consultant; Jason Wertz, Computer Science
Instructor, Montgomery County Community College; Justin Howell, Solano Community College; Ed Sawicki, The Accelerated Learning Center; David Mercer;
Jeffrey Bianchine, Advocate, Author, Journalist; John Kennedy; and Jim Dennis,
Starshine Technical Services.
Thanks also to Dustin Puryear, Puryear Information Technology; Gabor Liptak,
Independent Consultant; Bart Schaefer, Chief Technical Officer, iPost; Michael J.
Jordan, Web Developer, Linux Online; Steven Gibson, Owner, SuperAnt.com; John
Viega, Founder and Chief Scientist, Secure Software; K. Rachael Treu, Internet
Security Analyst, Global Crossing; Kara Pritchard, K & S Pritchard Enterprises;
Glen Wiley, Capital One Finances; Karel Baloun, Senior Software Engineer, Looksmart; Matthew Whitworth; Dameon D. Welch-Abernathy, Nokia Systems; Josh
Simon, Consultant; Stan Isaacs; and Dr. Eric H. Herrin II, Vice President, Herrin
Software Development. And thanks to Doug Hughes, long-time system designer

li

PREFACE

and administrator, who gave me a big hand with the sections on system administration, 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 Network Associates; Kaowen Liu; Andy Spitzer; Rik Schneider; Jesse St. Laurent; Steve
Bellenot; Ray W. Hiltbrand; Jennifer Witham; Gert-Jan Hagenaars; and Casper Dik.
A Practical Guide to Ubuntu Linux®, Third Edition, is based in part on two of my
previous UNIX books: UNIX System V: A Practical Guide and A Practical Guide to
the UNIX System. Many people helped me with those books, and thanks here go to
Pat Parseghian; Dr. Kathleen Hemenway; Brian LaRose; Byron A. Jeff, Clark Atlanta
University; Charles Stross; Jeff Gitlin, Lucent Technologies; Kurt Hockenbury;
Maury Bach, Intel Israel; Peter H. Salus; Rahul Dave, University of Pennsylvania;
Sean Walton, Intelligent Algorithmic Solutions; Tim Segall, Computer Sciences Corporation; Behrouz Forouzan, DeAnza College; Mike Keenan, Virginia Polytechnic
Institute and State University; Mike Johnson, Oregon State University; Jandelyn
Plane, University of Maryland; Arnold Robbins and Sathis Menon, Georgia Institute
of Technology; Cliff Shaffer, Virginia Polytechnic Institute and State University; and
Steven Stepanek, California State University, Northridge, for reviewing the book.
I continue to be grateful to the many people who helped with the early editions of
my UNIX books. Special thanks are due to Roger Sippl, Laura King, and Roy
Harrington for introducing me to the UNIX system. My mother, Dr. Helen Sobell,
provided invaluable comments on the original manuscript at several junctures. Also,
thanks go to Isaac Rabinovitch, Professor Raphael Finkel, Professor Randolph
Bentson, Bob Greenberg, Professor Udo Pooch, Judy Ross, Dr. Robert Veroff,
Dr. Mike Denny, Joe DiMartino, Dr. John Mashey, Diane Schulz, Robert Jung, Charles
Whitaker, Don Cragun, Brian Dougherty, Dr. Robert Fish, Guy Harris, Ping Liao,
Gary Lindgren, Dr. Jarrett Rosenberg, Dr. Peter Smith, Bill Weber, Mike Bianchi,
Scooter Morris, Clarke Echols, Oliver Grillmeyer, Dr. David Korn, Dr. Scott
Weikart, and Dr. Richard Curtis.
Finally, thanks to Peter and his family for providing
fortable place to work. I spent many hours reading
Peter's neighborhood coffee and sandwich shop. If
(24th & Guerrero in San Francisco), stop by and say

nourishment and a very comthe manuscript at JumpStart,
you are in the neighborhood
"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 pointers to interesting Linux pages on the Internet.
Mark G. Sobell
San Francisco, California

1
WELCOME TO LINUX
IN THIS CHAPTER
Ubuntu Linux

2

The History of UNIX and
GNU-Linux

3

The Heritage of Linux: UNIX

3

What Is So Good About Linux?

6

Overview of Linux

12

Additional Features of Linux

17

Conventions Used in This B o o k . . . 19

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

1

2

CHAPTER 1

WELCOME TO LINUX

Linux kernel

The Linux kernel was developed by Finnish undergraduate student Linus Torvalds,
who used the Internet to make the source code immediately available to others for
free. Torvalds released Linux version 0.01 in September 1991.
The new operating system came together through a lot of hard work. Programmers
around the world were quick to extend the kernel and develop other tools, adding
functionality to match that already found in both BSD UNIX and System V UNIX
(SVR4) as well as new functionality. The name Linux is a combination of Linus
and UNIX.
The Linux operating system, which was developed through the cooperation of
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, documentation, or support (via newsgroups, mailing lists, and other Internet resources). As the GNU Free Software Definition (reproduced in Appendix D) puts it:

Free beer

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

UBUNTU LINUX
Distributions

Various organizations package the Linux kernel and system programs as Linux distributions (visit distrowatch.com for more information). Some of the most popular
distributions are SUSE, Fedora, Ubuntu, Red Hat, Debian, and Mandriva. One of
the biggest differences between distributions typically is how the user installs the
operating system. Other differences include which graphical configuration tools are
installed by default and which tools are used to keep the system up-to-date.

Canonical

Under the leadership of Mark Shuttleworth, Canonical Ltd. (www.canonical.com),
the sponsor of Ubuntu Linux, supports many, similar Linux distributions: Ubuntu
runs the GNOME desktop manager, Kubuntu (www.kubuntu.org) runs the KDE
desktop manager, Edubuntu (www.edubuntu.org) includes many school-related
applications, and Xubuntu (www.xubuntu.org) runs the lightweight Xfce desktop,
which makes it ideal for older, slower machines.
From its first release in October 2004, Ubuntu has been a community-oriented
project. Ubuntu maintains several structures that 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 is based on Debian Linux and focuses on enhancing usability, accessibility, and internationalization. Although Ubuntu initially targeted the desktop

THE H I S T O R Y OF U N I X AND G N U - L I N U X

3

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.
The Ubuntu governance structure follows a benevolent dictator model: Mark
Shuttleworth is the Self-Appointed Benevolent Dictator for Life (SABDFL). The
structure includes the Technical Board, Ubuntu Community Council, Local Communities (LoCos), and Masters of the Universe (MOTU; wiki.ubuntu.com/MOTU).
For more information about Ubuntu, see www.ubuntu.com/aboutus/faq.

THE HISTORYOF UNIX AND G N U - L I N U X
This section presents some background on the relationships between UNIX and
Linux and between GNU and Linux.

THE HERITAGE OF LINUX: UNIX
The UNIX system was developed by researchers who needed a set of modern computing tools to help them with their projects. The system allowed a group of people
working together on a project to share selected data and programs while keeping
other information private.
Universities and colleges played a major role in furthering the popularity of the UNIX
operating system through the "four-year effect." When the UNIX operating system
became widely available in 1975, Bell Labs offered it to educational institutions at
nominal cost. The schools, in turn, used it in their computer science programs, ensuring that computer science students became familiar with it. Because UNIX was such
an advanced development system, the students became acclimated to a sophisticated
programming environment. As these students graduated and went into industry, they
expected to work in a similarly advanced environment. As more of them worked their
way up the ladder in the commercial world, the UNIX operating system found its way
into industry.
In addition to introducing students to the UNIX operating system, the Computer
Systems Research Group (CSRG) at the University of California at Berkeley made
significant additions and changes to it. In fact, it made so many popular changes
that one version of the system is called the Berkeley Software Distribution (BSD) of
the UNIX system (or just Berkeley UNIX). The other major version is UNIX
System V (SVR4), which descended from versions developed and maintained by
AT&T and UNIX System Laboratories.

4

CHAPTER 1

W E L C O M E TO L I N U X

FADE TO 1 9 8 3
Richard Stallman (www.stallman.org) announced1 the GNU Project for creating an
operating system, both kernel and system programs, and presented the GNU Manifesto,2 which begins as follows:
GNU, which stands for Gnu's Not UNIX, is the name for the complete UNIX-compatible software system which I am writing so that
I can give it away free to everyone who can use it.
Some years later, Stallman added a footnote to the preceding sentence when he realized that it was creating confusion:
The wording here was careless. The intention was that nobody
would have to pay for * permission* to use the GNU system. But
the words don't make this clear, and people often interpret them as
saying that copies of GNU should always be distributed at little or
no charge. That was never the intent; later on, the manifesto mentions the possibility of companies providing the service of distribution for a profit. Subsequently I have learned to distinguish
carefully between "free" in the sense of freedom and "free" in the
sense of price. Free software is software that users have the freedom to distribute and change. Some users may obtain copies at no
charge, while others pay to obtain copies—and if the funds help
support improving the software, so much the better. The important
thing is that everyone who has a copy has the freedom to cooperate
with others in using it.
In the manifesto, after explaining a little about the project and what has been
accomplished so far, Stallman continues:
Why I Must Write GNU
I consider that the golden rule requires that if I like a program I must
share it with other people who like it. Software sellers want to divide
the users and conquer them, making each user agree not to share
with others. I refuse to break solidarity with other users in this way.
I cannot in good conscience sign a nondisclosure agreement or a
software license agreement. For years I worked within the Artificial
Intelligence Lab to resist such tendencies and other inhospitalities,
but eventually they had gone too far: I could not remain in an institution where such things are done for me against my will.
So that I can continue to use computers without dishonor, I have
decided to put together a sufficient body of free software so that I
will be able to get along without any software that is not free. I
1. www.gnu.org/gnu/initial-announcement.html
2. www.gnu.org/gnu/manifesto.html

THE H I S T O R Y OF U N I X AND G N U - L I N U X

5

have resigned from the AI Lab to deny MIT any legal excuse to
prevent me from giving GNU away.

NEXT SCENE, 1 9 9 1
The GNU Project has moved well along toward its goal. Much of the GNU operating
system, except for the kernel, is complete. Richard Stallman later writes:
By the early '90s we had put together the whole system aside from
the kernel (and we were also working on a kernel, the GNU Hurd,3
which runs on top of Mach 4 ). Developing this kernel has been a lot
harder than we expected, and we are still working on finishing it. 5
...[M]any believe that once Linus Torvalds finished writing the kernel, his friends looked around for other free software, and for no
particular reason most everything necessary to make a UNIX-like
system was already available.
What they found was no accident—it was the GNU system. The
available free software6 added up to a complete system because the
GNU Project had been working since 1984 to make one. The GNU
Manifesto had set forth the goal of developing a free UNIX-like
system, called GNU. The Initial Announcement of the GNU
Project also outlines some of the original plans for the GNU system. By the time Linux was written, the [GNU] system was almost
finished.7
Today the GNU "operating system" runs on top of the FreeBSD (www.freebsd.org)
and NetBSD (www.netbsd.org) kernels with complete Linux binary compatibility
and on top of Hurd pre-releases and Darwin (developer.apple.com/opensource)
without this compatibility.

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

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

6

CHAPTER 1

W E L C O M E TO LINUX

MINIX Two professors created their own stripped-down UNIX look-alikes for educational
purposes: Doug Comer created XINU and Andrew Tanenbaum created MINIX.
Linus Torvalds created Linux to counteract the shortcomings in MINIX. Every time
there was a choice between code simplicity and efficiency/features, Tanenbaum
chose simplicity (to make it easy to teach with MINIX), which meant this system
lacked many features people wanted. Linux went in the opposite direction.
You can obtain Linux at no cost over the Internet (page 43). 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 installation instructions,
software, and support.
GPL Linux and GNU software are distributed under the terms of the GNU General Public
License (GPL, www.gnu.org/licenses/licenses.html). The GPL says you have the right
to copy, modify, and redistribute the code covered by the agreement. When you
redistribute the code, however, you must also distribute the same license with the
code, thereby making the code and the license inseparable. If you get source code off
the Internet for an accounting program that is under the GPL and then modify that
code and redistribute an executable version of the program, you must also distribute
the modified source code and the GPL agreement with it. Because this arrangement is
the reverse of the way a normal copyright works (it gives rights instead of limiting
them), it has been termed a copyleft. (This paragraph is not a legal interpretation of
the GPL; it is intended merely to give you an idea of how it works. Refer to the GPL
itself when you want to make use of it.)

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

WHAT IS S O GOOD ABOUT LINUX?
In recent years Linux has emerged as a powerful and innovative UNIX work-alike.
Its popularity has surpassed that of its UNIX predecessors. Although it mimics
UNIX in many ways, the Linux operating system departs from UNIX in several significant ways: The Linux kernel is implemented independently of both BSD and System V, the continuing development of Linux is taking place through the combined

WHAT I s S o GOOD ABOUT LINUX?

7

efforts of many capable individuals throughout the world, and Linux puts the power
of UNIX within easy reach of both business and personal computer users. Using the
Internet, today's skilled programmers submit additions and improvements to the
operating system to Linus Torvalds, GNU, or one of the other authors of Linux.
Standards

In 1985, individuals from companies throughout the computer industry joined
together to develop the POSIX (Portable Operating System Interface for Computer
Environments) standard, which is based largely on the UNIX System V Interface
Definition (SVID) and other earlier standardization efforts. These efforts were
spurred by the U.S. government, which needed a standard computing environment
to minimize its training and procurement costs. Released in 1988, POSIX is a group
of IEEE standards that define the API (application programming interface), shell,
and utility interfaces for an operating system. Although aimed at UNIX-like systems,
the standards can apply to any compatible operating system. Now that these standards have gained acceptance, software developers are able to develop applications
that run on all conforming versions of UNIX, Linux, and other operating systems.

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 supporter. Linux conforms increasingly more closely to POSIX standards, and some
distributions and parts of others meet this standard. These developments indicate
that Linux is becoming mainstream and is respected as an attractive alternative to
other popular operating systems.

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 programs 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. Its sibling Mozilla/Thunderbird/Firefox is also a viable browser, mail client, and newsreader, performing
many other functions as well.

Platforms

Linux is not just for Intel-based platforms (which now include Apple computers): It
has been ported to and runs on the Power PC—including older Apple computers
(ppclinux), Compaq's (née Digital Equipment Corporation) Alpha-based machines,
MlPS-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,

8

CHAPTER 1

WELCOME TO LINUX

it runs on multiple-processor machines (SMPs; page 1172). It also includes an O ( l )
scheduler, which dramatically increases scalability on SMP systems.
Emulators

Linux supports programs, called emulators, that run code intended for other operating systems. By using emulators you can run some DOS, Windows, and Macintosh
programs under Linux. For example, Wine (www.winehq.com) is an open-source
implementation of the Windows API that runs on top of the X Window System and
UNIX/Linux.

Virtual machines

A virtual machine (VM or guest) appears to the user and to the software running on
it as a complete physical machine. It is, however, one of potentially many such VMs
running on a single physical machine (the host). The software that provides the virtualization is called a virtual machine monitor (VMM) or hypervisor. Each V M can
run a different operating system from the other VMs. For example, on a single host
you could have VMs running Windows, Ubuntu 7.10, Ubuntu 8.04, and Fedora 9.
A multitasking operating system allows you to run many programs on a single
physical system. Similarly, a hypervisor allows you to run many operating systems
(VMs) on a single physical system.
VMs provide many advantages over single, dedicated machines:
• Isolation—Each V M is isolated from the other VMs running on the same
host: Thus, if one V M crashes or is compromised, the others are not
affected.
• Security—When a single server system running several servers is compromised, all servers are compromised. If each server is running on its own
VM, only the compromised server is affected; other servers remain secure.
• Power consumption—Using VMs, a single powerful machine can replace
many less powerful machines, thereby cutting power consumption.
• Development and support—Multiple VMs, each running a different version
of an operating system and/or different operating systems, can facilitate
development and support of software designed to run in many environments.
With this organization you can easily test a product in different environments
before releasing it. Similarly, when a user submits a bug, you can reproduce
the bug in the same environment it occurred in.
• Servers—In some cases, different servers require different versions of system libraries. In this instance, you can run each server on its own VM, all
on a single piece of hardware.
• Testing—Using VMs, you can experiment with cutting-edge releases of
operating systems and applications without concern for the base (stable)
system, all on a single machine.
• Networks—You can set up and test networks of systems on a single
machine.

WHAT I s S o GOOD ABOUT LINUX?

9

• Sandboxes—A V M presents a sandbox—an area (system) that you can
work in without regard for the results of your work or for the need to
clean up.
• Snapshots—You can take snapshots of a V M and return the V M to the
state it was in when you took the snapshot simply by reloading the V M
from the snapshot.
Xen Xen, which was created at the University of Cambridge and is now being developed
in the open-source community, is an open-source virtual machine monitor (VMM).
A V M M enables several virtual machines (VMs), each running an instance of a separate operating system, to run on a single computer. Xen isolates the VMs so that if
one crashes it does not affect any of the others. In addition, Xen introduces minimal
performance overhead when compared with running each of the operating systems
natively.
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 Lucid release of Ubuntu supports Xen 3.3. 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 Xen home page
at www.cl.cam.ac.uk/research/srg/netos/xen and to wiki.xensource.com/xenwiki.
VMware

VMware, Inc. (www.vmware.com) offers VMware Server, a free, downloadable,
proprietary product you can install and run as an application under Ubuntu.
VMware Server enables you to install several VMs, each running a different
operating system, including Windows and Linux. VMware also offers a free
VMware player that enables you to run VMs you create with the VMware
Server.

KVM

The Kernel-based Virtual Machine (KVM; kvm.qumranet.com and libvirt.org) is an
open-source V M and runs as part of the Linux kernel. It works only on systems
based on the Intel VT (VMX) CPU or the AMD SVM CPU; it is implemented as the
kvm, libvirt-bin, and ubuntu-vm-builder packages. For more information refer to
help.ubuntu.com/community/KVM.

Qemu

Qemu (bellard.org/qemu), written by Fabrice Bellard, is an open-source V M M that
runs as a user application with no CPU requirements. It can run code written for a
different CPU than that of the host machine. For more information refer to
https://help.ubuntu.com/community/Installation/QemuEmulator.

VirtualBox

VirtualBox (www.virtualbox.org) is a V M developed by Sun Microsystems. If you
want to run a virtual instance of Windows, you may want to investigate KVM
(help.ubuntu.com/community/KVM) and VirtualBox.

10

CHAPTER 1

WELCOME TO LINUX

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

Second, with the cost of hardware continually dropping, hardware manufacturers
could no longer afford to develop and support proprietary operating systems. A
proprietary operating system is one that is written and owned by the manufacturer
of the hardware (for example, DEC/Compaq owns VMS). Today's manufacturers
need a generic operating system 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 (which they have to pay to Microsoft for each copy of Windows
they sell), they are much better off. In turn, software developers need to keep the prices
of their products down; they cannot afford to create new versions of their products to
run under many different proprietary operating systems. Like hardware manufacturers,
software developers need a generic operating system.
Although the UNIX system once met the needs of hardware companies and
researchers for a generic operating system, over time it has become more proprietary as manufacturers added support for their own specialized features and introduced new software libraries and utilities. Linux emerged to serve both needs: It is a
generic operating system that takes advantage of available hardware power.

LINUX Is PORTABLE
A portable operating system is one that can run on many different machines. More
than 95 percent of the Linux operating system is written in the C programming language, and C is portable because it is written in a higher-level, machine-independent
language. (The C compiler is written in C.)
Because Linux is portable, it can be adapted (ported) to different machines and can
meet special requirements. For example, Linux is used in embedded computers,
such as the ones found in cellphones, PDAs, and the cable boxes on top of many
TVs. The file structure takes full advantage of large, fast hard disks. Equally important, Linux was originally designed as a multiuser operating system—it was not

WHAT I s S o G O O D A B O U T LINUX?

11

modified to serve several users as an afterthought. Sharing the computer's power
among many users and giving them the ability to share data and programs are central features of the system.
Because it is adaptable and takes advantage of available hardware, Linux runs on
many different microprocessor-based systems as well as mainframes. The popularity of the microprocessor-based hardware drives Linux; these microcomputers are
getting faster all the time, at about the same price point. Linux on a fast microcomputer has become good enough to displace workstations on many desktops. This
widespread acceptance benefits both users, who do not like having to learn a new
operating system for each vendor's hardware, and system administrators, who like
having a consistent software environment.
The advent of a standard operating system has given a boost to the development of
the software industry. Now software manufacturers can afford to make one version
of a product available on machines from different manufacturers.

THE C PROGRAMMING LANGUAGE
Ken Thompson wrote the UNIX operating system in 1969 in PDP-7 assembly language. Assembly language is machine dependent: Programs written in assembly
language work on only one machine or, at best, on one family of machines. For this
reason, the original UNIX operating system could not easily be transported to run
on other machines (it was not portable).
To make UNIX portable, Thompson developed the B programming language, a
machine-independent language, from the BCPL language. Dennis Ritchie developed
the C programming language by modifying B and, with Thompson, rewrote UNIX
in C in 1973. Originally, C was touted as a "portable assembler." The revised operating system could be transported more easily to run on other machines.
That development marked the start of C. Its roots reveal some of the reasons why it
is such a powerful tool. C can be used to write machine-independent programs. A
programmer who designs a program to be portable can easily move it to any computer that has a C compiler. C is also designed to compile into very efficient code.
With the advent of C, a programmer no longer had to resort to assembly language
to 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 a highly structured but is not necessarily a high-level language. C allows a
programmer to manipulate bits and bytes, as is necessary when writing an operating system. At the same time, it has high-level constructs that allow for efficient,
modular programming.
In the late 1980s the American National Standards Institute (ANSI) defined a standard version of the C language, commonly referred to as ANSI C or C89 (for the

12

CHAPTER 1

W E L C O M E TO LINUX

Compilers

Figure 1-1

Database
Management
Systems

Word
Processors

Ma

"and
^ ffge
Facilities
eS

Shells

A layered view of the Linux operating system

year the standard was published). Ten years later the C99 standard was published;
it is mostly supported by the GNU Project's C compiler (named gcc). The original
version of the language is often referred to as Kernighan & Ritchie (or K&R) C,
named for the authors of the book that first described the C language.
Another researcher at Bell Labs, Bjarne Stroustrup, created an object-oriented programming language named C++, which is built on the foundation of C. Because
object-oriented programming is desired by many employers today, C++ is preferred
over C in many environments. Another language of choice is Objective-C, which
was used to write the first Web browser. The GNU Project's C compiler supports C,
C++, and Objective-C.

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

LINUX HAS A KERNEL PROGRAMMING INTERFACE
The Linux kernel—the heart of the Linux operating system—is responsible for allocating the computer's resources and scheduling user jobs so each one gets its fair
share of system resources, including access to the CPU; peripheral devices, such as
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 of devices. For
example, there is one write() system call, rather than many device-specific ones.

OVERVIEW OF LINUX

13

When a program issues a write() request, the kernel interprets the context and passes
the request to the appropriate device. This flexibility allows old utilities to work with
devices that did not exist when the utilities were written. It also makes it possible to
move programs to new versions of the operating system without rewriting them
(provided the new version recognizes the same system calls).

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

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

LINUX PROVIDES A SECURE HIERARCHICAL FILESYSTEM
A file is a collection of information, such as text for a memo or report, an accumulation of sales figures, an image, a song, or an executable program. Each file is
stored under a unique identifier on a storage device, such as a hard disk. The Linux
filesystem provides a structure whereby files are arranged under directories, which
are like folders or boxes. Each directory has a name and can hold other files and
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

14

CHAPTER 1

WELCOME TO LINUX

Figure 1-2

The Linux filesystem structure

files by grouping related files in directories. Each user has one primary directory
and as many subdirectories as required (Figure 1-2).
Standards

With the idea of making life easier for system administrators and software developers,
a group got together over the Internet and developed the Linux Filesystem Standard
(FSSTND), which has since evolved into the Linux Filesystem Hierarchy Standard
(FHS). Before this standard was adopted, key programs were located in different
places in different Linux distributions. Today you can sit down at a Linux system and
expect to find any given standard program at a consistent location (page 213).

Links

A link allows a given file to be accessed by means of two or more names. The alternative names can be located in the same directory as the original file or in another
directory. Links can make the same file appear in several users' directories, enabling
those users to share the file easily. Windows uses the term shortcut in place of link
to describe this capability. Macintosh users will be more familiar with the term
alias. Under Linux, an alias is different from a link; it is a command macro feature
provided by the shell (page 346).

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 certain other users by means of a simple but effective protection scheme. This
level of security is provided by file access permissions, which limit the users who can
read from, write to, or execute a file. 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 four most popular shells are

OVERVIEW OF LINUX

15

• The Bourne Again Shell (bash), an enhanced version of the original Bourne
Shell (the original UNIX shell).
• The Debian Almquist Shell (dash; page 292), a smaller version of bash,
with fewer features. Most startup shell scripts call dash in place of bash to
speed the boot process.
• The TC Shell (tcsh), an enhanced version of the C Shell, developed as part
of BSD UNIX.
• The Z Shell (zsh), which incorporates features from a number of shells,
including the Korn Shell.
Because different users may prefer different shells, multiuser systems can have several different shells in use at any given time. The choice of shells demonstrates one
of the advantages of the Linux operating system: the ability to provide a customized
interface for each user.
Shell scripts

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

Wildcards and

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

FILENAME GENERATION
ambiguous file
references

COMPLETION
In conjunction with the Readline library, the shell performs command, filename,
pathname, and variable completion: You type a prefix and press ESCAPE, and the shell
lists the items that begin with that prefix or completes the item if the prefix specifies
a unique item.

DEVICE-INDEPENDENT INPUT AND OUTPUT
Redirection

Devices (such as a printer or a terminal) and disk files appear as files to Linux programs. When you give a command to the Linux operating system, you can instruct
it to send the output to any one of several devices or files. This diversion is called
output redirection.

16

CHAPTER 1
Device
independence

WELCOME TO LINUX

In a similar manner, a program's input, which normally comes from a keyboard, can
b e 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.

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

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

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

INTERPROCESS COMMUNICATION
Pipes and filters

Linux enables users to establish both pipes and filters on the command line. A pipe
sends the output of one program to another program as input. A filter is a special
kind of pipe that processes a stream of input data to yield a stream of output data.

ADDITIONAL FEATURES OF LINUX

17

A filter processes another program's output, altering it as a result. The filter's output
then becomes input to another program.
Pipes and filters frequently join utilities to perform a specific task. For example, you
can use a pipe to send the output of the sort utility to head (a filter that lists the first
ten lines of its input); you can then use another pipe to send the output of head to a
third utility, Ipr, that sends the data to a printer. Thus, in one command line, you can
use three utilities together to sort and print part of a file.

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

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

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

Usually two layers run on top of X: a desktop manager and a window manager. A desktop manager is a picture-oriented user interface that enables you to interact with system
programs by manipulating icons instead of typing the corresponding commands to a shell.

18

CHAPTER 1

WELCOME TO LINUX

Figure 1 -3

A GNOME workspace

Ubuntu runs the GNOME desktop manager (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 desktops,
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.

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

CONVENTIONS USED IN THIS B O O K

19

and download new software over the Internet quickly and reliably. Chapter 10 discusses networks, the Internet, and the Linux network facilities.

SOFTWARE DEVELOPMENT
One of Linux's most impressive strengths is its rich software development environment. Linux supports compilers and interpreters for many computer languages.
Besides C and C++, languages available for Linux include Ada, Fortran, Java, Lisp,
Pascal, Perl, and Python. The bison utility generates parsing code that makes it easier to write programs to build compilers (tools that parse files containing structured information). The flex utility generates scanners (code that recognizes lexical
patterns in text). The make utility and the GNU Configure and Build System make
it easier to manage complex development projects. Source code management systems, such as CVS, simplify version control. Several debuggers, including ups and
gdb, can help you track down and repair software defects. The GNU C compiler
(gcc) works with the gprof profiling utility to help programmers identify potential
bottlenecks in a program's performance. The C compiler includes options to perform extensive checking of C code, thereby making the code more portable and
reducing debugging time. Table B-4 on page 1104 lists some sites you can download software from.

CONVENTIONS USED IN THIS BOOK
This book uses conventions to make its explanations shorter and clearer. The following paragraphs describe these conventions.
Widgets

A widget is a simple graphical element that a user interacts with, such as a text box,
radio button, or combo box. When referring to a widget, this book specifies the
type of widget and its label. The term "tick" refers to the mark you put in a check
box, sometimes called a check mark. For example, "put a tick in the check box
labeled Run in terminal." See the glossary for definitions of various widgets.

Tabs and frames

Tabs allow windows to display sets of related information, one set at a time. For
example, Figure 4-12 on page 114 shows the Appearance Preferences window, which
has four tabs; the Theme tab is highlighted. A frame isolates a set of information
within a window. See Figure 14-3 on page 551 for an example.

Menu selection path

The menu selection path is the name of the menu or the location of the menu, followed by a colon, a SPACE, and the menu selections separated by •=> markers. The
entire menu selection path appears in bold type. You can read Main menu: System1^
Preferences "^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.

20

CHAPTER 1

Items

W E L C O M E TO LINUX

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, thi s one is used. In the previous example, the dollar sign ($) on the first line is a prompt that Linux displays, so
it is not bold; the remainder of the first line is entered by a user, so it is bold.

Utility names

Names of utilities are printed in t h i s s a n s s e r i f t y p e f a c e . This book references the
e m a c s text editor and the Is utility or Is command (or just Is) but instructs you to
enter Is - 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 sensitive (page 1139), so memo5, M E M 0 5 , 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 delimiters before and after a string. An example is the following string, which is displayed
by the p a s s w d 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 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 C0NTR0L-D. (Even
though D is shown as an uppercase letter, you do not have to press the SHIFT
key; enter C0NTR0L-D by holding the CONTROL key down and pressing d.)

Most examples include the shell prompt—the signal that Linux is waiting for a
RETURNS command—as a dollar sign ($), a hashmark (#), or sometimes a percent sign (%).
The prompt does not appear in a bold typeface in this book because you do not
enter it. Do not type the prompt on the keyboard when you are experimenting with
examples from this book. If you do, the examples will not work.

Prompts and

Examples omit the RETURN keystroke that you must use to execute them. An example
of a command line is
$ vim.tiny memo.1204

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

8. Different keyboards use different keys to move the cursor (page 1143) 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.

CHAPTER SUMMARY
Definitions

21

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

optional OPTIONAL INFORMATION
Passages marked as optional appear in a gray box. This material is not central to the
ideas presented in the chapter but often involves more challenging concepts. A good
strategy when reading a chapter is to skip the optional sections and then return to
them when you are comfortable with the main ideas presented in the chapter. This is
an optional paragraph.
URLs (Web Web addresses, or URLs, have an implicit http:// prefix, unless ftp:// or https:// is
addresses) 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
administrating a Linux system.

or

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 intended for system
administrators, but some apply to all users.

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

22

CHAPTER 1

W E L C O M E TO LINUX

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?

PART I
INSTALLING UBUNTU LINUX
CHAPTER 2
INSTALLATION OVERVIEW

25

CHAPTER 3
STEP-BY-STEP INSTALLATION

51

23

This page intentionally left blank

2
INSTALLATION
OVERVIEW
IN THIS CHAPTER
More Information

26

Planning the Installation

27

Setting Up the Hard Disk

33

LVM: Logical Volume Manager

41

The Installation Process

42

Downloading and Burning a
CD/DVD

43

Using BitTorrent

46

Gathering Information About the
System

47

Installing Ubuntu Linux is the process of copying operating
system files from a CD, DVD, or USB flash drive to hard
disk(s) on a system and setting up configuration files so 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 a live/install DVD
and boot the system. After you answer a few questions, you are
done. Of course, sometimes you may want to customize the system
or you may be installing on nonstandard hardware: The installer
presents you with these kinds of choices as the installation process
unfolds. Ubuntu also provides a textual installer that gives you
more control over the installation. Refer to "Booting from a
25

26

CHAPTER 2

INSTALLATION OVERVIEW

Live/Install Desktop CD or a Live/Install DVD" (page 52) and "Advanced Installation"
(page 77) for information about installing and customizing Ubuntu Linux.

THE LIVE/INSTALL DESKTOP C D AND THE LIVE/INSTALL DVD
The live/install Desktop CD and the live/install DVD run Ubuntu without installing
it on the hard disk. To boot from a live/install Desktop CD or a live/install DVD,
make sure the computer is set up to boot from a CD/DVD; see "BIOS setup" and
" C M O S " on page 28 for more information. 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 returns to the state it was in before you
booted from the CD/DVD. If the system has a Linux swap partition (most Linux
systems have one; see page 37), 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.
Running a live session is a good way to test hardware and fix a system that will not
boot from the hard disk. You can use a live session before you upgrade an Ubuntu
system to a new release: In some cases a new kernel may not boot. A live session is
also 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.

Saving files during a live session
tip You cannot save a file to a live/install CD/DVD as these are readonly media. During a live session,
even though you may appear to save a file, it will not be there after you exit from the live session. To
save data from a live session, save it to a network share or a USB flash drive, or mail it to yourself.

MORE INFORMATION
In addition to the following references, see "Where to Find Documentation" on
page 136 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
www. tldp. org/HO WTO/Bo otPrompt-HO WTO. html
Releases: wiki.ubuntu.com/Releases
Release notes: www.ubuntu.com/getubuntu/releasenotes
Burning a CD: help.ubuntu.com/community/BurninglsoHowto
Installing from a USB flash drive:
help.ubuntu.com/community/Installation/FromUSBStick

PLANNING THE INSTALLATION

27

RAID: help.ubuntu.com/community/Installation/SoftwareRAID,
en.wikipedia.org/wiki/RAID, and raid.wiki.kernel.org/index.php/Linux_Raid
LVM Resource Page (includes many links): sourceware.org/lvm2
LVM HO WTO: www.tldp.org/HOWTO/LVM-HOWTO
BitTorrent: help.ubuntu.com/community/BitTorrent and azureus.sourceforge.net
ARM: wiki.ubuntu.com/ARM/LucidReleaseNotes
X.org release information: wiki.x.org
Download Ubuntu

Easiest download: www.ubuntu.com/getubuntu
Released versions: releases.ubuntu.com
Minimal CD: help.ubuntu.com/community/Installation/MinimalCD
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/releases

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 75 for information 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. Including additional services can also slow the system down.
If you want a system to learn on or a development system, additional packages
services may be useful. For a more secure production system, it is best to install
maintain the minimum number of packages required and to enable only needed
vices. See page 432 for information on the Upstart init daemon, which starts
stops system services.

Minimal CD

and
and
serand

The Minimal CD is small and provides a quick installation (page 32).

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 hardware. You

28

CHAPTER 2

INSTALLATION OVERVIEW

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) to search for linux hardware, ubuntu hardware, or
linux and the specific hardware you want more information on (for example, linux
sata or linux a8n). In addition, many HOWTOs cover specific hardware. The Linux
Hardware Compatibility HO WTO is also available, although it may not be up-todate at the time you read it. Ubuntu Linux usually runs on the same systems Windows
runs on, unless the system includes a very new or unusual component.
The hardware required to run Ubuntu depends on which kind of system you want
to set up. A very minimal system that runs a textual (command-line) interface and
has very few software packages installed requires very different hardware from a
system that runs a GUI, has many installed packages, and supports visual effects
(page 115). Use the Alternate CD (page 32) 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, look up visual effects on help.ubuntu.com.
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 that uses system resources
more efficiently than Ubuntu does.
RAM (memory) An extremely minimal textual (command-line) system requires 48 megabytes of
RAM. A standard desktop system requires 320 megabytes, although you may be
able to use less RAM if you install Xubuntu. Installing Ubuntu from a live session
requires 256 megabytes, although it will run slowly if the system has less than 512
megabytes of RAM. Use the textual installer (page 85) if the system has less than
256 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.
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 do 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 up to boot from a CD/DVD, hard disk, or USB flash
drive. The BIOS determines the order in which the system tries to boot from each
device. You 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. See page 583
for more information.

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

PLANNING THE INSTALLATION

29

system boots, it displays a brief message about how to enter System Setup or CMOS
Setup mode. Usually you need to press DEL or F2 while the system is booting. Press the
key that is called for and then move the cursor to the screen and line that deal with
booting the system. Generally there is a list of three or four devices that the system
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.

Should I install 32-bit or 64-bit Ubuntu on a 64-bit-capable processor?
tip

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

30

CHAPTER 2

INSTALLATION OVERVIEW

¡386 (Intel x86)

Software on an Ubuntu PC (Intel x 8 6 ) CD/DVD is compiled to run on Intel x 8 6 compatible processors, including most machines with Intel and AMD processors,
almost all machines that run Microsoft Windows, and newer Apple Macintosh
machines that use Intel processors. If you are not sure which type of processor a
machine has, assume it has this type of processor.

amd64 (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 features of proprietary third-party applications are not available for 64-bit architecture, you may want to run Ubuntu compiled for a 32-bit (Intel x86) processor on a
system with a 64-bit processor.

and Intel EM64T)

armel+dove Dove refers to the Marvell Dove System-on-Chip (code name for the Armada 500).
This all-in-one chipset features an ARM general processor and a specific set of peripherals. For more information see www.ubuntu.com/products/whatisubuntu/arm and
wiki.ubuntu.com/ARM/LucidReleaseNotes.
powerpc (PowerPC)

Ubuntu does not officially support the IBM/Motorola PowerPC (used by older
Apple Macintosh computers), but extensive community support for this processor
architecture is available. See wiki.ubuntu.com/PowerPCFAQ for more information
about running Ubuntu on a PowerPC. You can download PowerPC versions of
Ubuntu from wiki.ubuntu.com/PowerPCDownloads.

spare (Sun SPARC)

Ubuntu supports UltraSPARC machines, including those based on the multicore
UltraSPARC T1 (Niagara) processors.

ia64 (Intel IA-64)

Ubuntu supports the Itanium family of 64-bit Intel processors.

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). You also have a choice of interfaces to use to work with
the installed system. This section describes the two basic interfaces: textual and
graphical.
Textual (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 125). 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
elements 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. Pressing the TAB key frequently moves the cursor from one element to the next and pressing the RETURN key
selects the element the cursor is on.

PLANNING THE INSTALLATION

31

1 I!) Curitigure ths IJIULK
W h e r e are yau?
SclecE y• ij * •*!:
rh.i " lyi'ri' [ nr l
lot y• ij [ Din[ry fdch updates from i 'C. (late To yi
Wect loi.il Lime.

Select your time îcnç:
d M'L [he clEKk

Ihe

Havana
«I , ' 11 y
Ind iana/Ind i an a polis
Indians/Knox
K r : i-yy r ( T — ' i i y

Indiana/leii. City
Indiana/Veva4
Ind lana/vincennes
Tndinna/hlnnman
inuvix
Iqilul*'

Juneau
Kentucky/Lou is y i 110
Kent ucky/Hont ice1lo
m: j United s

Time zww: I united
Oirf

Figure 2-1
Advantages

lias

ANVCN H^

Limo

Back

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

A GUI is user friendly, whereas the textual interface is compact, uses fewer system
resources, and can work on a text-only terminal or over a text-only connection.
Because it is more efficient, a textual interface is useful for older, slower systems and
systems with minimal amounts of RAM. Server systems frequently use a textual
interface because it allows the system to dedicate more resources to carrying out the
job it is set up to do and fewer resources to pleasing the system administrator. Not
running a GUI can also improve system security.
Even though it uses a graphical interface, Ubuntu's live installer installs Ubuntu
faster than the textual installer. The live installer copies an installed system image to
the hard disk and then sets up the system, whereas the textual installer uses APT
and dpkg to unpack hundreds of packages one by one.

Installer interfaces

Ubuntu provides a user-friendly graphical installer ( u b i q u i t y ) as well as an efficient
pseudographical installer (debian-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
Canonical, the company that supports Ubuntu, distributes a new release about every
six months. Each release has both a number and a name. The number comprises the
last one or two digits of the year and the two digits of the month of the release. For
example, the 9.10 release was released in October 2009. In sequence, recent releases
are 7.10 (Gutsy Gibbon), 8.04 (Hardy Heron), 8.10 (Intrepid Ibex), 9.04 (Jaunty
Jackalope), 9.10 (Karmic Koala), and 10.04 (Lucid Lynx). Ubuntu supports (i.e.,
provides updates for, including security updates) each release for at least 18 months.
For a complete list of Ubuntu releases, see wiki.ubuntu.com/Releases.
LTS releases

Some releases of Ubuntu are marked LTS (long-term support); for example, Lucid
Lynx is an LTS release. Canonical supports LTS releases for three years for a desktop

32

CHAPTER 2

INSTALLATION OVERVIEW

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, servers, and highly customized distributions frequently fall into this category. You can install and upgrade
an LTS release just as you would any other release.

UBUNTU EDITIONS
Each Ubuntu release disk is called an edition. Following is an overview of each disk.
Table 3-1 on page 78 lists the selections available on each disk menu.
The DVD is a live/install DVD (page 26); you can use it to boot into a live session. You
can install Ubuntu from a live session (page 57). This DVD is available for PC and 64bit PC architectures (page 29), 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.
Desktop CD

The Desktop CD is a live/install CD (page 26); you can use it to boot into a live session. You can install Ubuntu from a live session (page 57). This CD is available for
PC and 64-bit PC architectures (page 29), uses the graphical installer, and installs a
graphical (desktop) Ubuntu system.

Alternate CD

The Alternate Install CD is not a live CD; it is intended for special installations only.
It presents more advanced installation options than the Desktop CD does. This CD
is available for PC and 64-bit PC architectures (page 29), uses the textual installer,
and installs an Ubuntu system that displays either a graphical or a textual interface.

Server CD

The Server CD is not a live CD; it is intended for installation only. This CD is available for PC, 64-bit PC, and SPARC architectures (page 29). 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 any of
several servers including DNS and LAMP (Linux, Apache, MySQL, and PHP). A
system installed using this CD has no open ports (page 401) and includes only software essential to a server.

Minimal CD

Not an official edition, the Minimal CD is small (5-20 megabytes) and provides a
quick installation. Because it downloads software packages while it installs the
system, you do not have to update the packages immediately after you install the
system. Also, the Minimal CD installs only those packages required to install
Ubuntu, so installing with this CD results in a minimal system. You can install
additional packages once the system is up and running. The Minimal CD uses the
textual installer (page 85), which also allows this CD to be compact. For more
information see help.ubuntu.com/community/Installation/MinimalCD.

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

PLANNING THE INSTALLATION

33

the kernel. You can preserve some user data during an installation depending on
where it is located and how you format/partition the hard disk. Alternatively, you
can perform a clean install on an existing system without overwriting data by setting up a dual-boot system (page 76).

If you have a system running Ubuntu, upgrade instead of install
tip Ubuntu recommends that, if you have a system running Ubuntu and want to run a newer release,
you upgrade the release on the system. The rationale for this recommendation is twofold. First,
the upgrade path is more aggressively tested by Ubuntu developers than are the installers. Thus
you are less likely to run into problems with an update. Second, an upgrade preserves the system
settings and applications, making an upgrade easier perform than a clean install.
Upgrade

An upgrade replaces all installed software packages with the most recent version
available on the new release. During an upgrade, the installation program preserves
both system configuration and user data files. An upgrade brings utilities that are
present in the old release up-to-date and installs new utilities. Before you upgrade a
system, back up all files on the system.
In general, all new features are provided by an upgrade. However, GRUB is not
automatically updated to GRUB 2 (page 584) during an upgrade. For information
on upgrading from GRUB to GRUB 2, see help.ubuntu.com/community/Grub2. See
page 74 for instructions on upgrading an Ubuntu system to a new release. See
www.ubuntu.com/getubuntu/releasenotes to learn about features that will not take
effect with an upgrade.

SETTING UP THE HARD DISK
Free space

A hard disk must be prepared in several ways so Linux can write to and read from
it. Low-level formatting is the first step in preparing a disk for use. You do not need
to perform this task, as it is done at the factory where the hard disk is manufactured. The next steps in preparing a hard disk for use are to write a partition table
to it and to create partitions on the disk. Finally, you need to create a filesystem on
each partition. The area of a partitioned disk that is not occupied by partitions is
called free space. A new disk has no partition table, no partitions, and no free space.
Under DOS/Windows, the term formatting means creating a filesystem on a partition; see "Filesystems" below.

Partitions A partition, or slice, is a logical section of a hard disk that has a device name, such
as /dev/sdal, so you can refer to it separately from other sections. For normal use,
you must create at least one partition on a hard disk (pages 34 and following).
From a live session, and after you install Ubuntu, you can use the GNOME Partition Utility (page 66) to view, resize, and create partitions on an existing system.
During installation, you can use the graphical partition editor (pages 60 and 70) or
the textual partition editor (page 87) to create partitions. After installation, you can
use parted (page 611) or fdisk to manipulate partitions. See /dev on page 488 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

34

CHAPTER 2

INSTALLATION OVERVIEW

up an empty partition table on the disk. As partitions are added, removed, and
modified, information about these changes is recorded in the partition table. If you
remove the partition table, you can no longer access information on the disk except
by extraordinary means.
Filesystems

Before most programs can write to a partition, a data structure (page 1144), called
a filesystem, needs to be written to the partition. This data structure holds inodes
(page 501) that map locations on the disk that store files to the names of the files.
At the top of the data structure is a single unnamed directory. As will be explained
shortly, this directory joins the system directory structure when the filesystem is
mounted.
When the Ubuntu installer creates a partition, it automatically writes a filesystem to
the partition. You can use the mkfs (make filesystem; page 458) utility, which is similar to the DOS/Windows format utility, to manually create a filesystem on a partition. Table 12-1 on page 505 lists some common types of filesystems. Ubuntu Linux
typically creates ext4 filesystems for data; unless you have reason to use another
filesystem type, use ext4. Windows uses FAT16, FAT32, and NTFS filesystems.
Apple uses HFS (Hierarchical Filesystem) and HFS+. OS X uses either HFS+ or
UFS. Different types of filesystems can coexist in different partitions on a single
hard disk, including both Windows and Linux filesystems.

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

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

PLANNING THE INSTALLATION

35

THE LINUX DIRECTORY HIERARCHY
Skip this section for a basic installation
tip This section briefly describes the Linux directory hierarchy so you may better understand some
of the decisions you may need to make when you divide the hard disk into partitions while installing Linux. You do not have to read this section to install Linux. You can use guided partitioning
(pages 60 and 70) to set up the disk and return to this section when and if you want to. See the
beginning of Chapter 6 for a more thorough explanation of the Linux directory hierarchy.
Namespace
Windows versus
Linux

A namespace

is a set of names (identifiers) in which each name is unique.

As differentiated from a Windows machine, a Linux system presents a single
namespace that holds all files, including directories, on the local system. The Linux
system namespace is called the directory hierarchy or directory tree. Under Windows, C : \ is a separate namespace from D : \ . The directory hierarchy rooted at C : \ is
separate from the directory hierarchy rooted at D : \ and there is no path or connection between them. Under Linux, the single system namespace is rooted at /, which
is the root directory. Under the root directory are top-level subdirectories such as
bin, boot, etc, home, and usr.

Absolute pathnames

All files on a Linux system, including directories, have a unique identifier called an
absolute pathname. An absolute pathname traces a path through the directory hierarchy starting at the root directory and ending at the file or directory identified by
the pathname. Thus the absolute pathname of the top-level directory named home
is /home. See page 205 for more information.

Slashes (/) in

Within a pathname, a slash (/) follows (appears to the right of) the name of a directory. Thus /home/sam specifies that the ordinary or directory file named sam is
located in the directory named home, which is a subdirectory of the root directory
(/). The pathname /home/sam/ (with a trailing slash) specifies that sam is a directory file. In most instances this distinction is not important. The root directory is
implied when a slash appears at the left end of a pathname or when it stands alone.

pathnames

Linux system
namespace

The Linux system namespace comprises the set of absolute pathnames of all files,
including directories, in the directory hierarchy of a system.

MOUNT POINTS
A filesystem on a partition holds no information about where it will be mounted in
the directory hierarchy (the top-level directory of a filesystem does not have a
name). When you use the installer to create most partitions, you specify the type of
filesystem to be written to the partition and the name of a directory that Ubuntu
associates with the partition.
Mounting a filesystem associates the filesystem with a directory in the directory
hierarchy. You can mount a filesystem on any directory in the directory hierarchy.

36

CHAPTER 2

INSTALLATION OVERVIEW

The directory that you mount a filesystem on is called a mount point. The directory
you specify when you use the installer to create a partition is the mount point for
the partition. Most mount points are top-level subdirectories, with a few exceptions
(such as /usr/local, which is frequently used as a mount point).

Do not create files on mount points before mounting a filesystem
caution Do not put any files in a directory that is a mount point while a filesystem is not mounted on that
mount point. Any files in a directory that is used as a mount point are covered up while the filesystem is mounted on that directory; you will not be able to access them. They reappear when the
filesystem is unmounted.
For example, suppose the second partition on the first hard disk has the device
name /dev/sda2. To create an ext4 filesystem that you want to appear as /home in
the directory hierarchy, you must instruct Linux to mount the /dev/sda2 partition
on /home when the system boots. With this filesystem mounted on its normal
mount point, you can access it as the /home directory.
Filesystem
independence

The state of one filesystem does not affect other filesystems: One filesystem on a
Jriyg 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.

/etc/fstab The file that holds the information relating partitions to mount points is /etc/fstab
(filesystem table; page 510). The associations stored in the fstab file are the normal
ones for the system, but you can easily override them. When you work in recovery
mode, you may mount a filesystem on the /target directory so you can repair the
filesystem. For example, if you mount on /target the partition holding the filesystem
normally mounted on /home, the directory you would normally find at /home/sam
will be found at /target/sam.
Naming partitions
and filesystems

A partition and any filesystem it holds have no name or identification other than a
device name (and a related UUID value—see page 510). Instead, the partition and
the filesystem are frequently referred to by the name of the partition's normal
mount point. Thus "the /home partition" and "the /home filesystem" refer to the
partition that holds the filesystem normally mounted on the /home directory. See
page 506 for more information on mounting filesystems.

PARTITIONING A D I S K
During installation, the installer calls a partition editor to set up disk partitions.
This section discusses how to plan partition sizes. Although this section uses the
term partition, planning and sizing LVs (logical volumes; page 41) works the same
way. For more information refer to pages 64 and 70 and to the Linux Partition
HO WTO at www.tldp.org/HOWTO/Partition.
GUIDED PARTITIONING
It can be difficult to plan partition sizes appropriately if you are not familiar with
Linux. During installation, Ubuntu provides guided partitioning. Without asking

PLANNING THE INSTALLATION

37

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. The next section discusses the advantages of manual partitioning.

GiB versus GB
tip Historically a gigabyte (GB) meant either 2 30 (1,073,741,824) or 10 9 (1,000,000,000) bytes.
Recently the term gibibyte (giga binary byte; abbreviated as GiB) has been used to mean 2 30 bytes;
in turn, gigabyte is used more frequently to mean 109 bytes. Similarly, a mebibyte (MiB) is 2 20
(1,048,576) bytes. The Ubuntu partition editor uses mebibytes and gibibytes for specifying the
size of partitions. See wiki.ubuntu.com/UnitsPolicy for information about the Ubuntu policy
regarding this issue.
MANUAL PARTITIONING: PLANNING PARTITIONS
If you decide to manually partition the hard disk and set up partitions other than a
root partition (/) and a swap partition, first consider which kinds of activities will
occur under each top-level subdirectory. Then decide whether it is appropriate to
isolate that subdirectory by creating a filesystem in a partition and mounting it on
its own mount point. Advantages of creating additional filesystems include the following points:
• Separating data that changes frequently (e.g., /var and /home) from data
that rarely changes (e.g., /usr and /boot) can reduce fragmentation on the
less frequently changing filesystems, helping to maintain optimal system
performance.
• Isolating filesystems (e.g., /home) can preserve data when you reinstall
Linux.
• Additional filesystems can simplify backing up data on a system.
• If all directories are part of a single filesystem, and if a program then runs
amok or the system is the target of a DoS attack (page 1146), 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. On a system
with multiple filesystems, such problems typically fill a single filesystem
and do not affect other filesystems. Data that may help determine what
went wrong will likely be preserved and the system is less likely to crash.
/(root) The following paragraphs discuss the advantages of making each of the major
top-level subdirectories a separate, mountable filesystem. Any directories you do
not create filesystems for automatically become part of the root (/) filesystem.
For example, if you do not create a /home filesystem, /home is part of the root (/)
filesystem.
(swap) Linux temporarily stores programs and data on a swap partition when it does not
have enough RAM to hold all the information it is processing. The swap partition is

38

CHAPTER 2

INSTALLATION OVERVIEW

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

Where to put the /boot partition
caution On some systems, the /boot partition must reside completely below cylinder 1023 of the hard disk.
An easy way to ensure compliance with this restriction is to make the /boot partition one of the
first partitions on the disk. When a system has more than one hard disk, the /boot partition must
also reside on a drive in the following locations:
• Multiple IDE or EIDE drives: the primary controller
• Multiple SCSI drives: ID 0 or ID 1
• Multiple IDE and SCSI drives: the primary IDE controller or SCSI ID 0
/var The name var is short for variable: The data in this partition changes frequently.
Because it holds the bulk of system logs, package information, and accounting data,
making /var a separate partition is a good idea. Then, if a user runs a job that consumes all of the users' disk space, system log files in /var/log will not be affected.
The /var partition can occupy from 500 megabytes to as much as several gigabytes
for extremely active systems with many verbose daemons and a lot of printer and
mail activity (the print queues reside in /var/spool/cups and incoming mail is stored
in /var/mail). For example, software license servers are often extremely active systems. By default, Apache content (the Web pages it serves) is stored on /var under
Ubuntu; you may want to change the location Apache uses.
Although such a scenario is unlikely, many files or a few large files may be created
under the /var directory. Creating a separate filesystem to hold the files in /var will
prevent these files from overrunning the entire directory structure, bringing the system to a halt, and possibly creating a recovery problem.

PLANNING THE INSTALLATION

39

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

Set up partitions to aid in making backups
tip Plan partitions based on which data you want to back up and how often you want to back it up.
One very large partition can be more difficult to back up than several smaller ones.
/usr Separating the /usr partition can be useful if you plan to export /usr to another system and want the security that a separate partition can give. Many administrators
put an ext2 filesystem on this partition because the data on it does not change frequently enough to justify the overhead of the ext4 journal. The size of /usr depends
on the number of packages you install. On a default system, it is typically 2 - 4
gigabytes.
/usr/local Both /usr/local and /opt are candidates for separation. If you plan to install many
and/opt packages in addition to Ubuntu Linux, such as on an enterprise system, 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;
from there, they export the software to other systems. If you choose to create a
/usr/local or /opt partition, its size should be appropriate to the software you plan
to install.
Table 2-1 gives guidelines for minimum sizes for partitions used by Linux. Set the
sizes of other partitions, such as those for /home, /opt, and /usr/local, according to
need and the size of the hard disk. If you are not sure how you will use additional
disk space, you can create extra partitions using whatever names you like (for
example, /bOl, /b02, and so on). Of course, you do not have to partition the entire
drive when you install Linux; you can wait until later to divide the additional space
into partitions.
Table 2-1

Example minimum partition sizes 3

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

40

CHAPTER 2

INSTALLATION OVERVIEW

Table 2-1

Example minimum partition sizes 3 (continued)

Partition

Example size

/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; depends on which and how many software packages you install

/var

Minimum of 500 megabytes—much larger if you are running a server

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

RAID

RAID (Redundant Array of Inexpensive/Independent Disks) employs two or more
hard disk drives or partitions in combination to improve fault tolerance and/or performance. Applications and utilities see these multiple drives/partitions as a single
logical device. RAID, which can be implemented in hardware or software (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 hardware 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 does not replace backups
caution The purposes of RAID are to improve performance and/or to minimize downtime in the case of a
disk failure. RAID does not replace backups.
Do not use RAID as a replacement for regular backups. If the system experiences a catastrophic
failure, RAID is useless. Earthquake, fire, theft, and other disasters 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 replacing a file when a user deletes it by accident. In these situations, a backup on a removable medium (which has been removed) is the only way you will be able to restore a filesystem.
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 Oxfd (raid autodetect).
Software RAID, as implemented in the kernel, is much cheaper than hardware
RAID. Not only does this approach avoid the need for specialized RAID disk controllers, but it also works with the less expensive ATA disks as well as SCSI disks.

PLANNING THE INSTALLATION
Fake RAID

41

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

LVM: LOGICAL VOLUME MANAGER
The Logical Volume Manager (LVM2, which this book refers to as LVM) allows
you to change the size of logical volumes (LVs, the LVM equivalent of partitions) on
the fly. With LVM, if you make a mistake in setting up LVs or if your needs change,
you can make LVs either smaller or larger without affecting user data. You must
choose to use LVM at the time you install the system or add a hard disk; you cannot
retroactively apply it to a disk full of data. LVM supports IDE and SCSI drives as
well as multiple devices such as those found in RAID arrays.

42

CHAPTER 2

INSTALLATION OVERVIEW

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.
An LV is similar in function to a traditional disk partition in that you can create a filesystem on an LV. It is much easier, however, to change and move LVs than partitions:
When you run out of space on a filesystem on an LV, you can grow (expand) the LV
and its filesystem into empty or new disk space, or you can move the filesystem to a
larger LV. For example, you can add a hard disk to a system and incorporate it into an
LV to expand the capacity of that LV. LVM's disk space manipulation is transparent
to users; service is not interrupted.
LVM also eases the burden of storage migration. When you outgrow the PVs or
need to upgrade them, LVM can move data to new PVs. To read more about LVM,
refer to the resources listed on page 26.

THE INSTALLATION PROCESS
The following steps outline the process of installing Ubuntu Linux from a CD/DVD.
See Chapter 3 for specifics.
1. Make sure the BIOS is set to boot from the CD/DVD (page 28). Insert the
installation CD/DVD in and reset the computer. The computer boots from
the CD/DVD and displays a language overlay (Figure 3-1, page 52) over a
disk menu (Figure 3-3, page 54).
2. You can press function keys to display options, make a selection from the
disk menu, and begin bringing up a live session or installing Ubuntu when
you are ready. With a live/install DVD, you can also do nothing: A
live/install DVD starts to bring up the system after 30 seconds. When the
Welcome screen appears, click Try Ubuntu to bring up a live session or

DOWNLOADING AND BURNING A C D / D V D

43

click Install Ubuntu to begin installation. The installation CDs wait 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 1168) that it uses in place of the
hard disk used for a normal boot operation. The installer copies tools
required for the installation or to bring up a system from a live/install
Desktop CD or a live/install DVD to the RAM disks. The use of RAM
disks allows the installation process to run through the specification and
design phases without writing to the hard disk and enables you to opt out
of the installation at any point before the last step of the installation. If
you opt out before this point, the system is left in its original state. The
RAM disks also allow a live session to leave the hard disk untouched.
4. The installer prompts you with questions about how you want to configure
Ubuntu Linux.
5. When the installer is finished collecting information, it displays the Ready
to install screen (Figure 3-9, page 63). When you click Install, it writes the
operating system files to the hard disk.
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 ISO 9660 standard that defines the CD filesystem). This section describes how to download one of
these images and burn a CD/DVD. You can also purchase a CD/DVD from a Web
site. If you cannot obtain Ubuntu by any other means, you can point a browser at
shipit.ubuntu.com to display a Web page with links that enable you to request a free
CD from Ubuntu (but first read blog.canonical.com/?p=264).

THE EASY WAY TO DOWNLOAD A CD I S O IMAGE FILE
This section explains the easiest way to download a CD ISO image file. This technique works in most situations; it is straightforward but limited. For example, it
does not allow you to use BitTorrent to download the file nor does it download a
DVD image.

You can find ISO images for all supported architectures here
tip If you cannot find an ISO image for a CD that supports the type of hardware you want to install
Ubuntu on, go to this site: cdimage.ubuntu.com/ports/releases/10.04/release.

44

CHAPTER 2

INSTALLATION OVERVIEW

fife Edit View Mitny Book marts
O Ubuntu IWt««

y
'J

+

Ubuntu Releases
The (ofcfivng r«lc«n>s or Ubuim arif nviUiUit:
• Ubuntu 6.06.2 LTS (Dapper DialuO
. Ubuntu 8 04.4 LT3(HiVrJ>C.:.rn..j
* Ubuntu 9.04 (Jaunty JackalOM)
« Ubuntu 5 10 (Knimic Koala)
• Ubuntu 10.W LT5(Ltic-d Lynn)
Releases of Kub until and Cdubuntu are also available here.
We aie happytopiovnte Irortno for Ow iolowina projects via the crijrnaoe sctwr
tfiey
Whare
* tKX cotnmorciafy
supported by Canonical Biey »ecewe ful supporttoomfrieitcommunities.
• Myth bunlu
* UbuntuSfcidio
• XubutHu
For OM rcteasci, m;« oMrelcascs.ubunau con.
t—«
LD 13.IM/
i.i 36 2/
C] !l5B/
Q 0 'MJ/
D DW
'III 9 W
C3 9 UW
• dJflflttf
L3 tiAmtu/

F~i

Figure 2-3

Liil »t-fcfud

Sm tWsi nplton

S "(Ir 3010 09: 22
Lfcurtu 10. M LTSftjjiiJLpU)
01 Oct 3009 02: 38
Lbuftu Î.B.ÎltS ID*»«' Dr»t«J
01 Oct 3009 02»
»->*n-»](l llr»
t*uniu 1M 4 ITS until 10.04 LTS [Lucid Lynx) -..UFirrt«
Men
(lit ytoi Hijbtfy (ntmuU Rdj QCp
t

•

* C WFTC HTPY^J-FT^^FTGTWM
I- XJUFFFALUÖ^

C Utnflu 10 04 US (Luüd Lfrul

?

J

45


Figure 3-5

The symbols on the initial boot screen

THE LIVE/INSTALL DESKTOP CD
When you boot from a live/install desktop CD, Ubuntu displays the initial boot
screen, a mostly blank screen with keyboard layout and accessibility symbols at the
bottom (Figure 3-5). While the initial boot screen is displayed, whether you press a
key determines what happens next. If you do not press a key, after a few seconds
Ubuntu displays a logo and progress dots and then displays the Welcome screen of
the Install window (Figure 3-2). To select a default language other than English
from this screen, see "Changing the default language" on page 57.
Bring up a live

session

From the Welcome screen, click Try Ubuntu 10.04 to bring up a live session running
GNOME desktop (Figure 3-4).

a

Check the CD/DVD for defects
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 due to bad media. Whether you burned your own
CD/DVD, purchased it, or are using the disk included with this book, it is a good idea to verify that
the contents of the CD/DVD is correct.
With the DVD menu screen or one of the CD menu screens displayed, use the ARROW keys to highlight Check disc for Defects 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 reboot the system.
Install Ubuntu

From the Welcome screen, click Install Ubuntu 10.04 to install Ubuntu on the hard
disk; continue with the Where are you? screen as described on page 59.
If you press a key while the initial boot screen is displayed, Ubuntu displays the language overlay covering the Desktop CD menu. This screen looks similar to
Figure 3-1 on page 52 except that no countdown timer is visible because the system
is not counting down; instead it is waiting for your input. You can use the ARROW

B O O T I N G FROM A L I V E / I N S T A L L D E S K T O P C D OR A L I V E / I N S T A L L D V D

57

keys to highlight a language for the installer to use and press RETURN to select the language and expose the CD menu screen (similar to Figure 3-3 on page 54). On this
screen you can use the ARROW keys to highlight a selection and press RETURN to make
the selection. For more information refer to "Advanced Installation" on page 77.

THE WELCOME SCREEN
Two varieties of the Welcome screen exist. One screen, shown in Figure 3-2, allows
you to choose between bringing up a live Ubuntu system and installing Ubuntu on
the hard disk. It has two buttons: Try Ubuntu 10.04 and Install Ubuntu 10.04. The
other screen, which is similar to the one shown in Figure 3-2, simply marks the start
of the installation process. It has three buttons: Quit, Back (grayed out and nonfunctional because you cannot go back from this screen), and Forward.
Quit button

When you click Quit, Ubuntu displays a GNOME desktop running under a live
session (Figure 3-4, page 55).

Changing the default Along the left side of both Welcome screens is a list box (page 1157) that holds a list
language of languages. The highlighted language is the language the live session or the
installer/installed system will use. If the highlighted language is not the language
you want, use the ARROW keys or the mouse to highlight your desired language before
proceeding. See "The Function Keys" on page 79 for information about changing
the language, keyboard layout, and accessibility features used by a live session and
the installer/installed system.

optional S E E I N G W H A T I s G O I N G O N
If you are curious and want to see what Ubuntu is doing as it boots, perform an
advanced installation (page 77) and remove quiet and splash from the boot command
line (Figure 3-22, page 81): With the DVD menu screen or one of the CD menu
screens displayed, press F6 to display the boot command line and a drop-down list.
Next press ESCAPE to close the drop-down list. Then 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 LEFT 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. Press RETURN. Now, as Ubuntu boots, it displays information about what it is
doing. Text scrolls on the screen, although sometimes too rapidly to read.

ubiquity: INSTALLING UBUNTU GRAPHICALLY
This section covers the ubiquity graphical installer, written mostly in Python, that
installs Ubuntu. You can also install Ubuntu using the textual installer (debianinstaller; page 85).

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 hard disk before you start installing Ubuntu. You can use
the palimpsest disk utility to mount partitions on a hard disk. You can then examine the files in
these partitions and see what is on the disk. See page 66 for more information on palimpsest.

58

3

S T E P - B Y - S T E P INSTALLATION

USING THE MOUSE TO WORK WITH THE INSTALL
WINDOW S C R E E N S
You can use either the mouse or the 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-2 on page 53 and Figure 4-16 on page 123) to display more languages; then click the language you want to use. Ubuntu highlights the language
you click. Once you select a language, you are finished working with the Welcome
screen. Click the button labeled Forward or Install Ubuntu 10.04 to display the
next screen.

USING THE KEYBOARD TO WORK WITH THE INSTALL
WINDOW S C R E E N S
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 or Install Ubuntu 10.04 or
Quit, Back, and Forward.
List

box 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 automatically
when you move the highlight to the next, undisplayed entry in the list.

Button

Once you select a language, you are finished working with the Welcome screen. Use
the TAB key to highlight the button labeled Forward or the button labeled Install
Ubuntu 10.04. The button turns orange with an orange border when it is highlighted. Press RETURN to display the next screen.

Drop-down list

To make a selection from a drop-down list, such as the one in the box labeled
Region shown in Figure 2-1 on page 31, use the TAB key to highlight the box and
then use the ARROW keys to move the highlight from one item to the next. With the
selection you want to choose highlighted, press RETURN.

STARTING THE INSTALLATION
This book describes using the mouse to make selections from a graphical interface;
you can use the keyboard if you prefer.
WELCOME SCREEN
The Welcome screen of the Install window (Figure 3-2) contains a welcome message
and a list of languages for you to choose from. The language you choose will be the
one ubiquity uses as you install the system and the default language for the installed
system; you can change this default once the system is installed (page 145). Click
Forward.
Ubuntu displays the Setting up the clock window and, if it can connect to a network
time server, sets the clock. You can click Skip to bypass this step.

BOOTING FROM A LIVE/INSTALL DESKTOP C D OR A LIVE/INSTALL DVD

O ©

59

Install

Keyboard

layout

Which layout is most similar to your keyboard?
t suggested option:
C Guess keymap;

LJSA
Guess.,.

Choose your own:
USA

Thailand

USA - AILsiridliue MiLerik)Uonal (run

lurkey

USA

Turkmenistan

Cherokee

USA - Classic Dvorak

USA

USA - colemak

Ukraine
u n i t e d Kingdom
Uzbekistan

0

USA - Dvorak
USA - Dvorak international
USA - Group roggte on multlply/drvi

TU'U tail Lype inlo II s l>jx Lo LebL you I < •/, keyboard Idyuul.

step 3 nf 7

Figure 3-6

quit

The Keyboard layout screen

WHERE ARE YOU?
As the first step in installing Ubuntu, ubiquity displays the Where are you? screen.
This screen allows you to specify the time zone where the computer is located. You
can use the map or the drop-down lists labeled Region and Time Zone to specify
the time zone. When you click the name of a city on the map, the appropriate region
appears in the box labeled Region and the name of the time zone or a city within
the time zone appears in the box labeled Time Zone.
To use the Region drop-down list, click the down arrow at the right end of the box
labeled Region; ubiquity expands the box into a list of parts of the world. Click the
region you want to select. Now, repeat this process with the box labeled Time
Zone. Click Forward.
KEYBOARD LAYOUT
The Keyboard layout screen (Figure 3-6) allows you to specify the type of keyboard
to be used by the installed system. (See "F3 Keymap" on page 79 to change the layout of the keyboard ubiquity uses during installation.) When ubiquity displays the
Keyboard layout screen, the radio button (page 1167) labeled Suggested option is
selected and the name of a keyboard layout appears to the right of these words. If
the suggested option is acceptable, click Forward.
Anytime the Keyboard layout screen is displayed, you can highlight the text box at
the bottom of the screen and type some letters to see if the selected option is correct
for the keyboard you are using.

60

3

S T E P - B Y - S T E P INSTALLATION

When you select the radio button labeled Guess keymap and click Guess, ubiquity
leads you through a series of questions and, based on your answers, tries to determine which type of keyboard you are using. Click Forward when you are satisfied
with the result.
When you select the radio button labeled Choose your own, ubiquity activates the
two list boxes below these words. Select a country and keyboard type from these list
boxes and click Forward.
PREPARE DISK SPACE
The Prepare disk space screen controls how ubiquity partitions the hard disk. See
page 36 for a discussion of the issues involved in partitioning a hard disk.
GUIDED PARTITIONING
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 partition editor displays a
Prepare disk space screen similar to the one shown in Figure 3-7. In this case, the
simplest way to partition the disk is to allow the ubiquity partitioner to do it for you.
This technique is called guided partitioning. By default, the radio button labeled
Erase and use the entire disk is selected and the name of the only hard disk in the
system is displayed in the drop-down list below these words. If the system has two
or more hard disks, you must select from this list the disk where you want to install
Ubuntu. Click Forward. The ubiquity partition editor creates two partitions on the
hard disk: a small swap partition (page 37) and a root partition (/, page 37) that
occupies the rest of the disk.
The ubiquity partition editor does not partition the disk at this time. At any time
before you click Install on the Ready to install screen, you can change your mind
about how you want to partition the disk. Click the button labeled Back. You may
have to back up through several screens to display the Prepare disk space screen
again, but you can then set up the disk the way you want it.
See "Advanced Guided Partitioning" on page 70 for information on using the other
selections in the Prepare disk space 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 those 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.
Click Forward.

BOOTING FROM A LIVE/INSTALL D E S K T O P C D OR A LIVE/INSTALL D V D

O S Q

61

Install

P r e p a r e disk space
This computer has no operating systems on it.

Where do you want to put Ubuntu 10.04 US?
^ Erase and use the entire disk
SCSI3 (o.o.o) Isdal - 214.7 GB VMware. VMware virtual s

T

Specify partitions manually (advanced)
• Ubuntu 10.04 ITS

step 4 nf 7

Figure 3-7

Quit

Bark

Forward

The ubiquity partition editor showing one empty hard disk

WHO ARE YOU?
The Who are you? screen (Figure 3-8, next page) sets up the first Ubuntu user. This
user can use sudo (page 98) to administer the system, including setting up additional
users (page 594). Enter the full name of the user in the text box labeled What is
your name?. As you type, ubiquity enters the first name from the name you are entering in the box labeled What name do you want to use to log in?. Press TAB to move
the cursor to this box. If you want to use a different username, press BACKSPACE
(page 151) to erase the username and enter a new one. Press TAB.
Enter the same password in the two (adjacent) boxes labeled Choose a password to
keep your account safe. The strength of the password is displayed to the right of the
password boxes. Although ubiquity accepts any password, it is a good idea to choose
a stronger (more secure) password if the system is connected to the Internet. See
"Changing Your Password" on page 148 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 fox8. If you are setting up a server system, see " F Q D N " on
page 823 for information on names that are valid on the Internet.
The three radio buttons at the bottom of the window configure the login process for
the user you are specifying. Select Require my password to log in to cause Ubuntu
to require a password for you log in on the system.
Select Require my password to log in and to decrypt my home folder if you are setting up an encrypted home folder.

62

3

STEP-BY-STEP

INSTALLATION

O S O

Install

W h o are you?
What is your name?
[Sam Smith

)

What name do you want to use to log in?

flam

] 4

•r m u t e Hid 11one [ - ' M > i mill Jit' U i s L'j i r ip u Lt , y uu < III act. u p multiple d t t u u n l s tiHei
installation.

Choose a password to keep your account safe.
•

strength: weak

What is the name ol llns computer?
from

4

T h i s n a m e will be u s e d if y o u m a k e the c o m p u t e r v i s i b l e t o o t h e r s a n a network.

O Loq in automatically
& It'iiti'i' my passwuid to log in
Require my password to log in and to decrypt my home tolder
step 5 of 7

Figure 3-8

Quit

Bark

Forward

The Install window, Who are you? screen

Select Log in automatically if you want Ubuntu to log you in automatically when
the system boots—select this option only if you trust everyone who has physical
access to the system. Click Forward.
READY TO INSTALL
The final screen ubiquity displays is the Ready to install screen (Figure 3-9). At this
point, the ubiquity partition editor has not yet written to the disk. Thus, if you click
Quit 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 to set up a network proxy
(page 405). Click OK to close the Advanced Options window. If everything looks
right in the summary, click Install. The installer begins installing Ubuntu on the
hard disk.

When ubiquity writes to the hard disk
caution You can abort the installation by clicking the Quit button at any point, up to and including when
the Ready to install screen (Figure 3-9) is displayed, without making any changes to the hard disk.
Once you click Install in this screen, ubiquity writes to the hard disk.
The ubiquity installer displays messages 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 (Continue Testing) or

GRAPHICAL PARTITION EDITORS

O S Q

63

Install

Ready to install
Your new operating system will row be installed with the following settings:
Language: English
Keyboard layout: USA
Name: Sam Smith
Login name: sam
Location: Amcrica/Los_Angclc5
Migration Assi stank

If you continue, the changes listed below will be written to the disks.
Otherwise, you will be able to make further changes manually.
'N: partition tables of the following devices are changed:
SCSI 3 (0,0,0) (sda)
Ttie following partitions are going to be formatted:
partition #1 ot SCSI3 (0,0,0) (sda) as ext4
partition #5 ot SCSI3 (0,0,0) (sda) as swap
Advanced...
step I nf 7

Figure 3-9

Quit

Hack

Install

The Install window, Ready to install screen

rebooting the system so you can use the newly installed copy of Ubuntu. Click
Restart N o w to reboot the system.
The installer displays the Ubuntu logo and progress dots. When it has finished shutting
down the system, it asks you to remove the disk (so you do not reboot from the
CD/DVD) and press RETURN. After you complete these steps, Ubuntu reboots the system
and displays the Ubuntu GNOME login screen (Figure 4-1, page 100).
Log in as the user you specified on the Who are you? screen and continue with
Chapter 4.

GRAPHICAL PARTITION EDITORS
A partition editor displays and can add, delete, and modify partitions on a hard
disk. This section describes three graphical partition editors you can use to configure a hard disk in the process of installing Ubuntu. The gparted and palimpsest partition editors are available from a live session. The other partition editor is part of
the ubiquity installer and is not available by itself. See page 87 for information on
using the textual partition editor, which is available when you use the textual
installer. After you install Ubuntu Linux, you can use parted (page 611) or palimpsest (page 66) to view and manipulate partitions. The gparted partition editor is not

64

3

S T E P - B Y - S T E P INSTALLATION

•Cf Applications Places SystemJ

•

Examples

Preferences

*

Administration

•

Help and Support

Computer Janitor
•

Disk Utility
GParted

About GNOME

£ Hardware Drivers

About Ubuntu

£ 5 Install Ubuntu 10.04 LIS
fp Language Support
^

Log file Viewer
Login Screen
Network Tools

Figure 3-10 Selecting gparted from the Main menu
available from an installed system unless you install the gparted package
(page 519). If you want a basic set of partitions, you can allow ubiquity to partition
the hard disk automatically using guided partitioning.
See "Setting Up the Hard Disk" on page 33 for a discussion of free space, partitions, partition tables, and filesystems. "Manual Partitioning: Planning Partitions"
on page 37 discusses some of the filesystems for which you may want to set up partitions if you choose to partition the hard disk manually.
Unless you are certain 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 disk before you start the installation. The gparted and palimpsest partition editors, which are available from a live session, are good tools for
this job.

gparted: THE G N O M E PARTITION EDITOR
Open a GParted window by selecting Main menu: SystemOAdministrationO
GParted as shown in Figure 3-10.
The gparted utility displays the layout of the hard disk and can be used to resize partitions, such as when you are setting up a dual-boot system by adding Ubuntu to a
Windows system (page 76). Although you can create partitions using gparted, you
cannot specify the mount point (page 35) for a partition—this step must wait until
you are installing Ubuntu and using the ubiquity partition editor.

AN EMPTY HARD D I S K
The gparted utility shows one large unallocated space for a new hard disk (empty,
with no partition table). An exclamation point in a triangle is a warning; on a new
disk it indicates an unrecognized file system (there is no partition table). If you have
more than one hard disk, use the list box in the upper-right corner of the window to
select which disk gparted displays information about. Figure 3-11 shows an empty

GRAPHICAL PARTITION EDITORS

65

© 9 ® /dev/sda - GParted
GParted Edit View Device Partition Help
/dev/sda (200.00 GiB) *
unal located
200.00 Gi8
Partition
Filesystem
unallocated A H unallocated

Size
200.00 GIB

used

Unused

Flags

—

[o operations pending

|

Figure 3-11 The gparted utility displaying an empty disk drive
200-gibibyte (page 1150) hard disk on the device named /dev/sda. Figure 3-7 on
page 61 shows the ubiquity partition editor ready to partition an empty drive similar
to the one shown in Figure 3-11.

R E S I Z I N G A PARTITION
Although you can resize a partition using the ubiquity partition editor while you are
installing Ubuntu, you may find it easier to see what you are doing when you use
the gparted partition editor from a live session for this task. This section explains
how to use gparted to resize a partition.

Always back up the data on a hard disk
caution

If you are installing Ubuntu on a disk that holds important data, 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 partitioning software that
destroys a filesystem. Although it is unlikely, you might make a mistake and format a partition
holding data you want to keep.
Figure 3-12 (next page) shows gparted displaying information about a hard disk
with a single partition that occupies the entire disk. This partition holds a single
200-gibibyte NTFS filesystem. The process of resizing a partition is the same
regardless of the type of partition, so 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. To resize the partition, right-click to highlight the line that
describes the partition and click the arrow pointing to a line on the toolbar at the
top of the window. The partition editor opens a small Resize/Move window, as
shown in Figure 3-12.
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 N e w Size ( M i B )

66

3

S T E P - B Y - S T E P INSTALLATION

* -• " /dev/sda - GParted
GParted Edit View Device Partition Help
© NN

te

/dev/sda (200.00 GiBi Y

/dev/sda 1
200,00 GiB
Paitilion
File System Label
/dcv/srial | riffs Windows
0

Used
so.b'JMiB

Unused
i0lJ.03GiB

Flags

Resize/Move /dev/sdai

«
0 operations pend

Size
200.00 GÎK

Mn
im
i um size: 86 M¡8
Maximum size: 201797 MiB
Free space preceding (MiB): 10
New (MiB):
I 10OOOO
Free space folowing (MiB): 104797*
¿i Fiourvd to cylinders
Cancel

nesi7e/Move

Figure 3-12 The gparted partition editor displaying a disk drive
holding a Windows system
shows the number of mebibytes occupied by the partition—in this case, the whole
disk. The two spin boxes labeled Free Space show no free space.
You can specify how the partition should be resized 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 mebibytes you want to shrink the Windows partition to in
the spin box labeled New Size (MiB). The value in one of the spin boxes labeled Free
Space increases when you make this change (as shown in Figure 3-12). Click
Resize/Move to add the resize operation to the list of pending operations at the bottom of the window. Click the green check mark on the toolbar to resize the partition.

DELETING A PARTITION
Before you delete a partition, make sure it does not contain any data you need. To
use gparted to delete a partition, highlight the partition you want to delete, click the
circle with a line through it, and then click the green check mark on the toolbar.

palimpsest: THE G N O M E DISK UTILITY
The palimpsest graphical disk utility can create, remove, and modify partitions and
filesystems on many types of media, including internal and external hard disks,
CD/DVDs, and USB flash drives. It can encrypt partitions and change passwords on
already encrypted partitions.
Open the Palimpsest Disk Utility window by selecting Main menu: System^
AdministrationODisk Utility (just above GParted in Figure 3-10 on page 64). To

GRAPHICAL PARTITION EDITORS

67

6 0 A 2IS GB Hard DLTK (VMwar*. VMi«w< Virtual St tidavMa] — DISK Utility
file he*0

Figure 3-13 The palimpsest Disk Utility window
display information about a hard disk, click a hard disk under Storage
Devices/Peripheral Devices on the left side of the window
With a hard disk selected, the palimpsest Disk Utility window is divided into three
sections (Figure 3-13): Storage Devices holds a list of CD/DVD drives, hard disks,
and other devices; Drive holds information about the hard disk that is highlighted
in the list of storage devices; and Volumes displays information about the partition
that is highlighted in the graphical representation of the hard drive.
When you select a hard disk in the Storage Devices section, palimpsest displays
information about that disk in the Drive section of the window. Click one of the
partitions in the graphical representation of the hard disk and palimpsest displays
information about that partition in the Volumes section.
From this window you can view, create, and delete partitions. Although you can
create partitions using palimpsest, you cannot specify the mount point (page 35) for
a partition—this step must wait until you are installing Ubuntu and using the ubiquity partition editor. You can save time if you use palimpsest to examine a hard disk
and ubiquity to set up the partitions you install Ubuntu on.

DISPLAYING THE CONTENTS OF A FILESYSTEM
To display the contents of a filesystem, select the partition holding the filesystem as
described above and click Mount Volume in the Volumes section of the Disk Utility
window. Figure 3-13 shows Unmount Volume because the partition is already
mounted. When palimpsest mounts the highlighted filesystem, the mounted filesystem appears as a directory (folder) on the desktop. When you click the mount
point (the link following Mount Point: mounted at) in the Volumes section or
double-click the directory icon on the desktop, Nautilus displays the filesystem in a
file browser window (page 107). When you have finished examining the contents of
the filesystem, click Unmount Volume to unmount the filesystem.

68

3

S T E P - B Y - S T E P INSTALLATION

Figure 3-14 The palimpsest Disk Utility showing a disk without a partition table

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

CREATING A PARTITION AND A FILESYSTEM
Once you have created a partition table, you will be able to create a partition that
holds a filesystem in the free space. When you click Create Partition, palimpsest
opens a Create partition window (Figure 3-15).

GRAPHICAL PARTITION EDITORS

fi

69

Create partition on VMware, VMware Virtual S
100 GB

Size:
Type:

100.000 GB

Ext4

;
¥

1

This file system is compatible with Linux systems only
and provides classic UNIX file permissions support.

Name:

vol l]

Ul Take ownership of filesystem
Encrypt underlying device
Cancel

Create

Figure 3-15 The palimpsest Create partition window
In this window, use the slider labeled Size, or the adjacent spin box, to specify the
size of the new partition. Next specify a filesystem type; ext4 filesystems are the
most common. You can optionally enter a disk label in the text box labeled Name.
This name is not the mount point for the disk. Typically you will want to own the
filesystem, so allow the tick to remain in the check box labeled Take ownership of
file system. If you want the filesystem to be encrypted, put a tick in the check box
labeled Encrypt underlying device. Click Create. After checking with you, palimpsest creates the filesystem. Now the graphical representation of the disk is divided
to represent the division of the hard disk and Usage corresponds to the highlighted
section of the graphical representation (Filesystem or Unallocated Space). If you did
not use all the free space, you can create additional partitions and filesystems in the
same manner.

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

USING S M A R T TO DISPLAY D I S K PERFORMANCE INFORMATION
SMART (Self-Monitoring, Analysis, and Reporting Technology) monitors hard
disks and attempts to predict hard disk failures. To see a SMART report for a disk
on the system, highlight the disk in the Storage Devices section and click Smart Data
in the Drive section; palimpsest displays a window similar to the one shown in

70

3

S T E P - B Y - S T E P INSTALLATION

Updated:

3 minutes ago

Self-tests:

Completed OK

Powered On:

72.5 days

Power Cycles:

956

Temperature:

24° C / 75° T

Dad Sectors:

None

Gelt Assessment:

Passed

Overall Assessment:

• Disk is healthy

Refresh
Reads SMART Data, waking up the disk

2

Run Self-test
Test the disk surface tor errors

ID

Attribute

1

R e a d brror K a t e
Frequency of errors while reading raw riara from
• Gond
the disk. A non zero value indicates a problem with
either liie disk surf ate or lead/write heads

Assessment Value

7

Throughput Performance
Average efficiency of the disk

• Gond

3

Spinup Time
lime needed to spin up the disk

• Good

start/stop count

Normalized:
Worst
Threshold:
Value:

100
100
46
141178

Normalized:
MnsL
Threshold:
Value:
Normalized:
Wjrst:
Threshnld
Value:
Normal&ed"
Worst:

100
100
30
N/A
100
100
2 msec
99
99

Don't warn if the disk is failing

Figure 3-16

SMART data as displayed by palimpsest

Figure 3-16. From this window you can run various self-tests and scroll through the
information at the bottom of the window.

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 37) and one for / (root, where the entire Ubuntu filesystem gets mounted;
page 37). 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; you can also specify the mount point for each partition.

ADVANCED GUIDED PARTITIONING
"Prepare Disk Space" on page 60 explained how to use guided partitioning to partition an empty disk. This section explains how guided partitioning works on a disk
that is already partitioned.
Depending on the contents of the hard disk you are installing Ubuntu on, the ubiquity partition editor presents different choices. Figure 3-17 shows the Prepare disk
space screen for a hard disk with one partition that occupies the entire disk. That
partition holds a Windows system. This screen shows all possible choices. In some
cases, not all of these choices appear. Click the radio button adjacent to a choice to
select it. The choices possible are outlined here:
• Install them side by side, choosing between them each startup—Allows
you to shrink a partition and use the space freed up by this operation to
install Ubuntu. You can use this choice to set up a dual-boot system

G R A P H I C A L PARTITION E D I T O R S

O S Q

Figure 3-17

71

Install

The Prepare disk space screen showing a hard disk with a Windows
partition occupying the entire disk

(page 76) on a system where a single Windows partition occupies the
whole disk. This section describes how to use the ubiquity partition editor
to resize a partition. See "Resizing a Partition" on page 65 for instructions
on using gparted to resize a partition.
This choice includes a slider with a handle that allows you to specify how
you want to resize the partition. See Figure 3-17. Click and drag the handle to specify the new size of the partition you are resizing and, by default,
the size of the new partition where Ubuntu will be installed.
• Erase and use the entire disk—Deletes all information on the disk and
installs Ubuntu on the entire disk. After deleting information from the
disk, this choice uses guided partitioning as explained on page 60.
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 will be gone for good. If you are not sure about
the contents of the disk, use palimpsest (page 66) to take a look.
• Use the largest continuous free space—Installs Ubuntu in the largest chunk
of free space on the disk. Because free space holds no data, this technique
does not change any data on the disk. This choice uses guided partitioning
(as explained on page 60) on the free space. If an operating system occupies the existing partition, this choice sets up a dual-boot system (page 76).

72

3

STEP-BY-STEP

INSTALLATION

O Q 9

Install

Prepare

partitions

• Free spdLe
214.7 GB

Add...
step 5 of R

Figure 3-18

Change.

| Delete
quit

Reuert
Rack

Forward

An empty hard disk with a partition table

• Specify partitions manually (advanced)—Gives you total control over the
size, placement, and naming of partitions where Ubuntu is installed. See
the next section.
When you are done working with the Prepare disk space screen, click Forward.

MANUAL PARTITIONING
This section explains how to use the ubiquity partition editor to create a partition on
an empty hard disk. Figure 3-7 on page 61 shows the Prepare disk space screen for
an empty hard disk. To create partitions manually, select Specify the Partitions
Manually (advanced) and click Forward. The ubiquity partition editor displays a
Prepare partitions screen that shows a device without any partitions—only free
space. Before you can create partitions, you must set up a partition table (page 33).
To do so, highlight the device name (e.g., /dev/sda) and click New partition table.
The partition editor asks you to confirm that you want to create a new, empty partition table. Click Continue to create a partition table that contains only free space.
Now ubiquity displays a screen that looks similar to the one in Figure 3-18. (Note:
214,748 MiB [mebibytes; page 1159] equals 200 GB [gigabytes; page 1150]—
Figure 3-18 should show MiB in place of MB.) The hard disk at /dev/sda has a partition table without any partitions; it contains only free space.
To create a partition, highlight the line containing free space in the Device column
and click Add. The ubiquity partition editor displays a Create partition window
(Figure 3-19), which asks you to specify whether you want to create a primary or a
logical partition (page 34), what size the partition should be (in megabytes),
whether the partition should appear at the beginning or end of the free space, what

GRAPHICAL PARTITION EDITORS

0

73

Create partition

Create a new partition
Type tor the new partition:

$ Primary

New partition si7e in megabytes (1000000 byres):

100000

Location for the new partition:

$ Beqinninq

Use as:

Logical
T
End

Ext4 joumaiing file s ystcm

Mount point:

H

|/horne
Cancel

OK

The Create partition window

Figure 3-19

type the partition should be (Use as), and what name the mount point (page 35) for
the partition should have. Because Linux does not mount a swap partition, you cannot specify a mount point for a type swap partition. If you are unsure of which type
a partition should be, choose ext4 (page 505). Click OK.
After a few moments the Prepare partitions screen displays the new partition
(Figure 3-20). To create another partition, highlight the line containing free space
and repeat the preceding steps. Remember to create a swap partition (page 37).
When you have finished creating partitions, click Forward.
The Prepare partitions screen displays five buttons immediately below the frame
that lists the disks and partitions. Some of these buttons are grayed out (inactive)
Q 9 9

Install

Prepare partitions

• s d a l (exl4)
100.0 GB
Devtce
|

• Free spate
114.7 GB

Type Mount point Format? Size

Used

h

/riev/sria
/dev/sdal ext4 /home
free space

•s

| New Partition Table...] |AdcL, | Change...
|
step 5 Of 8

Figure 3-20

99998 MB unknown
114748 MB

Delete
Quit

Revert
Bark

F onward

The Prepare partitions screen displaying a new partition

74

3

S T E P - B Y - S T E P INSTALLATION

depending on what is highlighted in the frame above. The button labeled Revert is
always active. When a device is highlighted, the button labeled New Partition Table
is active. Clicking this button creates a new partition table, thereby 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 re-create a partition to change any of
its other attributes. As mentioned earlier, highlighting the line containing 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 advises against
upgrading systems that have had packages installed from repositories (page 522) it does
not control. These packages may corrupt the software package database, causing the
upgrade to fail. For release notes that detail features that will not take effect with an
upgrade, see www.ubuntu.com/getubuntu/releasenotes.

Use a standard upgrade procedure
caution Do not use procedures for upgrading to a new release of Ubuntu other than the ones specified in
this section or atwww.ubuntu.com/getubuntu/upgrading. Specifically, do not use apt-get distupgrade, aptitude full-upgrade, or any Debian tools.

Upgrading from an LTS release to a non-LTS release
tip When you start the Update Manager from the Main menu, it does not offer you the option of
upgrading from an LTS release to a non-LTS release. To upgrade from an LTS release to a nonLTS release, you must enter update-manager - c on a command line (from a terminal emulator or
Run Command window [ALT-F2]) to open the Update Manager window.

Watch out for Pop-up windows
tip During the installation phase of an upgrade, some packages open windows that ask questions
about how you want to handle the upgrade of a package. These windows can be hidden by other
windows on the workspace. If the upgrade stops for no apparent reason, drag windows around to
see if a window with a question in it is hidden below another window. When you respond to the
question, the upgrade will continue.
Before you upgrade a system, it is a good idea to back up all user files on the system.
Also make sure the drop-down list labeled Show new distribution releases in the
Updates tab of the Software Sources window (page 132) displays the type of release
you want to upgrade to.
The following procedure assumes you have a desktop system connected to the Internet. Even with a fast Internet connection, this process takes a long time. Follow
these steps to upgrade a system:

INSTALLING K D E

75

1. Open the Update Manager window (Figure 4-11, page 112) by selecting
Main menu: System •=> Administration1^ Update 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 upto-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.
4. At this point, if a new release is available, the window displays the message New distribution release 'XX.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
upgrading Ubuntu.

for more detailed instructions on

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
burning 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 plus a host of other programs (e.g., Amarok, Kate) in addition to
GNOME. After you install Ubuntu as explained in this chapter, use Synaptic
(page 133) or aptitude (page 526) 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 526). To use aptitude, give the
command sudo aptitude install kubuntu-desktop from a command line,
terminal emulator, or Run Application window (ALT-F2).

76

3

S T E P - B Y - S T E P INSTALLATION

After the software is downloaded, while it is being installed, debconf asks if you
want to use the gdm (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 145 to display the Sessions drop-down list and select the session you want to run (GNOME or KDE).

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,
thereby creating 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 following paragraphs discuss these options in order from easiest to
most difficult.
Add a new hard disk

Add another hard disk to the system and install Linux on the new disk, which
contains only free space. This technique is very easy and clean but requires a new
hard disk.

Use existing
free space

If there is sufficient free space on the Windows disk, you can install Linux there.
This technique is the optimal choice, but there is rarely enough free space on an
installed Windows system to use it.

Resize Windows
partitions

Windows partitions typically occupy the entire disk, making resizing a Windows
partition the technique most commonly used to free up space. Windows systems
typically use NTFS, FAT32, and/or FAT16 filesystems. You can use the gparted partition editor to examine and resize an existing Windows partition to open up free
space in which to install Linux (page 65). You can also use the ubiquity partition editor while you are installing Ubuntu for the same purpose. See "Install them side-byside..." on page 70.

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

ADVANCED INSTALLATION

77

preserve the data by moving it from the partition you will delete to another Windows partition.
Once you are sure a partition contains no useful information, you can use a gparted
(page 66) or palimpsest (page 69) to delete it . After deleting the partition, you can
install Ubuntu Linux in the free space opened by removal of the partition.

INSTALLING UBUNTU LINUX AS THE SECOND OPERATING SYSTEM
When enough free space on a Windows system is available (see the previous section), you can install Ubuntu Linux. On the ubiquity Prepare disk space screen, select
Use the largest continuous free space (page 71). Alternatively, if you are installing
Ubuntu on its own hard disk, select Erase and use the entire disk (page 71) and click
the radio button next to the disk you want to install Ubuntu on. Click Forward.
After the installation is complete, when you boot from the hard disk, you will be
able to choose which operating system you want to run.

ADVANCED INSTALLATION
This section explains how to install Ubuntu from each of the four disk menus: the
DVD menu, the Desktop CD menu, the Alternate CD menu, and the Server CD
menu. It also describes using the Ubuntu textual installer from the DVD.
Each menu screen includes a menu centered on the screen and a list of function key
names and labels along the bottom. Figure 3-3 on page 54 shows the DVD menu
screen.
The DVD menu

The Ubuntu DVD includes most of the selections from each of the CDs 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 Desktop CD
menu

The Desktop CD can bring up a live session, install Ubuntu on a hard disk, and rescue a broken system.

The Server CD menu

The Server CD uses the textual installer (page 85) to install a minimal system with a
textual interface and no open ports. The installed system is appropriate for a server.

The Alternate CD
menu

The Alternate CD uses the textual installer (page 85) to install a system that uses a
graphical interface or one that uses a textual interface. It is not a live CD (i.e., it
does not bring up a desktop to install from). The textual installer does not require
as much RAM to install Ubuntu and presents more installation options than the
graphical installer.
Ubuntu displays the language overlay (page 54) on top of each of these four menus.
After you select a language from the overlay, you can work with the disk menu. The
language you select from the language overlay is the default language. As you install
Ubuntu, you can change the default language from the Welcome screen (page 58).

78

3

S T E P - B Y - S T E P INSTALLATION

THE DISK MENU SCREENS
Each of the four disk menus holds different selections. In addition, the F4 key displays different selections from each of these menus. This section discusses each of
the menu selections and describes what happens when you press each of the function keys from each of these menus. The final part of this section covers boot command-line parameters.

MENU SELECTIONS
The Minimal CD (page 32) does not display a menu, but rather displays a boot:
prompt. Enter linux RETURN to start a textual installation from this disk or enter help
RETURN to display more information.
Table 3-1 details the menu selections available from each installation disk. The following paragraphs describe what each menu selection does. With the Try Ubuntu
without installing and Install Ubuntu selections, you can further modify the installation by pressing F4. Pressing F4 while Install Ubuntu in text mode is highlighted on
the DVD menu also modifies the installation. See "F4 Modes" on page 80 for more
information.
Try Ubuntu without installing—Boots to a live session (page 52). You can install
Ubuntu from a live session.
Install Ubuntu—Boots an X session with the Metacity window manager and ubiquity
installer, rather than launching a full GNOME desktop. For systems with minimal
RAM, this selection installs Ubuntu more quickly than installing from a live session.
Table 3-1

Menu selections on Ubuntu CD/DVD

DVD

Desktop

Try Ubuntu without installing

X

X

Install Ubuntu

X

X

Install Ubuntu in text mode

X

Check disc for defects

X

X

X

X

Test memory

X

X

X

X

Boot from first hard disk

X

X

X

X

Rescue a broken system

Xa

Xa

Xa

Install Ubuntu Server

Xa

Xa

Install Ubuntu Enterprise Cloud

X

Xa

Name of CD/DVD

Server

Alternate

Menu selection

a. R u n s i n t e x t m o d e .

Xa

ADVANCED INSTALLATION

79

Install Ubuntu in text mode—Installs a graphical Ubuntu system using the debianinstall textual installer. For more information refer to "The Ubuntu Textual
Installer" on page 85.
Check disc for defects—Verifies the contents of the CD/DVD you are booting from;
see the tip on page 56. Ubuntu reboots the system after checking the disk.
Test memory—Runs m e m t e s t 8 6 + , a GPL-licensed, stand-alone memory test utility
for x86-based computers. Press C to configure the test; press ESCAPE to exit and
reboot. For more information see www.memtest.org.
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 28] is set up).
Rescue a broken system—Provides tools to repair a system that will not boot or that
has a problem with the filesystem mounted at / (root). See page 83.
Install Ubuntu Server—Installs a textual Ubuntu server system using the textual
installer. For more information refer to "The Ubuntu Textual Installer" on page 85.
During the installation, the installer displays the Software selection screen, which
asks if you want to install various servers, including a DNS server (Chapter 24), a
LAMP server (includes Apache [Chapter 26], MySQL [page 628], and PHP), an
OpenSSH server (Chapter 18), a Samba server (Chapter 23), and others. Use the
ARROW keys to move the highlight to the space between the brackets ([ ]) and press the
SPACE bar to select a choice.
Install Ubuntu Enterprise Cloud—Brings up a private cloud. For more information
see www.ubuntu.com/cloud/private.
The boot: prompt You can press ESCAPE from any of these menus to display a b o o t : prompt.

THE FUNCTION KEYS
Along the bottom of each menu screen is a row of labeled function key names.
Pressing each function key displays information or a menu that may be helpful if
you experience a problem while booting Ubuntu or working in a live session. Some
of the keys allow you to change boot parameters.
F1 Help The F1 key displays the help window shown in Figure 3-21 (next page). Pressing a

function key while this window is visible displays yet another help window. Pressing
a function 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 the language overlay (Figure 3-1, page 52). Use the ARROW keys to

highlight the language you want the live session or the installer/installed system to
use and press RETURN. Ubuntu gives you the opportunity to change this selection for
the installed system as you install the system.
F3 Keymap The F3 key displays a country overlay. Use the ARROW keys to highlight the country of

the keyboard layout you want the live session or the installer/installed system to use

80

3

STEP-BY-STEP

INSTALLATION

Welr.nme tn llhuntu!
This i s a l i v e system tor Ubuntu 10.04. I t
mas built nn ?01004i"J.
HtLH INUtX

KRY TOPIC










Tress

This page, the help index.
Prerequisites for running ubuntu.
Oeet methods tor special ways ot using t h i s system.
Additional boot methods; rescuing a broken system.
Special buut paraiiietiirs, overview.
special boot parameters for special machines.
Special boot parameters tor selected disk controllers.
Sper.ia] bnnt parameters fnr the bootstrap system.
Hum Lu ¿el fielp.
copyrights and warranties.
through T10 for details, or Escape to exit help.

F1 Help F2 Language F3 Keymap F4 Mudes F5 Accessibility

Figure 3-21

F6 Other Opliuri^

The Menu screen, F1 help window

and press RETURN. Ubuntu gives you the opportunity to change this selection as you
install the system.
F4 Modes The F4 key displays a different set of startup modes depending on which CD/DVD you
booted from. The F4 key is effective only when either the Try Ubuntu without installing or Install Ubuntu selection is highlighted. An exception is when you are installing
from the live/install DVD and the Install Ubuntu in text mode selection is highlighted. See Table 3-2 for a list of which modes are available from which CD/DVD.
Following is a list of all available modes:
• Normal—Starts Ubuntu in normal mode, as though you had not
pressed F4.
• OEM install (for manufacturers)—Allows a manufacturer or reseller to
preinstall Ubuntu but leaves some configuration details, such as creation
of a user account, to the purchaser.
• Use driver update disk—Installs Ubuntu with an updated driver.
• Install an LTSP server—Installs a Linux Terminal Server Project server. For
more information refer to "Diskless systems" on page 774.
• Install a command-line system—Installs a textual Ubuntu system (no
graphical interface [GUI] or desktop; only a textual interface [page 30]).
• Install a minimal system—Installs the absolute minimum set of packages
required for a working Ubuntu system as specified by the ubuntu-minimal
virtual package (page 526). In earlier releases, this setup was called JeOS;
it is useful for routers and other systems that must occupy minimal disk
space. Contrast a minimal system with the default server system, which
installs additional packages such as Python and rsync.

A D V A N C E D INSTALLATION

Table 3-2

81

F4 selections on the CD/DVD with the Try Ubuntu or Install Ubuntu
selection highlighted
DVD

DVDa

Desktop

Server

Alternate

Normal

X

X

X

X

X

OEM install (for manufacturers)

X

X

X

X

X

Use driver update disk

X

X

Install an LTSP server

X

X

Install a command-line system

X

X

Install a minimal system

X

Install a minimal virtual machine

X
X

Install a server
a. With I n s t a l l i n t e x t m o d e highlighted.

• Install a minimal virtual machine—Installs a virtual machine (page 8) that
will use the least amount of disk space possible.
• Install a server—Installs a Ubuntu server. With this selection you will be
prompted for the type of server(s) you want to install.
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 or the installer/installed system to
use and press RETURN.

F6 Other Options

The F6 key displays part of the boot command line and a drop-down list holding a
menu of parameters (Figure 3-22). Use the ARROW keys to highlight the parameter

Boot c o m m a n d
line^

ubuntu®
T r y Uburilu w i t h u u l iris tailing

^ ^ ^
^s.
^x.

Install tlhiintu
Check disc tor detects
rest memory
Boot from first hard disk

QCOi=Ott
nnapir.
noiapic
cdd=on
nnrlmrairl
nuuiudese t

rinntflptinns =sfiprl/ijhijntij.seeri hnnt=ras|rip.r initrr1=/nfisper/i

l-res s o f t w a r e o n m
| F1 Help

Figure 3-22

F 2 Ls

f

F 3 Kt

p

F 4 Hu

S

F 5 At

The Desktop menu screen after pressing F6

y

F6 Qt

|

82

3

STEP-BY-STEP INSTALLATION

you want to add to the boot command line (discussed in the next section) and press
RETURN to select the highlighted parameter. Press ESCAPE to close the list.
With the drop-down list closed, the ARROW keys can once again be used to move the
highlight on the disk menu; the boot command line changes to reflect the highlighted
selection.
On the Alternate and Server CDs, F6 also offers Expert mode. When you select
this mode, the installer asks more questions about how you want to configure the
system.
One special selection in this menu is Free software only. This selection installs free
software only; it does not install proprietary software, including proprietary device
drivers.
Alternatively, you can enter the parameters you wish to add after the double hyphen
at the end of the displayed portion of the boot command line and press RETURN to
boot the system. If you remove quiet and splash from this line, Ubuntu displays
information about what it is doing while it boots (page 57).

BOOT COMMAND-LINE PARAMETERS (BOOT OPTIONS)
Following are some of the parameters you can add to the boot command line (see
"F6 Other Options" on the previous page). You can specify multiple parameters separated by SPACES. See help.ubuntu.com/community/BootOptions (Common Boot
Options) 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.
nodmraid Disables DMRAID (Device-Mapper Software Raid), also called fake raid (page 41).
The default is to enable DMRAID.
edd=on Enables EDD (BIOS Enhanced Disk Drive services).
noframebuffer Turns off the framebuffer (video memory). Useful if problems occur when the
graphical phase of the installation starts. Particularly useful for systems with LCD
displays. Also framebuffer=false.
irqpoll Changes the way the kernel handles interrupts.
nolapic Disables local APIC. The default is to enable local APIC.
nomodeset Disables KMS (kernel-mode-setting technology), which may help some older
graphics chips work properly. Include this parameter if the display does not work
properly as you boot from a CD/DVD.

ADVANCED INSTALLATION

83

~1 [!!] Enter rescue mode I
Enter a device you wish to use as your root f i l e system. You w i l l be
able to choose among various rescue operations to perform on t h i s
f i l c system.
If you choose not to use a root f i l e system, you w i l l be given a
reduced choice of operations that can be performed without one. This
may be useful i f you need to correct a partitioning problem.
Device to use as root t i l e system:

/dev/sda'ii
/dev/sdB5

rjn nnt IJRP a rnnt F L I P system

Figure 3-23

Selecting the root filesystem while rescuing a broken system

VIRTUAL C O N S O L E S
While it is running, Ubuntu opens a shell on each of the six virtual consoles (also
called virtual terminals; page 149). 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 that change any part of the installation process. To switch back to the graphical installation screen, press C0NTR0L-ALT-F7. To switch to the textual (pseudographical)
installation screen, press C0NTR0L-ALT-F1.

RESCUING A BROKEN SYSTEM
Rescuing a broken system versus recovery mode
tip To rescue a broken system, boot Ubuntu from an Alternate CD, a Server CD, or a live/install DVD,
and select Rescue a broken system from the Disk menu. Ubuntu displays the pseudographical
Rescue Operations menu (Figure 3-24). This section explains how to rescue a broken system.
When you bring a system up in recovery mode (classically called single-user mode), Ubuntu
boots from the hard disk and displays the pseudographical Recovery menu (Figure 11-2,
page 447) as explained on page 445.
The Rescue a broken system selection on the Alternate CD, Server CD, and
live/install DVD brings up Ubuntu but does not install it. After beginning a textual
installation (page 85), asking a few questions, and detecting the system's disks and
partitions, Ubuntu presents a menu from which you can select the device you want
to mount as the root filesystem (Figure 3-23).
Use the ARROW keys to highlight the device holding the filesystem you want Ubuntu to
mount as the root filesystem while you are rescuing it. If you choose the wrong
device, you can easily return to this menu and select a different device. Press RETURN
to select the highlighted device.

84

3

S T E P - B Y - S T E P INSTALLATION

I [! ! ] Enter rescue mode
Rescue operations
¡Execute a s h e l l in /dev/sdal
Execute a s h e l l in the i n s t a l l e r environment
R e i n s t a l l GRUB boot loader
Choose a different root f i l e system
Reboot the system


Figure 3-24

The Rescue Operations menu

Once you select a device, Ubuntu displays the Rescue Operations menu (Figure 3-24).
The following paragraphs list the selections on the Rescue Operations menu:
• 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 98) and can make changes to the filesystem on the device you
selected. You have access only to the shell and utilities on the mounted filesystem, although you may be able to mount other filesystems. If the
mounted filesystem does not include a shell, you must use the next selection. Give an exit command to return to the Rescue Operations menu.
• Execute a shell in the installer environment—Mounts the device you
selected (/dev/xxx) as /target; runs Busybox (www.busybox.net), a sizeand resource-optimized collection of Linux-like utilities; and spawns a
shell. You are running a minimal Busybox shell with root privileges
(page 98). You have access to the many BusyBox utilities and can use nano
to edit files, but some familiar utilities to may not be available and others
may take fewer parameters than their Linux counterparts. 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 Rescue Operations menu.
• Reinstall the GRUB boot loader—Updates the GRUB boot loader by
prompting for a device and running update-grub (page 587) and grub-install
(page 589) to update GRUB and install it on the device you specify. A typical system has GRUB installed on the MBR (master boot record) of the
first hard disk (e.g., /dev/sda). This selection will not upgrade from GRUB
legacy to GRUB 2; see the tip on page 584.
• 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/DVD if you
want to boot from the hard disk.

ADVANCED INSTALLATION

85

1 [!] Ubuntu i n s t a l l e r main menu |—
Choose the next step in the i n s t a l l process:
Choose language
Configure the keyboard
Detect and mount CD-ROM
Load debconf préconfiguration f i l e
Load i n s t a l l e r components from CD
Detect network hardware
Configure the network
Configure the clock
Detect disks
P a r t i t i o n disks
I n s t a l l the base system
Set up users and passwords
Configure the package manager
Select and i n s t a l l software
I n s t a l l the GRUB boot loader on a hard disk
I n s t a l l the LILD boot loader on a hard disk
Continue without boot loader
F i n i s h the i n s t a l l a t i o n
Change debconf p r i o r i t y
Check the CD-ROM(s) i n t e g r i t y
Save debug logs

Figure 3-25

The Ubuntu installer main menu

THE UBUNTU TEXTUAL INSTALLER
The Ubuntu textual installer (debian-installer) gives you more control over the
installation process than the Ubuntu graphical installer (page 57) does. The textual
installer displays a pseudographical (page 30) interface and uses fewer system
resources, including less RAM, than the graphical installer does, making it ideal for
older systems. You can install either a graphical (desktop) or textual (commandline) system using the textual installer, depending on which CD/DVD you use and
which selections you make from the disk menu and the F4 menu.
Many of the screens the textual installer displays parallel the screens displayed by
the graphical installer. Within the textual installer's screens, TAB moves between
items, the ARROW keys move between selections in a list, and RETURN selects the highlighted item 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.
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.
The textual installer main menu (the contents of this menu varies—Figure 3-25
shows an example) allows you to go directly to any step of the installation process
or enter recovery mode (see "Rescuing a Broken System" on page 83). At the lower-

86

3

S T E P - B Y - S T E P INSTALLATION

~1 [ ! f] ChDose language |
Please choose the language used for the i n s t a l l a t i o n process. Ttiis
language w i l l be the default language for the f i n a l system.
Choose o language:
Arabic
fisturion
Basque
Belarusian
Bosnian
Bulgarian
Catalan
Chinese (Simplified)
Chinese (Traditional)
Croat ian
czech
Danish
DuTnh
Fsperantn
Esluriiari

Figure 3-26

-

fisturianu
Euskara
Ee/tapycKafi
Bosanski
EtflrapcKH
Catala

•-

Hrvatski
cestina
uansk
Nfirterlands

-

Fsperantn
Eesli

-

m
ffSctSS;i

The Choose a language screen

left corner of most textual installer screens is . See Figure 3-26 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-26). Use
the UP and DOWN arrow keys to select a language. 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 installer/installed system; you can
change the default once the system is installed (page 145). 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 to make a selection 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 " F Q D N " on page 823 for information on names that are valid
on the Internet.
After this step, the installer asks which time zone the computer is in, continues
detecting hardware, starts the partition editor, and displays the Partitioning method
screen (Figure 3-27). Many of the selections available from the textual partition editor parallel those available from the graphical partition editor. This section
describes how to use the textual partition editor to partition a hard disk manually.

ADVANCED INSTALLATION

87

I [!! ] F a r t i t ian d i s k s I
The i n s t a l l e r can guide you through p a r t i t i o n i n g a d i s k (using
d i f f e r e n t standard schemes) o r , i f you p r e f e r , you can do i t
manually. ! : ' ' i guided p a r t i t i o n i n g you w i l l s t i l l have a chance l a t e r
ta review and customise the r e s u l t s .
I f you choose guided p a r t i t i o n i n g f o r an e n t i r e d i s k , you w i l l next
he asked which d i s k should be used.
P a r t i t i o n i n g method:
; i: I ' l - use e n t i r e d i s k
; i: I ' l - use e n t i r e d i s k and set up

'•-!

(Go Bai;k>

Figure 3-27

The Partitioning method screen

Page 70 describes guided partitioning using the graphical partition editor. Guided
partitioning using the textual installer is similar but offers more options.

MANUAL PARTITIONING
When you select Manual from the Partitioning method screen (Figure 3-27), the
textual partition editor displays the Partition overview screen, which lists the hard
disks in the system and partitions on those disks. If a hard disk has no partitions,
the partition editor displays only information about the hard disk. Figure 3-28
shows a single 200-gigabyte hard disk (highlighted) that has no partition table (and
no partitions). (Note: 214.7 GB equals 200 GiB—Figure 3-28 should show GiB in
place of GB. See the tip on page 37.)
If you want to set up RAID, see page 91 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-28, 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 partition table and you can skip this step and continue with "Creating a
partition" on the next page.
I E! f] Part i t ion d i s k s I
T h i s i s an overview of your c u r r e n t l y configured p a r t i t i o n s and mount
p a i n t s . S e l e c t a p a r t i t i o n to modify i t s s e t t i n g s ( f i l e system, mount
p a i n t , c t c . ) , a f r e e space to c r o a t c p a r t i t i o n s , or a device to
i n i t i a l i z e i t s partition table.
Guided p a r t i t i o n i n g
Configure i S C E I volumes
ISCSI3 (Ci.0.0) (sda) - 214.7 GB VMniare. VHuare V i r t u a l ;:|
Iiniin nhangas t n part i t inns
F i n i s h part I t Inning anrl w r i t s nhangps tn d i s k


Figure 3-28

The Partition overview screen I

88

3

S T E P - B Y - S T E P INSTALLATION

I [! f] Fart it ian disKs I
This i s an over view of your currently configured p a r t i t i o n s and (riount
paints. Select a p a r t i t i o n to modify i t s settings [ f i l e system, mount
point, c t c . ) , o free spoco to crcotc p a r t i t i o n s , or o device to
i n i t i a l i z e i t s p a r t i t i o n table.
Guided p a r t i t i o n i n g
Configure software RAID
Configure the L o g i c a l Volume Manager
Configure encrypted volumes
Configure iSCEI volumes
BlTBia [u.u.u) (sda) - 31g-7 Lib! VMniare. VHuiare v i r t u a l E
llnrtn nhangen tn p a r t i t i o n s
F i n i s h part It Inning and write nhangps tn disk

Figure 3-29

The Partition overview screen II

The iSCSI (page 1155) selection creates a partition on a remote system.
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 overview screen showing the disk with
a single block of free space as large as the disk (Figure 3-29). The Partition overview
screen displays additional choices because the hard disk now has a partition table.
Creating a partition

To create a partition, highlight the line containing the words FREE SPACE and press
RETURN. The partition editor asks how you want to use the free space; highlight Create
a new partition and press RETURN. Next the partition editor asks you to specify the size
of the new partition. You can enter either a percentage (e.g., 5 0 % ) or a number of
gigabytes followed by GB (e.g., 30 GB). Press RETURN. The partition editor then asks
you to specify the type of the new partition (primary or logical; page 34) 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 partition editor then displays the Partition
settings screen (Figure 3-30).

Ubuntu officially supports ext3 and ext4 filesystems only
caution The ext3 and ext4 filesystems are the only type of filesystems officially supported by Ubuntu
(other than swap). Set up other types of filesystems—such as JFS, XFS, or reiserfs—only if you
know what you are doing. Filesystems other than ext3 and ext4 may be more likely to become corrupted when the system crashes and may exhibit unusual performance characteristics (e.g., XFS
runs slowly with small files and may take a long time to upgrade).
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 partition editor
displays a screen that allows you to change the setting.
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 ext4, which is a good choice for most
filesystems. If you want to change the filesystem type, move the highlight to
n o r m a l

ADVANCED INSTALLATION

89

I [ i! ] Part i t ion disks I
You are editing p a r t i t i o n
of SCSI3 (0,0,0) (sda). No e x i s t i n g f i l e
system was detected in t h i s p a r t i t i o n .
Partition settings:
Use as:

Ext4 journaling f i l e system

|Hount point:
Mount options:
Label:
Reserved blocks:
l y p i c a l usage:
Hoatabie f l a g :

/
defaults
none
5%
standard
off

nntiy data frnrn annihpr p a r t i t i o n
nplRTp The parttttnn
nnnp seTT1ng up the p a r t i t i o n


Figure 3-30

The Partition settings screen

this line and press RETURN; the installer displays the How to use this partition screen
(Figure 3-31). You can select ext2 for /boot and /usr, swap area (page 37), RAID
(page 91), LVM (page 41), or another type of filesystem. Table 12-1 on page 505
lists some common types of filesystems. Move the highlight to the selection you want
and press RETURN. The partition editor redisplays 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 m o u n t
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 partition editor displays a
1 [!!] Partition disks
How to use this partition:
|Ext4 .journaling file system
Ext3 journaling file system
Ext2 file system
ReiserFS journaling file system
JFS journaling file system
XFS journaling file system
FAT16 file system
FAT32 file system
swap area
physical volume for encryption
physical volume for RAID
physical volume for LVM
do not use the partition


Figure 3-31

The How to use this partition screen

90

3

S T E P - B Y - S T E P INSTALLATION

| [! !] Partition disks |—
Mount point for this partition:
/ - the root f i l e system
/boot - static f i l e s of the boot loader
l/home - user home directories
/tmp - temporary f i l e s
/usr - static data
/var - variable data
/srv - data for services provided by this system
/opt - add-on application software packages
/usr/local - local hierarchy
Enter manually
Do not mount it


Figure 3-32

The Mount point for this partition screen

screen that allows you to specify a mount point (Figure 3-32). 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
partition editor redisplays the screen, now showing the changed state of this flag.

Done setting up

When you are satisfied with the partition settings, highlight Done setting up the
partition and press RETURN. The partition editor displays the Partition overview
screen showing the new partition setup. To create another partition, repeat the steps
starting with "Creating a partition" on page 88. To modify a partition, highlight
the partition and press RETURN.

the partition

Writing the
partitions to disk

Continuing the
installation

Specifying software
packages

When you are satisfied with the design of the partition table(s), highlight Finish partitioning and write changes to disk and press RETURN. After giving you another chance
to back out, the partition editor writes the partitions to the hard disk.
The installer continues by installing the base system and asking you to set up a user
account. It gives you the option of setting up an encrypted home directory and specifying an HTTP proxy and continues installing the system.
Next the installer displays the Software selection screen (Figure 3-33), which allows
y
t 0 specify the packages to be installed. The Ubuntu desktop package is specified
by default. Use the ARROW keys to move the highlight and use the SPACE bar to add and
remove the asterisk next to each selection. The asterisk indicates an item is selected.
The last selection, Manual package selection, installs the selected packages; you can
install additional packages once the system has been installed.
O U

ADVANCED INSTALLATION

91

I [ ! ] Software s e l e c t i o n I
fit the moment, o n l y the core of the system i s i n s t a l l e d . To tune the
system to your needs, you can choose to i n s t a l l one or more of the
f a l l o w i n g predefined c o l l e c t i o n s of software.
Choose software to i n s t a l l :
]
]
]
]
J
J
J
]
]
]
]
*]
1

B a s i c Ubuntu s e r v e r ^ l
DNS s e r v e r
LAMP s e r v e r
Mail s e r v e r
OpenSSH s e r v e r
PostgreSQL database
Print server
yarrtba f i l e s e r u e r
lomcat J a v a s e r u e r
ilhuntu h n t e r p r i s R Klnuri ( i n s t a n c e )
v i r t u a l Machine host
Ilhuntu rlpsktnp
Manual package selection


Figure 3-33
GRUB

Finishing the
installation

The Software selection screen

The Configuring grub-pc screen asks you to confirm that you want to write the
boot loader to the MBR (master boot record) of the first hard drive. Unless you
have another boot manager in the MBR, such as Smart Boot Manager, another
operating system's GRUB, or the Windows 7 bootmgr boot loader, and want to
manually edit that boot manager to boot Ubuntu from the boot sector on the
Ubuntu partition, choose to write the boot loader to the MBR. When all selections
are correct, highlight Yes and press RETURN.
Finally the installer asks if the system clock is set to UTC (page 1179). When the
installer displays the Installation Complete window, remove the CD/DVD and click
Continue to reboot the system.

SETTING UP A RAID ARRAY
To set up a RAID array (page 40), you must first create two or more partitions of
the same size. Usually these partitions will be on different hard disks. You create
RAID partitions as explained in the preceding section, except instead of making the
partitions of type ext4 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
partition editor 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 87 to create a new partition table on each hard disk. Then create two 100gigabyte partitions, one on each disk. When the partition editor displays the How
to use this partition screen (Figure 3-31, page 89), follow the instructions on
page 88 and specify a partition type of physical volume for RAID.

92

3

STEP-BY-STEP

INSTALLATION

I [ i! ] Part i t ion d i s k s I
T h i s i s an overview of your c u r r e n t l y configured p a r t i t i o n s and mount
p o i n t s . S e l e c t a p a r t i t i o n to modify i t s s e t t i n g s ( f i l e system, mount
p o i n t , e t c . ) , a f r e e space to c r e a t e p a r t i t i o n s , or a d e v i c e to
i n i t i a l i z e i t s partition tabic.
Guided p a r t i t i o n i n g
IConfigure software RAID
Configure the L o g i c a l Volume Manager
Configure encrypted volumes
Configure i S C E I volumec
SCSI3 10,0,0) (sda) - 214.7 GB VMmare, VHirare V i r t u a l S
#1 primary 100.0 GE
K raid
p r i / i o g 114. I l-it!
hKht s r a c t
s c s m [[J,1,0) (sdb) - 214./ GH VMW3re, VHirare v i r t u a l G
#1 primary 100.0 GE
t raid
p r i / i n g 114.7 GFi
FRFF SPAGF
llnrtn changes Tn p a r t l t t n n s
F i n i s h n a r t l t l o r l r g and ourite changes to dlsK.


The partition editor ready to set up RAID

Figure 3-34

Figure 3-34 shows the partition editor screen after setting up the RAID volumes. Once
you have at least two RAID volumes, the partition editor adds the Configure software
RAID selection as the top line of its menu (this line is highlighted in Figure 3-34).
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, 5, 6, or 10 and press
RETURN. The different types of RAID arrays are described on page 41. The partition
editor then asks you to specify the number of active devices (2) and the number of
spares (0) in the RAID array. The values the partition editor 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-35) and press RETURN.
I [ ! ! ] F a r t i t ion d i s K s I
You have chosen to c r e a t e a I ' ¡ 1 1 a r r a y with ' a c t i v e d e v i c e s .
P l e a s e chaase which p a r t i t i o n s a r c a c t i v e d e v i c c s . You must s c l c c t
exactly 2 partitions.
A c t i v e devices f o r the RfilOl a r r a y :
l*J
[ ]
I»]
[ ]

/dev/sdal
/dev/sda f r e e fll
/deu/sdbi
/dev/srih f r e e

< j i B j:A;

Figure 3-35

133393MB; r a i d )
(114248MBJ FREE. SHHCt)
(33333HB; r a i d )
CI14746MB; FRFF KPftCF)


Specifying the active devices in the RAID array

CHAPTER SUMMARY

93

I [!! ] Fart it ian disKs I
This i s an overview of your currently configured p a r t i t i o n s and mount
points. Select a p a r t i t i o n to modify i t s settings [ f i l e system, mount
point, e t c . ) , a free space to create p a r t i t i o n s , or a device to
i n i t i a l i z e i t s p a r t i t i o n table.
Configure iSCEI volumes

+

RAIDI device m - 100..0 GB Software RftID device
1
/Il
100. GB
f extl
/home I
963.0 KB
unusable
SCSI3 [0,0,0) (sda) - 214.7 GB VMware, VHuiare V i r t u a l S |
U1 primary 100.0 GB
K raid
•
t2 primary 100.0 GE B t eKt4
/
pri/log
14.7 GE
FREE SPACE
licsia [0,1.0) (sdb) - 214./ OB VMiiiare, Wluiare v i r t u a l K
#1 primary 100-0 HE
K raid
nz primary
4.0 HE
f sw3p
sutap
p r i z i n g 110.7 OFi
FRFF SPfl^F
llnrtn nhangen tn p a r t U i n n s
F i n i s h p a r t i t i o n i n g and wrile uhanges Lu disk

+

iGu Bai;K>

Figure 3-36

The finished partition tables

Highlight Finish on 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 (this line is highlighted in Figure 3-36, but is shown after the partition is created). Press RETURN. Highlight Use as: do not use and press RETURN. The installer displays the How to use this partition screen (Figure 3-31, page 89). Highlight the type
of filesystem you want to create on the RAID array (typically ext4) and press RETURN.
Continue to set up the RAID array as you would any other partition by following
the instructions under "Creating a partition" on page 88. In the example, the full
100 gigabytes is used for an ext4 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-36 shows the Partition overview screen that includes these changes. Highlight Finish partitioning and write changes to disk (you may have to scroll down to
expose this line) and press RETURN.

CHAPTER SUMMARY
Most installations of Ubuntu Linux begin by booting from the live/install DVD or
the live/install Desktop CD and running a live session that displays a GNOME
desktop. To start the installation, double-click the object on the desktop labeled
Install.

94

3

S T E P - B Y - S T E P INSTALLATION

Ubuntu provides a graphical installer (ubiquity) on the live/install Desktop CD/DVD;
it offers a textual installer (debian-install) on the Alternate and Server CDs and the
DVD. Both installers identify the hardware present in the system, build the filesystems, and install the Ubuntu Linux operating system. The ubiquity installer does not
write to the hard disk until it displays 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 Windows or Linux. You can use the GNOME Partition Editor (gparted) or the GNOME
Disk Utility (palimpsest) 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 partition editor
to add, delete, and modify partitions.

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 steps should you take before you start a live session the first time or
install Ubuntu with a new CD/DVD? How would you do it?
3. What is guided partitioning?
4. What is ubiquity?
5. Describe the ubiquity partition editor. How does it differ from the partition
editor found on the Alternate and Server CDs?
6. When is it beneficial to use an ext2 filesystem instead of an ext4 filesystem?

ADVANCED EXERCISES
7. What is a virtual console? During installation, for what purposes can you
use a virtual console? If the system is displaying a virtual console, how do
you display the graphical installation screen instead?
8. What steps would you take to have the system display all the things it is
doing as it boots from a live/install Desktop CD/DVD?

P A R T II
GETTING STARTED WITH
UBUNTU LINUX
CHAPTER 4
INTRODUCTION TO UBUNTU LINUX
CHAPTER 5
THE LINUX UTILITIES
CHAPTER 6
THE LINUX FILESYSTEM
CHAPTER 7
THE SHELL

237

159
199

97

This page intentionally left blank

4
INTRODUCTION TO
UBUNTU LINUX
IN THIS CHAPTER
Curbing Your Power: root
Privileges/sudo

98

ATourof the Ubuntu Desktop

99

Mouse Preferences

105

Using Nautilus to Work with
Files

107

The Update Manager

112

Updating, Installing, and
Removing Software
Packages

131

Where to Find Documentation . . . 136
More About Logging In

144

What to Do If You Cannot
Log In

146

Working from the Command
Line

150

Controlling Windows: Advanced
Operations

153

One way or another you are sitting in front of a computer that
is running Ubuntu Linux. After describing root (Superuser)
privileges, this chapter takes you on a tour of the system to
give you some ideas about what you can do with it. The tour
does not go into depth about choices, options, menus, and so
on; that is left for you to experiment with and to explore in
greater detail in Chapter 8 and throughout later chapters of
this book. Instead, this chapter presents a cook's tour of the
Linux kitchen: As you read it, you will have a chance to sample
the dishes that you will enjoy more fully as you read the rest of
this book.
Following the tour is a section that describes where to find
Linux documentation (page 136). The next section offers
more about logging in on the system, including information
about passwords (page 144). The chapter concludes with a
more advanced, optional section about working with Linux
windows (page 153).
Be sure to read the warning about the dangers of misusing the
powers of root (sudo) in the next section. While heeding that

97

98

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

warning, feel free to experiment with the system: Give commands, create files,
click objects, choose items from menus, follow the examples in this book, and
have fun.

root account
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 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 s u d o and provide your password to run a
command 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 419 for more information on root
privileges.

CURBING YOUR POWER: root PRIVILEGES/SUCIO
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 system, 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.

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 s u d o to execute any command. When you add user accounts, you can specify whether
they are allowed to administer the system. See page 594 and Figure 16-3 on page 596 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.
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 password

A TOUR OF THE UBUNTU D E S K T O P

99

to perform administrative tasks. After you enter your password, the program runs
with root privileges. Second, if you use the sudo utility (for textual applications;
page 421) or gksudo utility (for graphical applications; page 423) from the command
line (such as from a terminal emulator; page 125) and provide your password, 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 419.

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

A TOUR OF THE UBUNTU DESKTOP
This section presents new words (for some readers) in a context that explains the
terms well enough to get you started with the Linux desktop. If you would like
exact definitions as you read this section, refer to "GNOME Desktop Terminology"
on page 117 and to the Glossary. The Glossary also describes the data entry widgets
(page 1180), such as the combo box (page 1141), drop-down list (page 1146), list
box (page 1157), and text box (page 1176).
GNOME

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

100

CHAPTER 4

INTRODUCTION TO UBUNTU L I N U X

Figure 4-1

The Ubuntu GNOME Login screen

LOGGING IN ON THE SYSTEM
When you boot a standard Ubuntu system, GDM (GNOME display manager)
displays a Login screen (Figure 4-1) on the system console. In the middle of the
screen is a window that holds a list of names. When you click a name, Ubuntu
displays a text box labeled Password. In addition, in the panel at the bottom of
the screen, Ubuntu displays icons that allow you to work in a different language,
select a different keyboard layout, change your accessibility preferences (e.g.,
make the text larger and easier to read), and restart or shut down the system. For
more information refer to "The Login Screen" on page 145.
To log in, click your name. A text box labeled Password appears. Enter your
password and press RETURN. If Ubuntu displays an error message, try clicking your
name and entering your password again. Make sure the CAPS LOCK key is not on
(Ubuntu displays a message if it is) because the routine that verifies your entries
is case sensitive. See page 146 if you need help with logging in and page 148 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 (page 594) and work as that user while you experiment with the desktop. When you figure out which features you like, you can log in as yourself and
implement those features. That way you need not concern yourself with "ruining"
your desktop and not being able to get it back to a satisfactory configuration.

A TOUR OF THE UBUNTU DESKTOP

Firefox Web browser

101

NetworkManagen

Sound
Indicator
Clock

Main menu

Session Indicator

Show Desktop
Bottom panek

Figure 4-2
Panels and objects

Workspace Switchei

The initial workspace

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 panels holds several icons and words called objects. (Buttons, applets, and menus, for example, are all types of objects.) When you click an
object, something happens.
A panel does not allow you to do anything you could not do otherwise, but rather
collects objects in one place and makes your work with the system easier. Because
the panels are easy to configure, you can set them up to hold those tools you use frequently. 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 four workspaces. The desktop, which is not displayed all at once, is
the collection of all workspaces. "Switching Workspaces" on page 104 describes
some of the things you can do with workspaces.

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. It uses the term rightclick when you need to click the right mouse button. See page 105 for instructions on adapting
the mouse for left-handed use.

102

CHAPTER 4

I N T R O D U C T I O N TO U B U N T U L I N U X

•£} Applications! Places System ? )
Accessories
g j Games
^

- ^

?

Calculator

> P f CD/DVD Creator
•

ChararterMap

- ^

Disk Usage Analyzer

^Office

• ^

gedlt Text Editor

( f . Soond & Video

• ft Manage Print Jobs

^

firaphirs
Internet

f j Ubuntu Software Center

^

Passwords and Encryption Keys

*

Search for Files...
Take Screenshot

Figure 4-3

Main menu: Applications•=>Accessories•=>Terminal

LAUNCHING PROGRAMS FROM THE DESKTOP
This section describes three of the many ways you can start a program running from
the desktop.
Click an object

The effect of clicking an object depends on what the object is designed to do. Clicking an object may, for example, start a program, display a menu or a folder, or open
a file, a window, or a dialog box.
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 left 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 f r o m the
Main menu

The second way to start a program is by selecting it from a menu. The Main menu
¡ s t he 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 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 118).

A TOUR OF THE UBUNTU D E S K T O P

#

103

t'rtox
Run in terminal

Run with file...

+ Show list of known applications
hplp

Figure 4-4

The Run Application window

Experiment with the Main menu. Start Sudoku (Main menu: Applications1^
Games^Sudoku), a terminal emulator (Main menu: Applications1^Accessories1^
Terminal), and other programs from the Applications menu. The Places and System
menus are discussed on page 122.
Use the Run
Application window

optional
Running textual
applications

You can also 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, for
example, the window recognizes what you are typing and displays the Firefox logo
and the rest of the word firefox. Click Run to start Firefox.

You can run command-line utilities, which are textual (not graphical), from the Run
Applications window. When you run a textual utility from this window, you must
put a tick in the check box labeled Run in terminal (click the check box to put a tick
in it; click it again to remove the tick). The tick tells GNOME to run the command
in a terminal emulator window. When the utility finishes running, GNOME closes
the window.
For example, type vim.tiny (the name of a text-based editor) in the text box, put a tick
in the check box labeled Run in terminal, and click Run. GNOME opens a Terminal
(emulator) window and runs the vim text editor in that window. When you exit from
vim (press ESCAPE:q!RETURN sequentially to do so), GNOME closes the Terminal window.
You can run a command-line utility that only displays output and then terminates.
Because the window closes as soon as the utility is finished running, and because
most utilities run quickly, you will probably not see the output. Type the following
command in the text box to run the df (disk free; page 774) utility and keep the window open until you press RETURN (remember to put a tick in the check box labeled
Run in terminal):
bash - c "df -h ; read"
This command starts a b a s h shell (Chapter 7) that executes the command line following the - c option. The command line holds two commands separated by a semicolon.
The second command, r e a d (page 1003), 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.

104

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

SWITCHING WORKSPACES
Workspace Switcher

Each rectangle in the Workspace Switcher applet (or just Switcher)—the group of
rectangles near the right end of the Bottom panel—represents a workspace
(Figure 4-2, page 101). When you click a rectangle, the Switcher displays the corresponding workspace and highlights the rectangle to indicate which workspace is
displayed. You can also press CONTROL-ALT-RIGHT ARROW to display the workspace to the
right of the current workspace; pressing CONTROL-ALT-LEFT ARROW works in the opposite
direction.
Click the rightmost rectangle in the Switcher (not the Trash applet to its right) and
then select Main menu: Systerm•=>PreferencesOiViouse. GNOME opens the Mouse
Preferences window. The Switcher rectangle that corresponds to the workspace you
are working in displays a small rectangle. This rectangle corresponds in size and location within the Switcher rectangle to the window within the workspace. Click and
hold the left mouse button with the mouse pointer on the titlebar at the top of the
window and drag the window to the edge of the desktop. The small rectangle within
the Switcher moves to the corresponding location within the Switcher rectangle.
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).
You can move a window from one workspace to another by right-clicking the Window List applet (page 121) on the Bottom panel and selecting one of the choices
that starts with Move.

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

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 rightclick anywhere on the Switcher to display the Switcher menu and then select Preferences. (The window looks different when visual effects [page 115] are enabled.)
Specify the number of workspaces you want in the spin box labeled Number of
workspaces. The number of workspaces the Switcher displays changes as you
change the number in the spin box—you can see the result of your actions before
you close the Preferences window. Four workspaces is typically a good number to
start with. Click Close.

A TOUR OF THE UBUNTU D E S K T O P

S O

105

Workspace Switcher Preferences

Switcher
Show only the current workspace
it Show all workspaces in:
Workspaces
Number of workspaces: KT
Workspace names:
Desk 1
Desk 2
Desk 3
Desk 4
Show workspace names in switcher
Help
Figure 4-5
Clock applet

Close

The Workspace Switcher Preferences window

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

MOUSE PREFERENCES
The Mouse Preferences window (Figure 4-6, next page) enables you to change the
characteristics of the mouse to suit your needs. To display this window, select Main
menu: SystemOPreferences^Mouse or give the command gnome-mouse-properties
from a terminal emulator or Run Application window (ALT-F2). The Mouse Preferences
window has two tabs: General and Accessibility (and a third, Touchpad, on a laptop).
Left-handed mouse

Click the General tab. To change the orientation of the mouse buttons for use by a
left-handed person, click the radio button labeled Left-handed. If you change the
setup of the mouse buttons, remember to reinterpret the descriptions in this book
accordingly. That is, when this book asks you to click the left button or does not
specify a button to click, click the right button, and vice versa. See "Remapping
Mouse Buttons" on page 274 for information on changing the orientation of the
mouse buttons from the command line.

106

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

Q

Mouse Preferences

General Accessibility
Mouse Orientation
«1 Klght handed
Lett handed

Locate Painter
Show position of pointer when'K' Control key is pressed
Pointer S p e e d

Acceleration: sio» :
SensiLlvily: urn

^^^^^^^^
— Hmn

Drag and Drop
Threshold:

1

,

Double-Click Timeout

TlmeOUtr

Sfwrt =

test your double-click
settings, try Ta double
dick on Lf re light bulb.

Help

Figure 4-6
Double-click
timeout

The Mouse Preferences window, General tab

Use the Double-Click Timeout slider to change the speed with which you must
double-click a mouse button to have the system recognize your action as a doubleclick rather than as two single clicks. You can also control the acceleration and
sensitivity of the mouse. The Drag and Drop Threshold specifies how far you
must drag an object before the system considers the action to be the drag part of a
drag-and-drop operation.
You can control different aspects of mouse clicks from the Accessibility tab.

WORKING WITH WINDOWS
To resize a window, position the mouse pointer over an edge of the window; the
pointer turns into an arrow pointing to a line. When the pointer is an arrow pointing to a line, you can click and drag the side of a window. When you position the
mouse pointer over a corner of the window, you can resize both the height and the
width of the window simultaneously.
To move a window, click and drag the titlebar (the bar across the top of the window
with the name of the window in it). Alternatively, when you hold the ALT key down
you can move a window by clicking and dragging any part of the window. For fun,
try moving the window past either side of the workspace. What happens? The result
depends on how visual effects (page 115) is set.
Titlebar

At the left of the titlebar are three icons that control the window (Figure 4-17,
page 123). Clicking the down arrow, which usually appears in the middle of the set

A TOUR OF THE UBUNTU D E S K T O P

107

of icons, minimizes (iconifies) the window so that 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 121). Click the Window List applet to toggle the window between visible and minimized. Clicking the up arrow icon, which usually appears at the right
end of the three icons, maximizes the window (displays the window at its maximum
size) and changes the up arrow to a rectangle. Clicking the rectangle returns the
window to its normal size. Double-clicking the titlebar toggles the window between
its normal and maximum size.
Terminating a
program

Clicking the x closes the window and usually terminates the program running in
window. In some cases you may need to click several times. Some programs,
such as Rhythmbox Music Player, do not terminate, but rather continue to run in
the background. When in this state, the program displays an icon on the Top
panel. Click the icon and select Quit from the drop-down list to terminate the
program.

USING NAUTILUS TO WORK WITH FILES
Nautilus, the GNOME file manager, is a simple, powerful file manager. You can
use it to create, open, view, move, and copy files and folders as well as to execute
programs and scripts. One of its most basic and important functions is to create
and manage the desktop. This section introduces Nautilus and demonstrates the
correspondence between Nautilus and the desktop. See page 276 for more detailed
information on Nautilus.
Terms: folder and
directory

Term: File Browser

Opening Nautilus

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.
This book sometimes uses the terms File Browser
referring to the Nautilus File Browser window.

window

and File Browser

when

Select Main menu: PlacesOHome Folder to open a Nautilus File Browser window
that shows the files in your home folder.
Double-clicking an object in a File Browser window has the same effect as doubleclicking an object on the desktop: Nautilus takes an action appropriate to the
object. For example, when you double-click a text file, Nautilus opens the file with
a text editor. When you double-click an 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 appeared in the window.
From within a Nautilus File Browser window, you can open a folder in a new tab.
To do so, middle-click the folder or right-click the folder and select Open in New
Tab from the drop-down list; Nautilus displays a new tab named for the folder you
clicked. Click the tab to display contents of the directory.

108

CHAPTER 4

I N T R O D U C T I O N TO U B U N T U L I N U X

0 A O

.*_»y..

sam

m

Desktop

C

Documents

¿ y roiwmd

•3

m
Videos

X

Downloads

B

Templates

sam - File Browser

Fite Edit View Go Bookmarks Help

File Edit View Places Help

IB Desktop

¿J Ole System
jîi Network

_ Floppy Drive
"
vmwcKC-toolsdistrib

• sam f 10 items, Free space: 181.? GH

trash

i 3 Documents

g l

*

T

Q, 100* ^

C m

urn

m

Desktop

J

mJ

Documents

m

Music

Pictures

m

j a

£ Music
¿a Pictures

A Vide«
¿1 Downloads

Tempiares

videos

mJ

Downloads

m

Public

H

vmware-tßölsdistrib

10 items. Free spate: 181.2 GB

Figure 4-7

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

THE TWO FACES OF NAUTILUS
The appearance of Nautilus differs depending on how it is set up: It can display a
Spatial view or a File Browser window. Figure 4-7 shows an example of each type
of display. By default, Ubuntu displays browser windows. See page 282 for information on the Spatial view.

THE

Desktop DIRECTORY

The files on the desktop are held in a directory that has a pathname (page 205) of
/home/wseratfme/Desktop, where username is your login name or, if you are logged
in on a live session, ubuntu. The simple directory name is Desktop. When you select
Main menu: Places "^Desktop, GNOME opens a File Browser window showing the
files on the desktop (Figure 4-8). Initially there are no files. The buttons below the
toolbar and to the right of Places show the pathname of the directory Nautilus is
displaying (/sam/Desktop in the Figure 4-8).
To see the correspondence between the graphical desktop and the Desktop directory, right-click anywhere within the large clear area of the Desktop File Browser
window. Select Create Document^Empty 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 the name. If you double-click the
new file, Nautilus assumes it is a text file and opens the file in a gedit window. (The
gedit utility is a simple text editor.) Type some text and click Save on the toolbar.
Close the window either by using the File menu or by clicking the x at the left end of
the titlebar. You have created a text document on the desktop. You can now doubleclick 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

A TOUR OF THE UBUNTU D E S K T O P

•IS Applications Place

untitled fofder

•

new file

Sy-Jpml» V A A .
Change system appearance and behavior, or qet help
0 0 6

4)

109

£23 Thu

Desktop - File Browser

file Edit View Go Bookmarks Help
^
Places T

X

jjjjSam

^

tJ

Q.

100% Q,

Icon View

T

Q^

Desktop

jtisam
I B Desktop
File System
^¡1 Network

m

untitied folder

new file

Floppy Drive
3 Tfash
C Documents
^ Music
j i Pictures

Figure 4-8

» 2 items, Free space: 1BI.7 GB

|

Part of a workspace with a Nautilus File Browser window

you named the file you created previously. The folder object appears on the desktop
and within the Desktop File Browser window.
On the desktop, drag the file until it is over the folder; the folder opens. Release the
mouse button to drop the file into the folder; GNOME moves the file to the folder.
Again on the desktop, double-click the folder you just moved the file to. GNOME
opens another File Browser window, this one displaying the contents of the folder.
The file you moved to the folder appears in the new window. Now drag the file from
the window to the previously opened Desktop File Browser window. The file is back
on the desktop, although it may be hidden by one of the File Browser windows.
Next, open a word processing document by selecting Main menu: Applications1^
Office^OpenOffice.org Word Processor. Type some text and click the Save icon
(the arrow pointing down to a hard disk drive) or select menubar: File^Save to
save the document. OpenOffice.org displays a Save window (Figure 4-9, next page).
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 plus sign (+) to the left of Browse for other folders to open this section of
the window. When you open this section, the plus sign changes to a minus sign (-);
click the minus sign to close this section. Figure 4-9 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 limited number of choices of folders. By default, it saves the
document in Documents (/home/wseratfme/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 stripe or picture in it.

110

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

0

Save

Name;
S a v e in folder:

memo)
|j

Datumente

+ Browse for other f o l d e n
+ File type
S a v e with password
cancel

Figure 4-9

optional
Browse/Save
window

save

The Save window

With the Browse for other folders section opened (click the plus sign [+] to the left
0 f Browse for other folders), the Save window grays out the drop-down list labeled
Save in folder and expands the Browse for other folders section, as shown in
Figure 4-10. This expanded section holds two large side-by-side list boxes: Places
and Name. The list box labeled Places displays directories and locations on the system, including File System. The list box labeled Name lists the files within the
directory highlighted in Places.
The Browse for other folders section of the Save window allows you to look through
the filesystem and select a directory or file. GNOME utilities and many applications
use this window, although sometimes applications call it a Browse window. In this
O

Save

Name;

memo

s a v e in folder:

—

tmp

- Growse for other folders
Create Folder

tmp
Maces

Name

Search

» Size

Modified

^ keyrirvg-z2rvqf

05/06/2010

© Recently Used

^ orbit-qdm

Ob/06/7010

jy, sam

m «ttt-sam
^ pulse-l7972GhVrRvm

Ob/06/7010

U Desktop

05/06/2010
16:35

m svk68.tmp
M virtual-sam.EFid7Y

¡ 3 Documents
Music

H Pictures
u Videos
BQ, Downloads

||Adrj

05/06/2010

m pulse~PKdhtXMmrl8n
^üsh-jrprisflSOa

' 1 File System
u . Floppy Drive
4

16:35

05/06/2010

alVMwaraOnD
^ v m ware-root

05/06/2010

*vmware-sam

05/06/2010

[Remove

05/06/2010

ODF Text Document (.odl)

(

+ File type
Save with password
Cancel

Figure 4-10

Save

The Save window with Browse for other folders open

A T O U R OF THE U B U N T U D E S K T O P

111

example, the word processor calls it a Save window and uses it to locate the directory where the document will be saved.
Assume you want to save a file in the /tmp directory. Click File System in the list
box on the left. The list box on the right displays the files and directories in the root
directory (represented by /; see "Absolute Pathnames" on page 205 for more information). Next, double-click tmp in the list box on the right. The buttons above the
list box on the left change to reflect the directory displayed in the list box on the
right. Click Save.
The buttons above the left-side list box represent directories. The right-side list box
displays the directories found within the directory named in the highlighted (darker)
button. This directory is the one you would save the file to if you clicked Save at
this point. Click one of these buttons to display the corresponding directory in the
list box on the right and then click Save to save the file in that directory.
When you have finished editing the document, close the window. If you have made
any changes since you last saved it, the word processor asks if you want to save the
document. If you choose to save it, the word processor saves the revised version
over (in the same file as) the version you saved previously. Now the memo.odt
object appears on the desktop and in the Desktop File Browser window. Doubleclick either object to open it.
The Desktop In summary, the Desktop directory is like any other directory, except that GNOME
directory is special displays its contents on the desktop (in every workspace). It is as though the desktop is a large, plain Desktop File Browser window. You can work with the Desktop
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 can be used to select one or more objects in a File Browser
window or on the desktop. Select an object by clicking it once; GNOME highlights
the object. Select additional objects by holding down the CONTROL key while you click
each object. To select a group of adjacent objects, highlight the first object and then,
while holding down the SHIFT key, click the last object; GNOME highlights all objects
between the two objects you clicked. Alternatively, you can use the mouse pointer
to drag a box around a group of objects.
To experiment with these techniques, open a File Browser window displaying your
home folder. 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.

EMPTYING THE TRASH
Selecting Move to Trash from an object's context menu moves the selected (highlighted) object to the Trash directory. Because files in the trash take up space on the

112

CHAPTER 4

I N T R O D U C T I O N TO U B U N T U L I N U X

0 9 9

»

Update Manager

Software updates are available for this computer

If you don't waul lo install them now, choose "Update Manager" from liie
Administration menu later.

Recommended updates
apt
Advancea

front-end for dpkg (Size: 1 . 0 MB)

apt-transport- https

y

APT https transport (Size: 7 8 KB)

1*5

APT utility programs {Size: 2 3 0 KB)

^

apt-uttls
ghostscript
T h e GPL Ghostscript PostScript/PDf interpreter (Size. 7GG KB)

ghostscript-cups
Tlie GPL G h u i b u i p L PustScr ipl/PDF inter pi elei - CUPS filLeis ( S u e : SO KB)

ghostscript-x
The GPLGnostscrlpt PostScript/PDF interpreter - X Display support (Size: 6 7 KB)

hoi

Hardware Abstraction Layer (Size: i S b KB)

1 1 2 MH

rhpric

i^ 1 ^ 11 ^ ^

+ Description of update
Settings...

Figure 4-11

Close

The Update Manager window

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: File^Empty Trash. To view the files in the trash,
click the Trash applet at the right end of the Bottom panel (Figure 4-2, page 101);
Nautilus 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.) Alternatively, you can
right-click an object in the Trash File Browser window and select Delete Permanently to remove only that object (file) or you can select Restore to move the file
back to its original location. You can drag and drop files to and from the trash just
as you can with any other folder.

THE UPDATE MANAGER
On systems connected to the Internet, Ubuntu notifies you when software updates
are available by opening the Update Manager window (Figure 4-11). You can open
this window manually by selecting Main menu: System •=> Administration1^ Update
Manager or by giving the command update-manager from a terminal emulator or
Run Application window (ALT-F2).

A TOUR OF THE UBUNTU D E S K T O P

113

When the Update Manager window opens, it displays the message Starting Update
Manager; after a moment it displays the number of available updates. If no updates
are available, the window displays the message Your system is up-to-date. If you
have reason 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 packages 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
message Your system is up-to-date. Click Close. If the updates require you to reboot
the system, the Update Manager asks if you want to restart the system now or later.
Selecting now restarts the system immediately. Selecting later closes the Update
Manager window and turns the Session Indicator applet (Figure 4-2, page 101) red.
Click this applet and select Restart Required from the drop-down list as soon as you
are ready to reboot the system. For more information refer to "Updating, Installing,
and Removing Software Packages" on page 131.

CHANGING APPEARANCE (THEMES)
One of the most exciting aspects of a Linux desktop is the flexibility it offers in
allowing you to change its appearance. You can change not only the backgrounds,
but also window borders (including the titlebar), icons, the buttons used by applications, 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. The first and easiest choice is to leave well enough alone. Ubuntu comes with
a good-looking theme named Ambiance. 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 used by a theme. 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 the desktop is the Appearance Preferences
window. Display this window by choosing Main menu: SystemOPreferences1^
Appearance or by right-clicking the root window (any empty area on a workspace)

114

CHAPTER 4

I N T R O D U C T I O N TO U B U N T U L I N U X

$

Appearance Preferences

Theme Rarfcgmnnrl Fonts visual Ffferts

Delete

Save As...

Customize...

Install...

Get more themes online
Help

Figure 4-12

Close

The Appearance Preferences window, Theme tab

and selecting Change Desktop Background. The Appearance Preferences window
has four tabs:
• The Theme tab (Figure 4-12) enables you to select one of several themes.
Click a theme and the workspace immediately reflects the use of that
theme. Ambiance 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 either click Close if you are satisfied with your
choice or 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 110 for instructions on
selecting 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 Desktop
Background 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 Desktop Background. Select
that (non)wallpaper. (Initially the icon for this wallpaper appears at the

A TOUR OF THE UBUNTU D E S K T O P

115

upper-left corner of the wallpaper icons.) 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 dragging the little circle within
the triangle. Click O K when you are done. The color you chose becomes
the background color of the desktop. See page 285 for more information
on the Pick a Color window.
• The Fonts tab (Figure 8-8, page 284) 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 284).
Visual effects

• The Visual Effects tab enables you to select one of three levels of visual
effects: None, Normal, and Extra. The 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. You can use the simple-ccsm (Compizconfig settings manager)
package to configure Compiz. If you experience problems with the system,
select None.

Visual effects can cause problems
caution Selecting Normal or Extra in the Visual Effects tab can cause unexpected graphical artifacts,
shorten battery life, reduce performance in 3D applications and video playback, and in some rare
cases cause the system to lock up. If you are having problems with an Ubuntu system, try selecting None in the Visual Effects tab and see if the problem goes away.
The changes you make in the Background, Fonts, and Visual Effects tabs are used
by any theme you select, including ones you customize. When you have finished
making changes in the Appearance Preferences window tabs, you can either click
Close to use the theme as you have modified it or return to the Theme tab to customize the theme.
Customizing From the Theme tab of the Appearance Preferences window, select the theme you
a theme w a n t to customize or continue with the theme you modified in the preceding sections. Click Customize to open the Customize Theme window. Go through each
tab in this window; choose entries and watch how each choice changes 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 unique name for the
theme. After you save a theme, it appears among the themes listed in the Theme tab.

116

CHAPTER 4

INTRODUCTION TO UBUNTU L I N U X

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 workspaces, and programs will be as you left them.
The Startup Applications Preferences window allows you to select which applications you want to run each time you log in. It also allows you to save automatically
those applications that were running and those windows that were open when you
logged out; they will start running when you log on again. To open the Startup
Applications Preferences window, select Main menu: SystemOPreferences"^Startup
Applications or give the command gnome-session-properties from a terminal emulator or Run Application window (ALT-F2). You must give this command while logged
in as yourself (not while working with root privileges).
To save a session, first make sure you have only those windows open that you
want to appear the next time you log in. Then open the Startup Applications
Preferences window. Click the 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 as when you clicked Remember currently running applications. 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 about
Ubuntu. 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 136 for more resources.

FEEL FREE TO EXPERIMENT
Try selecting different items from the Main menu and see what you discover. Following are some applications you may want to explore:
• The gedit text editor is a simple text editor. Select Main menu: Applications1^
Accessories^gedit Text Editor to access it.
• OpenOffice.org's Writer is a full-featured word processor that can import
and export Microsoft Word documents. Select Main menu: Applications1^
Office^OpenOffice.org Word Processor. The Office menu also offers a
dictionary, presentation manager, and spreadsheet.

GETTING THE M O S T OUT OF THE D E S K T O P

117

• 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: Applications Olnternet1^Firefox Web Browser.
• Empathy is a graphical IM (instant messaging) client that allows you to
chat on the Internet with people who are using IM clients such as AOL,
MSN, and Yahoo! To start Empathy, select Main menu: Applications1^
Internet1^ Empathy IM Client.
The first time you start Empathy, it opens a window that says Welcome to
Empathy. Follow the instructions to access an existing IM account or open
a new one. Visit live.gnome.org/Empathy for more information.

LOGGING OUT
To log out, click the Session Indicator button (Figure 4-2, page 101) at the upperright corner of the workspace. GNOME displays a drop-down list; select Log Out.
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 its panels, the Main menu, windows, terminal emulation, and ways
to update, install, and remove software. Chapter 8 continues where this chapter
leaves off, discussing the X Window System, covering Nautilus in more detail, and
describing a few of the GNOME utilities.

G N O M E DESKTOP TERMINOLOGY
The following terminology, which is taken from the GNOME Users Guide, establishes a foundation for discussing the GNOME desktop. Figure 4-2 on page 101
shows the initial Ubuntu GNOME desktop.
Desktop

The desktop comprises all aspects of the GNOME GUI. While you are working with
GNOME, you are working on the desktop. There is always exactly one desktop.

Panels

Panels are bars that appear on the desktop and hold (panel) objects. Initially there
are two panels: one along the top of the screen (the Top Edge panel, or just Top
panel) and one along the bottom (the Bottom Edge panel, or just Bottom panel).
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 locations. The desktop can have no panels, one panel, or several panels. See the next
page for more information on panels.

118

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

Panel objects Panel objects appear as words or icons on panels. You can click these objects to display menus, run applets, or launch programs. The five types of panel objects are
applets, launchers, buttons, menus, and drawers. See page 120 for more information on panel objects.
Windows A graphical application typically displays a window and runs within that window.
At the top of most windows is a titlebar you can use to move, resize, and close the
window. The root window is the unoccupied area of the workspace and is frequently obscured. The desktop can have no windows, one window, or many windows. Although most windows have decorations (page 155), 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 four 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 104) enables you to display any one of several workspaces.
Tooltips Tooltips (Figure 4-2, page 101) is a minicontext help system that you activate by
moving the mouse pointer over a button, icon, window border, or applet (such as
those on a panel) and allowing it to hover there. When the mouse pointer hovers
over an object, GNOME displays a brief explanation of the object called a tooltip.

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

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

GETTING THE M O S T OUT OF THE D E S K T O P

Figure 4-13

119

The Add to Panel window

THE PANEL (CONTEXT) 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-13). 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 Eyes 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 286).
Properties Selecting Properties displays the Panel Properties window (Figure 4-14, next page).
This window 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,

120

CHAPTER 4

INTRODUCTION TO UBUNTU

LINUX

©

Panel properties

General

Background

Orientation:

Tup

Size:

24

,T
pixels

v: Fxpanri
r

Autohide

r

Show hide buttons
t

Arrows on hide buttons

Help

Figure 4-14

Close

The Panel Properties window, General tab

leaving only a button remaining. When you click that button, the panel slides
back into place.
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. Like Preferences windows, Properties
windows lack Apply and Cancel buttons; they implement changes immediately. Use
the same procedure to put the panel back where it was.
The Background tab of the Panel Properties window enables you to specify a color
and transparency or an image for the panel. See "Pick a Color Window" on
page 285 for instructions on changing 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 theme) to return the panel to its default appearance. Click Close.
Delete This Panel Selecting Delete This Panel does what you might expect. Be careful with this selection: When it removes a panel, it removes all 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 then move the panel to somewhere else using the drop-down list
labeled Orientation in the General tab of the Panel Properties window for the new
panel.

PANEL OBJECTS
The icons and words on a panel, called panel objects, display menus, launch programs, and present information. The panel object with the blue and orange globe,
for example, starts Firefox. The Indicator applet (the envelope icon; Figure 4-2 on
page 101) can start Evolution (www.gnome.org/projects/evolution), an email and
calendaring application. The Session Indicator applet (Figure 4-2, page 101) can log

GETTING THE MOST OUT OF THE D E S K T O P

g

memo.odt - OpenOffice... B

Figure 4-15

Sudoku

121

0 samtaiynxl: •

Window List applets

you out or shut down the system. You can start almost any utility or program on
the system using a panel object. This section describes the various types of panel
objects.
Applets

An applet is a small program that displays its user interface on or adjacent to the
panel. You interact with the applet using its Applet panel object. The Clock (date
and time) and Workspace Switcher (both shown in Figure 4-2 on page 101) are
applets.

W i n d o w 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-15) appears on the Bottom panel
for each open or iconified window on the displayed workspace. Left-clicking this
object minimizes its window or restores the window if it is minimized. Right-click
this object to display the Window Operations menu (page 124). If a window is buried under other windows, click its Window List applet to make it visible.

Launchers

When you open a launcher, it can execute a command, start an application, display
the contents of a folder or file, open a URI in a Web browser, and so on. In addition
to appearing on panels, launchers can appear on the desktop. The Firefox object is a
launcher: It starts the Firefox application. Under Main menu: Applications, you can
find launchers that start other applications. Under Main menu: Places, the Home
Folder, Documents, Desktop, and Computer objects are launchers that open File
Browser windows to display folders.

Buttons

A button performs a single, simple action. The Sound button (Figure 4-2, page 101)
displays a volume control. The Show Desktop button, which may appear at the left
end 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 section 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 to display this menu). 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

122

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

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 end 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 applications (e.g., Games, Graphics, Internet, Office—the list varies depending on the
software installed on the system). The last selection, Ubuntu Software Center, is discussed on page 132. Selections from the submenus launch applications—peruse
these selections, hovering over those you are unsure of to display the associated
tooltips.

Places

The Places menu holds a variety of launchers, most of which open a File Browser
window. The Home Folder, Desktop, and Documents objects display your directories with corresponding names. The Computer, CD/DVD Creator, and Network
objects display special locations. Each of these locations enables you to access file
manager functions. A special URI (page 1179) specifies each of these locations. For
example, the CD/DVD Creator selection displays the b u r n : / / / URI, which enables
you to burn a CD or DVD. The Connect to Server selection opens a window that
allows you to connect to various types of servers, including SSH and FTP servers
(see "File" on page 280). Below these selections are mounted filesystems; click one
of them to display the top-level directory of that filesystem. The Search for Files
selection enables you to search for files (page 286).

System

The System menu holds two submenus as well as selections that provide support.
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 various ways you can customize your account on an Ubuntu system.
The Administration submenu controls the way the system works. For example,
AdministrationOPrinting (page 550) sets up and configures printers you can use
from the system and AdministrationOSoftware Sources (page 131) controls which
repositories you can download software from and how often the system checks for
updated software. 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 dropdown list that enables you to add the launcher to the Top panel or desktop.

GETTING THE MOST OUT OF THE DESKTOP

1

123

— Titlebar
Buttons

Toolbar

Desktop

H

Pictures

Pub c

-

scrollbar

Templates

Window contents

A.
rai-Oi^-afl.memos.Ldi.bz2

Examples

"letter" selected (0 bytes}

Figure 4-16

A typical window

WINDOWS
In a workspace, a window is a region that runs, or is controlled by, a particular program (Figure 4-16). 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 121) on the
Bottom panel.
Titlebar

A titlebar (Figures 4-16 and 4-17) appears at the top of most windows and controls
the window it is attached to. You can change the appearance and function of a titlebar, but it will usually have at least the functionality of the buttons shown in
Figure 4-17.
The minimize (iconify) button collapses the window so that the only indication of
its presence is its Window List applet on the Bottom panel; click this applet to
restore the window. Click the maximize button to expand the window so that it

Figure 4-17

A window titlebar

124

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

occupies the whole workspace; click the same button (now displaying a square in
place of the up arrow) on the titlebar of a maximized window to restore the window to its former size. You can also double-click the titlebar to maximize and
restore a window. Clicking the close button closes the window and usually terminates the program running in it. To reposition the window, left-click the titlebar and
drag the window to the desired location.
W i n d o w Operations
menu

Toolbar

The Window Operations menu contains operations that you most commonly need
t 0 perform on any window. Right-click either the titlebar or the Window List applet
(page 121) to display this menu. You can use this menu to move a window to
another workspace, keep the window on top of or below other windows, and cause
the window to always be visible on the displayed workspace.
A toolbar (Figure 4-16, preceding page) usually appears near the top of a window
and contains icons, text, applets, menus, and more. Many kinds of toolbars exist.
The titlebar is not a toolbar; rather, it is part of the window decorations placed
there by the window manager (page 155).

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 121), you
can change which window on the displayed workspace has the input focus by using
the keyboard; this process is called window cycling. When you press ALT-TAB,
GNOME displays in the center of the workspace a box that holds icons representing the programs running in the windows in the workspace. It also shifts the input
focus to the window that was active just before the currently active window, making it easy to switch back and forth between two windows. When you hold ALT and
press TAB multiple times, the focus moves from window to window. Holding ALT and
SHIFT and repeatedly pressing TAB cycles in the other direction. See page 153 for more
information on the 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. In the first method, you use the clipboard,
technically called the copy buffer, to copy or move objects or text. To do so, you
explicitly copy an object or text to the buffer and then paste it somewhere else.
Applications that follow the user interface guidelines use CONTROL-X to cut, CONTROL-C to
copy, and CONTROL-V to paste. Application context menus frequently provide these
same options.
You may not be familiar with the second method to copy and paste text—using the
selection or primary buffer, which always contains the text you most recently selected
(highlighted). You cannot use this method to copy objects. Clicking the middle mouse
button (click the scroll wheel on a mouse that has one) pastes the contents of the

GETTING THE M O S T OUT OF THE D E S K T O P

125

selection buffer at the location of the mouse pointer. If you are using a two-button
mouse, click both buttons at the same time to simulate clicking the middle button.
With both these techniques, start by highlighting an object or text to select it. You
can drag a box around multiple objects to select them or drag the mouse pointer
over text to select it. Double-click to select a word or triple-click to select a line or a
paragraph.
Next, to use the clipboard, explicitly copy (CONTROL-C) or cut (CONTROL-X) the objects or
text. If you want to use the selection buffer, skip this step.
To paste the selected objects or text, position the mouse pointer where you want to
put it and then either press CONTROL-V (clipboard method) or press the middle mouse
button (selection buffer method).
Use SHIFT-CONTROL-Cand SHIFT-CONTROL-V within a terminal emulator
tip The CONTROL-C, CONTROL-X, and CONTROL-V characters do not work in a terminal emulator window
because the shell running in the window intercepts them before the terminal emulator can receive
them. However, you can use SHIFT-CONTROL-Cand SHIFT-CONTROL-X, respectively, in their place. There
is no keyboard shortcut for CONTROL-X. YOU can also use the selection buffer in this environment
or use copy/paste from the Edit selection on the menubar or from the context menu (right-click).
When using the clipboard, you can give as many commands as you like between the
CONTROL-C or CONTROL-X and C0NTR0L-V, as long as you do not press C0NTR0L-C or CONTROL-X
again. When using the selection buffer, you can give other commands after selecting
text and before pasting it, as long as you do not select (highlight) other text.

USING THE ROOT WINDOW
The root window is any part of a workspace that is not occupied by a window,
panel, or object. It is the part of the workspace where you can see the background.
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 113) to its Background tab.

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-18, next page),
select Main menu: Applications •=>Accessories •=>Terminal or enter the command
gnome-terminal from a Run Application window (ALT-F2). Because you are already

126

CHAPTER 4

I N T R O D U C T I O N TO U B U N T U L I N U X

O O O »m@]ynxl: File Edit View Terminal Help

sflm£Lynxl:-s Is
all.ieiDS.tar.bz2 Desktop
Downloads
Husic
Public Videos
app.pine
Documents l i t t e r {copy) Pictures Templates Videos (copyl
sam$lynxl:~s
samfil.yiixl:-$ df -h
F1 lesystern
size Used Avail (Jse% Mounted on
/dev/sdal
195G 3.1G 182G 2\/
none
498M 248K 497M 1\/dev
none
592M 264K 591M I% /dev/shm
none
502M 194K 502M 1% /var/run
B 502M 0% /var/lock
none
502M
none
592H
B 592H
/Ub/inlt/rw
saAdministration1^
Software 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 Sources window has five tabs, which are discussed next.

Ubuntu Software

The Ubuntu Software tab controls which categories of packages (page 522) APT
(page 522) and s y n a p t i c 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 dropdown 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 frame 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
Other Software tab.

Add only repositories you know to be trustworthy
security Adding software from sources other than the official Ubuntu repositories can cause the system to
not work properly and cause updates to fail. Even worse, it can make the system vulnerable to
attack.
The package installation process runs with root privileges. Regard adding a repository as giving
the person in control of that repository the s u d o password. Do not add a third-party repository
unless you trust it implicitly.

132

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

0 9 0

Software Sources

Ubuntu Software Other Software Updates Authentication Statistics
Ubuntu updates
Important security updates (lucid-security)
i£ Recommended updates [lucid-updates)
Pre-released updates (lucid-proposed)
r unsupported updates (iucld-backports)
Automatic updates
kif Check for updates:

T

Daily

Install security updates without confirmation
P Download all updates in the background
« only notify about available updates
Release upgrade
Show new distribution releases:

Long term support releases oniy T

| Revert

Figure 4-22

Close

The Software Sources window, Updates tab

Other Software You can add, edit, and remove repositories from the Other 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 repositories.
To add a CD/DVD as a repository, click Add CD-ROM.
Updates The top part of the Updates tab (Figure 4-22) specifies which types of updates you
want the Update Manager to monitor. Typically you will want to monitor important security updates and recommended updates. In the middle section 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. The drop-down list labeled Show new distribution releases allows you to specify whether you want the Update Manager to inform
you when you can upgrade the system to a new release of Ubuntu and whether you
are interested in all releases or just LTS (page 31) releases.
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.

THE UBUNTU SOFTWARE CENTER
You can use the Ubuntu Software Center window (Figure 4-23) to add and remove
applications from the system. It is simpler and has fewer selections than synaptic

UPDATING, INSTALLING, AND REMOVING SOFTWARE P A C K A G E S

Figure 4-23

133

The Add/Remove Applications window

(described next). Open this window by selecting Main menu: Applications1^
Ubuntu Software Center or by giving the command software-center from a terminal
emulator or Run Application window (ALT-F2).
When you select a category of applications from the window when you first open it,
the Ubuntu Software Center displays a list of applications in that category on the
right side of the window. If you know the name of the application you want to
install, you can query for it by entering the name or part of the name of the application in the text box at the upper-right corner of the window. The Ubuntu Software
Center displays a list of applications that satisfy your query.
Scroll through the applications displayed on the right side of the window. When you
click/highlight an application, the window displays two buttons: More Info and
Install. Click the first button to display information about the application. When
you click Install, the Ubuntu Software Center asks for your password and starts
downloading and installing the application. While it is working, you can search for
and select additional applications to install. When it is finished, the Ubuntu Software Center puts a green check mark next to the name of the package. Close the
window. Packages you installed should be available 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
System1^AdministrationOSynaptic Package Manager from the Main menu or by

134

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

O * *

Synaptic Package Manager

File Edit Package Settings Help
^il

^

Reload

Mark All Upgrades

Quick search
A|JJJI y

Properties

Seaich

Category list box
Handle
button
on the essential components for a basic exim4 installation.
The Debian exim4 packages have their own web page,
Custom FilU:i:>

http;1|j,pkg-exim4,a lioth.debian.org', There is also a Debian-specific

Search Resui S

configured can be found in

FAQ list, information about the way the Debian packages are
1? packages listed, 1306 installed, 0 broken. 0 to installAipgrade, 0 to remove

Figure 4-24

The Synaptic Package Manager window

giving the command gksudo synaptic from a terminal emulator or Run Application
window (ALT-F2). Figure 4-24 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 searching for a package. To
do so, 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 in the text box labeled Search.
(Alternatively, you can search using the text box labeled Quick search on the main
Synaptic window.) For example, to display all packages related to exim4, enter
exim4 in the text box labeled Search and select Description and Name from the
drop-down list labeled Look in (Figure 4-25). Click Search. The Synaptic Package
Manager window displays the list of packages meeting the search criteria specified
9

Find

Search:

exim4

Look in:

Description and Name

Cancel
Figure 4-25

i

Qj Search

The Find window

UPDATING,

INSTALLING, AND REMOVING SOFTWARE

PACKAGES

1 3 5

Synaptic Package Manager
File Edit Package Settings Help

C

Reload

searcn
Mark All Upgrades

All

Package

chess

Q|

email
exim4

q^
Search

("I

installed version

Latest version

dreamchess
• Unmark
dreamchess-da
Mark for installation
eboard

a
df

L

Mark for Reinstallation
a 3D c h e s s gam
Get Screens hot
Sections
Status

Dreamchess featur
board sets,
ranging from classli

Origin

Ö
*

Mark loi Upqradé
Mark fot Removal
Mark for Complete Removal

; chess

Properties
Mark Recommended for installation

Mark Suggested for Installation
A rnoderaLely shorn
er, should
custom Filters
this
engine be too weak for you. OKU you can use any other XBoard
Search Results
rnmriatlhle r h « (
SFI packacjes liL,1edr 130(5 installed, 0 broken. 0 ID irislall/upqrarie, 0 to remove

Figure 4-26

The Synaptic Package Manager window displaying chess programs

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. The synaptic utility 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; see the
www.dreamchess.org Web site). 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-26); click this selection. Because the dreamchess package is dependent on other packages that are not
installed, synaptic displays a window asking if you want to mark additional required
changes (Figure 4-27). This window lists the additional packages synaptic needs to
install so Dream Chess will run. Click Mark to mark the additional packages; these
packages are then highlighted in green.

Mark additional required changes?
The chosen action also affects other packages.
The following changes arcrequiredin order to
- lb be installed
drewnchcu-diiEii

Figure 4-27

Mark additional required changes screen

136

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

To apply the changes you have marked, click Apply on the toolbar; synaptic displays
a Summary window. (If you were installing and/or removing several packages, this
summary would be longer.) Click Apply. The synaptic utility 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: Applications^Games menu.

WHERETO 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 m a n (or manual) and info pages have been available via the m a n and
info utilities since early releases of the operating system. Ubuntu provides a graphical help center. Not surprisingly, with the ongoing growth of Linux and the Internet,
the sources of documentation have expanded as well. This section discusses some of
the places you can look for information on Linux in general and on Ubuntu in particular. See also Appendix B.

UBUNTU HELP CENTER
To display the Ubuntu Help Center window (Figure 4-28), click the blue object
with a question mark in it on the Top panel or select Main menu: SystemOHelp
and Support. 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.

m a n : DISPLAYS THE SYSTEM MANUAL
In addition to the graphical Ubuntu Help Center, the textual m a n utility displays
( m a n ) 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 m a n 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.

Online m a n pages
t i p The Ubuntu manpages.ubuntu.com site holds dynamically generated copies of m a n pages from
every package of every supported Ubuntu release. In addition to presenting m a n pages in easyto-read HTML format, this site does not require you to install the package holding a utility to read
its m a n page. It also allows you to read m a n pages for a release you do not have installed.
Because m a n is a character-based utility, you need to open a terminal emulator window (page 125) to run it. You can also log in on a virtual terminal (page 149) and
run m a n from there.

W H E R E TO FIND D O C U M E N T A T I O N

O O Q

137

Ubuntu Help Center

File Edit Go Bookmarks Help
Search:

Ubuntu Help Center
Topics

W e l c o m e to the Ubuntu Help Center

New to Ubuntu?
Adding, Removing
and Updating
Applications
Files, Folders a n d
Documents

To find lu'b insert d keyword in Üie search bdr
Common Questions
.
.
.
.
.

Connecting to the internet
Cndblinq visudl effects
P]dying music
Importing phnrns
Keeping your computer updated

Customizing Your
Computer

Can't find the

Internet and
Networks

Tfie Ubuntu community provides extensive free
sqjfnt

Music, Video and
Photos
Assistive Tools
Keeping Your
Computer Safe

answer?

Canonical, its partners and approved companies
provide cummer Lid! technicdl suppuiL
How to Contribute
Ubuntu has dn open dnd vibrant community of
contributors. Find out how to contribute

The Ubuntu Help Center window

Figure 4-28

To find out more about a utility, give the command man, followed by the name of
the utility. Figure 4 - 2 9 shows man displaying information about itself; the user
entered a man man command.
O O O

lannglynxl: -

File Edit View Terminal Help

MAN(X)
NAME

Manual pager u t l l s

MAN(1}

man - art interface to the on-line reference manuals

SYNOPSIS
nan [-C f i l e ] l - d j [-D] I--warnings[=warninas11 [-R encoding 1 l - L
locale] [-b s y s i f i j n t . . . . ] ] (-M path] [ - S l i s t ) [ - e extension! ( - i | - I )
[--regex|--wildcard]
f--names-only] f-a] [ - u ] [--no-subpages] [ - P
pagerl f - r proopt] [ - 7 ] [ - E encoding] ¡--no-hyphenation] [ - n o - j u s t i f i cation! [ - P strlnol [~t] l - T l d e v l c e l I I-HI browser 11 [ - X l f l ß i J ] [ - Z ]
I[section! pane . . . ] . . .
man -k fapropos options] reqexp .
man -K [ w|-Wl [ - S U s t l i - i | - I ] t ^ K f l S X l ISetULfifl) tMJM . . .
man - f Iwliatis options] page . . .
man -I | - c t i l e I l - d j I-DJ I--warnings[=warnlna5]1 [-R encoding] l - L
locale 1 [ - P aaaer1 l - r prompt] 1-7J [ - E encoding] [ - p s t r i n g ] [ - t j
[-TtdfiYiifi]] f-Hfbrowser] 1 i-Xtstpi]] J-Z] f i l e . . .
man -w|-W f-C i i i f i ] f-dl f-DJ page . . .
man -c [-C i l l s ) f-d] f-D] page . . .
man

[-hV]

DESCRIPTION
man i s the system's manual pager. Each page argument given to man i s
normally the name of a program, u t i l i t y or function. The manual page
associated with each of these arguments i s then found and displayed. A
section, i f provided, w i l l direct nan to look only in that section of
the manual. The default action i s to search i n a l l of the available

Figure 4-29

The man utility displaying information about itself

138

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

less (pager) The m a n utility automatically sends its output through a pager—usually
less
(page 162), 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(l) line 1] at the bottom
of the screen after it displays each screen of text and waits for you to request
another screen of text by pressing the SPACE bar. You can also use the PAGEUP, PAGEDOWN,
UP ARROW, and DOWN ARROW keys to navigate the text. Pressing h (help) displays a list of
less commands. Pressing q (quit) stops less and causes the shell to display a prompt.
You can search for topics covered by m a n pages using the apropos utility (page 139).
Manual sections Based on the FHS (Filesystem Hierarchy Standard; page 213), the Linux system
manual and the m a n 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 U N I X manuals has always been
divided. Unless you specify a manual section, m a n displays the earliest occurrence in
the manual of the word you specify on the command line. Most users find the information they need in sections 1, 6, and 7; programmers and system administrators
frequently need to consult the other sections.
In some cases the manual contains entries for different tools with the same name.
For example, the following command displays the m a n page for the p a s s w d utility
from section 1 of the system manual:
$ man passwd

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

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

WHERE TO FIND DOCUMENTATION

139

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 1135) to the command follow the option and a SPACE. For more information refer to "Options" on page 239.

a p r o p o s : SEARCHES FOR A KEYWORD
When you do not know the name of the command required
task, you can use apropos with a keyword to search for it.
the keyword in the short description line of all m a n pages
contain a match. The m a n utility, when called with the - k
vides the same output as apropos.

to carry out a particular
This utility searches for
and displays those that
(keyword) option, pro-

The database a p r o p o s uses, named whatis, is not available on Ubuntu systems when
they are first installed, but is built automatically by crond (page 605) using m a n d b .
If a p r o p o s does not produce any output, give the command s u d o mandb.
The following example shows the output of apropos when you call it with the w h o
keyword. The output includes the name of each command, the section of the manual that contains it, and the brief description from the m a n page. This list includes
the utility you need ( w h o ) and identifies other, related tools you might find useful:
$ apropos who
at.allow (5)
at.deny (5)
from (1)
w (1)
w.procps (1)
who (1)
whoami (1)

-

determine who can submit jobs v i a at or
determine who can submit jobs v i a at or
print names of those who have sent mail
Show who is logged on and w h a t they are
Show who is logged on and w h a t they are
show who is logged on
print effective userid

batch
batch
doing,
doing,

whatis The whatis utility is similar to a p r o p o s but finds only complete word matches for the
name of the utility:
$ whati s who
who (1)

- show who is logged on

info: DISPLAYS INFORMATION ABOUT UTILITIES
The textual info utility is a menu-based hypertext system developed by the GNU
project (page 4) and distributed with Ubuntu. It includes a tutorial on itself and
documentation on many Linux shells, utilities, and programs developed by the
GNU project (www.gnu.org/software/texinfo/manual/info). Figure 4 - 3 0 (next page)
shows the screen that info displays when you give the command i n f o c o r e u t i l s (the
c o r e u t i l s software package holds the Linux core utilities).

140

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

Figure 4-30

The initial screen displayed by the command info coreutils

m a n and info display different information
t i p The info utility displays more complete and up-to-date information on GNU utilities than does
m a n . When a m a n page displays abbreviated information on a utility that is covered by info, the
m a n page refers to info. The m a n 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 m a n page.
Because the information on this screen is drawn from an editable file, your display
may differ from the screens shown in this section. You can press any of the following keys while the initial info screen is displayed:
• h to go through an interactive tutorial on info
• ? to list info commands
• SPACE t o s c r o l l t h r o u g h t h e m e n u o f i t e m s f o r w h i c h i n f o r m a t i o n is a v a i l a b l e
• m f o l l o w e d b y t h e n a m e o f t h e m e n u y o u w a n t t o d i s p l a y o r a SPACE t o d i s p l a y a list o f m e n u s

• q or C0NTR0L-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 C0NTR0L-H. Similarly, M - x means hold down the META or ALT
key and press x . (On some systems you need to press ESCAPE and then x to duplicate
the function of META-X.)
After giving the command info coreutils, press the SPACE bar a few times to scroll
through the display. Type /sleepRETURN to search for the string sleep. When you type
/, the cursor moves to the bottom line of the window and displays Search for string
[string]:, where string is the last string you searched for. Press RETURN to search for
string or enter the string you want to search for. Typing sleep displays sleep on that
line, and pressing RETURN displays the next occurrence of sleep.

W H E R E TO FIND DOCUMENTATION

O O O iam@1ynxl: File Edit View Terminal Help
* su invocation::

Run a command with substitute user and group I\

* timeout invocation::

Run a command with a time l i m i t

141

Process control
* k i l l invocation::

Sending a s i g n a l to processes.

Delaying
* 0leep invocation::

Delay Tor a s p e c i f i e d t i n e

Numeric operations
* factor invocation::
* seq invocation::

Print prime f a c t o r s
Print numeric sequences

F i l e permissions
* Mode S t r u c t u r e : :
Structure of f i l e mode b i t s .
* Symbolic Hades::
Mnemonic representation of f i l e mode b i t s .
* Numer i c Modes : :
F i l e node b i t s as o c t a l numbers.
- - z z - I n f o : ( c o r e u t i l s . i n f o . g z ) T o p , 328 l i n e s

1

Figure 4-31

I

The screen displayed by the command i n f o c o r e u t i l s
after you type /sleepRETURN twice

You may find p i n f o easier to use than info
t i p The p i n f o utility is similar to info but is more intuitive if you are not familiar with the e m a c s editor. This utility runs in a textual environment, as does info. When it is available, p i n f o uses color
to make its interface easier to use. If p i n f o is not installed on the system, use s y n a p t i c
(page 133) to install the pinfo package. Run p i n f o from a terminal emulator or Run Application
window (ALT-F2) and select Run in terminal.
Now type /RETURN (or /sleepRETURN) to search for the next occurrence of sleep as
shown in Figure 4 - 3 1 . The asterisk at the left end of the line indicates that this entry
is a menu item. Following the asterisk is the name of the menu item and a description of the item.
Each menu item is a link to the info page that describes the item. To jump to that
page, search for or use the ARROW keys to move the cursor to the line containing the
menu item and press RETURN. With the cursor positioned as it is in Figure 4 - 3 1 , press
RETURN to display information on sleep. Alternatively, you can type the name of the
menu item in a menu command to view the information: To display information on
sleep, for example, you can give the command m sleep, followed by RETURN. When
you type m (for menu), the cursor moves to the bottom line of the window (as it did
when you typed /) and displays Menu item:. Typing sleep displays sleep on that line,
and pressing RETURN displays information about the menu item you have chosen.
Figure 4 - 3 2 (on the next page) shows the top node of information on sleep. A node
groups a set of information you can scroll through by pressing the SPACE bar. To display the next node, press n . Press p to display the previous node.
As you read through this book and learn about new utilities, you can use m a n or info
to find out more about those utilities. If you can print PostScript documents, you
can print a manual page by using the m a n utility with the - t option. For example,

142

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

Figure 4-32

The info page on the sleep utility

man - t cat I lpr prints information about the cat utility. You can also use a Web
browser to display the documentation at manpages.ubuntu.com, www.tldp.org,
help.ubuntu.com, help.ubuntu.com/community, or answers.launchpad.net/ubuntu
and then print the desired information from the browser.

THE — h e l p OPTION
Another tool you can use in a textual environment is the —help option. Most GNU
utilities provide a —help option that displays information about the utility. NonGNU utilities 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
-A,
-b,
-e
-E,

--show-all
--number-nonblank
--show-ends

output.

equivalent to - v E T
number nonblank output lines
equivalent to - v E
display $ at end of each line

If the information that —help displays runs off the screen, send the output through
the less pager (page 138) using a pipe (page 170):
$ Is --help | less

H O W T O s : FINDING OUT HOW THINGS WORK
A H O W T O 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. M i n i - H O W T O s
offer shorter explanations. As with Linux software, one person or a few people generally are responsible for writing and maintaining a H O W T O document, but many
people may contribute to it.

W H E R E TO FIND DOCUMENTATION

0 Ö 9

143

• getpeemame failed, error was transport end point" - Google Search - Mozilla FlreTox

file B W » c w History fiookmailcs ixrts tHHp
*

*

W

&

' S I hUiiJfwww. g«yjlJ II Ii m» mtdmanng Mlh regular ...
gMJ^fluo^xoiii'uioupftemnaanili^.n
• CwSTM • Slnilai

» Shaw HHKf) |M*5

q e t p f c t m a n » taJlad. Error w a s T r a n s p o r t e n d p o i n t tr. noi connoded
2 pwili - Z mJtm - Lhü puü CW 14. ÏOJU
•j«pecrnam* tailed. Enor was Tramfxxi crtdpolm ft not ccmecicfl Untn NccmriUng
Cuclwd SnKui

IB Transport endpoint re riot...
Oct 4, 7005 ... (»09100* II OJ-IO. 0) H*idll_SDCk r . 1 5 0 ) gatpMriumit
MM. Error was Transport ei'dpt-nK a nut cam«*«! lïWS/lOW...
A Ranger's Tale » Samba etron getpeemame faded
Apr IX XOF.... Apr 13 ffl i.H I
tmi^l 1&17] gnpMnuma Mil«]. Error IHM

Problem Accessing Samba S

Transport endpoint not connected

Figure 4-33

Google reporting on an error message

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

GETTING HELP
G N O M E provides tooltips (page 118), a context-sensitive Help system, and Ubuntu
provides the Ubuntu Help Center discussed on page 136.

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

USING THE INTERNET TO GET HELP
The Internet provides many helpful sites related to Linux. Aside from sites that offer
various forms of documentation, you can enter an error message from a program
you are having a problem with in a search engine such as Google (www.google.com,
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-33.

144

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

Ubuntu Web sites The Ubuntu Web site is a rich source of information. The following list identifies
some locations that may be of interest:
• 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 another 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 package 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) to obtain other documentation and GNU resources.
Many of the GNU pages and resources are available in a variety of languages.
The Linux The Linux Documentation Project (www.tldp.org; Figure 4-34), which has been
Documentation around for almost as long as Linux, houses a complete collection of guides, HOWProject J Q S j pAQs, man pages, and Linux magazines. The home page is available in
English, Portuguese, Spanish, Italian, Korean, and French. It is easy to use and supports local text searches. It also provides a complete set of links you can use to find
almost anything you want related to Linux (click Links in the Search box or go to
www.tldp.org/links). The links page includes sections on general information,
events, getting started, user groups, mailing lists, and newsgroups, with each section
containing many subsections.

MORE ABOUT LOGGING IN
Refer to "Logging In on the System" on page 100 for information about logging in.
This section covers options you can choose from the Login screen and solutions to
common login problems. It also describes how to log in from a terminal and from a
remote system.

Always use a password
security

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

MORE ABOUT LOGGING IN

0 Ô ®

145

The Linux D o c u m e n t a t i o n P r o j e c t - M o z i l l a F l r e f o x

flic B M ¥icw History fiookmailcs î w * s ö d p
&
—I

-'I - I-.,I ,•.-.:•'

"llhtWfldp-org/

The LiniA Documentation Project

-, I .

+

T h e
L i nil*
Documentation
P

Figure 4-34

roject

The Linux Documentation Project home page

THE LOGIN SCREEN
The Login screen (Figure 4-1, page 100) presents a list of users who are allowed to
log in on the system. On the panel at the bottom of the screen are two buttons.
Click the button depicting a person in a circle to select from a list of accessibility
preferences that may make it easier for some people to use the system. Click the
button depicting a broken circle with a vertical line running through the break to
restart or shut down the system. Click your name from the list of users to log in.
Once you have clicked your name, the login screen displays a text box labeled
Password. In addition, it adds drop-down lists labeled Language, Keyboard, and
Sessions to the panel at the bottom of the screen. Enter your password in the text
box and press RETURN to log in.
Languages Before you log in, the drop-down list labeled Language displays the name of the language the upcoming session will use. To change the language of the upcoming and
future sessions, click the arrow at the right end of the list and select a language from
the drop-down list. If the language you want is not listed, select Other; Ubuntu displays the Languages window. Select the language you want from this window, click
OK, and then log in. The change in language preference affects window titles,
prompts, error messages, and other textual items displayed by G N O M E and many
applications.
Keyboard You can change the keyboard layout that the upcoming and future sessions expect
from the drop-down list labeled Keyboard.
Sessions You can use the drop-down list labeled Sessions to choose between window managers for the upcoming and future sessions. Click the arrow at the right end of the list,

146

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

select a session from the drop-down list, and continue logging in. Selections in this
list vary but can include the following choices:
• GNOME—Brings up the G N O M E desktop environment.
• KDE—Brings up the KDE desktop environment (if you have installed
Kubuntu or KDE; page 75).
• Failsafe GNOME—Brings up a default G N O M E session without running any startup scripts. Use this choice to fix problems that prevent
you from logging in normally.
• xterm—Brings up an x t e r m 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 standard login
does not work well enough to allow you to log in to fix a problem.
Give the command exit from the x t e r m window to log out and display
the Login screen.

WHAT TO D o IF Y o u CANNOT LOG IN
If you enter either your username or your password incorrectly, the system displays
an error message after you enter both your username and your password. This message indicates that you have entered either the username or the password incorrectly
or that they are not valid. It does not differentiate between an unacceptable username and an unacceptable password—a strategy meant to discourage unauthorized
people from guessing names and passwords to gain access to the system.
Following are some common reasons why logins fail:
• The username and password are case sensitive. Make sure the CAPS LOCK key
is off and enter your username and password exactly as specified or as you
set them up.
• You are not logging in on the right machine. The login/password combination 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 administrator, refer to "Configuring User and Group Accounts" on page 594.
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 boot the system in recovery mode
(page 4 4 5 ) and delete some files.
• The account is disabled. The root account is disabled by default. An
administrator may disable other accounts. Often the root account is not
allowed to log in over a network. Use sudo (page 4 2 1 ) if you need to work
with root privileges.

MORE ABOUT LOGGING IN

147

Refer to "Changing Your Password" on page 148 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.

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 1106.
When you log in via a dial-up line, the connection is straightforward: You instruct
the local emulator program to contact the remote Linux system, it dials the phone,
and the remote system displays a login prompt. When you log in via a directly connected network, you either use ssh (secure; page 670) or telnet (not secure;
page 391) to connect to the remote system. The ssh program has been implemented
on many operating systems, not just Linux. Many user interfaces to ssh include a
terminal emulator. From an Apple, Windows, or U N I X machine, open the program
that runs ssh and give it the name or IP address (refer to "Host Address" on
page 381) 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 125. The next section provides more information about
logging in from a terminal emulator.

LOGGING IN FROM A TERMINAL (EMULATOR)
Before you log in on a terminal, terminal emulator, or other textual device, the system displays a message called issue (stored in the /etc/issue file) that identifies the
version of Ubuntu running on the system. A sample issue message follows:
Ubuntu 10.04 LTS plum tty2

This message is followed by a prompt to log in. Enter your username and password
in response to the system prompts. If you are using a terminal (page 1176) 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 C0NTR0L-Q (Xoff). If you are using a workstation
(page 1181), run
ssh (page 670), telnet (page 391), or whatever communications/emulation software
you use to log in on the system.
Once the shell prompt (or just prompt) appears, you have successfully logged in; this
prompt shows the system is ready for you to give a command. The first shell prompt

148

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

line may be preceded by a short message called the message of the day, or motd
(page 494), which is stored in the /etc/motd file. Ubuntu establishes a prompt of
[user@host: directory]$, where user is your username, host is the name of the system,
and directory is the name of the directory you are working in. A tilde (~) represents
your home directory. For information on how to change the prompt, refer to page 321.

Did you log in last?
s e c u r i t y When you log 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 has accessed the
account since you last used it. If someone has, perhaps an unauthorized user has learned your
password and logged in as you. In the interest of maintaining security, advise the system administrator of any circumstances that make you suspicious—and change your password.

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

Protect your password
security

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

Choose a password that is difficult to guess
security

Do not use phone numbers, names of pets or kids, birthdays, words from a dictionary (not even
a foreign language), and so forth. Do not use permutations of these items or a I33t-speak variation
of a word: 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.
To change your password, select Main menu: SystemOPreferences1^About 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.
To be relatively secure, a password should contain a combination of numbers,
uppercase and lowercase letters, and punctuation characters. It should also meet the
following criteria:

MORE ABOUT LOGGING IN

149

• Must be at least six characters long (or longer if the system administrator
sets it up that way). Seven or eight characters is a good compromise
between length and security.
• Should not be a word in a dictionary of any language, no matter how
seemingly obscure.
• Should not be the name of a person, place, pet, or other thing that might
be discovered easily.
• Should contain at least two letters and one digit or punctuation character.
• Should not be your username, the reverse of your username, or your username shifted by one or more characters.
Only the first item is mandatory. Avoid using control characters (such as CONTROL-H)
because they 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 619 for more information about choosing a password.

p w g e n helps you pick a password
s e c u r i t y The p w g e n utility (install the pwgen package) generates a list of almost random passwords. With
a little imagination, you can pronounce, and therefore remember, some of these passwords.
After you enter your new password, the system asks you to retype it to ensure you
did not make a mistake when you entered it the first time. If the new password is
the same both times you enter it, your password is changed. If the passwords differ,
you made an error in one of them. In this situation the system displays an error message or does not allow you to click the OK button. If the password you enter is not
long enough, the system displays a message similar to The password is too short.
When you successfully change your password, you change the way you log in. If
you forget your password, a user running with root privileges can change it and tell
you the new password.

USING VIRTUAL CONSOLES
When running Linux on a personal computer, you will frequently work with the
display and keyboard attached to the computer. Using this physical console, you can
access as many as 63 virtual consoles (also called virtual terminals). Some are set up
to allow logins; others act as graphical displays. To switch between virtual consoles,
hold the CONTROL and ALT keys down and press the function key that corresponds to
the console you want to view. For example, C0NTR0L-ALT-F5 displays the fifth virtual
console. This book refers to the console you see when you press C0NTR0L-ALT-F1 as the
system console, or just console.
By default, five or six virtual consoles are active and have textual login sessions running. When you want to use both textual and graphical interfaces, you can set up a

150

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

textual session on one virtual console and a graphical session on another. By
default, a graphical session runs on virtual console number 8.

WORKING FROM THE COMMAND LINE
Before the introduction of the graphical user interface (GUI), U N I X and then Linux
provided only a command-line (textual) interface (CLI). Today, a CLI is available
when you log in from a terminal, a terminal emulator, or a textual virtual console,
or when you use ssh (secure; page 6 6 7 ) or telnet (not secure; page 3 9 1 ) to log in on
a system.
This section introduces the Linux CLI. Chapter 5 describes some of the more important utilities you can use from the command line. Most of the examples in Parts IV
and V of this book use the CLI, adding examples of graphical tools where available.
Advantages Although the concept may seem antiquated, the CLI has a place in modern computof the CLI i n g_ i n 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 considerations mandate that a server
system run as few tasks as possible because each additional task can make the system more vulnerable to attack.
You can also write scripts using the CLI. Using scripts, you can easily reproduce
tasks on multiple systems, enabling you to scale the tasks to larger environments.
When you are the administrator of only a single system, using a GUI is often the easiest way to configure the system. When you act as administrator for many systems,
all of which need the same configuration installed or updated, a script can make the
task go more quickly. Writing a script using command-line tools is frequently easy,
whereas the same task can be difficult to impossible using graphical tools.
Pseudographical Before the introduction of GUIs, resourceful programmers created textual interfaces
interface that included graphical elements such as boxes, borders outlining rudimentary windows, highlights, and, more recently, color. These textual interfaces, called pseudographical interfaces, bridge the gap between textual and graphical interfaces.
One example of a modern utility that uses a pseudographical interface is the dpkgreconfigure 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 utilities do not interpret the command line or other text until after you press RETURN, you
can readily correct your typing mistakes before you press RETURN.

WORKING FROM THE COMMAND LINE

151

You can correct such mistakes in several ways: erase one character at a time, back
up a word at a time, or back up to the beginning of the command line in one step.
After you press RETURN, it is too late to correct a mistake: At that point, you must
either wait for the command to run to completion or abort execution of the program (page 151).

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

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

suspends a program

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. If you see a message containing the word
Stopped, you have just stopped your job using job control (page 255). If you give the command
fg to continue your job In the foreground, you should return to where you were before you pressed
the suspend key. For more Information refer to "bg: Sends a Job to the Background" on page 309.

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

ABORTING EXECUTION
Sometimes you 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

1. The command stty is an abbreviation for set teletypewriter,
stty is commonly thought of as meaning set terminal.

the first terminal UNIX was run on. Today

152

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

contents of a file that is several hundred pages long or copying a large file that is
not the one you meant to copy.
To terminate a program from a textual display, press the interrupt key (CONTROL-C or
sometimes DELETE or DEL). When you press this key, the Linux operating system sends
a termination signal to the program you are running and to the shell. Exactly what
effect this signal has depends on the program. Some programs stop execution immediately, some ignore the signal, and some take other actions. When the shell receives
a termination signal, it displays a prompt and waits for another command.
If these methods do not terminate the program, try sending the program a quit signal (CONTROLA). If all else fails, try pressing the suspend key (typically CONTROL-Z), giving
a jobs command to verify the number of the job running the program, and using kill
to abort the job. The job number is the number within the brackets at the left end of
the line displayed by jobs ([1]). In the next example, the kill command (page 455)
uses - T E R M to send a termination signal2 to the job specified by the job number,
which is preceded by a percent sign (%1). You can omit - T E R M from the command, as kill sends a termination signal by default.
$ bigjob
Z
[1]+
Stopped
$ jobs
[1]+
Stopped
$ kill - T E R M % 1
$ RETURN
[1]+
Killed

A

bi g job
bi g job

bi g job

The kill command returns a prompt; press RETURN again to see the confirmation message. For more information refer to "Running a Command in the Background" on
page 254.

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.
You can also repeat the previous command using !!. This technique is useful if you
forgot to use sudo (page 421) before a command. In this case, if you type sudo !!,
the shell will repeat the previous command preceded by sudo.
The command A old A new A reruns the previous command, substituting the first
occurrence of old with new. Also, on a command line, the shell replaces the characters !$ with the last argument (word) of the previous command. The following

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

CONTROLLING WINDOWS: ADVANCED OPERATIONS

153

example shows the user correcting the filename meno to memo using A n A m A and
then printing the file named memo by giving the command lpr !$. The shell replaces
!$ with memo, the last argument of the previous command.
$ cat meno
cat: meno: No such file or directory
$

AnAmA

cat memo
This is the memo file.
$ lpr !$
lpr memo

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 332.

optional

CONTROLLING WINDOWS: ADVANCED OPERATIONS
Refer to "Windows" on page 123 for an introduction to working with windows
under Ubuntu. This section explores the following topics: changing the input focus
on the workspace, 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 155) directs the characters you type somewhere, usually to a window. The active window is the window
accepting input from the keyboard; it 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 124):
• 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 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
mouse pointer onto another window, at which point the new window gets

154

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

the focus. 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 focus)—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 mouse pointer off the
window, at which point no window has the focus. 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.
You can use the Window Preferences window to change the focus policy. To display
this window, select Main menu: System "^Preferences "^Windows 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 available 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 focusstrictly-under-mouse method. To determine which of the latter methods you are
using, start typing something, with the mouse pointer positioned on the active window. Then move the mouse pointer over the root window and continue typing. If
characters continue to appear within the window, you are using focus-followsmouse; otherwise, you are using focus-strictly-under-mouse.

CHANGING THE RESOLUTION OF THE DISPLAY
The X server (the basis for the Linux graphical interface; page 268) starts at a specific display resolution and color depth (page 1141). 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. 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 - keys on the keyboard's numeric keypad. You can also use the Monitor Resolution Settings window (Main menu: SystemOPreferences^Monitors) to change
the resolution of the display.

CONTROLLING WINDOWS: ADVANCED OPERATIONS

155

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. To scroll the
display, push 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 G N O M E or KDE) and controls all
aspects of the windows in the X Window System environment. The window manager defines the appearance of the windows on the desktop and controls how you
operate and position them: open, close, move, resize, minimize, and so on. It may
also handle some session management functions, such as how a session is paused,
resumed, restarted, or ended (page 116).
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 do so. This setup is very different from that of many other operating systems, and the way that GNOME deals with window managers is different from
how other desktop environments work. Window managers do more than simply
manage windows—they provide a useful, good-looking, graphical shell where you
can work. Their open design allows users to define their own policies, down to the
fine details.
Theoretically GNOME is not dependent on any particular window manager and
can work with any of several window managers. Because of their flexibility, you
would not see major parts of the desktop environment change if you were to switch
from one window manager to another. A desktop manager collaborates with the
window manager to make your work environment intuitive and easy to use.
Although the desktop manager does not control window placement, it does get
information from the window manager about window placement.

UBUNTU WINDOW MANAGERS
Metacity and Compiz—the default window managers for GNOME—provide window management and start many components through GNOME panel objects.
They also communicate with and facilitate access to other components in the environment. The Visual Effects tab of the Appearance Preferences window (page 115)
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 133)
to install any of these packages.

156

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

CHAPTER SUMMARY
As with many operating systems, your access to a Linux system is authorized when
you log in. To do so, you enter your username and password on the Login screen.
You can change your password at any time while you are logged in. Choose a password that is difficult to guess and that conforms to the criteria imposed by the utility that changes your password.
The system administrator is responsible for maintaining the system. On a singleuser 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 something that requires root privileges, work with root privileges for only as long as
absolutely necessary; revert to working as yourself as soon as possible.
Understanding the desktop and its components is essential to getting the most out
of the Ubuntu GUI. Its panels offer a convenient way to launch applications, either
by clicking objects or by using the Main menu. The Main menu is a multilevel menu
you can work with to customize and maintain the system and to start many commonly used applications. A window is the graphical manifestation of an application. You can control its size, location, and appearance by clicking buttons on the
window's titlebar. A terminal emulator allows you to use the Linux command-line
interface from a graphical environment. You can use a terminal emulator to launch
both textual and graphical programs.
Panels and menus enable you to select an object (which can be just about anything
on the system). On a panel, you generally click an object; on a menu, you typically
click text in a list.
The GNOME environment provides users with a variety of interests and experience
levels—the casual user, the office worker, the power user, and the programmer/system designer—with a space to work in and a set of tools to work with. GNOME
also provides off-the-shelf productivity and many ways to customize its look, feel,
and response.
Nautilus is GNOME's simple, yet powerful file manager. It can create, open, display, move, and copy files and directories as well as execute programs and scripts.
One of its most basic and important functions is to create and manage the desktop.
The man utility provides online documentation for system utilities. This utility is
helpful both to new Linux users and to experienced users, who must often delve
into system documentation for information on the finer points of a utility's behav-

EXERCISES

157

ior. 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:
Login

incorrect

a. This message does not indicate whether your username, your password,
or both are invalid. Why does it not reveal this information?
b. Why does the system wait for a couple of seconds to respond after you
supply an incorrect username or password?
2. Give three examples of poor password choices. What is wrong with each?
3. Is fido an acceptable password? Give several reasons why or why not.
4. What is a context menu? How does a context menu differ from other
menus?
5. What appears when you right-click the root window? How can you use
this object?
6. How would you swap the effects of the right and left buttons on a mouse?
What is the drag-and-drop threshold? How would you change it?
7. What are the primary functions of the Main menu?
8. What is the input focus? When no window has the input focus, what happens to the letters you type on the keyboard? Which type of input focus
would you prefer to work with? Why?
9. What are the functions of a Window Operations menu? How do you display this menu?
10. What is a panel? Name a few objects on the panels and explain what you
can use them for. What do the Workspace Switcher applet and the Window List applet do?
11. What are tooltips? How are they useful?

158

CHAPTER 4

INTRODUCTION TO UBUNTU LINUX

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

5
THE L I N U X U T I L I T I E S
IN T H I S C H A P T E R
Special Characters

160

Basic Utilities

161

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

162

Working with Files

163

Ipr: Prints a File

165

| (Pipe): Communicates Between
Processes
170
Compressing and Archiving
Files

174

Obtaining User and System
Information

180

Tutorial: Using vim to Create
and Edit a File

186

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

159

160

CHAPTER 5

THE LINUX UTILITIES

SPECIAL CHARACTERS
Special characters, which have a special meaning to the shell, are discussed in "Filename Generation/Pathname Expansion" on page 256. These characters are mentioned 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:
[1

( ) $ < > { } # / \ !

Whitespace Although not considered special characters, RETURN, SPACE, and TAB have special meanings to the shell. RETURN usually ends a command line and initiates execution of a
command. The SPACE and TAB characters separate elements on the command line and
are collectively known as whitespace or blanks.
Quoting special If you need to use a character that has a special meaning to the shell as a regular
characters character, you can quote (or escape) it. When you quote a special character, you
keep the shell from giving it special meaning. The shell treats a quoted special character as a regular character. However, a slash (!) is always a separator in a pathname, even when you quote it.
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 Another way of quoting special characters is to enclose them between single quotation
marks marks: ' * * ' . You can quote many special and regular characters between a pair of single quotation marks: 'This is a special character: >'. The regular characters are interpreted as usual, and the shell also interprets the special characters as regular characters.
The only way to quote the erase character (C0NTR0L-H), the line kill character
(C0NTR0L-U), and other control characters (try C0NTR0L-M) is by preceding each with a
C0NTR0L-V. Single quotation marks and backslashes do not work. Try the following:
$ echo ' xxxxxxCONTROL-U'
$ echo xxxxxxCONTROL-VCONTROL-U

optional Although you cannot see the

C0NTR0L-U displayed by the second of the preceding pair
of commands, it is there. The following command sends the output of echo
(page 171) through a pipe (page 170) to od (octal display, see the od man page) to
display C0NTR0L-U as octal 25 (025):
$ echo xxxxxxCONTROL-VCONTROL-U | od -c
0000000
X
x
x
x
x
x 025
0000010

\n

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

BASIC UTILITIES

161

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

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

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

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

Is: 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 186.) After exiting from the editor, you can use the Is
(list) utility to display a list of the names of the files in your home directory. In the
first command in Figure 5-1, Is lists the name of the practice file. (You may also see
files that the system or a program created automatically.) Subsequent commands in
Figure 5-1 display the contents of the file and remove the file. These commands are
described next.
$ Is
practi ce
$ cat practice
This is a small file that I created
with a text editor.
$ rm practice
$ Is
$ cat practice
cat: practice: No such file or di rectory

S

Figure 5-1

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

162

CHAPTER 5

THE LINUX UTILITIES

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 2 4 7 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 command cat, followed by a SPACE and the name of the file. Figure 5-1 shows cat displaying the contents of practice. This figure shows the difference between the Is and cat
utilities: The Is utility displays the name of a file, whereas cat displays the contents
of a file.

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

A safer way of removing files
t i p 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 139 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
begins with a character other than y.
$ rm -i toollist
rm: remove regular file 'toollist'? y

Optional: You can create an alias (page 346) for rm - i and put it in your startup file (page 204) so
that rm always runs in interactive mode.

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

WORKING WITH FILES

163

h o s t n a m e : DISPLAYS THE SYSTEM NAME
The h o s t n a m e 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.

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 342.

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
name that cp assigns to the resulting (new) copy of the file.
$ Is
memo
$ cp memo memo.copy
$ Is
memo memo..copy

Figure 5-2

cp copies a file

is the

164

CHAPTER 5

THE LINUX UTILITIES

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 Is command shows
that memo is the only file in the directory. After the cp command, a second Is shows
two files in the directory, memo and memo.copy.
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 ( 0 1 3 0 ) in the copied file:
$ cp memo memo.0130

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

c p can destroy a file
caution

If the destination-file exists before you give a c p command, c p overwrites It. Because c p overwrites (and destroys the contents of) an existing destination-file without warning, you must take
care not to cause c p to overwrite a file that you need. The c p - i (Interactive) option prompts you
before It overwrites a file. See page 139 for a tip on options.
The following example assumes that the file named orange.2 exists before you give the c p command. The user answers y to overwrite the file:
$

cp

—i

orange

cp: overwrite

orange.2

'orange.2'?y

mv: CHANGES THE NAME OF A FILE
The mv (move) utility can rename a file without making a copy of it. The mv command line specifies an existing file and a new filename using the same syntax as cp:
mv existing-filename

new-filename

The command line in Figure 5-3 changes the name of the file memo to memo.0130.
The initial Is command shows that memo is the only file in the directory. After you
give the mv command, memo.0130 is the only file in the directory. Compare this
result to that of the cp example in Figure 5-2.
The mv utility can be used for more than changing the name of a file. Refer to " m v ,
cp: Move or Copy Files" on page 2 1 2 . See the mv info page for more information.

m v can destroy a file
c a u t i o n Just as c p can destroy a file, so can mv. Also like cp, m v has a - i (Interactive) option. See the
caution box labeled "cp can destroy a file."

WORKING WITH FILES

$

165

I s

memo
$ mv memo memo.0130
$

I s

memo.0130

Figure 5-3

mv renames a file

Ipr: PRINTS A FILE
The Ipr (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 single 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 Ipr 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:
$ Ipr 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:
$ Ipr - P mailroom report

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

Total Size
954061 bytes

In this example, M a x 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 Iprm utility to remove
the job from the print queue and stop it from printing:
$ Iprm 86

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

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

166

CHAPTER 5

THE LINUX UTILITIES

$ cat memo
Helen:
In our m e e t i n g on June 6 we
d i s c u s s e d the issue of credit.
Have you had any f u r t h e r t h o u g h t s
a b o u t it?
Max

$ grep 'credit' memo
d i s c u s s e d the issue of credit.

Figure 5-4

grep searches for a string

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

h e a d : DISPLAYS THE BEGINNING OFA 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 h e a d 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

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.

WORKING WITH FILES

167

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

Figure 5-5

head displays the first ten lines of a file

displayed, include a hyphen followed by the number of lines you want head to
display. For example, the following command displays only the first line of
months:
$ head -1 months
Jan

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

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

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

168

CHAPTER 5

THE LINUX UTILITIES

$ cat days
Monday
Tuesday
Wednesday
Thursday
Fri day
Saturday
Sunday

$ sort days
Fri day
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday

Figure 5-6

sort displays the lines of a file in order

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

uniq: REMOVES DUPLICATE LINES FROM A FILE
The uniq (unique) utility displays a file, skipping adjacent duplicate lines, 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.

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

WORKING WITH FILES

169

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

Figure 5-7

uniq removes duplicate lines

and which by a minus sign (-). In Figure 5-8, a minus sign indicates the colors.1 file;
a plus sign indicates the colors.2 file.
The diff - u command breaks long, multiline text into hunks. Each hunk is preceded
by a line starting and ending with two at signs (@@). This hunk identifier indicates
the starting line number and the number of lines from each file for this hunk. In
Figure 5-8, the hunk covers the section of the colors. 1 file (indicated by a minus
sign) from the first line through the sixth line. The + 1 , 5 then indicates 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 a SPACE. A leading minus sign indicates that the line
occurs only in the file denoted by the minus sign. A leading plus sign indicates that
the line occurs only in the file denoted by the plus sign. A line that begins with a
SPACE (neither a plus sign nor a minus sign) occurs in both files in the same location.
Refer to the diff info page for more information.
$ diff -u colors.1 colors.2
--- colors.1
2010-07-29 16:: 41 :
11.00000 10000 -0700
+++ colors.2
2010-07-29 16:: 41 :
17.00000 10000 -0700
@@ -1,6 +1,5 @@
red
+bl ue
green
yellow
-pi nk
-purple
orange

Figure 5-8

diff displaying the unified output format

170

CHAPTER 5

THE LINUX UTILITIES

file: IDENTIFIES THE CONTENTS OF A FILE
You can use the file utility to learn about the contents of any file on a Linux system
without having to open and examine the file yourself. In the following example, file
reports that l e t t e r _ e . b z 2 contains data that was compressed by the bzip2 utility
(page 174):
$ file letter_e.bz2
letter_e.bz2: bzip2 compressed data, block size = 900k

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

Refer to the file m a n page for more information.

(PIPE): COMMUNICATES BETWEEN PROCESSES
Because pipes are integral to the functioning of a Linux system, this chapter introduces them for use in examples. Pipes are covered in detail beginning on page 2 5 1 .
A process is the execution of a command by Linux (page 328). Communication
between processes is one of the hallmarks of both U N I X and Linux. A pipe (written
as a vertical bar, I, on the command line and appearing as a solid or broken vertical
line on a keyboard) provides the simplest form of this kind of communication. Simply 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 243.) Most of
what a process displays on the screen is sent to standard output. If you do not redirect it, this output appears on the screen. Using a pipe, you can redirect standard
output so it becomes 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. The following
command line sorts the lines of the months file (Figure 5-5, page 167) and uses head
to display the first four months of the sorted list:
$ sort months | head -4
Apr
Aug
Dec
Feb

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

FOUR MORE UTILITIES

171

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

FOUR MORE UTILITIES
The echo and date utilities are two of the most frequently used members of the large
collection of Linux utilities. The script utility records part of a session in a file, and
t o d o s makes a copy of a text file that can be read on either a Windows or a
Macintosh machine.

echo: DISPLAYS TEXT
The echo utility copies the characters you type on the command line after e c h o to
the screen. Figure 5-9 shows some examples. 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 utilities.
Some examples on page 2 5 7 use echo to illustrate how special characters, such as
the asterisk, work. Throughout Chapters 7, 9, and 27, e c h o helps explain how shell
variables work and how you can send messages from shell scripts to the screen.
Refer to the c o r e u t i l s info page, echo section for more information.
optional

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

The greater than (>) sign tells the shell to send the output of echo to the file named
m y f i l e instead of to the screen. For more information refer to "Redirecting Standard
Output" on page 2 4 6 .

$

I s

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 e c h o ) to the screen

172

CHAPTER 5

THE LINUX UTILITIES

d a t e : DISPLAYS THE TIME AND DATE
The date utility displays the current date and time:
$ date
Thu Jan 21 10:24:00 PST 2010

The following example shows how you can choose the format and select the contents of the output of date:
$ date +"%A %B %d"
Thursday January 21

Refer to the date info page for more information.

script: RECORDS A SHELL SESSION
The script utility records all or part of a login session, including your input and the
system's responses. This utility is useful only from character-based devices, such as a
terminal or a terminal emulator. It does capture a session with vim; however, because
vim uses control characters to position the cursor and display different typefaces,
such as bold, the output will be difficult to read and 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 specify a different
filename, follow the script command with a SPACE and the filename. To append to a
file, use the - a option after script but before the filename; otherwise script overwrites an existing file. Following is a session being recorded by script:
$ script
Script started, file is typescript
$ whoami
sam

$ Is -1 /bin
total 6632
-rwxr-xr-x 1
-rwxr-xr-x 1
-rwxr-xr-x 1
-rwxr-xr-x 1
$ exit
exi t
Script done,

| head -5
root
root
root
root

root 818232 2010-04-10
root
30200 2010-02-08
root 1269432 2010-01-22
root
30200 2010-02-08

05:10
02:54
08:23
02:54

bash
bunzip2
busybox
bzcat

file is typescript

Use the exit command to terminate a script session. You can then view the file you
created using cat, less, more, or an editor. Following is the file that was created by
the preceding script command:
$ cat typescript
Script started on Mon Sep 27 20:54:59 2010
$ whoami
sam

FOUR MORE UTILITIES
$ Is -1 /bin
total 6632
-rwxr-xr-x 1
-rwxr-xr-x 1
-rwxr-xr-x 1
-rwxr-xr-x 1
$ exit
exi t

173

| head -5
root
root
root
root

root 818232 2010-04-10
root
30200 2010-02-08
root 1269432 2010-01-22
root
30200 2010-02-08

05:10
02:54
08:23
02:54

bash
bunzip2
busybox
bzcat

Script done on Mon Sep 27 20:55:29 2010

If you will be editing the file with v i m , e m a c s , or another editor, you can use f r o m d o s
(below) to eliminate from the typescript file the A M characters that appear at the
ends of the lines. Refer to the script m a n page for more information.

t o d o s : CONVERTS LINUX AND MACINTOSH FILES TO
WINDOWS FORMAT
If you want to share a text file you created on a Linux system with someone on a
Windows or Macintosh system, you need to convert the file before the person on
the other system can read it easily. The todos (to DOS) utility converts a Linux text
file so 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:
$ todos memo.txt

You can now email the file as an attachment to someone on a Windows or Macintosh
system. Without any options, t o d o s overwrites the original file. Use the - b (backup)
option to cause t o d o s to make a copy of the file with a .bak filename extension before
modifying it.
fromdos You can use the f r o m d o s utility to convert Windows or Macintosh files so they can
be read on a Linux system:
$ fromdos memo.txt

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

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

174

CHAPTER 5

THE LINUX UTILITIES

COMPRESSING AND ARCHIVING FILES
Large files use a lot of disk space and take longer than smaller files to transfer from
one system to another over a network. If you do not need to look at the contents of
a large file often, you 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 another medium 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 frequently download compressed, archived files from the
Internet. The utilities described in this section compress and decompress files and
pack and unpack archives.

b z i p 2 : 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 , 0 0 0 lines of the l e t t e r _ 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.
$

I s

- 1

-rw-rw-r--

1 sam sam 584000 2010-03-01 22:31 letter_e

The -1 (long) option causes Is to display more information about a file. Here it
shows that l e t t e r _ e is 5 8 4 , 0 0 0 bytes long. The - v (verbose) option causes bzip2 to
report how much it was able to reduce the size of the file. In this case, it shrank the
file by 9 9 . 9 9 percent:
$ bzip2 -v letter_e
letter_e: 11680.00:1, 0.001 bits/byte, 99.99% saved, 584000 in, 50 out.
$ I s -1
-rw-rw-r-- 1 sam sam 50 2010-03-01 22:31 letter_e.bz2

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

Keep the original file by using the - k option
t i p The b z i p 2 utility (and its counterpart, b u n z i p 2 ) remove the original file when they compress or
decompress a file. Use the - k (keep) option to keep the original file.

COMPRESSING AND ARCHIVING FILES

175

In the following, more realistic example, the file zach.jpg contains a computer
graphics image:
$

I s

- 1

-rw-r--r--

1 sam sam 33287 2010-03-01 22:40 zach.jpg

The bzip2 utility can reduce the size of the file by only 28 percent because the image
is already in a compressed format:
$ bzip2 -v zach.jpg
zach.jpg:
$ I s -1
-rw-r—r—

1.391:1,

5.749 bits/byte, 28.13% saved, 33287 in, 23922 out.

1 sam sam 23922 2010-03-01 22:40 zach.jpg. bz2

Refer to the bzip2 m a n page, www.bzip.org, and the Bzip2 mini-HOWTO
page 142 for instructions on obtaining this document) for more information.

(see

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
$

I s

- 1

-rw-rw-r--

1 sam sam 584000 2010-03-01 22:31 letter_e

$ bunzip2 zach.jpg.bz2
$

I s

- 1

-rw-r--r--

1 sam sam

33287 2010-03-01 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
decompressed data. Like cat, bzcat does not change the source file. The pipe in the
following example redirects the output of bzcat so instead of being displayed on the
screen it becomes the input to head, which displays the first two lines of the file:
$ bzcat letter_e.bz2 | head -2
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

After bzcat is run, the contents of letter_e.bz is unchanged; the file is still stored on
the disk in compressed form.
bzip2recover The bzip2recover utility supports limited data recovery from media errors. Give the
command bzip2recover followed by the name of the compressed, corrupted file
from which you want to try to recover data.

gzip: COMPRESSES A FILE
gunzip and zcat The gzip (GNU zip) utility is older and less efficient than bzip2. Its flags and operation are very similar to those of bzip2. A file compressed by gzip is marked 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.

176

CHAPTER 5

THE LINUX UTILITIES

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.

gzip versus zip
tip

Do not confuse g z i p and g u n z i p with the z i p and u n z i p 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 z i p utility constructs a zip
archive, whereas u n z i p unpacks z i p archives. The zip and u n z i p utilities are compatible with
PKZIP, a Windows program that compresses and archives files.

tar: PACKS AND UNPACKS ARCHIVES
The tar utility performs many functions. Its name is short for tape archive, as its
original function was to create and read archive and backup tapes. Today it is used
to create a single file (called a tar file, archive, or tarball) from multiple files or
directory hierarchies and to extract files from a tar file. The cpio utility (page 602)
performs a similar function.
In the following example, the first Is shows the sizes of the files g, b, and d. Next tar
uses the - c (create), - v (verbose), and - f (write to or read from a file) options to create an archive named a l l . t a r from these files. Each line of output displays the name
of the file tar is appending to the archive it is creating.
The tar utility adds overhead when it creates an archive. The next command shows
that the archive file a l l . t a r 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:
t l s - l g b d
-rw-r--r-1 zach
-rw-r--r-1 zach
-rw-r--r-1 zach

other 1178 2 0 1 0 - 0 8 - 2 0 14:16 b
zach
3783 2 0 1 0 - 0 8 - 2 0 14:17 d
zach
1302 2 0 1 0 - 0 8 - 2 0 14:16 g

$ tar - c v f a l l . t a r g b

d

g
b
d
$ Is -1 all .tar
-rw-r--r-1 zach
$ tar - t v f
-rw-r--r--rw-r--r--rw-r--r--

all.tar
zach / z a c h
zach / o t h e r
zach / z a c h

zach

9728 2 0 1 0 - 0 8 - 2 0 14:17

all.tar

1302 2 0 1 0 - 0 8 - 2 0 14:16 g
1178 2 0 1 0 - 0 8 - 2 0 14:16 b
3783 2 0 1 0 - 0 8 - 2 0 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
2. Although the original UNIX tar did not use a leading hyphen to indicate an option on the command
line, the GNU/Linux version accepts hyphens, but works as well without them. This book precedes tar
options with a hyphen for consistency with most other utilities.

COMPRESSING AND ARCHIVING FILES

177

You can use bzip2, c o m p r e s s , or gzip to compress tar files, making them easier to
store and handle. Many files you download from the Internet will already be in one
of these formats. Files that have been processed by tar and compressed by bzip2 frequently have a filename extension of . t a r . b z 2 or . t b z . Those processed by tar and
gzip have an extension of . t a r . g z or .tz, whereas files processed by tar and c o m p r e s s
use . t a r . 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 example shows how to unpack the GNU make utility after it has been downloaded
(ftp.gnu.org/pub/gnu/make/make-3.80.tar.gz):
$ Is -1 mak*
-rw-r—r—

1 sam sam 1564560 2010-04-12 15:51 make-B.81. tar.gz

$ gunzip mak*
$ Is -1 mak*
-rw-r--r-- 1 sam sam 6072B20 2010-04-12 15:51 make-B.81.tar

$ tar -xvf mak*
make-3.81/
make-3.81/confi g/
make-3.81/confi g/dospaths. m4
make-3.81/tests/run_make_tests.pi
make-3.81/tests/test_driver.pi

The first command lists the downloaded tarred and g z i p p e d file: m a k e - 3 . 8 0 . t a r . g z
(about 1 . 2 megabytes). The asterisk ( * ) in the filename matches any characters in
any filenames (page 2 5 7 ) , so Is displays a list of files whose names begin with m a k ;
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
m a k e - 3 . 8 0 . t a r (no .gz extension), which is about 4 . 8 megabytes. The tar command
creates the m a k e - 3 . 8 0 directory in the working directory and unpacks the files into it.
$ Is -Id mak*
drwxr-xr-x 8 sam sam
4096 2006-03-31 22:42 make-3.81
-rw-r--r-- 1 sam sam 6072320 2010-04-12 15:51 make-3.81.tar

$ Is -1 make-3.80
total 1816
-rw-r--r--rw-r--r--rw-r--r--rw-r--r--

1
1
1
1

sam
sam
sam
sam

sam
sam
sam
sam

-rw-r--r-- 1 sam sam
-rw-r--r-- 1 sam sam
drwxr- xr-x 6 sam sam

53838
4918
33872
14231

2006--03-31
2006--02-11
2006--03-31
2002--10-14

22::
14::
22::
14::

39
16
39
54

AB0UT-NLS
acinclude.m4
aclocal.m4
alloca.c

16907 2006--02-11 14:: 16 vmsjobs.c
17397 2006--02-11 14::16 vpath.c
4096 2006--03-31 22::42 w32

After tar extracts the files from the archive, the working directory contains two files
whose names start with m a k : m a k e - 3 . 8 0 . t a r and m a k e - 3 . 8 0 . The - d (directory)
option causes Is to display only file and directory names, not the contents of directories as it normally does. The final Is command shows the files and directories in the
m a k e - 3 . 8 0 directory. Refer to the tar info page for more information.

178

CHAPTER 5

THE LINUX UTILITIES

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 208]), move the tar file into that directory, and expand it there. That way
the unpacked files will not mingle with existing files, and no confusion will occur. This strategy
also makes it easier to delete the extracted files. Depending on how they were created, some tar
files automatically create a new directory and put the files into it; the - t option indicates where tar
will place the files you extract.

tar: the - x option can overwrite files
caution

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

optional

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

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

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

LOCATING COMMANDS
The w h e r e i s and mlocate utilities can help you find a command whose name you
have forgotten or whose location you do not know. When multiple copies of a utility or program are present, which tells you which copy you will run. The mlocate
utility searches for files on the local system.

which AND whereis: LOCATE A UTILITY
When you give Linux a command, the shell searches a list of directories for a program with that name 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 3 1 9 . If you do not change the search
path, the shell searches only a standard set of directories and then stops searching.
However, other directories on the system may also contain useful utilities.
which The which utility locates utilities by displaying the full pathname of the file for the
utility. (Chapter 6 contains more information on pathnames and the structure of the

LOCATING COMMANDS

179

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

The which utility can be helpful when a utility 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 from the one you expected. ("Important Standard Directories and Files" on page 213 provides a list of standard locations for
executable files.) For example, if tar is not working properly and you find that you
are running / u s r / l o c a l / b i n / t a r 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 locations instead of using your search path. For example, you can find the locations for
files related to tar:
$ whereis tar
tar: /bin/tar /usr/include/tar.h /usr/share/man/manl/tar.l.gz

...

In this example whereis finds three references to tar: the tar utility file, a tar header
file, and the tar man page.

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

which, whereis, and builtin commands
caution

Both the w h i c h and w h e r e i s utilities report only the names for utilities as they are found on the
disk; they do not report shell builtins (utilities that are built into a shell; see page 261). When you
use w h e r e i s to try to find where the e c h o 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/manl/echo.1.gz

The w h e r e i s utility does not display the e c h o builtin. Even the w h i c h utility reports the wrong
information:
$ which echo
/bi n/echo

Under b a s h you can use the t y p e builtin (page 1003) to determine whether a command is a builtin:
$ type echo
echo is a shell builtin

180

CHAPTER 5

THE LINUX UTILITIES

m l o c a t e : SEARCHES FOR A FILE
The mlocate utility searches for files on the local system:
$ mlocate upstart
/etc/i ni t/upstart-udev-bri dge.conf
/etc/network/if-down.d/upstart
/etc/network/if-up.d/upstart
/li b/i ni t/upstart-job
/sbi n/upstart-udev-bri dge

This utility is part of the m l o c a t e software package; give the command sudo aptitude install m l o c a t e to install this package. Before you can use mlocate, the updatedb
utility must build or update the mlocate database. Typically the database is updated
once a day by a cron script (page 605).

If you are not on a network, skip to the v i m tutorial
tip

If you are the only user on a system that is not connected to a network, you may want to skip to
the tutorial on the vim editor on page 186. If you are not on a network but are set up to send and
receive email, read "Email" on page 185.

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,
w h o , 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 information about users on remote systems if the local system is attached to a network.
Table 5-1 on page 183 summarizes the output of these utilities.

w h o : LISTS USERS ON THE SYSTEM
The w h o utility displays a list of users who are logged in on the local system. In
Figure 5-10 the first column w h o displays shows that Sam, Max, and Zach are logged
in. (Max is logged in from two locations.) The second column shows the device that
each user's terminal, workstation, or terminal emulator is connected to. The third column shows the date and time the user logged in. An optional fourth column shows (in
parentheses) the name of the system that a remote user logged in from.

OBTAINING USER AND SYSTEM INFORMATION

181

$ who
sam
max
zach
max

Figure 5-10

tty4
tty2
ttyl
pts/4

2010-07-25 17:18
2010-07-25 16:42
2010-07-25 16:39
2010-07-25 17:27 (coffee)

w h o lists who is logged in

The information that w h o displays is useful when you
user on the local system. When the user is logged in,
to establish communication immediately. If w h o does
not need to communicate immediately, you can
(page 1 8 5 ) .

want to communicate with a
you can use write (page 1 8 4 )
not list the user or if you do
send email to that person

If the output of w h o scrolls off the screen, you can redirect the output through a
pipe (I, page 1 7 0 ) so that it becomes the input to less, which displays the output one
screen at a time. You can also use a pipe to redirect the output through grep to look
for a specific name.
If you need to find out which terminal you are using or what time you logged in,
you can use the command w h o a m i:
$ who am i
max

tty2

2010-07-25

16:42

finger: LISTS USERS ON THE SYSTEM
You can use finger to display a list of users who are logged in on the local 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 system is shown as the user's location. For example, in Figure 5 - 1 1
M a x is logged in from the remote system named c o f f e e . The asterisks ( * ) in front
of the device names in the T t y column indicate that the user has blocked messages sent directly to his terminal (refer to " m e s g : Denies or Accepts Messages"
on page 1 8 5 ) .
$ finger
Logi n
max
max
sam
zach

Figure 5-11

Name
Max W i I d
Max W i I d
Sam the Great
Zach Brill

Tty
*tty2
pts/4
*tty4
*ttyl

finger I: lists who is logged in

Idle
3
29
1:07

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

182

CHAPTER 5

THE LINUX UTILITIES

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

Figure 5-12

finger I I : lists details about one user

f i n g e r 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.
You can also use finger to learn more about an individual by specifying a username
on the command line. In Figure 5 - 1 2 , finger displays detailed information about
M a x . M a x is logged in and actively using one of his terminals ( t t y 2 ) ; he has not
used his other terminal ( p t s / 4 ) for 3 minutes and 7 seconds. You also learn from
finger that if you want to set up a meeting with M a x , you should contact Zach at
extension 1693.

.plan and .project Most of the information in Figure 5 - 1 2 was collected by finger from system files.
The information shown after the heading Plan:, however, was supplied by Max. The
finger utility searched for a file named .plan in Max's home directory and displayed
its contents.
(Filenames that begin with a period, such as .plan, are not normally listed by Is and
are called hidden filenames [page 204].) 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 M a x 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.
S w
17:47: 35 up 1 day,
8:10,
USER
TTY
FROM
s am
tty4
max
tty2
zach
ttyl
max
pts/4
coffee

Figure 5-13

T h e w utility

6 users, load average : 0.34, 0.23, 0.26
LOGIN®
IDLE
JCPU
PCPU WHAT
17:18
29:14m 0 ,20s 0 .00s vi memo
16:42
0.00s 0 ,20s 0 . 07s w
16:39
1:07
0 ,05s 0 .00s run_bdgt
17:27
3:10m 0 ,24s 0 .24s -bash

OBTAINING USER AND SYSTEM INFORMATION

183

You can also use finger to display a user's username. For example, on a system with
a user named Helen Simpson, you might know that Helen's last name is Simpson
but might not guess her username is his. The finger utility, which is not case sensitive, can search for information on Helen using her first or last name. The following
commands find the information you seek as well as information on other users
whose names are Helen or Simpson:
$ finger HELEN
Login: his

Name: Helen

Simpson.

Name: Helen

Simpson.

$ finger simpson
Login: his

See page 3 8 9 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 section on w h o , the information that w displays is useful when you want to communicate with someone at your installation.
The first column in Figure 5 - 1 3 shows that Max, Zach, and Sam are logged in. The
second column shows the name of the device file each user's terminal is connected
to. The third column shows the system that a remote user is logged in from. The
fourth column shows the time each user logged in. The fifth column indicates how
long each user has been idle (how much time has elapsed since the user pressed a
key on the keyboard). The next two columns identify how much computer processor time each user has used during this login session and on the task that user is running. The last column shows the command each user is running.
The first line that the w utility displays includes the time of day, the period of time
the computer has been running (in days, hours, and minutes), the number of users
logged in, and the load average (how busy the system is). The three load average
numbers represent the number of jobs waiting to run, averaged over the past 1, 5 ,
and 15 minutes. Use the uptime utility to display just this line. Table 5 - 1 compares
the w, w h o , and finger utilities.
Table 5 - 1

Comparison of w , w h o , and f i n g e r

Information displayed

w

who

finger

Username

X

X

X

Terminal-line identification (tty)

X

X

X

Login time (and day for old logins)

X
X

X

Login date and time
Idle time

x

x

184

CHAPTER 5

THE LINUX UTILITIES

Table 5 - 1

Comparison of w , w h o , and f i n g e r (continued)

Information displayed

w

Program the user is executing

X

Location the user logged in from
CPU time used

who

finger

X
X

Full name (or other information from /etc/passwd)

X

User-supplied vanity information

X

System uptime and load average

X

COMMUNICATING WITH OTHER USERS
You can use the utilities discussed in this section to exchange messages and files
with other users either interactively or through email.

write: SENDS A MESSAGE
The write utility sends a message to another user who is logged in. When you and
another user use write to send messages to each other, you establish two-way communication. Initially a write command (Figure 5-14) displays a banner on the other
user's terminal, saying that you are about to send a message.
The syntax of a write command line is
write username

[terminal]

The username is the username of the user you want to communicate with. The terminal is an optional device name that is useful if the user is logged in more than
once. You can display the usernames and device names of all users who are logged
in on the local system by using who, w, or finger.
To establish two-way communication with another user, you and the other user must
each execute write, specifying the other's username as the username. The write utility
then copies text, line by line, from one keyboard/display to the other (Figure 5-15).
Sometimes it helps to establish a convention, such as typing o (for "over") when you
are ready for the other person to type and typing oo (for "over and out") when you
are ready to end the conversation. When you want to stop communicating with the
other user, press CONTROL-D at the beginning of a line. Pressing CONTROL-D tells write to
$ write max
Hi Max, are you there? o

Figure 5-14

The write utility I

EMAIL

185

$ write max
Hi Max, are you there?
Message from maxObravo..example.com on pts/0 at 16:23 ...
Yes Zach, I'm here, o

Figure 5-15

The write utility II

quit, displays E O F (end of file) on the other user's terminal, and returns you to the
shell. The other user must do the same.
If the Message from banner appears on your screen and obscures something you are
working on, press CONTROL-L or CONTROL- R to refresh the screen and remove the banner.
Then you can clean up, exit from your work, and respond to the person who is
writing to you. You have to remember who is writing to you, however, because the
banner will no longer appear on the screen.

m e s g : DENIES OR ACCEPTS MESSAGES
By default, messages to your screen are blocked. Give the following m e s g command
to allow other users to send you messages:
$ mesg y

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

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

EMAIL
Email enables you to communicate with users on the local system and, if the installation 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. In this case the email is stored until the recipient reads it.
These utilities can also send the same message to more than one user at a time.

186

CHAPTER 5

THE LINUX UTILITIES

Many email programs are available for Linux, including the original character-based
mail program, Mozilla/Thunderbird, pine, mail through e m a c s , KMail, and evolution.
Another popular graphical email program is s y l p h e e d (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 incoming email; forwards email; and so on. The GNU Privacy Guard (GPG or GNUpg,
page 1113) encrypts and decrypts email and makes it almost impossible for an
unauthorized person to read.
Refer to Chapter 2 0 for more information on setting email clients and servers.
Network addresses If the local system is part of a LAN, you can generally send email to and receive
email from users on other systems on the LAN by using their usernames. Someone
sending M a x email on the Internet would need to specify his domain
name
(page 1146) along with his username. Use this address to send email to the author
of this book: mgs@sobell.com.

TUTORIAL: USING vim TO CREATE AND EDIT A FILE
This section explains how to start v i m , enter text, move the cursor, correct text, save
the file to the disk, and exit from v i m . The tutorial discusses three of the modes of
operation of v i m 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 instructional
program, named vimtutor. Give its name as a command to run it.

v i m t u t o r and v i m help files are not installed by default
t i p To run vimtutor and to get help as described on page 190, you must install the vim-runtime package; give the command sudo aptitude install vim-runtime to install this package.
Specifying a Because v i m takes advantage of features that are specific to various kinds of termiterminal n a l S j y O U m u s t 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 1106.

STARTING vim
Start v i m with the following command 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.

TUTORIAL: USING vim TO CREATE AND EDIT A FILE

Figure 5-16

187

Starting v i m

v i m is not installed by default: use v i m . t i n y
t i p The full version of the vim editor is not installed by default. Instead, a small version of vim, named
vim.tiny is installed. You can either replace each vim command in this section with vim.tiny, or
you can install the full vim editor by giving the command sudo aptitude install vim and then use
the vim command as shown in this section.

The vi command runs v i m
tip

On Ubuntu Linux systems the command vi runs vim in vi-compatible mode (page 193).
If you start v i m with a terminal type that is not in the terminfo database, v i m displays an error message and the terminal type defaults to ansi, which works on many
terminals. In the following example, the user mistyped v t l O O and set the terminal
type to v g l O O :
E558: Terminal entry not found in terminfo
'vgl00' not known. Available builtin terminals are:
bui1ti n_ansi
bui 1 ti n_xterm
bui 1 ti n_i ri s-ansi
bui1ti n_dumb
defaulting to 'ansi'

Emergency exit

T o r e s e t t h e t e r m i n a l t y p e , p r e s s ESCAPE a n d t h e n g i v e t h e f o l l o w i n g c o m m a n d t o e x i t
f r o m vim a n d display the shell p r o m p t :
:q!

When you enter the colon (:), v i m moves the cursor to the bottom line of the screen.
The characters q ! tell v i m to quit without saving your work. (You will not ordinarily
exit from v i m this way because you typically want to save your work.) You must

188

CHAPTER 5

THE LINUX UTILITIES

1

VIM - Vi IMproved
version 7.2.330
by Bram Moolenaar et
.
Vim is open source and freely distributable

Figure 5-17

type

Help poor children in Uganda!
¡help iccf
tor information

type
type
type

:q
¡help or 
¡help version7

to exit
tor on-line help
for version info

Starting vim without a filename

press RETURN after you give this command. Once you get the shell prompt back, refer
to "Specifying a Terminal" on page 1 1 0 6 , and then start v i m 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 - 1 7 ) .
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 - 1 6 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.

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

Last Line mode The colon (:) in the preceding command puts v i m 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 n o n u m b e r RETURN to turn off line numbers.

o

TUTORIAL: USING vim TO CREATE AND EDIT A FILE

Colon (:)

RETURN
Insert,
Append
Open,
Replace,
Change

a
ESCAPE

Figure 5-18

189

Modes in v i m

vim is case When you give v i m a command, remember that the editor is case sensitive. In
sensitive other words, v i m interprets the same letter as two different commands, depending
on whether you enter an uppercase or lowercase character. Beware of the CAPS LOCK
(SHIFTLOCK) key. If you set this key to enter uppercase text while you are in Input
mode and then exit to Command mode, v i m interprets your commands as uppercase letters. It can be confusing when this happens because v i m does not appear to
be executing the commands you are entering.

ENTERING TEXT
i/a (Input mode) When you start v i m , you must put it in Input mode before you can enter text. To put v i m
in Input mode, press the i (insert before cursor) key or the a (append after cursor) key.
If you are not sure whether v i m is in Input mode, press the ESCAPE key; v i m returns to
Command mode if it was in Input mode or beeps, flashes, or does nothing if it is
already in Command mode. You can put v i m back in Input mode by pressing the i
or a key again.
While v i m 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, v i m is not in Input mode.
To continue with this tutorial, enter the sample paragraph shown in Figure 5 - 1 9
(next page), pressing the RETURN key at the end of each line. If you do not press RETURN
before the cursor reaches the right side of the screen or window, v i m wraps the text
so that it appears to start a new line. Physical lines will not correspond to programmatic (logical) lines in this situation, so editing will be more difficult. While you are
using v i m , 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 190). You can correct other mistakes later. When you finish entering the paragraph, press ESCAPE to return v i m to Command mode.

190

CHAPTER 5

THE LINUX UTILITIES

I f you a r e not s u r e w h e t h e r vim i s i n I n p u t mode, p r e s s t h e ESCAPE
k e y ; v i m r e t u r n s t o Command mode i f i t was i n I n p u t mode o r b e e p s ,
flashes* or docs nothing if it is already in Command made. Yon can
put vim back in Input mode by pressing the i or a key again.[

- - INSERT - -

Figure 5-19

Entering text with vim

GETTING HELP
You must have the v i m - r u n t i m e package installed to use vim's help system; see the
tip on page 186.
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 displays a file, such as the two areas shown in Figure 5-20, is a vim "window.") The
h e l p . t x t 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 C0NTR0L-D or
C0NTR0L-U to scroll the cursor down or up half a window at a time. Give the command
:q to close the Help window.
You can display information about the insert commands by giving the command
:help insert while vim is in Command mode (Figure 5-21).

CORRECTING TEXT AS YOU INSERT IT
The keys that back up and correct a shell command line serve the same functions
when vim is in Input mode. These keys include the erase, line kill, and word kill keys
(usually C0NTR0L-H, C0NTR0L-U, and C0NTR0L-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.

TUTORIAL: U S I N G v i m TO CREATE AND EDIT A FILE

[help, txt*

For Vim version 7.2.
VIH

Move around:
Close this window:
Get out of Vim:
Jump to a subject:
With the mouse:
Jump back:
Get specific help:

Use
"J"
Use
Use

191

Last change: 20O8 Jul 21

main help file

h
the cursor keys, or "h" to go left.
to go down, "k" to go up, "I" to go right»
":q".
" :qa! " (careful, all changes are lost!).

k
j

1

Position the cursor on a tag (e.g. |bars|) and hit CTRL-].
":set mouse=a" to enable the mouse (in xtertn or GUI).
Double-click the left mouse button on a tag. e.g. |bars|.
Type CTRL-T or CTRL-0 (repeat to go further back).
It is possible to go directly to whatever you want help
on. by giving an argument to the |:help| command.
It is possible to further specify the context:
^help-context*
WHAT
PREPEND
EXAMPLE

f you are not sure whether vim is in Input mode, press the ESCAPE
key; vim returns to Command mode if it was in Input mode or beeps,
flashes, or does nothing if it is already in Command mode. You can
p u ^ / ^ i ^ a c l ^ ^ ^ n p u ^ i o d ^ b ^ p ^ s s i n g the i or a key again.
'help.txt" I readonly 1 217L, 8055C

Figure 5-20

The main v i m Help screen

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 v i m 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 1 (lowercase "1") keys to move the cursor left, down, up, and right,
respectively.
flinsert* **
Insert text before the cursor [count] times.
When using CTRL 0 in Insert mode |i CTRL-Q| the count
is not supported.
Insert text before the first non-btank in the line
(count] times.
When the 1H" flag is present in 'cpoptions" and the
line only contains blanks, insert start just before
the last blank.

gi

*gl*
Insert text in column 1 [count] times,

{not in Vi}

*gi*
Insert text in the same position as where Insert mode
was stopped last time in the current buffer.
This uses the | , A | mark. It's different from
when the mark is past the end of the line.
insert,txt [Helo RO
It you are not sure whether vim is in Input mode, press the ESCAPE
key; vim returns to Command mode if it was in Input mode or beeps,
flashes, or does nothing if it is already in Command mode. You can
"insert.txt" [readonly] 1879L, 77029C

Figure 5-21

Help with insert commands

192

CHAPTER 5

THE LINUX UTILITIES

DELETING TEXT
x (Delete character) You can delete a single character by moving the cursor until it is over the character
dw (Delete word) y O U w a n t to delete and then giving the command x. You can delete a word by posidd (Delete line) t j o n j n g ^ g c u r s 0 r on the first letter of the word and then giving the command dw
(Delete word). You can delete a line of text by moving the cursor until it is anywhere
on the line and then giving the command dd.

UNDOING MISTAKES
u (Undo) If you delete a character, line, or word by mistake or give any command you want
to reverse, give the command u (Undo) immediately after the command you want to
undo. The vim editor will restore the text to the way it was before you gave the last
command. If you give the u command again, vim will undo the command you gave
before the one it just undid. You can use this technique to back up over many of
your actions. With the compatible parameter (page 193) 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.

ENTERING ADDITIONAL TEXT
i (Insert) When you want to insert new text within existing text, move the cursor so it is on
a (Append) 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
below the line the cursor was on, puts the cursor on the new, empty line, and goes
into Input mode. Enter the new text, ending each line with a RETURN. When you are
finished entering text, press ESCAPE to return vim to Command mode. The O command works in the same way o works, except it opens a blank line above the line
the cursor is on.

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 190,
you might use the ARROW keys to move the cursor until it is on top of the p in pressing. Then give the command dw to delete the word pressing. Put vim in Input mode
by giving an i command, enter the word hitting followed by a SPACE, and press ESCAPE.
The word is changed and vim is in Command mode, waiting for another command.
A shorthand for the two commands dw followed by the i command is cw (Change
word). The command cw puts vim into Input mode.

CHAPTER SUMMARY

193

Page breaks for the printer
tip

CONTROL-Ltells 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 A L does not appear, press
CONTROL-V b e f o r e CONTROL-L.

ENDING THE EDITING S E S S I O N
While you are editing, v i m 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 v i m is in Command mode, and use the Z Z command (you must use
uppercase Zs) to write your newly entered text to the disk and end the editing session. After you give the Z Z command, v i m returns control to the shell. You can exit
with :q! if you do not want to save your work.

Do not confuse zzwith CONTROL-Z
c a u t i o n When you exit from vim with ZZ, make sure that you type ZZ and not COI\ITROL-Z (typically the suspend key). When you press COI\ITROL-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 307. If you try to start editing the same file with a new vim command, vim displays a message about a swap file.

THE compatible PARAMETER
The compatible parameter makes v i m more compatible with vi. By default this
parameter is not set. From the command line use the - C option to set the compatible parameter and use the - N option to unset it. To get started with v i m you can
ignore this parameter.
Setting the compatible parameter changes many aspects of how v i m works. For
example, when the compatible parameter is set, the Undo command (page 192) 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 190 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.

194

CHAPTER 5

THE LINUX UTILITIES

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 163)

diff

Displays the differences between two files (page 168)

file

Displays information about the contents of a file (page 170)

grep

Searches file(s) for a string (page 166)

head

Displays the lines at the beginning of a file (page 166)

Ipq

Displays a list of jobs in the print queue (page 165)

Ipr

Places file(s) in the print queue (page 165)

Iprm

Removes a job from the print queue (page 165)

mv

Renames a file or moves file(s) to another directory (page 164)

sort

Puts a file in order by lines (page 168)

tail

Displays the lines at the end of a file (page 167)

uniq

Displays the contents of a file, skipping adjacent duplicate lines (page 168)

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 b z i p 2 — b u n z i p 2 —
restores a file to its original, decompressed form. Table 5-3 lists utilities that compress
and decompress files. The bzip2 utility is the most efficient of these.
Table 5 - 3

(De)compression utilities

Utility

Function

bunzip2

Returns a file compressed with b z i p 2 to its original size and format
(page 175)

bzcat

Displays a file compressed with b z i p 2 (page 175)

bzip2

Compresses a file (page 174)

compress

Compresses a file (not as well as b z i p 2 or g z i p ; page 176)

gunzip

Returns a file compressed with g z i p or c o m p r e s s to its original size and format (page 175)

gzip

Compresses a file (not as well as b z i p 2 ; page 175)

zcat

Displays a file compressed with g z i p (page 175)

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 . t a r . b z 2 ,

CHAPTER SUMMARY

195

.tar.gz, and .tgz identify compressed tar archive files and are often seen on software
packages obtained over the Internet.
Table 5 - 4

Archive utility

Utility

Function

tar

Creates or extracts files from an archive file (page 176)

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

Location utilities

Utility

Function

m locate

Searches for files on the local system (page 180)

whereis

Displays the f iill pathnames of a utility, source code, or m a n page
(page 178)

which

Displays the f iill pathname of a command you can run (page 178)

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 - 6

User and system information utilities

Utility

Function

finger

Displays detailed information about users, including their full names
(page 181)

hostname

Displays the name of the local system (page 163)

w

Displays detailed information about users who are logged in on the local
system (page 183)

who

Displays information about users who are logged in on the local system
(page 180)

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

User communication utilities

Utility

Function

mesg

Permits or denies messages sent by w r i t e (page 185)

write

Sends a message to another user who is logged in (page 184)

196

CHAPTER 5

THE LINUX UTILITIES

Table 5-8 lists miscellaneous utilities.
Table 5 - 8

Miscellaneous utilities

Utility

Function

date

Displays the current date and time (page 172)

echo

Copies its arguments

vim

Edits text (page 186)

(page 1135) to the screen (page 171 )

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

4. How can you find out which utilities are available on your system for editing files? Which utilities are available for editing on your system?
5. How can you find the phone number for Ace Electronics in a file named
phone that contains a list of names and phone numbers? Which command
can you use to display the entire file in alphabetical order? How can you
display the file without any adjacent duplicate lines? How can you display
the file without any duplicate lines?
6. What happens when you use diff to compare two binary files that are not
identical? (You can use gzip to create the binary files.) Explain why the diff
output for binary files is different from the diff output for ASCII files.
7. Create a .plan file in your home directory. Does finger display the contents
of your .plan file?
8. What is the result of giving the which utility the name of a command that
resides in a directory that is not in your search path?
9. Are any of the utilities discussed in this chapter located in more than one
directory on the local system? If so, which ones?
10. Experiment by calling the file utility with the names of files in /usr/bin.
How many different types of files are there?

ADVANCED EXERCISES

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 169.
Test your files by running diff - u on them. Do you get the same results as
in the figure?
13. Try giving these two commands:
$ echo cat
$ cat echo

Explain the differences between the output of each command.
14. Repeat exercise 5 using the file phone.gz, a compressed version of the list
of names and phone numbers. Consider more than one approach to
answer each question, and explain how you made your choices.
15. Find 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 Is -1 to determine the sizes of the files in question. Can you characterize the files in a, b, and c?
16. Older email programs were not able to handle binary files. Suppose 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 519]; it is part
of the sharutils package.)
b. Would it ever make sense to use uuencode on a file before compressing
it? Explain.

197

This page intentionally left blank

6
THE LINUX FILESYSTEM

The Hierarchical Filesystem

200

A filesystem
is a set of data structures
(page 1 1 4 4 ) that usually
resides on part of a disk and that holds directories of files. Filesysterns store user and system data that are the basis of users' work

Directory Files and Ordinary
Files

200

on the system and the system's existence. This chapter discusses
the organization and terminology of the Linux filesystem, defines

The Working Directory

204

Your Home Directory
Pathnames

204
205

Relative Pathnames
... ..
, .
Working with Directories

206

Access Permissions

215

important files and directories as well as file access permissions
and Access Control Lists (ACLs), which allow you to share
,
....
• ,
,
, ,
•,
,.
•
T
r
selected files with other users. It concludes with a discussion of
hard and symbolic links, which can make a single file appear in

ACLs: Access Control Lists

221

more than one directory.

Hard Links

228

In addition to reading this chapter, you may want to refer to the

Symbolic Links

230

^f
P a 8 e a n d to the fsck, mkfs, and tune2fs man pages for
more information on filesystems.

IN T H I S C H A P T E R

207

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

199

200

CHAPTER 6

THE LINUX FILESYSTEM

Figure 6-1

A f a m i l y tree

THE HIERARCHICAL FILESYSTEM
Family tree

A hierarchical

structure (page 1151) frequently takes the shape of a p y r a m i d .

One

e x a m p l e o f t h i s t y p e o f s t r u c t u r e is f o u n d b y t r a c i n g a f a m i l y ' s l i n e a g e : A c o u p l e h a s
a child, w h o m a y i n t u r n have several children, each o f w h o m m a y have m o r e child r e n . T h i s h i e r a r c h i c a l s t r u c t u r e is c a l l e d a family
Directory tree

tree ( F i g u r e 6 - 1 ) .

L i k e t h e f a m i l y t r e e i t r e s e m b l e s , t h e L i n u x f i l e s y s t e m i s c a l l e d a tree.

It consists of a

set o f c o n n e c t e d f i l e s . T h i s s t r u c t u r e a l l o w s y o u t o o r g a n i z e f i l e s s o y o u c a n e a s i l y
f i n d a n y p a r t i c u l a r one. O n a s t a n d a r d L i n u x system, each user starts w i t h

one

directory, t o w h i c h the user c a n a d d subdirectories t o a n y desired level. B y c r e a t i n g
m u l t i p l e l e v e l s o f s u b d i r e c t o r i e s , a u s e r c a n e x p a n d t h e s t r u c t u r e as n e e d e d .
Subdirectories

T y p i c a l l y e a c h s u b d i r e c t o r y i s d e d i c a t e d t o a s i n g l e s u b j e c t , s u c h as a p e r s o n , p r o j e c t ,
or event. T h e subject dictates w h e t h e r a subdirectory s h o u l d be subdivided further.
F o r e x a m p l e , F i g u r e 6 - 2 s h o w s a s e c r e t a r y ' s s u b d i r e c t o r y n a m e d correspond.

This

d i r e c t o r y c o n t a i n s t h r e e s u b d i r e c t o r i e s : business, m e m o s , a n d personal. T h e business
d i r e c t o r y c o n t a i n s files t h a t store e a c h letter t h e secretary types. I f y o u e x p e c t m a n y
l e t t e r s t o g o t o o n e c l i e n t , as is t h e c a s e w i t h m i l k _ c o , y o u c a n d e d i c a t e a s u b d i r e c t o r y to that client.
O n e m a j o r s t r e n g t h o f t h e L i n u x f i l e s y s t e m is its a b i l i t y t o a d a p t t o u s e r s ' n e e d s .
Y o u c a n t a k e a d v a n t a g e o f this s t r e n g t h b y s t r a t e g i c a l l y o r g a n i z i n g y o u r files so t h e y
are m o s t convenient a n d useful for y o u .

D I R E C T O R Y FILES A N D O R D I N A R Y FILES
L i k e a f a m i l y t r e e , t h e t r e e r e p r e s e n t i n g t h e f i l e s y s t e m is u s u a l l y p i c t u r e d
d o w n , w i t h i t s root

at the t o p . Figures 6 - 2 a n d 6 - 3 s h o w t h a t the tree

upside

"grows"

DIRECTORY FILES AND ORDINARY FILES

Figure 6-2

201

A secretary's directories

d o w n w a r d f r o m t h e r o o t , w i t h p a t h s c o n n e c t i n g t h e r o o t t o e a c h o f t h e o t h e r files. A t
t h e e n d o f e a c h p a t h is e i t h e r a n o r d i n a r y

file o r a d i r e c t o r y file. Special

w h i c h c a n also appear at the ends o f paths, are described o n page 5 0 1 .
files,

o r s i m p l y files,

Directory

files,

appear at the ends of paths that c a n n o t support other

a l s o r e f e r r e d t o as directories

paths can branch off from.

o r folders,

(Figures 6-2 and 6-3 s h o w some e m p t y

D i r e c t o r i e s d i r e c t l y c o n n e c t e d b y a p a t h a r e c a l l e d parents
( f a r t h e r f r o m t h e r o o t ) . A pathname

paths.

are the p o i n t s t h a t other

W h e n y o u r e f e r t o t h e t r e e , up i s t o w a r d t h e r o o t a n d down
children

files,

Ordinary

directories.)

is a w a y f r o m t h e r o o t .
(closer to the r o o t )

and

is a series o f n a m e s t h a t t r a c e a p a t h

a l o n g b r a n c h e s f r o m o n e f i l e t o a n o t h e r . See p a g e 2 0 5 f o r m o r e i n f o r m a t i o n

about

pathnames.

FILENAMES
E v e r y f i l e h a s a filename.

T h e m a x i m u m length of a filename varies w i t h the type of

filesystem; L i n u x

supports several types of filesystems. A l t h o u g h m o s t of

filesystems

files w i t h

allow

Figure 6-3

names

up

to 255

characters

D i r e c t o r i e s a n d o r d i n a r y files

long,

some

today's

filesystems

202

CHAPTER 6

THE LINUX FILESYSTEM
restrict filenames t o f e w e r characters. W h i l e y o u c a n use a l m o s t a n y c h a r a c t e r i n a
f i l e n a m e , y o u w i l l a v o i d c o n f u s i o n i f y o u c h o o s e characters f r o m the f o l l o w i n g list:
• U p p e r c a s e letters ( A - Z )
• L o w e r c a s e letters (a-z)
• Numbers

(0-9)

• Underscore (_)
• P e r i o d (.)
• C o m m a (,)
L i k e t h e c h i l d r e n o f o n e p a r e n t , n o t w o files i n t h e s a m e d i r e c t o r y c a n h a v e t h e s a m e
n a m e . (Parents g i v e t h e i r c h i l d r e n d i f f e r e n t n a m e s because it m a k e s g o o d sense, b u t
L i n u x requires it.) Files i n d i f f e r e n t directories, like the c h i l d r e n o f d i f f e r e n t parents,
can have the same name.
T h e f i l e n a m e s y o u c h o o s e s h o u l d m e a n s o m e t h i n g . T o o o f t e n a d i r e c t o r y is f i l l e d
w i t h i m p o r t a n t f i l e s w i t h s u c h u n h e l p f u l n a m e s as h o l d l , w o m b a t , a n d junk, n o t t o
m e n t i o n f o o a n d foobar. S u c h n a m e s a r e p o o r c h o i c e s b e c a u s e t h e y d o n o t h e l p y o u
recall w h a t y o u s t o r e d i n a file. T h e f o l l o w i n g filenames c o n f o r m t o the suggested
s y n t a x and c o n v e y i n f o r m a t i o n a b o u t t h e c o n t e n t s o f t h e f i l e :
• correspond
• january
• davis
• reports

• 2001
• acct_payable
Filename length

W h e n y o u share y o u r files w i t h users o n o t h e r systems, y o u m a y n e e d t o m a k e l o n g
filenames differ w i t h i n the first f e w characters. Systems r u n n i n g D O S or older versions of W i n d o w s have an 8-character filename b o d y length l i m i t a n d a 3-character
filename extension length limit. Some U N I X systems have a 14-character l i m i t a n d
older M a c i n t o s h systems have a 31-character limit. If y o u keep the filenames short,
t h e y are easy t o type; later y o u c a n a d d extensions t o t h e m w i t h o u t e x c e e d i n g the
s h o r t e r l i m i t s i m p o s e d b y s o m e f i l e s y s t e m s . T h e d i s a d v a n t a g e o f s h o r t f i l e n a m e s is
t h a t t h e y a r e t y p i c a l l y l e s s d e s c r i p t i v e t h a n l o n g f i l e n a m e s . See s t a t o n p a g e 4 5 9 f o r a
w a y to determine the m a x i m u m length of a filename o n the local system.
L o n g f i l e n a m e s e n a b l e y o u t o assign d e s c r i p t i v e n a m e s t o files. T o h e l p y o u select
a m o n g files w i t h o u t t y p i n g entire f i l e n a m e s , shells s u p p o r t f i l e n a m e c o m p l e t i o n . F o r
m o r e i n f o r m a t i o n a b o u t t h i s f e a t u r e , see t h e " F i l e n a m e c o m p l e t i o n " t i p o n p a g e 1 6 3 .

Case sensitivity

Y o u c a n use u p p e r c a s e a n d / o r l o w e r c a s e l e t t e r s w i t h i n f i l e n a m e s . L i n u x is case sens i t i v e , so files n a m e d J A N U A R Y , January, a n d january r e p r e s e n t t h r e e d i s t i n c t files.

DIRECTORY FILES AND ORDINARY FILES

203

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 character, you must quote It on a command line. Quoting a character on a command line can be difficult
for a novice user and cumbersome for an experienced user. Use periods or underscores Instead of
SPACES: joe.05.04.26, new_stuff.
If you are working with a filename that Includes a SPACE, such as a file from another operating system, you must quote the SPACE on the command line by preceding It with a backslash or by placing
quotation marks on either side of the filename. The two following commands send the file named
my file to the printer.
$ lpr my\ file
$ lpr "my file"

FILENAME EXTENSIONS
A filename

extension

is t h e p a r t o f t h e f i l e n a m e f o l l o w i n g a n e m b e d d e d p e r i o d . I n t h e

filenames listed i n T a b l e 6 - 1 , f i l e n a m e extensions h e l p describe the c o n t e n t s o f the file.
S o m e p r o g r a m s , s u c h as t h e C p r o g r a m m i n g l a n g u a g e c o m p i l e r , d e f a u l t t o

specific

f i l e n a m e e x t e n s i o n s ; i n m o s t cases, h o w e v e r , f i l e n a m e e x t e n s i o n s are o p t i o n a l . U s e
e x t e n s i o n s f r e e l y t o m a k e f i l e n a m e s easy t o u n d e r s t a n d . I f y o u l i k e , y o u c a n use seve r a l p e r i o d s w i t h i n t h e s a m e f i l e n a m e — f o r e x a m p l e , n o t e s . 4 . 1 0 . 0 1 o r files.tar.gz.

Table 6-1

Filename extensions

Filename with extension

Meaning of extension

compute.c

A C programming language source file

compute.o

The object code file for compute.c

compute

The executable file for compute.c

memo.0410.txt

A text file

memo.pdf

A PDF file; view with

memo.ps

A PostScript file; view with gs or kpdf under a GUI

memo.Z

A file compressed with compress (page 176); use
uncompress or gunzip (page 175) to decompress

memo.tgz or memo.tar.gz

A tar (page 176) archive of files compressed with gzip (page 175)

memo.gz

A file compressed with gzip (page 175); view with zcat or
decompress with gunzip (both on page 175)

memo.bz2

A file compressed with bzip2 (page 174); view with bzcat or
decompress with bunzip2 (both on page 175)

memo.html

A file meant to be viewed using a Web browser, such as Flrefox

photo.gif, photo.jpg,
photo.jpeg, photo.bmp,
photo.tif, or photo.tiff

A file containing graphical Information, such as a picture

xpdf

or kpdf under a GUI

204

CHAPTER 6

THE LINUX FILESYSTEM

login:
max
Password:
Last login:
$ pwd
/home/max

Figure 6-4

Wed O c t

20 1 1 : 1 4 : 2 1 f r o m

bravo

Logging in and displaying the pathname of your h o m e

directory

HIDDEN FILENAMES
A f i l e n a m e t h a t b e g i n s w i t h a p e r i o d i s c a l l e d a hidden
s o m e t i m e s a n invisible

file)

filename

( o r a hidden

file

or

b e c a u s e Is d o e s n o t n o r m a l l y d i s p l a y i t . T h e c o m m a n d Is

- a d i s p l a y s all f i l e n a m e s , e v e n h i d d e n o n e s . N a m e s o f s t a r t u p f i l e s ( f o l l o w i n g )

usu-

ally b e g i n w i t h a p e r i o d so t h a t t h e y are h i d d e n a n d d o n o t c l u t t e r a d i r e c t o r y listing.
T h e .plan f i l e ( p a g e 1 8 2 ) is a l s o h i d d e n . T w o s p e c i a l h i d d e n e n t r i e s — a s i n g l e

and

d o u b l e p e r i o d (. a n d . . ) — a p p e a r i n e v e r y d i r e c t o r y ( p a g e 2 1 0 ) .

THE WORKING DIRECTORY
pwd

W h i l e y o u are logged i n o n a character-based interface to a L i n u x system, y o u are
a l w a y s a s s o c i a t e d w i t h a d i r e c t o r y . T h e d i r e c t o r y y o u a r e a s s o c i a t e d w i t h is c a l l e d
t h e working

directory

o r current

directory.

i n a p h y s i c a l s e n s e : " Y o u a r e in ( o r working

S o m e t i m e s t h i s a s s o c i a t i o n is r e f e r r e d t o
in) t h e z a c h d i r e c t o r y . " T h e pwd ( p r i n t

w o r k i n g directory) builtin displays the p a t h n a m e of the w o r k i n g directory.

YOUR HOME DIRECTORY
W h e n y o u first log i n o n a L i n u x system or start a t e r m i n a l e m u l a t o r w i n d o w ,
w o r k i n g d i r e c t o r y i s y o u r home

directory.

To display the pathname of your

the

home

d i r e c t o r y , u s e pwd j u s t a f t e r y o u l o g i n ( F i g u r e 6 - 4 ) .
W h e n u s e d w i t h o u t a n y a r g u m e n t s , t h e Is u t i l i t y d i s p l a y s a l i s t o f t h e f i l e s i n

the

w o r k i n g directory. Because y o u r h o m e directory has been the only w o r k i n g directory
y o u h a v e u s e d s o f a r , Is h a s a l w a y s d i s p l a y e d a l i s t o f f i l e s i n y o u r h o m e

directory.

( A l l t h e files y o u h a v e c r e a t e d u p t o this p o i n t w e r e created i n y o u r h o m e d i r e c t o r y . )

STARTUP FILES
Startup

files,

w h i c h appear i n y o u r h o m e directory, give the shell a n d other

pro-

g r a m s i n f o r m a t i o n a b o u t y o u a n d y o u r preferences. F r e q u e n t l y o n e o f these files
t e l l s t h e s h e l l w h a t k i n d o f t e r m i n a l y o u a r e u s i n g ( p a g e 1 1 0 6 ) a n d e x e c u t e s t h e stty
(set t e r m i n a l ) u t i l i t y t o e s t a b l i s h t h e erase ( p a g e 1 5 1 ) a n d l i n e k i l l ( p a g e 1 5 1 ) k e y s .
Either y o u o r the system a d m i n i s t r a t o r c a n p u t a shell s t a r t u p file c o n t a i n i n g shell
c o m m a n d s i n y o u r h o m e directory. T h e shell executes the c o m m a n d s i n this
e a c h t i m e y o u l o g in. B e c a u s e t h e s t a r t u p files h a v e h i d d e n f i l e n a m e s , y o u

file
must

u s e t h e Is - a c o m m a n d t o s e e w h e t h e r o n e i s i n y o u r h o m e d i r e c t o r y . A G U I

has

m a n y s t a r t u p files. U s u a l l y y o u d o n o t n e e d t o w o r k w i t h these files d i r e c t l y

but

c a n c o n t r o l s t a r t u p s e q u e n c e s u s i n g i c o n s o n t h e d e s k t o p . See p a g e 2 9 3 f o r
i n f o r m a t i o n a b o u t s t a r t u p files.

more

PATHNAMES

Figure 6-5

205

Absolute pathnames

PATHNAMES
E v e r y f i l e h a s a pathname,

w h i c h is a t r a i l f r o m a d i r e c t o r y t h r o u g h p a r t o f t h e

d i r e c t o r y h i e r a r c h y to a n o r d i n a r y file or a directory. W i t h i n a p a t h n a m e , a slash (!)
f o l l o w i n g ( t o t h e r i g h t o f ) a f i l e n a m e i n d i c a t e s t h a t t h e f i l e is a d i r e c t o r y f i l e . T h e f i l e
f o l l o w i n g (to t h e r i g h t of) t h e slash c a n be a n o r d i n a r y file o r a d i r e c t o r y file. T h e
s i m p l e s t p a t h n a m e is a s i m p l e f i l e n a m e , w h i c h p o i n t s t o a f i l e i n t h e w o r k i n g d i r e c tory. T h i s section discusses a b s o l u t e a n d relative p a t h n a m e s a n d e x p l a i n s h o w

to

use each.

ABSOLUTE PATHNAMES
/ (root)

T h e r o o t d i r e c t o r y o f t h e f i l e s y s t e m h i e r a r c h y d o e s n o t h a v e a n a m e ; i t is r e f e r r e d t o
as t h e root

directory

a n d is r e p r e s e n t e d b y a / ( s l a s h ) s t a n d i n g a l o n e o r a t t h e l e f t

end of a pathname.
A n absolute

pathname

starts w i t h a slash (/), w h i c h represents the r o o t directory.

T h e s l a s h is f o l l o w e d b y t h e n a m e o f a f i l e l o c a t e d i n t h e r o o t d i r e c t o r y .
absolute p a t h n a m e can continue, tracing a p a t h t h r o u g h all intermediate

An

direc-

tories, t o the file identified b y the p a t h n a m e . String all the filenames i n the p a t h
together, f o l l o w i n g each directory w i t h a slash (/). This string of filenames

is

called a n absolute p a t h n a m e because it locates a file absolutely b y t r a c i n g a p a t h
f r o m the r o o t d i r e c t o r y t o the file. T y p i c a l l y the absolute p a t h n a m e o f a direct o r y does n o t i n c l u d e the t r a i l i n g slash, a l t h o u g h that f o r m a t m a y be used
e m p h a s i z e t h a t t h e p a t h n a m e specifies a d i r e c t o r y (e.g., /home/zach/). T h e
o f a p a t h n a m e f o l l o w i n g t h e f i n a l s l a s h i s c a l l e d a simple
basename.

filename,

filename,

Figure 6-5 shows the absolute pathnames of directories and

files i n p a r t o f a f i l e s y s t e m h i e r a r c h y .

to

part
or

ordinary

206

CHAPTER 6

THE LINUX FILESYSTEM
U s i n g a na b s o l u t e p a t h n a m e , y o u c a n list o r o t h e r w i s e w o r k w i t h a n y file o n the
l o c a l system, a s s u m i n g y o u h a v e p e r m i s s i o n t o d o so, regardless o f the

working

directory a tthe t i m e y o u give the c o m m a n d . For example, S a m c a n give the

follow-

i n g c o m m a n d w h i l e w o r k i n g i n his h o m e d i r e c t o r y t o list t h e files i n t h e /etc/apt
directory:
$ pwd
/home/sam
$ Is /etc/apt
apt.conf.d
preferences.d

secring.gpg
sources.list

sources.list.d
trustdb.gpg

trusted.gpg
trusted.gpg. d
trusted.gpg-

- (TILDE) IN PATHNAMES
I n another f o r m o f absolute p a t h n a m e , the shell expands the characters - / (a tilde
f o l l o w e d b y a slash) a t the start o f a p a t h n a m e i n t o the p a t h n a m e o f y o u r

home

d i r e c t o r y . U s i n g t h i s s h o r t c u t , y o u c a n d i s p l a y y o u r .bashrc s t a r t u p file ( p a g e 2 9 4 )
w i t h the f o l l o w i n g c o m m a n d , n o matter w h i c h directory isthe w o r k i n g
$ less

directory:

bashrc

A t i l d e q u i c k l y r e f e r e n c e s p a t h s t h a t s t a r t w i t h y o u r o r s o m e o n e else's h o m e

direc-

tory. T h e shell e x p a n d s a tilde f o l l o w e d b y a u s e r n a m e a t the b e g i n n i n g o f a p a t h n a m e i n t o the p a t h n a m e o f t h a t user's h o m e directory. F o r e x a m p l e , a s s u m i n g h e
has p e r m i s s i o n t o d o so, M a x

c a n e x a m i n e S a m ' s .bashrc file w i t h t h e

following

command:
$ less ~sam/.bashrc
Refer t o "Tilde Expansion" on page 359 for more

information.

RELATIVE PATHNAMES
A relative

pathname

n a m e i s relative

traces a p a t h f r o m t h e w o r k i n g d i r e c t o r y t o a file. T h e

path-

t othe w o r k i n g directory. A n y p a t h n a m e that does not begin w i t h the

r o o t d i r e c t o r y (represented b y/ ) o r a tilde (~) is a relative p a t h n a m e . L i k e

absolute

pathnames, relative pathnames can trace a p a t h t h r o u g h m a n y directories. T h e
plest relative p a t h n a m e is a s i m p l e filename, w h i c h identifies a file i n the

sim-

working

directory. T h e e x a m p l e s i nthe n e x t sections use a b s o l u t e a n d relative p a t h n a m e s .

SIGNIFICANCE OF THE WORKING DIRECTORY
T o access a n y file i n t h e w o r k i n g d i r e c t o r y , y o u n e e d o n l y a s i m p l e f i l e n a m e . T o
a c c e s s a f i l e i n a n o t h e r d i r e c t o r y , y o u must

use a p a t h n a m e . T y p i n g a l o n g p a t h n a m e

is t e d i o u s a n d i n c r e a s e s t h e c h a n c e o f m a k i n g a m i s t a k e . T h i s p o s s i b i l i t y is less l i k e l y
under a G U I , w h e r e y o u click filenames o r icons. Y o u can choose a w o r k i n g

direc-

tory for any particular task t oreduce the need for long pathnames. Your choice o f a
working

directory

does

n o t allow

y o u t o d o anything

o t h e r w i s e — i t j u s t m a k e s s o m e o p e r a t i o n s easier.

y o u could

n o td o

WORKING WITH DIRECTORIES

207

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 creating a file using
vim and you are not where you think you are in the file hierarchy, the new file will end up in an
unexpected location.
It does not matter which directory is the working directory when you use an absolute pathname.
Thus, the following command always edits a file named goals in your home directory:
$ vim.tiny ~/goals
R e f e r t o F i g u r e 6 - 6 as y o u r e a d t h i s p a r a g r a p h . F i l e s t h a t a r e c h i l d r e n o f t h e
ing directory can be referenced by simple filenames. Grandchildren of the

work-

working

directory can be referenced b y short relative pathnames: t w o filenames separated b y
a slash. W h e n y o u m a n i p u l a t e files i n a large d i r e c t o r y s t r u c t u r e , u s i n g s h o r t r e l a t i v e
p a t h n a m e s c a n save y o u t i m e a n d a g g r a v a t i o n . If y o u choose a w o r k i n g

directory

t h a t c o n t a i n s t h e files used m o s t o f t e n f o r a p a r t i c u l a r task, y o u n e e d use
long, cumbersome

WORKING WITH

fewer

pathnames.

DIRECTORIES

T h i s section discusses h o w t o create directories (mkdir), s w i t c h b e t w e e n
(cd), r e m o v e

directories

(rmdir), use p a t h n a m e s

to make

your

work

directories
easier,

and

m o v e a n d c o p y files a n d d i r e c t o r i e s b e t w e e n d i r e c t o r i e s . I t c o n c l u d e s w i t h a sect i o n t h a t lists a n d describes b r i e f l y i m p o r t a n t s t a n d a r d directories a n d files i n t h e
Ubuntu

filesystem.

Figure 6-6

Relative

pathnames

208

CHAPTER 6

THE LINUX FILESYSTEM

promo

Figure 6-7

T h e file structure developed i n the examples

mkdir: CREATES A DIRECTORY
T h e mkdir u t i l i t y c r e a t e s a d i r e c t o r y . T h e argument

( p a g e 1 1 3 5 ) t o mkdir b e c o m e s t h e

p a t h n a m e of the n e w directory. T h e f o l l o w i n g examples develop the directory structure s h o w n i n Figure 6-7. I n the figure, the directories that are a d d e d appear i na
lighter shade t h a n the others a n d are c o n n e c t e d b y dashes.
I n F i g u r e 6 - 8 , pwd s h o w s t h a t M a x is w o r k i n g i n h i s h o m e d i r e c t o r y ( / h o m e / m a x )
a n d Is s h o w s t h e n a m e s o f t h e f i l e s i n h i s h o m e d i r e c t o r y : d e m o , n a m e s , a n d temp.
U s i n g mkdir, M a x c r e a t e s a d i r e c t o r y n a m e d literature as a c h i l d o f h i s h o m e d i r e c t o r y . H e u s e s a r e l a t i v e p a t h n a m e (a s i m p l e f i l e n a m e ) b e c a u s e h e w a n t s t h e literature
d i r e c t o r y t o be a child o f the w o r k i n g directory. M a x c o u l d have used a n absolute
p a t h n a m e t o c r e a t e t h e s a m e d i r e c t o r y : mkdir / h o m e / m a x / l i t e r a t u r e .
T h e s e c o n d Is i n F i g u r e 6 - 8 v e r i f i e s t h e p r e s e n c e o f t h e n e w d i r e c t o r y . T h e - F o p t i o n
t o Is d i s p l a y s a s l a s h a f t e r t h e n a m e o f e a c h d i r e c t o r y a n d a n a s t e r i s k a f t e r e a c h e x e cutable file (shell script, utility, o r application). W h e n y o u call i t w i t h a n a r g u m e n t
$ pwd
/home/max
$ Is
demo names
temp
$ mkdir l i t e r a t u r e
$ Is
demo literature
names temp
$ I s -F
demo literature/
names temp
$ Is l i t e r a t u r e
S

Figure 6-8

T h e mkdir u t i l i t y

W O R K I N G WITH D I R E C T O R I E S 2 0 9
t h a t i s t h e n a m e o f a d i r e c t o r y , Is l i s t s t h e c o n t e n t s o f t h a t d i r e c t o r y . T h e f i n a l Is d o e s
n o t d i s p l a y a n y t h i n g b e c a u s e t h e r e a r e n o f i l e s i n t h e literature d i r e c t o r y .
T h e f o l l o w i n g c o m m a n d s s h o w t w o w a y s t o c r e a t e t h e p r o m o d i r e c t o r y as a c h i l d o f
t h e n e w l y c r e a t e d literature d i r e c t o r y . T h e f i r s t w a y c h e c k s t h a t / h o m e / m a x i s t h e
w o r k i n g d i r e c t o r y a n d uses a r e l a t i v e

pathname:

$ pwd
/home/max
$ mkdir l i t e r a t u r e / p r o m o
T h e s e c o n d w a y uses a n a b s o l u t e
$ mkdir

pathname:

/home/max/literature/promo

U s e t h e - p ( p a r e n t s ) o p t i o n t o m k d i r t o c r e a t e b o t h t h e literature a n d p r o m o d i r e c t o ries w i t h o n e

command:

$ pwd
/home/max
$ I s
demo
names
temp
$ mkdir -p l i t e r a t u r e / p r o m o

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

cd: CHANGES TO ANOTHER WORKING DIRECTORY
T h e cd (change directory) utility m a k e s another directory the w o r k i n g directory
d o e s not c h a n g e t h e c o n t e n t s o f t h e w o r k i n g d i r e c t o r y . F i g u r e 6 - 9 s h o w s t w o

but
ways

t o m a k e the /home/max/literature d i r e c t o r y the w o r k i n g directory, as verified b y
pwd. First M a x

uses c d w i t h a n a b s o l u t e p a t h n a m e t o m a k e literature h i s

working

d i r e c t o r y — i t does n o t m a t t e r w h i c h is the w o r k i n g d i r e c t o r y w h e n y o u give a c o m m a n d w i t h an absolute

pathname.

A pwd c o m m a n d confirms the change m a d e b yM a x . W h e n used w i t h o u t a n argument, cdmakes your home

directory the w o r k i n g

directory, as i t was w h e n

you

logged in. T h e second c dc o m m a n d i n Figure 6-9 does n o t have a na r g u m e n t so i t

$ cd / h o m e / m a x / l i t e r a t u r e
$ pwd
/home/max/1i terature
$ cd
$ pwd
/home/max
$ cd l i t e r a t u r e
$ pwd
/home/max/1i terature

Figure 6-9

cd changes the w o r k i n g

directory

210

CHAPTER 6

THE LINUX FILESYSTEM
makes M a x ' s h o m e directory the w o r k i n g directory. Finally, k n o w i n g that h e is
w o r k i n g i n h i s h o m e d i r e c t o r y , M a x u s e s a s i m p l e f i l e n a m e t o m a k e t h e literature
d i r e c t o r y h i s w o r k i n g d i r e c t o r y (cd literature) a n d c o n f i r m s t h e c h a n g e u s i n g pwd.

The working directory versus your home directory
tip The working directory is not the same as your home directory. Your home directory remains the
same for the duration of your session and usually from session to session. Immediately after you
log in, you are always working in the same directory: your home directory.
Unlike your home directory, the working directory can change as often as you like. You have no
set working directory, which explains why some people refer to it as the current directory. When
you log in and until you change directories using cd, your home directory is the working directory.
If you were to change directories to Sam's home directory, then Sam's home directory would be
the working directory.

THE . AND .. DIRECTORY ENTRIES
T h e mkdir u t i l i t y a u t o m a t i c a l l y p u t s t w o e n t r i e s i n e a c h d i r e c t o r y i t c r e a t e s : a s i n g l e
p e r i o d (.) a n d a d o u b l e p e r i o d (..). T h e . is s y n o n y m o u s w i t h t h e p a t h n a m e o f t h e
w o r k i n g d i r e c t o r y a n d c a n be used i n its p l a c e ; t h e . . is s y n o n y m o u s w i t h t h e p a t h n a m e of the p a r e n t of the w o r k i n g directory. These entries are h i d d e n because their
filenames begin w i t h a period.
W i t h t h e literature d i r e c t o r y as t h e w o r k i n g d i r e c t o r y , t h e f o l l o w i n g e x a m p l e u s e s . .
three times: first t o list the contents o fthe p a r e n t directory (/home/max), second t o
c o p y the memoA file t o the p a r e n t directory, a n d t h i r d t o list the contents o f the
parent directory again.
$ pwd
/home/max/1i t e r a t u r e
$ I s ..
demo l i t e r a t u r e
names
$ cp memoA . .
$ I s ..
demo l i t e r a t u r e
memoA

temp

names

temp

A f t e r u s i n g cd t o m a k e p r o m o ( a s u b d i r e c t o r y o f literature) h i s w o r k i n g

directory,

M a x c a n u s e a r e l a t i v e p a t h n a m e t o c a l l vim t o e d i t a f i l e i n h i s h o m e d i r e c t o r y .
$ cd promo
$ v i m . t i n y ../../names
Y o u c a n use a n a b s o l u t e o r r e l a t i v e p a t h n a m e o r a s i m p l e f i l e n a m e v i r t u a l l y

any-

w h e r e a utility o r p r o g r a m requires a filename o r p a t h n a m e . This usage holds true
f o r Is, vim, mkdir, rm, a n d m o s t o t h e r L i n u x u t i l i t i e s .

rmdir: DELETES A DIRECTORY
T h e rmdir ( r e m o v e d i r e c t o r y ) u t i l i t y d e l e t e s a d i r e c t o r y . Y o u c a n n o t d e l e t e t h e w o r k i n g d i r e c t o r y o r a d i r e c t o r y t h a t c o n t a i n s files o t h e r t h a n t h e . a n d . . entries. I f y o u

W O R K I N G WITH D I R E C T O R I E S 2 1 1
n e e d t o d e l e t e a d i r e c t o r y t h a t h a s f i l e s i n i t , f i r s t u s e rm t o d e l e t e t h e f i l e s a n d

then

delete the directory. Y o u d o n o t have t o (nor c a n y o u ) delete the . a n d . . entries;
rmdir r e m o v e s

them

automatically.

The

following

command

deletes

t h e promo

directory:
$ rmdir

/home/max/literature/promo

T h e r m u t i l i t y h a s a - r o p t i o n ( r m - r filename)

t h a t r e c u r s i v e l y deletes files, i n c l u d -

i n g directories, w i t h i n a d i r e c t o r y a n d also deletes the d i r e c t o r y itself.

Use rm - r carefully, if at all
caution Although rm -r Is a handy command, you must use It carefully. Do not use It with an ambiguous
file reference such as *. It Is frlghtenlngly easy to wipe out your entire home directory with a single
short command.

USING PATHNAMES
touch

U s e a t e x t e d i t o r t o c r e a t e a f i l e n a m e d letter i f y o u w a n t t o e x p e r i m e n t w i t h

the

e x a m p l e s t h a t f o l l o w . A l t e r n a t i v e l y y o u c a n u s e touch t o c r e a t e a n e m p t y f i l e :
$ cd
$ pwd
/home/max
$ touch l e t t e r
W i t h / h o m e / m a x a s t h e w o r k i n g d i r e c t o r y , t h e f o l l o w i n g e x a m p l e u s e s cp w i t h a
r e l a t i v e p a t h n a m e t o c o p y t h e f i l e letter t o t h e / h o m e / m a x / l i t e r a t u r e / p r o m o d i r e c tory. ( Y o u w i l l need t o create promo a g a i n i f y o u deleted i t earlier.) T h e c o p y o f the
file h a s t h e s i m p l e f i l e n a m e letter.0610:
$ cp l e t t e r
If M a x

literature/promo/letter.0610

does n o t c h a n g e t o a n o t h e r d i r e c t o r y , h ec a n use vim as s h o w n t o edit

the

c o p y o fthe file he just m a d e :
$ vim.tiny
If M a x

literature/promo/letter.0610

d o e s n o t w a n t t o u s e a l o n g p a t h n a m e t o s p e c i f y t h e f i l e , h e c a n u s e cd t o

m a k e p r o m o t h e w o r k i n g d i r e c t o r y b e f o r e u s i n g vim:
$ cd l i t e r a t u r e / p r o m o
$ pwd
/home/max/1i terature/promo
$ vim.tiny letter.0610
To m a k e the parent o f the w o r k i n g

directory

( n a m e d /home/max/literature) t h e

n e w w o r k i n g directory, M a x can give the f o l l o w i n g c o m m a n d , w h i c h takes
tage o fthe .. directory entry:
$ cd . .
$ pwd
/home/max/1i terature

advan-

212

CHAPTER 6

THE LINUX FILESYSTEM

Figure 6-10

U s i n g mv t o m o v e n a m e s a n d t e m p

mv, cp: MOVE OR COPY FILES
C h a p t e r 5 d i s c u s s e d t h e u s e o f mv t o r e n a m e f i l e s . H o w e v e r , mv w o r k s e v e n m o r e
generally: Y o u c a n use this u t i l i t y t o m o v e files f r o m o n e d i r e c t o r y t o a n o t h e r
( c h a n g e t h e p a t h n a m e o f a f i l e ) as w e l l as t o c h a n g e a s i m p l e f i l e n a m e . W h e n u s e d
t o m o v e o n e o r m o r e f i l e s t o a n e w d i r e c t o r y , t h e mv c o m m a n d h a s t h i s s y n t a x :
mv existing-file-list

directory

I f t h e w o r k i n g d i r e c t o r y is / h o m e / m a x , M a x c a n u s e t h e f o l l o w i n g c o m m a n d t o m o v e
t h e f i l e s n a m e s a n d t e m p f r o m t h e w o r k i n g d i r e c t o r y t o t h e literature d i r e c t o r y :
$ mv names temp l i t e r a t u r e
T h i s c o m m a n d c h a n g e s t h e a b s o l u t e p a t h n a m e s o f t h e names a n d temp files

from

/home/max/names a n d /home/max/temp t o /home/max/literature/names a n d
/home/max/literature/temp, respectively

(Figure 6-10).

Like

most

Linux

com-

m a n d s , mv a c c e p t s e i t h e r a b s o l u t e o r r e l a t i v e p a t h n a m e s .
A s y o u w o r k w i t h L i n u x a n d create m o r e files, y o u w i l l n e e d t o create n e w d i r e c t o r i e s u s i n g mkdir t o k e e p t h e f i l e s o r g a n i z e d . T h e mv u t i l i t y i s a u s e f u l t o o l f o r m o v i n g
f i l e s f r o m o n e d i r e c t o r y t o a n o t h e r as y o u e x t e n d y o u r d i r e c t o r y h i e r a r c h y .
T h e cp u t i l i t y w o r k s i n t h e s a m e w a y as mv d o e s , e x c e p t t h a t i t m a k e s c o p i e s o f t h e
existing-file-list

in the specified

directory.

mv: MOVES A DIRECTORY
J u s t as i t m o v e s o r d i n a r y f i l e s f r o m o n e d i r e c t o r y t o a n o t h e r , s o mv c a n m o v e d i r e c tories. T h e syntax is similar except that y o u specify one o r m o r e directories,
o r d i n a r y files, t o m o v e :
mv existing-directory-list

new-directory

not

WORKING WITH DIRECTORIES

Figure 6-11

213

A typical FHS-based L i n u x filesystem structure

I f new-directory

d o e s n o t e x i s t , t h e existing-directory-list

d i r e c t o r y n a m e , w h i c h mv c h a n g e s t o new-directory

m u s t contain just
(mv r e n a m e s t h e

one

directory).

A l t h o u g h y o u c a n r e n a m e d i r e c t o r i e s u s i n g mv, y o u c a n n o t c o p y t h e i r c o n t e n t s w i t h
cp u n l e s s y o u u s e t h e - r ( r e c u r s i v e ) o p t i o n . R e f e r t o t h e tar a n d cpio man p a g e s f o r
other ways to copy and m o v e directories.

IMPORTANT STANDARD DIRECTORIES AND FILES
O r i g i n a l l y files o n a L i n u x s y s t e m w e r e n o t l o c a t e d i n s t a n d a r d places w i t h i n t h e
d i r e c t o r y h i e r a r c h y . T h e scattered files m a d e it d i f f i c u l t t o d o c u m e n t a n d m a i n t a i n a
L i n u x system a n d just a b o u t i m p o s s i b l e f o r s o m e o n e to release a s o f t w a r e p a c k a g e
that w o u l d c o m p i l e a n d r u n o n all L i n u x systems. T h e first s t a n d a r d for the L i n u x
filesystem, the F S S T N D ( L i n u x Filesystem Standard), w a s released early i n 1994. I n
e a r l y 1 9 9 5 w o r k w a s s t a r t e d o n a b r o a d e r s t a n d a r d c o v e r i n g m a n y U N I X - l i k e systems: F H S ( L i n u x Filesystem H i e r a r c h y Standard; proton.pathname.com/fhs).

More

recently F H S has been i n c o r p o r a t e d i n L S B ( L i n u x S t a n d a r d Base; w w w . l i n u x f o u n dation.org/collaborate/workgroups/lsb),

a

workgroup

of

FSG

(Free

Standards

Group). Finally, FSG c o m b i n e d w i t h O p e n Source Development Labs ( O S D L )
f o r m the L i n u x

Foundation

(www.linuxfoundation.org).

Figure 6-11

shows

l o c a t i o n s o f s o m e i m p o r t a n t d i r e c t o r i e s a n d f i l e s as s p e c i f i e d b y F H S . T h e

to
the

signifi-

c a n c e o f m a n y o f t h e s e d i r e c t o r i e s w i l l b e c o m e c l e a r as y o u c o n t i n u e r e a d i n g .
T h e f o l l o w i n g list describes the directories s h o w n i n Figure 6 - 1 1 , s o m e of

the

directories specified by F H S , and some other directories. U b u n t u L i n u x , however,
does n o t

use all t h e d i r e c t o r i e s

specified by FHS.

Be a w a r e that y o u

a l w a y s d e t e r m i n e t h e f u n c t i o n o f a d i r e c t o r y b y its n a m e . F o r e x a m p l e ,

cannot
although

/opt stores a d d - o n s o f t w a r e , /etc/opt stores c o n f i g u r a t i o n files f o r t h e s o f t w a r e i n
/opt. See a l s o " I m p o r t a n t F i l e s a n d D i r e c t o r i e s " o n p a g e 4 8 8 .
/

Root

T h e r o o t d i r e c t o r y , p r e s e n t i n a l l L i n u x f i l e s y s t e m s t r u c t u r e s , is t h e a n c e s t o r

o f a l l files i n t h e filesystem.
/bin

Essential c o m m a n d binaries H o l d s t h e f i l e s n e e d e d t o b r i n g t h e s y s t e m u p a n d r u n i t
w h e n it first comes u p i n recovery m o d e (page 445).

/boot
/dev

Static files of the b o o t loader C o n t a i n s a l l t h e f i l e s n e e d e d t o b o o t t h e s y s t e m .
Device files C o n t a i n s a l l f i l e s t h a t r e p r e s e n t p e r i p h e r a l d e v i c e s , s u c h as d i s k d r i v e s ,
terminals, a n d printers. Previously this d i r e c t o r y w a s filled w i t h all possible devices.
T h e udev u t i l i t y ( p a g e 5 0 2 ) p r o v i d e s a d y n a m i c d e v i c e d i r e c t o r y t h a t e n a b l e s / d e v t o
c o n t a i n o n l y devices that are present o n the system.

214

CHAPTER 6

THE LINUX FILESYSTEM

/etc M a c h i n e - l o c a l system configuration files H o l d s a d m i n i s t r a t i v e , c o n f i g u r a t i o n ,

and

o t h e r s y s t e m files. O n e o f t h e m o s t i m p o r t a n t is /etc/passwd, w h i c h c o n t a i n s a l i s t
o f a l l users w h o h a v e p e r m i s s i o n t o use t h e system.
/etc/opt C o n f i g u r a t i o n files for a d d - o n s o f t w a r e p a c k a g e s kept in / o p t
/etc/Xll
/home

M a c h i n e - l o c a l configuration files for the X W i n d o w System
U s e r h o m e directories E a c h u s e r ' s h o m e d i r e c t o r y is t y p i c a l l y o n e o f m a n y

sub-

directories o f the /home directory. A s a n e x a m p l e , a s s u m i n g t h a t users' directories
a r e u n d e r / h o m e , t h e a b s o l u t e p a t h n a m e o f Z a c h ' s h o m e d i r e c t o r y is / h o m e / z a c h .
O n s o m e systems the users' directories m a y n o t be f o u n d u n d e r /home b u t instead
m i g h t b e s p r e a d a m o n g o t h e r d i r e c t o r i e s s u c h as / i n h o u s e a n d /clients.
/lib
/lib/modules
/mnt
/opt

Shared libraries
L o a d a b l e kernel m o d u l e s
M o u n t point for t e m p o r a r i l y m o u n t i n g filesystems
A d d - o n (optional) s o f t w a r e p a c k a g e s

/proc

Kernel and process i n f o r m a t i o n virtual filesystem

/root

H o m e directory for the r o o t a c c o u n t

/sbin

Essential system binaries U t i l i t i e s u s e d f o r s y s t e m a d m i n i s t r a t i o n a r e s t o r e d i n / s b i n
a n d /usr/sbin. T h e /sbin d i r e c t o r y i n c l u d e s u t i l i t i e s n e e d e d d u r i n g t h e b o o t i n g p r o cess, a n d /usr/sbin h o l d s u t i l i t i e s u s e d a f t e r t h e s y s t e m is u p a n d r u n n i n g . I n o l d e r
versions o f L i n u x , m a n y s y s t e m a d m i n i s t r a t i o n utilities w e r e scattered t h r o u g h several directories that often included other

s y s t e m files (/etc, /usr/bin, /usr/adm,

/usr/include).
/sys
/tmp
/usr

Device pseudofilesystem See u d e v o n p a g e 5 0 2 f o r m o r e i n f o r m a t i o n ,
T e m p o r a r y files
Second m a j o r hierarchy T r a d i t i o n a l l y i n c l u d e s s u b d i r e c t o r i e s t h a t c o n t a i n i n f o r m a t i o n u s e d b y t h e s y s t e m . Files i n /usr s u b d i r e c t o r i e s d o n o t c h a n g e o f t e n a n d m a y be
shared b y several systems.

/usr/bin

M o s t user c o m m a n d s C o n t a i n s t h e s t a n d a r d L i n u x u t i l i t y p r o g r a m s — t h a t i s , b i n a r i e s
that are n o t needed i n recovery m o d e (page 4 4 5 ) .

/usr/games
/usr/include
/usr/lib
/usr/local

G a m e s and educational p r o g r a m s
H e a d e r files included by C p r o g r a m s
Libraries
L o c a l hierarchy H o l d s l o c a l l y i m p o r t a n t f i l e s a n d d i r e c t o r i e s t h a t a r e a d d e d t o t h e
s y s t e m . S u b d i r e c t o r i e s c a n i n c l u d e bin, games, include, lib, sbin, share, a n d src.

/usr/sbin
/usr/share

N o n v i t a l system administration binaries See /sbin.
Architecture-independent d a t a S u b d i r e c t o r i e s c a n i n c l u d e diet, d o c , games, info,
locale, m a n , misc, terminfo, a n d zoneinfo.

ACCESS PERMISSIONS 2 1 5
/usr/share/doc

Documentation

/usr/share/info G N U info system's p r i m a r y directory
/usr/share/man Online m a n u a l s
/usr/src Source code
/var

Variable d a t a F i l e s w i t h c o n t e n t s t h a t v a r y a s t h e s y s t e m r u n s a r e k e p t i n s u b d i r e c t o r i e s u n d e r /var. T h e m o s t c o m m o n e x a m p l e s a r e t e m p o r a r y f i l e s , s y s t e m l o g
f i l e s , s p o o l e d f i l e s , a n d u s e r m a i l b o x f i l e s . S u b d i r e c t o r i e s c a n i n c l u d e cache, lib, lock,
log, mail, opt, r u n , spool, t m p , a n d yp. O l d e r v e r s i o n s o f L i n u x s c a t t e r e d s u c h f i l e s
t h r o u g h several s u b d i r e c t o r i e s o f / u s r (/usr/adm, /usr/mail, /usr/spool, /usr/tmp).

/var/log L o g files C o n t a i n s lastlog ( a r e c o r d o f t h e l a s t l o g i n b y e a c h u s e r ) , messages ( s y s t e m m e s s a g e s f r o m syslogd), a n d w t m p ( a r e c o r d o f a l l l o g i n s / l o g o u t s ) ,

among

o t h e r l o g files.
/var/spool Spooled application d a t a C o n t a i n s a n a c r o n , at, c r o n , lpd, mail, mqueue, s a m b a ,
a n d o t h e r d i r e c t o r i e s . T h e f i l e / v a r / s p o o l / m a i l is t y p i c a l l y a l i n k t o / v a r / m a i l .

ACCESS

PERMISSIONS
U b u n t u L i n u x s u p p o r t s t w o m e t h o d s o f c o n t r o l l i n g w h o c a n access a file a n d

how

t h e y c a n access i t : t r a d i t i o n a l L i n u x access p e r m i s s i o n s a n d A c c e s s C o n t r o l

Lists

( A C L s ) . T h i s s e c t i o n d e s c r i b e s t r a d i t i o n a l L i n u x a c c e s s p e r m i s s i o n s . See p a g e 2 2 1
f o r a d i s c u s s i o n o f A C L s , w h i c h p r o v i d e f i n e r - g r a i n e d c o n t r o l o f access p e r m i s s i o n s
t h a n d o t r a d i t i o n a l access p e r m i s s i o n s .
T h r e e t y p e s o f u s e r s c a n a c c e s s a f i l e : t h e o w n e r o f t h e f i l e [owner),
g r o u p t h a t t h e f i l e i s a s s o c i a t e d w i t h [group;

a member of a

see p a g e 4 9 2 f o r m o r e i n f o r m a t i o n o n

g r o u p s ) , a n d e v e r y o n e e l s e [other).

A user c a n a t t e m p t t o access a n o r d i n a r y file i n

t h r e e w a y s : b y t r y i n g t o read

write

from,

to, o r execute

it.

Is - I : DISPLAYS PERMISSIONS
W h e n y o u c a l l Is w i t h t h e - 1 o p t i o n a n d t h e n a m e o f o n e o r m o r e o r d i n a r y f i l e s , Is
displays a line o f i n f o r m a t i o n a b o u t the file. T h e f o l l o w i n g e x a m p l e displays i n f o r mation

f o r t w o files.

^

T h e file

/ir-1

Ci Aj

i?

contains

t h e text

o f a letter, a n d

C- ¿s,
/

-rwxrwxr-x+

Figure 6-12

letter.0610

/

3 max pubs

/
2048 2010-08-12 13:15 memo

T h e c o l u m n s d i s p l a y e d b y t h e Is

-1

command

216

CHAPTER 6

THE LINUX FILESYSTEM
check_spell c o n t a i n s a s h e l l s c r i p t , a p r o g r a m w r i t t e n i n a h i g h - l e v e l s h e l l p r o g r a m m i n g language:
$ I s -1 l e t t e r . 0 6 1 0 check_spell
- r w x r - x r - x 1 max p u b s
852 2010-07-31 13:47 check_spell
- r w - r - - r - - 1 max p u b s 3355 2 0 1 0 - 0 6 - 2 2 1 2 : 4 4 l e t t e r . 0 6 1 0
F r o m l e f t t o r i g h t , t h e l i n e s t h a t a n Is - 1 c o m m a n d d i s p l a y s c o n t a i n t h e

following

i n f o r m a t i o n (refer t o Figure 6 - 1 2 , preceding page):
• T h e type o ffile (first character)
• T h e file's access p e r m i s s i o n s (the n e x t n i n e c h a r a c t e r s )
• The A C L

flag

(present i f the file has a n A C L , page 2 2 1 )

• T h e n u m b e r o flinks t othe file (page 2 2 6 )
• T h e n a m e o fthe o w n e r o fthe file (usually the p e r s o n w h o created the file)
• T h e n a m e o fthe g r o u p the file is associated w i t h
• T h e size o f t h e file i n c h a r a c t e r s (bytes)
• T h e date a n d t i m e the file w a s created o rlast m o d i f i e d
• T h e n a m e o fthe file
T h e t y p e o f file (first c o l u m n ) f o r letter.0610 i s a h y p h e n ( - ) b e c a u s e i t i s a n o r d i n a r y file ( d i r e c t o r y files h a v e a d i n this c o l u m n ) .
T h e n e x t t h r e e c h a r a c t e r s s p e c i f y t h e a c c e s s p e r m i s s i o n s f o r t h e owner

o f t h e file: r

indicates read permission, w indicates write permission, and x indicates execute perm i s s i o n . A - i n a c o l u m n i n d i c a t e s t h a t t h e o w n e r d o e s not h a v e t h e p e r m i s s i o n t h a t
c o u l d have appeared i nt h a t position.
In a similar m a n n e r the next three characters represent permissions for the
a n d t h e f i n a l t h r e e c h a r a c t e r s r e p r e s e n t p e r m i s s i o n s f o r other

group,

( e v e r y o n e else). I n t h e

p r e c e d i n g e x a m p l e , t h e o w n e r o f letter.0610 c a n r e a d f r o m a n d w r i t e t o t h e

file,

w h e r e a s the g r o u p a n d others c a n o n l y r e a d f r o m the file a n d n oo n e is a l l o w e d t o
execute i t . A l t h o u g h execute p e r m i s s i o n c a n b e a l l o w e d f o r a n y file, i t does

not

m a k e sense t o assign e x e c u t e p e r m i s s i o n t o a file t h a t c o n t a i n s a d o c u m e n t , s u c h as
a l e t t e r . T h e check_spell f i l e i s a n e x e c u t a b l e s h e l l s c r i p t , s o e x e c u t e p e r m i s s i o n i s
a p p r o p r i a t e f o r it. ( T h e o w n e r , g r o u p , a n d others h a v e execute permission.)

c h m o d : CHANGES ACCESS PERMISSIONS
T h e L i n u x file access p e r m i s s i o n s c h e m e lets y o u g i v e o t h e r users access t o t h e files
y o u w a n t t o share yet k e e p y o u r p r i v a t e files c o n f i d e n t i a l . Y o u c a n a l l o w o t h e r users
t o r e a d f r o m and w r i t e t o a f i l e ( h a n d y i f y o u a r e o n e o f s e v e r a l p e o p l e w o r k i n g o n
a j o i n t project). Y o u c a n a l l o w others o n l y t o r e a d f r o m a file (perhaps a project
specification y o u are p r o p o s i n g ) . O r y o u c a n a l l o w others o n l y t o w r i t e t o a file
(similar t o a n i n b o x o rm a i l b o x , w h e r e y o u w a n t others t o be able t o send y o u m a i l
but d onot w a n t t h e m t o read y o u r mail). Similarly y o u can protect entire directories f r o m b e i n g s c a n n e d ( c o v e r e d s h o r t l y ) .

ACCESS PERMISSIONS 2 1 7

A user with root privileges can access any file on the system
security There is an exception to the access permissions described in this section. Anyone who can gain
root privileges has full access to a//files, regardless of the file's owner or access permissions.
T h e o w n e r o f a file c o n t r o l s w h i c h users h a v e p e r m i s s i o n t o access t h e file a n d
t h o s e users c a n access i t . W h e n y o u o w n

a file, y o u c a n use the c h m o d

m o d e ) u t i l i t y t o c h a n g e access p e r m i s s i o n s f o r t h a t file. Y o u c a n specify

how

(change
symbolic

(relative) o rn u m e r i c (absolute) arguments t o chmod.

SYMBOLIC ARGUMENTS TO c h m o d
T h e f o l l o w i n g e x a m p l e , w h i c h uses s y m b o l i c a r g u m e n t s t o c h m o d , a d d s (+) r e a d a n d
w r i t e p e r m i s s i o n s ( r w ) f o r a l l (a) users:
$ I s -1 l e t t e r . 0 6 1 0
_rw
i m a x pubs 3355 2 0 1 0 - 0 6 - 2 2
$ chmod a+rw l e t t e r . 0 6 1 0
$ I s -1 l e t t e r . 0 6 1 0
- r w - r w - r w - 1 max p u b s 3355 2 0 1 0 - 0 6 - 2 2

12:44

letter.0610

12:44

letter.0610

You must have read permission to execute a shell script
tip Because a shell needs to read a shell script (a text file containing shell commands) before it can
execute the commands within that script, you must have read permission for the file containing
the script to execute it. You also need execute permission to execute a shell script directly from
the command line. In contrast, binary (program) files do not need to be read; they are executed
directly. You need only execute permission to run a binary program.
Using symbolic arguments w i t h chmod modifies existing permissions; the change a
g i v e n a r g u m e n t m a k e s depends o n (isrelative to) the existing permissions. I n the
n e x t e x a m p l e , c h m o d r e m o v e s ( - ) r e a d (r) a n d e x e c u t e (x) p e r m i s s i o n s f o r o t h e r

(o)

users. T h e o w n e r a n d g r o u p p e r m i s s i o n s are n o t affected.
$ I s -1 check_spell
- r w x r - x r - x 1 max p u b s 852
$ chmod o - r x check_spell
$ I s -1 check_spell
- r w x r - x — 1 max p u b s 852

2010-07-31 13:47

check_spell

2010-07-31 13:47

check_spell

I n a d d i t i o n t o a (all) a n d o ( o t h e r ) , y o u c a n use g ( g r o u p ) a n d u (user, a l t h o u g h
r e f e r s t o t h e owner

user

o fthe file w h o m a y o rm a y n o t be the user o fthe file a t a n y g i v e n

time) i n the argument t o chmod. For example, c h m o d a+x adds execute

permission

for all users (other, g r o u p , a n d o w n e r ) a n d c h m o d g o - r w x r e m o v e s all permissions
f o r all b u t t h e o w n e r o ft h e file.

chmod: o for other, u for owner
tip When using chmod, many people assume that the o stands for owner; it does not. The o stands
for other, whereas u stands for owner [user). The acronym UGO (user-group-other) may help you
remember how permissions are named.

218

CHAPTER 6

THE LINUX FILESYSTEM

NUMERIC ARGUMENTS TO c h m o d
Y o u c a n also use n u m e r i c a r g u m e n t s t o s p e c i f y p e r m i s s i o n s w i t h chmod. I n p l a c e
of the letters a n d s y m b o l s specifying permissions used i n the previous

examples,

n u m e r i c a r g u m e n t s comprise three octal digits. ( A fourth, leading digit

controls

setuid a n d setgid permissions a n d is discussed next.) T h e first digit specifies

per-

missions f o r the o w n e r , the s e c o n d f o r the g r o u p , a n d the t h i r d f o r o t h e r users. A
1 gives the specified user(s) execute permission, a 2 gives w r i t e permission, a n d a
4 gives read permission. C o n s t r u c t the digit representing the permissions for

the

o w n e r , g r o u p , o r o t h e r s b y O R i n g ( a d d i n g ) t h e a p p r o p r i a t e v a l u e s as s h o w n i n t h e
f o l l o w i n g e x a m p l e s . U s i n g n u m e r i c a r g u m e n t s sets file p e r m i s s i o n s a b s o l u t e l y ; i t
d o e s n o t m o d i f y e x i s t i n g p e r m i s s i o n s as s y m b o l i c a r g u m e n t s

do.

I n the f o l l o w i n g e x a m p l e , chmod changes permissions s oo n l y the o w n e r o f the file
c a n read f r o m a n d w r i t e t o the file, regardless o f h o w p e r m i s s i o n s w e r e
set. T h e 6 i n t h e f i r s t p o s i t i o n g i v e s t h e o w n e r r e a d (4) a n d w r i t e (2)

previously

permissions.

T h e Os r e m o v e a l l p e r m i s s i o n s f o r t h e g r o u p a n d o t h e r u s e r s .
$ chmod 600 l e t t e r . 0 6 1 0
$ I s -1 l e t t e r . 0 6 1 0
_rw
i m a x pubs 3355

2010-06-22

12:44

letter.0610

N e x t , 7 (4 + 2 + 1) g i v e s t h e o w n e r r e a d , w r i t e , a n d e x e c u t e p e r m i s s i o n s . T h e 5 (4 + 1 )
gives the g r o u p a n d o t h e r users r e a d a n d execute permissions:
$ chmod 755 check_spell
$ I s -1 check_spell
-rwxr-xr-x

1 max p u b s

852

2010-07-31 13:47

Refer t oTable 6-2 for m o r e examples o fnumeric

Table 6-2

check_spell

permissions.

Examples of numeric permission specifications

Mode

Meaning

777

Owner, group, and others can read, write, and execute file

755

Owner can read, write, and execute file; group and others can read and execute file

711

Owner can read, write, and execute file; group and others can execute file

644

Owner can read and write file; group and others can read file

640

Owner can read and write file, group can read file, and others cannot access file

Refer t o page 3 0 0 for m o r e i n f o r m a t i o n o nu s i n g chmod t o m a k e a file

executable

a n d t o t h e chmod man p a g e f o r i n f o r m a t i o n o n a b s o l u t e a r g u m e n t s a n d chmod i n
general. Refer t opage 4 9 2 for m o r e i n f o r m a t i o n o n groups.

SETUID AND SETGID PERMISSIONS
W h e n y o u e x e c u t e a file t h a t h a s s e t u i d (set user I D ) p e r m i s s i o n , t h e p r o c e s s

exe-

c u t i n g t h e file t a k e s o n t h e p r i v i l e g e s o f t h e file's o w n e r . F o r e x a m p l e , i f y o u r u n a

ACCESS PERMISSIONS 2 1 9
setuid p r o g r a m t h a t r e m o v e s all files i n a d i r e c t o r y , y o u c a n r e m o v e files i n a n y o f
t h e file o w n e r ' s directories, e v e n i f y o u d o n o t n o r m a l l y h a v e p e r m i s s i o n t o d o so.
I n a s i m i l a r m a n n e r , s e t g i d (set g r o u p I D ) p e r m i s s i o n gives t h e p r o c e s s
the file the privileges o f the g r o u p the file is associated

executing

with.

Minimize use of setuid and setgid programs owned by root
security Executable files that are setuid and owned by root have root privileges when they run, even if they
are not run by root. This type of program is very powerful because it can do anything that root can
do (and that the program is designed to do). Similarly executable files that are setgid and belong
to the group root have extensive privileges.
Because of the power they hold and their potential for destruction, it is wise to avoid indiscriminately creating and using setuid programs owned by root and setgid programs belonging to the
group root. Because of their inherent dangers, many sites minimize the use of these programs on
their systems. One necessary setuid program is passwd. See page 421 for a tip on setuid files
owned by root and page 454 for a command that lists setuid files on the local system.
T h e f o l l o w i n g e x a m p l e s h o w s a user w o r k i n g w i t h r o o t privileges a n d using s y m b o l i c
arguments t oc h m o d to give one p r o g r a m setuid privileges a n d another p r o g r a m setgid
privileges. T h e Is- 1 o u t p u t (page 2 1 5 ) s h o w s setuid p e r m i s s i o n b yd i s p l a y i n g a n s i n
the owner's

executable

position

and

setgid permission

b y displaying a n s i n t h e

group's executable position:
$ I s -1 myprog*
-rwxr-xr-x 1 root
-rwxr-xr-x 1 root

pubs
pubs

19704
19704

2010-07-31 14:30
2010-07-31 14:30

myprogl
myprog2

2010-07-31 14:30
2010-07-31 14:30

myprogl
myprog2

$ sudo chmod u+s myprogl
$ sudo chmod g+s myprog2
$ I s -1 myprog*
-rwsr-xr-x 1 root
-rwxr-sr-x 1 root
The next example

pubs
pubs

19704
19704

uses n u m e r i c

arguments t o chmod t o m a k e the same

changes.

W h e n y o u use f o u r d i g i t s t o s p e c i f y p e r m i s s i o n s , s e t t i n g t h e f i r s t d i g i t t o 1 sets t h e
sticky

bit ( p a g e 1 1 7 4 ) , s e t t i n g i t t o 2 s p e c i f i e s s e t g i d p e r m i s s i o n s , a n d s e t t i n g i t t o 4

specifies setuid permissions:
$ I s -1 myprog*
-rwxr-xr-x
-rwxr-xr-x

1 root
1 root

pubs
pubs

19704
19704

2010-07-31 14:30
2010-07-31 14:30

myprogl
myprog2

2010-07-31 14:30
2010-07-31 14:30

myprogl
myprog2

$ sudo chmod 4755 myprogl
$ sudo chmod 2755 myprog2
$ I s -1 myprog*
-rwsr-xr-x 1 root
-rwxr-sr-x 1 root

pubs
pubs

19704
19704

Do not write setuid shell scripts
security Never give shell scripts setuid permission. Several techniques for subverting them are well known.

220

CHAPTER 6

THE LINUX FILESYSTEM

DIRECTORY ACCESS PERMISSIONS
Access permissions have slightly different meanings w h e n they are used w i t h

direc-

tories. A l t h o u g h the three types o f users c a n r e a d f r o m o r w r i t e t o a directory,

the

directory cannot b e executed. Execute permission is redefined f o ra directory: I t
m e a n s t h a t y o u c a n c d i n t o t h e d i r e c t o r y a n d / o r e x a m i n e files t h a t y o u h a v e p e r m i s s i o n t o r e a d f r o m i n t h e d i r e c t o r y . I t has n o t h i n g t o d o w i t h e x e c u t i n g a file.
W h e n y o u h a v e o n l y e x e c u t e p e r m i s s i o n f o r a d i r e c t o r y , y o u c a n u s e Is t o l i s t a f i l e
i n t h e d i r e c t o r y i f y o u k n o w i t s n a m e . Y o u c a n n o t u s e Is w i t h o u t a n a r g u m e n t t o l i s t
the entire contents o f the directory. I n the f o l l o w i n g exchange, Z a c h first

verifies

t h a t h e i s l o g g e d i n as h i m s e l f . H e t h e n c h e c k s t h e p e r m i s s i o n s o n M a x ' s info d i r e c t o r y . Y o u c a n v i e w t h e a c c e s s p e r m i s s i o n s a s s o c i a t e d w i t h a d i r e c t o r y b y r u n n i n g Is
w i t h t h e - d ( d i r e c t o r y ) a n d -1 ( l o n g )

options:

$ who am i
zach
pts/7
Aug 2 1 1 0 : 0 2
$ I s - I d /home/max/info
drwx
x
2 max pubs 5 1 2 2 0 1 0 - 0 8 - 2 1 0 9 : 3 1
$ I s -1 /home/max/info
Is: /home/max/info: Permission denied

/home/max/info

T h e d a t t h e l e f t e n d o f t h e l i n e t h a t Is d i s p l a y s i n d i c a t e s t h a t / h o m e / m a x / i n f o i s a
o f the

pubs

g r o u p h a v e n o access p e r m i s s i o n s ; a n d o t h e r users h a v e e x e c u t e p e r m i s s i o n

directory.

M a x

has read, write,

and

execute permissions;

members

only,

indicated b ythe x a tthe right end o fthe permissions. Because Z a c h does n o t

have

r e a d p e r m i s s i o n f o r t h e d i r e c t o r y , t h e Is - 1 c o m m a n d r e t u r n s a n e r r o r .
W h e n Z a c h specifies t h e n a m e s o f t h e files h ew a n t s i n f o r m a t i o n a b o u t , h eis n o t
reading new

directory i n f o r m a t i o n b u t rather searching f o rspecific

information,

w h i c h h e is a l l o w e d t o d o w i t h e x e c u t e access t o t h e d i r e c t o r y . H e has r e a d p e r m i s s i o n f o r notes s o h e h a s n o p r o b l e m u s i n g cat t o d i s p l a y t h e f i l e . H e c a n n o t

display

financial b e c a u s e h e d o e s n o t h a v e r e a d p e r m i s s i o n f o r i t :
$ I s -1 / h o m e / m a x / i n f o / f i n a n c i a l /home/max/info/notes
-rw
1 max pubs 3 4 2 0 1 0 - 0 8 - 2 1 0 9 : 3 1 / h o m e / m a x / i n f o / f i n a n c i a l
-rw-r--r-1 max pubs 3 0 2 0 1 0 - 0 8 - 2 1 0 9 : 3 2 / h o m e / m a x / i n f o / n o t e s
$ cat /home/max/info/notes
T h i s i s t h e f i l e named n o t e s .
$ cat / h o m e / m a x / i n f o / f i n a n c i a l
cat: /home/max/info/financial: Permission denied
N e x t M a x g i v e s o t h e r s r e a d a c c e s s t o h i s info d i r e c t o r y :
$ chmod o+r /home/max/info
W h e n Z a c h c h e c k s h i s a c c e s s p e r m i s s i o n s o n info, h e f i n d s t h a t h e h a s b o t h
a n d e x e c u t e a c c e s s t o t h e d i r e c t o r y . N o w Is - 1 w o r k s j u s t f i n e w i t h o u t

read

arguments,

b u t h e s t i l l c a n n o t r e a d financial. ( T h i s r e s t r i c t i o n i s a n i s s u e o f f i l e p e r m i s s i o n s ,

not

d i r e c t o r y p e r m i s s i o n s . ) F i n a l l y , Z a c h t r i e s t o c r e a t e a f i l e n a m e d n e w f i l e u s i n g touch.

A C L s : A C C E S S CONTROL LISTS 2 2 1
I f M a x w e r e t o g i v e h i m w r i t e p e r m i s s i o n t o t h e info d i r e c t o r y , Z a c h w o u l d b e a b l e
t o create n e w files i n i t :
$ I s - I d /home/max/info
drwx—r-x
2 max pubs 5 1 2 2 0 1 0 - 0 8 - 2 1 0 9 : 3 1 / h o m e / m a x / i n f o
$ I s -1 /home/max/info
total 8
_rw
i max pubs 34 2 0 1 0 - 0 8 - 2 1 0 9 : 3 1 f i n a n c i a l
-rw-r—r—
1 max pubs 3 0 2 0 1 0 - 0 8 - 2 1 0 9 : 3 2 n o t e s
$ cat / h o m e / m a x / i n f o / f i n a n c i a l
cat: f i n a n c i a l : Permission denied
$ touch /home/max/info/newfile
touch: cannot touch '/home/max/info/newfile': Permission denied

ACLs:

ACCESS CONTROL LISTS
Access

Control

access

specific

Lists ( A C L s )
directories

provide
a n d files

finer-grained control
than

over w h i c h

d o traditional

Linux

users

can

permissions

(page 2 1 5 ) . U s i n g A C L s y o u c a n specify the w a y s i nw h i c h each o fseveral users c a n
access a d i r e c t o r y o r file. B e c a u s e A C L s

can reduce performance,

d onot

t h e m o nfilesystems t h a t h o l d s y s t e m files, w h e r e t h e t r a d i t i o n a l L i n u x

enable

permissions

are sufficient. A l s o be c a r e f u l w h e n m o v i n g , c o p y i n g , o r a r c h i v i n g files: N o t all utilities preserve A C L s . I n a d d i t i o n , y o u c a n n o t c o p y A C L s t o filesystems t h a t d o n o t
support

ACLs.

A n A C L c o m p r i s e s a set o f r u l e s . A r u l e specifies h o w a s p e c i f i c user o r g r o u p
access t h e file t h a t t h e A C L is a s s o c i a t e d w i t h . T h e r e are t w o k i n d s o f rules:

rules a n d default
ACLs,

rules.

( T h e d o c u m e n t a t i o n r e f e r s t o access

ACLs

and

even t h o u g h there is o n l y one t y p e o f A C L : T h e r e is one t y p e o f list

can
access

default
[ACL]

a n d there are t w o types o frules that a n A C L can contain.)
A n access r u l e specifies access i n f o r m a t i o n f o r a single file o r d i r e c t o r y . A
A C L p e r t a i n s t o a d i r e c t o r y o n l y ; i t specifies d e f a u l t access i n f o r m a t i o n ( a n
for a n y file i nthe d i r e c t o r y t h a t is n o t g i v e n a n explicit

default
ACL)

ACL.

Most utilities do not preserve ACLs
caution When used with the -p (preserve) or -a (archive) option, cp preserves ACLs when it copies files.
The mv utility also preserves ACLs. When you use cp with the -p or -a option and it is not able
to copy ACLs, and in the case where mv is unable to preserve ACLs, the utility performs the operation and issues an error message:
$ m v report /tmp
mv: p r e s e r v i n g p e r m i s s i o n s

f o r '/tmp/report' : Operation

n o t 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.

222

CHAPTER 6

THE LINUX FILESYSTEM

ENABLING A C L S
B e f o r e y o u c a n u s e A C L s y o u m u s t i n s t a l l t h e acl s o f t w a r e p a c k a g e :
$ sudo aptitude i n s t a l l

acl

U b u n t u L i n u x o f f i c i a l l y s u p p o r t s A C L s o n ext2, ext3, a n d ext4 f i l e s y s t e m s

only,

a l t h o u g h i n f o r m a l s u p p o r t f o r A C L s is a v a i l a b l e o n o t h e r filesystems. T o use A C L s
o n a n e x t 2 / e x t 3 / e x t 4 f i l e s y s t e m , y o u m u s t m o u n t t h e d e v i c e w i t h t h e acl

option

( n o _ a c l is t h e d e f a u l t ) . F o r e x a m p l e , i f y o u w a n t t o m o u n t t h e d e v i c e r e p r e s e n t e d b y
/ h o m e s o t h a t y o u c a n u s e A C L s o n f i l e s i n / h o m e , y o u c a n a d d acl t o i t s o p t i o n s l i s t
i n /etc/fstab:
$ grep home / e t c / f s t a b
LABEL=/home
/home

ext4

defaults,acl

12

r e m o u n t option A f t e r c h a n g i n g fstab, y o u n e e d t o r e m o u n t / h o m e b e f o r e y o u c a n u s e A C L s . I f n o
o n e else i s u s i n g t h e s y s t e m , y o u c a n u n m o u n t i t a n d m o u n t i t a g a i n ( w o r k i n g w i t h
root p r i v i l e g e s ) as l o n g as t h e w o r k i n g d i r e c t o r y i s n o t i n t h e /home

hierarchy.

A l t e r n a t i v e l y y o u c a n u s e t h e r e m o u n t o p t i o n t o mount t o r e m o u n t / h o m e w h i l e t h e
device is i n use:
$ sudo mount -v -o remount /home
/ d e v / s d a B on /home type e x t 4 ( r w , a c l )
See p a g e 5 1 0 f o r i n f o r m a t i o n o n fstab a n d p a g e 5 0 6 f o r i n f o r m a t i o n o n mount.

WORKING WITH ACCESS RULES
T h e setfacl u t i l i t y m o d i f i e s a f i l e ' s A C L a n d getfacl d i s p l a y s a f i l e ' s A C L . W h e n

you

u s e getfacl t o o b t a i n i n f o r m a t i o n a b o u t a f i l e t h a t d o e s n o t h a v e a n A C L , i t d i s p l a y s
t h e s a m e i n f o r m a t i o n as a n Is -1 c o m m a n d , a l b e i t i n a d i f f e r e n t f o r m a t :
$ I s -1 report
-rw-r--r-1 max max 9 5 3 7 2 0 1 0 - 0 1 - 1 2

23:17

report

$ getfacl report
# file:
report
# o w n e r : max
# g r o u p : max
user::rwgroup::r-other::r-T h e f i r s t t h r e e l i n e s o f t h e getfacl o u t p u t c o m p r i s e t h e h e a d e r ; t h e y s p e c i f y t h e n a m e
o f the file, the o w n e r o fthe file, a n d t h e g r o u p the file is associated w i t h . F o r m o r e
i n f o r m a t i o n r e f e r t o "Is -1: D i s p l a y s P e r m i s s i o n s " o n p a g e 2 1 5 . T h e — o m i t - h e a d e r
( o r j u s t — o m i t ) o p t i o n c a u s e s getfacl n o t t o d i s p l a y t h e h e a d e r :
$ getfacl --omit-header
user::rwgroup::r-other::r--

report

A C L s : ACCESS CONTROL LISTS 2 2 3
I n t h e l i n e t h a t s t a r t s w i t h user, t h e t w o c o l o n s ( : : ) w i t h n o n a m e b e t w e e n t h e m i n d i cate t h a t the l i n e specifies t h e p e r m i s s i o n s f o r t h e o w n e r o f t h e file. S i m i l a r l y , the
t w o c o l o n s i n t h e group l i n e i n d i c a t e t h a t t h e l i n e specifies p e r m i s s i o n s f o r t h e g r o u p
t h e f i l e i s a s s o c i a t e d w i t h . T h e t w o c o l o n s f o l l o w i n g other a r e t h e r e f o r c o n s i s t e n c y :
N o n a m e c a n b e a s s o c i a t e d w i t h other.
T h e setfacl — m o d i f y ( o r - m ) o p t i o n a d d s o r m o d i f i e s o n e o r m o r e r u l e s i n a f i l e ' s
A C L using the f o l l o w i n g format:

setfacl —modify

ugo.name.permissions

file-list

w h e r e ugo c a n b e e i t h e r u, g , o r o t o i n d i c a t e t h a t t h e c o m m a n d sets f i l e p e r m i s s i o n s
f o r a u s e r , a g r o u p , o r a l l o t h e r u s e r s , r e s p e c t i v e l y ; name
g r o u p t h a t p e r m i s s i o n s a r e b e i n g s e t f o r ; permissions
s y m b o l i c o r a b s o l u t e f o r m a t ; a n d file-list
a p p l i e d t o . Y o u m u s t o m i t name

is the n a m e o f the user o r
is the permissions i n either

is t h e l i s t o f f i l e s t h e p e r m i s s i o n s a r e t o b e

w h e n y o u s p e c i f y p e r m i s s i o n s f o r o t h e r u s e r s (o).

S y m b o l i c p e r m i s s i o n s use letters t o r e p r e s e n t file p e r m i s s i o n s (rwx, r - x , a n d so o n ) ,
w h e r e a s a b s o l u t e p e r m i s s i o n s u s e a n o c t a l n u m b e r . W h i l e c h m o d u s e s t h r e e sets o f
permissions o r three octal numbers

(one each f o r the owner, group, a n d

other

u s e r s ) , setfacl u s e s a s i n g l e s e t o f p e r m i s s i o n s o r a s i n g l e o c t a l n u m b e r t o r e p r e s e n t
t h e p e r m i s s i o n s b e i n g g r a n t e d t o t h e u s e r o r g r o u p r e p r e s e n t e d b y ugo a n d

name.

See t h e d i s c u s s i o n o f c h m o d o n p a g e 2 1 6 f o r m o r e i n f o r m a t i o n a b o u t s y m b o l i c

and

absolute representations o f file permissions.
F o r e x a m p l e , b o t h o f t h e f o l l o w i n g c o m m a n d s a d d a r u l e t o t h e A C L f o r t h e report
file t h a t gives S a m r e a d a n d w r i t e p e r m i s s i o n t o t h a t file:
$ setfacl --modify u:sam:rw- report

or
$ setfacl --modify u:sam:6 report
$ getfacl report
# file:
report
# o w n e r : max
# g r o u p : max
user::rwuser:sam:rwgroup::r-mask::rwother::r-T h e l i n e c o n t a i n i n g user:sam:rw- s h o w s t h a t t h e u s e r n a m e d sam h a s r e a d

and

w r i t e a c c e s s ( r w - ) t o t h e f i l e . See p a g e 2 1 5 f o r a n e x p l a n a t i o n o f h o w t o r e a d a c c e s s
p e r m i s s i o n s . See t h e f o l l o w i n g o p t i o n a l s e c t i o n f o r a d e s c r i p t i o n o f t h e l i n e

that

s t a r t s w i t h mask.
W h e n a f i l e h a s a n A C L , Is -1 d i s p l a y s a p l u s s i g n ( + ) f o l l o w i n g t h e
even i f the A C L is empty:
$ I s -1 report
- r w - r w - r - - + 1 max max 9 5 3 7 2 0 1 0 - 0 1 - 1 2

23:17

report

permissions,

224

CHAPTER 6

THE LINUX FILESYSTEM

optional EFFECTIVE RIGHTS MASK
T h e l i n e t h a t s t a r t s w i t h m a s k s p e c i f i e s t h e effective

rights

mask.

This mask limits the

effective p e r m i s s i o n s g r a n t e d t o A C L g r o u p s a n d users. I t does n o t affect the o w n e r o f
the file o rthe g r o u p the file is associated w i t h . I no t h e r w o r d s , i t does n o t affect tradit i o n a l L i n u x p e r m i s s i o n s . H o w e v e r , b e c a u s e setfacl a l w a y s sets t h e e f f e c t i v e

rights

m a s k t o the least restrictive A C L p e r m i s s i o n s f o r the file, the m a s k has n o effect unless
y o u set i t e x p l i c i t l y a f t e r y o u set u p a n A C L f o r t h e file. Y o u c a n set t h e m a s k b y speci f y i n g m a s k i n p l a c e o f ugo a n d b y n o t s p e c i f y i n g a name

i n a setfacl c o m m a n d .

T h e f o l l o w i n g e x a m p l e sets t h e e f f e c t i v e r i g h t s m a s k t o read f o r t h e report file:
$ setfacl

-m m a s k : : r - -

report

T h e m a s k l i n e i n t h e f o l l o w i n g getfacl o u t p u t s h o w s t h e e f f e c t i v e r i g h t s m a s k set t o
r e a d ( r — ) . T h e l i n e t h a t d i s p l a y s S a m ' s file access p e r m i s s i o n s s h o w s t h e m still set
to read a n d write. H o w e v e r , the c o m m e n t a tthe right end o fthe line s h o w s that his
effective permission is read.
$ getfacl report
# file:
report
# owner: max
# group: max
user::rwuser:sam:rwgroup::r-mask::r-other::r--

#effective:r--

A s t h e n e x t e x a m p l e s h o w s , setfacl c a n m o d i f y A C L r u l e s a n d c a n set m o r e t h a n o n e
A C L rule ata time:
$ setfacl

-m u : s a m : r - - , u : z a c h : r w -

$ g e t f a c l --omit-header
user::rwuser:sam:r-user:zach:rwgroup::r-mask::rwother::r--

report

report

T h e - x o p t i o n removes A C L rules for a user o r a g r o u p . I t has n oeffect o n

permis-

sions for the o w n e r o fthe file o r the g r o u p that the file is associated w i t h . T h e

next

e x a m p l e s h o w s setfacl r e m o v i n g t h e r u l e t h a t g i v e s S a m p e r m i s s i o n t o access t h e f i l e :
$ setfacl

- x u:sam r e p o r t

$ g e t f a c l --omit-header
user::rwuser:zach:rwgroup::r-mask::rwother::r--

report

A C L s : A C C E S S CONTROL L I S T S 2 2 5
Y o u m u s t n o t s p e c i f y permissions
t h e ugo a n d name.

w h e n y o u use the - xo p t i o n . Instead, specify o n l y

T h e - bo p t i o n , f o l l o w e d b y a filename only, removes all

ACL

rules a n d the A C L itself f r o m the file o r d i r e c t o r y y o u specify.
B o t h s e t f a c l a n d getfacl h a v e m a n y o p t i o n s . U s e t h e — h e l p o p t i o n t o d i s p l a y

brief

lists o f o p t i o n s o r refer t o t h e man pages f o r details.

SETTING DEFAULT RULES FORA DIRECTORY
T h e f o l l o w i n g e x a m p l e s h o w s t h a t t h e dir d i r e c t o r y i n i t i a l l y h a s n o A C L . T h e s e t f a c l
c o m m a n d u s e s t h e - d ( d e f a u l t ) o p t i o n t o a d d t w o d e f a u l t r u l e s t o t h e A C L f o r dir.
T h e s e r u l e s a p p l y t o a l l f i l e s i n t h e dir d i r e c t o r y t h a t d o n o t h a v e e x p l i c i t A C L s .
r u l e s g i v e m e m b e r s o f t h e pubs g r o u p r e a d a n d e x e c u t e p e r m i s s i o n s a n d g i v e

The

mem-

bers o f t h e admin g r o u p r e a d , w r i t e , a n d e x e c u t e p e r m i s s i o n s .
$ Is - I d dir
drwx
2 max max 4 0 9 6 2 0 1 0 - 0 2 - 1 2
$ getfacl d i r
# file: d i r
# owner: max
# group: max
user::rwx
group:: —
other:: —
$ setfacl
The

following

23:15 d i r

-d -m g:pubs:r-x,g:admin:rwx
Is c o m m a n d

shows

dir

t h a t t h e dir d i r e c t o r y n o w

has a n A C L , as

indicated b ythe + t o the right o f the permissions. Each o f the default rules
getfacl d i s p l a y s

starts

with

default:.

The

first

t w odefault

rules

and

that

t h e last

d e f a u l t rule specify the p e r m i s s i o n s f o r the o w n e r o f the file, t h e g r o u p t h a t
file is associated w i t h , a n d a l lother users. These three rules specify the
tional L i n u x permissions and take precedence over other A C L

rules. T h e

the

tradithird

a n d f o u r t h r u l e s s p e c i f y t h e p e r m i s s i o n s f o r t h e pubs a n d admin g r o u p s . N e x t i s
the default effective rights

mask.

$ Is - I d dir
drwx
+ 2 max max 4 0 9 6 2 0 1 0 - 0 2 - 1 2
$ getfacl d i r
# file: d i r
# owner: max
# group: max
user::rwx
group:: —
other:: —
d e f a u l t : u s e r : : rwx
default:group: :
default:group:pubs: r-x
default:group:admin:rwx
default:mask::rwx
default:other::

23:15 d i r

226

CHAPTER 6

THE LINUX FILESYSTEM
R e m e m b e r t h a t t h e d e f a u l t rules p e r t a i n t o files h e l d i n t h e d i r e c t o r y t h a t are

not

a s s i g n e d A C L s e x p l i c i t l y . Y o u c a n also s p e c i f y access rules f o r t h e d i r e c t o r y itself.
W h e n y o u create a file w i t h i n a d i r e c t o r y t h a t has d e f a u l t rules i nits A C L , t h e effect i v e r i g h t s m a s k f o r t h a t file i s c r e a t e d b a s e d o n t h e file's p e r m i s s i o n s . I n s o m e cases
the mask m a y override default A C L
In the next example,

touch c r e a t e s

rules.

a f i l e n a m e d n e w i n t h e dir d i r e c t o r y . T h e

m a n d s h o w s t h a t this file has a n A C L . Based o n the v a l u e o f

umask

Is

com-

(page 459),

b o t h the o w n e r a n d the g r o u p t h a t the file is associated w i t h h a v e r e a d a n d

write

p e r m i s s i o n s f o r t h e file. T h e e f f e c t i v e r i g h t s m a s k i s set t o r e a d a n d w r i t e s o t h a t
t h e e f f e c t i v e p e r m i s s i o n f o r pubs i s r e a d a n d t h e e f f e c t i v e p e r m i s s i o n s f o r admin a r e
read and write. Neither g r o u p has execute

permission.

$ cd d i r
$ touch new
$ I s -1 new
-rw-rw
+ 1 max max 0 2 0 1 0 - 0 2 - 1 3 0 0 : 3 9 new
$ g e t f a c l - - o m i t new
user::rwgroup::—
group:pubs:r-x
#effective:r-group:admin:rwx
#effective:rwmask::rwo t h e r : : —
I f y o u c h a n g e t h e file's t r a d i t i o n a l p e r m i s s i o n s t o r e a d , w r i t e , a n d e x e c u t e f o r
o w n e r a n d the group, the effective rights m a s k changes t o read, write, a n d

the

execute

a n d t h e g r o u p s s p e c i f i e d b y t h e d e f a u l t rules g a i n e x e c u t e access t o t h e file.
$ chmod 770 new
$ I s -1 new
- r w x r w x — + 1 max max 0 2 0 1 0 - 0 2 - 1 3
$ g e t f a c l - - o m i t new
user::rwx
group::—
group:pubs:r-x
group:admin:rwx
mask::rwx
o t h e r : : —

00:39

new

LINKS
A

link i s a p o i n t e r t o a f i l e . E a c h t i m e y o u c r e a t e a f i l e u s i n g vim, touch, cp, o r b y

another other means, y o u are p u t t i n g a pointer i n a directory. This pointer

associ-

ates a f i l e n a m e w i t h a p l a c e o n t h e disk. W h e n y o u specify a f i l e n a m e i n a c o m mand,

y o u are indirectly

information you

want.

pointing

t o t h e place

o n t h e disk

that

holds t h e

LINKS

Figure 6-13

227

U s i n g l i n k s t o cross-classify files

S h a r i n g files c a n be u s e f u l w h e n t w o o r m o r e p e o p l e are w o r k i n g o n t h e

same

p r o j e c t a n d n e e d t o share s o m e i n f o r m a t i o n . Y o u c a n m a k e it easy f o r o t h e r users t o
access o n e o f y o u r files b y c r e a t i n g a d d i t i o n a l l i n k s t o t h e file.
T o share a file w i t h a n o t h e r user, first g i v e t h e user p e r m i s s i o n t o r e a d f r o m

and

w r i t e t o t h e file ( p a g e 2 1 6 ) . Y o u m a y also h a v e t o c h a n g e t h e access p e r m i s s i o n s
of the p a r e n t d i r e c t o r y o f the file t o give the user read, w r i t e , or execute p e r m i s s i o n ( p a g e 2 2 0 ) . O n c e t h e p e r m i s s i o n s are a p p r o p r i a t e l y set, t h e user c a n c r e a t e a
l i n k t o t h e f i l e so t h a t e a c h o f y o u c a n access t h e file f r o m y o u r s e p a r a t e d i r e c t o r y
hierarchies.
A l i n k c a n also be useful t o a single user w i t h a large d i r e c t o r y hierarchy. Y o u c a n
create l i n k s t o cross-classify files i n y o u r d i r e c t o r y h i e r a r c h y , u s i n g d i f f e r e n t classifications f o r different tasks. F o r e x a m p l e , if y o u have the file l a y o u t depicted

in

F i g u r e 6 - 2 o n p a g e 2 0 1 , a file n a m e d to_do m i g h t a p p e a r i n e a c h s u b d i r e c t o r y

of

t h e c o r r e s p o n d d i r e c t o r y — t h a t i s , i n personal, m e m o s , a n d business. I f y o u f i n d i t
difficult to keep track of everything y o u need to do, y o u can create a separate direct o r y n a m e d to_do i n t h e correspond d i r e c t o r y . Y o u c a n t h e n l i n k e a c h s u b d i r e c t o r y ' s
t o - d o list i n t o t h a t d i r e c t o r y . F o r e x a m p l e , y o u c o u l d l i n k t h e file n a m e d to_do i n
t h e memos d i r e c t o r y t o a f i l e n a m e d memos i n t h e to_do d i r e c t o r y . T h i s set o f l i n k s
is s h o w n i n F i g u r e 6 - 1 3 .
A l t h o u g h it m a y s o u n d c o m p l i c a t e d , this t e c h n i q u e keeps all y o u r t o - d o lists c o n v e n i e n t l y i n o n e p l a c e . T h e a p p r o p r i a t e l i s t is e a s i l y a c c e s s i b l e i n t h e t a s k - r e l a t e d d i r e c t o r y w h e n y o u are busy c o m p o s i n g letters, w r i t i n g m e m o s , or h a n d l i n g

personal

business.

About the discussion of hard links
tip Two kinds of links exist: hard links and symbolic (soft) links. Hard links are older and becoming
outdated. The section on hard links is marked as optional; you can skip it, although it discusses
inodes and gives you insight into the structure of the filesystem.

228

CHAPTER 6

THE LINUX FILESYSTEM

optional

HARD LINKS
A h a r d l i n k t o a f i l e a p p e a r s as a n o t h e r f i l e . I f t h e f i l e a p p e a r s i n t h e s a m e d i r e c t o r y
as t h e l i n k e d - t o f i l e , t h e l i n k s m u s t h a v e d i f f e r e n t f i l e n a m e s b e c a u s e t w o f i l e s i n t h e
same d i r e c t o r y c a n n o t have the same n a m e . Y o u c a n create a h a r d l i n k t o a file o n l y
f r o m w i t h i n the filesystem t h a t h o l d s the file.

In: CREATES A HARD LINK
T h e In ( l i n k ) u t i l i t y ( w i t h o u t t h e - s o r — s y m b o l i c o p t i o n ) c r e a t e s a h a r d l i n k t o a n
existing file using the f o l l o w i n g syntax:
In existing-file

new-link

The next c o m m a n d shows Z a c h m a k i n g the link s h o w n in Figure 6-14 by creating a
n e w l i n k n a m e d / h o m e / m a x / l e t t e r t o a n e x i s t i n g f i l e n a m e d draft i n Z a c h ' s

home

directory:
$ pwd
/home/zach
$ In d r a f t /home/max/letter
T h e n e w l i n k a p p e a r s i n t h e / h o m e / m a x d i r e c t o r y w i t h t h e f i l e n a m e letter. I n p r a c tice, M a x m a y need t o c h a n g e d i r e c t o r y p e r m i s s i o n s so Z a c h w i l l be able t o create
the l i n k . E v e n t h o u g h /home/max/letter appears i n M a x ' s d i r e c t o r y , Z a c h is the
o w n e r o f the file because he created i t .
T h e In u t i l i t y c r e a t e s a n a d d i t i o n a l p o i n t e r t o a n e x i s t i n g f i l e b u t i t d o e s not m a k e
a n o t h e r c o p y o f t h e f i l e . B e c a u s e t h e r e is o n l y o n e f i l e , t h e f i l e s t a t u s i n f o r m a t i o n — s u c h
as a c c e s s p e r m i s s i o n s , o w n e r , a n d t h e t i m e t h e f i l e w a s l a s t m o d i f i e d — i s t h e s a m e f o r
all links; o n l y the filenames differ. W h e n Z a c h m o d i f i e s /home/zach/draft, for e x a m p l e , M a x sees t h e c h a n g e s i n / h o m e / m a x / l e t t e r .

Figure 6-14

T w o l i n k s to the same file: /home/max/letter a n d /home/zach/draft

LINKS 2 2 9

cp VERSUS In
T h e f o l l o w i n g c o m m a n d s v e r i f y t h a t In d o e s n o t m a k e a n a d d i t i o n a l c o p y o f a f i l e .
C r e a t e a f i l e , u s e In t o m a k e a n a d d i t i o n a l l i n k t o t h e f i l e , c h a n g e t h e c o n t e n t s o f t h e
file t h r o u g h one l i n k , a n d verify the c h a n g e t h r o u g h the other l i n k :
$ cat f i l e _ a
Thi s i s f i le A.
$ In file_a file_b
$ cat f i l e _ b
Thi s i s f i le A.
$ vim.tiny file_b
$ cat
This i
$ cat
This i

file_b
s file B after
file_a
s file B after

t h e change.
t h e change.

I f y o u t r y t h e s a m e e x p e r i m e n t u s i n g c p i n s t e a d o f In a n d c h a n g e a copy

o f the file,

the difference b e t w e e n the t w o utilities w i l l b e c o m e clearer. O n c e y o u c h a n g e a

copy

o f a file, the t w o files are d i f f e r e n t :
$ cat f i l e _ c
T h i s i s f i l e C.
$ cp f i l e _ c f i l e _ d
$ cat f i l e _ d
T h i s i s f i l e C.
$ vim.tiny file_d
$ cat
This i
$ cat
This i
Is and link counts

file_d
s file D after
file_c
s f i l e C.

t h e change.

Y o u c a n u s e Is w i t h t h e - 1 o p t i o n , f o l l o w e d b y t h e n a m e s o f t h e f i l e s y o u w a n t t o
c o m p a r e , t o c o n f i r m t h a t the status i n f o r m a t i o n is the s a m e f o rt w o links t o the
s a m e file a n d is d i f f e r e n t f o r files t h a t are n o t l i n k e d . I nt h e f o l l o w i n g e x a m p l e ,

the

2 i n t h e l i n k s f i e l d ( j u s t t o t h e l e f t o f m a x ) s h o w s t h e r e a r e t w o l i n k s t o file_a a n d
file_b ( f r o m t h e p r e v i o u s
$ I s -1
-rw-r—r—
-rw-r--r
-rw-r--r
-rw-r--r

example):

file_a file_b
2 max p u b s
- - 2 max p u b s
- - 1 max p u b s
- - 1 max p u b s

file_c file_d
33 2 0 1 0 - 0 5 - 2 4 10:52
33 2 0 1 0 - 0 5 - 2 4 10:52
16 2010-05-24 10:55
33 2 0 1 0 - 0 5 - 2 4 1 0 : 5 7

file_a
file_b
file_c
file_d

A l t h o u g h i t i s e a s y t o g u e s s w h i c h f i l e s a r e l i n k e d t o o n e a n o t h e r i n t h i s e x a m p l e , Is
does n o t explicitly tell you.
Is and ¡nodes

U s e Is w i t h t h e - i o p t i o n t o d e t e r m i n e w i t h o u t a d o u b t w h i c h f i l e s a r e l i n k e d . T h e - i
o p t i o n l i s t s t h e inode

(page 1153) n u m b e r

f o r each file. A n i n o d e is the

control

s t r u c t u r e f o r a file. I f the t w o f i l e n a m e s h a v e the s a m e i n o d e n u m b e r , t h e y share the
s a m e c o n t r o l s t r u c t u r e a n d are l i n k s t o t h e s a m e file. C o n v e r s e l y , w h e n t w o
n a m e s h a v e d i f f e r e n t i n o d e n u m b e r s , t h e y are d i f f e r e n t files. T h e f o l l o w i n g

file-

example

230

CHAPTER 6

THE LINUX FILESYSTEM
s h o w s t h a t file_a a n d file_b h a v e t h e s a m e i n o d e n u m b e r a n d t h a t file_c a n d file_d
have different inode numbers:
$ Is - i file_a file_b file_c
3534 f i l e _ a
3534 f i l e _ b

file_d
5800 f i l e _ c

7328

file_d

A l l links t o a file are o f e q u a l value: T h e o p e r a t i n g system c a n n o t distinguish the
o r d e r i n w h i c h m u l t i p l e links w e r e created. W h e n a file has t w o links, y o u

can

r e m o v e e i t h e r o n e a n d still access t h e file t h r o u g h t h e r e m a i n i n g l i n k . Y o u c a n r e m o v e
t h e l i n k u s e d t o c r e a t e t h e f i l e , f o r e x a m p l e , a n d , as l o n g as o n e l i n k r e m a i n s , s t i l l
access t h e file t h r o u g h t h a t l i n k .

SYMBOLIC LINKS
I n a d d i t i o n t o h a r d l i n k s , L i n u x s u p p o r t s symbolic
symlinks.

links,

a l s o c a l l e d soft

links o r

A h a r d l i n k is a p o i n t e r t o a file (the d i r e c t o r y entry p o i n t s t o the inode),

w h e r e a s a s y m b o l i c l i n k i s a n indirect

p o i n t e r t o a file (the d i r e c t o r y entry c o n t a i n s

the p a t h n a m e o f the p o i n t e d - t o f i l e — a p o i n t e r t o the h a r d l i n k t o the file).
Advantages of

S y m b o l i c links w e r e developed because o f the limitations inherent i n h a r d links. Y o u can-

symbolic links

n o

t create a h a r d l i n k t o a directory, b u t y o u c a n create a s y m b o l i c l i n k t o a directory.

I n m a n y cases t h e L i n u x file h i e r a r c h y e n c o m p a s s e s several f i l e s y s t e m s .

Because

e a c h f i l e s y s t e m k e e p s s e p a r a t e c o n t r o l i n f o r m a t i o n ( t h a t is, s e p a r a t e i n o d e t a b l e s o r
f i l e s y s t e m s t r u c t u r e s ) f o r t h e files i t h o l d s , i t is n o t p o s s i b l e t o create h a r d

links

b e t w e e n files i n d i f f e r e n t filesystems. A s y m b o l i c l i n k c a n p o i n t t o a n y file, r e g a r d less o f w h e r e i t is l o c a t e d i n t h e f i l e s t r u c t u r e , b u t a h a r d l i n k t o a f i l e m u s t b e i n t h e
s a m e filesystem as t h e o t h e r h a r d link(s) t o t h e file. W h e n y o u create l i n k s

only

a m o n g files i n y o u r h o m e d i r e c t o r y , y o u w i l l n o t n o t i c e this l i m i t a t i o n .
A m a j o r a d v a n t a g e o f a s y m b o l i c l i n k is t h a t i t c a n p o i n t t o a n o n e x i s t e n t file. T h i s
a b i l i t y is useful i f y o u need a l i n k t o a file t h a t is p e r i o d i c a l l y r e m o v e d a n d r e created. A h a r d l i n k keeps p o i n t i n g t o a " r e m o v e d " file, w h i c h the l i n k keeps alive
even after a n e w file is created. I n contrast, a s y m b o l i c l i n k a l w a y s p o i n t s t o the
n e w l y created file a n d does n o t interfere w h e n y o u delete t h e o l d file. F o r e x a m p l e ,
a s y m b o l i c l i n k c o u l d p o i n t t o a file t h a t gets c h e c k e d i n a n d o u t u n d e r a s o u r c e
c o d e c o n t r o l s y s t e m , a . o f i l e t h a t is r e - c r e a t e d b y t h e C c o m p i l e r e a c h t i m e y o u r u n
make, o r a l o g f i l e t h a t is r e p e a t e d l y a r c h i v e d .
A l t h o u g h they are m o r e general t h a n h a r d links, s y m b o l i c links have some disadvantages. W h e r e a s all h a r d links t o a file have equal status, s y m b o l i c links d o n o t
h a v e t h e s a m e s t a t u s as h a r d l i n k s . W h e n a f i l e h a s m u l t i p l e h a r d l i n k s , i t i s a n a l o g o u s t o a p e r s o n h a v i n g m u l t i p l e f u l l l e g a l n a m e s , as m a n y m a r r i e d w o m e n d o . I n
contrast, symbolic links are analogous t onicknames. A n y o n e can have one o r m o r e
n i c k n a m e s , b u t these n i c k n a m e s h a v e a lesser status t h a n l e g a l n a m e s . T h e f o l l o w i n g sections describe some o f the peculiarities o fs y m b o l i c links.

In: CREATES SYMBOLIC LINKS
T h e In u t i l i t y w i t h t h e — s y m b o l i c ( o r - s ) o p t i o n c r e a t e s a s y m b o l i c l i n k . T h e f o l l o w i n g
e x a m p l e c r e a t e s a s y m b o l i c l i n k / t m p / s 3 t o t h e f i l e sum i n M a x ' s h o m e d i r e c t o r y . W h e n

LINKS 2 3 1
y o u u s e a n Is - 1 c o m m a n d t o l o o k a t t h e s y m b o l i c l i n k , Is d i s p l a y s t h e n a m e o f t h e l i n k
a n d t h e n a m e o f t h e f i l e i t p o i n t s t o . T h e f i r s t c h a r a c t e r o f t h e l i s t i n g is 1 ( f o r l i n k ) .
$ I n --symbolic /home/max/sum /tmp/s3
$ I s -1 /home/max/sum /tmp/s3
-rw-rw-r-1 max max 3 8 2 0 1 0 - 0 6 - 1 2 0 9 : 5 1 / h o m e / m a x / s u m
lrwxrwxrwx
1 max max 1 4 2 0 1 0 - 0 6 - 1 2 0 9 : 5 2 / t m p / s 3 - > / h o m e / m a x / s u m
$ cat /tmp/s3
T h i s i s sum.
T h e sizes a n d t i m e s o f t h e last m o d i f i c a t i o n s o f t h e t w o files a r e d i f f e r e n t . U n l i k e a
h a r d l i n k , a s y m b o l i c l i n k t o a f i l e d o e s n o t h a v e t h e s a m e s t a t u s i n f o r m a t i o n as t h e
file itself.
Y o u c a n a l s o u s e In t o c r e a t e a s y m b o l i c l i n k t o a d i r e c t o r y . W h e n

y o u use t h e

— s y m b o l i c o p t i o n , In w o r k s a s e x p e c t e d w h e t h e r t h e f i l e y o u a r e c r e a t i n g a l i n k t o
is a n o r d i n a r y f i l e o r a d i r e c t o r y .

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 the relative pathname is relative to the directory that
the link was created in (not the directory the link was created from). In the following example, the
link points to the file named sum in the /tmp directory. Because no such file exists, cat gives an
error message:
$ pwd
/home/max
$ I n --symbolic sum /tmp/s4
$ I s -1 sum /tmp/s4
l r w x r w x r w x 1 max max
3 2010-06-12 10:13 /tmp/s4
- r w - r w - r - - 1 max max
3 8 2 0 1 0 - 0 6 - 1 2 0 9 : 5 1 sum
$ cat /tmp/s4
c a t : / t m p / s 4 : No s u c h f i l e o r d i r e c t o r y

- > sum

optional c d A N D S Y M B O L I C L I N K S
When

y o u use a s y m b o l i c

link

as a n a r g u m e n t

t o cdt o c h a n g e d i r e c t o r i e s , t h e

results can be confusing, particularly i f y o u did n o t realize that y o u were using a
symbolic

link.

I f y o u u s e cd t o c h a n g e t o a d i r e c t o r y t h a t i s r e p r e s e n t e d b y a s y m b o l i c l i n k , t h e pwd
shell

builtin

(page 261)

lists

thename

o f the symbolic

link.

T h e pwd

utility

(/bin/pwd) lists t h e n a m e o f t h e l i n k e d - t o d i r e c t o r y , n o t t h e l i n k , regardless o f h o w
y o u got there.
$ I n - s /home/max/grades / t m p / g r a d e s . o l d
$ pwd
/home/max
$ cd / t m p / g r a d e s . o l d
$ pwd
/tmp/grades.old
$ /bin/pwd
/home/max/g rades

232

CHAPTER 6

THE LINUX FILESYSTEM
W h e n y o u change directories back t o the parent, y o u end u pi n the directory
ing the symbolic

hold-

link:

$ cd . .
$ pwd
/tmp
$ /bin/pwd
/tmp

rm: REMOVES A LINK
W h e n y o u create a file, there is o n e h a r d l i n k t o i t . Y o u c a n t h e n delete the file or,
u s i n g L i n u x t e r m i n o l o g y , r e m o v e t h e l i n k w i t h t h e rm u t i l i t y . W h e n y o u r e m o v e

the

last h a r d l i n k t o a file, y o u c a n n o l o n g e r access t h e i n f o r m a t i o n s t o r e d t h e r e a n d t h e
o p e r a t i n g s y s t e m releases the space the file o c c u p i e d o n the disk f o ruse b y o t h e r
files. T h i s space is released e v e n i f s y m b o l i c l i n k s t o t h e file r e m a i n . W h e n t h e r e is
m o r e t h a n o n e h a r d l i n k t o a file, y o u c a n r e m o v e a h a r d l i n k a n d still access t h e file
f r o m any remaining link. Unlike D O S

and W i n d o w s , L i n u x does not provide a n

easy w a y t o u n d e l e t e a file o n c e y o u h a v e r e m o v e d it. A s k i l l e d hacker, h o w e v e r , c a n
s o m e t i m e s piece the file together w i t h t i m e a n d effort.
W h e n y o u r e m o v e a l lh a r d l i n k s t o a file, y o u w i l l n o t b e a b l e t o access t h e

file

t h r o u g h a s y m b o l i c l i n k . I n t h e f o l l o w i n g e x a m p l e , cat r e p o r t s t h a t t h e f i l e total
does n o t exist because i t is a s y m b o l i c l i n k t o a file t h a t has been r e m o v e d :
$ I s -1 sum
- r w - r - - r - - 1 max p u b s 9 8 1
2 0 1 0 - 0 5 - 2 4 11:05 sum
$ I n - s sum t o t a l
$ rm sum
$ cat t o t a l
c a t : t o t a l : No s u c h f i l e o r d i r e c t o r y
$ I s -1 t o t a l
l r w x r w x r w x 1 max p u b s 6 2 0 1 0 - 0 5 - 2 4 1 1 : 0 9 t o t a l - > sum
W h e n y o u r e m o v e a file, be sure t o r e m o v e all s y m b o l i c l i n k s t o it. R e m o v e a s y m b o l i c l i n k i n t h e s a m e w a y y o u r e m o v e o t h e r files:
$ rm t o t a l

CHAPTER

SUMMARY
L i n u x has a hierarchical, o r treelike, file structure t h a t m a k e s i t possible t o o r g a n i z e
files so y o u c a n f i n d t h e m q u i c k l y a n d easily. T h e file s t r u c t u r e c o n t a i n s d i r e c t o r y files
a n d o r d i n a r y files. D i r e c t o r i e s c o n t a i n o t h e r files, i n c l u d i n g o t h e r d i r e c t o r i e s ; o r d i n a r y
files g e n e r a l l y c o n t a i n t e x t , p r o g r a m s , o r i m a g e s . T h e a n c e s t o r o f all files is t h e r o o t
directory a n d isrepresented b y / s t a n d i n g alone o r a tthe left end o fa p a t h n a m e .

CHAPTER SUMMARY

233

M o s t L i n u x f i l e s y s t e m s s u p p o r t 2 5 5 - c h a r a c t e r f i l e n a m e s . N o n e t h e l e s s , i t is a g o o d
idea t o keep filenames simple a n d intuitive. F i l e n a m e extensions c a n help m a k e filenames more meaningful.
W h e n y o u are logged in, y o u are always associated w i t h a w o r k i n g directory. Y o u r
h o m e d i r e c t o r y is t h e w o r k i n g d i r e c t o r y f r o m t h e t i m e y o u l o g i n u n t i l y o u use cd t o
change directories.
A n absolute p a t h n a m e starts w i t h the r o o t d i r e c t o r y a n d contains all the filenames
t h a t trace a p a t h t o a g i v e n file. T h e p a t h n a m e starts w i t h a slash, r e p r e s e n t i n g t h e
r o o t d i r e c t o r y , a n d c o n t a i n s a d d i t i o n a l slashes f o l l o w i n g all t h e d i r e c t o r i e s i n t h e
p a t h , e x c e p t f o r t h e last d i r e c t o r y i n t h e case o f a p a t h t h a t p o i n t s t o a d i r e c t o r y file.
A r e l a t i v e p a t h n a m e is s i m i l a r t o a n a b s o l u t e p a t h n a m e b u t t r a c e s t h e p a t h s t a r t i n g
f r o m t h e w o r k i n g d i r e c t o r y . A s i m p l e f i l e n a m e is t h e l a s t e l e m e n t o f a p a t h n a m e a n d
is a f o r m o f a r e l a t i v e p a t h n a m e ; i t r e p r e s e n t s a f i l e i n t h e w o r k i n g d i r e c t o r y .
A L i n u x filesystem c o n t a i n s m a n y i m p o r t a n t directories, i n c l u d i n g /usr/bin, w h i c h
s t o r e s m o s t o f t h e L i n u x u t i l i t y c o m m a n d s , a n d /dev, w h i c h s t o r e s d e v i c e f i l e s , m a n y o f
which

represent

physical

pieces

of

hardware.

An

important

standard

file

is

/ e t c / p a s s w d ; i t c o n t a i n s i n f o r m a t i o n a b o u t u s e r s , s u c h as e a c h u s e r ' s ID a n d f u l l n a m e .
A m o n g t h e a t t r i b u t e s a s s o c i a t e d w i t h e a c h f i l e a r e access p e r m i s s i o n s . T h e y d e t e r m i n e w h o c a n access t h e f i l e a n d h o w t h e file m a y b e accessed. T h r e e g r o u p s

of

users c a n p o t e n t i a l l y access t h e file: t h e o w n e r , t h e m e m b e r s o f a g r o u p , a n d

all

o t h e r users. A n o r d i n a r y file c a n be accessed i n t h r e e w a y s : read, w r i t e , a n d execute.
T h e Is u t i l i t y w i t h t h e - 1 o p t i o n d i s p l a y s t h e s e p e r m i s s i o n s . F o r d i r e c t o r i e s , e x e c u t e
access is r e d e f i n e d t o m e a n t h a t t h e d i r e c t o r y c a n b e s e a r c h e d .
T h e o w n e r o f a file o r a user w o r k i n g w i t h root privileges c a n use t h e chmod u t i l i t y
t o c h a n g e t h e access p e r m i s s i o n s o f a file. T h i s u t i l i t y specifies r e a d , w r i t e , a n d exec u t e p e r m i s s i o n s f o r t h e file's o w n e r , t h e g r o u p , a n d a l l o t h e r users o n t h e system.
Access C o n t r o l Lists ( A C L s ) p r o v i d e f i n e r - g r a i n e d c o n t r o l over w h i c h users
access s p e c i f i c d i r e c t o r i e s a n d files t h a n d o t r a d i t i o n a l L i n u x p e r m i s s i o n s .

can

Using

A C L s y o u c a n s p e c i f y t h e w a y s i n w h i c h e a c h o f s e v e r a l users c a n access a d i r e c t o r y
o r file. F e w utilities preserve A C L s w h e n w o r k i n g w i t h files.
A n o r d i n a r y f i l e s t o r e s u s e r d a t a , s u c h as t e x t u a l i n f o r m a t i o n , p r o g r a m s , o r i m a g e s .
A d i r e c t o r y is a s t a n d a r d - f o r m a t d i s k f i l e t h a t s t o r e s i n f o r m a t i o n , i n c l u d i n g n a m e s ,
a b o u t o r d i n a r y files a n d o t h e r d i r e c t o r y files. A n i n o d e is a d a t a s t r u c t u r e , s t o r e d o n
d i s k , t h a t d e f i n e s a file's e x i s t e n c e a n d is i d e n t i f i e d b y a n i n o d e n u m b e r . A

directory

relates each o f the filenames it stores t o a n inode.
A l i n k is a p o i n t e r t o a f i l e . Y o u c a n h a v e s e v e r a l l i n k s t o a f i l e so y o u c a n s h a r e t h e
file w i t h o t h e r users o r h a v e the file a p p e a r i n m o r e t h a n o n e d i r e c t o r y . Because o n l y
one c o p y o f a file w i t h m u l t i p l e links exists, c h a n g i n g the file t h r o u g h a n y o n e l i n k
causes the changes t o a p p e a r i n all the links. H a r d l i n k s c a n n o t l i n k directories o r
span filesystems, whereas symbolic links can.

234

CHAPTER 6

THE LINUX FILESYSTEM
Table 6-3 summarizes the utilities introduced in this chapter.

Table 6-3

Utilities Introduced In Chapter 6

Utility

Function

cd

Associates you with another working directory (page 209)

chmod

Changes access permissions on a file (page 216)

getfacl

Displays a file's ACL (page 222)

In

Makes a link to an existing file (page 228)

mkdir

Creates a directory (page 208)

pwd

Displays the pathname of the working directory (page 204)

rmdir

Deletes a directory (page 210)

setfacl

Modifies a file's ACL (page 222)

EXERCISES
1. I s e a c h o f t h e f o l l o w i n g a n a b s o l u t e p a t h n a m e , a r e l a t i v e p a t h n a m e , o r a
simple filename?
a. m i l k _ c o
b. correspond/business/milk_co
c. / h o m e / m a x
d. /home/max/literature/promo
e. . .
f . letter. 0 6 1 0
2. L i s t the c o m m a n d s y o u c a n use t o p e r f o r m these o p e r a t i o n s :
a. M a k e y o u r h o m e d i r e c t o r y t h e w o r k i n g d i r e c t o r y
b. I d e n t i f y t h e w o r k i n g d i r e c t o r y
3 . I f t h e w o r k i n g d i r e c t o r y is / h o m e / m a x w i t h a s u b d i r e c t o r y n a m e d literature, g i v e t h r e e sets o f c o m m a n d s t h a t y o u c a n u s e t o c r e a t e a s u b d i r e c t o r y
n a m e d classics u n d e r literature. A l s o g i v e s e v e r a l s e t s o f c o m m a n d s

you

c a n u s e t o r e m o v e t h e classics d i r e c t o r y a n d i t s c o n t e n t s .
4 . T h e df u t i l i t y d i s p l a y s a l l m o u n t e d f i l e s y s t e m s a l o n g w i t h i n f o r m a t i o n
a b o u t e a c h . U s e t h e df u t i l i t y w i t h t h e - h ( h u m a n - r e a d a b l e ) o p t i o n t o
answer the f o l l o w i n g questions.
a. H o w m a n y f i l e s y s t e m s a r e m o u n t e d o n y o u r L i n u x s y s t e m ?
b. W h i c h filesystem stores y o u r h o m e d i r e c t o r y ?
c . A s s u m i n g t h a t y o u r a n s w e r t o e x e r c i s e 4 a is t w o o r m o r e , a t t e m p t t o
create a h a r d l i n k t o a file o n a n o t h e r filesystem. W h a t error message d o

EXERCISES
y o u get? W h a t h a p p e n s w h e n y o u a t t e m p t t o create a s y m b o l i c l i n k t o
the file instead?
5 . S u p p o s e y o u h a v e a f i l e t h a t is l i n k e d t o a f i l e o w n e d b y a n o t h e r user. H o w
c a n y o u ensure t h a t changes t o the file are n o l o n g e r shared?
6. Y o u s h o u l d h a v e r e a d p e r m i s s i o n f o r t h e /etc/passwd file. T o a n s w e r t h e
f o l l o w i n g q u e s t i o n s , u s e cat o r less t o d i s p l a y / e t c / p a s s w d . L o o k a t t h e
fields o f i n f o r m a t i o n i n /etc/passwd f o r the users o n y o u r system.
a. W h i c h c h a r a c t e r is u s e d t o s e p a r a t e f i e l d s i n /etc/passwd?
b. H o w m a n y fields are used t o describe each user?
c. H o w m a n y u s e r s a r e o n t h e l o c a l s y s t e m ?
d . H o w m a n y d i f f e r e n t l o g i n s h e l l s a r e i n u s e o n y o u r s y s t e m ? (Hint:

Look

at the last field.)
e. T h e s e c o n d f i e l d o f / e t c / p a s s w d s t o r e s u s e r p a s s w o r d s i n e n c o d e d f o r m .
I f t h e p a s s w o r d f i e l d c o n t a i n s a n x, y o u r s y s t e m uses s h a d o w p a s s w o r d s
a n d stores t h e e n c o d e d p a s s w o r d s elsewhere. D o e s y o u r s y s t e m use
shadow passwords?
7. If /home/zach/draft a n d /home/max/letter are l i n k s t o the same file a n d
the f o l l o w i n g sequence o f events occurs, w h a t w i l l be the date i n the o p e n i n g o f the letter?
a. M a x g i v e s t h e c o m m a n d vim.tiny letter.
b . Z a c h g i v e s t h e c o m m a n d vim.tiny draft.
c. Z a c h c h a n g e s t h e d a t e i n t h e o p e n i n g o f t h e l e t t e r t o J a n u a r y 3 1 , 2 0 1 0 ,
w r i t e s t h e f i l e , a n d e x i t s f r o m vim.
d. M a x c h a n g e s t h e d a t e t o F e b r u a r y 1, 2 0 1 0 , w r i t e s t h e file, a n d exits
f r o m vim.
8. S u p p o s e a user b e l o n g s t o a g r o u p t h a t has all p e r m i s s i o n s o n a file n a m e d
jobs_list, b u t t h e u s e r , as t h e o w n e r o f t h e f i l e , h a s n o p e r m i s s i o n s .
Describe w h i c h operations, if any, the user/owner c a n p e r f o r m o n
jobs_list. W h i c h c o m m a n d c a n t h e u s e r / o w n e r g i v e t h a t w i l l g r a n t t h e
u s e r / o w n e r a l l p e r m i s s i o n s o n t h e file?
9 . D o e s t h e r o o t d i r e c t o r y h a v e a n y s u b d i r e c t o r i e s y o u c a n n o t s e a r c h as a n
o r d i n a r y user? D o e s the r o o t d i r e c t o r y h a v e a n y s u b d i r e c t o r i e s y o u c a n n o t
r e a d as a r e g u l a r u s e r ? E x p l a i n .
10. A s s u m e y o u are given the directory structure s h o w n i n Figure 6 - 2 o n
page 2 0 1 and the f o l l o w i n g directory permissions:
d--x--x—
drwxr-xr-x

3 z a c h p u b s 512 2 0 1 0 - 0 3 - 1 0 1 5 : 1 6
2 z a c h p u b s 512 2 0 1 0 - 0 3 - 1 0 1 5 : 1 6

business
business/mi1k_co

For each category of permissions—owner, group, and o t h e r — w h a t happens w h e n y o u r u n each of the f o l l o w i n g c o m m a n d s ? A s s u m e the w o r k i n g

235

236

CHAPTER 6

THE LINUX FILESYSTEM
d i r e c t o r y is t h e p a r e n t o f c o r r e s p o n d a n d t h a t t h e f i l e cheese_co is r e a d a b l e
by everyone.
a. cd c o r r e s p o n d / b u s i n e s s / m i l k _ c o
b . Is -1 c o r r e s p o n d / b u s i n e s s
c . cat c o r r e s p o n d / b u s i n e s s / c h e e s e _ c o

ADVANCED

EXERCISES

1 1 . W h a t is a n i n o d e ? W h a t h a p p e n s t o t h e i n o d e w h e n y o u m o v e a f i l e w i t h i n
a filesystem?
12. W h a t does the . . entry i n a d i r e c t o r y p o i n t to? W h a t does this entry p o i n t
t o i nthe r o o t (/) directory?
13. H o w c a n y o u create a file n a m e d -i? W h i c h t e c h n i q u e s d o n o t w o r k , a n d
w h y d o t h e y n o t w o r k ? H o w c a n y o u r e m o v e t h e file n a m e d -i?
1 4 . S u p p o s e t h e w o r k i n g d i r e c t o r y c o n t a i n s a s i n g l e f i l e n a m e d andor. W h a t
error message d o y o u get w h e n y o u r u n the f o l l o w i n g c o m m a n d line?
$ mv andor and\/or
U n d e r w h a t circumstances isi t possible t or u n the c o m m a n d w i t h o u t p r o ducing an error?
1 5 . T h e Is - i c o m m a n d d i s p l a y s a f i l e n a m e p r e c e d e d b y t h e i n o d e n u m b e r o f
the file (page 2 2 9 ) . W r i t e a c o m m a n d t o o u t p u t i n o d e / f i l e n a m e pairs for
t h e f i l e s i n t h e w o r k i n g d i r e c t o r y , s o r t e d b y i n o d e n u m b e r . (Hint:

Use a

pipe.)
16. D o y o u t h i n k t h e s y s t e m a d m i n i s t r a t o r has access t o a p r o g r a m t h a t c a n
d e c o d e u s e r p a s s w o r d s ? W h y o r w h y n o t ? (See e x e r c i s e 6 . )
17. Is i t p o s s i b l e t o d i s t i n g u i s h a file f r o m a h a r d l i n k t o a file? T h a t is, g i v e n a
f i l e n a m e , c a n y o u t e l l w h e t h e r i t w a s c r e a t e d u s i n g a n In c o m m a n d ?
Explain.
18. E x p l a i n the e r r o r messages displayed i n the f o l l o w i n g sequence o f c o m m a n d s :
$ I s -1
total

1

drwxrwxr-x

2 max p u b s 1 0 2 4 2 0 1 0 - 0 3 - 0 2

$ I s dirtmp
$ rmdir dirtmp
rmdir:

dirtmp:

$ rm di rtmp/*
r m : No m a t c h .

Directory

n o t empty

17:57

dirtmp

7
THE SHELL
IN THIS CHAPTER

T h i s c h a p t e r takes a close l o o k at the shell a n d explains h o w t o
use s o m e o f its features. F o r e x a m p l e , it discusses

The Command Line

238

command-

line syntax. It also describes h o w the shell processes a

com-

m a n d line and initiates execution of a p r o g r a m . I n a d d i t i o n the

Standard Input and Standard
Output

243

chapter explains h o w to redirect input to and output f r o m a

Pipes

251

c o m m a n d , construct pipes a n d filters o n the c o m m a n d line, a n d

Running a Command in the
Background

254

kill: Aborting a Background Job . . 255
Filename Generation/Pathname
Expansion

256

Builtins

261

r u n a c o m m a n d in the background. T h e final section

covers

f i l e n a m e e x p a n s i o n a n d e x p l a i n s h o w y o u c a n use t h i s f e a t u r e
in your everyday w o r k .
T h e exact w o r d i n g o f the shell o u t p u t differs f r o m shell t o shell:
W h a t y o u r shell displays m a y differ slightly f r o m w h a t appears
i n this b o o k . R e f e r t o C h a p t e r 9 f o r m o r e i n f o r m a t i o n o n bash
and to Chapter 2 7 for information on writing and

executing

bash shell scripts.

237

238

CHAPTER 7

THE SHELL

THE C O M M A N D

LINE

T h e shell executes a p r o g r a m w h e n y o u give i t a c o m m a n d

i n response t o its

p r o m p t . F o r e x a m p l e , w h e n y o u g i v e t h e Is c o m m a n d , t h e s h e l l e x e c u t e s t h e u t i l i t y
p r o g r a m n a m e d Is. Y o u c a n c a u s e t h e s h e l l t o e x e c u t e o t h e r t y p e s o f p r o g r a m s —
s u c h as s h e l l s c r i p t s , a p p l i c a t i o n p r o g r a m s , a n d p r o g r a m s y o u h a v e w r i t t e n — i n t h e
same way. T h e line that contains the c o m m a n d , i n c l u d i n g any arguments, is called
t h e command

line. T h i s b o o k u s e s t h e t e r m command

to refer to b o t h the characters

y o u type o n the c o m m a n d line and the p r o g r a m that action invokes.

SYNTAX
C o m m a n d - l i n e syntax dictates the o r d e r i n g a n d separation o f the elements o n a
c o m m a n d l i n e . W h e n y o u p r e s s t h e RETURN k e y a f t e r e n t e r i n g a c o m m a n d , t h e s h e l l
scans t h e c o m m a n d line f o r p r o p e r s y n t a x . T h e s y n t a x f o r a basic c o m m a n d l i n e is
command

[argl]

[arg2]

... [argn]

RETURN

O n e o r m o r e SPACEs m u s t s e p a r a t e e l e m e n t s o n t h e c o m m a n d l i n e . T h e command
t h e n a m e o f t h e c o m m a n d , argl

t h r o u g h argn

is

a r e a r g u m e n t s , a n d RETURN i s t h e k e y -

s t r o k e t h a t terminates all c o m m a n d lines. T h e brackets i nthe c o m m a n d - l i n e s y n t a x
indicate that the a r g u m e n t s they enclose are o p t i o n a l . N o t all c o m m a n d s
arguments: Some commands

require

d o not allow arguments; other commands allow a

variable n u m b e r o f arguments; a n d still others require a specific n u m b e r o f arguments. O p t i o n s , a special k i n d o f a r g u m e n t , are usually preceded b y one o r t w o
h y p h e n s (also called a dash or m i n u s sign: - ) .

COMMAND NAME
Usage message

S o m e useful L i n u x c o m m a n d lines consist o fo n l y the n a m e o f the c o m m a n d w i t h o u t
a n y a r g u m e n t s . F o r e x a m p l e , Is b y i t s e l f l i s t s t h e c o n t e n t s o f t h e w o r k i n g d i r e c t o r y .
C o m m a n d s that require arguments typically give a short error message, called a
usage

message,

w h e n y o u use t h e m w i t h o u t a r g u m e n t s , w i t h i n c o r r e c t a r g u m e n t s , o r

w i t h the w r o n g number o f arguments.

ARGUMENTS
O n t h e c o m m a n d l i n e e a c h s e q u e n c e o f n o n b l a n k c h a r a c t e r s i s c a l l e d a token
word.

A n argument

or

i s a t o k e n , s u c h as a f i l e n a m e , s t r i n g o f t e x t , n u m b e r , o r o t h e r

o b j e c t t h a t a c o m m a n d acts o n . F o r e x a m p l e , t h e a r g u m e n t t o a

vim o r emacs

com-

m a n d is the n a m e o f the file y o u w a n t t o edit.
T h e f o l l o w i n g c o m m a n d l i n e s h o w s cp c o p y i n g t h e f i l e n a m e d t e m p t o t e m p c o p y :
$ cp temp tempcopy
A r g u m e n t s a r e n u m b e r e d s t a r t i n g w i t h t h e c o m m a n d i t s e l f , w h i c h is a r g u m e n t z e r o . I n
t h i s e x a m p l e , cp i s a r g u m e n t z e r o , t e m p i s a r g u m e n t o n e , a n d t e m p c o p y i s a r g u m e n t
t w o . T h e cp u t i l i t y r e q u i r e s a t l e a s t t w o a r g u m e n t s o n t h e c o m m a n d l i n e . A r g u m e n t
o n e is t h e n a m e o f a n e x i s t i n g f i l e . A r g u m e n t t w o is t h e n a m e o f t h e f i l e t h a t cp is c r e a t i n g o r o v e r w r i t i n g . H e r e the a r g u m e n t s are n o t optional; b o t h arguments m u s t b e

THE COMMAND LINE 2 3 9

$ Is
hold
mark
names
oldstuff
temp
zach
house
max o f f i c e
personal
test
$ Is - r
zach
temp
o l d s t u f f names
mark
hold
test
personal
office
max
house
$ I s -x
hold
house
m a r k max
names o f f i ce
oldstuff
personal
temp t e s t
zach
$ Is -rx
zach
test
temp
personal
o l d s t u f f o f f i ce
names m a x m a r k
house
hold

Figure 7-1

Using options

present for the c o m m a n d t ow o r k . W h e n y o u do n o t supply the right n u m b e r o r k i n d
o f a r g u m e n t s , cp d i s p l a y s a u s a g e m e s s a g e . T r y t y p i n g cp a n d t h e n p r e s s i n g RETURN.

OPTIONS
A n option

is a n a r g u m e n t t h a t m o d i f i e s the effects o f a c o m m a n d . Y o u c a n fre-

quently specify m o r e t h a n one o p t i o n , m o d i f y i n g the c o m m a n d i n several different
ways. O p t i o n s are specific to a n d interpreted b y the p r o g r a m that the c o m m a n d line
calls, n o t b y the shell.
B y c o n v e n t i o n options are separate arguments that f o l l o w the n a m e of the c o m m a n d
a n d u s u a l l y p r e c e d e o t h e r a r g u m e n t s , s u c h as f i l e n a m e s . M o s t u t i l i t i e s r e q u i r e y o u t o
p r e f i x o p t i o n s w i t h a s i n g l e h y p h e n . H o w e v e r , t h i s r e q u i r e m e n t is s p e c i f i c t o t h e u t i l i t y a n d n o t the shell. G N U p r o g r a m o p t i o n s are f r e q u e n t l y p r e c e d e d b y t w o h y p h e n s
i n a r o w . F o r e x a m p l e , —help generates a (sometimes extensive) usage message.
F i g u r e 7 - 1 f i r s t s h o w s t h e o u t p u t o f a n Is c o m m a n d w i t h o u t a n y o p t i o n s . B y d e f a u l t
Is l i s t s t h e c o n t e n t s o f t h e w o r k i n g d i r e c t o r y i n a l p h a b e t i c a l o r d e r , v e r t i c a l l y s o r t e d
i n c o l u m n s . N e x t the - r (reverse o r d e r ; because this is a G N U utility, y o u c a n also
u s e — r e v e r s e ) o p t i o n c a u s e s t h e Is u t i l i t y t o d i s p l a y t h e l i s t o f f i l e s i n r e v e r s e a l p h a b e t i c a l o r d e r , s t i l l s o r t e d i n c o l u m n s . T h e - x o p t i o n c a u s e s Is t o d i s p l a y t h e l i s t o f
files i n h o r i z o n t a l l y s o r t e d r o w s .
Combining options

W h e n y o u n e e d t o use several o p t i o n s , y o u c a n u s u a l l y g r o u p m u l t i p l e single-letter
o p t i o n s i n t o o n e a r g u m e n t t h a t s t a r t s w i t h a s i n g l e h y p h e n ; d o n o t p u t SPACES b e t w e e n
the options. Y o u c a n n o t c o m b i n e o p t i o n s t h a t are preceded b y t w o h y p h e n s i n this
way. Specific rules for c o m b i n i n g o p t i o n s d e p e n d o n the p r o g r a m y o u are r u n n i n g .
F i g u r e 7 - 1 s h o w s b o t h t h e - r a n d - x o p t i o n s w i t h t h e Is u t i l i t y . T o g e t h e r t h e s e o p t i o n s
generate a list o f filenames i n h o r i z o n t a l l y sorted c o l u m n s , i n reverse

alphabetical

o r d e r . M o s t u t i l i t i e s a l l o w y o u t o l i s t o p t i o n s i n a n y o r d e r ; t h u s Is - x r p r o d u c e s t h e
s a m e r e s u l t s as Is - r x . T h e c o m m a n d Is - x - r a l s o g e n e r a t e s t h e s a m e l i s t .
Option arguments

S o m e u t i l i t i e s h a v e o p t i o n s t h a t t h e m s e l v e s r e q u i r e a r g u m e n t s . F o r e x a m p l e , t h e gcc
utility has a - o o p t i o n t h a t m u s t be f o l l o w e d b y the n a m e y o u w a n t t ogive the exec u t a b l e f i l e t h a t gcc g e n e r a t e s . T y p i c a l l y a n a r g u m e n t t o a n o p t i o n is s e p a r a t e d f r o m
i t s o p t i o n l e t t e r b y a SPACE:
$ gcc -o prog prog.c

240

CHAPTER 7

THE S H E 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 the df -h (disk free) and Is -Ih commands.
Arguments that start
with a hyphen

A n o t h e r c o n v e n t i o n a l l o w s u t i l i t i e s t o w o r k w i t h a r g u m e n t s , s u c h as f i l e n a m e s , t h a t
start w i t h a h y p h e n . I f a file's n a m e is-1, t h e f o l l o w i n g c o m m a n d is a m b i g u o u s :
$ I s -1
T h i s c o m m a n d c o u l d m e a n a l o n g l i s t i n g o f all files i n t h e w o r k i n g d i r e c t o r y o r a
l i s t i n g o f t h e f i l e n a m e d - 1 . I t i s i n t e r p r e t e d as t h e f o r m e r . A v o i d c r e a t i n g files w h o s e
names begin w i t h hyphens. I f y o u d o create them, m a n y utilities f o l l o w the convention that a — argument (two consecutive hyphens) indicates the end o fthe

options

(and the b e g i n n i n g o fthe arguments). T o d i s a m b i g u a t e the c o m m a n d , y o u c a n type
$ I s

1

Y o u c a n use a n alternative f o r m a t i n w h i c h the p e r i o d refers t othe w o r k i n g
a n d the slash indicates t h a t the n a m e refers t o a file i nthe w o r k i n g

directory

directory:

$ I s . / - I
A s s u m i n g t h a t y o u are w o r k i n g i n the / h o m e / m a x directory, the preceding

command

is f u n c t i o n a l l y e q u i v a l e n t t o
$ I s /home/max/-1
T h e f o l l o w i n g c o m m a n d displays a l o n g l i s t i n g o fthis file:
$ I s -1

1

These are conventions, n o t hard-and-fast rules, a n d a n u m b e r o futilities d o n o t foll o w t h e m (e.g., find). F o l l o w i n g s u c h c o n v e n t i o n s i s a g o o d i d e a ; i t b e c o m e s

much

easier f o rusers t o w o r k w i t h y o u r p r o g r a m . W h e n y o u w r i t e shell p r o g r a m s
require options, follow the L i n u x option

that

conventions.

PROCESSING THE COMMAND LINE
As y o u enter a c o m m a n d line, the L i n u x tty device driver (part o fthe L i n u x

kernel)

e x a m i n e s e a c h c h a r a c t e r t o see w h e t h e r i t m u s t t a k e i m m e d i a t e a c t i o n . W h e n
p r e s s C0NTR0L-H ( t o e r a s e a c h a r a c t e r ) o r C0NTR0L-U ( t o k i l l a l i n e ) , t h e d e v i c e
i m m e d i a t e l y a d j u s t s t h e c o m m a n d l i n e a s r e q u i r e d ; t h e s h e l l n e v e r sees t h e

charac-

t e r s ) y o u erased o rthe line y o u killed. O f t e n a similar adjustment occurs w h e n
p r e s s C0NTR0L-W ( t o e r a s e a w o r d ) . W h e n t h e c h a r a c t e r y o u e n t e r e d d o e s n o t

you

driver
you

require

i m m e d i a t e action, the device driver stores the character i n a buffer a n d waits f o r
a d d i t i o n a l c h a r a c t e r s . W h e n y o u p r e s s RETURN, t h e d e v i c e d r i v e r p a s s e s t h e
line t o the shell for
Parsing the
command line

command

processing.

W h e n t h e s h e l l p r o c e s s e s a c o m m a n d l i n e , i t l o o k s a t t h e l i n e as a w h o l e a n d

parses

( b r e a k s ) i t i n t o its c o m p o n e n t p a r t s ( F i g u r e 7 - 2 ) . N e x t t h e shell l o o k s f o r t h e n a m e o f
the c o m m a n d . U s u a l l y the n a m e o f the c o m m a n d is the first i t e m o nthe

command

THE COMMAND LINE

241

T h e — h e l p 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 4) accept this option. An example
follows.
$ bzip2 --help
bzip2, a block-sorting
usage:

bunzip2

file

[flags

-h
-d
-z
-k
-f

--help
--decompress
--compress
--keep
--force

If

i n v o k e d as
as
as

compressor.

and i n p u t

Version

files

i n any

p r i n t t h i s message
force decompression
force compression
keep ( d o n ' t d e l e t e ) i n p u t
overwrite existing output

'bzip2', default action is
'bunzip2',
default action
'bzcat', default action is

1.0.5,

10-Dec-2007.

order]

files
files

t o compress.
i s t o decompress,
t o decompress t o s t d o u t .

line after the p r o m p t ( a r g u m e n t zero). T h e shell takes the first characters o n the c o m m a n d l i n e u p t o t h e f i r s t b l a n k (TAB o r SPACE) a n d t h e n l o o k s f o r a c o m m a n d w i t h t h a t
name. T h e c o m m a n d n a m e (the first token) can be specified o n the c o m m a n d

Get first word
and save as
command name

no

Execute program - »

Get more
of the
command line

Display

not found

yes

Issue prompt

Figure 7-2

».

Processing the c o m m a n d

line

line

242

CHAPTER 7

THE SHELL
e i t h e r as a s i m p l e f i l e n a m e o r as a p a t h n a m e . F o r e x a m p l e , y o u c a n c a l l t h e Is c o m m a n d i n either of the f o l l o w i n g ways:
$ Is
$ /bin/Is

optional

T h e shell does n o t require that the n a m e of the p r o g r a m appear first o n the c o m m a n d l i n e . T h u s y o u c a n s t r u c t u r e a c o m m a n d l i n e as f o l l o w s :
$ >bb



) instructs the shell t o redirect the o u t p u t o f a c o m -

m a n d to the specified file instead of to the screen (Figure 7-6). T h e f o r m a t of a
c o m m a n d l i n e t h a t r e d i r e c t s o u t p u t is
command

[arguments]

w h e r e command
u t i l i t y ) , arguments

>

filename

i s a n y e x e c u t a b l e p r o g r a m ( s u c h as a n a p p l i c a t i o n p r o g r a m o r a
a r e o p t i o n a l a r g u m e n t s , a n d filename

is t h e n a m e o f t h e o r d i n a r y

file the shell redirects the o u t p u t to.
F i g u r e 7 - 7 u s e s cat t o d e m o n s t r a t e o u t p u t r e d i r e c t i o n . T h i s f i g u r e c o n t r a s t s
F i g u r e 7 - 5 , w h e r e s t a n d a r d i n p u t and

with

s t a n d a r d o u t p u t are associated w i t h the key-

b o a r d a n d screen. T h e i n p u t i n F i g u r e 7 - 7 comes f r o m the k e y b o a r d . T h e

redirect

o u t p u t s y m b o l o n t h e c o m m a n d l i n e c a u s e s t h e s h e l l t o a s s o c i a t e cat's s t a n d a r d o u t p u t w i t h t h e sample.txt f i l e s p e c i f i e d o n t h e c o m m a n d l i n e .

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 249.
A f t e r g i v i n g t h e c o m m a n d a n d t y p i n g t h e t e x t s h o w n i n F i g u r e 7 - 7 , t h e sample.txt
f i l e c o n t a i n s t h e t e x t y o u e n t e r e d . Y o u c a n use c a t w i t h a n a r g u m e n t o f sample.txt
t o d i s p l a y t h i s file. T h e n e x t s e c t i o n s h o w s a n o t h e r w a y t o use cat t o d i s p l a y t h e file.
F i g u r e 7 - 7 s h o w s t h a t r e d i r e c t i n g s t a n d a r d o u t p u t f r o m c a t is a h a n d y w a y t o c r e a t e
a f i l e w i t h o u t u s i n g a n e d i t o r . T h e d r a w b a c k is t h a t o n c e y o u e n t e r a l i n e a n d p r e s s
RETURN, y o u c a n n o t e d i t t h e t e x t . W h i l e y o u a r e e n t e r i n g a l i n e , t h e e r a s e a n d k i l l k e y s
w o r k t o d e l e t e t e x t . T h i s p r o c e d u r e is u s e f u l f o r c r e a t i n g s h o r t , s i m p l e files.
F i g u r e 7 - 8 s h o w s h o w t o u s e c a t a n d t h e r e d i r e c t o u t p u t s y m b o l t o catenate

(join one

a f t e r t h e o t h e r — t h e d e r i v a t i o n o f t h e n a m e o f t h e cat u t i l i t y ) several files i n t o

one

STANDARD INPUT AND STANDARD OUTPUT 2 4 7

$ cat > sample.txt
This t e x t i s being entered a t the keyboard and
cat i s copying i t t o a f i l e .
Press CONTROL-D t o i n d i c a t e the
end o f f i l e .
CONTROL-D

s

Figure 7-7

cat w i t h i t s o u t p u t

redirected

l a r g e r f i l e . T h e f i r s t t h r e e c o m m a n d s d i s p l a y t h e c o n t e n t s o f t h r e e f i l e s : stationery,
tape, a n d pens. T h e n e x t c o m m a n d s h o w s cat w i t h t h r e e f i l e n a m e s a s a r g u m e n t s .
W h e n y o u c a l l i t w i t h m o r e t h a n o n e f i l e n a m e , cat c o p i e s t h e f i l e s , o n e a t a t i m e , t o
s t a n d a r d o u t p u t . T h i s c o m m a n d r e d i r e c t s s t a n d a r d o u t p u t t o t h e file supply_orders.
T h e f i n a l cat c o m m a n d s h o w s t h a t supply_orders c o n t a i n s t h e c o n t e n t s o f a l l t h r e e o f
t h e o r i g i n a l files.

REDIRECTING STANDARD INPUT
Just asy o u can redirect standard o u t p u t , s oy o u can redirect standard input.
redirect

input

symbol

The

(<) instructs the shell t o redirect a c o m m a n d ' s i n p u t t o c o m e

f r o m the specified file instead o f f r o m the k e y b o a r d

(Figure 7-9, next page).

The

f o r m a t o fa c o m m a n d line that redirects input is
command

[arguments]

w h e r e command
u t i l i t y ) , arguments

<

filename

is any executable p r o g r a m (such as a napplication p r o g r a m o r a
a r e o p t i o n a l a r g u m e n t s , a n d filename

file the shell redirects the i n p u t

from.

$ cat s t a t i o n e r y
2,000 sheets letterhead ordered:
$ cat tape
1 box masking tape o r d e r e d :
5 boxes f i l a m e n t tape ordered:
$ cat pens
12 d o z . b l a c k p e n s o r d e r e d :

10/7/10
10/14/10
10/28/10
10/4/10

$ cat s t a t i o n e r y tape pens > supply_orders
$ cat supply_orders
2,000 sheets letterhead ordered:
1 box masking tape o r d e r e d :
5 boxes f i l a m e n t tape ordered:
12 d o z . b l a c k p e n s o r d e r e d :
S

Figure 7-8

U s i n g cat t o c a t e n a t e f i l e s

10/7/10
10/14/10
10/28/10
10/4/10

isthe n a m e o fthe o r d i n a r y

248

CHAPTER 7

THE SHELL

Standard
input
i "

Ji
w

Standard
Put

out

Command

Figure 7-9

Redirecting standard input

F i g u r e 7 - 1 0 s h o w s cat w i t h i t s i n p u t r e d i r e c t e d f r o m t h e supply_orders f i l e c r e a t e d
i n F i g u r e 7 - 8 a n d s t a n d a r d o u t p u t g o i n g t o t h e s c r e e n . T h i s s e t u p c a u s e s cat t o d i s p l a y the s a m p l e file o n the screen. T h e system a u t o m a t i c a l l y supplies a n E O F signal
at the end o fa n o r d i n a r y file.
Utilities that take
input from a file or
standard input

G i v i n g a c a t c o m m a n d w i t h i n p u t r e d i r e c t e d f r o m a f i l e y i e l d s t h e s a m e r e s u l t as g i v ¡ng

a ca

cjass

0

t c o m m a n d w i t h t h e f i l e n a m e as a n a r g u m e n t . T h e c a t u t i l i t y i s a m e m b e r o f a
fLi

n u x

utilities t h a t f u n c t i o n i n t h i s m a n n e r . O t h e r m e m b e r s o f t h i s class o f

u t i l i t i e s i n c l u d e Ipr, s o r t , g r e p , a n d P e r l . T h e s e u t i l i t i e s f i r s t e x a m i n e t h e c o m m a n d l i n e
that y o u call t h e m w i t h . I f y o u include a filename o n the c o m m a n d line, the utility
takes its i n p u t f r o m t h e file y o u specify. I f y o u d o n o t specify a f i l e n a m e , t h e u t i l i t y
takes i t s i n p u t f r o m s t a n d a r d i n p u t . I t is the u t i l i t y o r p r o g r a m — n o t the shell o r
operating s y s t e m — t h a t functions in this manner.

n o c l o b b e r : AVOIDS OVERWRITING FILES
T h e s h e l l p r o v i d e s t h e noclobber f e a t u r e t h a t p r e v e n t s o v e r w r i t i n g a f i l e u s i n g r e d i r e c t i o n . E n a b l e t h i s f e a t u r e b y s e t t i n g n o c l o b b e r u s i n g t h e c o m m a n d set - o noclobber.
T h e s a m e c o m m a n d w i t h +o u n s e t s noclobber. W i t h n o c l o b b e r s e t , i f y o u r e d i r e c t
o u t p u t t o a n existing file, the shell displays a n e r r o r message a n d does n o t execute the
c o m m a n d . T h e f o l l o w i n g e x a m p l e c r e a t e s a f i l e u s i n g touch, sets noclobber, a t t e m p t s
t o r e d i r e c t t h e o u t p u t f r o m echo t o t h e n e w l y c r e a t e d f i l e , u n s e t s n o c l o b b e r , a n d p e r forms the same redirection:
$ touch tmp
$ set -o noclobber
$ echo "hi there" > tmp
bash: tmp: cannot o v e r w r i t e
$ set +o noclobber
$ echo "hi there" > tmp

existing

file

Y o u c a n o v e r r i d e n o c l o b b e r b y p u t t i n g a p i p e s y m b o l a f t e r t h e r e d i r e c t s y m b o l (>l).
I n t h e f o l l o w i n g e x a m p l e , t h e u s e r c r e a t e s a f i l e b y r e d i r e c t i n g t h e o u t p u t o f date.

STANDARD INPUT AND STANDARD OUTPUT 2 4 9

$ cat < supply_orders
2,000 sheets letterhead ordered:
1 box masking tape o r d e r e d :
5 boxes f i l a m e n t tape ordered:
12 d o z . b l a c k p e n s o r d e r e d :

Figure 7-10

cat w i t h i t s i n p u t

10/7/10
10/14/10
10/28/10
10/4/10

redirected

N e x t t h e u s e r sets t h e noclobber v a r i a b l e a n d r e d i r e c t s o u t p u t t o t h e s a m e f i l e a g a i n .
T h e shell displays a nerror message. T h e n the user places a pipe s y m b o l after

the

redirect s y m b o l a n d the shell a l l o w s the user t o o v e r w r i t e the file.
$ date > tmp2
$ set - o noclobber
$ date > tmp2
bash: a : cannot o v e r w r i t e
$ date >| tmp2

existing

file

Redirecting output can destroy a file II
caution Depending on which shell you are using and how the environment is set up, a command such as
the following may yield undesired results:
$ cat orange pear > orange
cat: orange: input f i l e i s output

file

Although cat displays an error message, the shell destroys the contents of the existing orange
file. The new orange file will have the same contents as pear because the first action the shell
takes when it sees the redirection symbol (>) is to remove the contents of the original orange file.
If you want to catenate two files into one, use cat to put the two files into a temporary file and then
use mv to rename 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 166)
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
g r e p : o u t p u t : No s u c h f i l e o r d i r e c t o r y

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 have destroyed the contents of a.

APPENDING STANDARD OUTPUT TO A FILE
T h e append

output symbol

( » ) causes t h e shell t o a d d n e w i n f o r m a t i o n t o t h e e n d

o f a file, l e a v i n g existing i n f o r m a t i o n intact. T h i s s y m b o l p r o v i d e s a c o n v e n i e n t

way

o f c a t e n a t i n g t w o files i n t o o n e . T h e f o l l o w i n g c o m m a n d s d e m o n s t r a t e t h e a c t i o n o f

250

CHAPTER 7

THE S H E L L

$ date > whoson
$ cat whoson
S a t M a r 2 7 1 4 : 3 1 : 1 8 PST 2 0 1 0
$ who » whoson
$ cat whoson
S a t M a r 2 7 1 4 : 3 1 : 1 8 PST 2 0 1 0
sam
console
2010max
pts/4
2010max
pts/5
2010zach
pts/7
2010-

03030303-

27
27
27
26

05
12
12
08

00(:0)
2 3(:0.0)
33(:0.0)
45 ( b r a v o . e x a m p l e . c o m )

Figure 7-11

Redirecting and appending output

the

output

append

symbol.

The

second

command

accomplishes

t h e catenation

described i nthe preceding caution box:
$ cat
this i
$ cat
$ cat
this i
this i

orange
s orange
pear » orange
orange
s orange
s pear

T h e first c o m m a n d d i s p l a y s t h e c o n t e n t s o f t h e orange file. T h e s e c o n d

command

a p p e n d s t h e c o n t e n t s o f t h e p e a r f i l e t o t h e o r a n g e f i l e . T h e f i n a l cat d i s p l a y s t h e r e s u l t .

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 set, the shell will overwrite the orange file. Even if you have the
noclobber feature turned on, it is a good idea to keep backup copies of the files you are manipulating in case you make a mistake.
Although it protects you from overwriting a file using redirection, noclobber does not stop you
from overwriting a file using cp or mv. These utilities include the - i (interactive) option that helps
protect you from this type of mistake by verifying your intentions when you try to overwrite a file.
For more information see the tip "cp can destroy a file" on page 164.
T h e n e x t e x a m p l e s h o w s h o w t o create a file t h a t contains the date a n d t i m e

(the

o u t p u t f r o m date), f o l l o w e d b y a l i s t o f w h o i s l o g g e d i n ( t h e o u t p u t f r o m who). T h e
f i r s t l i n e i n F i g u r e 7 - 1 1 r e d i r e c t s t h e o u t p u t f r o m date t o t h e f i l e n a m e d

whoson.

T h e n cat d i s p l a y s t h e f i l e . N e x t t h e e x a m p l e a p p e n d s t h e o u t p u t f r o m who t o t h e
w h o s o n f i l e . F i n a l l y cat d i s p l a y s t h e f i l e c o n t a i n i n g t h e o u t p u t o f b o t h u t i l i t i e s .

/ d e v / n u l l : MAKING DATA DISAPPEAR
T h e /dev/null d e v i c e is a data sink, c o m m o n l y r e f e r r e d t o as a bit bucket.

Y o u can

r e d i r e c t o u t p u t t h a t y o u d o n o t w a n t t o k e e p o r see t o / d e v / n u l l a n d t h e o u t p u t w i l l
disappear w i t h o u t a trace:

STANDARD INPUT AND STANDARD OUTPUT 2 5 1
$ echo "hi there" > / d e v / n u l l
$
W h e n y o u r e a d f r o m / d e v / n u l l , y o u g e t a n u l l s t r i n g . G i v e t h e f o l l o w i n g cat c o m m a n d
t o t r u n c a t e a file n a m e d messages t o z e r o l e n g t h w h i l e p r e s e r v i n g t h e o w n e r s h i p a n d
p e r m i s s i o n s o f the file:
$ I s -1 messages
-rw-r--r-1 max p u b s 2 5 3 1 5 2 0 1 0 - 1 0 - 2 4 1 0 : 5 5 m e s s a g e s
$ cat / d e v / n u l l > messages
$ I s -1 messages
-rw-r--r-1 max p u b s 0 2 0 1 0 - 1 0 - 2 4 1 1 : 0 2 m e s s a g e s

PIPES
T h e s h e l l u s e s a pipe

to connect standard output o fone c o m m a n d t ostandard input

o f a n o t h e r c o m m a n d . A p i p e ( s o m e t i m e s r e f e r r e d t o a s a pipeline)

has the

same

e f f e c t as r e d i r e c t i n g s t a n d a r d o u t p u t o f o n e c o m m a n d t o a f i l e a n d t h e n u s i n g t h a t
f i l e as s t a n d a r d i n p u t t o a n o t h e r c o m m a n d . A p i p e d o e s a w a y w i t h s e p a r a t e c o m m a n d s a n d t h e i n t e r m e d i a t e f i l e . T h e s y m b o l f o r a p i p e is a v e r t i c a l b a r ( I ) . T h e s y n tax o fa c o m m a n d line using a pipe is
command_a

[arguments]

I commandjb

[arguments]

T h e p r e c e d i n g c o m m a n d l i n e uses a p i p e o n a s i n g l e c o m m a n d l i n e t o g e n e r a t e t h e
s a m e r e s u l t as t h e f o l l o w i n g t h r e e c o m m a n d l i n e s :
command_a

[arguments]

> temp

commandjb

[arguments]

< temp

rm

temp

I n the preceding sequence o f c o m m a n d s , the first line redirects standard
f r o m command_a

t o a n i n t e r m e d i a t e f i l e n a m e d temp.

s t a n d a r d i n p u t f o r commandjb

T h e second line

output
redirects

t o c o m e f r o m temp. T h e f i n a l l i n e d e l e t e s temp.

c o m m a n d u s i n g a p i p e is n o t o n l y easier t o t y p e b u t is g e n e r a l l y m o r e

The

efficient

because i t does n o t create a t e m p o r a r y file.
tr

Y o u c a n use a p i p e w i t h a n y o f the L i n u x utilities t h a t accept i n p u t either f r o m a file
specified o n t h e c o m m a n d l i n e o r f r o m s t a n d a r d i n p u t . Y o u c a n also use pipes w i t h
c o m m a n d s t h a t a c c e p t i n p u t o n l y f r o m s t a n d a r d i n p u t . F o r e x a m p l e , t h e tr ( t r a n s l a t e ) u t i l i t y t a k e s i t s i n p u t f r o m s t a n d a r d i n p u t o n l y . I n i t s s i m p l e s t u s a g e tr h a s t h e
following format:
tr stringl

string2

T h e tr u t i l i t y a c c e p t s i n p u t f r o m s t a n d a r d i n p u t a n d l o o k s f o r c h a r a c t e r s t h a t m a t c h
o n e o f t h e c h a r a c t e r s i n stringl.
c h a r a c t e r i n stringl
stringl

U p o n finding a match, i t translates the

t o t h e c o r r e s p o n d i n g c h a r a c t e r i n string2.

t r a n s l a t e s i n t o t h e f i r s t c h a r a c t e r i n string2,

matched

(The first character i n

a n d s o f o r t h . ) T h e tr u t i l i t y

s e n d s i t s o u t p u t t o s t a n d a r d o u t p u t . I n b o t h o f t h e f o l l o w i n g e x a m p l e s , tr d i s p l a y s

252

CHAPTER 7

THE S H E L L

$ 1s > temp
$ l p r temp
$ rm temp

or
$ Is

|

Figure 7-12

lpr
A pipe

t h e c o n t e n t s o f t h e a b s t r a c t f i l e w i t h t h e l e t t e r s a, b, a n d c t r a n s l a t e d i n t o A , B , a n d
C, respectively:
$ cat a b s t r a c t | t r abc ABC
$ t r abc ABC < a b s t r a c t
T h e tr u t i l i t y d o e s n o t c h a n g e t h e c o n t e n t s o f t h e o r i g i n a l f i l e ; i t c a n n o t c h a n g e

the

o r i g i n a l file because i t does n o t " k n o w " the source o fits i n p u t .
lpr

T h e lpr ( l i n e p r i n t e r ) u t i l i t y a l s o a c c e p t s i n p u t f r o m e i t h e r a f i l e o r s t a n d a r d

input.

W h e n y o u t y p e t h e n a m e o f a f i l e f o l l o w i n g lpr o n t h e c o m m a n d l i n e , i t p l a c e s t h a t
f i l e i n t h e p r i n t q u e u e . W h e n y o u d o n o t s p e c i f y a f i l e n a m e o n t h e c o m m a n d l i n e , lpr
takes i n p u t f r o m s t a n d a r d i n p u t . T h i s feature enables y o u t o use a p i p e t o redirect
i n p u t t o lpr. T h e f i r s t s e t o f c o m m a n d s i n F i g u r e 7 - 1 2 s h o w s h o w y o u c a n u s e Is a n d
lpr w i t h a n i n t e r m e d i a t e f i l e ( t e m p ) t o s e n d a l i s t o f t h e f i l e s i n t h e w o r k i n g

directory

t o t h e p r i n t e r . I f t h e temp file exists, t h e first c o m m a n d o v e r w r i t e s its c o n t e n t s .
s e c o n d set o f c o m m a n d s

The

uses a p i p e t o s e n d t h e s a m e list ( w i t h t h e e x c e p t i o n o f

temp) t othe printer.
T h e c o m m a n d s i n F i g u r e 7 - 1 3 r e d i r e c t t h e o u t p u t f r o m t h e who u t i l i t y t o temp

and

t h e n d i s p l a y t h i s f i l e i n s o r t e d o r d e r . T h e sort u t i l i t y ( p a g e 1 6 8 ) t a k e s i t s i n p u t
the file specified o n the c o m m a n d line or, w h e n a file isn o t specified, f r o m

from

standard

i n p u t ; i t s e n d s i t s o u t p u t t o s t a n d a r d o u t p u t . T h e sort c o m m a n d l i n e i n F i g u r e 7 - 1 3
t a k e s its i n p u t f r o m s t a n d a r d i n p u t , w h i c h is r e d i r e c t e d (<) t o c o m e f r o m temp. T h e
o u t p u t t h a t sort s e n d s t o t h e s c r e e n l i s t s t h e u s e r s i n s o r t e d ( a l p h a b e t i c a l )

order.

B e c a u s e sort c a n t a k e i t s i n p u t f r o m s t a n d a r d i n p u t o r f r o m a f i l e n a m e o n t h e c o m m a n d line, o m i t t i n g the < s y m b o l f r o m Figure 7 - 1 3 yields the same result.
F i g u r e 7 - 1 4 achieves t h e s a m e result w i t h o u t c r e a t i n g t h e temp file. U s i n g a p i p e ,
t h e s h e l l r e d i r e c t s t h e o u t p u t f r o m w h o t o t h e i n p u t o f sort. T h e sort u t i l i t y
i n p u t f r o m standard i n p u t because n o filename follows i t o n the c o m m a n d

takes

line.

W h e n m a n y people are using the system a n d y o u w a n t i n f o r m a t i o n a b o u t o n l y

one

o f t h e m , y o u c a n s e n d t h e o u t p u t f r o m who t o grep ( p a g e 1 6 6 ) u s i n g a p i p e . T h e grep
utility

displays t h eline c o n t a i n i n g the string y o u

example:
$ who | grep 'sam'
sam
console

2010-03-24

05:00

specify—sam i n the

following

STANDARD INPUT AND STANDARD OUTPUT 2 5 3

$ who > temp
$ s o r t < temp
max
pts/4
max
pts/5
zach
pts/7
sam
console
$ rm temp

2010201020102010-

03030303-

24
24
23
24

12 23
12 33
08 45
05 00

Figure 7-13 Using a temporary file to store intermediate results
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 162).
$ Is

| 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 Is by itself and when you send it through a pipe to less.

FILTERS
A filter is a command that processes an input stream of data to produce an output
stream of data. A command line that includes a filter uses a pipe to connect standard output of one command to the filter's standard input. Another pipe connects
the filter's standard output to standard input of another command. Not all utilities
can be used as filters.
In the following example, sort is a filter, taking standard input from standard output
of who and using a pipe to redirect standard output to standard input of Ipr. This
command line sends the sorted output of who to the printer:
$ who | s o r t

| Ipr

The preceding example demonstrates the power of the shell combined with the versatility of Linux utilities. The three utilities who, sort, and Ipr were not specifically
designed to work with each other, but they all use standard input and standard output in the conventional way. By using the shell to handle input and output, you can
piece standard utilities together on the command line to achieve the results you want.
$ who |
max
max
zach
sam

sort
pts/4
pts/5
pts/7
console

2010201020102010-

03030303-

24
24
23
24

12 23
12 33
08 45
05 00

Figure 7-14 A pipe doing the work of a temporary file

254

CHAPTER 7

THE S H E L L

$ who | tee who.out

sam

console

grep sam

2010-03-24

05 0 0

2010-03-24
2010-03-24
2010-03-24
2010-03-23

05
12
12
08

$ cat who.out

sam
max
max
zach

console
pts/4
pts/5
pts/7

00
23
33
45

Figure 7-15 Using tee

tee: SENDS OUTPUT IN TWO DIRECTIONS
The tee utility copies its standard input both to a file and to standard output. This
utility is aptly named: It takes a single stream of input and sends the output in two
directions. In Figure 7-15 the output of w h o is sent via a pipe to standard input of
tee. The tee utility saves a copy of standard input in a file named w h o . o u t and also
sends a copy to standard output. Standard output of tee goes via a pipe to standard
input of grep, which displays only those lines containing the string s a m . Use the - a
(append) option to cause tee to append to a file instead of overwriting it.

R U N N I N G A C O M M A N D IN THE

BACKGROUND

Foreground All commands up to this point have been run in the foreground. When you run a
command in the foreground,
the shell waits for it to finish before displaying another
prompt and allowing you to continue. When you run a command in the
background,
you do not have to wait for the command to finish before running another command.
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 supervision. It leaves the screen free so you can use it for other work. O f course, when
you are using a GUI, you can open another window to run another job.
Job number, To run a command in the background, type an ampersand ( & ) just before the RETURN
PID number that ends the command line. The shell assigns a small number to the job and displays this job number between brackets. Following the job number, the shell displays the process identification
(PID) number—a larger number assigned by the
operating system. Each of these numbers identifies the command running in the
background. The shell then displays another prompt and you can enter another
command. When the background job finishes, the shell displays a message giving
both the job number and the command line used to run the command.
The next example runs in the background; it sends the output of Is through a pipe
to Ipr, which sends it to the printer.
$ I s -1

[1]

S

| Ipr &

22092

RUNNING A COMMAND IN THE BACKGROUND 2 5 5
T h e [1] f o l l o w i n g the c o m m a n d line indicates t h a t the shell has assigned j o b n u m b e r
1 t o this job. T h e 2 2 0 9 2 is the P I D n u m b e r o fthe first c o m m a n d i nthe job. W h e n
t h i s b a c k g r o u n d j o b c o m p l e t e s e x e c u t i o n , y o u see t h e m e s s a g e
[ 1 ] + Done

Is -1 |l p r

( I n p l a c e o f Is -1, t h e s h e l l m a y d i s p l a y s o m e t h i n g s i m i l a r t o Is — c o l o r = a l w a y s -1.
T h i s d i f f e r e n c e is d u e t o t h e f a c t t h a t Is i s a l i a s e d [ p a g e 3 4 6 ] t o Is — c o l o r = a l w a y s . )

MOVING A JOB FROM THE FOREGROUND TO THE BACKGROUND
CONTROL-Z Y O U c a n s u s p e n d a f o r e g r o u n d j o b ( s t o p i t f r o m r u n n i n g ) b y p r e s s i n g t h e s u s p e n d
k e y , u s u a l l y CONTROL-Z. T h e s h e l l t h e n s t o p s t h e p r o c e s s a n d d i s c o n n e c t s

standard

input f r o m the keyboard. Y o u can put a suspended job i n the b a c k g r o u n d

and

restart i t by using the bg c o m m a n d f o l l o w e d by the job number. Y o u d o n o t need t o
specify the j o b n u m b e r w h e n there is o n l y one stopped job.
O n l y the foreground job can take input f r o m the keyboard. T o connect the keyboard t o a p r o g r a m r u n n i n g i n the background, y o u must bring i t t o the
g r o u n d . T o d o s o , t y p e fg w i t h o u t

fore-

any arguments w h e n o n l y one job is i n the

b a c k g r o u n d . W h e n m o r e t h a n o n e j o b i s i n t h e b a c k g r o u n d , t y p e fg, o r a p e r c e n t
sign ( % ) , f o l l o w e d by the n u m b e r o fthe job y o u w a n t to b r i n g into the foreground.
T h e s h e l l d i s p l a y s t h e c o m m a n d y o u u s e d t o s t a r t t h e j o b (promptme i n t h e f o l l o w ing example), a n d y o u can enter any i n p u t the p r o g r a m requires t o continue:
b a s h $ fg 1
promptme
Redirect the output o f a job y o u r u n i nthe background t o keep it f r o m interferi n g w i t h w h a t e v e r y o u are w o r k i n g o n i n the f o r e g r o u n d ( o n the screen). Refer
to "Separating and G r o u p i n g C o m m a n d s "

o npage 303 f o r more detail

about

b a c k g r o u n d tasks.

kill: ABORTING A BACKGROUND JOB
T h e i n t e r r u p t k e y ( u s u a l l y C0NTR0L-C) c a n n o t a b o r t a b a c k g r o u n d p r o c e s s ; y o u m u s t
u s e kill ( p a g e 4 5 5 ) f o r t h i s p u r p o s e . F o l l o w kill o n t h e c o m m a n d l i n e w i t h e i t h e r t h e
P I D n u m b e r of the process y o u w a n t t o abort o r a percent sign ( % ) f o l l o w e d by the
job number.
Determining the
PID of a process
using ps

I f y o u f o r g e t a P I D n u m b e r , y o u c a n u s e t h e ps ( p r o c e s s s t a t u s ) u t i l i t y ( p a g e 3 2 8 ) t o
d i s p l a y i t . T h e f o l l o w i n g e x a m p l e r u n s a tail - f outfile c o m m a n d ( t h e - f [ f o l l o w ]
0

pti

0 n

cauS

e s t a i l t o w a t c h outfile a n d d i s p l a y n e w l i n e s as t h e y a r e w r i t t e n t o t h e

f i l e ) a s a b a c k g r o u n d j o b , u s e s ps t o d i s p l a y t h e P I D n u m b e r o f t h e p r o c e s s ,
a b o r t s t h e j o b w i t h kill:
$ tail

-f outfile &

[1] 18228
$ ps | grep t a i l
18228 p t s / 4
00:00:00
$ k i l l 18228
[1]+
Terminated
$

tail
tail

- f outfile

and

256

CHAPTER 7
Determining the
number of a job
using jobs

THE SHELL
I f y o u f o r g e t a j o b n u m b e r , y o u c a n use t h e jobs c o m m a n d t o d i s p l a y a list o f j o b
n u m b e r s . T h e n e x t e x a m p l e is s i m i l a r t o t h e p r e v i o u s o n e e x c e p t i t uses t h e
n u m

b

e r

job

instead o f the P I D n u m b e r t o identify the j o b t o be killed. Sometimes the

m e s s a g e s a y i n g t h e j o b i s t e r m i n a t e d d o e s n o t a p p e a r u n t i l y o u p r e s s RETURN a f t e r t h e
RETURN t h a t e x e c u t e s t h e kill c o m m a n d .
$ tail -f outfile &
[ 1 ] 18236
$ bigjob &
[ 2 ] 18237
$ jobs
[1]Running
[2]+
Running
$ k i n %i
$

tai 1 - f outfi le &
bigjob &

RETURN

[1]S

Terminated

tai 1 - f outfi l e

FILENAME GENERATION/PATHNAME
Wildcards, globbing

EXPANSION

W h e n y o u g i v e t h e s h e l l a b b r e v i a t e d f i l e n a m e s t h a t c o n t a i n special
c a l l e d metacharacters,

characters,

also

the shell c a n generate filenames that m a t c h the names o f

e x i s t i n g f i l e s . T h e s e s p e c i a l c h a r a c t e r s a r e a l s o r e f e r r e d t o as wildcards

because they

a c t m u c h as t h e j o k e r s d o i n a d e c k o f c a r d s . W h e n o n e o f t h e s e c h a r a c t e r s a p p e a r s
i n a n a r g u m e n t o nthe c o m m a n d line, the shell e x p a n d s that a r g u m e n t i n sorted
o r d e r i n t o a list o f f i l e n a m e s a n d passes t h e list t o t h e p r o g r a m called b y t h e c o m m a n d l i n e . F i l e n a m e s t h a t c o n t a i n t h e s e s p e c i a l c h a r a c t e r s a r e c a l l e d ambiguous
references

file

because t h e y d o n o t refer t o a n y o n e specific file. T h e process t h a t t h e

s h e l l p e r f o r m s o n t h e s e f i l e n a m e s i s c a l l e d pathname

expansion

or

globbing.

A m b i g u o u s file references refer t o a g r o u p o f files w i t h s i m i l a r n a m e s q u i c k l y , savi n g the effort o ft y p i n g the n a m e s individually. T h e y c a n also help f i n d a file w h o s e
n a m e y o u d o n o t r e m e m b e r i n its entirety. I f n o f i l e n a m e m a t c h e s t h e a m b i g u o u s file
r e f e r e n c e , t h e shell g e n e r a l l y passes t h e u n e x p a n d e d r e f e r e n c e — s p e c i a l c h a r a c t e r s a n d
all—to the c o m m a n d .

THE ? SPECIAL CHARACTER
T h e q u e s t i o n m a r k (?) is a special c h a r a c t e r t h a t causes t h e shell t o generate filenames. I t m a t c h e s a n y single c h a r a c t e r i n the n a m e o fa n e x i s t i n g file. T h e f o l l o w i n g
c o m m a n d u s e s t h i s s p e c i a l c h a r a c t e r i n a n a r g u m e n t t o t h e Ipr u t i l i t y :
$ l p r memo?
T h e shell e x p a n d s t h e memo? a r g u m e n t a n d generates a list o f files i n t h e w o r k i n g
directory that have names c o m p o s e d of memo f o l l o w e d by any single character. T h e
s h e l l t h e n p a s s e s t h i s l i s t t o Ipr. T h e Ipr u t i l i t y n e v e r " k n o w s " t h e s h e l l g e n e r a t e d t h e
filenames i t w a s called w i t h . I f n o f i l e n a m e m a t c h e s the a m b i g u o u s file reference,

FILENAME GENERATION/PATHNAME EXPANSION 2 5 7
t h e s h e l l p a s s e s t h e s t r i n g i t s e l f ( m e m o ? ) t o Ipr o r , i f i t is s e t u p t o d o s o , p a s s e s a n u l l
s t r i n g ( s e e nullglob o n p a g e 3 5 5 ) .
T h e f o l l o w i n g e x a m p l e u s e s Is f i r s t t o d i s p l a y t h e n a m e s o f a l l f i l e s i n t h e

working

d i r e c t o r y a n d t h e n t o d i s p l a y t h e f i l e n a m e s t h a t memo? m a t c h e s :
$ Is
mem
memol2
memo memo5
S i s memo?
memo5 memo9
The

memo?

memo9
memoa

memomax
memos

memoa

memos

ambiguous

newmemo5

file reference

does

n o tmatch

mem, memo, memo 12,

memomax, o r newmemo5. Y o u c a n also use a q u e s t i o n m a r k i n t h e m i d d l e o f a n
a m b i g u o u s file reference:
$ Is
7may4report
may4report
mayqreport
may_report
mayl4report
may4report.79
mayreport
may.report
S i s may?report
may.report
may4report
may_report
mayqreport
Y o u c a n u s e echo a n d Is t o p r a c t i c e g e n e r a t i n g f i l e n a m e s . T h e echo u t i l i t y d i s p l a y s
t h e a r g u m e n t s t h a t t h e shell passes t o i t :
$ echo may?report
may.report may4report may_report

mayqreport

T h e shell first e x p a n d s t h e a m b i g u o u s file reference i n t o a list o f a l l files i n t h e
w o r k i n g d i r e c t o r y t h a t m a t c h t h e s t r i n g m a y ? r e p o r t . I t t h e n p a s s e s t h i s l i s t t o echo,
j u s t as t h o u g h y o u h a d e n t e r e d t h e l i s t o f f i l e n a m e s as a r g u m e n t s t o echo. T h e echo
u t i l i t y displays the list o f filenames.
A q u e s t i o n m a r k does n o t m a t c h a l e a d i n g p e r i o d (one t h a t indicates a h i d d e n filen a m e ; see p a g e 2 0 4 ) . W h e n y o u w a n t t o m a t c h f i l e n a m e s t h a t b e g i n w i t h a p e r i o d ,
y o u m u s t explicitly i n c l u d e the p e r i o d i n the a m b i g u o u s file reference.

THE * SPECIAL CHARACTER
T h e asterisk ( * ) p e r f o r m s

a function similar t o that o f the question m a r k b u t

m a t c h e s a n y n u m b e r o f c h a r a c t e r s , including

zero

characters,

i na filename. T h e fol-

l o w i n g e x a m p l e first s h o w s all files i n t h e w o r k i n g d i r e c t o r y a n d t h e n s h o w s t h r e e
c o m m a n d s t h a t d i s p l a y all t h e f i l e n a m e s t h a t b e g i n w i t h t h e s t r i n g memo, e n d w i t h
t h e s t r i n g m o , a n d c o n t a i n t h e s t r i n g alx:
$ Is
amemo
memo
memoalx.0620
mem
memo.0612
memoalx.keep
m e m a l x memoa
memorandum
$ echo memo*
memo m e m o . 0 6 1 2 memoa m e m o a l x . 0 6 2 0
$ echo *mo
amemo memo s a l l y m e m o u s e r . m e m o
$ echo * a l x *
memalx memoalx.0620 memoalx.keep

memosally
user.memo
sallymemo
typescript
m e m o a l x . k e e p memorandum

memosally

258

CHAPTER 7

THE SHELL
T h e a m b i g u o u s f i l e r e f e r e n c e m e m o * d o e s n o t m a t c h a m e m o , m e m , sallymemo, o r
u s e r . m e m o . L i k e t h e q u e s t i o n m a r k , a n a s t e r i s k d o e s not m a t c h a l e a d i n g p e r i o d i n a
filename.
T h e - a o p t i o n c a u s e s Is t o d i s p l a y h i d d e n f i l e n a m e s . T h e c o m m a n d echo * d o e s n o t
d i s p l a y . ( t h e w o r k i n g d i r e c t o r y ) , . . ( t h e p a r e n t o f t h e w o r k i n g d i r e c t o r y ) , .aaa, o r
.profile. I n c o n t r a s t , t h e c o m m a n d echo . * d i s p l a y s o n l y t h o s e f o u r n a m e s :
$ Is
aaa
memo.sally
sally.0612
thurs
memo.0612
report
Saturday
$ Is -a
.aaa
aaa
memo.sally
sally.0612
thurs
.profile
memo.0612
report
Saturday
$ echo *
aaa memo.0612 m e m o . s a l l y r e p o r t s a l l y . 0 6 1 2 S a t u r d a y t h u r s
$ echo
. . . . a a a
.profile
I n t h e f o l l o w i n g e x a m p l e , . p * d o e s n o t m a t c h m e m o . 0 6 1 2 , private, reminder, o r
r e p o r t . T h e Is . * c o m m a n d c a u s e s Is t o l i s t .private a n d .profile i n a d d i t i o n t o t h e
contents o f the . d i r e c t o r y (the w o r k i n g d i r e c t o r y ) a n d the .. d i r e c t o r y (the p a r e n t o f
t h e w o r k i n g d i r e c t o r y ) . W h e n c a l l e d w i t h t h e s a m e a r g u m e n t , echo d i s p l a y s

the

n a m e s o f files ( i n c l u d i n g directories) i n t h e w o r k i n g d i r e c t o r y t h a t b e g i n w i t h a d o t
(.), b u t n o t t h e c o n t e n t s o f d i r e c t o r i e s .
$ Is

-a

.private
.profile
$ echo . p *
.private
.profile
$ Is .*
.private
.profile

memo.0612

memo.0612
private

private

$ echo . *
. . . . private

reminder
report

reminder

report

.profi le

Y o u can p l a n to take advantage o f a m b i g u o u s file references w h e n y o u

establish

c o n v e n t i o n s f o r n a m i n g files. F o r e x a m p l e , w h e n y o u e n d all t e x t f i l e n a m e s

with

.txt, y o u c a n r e f e r e n c e t h a t g r o u p o f f i l e s w i t h * . t x t . T h e n e x t c o m m a n d uses t h i s
c o n v e n t i o n t o send all t e x t files i n t h e w o r k i n g d i r e c t o r y t o t h e printer. T h e a m p e r s a n d c a u s e s Ipr t o r u n i n t h e b a c k g r o u n d .
$ lpr

*.txt

&

FILENAME GENERATION/PATHNAME EXPANSION 2 5 9

THE [ ] SPECIAL CHARACTERS
A p a i r o f brackets s u r r o u n d i n g a list o f characters causes the shell t o m a t c h

file-

n a m e s c o n t a i n i n g t h e i n d i v i d u a l characters. W h e r e a s memo? m a t c h e s memo

fol-

l o w e d b y a n y character, memo[17a] is m o r e restrictive: I t m a t c h e s o n l y
m e m o 7 , a n d m e m o a . T h e b r a c k e t s d e f i n e a character

class

c h a r a c t e r s w i t h i n t h e b r a c k e t s . ( G N U c a l l s t h i s a character
class

memol,

that includes a l l the
list;

a GNU

character

is s o m e t h i n g different.) T h e shell e x p a n d s a n a r g u m e n t that includes a charac-

t e r - c l a s s d e f i n i t i o n , b y s u b s t i t u t i n g e a c h m e m b e r o f t h e c h a r a c t e r c l a s s , one at a
time,

i n p l a c e o f t h e b r a c k e t s a n d t h e i r c o n t e n t s . T h e shell t h e n passes t h e list o f

m a t c h i n g f i l e n a m e s t o t h e p r o g r a m i t is c a l l i n g .
Each character-class definition c a n replace o n l y a single character w i t h i n a filename.
T h e brackets a n d their contents are like a question m a r k that substitutes o n l y the
m e m b e r s o f t h e c h a r a c t e r class.
T h e first o f t h e f o l l o w i n g c o m m a n d s lists t h e n a m e s o f all files i n t h e w o r k i n g direct o r y t h a t b e g i n w i t h a , e, i, o , o r u. T h e s e c o n d c o m m a n d d i s p l a y s t h e c o n t e n t s o f
t h e files n a m e d page2.txt, page4.txt, page6.txt, a n d page8.txt.
$ echo [aeiou]*
$ less page[2468].txt

A h y p h e n w i t h i n brackets defines a range o f characters w i t h i n a character-class defi n i t i o n . F o r e x a m p l e , [ 6 - 9 ] r e p r e s e n t s [ 6 7 8 9 ] , [a-z] r e p r e s e n t s a l l l o w e r c a s e l e t t e r s
i n English, a n d [a-zA-Z] represents a l l letters, b o t h uppercase a n d lowercase, i n
English.
T h e f o l l o w i n g c o m m a n d lines s h o w t h r e e w a y s t o p r i n t t h e files n a m e d

partO,

parti, part2, part3, a n d part5. E a c h o f t h e s e c o m m a n d lines causes t h e s h e l l t o c a l l
Ipr w i t h f i v e f i l e n a m e s :
$ Ipr partO p a r t i part2 part3 parts
$ Ipr part[01235]
$ Ipr part[0-35]
T h e first c o m m a n d line e x p l i c i t l y specifies the five filenames. T h e second a n d t h i r d
c o m m a n d lines use a m b i g u o u s file references, i n c o r p o r a t i n g character-class

defini-

tions. T h e shell e x p a n d s the a r g u m e n t o n the second c o m m a n d line t o include a l l
files t h a t h a v e n a m e s b e g i n n i n g w i t h part a n d e n d i n g w i t h a n y o f t h e c h a r a c t e r s i n
t h e c h a r a c t e r c l a s s . T h e c h a r a c t e r c l a s s i s e x p l i c i t l y d e f i n e d as 0 , 1 , 2 , 3 , a n d 5 . T h e
t h i r d c o m m a n d l i n e a l s o uses a c h a r a c t e r - c l a s s d e f i n i t i o n b u t d e f i n e s t h e c h a r a c t e r
class t o be a l l c h a r a c t e r s i n t h e r a n g e 0 - 3 p l u s 5 .

260

CHAPTER 9 THEBOURNEAG INS H E L L

The following command line prints 39 files, partO through part38:
$

l p r

p a r t [ 0 - 9 ]

p a r t [ 1 2 ] [ 0 - 9 ]

p a r t 3 [ 0 - 8 ]

The first of the following commands lists the files in the working directory whose
names start with a through m. The second lists files whose names end with x, y, or z.
$

e c h o

[ a - m ] *

$

e c h o

* [ x - z ]

optional When an exclamation point (!) or a caret ( A ) immediately follows the opening
bracket ([) that defines a character class, the string enclosed by the brackets matches
any character not between the brackets. Thus [ A tsq]* matches any filename that
does not begin with t, s, or q.
The following examples show that * [Aab] matches filenames that do not end with the
letters a or b and that [Ab-d] * matches filenames that do not begin with b, c, or d.
$

I s

aa
$

ab
I s

ac
$

ad
I s

aa

ac

ad

ba

bb

bd

cc

dd

be

bd

cc

dd

* [ A a b ]

be

[ A b - d ] *

ab

ac

ad

You can cause a character class to match a hyphen (-) or a closing bracket (]) by
placing it immediately before the final closing bracket.
The next example demonstrates that the Is utility cannot interpret ambiguous file
references. First Is is called with an argument of ?old. The shell expands ?old into a
matching filename, hold, and passes that name to Is. The second command is the
same as the first, except the ? is quoted (refer to "Special Characters" on page 160).
The shell does not recognize this question mark as a special character and passes it
to Is. The Is utility generates an error message saying that it cannot find a file named
?old (because there is no file named ?old).
$

I s

? o l d

hold
$

I s

Is:

\ ? o l d

?old:

No s u c h f i l e

or

directory

Like most utilities and programs, Is cannot interpret ambiguous file references; that
work is left to the shell.

The shell expands ambiguous file references
tip The shell does the expansion when it processes an ambiguous file reference, not the program that
the shell runs. In the examples in this section, the utilities{\s, cat, echo, lpr) never see the ambiguous file references. The shell expands the ambiguous file references and passes a list of ordinary
filenames to the utility. In the previous examples, echo shows this to be true because it simply
displays its arguments; it never displays the ambiguous file reference.

CHAPTER SUMMARY

261

BUILTINS
A builtin is a utility (also called a command)
that is built into a shell. Each of the
shells has its own set of builtins. When it runs a builtin, the shell does not fork a
new process. Consequently builtins run more quickly and can affect the environment of the current shell. Because builtins are used in the same way as utilities, you
will not typically be aware of whether a utility is built into the shell or is a standalone utility.
The echo utility, for example, is a shell builtin. The shell always executes a shell
builtin before trying to find a command or utility with the same name. See
page 1002 for an in-depth discussion of builtin commands and page 1015 for a list
of bash builtins.
Listing bash To display a list of bash builtins, give the command info bash builtin. To display a
builtins 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 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 search for the section on builtins with the command / A SHELL BUILT I N C O M M A N D S (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 command. Most Linux utilities identify an option by its leading one or two hyphens.
When you give it a command, the shell tries to find an executable program with the
same name as the command. When it does, the shell executes the program. When it
does not, the shell tells you that it cannot find or execute the program. If the command 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 command's standard input to come from the keyboard and its standard output to go to
the screen. You can instruct the shell to redirect a command's standard input from
or standard output to any file or device. You can also connect standard output of
one command to standard input of another command using a pipe. A filter is a

262

CHAPTER 9 THEBOURNEAG INS H E L L

command that reads its standard input from standard output of one command and
writes its standard output to standard input of another command.
When a command runs in the foreground, the shell waits for it to finish before it
displays a prompt and allows you to continue. When you put an ampersand ( & ) at
the end of a command line, the shell executes the command in the background and
displays another prompt immediately. Run slow commands in the background
when you want to enter other commands at the shell prompt. The jobs builtin displays a list of suspended jobs and jobs running in the background; it 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.
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.

Table 7-1

New utilities

Utility

Function

tr

Maps one string of characters to another (page 251)

tee

Sends standard input to both a file and standard output (page 254)

bg

Moves a process to the background (page 255)

fg

Moves a process to the foreground (page 255)

jobs

Displays a list of suspended jobs and jobs running in the background
(page 256)

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:

EXERCISES

$ sort list > temp
$ Ipr temp
$ rm temp

3. What is a PID number? Why are these numbers useful when you run
processes in the background? Which utility displays the PID numbers of
the commands you are running?
4. Assume that the following files are in the working directory:
$

I s

intro
notesa

notesb
refl

ref2
refB

sectionl
section2

sectionB
section4a

section4b
sentrev

Give commands for each of the following, using wildcards to express
filenames with as few characters as possible.
a. List all files that begin with section.
b. List the sectionl, section2, and section3 files only.
c. List the intro file only.
d. List the sectionl, section3, refl, and ref3 files.
5. Refer to the man pages to determine which command 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 reverse alphabetical order.
d. Send a list of files in the working directory to the printer, sorted by size.
6. Give a command to
a. Redirect standard output from a sort command to a file named
phone_list. Assume the input file is named numbers.
b. Translate all occurrences of the characters [ and { t o 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:
parti and part2.
7. The Ipr and sort utilities accept input either from a file named on the
command line or from standard input.
a. Name two other utilities that function in a similar manner.
b. Name a utility that accepts its input only from standard input.
8. Give an example of a command that uses grep

263

264

CHAPTER 9 THEBOURNEAG INSHELL

a. With both input and output redirected.
b. With only input redirected.
c. With only output redirected.
d. Within a pipe.
In which of the preceding cases is grep used as a filter?
9. Explain the following error message. Which filenames would a subsequent
Is display?
$

I s

abc
abd abe
abf
abg
$ rm abc a b *
rm: c a n n o t remove ' a b c ' :

abh
No s u c h f i l e

or

directory

ADVANCED EXERCISES
10. When you use the redirect output symbol (>) with a command, the shell
creates the output file immediately, before the command is executed.
Demonstrate that this is true.
11. In experimenting with shell variables, Max accidentally deletes his PATH
variable. He decides he does not need the PATH variable. Discuss some of
the problems he may soon encounter and explain the reasons for these
problems. How could he easily return PATH to its original value?
12. Assume 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 cannot
delete.
13. If you accidentally create a filename that contains a nonprinting character,
such as a CONTROL character, how can you remove the file?
14. Why does the noclobber variable not protect you from overwriting an
existing file with cp or mv?
15. Why do command names and filenames usually not have embedded SPACES?
How would you create a filename containing a SPACE? How would you
remove it? (This is a thought exercise, not recommended practice. If you
want to experiment, create and work in a directory that contains only
your experimental file.)
16. Create a file named answer and give the following command:
$ > answers.0102 < answer cat

Explain what the command does and why. What is a more conventional
way of expressing this command?

P A R T III
DIGGING INTO UBUNTU LINUX
CHAPTER 8

LINUX GUIs: X AND GNOME
CHAPTER 9
THE BOURNE AGAIN SHELL

267
291

C H A P T E R 10
NETWORKING AND THE INTERNET

371

265

This page intentionally left blank

8
LINUX GUIS: X AND
GNOME
IN THIS CHAPTER
X Window System

268

StartingX from a CharacterBased Display
Remote Computing and Local
Displays
Desktop Environments/
Managers

275

The Nautilus File Browser
Window

276

The Nautilus Spatial View

282

GNOME Utilities

284

Run Application W i n d o w

286

GNOME Terminal Emulator/
Shell

287

270
270

This chapter covers the Linux graphical user interface (GUI). It
continues where Chapter 4 left off, going into more detail
a b 0 u t 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, the
menubar, and the Spatial view. The final section explores some
GNOME utilities, including Terminal, the GNOME terminal
emulator.

267

268

CHAPTER 8

LINUX G U I s : X AND G N O M E

X WINDOW SYSTEM
History of X The X Window System (www.x.org) was created in 1984 at the Massachusetts
Institute of Technology (MIT) by researchers working on a distributed computing
project and a campuswide distributed environment, called Project Athena. This system was not the first windowing software to run on a U N I X system, but it was the
first to become widely available and accepted. In 1985, M I T 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 2 0 0 1 ,
the Open Group released X version 11, release 6.6 ( X 1 1 R 6 . 6 ) .
The X Window System was inspired by the ideas and features found in earlier proprietary window systems but is written to be portable and flexible. X is designed to
run on a workstation, typically attached to a LAN. The designers built X with the
network in mind. If you can communicate with a remote computer over a network,
running an X application on that computer and sending the results to a local display
is straightforward.
Although the X protocol has remained stable for a long time, additions to it in the
form of extensions are quite common. One of the most interesting—albeit one that
has not yet made its way into production—is the Media Application Server, which
aims to provide the same level of network transparency for sound and video that X
does for simple windowing applications.
XFree86 and X.org Many distributions of Linux used the XFree86 X server, which inherited its license
from the original M I T X server, through release 4.3. In early 2 0 0 4 , 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 6). 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 functionally equivalent to the one distributed by XFree86 because most of the code is the same. Thus
modules designed to work with one server work with the other.
The 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 programs 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 programming easier, but also brings consistency to applications.
In recent years, the popularity of X has grown outside the U N I X 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 S Y S T E M

Figure 8-1

269

The X stack

Client/server Computer networks are central to the design of X. It is possible to run an applicaenvironment tion on one computer and display the results on a screen attached to a different
computer; the ease with which this can be done distinguishes X from other window systems available today. Thanks to this capability, a scientist can run and
manipulate a program on a powerful supercomputer in another building or
another country and view the results on a personal workstation or laptop computer. For more information refer to "Remote Computing and Local Displays" on
page 270.
When you start an X Window System session, you set up a client/server
environment. One process, called the X server, displays a desktop and windows under X.
Each application program and utility that makes a request of the X server is a client
of that server. Examples of X clients include xterm, Compiz, gnome-calculator, and
such general applications as word processing and spreadsheet programs. A typical
request from a client is to display an image or open a window.

The roles of X client and server 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 program is the client.
This disparity becomes even more apparent when you run an application program on a remote
system. You might think of the system running the program as the server and the system providing the display as the client, but in fact it is the other way around. With X, the system providing
the display is the server, and the system running the program is the client.
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.

270

CHAPTER 8

LINUX G U I s : X AND G N O M E

Separating the physical control of the display (the server) from the processes needing
access to the display (the client) makes it possible to run the server on one computer
and the client on another computer. Most of the time, this book discusses running
the X server and client applications on a single system. "Remote Computing and
Local Displays" describes using X in a distributed environment.
optional

You can run xev (X event) by giving the command x e v 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 149), you can start an X Window System server by using startx. See "rc-sysinit task and inittab" on page 4 3 9 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 D I S P L A Y S
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
global 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 turn off the X
-nolisten tcp option on the server and you must run xhost on the server to give the
client permission to connect to the X server. Unless you have a reason to leave
these features off, turn them back on when you finish with the examples in this section—leaving them off weakens system security. These tasks must be performed on
the X server because the features protect the server. You do not have to prepare the

X WINDOW SYSTEM

271

client. The examples in this section assume the server is named tiny and the client is
named dog.

Security and the X o r g -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 howX works. See "Forwarding X11" on page 681 for information on setting up
ssh so it forwards X.
THE X -nolisten tcp OPTION
As Ubuntu is installed, the X server starts with the -nolisten tcp option, which protects
the X server by preventing TCP connections to the X server. To connect to a remote X
server, you must turn this option off on the server. To turn it off, while working with
root privileges create a file named /etc/gdm/custom.conf with the following lines:
m a x @ t i n y : ~ $ cat /etc/gdm/custom.conf
[securi ty]
Di s a l 1 o w T C P = f a l se

Reboot the system to restart the X server and gdm (gdm-binary) to effect this change.
See library.gnome.org/admin/gdm/2.28/configuration.html.en#daemonconfig for
more information.
x h o s t GRANTS A C C E S S TO A DISPLAY
As installed, xhost protects each user's X server. A user who wants to grant access to his
X server needs to run xhost. Assume Max is logged in on the system named tiny and
wants to allow a user on dog to use his display (X server). Max runs this command:
m a x @ t i n y : ~ $ x h o s t +dog
dog b e i n g added t o a c c e s s c o n t r o l
list
m a x @ t i n y : ~ $ xhost
access c o n t r o l enabled, only a u t h o r i z e d
INET:dog

clients

can

connect

Without any arguments, xhost describes its state. In the preceding example, INET
indicates an IPv4 connection. If Max wants to allow all systems to access his display, he can give the following command:
$ xhost +
access c o n t r o l

disabled,

clients

can c o n n e c t f r o m 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 293)—but see the tip on the next
page regarding security and xhost. Be selective in granting access to your X display
with xhost; if another system has access to your display, you may find your work
frequently interrupted.

272

CHAPTER 8

LINUX G U I s : X AND G N O M E

Security and xhost
security Giving a remote system access to your display using xhost means any user on the remote system can
watch everything you type in a terminal emulation window, including passwords. For this reason, some
software packages, such as the Tcl/Tk development systemfyww.tel.tk),restrict their own capabilities
when xhost permits remote access to the X server. If you are concerned about security or want to take
full advantage of systems such as Tcl/Tk, you should use a safer means of granting remote access to
your X session. See the xauth man page for information about a more secure replacement for xhost.
THE D I S P L A Y VARIABLE
The most common method of identifying a display is to use the DISPLAY shell environment variable to hold the X server ID string. This locally unique identification
string is automatically set up when the X server starts. The DISPLAY variable holds
the screen number of a display:
$ echo $DISPLAY
:0.0

The format of the complete (globally unique) ID string for a display is
[hostname]:display-number[.screen-number]
where hostname is the name of the system running the X server, display-number is
the number of the logical (physical) display (0 unless multiple monitors or graphical
terminals are attached to the system, or if you are running X over ssh), and screennumber is the logical number of the (virtual) terminal (0 unless you are running multiple instances of X). When you are working with a single physical screen, you can
shorten the identification string. For example, you can use tiny:0.0 or tiny:0 to identify the only physical display on the system named tiny. When the X server and the X
clients are running on the same system, you can shorten this identification string
even further to :0.0 or :0. An ssh connection shows DISPLAY as localhost:10.0. You
may have to use ssh - X to see this value. See " X I 1 forwarding" on page 664 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 gnome-calculator, on the local system but have it use
the X display on a remote system is to change the value of the DISPLAY variable on
the client system so it identifies the remote X server.
s a m @ d o g : ~ $ export DISPLAY=tiny:0.0
s a m @ d o g : ~ $ gnome-calculator &

The preceding example shows Sam running gnome-calculator with the default X server
running on the system named tiny. After setting the DISPLAY variable to the ID of the
tiny server, all X programs (clients) Sam starts use tiny as their server (i.e., output
appears on tiny's display and input comes from tiny's keyboard and mouse). Try running xterm in place of gnome-calculator and see which keyboard it accepts input from.
If this example generates an error, refer to the two preceding sections, which explain
how to set up the server to allow a remote system to connect to it.

X WINDOW S Y S T E M

273

When you change the value of DISPLAY
tip When you change the value of the DISPLAY variable, all X programs send their output to the new
display named by DISPLAY.
THE -display OPTION
For a single command, you can usually specify the X server on the command line:
s a m @ d o g : ~ $ gnome-calculator -display tiny:0.0

Many X programs accept the -display option. Those that do not accept this option
send their output to the display specified by the DISPLAY variable.
RUNNING MULTIPLE X S E R V E R S
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 1141]). 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 2 5 6 colors at the same time.
The maximum number of bits per pixel allowed depends on the computer graphics hardware and X server. With fewer bits per pixel, the system has to transfer less data, possibly
making it more responsive. In addition, many games work with only 256 colors.
When you start multiple X servers, each must have a different ID string. The following command starts a second X server:
$ startx —

:1

The — option marks the end of the startx options and arguments. The startx script
uses the arguments to the left of this option and passes arguments to the right of this
option to the X server. When you give the preceding command in a graphical environment, such as from a terminal emulator, you must work with root privileges;
you will initiate a privileged X session. The following command starts a second X
server running at 16 bits per pixel:
$ startx -- :1 -depth 16 &

"Using Virtual Consoles" on page 149 describes how to switch to a virtual console
to start a second server where you do not have to work with root privileges.
Guest Session When you click the Session Indicator (Figure 4-2, page 101), select Guest Session
and Ubuntu starts a second X server to accommodate the guest user. When the guest
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 149) that
displays the X server you want to work with.
X over ssh See "Tunneling/Port Forwarding" on page 6 8 1 for information about running X
over an ssh connection.

STOPPING THE X S E R V E R
How you terminate a window manager depends on which window manager is running
and how it is configured. If X stops responding, switch to a virtual terminal, log in from

274

CHAPTER 8

LINUX GUIs: X AND GNOME

another terminal or a remote system, or use ssh to access the system. Then kill
(page 455) 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.

REMAPPING M O U S E BUTTONS
Throughout this book, each description of a mouse click refers to the button by its
position (left, middle, or right, with left implied when no button is specified)
because the position of a mouse button is more intuitive than an arbitrary name or
number. X numbers buttons starting at the left and continuing with the mouse
wheel. The buttons on a three-button mouse are numbered 1 (left), 2 (middle), and
3 (right). A mouse wheel, if present, is numbered 4 (rolling it up) and 5 (rolling it
down). Clicking the wheel is equivalent to clicking the middle mouse button. The
buttons on a two-button mouse are 1 (left) and 2 (right).
If you are right-handed, you can conveniently press the left mouse button with your
index finger; X programs take advantage of this fact by relying on button 1 for the
most common operations. If you are left-handed, your index finger rests most conveniently on button 2 or 3 (the right button on a two- or three-button mouse).
"Mouse Preferences" on page 105 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 - p p option to display the number of buttons X has defined for the mouse:
$ xmodmap -pp
There are 9 p o i n t e r
Physi cal
Button

buttons
Button
Code

1

1

2

2
3
4
5
6
7
8
9

3

4
5
6
7
8
9

defined.

Then expand the previous command, adding numbers to complete the list. If the
- p p option shows nine buttons, give the following command:

X WINDOW S Y S T E M

$ xmodmap -e 'pointer

275

= 3 2 1 4 5 6 7 8 9 '

Changing the order of the first three buttons is critical to making the mouse suitable
for a left-handed user. When you remap the mouse buttons, remember to reinterpret
the descriptions in this book accordingly. When this book asks you to click the left
button or does not specify which button to click, use the right button, and vice versa.

DESKTOP ENVIRONMENTS/MANAGERS
Conceptually X is very simple. As a consequence, it does not provide some of the
more common features found in GUIs, such as the ability to drag windows. The
UNIX/Linux philosophy is one of modularity: X relies on a window manager, such
as 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 G N O M E
or KDE, provides a means of launching applications and utilities, such as a file manager, that work with a window manager.

G N O M E AND K D E
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 6). For this reason the Free Software Foundation
decided to support a different project, the GNU Network Object Model Environment (GNOME). More recently Qt has been released under the terms of the GPL,
eliminating part of the rationale for GNOME's existence.
GNOME G N O M E is the default desktop environment for Ubuntu Linux. It provides a simple, coherent user interface that is suitable for corporate use. G N O M E uses G T K
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.
G N O M E does not take much advantage of its component architecture. Instead, it
continues to support the traditional U N I X philosophy of relying on many small
programs, each of which is good at doing a specific task.
KDE KDE is written in C++ on top of the Qt framework. KDE tries to use existing technology, if it can be reused, but creates its own if nothing else is available or if a
superior solution is needed. For example, KDE implemented an H T M L 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 G N O M E office applications are stand-alone programs that originated outside
the G N O M E project. KDE's portability is demonstrated by the use of most of its
core components, including Konqueror and KOffice, under M a c OS X .
Interoperability Since the release of version 2, the G N O M E project has focused on simplifying the
user interface, removing options where they are deemed unnecessary, and aiming

276

CHAPTER 8

LINUX G U I s : X AND G N O M E

O V V

sani - File B r o w s e r

File Edit View Go Bookmarks Help
..p Back

»

Lyi* forward

•Menubar

Places T-

•Main toolbar
•Location bar
•Side pane button
Side pane
Handle
•View pane
ubunLu 10.04
alternate- i306,iso

ubuuLu-10.04
desktop-138«. Iso

uburtu-10.04
server-1386, Iso

Status bar

16 items, Free space: 179.2 GB -

Figure 8-2

A Nautilus File Browser window displaying icons

for a set of default settings that the end user will not wish to change. KDE has
moved in the opposite direction, emphasizing configurability.
The freedesktop.org group (freedesktop.org), whose members are drawn from the
G N O M E 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 G N O M E 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
G N O M E projects, is creating an open-source implementation of the OPENSTEP
API and desktop environment. The result is a very clean and fast user interface.
The default look of WindowMaker, the GNUStep window manager, is somewhat
dated, but it supports themes so you can customize its appearance. The user interface is widely regarded as one of the most intuitive found on a U N I X platform.
Because GNUStep has less overhead than G N O M E and KDE, it runs better on
older hardware. If you are running Linux on hardware that struggles with G N O M E
and KDE or if 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 107 presented an introduction to
using Nautilus. This section discusses the Nautilus File Browser window in
more depth.

THE NAUTILUS FILE B R O W S E R WINDOW

Figure 8-3

277

A Nautilus File Browser window displaying a
List view and a textual location bar

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 display your
home folder in a File Browser window, select Main menu: PlacesOHome 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 an Icon
view and Figure 8-3 shows a List view. A Compact view is also available. Objects in
the View pane behave exactly as objects on the desktop do. See the sections starting
on page 101 for information on working with objects.
You can cut/copy and paste objects within a single View pane, between View panes,
or between a View pane and the desktop. The Object context menu (right-click) has
cut, copy, and paste selections. Alternatively, you can use the clipboard (page 124)
to cut/copy and paste objects.

Nautilus can open a terminal emulator
tip When you install the nautilus-open-terminal package (see page 519 for instructions) and log out
and log back in, Nautilus presents an Open in Terminal selection in context menus where appropriate. For example, with this package installed, when you right-click a folder (directory) object
and select Open in Terminal, Nautilus opens a terminal emulator with that directory as the working directory (page 204).

THE SIDE PANE
The Side pane augments the information Nautilus displays in the View pane. Press F9 or
click the X at the top of the Side pane to close it. You can display the Side pane by

278

CHAPTER 8

LINUX G U I s : X AND G N O M E

pressing F9 or by selecting File Browser menubar: Vievv"=>Side Pane. To change the
horizontal size of the Side pane, drag the handle (Figure 8-2, page 276) on its right side.
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 selections described next.
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 rightclicking the directory in Places and selecting Open in New Window. Right-click and
select Open in New Tab to open the directory in a new tab.
Places contains two parts: The list above the divider is static and holds your home
directory, your desktop, the filesystem, the network, a C D - R O M drive (when it
contains a disk), unmounted filesystems (if present), and the trash. The list below
the divider holds bookmarks. Add a bookmark by displaying the directory you
want to bookmark in the View pane and pressing CONTROL-D or by selecting File
Browser menubar: Bookmarks 1 ^ Add Bookmark. Remove a bookmark by selecting
File Browser menubar: Bookmarks 1 ^ Edit 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 or highlighted in
the View pane.
Tree Tree presents an expandable tree view of your home folder and each mounted
filesystem. Each directory in the tree has a plus (+) or minus ( - ) sign to its left.
Click a plus sign to expand a directory; click a minus sign to close a directory.
Click a directory in the tree to display that directory in the View pane. Doubleclick 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 129), 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,
page 276). From File Browser menubar: View, you can choose which of these bars
to display—except for the menubar, which Nautilus always displays.

THE NAUTILUS FILE BROWSER WINDOW

279

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
depend on what the View pane is displaying and which objects are selected. The
next section describes the menubar in detail.
Main toolbar The Main toolbar appears below the menubar and holds navigation tool icons:
Back, Forward, Up, Stop, Reload, Home, Computer, Magnification, View, and
Search. If the Main toolbar is too short to hold all icons, Nautilus displays a button
with a triangle pointing down at the right end of the toolbar. Click this button to
display a drop-down list of the remaining icons.
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 magnifications. Click
the button to the right of the right-hand magnifying glass to choose whether to view
files as icons, as a list, or in compact format. Click the magnifying glass at the right
end of the toolbar to change the Location bar into a search text box.
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.
When you display a different directory in the View pane, Nautilus changes the
Location bar back to iconic format.
In iconic format, each button represents a directory in a pathname (page 205). The
View pane displays the directory of the depressed (darker) button. Click one of
these buttons to display that directory. If the leftmost button holds a triangle that
points to the left, Nautilus is not displaying buttons for all the directories in the
absolute (full) pathname; click the button with a triangle in it to display more directory buttons.
In textual format, the text box displays the absolute pathname of the displayed
directory. To have Nautilus display another directory, enter the pathname of the
directory and press RETURN.
Status bar If no items are selected, the status bar, at the bottom of the window, indicates how
many items are displayed in the View pane. If the directory you are viewing is on
the local system, it also tells you how much free space is available on the device that
holds the directory displayed by the View pane. If an item is selected, the status bar
displays the name of the item and its size.

MENUBAR
The Nautilus File Browser menubar controls which information the File Browser displays and how it displays that information. Many of the menu selections duplicate
controls found elsewhere in the File Browser window. This section highlights some of

280

CHAPTER 8

LINUX GUIs: X AND GNOME

Q

Connect to Server

Service type: Public FTP
Server:

minors.kernel.org|

Optional information:
Port:
Folder
Add bookmark
Bookmark name:

Help

Figure 8-4

Cancel

Connect

The Connect to Server window

the selections on the menubar; click Help on the menubar and select Contents or Get
Help Online for more information. The menubar holds the menus described next.
File The several Open selections and the Property selection of File work with the highlighted object(s) in the View pane. If no objects are highlighted, these selections are
grayed out or absent. Selecting Connect to Server (also available from Main menu:
Places) displays the Connect to Server window (Figure 8-4). This window presents a
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 f t p : / / part of the URL. Fill in the optional information as appropriate. Click Connect. If the server requires authentication, Nautilus displays a window in which you can enter a username and password. Nautilus
opens a window displaying a directory on the server and an object, named for the
URL you specified, on the desktop. After you close the window, you can open the
object to connect to and display a directory on the server.
Edit Many of the Edit selections work with highlighted object(s) in the View pane; if no
objects are highlighted, these selections are grayed out or absent. This section discusses
three selections from Edit: Compress, Backgrounds and Emblems, and Preferences.
The Edit^Compress selection creates a single archive file comprising the selected
objects. This selection opens a Compress window (Figure 8-5) that allows you to
specify the name and location of the archive. The drop-down list to the right of the
text box labeled Filename allows you to specify a filename extension that determines the type of archive this tool creates. For example, .tar.gz creates a tar
(page 176) file compressed by gzip (page 175) and .tar.bz2 creates a tar file compressed by bzip2 (page 174). Click the plus sign to the left of Other Objects to specify a password for and/or to encrypt the archive (available only with certain types of
archives). You can also split the archive into several files (volumes).

THE NAUTILUS FILE B R O W S E R WINDOW

O O

281

Compress

Filename: 1 pictures
Location:

] I.tar.bz2

•

jfijsam

i Other Options
Help

Figure 8-5

The Compress window

The Edit^Backgrounds and Emblems selection has three buttons on the left: Patterns, 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
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 278).
The Edit^Preferences selection displays the File Management Preferences window
(Figure 8-6). This window has six tabs that control the appearance and behavior of
File Browser windows.
O

Ffte Management Preferences

Views Behavior Display List Columns Preview Media
Default View
View newfoldersusing:

Icon View

Arrange items:

By Name

^ Sort folders before files

1*

¿U

show hidden and backup files
Icon View Defaults
Default zoom level:

100%

T

100%

J'l

Use compart layout
f | Text beside Icons
Compact View Defaults
Default ¿oorn level:

w All columns have the same width
List View Defaults
Default ¿oorn level:

50%

Tree view Defaults
& Show only folders
Help

Figure 8-6

close

The File Management Preferences window, Views tab

282

CHAPTER 8

LINUX G U I s : X AND G N O M E

The Views tab sets several defaults, including which view the File Browser displays
(Icon, List, or Compact view), the arrangement of the objects, the default zoom
level, and default settings for the Compact view.
Delete versus The Behavior tab controls how many clicks it takes to open an object and what NauMove to Trash t i l u s d o e s w h e n 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. This tab also holds the check
box labeled Open each folder in its own window that is described in the next section.
The Display tab specifies which information Nautilus includes in object (icon) captions. The three drop-down lists specify the order in which Nautilus displays information as you increase the zoom level of the View pane. This tab also specifies the
date format Nautilus uses.
The List Columns tab specifies which columns Nautilus displays, and in what order
it displays them, in the View pane when you select List View.
The Preview tab controls when Nautilus displays or plays previews of files (Always,
Local Files Only, Never).
The Media tab specifies which action Nautilus takes when you insert media such as
a CD/DVD, or connect devices such as a USB flash drive, to the system.
View Click the Main Toolbar, Side Pane, Location Bar, and Statusbar selections in the View
submenu to display or remove these elements from the window. The Show Hidden
Files selection displays in the View pane those files with hidden filenames (page 204).
Go The Go selections display various folders in the View pane.
Bookmarks Bookmarks appear at the bottom of this menu and in the Side pane under Places.
The Bookmarks selections are explained under "Places" on page 2 7 8 .
Help The Help selections display local and online information about Nautilus.

optional

THE NAUTILUS SPATIAL VIEW
Nautilus gives you two ways to work with files: the traditional File Browser view
described in the previous section and the innovative Spatial view shown in
Figure 8-7. By default, Ubuntu displays the Browser view.
The Nautilus Spatial (as in "having the nature of space") view has many powerful
features but may take some getting used to. It always provides one window per
folder. By default, when you open a folder, Nautilus displays a new window.
Turn on the Spatial view by selecting File Browser menubar: Edit^Preferences.
Then click the Behavior tab in the File Management Preferences window and put a
tick in the check box labeled Open each folder in its own window, click Close, and
close the File Browser window. Next time you open a File Browser window, it will
display a Spatial view.

THE NAUTILUS S P A T I A L V I E W

0

0

9

s a m

File Edit View Places Help

Parent-folders
button and
pop-up menu

«Elia

echo
ecjio
f ctio

addit

L-

all.tar.bz

Examples

283

u
bailey4.jpg

letter

items. Free space: 179.2 GB
Figure 8-7

The Nautilus Spatial view

To open a Spatial view of your home directory, Select Main menu: Home Folder and
experiment as you read this section. If you double-click the Desktop icon in the Spatial view, Nautilus opens a new window that displays the Desktop folder.

You can turn off the Nautilus Spatial view
To turn off the Nautilus Spatial view, open a File Browser window. From the menubar, open the File
Management Preferences window by selecting EditoPreferences. Click the Behavior tab in this
window and remove the tick from the check box labeled Open each folder its own window.
A Spatial view can display icons, a list of filenames, or a compact view. To select
your preferred format, click View on the menubar and choose Icons, List, or Compact. To create files to experiment with, right-click in the window (not on an icon)
to display the Nautilus context menu and select Create Folder or Create Document.

Use SHIFT to close the current window as you open another window
If you hold the SHIFT key down when you double-click to open a new window, Nautilus closes the
current window as it opens the new one. This behavior may be more familiar and can help keep
the desktop from becoming cluttered. If you do not want to use the keyboard, you can achieve the
same result by double-clicking the middle mouse button.
Window memory Move the window by dragging the titlebar. The Spatial view has window
memory—
that is, the next time you open that folder, Nautilus opens it at the same size and in
the same location. Even the scrollbar will be in the same position.
The key to closing the current window and returning to the window of the parent
button directory is the Parent-folders button (Figure 8-7). Click this button to display the
Parent-folders pop-up menu. Select the directory you want to open from this menu.
Nautilus then displays in a Spatial view the directory you specified.

Parent-folders

From a Spatial view, you can open a folder in a traditional view by right-clicking the
folder and selecting Browse Folder.

284

CHAPTER 8

LINUX GUIs: X AND GNOME

O

Appearance Preferences

Theme Background Font«; Visual Effects
Application font:

Sam

10

Document font;

i

Sans

10

Desktop font

Sans

10

Window title foot:
Fixed width font:

.
|

Sans Bold
Honosp«icc

10

Rendering
Monochiorne

[ a b e f g o p A O abefgop]
Best contrast

a b e f g o p A O abefgop|

Besl shapes

|abcfgop AO

abcfgop\

a Subpixel smoothing (LCDS)

[abefgop A O

abefgop\
Details..

Help

Figure 8-8

dose

The Appearance Preferences window, Fonts tab

G N O M E UTILITIES
G N O M E 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 G N O M E .

FONT PREFERENCES
The Fonts tab of the Appearance Preferences window (Figure 8-8) enables you to
change the font G N O M E uses for applications, documents, the desktop, window
titles, and terminal emulators (fixed width). To display this window, select Main
menu: SystemOPreferences 1 ^Appearance 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).
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 monitors. Click Details to refine the font rendering further, again picking the box in each
section 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
the previous section. From this window you can select a font family, a style, and a
size. A preview of your choice appears in the Preview frame in the lower part of the
window. Click OK when you are satisfied with your choice.

G N O M E UTILITIES

0

285

Pick a Font

Family:
Rachana
Rekha

Style:

Size:

Regular

H

Italic

Saab
Sans
Sawasdee
serif

Bold

8

Bold Italic

9

•0

A

=]

10
11
12
1

Standard Symbols L

T

Preview:
abcdefghijk ABCOEFGHIJK
Cancel

Figure 8-9

OK

The Pick a Font window

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 114) or a
panel. To specify a color for a panel, right-click the panel to display its context menu,
select Properties, 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.
O

Pick a Color

Figure 8-10

The Pick a Color window

286

CHAPTER 8

LINUX GUIs: X AND GNOME

RUN APPLICATION WINDOW
The Run Application window (Figure 4-4, page 103) enables you to run a program
as though you had initiated it from a command line. To display the Run Application
window, press ALT-F2. Enter a command in the text box. As soon as G N O M E 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.tiny, in a terminal emulator window.

SEARCHING FOR FILES
The Search for Files window (Figure 8-11) can help you find files whose locations
or names you do not know or have forgotten. To open this window, select Main
menu: Places "^Search for Files or enter gnome-search-tool on a command line from
a terminal emulator or Run Application window (ALT-F2). To search by filename or
partial filename, enter the (partial) filename in the combo box labeled Name contains and then select the folder you want to search in from the drop-down list
labeled Look in folder. When G N O M E 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; G N O M E
opens the Browse/Save window (page 110). Once you have entered the search criteria, click Find. G N O M E displays the list of files matching the criteria in the list box
labeled Search results. Double-click a file in this list box to open it.
To refine the search, you can enter more search criteria. Click the plus sign to the
left of Select more options to expand the window and display more search criteria.
O $ O

10 Flies Found - search for Flies

Name contains: [mailbox
Look in folder:

Hie system

* selcct more options
search results:
Name

10 files found
Polder

mal I box. py /usr>11 b/py modul es/python2.6/papyon/event
rndilbox.pyt /um /I i b/py modul ev'pylhui i2,6/papy on/ev ei i L
rndilbox.py fust A i U(py modul es/py lhun2.6/pdpyor i/rnsnp
mail box. pyc fusrA 1 h/py modul es/python2,6/papyon/msnp

Help

Figure 8-11

Close

The Search for Files window

Find

G N O M E

O d d

UTILITIES

2 8 7

s e a r c h for Files

Nftme rontnins:
Look in folder:
:,cle: : m o r e options
c o n t a i n s the
Available opUoifo:

text:
DdLtf modified L^s iIihi i

Kemove
*

Add

11 r e s u l t s .

Help

Figure 8-12

The Search for Files window with Select more options expanded

GNOME initially displays one search criterion and a line for adding another criterion as shown 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.
If nothing is entered in this text box, the search matches all files. You can leave this
text box as is or remove the line by clicking Remove at the right end of the line. To
search for a file that contains a specific string of characters (text), enter the string in
this text box.
To add search criteria, make a selection from the list box labeled Available options
and click Add to the right of the drop-down list. To remove criteria, click Remove
at the right end of the line that holds the criterion you want to remove.
To select files that were modified fewer than a specified number of days ago, select
Date modified less than from the drop-down list labeled Available options and click
Add. The Search for Files window adds a line with a spin box labeled Date modified
less than. With this spin box showing 0 (zero), as it does initially, no file matches the
search criteria. Change this number as desired and click Find to begin the search.

G N O M E TERMINAL EMULATOR/SHELL
The G N O M E terminal emulator displays a window that mimics a characterbased terminal (page 125). To display a terminal emulator window, select Main
menu: Applications "^Accessories1^Terminal or enter gnome-terminal on a command line or from a Run Application window (ALT-F2). When the G N O M E terminal emulator is already displayed, select Terminal menubar: File^Open Terminal
or right-click within the Terminal window and select Open Terminal to display a
new terminal emulator window.
To open an additional terminal session within the same Terminal window, rightclick the window and select Open Tab from the context menu or select Terminal

288

CHAPTER 8

LINUX G U I s : X AND G N O M E

menubar: File^Open Tab. A row of tabs appears below the menubar as gnometerminal opens another terminal session on top of the existing one. Add as many terminal sessions as you like; click the tabs to switch between sessions.

GNOME terminal emulator shortcuts
tip While using the GNOME terminal emulator, CONTROL-SHIFT-N opens a new window and
CONTROL-SHIFT-T opens a new tab. New windows and tabs open to the working directory. In addition,
you can use CONTROL-PAGE UP and CONTROL-PAGE DOWN to switch between tabs.
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, G N O M E gives you a
choice of profiles, if more than one is available. You can add and modify profiles,
including the Default profile, by selecting Terminal menubar: Edit^Profiles. Highlight the profile you want to modify or click New to design a new profile.

CHAPTER SUMMARY
The X Window System GUI is portable and flexible and makes it easy to write
applications that work on many different types of systems without having to know
low-level details for the individual systems. This GUI can operate in a networked
environment, allowing a user to run a program on a remote system and send the
results to a local display. The client/server concept is integral to the operation of the
X Window System, in which the X server is responsible for fulfilling requests made
of X Window System applications or clients. Hundreds of clients are available that
can run under X . Programmers can also write their own clients, using tools such as
the GTK+ and GTK+2 G N O M E libraries to write G N O M E programs and the Qt
and KDE libraries to write KDE programs.
The window managers, and virtually all X applications, are designed to help users
tailor their work environments in simple or complex ways. You can designate applications that start automatically, set such attributes as colors and fonts, and even
alter the way keyboard strokes and mouse clicks are interpreted.
Built on top of the X Window System, the G N O M E desktop manager can be used
as is or customized to better suit your needs. It is a graphical user interface to system services (commands), the filesystem, applications, and more. Although not part
of G N O M E , the Metacity and Compiz window managers work closely with
G N O M E and are the default window managers for G N O M E under Ubuntu. A window manager controls all aspects of the windows, including placement, decoration,
grouping, minimizing and maximizing, sizing, and moving.
The Nautilus File Browser window is a critical part of G N O M E ; the desktop is a
modified File Browser window. The File Browser View pane displays icons or a list
of filenames you can work with. The Side pane, which can display six types of
information, augments the information Nautilus displays in the View pane.

ADVANCED E X E R C I S E S

289

GNOME also provides many graphical utilities you can use to customize and work
with the desktop. It supports M I M E types; thus, when you double-click an object,
GNOME generally knows which tool to use to display the data represented by the
object. In sum, GNOME is a powerful desktop manager that can make your job
both easier and more fun.

EXERCISES
1. a. What is Nautilus?
b. List four things you can do with Nautilus.
c. How do you use Nautilus to search for a file?
2. What is a terminal emulator? What does it allow you to do from a GUI
that you would not be able to do without one?
3. How would you search the entire filesystem for a file named today.odt?
4. a. List two ways you can open a file using Nautilus.
b. How does Nautilus "know" which program to use to open different
types of files?
c. What are the three common Nautilus control bars? Which kinds of
tools do you find on each?
d. Discuss the use of the Nautilus location bar in textual mode.

ADVANCED EXERCISES
5. Assume you are using a mouse with nine pointer buttons defined. How
would you reverse the effects of using the mouse wheel?
6. a. How would you use Nautilus to connect to the FTP server at
ftp.ubuntu.com?
b. Open the following folders: ubuntu, dists, and lucid. How would you
copy the file named Contents-i386.gz to the desktop? What type of file
is Contents-i386.gz?
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 set up by the X Window System.
How does the X server work? List three X clients. Where is the client and
where is the server when you log in on a local system? What is an advantage of this setup?

290

CHAPTER 8

LINUX G U I s : X AND G N O M E

8. Run xwininfo from a terminal emulator window and answer these questions:
a. What does xwininfo do?
b. What does xwininfo give as the name of the window you clicked? Does
that agree with the name in the window's titlebar?
c. What is the size of the window? In which units does xwininfo display this
size? What is the depth of a window?
d. How can you get xwininfo to display the same information without having to click the window?
9. Find and install xeyes (not tuxeyes). Write an xeyes command to display a
window that is 6 0 0 pixels wide and 4 0 0 pixels tall, is located 2 0 0 pixels
from the right edge of the screen and 3 0 0 pixels from the top of the screen,
and contains orange eyes outlined in blue with red pupils. (Hint: Refer to
the xeyes m a n page.)

9
THE BOURNE AGAIN
SHELL
IN THIS CHAPTER
Startup Files

293

Redirecting Standard Error

297

Writing a Simple Shell S c r i p t . . . . 3 0 0
Job Control

307

M a n i p u l a t i n g the Directory
Stack

310

Parameters and Variables

312

Processes

328

History

330

Reexecuting and Editing
Commands

332

Functions

349

Controlling bash: Features and
Options

352

Processing the Command L i n e . . . 3 5 6

This chapter picks up where Chapter 7 left off. Chapter 27
expands on this chapter, exploring control flow commands and
more advanced aspects of programming the Bourne Again Shell
(bash). The bash home page is at www.gnu.org/software/bash.
The bash info page is a complete Bourne Again Shell reference.
The Bourne Again Shell is a command interpreter and high-level
programming language. As a command interpreter, it processes
commands you enter on the command line in response to a
prompt. When you use the shell as a programming language, it
processes commands stored in files called shell scripts. Like other
languages, shells have variables and control flow commands (for
example, for loops and if statements).
When you use a shell as a command interpreter, you can customize the environment you work in. You can make your
prompt display the name of the working directory, create a
function or an alias for cp that keeps it from overwriting certain
kinds of files, take advantage of keyword variables to change
aspects of how the shell works, and so on. You can also write
shell scripts that do your bidding—anything from a one-line

291

292

CHAPTER 9

THE B O U R N E AGAIN S H E L L

script that stores a long, complex command to a longer script that runs a set of
reports, prints them, and mails you a reminder when the job is done. More complex
shell scripts are themselves programs; they do not just run other programs.
Chapter 2 7 has some examples of these types of scripts.
Most system shell scripts are written to run under bash (or dash; see below). If you
will ever work in recovery mode—when you boot the system or perform system
maintenance, administration, or repair work, for example—it is a good idea to
become familiar with this shell.
This chapter expands on the interactive features of the shell described in Chapter 7,
explains how to create and run simple shell scripts, discusses job control, introduces
the basic aspects of shell programming, talks about history and aliases, and
describes command-line expansion. Chapter 2 7 presents some more challenging
shell programming problems.

BACKGROUND
The Bourne Again Shell is based on the Bourne Shell (the early U N I X shell; this
book refers to it as the original Bourne Shell to avoid confusion), which was written
by Steve Bourne of AT&T's Bell Laboratories. Over the years the original Bourne
Shell has been expanded but it remains the basic shell provided with many commercial versions of UNIX.
sh Shell Because of its long and successful history, the original Bourne Shell has been used
to write many of the shell scripts that help manage U N I X 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 U N I X systems the original Bourne Shell is
named sh.
dash Shell The bash executable file is about 8 0 0 kilobytes, has many features, and is well
suited as a user login shell. The dash (Debian Almquist) shell is about 100 kilobytes, offers Bourne Shell compatibility for shell scripts (noninteractive use), and,
because of its size, can load and execute shell scripts much more quickly than
bash. 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 many Linux systems sh is a symbolic link to bash, ensuring scripts that require
the presence of the Bourne Shell still run. When called as sh, bash does its best to
emulate the original Bourne Shell.

SHELL BASICS

293

Korn Shell System V U N I X introduced the Korn Shell (ksh), written by David Korn. This shell
extended many features of the original Bourne Shell and added many new features.
Some features of the Bourne Again Shell, such as command aliases and commandline editing, are based on similar features from the Korn Shell.
POSIX The POSIX (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.
POSIX standard 1003.2 describes shell functionality. The Bourne Again Shell provides the features that match the requirements of this standard. Efforts are under
way to make the Bourne Again Shell fully comply with the POSIX standard. In the
meantime, if you invoke bash with the — p o s i x option, the behavior of the Bourne
Again Shell will 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, implementing
job control, and manipulating the directory stack.

chsh: changes your login shell
tip The person who sets up your account determines which shell you use when you first log in on the
system or when you open a terminal emulator window in a GUI environment. Under Ubuntu, bash
is the default shell. You can run any shell you like once you are logged in. Enter the name of the
shell you want to use (bash, tcsh, or another shell) and press RETURN; the next prompt will be
that of the new shell. Give an exit command to return to the previous shell. Because shells you
call in this manner are nested (one runs on top of the other), you will be able to log out only from
your original shell. When you have nested several shells, keep giving exit commands until you
reach your original shell. You will then be able to log out.
Use the chsh utility to change your login shell permanently. First give the command chsh. In
response to the prompts, enter your password and the absolute pathname of the shell you want
to use (/bin/bash, /bin/tcsh, or the pathname of another shell). When you change your login shell
in this manner using a terminal emulator (page 125) under a GUI, subsequent terminal emulator
windows will not reflect the change until you log out of the system and log back in. See page 457
for an example of how to use chsh.

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

294

CHAPTER 9

THE BOURNE AGAIN S H E L L

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 S H E L L S
The files covered in this section are executed by login shells and shells that you start
with the bash —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
privileges can set up this file to establish systemwide default characteristics for users
running bash.

,bash_profile

Next the shell looks for ~/.bash_profile, ~/.bash_login, and -/.profile (-/ is shorthand for your home directory), in that order, executing the commands in the first of
these files it finds. You can put commands in one of these files to override the
defaults set in /etc/profile. A shell running on a virtual terminal does not execute
commands in these files.

.bashjogin
.profile

.bashjogout

When you log out, bash executes commands in the ~/.bash_logout file. This file
often holds commands that clean up after a session, such as those that remove
temporary files.

INTERACTIVE NONLOGIN S H E L L S
The commands in the preceding startup files are not executed by interactive, nonlogin shells. However, these shells inherit values from the login shell variables that
are set by these startup files.
/etc/bashrc

Although not called by bash directly, many - / . b a s h r c 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 - / . b a s h r c file. Typically a
startup file for a login shell, such as .bash_profile, runs this file, so both login and
nonlogin shells run the commands in .bashrc.

NON INTERACTIVE S H E L L S
The commands in the previously described startup files are not executed by noninteractive shells, such as those that runs shell scripts. However, these shells inherit
login shell variables that are set by these startup files.
BASH_ENV

Noninteractive shells look for the environment variable BASH_ENV (or ENV if the
shell is called as sh) and execute commands in the file named by this variable.

SETTING U P 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

SHELL BASICS

295

following in .bash_profile run commands from .bashrc for login shells (when
.bashrc exists). With this setup, the commands in .bashrc are executed by login and
nonlogin shells.
if

[ -f

-/.bashrc

];

then

. -/.bashrc;

fi

The [ - f -/.bashrc ] tests whether the file named .bashrc in your home directory
exists. See pages 955 and 957 for more information on test and its synonym [ ]. See
page 296 for information on the . (dot) builtin.

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 319) and should go in ,bash_profile:
PATH=$PATH:$HOME/bi n

When you put this command in .bash_profile and not in .bashrc, the string is added to the PATH
variable only once, when you log in.
Modifying a variable in .bash_profile causes changes you make in an interactive session to propagate to subshells. In contrast, modifying a variable in .bashrc overrides changes inherited from
a parent shell.
Sample .bash_profile and .bashrc files follow. Some commands used in these files
are not covered until later in this chapter. In any startup file, you must export variables and functions that you want to be available to child processes. For more information refer to "Locality of Variables" on page 992.
$ cat ~/.bash_profile
i f [ - f - / . b a s h r c ]; then
. -/.bashrc
fi
PATH=$PATH:.
e x p o r t P S l = ' [ \ h \W \ ! ] \ $ '

# Read l o c a l

startup

file

if

# Add t h e w o r k i n g d i r e c t o r y
# Set prompt

it
to

exists
PATH

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 319). 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 321), which controls the user's prompt.
A sample .bashrc file is shown on the next page. The first command executes the
commands in the /etc/bashrc file if it exists. Next the file sets and exports the LANG
(page 326) and VIMINIT (for vim initialization) variables and defines several aliases.
The final command defines a function (page 349) that swaps the names of two files.

296

CHAPTER 9

THE BOURNE AGAIN SHELL
$ cat ~/.bashrc
i f [ - f / e t c / b a s h r c ]; then
source / e t c / b a s h r c
fi

# read g l o b a l

startup

file

if

set -o noclobber
u n s e t MAILCHECK
e x p o r t LANG=C
export VIMINIT='set ai
alias df='df -h'
a l i a s r m = ' rm - i '
a l i a s l t = ' l s - 1 t r h | t: a i 1 '
a l i a s h='hi story | tai 1'
a l i a s c h = ' c h m o d 75 5 '

#
#
#
#
#
#

function
{

# a f u n c t i o n t o exchange the
# o f two f i l e s

switch()

it

prevent overwriting f i l e s
t u r n o f f " y o u h a v e new m a i l "
s e t LANG v a r i a b l e
set vim options
s e t up a l i a s e s
a l w a y s do i n t e r a c t i v e r m ' s

exists

notice

names

local tmp=$$switch
mv " $ 1 " $ t m p
mv " $ 2 " " $ 1 "
mv $ t m p " $ 2 "

}

. (DOT) OR source: RUNS A STARTUP FILE IN THE
CURRENT S H E L L
After you edit a startup file such as .bashrc, you do not have to log out and log in
again to put the changes into effect. Instead, you can run the startup file using the .
(dot) or source builtin (they are the same command). As with all other commands,
the . must be followed by a SPACE on the command line. Using . or source is similar to
running a shell script, except these commands run the script as part of the current
process. Consequently, when you use . or source to run a script, changes you make
to variables from within the script affect the shell you run the script from. If you ran
a startup file as a regular shell script and did not use the . or source builtin, the variables created in the startup file would remain in effect only in the subshell running
the script—not in the shell you ran the script from. You can use the . or source command to run any shell script—not just a startup file—but undesirable side effects
(such as changes in the values of shell variables you rely on) may occur. For more
information refer to "Locality of Variables" on page 992.
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
export
export
export
stty k

bashrc
TERM=vtl00
PSl="$(hostname
CDPATH=:SHOME
i l l 'Au'

$ . -/•bashrc
bravo.example.com:

-f):

#
" #
#
#

set
set
add
set

the terminal type
the prompt s t r i n g
HOME t o CDPATH s t r i n g
k i l l line to control-u

SHELL BASICS

297

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 in this book.

Table 9-1

Builtin commands that are symbols

Symbol

Command

0

Subshell (page 306)

$0

Command substitution (page 362)

(( ))

Arithmetic evaluation; a synonym for let (use when the enclosed value contains an equal sign; page 1016)

$(())

Arithmetic expansion (not for use with an enclosed equal sign; page 360)

[]

The test command (pages 955 and 957)

[[]]

Conditional expression; similarto [ ] but adds string comparisons (page 1017)

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 directs standard error to
the screen. Unless you redirect one or the other, you may not know the difference
between the output a command sends to standard output and the output it sends
to standard error. This section describes the syntax used by the Bourne Again
Shell to redirect standard error and to distinguish between standard output and
standard error.
File descriptors A file descriptor is the place a program sends its output to and gets its input from.
When you execute a program, Linux opens three file descriptors for the program: 0
(standard input), 1 (standard output), and 2 (standard error). The redirect output
symbol (> [page 246]) is shorthand for 1>, which tells the shell to redirect standard
output. Similarly < (page 247) is short for 0<, which redirects standard input. The
symbols 2> redirect standard error. For more information refer to "File Descriptors" on page 987.
The following examples demonstrate how to redirect standard output and standard
error to different files and to the same file. When you run the cat utility with the
name of a file that does not exist and the name of a file that does exist, cat sends an
error message to standard error and copies the file that does exist to standard output. Unless you redirect them, both messages appear on the screen.

298

CHAPTER 9

THE BOURNE AGAIN SHELL
$ cat y
This is y.
$ cat x
cat:

x:

No s u c h f i l e

or

directory

or

directory

$ cat x y
cat: x:
This is

No s u c h f i l e
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
c a t : x : No s u c h f i l e
$ cat hold
This is y.

or

directory

Similarly, when you send standard output through a pipe, standard error is not
affected. The following example sends standard output of cat through a pipe to tr,
which in this example converts lowercase characters to uppercase. (See the tr info
page for more information.) The text that cat sends to standard error is not translated because it goes directly to the screen rather than through the pipe.
$ cat x y | tr "[a-z]" "[A-Z]"
c a t : x : No s u c h f i l e o r d i r e c t o r y
THIS I S Y.

The following example redirects standard output and standard error to different
files. The token following 2> tells the shell where to redirect standard error (file
descriptor 2). The token following 1> tells the shell where to redirect standard output (file descriptor 1). You can use > in place of 1>.
$ cat x y 1> holdl 2> hold2
$ cat holdl
This is y.
$ cat hold2
c a t : x : No s u c h f i l e o r d i r e c t o r y

Combining In the next example, the & > token redirects standard output and standard error to
standard output and
standard error

a

single file:

$ cat x y &> hold
$ cat hold
c a t : x : No s u c h f i l e
This is y.

or

directory

Duplicating a file In the next example, first 1> redirects standard output to hold and then 2 > & 1 declares
descriptor 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
c a t : x : No s u c h f i l e o r
This is y.

directory

In this case, 1> hold precedes 2>&1. If they had been listed in the opposite order, standard error would have been made a duplicate of standard output before standard

SHELL BASICS

299

output was redirected to hold. Only standard output would have been redirected to
hold in that scenario.
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 F I L E OR DIRECTORY
THIS I S Y.

Sending errors to You can use 1 > & 2 to redirect standard output of a command to standard error,
standard error Shell scripts use this technique to send the output of echo to standard error. In the
following script, standard output of the first echo is redirected to standard error:
$ cat message_demo
e c h o T h i s i s an e r r o r m e s s a g e . 1>&2
e c h o T h i s i s n o t an e r r o r m e s s a g e .

If you redirect standard output of message_demo, error messages such as the one produced by the first echo appear on the screen because you have not redirected standard
error. Because standard output of a shell script is frequently redirected to another file,
you can use this technique to display on the screen any error messages generated by
the script. The Inks script (page 962) 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 1007).
The Bourne Again Shell supports the redirection operators shown in Table 9-2.

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 248) is set. If noclobber is not set, this redirection creates filename s it
does not exist and overwrites it if it does exist.

>| filename

Redirects standard output to filename, even if the file exists and noclobber
(page 248) is set.

»filename

Redirects and appends standard output to filename unless filename exists and
noclobber (page 248) is set. If noclobber is not set, this redirection creates
filename if it does not exist.

&> filename

Redirects standard output and standard error to filename.

<&m

Duplicates standard input from file descriptor m (page 988).

[n]>&m

Duplicates standard output or file descriptor n if specified from file descriptor
m (page 988).

[n]<&-

Closes standard input or file descriptor n if specified (page 988).

[n]>&-

Closes standard output or file descriptor n if specified.

300

CHAPTER 9

THE BOURNE AGAIN S H E L L

WRITING A SIMPLE SHELL SCRIPT
A shell script is a file that holds commands that the shell can execute. The commands
in a shell script can be any commands you can enter in response to a shell prompt.
For example, a command in a shell script might run a Linux utility, a compiled program, or another shell script. Like the commands you give on the command line, a
command in a shell script can use ambiguous file references and can have its input or
output redirected from or to a file or sent through a pipe. You can also use pipes and
redirection with the input and output of the script itself.
In addition to the commands you would ordinarily use on the command line, control
flow commands (also called control structures) find most of their use in shell scripts.
This group of commands enables you to alter the order of execution of commands in
a script in the same way you would alter the order of execution of statements using a
structured programming language. Refer to "Control Structures" on page 954 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: M A K E S A FILE EXECUTABLE
To execute a shell script by giving its name as a command, you must have permission
to read and execute the file that contains the script (refer to "Access Permissions" on
page 215). 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
who

"Users

Currently

Logged

In"

$ ./whoson
bash:

./whoson:

Permission

denied

You cannot execute whoson by giving its name as a command because you do not
have execute permission for the file. The shell does not recognize whoson as an executable file and issues the error message Permission denied when you try to execute it.
(See the tip on the next page if you get a command not found error message.) When
you give the filename as an argument to bash (bash whoson), bash takes the argument to be a shell script and executes it. In this case bash is executable and whoson is
an argument that bash executes so you do not need to have execute permission to
whoson. You must have read permission.

SHELL BASICS

$ Is -1 whoson
-nQ-w-r-1 max g r o u p 4 0 May 2 4 1 1 : 3 0

whoson

$ chmod u+x whoson
$ Is -1 whoson
-n^^w-r-1 max g r o u p 4 0 May 2 4 1 1 : 3 0

whoson

$ ./whoson
Mon May 2 5 1 1 : 4 0 : 4 9 PDT 2 0 1 0
Users C u r r e n t l y Logged I n
zach
pts/7
2010-05-23
his
pts/1
2010-05-24
sam
pts/12
2010-05-24
max
pts/4
2010-05-24

Figure 9-1

18
09
06
09

301

17
59
29 ( b r a v o . e x a m p l e . c o m )
08

Using chmod to make a shell script executable

The chmod utility changes the access privileges associated with a file. Figure 9-1
shows Is with the -1 option displaying the access privileges of whoson before and
after chmod gives execute permission to the file's owner.
The first Is 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: u + x causes chmod to add (+) execute permission (x) for the owner (u). (The u
stands for user, although it means the owner of the file.) The second argument is the
name of the file. The second Is shows an x in the fourth position, indicating that the
owner has execute permission.

Command not found?
tip If you give the name of a shell script as a command without including the leading ./, the shell typically displays the following error message:
$ whoson
bash: whoson:

command n o t

found

This message indicates 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 the shell searches the working directory automatically, see the section about PATH
on page 319.
If other users will execute the file, you must also change group and/or public access
permissions for the file. Any user must have execute access to use the file's name as a
command. If the file is a shell script, the user trying to execute the file must have read
access to the file as well. You do not need read access to execute a binary executable
(compiled program).

302

CHAPTER 9

THE BOURNE AGAIN SHELL

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 2 1 5
as well as the discussions of Is (page 215) and chmod (page 216).

#! S P E C I F I E S A S H E L L
You can put a special sequence of characters on the first line of a shell script to tell
the operating system which shell (or other program) should execute the file. Because
the operating system checks the initial characters of a program before attempting to
execute it using exec, these characters save the system from making an unsuccessful
attempt. If #! are the first two characters of a script, the system interprets the characters that follow as the absolute pathname of the 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:
$ cat bash_script
# ! / b i n/bash
echo " T h i s i s a Bourne Again S h e l l

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 next example shows a
script that should be executed by tcsh (part of the tcsh package):
$ cat tcsh_script
#!/bi n/tcsh
echo " T h i s i s a t c s h s c r i p t . "
set person = zach
echo "person i s Sperson"

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 executing the script. The three lines that ps displays in the following example show the
process running the parent bash shell, the process running the tcsh script, and the
process running the ps command:
$ cat tcsh_script2
#!/bi n/tcsh
ps - f
$ ./tcsh_script2
UID
PID
PPID
max
B 0 B 1 B0B0
max
9358
3031
max
9375
9358

C
0
0
0

STIME
Novl6
21:13
21:13

TTY
pts/4
pts/4
pts/4

£
£
£

T I M E CMD
-bash
/bin/tcsh
ps - f

./tcsh_script2

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 script will run under bash and may generate error messages or not run properly.

SHELL BASICS

303

# B E G I N S A COMMENT
Comments make shell scripts and all code easier to read and maintain by you and
others. If a hashmark (#) in the first character position of the first line of a script is
not immediately followed by an exclamation point (!) or if a hashmark occurs in
any other location in a script, the shell interprets it as the beginning of a comment.
The shell then ignores everything between the hashmark and the end of the line (the
next NEWLINE character).

EXECUTING A S H E L L S C R I P T
fork and exec A command on the command line causes the shell to fork a new process, creating a
system calls duplicate of the shell process (a subshell). The new process attempts to exec (execute) 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 compiled
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 subshell takes its input from a file—namely, the shell script.
As discussed earlier, you can run commands in a shell script file that you do not
have execute permission for by using a bash command to exec a shell that runs the
script directly. In the following example, bash creates a new shell that takes its input
from the file named whoson:
$ bash whoson

Because the bash command expects to read a file containing commands, you do not
need execute permission for whoson. (You do need read permission.) Even though
bash reads and executes the commands in whoson, standard input, standard output,
and standard error remain directed from/to the terminal.
Although you can use bash to execute a shell script, this technique causes the script
to run more slowly than giving yourself execute permission and directly invoking the
script. Users typically prefer to make the file executable and run the script by typing
its name on the command line. It is also easier to type the name, and this practice is
consistent with the way other kinds of programs are invoked (so you do not need to
know whether you are running a shell script or an executable file). However, if bash
is not your interactive shell or if you want to see how the script runs with different
shells, you 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.

304

CHAPTER 9

THE B O U R N E AGAIN S H E L L

; AND NEWLINE SEPARATE COMMANDS
The NEWLINE character is a unique command separator because it initiates execution
of the command preceding it. You have seen this behavior throughout this book
each time you press the RETURN key at the end of a command line.
The semicolon (;) is a command separator that does not initiate execution of a command and does not change any aspect of how the command functions. You can execute a series of commands sequentially by entering them on a single command line
and separating each from the next with a semicolon (;). You initiate execution of the
sequence of commands by pressing RETURN:
$ x ; y ; z
If x , y, and z are commands, the preceding command line yields the same results as
the next three commands. The difference is that in the next example the shell issues
a prompt after each of the commands ( x , y, and z) finishes executing, whereas the
preceding command line causes the shell to issue a prompt only after z is complete:
S x
$ y
S 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 the
shell does not treat the NEWLINE as a command terminator. Enclosing a backslash
within single quotation marks or preceding it with another backslash turns off the
power of a backslash to quote special characters such as NEWLINE. Enclosing a backslash within double quotation marks has no effect on the power of the backslash.
Although you can break a line in the middle of a word (token), it is typically simpler
to break a line immediately before or after whitespace.
optional

You can enter a RETURN in the middle of a quoted string on a command line without
using a backslash. The NEWLINE (RETURN) you enter will then be part of the string:
$ echo "Please enter the three values
> required to complete the transaction."
Please enter the three values
required to complete the transaction.

In the three examples in this section, the shell does not interpret RETURN as a command
terminator because it occurs within a quoted string. The greater than (>) sign is a secondary prompt (PS2; page 322) indicating the shell is waiting for you to continue the

SHELL BASICS

305

unfinished command. In the next example, the first RETURN is quoted (escaped) so the
shell treats it as a separator and does not interpret it literally.
$ echo "Please enter the three values \
> required to complete the transaction."
Please enter

the

three values

required

to complete

the

transaction.

Single q u o t a t i o n marks cause the shell t o interpret a backslash l i t e r a l l y :
$ echo 'Please enter the three values \
> required to complete the transaction.'
Please enter the three values \
required to complete the transaction.

| AND & SEPARATE COMMANDS AND DO SOMETHING E L S E
The pipe symbol (I) and the background task symbol (&) are also command separators. They do not start execution of a command but do change some aspect of
how the command functions. The pipe symbol alters the source of standard input
or the destination of standard output. The background task symbol causes the
shell to execute the task in the background and display a prompt immediately; you
can continue working on other tasks.
Each of the following command lines initiates a single job comprising three tasks:
$ x | y | z
$ Is -1 | 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 Is -1 command, task y is grep
tmp, and task z is the pager less. The shell displays a long (wide) listing of the files
in the working directory that contain the string tmp, piped through less.
The next command line executes tasks d and e in the background and task f in the
foreground:
$ d & e & f
[1] 14271
[ 2 ] 14272

The shell displays the job number between brackets and the PID number for each
process running in the background. It displays a prompt as soon as f finishes, which
may be before d or e finishes.
Before displaying a prompt for a new command, the shell checks whether any
background jobs have completed. For each completed job, the shell displays its
job number, the word Done, and the command line that invoked the job; the shell
then displays a prompt. When the job numbers are listed, the number of the last
job started is followed by a + character and the job number of the previous job is

306

CHAPTER 9

THE BOURNE AGAIN S H E L L

followed by a - character. Other jobs are followed by a SPACE character. After running the last command, the shell displays the following lines before issuing a
prompt:
[1][2]+

Done
Done

d
e

The next command line executes all three tasks as background jobs. The shell displays
a shell prompt immediately:
$ d
[1]
[2]
[3]

& e & f &
14290
14291
14292

You can use pipes to send the output from one task to the next task and an ampersand (&) to run the entire job as a background task. Again the shell displays the
prompt immediately. The shell regards the commands joined by a pipe as a single
job. That is, it treats all pipes as single jobs, no matter how many tasks are connected with the pipe (I) symbol or how complex they are. The Bourne Again Shell
reports only one process in the background (although there are three):
$ d | e | f &
[ 1 ] 14295

optional ( ) G R O U P S COMMANDS
You can use parentheses to group commands. The shell creates a copy of itself,
called a subshell, for each group. It treats each group of commands as a job and
creates a new process to execute each command (refer to "Process Structure" on
page 328 for more information on creating subshells). Each subshell (job) has its
own environment, meaning that it has its own set of variables whose values can
differ from those found in other subshells.
The following command line executes commands a and b sequentially in the background while executing c in the background. The shell displays a prompt immediately.
$ (a ; b) & c &
[1] 15520
[2] 15521

The preceding example differs from the earlier example d & e & f & in that tasks a
and b are initiated sequentially, not concurrently.
Similarly the following command line executes a and b sequentially in the background and, at the same time, executes c and d sequentially in the background. The
subshell running a and b and the subshell running c and d run concurrently. The
shell displays a prompt immediately.
$ (a ; b) & (c ; d) &
[1] 15528
[ 2 ] 15529

SHELL BASICS

307

The next script copies one directory to another. The second pair of parentheses creates
a subshell to run the commands following the pipe. Because of these parentheses, the
output of the first tar command is available for the second tar command despite the
intervening cd command. Without the parentheses, the output of the first tar command
would be sent to cd and lost because cd does not process input from standard input.
The shell variables S I and S2 represent the first and second command-line arguments
(page 997), respectively. The first pair of parentheses, which creates a subshell to run
the first two commands, allows users to call cpdir with relative pathnames. Without
them, the first cd command would change the working directory of the script (and consequently the working directory of the second cd command). With them, only the
working directory of the subshell is changed.
$ cat cpdir
( c d $ 1 ; t a r - c f - . ) | ( c d $2 ; t a r - x v f - )
$ ./cpdir /home/max/sources /home/max/memo/biblio

The cpdir command line copies the files and directories in the /home/max/sources
directory to the directory named /home/max/memo/biblio. This shell script is
almost the same as using cp with the - r option. Refer to the cp and tar man pages for
more information.

JOB CONTROL
A job is a command pipeline. You run a simple job whenever you give the shell a
command. For example, if you type date on the command line and press RETURN, you
have run a job. You can also create several jobs with multiple commands on a single
command line:
$ find . -print | sort | lpr & grep -1 max /tmp/* > maxfiles &
[ 1 ] 18839
[ 2 ] 18876

The portion of the command line up to the first & is one job consisting of three processes connected by pipes: find, sort (page 168), and Ipr (page 165). The second job is a
single process running grep. The trailing & characters put each job in the background,
so bash does not wait for them to complete before displaying a prompt.
Using job control you can move commands from the foreground to the background
(and vice versa), stop commands temporarily, and list all commands that are running in the background or stopped.

jobs:

LISTS JOBS

The jobs builtin lists all background jobs. Following, the sleep command runs in the
background and creates a background job that jobs reports on:
$ sleep 60 &
[ 1 ] 7809
$ jobs
[1] + Running

s l e e p 60 &

308

CHAPTER 9

THE BOURNE AGAIN SHELL

fg: B R I N G S A JOB TO THE FOREGROUND
The shell assigns a job number to each command you run in the background. For
each job run in the background, the shell lists the job number and PID number
immediately, just before it issues a prompt:
$ xclock &
[ 1 ] 1246
$ date &
[ 2 ] 1247
$ Tue Dec 7 11:44:40 PST 2010
[ 2 ] + Done
date
$ find /usr -name ace -print > findout &
[ 2 ] 1269
$ jobs
[ 1 ] - Running
xclock &
[2]+ Running
f i n d / u s r -name ace - p r i n t

> 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.
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 command became job 2.
To move a background job to the foreground, use the fg builtin followed by the job
number. Alternatively, you can give a percent sign ( % ) followed by the job number
as a command. Either of the following commands moves job 2 to the foreground.
When you move a job to the foregound, the shell displays the command it is now
executing in the foreground.
$ fg 2
find /usr

-name ace - p r i n t

>

findout

-name ace - p r i n t

>

findout

or
$ %2

find /usr

You can also refer to a job by following the percent sign with a string that uniquely
identifies the beginning of the command line used to start the job. Instead of the
preceding command, you could have used either fg %find or fg %f because both
uniquely identify job 2. If you follow the percent sign with a question mark and a
string, the string can match any part of the command line. In the preceding example, fg %?ace also brings job 2 to the foreground.
Often the job you wish to bring to the foreground is the only job running in the
background or is the job that jobs lists with a plus (+). In these cases fg without an
argument brings the job to the foreground.

SHELL BASICS

309

S U S P E N D I N G A JOB
Pressing the suspend key (usually C0NTR0L-Z) immediately suspends (temporarily
stops) the job in the foreground and displays a message that includes the word
Stopped.
C0NTR0L-Z
[2]+
Stopped

find /usr

-name ace - p r i n t

>

findout

For more information refer to "Moving a Job from the Foreground to the Background" on page 255.

bg: S E N D S A JOB TO THE BACKGROUND
To move the foreground job to the background, you must first suspend the job (above).
You can then use the bg builtin to resume execution of the job in the background.
$ bg
[2]+ find /usr

-name ace - p r i n t

> findout

&

If a background job attempts to read from the terminal, the shell stops the program
and displays a message saying the job has been stopped. You must then move the
job to the foreground so it can read from the terminal.
$ (sleep 5; cat > mytext) &
[ 1 ] 1343
$ date
T u e Dec
7 1 1 : 5 8 : 2 0 PST 2 0 1 0
[1]+ Stopped

$ fg

( sleep

5;

cat >mytext

)

( s l e e p 5; c a t > m y t e x t )
Remember to let the cat out!
C0NTR0L-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 output
appears on the screen. The shell waits until just before it issues a prompt (after date
has finished) to notify you that job 1 is stopped. When you give an fg command, the
shell puts the job in the foreground and you can enter the data the command is
waiting for. In this case the input needs to be terminated with C0NTR0L-D, which sends
an EOF (end of file) signal to the shell. The shell then displays another prompt.
The shell keeps you informed about changes in the status of a job, notifying you when
a background job starts, completes, or stops, perhaps because it is waiting for input
from the terminal. The shell also lets you know when a foreground job is suspended.
Because notices about a job being run in the background can disrupt your work, the
shell delays displaying these notices until just before it displays a prompt. You can set
notify (page 355) to cause the shell to display these notices without delay.

310

CHAPTER 9

THE BOURNE AGAIN S H E L L

Figure 9-2

The directory structure in the examples

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 exit from the shell again, the shell allows you to exit. If huponexit
(page 355) is not set (the default), stopped and background jobs keep running in the
background. If it is set, the shell terminates the jobs.

MANIPULATING THE DIRECTORY STACK
The Bourne Again Shell allows you to store a list of directories you are working
with, enabling you to move easily among them. This list is referred to as a stack. It
is analogous to a stack of dinner plates: You typically add plates to and remove
plates from the top of the stack, so this type of stack is named a 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
~/li terature

Figure 9-3

Creating a directory stack

SHELL BASICS

pushd

Figure 9-4

311

pushd

Using pushd to change working directories

The dirs builtin uses a tilde (~) to represent the name of a user's home directory. The
examples in the next several sections assume that you are referring to the directory
structure shown in Figure 9-2.

pushd: P U S H E S A DIRECTORY ON THE STACK
When you supply the pushd (push directory) builtin with one argument, it pushes the
directory specified by the argument on the stack, changes directories to the specified
directory, and displays the stack. The following example is illustrated in Figure 9-3:
$ pushd ../demo
~/demo - / l i t e r a t u r e
$ pwd
/home/sam/demo
$ pushd ../names
-/names -/demo - / l i t e r a t u r e
$ pwd
/home/sam/names

When you use pushd without an argument, it swaps the top two directories on the
stack, makes the new top directory (which was the second directory) the new working directory, and displays the stack (Figure 9-4):
$ pushd
-/demo -/names - / I i t e r a t u r e
$ pwd
/home/sam/demo

Using pushd in this way, you can easily move back and forth between two directories. You can also use cd - to change to the previous directory, whether or not you
have explicitly created a directory stack. To access another directory in the stack,
call pushd with a numeric argument preceded by a plus sign. The directories in the
stack are numbered starting with the top directory, which is number 0. The following pushd command continues with the previous example, changing the working
directory to literature and moving literature to the top of the stack:
$ pushd +2
- / l i t e r a t u r e -/demo -/names
$ pwd
/home/sam/1i t e r a t u r e

312

CHAPTER 9

THE BOURNE AGAIN SHELL

Figure 9-5

Using popd to remove a directory from the stack

popd: P O P S A DIRECTORY OFF THE STACK
To remove a directory from the stack, use the popd (pop directory) builtin. As the following example and Figure 9-5 show, without an argument, popd removes the top
directory from the stack and changes the working directory to the new top directory:
$ dirs
- / l i t e r a t u r e ~/demo
$ popd
~/demo -/names
$ pwd
/home/sam/demo

-/names

To remove a directory other than the top one from the stack, use popd with a
numeric argument preceded by a plus sign. The following example removes directory number 1, demo. Removing a directory other than directory number 0 does
not change the working directory.
$
-/
$
-/

dirs
literature
popd +1
literature

-/demo

-/names

-/names

PARAMETERS AND VARIABLES
Variables Within a shell, a shell parameter is associated with a value that is accessible to the
user. There are several kinds of shell parameters. Parameters whose names consist of
letters, digits, and underscores are often referred to as shell variables, or simply
variables. A variable name must start with a letter or underscore, not with a number. Thus A76, MY_CAT, and
X
are valid variable names, whereas
69TH_STREET (starts with a digit) and MY-NAME (contains a hyphen) are not.
User-created Shell variables that you name and assign values to are user-created variables. You
variables c a n 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

PARAMETERS AND VARIABLES

313

to all shells and other programs you fork from the original shell. One naming convention is to use only uppercase letters for global variables and to use mixed-case or
lowercase letters for other variables. Refer to "Locality of Variables" on page 992
for more information on global variables.
To assign a value to a variable in the Bourne Again Shell, use the following syntax:
VARIABLE=value
There can be no whitespace on either side of the equal sign (=). An example assignment follows:
$ myvar=abc

The Bourne Again Shell permits you to put variable assignments on a command
line. This type of assignment creates a variable that is local to the command
shell—that is, the variable is accessible only from the program the command runs.
The my_script shell script displays the value of TEMPDIR. The following command
runs my_script with TEMPDIR set to /home/sam/temp. The echo builtin shows
that the interactive shell has no value for TEMPDIR after running my_script. If
TEMPDIR had been set in the interactive shell, running my_script in this manner
would have had no effect on its value.
$ cat my_script
e c h o STEMPDIR
$ TEMPDIR=/home/sam/temp
/home/sam/temp
$ echo $TEMPDIR

./my_scri pt

$

Keyword variables Keyword shell variables (or simply keyword variables) have special meaning to the
shell and usually have short, mnemonic names. When you start a shell (by logging
in, for example), the shell inherits several keyword variables from the environment.
Among these variables are HOME, which identifies your home directory, and
PATH, which determines which directories the shell searches and in what order to
locate commands that you give the shell. The shell creates and initializes (with
default values) other keyword variables when you start it. Still other variables do
not exist until you set them.
You can change the values of most keyword shell variables. It is usually not necessary to change the values of keyword variables initialized in the /etc/profile or
/etc/csh.cshrc systemwide startup files. If you need to change the value of a bash
keyword variable, do so in one of your startup files (page 293). Just as you can
make user-created variables global, so you can make keyword variables global—a
task usually done automatically in startup files. You can also make a keyword variable readonly.
Positional and The names of positional and special parameters do not resemble variable names,
special parameters Most of these parameters have one-character names (for example, 1, ?, and #) and

314

CHAPTER 9

THE BOURNE AGAIN SHELL

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 (page 996). Positional parameters enable you to
access command-line arguments, a capability that you will often require when you
write shell scripts. The set builtin (page 998) enables you to assign values to positional parameters.
Other frequently needed shell script values, such as the name of the last command
executed, the number of command-line arguments, and the status of the most
recently executed command, are available as special parameters (page 994). You
cannot assign values to special parameters.

USER-CREATED VARIABLES
The first line in the following example declares the variable named person and
initializes it with the value max:
$ person=max
$ echo person
person
$ echo $person
max
Parameter
substitution

Because the echo builtin copies its arguments to standard output, you can use it to
display the values of variables. The second line of the preceding example shows that
person does not represent max. Instead, the string person is echoed as person. The
shell substitutes the value of a variable only when you precede the name of the variable with a dollar sign ($). Thus the command echo Sperson displays the value of
the variable person; it does not display Sperson because the shell does not pass
Sperson to echo as an argument. Because of the leading $, the shell recognizes that
Sperson 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
max
$ echo
max
$ echo
Sperson
$ echo
Sperson

Sperson
"Sperson"
'Sperson'
\Sperson

PARAMETERS AND V A R I A B L E S

315

SPACES Because they do not prevent variable substitution but do turn off the special meanings of most other characters, double quotation marks are useful when you assign
values to variables and when you use those values. To assign a value that contains
SPACEs or TABs to a variable, use double quotation marks around the value. Although
double quotation marks are not required in all cases, using them is a good habit.
$ person="max and zach"
$ echo Sperson

max and zach
$ person=max and zach

b a s h : a n d : command n o t f o u n d
W h e n you reference a variable whose value contains TABs or multiple adjacent SPACEs,
you need to use quotation marks to preserve the spacing. If you do not quote the
variable, the shell collapses each string of blank characters into a single SPACE before
passing the variable to the utility:
$ person="max
$ echo Sperson

and

zach"

max and zach
$ echo "Sperson"

max

and

zach

Pathname When you execute a command with a variable as an argument, the shell replaces the
expansion in name of the variable with the value of the variable and passes that value to the proassignments

gram being executed. If the value of the variable contains a special character, such
as * or ?, the shell may expand that variable.

The first line in the following sequence of commands assigns the string max 5 :- to the
variable m e m o . The Bourne Again Shell does not expand the string because bash
does not perform pathname expansion (page 2 5 6 ) when it assigns a value to a variable. All shells process a command line in a specific order. Within this order bash
expands variables before it interprets commands. In the following echo command
line, the double quotation marks quote the asterisk ( * ) in the expanded value of
S m e m o and prevent bash from performing pathname expansion on the expanded
m e m o variable before passing its value to the echo command:
$ memo=max-.'c
$ echo "Smemo"

max*
All shells interpret special characters as special when you reference a variable that
contains an unquoted special character. In the following example, the shell expands
the value of the m e m o variable because it is not quoted:
$

I s

max.report
max.summary
$ echo Smemo

m a x . r e p o r t max.summary

316

CHAPTER 9

THE B O U R N E AGAIN S H E L L

Here the shell expands the Smemo variable to max»:-, expands max»:- to max.report
and max.summary, and passes these two values to echo.

optional

Braces The $VARIABLE syntax is a special case of the more general syntax
${VARIABLEj,
in which the variable name is enclosed by SU- The braces insulate the variable name
from adjacent characters. Braces are necessary when catenating a variable value
with a string:
$
$
$
$

PREF=counter
WAY=$PREFclockwise
FAKE=$PREFfeit
echo $WAY $FAKE

<
J>r
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=${PREFECTockwise
$ FAKE=$-{PREF}-feit
$ echo $WAY $FAKE
counterclockwise counterfeit
The Bourne Again Shell refers to the arguments on its command line by position,
using the special variables $1, $2, S3, and so forth up to $9. If you wish to refer to
arguments past the ninth argument, you must use braces: S{10}. The name of the
command is held in $0 (page 997).

unset: REMOVES A VARIABLE
Unless you remove a variable, it exists as long as the shell in which it was created
exists. To remove the value of a variable but not the variable itself, assign a null
value to the variable:
$ person=
$ echo $person
S
You can remove a variable using the unset builtin. The following command removes
the variable person:
$ unset person

PARAMETERS AND V A R I A B L E S

317

VARIABLE ATTRIBUTES
This section discusses attributes and explains how to assign them to variables.

readonly:

M A K E S THE VALUE OF A VARIABLE PERMANENT

You can use the readonly builtin to ensure that the value of a variable cannot be
changed. The next example declares the variable person to be readonly. You must
assign a value to a variable before you declare it to be readonly; you cannot change
its value after the declaration. When you attempt to unset or change the value of a
readonly variable, the shell displays an error message:
$ person=zach
$ echo Sperson
zach
$ readonly person
$ person=helen
bash: person: r e a d o n l y

variable

If you use the readonly builtin without an argument, it displays a list of all readonly
shell variables. This list includes keyword variables that are automatically set as readonly as well as keyword or user-created variables that you have declared as readonly.
See page 318 for an example (readonly and declare - r produce the same output).

declare

AND

typeset:

A S S I G N A T T R I B U T E S TO V A R I A B L E S

The declare and typeset builtins (two names for the same command) set attributes
and values for shell variables. Table 9-3 lists five of these attributes.

Table 9-3

Variable attributes (typeset or declare)

Attribute

Meaning

-a

Declares a variable as an array (page 990)

-f

Declares a variable to be a function name (page 349)

-i

Declares a variable to be of type integer (page 318)

-r

Makes a variable readonly; also readonly (page 317)

-X

Exports a variable (makes it global); also export (page 992)

The following commands declare several variables and set some attributes. The first
line declares personl and assigns it a value of max. This command has the same
effect with or without the word declare.
$
$
$
$

declare
declare
declare
declare

personl=max
-r person2=zach
-rx person3=helen
-x person4

318

CHAPTER 9

THE BOURNE AGAIN SHELL

The readonly and export builtins are synonyms for the commands declare - r and
declare - x , respectively. You can declare a variable without assigning a value to it,
as the preceding declaration of the variable person4 illustrates. This declaration
makes person4 available to all subshells (i.e., makes it global). Until an assignment
is made to the variable, it has a null value.
You can list the options to declare separately in any order. The following is equivalent
to the preceding declaration of person3:
$ declare -x -r person3=helen

Use the + character in place of - when you want to remove an attribute from a variable. You cannot remove the readonly attribute. After the following command is
given, the variable person3 is no longer exported but it is still readonly.
$ declare +x person3

You can use typeset instead of declare.
Listing variable Without any arguments or options, d e c l a r e lists all shell variables. The same list is
attributes output when you run s e t (page 9 9 8 ) without any arguments.
If you use a declare builtin with options but no variable names as arguments, the
command lists all shell variables that have the indicated attributes set. For example,
the command declare - r displays a list of all readonly shell variables. This list is the
same as that produced by the readonly command without any arguments. After the
declarations in the preceding example have been given, the results are as follows:
$ declare -r
declare - a r BASH_VERSINFO='([0]="3" [ 1 ] = " 2 " [ 2 ] = " 3 9 " [ 3 ] = " 1 " . . . )'
d e c l a r e - i r EUID="500"
declare - i r PPID="936"
declare - r SHELLOPTS="braceexpand:emacs:hashal1:hi stexpand:hi s t o r y : . .
declare - i r UID="500"
declare - r person2="zach"
declare - r x person3="helen"

The first five entries are keyword variables that are automatically declared as readonly. Some of these variables are stored as integers (-i). The - a option indicates that
BASH_VERSINFO is an array variable; the value of each element of the array is
listed to the right of an equal sign.
Integer By default the values of variables are stored as strings. When you perform arithmetic on a string variable, the shell converts the variable into a number, manipulates
it, and then converts it back to a string. A variable with the integer attribute is
stored as an integer. Assign the integer attribute as follows:
$ declare -i COUNT

KEYWORD VARIABLES
Keyword variables 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

PARAMETERS AND VARIABLES

319

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 9 9 2 ) to make them available to child shells.

HOME: YOUR HOME DIRECTORY
By default your home directory is the working directory when you log in. Your
home directory is established when your account is set up; its name is stored in the
/etc/passwd file.
$ grep sam /etc/passwd
s a m : x : 5 0 1 : 5 0 1 : S a m S. x 3 0 1 : / h o m e / s a m : / b i n / b a s h

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 H O M E the working directory:
$ pwd
/home/max/laptop
$ echo $HOME
/home/max
$ cd
$ pwd
/home/max

This example shows the value of the H O M E variable and the effect of the cd
builtin. After you execute cd without an argument, the pathname of the working
directory is the same as the value of HOME: your home directory.
Tilde (~) The shell uses the value of H O M E to expand pathnames that use the shorthand
tilde (~) notation (page 2 0 6 ) to denote a user's home directory. The following example uses echo to display the value of this shortcut and then uses Is to list the files in
Max's laptop directory, which is a subdirectory of his home directory:
$ echo ~
/home/max
S i s ~/1aptop
tester
count

lineup

PATH: WHERE THE S H E L L L O O K S FOR PROGRAMS
When you give the shell an absolute or relative pathname rather than a simple filename as a command, it looks in the specified directory for an executable file with
the specified filename. If the file with the pathname you specified does not exist, the
shell reports command not found. If the file exists as specified but you do not have
execute permission for it, or in the case of a shell script you do not have read and
execute permission for it, the shell reports Permission denied.
If you give a simple filename as a command, the shell searches through certain
directories (your search path) for the program you want to execute. It looks in several directories for a file that has the same name as the command and that you have
execute permission for (a compiled program) or read and execute permission for (a
shell script). The PATH shell variable controls this search.

320

CHAPTER 9

THE BOURNE AGAIN SHELL

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. 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 directory. If it does not find the file in this directory, the shell looks
next in /bin, and then in /usr/bin. If the search fails in those directories, the shell
looks in the -/bin directory, a subdirectory of the user's home directory. Finally the
shell looks in the working directory. Exporting PATH makes its value accessible to
subshells:
$ export

PATH=/usr/local/bin:/bin:/usr/bin:~/bin:

A null value in the string indicates the working directory. In the preceding example,
a null value (nothing between the colon and the end of the line) appears as the last
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 put your
own bin directory at the end of your PATH, as in the preceding example, the shell
looks there for any commands that it cannot find in directories listed earlier in PATH.

PATH and security
security Do not put the working directory first in PATH when security is a concern. If you are working as
root, you should never put the working directory first in PATH. It is common for root's PATH to
omit the working directory entirely. You can always execute a file in the working directory by
prepending ./tothe name: ,/myprog.
Putting the working directory first in PATH can create a security hole. Most people type Is as the
first command when entering a directory. If the owner of a directory places an executable file
named Is in the directory, and the working directory appears first in a user's PATH, the user giving
an Is command from the directory executes the Is program in the working directory instead of the
system Is utility, possibly with undesirable results.
If you want to add directories to PATH, you can reference the old value of the
PATH variable in setting PATH to a new value (but see the preceding security tip).
The following command adds /usr/local/bin to the beginning of the current PATH
and the bin directory in the user's home directory (-/bin) to the end:

PARAMETERS AND VARIABLES

321

$ PATH=/usr/loca!/bi n : $ P A T H : ~ / b i n

MAIL: WHERE YOUR MAIL I S KEPT
The MAIL variable contains the pathname of the file that holds your mail (your
mailbox,
usually / v a r / i n a i l / / M w e , where name is your username). If MAIL is set
and MAILPATH (next) is not set, the shell informs you when mail arrives in the file
specified by MAIL. In a graphical environment you can unset MAIL so the shell
does not display mail reminders in a terminal emulator window (assuming you are
using a graphical mail program).
The MAILPATH variable contains a list of filenames separated by colons. If this
variable is set, the shell informs you when any one of the files is modified (for
example, when mail arrives). You can follow any of the filenames in the list with a
question mark (?), followed by a message. The message replaces the you have mail
message when you receive mail while you are logged in.
The M A I L C H E C K 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.

P S 1 : U S E R PROMPT (PRIMARY)
The default Bourne Again Shell prompt is a dollar sign ($). When you run bash with
root privileges, bash typically displays a hashmark (#) prompt. The PS1 variable
holds the prompt string 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.
You can customize the prompt displayed by PS1. For example, the assignment
$ PS1="[\u@\h \W \!]$ "

displays the following prompt:
[user@bost directory

event]$

where user is the username, host is the hostname up to the first period, directory is
the basename of the working directory, and event is the event number (page 331) of
the current command.
If you are working on more than one system, it can be helpful to incorporate the
system name into your prompt. For example, you might change the prompt to the
name of the system you are using, followed by a colon and a SPACE (a SPACE at the end
of the prompt makes the commands you enter after the prompt easier to read). This
command uses command substitution (page 362) in the string assigned to PS1:
$ PS1="$(hostname): "
b r a v o . e x a m p l e . c o m : echo test
test
bravo.example.com:

322

CHAPTER 9

THE BOURNE AGAIN SHELL

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 r o o t privileges, a hashmark).
The second example changes the prompt to the time followed by the name of the
user. The third example changes the prompt to the one used in this book (a hashmark for r o o t and a dollar sign otherwise):
$ PSl='\h \$ '
bravo $
$ PS1='\@ \u $ '
09:44 PM max $
$ PS1='\$ '

s

Table 9-4 describes some of the symbols you can use in PS1. For a complete list of special characters you can use in the prompt strings, open the bash man page and search
for the 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 335)

\d

Date in Weekday Month Date format

Mi

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,

\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

AM/PM

format

PS2: USER PROMPT (SECONDARY)
The PS2 variable holds the secondary prompt. On the first line of the next example,
an unclosed quoted string follows echo. The shell assumes the command is not finished and, on the second line, gives the default secondary prompt (>). This prompt
indicates the shell is waiting for the user to continue the command line. The shell
waits until it receives the quotation mark that closes the string. Only then does it
execute the command:

PARAMETERS AND VARIABLES 3 2 3
$ echo " d e m o n s t r a t i o n o f
>

prompt

string

2 "

demonstration of prompt s t r i n g
2
$ PS2="secondary prompt: "
$ echo " t h i s demonstrates

secondary prompt: prompt string 2"
t h i s demonstrates
prompt s t r i n g 2
The second command changes the secondary prompt to secondary p r o m p t : followed by a SPACE. A multiline echo demonstrates the new prompt.
PS3: MENU PROMPT
The PS3 variable holds the menu prompt for the select control structure (page 984).
PS4: DEBUGGING PROMPT
The PS4 variable holds the bash debugging symbol (page 966).
IFS: SEPARATES INPUT FIELDS ( W O R D SPLITTING)
The IFS (Internal Field Separator) shell variable specifies the characters you can use
to separate arguments on a command line. It has the default value of SPACE TAB NEWLINE.
Regardless of the value of IFS, you can always use one or more SPACE or TAB characters to separate arguments on the command line, provided these characters are not
quoted or escaped. When you assign IFS character values, these characters can also
separate fields—but only if they undergo expansion. This type of interpretation of
the command line is called word splitting.

Be careful when changing IFS
caution Changing IFS has a variety of side effects, so work cautiously. You may find it useful to save the
value of IFS before changing it. Then you can easily restore the original value if you get unexpected
results. Alternatively, you can fork a new shell 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.
The following example demonstrates how setting IFS can affect the interpretation
of a command line:
$ a=w:x:y:z
$ c a t $a

cat: w : x : y : z : No such f i l e or d i r e c t o r y
$ IFS=

II . II

$ c a t $a

cat:
cat:
cat:
cat:

w:
x:
y:
z:

No
No
No
No

such
such
such
such

file
file
file
file

or
or
or
or

directory
directory
directory
directory

324

CHAPTER 9

THE B O U R N E AGAIN SHELL

The first time cat is called, the shell expands the variable a, interpreting the string
w : x : y : z as a single word to be used as the argument to cat. The cat utility cannot
find a file named w : x : y : z and reports an error for that filename. After IFS is set to a
colon (:), the shell expands the variable a into four words, each of which is an argument to cat. Now cat reports errors for four files: w , x , y, and z. Word splitting
based on the colon (:) takes place only after the variable a is expanded.
The shell splits all expanded words on a command line according to the separating
characters found in IFS. When there is no expansion, there is no splitting. Consider
the following commands:
$ IFS="p"
$ e x p o r t VAR

Although IFS is set to p, the p on the e x p o r t command line is not expanded, so the
word e x p o r t is not split.
The following example uses variable expansion in an attempt to produce an e x p o r t
command:
$ IFS="p"
$ aa=export
$ echo $aa

ex ort
This time expansion occurs, so the character p in the token e x p o r t is interpreted as
a separator (as the echo command shows). Now when you try to use the value of
the aa variable to export the V A R variable, the shell parses the $aa V A R command
line as ex o r t V A R . The effect is that the command line starts the ex editor with two
filenames: o r t and V A R .
$ Saa VAR

2 f i l e s to e d i t
"ort" [New F i l e ]
Entering Ex mode.

Type " v i s u a l " to go to Normal mode.

:q

E17B: 1 more f i l e to e d i t
:q

s

If you unset IFS, only SPACES and TABs work as field separators.

Multiple separator characters
tip Although the shell treats sequences of multiple SPACE or TAB characters as a single separator, it
treats each occurrence of another field-separator character as a separator.

CDPATH:

B R O A D E N S T H E S C O P E O F cd
The CDPATH variable allows you to use a simple filename as an argument to the cd
builtin to change the working directory to a directory other than a child of the
working directory. If you have several directories you typically work out of, this

PARAMETERS AND VARIABLES

325

variable can speed things up and save you the tedium of using cd with longer pathnames to switch among them.
When CDPATH is not set and you specify a simple filename as an argument to cd, cd
searches the working directory for a subdirectory with the same name as the argument. If the subdirectory does not exist, cd displays an error message. When
CDPATH is set, cd searches for an appropriately named subdirectory in the directories in the CDPATH list. If it finds one, that directory becomes the working directory.
With CDPATH set, you can use cd and a simple filename to change the working
directory to a child of any of the directories listed in CDPATH.
The CDPATH variable takes on the value of a colon-separated list of directory
pathnames (similar to the PATH variable). It is usually set in the ~ / . b a s h _ p r o f i l e
startup file with a command line such as the following:
export CDPATH=$HOME:$H0ME/1i terature
This command causes cd to search your home directory, the l i t e r a t u r e directory, and
then the working directory when you give a cd command. If you do not include the
working directory in CDPATH, cd searches the working directory if the search of
all the other directories in CDPATH fails. If you want cd to search the working
directory first, include a null string, represented by two colons (::), as the first entry
in CDPATH:
export CDPATH=::$HOME:$H0ME/1iterature
If the argument to the cd builtin is an absolute pathname—one starting with a slash
(/)—the shell does not consult CDPATH.
KEYWORD VARIABLES: A S U M M A R Y
Table 9-5 presents a list of bash keyword variables.
Table 9-5

b a s h keyword variables

Variable

Value

BASH_ENV

The pathname of the startup file for noninteractive shells (page 294)

CDPATH

The c d search path (page 324)

COLUMNS

The width of the display used by select (page 983)

FCEDIT

The name of the editor that fc uses by default (page 334)

HISTFILE

The pathname of the file that holds the history list (default: ~/.bash_history;
page 330)

HISTFILESIZE

The maximum number of entries saved in HISTFILE (default: 500; page 330)

HISTSIZE

The maximum number of entries saved in the history list (default: 500;
page 330)

326

CHAPTER 9

THE BOURNE AGAIN SHELL

Table 9-5

b a s h keyword variables (continued)

Variable

Value

HOME

The pathname of the user's home directory (page 319); used as the default
argument for c d and in tilde expansion (page 206)

IFS

Internal Field Separator (page 323); used for word splitting (page 363)

INPUTRC

The pathname of the Readline startup file (default: -/.inputrc; page 343)

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 983)

MAIL

The pathname of the file that holds a user's mail (page 321)

MAILCHECK

How often, in seconds, b a s h checks for mail (page 321)

MAILPATH

A colon-separated list of file pathnames that b a s h checks for mail in
(page 321)

PATH

A colon-separated list of directory pathnames that b a s h looks for commands
in (page 319)

PR0MPT_C0MMAND A command that b a s h executes just before it displays the primary prompt
PS1

Prompt String 1; the primary prompt (page 321)

PS2

Prompt String 2; the secondary prompt (default: "> '; page 322)

PS3

The prompt issued by select (page 983)

PS4

The b a s h debugging symbol (page 966)

REPLY

Holds the line that read accepts (page 1004); also used by select (page 983)

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 304)
Separates commands (page 304)

SPECIAL CHARACTERS

Table 9-6

327

Shell special characters (continued)

Character

Use

0

Groups commands (page 306) for execution by a subshell or identifies a function (page 349)

(( ))

Expands an arithmetic expression (page 360)

&

Executes a command in the background (pages 254 and 305)

1

Sends standard output of the preceding command to standard input of the following command (pipe; page 305)

>

Redirects standard output (page 246)

»

Appends standard output (page 249)

<

Redirects standard input (page 247)

«

Here document (page 985)
Any string of zero or more characters in an ambiguous file reference
(page 257)

»

?

Any single character in an ambiguous file reference (page 256)

\

Quotes the following character (page 160)
Quotes a string, preventing all substitution (page 160)

1
•I

Quotes a string, allowing only variable and command substitution (pages 160
and 314)

\

\

Performs command substitution (page 362)

[ ]

Character class in an ambiguous file reference (page 259)

$

References a variable (page 312)

.

(dot builtin)

Executes a command (page 296)

#

Begins a comment (page 303)

{}

Surrounds the contents of a function (page 349)

:

(null builtin)

&&
(Boolean AND)

Returns true{page

1011)

Executes command on right only if command on left succeeds (returns a zero
exit status; page 1022)

||

(Boolean OR) Executes command on right only if command on left fails (returns a nonzero
exit status; page 1022)

I

(Boolean NOT) Reverses exit status of a command

$0

Performs command substitution (preferred form; page 362)

[]

Evaluates an arithmetic expression (page 360)

328

CHAPTER 9

THE BOURNE AGAIN SHELL

PROCESSES
A process is the execution of a command by the Linux kernel. 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 f o r k ( ) .
When Linux begins execution when a system is started, it starts in it, a single process
called a spontaneous process, with PID number 1. This process holds the same position in the process structure as the root directory does in the file structure: It is the
ancestor of all processes the system and users work with. When a command-line
system is in multiuser mode, init runs getty or mingetty processes, which display
login: prompts on terminals. When a user 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 numbers 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 session the same process is always executing the login shell. When you fork a new process—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. When you call it with the - f option, ps displays 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 number
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 sleep

PROCESSES 3 2 9

(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
max
max
max

P I D PPID C STIME
21341 21340 3 10:42
22789 21341 3 1 7 : 3 0
22790 21341 3 1 7 : 3 0

TTY
pts/16
pts/16
pts/16

TIME CMD
5 bash
5 s l e e p 10
5 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

UID
max
max
max

-f

P I D PPID C STIME
21341 21340 3 10:42
22791 21341 3 1 7 : 3 1
22792 21341 3 1 7 : 3 1

TTY
pts/16
pts/16
pts/16

TIME CMD
5 bash
5 s l e e p 10
5 ps - f

You can also use pstree (or ps — f o r e s t , 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(l)-+-acpid(1395)
|-atd(1758)
| -crond(1702)
-kdeini t(2223)-+-firefox(8914)---run-mozilla.sh(8920)
f i r e f o x - b i n(8925)
gaim(2306)
gqview(14062)
kdei ni t ( 2 2 2 8 )
kdei ni t ( 2 2 9 4 )
kdei n i t ( 2 3 1 4 ) - + - b a s h ( 2 3 2 9 ) — s s h ( 2 5 6 1 )
| -bash(2339)
'-bash(15821)---bash(16778)
kdei ni t ( 1 6 4 4 8 )
kdei ni t ( 2 0 8 8 8 )
oclock(2317)
'-pam-panel-icon(2305)—pam_timestamp_c(2307)
-1ogin(1823)---bash(20986)-+-pstree(21028)
'-sleep(21026)

The preceding output is abbreviated. The line that starts with - k d e i n i t shows a
graphical user running many processes, including f i r e f o x , g a i m , and o c l o c k . The

line that starts with - l o g i n shows a textual user running sleep in the background
and running pstree in the foreground. Refer to " $ $ : PID Number" on page 995 for

a description of how to instruct the shell to report on PID numbers.

330

CHAPTER 9

THE BOURNE AGAIN SHELL

EXECUTING A C O M M A N D
fork and sleep When you give the shell a command, it usually forks [spawns using the f o r k ( ) system call] a child process to execute the command. While the child process is executing the command, the parent process sleeps [implemented as the sleep() system call].
While a process is sleeping, it does not use any computer time; it 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 ampersand
(&), 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 process 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 261.
Variables Within a given process, such as your login shell or a subshell, you can declare, initialize, 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 992).

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 quick way to
reexecute any of the events in the list. This mechanism also enables you to execute
variations of previous commands and to reuse arguments from them. You can use
the history list to 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 the next section,
which describes the variables you need to set.

VARIABLES THAT C O N T R O L H I S T O R Y
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 whose name is stored in the HISTFILE variable (the default is ~/.bash_history).

HISTORY 3 3 1

The next time you start the shell, this file initializes the history list. The value of the
H I S T F I L E S I Z E variable determines the number of lines of history saved in H I S T F I L E .
See Table 9-7.

h i s t o r y can help track down mistakes
When you have made a mistake on a command line (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.

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

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 321). 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 ~ / . b a s h _ p r o f i l e 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 H I S T F I L E S I Z E , 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 h i s t o r y to display the events in the history list. This list is
ordered so that the oldest events appear at the top. The following history list
includes a command to modify the bash prompt so it displays the history event
number. The last event in the history list is the h i s t o r y command that displayed
the list.
32

$ history

23
24
25
26
27
28
29
30
31
32

|

tail

PS1="\! bash$ "
I s -1
cat temp
rm temp
v i m . t i n y memo
l p r memo
vim. t i n y memo
l p r memo
rm memo
history | t a i l

332

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 h i s t o r y 1 0 or h i s t o r y I t a i l to look at the
ten most recent commands.

A handy history alias
tip Creating the following aliases makes working with history easier. The first allows you to give the
command h to display the ten most recent events. The second alias causes the command hg string
to display all events in the history list that contain string. Put these aliases in your -/.bashrc file
to make them available each time you log in. See page 346 for more information.
$ a l i a s 'h=history | t a i l '
$ a l i a s 'hg=history | grep'

REEXECUTING AND EDITING C O M M A N D S
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 command line in its entirety. You can recall, modify, and reexecute previously executed events in three ways: You can use the fc builtin (covered next), the
exclamation point commands (page 335), or the Readline Library, which uses a
one-line vi- or emacs-like editor to edit and execute events (page 340).

Which method to use?
tip

If you are more familiar with vi or e m a c s 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, 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.
fc: DISPLAYS, EDITS, A N D REEXECUTES C O M M A N D S
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
command-line editors.
VIEWING THE HISTORY LIST
When you call fc with the -1 option, it displays commands from the history list.
Without any arguments, fc - 1 lists the 16 most recent commands in a numbered list,
with the oldest appearing first:
$ fc -1
1024
1025
1026
1027
1028
1029
1030

cd
view calendar
v i m . t i n y letter.adams01
a s p e l l - c letter.adams01
v i m . t i n y letter.adams01
l p r letter.adams01
cd ../memos

HISTORY 3 3 3

1031
1032
1033
1034
1035
1036
1037
1038
1039
1040

Is
rm *0405
f c -1
cd
whereis a s p e l l
man a s p e l l
cd / u s r / s h a r e / d o c / * a s p e l 1*
pwd
Is
I s man-html

The fc builtin can take zero, one, or two arguments with the -1 option. The arguments
specify the part of the history list to be displayed:
fc -I [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 command line, or a negative number, which is taken to be the nth previous command.
Without last, fc displays events through the most recent. If you include 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

1030
1031
1032
1033
1034
1035

- 1 1030 1035

cd ../memos
Is
rm *0405
f c -1
cd
whereis a s p e l l

The following command lists the most recent event that begins with v i e w through
the most recent command line that begins with whereis:
$ fc

102 5
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035

- 1 view whereis

view c a l e n d a r
v i m . t i n y letter.adams01
aspell - c letter.adams01
v i m . t i n y letter.adams01
l p r letter.adams01
cd ../memos
Is
rm *0405
f c -1
cd
whereis a s p e l l

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

1027

- 1 1027 1027

aspell - c letter.adams01

334

CHAPTER 9

THE B O U R N E AGAIN SHELL

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, assuming the editor you specify is installed.
By default, fc invokes the nano editor. Without first and last, it defaults to the most
recent command. The next example invokes the vim editor to edit the most recent
command:
$ fc

-e

vi

The fc builtin uses the stand-alone vim 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 using the emacs editor (part
of the emacs package; not installed by default):
$ export
$ fc

FCEDIT=/usr/bin/emacs

If you call it with a single argument, fc invokes the editor on the specified command.
The following example starts the editor with event 1029 in the Work buffer. When
you exit from the editor, the shell executes the command:
$ fc

1029

As described earlier, 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 v i m
through event 1030:
$ f c vim 1030

Clean up the fc buffer
c a u t i o n 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.
REEXECUTING COMMANDS WITHOUT CALLING THE EDITOR
You can reexecute previous commands without using an editor. If you call fc with
the - s option, it skips the editing phase and reexecutes the command. The following
example reexecutes event 1029:
$ fc

- s 1029

l p r letter.adams01
The next example reexecutes the previous command:
$ fc

-s

HISTORY 3 3 5

When you reexecute a command, you can tell fc to substitute one string for another.
The next example substitutes the string j o h n for the string adams in event 1029 and
executes the modified event:
$ fc

lpr

- s adams=john 1029

letter.john01

U S I N G A N E X C L A M A T I O N P O I N T (!) TO REFERENCE EVENTS
The C Shell history mechanism uses an exclamation point to reference events. This
technique, which is available under bash, is frequently more cumbersome to use
than fc but nevertheless has some useful features. For example, the !! command
reexecutes the previous event, and the shell replaces the !$ token with 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 exclamation point to specify an event.
You can put history events anywhere on a command line. To escape an exclamation point
so that the shell interprets it literally instead of as the start of a history event, precede the
exclamation point 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 the
next page for a list of event designators.
!! reexecutes the You can reexecute the previous event by giving a !! command. In the following
previous event example, event 45 reexecutes event 44:
44 $ I s

-1

-rw-rw-r-45 $ ! !
I s -1 t e x t
-rw-rw-r--

text

1 max group 45 2010-04-30 14:53 t e x t
1 max group 45 2010-04-30 14:53 t e x t

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 1-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
I s -1 t e x t
-rw-rw-r-52 $ ! - 8
I s -1 t e x t
-rw-rw-r--

1 max group 45 2010-04-30 14:53 t e x t
1 max group 45 2010-04-30 14:53 t e x t

336

CHAPTER 9

THE B O U R N E AGAIN SHELL

\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
within question marks, the shell executes the most recent event that contained that
string. The final question mark is optional if a RETURN would immediately follow it.
68

$ h i s t o r y 10

I s

- 1

59 I s -1 t e x t *
60 t a i l text5
61 cat t e x t l text5 > l e t t e r
62 v i m . t i n y l e t t e r
63 cat l e t t e r
64 cat memo
65 l p r memo
66 pine zach
67 I s -1
68 h i s t o r y
69 $ n
70 $ ! l p r
l p r memo
71 $ ! ? l e t t e r ?

cat l e t t e r
Table 9-8

Event designators

Designator

Meaning

!

Starts a history event unless followed immediately by SPACE, NEWLINE, =, or (.

Ü

The previous command.

!/i

Command number n in the history list.

I-/J

The nth preceding command.

I string

The most recent command line that started with string.

\?string[7]

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.

optional WORD DESIGNATORS
A word designator specifies a word (token) or series of words from an event.
(Table 9-9 on page 338 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 ending with n (the last word on the line).

HISTORY 3 3 7

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
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) using a caret ( A )
and the last word using a dollar sign ($). You can specify a range of words by separating two word designators with a hyphen.
72 $ echo a p p l e grape orange pear

apple grape orange pear
73 $ echo

!72:2

echo grape
grape
74 $ echo

!72:A

echo apple
apple
75 $ ! 7 2 : 0

!72:$

echo pear
pear
76 $ echo

!72:2-4

echo grape orange pear
grape orange pear
77 $ ! 7 2 : 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.tiny

vim.tiny

!$

report.718

If an event contains a single command, the word numbers correspond to the argument 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 the shell executes them
sequentially; the semicolon is word number 5.
78

$ ! 7 2 ; echo he!en zach barbara

echo apple grape orange pear ; echo helen zach b a r b a r a
apple grape orange pear
helen zach b a r b a r a
79 $ echo

!78:7

echo helen
helen
80

$ echo

!78:4-7

echo pear ; echo helen
pear
helen

338

CHAPTER 9

THE B O U R N E AGAIN SHELL

Table 9-9

Word designators

Designator

Meaning

n

The nth word. Word 0 is normally the command name.

A

The first word (after the command name).

$

The last word.

m-n

All words from word number /»through word number n;m defaults to 0 if you
omit it (0-/J).

n*

All words from word number «through the last word.

*

All words except the command name. The same as 1*.

%

The word matched by the most recent Istring? search.

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 following example shows the substitute modifier correcting a typo in the previous event:
$ car /home/zach/memo.0507

/home/max/letter.0507

bash: car: command not found
$

!!:s/car/cat

cat /home/zach/memo.0507 /home/max/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, replacing all occurrences of old. Although / is the delimiter in the examples, 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 (sllnewl) with the previous old string or string within a command that
you searched for with ?string?.
Quick substitution An abbreviated form of the substitute modifier is quick substitution. Use it to
reexecute the most recent event while changing some of the event text. The quick
substitution character is the caret ( A ). For example, the command

HISTORY

$

339

A o l d A n e w A

produces the same results as
$ !!:s/old/new/
Thus substituting cat for car in the previous event could have been entered as
$ AcarAcat
cat /home/zach/memo.0507 /home/max/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 designators. You can use multiple modifiers, each preceded by a colon (:).
The following series of commands uses Is to list the name of a file, repeats the command without executing it (p modifier), and repeats the last command, removing
the last part of the pathname (h modifier) again without executing it:
$ I s /etc/default/locale
/etc/default/locale

S ! ! :p

Is /etc/default/locale

$ ! ! :h:p

I s /etc/default

S

Table 9-10 lists event modifiers other than the substitute modifier.
Table 9-10

Event 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 individually

340

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 application 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
- / . i n p u t r c (page 343) for key binding information and configuration settings. The
— n o e d i t i n g 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 standalone versions of the emacs and vim 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 342). 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 E D I T I N G M O D E
Before you start, make sure the shell is in vi mode.
When you enter bash commands while in vi editing mode, you are in Input mode
(page 188). As you enter a command, if you discover an error before you press
RETURN, you can press ESCAPE to switch to vim Command mode. This setup is different
from the stand-alone vim editor's initial mode. While in Command mode you can
use many vim commands to edit the command line. It is as though you were using
vim to edit a copy of the history file with a screen that has room for only one command. 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 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.

The stand-alone editor starts in Command mode
tip The stand-alone vim editor starts in Command mode, whereas the command-line vim editor
starts in Input mode. If commands display characters and do not work properly, you are in Input
mode. Press ESCAPE and enter the c o m m a n d again.

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 vim editor, these search strings cannot

HISTORY

341

contain regular expressions. You can, however, start the search string with a caret ( A )
to force the shell to locate commands that start with the search string. As in vim,
pressing n after a successful search looks for the next occurrence of the same string.
You can also use event numbers to access events in the history list. 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: You can edit the command or press RETURN to execute it.
Once the command you want to edit is displayed, you can modify the command line
using vim 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 execute a command; simply press RETURN, even
if the cursor is in the middle of the command line.

emacs E D I T I N G M O D E
Unlike the vim 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 vim, 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 command. However, in a few cases, the emacs command-line editor commands differ
from those 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 C0NTR0L-B.
Press C0NTR0L-F to move one character forward. As in vim, you may precede these
movements with counts. To use a count you must first press ESCAPE; otherwise, the
numbers you type will appear on the command line.
Like vim, 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 using a count, press ESCAPE followed by the number and the appropriate
escape sequence. To move to the beginning of the line, press C0NTR0L-A; to the end of
the line, press CONTROL-E; and to the next instance of the character c, press C0NTR0L-X
C0NTR0L-F followed by c.
You can add text to the command line by moving the cursor to the position you
want to enter text and typing the desired text. To delete text, move the cursor just to
the right of the characters that you want to delete and press the erase key
(page 151) once for each character you want to delete.
CONTROL-D can terminate your screen s e s s i o n
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.

342

CHAPTER 9

THE B O U R N E AGAIN SHELL

If you want to delete the entire command line, type the line kill character (page 151).
You can type this character while the cursor is anywhere in the command line. If you
want to delete from the cursor to the end of the line, press CONTROL-K.
READLINE COMPLETION C O M M A N D S
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 (usually the first word on the command
line), pressing TAB initiates command completion, in which bash looks for a command whose name starts with the part of the word you have typed. If no command
starts with the characters you entered, bash beeps. If there is one such command,
bash completes the command name. If there is more than one choice, bash does
nothing 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 continue 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
bzcmp
bzgrep
$ bzU

bzip2
bzip2recover

bzless
bzmore

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. At this point the shell completes the
command because only one command starts with bzca.
$ bzc —>TAB (beep) —>TAB
bzcat
bzcmp
$ b z c a —>TAB — > t •

PATHNAME COMPLETION
Pathname completion, which also uses TABs, allows you to type a portion of a pathname and have bash supply the rest. If the portion of the pathname 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

$ cat films/dar —^TAB (beep)

343

cat f i lms/dark_B

In the f i l m s directory every file that starts with d a r has k _ as the next characters, so
bash cannot extend the line further without making a choice among files. The shell
leaves 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_ —>TABfbeepJ —>TAB
dark_passage dark_victory
$ cat f i l m s / d a r k _ B
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 —Passage
Because there is no further ambiguity, the shell appends a SPACE so you can finish typing 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 you are typing a variable name, pressing TAB results in variable
completion,
wherein bash attempts 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

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.

.¡nputrc:

CONFIGURING THE READLINE LIBRARY
The Bourne Again Shell and other programs that use the Readline Library read the
file specified by the INPUTRC environment variable to obtain initialization information. If INPUTRC is not set, these programs read the - / . i n p u t r c file. They ignore
lines of . i n p u t r c that are blank or that start with a hashmark (#).
VARIABLES
You can set variables in . i n p u t r c to control the behavior of the Readline Library
using the following syntax:
set variable value
Table 9-11 (on the next page) lists some variables and values you can use. See
R e a d l i n e Variables in the bash man or info page for a complete list.

344

CHAPTER 9

THE BOURNE AGAIN SHELL

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 e m a c s mode (the default). Similar to the set - o vi
and set - o emacs shell commands (page 340).

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. The default value is on.

mark-modified-lines

Set to on to cause Readline to precede modified history lines
with an asterisk. The default value is off.

KEY BINDINGS
You can specify bindings that map keystroke sequences to Readline commands, allowing you to change or extend the default bindings. Like the emacs editor, 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 c o n t r o l - 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 ) , M E T A ( \ M ) , and ESCAPE (\e). Specify a backslash
by escaping it with another backslash: \\. Similarly, a double or single quotation
mark can be escaped with a backslash: \" or V.
The k i l l - w h o l e - l i n e command, available in emacs mode only, deletes the current
line. Put the following command in . i n p u t r c to bind the k i l l - w h o l e - l i n e command
(which is unbound by default) to the keystroke sequence C0NTR0L-R:
control-r:

kill-whole-line

bind Give the command b i n d - P to display a list of all Readline commands. If a command is bound to a key sequence, that sequence is shown. Commands you can use
in vi mode start with vi. For example, v i - n e x t - w o r d and v i - p r e v - w o r d move the cursor to the beginning of the next and previous words, respectively. Commands that
do not begin with v i are generally available in emacs mode.
Use b i n d - q to determine which key sequence is bound to a command:

HISTORY 3 4 5
$ bind -q

kill-whole-line

k i l l - w h o l e - l i n e can be invoked v i a " \ 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 T h e L i n u x O p e r a t i n g System when
you type QQ.
CONDITIONAL CONSTRUCTS
You can conditionally select parts of the . i n p u t r c file using the $ i f directive. The
syntax of the conditional construct is
$if test[=value]
commands
[Seise
commands]
Sendif
where test is m o d e , t e r m , or bash. If test equals value (or if test is true when value is
not specified), this structure executes the first set of commands. If test does not
equal value (or if test is false when value is not specified), this construct executes
the second set of commands if they are present or exits from the structure if they are
not present.
The power of the $ i f 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 v i and false otherwise. You can test for v i or emacs.
2. You can test the type of terminal.
$if

term=xterm

The preceding test is true if the T E R M variable is set to x t e r m . 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 program 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 you are using the Readline Library with bash and other programs.

346

CHAPTER 9

THE BOURNE AGAIN SHELL

The following commands in .inputrc cause CONTROL-Y to move the cursor to the beginning of the next word regardless of whether bash is in vi or emacs mode:
$ cat

inputrc

set editing-mode v i
$ i f mode=vi
"\C-y": vi-next-word
$el se
"\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 / A R E A D L I N E , which searches for the word R E A D L I N E at the beginning
of a line.

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 will take effect.

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 - / . b a s h r c startup files so that they are available to interactive
subshells.
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 within quotation marks. An alias does not accept an argument
from the command line in value. Use a function (page 349) 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, give the command alias followed by the name of the alias.
You can use the unalias builtin to remove an alias.

ALIASES 3 4 7

When you give an alias builtin command without any arguments, the shell displays
a list of all defined aliases:
$ alias

alias
alias
alias
alias

ll ='ls -1'
l = ' l s -1 t r '
ls='ls -F'
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.

S I N G L E V E R S U S D O U B L E Q U O T A T I O N M A R K S IN A L I A S E S
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 P W D keyword variable holds the pathname of the working directory. Max creates two aliases while he is working in his home directory. Because he uses double
quotation marks when he creates the d i r A alias, the shell substitutes the value of the
working directory when he creates this alias. The alias d i r A command displays the
d i r A alias and shows that the substitution has already taken place:
$ echo $PWD

/home/max
$ alias
$ alias

d i r A = " e c h o Working d i r e c t o r y
dirA

i s $PWD"

a l i a s d i r A = ' e c h o Working d i r e c t o r y i s /home/max'

When Max creates the d i r B alias, he uses single quotation marks, which prevent the
shell from expanding the S P W D variable. The alias d i r B command shows that the
d i r B alias still holds the unexpanded S P W D variable:
$ alias
$ alias

d i r B = ' e c h o Working d i r e c t o r y
dirB

i s $PWD'

a l i a s d i r B = ' e c h o Working d i r e c t o r y i s SPWD'

After creating the d i r A and d i r B aliases, Max uses cd to make cars his working
directory and gives each of the aliases as commands. The alias he created using double quotation marks displays the name of the directory he created the alias in as the
working directory (which is wrong). In contrast, the d i r B alias displays the proper
name of the working directory:
$ cd c a r s
$ dirA

Working d i r e c t o r y i s /home/max
$ dirB

Working d i r e c t o r y i s /home/max/cars

348

CHAPTER 9

THE B O U R N E AGAIN SHELL

How to prevent the shell from invoking an alias
t i p 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 backslash, specify the command's absolute pathname, or give the command as ./command.

EXAMPLES OF ALIASES
The following alias allows you to type r to repeat the previous command or r a b c to
repeat the last command line that began with abc:
$ alias

r='fc

-s'

If you use the command Is —ltr frequently, you can create an alias that substitutes Is
- l t r when you give the command 1:
$ alias

l = '"ls

S 1
t o t a l 41
-rw-r--r-rw-r
-rw-r--r-rw-r--rdrwxrwxrdrwxrwxr-rwxr-xrdrwxrwxr-

-

X
X
X
X

1
1
1
1
2
2
1
2

-ltr'

max
max
max
max
max
max
max
max

group
group
group
group
group
group
group
group

30015
3089
641
484
1024
1024
485
1024

2009--03--01
2010--02--11
2010--04--01
2010--04--09
2010--08--09
2010--09--10
2010--09--21
2010--09--27

14:: 24
16:: 24
08:: 12
08:: 14
17:: 41
11:: 32
08:: 03
20:: 19

flute.ps
XTe rm.ad
f i x t a x . i cn
maptax.i cn
T i ger
testdi r
floor
Test_Emacs

Another common use of aliases is to protect yourself from mistakes. The following example substitutes the interactive version of the r m utility when you give the command z a p :
$ a l i a s zap='rm - i '
$ zap f *

rm: remove ' f i x t a x . i c n ' ? n
rm: remove ' f l u t e . p s ' ? n
rm: remove ' f l o o r ' ? n
The - i option causes rm to ask you to verify each file that would be deleted, thereby
helping you avoid deleting the wrong file. You can also alias rm with the r m - i command: alias r m = ' r m - i ' .
The aliases in the next example cause the shell to substitute Is - 1 each time you give
an 11 command and Is - F each time you use Is:
$ alias
$ alias

s n

ls='ls
ll='ls

t o t a l 41
drwxrwxrdrwxrwxr-rw-r
-rw-r--r-rw-r--r-rwxr-xr-rw-r--rdrwxrwxr-

X
X
-

X
-

X

2
2
1
1
1
1
1
2

-F'
-T

max
max
max
max
max
max
max
max

group
group
group
group
group
group
group
group

1024 2010- 09- 27 20:19 Test_Emacs/
1024 2010- 08- 09 17:41 T i g e r /
3089 2010- 02- 11 16:24 XTe rm.ad
641 2010- 04- 01 08:12 f i x t a x . i c n
30015 2009 -03 -01 14:24 f l u t e . p s
485 2010- 09- 21 08:03 f l o o r *
484 2010- 04- 09 08:14 maptax.icn
1024 2010- 09- 10 11:32 t e s t d i r /

FUNCTIONS 3 4 9

The - F option causes Is 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 11 (Is -1) itself contains an alias (Is). 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 Is, so a second substitution occurs to produce the final command Is - F -1. (To
avoid a recursive plunge, the Is 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

alias
alias
alias
alias
bash:

11 1 I s zap wx

ll ='ls -1'
l = ' l s -1 t r '
ls='ls -F'
zap=' rm - i '
a l i a s : wx: not found

You can avoid alias substitution by preceding the aliased command with a backslash (\):
S \ls

Test_Emacs XTerm.ad
Tiger
fixtax.icn

flute.ps
floor

maptax.icn
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:
$ 11 f-.'c

-rw-r--r--rw-r--r--rwxr-xr-x

1 max
1 max
1 max

group
group
group

641 2010-04-01 08:12 f i x t a x . i c n
30015 2009-03-01 14:24 f l u t e . p s
485 2010-09-21 08:03 f l o o r *

You can remove an alias with the unalias builtin. When the z a p alias is removed, it is no
longer displayed with the alias builtin and its subsequent use results in an error message:
$ unalias
$ alias

zap

alias ll = 'ls -1'
a l i a s l = ' l s -1 t r '
alias ls='ls - F '
$ zap m a p t a x . i c n

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 computer'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 a

350

CHAPTER 9

THE B O U R N E 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 ~ / . b a s h _ p r o f i l e 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 retain functions after you log out.

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.
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 opening 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 b r e a k statement (page 976) 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 output normally displayed by process being saved in .process.out:
start_process() {
process > .process.out 2>&1 &
}
The next example creates a simple function that displays the date, a header, and a
list of the people who are logged in on the system. This function runs the same commands as the w h o s o n script described on page 300. In this example the function is
being entered from the keyboard. The greater than (>) signs are secondary shell
prompts (PS2); do not enter them.
$ f u n c t i o n whoson

()

> *
>
date
>
echo "Users C u r r e n t l y Logged On"
>
who
> *
$ whoson

Mon Aug 9 15:44:58 PDT 2010
Users Currently Logged On
his
console
2010-08-08 08:59
max
pts/4
2010-08-08 09:33
zach
pts/7
2010-08-08 09:23

(:0)
(0.0)
(bravo.example.com)

FUNCTIONS

3 5 1

Functions in If you want to have the w h o s o n function always be available without having to
startup files enter it each time you log in, put its definition in ~ / . b a s h _ p r o f i l e . Then run
. b a s h _ p r o f i l e , using the . (dot) command to put the changes into effect immediately:
$ cat

~/.bash_profile

export TERM=vtl00
stty k i l l 'Au'
whoson ( )
{
date
echo "Users C u r r e n t l y 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 996). The following example
shows the a r g l function entered from the keyboard:
$ argl ( ) {
> echo " $ 1 "
>

}

$ argl

first_arg

f i rst_arg

See the function s w i t c h () on page 296 for another example of a function. "Functions" on page 993 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 t c s h users happy
function setenv()
{
if

fi

[ $# -eq 2 ]
then
eval $1=$2
export $1
el se
echo "Usage: setenv NAME VALUE" 1>&2

}
$ . ~/.bash_profile
$ setenv TCL_LIBRARY / u s r / l o c a l / 1 i b / t c l
$ env | grep TCL_LIBRARY

TCL_LIBRARY=/us r / 1 o c a l / I i b / t c l

eval The $# special parameter (page 997) takes on the value of the number of commandline arguments. This function uses the eval builtin to force bash to scan the command
$ 1 = $ 2 twice. Because $ 1 = $ 2 begins with a dollar sign ($), the shell treats the entire

352

CHAPTER 9

THE BOURNE AGAIN SHELL

string as a single token—a command. With variable substitution performed, the
command name becomes T C L _ L I B R A R Y = / u s r / l o c a l / l i b / t c l , 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 A N D O P T I O N S

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 - 1 2 lists some commonly used command-line options.
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 340) to edit command lines in an interactive
shell.

—noediting

Prevents reading these startup files (page 293):

—noprofile

No rc

Prevents reading the -/.bashrc startup file
(page 294). This option is on by default if the shell is
called as sh.

—norc

POSIX

Runs b a s h in POSIX mode.

—posix

Version

Displays b a s h version information and exits.

—version

Login

Causes b a s h to run as though it were a login shell.

- I (lowercase "I")

shopt

Runs a shell with the opt s h o p t option (next page).
A - 0 (uppercase "0") sets the option; +0 unsets it.

[±]0 [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 (-).

No profile

/etc/profile, ~/.bash_profile, -/.bashjogin, and
-/.profile.

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

CONTROLLING

bash:

FEATURES A N D O P T I O N S 3 5 3

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.

Features, options, variables?
tip To avoid confusing terminology, this book refers to the various shell behaviors that you can control
as features. The b a s h info page refers to them as "options" and "values of variables controlling
optional shell behavior."
s e t ±o: T U R N S S H E L L FEATURES O N A N D O F F
The set builtin, when used with the - o or +o option, enables, disables, and lists certain bash features. For example, the following command turns on the noclobber
feature (page 248):
$ set - o

noclobber

You can turn this feature off (the default) by giving the command
$ s e t +o noclobber

The command set - o without an option lists each of the features controlled by set, followed by its state (on or off). The command set +o without an option lists the same features in a form you can use as input to the shell. Table 9-13 (next page) lists bash features.

shopt: T U R N S S H E L L F E A T U R E S O N A N D O F F
The shopt (shell option) builtin enables, disables, and lists certain bash features that
control the behavior of the shell. For example, the following command causes bash
to include filenames that begin with a period (.) when it expands ambiguous file references (the - s stands for set):
$ shopt

-s

dotglob

You can turn this feature off (the default) by giving the following command (the - u
stands for unset):
$ shopt

-u dotglob

The shell displays how a feature is set if you give the name of the feature as the only
argument to shopt:
$ shopt

dotglob

dotglob

off

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 features controlled by shopt that are set or on. The command shopt - u lists the features
that are unset or off. Table 9-13, next page) lists bash features.

Setting set ±o features using s h o p t
tip You can use s h o p t to set/unset features that are otherwise controlled by set ±o. Use the regular
shopt syntax with - s or - u and include the - o option. For example, the following command turns
on the noclobber feature:
$ shopt

- o - s noclobber

354

CHAPTER 9

Table 9-13

THE BOURNE AGAIN SHELL

b a s h features

Feature

Description

Syntax

Alternate syntax

allexport

Automatically exports all variables and
functions you create or modify after giving
this command.

set-o allexport

set -a

braceexpand

Causes b a s h to perform brace expansion
(the default; page 358).

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 256) in an ambiguous file reference
to match a leading period in a filename. By
default special characters do not match a
leading period. You must always specify
the filenames. and .. explicitly because no
pattern ever matches them.

shopt-s dotglob

emacs

Specifies e m a c s editing mode for
command-line editing (the default;
page 341).

set-o emacs

errexit

Causes b a s h to exit when a simple command (not a control structure) fails.

set-o errexit

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 e x e c cannot find the file
that is given as its argument.

shopt-s execfail

expand_aliases

Causes aliases (page 346) to be expanded
(by default it is on for interactive shells and
off for noninteractive shells).

shopt-s expand_alias

hashall

Causes b a s h to remember where commands it has found using PATH (page 319)
are located (default).

set-o hashall

histappend

Causes b a s h to append the history list to
the file named by HISTFILE (page 330)
when the shell exits. By default b a s h overwrites this file.

shopt-s histappend

histexpand

Turns on the history mechanism (which
uses exclamation points by default;
page 335). Turn this feature off to turn off
history expansion.

set-o histexpand

set -e

set -h

set -H

CONTROLLING

Table 9-13

bash:

FEATURES A N D O P T I O N S

355

b a s h features (continued)

Feature

Description

Syntax

history

Enables command history (on by default;
page 330).

set-o history

huponexit

Specifies that b a s h send a SIGHUP signal
to all jobs when an interactive login shell
exits.

shopt-s huponexit

ignoreeof

Specifies that b a s h 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 307).

set-o monitor

nocaseglob

Causes ambiguous file references
(page 256) to match filenames without
regard to case (off by default).

shopt-s nocaseglob

noclobber

Helps prevent overwriting files (off by
default; page 248).

set-o noclobber

set -C

noglob

Disables pathname expansion (off by
default; page 256).

set-o noglob

set -f

notify

With job control (page 307) enabled,
reports the termination status of background 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 b a s h to expand ambiguous file
references (page 256) that do not match a
filename to a null string. By default b a s h
passes these file references without
expanding them.

shopt-s nullglob

posix

Runs b a s h in POSIX mode.

set-o posix

verbose

Displays command lines as b a s h reads
them.

set-o verbose

vi

Specifies vi editing mode for commandline editing (page 340).

set-o vi

xpg_echo

Causes the e c h o builtin to expand backslash escape sequences without the need
for the - e option (page 980).

shopt - s xpg_echo

xtrace

Turns on shell debugging (page 966).

set -o xtrace

Alternate syntax

set -m

set - V

set-x

356

CHAPTER 9

THE B O U R N E AGAIN SHELL

PROCESSING THE C O M M A N D 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 i f 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 322) as you type each line of a multiline command until it recognizes
the end of the command:
$ echo
> end'

'hi

hi
end
$ function hello

()

{

> echo h e l l o there
> }

S

After reading a command line, bash applies history expansion and alias substitution
to the line.

HISTORY EXPANSION
"Reexecuting and Editing Commands" on page 332 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 executable 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 h i s t e x p a n d turns it off. History
expansion does not apply to noninteractive shells (shell scripts).

ALIAS SUBSTITUTION
Aliases (page 346) 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 A N D S C A N N I N G THE C O M M A N D LINE
After processing history commands and aliases, bash does not execute the command
immediately. One of the first things the shell does is to parse (isolate strings of characters in) the command line into tokens or words. The shell then scans each token for
special characters and patterns that instruct the shell to take certain actions. These
actions can involve substituting one word or words for another. When the shell parses
the following command line, it breaks it into three tokens (cp, - / l e t t e r , and .):
$ cp - / l e t t e r

.

P R O C E S S I N G THE C O M M A N D LINE 3 5 7

After separating tokens and before executing the command, the shell scans the
tokens and performs command-line expansion.

COMMAND-LINE EXPANSION
Both interactive and noninteractive shells transform the command line using commandline expansion before passing the command line to the program being called. You can
use a shell without knowing much about command-line expansion, but you can use
what a shell has to offer to a better advantage with an understanding of this topic. This
section covers Bourne Again Shell command-line expansion.
The Bourne Again Shell scans each token for the various types of expansion and
substitution in the following order. Most of these processes expand a word into a
single word. Only brace expansion, word splitting, and pathname expansion can
change the number of words in a command (except for the expansion of the variable
seepage 1000).
1. Brace expansion (page 358)
2. Tilde expansion (page 359)
3. Parameter and variable expansion (page 360)
4. Arithmetic expansion (page 360)
5. Command substitution (page 362)
6. Word splitting (page 363)
7. Pathname expansion (page 363)
8. Process substitution (page 365)
Quote removal After bash finishes with the preceding list, it removes from the command line single
quotation marks, double quotation marks, and backslashes that are not a result of
an expansion. This process is called quote removal.
ORDER OF EXPANSION
The order in which bash carries out these steps affects the interpretation of commands. For example, if you set a variable to a value that looks like the instruction
for output redirection and then enter a command that uses the variable's value to
perform redirection, you might expect bash to redirect the output.
$ SENDIT="> / t m p / s a v e i t "
$ echo xxx $SENDIT

xxx > /tmp/saveit
$ cat

/tmp/saveit

c a t : / t m p / s a v e i t : No such f i l e o r d i r e c t o r y

In fact, the shell does not redirect the output—it recognizes input and output redirection before it evaluates variables. When it executes the command line, the shell
checks for redirection and, finding none, evaluates the SENDIT variable. After

358

CHAPTER 9

THE B O U R N E AGAIN SHELL

replacing the variable with > / t m p / s a v e i t , bash passes the arguments to echo, which
dutifully copies its arguments to standard output. No / t m p / s a v e i t file is created.
The following sections provide more detailed descriptions of the steps involved in
command processing. Keep in mind that double and single quotation marks cause
the shell to behave differently when performing expansions. Double quotation
marks permit parameter and variable expansion but suppress other types of expansion. Single quotation marks suppress all types of expansion.
BRACE EXPANSION
Brace expansion, which originated in the C Shell, provides a convenient way to
specify filenames when pathname expansion does not apply. Although brace expansion is almost always used to specify filenames, the mechanism can be used to generate arbitrary strings; the shell does not attempt to match the brace notation with
the names of existing files.
Brace expansion is turned on in interactive and noninteractive shells by default; you
can turn it off with set +o b r a c e e x p a n d . The shell also uses braces to isolate variable
names (page 316).
The following example illustrates how brace expansion works. The Is 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 (because there are no files in the working directory).
$

I s

$ echo chap_{one, two, three}- . t x t

chap_one.txt chap_two.txt

chap_three.txt

The shell expands the comma-separated strings inside the braces in the echo command into a SPACE-separated list of strings. Each string from the list is prepended
with the string chap_, called the preamble, and appended with the string . t x t , called
the postscript. Both the preamble and the postscript are optional. The left-to-right
order of the strings within the braces is preserved in the expansion. For the shell to
treat the left and right braces specially and for brace expansion to occur, at least one
comma and no unquoted whitespace characters must be inside the braces. You can
nest brace expansions.
Brace expansion is useful when there is a long preamble or postscript. The following example copies four files—main.c, f l . c , f 2 . c , and tmp.c—located in the
/ u s r / l o c a l / s r c / C directory to the working directory:
$ cp / u s r / " l o c a " l / s r c / C / - { m a i n , f l , f 2 , t m p } - . c

.

You can also use brace expansion to create directories with related names:
$ Is

fi lei

-F

f i le2

fileB

$ mkdir v r s { A , B , C , D , E }
$ I s -F

filel

file2

fileB

vrsA/

vrsB/

vrsC/

vrsD/

vrsE/

P R O C E S S I N G THE C O M M A N D LINE 3 5 9

The - F option causes Is to display a slash (/) after a directory and an asterisk ( * )
after an executable file.
If you tried to use an ambiguous file reference instead of braces to specify the directories, the result would be different (and not what you wanted):
$ rmdir v r s *
$ mkdir v r s [ A - E ]
$ I s -F

fi lei

f i le2

file 3

vrs[A-E]/

An ambiguous file reference matches the names of existing files. In the preceding
example, because it found no filenames matching vrs[A-E], bash passed the ambiguous file reference to mkdir, which created a directory with that name. Brackets in
ambiguous file references are discussed on page 259.
TILDE EXPANSION
Chapter 6 introduced a shorthand notation to specify your home directory or the
home directory of another user. This section provides a more detailed explanation
of tilde expansion.
The tilde (~) is a special character when it appears at the start of a token on a command line. When it sees a tilde in this position, bash looks at the following string of
characters—up to the first slash (/) or to the end of the word if there is no slash—as
a possible username. If this possible username is null (that is, if the tilde appears as
a word by itself or if it is immediately followed by a slash), the shell substitutes the
value of the HOME variable for the tilde. The following example demonstrates this
expansion, where the last command copies the file named letter from Max's home
directory to the working directory:
$ echo $HOME

/home/max
$ echo ~

/home/max
$ echo

~/1etter

/home/max/letter
$ cp - / l e t t e r

.

If the string of characters following the tilde forms a valid username, the shell substitutes the path of the home directory associated with that username for the tilde
and name. If the string is not null and not a valid username, the shell does not make
any substitution:
$ echo ~zach

/home/zach
$ echo ~ r o o t

/ root
$ echo ~xx
-XX

360

CHAPTER 9

THE BOURNE AGAIN SHELL

Tildes are also used in directory stack manipulation (page 310). In addition, ~+ is a
synonym for P W D (the name of the working directory), and — is a synonym for
O L D P W D (the name of the previous working directory).
PARAMETER AND VARIABLE EXPANSION
On a command line, a dollar sign ($) that is not followed by an open parenthesis
introduces parameter or variable expansion. Parameters include both commandline, or positional, parameters (page 996) and special parameters (page 994). Variables include both user-created variables (page 314) and keyword variables
(page 318). The bash man and info pages do not make this distinction.
Parameters and variables are not expanded if they are enclosed within single quotation
marks or if the leading dollar sign is escaped (i.e., 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 27-8 on
page 1019). Arithmetic in bash is done using integers. Unless you use variables of
type integer (page 318) or actual integers, however, the shell must convert stringvalued variables to integers for the purpose of the arithmetic evaluation.
You do not need to precede variable names within expression with a dollar sign ($).
In the following example, after read (page 1003) assigns the user's response to age,
an arithmetic expression determines how many years are left until age 60:
$ cat

#!/bi
echo
read
echo
$

age_check

n/bash
- n "How o l d are you? "
age
"Wow, i n $ ( ( 6 0 - a g e ) ) y e a r s , y o u ' l l be 60!"

./age_check

How o l d are you? 55
Wow, i n 5 y e a r s , y o u ' l l 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:

P R O C E S S I N G THE C O M M A N D LINE 3 6 1
$ echo There a r e $ ( ( 6 0 * 6 0 * 2 4 * 3 6 5 ) )

seconds i n a n o n - l e a p

year.

There are 31536000 seconds i n a n o n - l e a p y e a r .

The next example uses wc, cut, arithmetic expansion, and command substitution
(page 362) 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 -1 option is the
number of lines in the file, in columns (character positions) 1 through 4, followed
by a SPACE and the name of the file (the first command following). The cut utility with
the - c l - 4 option extracts the first four columns.
$ wc - 1

letter.txt

351 l e t t e r . t x t
$ wc - 1 l e t t e r . t x t

| cut

-cl-4

351

The dollar sign and single parenthesis instruct the shell to perform command substitution; the dollar sign and double parentheses indicate arithmetic expansion:
$ echo $ ( (

$(wc - 1 l e t t e r . t x t

| cut

- c l - 4 ) / 6 6 + 1))

6

The preceding example sends standard output from wc to standard input of cut via a
pipe. Because of command substitution, the output of both commands replaces the
commands between the $( and the matching ) on the command line. Arithmetic
expansion then divides this number by 66, the number of lines on a page. A 1 is
added because the integer division results in any remainder being discarded.

Fewer dollar signs ($)
tip When you use variables within $(( and )), the dollar signs that precede individual variable references are optional:
$ x=23 y=37
$ echo $( ( 2 * $ x + 3 * $ y ) )

157

$ echo $( ( 2 * x + 3 * y ) )

157

Another way to get the same result without using cut is to redirect the input to wc
instead of having wc get its input from a file you name on the command line. When
you redirect its input, wc does not display the name of the file:
$ wc - 1 <

letter.txt

351

It is common practice to assign the result of arithmetic expansion to a variable:
$ numpages=S(( $(wc - 1 < l e t t e r . t x t ) / 6 6

+1))

letbuiltin The let builtin evaluates arithmetic expressions just as the $(( )) syntax does. The
following command is equivalent to the preceding one:
$ let

"numpages=$(wc - 1 < l e t t e r . t x t ) / 6 6

+ 1"

362

CHAPTER 9

THE B O U R N E 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, its
exit status is 0.
You can supply let with multiple arguments on a single command line:
$ l e t a=5+3 b=7+2
$ echo $a $b

89

When you refer to variables when doing arithmetic expansion with let or $(( )), the
shell does not require a variable name to begin with a dollar sign ($). Nevertheless,
it is a good practice to do so for consistency, as in most places you must precede a
variable name with a dollar sign.
COMMAND SUBSTITUTION
Command substitution replaces a command with the output of that command. The
preferred syntax for command substitution under bash follows:
$ (command)
Under bash you can also use the following, older syntax:
'

command'

The shell executes command within a subshell and replaces command, along with
the surrounding punctuation, with standard output of command.
In the following example, the shell executes pwd and substitutes the output of the
command for the command and surrounding punctuation. Then the shell passes the
output of the command, which is now an argument, to echo, which displays it.
$ echo $(pwd)

/home/max

The next script assigns the output of the pwd builtin to the variable w h e r e and displays a message containing the value of this variable:
$ c a t where

where=$(pwd)
echo "You are using the Swhere d i r e c t o r y . "
$

./where

You are using the /home/zach d i r e c t o r y .
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:
$ c a t where2

echo "You are using the $(pwd) d i r e c t o r y . "
$

./where2

You are using the /home/zach d i r e c t o r y .

P R O C E S S I N G T H E C O M M A N D LINE 3 6 3

The following command uses find to locate files with the name R E A D M E in the
directory tree rooted at the working directory. This list of files is standard output of
find and becomes the list of arguments to Is.
$ Is

-1 $ ( f i n d

. -name README - p r i n t )

The next command line shows the older * command * syntax:
$ Is

-1

* find

. -name README - p r i n t '

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 R E A D M E files whose size
exceeds the size of . / R E A D M E with the following command:
$ Is

-1 $ ( f i n d

. -name README - s i z e +$(echo $ ( c a t

./README | wc - c ) c

) -print

)

Try giving this command after giving a set - x command (page 9 6 6 ) to see how bash
expands it. If there is no R E A D M E file, you just get the output of Is - 1 .
For additional scripts that use command substitution, see pages 9 6 2 , 9 8 1 , and 1011.

$(( Versus $(
tip The symbols $(( constitute a single token. They introduce an arithmetic expression, not a command substitution. Thus, if you want to use a parenthesized subshell (page 306) within $(), you
must insert a SPACE between the $( and the following (.
W O R D SPLITTING
The results of parameter and variable expansion, command substitution, and arithmetic expansion are candidates for word splitting. Using each character of IFS
(page 323) 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 256), also called filename generation or globbing, is the
process of interpreting ambiguous file references and substituting the appropriate list
of filenames. Unless n o g l o b (page 355) is set, the shell performs this function when it
encounters an ambiguous file reference—a token containing any of the unquoted
characters * , ?, [, or ]. If bash cannot locate any files that match the specified pattern, the token with the ambiguous file reference is left alone. The shell does not
delete the token or replace it with a null string but rather passes it to the program as
is (except see n u l l g l o b on page 355).
In the first echo command in the following example, the shell expands the ambiguous file reference t m p * and passes three tokens ( t m p l , t m p 2 , and t m p 3 ) to echo.
The echo builtin displays the three filenames it was passed by the shell. After r m

364

CHAPTER 9

THE B O U R N E AGAIN SHELL

removes the three t m p * files, the shell finds no filenames that match t m p * when it
tries to expand it. It then passes the unexpanded string to the echo builtin, which
displays the string it was passed.
$

I s

tmpl tmp2 tmpB
$ echo tmp*

tmpl tmp2 tmpB
$ 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 d o t g l o b (page 354). The option nocaseglob
(page 355) 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 kinds of expansion except parameter and variable expansion. Putting single quotation marks around an argument suppresses all types of
expansion. The second echo command in the following example shows the variable
Smax 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 t m p * .
$ echo tmp* $max

tmpl tmp2 tmp3 sonar
$ echo " t m p * $max"

tmp* sonar
$ echo

'tmp*

$max'

tmp* Smax

The shell distinguishes between the value of a variable and a reference to the variable and does not expand ambiguous file references if they occur in the value of a
variable. As a consequence you can assign to a variable a value that includes special
characters, such as an asterisk ( * ) .
Levels of expansion In the next example, the working directory has three files whose names begin with
letter. When you assign the value l e t t e r * 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
l e t t e r * ; context alone prevents the expansion. After the assignment the set builtin
(with the help of grep) shows the value of var to be l e t t e r * .
$ Is

letter*

letterl

letter2

$ var=letter*
$ s e t | grep var

var=' l e t t e r * '

letterB

CHAPTER S U M M A R Y

$ echo

3 6 5

'$var'

$var
$ echo

"$var"

letter*
$ echo $var

l e t t e r l letter2

letterB

The three echo commands demonstrate three levels of expansion. When Svar is
quoted with single quotation marks, the shell performs no expansion and passes the
character string Svar to echo, which displays it. With double quotation marks, the
shell performs variable expansion only and substitutes the value of the v a r variable
for its name, preceded by a dollar sign. No pathname expansion is performed on
this command because double quotation marks suppress it. In the final command,
the shell, without the limitations of quotation marks, performs variable substitution
and then pathname expansion before passing the arguments to echo.
PROCESS SUBSTITUTION
A special feature of the Bourne Again Shell is the ability to replace filename arguments with processes. An argument with the syntax <(command) 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 command
reads as standard input.
The following example uses sort (page 168) with the - m (merge, which works correctly only if the input files are already sorted) option to combine two word lists
into a single list. Each word list is generated by a pipe that extracts words matching
a pattern from a file and sorts the words in that list.
$ sort

-m - f

< ( g r e p "[AA-Z]..$" memol | s o r t )

<(grep " . * a b a . * "

memo2

|sort)

CHAPTER S U M M A R Y
The shell is both a command interpreter and a programming language. As a command interpreter, it 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 Assuming the file holding a shell script is in the working directory, there are three
shell script 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 b a s h followed by the name of the file.

366

CHAPTER 9

THE BOURNE AGAIN SHELL

Technique 1 requires that the working directory be in the PATH variable. Techniques 1 and 2 require that you have execute and read permission for the file holding 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 using the fg builtin. You can put a foreground
job into the background using the b g builtin, provided that you first suspend 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 using unset. Variables
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 variables.
The shell defines 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. 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; 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 When using an interactive Bourne Again Shell, you can edit a command line and
editors commands from the history file, using either of the Bourne Again Shell's command-

line editors (vim or e m a c s ) . When you use the v i m command-line editor, you start in
Input mode, unlike vim. You can switch between Command and Input modes. The
e m a c s editor is modeless and distinguishes commands from editor input by recognizing control characters as commands.
Aliases An alias is a name that the shell translates into another name or (complex) command. 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, is parsed prior to
being stored in memory. As a consequence shell functions run faster than shell
scripts. Shell scripts are parsed at runtime and are stored on disk. A function can be
defined on the command 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 functions in many programming languages, a shell function is called by giving
its name followed by any arguments.

EXERCISES 3 6 7

Shell features There are several ways to customize the shell's behavior. You can use options on the
command line when you call bash. You can use the bash set and shopt builtins to
turn features on and off.
Command-line When it processes a command line, the Bourne Again Shell may replace some
expansion 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). Table 9-6 on page 326 lists these special characters. The
expansions take place in a specific order. Following the history and alias expansions, 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: / b i n/date . . .
$ echo $PATH

. :/usr/local/bin:/usr/bin:/bin
$ cat > date

echo " T h i s i s my own v e r s i o n o f d a t e . "
$

./date

F r i May 21 11:45:49 PDT 2010

2. What are two ways you can execute a shell script when you do not have
execute permission for the file containing the script? Can you execute a
shell script if you do not have read 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 b i n directory in your home directory
• The working directory

368

CHAPTER 9

THE B O U R N E AGAIN SHELL

b. If there is a file named d o i t in / u s r / b i n and another file with the same
name in your - / b i n directory, which one will be executed? (Assume that
you have execute permission for both files.)
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 you have made the following assignment:
$ person=zach

Give the output of each of the following commands:
a. e c h o S p e r s o n
b. e c h o ' S p e r s o n '
c. e c h o " S p e r s o n "

5. The following shell script adds entries to a file named j o u r n a l - f i l e in your
home directory. This script helps you keep track of phone conversations
and meetings.
$ cat

journal

# j o u r n a l : add j o u r n a l e n t r i e s t o the f i l e
# SHOME/journal - f i l e
file=$HOME/journal-file
date » S f i l e
echo - n " E n t e r name o f person o r group: "
read name
echo "Sname" » S f i l e
echo » S f i l e
cat » S f i l e
echo "
echo » S f i l e

" »

Sfile

a. What do you have to do to the script to be able to execute it?
b. Why does the script use the read builtin the first time it accepts input
from the terminal and the cat utility the second time?
6. Assume the / h o m e / z a c h / g r a n t s / b i b l i o s and / h o m e / z a c h / b i b l i o s directories
exist. Give Zach's working directory after he executes each sequence of
commands given. Explain what happens in each case.
a.

S

pwd

/home/zach/grants
$ CDPATH=$(pwd)
$ cd
$ cd b i b l i o s

ADVANCED EXERCISES 3 6 9

b.
$ pwd
/home/zach/grant s
$ CDPATH=$(pwd)
$ cd $HOME/biblios

7. Name two ways you can identify the PID number of the login shell.
8. Give the following command:
$ sleep 30 | c a t

/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?

A D V A N C E D EXERCISES
9. Write a sequence of commands or a script that demonstrates variable
expansion occurs before pathname expansion.
10. Write a shell script that outputs the name of the shell executing it.
11. Explain the behavior of the following shell script:
$ cat

quote_demo

twol i ner="Thi s i s l i n e 1.
This i s line 2 . "
echo " $ t w o l i n e r "
echo $ t w o l i n e r

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 ] Is

xxx

I s : x x x : No such f i l e o r d i r e c t o r y
$ [11

13. The dirname utility treats its argument as a pathname and writes to standard output the path prefix—that is, everything up to but not including
the last component:
$ dirname

a/b/c/d

a/b/c

If you give d i r n a m e a simple filename (no / characters) as an argument,
d i r n a m e writes a . to standard output:

370

CHAPTER 9

THE B O U R N E AGAIN SHELL
$ dirname

simple

Implement dirname as a bash function. Make sure that it behaves sensibly
when given such arguments as /.
14. Implement the basename utility, which writes the last component of its
pathname argument to standard output, as a bash function. For example,
given the pathname a / b / c / d , basename writes d to standard output:
$ basename a / b / c / d

d

15. The Linux basename utility has an optional second argument. If you give
the command basename path suffix, basename removes the suffix and the
prefix from path:
$ basename s r c / s h e l l f i l e s / p r o g . b a s h

.bash

$ basename s r c / s h e l l f i l e s / p r o g . b a s h

.c

prog

prog.bash

Add this feature to the function you wrote for exercise 14.

10
NETWORKING AND THE
INTERNET
The communications facilities linking computers are continually
improving, allowing faster and more economical connections. The
earliest computers were unconnected stand-alone systems. To

IN THIS CHAPTER
Types of Networks and How
They Work
Network Protocols

373
379

transfer information from one system to another, you had to store
it in some form (usually magnetic tape, paper tape, or punch

Network Utilities

390

PIConnecHonNetWOr

393

cards—called IBM or Hollerith cards), carry it to a compatible system, and read it back in. A notable advance occurred when computers began to exchange data over serial lines, although the
transfer rate was slow (hundreds of bits per second). People
quickly invented new ways to take advantage of this computing

traceroute: Traces a Route over
394

power, such as email, news retrieval, and bulletin board services.

host and dig: Query Internet
Nameservers

396

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.

Distributed Computing

397

T o d a y it W Q u l d b e

Usenet

407

not include a LAN to link its systems. Linux systems are typi-

WWW: World Wide Web

409

the Internet

difficult to find a computer facility that does

ca

^ y attached to an Ethernet (page 1147) 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).
371

372

CHAPTER 1 0

N E T W O R K I N G 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
thousands of miles away and receive a reply within minutes. A related term, intranet, refers to the networking infrastructure within a company or other institution.
Intranets are usually private; access to them from external networks may be limited
and carefully controlled, typically using firewalls (page 379).
Network services Over the past decade many network services have emerged and become standardized. On Linux and UNIX systems, special processes called daemons (page 1144)
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, v i m , Ipr,
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 663) 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. Communication 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
Pidgin) 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 running different versions of Windows, machines running UNIX and Linux, and so on.
Each of these machines can communicate via IP (page 380), 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 transmits 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 N E T W O R K S A N D H O W THEY W O R K

373

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 1135)

hub (page 1152)

packet (page 1164)

bridge (page 1138)

internet (page 1154)

router (page 1170)

extranet (page 1147)

Internet (page 1154)

sneakernet

firewall (page 1148)

intranet (page 1154)

switch (page 1175)

gateway (page 1149)

ISP (page 1155)

VPN (page 1180)

(page 1172)

TYPES O F N E T W O R K S A N D H O W T H E Y W O R K
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 popular, 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 lOOOBaseT (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 40 gigabits per second
(OC768) to accommodate the ever-increasing demand for network services.
Table 10-1 lists some of the specifications in use today.
Table 10-1

Network specifications

Specification

Speed

DSO

64 kilobits per second

ISDN

Two DSO lines plus signaling (16 kilobits per second) or 128 kilobits per
second

T-1

1.544 megabits per second (24 DSO lines)

T-3

43.232 megabits per second (28 T-1 s)

0C3

155 megabits per second (100 T-1 s)

0C12

622 megabits per second (4 0C3s)

0C48

2.5 gigabits per seconds (4 0C12s)

OC192

9.6 gigabits per second (4 0C48s)

0C768

38.4 gigabits per second (4 0C192s)

374

CHAPTER 1 0

N E T W O R K I N G 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 garbled. 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 network; if the collision rate gets too high, retransmissions
may result in more collisions. 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 endpoints are involved. However, most connections to WANs (wide area networks) go
through point-to-point links, using wire cable, radio, or satellite links. The advantage
of a 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 system
can typically be equipped for only a small number of such links; it is impractical and
costly to establish point-to-point links that connect each computer to all the rest.
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 inexpensively 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-l, 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 x D S L suite o f technologies, which includes A D S L , X D S L , SDSL, and H D S L .

TYPES OF N E T W O R K S A N D H o w THEY W O R K

375

L A N : LOCAL AREA N E T W O R K
Local area networks (LANs) are confined to a relatively small area—a single computer 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 376). A router may be a general-purpose computer 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 overhead, file transfer rates on an Ethernet are always slower than the
maximum, theoretical transfer rate.
Cables An Ethernet network transfers data using copper or fiberoptic cable or wireless transmitters and receivers. Originally, each computer was attached to a thick coaxial cable
(called thicknet) at tap points spaced at six-foot intervals along the cable. The thick
cable was awkward to deal with, so other solutions, including a thinner coaxial cable
called thinnet, or 10Base2,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], lOBaseT, 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 380).
Duplex In half-duplex mode, packets travel in one direction at a time over the cable. In fullduplex 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 380).
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 packets only
to their destination devices, it can conserve network bandwidth and perform 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 380).

2 . Versions o f Ethernet are classified as X B a s e Y , where X is the d a t a rate in megabits per second, B a s e
means b a s e b a n d (as opposed to radio frequency), and Y is the category o f cabling.

376

CHAPTER 1 0

N E T W O R K I N G 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 the speed of the network (e.g., 100 megabits
per second) for an effective bandwidth between hosts of twice the speed of the network (e.g., 200 megabits per second), depending on the capacity of the switch.
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 380). 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 the Linux Wireless LAN
HO WTO at www.hpl.hp.com/personal/Jean_Tourrilhes/Linux.

W A N : W I D E AREA NETWORK
A wide area network (WAN) covers a large geographic area. In contrast, the technologies (such as Ethernet) used for LANs were designed to work over limited distances and for a certain number of host connections. A WAN 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 metropolitan area network (MAN) is a network that is contained in a smaller geographic
area, such as a city. Like WANs, MANs are typically used to interconnect LANs.

I N T E R N E T W O R K I N G T H R O U G H GATEWAYS A N D 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 format that can be processed by the communications equipment that

TYPES OF N E T W O R K S A N D H o w THEY W O R K

377

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.
Router A router is the most popular form of gateway. Routers play an important role in
internetworking. Just as you might study a map to plan your route when you need to
drive to an unfamiliar place, so a computer needs to know how to deliver a message
to a system attached to a distant network by passing through intermediary systems
and networks along the way. Although you might envision using a giant network
road map to choose the route that your data should follow, a static map of computer
routes is usually a poor choice for a large network. Computers and networks along
the route you choose 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 destination; instead you head for a nearby gas station and ask directions. Throughout the
journey you continue to stop at one gas station after another, getting directions at
each to find the next one. Although it would take a while to make the stops, the
owner of each gas station would advise you of bad traffic, closed roads, alternative
routes, and shortcuts.
The stops made by the data are much quicker than those you would make in your
car, but each message leaves each router on a path chosen based on the most current
information. Think of this system as a GPS (global positioning system) setup that
automatically gets updates at each intersection and tells you where to go next,
based on traffic and highway conditions.
Figure 10-1 (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 connection 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 sharing a single Ethernet LAN. Site B has an Ethernet LAN that serves a printer and
four Linux workstations. A firewall permits only certain traffic to pass between the
Internet router and the site's local router. Site C has three LANs linked by a single
router, perhaps to reduce the traffic load that would result if the LANs were combined or to keep workgroups or locations on separate networks. Site C also includes
a wireless access point that enables wireless communication with nearby computers.

Figure 10-1

A slice of the Internet

TYPES OF N E T W O R K S A N D H o w THEY W O R K

379

FIREWALL
A firewall in a car separates the engine compartment from the passenger compartment, protecting the driver and passengers from engine fires, noise, and fumes. In
much the same way, computer firewalls separate computers from malicious and
unwanted users.
A firewall prevents certain types of traffic from entering or leaving a network. For
example, a firewall might prevent traffic from your IP address from leaving the
network and prevent anyone except users from selected domains from using FTP to
retrieve data from the network. The implementations of firewalls vary
widely—from Linux machines with two interfaces (page 1154) running custom
software to a router (preceding section) with simple access lists to esoteric, vendorsupplied firewall appliances. Most larger installations have at least one kind of
firewall in place. A firewall is often accompanied by a proxy server/gateway
(page 405) that provides an intermediate point between you and the host you are
communicating with.
In addition to the firewalls found in multipurpose computers, firewalls are becoming increasingly common in consumer appliances. For example, they are built into
cable modems, wireless gateways, routers, and stand-alone devices.
Typically a single Linux machine will include a minimal firewall. A small group of
Linux systems may have an inexpensive Linux machine with two network interfaces
and packet-filtering software functioning as a dedicated firewall. One of the interfaces connects to the Internet, modems, and other outside data sources. The other
connects, normally through a hub or switch, to the local network. Refer to
Chapter 25 for information on gufw, 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
common language, or protocol (page 1166). The protocol determines the format
of message packets. The predominant network protocols used by Linux systems
are TCP and IP,3 collectively referred to as TCP/IP (Transmission Control Protocol and Internet 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

3. All references to IP imply IPv4

(page 1 1 5 5 ) .

4 . Voice and video protocols are delay sensitive, n o t integrity sensitive. T h e h u m a n ear and eye accept and
interpolate loss in an audio o r video stream but c a n n o t deal with variable delay. T h e guaranteed delivery
that T C P provides introduces a delay on a busy n e t w o r k when packets get retransmitted. T h i s delay is n o t
acceptable for video and audio transmissions, whereas less than 1 0 0 percent integrity is acceptable.

380

CHAPTER 1 0

N E T W O R K I N G AND THE INTERNET

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
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 network devices and describes how to put data into packets, transmit the
data, and check it for errors. Ethernet is found at this layer, as is 802.11
(page 1134) 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 guaranteed delivery, delivery of packets in the order of their transmission, flow
control, error detection, and error correction. The transport layer is responsible for dividing data streams into packets. In addition, this layer performs
port addressing, which allows it to distinguish among different services
using the same transport protocol. Port addressing keeps the data from
multiple applications using the same protocol (for example, TCP) separate.
5. Anything above the transport layer is the domain of the application and is
part of the fifth layer. Unlike the ISO model, the Internet model does not
distinguish among application, presentation, and session layers. All of the
upper-layer characteristics, such as character encoding, encryption, and
GUIs, are part of the application. Applications choose the transport characteristics they require as well as the corresponding transport layer protocol
with which to send and receive data.
TCP: TRANSMISSION CONTROL PROTOCOL
TCP is most frequently run on top of IP in a combination referred to as TCP/IP.
This protocol provides error recovery and guaranteed delivery in packet transmission order; it also works with multiple ports so that it can handle more than one
application. TCP is a connection-oriented protocol (page 1142), 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

TYPES OF N E T W O R K S A N D H o w THEY W O R K

381

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.
U D P : USER DATAGRAM PROTOCOL
UDP runs at layer 4 of the IP stack, just as TCP does, but is much simpler. Like TCP,
UDP works with multiple ports and multiple applications. It has checksums for
error detection but does not automatically retransmit datagrams (page 1144) that
fail the checksum test. UDP is a datagram-oriented protocol: Each datagram must
carry its own address and port information. Each router along the way examines
each datagram to determine the destination, one hop at a time. You can broadcast
or multicast UDP datagrams to many destinations at the same time by using special
addresses.
PPP: POINT-TO-POINT PROTOCOL
PPP provides serial line point-to-point connections that support IP. This protocol
compresses data to make the most of the limited bandwidth available on serial connections. 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 has a unique identifier called a MAC address (page 1158).
A system attached to more than one network has multiple interfaces—one for each
network, each with its own MAC 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.

5. SLIP w a s one o f the first serial line implementations o f IP and has slightly less overhead than PPP. PPP
supports multiple protocols (such as AppleTalk and I P X ) , whereas SLIP supports only IP.
6 . C o n t r a s t b r o a d c a s t p a c k e t s w i t h unicast p a c k e t s : E t h e r n e t h a r d w a r e on a c o m p u t e r filters o u t unic a s t p a c k e t s t h a t are n o t addressed to t h a t m a c h i n e ; the o p e r a t i n g system on t h a t m a c h i n e never sees
these p a c k e t s .

382

CHAPTER 1 0

N E T W O R K I N G AND THE INTERNET

The network address of a machine is an IP address, which, under IPv4, is represented 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 distributed system coordinated by ICANN (Internet Corporation for Assigned Names
and Numbers—www.icann.org) via many registrars (see www.internic.net). ICANN
is funded by the various domain name registries and registrars and by IP address
registries, which supply globally unique identifiers for hosts and services on the
Internet. Although you may not deal with any of these agencies directly, your Internet service provider most assuredly does.
How a company uses IP addresses is determined by the system or network administrator. For example, the leftmost two sets of numbers in an IP address might represent a large network (campuswide or companywide); the third set, a subnetwork
(perhaps a department or a single floor in a building); and the rightmost number, an
individual computer. The operating system uses the address in a different, lower-level
form, converting it to its binary equivalent, a series of Is and Os. See the following
optional section for more information. Refer to "Private address space" on page 642
for information about addresses you can use on a LAN without registering them.
STATIC V E R S U S D Y N A M I C IP A D D R E S S E S
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 maintain
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, establishes a
new connection with a server. The advantage of dynamic addressing is that it allows
inactive addresses to be reused, reducing the total number of IP addresses needed.

optional I P 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 system 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.

TYPES OF N E T W O R K S A N D H o w THEY W O R K

383

Each of the four numbers in the IP address is in the range 0 - 2 5 5 because each segment of the IP address is represented by 8 bits (an octet), with each bit being capable of taking on two values; the total number of values is therefore 2 s = 256. When
you start counting at 0, the range 1-256 becomes 0 - 2 5 5 . 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.
Table 10-2

IP classes

Class

Start bits

Address range

All bits (including start bits)
0-7

Class A

0

Class B

10

Class C

110

192.000.000.000-223.255.255.000 11 n

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-15

001.000.000.000-126.000.000.000 0-netid

16-23

24-31

hostid

129.000.000.000-191.255.000.000 1 0 — - n f îtid

hostid
=hostid=

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 2 24 , or
approximately 16 million, hosts on its network. Unused address space and subnets
(page 1174) lower this number quite a bit. The 127.0.0.0 subnet (page 387) is
reserved, as are several others (see private address space on page 1166).
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

7. Internally, the IP address is represented as a set o f four unsigned 8-bit fields o r a 3 2 - b i t unsigned number, depending on h o w programs are using it. T h e m o s t c o m m o n f o r m a t in C is to represent it as a union
o f an unsigned 3 2 - b i t long integer, four unsigned chars, and t w o unsigned short integers.
8. A 16-bit (class B) address can address 216 = 65,536

hosts, yet the potential number o f hosts is t w o 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 2 5 4 hosts (2 s - 2 = 2 5 4 ) . T h e 0 host address (for example, 1 9 4 . 1 6 . 1 0 0 . 0 for a
class C network or 1 3 1 . 2 0 4 . 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. T h e 2 5 5 host address (for example,
1 9 4 . 1 6 . 1 0 0 . 2 5 5 for a class C network or 1 3 1 . 2 0 4 . 2 5 5 . 2 5 5 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.
T h e netid portion o f a subnet does n o t have the same limitations. Often you are given the choice o f reserving the first and last networks in a range as you would a hostid, but this is rarely done in practice.
M o r e often the first and last n e t w o r k s in the netid range provide more usable address space. Refer t o
" S u b n e t s " on page 3 8 5 .

384

CHAPTER 1 0

NETWORKING AND THE INTERNET

bits ( 1 0 0 ) , 2 1 n e t i d bits (2 m i l l i o n n e t w o r k s ) , a n d 8 h o s t i d bits ( 2 5 4 hosts). T o d a y a
n e w l a r g e c u s t o m e r w i l l n o t r e c e i v e a class A o r B n e t w o r k b u t is l i k e l y t o r e c e i v e a
class C o r several ( u s u a l l y c o n t i g u o u s ) class C n e t w o r k s , i f m e r i t e d .

multicast ( p a g e 1 1 6 1 ) n e t w o r k s . W h e n y o u r u n n e t s t a t - n r o n a L i n u x s y s t e m , y o u c a n
Several o t h e r classes o f n e t w o r k s exist. Class D n e t w o r k s are r e s e r v e d f o r

see w h e t h e r t h e m a c h i n e is a m e m b e r o f a m u l t i c a s t n e t w o r k . A 2 2 4 . 0 . 0 . 0 i n t h e
Destination column that

netstat d i s p l a y s i n d i c a t e s

a class D , m u l t i c a s t

address

( T a b l e 1 0 - 2 ) . A m u l t i c a s t is l i k e a b r o a d c a s t , b u t o n l y h o s t s t h a t s u b s c r i b e t o t h e
m u l t i c a s t g r o u p r e c e i v e t h e m e s s a g e . T o u s e W e b t e r m i n o l o g y , a b r o a d c a s t is l i k e a
" p u s h . " A host pushes a broadcast o n the n e t w o r k , and every host o n the n e t w o r k
m u s t c h e c k e a c h p a c k e t t o see w h e t h e r i t c o n t a i n s r e l e v a n t d a t a . A m u l t i c a s t

is

l i k e a " p u l l . " A h o s t w i l l see a m u l t i c a s t o n l y i f i t r e g i s t e r s i t s e l f as s u b s c r i b e d t o a
multicast g r o u p or service and pulls the a p p r o p r i a t e packets f r o m the n e t w o r k .

Computations for IP address 131.204.027.027

Class B-

netid

hostid

131

.204

.027

83

CC

1B

1000 0011

11001100

0001 1011

255

.255

.255

FF

FF

FF

1111 1111

1111 1111

1111 1111

0000 0000 binary

IP address bitwise AND

1000 0011

11001100

0001 1011

0001 1011

Subnet mask

1111 1111

1111 1111

1111 1111

0000 0000 binary

= Subnet number

1000 0011

11001100

0001 1011

0000 0000

131

.204

.027

83

CC

1B

1000 0011

11001100

0001 1011

131

.204

.27

83

CC

1B

1000 0011

11001100

0001 1011

IP address

Subnet mask

Subnet number

Broadcast address
(set host bits to 1)

.027 decimal
1B hexadecimal
0001 1011 binary
.000 decimal
00 hexadecimal

.000 decimal
00 hexadecimal
0000 0000 binary
.255 decimal
FF hexadecimal
1111 1111 binary

Table 10-3 shows some of the c o m p u t a t i o n s for the IP address

131.204.027.027.

E a c h a d d r e s s is s h o w n i n d e c i m a l , h e x a d e c i m a l , a n d b i n a r y f o r m . B i n a r y is t h e easiest t o w o r k w i t h f o r b i t w i s e ( b i n a r y ) c o m p u t a t i o n s . T h e f i r s t t h r e e l i n e s s h o w t h e I P
address. T h e n e x t three lines s h o w the

subnet mask ( p a g e 1 1 7 5 ) i n t h r e e b a s e s .

N e x t the IP address a n d the subnet m a s k are A N D e d together bitwise to yield the

subnet number

( p a g e 1 1 7 5 ) , w h i c h is s h o w n i n t h r e e bases. T h e l a s t t h r e e

lines

TYPES OF N E T W O R K S A N D H o w THEY W O R K

s h o w the

broadcast address

385

( p a g e 1 1 3 8 ) , w h i c h is c o m p u t e d b y t a k i n g t h e s u b n e t

n u m b e r a n d t u r n i n g the h o s t i d bits to Is. T h e subnet n u m b e r identifies the

local

network. The subnet n u m b e r and the subnet mask determine w h a t range the

IP

address of the m a c h i n e m u s t be in. T h e y are also used b y routers to segment traffic;
see

network segment

(page 1162). A broadcast o n this n e t w o r k goes to all hosts i n

the range 1 3 1 . 2 0 4 . 2 7 . 1 t h r o u g h 1 3 1 . 2 0 4 . 2 7 . 2 5 4 b u t w i l l be acted o n o n l y b y hosts
t h a t h a v e a use f o r it.

SUBNETS
Each host o n a n e t w o r k m u s t process each broadcast packet to determine

whether

t h e i n f o r m a t i o n i n t h e p a c k e t is u s e f u l t o t h a t h o s t . I f t h e n e t w o r k i n c l u d e s n u m e r o u s
hosts, each host m u s t process m a n y packets. T o m a i n t a i n efficiency m o s t n e t w o r k s —
a n d p a r t i c u l a r l y s h a r e d m e d i a n e t w o r k s s u c h as E t h e r n e t — n e e d t o b e s p l i t i n t o s u b n e t w o r k s , o r subnets.9
performance

T h e m o r e hosts o n a n e t w o r k , the m o r e dramatically n e t w o r k

is a f f e c t e d . O r g a n i z a t i o n s

use r o u t e r

and

switch technology

V L A N s (virtual local area n e t w o r k s ) to g r o u p similar hosts into broadcast

called

domains

( s u b n e t s ) b a s e d o n f u n c t i o n . F o r e x a m p l e , i t i s n o t u n c o m m o n t o see a s w i t c h

with

d i f f e r e n t p o r t s b e i n g p a r t o f d i f f e r e n t s u b n e t s . See p a g e 4 6 2 f o r i n f o r m a t i o n o n

how

to specify a subnet.
Subnet mask

A

subnet mask

(or

address mask)

is a b i t m a s k t h a t i d e n t i f i e s w h i c h p a r t s o f a n I P

address correspond to the n e t w o r k address a n d the subnet p o r t i o n of the address.
This m a s k has Is in positions c o r r e s p o n d i n g to the n e t w o r k a n d subnet

numbers

a n d 0s i n t h e h o s t n u m b e r p o s i t i o n s . W h e n y o u p e r f o r m a b i t w i s e A N D

o n an IP

address and a subnet m a s k (Table 10-3), the resulting address contains

everything

except the host address (hostid) portion.
T h e r e are several w a y s to represent a subnet m a s k : A n e t w o r k c o u l d have a subnet
m a s k of 2 5 5 . 2 5 5 . 2 5 5 . 0 (decimal), F F F F F F 0 0 (hexadecimal), or / 2 4 (the n u m b e r

of

bits used f o r t h e s u b n e t m a s k ) . I f it w e r e a class B n e t w o r k ( o f w h i c h 16 bits are
already fixed), this yields 2s (24 total bits works

10

with 2

s

- 2 (256 - 2 = 254) hosts

11

16 fixed bits = 8 bits, 2s = 2 5 6 )

net-

o n each network.

F o r e x a m p l e , w h e n y o u d i v i d e the class C address 1 9 2 . 2 5 . 4 . 0 i n t o e i g h t subnets, y o u
g e t a s u b n e t m a s k o f 2 5 5 . 2 5 5 . 2 5 5 . 2 2 4 , F F F F F F E 0 , o r 127

(27 Is). T h e eight result-

ant n e t w o r k s are 192.25.4.0, 192.25.4.32, 192.25.4.64, 192.25.4.96,
192.25.4.160,
mask

192.25.4.128,

1 9 2 . 2 5 . 4 . 1 9 2 , a n d 1 9 2 . 2 5 . 4 . 2 2 4 . Y o u c a n use a W e b - b a s e d

calculator

to

calculate

subnet masks

(refer to

"Network

subnet

Calculators"

on

p a g e 1 1 0 5 ) . T o use this c a l c u l a t o r t o d e t e r m i n e t h e p r e c e d i n g s u b n e t m a s k , start w i t h
an IP host address of

192.25.4.0.

F o r m o r e i n f o r m a t i o n refer to "Specifying a S u b n e t " o n page 4 6 2 .

9 . Splitting a n e t w o r k is also an issue w i t h o t h e r p r o t o c o l s , p a r t i c u l a r l y A p p l e T a l k .
1 0 . T h e first a n d last n e t w o r k s are reserved in a m a n n e r similar t o the first a n d last hosts, a l t h o u g h the
standard is flexible. Y o u can c o n f i g u r e routers t o r e c l a i m the first a n d last n e t w o r k s in a s u b n e t . D i f f e r e n t
r o u t e r s have different t e c h n i q u e s for r e c l a i m i n g these n e t w o r k s .
1 1 . S u b t r a c t 2 because the first a n d last h o s t addresses o n every n e t w o r k are reserved.

386

CHAPTER 1 0

N E T W O R K I N G AND THE INTERNET

C I D R : CLASSLESS INTER-DO MAIN

ROUTING

C I D R ( p r o n o u n c e d " c i d e r " ) a l l o w s g r o u p s o f addresses t h a t are smaller t h a n a class
C b l o c k to be assigned to a n o r g a n i z a t i o n or ISP a n d then further subdivided

and

parceled out. I n addition, it helps to alleviate the potential p r o b l e m of r o u t i n g tables
o n m a j o r Internet b a c k b o n e a n d peering devices b e c o m i n g t o o large to

manage.

T h e p o o l o f a v a i l a b l e I P v 4 addresses has b e e n d e p l e t e d t o the p o i n t t h a t n o o n e gets
a c l a s s A a d d r e s s a n y m o r e . T h e t r e n d is t o r e c l a i m t h e s e h u g e a d d r e s s b l o c k s , i f p o s sible, a n d recycle t h e m i n t o g r o u p s

of smaller

a d d r e s s e s . A l s o , as m o r e

addresses are assigned, r o u t i n g tables o n the Internet

are filling u p

class

and

C

causing

m e m o r y o v e r f l o w s . T h e s o l u t i o n is t o a g g r e g a t e 1 2 g r o u p s o f a d d r e s s e s i n t o

blocks

a n d allocate t h e m t o ISPs, w h i c h i n t u r n s u b d i v i d e these b l o c k s a n d allocate t h e m t o
t h e i r c u s t o m e r s . T h e address class d e s i g n a t i o n s ( A , B, a n d C ) d e s c r i b e d i n t h e p r e v i o u s s e c t i o n a r e u s e d less o f t e n t o d a y , a l t h o u g h y o u m a y

still encounter

W h e n y o u r e q u e s t a n a d d r e s s b l o c k , y o u r I S P u s u a l l y g i v e s y o u as m a n y

subnets.
addresses

as y o u n e e d — a n d n o m o r e . T h e I S P a g g r e g a t e s s e v e r a l c o n t i g u o u s s m a l l e r
a n d r o u t e s t h e m t o y o u r l o c a t i o n . T h i s a g g r e g a t i o n is C I D R . W i t h o u t
I n t e r n e t as w e k n o w i t w o u l d n o t

blocks

CIDR,

the

function.

For example, y o u m i g h t be allocated the 192.168.5.0/22 IP address block,

which

c o u l d s u p p o r t 2 1 0 h o s t s ( 3 2 - 2 2 = 10). Y o u r I S P w o u l d set its r o u t e r s so t h a t
packets g o i n g to an address i n that b l o c k w o u l d be sent to y o u r n e t w o r k .

any

Internally,

y o u r o w n routers m i g h t further subdivide this b l o c k of 1,024 potential hosts

into

subnets, perhaps into four networks. Four n e t w o r k s require an additional t w o

bits

o f a d d r e s s i n g (22 = 4 ) . Y o u c o u l d t h e r e f o r e set u p y o u r r o u t e r t o s u p p o r t f o u r

net-

w o r k s w i t h this allocation:

and

192.168.5.0/24,

192.168.6.0/24,

192.168.7.0/24,

1 9 2 . 1 6 8 . 8 . 0 / 2 4 . E a c h o f these n e t w o r k s c o u l d t h e n h a v e 2 5 4 hosts. C I D R lets
arbitrarily divide networks and subnetworks into increasingly smaller blocks

you

along

the w a y . E a c h r o u t e r has e n o u g h m e m o r y to k e e p t r a c k o f the addresses it needs t o
direct a n d aggregates the rest.
T h i s s c h e m e uses m e m o r y a n d a d d r e s s space efficiently. F o r e x a m p l e , y o u c o u l d

take

1 9 2 . 1 6 8 . 8 . 0 / 2 4 a n d f u r t h e r divide it i n t o 16 n e t w o r k s w i t h 14 hosts each. T h e 16 networks

require

four

192.168.8.16/28,

more

bits

(24 = 16),

192.168.8.32/28,

and

so

so

you

on,

up

would

have

through

the

192.168.8.240/16, w h i c h w o u l d have the hosts 192.168.8.241 t h r o u g h

192.168.8.0/28,
last

subnet

of

192.168.8.254.

HOSTNAMES
People g e n e r a l l y f i n d it easier t o w o r k

w i t h n a m e s t h a n w i t h n u m b e r s , so

provides several w a y s to associate h o s t n a m e s w i t h IP addresses. T h e oldest

Linux
method

is t o c o n s u l t a l i s t o f n a m e s a n d a d d r e s s e s t h a t a r e s t o r e d i n t h e / e t c / h o s t s f i l e :

1 2 . Aggregate

m e a n s t o j o i n . In C I D R ,

the aggregate

2 0 8 . 1 7 8 . 9 9 . 1 2 4 / 2 3 (the a g g r e g a t i o n o f t w o class C b l o c k s ) .

of 2 0 8 . 1 7 8 . 9 9 . 1 2 4

and 2 0 8 . 1 7 8 . 9 9 . 1 2 5

is

TYPES OF N E T W O R K S A N D H o w THEY W O R K

$ cat /etc/hosts
127.0.0.1
localhost
130.128.52.1 gw-example.example, com
130.128.52.2
bravo.example.com
130.128.52.3
hurrah.example.com
130.128.52.4
kudos.example.com

387

gw-example
bravo
hurrah
kudos

localhost = T h e a d d r e s s 1 2 7 . 0 . 0 . 1 is r e s e r v e d f o r t h e s p e c i a l h o s t n a m e localhost, w h i c h s e r v e s
127.0.0.1

as

a

h o o k for the system's n e t w o r k i n g s o f t w a r e to operate o n the local

machine

w i t h o u t g o i n g o n t o a physical n e t w o r k . T h e names of the other systems are s h o w n
in t w o forms: in a

fully qualified domain name ( F Q D N ) f o r m a t t h a t is u n i q u e o n

t h e I n t e r n e t a n d as a n i c k n a m e t h a t i s l o c a l l y u n i q u e .
NIS A s m o r e h o s t s j o i n e d n e t w o r k s , s t o r i n g t h e s e n a m e - t o - a d d r e s s m a p p i n g s i n a t e x t
f i l e p r o v e d t o b e i n e f f i c i e n t a n d i n c o n v e n i e n t . T h e hosts f i l e g r e w i n c r e a s i n g l y l a r g e r
a n d became impossible to keep up-to-date. T o solve this p r o b l e m L i n u x

supports

N I S ( N e t w o r k I n f o r m a t i o n Service, p a g e 4 0 1 ) , w h i c h w a s d e v e l o p e d f o r use o n S u n
c o m p u t e r s . N I S stores i n f o r m a t i o n i n a database, m a k i n g it easier t o f i n d a specific
a d d r e s s , b u t i t is u s e f u l o n l y f o r h o s t i n f o r m a t i o n w i t h i n a s i n g l e

administrative

d o m a i n . H o s t s o u t s i d e t h e d o m a i n c a n n o t access t h e i n f o r m a t i o n .
DNS T h e s o l u t i o n t o t h i s d i l e m m a i s D N S ( D o m a i n N a m e S e r v i c e , p a g e 3 9 9 ) . D N S e f f e c t i v e l y addresses t h e efficiency a n d u p d a t e issues b y a r r a n g i n g t h e entire
namespace

network

( p a g e 1 1 6 1 ) as a h i e r a r c h y . E a c h d o m a i n i n t h e D N S m a n a g e s i t s

own

namespace (addressing a n d n a m e resolution), a n d each d o m a i n can easily q u e r y f o r
a n y host o r IP address b y f o l l o w i n g the tree u p or d o w n the namespace u n t i l it finds
the appropriate domain. By providing a hierarchical n a m i n g structure, D N S

distrib-

utes n a m e a d m i n i s t r a t i o n across the entire I n t e r n e t .

IPv6
T h e explosive g r o w t h of the Internet has u n c o v e r e d deficiencies i n the design of the
c u r r e n t address p l a n — m o s t n o t a b l y the shortage o f addresses. O v e r the n e x t

few

y e a r s , a r e v i s e d p r o t o c o l , n a m e d I P n g ( I P N e x t G e n e r a t i o n ) , a l s o k n o w n as I P v 6 ( I P
v e r s i o n 6 ) , 1 3 w i l l b e p h a s e d i n . ( I t m a y t a k e l o n g e r — t h e p h a s e - i n is g o i n g

quite

s l o w l y . ) T h i s n e w s c h e m e is d e s i g n e d t o o v e r c o m e t h e m a j o r l i m i t a t i o n s o f t h e c u r r e n t a p p r o a c h a n d c a n b e i m p l e m e n t e d g r a d u a l l y b e c a u s e i t is c o m p a t i b l e w i t h t h e
existing address usage. I P v 6 m a k e s it possible t o assign m a n y m o r e u n i q u e I n t e r n e t
addresses (2128, or 3 4 0

undecillion [ 1 0 3 6 ] ) . I t a l s o s u p p o r t s m o r e a d v a n c e d s e c u r i t y

a n d p e r f o r m a n c e c o n t r o l features:
• I P v 6 e n a b l e s a u t o c o n f i g u r a t i o n . W i t h I P v 4 , a u t o c o n f i g u r a t i o n is a v a i l a b l e
u s i n g o p t i o n a l D H C P ( p a g e 4 7 0 ) . W i t h I P v 6 , a u t o c o n f i g u r a t i o n is m a n d a t o r y , m a k i n g it easy f o r hosts t o c o n f i g u r e t h e i r IP addresses a u t o m a t i c a l l y .

1 3 . I P v 5 r e f e r r e d t o an e x p e r i m e n t a l r e a l - t i m e s t r e a m p r o t o c o l n a m e d S T — t h u s t h e j u m p f r o m I P v 4
to IPv6.

388

CHAPTER 1 0

NETWORKING AND THE INTERNET

• I P v 6 r e s e r v e s 2 4 b i t s i n t h e h e a d e r f o r a d v a n c e d s e r v i c e s , s u c h as r e s o u r c e
reservation protocols, better b a c k b o n e routing, and i m p r o v e d traffic
engineering.
• I P v 6 m a k e s m u l t i c a s t p r o t o c o l s m a n d a t o r y a n d uses t h e m e x t e n s i v e l y . I n
I P v 4 , m u l t i c a s t , w h i c h i m p r o v e s s c a l a b i l i t y , is o p t i o n a l .
• IPv6 aggregates address blocks m o r e efficiently because of the huge
a d d r e s s s p a c e . T h i s a g g r e g a t i o n m a k e s o b s o l e t e NAT

(page 1161), w h i c h

decreased s c a l a b i l i t y a n d i n t r o d u c e d p r o t o c o l issues.
• IPv6 provides a simplified packet header that allows h a r d w a r e accelerators
t o w o r k better.
A s a m p l e I P v 6 a d d r e s s is f e 8 0 : : a 0 0 : 2 0 f f : f e f f : 5 b e 2 / 1 0 . E a c h g r o u p o f f o u r h e x a d e c i m a l d i g i t s is e q u i v a l e n t t o a n u m b e r b e t w e e n 0 a n d 6 5 , 5 3 6 ( 1 6 4 ) . A p a i r o f a d j a c e n t
c o l o n s indicates a h e x v a l u e o f 0 x 0 0 0 0 ; l e a d i n g 0s need n o t be s h o w n . W i t h eight
sets o f h e x a d e c i m a l g r o u p i n g s , 6 5 , 5 3 6 s = 2 1 2 S a d d r e s s e s a r e p o s s i b l e . I n a n
address o n a host w i t h the default autoconfiguration, the first characters i n

IPv6
the

a d d r e s s a r e a l w a y s f e 8 0 . T h e l a s t 6 4 b i t s h o l d a n i n t e r f a c e I D d e s i g n a t i o n , w h i c h is
o f t e n t h e MAC address

(page 1 1 5 8 ) o f the system's Ethernet controller.

C O M M U N I C A T E OVER A N E T W O R K
M a n y c o m m a n d s t h a t y o u c a n use t o c o m m u n i c a t e w i t h o t h e r users o n a single
computer

system

have

been

extended

to

work

over

a network.

Examples

extended utilities include electronic m a i l p r o g r a m s , i n f o r m a t i o n - g a t h e r i n g

of

utilities

( s u c h as f i n g e r , p a g e 1 8 1 ) , a n d c o m m u n i c a t i o n s u t i l i t i e s ( s u c h as t a l k ) . T h e s e u t i l i t i e s
are examples of the U N I X p h i l o s o p h y : Instead of creating a new,

special-purpose

tool, m o d i f y an existing one.
M a n y utilities u n d e r s t a n d a c o n v e n t i o n f o r t h e f o r m a t o f n e t w o r k addresses: user@host
( s p o k e n as " u s e r a t h o s t " ) . W h e n y o u u s e a n @ s i g n i n a n a r g u m e n t t o o n e o f t h e s e u t i l i t i e s , t h e u t i l i t y i n t e r p r e t s t h e t e x t t h a t f o l l o w s as t h e n a m e o f a r e m o t e h o s t . W h e n y o u
o m i t the @ sign, a utility assumes t h a t y o u are requesting i n f o r m a t i o n f r o m or corres p o n d i n g w i t h someone o n the local system.
T h e p r o m p t s s h o w n i n t h e e x a m p l e s i n t h i s c h a p t e r i n c l u d e t h e h o s t n a m e o f t h e syst e m y o u are using. I f y o u f r e q u e n t l y use m o r e t h a n o n e s y s t e m o v e r a n e t w o r k , y o u
m a y f i n d it difficult t o keep t r a c k o f w h i c h system y o u are interacting w i t h at a n y
p a r t i c u l a r m o m e n t . I f y o u set y o u r p r o m p t t o i n c l u d e t h e h o s t n a m e o f t h e c u r r e n t
system, it w i l l a l w a y s be clear w h i c h system y o u are using. T o i d e n t i f y the c o m p u t e r

y o u are u s i n g , r u n h o s t n a m e o r uname - n :
$ hostname
kudos
See p a g e 3 2 1 f o r i n f o r m a t i o n o n h o w y o u c a n c h a n g e t h e p r o m p t .

C O M M U N I C A T E OVER A NETWORK

finger: DISPLAYS INFORMATION A B O U T REMOTE

389

USERS

T h e finger u t i l i t y d i s p l a y s i n f o r m a t i o n a b o u t o n e o r m o r e u s e r s o n a s y s t e m . T h i s
u t i l i t y w a s d e s i g n e d f o r l o c a l use, b u t w h e n n e t w o r k s b e c a m e p o p u l a r , it w a s o b v i o u s t h a t finger s h o u l d b e e n h a n c e d t o r e a c h o u t a n d c o l l e c t i n f o r m a t i o n r e m o t e l y . I n
t h e f o l l o w i n g e x a m p l e s , finger d i s p l a y s i n f o r m a t i o n a b o u t a l l u s e r s l o g g e d i n o n t h e
s y s t e m n a m e d bravo:
[kudos]$ finger ©bravo
[bravo.example.com]
Logi n
Name
s am
Sam the Great
max
Max WiId
max
Max WiId
zach
Zach Brill
his
Helen Simpson
A

Tty
*1
4
5
7
11

Idle
1:35
19
2:24
2d

Login Time
Oct 22
5:00
Oct 22 12:23
Oct 22 12:33
Oct 22 8:45
Oct 20 12:23

Office

Office Phone

(kudos)
(:0)
(:0)
(:0)

u s e r ' s u s e r n a m e i n f r o n t o f t h e C2> s i g n c a u s e s finger to d i s p l a y i n f o r m a t i o n

the r e m o t e system f o r the specified user only. If the r e m o t e system has

from

multiple

m a t c h e s f o r t h a t n a m e , finger d i s p l a y s t h e r e s u l t s f o r a l l o f t h e m :
[kudos]$ finger maxObravo
[bravo.example.com]
Login
Name
max
Max Wild
max
Max Wild

Tty
4
5

Idle
19

Login Time
Office
Oct 22 12:23 (kudos)
Oct 22 12:33 (:0)

Office Phone

T h e finger u t i l i t y w o r k s b y q u e r y i n g a s t a n d a r d n e t w o r k s e r v i c e , t h e in.fingerd d a e m o n , t h a t r u n s o n t h e s y s t e m b e i n g q u e r i e d . A l t h o u g h t h i s s e r v i c e is a v a i l a b l e i n t h e
fingerd p a c k a g e f o r U b u n t u L i n u x , s o m e s i t e s c h o o s e n o t t o r u n i t t o m i n i m i z e t h e
l o a d o n t h e i r s y s t e m s , r e d u c e s e c u r i t y r i s k s , o r m a i n t a i n p r i v a c y . W h e n y o u u s e finger
t o o b t a i n i n f o r m a t i o n a b o u t s o m e o n e a t s u c h a s i t e , y o u w i l l see a n e r r o r m e s s a g e o r
n o t h i n g a t a l l . T h e r e m o t e in.fingerd d a e m o n d e t e r m i n e s h o w m u c h i n f o r m a t i o n t o
share a n d i n w h a t f o r m a t . As a result, the report displayed for a n y given system m a y
differ f r o m that s h o w n in the preceding examples.

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.
T h e i n f o r m a t i o n f o r r e m o t e finger l o o k s m u c h t h e s a m e a s i t d o e s w h e n finger r u n s
on the local

system, w i t h

one difference:

Before

d i s p l a y i n g t h e r e s u l t s , finger

r e p o r t s t h e n a m e o f t h e r e m o t e s y s t e m t h a t a n s w e r e d t h e q u e r y ( b r a v o , as s h o w n
i n brackets i n the preceding example). T h e n a m e of the host that answers m a y be
different f r o m the system n a m e y o u specified o n the c o m m a n d line, depending o n
h o w t h e finger d a e m o n s e r v i c e i s c o n f i g u r e d o n t h e r e m o t e s y s t e m . I n s o m e c a s e s ,
s e v e r a l h o s t n a m e s m a y b e l i s t e d i f o n e finger d a e m o n c o n t a c t s a n o t h e r t o r e t r i e v e
the information.

390

CHAPTER 1 0

N E T W O R K I N G AND THE INTERNET

S E N D I N G M A I L TO A REMOTE USER
G i v e n a user's u s e r n a m e o n a r e m o t e s y s t e m a n d the n a m e o f the r e m o t e s y s t e m o r
its d o m a i n , y o u c a n use a n e m a i l p r o g r a m t o s e n d a m e s s a g e o v e r t h e n e t w o r k

or

the Internet, using the @ f o r m o f an address:
zachObravo

or
zachOexample.com
A l t h o u g h m a n y L i n u x utilities recognize the @ f o r m o f a n e t w o r k address, y o u

may

find that y o u can reach m o r e remote computers w i t h email than w i t h the other netw o r k i n g utilities d e s c r i b e d i n this chapter. T h i s d i s p a r i t y arises because t h e

email

s y s t e m c a n d e l i v e r a m e s s a g e t o a h o s t t h a t d o e s n o t r u n IP, e v e n t h o u g h i t a p p e a r s
to have an Internet

address. T h e

message m a y

be r o u t e d

over the network,

for

e x a m p l e , u n t i l it reaches a r e m o t e system t h a t has a p o i n t - t o - p o i n t , d i a l - u p

connec-

t i o n t o t h e d e s t i n a t i o n s y s t e m . O t h e r u t i l i t i e s , s u c h as talk, r e l y o n I P a n d

operate

only between networked

hosts.

M A I L I N G LIST SERVERS
A m a i l i n g list server (listserv14) a l l o w s y o u to create a n d m a n a g e a n email
A n electronic m a i l i n g list p r o v i d e s a m e a n s for people interested i n a
topic to participate in an electronic discussion and for a person to
information

periodically

to

a potentially

large

mailing

list.

One

list.

particular

disseminate
of the

most

p o w e r f u l f e a t u r e s o f m o s t l i s t s e r v e r s is t h e i r a b i l i t y t o a r c h i v e e m a i l p o s t i n g s

to

the list, create a n archive i n d e x , a n d a l l o w users to retrieve postings f r o m
archive based on keywords

or discussion threads. Typically you can

the

subscribe

a n d u n s u b s c r i b e f r o m the list w i t h or w i t h o u t h u m a n i n t e r v e n t i o n . T h e

owner

of the list c a n restrict w h o c a n subscribe, unsubscribe, a n d post messages to
list. See p a g e 7 3 4 f o r i n s t r u c t i o n s o n c o n f i g u r i n g t h e M a i l m a n l i s t server.
popular
and

list servers i n c l u d e L I S T S E R V

Majordomo

(www.lsoft.com),

fvww.greatcircle.com/majordomo).

Lyris

Ubuntu

\^ww.lyris.com),

maintains

quite

f e w m a i l i n g lists a n d list archives f o r t h o s e m a i l i n g lists at l i s t s . u b u n t u . c o m .
G o o g l e t o s e a r c h o n l i n u x m a i l i n g list t o f i n d o t h e r

the

Other
a

Use

lists.

NETWORK UTILITIES
T o realize t h e f u l l benefits o f a n e t w o r k e d e n v i r o n m e n t , it m a d e sense t o
certain tools, some of w h i c h have already been described. T h e advent of
also created a need for n e w

utilities to c o n t r o l a n d m o n i t o r

extend

networks

them, spurring

development of n e w tools that t o o k advantage of n e t w o r k speed and

the

connectivity.

This section describes concepts a n d utilities for systems attached to a n e t w o r k .

1 4 . A l t h o u g h the term listserv

is s o m e t i m e s used generically t o include m a n y different list server p r o g r a m s ,

it is a specific p r o d u c t a n d a registered t r a d e m a r k o f L - s o f t I n t e r n a t i o n a l , I n c . : L I S T S E R V (for m o r e inform a t i o n go t o w w w . l s o f t . c o m ) .

TRUSTED

NETWORK UTILITIES

391

S o m e c o m m a n d s , s u c h as r e p a n d r s h , w o r k o n l y i f t h e r e m o t e s y s t e m t r u s t s

your

HOSTS
local computer

( t h a t is, i f t h e r e m o t e s y s t e m k n o w s

your

local computer

and

b e l i e v e s t h a t i t is n o t p r e t e n d i n g t o b e a n o t h e r s y s t e m ) . T h e / e t c / h o s t s . e q u i v f i l e lists
t r u s t e d systems. F o r reasons o f security, t h e root a c c o u n t does n o t r e l y o n this file t o
i d e n t i f y t r u s t e d p r i v i l e g e d users f r o m o t h e r systems.
H o s t - b a s e d t r u s t is l a r g e l y o b s o l e t e . B e c a u s e t h e r e a r e m a n y w a y s t o
trusted

host

security,

including

subverting

DNS

systems

and

IP

circumvent

spoofing

( p a g e 1 1 5 4 ) , a u t h e n t i c a t i o n b a s e d o n I P a d d r e s s is w i d e l y r e g a r d e d as i n s e c u r e a n d
obsolete. I n a small homogeneous n e t w o r k of machines w i t h local D N S control, it
c a n b e " g o o d e n o u g h . " Its g r e a t e r ease o f use i n these s i t u a t i o n s m a y o u t w e i g h t h e
security concerns.

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 s s h and s c p instead of rsh and rep
whenever possible.

O P E N S S H TOOLS
T h e O p e n S S H p r o j e c t p r o v i d e s a set o f t o o l s t h a t r e p l a c e rep, rsh, a n d o t h e r s w i t h
secure equivalents. These tools are installed b y d e f a u l t i n U b u n t u L i n u x a n d c a n be
u s e d as d r o p - i n r e p l a c e m e n t s f o r t h e i r i n s e c u r e c o u n t e r p a r t s . T h e O p e n S S H

tool

s u i t e is c o v e r e d i n d e t a i l i n C h a p t e r 1 8 .

t e l n e t : LOGS IN O N A R E M O T E S Y S T E M
Y o u c a n use t h e T E L N E T p r o t o c o l t o i n t e r a c t w i t h a r e m o t e c o m p u t e r . T h e telnet
u t i l i t y , a u s e r i n t e r f a c e t o t h i s p r o t o c o l , is o l d e r t h a n s s h a n d i s n o t s e c u r e . N e v e r t h e l e s s , i t m a y w o r k w h e r e s s h ( p a g e 6 7 0 ) is n o t a v a i l a b l e ( t h e r e is m o r e

non-UNIX

s u p p o r t f o r T E L N E T access t h a n f o r ssh access). I n a d d i t i o n , m a n y l e g a c y d e v i c e s ,
s u c h as t e r m i n a l s e r v e r s a n d n e t w o r k d e v i c e s , d o n o t s u p p o r t s s h .
[bravo]$ telnet kudos
Trying 172 .19. 52 . 2 . . .
Connected to kudos.example.com
Escape character is 'A]'.
Welcome to SuSE Linux 7.3 (i 386) - Kernel 2.4.10-4GB (2).
kudos login: wild
Password:
You have old mail in /var/mai1/wiId.
Last login: Mon Feb 27 14:46:55 from bravo.example.com
wild@kudos:~>
wild@kudos:~> logout
Connection closed by foreign host.
[bravo]$

392

CHAPTER 1 0

N E T W O R K I N G AND THE INTERNET

telnet versus ssh W h e n y o u c o n n e c t t o a r e m o t e U N I X

o r L i n u x s y s t e m u s i n g telnet, y o u a r e p r e -

s e n t e d w i t h a r e g u l a r , t e x t u a l login: p r o m p t . U n l e s s y o u s p e c i f y d i f f e r e n t l y , t h e ssh
utility assumes that y o u r username o n the remote system matches that o n the local
s y s t e m . B e c a u s e telnet i s d e s i g n e d t o w o r k w i t h n o n - U N I X a n d n o n - L i n u x

systems,

it m a k e s n o such assumptions.

telnet is not secure
security

Whenever you enter sensitive information, such as your password, while you are using t e l n e t , it
is transmitted in cleartext and can be read by someone who is listening in on the session.
A n o t h e r d i f f e r e n c e b e t w e e n t h e s e t w o u t i l i t i e s is t h a t telnet a l l o w s y o u t o c o n f i g u r e
m a n y s p e c i a l p a r a m e t e r s , s u c h as h o w RETURNS o r i n t e r r u p t s a r e p r o c e s s e d . W h e n u s i n g
telnet b e t w e e n U N I X a n d / o r L i n u x s y s t e m s , y o u r a r e l y n e e d t o c h a n g e a n y p a r a m e t e r s .
W h e n y o u d o n o t s p e c i f y t h e n a m e o f a r e m o t e h o s t o n t h e c o m m a n d l i n e , telnet
r u n s i n a n i n t e r a c t i v e m o d e . T h e f o l l o w i n g e x a m p l e is e q u i v a l e n t t o t h e p r e v i o u s
telnet e x a m p l e :
[bravo]$ telnet
telnet> open kudos
Trying 172 .19. 52 . 2 . . .
Connected to kudos.example.com
Escape character is 'A]'.
B e f o r e c o n n e c t i n g y o u t o a r e m o t e s y s t e m , telnet t e l l s y o u w h a t t h e escape
i s ; i n m o s t c a s e s , i t is

A

] (where

A

character

r e p r e s e n t s t h e CONTROL k e y ) . W h e n y o u p r e s s CONTROL-],

y o u e s c a p e t o telnet's i n t e r a c t i v e m o d e . C o n t i n u i n g t h e p r e c e d i n g e x a m p l e :
[ kudos ]$ CONTROL-]
telnet> ?

(displays

help

information)

telnet> close
Connection closed,
[bravo]$
W h e n y o u e n t e r a q u e s t i o n m a r k i n r e s p o n s e t o t h e telnet> p r o m p t , telnet l i s t s i t s
c o m m a n d s . T h e close c o m m a n d e n d s t h e c u r r e n t telnet s e s s i o n , r e t u r n i n g y o u t o t h e
l o c a l s y s t e m . T o g e t o u t o f telnet's i n t e r a c t i v e m o d e a n d r e s u m e c o m m u n i c a t i o n w i t h
t h e r e m o t e s y s t e m , p r e s s RETURN i n r e s p o n s e t o a p r o m p t .
Y o u c a n u s e telnet t o a c c e s s s p e c i a l r e m o t e s e r v i c e s a t s i t e s t h a t h a v e c h o s e n t o m a k e
s u c h s e r v i c e s a v a i l a b l e . H o w e v e r , m a n y o f t h e s e s e r v i c e s , s u c h as t h e U . S . L i b r a r y o f
Congress I n f o r m a t i o n System (LOCIS), have m o v e d to the Web. As a consequence,
y o u can n o w obtain the same i n f o r m a t i o n using a W e b browser.

U S I N G telnet TO C O N N E C T TO OTHER

PORTS

B y d e f a u l t telnet c o n n e c t s t o p o r t 2 3 , w h i c h is u s e d f o r r e m o t e l o g i n s . H o w e v e r , y o u
c a n u s e telnet t o c o n n e c t t o o t h e r s e r v i c e s b y s p e c i f y i n g a p o r t n u m b e r . I n a d d i t i o n t o
s t a n d a r d services, m a n y o f t h e special r e m o t e services a v a i l a b l e o n t h e I n t e r n e t use
u n a l l o c a t e d p o r t n u m b e r s . F o r e x a m p l e , y o u c a n access s o m e m u l t i p l a y e r t e x t g a m e s ,

NETWORK UTILITIES

called M U D s

393

( M u l t i - U s e r D u n g e o n s , o r D i m e n s i o n s ) , u s i n g telnet t o c o n n e c t t o a

s p e c i f i e d p o r t , s u c h as 4 0 0 0 o r 8 8 8 8 . U n l i k e t h e p o r t n u m b e r s f o r s t a n d a r d p r o t o c o l s ,
these p o r t n u m b e r s c a n be p i c k e d a r b i t r a r i l y b y the a d m i n i s t r a t o r o f the game.
W h i l e telnet is n o l o n g e r c o m m o n l y e m p l o y e d t o l o g i n o n r e m o t e s y s t e m s , i t i s s t i l l
used extensively

as a d e b u g g i n g t o o l . T h i s

utility allows

y o u to

communicate

d i r e c t l y w i t h a T C P server. S o m e s t a n d a r d p r o t o c o l s are s i m p l e e n o u g h t h a t
experienced user c a n d e b u g p r o b l e m s b y c o n n e c t i n g t o a r e m o t e service

an

directly

u s i n g telnet. I f y o u a r e h a v i n g a p r o b l e m w i t h a n e t w o r k s e r v e r , a g o o d f i r s t s t e p i s
t o t r y t o c o n n e c t t o i t u s i n g telnet.
I n t h e f o l l o w i n g e x a m p l e , a s y s t e m a d m i n i s t r a t o r w h o is d e b u g g i n g a p r o b l e m w i t h
e m a i l d e l i v e r y u s e s telnet t o c o n n e c t t o t h e S M T P p o r t ( p o r t 2 5 ) o n a t h e s e r v e r a t
e x a m p l e . c o m t o see w h y i t is b o u n c i n g m a i l f r o m t h e s p a m m e r . c o m d o m a i n .

The

f i r s t l i n e o f o u t p u t i n d i c a t e s w h i c h I P a d d r e s s telnet i s t r y i n g t o c o n n e c t t o . A f t e r telnet d i s p l a y s t h e C o n n e c t e d t o s m t p s r v . e x a m p l e . c o m m e s s a g e , t h e u s e r e m u l a t e s

an

S M T P dialog, f o l l o w i n g the standard S M T P protocol. T h e first line, w h i c h

starts

w i t h helo, b e g i n s t h e s e s s i o n a n d i d e n t i f i e s t h e l o c a l s y s t e m . A f t e r t h e S M T P

server

r e s p o n d s , t h e u s e r e n t e r s a l i n e t h a t i d e n t i f i e s t h e m a i l s e n d e r as u s e r @ s p a m m e r . c o m .
T h e S M T P server's r e s p o n s e e x p l a i n s w h y t h e m e s s a g e is b o u n c i n g , so t h e u s e r e n d s
t h e s e s s i o n w i t h quit.
$ telnet smtpsrv 25
Trying 192.168.1.1...
Connected to smtpsrv.example.com.
Escape character is 'A]',
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
T h e telnet u t i l i t y a l l o w s y o u t o u s e a n y p r o t o c o l y o u w a n t , as l o n g as y o u k n o w i t
well enough to type commands manually.

f t p : TRANSFERS FILES OVER A N E T W O R K
T h e F i l e T r a n s f e r P r o t o c o l ( F T P ) is a m e t h o d o f d o w n l o a d i n g f i l e s f r o m a n d u p l o a d i n g
f i l e s t o a n o t h e r s y s t e m u s i n g T C P / I P o v e r a n e t w o r k . F T P is n o t a s e c u r e p r o t o c o l ; u s e
i t o n l y f o r d o w n l o a d i n g p u b l i c i n f o r m a t i o n f r o m a p u b l i c server. M o s t W e b b r o w s e r s
c a n d o w n l o a d files f r o m F T P servers. C h a p t e r 1 9 c o v e r s F T P clients a n d servers.

p i n g : TESTS A N E T W O R K C O N N E C T I O N
T h e ping 15 u t i l i t y ( h t t p : / / f t p . a r l . m i l / ~ m i k e / p i n g . h t m l )

sends a n

ECHO_REQUEST

p a c k e t t o a r e m o t e c o m p u t e r . T h i s p a c k e t causes the r e m o t e system t o send b a c k a

1 5 . The name p i n g mimics the sound o f a sonar burst used by submarines to identify and communicate with each
other. The word p i n g also expands to packet internet groper.

394

CHAPTER 1 0

N E T W O R K I N G AND THE INTERNET

r e p l y . T h i s e x c h a n g e is a q u i c k w a y t o v e r i f y t h a t a r e m o t e s y s t e m i s a v a i l a b l e a n d
t o c h e c k h o w w e l l t h e n e t w o r k i s o p e r a t i n g , s u c h as h o w f a s t i t is o r w h e t h e r i t i s
d r o p p i n g d a t a p a c k e t s . T h e ping u t i l i t y u s e s t h e I C M P ( I n t e r n e t C o n t r o l

Message

P r o t o c o l ) p r o t o c o l . W i t h o u t a n y o p t i o n s , ping t e s t s t h e c o n n e c t i o n o n c e p e r s e c o n d
u n t i l y o u a b o r t e x e c u t i o n w i t h C0NTR0L-C.
$ ping www.slashdot.org
PING www.slashdot.org (216.34.181.48) 56(84) bytes of data.
64 bytes from star.slashdot.org (216.34.181.48) icmp_seq=l ttl=2B8
64 bytes from star.slashdot.org (216.34.181.48) icmp_seq=2 ttl=2B8
64 bytes from star.slashdot.org (216.34.181.48) icmp_seq=3 ttl=238
64 bytes from star.slashdot.org (216.34.181.48) icmp_seq=4 ttl=238
CONTROL-C
—
www.slashdot.org ping statistics
—
4 packets transmitted, 4 received, 0% packet loss, time 3024ms
rtt min/avg/max/mdev = 57.553/67.899/72.605/6.039 ms
This

example

shows

that

a connection

to

time=70.2
time=72.6
time=57.5
time=71.2

www.slashdot.org

is

ms
ms
ms
ms

redirected

to

star.slashdot.org a n d t h a t t h a t s y s t e m i s u p a n d a v a i l a b l e o v e r t h e n e t w o r k .
B y d e f a u l t ping s e n d s p a c k e t s c o n t a i n i n g 6 4 b y t e s ( 5 6 d a t a b y t e s a n d 8 b y t e s o f p r o t o c o l header i n f o r m a t i o n ) . I n the p r e c e d i n g e x a m p l e , f o u r packets w e r e sent t o the
s y s t e m star.slashdot.org b e f o r e t h e u s e r i n t e r r u p t e d ping b y p r e s s i n g CONTROL-C. T h e
f o u r - p a r t n u m b e r i n p a r e n t h e s e s o n e a c h l i n e is t h e r e m o t e s y s t e m ' s I P a d d r e s s .

A

p a c k e t s e q u e n c e n u m b e r ( n a m e d icmp_seq) is a l s o g i v e n . I f a p a c k e t i s d r o p p e d , a
g a p o c c u r s i n t h e s e q u e n c e n u m b e r s . T h e r o u n d - t r i p t i m e is l i s t e d l a s t ; i t r e p r e s e n t s
the t i m e (in milliseconds) t h a t elapsed f r o m w h e n the p a c k e t w a s sent f r o m the local
system to the remote system until the reply f r o m the remote system was received by
t h e l o c a l s y s t e m . T h i s t i m e is a f f e c t e d b y t h e d i s t a n c e b e t w e e n t h e t w o s y s t e m s , n e t w o r k t r a f f i c , a n d t h e l o a d o n b o t h c o m p u t e r s . B e f o r e i t t e r m i n a t e s , ping s u m m a r i z e s
t h e r e s u l t s , i n d i c a t i n g h o w m a n y p a c k e t s w e r e s e n t a n d r e c e i v e d as w e l l as t h e m i n i m u m , average, m a x i m u m , and m e a n deviation r o u n d - t r i p times it measured. Use
ping6 t o t e s t I P v 6 n e t w o r k s .

When p i n g cannot connect
tip

If it is unable to contact the remote system, p i n g 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 p i n g s (try p i n g i n g www.microsoft.com or www.ibm.com).

t r a c e r o u t e : TRACES A ROUTE OVER THE INTERNET
T h e traceroute u t i l i t y ( t r a c e r o u t e p a c k a g e ) t r a c e s t h e r o u t e t h a t a n I P p a c k e t f o l l o w s ,
i n c l u d i n g a l l i n t e r m e d i a r y p o i n t s t r a v e r s e d ( c a l l e d network

hops), t o i t s d e s t i n a t i o n ( t h e

a r g u m e n t t o traceroute—an I n t e r n e t host). I t displays a n u m b e r e d list o f h o s t n a m e s , i f
available, a n d IP addresses, t o g e t h e r w i t h t h e r o u n d - t r i p t i m e it t o o k f o r a p a c k e t t o r e a c h

NETWORK UTILITIES

395

each r o u t e r a l o n g t h e w a y a n d a n a c k n o w l e d g m e n t t o get b a c k . Y o u c a n p u t this i n f o r m a t i o n t o g o o d use w h e n y o u are t r y i n g t o i d e n t i f y t h e l o c a t i o n o f a n e t w o r k b o t t l e n e c k .
T h e traceroute u t i l i t y h a s n o c o n c e p t o f t h e p a t h f r o m o n e h o s t t o t h e n e x t ; i n s t e a d ,
i t s i m p l y s e n d s o u t p a c k e t s w i t h i n c r e a s i n g T T L ( t i m e t o l i v e ) v a l u e s . T T L is a n I P
header field t h a t indicates h o w m a n y m o r e h o p s the p a c k e t s h o u l d be a l l o w e d t o
m a k e b e f o r e b e i n g d i s c a r d e d o r r e t u r n e d . I n t h e c a s e o f a traceroute p a c k e t ,

the

p a c k e t is r e t u r n e d b y t h e h o s t t h a t h a s t h e p a c k e t w h e n t h e T T L v a l u e i s z e r o . T h e
r e s u l t is a l i s t o f h o s t s t h a t t h e p a c k e t t r a v e l e d t h r o u g h t o g e t t o i t s d e s t i n a t i o n .
T h e traceroute u t i l i t y c a n h e l p y o u s o l v e r o u t i n g c o n f i g u r a t i o n p r o b l e m s a n d l o c a t e
r o u t i n g p a t h f a i l u r e s . W h e n y o u c a n n o t r e a c h a h o s t , u s e traceroute t o d i s c o v e r w h a t
p a t h t h e p a c k e t f o l l o w s , h o w f a r i t gets, a n d w h a t t h e d e l a y is.
T h e n e x t e x a m p l e s h o w s t h e o u t p u t o f traceroute w h e n i t f o l l o w s a r o u t e f r o m a l o c a l
c o m p u t e r t o w w w . l i n u x . o r g . T h e f i r s t l i n e i n d i c a t e s t h e IP a d d r e s s o f t h e t a r g e t , t h e
m a x i m u m n u m b e r o f h o p s t h a t w i l l b e t r a c e d , a n d t h e size o f t h e p a c k e t s t h a t w i l l b e
u s e d . E a c h n u m b e r e d l i n e c o n t a i n s t h e n a m e a n d IP a d d r e s s o f t h e i n t e r m e d i a t e d e s tination, f o l l o w e d b y the t i m e it takes a packet t o m a k e a trip to that destination a n d
b a c k a g a i n . T h e traceroute u t i l i t y s e n d s t h r e e p a c k e t s t o e a c h d e s t i n a t i o n ; t h u s t h r e e
t i m e s a p p e a r o n e a c h l i n e . L i n e 1 s h o w s t h e s t a t i s t i c s w h e n a p a c k e t is s e n t t o t h e
l o c a l g a t e w a y (less t h a n 3 m i l l i s e c o n d s ) . L i n e s 4 - 6 s h o w t h e p a c k e t b o u n c i n g a r o u n d
M o u n t a i n V i e w ( C a l i f o r n i a ) b e f o r e it goes t o S a n Jose. B e t w e e n h o p s 13 a n d 14 t h e
p a c k e t travels across t h e U n i t e d States (San F r a n c i s c o t o s o m e w h e r e i n t h e East). B y
h o p 1 8 t h e p a c k e t h a s f o u n d w w w . l i n u x . o r g . T h e traceroute u t i l i t y d i s p l a y s a s t e r i s k s
w h e n i t d o e s n o t r e c e i v e a r e s p o n s e . E a c h a s t e r i s k i n d i c a t e s t h a t traceroute h a s w a i t e d
t h r e e s