1000+ Commands for FreeBSD®, OpenBSD and NetBSD®Power Users
Christopher Negus & François Caen
Starting with BSD Systems
Installing FreeBSD and Adding Software
Using the Shell
Working with Files
Manipulating Text
Playing with Multimedia
Administering File Systems
Backups and Removable Media
Checking and Managing Running Processes
Managing the System
Managing Network Connections
Accessing Network Resources
Doing Remote System Administration
Locking Down Security
Installing FreeBSD and Adding Software
Using the Shell
Working with Files
Manipulating Text
Playing with Multimedia
Administering File Systems
Backups and Removable Media
Checking and Managing Running Processes
Managing the System
Managing Network Connections
Accessing Network Resources
Doing Remote System Administration
Locking Down Security
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.
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:
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
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.
Hopefully, if we have done it right, it will be easier to use this book than to Google for
the command lines or GUI tools you need.
After you have mastered many of the features described in this book, you’ll have gained
the following advantages:
❑ Hundreds of commands — By compressing a lot of information into a small space,
you will have access to hundreds of useful commands, in over 1,000 command lines,
in a handy form to carry with you.
❑ Critical BSD information — This book lists connections to the most critical information
on the Web for succeeding with BSD systems in general and FreeBSD in particular.
❑ Transferable knowledge — Most of the same commands and options you use in
BSD systems will work exactly the same way on other UNIX-like systems. Different
UNIX systems, on the other hand, offer different graphical administration tools.
And even within a particular distribution, graphical tools change more often than commands do.
❑ Quick problem solving — By the time others have started up a desktop and
launched a graphical administration tool, you will have already run a half dozen
commands and solved the problem.
❑ Enduring value — Many of the commands described in this book were used in
early UNIX systems. So you are gaining tools that reflect the experience of thousands
of computer experts for more than 30 years.
Because the full documentation for commands used in BSD systems consists of thousands
of man pages, info text, and help messages, you will surely want to reach
beyond the pages of this book from time to time. Luckily, FreeBSD and other UNIX
systems include helpful information installed on the system itself. Chapter 1 contains
descriptions of how to access that information that is probably already installed, or
can be easily installed, on your BSD system.
How This Book Is Structured
This book is neither a pure reference book (with alphabetically listed components) nor a
guide (with step-by-step procedures for doing tasks). Instead, the book is organized by
topics and aimed at including as many useful commands and options as we could fit in.
Chapter 1 starts by giving you a basic understanding of what BSD is and how it
relates to the operating systems that are derived from BSD, such as FreeBSD, NetBSD,
and OpenBSD. Then it describes some of the vast resources available to support your
experience with this book (such as man pages, info material, and help text). Chapter 2
provides a quick overview of installation and then describes useful commands such
as pkg_info and pkg_add for getting and managing your BSD software.
Chapters 3, 4, 5, and 6 describe commands that a regular user may find useful on BSD
systems. Chapter 3 describes tools for using the shell, Chapter 4 covers commands for
working with files, and Chapter 5 describes how to manipulate text. Chapter 6 tells
how to work with music and image files.
Starting with Chapter 7, we get into topics relating to system administration. Creating
and checking file systems is covered in Chapter 7, while commands for doing data
backups are described in Chapter 8. Chapter 9 describes how to manipulate running
processes, and Chapter 10 describes administrative tools for managing basic components,
such as hardware modules, CPU use, and memory use.
Chapter 11 begins the chapters devoted to managing network resources by describing
how to set up and work with wired, wireless, and dial-up network interfaces.
Chapter 12 covers text-based commands for web browsing, file transfer, file sharing,
chats, and e-mail. Tools for doing remote system administration are included in Chapter 13.
Chapter 14 covers how to lock down security using features such as firewalls and
logging. After that there are three appendices that provide reference information for
text editing, shell features (metacharacters and variables), and personal configuration files.
What You Need to Use This Book
Although we hope you enjoy the beauty of our prose, this is not meant to be a book
you curl up with in front of a nice fire with a glass of wine. We expect you will be sitting
in front of a computer screen trying to connect to a network, fix a file system, or
add a user. The wine is optional.
In other words, the book is meant to be a companion as you work on a FreeBSD,
NetBSD, or OpenBSD operating system. All those systems are available for the x86
and x86_64 computer architectures. Some specific versions of those systems are also
available for IBM PowerPC, SPARC, Intel ia64 (Itanium), and Alpha. If you don’t
already have one of those systems installed, refer to Chapter 2 for information on
getting and installing those systems.
All the commands in this book have been tested against FreeBSD on x86 or x86_64
architecture. However, because many of these commands have been around for a
long time (some dating back over 30 years to the original UNIX days), most will
work exactly as described here on NetBSD, OpenBSD, and other derivative systems,
regardless of CPU architecture.
Many of the commands described in this book will work on other UNIX and Linux
systems as well. Because this book focuses on FreeBSD and other BSD-based distributions,
descriptions will differ from other UNIX-like systems most prominently in the
areas of packaging, installation, and GUI administration tools.
Table of Contents
Acknowledgments xix
Introduction xxi
Chapter 1: Starting with BSD Systems 1
About FreeBSD, NetBSD, and OpenBSD 2
Finding BSD Resources 4
Focusing on BSD Commands 6
Finding Commands 7
Command Reference Information in BSD 8
Summary 11
Chapter 2: Installing FreeBSD and Adding Software 13
Before Installing FreeBSD 13
Installing FreeBSD 14
Booting the Install Disc 14
Starting the Install Process 15
Adding, Deleting, and Managing Software 19
Finding Software 19
Installing Software Packages (binary) 21
Removing Software Packages (binary) 22
Installing Software Using Ports (source code) 23
Getting and Installing Applications with Ports 25
Checking Packages and Ports 28
Checking Installed Packages Against Ports 28
Upgrading Ports 28
Auditing Installed Packages 29
Cleaning Up the Ports Collection 30
Summary 31
Chapter 3: Using the Shell 33
Terminal Windows and Shell Access 33
Using Terminal Windows 33
Using Virtual Terminals 35
Using the Shell 36
Using Bash History 36
Using Command Line Completion 38
Redirecting stdin and stdout 38
Using aliases 41
Tailing Files 41
Acquiring Super-User Power 41
Using the su Command 42
Delegating Power with sudo 43
Using Environment Variables 44
Creating Simple Shell Scripts 45
Editing and Running a Script 45
Adding Content to Your Script 46
Summary 50
Chapter 4: Working with Files 51
Understanding File Types 51
Using Regular Files 51
Using Directories 52
Using Symbolic and Hard Links 53
Using Device Files 54
Using Named Pipes and Sockets 54
Setting File and Directory Permissions 55
Changing Permissions with chmod 56
Setting the umask 58
Changing Ownership 58
Traversing the File System 59
Copying Files 60
Changing File Attributes 62
Searching for Files 63
Generating the locate Database 63
Finding Files with locate 64
Locating Files with find 65
Using Other Commands to Find Files 67
Finding Out More About Files 67
Listing Files 67
Verifying Files 68
Summary 69
Chapter 5: Manipulating Text 71
Matching Text with Regular Expressions 71
Editing Text Files 72
Using the JOE Editor 73
Using the Pico and Nano Editors 76
Graphical Text Editors 78
Listing, Sorting, and Changing Text 78
Listing Text Files 78
Paging Through Text 79
Paginating Text Files with pr 80
Searching for Text with grep 81
Replacing Text with sed 83
Translate or Remove Characters with tr 84
Checking Differences Between Two Files with diff 85
Using awk and cut to Process Columns 87
Converting Text Files to Different Formats 88
Summary 88
Chapter 6: Playing with Multimedia 89
Working with Audio 89
Starting with Audio 89
Playing Music 90
Adjusting Audio Levels 91
Ripping CD Music 92
Encoding Music 93
Converting Audio Files 96
Transforming Images 97
Getting Information about Images 97
Converting Images 98
Converting Images in Batches 100
Summary 101
Chapter 7: Administering File Systems 103
Understanding File System Basics 103
Setting Up the Disk Initially 104
Checking Your Disk Setup 104
Understanding File System Types 106
Creating and Managing File Systems 107
Slicing and Partitioning Hard Disks 107
Working with Linux-Compatible File Systems 114
Creating a Memory Disk File System 116
Creating and Using Swap Partitions 117
Mounting and Unmounting File Systems 118
Mounting File Systems from the fstab File 118
Mounting File Systems with the mount Command 120
Unmounting File Systems with umount 121
Checking File Systems 122
Finding Out About File System Use 124
Summary 126
Chapter 8: Backups and Removable Media 127
Backing Up Data to Compressed Archives 127
Creating Backup Archives with tar 127
Using Compression Tools 129
Listing, Joining, and Adding Files to tar Archives 132
Backing Up Over Networks 133
Backing Up tar Archives Over ssh 133
Backing Up Files with rsync 134
Backing Up with unison 136
Backing Up to Removable Media 137
Creating Backup Images with mkisofs 137
Burning Backup Images with cdrecord 140
Making and Burning DVDs with growisofs 141
Summary 142
Chapter 9: Checking and Managing Running Processes 143
Listing Active Processes 144
Viewing Active Processes with ps 144
Watching Active Processes with top 150
Finding and Controlling Processes 151
Using pgrep to Find Processes 152
Using fuser to Find Processes 152
Summary 159
Chapter 10: Managing the System 161
Monitoring Resources 161
Monitoring Memory Use 162
Monitoring CPU Usage 164
Monitoring Storage Devices 166
Mastering Time 168
Changing Time Zone 169
Displaying and Setting Your Time and Date 169
Using Network Time Protocol to Set Date/Time 170
Checking Uptime 172
Managing the Boot Process 172
Using the boot0 Boot Loader 173
Using bsdlabel to Check Out Partitions 176
Changing to the GRUB boot loader 176
Controlling System Services 178
Starting and Stopping Your System 179
Straight to the Kernel 179
Poking at the Hardware 182
Summary 183
Chapter 11: Managing Network Connections 185
Configuring Network Interfaces Using sysinstall 185
Managing Network Interface Cards 186
Managing Network Connections 190
Starting and Stopping Ethernet Connections 190
Starting and Stopping Network Services 191
Using Wireless Connections 192
Getting Wireless Driver 192
Configuring Wireless Interfaces 193
Checking Name Resolution 194
Troubleshooting Network Problems 196
Checking Connectivity to a Host 196
Checking Address Resolution Protocol (ARP) 197
Tracing Routes to Hosts 198
Displaying netstat Connections and Statistics 200
Other Useful Network Tools 202
Summary 203
Chapter 12: Accessing Network Resources 205
Running Commands to Browse the Web 205
Transferring Files 207
Downloading Files with wget 207
Transferring Files with cURL 208
Transferring Files with FTP Commands 209
Using SSH Tools to Transfer Files 211
Using Windows File Transfer Tools 213
Sharing Remote Directories 213
Sharing Remote Directories with NFS 213
Sharing Remote Directories with Samba 215
Sharing Remote Directories with SSHFS 218
Chatting with Friends in IRC 219
Using Text-Based e-mail Clients 220
Managing e-mail with mail 221
Managing e-mail with mutt 222
Summary 223
Chapter 13: Doing Remote System Administration 225
Doing Remote Login and Tunneling with SSH 225
Configuring SSH 227
Logging in Remotely with ssh 228
Using screen: A Rich Remote Shell 233
Using a Remote Windows Desktop 236
Connecting to a Windows Desktop with tsclient 236
Connecting to a Windows Desktop with rdesktop 237
Using Remote BSD Desktop and Applications 238
Sharing Desktops Using VNC 239
Setting Up the VNC Server 239
Starting Up the VNC Client 240
Using VNC on Untrusted Networks with SSH 241
Sharing a VNC Desktop with Vino 241
Summary 242
Chapter 14: Locking Down Security 243
Working with Users and Groups 244
Managing Users the GUI Way 244
Adding User Accounts 244
Adding Batches of Users 245
Setting User Account Defaults 247
Using Options When Adding Users 248
Using Login Classes 249
Modifying User Accounts 250
Deleting User Accounts 251
Managing Passwords 252
Adding Groups 252
Checking on Users 253
Securing Network Services 255
Configuring the Built-In Firewall 258
Working with System Logs 261
Using Advanced Security Features 261
Summary 262
Appendix A: Using vi or Vim Editors 263
Starting and Quitting the vi Editor 264
Moving Around in vi 265
Changing and Deleting Text in vi 266
Using Miscellaneous Commands 268
Modifying Commands with Numbers 268
Using Ex Commands 269
Working in Visual Mode 270
Summary 270
Appendix B: Shell Special Characters and Variables 271
Using Special Shell Characters 271
Using Shell Variables 272
Appendix C: Personal Configuration Files 277
Index 281