2817cifc.qxd 11/21/00 1:48 PM Page 1
BECOME A MASTER BY LEARNING
• to tell the differences among many Unix variants • to tell the difference between Free Software and commercial software • the history of Unix, the Free Software Foundation, and other community projects • the basics of Unix, including simple commands and utilities • to create complex commands using Unix shell syntax • to use and configure the Unix graphical user environment • to automate routine tasks using the Bourne Again SHell (BASH) scripting language • to use Unix's built-in utilities to schedule jobs for automatic execution • to create a functional network using Unix's native networking tools • to integrate Windows and Macintosh computers into your Unix network • to understand the basics of network security • to provide service to users and allow them to use your network • to identify, obtain, install, and manage Unix software • to configure and administer various internet servers including WWW, Usenet News, and email • to use dozens of useful Unix terms and commands 2817cfm.qxd 11/20/00 10:03 AM Page i
Mastering Unix This page intentionally left blank 2817cfm.qxd 11/20/00 10:03 AM Page iii
MasteringTM Unix
Kate Wrightson and Joe Merlino
San Francisco • London • Paris • Düsseldorf • Soest 2817cfm.qxd 11/20/00 10:03 AM Page iv
Associate Publisher: Dick Staron Screen reproductions produced with FullShot 99. FullShot 99 © Contracts and Licensing Manager: Kristine O’Callaghan 1991–1999 Inbit Incorporated. All rights reserved. FullShot is a Acquisitions and Developmental Editor: Diane Lowery trademark of Inbit Incorporated. Editor: Ronn Jost Production Editor: Lorrie Fink Netscape Communications Corporation has not authorized, Technical Editor: Patrick Ramseier sponsored, endorsed, or approved this publication and is not Book Designer: Kris Warrenburg responsible for its content. Netscape and the Netscape Communi- Graphic Illustrator: Richard Whitaker/Seventeenth Street Studios cations Corporate Logos are trademarks and trade names of Electronic Publishing Specialist: Seventeenth Street Studios Netscape Communications Corporation. All other product names Proofreader: Kevin Stoffel/Seventeenth Street Studios and/or logos are trademarks of their respective owners. Indexer: Nancy Guenther CD Technician: Keith McNeil TRADEMARKS: SYBEX has attempted throughout this book to CD Coordinator: Kara Eve Schwartz distinguish proprietary trademarks from descriptive terms by fol- Cover Designer: Design Site lowing the capitalization style used by the manufacturer. Cover Illustrator: Jack D. Myers The author and publisher have made their best efforts to prepare Copyright © 2001 SYBEX Inc., 1151 Marina Village Parkway, this book, and the content is based upon final release software Alameda, CA 94501. World rights reserved. No part of this pub- whenever possible. Portions of the manuscript may be based upon lication may be stored in a retrieval system, transmitted, or pre-release versions supplied by software manufacturer(s). The reproduced in any way, including but not limited to photocopy, author and the publisher make no representation or warranties of photograph, magnetic, or other record, without the prior agree- any kind with regard to the completeness or accuracy of the con- ment and written permission of the publisher. tents herein and accept no liability of any kind including but not limited to performance, merchantability, fitness for any particular Library of Congress Card Number: 00-106460 purpose, or any losses or damages of any kind caused or alleged to be caused directly or indirectly from this book. ISBN: 0-7821-2817-3 Manufactured in the United States of America SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc. in the United States and/or other 10 9 8 7 6 5 4 3 2 1 countries.
Mastering is a trademark of SYBEX Inc. 2817cfm.qxd 11/20/00 10:03 AM Page v
Software License Agreement: Terms and Conditions enclosed herein or posted to www.sybex.com. If you discover a defect in the media during this warranty period, you may obtain The media and/or any online materials accompanying this book a replacement of identical format at no charge by sending the that are available now or in the future contain programs and/or defective media, postage prepaid, with proof of purchase to: text files (the “Software”) to be used in connection with the book. SYBEX hereby grants to you a license to use the Software, subject SYBEX Inc. to the terms that follow. Your purchase, acceptance, or use of the Customer Service Department Software will constitute your acceptance of such terms. 1151 Marina Village Parkway Alameda, CA 94501 The Software compilation is the property of SYBEX unless oth- (510) 523-8233 erwise indicated and is protected by copyright to SYBEX or Fax: (510) 523-2373 other copyright owner(s) as indicated in the media files (the e-mail: [email protected] “Owner(s)”). You are hereby granted a single-user license to use WEB: HTTP://WWW.SYBEX.COM the Software for your personal, noncommercial use only. You may not reproduce, sell, distribute, publish, circulate, or com- After the 90-day period, you can obtain replacement media of mercially exploit the Software, or any portion thereof, without identical format by sending us the defective disk, proof of pur- the written consent of SYBEX and the specific copyright chase, and a check or money order for $10, payable to SYBEX. owner(s) of any component software included on this media. Disclaimer In the event that the Software or components include specific license requirements or end-user agreements, statements of con- SYBEX makes no warranty or representation, either expressed or dition, disclaimers, limitations or warranties (“End-User implied, with respect to the Software or its contents, quality, per- License”), those End-User Licenses supersede the terms and con- formance, merchantability, or fitness for a particular purpose. In ditions herein as to that particular Software component. Your no event will SYBEX, its distributors, or dealers be liable to you or purchase, acceptance, or use of the Software will constitute your any other party for direct, indirect, special, incidental, consequen- acceptance of such End-User Licenses. tial, or other damages arising out of the use of or inability to use the Software or its contents even if advised of the possibility of By purchase, use or acceptance of the Software you further agree such damage. In the event that the Software includes an online to comply with all export laws and regulations of the United update feature, SYBEX further disclaims any obligation to provide States as such laws and regulations may exist from time to time. this feature for any specific duration other than the initial posting. The exclusion of implied warranties is not permitted by some states. Therefore, the above exclusion may not apply to you. This Software Support warranty provides you with specific legal rights; there may be other rights that you may have that vary from state to state. The Components of the supplemental Software and any offers associ- pricing of the book with the Software by SYBEX reflects the allo- ated with them may be supported by the specific Owner(s) of that cation of risk and limitations on liability contained in this agree- material but they are not supported by SYBEX. Information regard- ment of Terms and Conditions. ing any available support may be obtained from the Owner(s) using the information provided in the appropriate read.me files or Shareware Distribution listed elsewhere on the media. This Software may contain various programs that are distributed Should the manufacturer(s) or other Owner(s) cease to offer sup- as shareware. Copyright laws apply to both shareware and ordi- port or decline to honor any offer, SYBEX bears no responsibility. nary commercial software, and the copyright Owner(s) retains all This notice concerning support for the Software is provided for rights. If you try a shareware program and continue using it, you your information only. SYBEX is not the agent or principal of the are expected to register it. Individual programs differ on details Owner(s), and SYBEX is in no way responsible for providing any of trial periods, registration, and payment. Please observe the support for the Software, nor is it liable or responsible for any sup- requirements stated in appropriate files. port provided, or not provided, by the Owner(s). Copy Protection Warranty The Software in whole or in part may or may not be copy- SYBEX warrants the enclosed media to be free of physical defects protected or encrypted. However, in all cases, reselling or redis- for a period of ninety (90) days after purchase. The Software is tributing these files without authorization is expressly forbid- not available from SYBEX in any other form or media than that den except as specifically provided for by the Owner(s) therein. This page intentionally left blank 2817cfm.qxd 11/20/00 10:03 AM Page vii
For TW, whose world was a different one but who was proud of us anyway 2817cfm.qxd 11/20/00 10:03 AM Page viii
ACKNOWLEDGMENTS
A large group of people worked to bring this book to you. Although Joe and Kate get their names on the cover, there are many other folks whose hard work and effort made this book possible. We appreciate their diligence and patience with us over a long and bumpy period of time. We’re especially grateful to Sybex and Seventeenth Street Studios for their understanding; during the last part of this book’s writing, Kate’s father died unexpectedly. Our team accommodated our transcontinental travels and made finishing the project as easy as possible under the circumstances. In addition to the people whose names follow, we’d like to thank our agent, David Fugate, and Waterside Productions. David and the Waterside team keep us going and keep us working, and we love being part of their team. Kate also thanks the usual suspects, especially Mason Kramer, who won the lucky random drawing to be named here.
Sybex Sybex people made the project possible and kept it going, from the first contact to the final printing. We’d especially like to thank the following people: • Roger Stewart initially brought us into the project. • Diane Lowery shepherded the first quarter of the book. • Colleen Strand managed administrative details of the full project.
Editing Team We have had good luck in our careers in being able to work with fine editors who understand what we’re trying to say and who catch all the places where we’ve said it poorly. This project was no different, and we’d like to thank the two edi- tors who reviewed each chapter as it came past: • Ronn Jost fixed our prose and ensured that we made sense, with the greatest of good humor and friendly e-mail. 2817cfm.qxd 11/20/00 10:03 AM Page ix
• Patrick Ramseier checked the technical validity of our work and kept us from giving you bad information.
Seventeenth Street Studios The folks at Seventeenth Street Studios did most of the dirty work involved in getting the book organized and arranged into a far more attractive package than the original documents ever promised. In particular, we’d like to thank the fol- lowing people: • Lorrie Fink, our production coordinator, kept track of the schedule and tried valiantly to keep us on target despite our wildly erratic lives over the past year. • Kevin Stoffel proofread everything that went into the book and caught all the errors before they hit the page. • Richard Whitaker and Bob Giles laid out the pages as they appear here, composing them into readable and appealing form; Richard also redrew our extremely amateur line drawings into more professional images. 2817cfm.qxd 11/20/00 10:03 AM Page x
CONTENTS AT A GLANCE
Introduction xxxvii
PART I Introducing Unix 1
Chapter 1 History and Background of Unix 3 Chapter 2 Which Unix? 13 Chapter 3 Some Basic Unix Concepts 29
PART II Getting Started 39
Chapter 4 Logging In and Looking Around 41 Chapter 5 Navigating the Filesystem 55
PART III Unix Desktop Environments 63
Chapter 6 The X Window System: An Overview 65 Chapter 7 Advanced X Techniques 77 Chapter 8 Window Managers 97 Chapter 9 KDE 121 Chapter 10 Gnome 149
PART IV Using the Shell 175
Chapter 11 Introduction to the Bourne (Again) Shell 177 Chapter 12 Manipulating Files and Directories 189 Chapter 13 Customizing the Shell Environment 201 Chapter 14 Input and Output Redirection 215 Chapter 15 Other Shells 227 2817cfm.qxd 11/20/00 10:03 AM Page xi
Contents at a Glance xi
PART V Using Text Editors 241
Chapter 16 The ed Editor 243 Chapter 17 The vi Editor 255 Chapter 18 GNU Emacs 269 Chapter 19 pico, joe, and jed 285 Chapter 20 Graphical Text Editors 297
PART VI Shell Programming 311
Chapter 21 An Introduction to Shell Programming 313 Chapter 22 Variables 321 Chapter 23 Flow Control, Part I: Conditional Flow Control 333 Chapter 24 Flow Control, Part II: Iterative Flow Control 351 Chapter 25 Regular Expressions 369 Chapter 26 Signals and Status 389
PART VII Basic System Administration 407
Chapter 27 What Is System Administration? 409 Chapter 28 System Programming 417 Chapter 29 Managing Users and Groups 439 Chapter 30 Disks and Filesystem Management 457 Chapter 31 Installing and Managing Software 477 Chapter 32 Getting to Know the Kernel 501 Chapter 33 Managing Print Services 517
PART VIII Network Administration 541
Chapter 34 Introduction to Unix Networking 543 2817cfm.qxd 11/20/00 10:03 AM Page xii
xii Contents at a Glance
Chapter 35 Network Interfaces and Routing 563 Chapter 36 The Distributed System 591 Chapter 37 Integrating Unix with Other Platforms 605 Chapter 38 Network Security 631
PART IX Administering Services 655
Chapter 39 Selecting a Suite of Services 657 Chapter 40 Electronic Mail 667 Chapter 41 USENET News 687 Chapter 42 World Wide Web Services 707 Chapter 43 Remote Access (inet) Services 743
Appendices 767
Appendix A Common Unix Commands 767 Appendix B Documentation and Resources 799 Appendix C Other Types of Unix 821 Glossary 832 Index 853 2817cfm.qxd 11/20/00 10:03 AM Page xiii
CONTENTS
Introduction xxxvii
PART I Introducing Unix 1
1 History and Background of Unix 3 What Is Unix? 5 All Versions of Unix Are Multiuser 6 All Versions of Unix Are Multitasking 6 All Versions of Unix Can Use the Same Commands 6 What Does This Mean to the End User? 7 Creation and History of Unix 8 The Story of C 8 The Rise of Unix Derivations 9 The Internet and Unix 9 Unix Today 10 The Unix Philosophy 11 Summary 12 2 Which Unix? 13 The Fragmentation of Unix 14 Differences between Unices 16 Unix Versions Used in This Book 17 Linux 17 FreeBSD 18 Solaris 19 We GNU, Do You? 19 Getting to Know GNU 20 The Free Software Foundation 23 If GNU’s Not Unix, What Is It? 23 The Free Software Explosion 24 The Meteoric Rise of Open Source 24 Summary 26 2817cfm.qxd 11/20/00 10:03 AM Page xiv
xiv Contents
3 Some Basic Unix Concepts 29 Structure of a Unix System 30 The Kernel 30 The Shell 31 Which Shell? 32 The File System 33 Files and Directories 34 Users 34 The Superuser 35 Commands 35 Summary 37
PART II Getting Started 39
4 Logging In and Looking Around 41 Getting Access to Unix 42 Logging In for the First Time 45 Changing Your Password 49 What Are These Files? 50 Logging Out 52 Summary 53 5 Navigating the Filesystem 55 Where Are You? 56 Absolute vs. Relative Path Names 57 Moving Around 57 What’s Where? 58 /bin 59 /etc 59 /home 60 /tmp 60 /usr 60 /usr/local 60 /var 61 Summary 61 2817cfm.qxd 11/20/00 10:03 AM Page xv
Contents xv
PART III Unix Desktop Environments 63
6 The X Window System: An Overview 65 What Is the X Window System? 66 Desktops and Window Managers 67 Window Managers 68 Desktop Environments 69 Which to Use? 70 The Structure of X 71 How Does X Work? 72 Installing and Configuring X 73 Basic X Configuration 74 X Window System Problems 74 Summary 75 7 Advanced X Techniques 77 Using X Applications over a Network 78 The DISPLAY Variable 79 Fonts 81 Installing Fonts 81 Linux 82 FreeBSD 82 Solaris 83 X Font Servers 83 Building a Font Server 84 Using a Font Server 84 Using International Fonts 85 Colors 86 Default Colors 86 Security 90 Using ssh 91 Securing Ports 91 The /etc/X0.hosts File 92 Using the xhost Client 93 The xauth Program 93 X and Users with Disabilities 95 Summary 96 2817cfm.qxd 11/20/00 10:03 AM Page xvi
xvi Contents
8 Window Managers 97 Graphic Interfaces 99 twm 100 Configuring twm 101 IceWM 103 Configuring IceWM 104 BlackBox 107 Configuring BlackBox 107 fvwm 108 Configuring fvwm 109 AfterStep 110 Configuring AfterStep 111 WindowMaker 113 Configuring WindowMaker 114 Enlightenment 116 Configuring Enlightenment 118 Installing Themes 118 Summary 120 9 KDE 121 What Is KDE? 122 Getting and Installing KDE 123 Downloading KDE 124 Base Package Downloads 124 Recommended Package Downloads 125 Unpacking the Source Code 126 Compiling and Installing the Source Code 127 Configuring X for KDE 128 The KDE Panel 129 Virtual Desktops 133 The KDE File Manager 134 The KDE Control Center 137 Desktop 138 Background 138 Borders 138 Display 139 Fonts 139 Desktop Icons 139 2817cfm.qxd 11/20/00 10:03 AM Page xvii
Contents xvii
Language 139 Screensaver 140 Style 140 Information 140 Input Devices 140 International Keyboard 141 Keyboard 141 Mouse 141 Keys 141 Global Keys 142 Standard Keys 142 Network 143 Sound 143 Windows 143 Advanced 143 Buttons 144 Mouse 144 Properties 144 Titlebar 144 Password 144 Date & Time 144 Printers 145 Desktop Themes 145 Summary 147 10 Gnome 149 What Is Gnome? 150 Getting and Installing Gnome 151 Downloading Gnome 152 Base Library Downloads 152 Core Application Downloads 153 Additional Source Downloads 153 Unpacking the Source Code 154 Compiling and Installing the Source Code 155 Configuring X for Gnome 157 Using Gnome 159 The Gnome Panel 159 The Main Menu 160 2817cfm.qxd 11/20/00 10:03 AM Page xviii
xviii Contents
Application Launchers 165 Applets 167 The File Manager 167 The Gnome Control Center 169 Themes and the Desktop 171 Summary 173
PART IV Using the Shell 175
11 Introduction to the Bourne (Again) Shell 177 Why Bourne Shell? 179 Bourne Shell vs. Bourne Again Shell 180 Some Common Shell Commands 181 ls 182 pwd 184 cd 184 mv 185 cp 185 cat 186 more and less 186 echo 187 grep 187 Summary 188 12 Manipulating Files and Directories 189 Creating and Editing Files 190 Copying Files 193 Moving Files 193 File Ownership and Permissions 195 Who Owns the File? 195 Who Can See the File? 196 Deleting Files 197 Managing Directories 198 Creating Directories 198 Directory Ownership and Permissions 199 Deleting Directories 199 Summary 200 2817cfm.qxd 11/20/00 10:03 AM Page xix
Contents xix
13 Customizing the Shell Environment 201 Elements of Shell Configuration 203 Run Control Files 204 .bash_profile 205 Environment Variables 207 Common Environment Variables and What They’re For 209 $USER 209 $MAIL 210 $PS1 210 $HOSTNAME 212 $PATH 212 Summary 213 14 Input and Output Redirection 215 Standard Input and Output 216 Introducing Redirection 217 Redirection Operators 218 Output Redirection Operators 218 Input Redirection Operators 219 Combining Input and Output Redirection Operators 221 Pipes 222 Command Substitution 223 Combining Operators 225 Summary 226 15 Other Shells 227 The Bourne Shell 228 The Korn Shell 229 Korn Shell Run Control Files 230 Environment Variables 231 pdksh 233 The C Shells 233 C Shell Run Control Files 233 Environment Variables 234 What’s the Difference? 236 The Z Shell 237 Other Shells 238 scsh 238 2817cfm.qxd 11/20/00 10:03 AM Page xx
xx Contents
rc and es 238 The Perl Shell 238 Summary 239
PART V Using Text Editors 241
16 The ed Editor 243 What Is ed? 244 Starting ed 245 Reading a File 247 Editing a File 248 Inputting Text 248 Deleting Text 250 Moving Text 250 Joining Lines 250 Saving and Quitting 251 Editing by Content 252 Matching 252 Substitution 253 Summary 254 17 The vi Editor 255 The One True Editor 256 vi’s Modes 257 Basic Editing in Command Mode 258 Moving the Cursor 258 Deleting Text 260 Pattern Matching and Replacing 261 Using the Shell within vi 262 Abbreviations 263 Macros 264 The set Command 265 The .exrc File 266 Saving and Exiting 267 Summary 268 2817cfm.qxd 11/20/00 10:03 AM Page xxi
Contents xxi
18 GNU Emacs 269 What Is GNU Emacs? 270 Running emacs 271 emacs Peculiarities 271 Data Structures 272 Key Bindings 273 Getting Started with emacs 274 Dealing with Buffers 275 Dealing with Windows 276 The GNU Emacs Window 278 The Mode Line 279 The Mini Buffer 279 Getting Help 279 Backups and Auto-Save 280 Killing and Yanking Text 281 Searching and Replacing 281 Saving and Exiting 282 Doctor 282 Summary 283 19 pico, joe, and jed 285 pico 286 Starting pico 287 Editing Text in pico 287 joe 289 jed 293 jed’s Run Control Files 293 Running jed 295 Summary 296 20 Graphical Text Editors 297 Why Graphical Editors? 298 NEdit 299 KEdit 305 The KEdit Toolbar 306 The Menu Bar 306 gEdit 307 Summary 309 2817cfm.qxd 11/20/00 10:03 AM Page xxii
xxii Contents
PART VI Shell Programming 311
21 An Introduction to Shell Programming 313 Why Program the Shell? 314 What Is a Script and What Is a Program? 315 Parts of a Program 316 Statements 317 Operators 317 Regular Expressions 318 Variables 318 Comments 319 Summary 320 22 Variables 321 What Is a Variable? 322 Variable Names 323 Variable Types 324 Arrays 326 Integers 327 The $ Operator 328 Assigning Values to Variables 329 Taking Input from the Keyboard 330 Special Variables 330 Summary 332 23 Flow Control, Part I: Conditional Flow Control 333 The if-then Statement 335 Evaluating Variables 336 Evaluating Non-Variables 338 The test Command 339 Commands 340 Evaluating Multiple Conditions 341 Building an Example 342 Extending the if-then Statement with else 344 The elif Statement 345 The case Statement 347 Taking Arguments from the Command Line 348 Summary 350 2817cfm.qxd 11/20/00 10:03 AM Page xxiii
Contents xxiii
24 Flow Control, Part II: Iterative Flow Control 351 The for Statement 352 Command-Line Processing 353 Building an Example with for 354 The select Statement 356 The while Loop 360 The until Loop 362 Nesting Loops 363 Summary 367 25 Regular Expressions 369 How Regular Expressions Work 371 Using Metacharacters 372 Alternative grep Syntax 373 More about grep 373 sed 375 Writing sed Scripts 376 Comments 376 Line Addresses 377 sed Commands 377 Substitution Commands 377 Deletion Commands 378 Commands for Appending, Inserting, and Changing 378 A sed Script Example 379 Using sed on the Command Line 380 Using sed in Shell Scripts 381 awk 382 awk Metacharacters 386 Printing in awk 387 Summary 387 26 Signals and Status 389 Exit Status 390 Managing Status 392 Reporting Status 392 Accessing Status 393 The Explicit Method 393 The Implicit Method 394 Building an Example 395 2817cfm.qxd 11/20/00 10:03 AM Page xxiv
xxiv Contents
Signals 397 Unique Identifiers 397 Managing Signals 399 Sending Signals 399 Signal Traps 402 Ignoring Signals 404 Summary 405
PART VII Basic System Administration 407
27 What Is System Administration? 409 The Administrator’s Job 410 Administering a Small System 411 Professional System Administration 413 Basic System Administration Tasks 413 System Programming 414 Managing Users 414 Managing Disks and Filesystems 414 Managing Software 415 Managing the Kernel 415 Managing Print Services 416 Summary 416 28 System Programming 417 Automating Common Tasks with Shell Scripts 418 Case Study: A Simple Backup Script 419 Adapting the Script for Multiple Backups 421 Adapting the Script for Future Flexibility 422 Adapting the Script to Include Logs 423 Executing Scripts with cron and at 426 The cron Command 427 crontab Syntax 428 The at Command 428 init Scripts 429 The Initialization Process 431 The System V Initialization Process 432 The BSD Initialization Process 436 Summary 437 2817cfm.qxd 11/20/00 10:03 AM Page xxv
Contents xxv
29 Managing Users and Groups 439 The Root Account 440 Accessing Superuser Powers 442 Superuser Powers with the Root Password 442 Superuser Powers without the Root Password 443 Adding New Users 445 Creating Accounts with Linux and FreeBSD 448 Creating Accounts with Solaris 449 Alternate Password Schemes 450 Removing Users 451 Removing Users with Linux and FreeBSD 452 Removing Users with Solaris 453 Groups 453 Groups with Solaris 455 Adding a Group 455 Modifying a Group 455 Deleting a Group 456 Summary 456 30 Disks and Filesystem Management 457 What Is a Disk? 458 Hard and Floppy Disks 459 Optical Disks 459 Other Types of Disks 460 Disk Partitions 461 How to Create Disk Partitions 462 Disk Partitions under Linux 463 Disk Partitions under FreeBSD 464 Disk Partitions under Solaris 467 Physical Media vs. Filesystems 468 Mounting Local Partitions 470 Automatic Mounting 471 Automatic Mounting under Linux and FreeBSD 471 Automatic Mounting under Solaris 472 Mounting Remote Partitions 473 Mounting Remote Directories under Linux and FreeBSD 474 Mounting Remote Directories under Solaris 474 Summary 475 2817cfm.qxd 11/20/00 10:03 AM Page xxvi
xxvi Contents
31 Installing and Managing Software 477 Software Formats 478 Compiling Software from Source Code 480 Configuring the Package 482 Building the Package 482 Installing the Package 483 Software Management for Unix Variants 484 Linux 485 dpkg: Debian Package Manager 486 dselect: A Graphical dpkg Interface 487 rpm: Red Hat Package Manager 488 FreeBSD 492 Installing a Port 493 Removing a Port 494 Finding Ports 494 Solaris 495 Installing with pkgadd 495 Removing with pkgrm 496 Keeping Up with Upgrades 497 Summary 498 32 Getting to Know the Kernel 501 What the Kernel Does 502 Kernel Development 503 Modules vs. Static Kernels 504 (Re)Compiling the Kernel under Linux and FreeBSD 506 Recompiling a Linux Kernel 507 Recompiling a FreeBSD Kernel 512 Users of a Nonupgraded Version of FreeBSD 513 Users Who Have Upgraded Their Source Tree 515 Summary 515 33 Managing Print Services 517 Unix and Printers 518 BSD Printing: Linux and FreeBSD 520 System V Printing: Solaris 520 Adding Local Printers 521 Adding a Local Printer with FreeBSD and Linux 522 2817cfm.qxd 11/20/00 10:03 AM Page xxvii
Contents xxvii
Adding a Local Printer with Solaris 524 Using admintool 525 Adding Network Printers 525 Adding a Network Printer with FreeBSD 526 Adding a Network Printer with Linux 527 Red Hat’s printtool 528 Adding a Network Printer with Solaris 530 Network Printers with Solaris 2.5 and Earlier 531 Network Printers with Solaris 2.6 and Newer 531 Using admintool 531 Removing a Printer 532 Removing a Printer with BSD 532 Removing a Printer with Linux 532 Removing a Printer with Solaris 533 Maintaining a Print Queue 533 Print Queues with FreeBSD and Linux 534 Print Queues with Solaris 534 Handling PostScript 535 The Common Unix Printing System 538 Summary 540
PART VIII Network Administration 541
34 Introduction to Unix Networking 543 Basic Networking Concepts 545 Basic TCP/IP 547 Internet Protocol 548 Static and Dynamic IP 549 Networking Hardware and Software 550 Common Networking Architectures 552 Local Area Network Topologies 553 Ring Architecture 553 Hub Architecture 554 Bus Architecture 555 Wide Area Network Topologies 557 Common Networking Concerns 559 Summary 561 2817cfm.qxd 11/20/00 10:03 AM Page xxviii
xxviii Contents
35 Network Interfaces and Routing 563 Configuring Network Devices 564 Dial-Up Networking 565 Dial-Up Hardware 567 Case Study: Kppp 568 Writing a Login Script 575 Dial-Up and FreeBSD 575 Dial-Up and Solaris 577 Ethernet Networking 577 DHCP and PPPoE 582 Routers and Gateways 582 Small Networks 584 IP Masquerading 585 IP Masquerading with FreeBSD 586 IP Masquerading with Linux 587 Summary 588 36 The Distributed System 591 Clients and Servers 592 Multiple-Service Machines 594 A Multiple-Service Case Study 594 Distributing Services across Multiple Machines 597 Backing Up Multiple Machines 599 The Security Advantage 601 Reinforcing Multiple-Server Security 602 Summary 603 37 Integrating Unix with Other Platforms 605 Integrating One Unix with Other Unices 607 Obtaining and Installing NFS 608 Configuring an NFS Server 609 Mounting Networked File Systems 610 Automatic Mounting with /etc/fstab 611 Integrating Unix and Windows 613 Obtaining and Installing Samba 613 Configuring Samba 614 2817cfm.qxd 11/20/00 10:03 AM Page xxix
Contents xxix
Starting Samba 616 Print Sharing with Samba 618 Integrating Unix and MacOS 621 Configuring netatalk 622 atalkd.conf 622 apfd.conf 623 AppleVolumes.default 625 AppleVolumes.system 626 config 627 Starting netatalk 628 Solaris and netatalk 628 Linux and netatalk 629 Summary 630 38 Network Security 631 How Important Is Security to You? 633 Eternal Vigilance 634 The Security Mindset 635 Physical Security 636 Internal Security 636 External Security 637 Internal Security 637 File Permissions 638 Passwords 640 Malicious Users 641 External Security 642 Shutting Down All Unnecessary Services 642 Using ssh 644 Keeping Your Software Up-to-Date 645 Intrusion Detection 645 Logs 647 Intrusion Detection Software 648 PortSentry 651 Port Scanners 651 Firewalls and Proxies 652 Proxies 653 Summary 654 2817cfm.qxd 11/20/00 10:03 AM Page xxx
xxx Contents
PART IX Administering Services 655
39 Selecting a Suite of Services 657 What Is a Service? 658 Why Not Run All of Them? 658 What Are Your Needs? 660 The Hobbyist 660 The Worker 660 The Specialist 661 A Word about Security 662 Managing Services 663 Turning Off Services: FreeBSD and Linux 664 Turning Off Services: Solaris 664 Summary 665 40 Electronic Mail 667 How Electronic Mail Works 669 The Software Components 669 The Process 670 An Overview of Mail Services 672 sendmail 674 Installing and Configuring sendmail 675 Postfix 675 Installing and Configuring Postfix 676 Starting Postfix 677 Exim 678 Obtaining and Installing Exim 678 Testing and Monitoring Exim 679 qmail 680 Installing and Configuring qmail 681 smail 682 Setting Up POP and IMAP Services 683 Summary 684 41 USENET News 687 How USENET Works 688 The Process 692 Retaining Posts 695 2817cfm.qxd 11/20/00 10:03 AM Page xxxi
Contents xxxi
Creating Newsgroups 696 Administering a Sound USENET Site 697 Arranging a Newsfeed 698 User Policies 699 INN 700 Obtaining and Installing INN 700 Installing INN on Linux 701 Installing INN on Solaris 701 Installing INN on FreeBSD 701 Configuring and Running INN 702 Managing Newsfeeds 702 Setting Expirations 703 Managing Groups 704 Summary 705 42 World Wide Web Services 707 Getting and Installing Apache 709 Configuring Apache 711 Other Web Servers 735 boa 736 dhttpd 736 fhttpd 738 Jigsaw 738 kHTTPd 739 WN 740 Summary 741 43 Remote Access (inet) Services 743 inetd: The Internet Supervisor 744 Configuring inetd 745 /etc/inetd.conf 746 /etc/services 749 xinetd: An inet Alternative 754 Running Services from inetd 756 Connection-Based Services 757 telnet 757 rlogin 758 rsh 759 2817cfm.qxd 11/20/00 10:03 AM Page xxxii
xxxii Contents
Data Transfer Services 760 Information Services 761 finger 761 netstat 762 Miscellaneous Services 763 Summary 764
Appendices 767
A Common Unix Commands 767 adduser 768 apropos 769 at 769 bash 769 cat 770 cd 770 cfdisk 770 chmod 770 chown 771 cp 772 crontab 772 date 772 dd 773 declare 773 diff 773 du 774 echo 774 exit 774 export 775 exportfs 775 expr 775 fdisk 775 fsck 776 grep 776 groups 777 gzip and gunzip 777 head and tail 777 ifconfig 778 2817cfm.qxd 11/20/00 10:03 AM Page xxxiii
Contents xxxiii
init 778 insmod 778 kill 779 less 779 ln 779 locate 779 logout 780 lpc 780 lpq 780 lpr 781 lprm 781 ls 782 make 782 man 782 mkdir 783 mke2fs 783 more 783 mount 783 mv 784 netstat 784 passwd 784 ping 785 ps 785 pwd 785 read 786 rm 786 rmdir 787 route 787 rsync 788 set 789 setenv 789 sh 789 shutdown 790 sort 790 ssh 790 startx 791 su 791 tar 791 2817cfm.qxd 11/20/00 10:03 AM Page xxxiv
xxxiv Contents
test 792 top 792 touch 793 traceroute 793 ulimit 793 umask 794 umount 794 useradd 795 userdel 795 w 796 wc 796 whereis 796 which 797 B Documentation and Resources 799 Introducing Unix 800 History of Unix 800 Unix Variants 801 Free Software and Open Source 802 Getting Started 803 Unix Desktop Environments 803 X Window System 804 Window Managers 805 KDE 806 Gnome 807 Using the Shell 807 bash 808 Other Shells 808 Using Text Editors 809 ed 809 vi 810 GNU Emacs 810 Other Text Editors 811 Graphical Editors 811 Shell Programming 812 Basic System Administration 813 2817cfm.qxd 11/20/00 10:03 AM Page xxxv
Table of Contents xxxv
Network Administration 814 Heterogeneous Networks 814 Security 815 Administering Services 816 Electronic Mail 816 USENET News 817 World Wide Web 817 Remote Access Services 818 On the CD 819 C Other Types of Unix 821 AIX 822 BSD 824 BSD/OS 824 NetBSD 824 OpenBSD 825 HP-UX 826 IRIX 827 OSF/1 828 SCO Unix 829 SunOS 830 System V 831 Xenix 831 Glossary 832
Index 853 This page intentionally left blank 2817cfm.qxd 11/20/00 10:03 AM Page xxxvii
INTRODUCTION
Call us evangelists, street corner preachers, radical partisans, or just plain junkies: We love Unix and all its derivatives. When we were offered the chance to write this book, we thought it was a tremendous opportunity to showcase this fine operating system and help those new to the OS learn how to use it to its fullest. Unix is a powerful and robust operating system that rarely fails and that offers even the most casual users the ability to manage individual accounts at a level far surpassing the options offered by other operating systems. Recently, we saw a Web banner that proclaimed Unix to be “The Original Alter- native.” This isn’t quite right: Unix is much closer to being the Original Operating System than it is to being an alternative of any sort. In fact, Microsoft Windows and the Macintosh Operating System (MacOS) were initially designed as alterna- tives to Unix, not the other way around! Unfortunately, the marketing and PR for the personal operating systems such as Windows and MacOS has been successful, and now most computer users either don’t know Unix exists or think that they are too stupid to use it because “Unix is for geeks and computer science majors.” That’s where this book comes in. Neither of us were computer science majors (English and economics), and we’ve learned most of what we know about Unix in a hands-on fashion—or, in the traditional manner, by pestering our more knowl- edgeable friends until they taught us what we needed to know. We used books like this one to lay the base for further experimentation, and we still refer to those old friends when we need a jogged memory. If we can do it, you can do it, too. Unix doesn’t have to be expensive; in many cases, you can get free copies of Unix variants from the Internet or pay just the cost of the CD and shipping to get a Unix variant on a disc. Unix doesn’t have to be hard; you don’t start out with programming, and with graphical user interfaces, Unix is eerily similar to those other operating systems you’ve used. Unix doesn’t have to be an either/or choice; we use a variety of operating systems in our daily work and recreation. We hope that after you have read this book, you will feel comfortable and capable when confronted with a Unix machine. Whether or not you delve into the world of administering your own Unix-variant system—and we hope you do—you will have the skills necessary to work with Unix at your school or 2817cfm.qxd 11/20/00 10:03 AM Page xxxviii
xxxviii Introduction
workplace. If you decide to run your own system, you’ll be able to manage your users, your files, and any services you choose to run. Unlike the users of other operating systems, you won’t be shielded from the actual operation of your com- puter; we hope this gives you a sense of power and accomplishment that is hard to equal in the computing world. Learn Unix, and new worlds open to you.
What the Book Contains Because we wrote this book intending it to be useful for a wide variety of readers, there is a lot of information here. Some of it is quite basic, while other chapters con- tain complex and advanced material. However, there should be nothing here that is beyond the average reader’s understanding, even if you never plan to use Unix. The book is divided into nine major parts, with 43 chapters distributed among those parts. We begin with a basic introduction to Unix and its history, move into basic Unix commands and concepts, and then move into discussion of the vari- ous components of a working Unix-based system. You’ll find separate sections devoted to graphical user interfaces, text editors, basic and advanced system administration, networking, and the various services used by networks across the Internet.
TIP You can find a complete listing of the chapters and their contents in the expanded table of contents, which precedes this introduction.
In each chapter, you’ll find real-world examples and sample output from a vari- ety of Unix commands. We show you how things work and give you the tools to try each concept out on your own system. Where pictures would be helpful, we’ve included them; however, because so much of this book is written about text-mode work, we thought you’d prefer to see the output in the main text, instead of in a shot of a terminal window containing some tiny text that’s barely readable.
Whom the Book Is For We’ve written the book so that absolute beginners and longtime system admin- istrators should both find some use in it, though the latter will probably also need more-specific books designed to answer hardcore questions. If you have 2817cfm.qxd 11/20/00 10:03 AM Page xxxix
Introduction xxxix
never seen Unix before, you will learn most of what you need to get going in the early chapters of the book. If you’ve worked with Unix, but don’t quite understand how it does what it does, you’ll find the information you need here as well. If you’re comfortable as a user, but find yourself in possession of a new Unix system (or you’ve just been hired as a system administrator), we show you what to do, too. We do make some assumptions about you, dear readers. We have based the book on the following “typical reader,” so if you don’t actually meet one of our assumptions, you’ll need to make adjustments for your own circumstances. Here are the basic assumptions we’ve made: • We assume that you have access to a computer running some variant of Unix. It can be one of the three Unices covered in the book (Linux, Solaris, or FreeBSD) or another variant, but you should know that we have targeted our discussion of how Unix works to those three Unices. • We assume that, on that computer, you have a valid user account and access to shell functions. If your system administrator has devised a menu system, you should be able to break out of the menu and get to the regular shell prompt; if you use a graphical user interface, you should be able to open a terminal window. • We assume that you do not have any restrictions on what you can do in your user account, within reason. By restrictions, we mean the ability to exe- cute shell commands, write basic shell scripts, change file permissions, and the like. We do not mean file size limitations, connection time restrictions, or other user-policy functions. • We assume that, if you are interested in running your own Unix variant sys- tem, you have obtained a suitable computer and have installed your pre- ferred Unix variant already. Whatever Unix-based operating system you buy should have clear installation directions and, in most cases, an auto- installation program. • We assume that you are interested in maintaining a secure system and that you will take the appropriate precautions to keep your system secure, espe- cially if you have other users or if you connect to the Internet from the system. 2817cfm.qxd 11/20/00 10:03 AM Page xl
xl Introduction
How to Use the Book You can read straight through or find each chapter as you need it, but regardless of how you use the information in the book, you’ll find consistent formatting to help you identify particular kinds of information. We use special conventions to highlight commands you need to type at a shell prompt, commands that are accessed with a mouse, lines of code, and key combinations. We also use margin icons to identify bits of information that might be dire warnings, helpful material beyond what’s in the text, or unique pieces of knowledge that should either give you a chuckle or help you understand more about the Unix world.
Formatting in the Text As you read along, you’ll see that the text on each page is not necessarily all the same. Those different fonts and the layout indicate a particular type of informa- tion. Most frequently, you’ll see text conventions that indicate something you should type, as written, at a shell prompt to perform a particular task. For exam- ple, if we ask you to issue the command that brings up the documentation for the ls command, you’ll see the sentence “Type man ls at the shell prompt.” Note that man ls is in a different typeface, indicating it is the actual command to be typed. The same typeface is used for directory paths, the way in which file loca- tions are noted in Unix. If you’re looking for a log file, we’ll tell you that logs are usually located in /var/log. Individual filenames also use this font. However, if we’re showing you a particularly long command (or if we think the command is important enough not to be buried in a block of text), we’ll use a dif- ferent convention. So, you’ll sometimes see the command set off on its own line, using a monospace font, like this: man ls or cp /var/log/mail maillog This convention is also used for excerpts from files, or for complete files, as well as for shell scripts and other programming examples.
TIP If you see a line that ends in an arrow, that arrow indicates that the line was so long it had to be broken up into two lines. The arrow will look like this: ➥. 2817cfm.qxd 11/20/00 10:03 AM Page xli
Introduction xli
Words in italics are new terms, which are usually defined in the next sentence. You can also find italicized terms in the Glossary. We’ve added other important terms to the Glossary, but it also contains all the important new concepts from the main text of the book. Commands introduced throughout the book are also listed in Appendix A, a Unix command compendium. In that appendix, we’ve shown you the command, its syntax, and some of the most important options or flags it can use. If we describe a process that uses mouse clicks instead of text commands, the command is written using arrows like this: ➣. That is, we might say, “From the menu at the top of the screen, select File ➣ Save.” This convention saves time and space, because we can use the arrow instead of saying, “Select the File menu. In the drop-down list that appears, click the Save option.” Finally, we show you some key combinations in various places throughout the book. A key combination requires that you press two or more keys simultane- ously. Usually, one of those keys is a metakey: Ctrl, Alt, or Esc. When you see the command “Press Ctrl+c” you need to press the Ctrl key and the c key at the same time. Key combinations are usually found in graphical programs, though the Ctrl+c combination will stop any active process in a Unix shell, and the Ctrl+z combination will suspend the active process so that you can perform another action at the prompt without ending the initial process. (Resume the process by typing fg at the prompt.)
Margin Icons You’ll see many special icons in the outer margins of this book’s pages. These icons are used to indicate particular pieces of information we felt were so important they should be flagged. There are three types of icons used in the following chapters:
WARNING A Warning is the most important thing to read on any page where it appears. We use Warnings to flag security risks and tell you about commands or habits that might damage your hardware or cause you to lose files and data. If you read noth- ing else in the book, at least read the Warnings. 2817cfm.qxd 11/20/00 10:03 AM Page xlii
xlii Introduction
NOTE Notes are extra information that didn’t quite fit into the main text. We use Notes to provide commands or concepts that are a bit more advanced, or that point out some feature of Unix history or the Unix community that might be useful to readers.
TIP Tips are shortcuts or handy hints that will speed up some of the work you need to do when you use Unix. We also use Tips to tell you about programs or Web pages that will make your Unix life easier or that will shed some light on a difficult concept. 2817c01.qxd 11/13/00 11:27 AM Page 1
PART I
Introducing Unix