February 15, 2007

My sysadmin toolbox

Author: Leslie P. Polzer

My working day includes a variety of tasks, and most of them take place on the command line, because that approach enables me to do things in the most efficient way. But you can also waste a lot of time on the command line if you don't know what utilities will give you what you need quickly. Here's an introduction to the most important tools I use every day.

zsh

The GNU Bourne Again Shell, bash, is the command line interpreter traditionally associated with Linux systems, and most GNU/Linux systems ship it as default. While it has considerably improved in terms of comfort, it stands behind the powerful Z Shell, which you can use as a superset of the Bourne Again Shell.

The additional features zsh offers can save you a lot of time. Zsh comes with a comprehensive completion system that has out-of-the-box support for SSH hostnames, Makefile targets, command-line arguments (for example, for MPlayer and configure scripts) and in general more than you can shake a stick at.

Among its fancier of extensions are:

ls -d *(/) # list directories
ls *(m0) # list files modified today
!$ # insert last argument of previous command
ls foo{1..3} # expands to ls foo1 foo2 foo3

These are only a fraction of its additional features. For a larger overview, consult zsh-lovers.

fmt/par

Outlook Express users send you messages with extra-long line lengths? Need to have a text file formatted with an 80-character boundary? Easy -- use fmt, the traditional Unix paragraph formatter, or a modern version called par:

  fmt -80 report.txt
  par w80 report.txt

I especially like to reformat mail quotes from within Vim by selecting the relevant paragraphs in visual mode (Ctrl-V) and then running the command !par w72 in ex mode. par will preserve any quotation marks and paragraph divisions.

cat

cat simply concatenates files or standard I/O. In combination with a
here document, cat can create entire files from scratch:

cat > /etc/resolv.conf <<EOD
search farpoint.local
nameserver longbow
EOD

Or combine files:

cat part1.txt part2.txt >> full.txt

If everything in Unix really was a file, as it is supposed to be in Plan 9, you could even use it on sockets, thereby using cat to send and receive data through a network connection; but since this isn't the case, you have to resort to its specialized companion netcat.

However, please take care to avoid the "useless uses of cat," lest you win an award for your folly.

muttng

Why prefer mutt (or its patched cousin muttng) to the many GUI messaging clients out there? For a lot of reasons: painless integration with your favorite text editor (vi and Emacs users will like that), sensible key bindings, high configurability, pretty standard-compliant support for the major mail protocols, and easy usage over network connections among them.

In my experience it's not worth adapting to mutt if you're only getting a few messages every other day, but once your mail volume rises above a
certain number, you will be way more effective with mutt.

dillo

The Dillo project has created an X Window Web browser with a very small footprint. Dillo is an excellent tool with which to quickly view HTML documents from the command line, as it starts up in an instant and has a very clean interface. I like to use it for HTML manuals and to browse TexInfo documentation in HTML format.

OpenSSH

Designed as a secure replacement for telnet, rologin, and ftp, OpenSSH offers substantially more comfort and features than the tools it replaces. You can create encrypted tunnels with it and forward X windows. Some people even like to use it for mass command execution in cluster environments or large installations.

xbindkeys/actkbd

These two programs are daemons that will execute a specific command when they receive the corresponding mouse or keyboard shortcut.

xbindkeys needs X running and reads input using Xlib. You can associate certain inputs with actions by defining them in rules in the file .xbindkeysrc in your home directory. For example, you could bind your fourth mouse button to start your favorite terminal emulator, and two function keys could be set up to control the volume.

actkbd works both under X and on the console. You could, for example, use it to eject your CD-ROM tray with a keypress both on the console and in X. It uses the evdev interface to receive events, and might therefore also work with exotic input devices (think of buttons on webcams).

For maximum efficiency, use both together.

vim

Here comes the religious part. What I like about Vim is its speed, its ubiquity (you can find a plain vi editor on every Unix box) and its modal interface, which enables me to modify my files with high speed.

grep, sed, cut, awk, perl

Here are five omnipresent and flexible tools to work on text. Use grep to filter out (un-)important stuff or search for something, sed to do quick patching or on-the-fly text rewriting, and cut to select columns.

If these don't suffice, throw in awk as a Turing-complete replacement for all three. If you're still not content or have a taste for it, use Perl, which scales well for larger projects, frees you from quoting hassles, and is faster.

I use the GNU versions of sed, cut, and awk, since they offer the most features, but you can find these utilities in a variety of flavors on every Unix system.

mlterm

Mlterm is my favorite X terminal emulator. Both fast and powerful, it also sports excellent support for non-English scripts. It's less cluttered than Konsole and has a smaller footprint, but it still offers a palette of features such as background fading (change brightness depending on focus), background images, and a GUI configurator. I use it with a black background, white text color, and a font that doesn't hurt my eyes (try ISO10646_UCS4_1_BOLD=-misc-fixed-bold-r-normal--15-140-75-75-c-90-iso10646-1:40;15 or something similar in your ~/.mlterm/font file).

pgrep/pkill

These two tools let you quickly list (pgrep) and signal (pkill) processes, as you can see here:

pgrep fox # roughly equivalent to 'ps -ax | grep fox'
pkill fox # kill all processes matching '*fox*'

Simulating the second example would likely take a combination of ps and awk -- not nice for stuff that you use often! The two utilities come with the procps package.

Conclusion

Since I need to mix the CLI with GUI applications, like browsers, I need a link to tie those two worlds together
-- best with a minimum of mouse usage. The usual WIMP-style
interfaces (KDE, GNOME, and most window managers and alternative desktop environments) cannot do this right,
so I'm using a tiling window manager (currently Ion3. Throw in a browser with configurable shortcuts, and you'll hardly ever have to reach for the mouse again!

Leslie P. Polzer works mainly as a system administrator, programmer, and consultant.

Click Here!