76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page iii
BSD UNIX®
TO O L B OX
1000+ Commands for FreeBSD®, OpenBSD,
and NetBSD®Power Users
Christopher Negus
François Caen
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page ii
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page i
BSD UNIX®
TO O L B OX
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page ii
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page iii
BSD UNIX®
TO O L B OX
1000+ Commands for FreeBSD®, OpenBSD,
and NetBSD®Power Users
Christopher Negus
François Caen
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page iv
BSD UNIX® Toolbox:
1000+ Commands for FreeBSD®, OpenBSD, and NetBSD®
Power Users
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2008 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-37603-4
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
Library of Congress Cataloging-in-Publication Data is available from the publisher.
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under
Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the
Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center,
222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis,
IN 46256, (317) 572-3447, fax (317) 572-4355, or online at />Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties
with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties,
including without limitation warranties of fitness for a particular purpose. No warranty may be created or
extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for
every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal,
accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of
further information does not mean that the author or the publisher endorses the information the organization or
Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites
listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department
within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, and related trade dress are trademarks or registered trademarks of John
Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without
written permission. UNIX is a registered trademark of X/Open Company. FreeBSD is a registered trademark
of FreeBSD Foundation. NetBSD is a registered trademark of NetBSD Foundation. All other trademarks are the
property of their respective owners. Wiley Publishing, Inc. is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be
available in electronic books.
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page v
As always, I dedicate my work on this book to my wife, Sheree.
— Christopher Negus
To my wife, Tonya, for supporting me in all my endeavors.
— François Caen
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page vi
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page vii
About the Authors
Christopher Negus is the author of the best-selling Fedora and Red Hat Linux Bibles,
Linux Toys, Linux Troubleshooting Bible, and Linux Bible 2008 Edition. He is a member of
the Madison Linux Users Group. Before becoming a full-time writer, Chris served for
eight years on development teams for the UNIX operating system at AT&T, where UNIX
was created and developed. He also worked with Novell on Linux development and
Caldera Linux.
François Caen, through his company Turbosphere LLC, hosts and manages business
application infrastructures, with 95 percent running on Linux systems. As an open
source advocate, he has lectured on OSS network management and Internet services
and served as president of the Tacoma Linux Users Group. François is a Red Hat
Certified Engineer (RHCE). In his spare time, he enjoys managing enterprise Cisco
networks.
About the Technical Editor
Thomas Blader first began dabbling in Linus/UNIX in 1993 with Yggdrasil Linux
and BSD. Since 1997, he has worked for the same company as a Solaris/Linux administrator. He has recently become involved with network security. He also does UNIXrelated consulting and software development as well as book writing and editing.
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page viii
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page ix
Credits
Acquisitions Editor
Vice President and Executive Publisher
Jenny Watson
Joseph B. Wikert
Development Editor
Project Coordinator, Cover
William Bridges
Lynsey Osborn
Technical Editor
Compositor
Thomas Blader
Laurie Stewart,
Happenstance Type-O-Rama
Production Editor
Daniel Scribner
Proofreader
Copy Editor
David Parise,
Word One
Michael Koch
Indexer
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and
Executive Group Publisher
Richard Swadley
Melanie Belkin
76034ffirs.qxd:Toolbox
4/2/08
12:50 PM
Page x
Contents at a Glance
Chapter 1: Starting with BSD Systems . . . . . . . . . . . . . . . . . . . .1
Chapter 2: Installing FreeBSD and Adding Software . . . . . . . . . .13
Chapter 3: Using the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . .33
Chapter 4: Working with Files . . . . . . . . . . . . . . . . . . . . . . . . .51
Chapter 5: Manipulating Text . . . . . . . . . . . . . . . . . . . . . . . . . .71
Chapter 6: Playing with Multimedia . . . . . . . . . . . . . . . . . . . . .89
Chapter 7: Administering File Systems . . . . . . . . . . . . . . . . . .103
Chapter 8: Backups and Removable Media . . . . . . . . . . . . . . .127
Chapter 9: Checking and Managing Running Processes . . . . . .143
Chapter 10: Managing the System . . . . . . . . . . . . . . . . . . . . .161
Chapter 11: Managing Network Connections . . . . . . . . . . . . .185
Chapter 12: Accessing Network Resources . . . . . . . . . . . . . . .205
Chapter 13: Doing Remote System Administration . . . . . . . . .225
Chapter 14: Locking Down Security . . . . . . . . . . . . . . . . . . . .243
Appendix A: Using vi or Vim Editors . . . . . . . . . . . . . . . . . . . .263
Appendix B: Shell Special Characters and Variables . . . . . . . .271
Appendix C: Personal Configuration Files . . . . . . . . . . . . . . . .277
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .281
76034ftoc.qxd:Toolbox
4/2/08
1:04 PM
Page xi
Contents
Acknowledgments
Introduction
Chapter 1: Starting with BSD Systems
About FreeBSD, NetBSD, and OpenBSD
Finding BSD Resources
Focusing on BSD Commands
Finding Commands
Command Reference Information in BSD
Summary
Chapter 2: Installing FreeBSD and Adding Software
Before Installing FreeBSD
Installing FreeBSD
Booting the Install Disc
Starting the Install Process
Adding, Deleting, and Managing Software
Finding Software
Installing Software Packages (binary)
Removing Software Packages (binary)
Installing Software Using Ports (source code)
Getting and Installing Applications with Ports
Checking Packages and Ports
Checking Installed Packages Against Ports
Upgrading Ports
Auditing Installed Packages
Cleaning Up the Ports Collection
Summary
Chapter 3: Using the Shell
Terminal Windows and Shell Access
Using Terminal Windows
Using Virtual Terminals
xix
xxi
1
2
4
6
7
8
11
13
13
14
14
15
19
19
21
22
23
25
28
28
28
29
30
31
33
33
33
35
76034ftoc.qxd:Toolbox
4/2/08
1:04 PM
Page xii
Contents
Using the Shell
Using Bash History
Using Command Line Completion
Redirecting stdin and stdout
Using aliases
Tailing Files
Acquiring Super-User Power
36
38
38
41
41
41
Using the su Command
Delegating Power with sudo
42
43
Using Environment Variables
Creating Simple Shell Scripts
44
45
Editing and Running a Script
Adding Content to Your Script
Summary
Chapter 4: Working with Files
Understanding File Types
Using
Using
Using
Using
Using
45
46
50
51
51
Regular Files
Directories
Symbolic and Hard Links
Device Files
Named Pipes and Sockets
51
52
53
54
54
Setting File and Directory Permissions
55
Changing Permissions with chmod
Setting the umask
Changing Ownership
Traversing the File System
Copying Files
Changing File Attributes
Searching for Files
Generating the locate Database
Finding Files with locate
Locating Files with find
Using Other Commands to Find Files
Finding Out More About Files
Listing Files
Verifying Files
Summary
xii
36
56
58
58
59
60
62
63
63
64
65
67
67
67
68
69
76034ftoc.qxd:Toolbox
4/2/08
1:04 PM
Page xiii
Contents
Chapter 5: Manipulating Text
Matching Text with Regular Expressions
Editing Text Files
Using the JOE Editor
Using the Pico and Nano Editors
Graphical Text Editors
Listing, Sorting, and Changing Text
Listing Text Files
Paging Through Text
Paginating Text Files with pr
Searching for Text with grep
Replacing Text with sed
Translate or Remove Characters with tr
Checking Differences Between Two Files with diff
Using awk and cut to Process Columns
Converting Text Files to Different Formats
Summary
Chapter 6: Playing with Multimedia
Working with Audio
Starting with Audio
Playing Music
Adjusting Audio Levels
Ripping CD Music
Encoding Music
Converting Audio Files
Transforming Images
Getting Information about Images
Converting Images
Converting Images in Batches
Summary
Chapter 7: Administering File Systems
71
71
72
73
76
78
78
78
79
80
81
83
84
85
87
88
88
89
89
89
90
91
92
93
96
97
97
98
100
101
103
Understanding File System Basics
103
Setting Up the Disk Initially
Checking Your Disk Setup
Understanding File System Types
104
104
106
xiii
76034ftoc.qxd:Toolbox
4/2/08
1:04 PM
Page xiv
Contents
Creating and Managing File Systems
Slicing and Partitioning Hard Disks
Working with Linux-Compatible File Systems
Creating a Memory Disk File System
Creating and Using Swap Partitions
Mounting and Unmounting File Systems
Mounting File Systems from the fstab File
Mounting File Systems with the mount Command
Unmounting File Systems with umount
Checking File Systems
Finding Out About File System Use
Summary
107
107
114
116
117
118
118
120
121
122
124
126
Chapter 8: Backups and Removable Media
127
Backing Up Data to Compressed Archives
127
Creating Backup Archives with tar
Using Compression Tools
Listing, Joining, and Adding Files to tar Archives
Backing Up Over Networks
Backing Up tar Archives Over ssh
Backing Up Files with rsync
Backing Up with unison
Backing Up to Removable Media
Creating Backup Images with mkisofs
Burning Backup Images with cdrecord
Making and Burning DVDs with growisofs
Summary
Chapter 9: Checking and Managing Running Processes
Listing Active Processes
127
129
132
133
133
134
136
137
137
140
141
142
143
144
Viewing Active Processes with ps
Watching Active Processes with top
144
150
Finding and Controlling Processes
151
Using pgrep to Find Processes
Using fuser to Find Processes
Summary
xiv
152
152
159
76034ftoc.qxd:Toolbox
4/2/08
1:04 PM
Page xv
Contents
Chapter 10: Managing the System
Monitoring Resources
Monitoring Memory Use
Monitoring CPU Usage
Monitoring Storage Devices
Mastering Time
Changing Time Zone
Displaying and Setting Your Time and Date
Using Network Time Protocol to Set Date/Time
Checking Uptime
Managing the Boot Process
Using the boot0 Boot Loader
Using bsdlabel to Check Out Partitions
Changing to the GRUB boot loader
Controlling System Services
Starting and Stopping Your System
Straight to the Kernel
Poking at the Hardware
Summary
Chapter 11: Managing Network Connections
Configuring Network Interfaces Using sysinstall
Managing Network Interface Cards
Managing Network Connections
Starting and Stopping Ethernet Connections
Starting and Stopping Network Services
Using Wireless Connections
Getting Wireless Driver
Configuring Wireless Interfaces
Checking Name Resolution
Troubleshooting Network Problems
Checking Connectivity to a Host
Checking Address Resolution Protocol (ARP)
Tracing Routes to Hosts
Displaying netstat Connections and Statistics
Other Useful Network Tools
Summary
161
161
162
164
166
168
169
169
170
172
172
173
176
176
178
179
179
182
183
185
185
186
190
190
191
192
192
193
194
196
196
197
198
200
202
203
xv
76034ftoc.qxd:Toolbox
4/2/08
1:04 PM
Page xvi
Contents
Chapter 12: Accessing Network Resources
205
Running Commands to Browse the Web
Transferring Files
205
207
Downloading Files with wget
Transferring Files with cURL
Transferring Files with FTP Commands
Using SSH Tools to Transfer Files
Using Windows File Transfer Tools
207
208
209
211
213
Sharing Remote Directories
Sharing Remote Directories with NFS
Sharing Remote Directories with Samba
Sharing Remote Directories with SSHFS
Chatting with Friends in IRC
Using Text-Based e-mail Clients
Managing e-mail with mail
Managing e-mail with mutt
Summary
Chapter 13: Doing Remote System Administration
Doing Remote Login and Tunneling with SSH
Configuring SSH
Logging in Remotely with ssh
Using screen: A Rich Remote Shell
Using a Remote Windows Desktop
213
213
215
218
219
220
221
222
223
225
225
227
228
233
236
Connecting to a Windows Desktop with tsclient
Connecting to a Windows Desktop with rdesktop
236
237
Using Remote BSD Desktop and Applications
Sharing Desktops Using VNC
238
239
Setting Up the VNC Server
Starting Up the VNC Client
Using VNC on Untrusted Networks with SSH
Sharing a VNC Desktop with Vino
Summary
Chapter 14: Locking Down Security
239
240
241
241
242
243
Working with Users and Groups
244
Managing Users the GUI Way
Adding User Accounts
244
244
xvi
76034ftoc.qxd:Toolbox
4/2/08
1:04 PM
Page xvii
Contents
Adding Batches of Users
Setting User Account Defaults
Using Options When Adding Users
Using Login Classes
Modifying User Accounts
Deleting User Accounts
Managing Passwords
Adding Groups
245
247
248
249
250
251
252
252
Checking on Users
Securing Network Services
Configuring the Built-In Firewall
Working with System Logs
Using Advanced Security Features
Summary
253
255
258
261
261
262
Appendix A: Using vi or Vim Editors
263
Starting and Quitting the vi Editor
Moving Around in vi
Changing and Deleting Text in vi
Using Miscellaneous Commands
Modifying Commands with Numbers
Using Ex Commands
Working in Visual Mode
Summary
264
265
266
268
268
269
270
270
Appendix B: Shell Special Characters and Variables
Using Special Shell Characters
Using Shell Variables
Appendix C: Personal Configuration Files
Index
271
271
272
277
281
xvii
76034flast.qxd:Toolbox
4/2/08
1:06 PM
Page xviii
76034flast.qxd:Toolbox
4/2/08
1:06 PM
Page xix
Acknowledgments
I would like to acknowledge the FreeBSD, OpenBSD, and NetBSD development communities, who have continued the noble tradition begun by the Berkeley Software
Distribution decades ago. Their efforts have led to some of the most secure and stable
computer operating systems in or out of the free and open source software world.
Special thanks to François Caen for giving up most of his free time over the past year
as we developed and wrote the books in our Toolbox series. Thomas Blader did his
usual excellent job tech editing this book. At Wiley, I’d like to thank Jenny Watson for
sticking with us through the development of the book. Special thanks to Bill Bridges,
who kept us on track during a challenging development schedule.
— Christopher Negus
I would like to thank Chris Negus for giving me the opportunity to co-author this book
with him. We had wanted to write together for the last couple of years, and this Toolbox
series was the perfect fit for our collaboration.
I couldn’t have worked on this book without the unrelenting support from my wife,
Tonya. Thank you for emptying the dishwasher all those times even though we both
know it’s my job.
Thanks to Thomas Blader for his detailed tech editing. Having done some tech editing in
the past, I know what a tough job it can be. Thanks to Sara Shlaer and Jenny Watson at
Wiley for being the most patient cat-herders out there. Special thanks to Wayne Tucker
and Jesse Keating for all the knowledge they’ve shared with me during and before this
project.
— François Caen
76034flast.qxd:Toolbox
4/2/08
1:06 PM
Page xx
76034flast.qxd:Toolbox
4/2/08
1:06 PM
Page xxi
Introduction
BSD UNIX Toolbox provides you with more than 1,000 specific command lines to help
you become a BSD power user. Whether you are a systems administrator or desktop
user, the book will show you commands to create file systems, troubleshoot networks,
lock down security, and dig out almost anything you care to know about your BSD
system.
This book’s focus for your BSD command line journey is FreeBSD, one of the most
popular BSD derivatives in the world. Tapping into the skills needed to run those
systems can help you to work with your own BSD systems and to learn what you
need as a BSD professional.
Who Should Read This Book
This book is for anyone who wants to access the power of a BSD system as a systems
administrator or user. You may be a free and open source software (FOSS) enthusiast,
a BSD professional, or possibly a computer professional who is increasingly finding
the Windows systems in your data center supplanted by BSD and Linux boxes.
The bottom line is that you want to find quick and efficient ways of getting FreeBSD,
NetBSD, and OpenBSD systems working at peak performance. Those systems may be
a few desktop systems at work, a file and print server at your school, or a home web
server that you’re doing just for fun.
In the best case, you should already have some experience with BSD, Linux or other
UNIX-like systems. However, if you are a computer professional with skills managing other types of operating systems, such as Windows, you should be able to easily
adapt your knowledge to be able to use the specific commands we cover in the book.
What This Book Covers
This is not a beginner’s BSD UNIX book. Before you jump in, it would be best if you
have a basic working knowledge of what BSD and other UNIX systems are, how the
shell works, and what processes, file systems, and network interfaces are. The book
will then supplement that knowledge with information you need to do the following
activities:
❑ Get software — FreeBSD offers both binary software packages and source packages
in the ports database that can be used to download, build, and install software from
76034flast.qxd:Toolbox
4/2/08
1:06 PM
Page xxii
Introduction
source code. With tools such as pkg_info, pkg_add, and pkg_delete, you’ll learn
the best ways to find, download, install, and otherwise manage software from the
command line.
❑ Access applications — Find what’s available from the FreeBSD distribution, then
select and install the ones you want using the sysinstall utility.
❑ Use the shell — Find neat techniques and tips for using the shell.
❑ Play with multimedia — Play and work with multimedia content from your computer. You can also modify audio and image files, and then convert the content of
those files to different formats.
❑ Work with files — Use, manipulate, convert, and secure a wide range of file types
in BSD systems.
❑ Administer file systems — Access, format, partition, and monitor your file storage hardware (hard disks, CD/DVD drives, floppy disks, USB flash drives, and
so on). Then create, format, and check the file systems that exist on those hardware devices.
❑ Back up and restore data — Use simple commands to gather, archive, and compress your files into efficient backup archives. Then store those archives locally
or on remote computers.
❑ Work with processes — List running processes in a variety of ways, such as by
CPU use, processor use, or process ID. Then change running processes to have
them run in the background or foreground. Send signals to processes to have
them re-read configuration files, stop and resume processing, or stop completely
(abort).
❑ Manage the system — Run commands to check system resources, such as memory
usage, boot loaders, and kernel modules.
❑ Monitor networks — Bring wired, wireless, and dial-up network connections up
and down. Check routing, DNS, and host information. Keep an eye on network
traffic.
❑ Get network resources — Connect to BSD and Windows remote file systems using
FTP, NFS, and Samba facilities. Use shell-based commands to browse the Web.
❑ Do remote administration — Access and administer other computers using remote
login (ssh, telnet, and so on), and screen. Learn about remote administration interfaces, such as SWAT and CUPS.
❑ Lock down security — Set up firewalls and system logging to secure your BSD
systems.
❑ Get reference information — Use the appendixes at the end of this book to get
more information about the shell (such as metacharacters and shell variables) and
personal configuration files.
xxii