BUY THIS BOOK
Add to Cart

Print Book $24.95


Add to Cart

Print+PDF $32.44

Add to Cart

PDF $19.99

Safari Books Online

What is this?

Add to UK Cart

Print Book £17.50

What is this?

Looking to Reprint or License this content?


Linux Desktop Hacks
Linux Desktop Hacks Tips & Tools for Customizing and Optimizing your OS By Nicholas Petreley, Jono Bacon
March 2005
Pages: 342

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Booting Linux
Some of you might be wondering why this book contains a chapter on booting your computer. If you're a new desktop Linux user, you might think the ideal boot experience should involve only two steps: press the computer's power button, and then log in.
Even experienced Linux users might question why this chapter exists. Talk to any longtime Linux aficionado, and he'll boast that one of the biggest advantages Linux has over that "other" popular x86 desktop operating system is that you almost never have to reboot Linux.
Regardless of what type of user you are, chances are good that you power off and boot your Linux desktop system now and then. So, why not make the experience a little more pleasant by sprucing up your bootloader with a fancy background graphic? One word of warning: if you choose to design your own LILO or GRUB bootloader backgrounds, the process is easy and highly addictive. It could become your next hobby.
And while you're at it, why not add a graphical boot-progress screen to Debian, one of the last of the popular distributions that lacks a boot splash screen with a progress bar?
In addition to teaching you how to accomplish these two tasks, this chapter will also help take you out of the Dark Ages of text-based virtual console screens. Whether you boot to a text login screen or a graphical login screen, there's no excuse for leaving the virtual console screens in archaic 80x20 character text mode. It is especially important to enable frame-buffer consoles if you want to take advantage of even fancier hacks in the following chapters.
The beginning of all Linux journeys originates with the humble bootloader—the small bit of code that jump-starts the whole boot process. Knowing how to configure your bootloader is almost requisite for using a Linux system. You also need to understand the bootloader if you want to configure your system to boot more than one operating system.
Within the exciting bootloader world are a number of choices for starting your computer. For x86 machines, there are two main contenders:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Hacks 1-9
Some of you might be wondering why this book contains a chapter on booting your computer. If you're a new desktop Linux user, you might think the ideal boot experience should involve only two steps: press the computer's power button, and then log in.
Even experienced Linux users might question why this chapter exists. Talk to any longtime Linux aficionado, and he'll boast that one of the biggest advantages Linux has over that "other" popular x86 desktop operating system is that you almost never have to reboot Linux.
Regardless of what type of user you are, chances are good that you power off and boot your Linux desktop system now and then. So, why not make the experience a little more pleasant by sprucing up your bootloader with a fancy background graphic? One word of warning: if you choose to design your own LILO or GRUB bootloader backgrounds, the process is easy and highly addictive. It could become your next hobby.
And while you're at it, why not add a graphical boot-progress screen to Debian, one of the last of the popular distributions that lacks a boot splash screen with a progress bar?
In addition to teaching you how to accomplish these two tasks, this chapter will also help take you out of the Dark Ages of text-based virtual console screens. Whether you boot to a text login screen or a graphical login screen, there's no excuse for leaving the virtual console screens in archaic 80x20 character text mode. It is especially important to enable frame-buffer consoles if you want to take advantage of even fancier hacks in the following chapters.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Give Your Computer the Boot
The beginning of all Linux journeys originates with the humble bootloader—the small bit of code that jump-starts the whole boot process. Knowing how to configure your bootloader is almost requisite for using a Linux system. You also need to understand the bootloader if you want to configure your system to boot more than one operating system.
Within the exciting bootloader world are a number of choices for starting your computer. For x86 machines, there are two main contenders: LILO and GRUB. If you are running Linux on a Mac, LILO and GRUB are not available, and the main bootloader is called yaboot. Another potential situation in which you might need to boot a computer is when you are trying to boot a CD to install an operating system. Sometimes you can encounter a problem booting from a CD if your computer's BIOS does not support booting from CD-ROM or if your CD-ROM drive does not support booting from a CDR. You can resolve this problem with a tool called the Smart Boot Manager.
This hack explores the GRUB, yaboot, and Smart Boot Manager bootloaders.
Without a doubt, GRUB is a far more flexible boot manager than LILO. It enables you to add new kernels or boot parameters without having to install each update to the Master Boot Record (MBR). GRUB also allows you to pass parameters to it or to the kernel at boot time. Many Linux distributions now ship with GRUB as the default bootloader. Although GRUB is reliable and flexible enough to recover from most disasters, it is still advisable to back up your boot sector just in case something goes wrong. To do this, run the following command:
root@bar:~# dd if=/dev/hda of=/root/hda.mbr bs=512 count=1
            
GRUB is managed by a central configuration file which is either /boot/grub/menu.lst on Debian and Gentoo systems, or /etc/grub/grub.conf on Red Hat-based systems. This file contains a number of entries that indicate the name of the kernel, the root partition where the kernel can be found, and an
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Kill and Resurrect the Master Boot Record
How to (carefully) back up and restore the Master Boot Record (MBR).
The MBR is a 512-byte segment at the very beginning (the first sector) of a hard drive. This segment contains two major parts: the boot code in the first 446 bytes and the partition table (plus a 2-byte signature) in the remaining 66 bytes. When you run lilo , grub-install, or fdisk /mbr in DOS, it writes to these first 446 bytes. When you run cfdisk or some other disk-partition program, it writes to the remaining 66 bytes.
Writing directly to your MBR can be dangerous. One typo or mistake can make your entire system unbootable or even erase your entire partition table. Make sure you have a complete backup of your MBR, if not your full hard drive, on other media (like a floppy or anything other than the hard drive itself) before you try any potentially destructive commands.
The MBR is very important and crucial for booting your system, and in the case of your partition table, crucial for accessing your data; however, many people never back up their MBR. Use Knoppix to easily create backups of your MBR, which you can later restore in case you ever accidentally overwrite your partition table or boot code. It is important to double-check each command you type, as typing 466 instead of 446 can mean the difference between blanking the boot code and partially destroying your partition table.
First, before you attempt anything potentially destructive, back up the current MBR. Boot into Knoppix, and type the following command into a terminal:
knoppix@ttyp0[knoppix]$ sudo dd if=
               /dev/hda 
               of=/home/knoppix/mbr_backup 
               bs=512 count=1
            
Change /dev/hda to match the drive you wish to back up. In your home directory, you should now see a 512-byte file called mbr_backup. Dd is used to create images of entire hard drives, and in this case, a similar command is used; however, it contains two new options:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Bypass the Boot Manager
Save your system from a broken or missing kernel.
Of all the fantastic things you can do with your Linux box, one of the most nerve-wracking activities is changing or upgrading a kernel. This not only involves the archaic process of compiling the kernel itself [Hack #88] , but it also requires you to update your bootloader so that you can actually use the new kernel. Updating the bootloader often involves editing a complex configuration file in which one wrong change can lead to a system that won't boot. Perhaps nothing is more worrisome in the Linux world than a computer containing all your important work and files that doesn't boot, because you screwed up the bootloader. Worrisome as it is, there is a useful hack you can use to get around the bootloader to boot a working kernel, fix the broken configuration file, and restart a healthy, working system.
The key to restoring your sanity when confronted with a broken kernel is to use the installation disk you used to install Linux on your computer in the first place. Every installation disk boots a generic kernel from the CD-ROM to perform the installation routine. Many of the install disks for distributions such as Debian, Ubuntu, Mandrake, Red Hat, and others include a slim-line shell called BusyBox that you can use to sneak behind the installer and fix your system.
When you run the CD-ROM-based installation program, nothing on your hard disk is affected until you get to the partitioning part of the program. Just before this section, jump to another virtual terminal by pressing Ctrl-Alt-F(n) (such as Ctrl-Alt-F2). If you are installing Linux on a Mac, this can be a little trickier, because you need to press the Apple Option Key-F(n)-F(n). These key combinations switch you to a blank screen where you see a root shell. This shell is a substantially cut-down version of a normal shell, but it has the essential commands to navigate directories, mount disks, and use an editor to change files.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Set a Bitmap Boot Screen for LILO
You can have a splash screen for just about everything these days, including your bootloader.
Most people use either the time-honored LInux LOader (LILO), or the GRand Unified Bootloader (GRUB). This hack shows you how to download a LILO splash screen and configure it to appear with LILO when you boot your machine. This hack assumes your video card supports VESA frame buffers that will render a 255-color, 640x480 graphics screen in bitmap format (practically every decent graphics card made in the last several years works). It is not necessary to configure your Linux kernel to support frame buffers. When LILO displays the splash screen, the Linux kernel isn't loaded yet, so LILO cannot depend on the kernel for frame-buffer support.
On the other hand, this hack works nicely with all the other frame-buffer hacks, such as [Hack #8] and [Hack #20] , so you might want to combine all these hacks to get the most out of your card's frame-buffer support.
You won't find a cornucopia of preconfigured LILO splash screens on the Internet, but a few do exist. Most of the downloads include a README file that includes instructions on the lines you need to add to lilo.conf to make the LILO boot splash work. Some of the URLs listed here have the instructions written on their respective web pages. Here are most of the splash screens available on http://www.kde-look.org:
Tux Lilo Selection Screen
http://www.kde-look.org/content/show.php?content=16756
Lilo Waterdrops Theme
http://www.kde-look.org/content/show.php?content=16739
EnterTheLinux lilo/grub splash screen
http://www.kde-look.org/content/show.php?content=11098
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Create Your Own LILO Boot Splash
Create or modify any picture for use as a LILO boot splash background.
You can easily start from scratch and create any kind of LILO boot splash background. Just make sure you consider the following:
  • The image resolution must be 640x480.
  • The image must be a bitmap.
  • The image must comprise 255 colors or less, and it must be indexed.
  • You should plan to create or allocate two areas on the screen—one for the LILO menu and one for the LILO timer, which ticks down until it automatically boots.
This example bases the LILO boot screen on a KDE screenshot with a Konsole window open (Figure 1-1). The idea is to place the LILO boot menu within the Konsole window, and the countdown timer where the clock appears on the KDE panel. I set up the KDE screen the way I wanted it to look. If you take a similar approach, do not use a complex and colorful background, because it probably will not translate well into 255 colors. I recommend you use the Gimp to take the screenshot and modify it for use as the LILO boot screen. [Hack #12] shows you one method of taking screenshots. Here's how to do it with the Gimp:
  1. Arrange the KDE screen like the one shown in Figure 1-1.
  2. Switch to another virtual desktop, and start the Gimp (starting the Gimp in another virtual desktop ensures that the Gimp won't appear in the screenshot).
  3. Select FileAcquireScreen Shot. This brings up a dialog box.
  4. Select the Whole Screen radio button.
  5. Give yourself about 5 seconds by entering 5 in the "Grab after [] Seconds Delay" field.
  6. Click the OK button.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Display a GRUB Boot Splash Background
Add a graphical background to your GRUB bootloader.
By default, the GRUB bootloader looks rather bland . It's a menu in a box with some instructions below the menu. It is possible, however, to customize GRUB to display a graphical background. This hack blends nicely with other custom screen hacks such as [Hack #8] and [Hack #20] , because each lets you customize the look of your screen at the different stages of booting up Linux, and starting up terminals or window managers.
It's hard to find many premade backgrounds for GRUB. You can view thumbnails of some available images at http://ruslug.rutgers.edu/~mcgrof/grub-images/images/, and download those images from http://ruslug.rutgers.edu/~mcgrof/grub-images/images/working-splashimages/. Place the image in the /boot/grub directory.
For example, let's assume you downloaded the file frag.xpm.gz. Log in as root, and copy that file to the /boot/grub directory:
# cp frag.xpm.gz /boot/grub
         
Now use your favorite editor to add or change the following line in your /boot/grub/grub.conf file:
splashimage=(hd0,0)/boot/grub/frag.xpm.gz
The preceding line assumes that your /boot directory is located on the first partition on the first disk on your system. Change (hd0,0) to point to the disk and partition where your /boot directory resides, if it is different from the example.
Reboot and voilà: you now have a new background splash screen for GRUB. [Hack #7] provides full details on how to make your own personal backgrounds.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Create a GRUB Boot Splash Background
Create your own personalized boot splash backgrounds for GRUB.
The default GRUB bootloader screen is rather bland, but you can spice it up a little by creating your own custom graphical background screen for the bootloader. GRUB imposes a number of limitations on the image size and number of colors. It also doesn't let you move the menu. The menu appears in a rectangle near the top of the screen, with some text instructions below the menu. This makes it relatively easy to create a graphical background screen for the GRUB bootloader, because you can focus primarily on making the bottom one-third of the screen interesting. That is not to say you cannot use other areas of the screen, but you should be careful. For example, don't make it difficult to read the GRUB instructions by placing complex graphics behind the text.
Here are the rather strict requirements for the image:
  • It must be sized at 640x480.
  • It must be a .xpm format image file (gzip compression is optional).
  • It must contain no more than 14 colors.
Most people will cringe at the 14-color limit, but it is rather amazing what you can do with just 14 colors. Cartoon images are quite suitable for this narrow range of colors, and the narrow range of colors to represent the official Linux penguin (Tux) works fine.
Find or create any image you want to try as a background for GRUB. If you create an image yourself, it's best to create a 640x480 image and use as few colors as possible so that you don't lose the fidelity of the image when you later reduce it to 14 colors. Don't worry about using your graphics editor to limit yourself to 14 colors, however. It is possible to use the Gimp to reduce your image to use 14 colors, which can be a good way of fine-tuning the results you want.
Here is what you need:
  • A graphics editor, such as the Gimp, if you want to create or modify an image.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Jazz Up Your Debian System Boot
Boot Debian with an optional graphics screen and progress bar, and spiff up your text consoles by running them in graphics mode.
Here's how to add a graphical boot splash to your Debian distribution, the only popular Linux distribution that lacks a built-in boot splash. Windows and Mac OS X have a graphical boot process, as do all the most popular versions of Linux, including Fedora Core (Red Hat), SUSE, and Mandrake. Debian lacks one. (OK, to be perfectly fair, so does Slackware, but currently there's no simple solution for bringing a graphical boot screen to Slackware.) Here's how to bring your Debian system up-to-date with the rest.
You must use a 2.6 version of the Linux kernel for the boot splash to work. If you are using a 2.4 or earlier version of the Linux kernel and are not willing to upgrade to a 2.6 version, you cannot use this hack. This hack also assumes you know how to configure, build, and install a new Linux kernel, and that you already have chosen a version of the 2.6 Linux kernel source code and have that source code on your hard drive. [Hack #88] provides instructions on how to build and install a Linux kernel if you have never done so. Once you know how to build and install a Linux kernel, you will need to build your kernel with support for frame-buffer graphics, frame-buffer graphics-based consoles, and graphical boot splash screens to make this hack work.
The boot splash portion of this hack gives you a choice of booting in silent mode or verbose mode. Silent mode displays a graphical background (such as a picture of Tux, the official Linux penguin), and a progress bar to give you a rough idea of where you are during the boot process. You can press F2 at any time to switch to verbose mode, where you see exactly what is happening at boot time. When the boot process is finished, all your virtual consoles work in frame-buffer graphics mode.
Whether you boot to a console or to a graphical login manager, you always have a number of virtual text consoles available to you. You can switch between them by pressing Ctrl-Alt-F1 to get to the first virtual console, Ctrl-Alt-F2 to get to the second, and so on. The difference between a frame-buffer console and a normal console is that the frame-buffer console works in graphics mode, which allows you to display more text on the screen (using smaller fonts), yet keep the text very sharp and readable. The consoles are still primarily for text-based use, but because they are running in graphics mode, you can also have a graphical background behind the text.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Graphics on the Console
Some of you might be familiar with a picture of Tux the penguin in the upper-left corner of the screen when your system is booting. Ever since kernel 2.2, this popular feature has graced many a Linux boot sequence. To the untrained eye, it simply looks like a penguin, but to the trained technical eye, it is a cunning use of the Linux frame buffer.
A frame buffer is an abstraction for graphics hardware. This abstraction provides a common set of instructions that programs can use to access the graphics hardware on the computer. Most people see the frame buffer as a grinning Tux when the system boots. However, there's more to the frame buffer than a cartoon penguin. Typically, frame buffers are used to render the console graphically so that a resolution can be set on it. Frame buffers are useful in a number of situations, some of which include the following:
Providing a higher resolution
This allows for the display area to be much larger than before.
Greater color depth
When you use frame buffers, you can use more colors.
Bypassing X Windows
In some instances, certain Linux programs such as Mplayer can use the frame buffer immediately, thus reducing the overhead of having to run an X Windows desktop such as KDE or GNOME. This is suitable for use on resource-limited embedded systems.
Support for the frame buffer comes from the kernel itself. Most stock kernels are supplied with frame-buffer support built into them, so this shouldn't be a problem for most people. To check if your kernel has support already, you can query it:
foo@bar:~$ grep -i config_fb /boot/config-$(uname -r)
CONFIG_FB=y
If you have frame buffers set, the line CONFIG_FB=y will be displayed (in which case you can move on to the next section "Configuring the Bootloader"). If
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Console
Some people actually prefer to use Linux as a desktop strictly in text mode, without running X Windows at all. If you're among those users, this chapter is definitely for you.
Graphical desktop users have no reason to scoff at this approach. Plenty of text-mode versions of productivity applications are equivalent to the applications most people use on a graphical desktop. For example, the text-mode email client called Mutt is actually more powerful and flexible in handling email than any graphical email program that I've tried. It might not have as many features as a combination email/organizer such as Evolution has, but graphical email programs often force you through several menus and submenus to accomplish a task that takes only one keystroke in Mutt.
If you're under the mistaken impression that you need to run a graphical desktop to use those special multimedia and Internet keys on your keyboard, this chapter will set you straight. You can redefine your keyboard so that your multimedia keys control your CD player, the Internet keys open your email program or web browser, and so on. If you use your imagination, you can automate just about any action.
This chapter focuses primarily on what you do at the virtual console. You can use some of the hacks in the chapter (such as how to colorize files in your pager) in graphics terminals on X Windows desktops. But you must avoid using the first two hacks with terminal emulators in X Windows; [Hack #10] and [Hack #11] are designed specifically for the text console. Because X generally redefines the keys the way it wants them, these console definitions will be overwritten, which means you lose the customizations. But at least it won't hurt anything you do in X.
This hack describes one of many ways you can customize the default behavior of keys and key combinations for your keyboard. If you just want to make your special function keys on your fancy Internet and multimedia keyboard work, take a look at
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Hacks 10-15
Some people actually prefer to use Linux as a desktop strictly in text mode, without running X Windows at all. If you're among those users, this chapter is definitely for you.
Graphical desktop users have no reason to scoff at this approach. Plenty of text-mode versions of productivity applications are equivalent to the applications most people use on a graphical desktop. For example, the text-mode email client called Mutt is actually more powerful and flexible in handling email than any graphical email program that I've tried. It might not have as many features as a combination email/organizer such as Evolution has, but graphical email programs often force you through several menus and submenus to accomplish a task that takes only one keystroke in Mutt.
If you're under the mistaken impression that you need to run a graphical desktop to use those special multimedia and Internet keys on your keyboard, this chapter will set you straight. You can redefine your keyboard so that your multimedia keys control your CD player, the Internet keys open your email program or web browser, and so on. If you use your imagination, you can automate just about any action.
This chapter focuses primarily on what you do at the virtual console. You can use some of the hacks in the chapter (such as how to colorize files in your pager) in graphics terminals on X Windows desktops. But you must avoid using the first two hacks with terminal emulators in X Windows; [Hack #10] and [Hack #11] are designed specifically for the text console. Because X generally redefines the keys the way it wants them, these console definitions will be overwritten, which means you lose the customizations. But at least it won't hurt anything you do in X.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Redefine Keyboard Actions
This hack describes one of many ways you can customize the default behavior of keys and key combinations for your keyboard. If you just want to make your special function keys on your fancy Internet and multimedia keyboard work, take a look at [Hack #28] and [Hack #29] . But if you want to fine-tune how keystrokes affect virtual text consoles, this is the place for you.
You might recall that people used to attach several text-based terminals to minicomputers and mainframes so that many people could access the computer at once. Each person could log in to the computer and work on his separate text console, because he had his own text-based terminal connected to the main computer.
Linux simulates that kind of behavior with a single terminal and several virtual consoles. Even if your Linux-based computer starts up automatically with a graphical login screen, you can get to the first virtual text console by pressing the key combination Ctrl-Alt-F1. If you want, you can log in and work, using text-based programs. If you press Alt-F2 from here (or Ctrl-Alt-F2), Linux takes you to another virtual console with another login prompt. This simulates another separate terminal hooked up to the computer. Alt-F3 takes you to yet another virtual console and login prompt.
You actually need to use Ctrl-Alt-Fn only when switching from an X console, such as virtual console 7. Otherwise, you can get away with just Alt-Fn.
You might have noticed by now that you can switch from one text console to another only by using the Alt key on the left side of the keyboard. It won't work if you use the Alt key on the right side of the keyboard with F1, F2, or any other function key. This is not an accident. Linux is configured such that the Alt key on the right side of the keyboard plus the F1 key will take you to virtual console 13. The problem is that very few people use as many as 13 virtual consoles, and most Linux distributions don't even enable more than 10 or 11 virtual consoles by default.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Energize Your Console with Macro Music Magic
Redefine keys to issue commands at the command line.
You can exploit the power of the preceding keyboard customization technique to a much greater degree than just redefining the action of a key. You can actually define keys to send strings of characters, which, at the console, means issuing commands.
In this example, you're going to redefine keys to control your CD-ROM as a CD player. Even if you have a plain keyboard, you can simply use unusual key combinations such as Ctrl-Alt-Right Arrow to perform the kind of magic you're about to explore. If you can determine the keycodes generated by any special keys you have on your Internet or multimedia keyboard, you can use those keys instead.
First, you want to create a file called /etc/mykeys, or add to your existing /etc/mykeys file if you are combining this hack with [Hack #10] . You will place in /etc/mykeys string definitions that represent commands. The cdtool program is really handy for controlling a CD player at the command line without a bothersome user interface. You can use another tool if you prefer, but you'll have to substitute your tool's commands for the ones defined by cdtool.
Assume you are using cdtool to define commands to play a CD, stop playing it, advance to the next track, move to the previous track, etc. First, define labels for the command strings. Here is what you add to /etc/mykeys:
string F100 = "cdplay\n"
string F101 = "cdstop\n"
string F102 = "cdplay +\n"
string F103 = "cdplay -\n"
string F104 = "eject\n"
Notice that each command string includes a trailing \n. This is the equivalent of pressing the Enter key. If you don't add the \n at the end of each string, the computer "types" the command, but doesn't execute the command until someone presses Enter.
If you have a normal keyboard with no added multimedia keys, decide on a set of keys you want to modify. In this example, you will assign the following keys these actions:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Take a Screenshot from the Command Line
Who needs a graphical tool to grab a screenshot? The command line has everything you need.
For many writers and programmers, screenshots are useful for showcasing how an interface or program looks. Although grabbing a screenshot is often as simple as running a small utility or clicking an option, some grabbers are not as flexible as you need them to be. For example, sometimes you might need a screenshot without the screenshot tool displayed in the taskbar, or you might need to take a screenshot in an environment, where you cannot run a graphical screenshot utility. This is a common problem for those who need to take screenshots of installation programs or software on embedded devices.
Although a graphical screenshot-grabbing tool is the obvious choice for making a screenshot, most of these utilities leave a trace of themselves in the screenshot by having an entry on the taskbar or being visible on the desktop. You can solve this problem by using a collection of command-line tools to take the screenshot from an X terminal or even from within the Run option in the KDE/GNOME main menu:
foo@bar:~$ sleep 2; import -window root screen.png
            
This command is actually composed of two separate utilities. The sleep command delays the process for two seconds before the screenshot is taken. This gives you time to minimize windows, expand menus, or make other necessary adjustments before the screenshot is taken. By changing the sleep value you can control the delay before the screenshot is taken. The second command uses the import utility that is part of the ImageMagick suite of tools (use your package manager to install ImageMagick if it isn't already on your system) to take a screenshot of the root window (the root window is the entire screen) and name the image screen.png. If you want to grab a particular part of the screen, you also can use the -crop option to grab that specific area (such as import -crop
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Put Your Command Prompt on a Diet
Change your bash prompt to show extra information without cluttering the area around your command prompt.
This hack saves command-line prompt space without sacrificing the informational value that prompt hacks often add to command-line prompts. Sure, it's great to see the date, time, uptime, and phase of the moon in your bash prompt, but most schemes for providing this extra information create a crowded mess and push more important text off the terminal screen. This hack uses the tput command to place some of that extra information in the upper-right corner of the terminal or console, leaving your prompt neat, clean, and short. Thanks to Giles Orr whose web site on bash prompts (http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/) inspired this example hack.
I'll keep this particular hack simple. You are going to put the current working directory somewhere on the terminal but not at the prompt itself. This hack creates a directory path that always appears in the upper-right corner. Of course, as explained later, you can modify this location.
The tput command is ideal for this type of trick, because tput manipulates the location and color of cursors. Figure 2-1 shows what it might look like when it is configured as the default prompt.
Figure 2-1: The current working directory in the upper-right corner
This might seem somewhat superfluous when using an X terminal, because it's easy to put the current working directory in the titlebar. However, not every terminal works that way, and there is no titlebar on a virtual console, so it still comes in really handy, especially when you are working without a graphical window manager or desktop.
Here is the script, with comments on how it works:
#!/bin/bash

function prompt_command {
# save the current position
tput sc
# backwash is where to position the cursor 
# to write the whole current working directory
# we back up 2 more for the brackets
let backwash=$(tput cols)-$(echo $(pwd) | wc -m)-2
# position the cursor at Y=0, X=calculated length
tput cup 0 ${backwash}
# set foreground color, bold
tput setaf 4 ; tput bold
# wrap the full path in brackets
echo -n "["
# set the color of the current path
tput setaf 6
# show the path
echo -n "$(pwd)"
# set the color of the closing bracket"
tput setaf 4 ; tput bold
# show the closing bracket
echo -n "]"
# return cursor to the saved position
tput rc
}

PROMPT_COMMAND=prompt_command

GREEN="\[$(tput setaf 2 ; tput bold)\]"
WHITE="\[$(tput setaf 7 ; tput bold)\]"
NO_COLOUR="\[$(tput sgr0)\]"

case $TERM in
    xterm*|rxvt*)
        TITLEBAR='\[\033]0;\u@\h \007\]'
        ;;
    *)
        TITLEBAR=""
        ;;
esac

PS1="${TITLEBAR}\
$GREEN\u@\h \
$WHITE\$$NO_COLOUR "
PS2='> '
PS4='+ '
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Simplify Changing Directories
This hack makes it ridiculously easy to jump right to your favorite directories from the command line.
Every Linux user probably knows there's an environment variable called PATH. When you type a command, your system looks through all the directories defined in the PATH environment variable until it finds the command. If it doesn't find the command by the time it has exhausted all the directories listed in the path, it tells you it can't find the file.
There's a similar environment variable that is a bit more obscure called CDPATH. You can define CDPATH to include a number of directories to search when you type the following command:
$ cd 
            somewhere
         
Here's how it works. Assume for a moment that you're an obsessively organized writer who likes to create directory trees that organize files by types, groups, categories, etc. Let's say you organized your documents such that you have the following directories:
/docs/pub/books/oreilly/linuxhacks/chapter1
/docs/pub/books/oreilly/linuxhacks/chapter2
/docs/pub/books/oreilly/linuxhacks/chapter3
Here's what you want to do. You want to be able to jump right to the chapter2 directory without having to type the entire path. Here's how to set the CDPATH environment variable so that you can do that:
$ export CDPATH=:.:/docs/pub/books/oreilly/linuxhacks
         
Notice that this command does not include any of the chapter directories in the CDPATH. That's because you want the CDPATH to define the directory immediately above the directory you want to find. Remember, the system searches the directories listed in the path for the contents you specify on the command line. The argument chapter2 is what you're looking for. chapter2 exists in the directory /docs/pub/oreilly/linuxhacks, so that is the directory you want to include in your search CDPATH.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Colorize Files in Your Pager
As a Linux user you might have noticed that filenames appear in different colors when you type ls or dir. Yet when you pipe the colored file listing through a pager such as less, the pager ignores the colors and turns the output into black and white. You might notice this when you execute a commonly used command, such as ls -al | less.
Most Linux distributions are configured to display the files in various colors to make it easy to identify symbolic links, executable files, compressed files, and so on. If yours is not configured in such a way, there's an easy way to correct this. Type the following command:
$ alias ls='ls --color=auto'
         
This tells your system that every time you type the ls command, it will actually type ls --color=auto for you. Now type the command:
$ ls
         
You should see your files appear in different colors, according to their type. If you didn't see colored filenames, you might have a distribution that requires a slightly different switch. Try this instead:
$ alias ls='ls --color=tty'
         
Now type the command to test the color capability:
$ ls
         
Chances are that one or more alias definitions are already defined for you in one of the automatically executed login files. See the sidebar for more information about how many popular distributions make these settings.
Now here's the problem that might plague you at times. You want to view a list of files, so you issue the command ls -a. That command lists all the regular and hidden files, and all the files and directories appear in living color. Or perhaps you prefer to use the command ls -al that lists all regular and hidden files in a detailed single column. Once again, the files appear in living color.
When there are many files, the list scrolls off the screen. This is particularly troublesome on a text console that doesn't let you scroll back far enough to see the beginning of the list. What's a geek to do? The intuitive solution is to issue this command:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: Login Managers
Personal computers are no longer as personal as they used to be. You might have a family computer, for example, and each member of the family uses the computer with his own personal account. In this case, at one time or another you will encounter this conversation, or something like it: "I need to use the computer." "But I'm right in the middle of typing my school report." "This can't wait, you need to log off and let me use it now!"
If you are ever caught in a situation in which you have to relinquish control of your computer to another person, it seems both logical and natural that you should close all your applications and log out before allowing the other person to access the computer. That isn't necessary with Linux. Linux is truly multiuser. You can have two or more people logged in to the same computer at the same time, even running separate graphical desktops at the same time. You don't have to close all your applications and log out. All you have to do to relinquish control to another person is to lock your session (for security purposes), and then let the next person start up his own session.
In some cases, desktop environments, such as KDE and GNOME, provide you with a handy menu option that will let someone start another desktop session. If your Linux distribution doesn't provide you with that easy method, don't worry. The following hacks will show you how you can set up two or more KDE or GNOME graphical login screens, where each user can start up her own session isolated from any other sessions that are running.
I've even provided a method for the staunch power user who resists point-and-click in favor of a command-line approach.
Finally, these hacks introduce a whole new approach to graphical login screens. These screens replace the dull text login prompt with a graphical frame-buffer console prompt that lets you start up anything from plain-text consoles to window managers or desktop environments such as KDE and GNOME. You can set up as many of these login screens as you want, and each one will start up a separate user session. You can even customize the graphical look and feel of each login screen.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Hacks 16-21
Personal computers are no longer as personal as they used to be. You might have a family computer, for example, and each member of the family uses the computer with his own personal account. In this case, at one time or another you will encounter this conversation, or something like it: "I need to use the computer." "But I'm right in the middle of typing my school report." "This can't wait, you need to log off and let me use it now!"
If you are ever caught in a situation in which you have to relinquish control of your computer to another person, it seems both logical and natural that you should close all your applications and log out before allowing the other person to access the computer. That isn't necessary with Linux. Linux is truly multiuser. You can have two or more people logged in to the same computer at the same time, even running separate graphical desktops at the same time. You don't have to close all your applications and log out. All you have to do to relinquish control to another person is to lock your session (for security purposes), and then let the next person start up his own session.
In some cases, desktop environments, such as KDE and GNOME, provide you with a handy menu option that will let someone start another desktop session. If your Linux distribution doesn't provide you with that easy method, don't worry. The following hacks will show you how you can set up two or more KDE or GNOME graphical login screens, where each user can start up her own session isolated from any other sessions that are running.
I've even provided a method for the staunch power user who resists point-and-click in favor of a command-line approach.
Finally, these hacks introduce a whole new approach to graphical login screens. These screens replace the dull text login prompt with a graphical frame-buffer console prompt that lets you start up anything from plain-text consoles to window managers or desktop environments such as KDE and GNOME. You can set up as many of these login screens as you want, and each one will start up a separate user session. You can even customize the graphical look and feel of each login screen.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Switch Users Fast
It's a personal computer. No, it's a family computer. No, it's two . . . two . . . two computers in one! Picture this. You're working at home on an important business document when your daughter tells you she needs to use the computer for a while to do research for a school project. Do you have to close all your applications and log out for her to do her work? Nope. The following hack shows you a few ways to let your daughter log in and work with her own desktop without you having to close your applications and log out first.
You can get two or more users logged in and running separate graphical desktops on the same machine in a number of ways. This hack explores the built-in method.
You might recall that people used to attach several text-based terminals to minicomputers and mainframes so that many users could access the computer at once. Each person could log in to the computer and work on his separate text console, because he had his own text-based terminal connected to the main computer. Linux simulates that kind of behavior with a single terminal and several virtual consoles. Most Linux distributions provide six text-based virtual consoles and usually three virtual consoles for graphical desktops, although more of both types are possible.
If you have used the graphical interface only on Linux and you're not familiar with virtual consoles, here's a quick way to see what they're all about. Press the key combination Ctrl-Alt-F1. You should see a text-based screen with a login prompt. This is virtual console 1. Press the key combination Ctrl-Alt-F2. This is virtual console 2. You can get back to the graphical desktop by pressing Ctrl-Alt-F7, because the default virtual console for graphical desktops is virtual console 7. If you can start up a second graphical desktop, it will be assigned to the next available virtual console after the previous graphical desktop, which is virtual console 8. You can switch to that desktop by pressing Ctrl-Alt-F8. So, when you achieve your goal of starting up two separate graphical desktops, you can switch between them by pressing Ctrl-Alt-F7 and Ctrl-Alt-F8.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Double Your KDM (KDE) Login Screens
Set up the graphical login manager KDM to run on two different screens when you boot the computer.
If you run the KDE graphical login manager, KDM, you can set up KDM so that one user can log in from one login screen, and if another user wants to jump in and do some work, he can log in from the second login screen. You're not limited to two login screens, but it takes some good horsepower to run more than two sessions at once.
If your Linux distribution runs KDE by default rather than GNOME, it probably uses the KDM graphical login manager to log in. This likely is true even if you're not actually using KDE as your desktop, simply because distributions that favor KDE also favor KDM.
In this case you need to locate the configuration files for KDM to make the changes necessary to enable multiple simultaneous desktop users. The KDM configuration files are located in the same place as your kdmrc file. To locate this file, log in as root and type this command:
# locate kdmrc
            
               /etc/kde3/kdm/kdmrc
            
         
In this case, the output tells you that kdmrc, and thus the KDM configuration files, are located in /etc/kde3/kdm. Change to this directory (or whatever directory your Linux distribution uses) and edit the Xservers file.
Your distribution might include support for more than one graphical login manager, in which case it might have more than one Xservers file; one for each alternative such as XDM or GDM. Make sure you are editing the Xservers file in the directory for the KDM graphical boot manager.
You should see something like the following in the Xservers file:
:0 local@tty1 /usr/X11R6/bin/X -nolisten tcp
#:1 local@tty2 reserve /usr/X11R6/bin/X -nolisten tcp :1
#:2 local@tty3 reserve /usr/X11R6/bin/X -nolisten tcp :2
#:3 local@tty4 reserve /usr/X11R6/bin/X -nolisten tcp :3
#:4 local@tty5 reserve /usr/X11R6/bin/X -nolisten tcp :4
Change the second line in the preceding output so that this section looks like the following:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Double Your GDM (GNOME) Login Screens
Set up the graphical login manager GDM to run on two different screens when you boot the computer.
This hack will set up GDM to run on two different screens when you boot the computer. One user can log in from one login screen, and if another user wants to jump in and do some work, he can log in from the second login screen. You're not limited to two login screens, but the more sessions you run, the more processor power and memory are needed. Performance shouldn't be a problem on most modern systems, however.
If your Linux distribution runs GNOME by default rather than KDE, or if you're using Fedora, you're probably also using the GDM graphical login manager to log in. This might be true even if you don't use GNOME as your desktop, because distributions that favor GNOME tend to use GDM.
In this case, you need to locate the configuration file called gdm.conf to make the changes necessary to enable multiple simultaneous desktop users. Use the locate command to find out where gdm.conf is located. Type the following command as root:
# locate gdm.conf
/etc/X11/gdm/gdm.conf
Open the gdm.conf file for editing and locate the section that looks something like the following (you can jump right to this spot by searching for the text string [servers]):
[servers]
# These are the standard servers. You can add as many you want here
# and they will always be started. Each line must start with a unique
# number and that will be the display number of that server. Usually just
# the 0 server is used.
0=Standard
#1=Standard
All you have to do is remove the # comment mark before the line 1=Standard so that this section looks like the following:
[servers]
# These are the standard servers. You can add as many you want here
# and they will always be started. Each line must start with a unique
# number and that will be the display number of that server. Usually just
# the 0 server is used.
0=Standard
1=Standard
Save your changes and exit the editor. The easiest way to make sure the changes take effect is to reboot your computer. When your computer presents the graphical login screen, you can check to make sure there are two graphical logins by pressing Ctrl-Alt-F8. If you see the same login screen, everything is working. Press Ctrl-Alt-F7 to get back to the default login screen, log in, and have fun.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Get Multiple Desktops the Macho Way
Power users don't need no stinkin' graphical login managers, so here's the command-line way.
This hack assumes you're not the type who likes to have graphical login screens running all the time. You want your computer to boot to a text login prompt, after which you're perfectly capable of getting a graphical desktop running. You might still want the ability to have a second person log in and start another desktop. So, here's how.
I'll assume you're the power user who avoids GDM, KDM, and XDM. When your computer finishes booting, it leaves you in a virtual console (virtual terminal 1) with a text login prompt. You log in, and then you start your favorite window manager or desktop using whatever command is most familiar to you. Perhaps you type startkde to start the KDE environment. You could type xinit /usr/bin/wmaker to start WindowMaker. You could also type startx alone to start your default window manager. The default window manager could be defined in various ways, depending on the Linux distribution you're using. In most cases, power users configure the ~/.xinitrc file to define the default window manager, among other things (~/.xinitrc also lets you define other programs to start automatically, etc.).
If you want another user, such as your power-user daughter, to log in and start up a separate desktop, press Ctrl-Alt-F2 to get to the second text console with a login prompt. When she logs in, she can start a new WindowMaker desktop without disturbing your desktop by typing either of the following two commands:
$ 
xinit /usr/bin/wmaker -- :1
$ startx /usr/bin/wmaker -- :1
         
In this case, you add a space, a double-dash followed by a space, then a colon, and then a 1. This tells your system to run WindowMaker on the second virtual console allocated for graphical desktops (the default is 0, so the next available graphical console is 1).
This is a rather inconvenient way to do things because you must be sure display 1 is not in use, and you must know the exact path to the window manager or desktop environment you want to start. Neither of the following simpler commands works, because they do not include the full path to the executables:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Scrap X11 for Fancy Login Consoles
Qingy is an attractive replacement for X Windows-based graphical login managers such as XDM, GDM, and KDM.
Qingy is an alternative login screen developed by Michele Noberasco (http://qingy.sourceforge.net/news.php). Qingy uses the graphical frame-buffer console capability in Linux to paint an attractive and powerful session login screen on one or more virtual terminals. Unlike XDM, GDM, and KDM, Qingy does not use X11. Despite this, Qingy has as many features as these traditional graphical login screens, if not more (see the sidebar, ). You can choose your preferred session type, such as KDE, GNOME, WindowMaker, Fluxbox, or just a text console. You can include buttons on the Qingy login screen that will start the screensaver, put your computer in sleep mode, reboot, shut down, and many other options. And of course, you can change the Qingy graphical theme.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Personalize Your Qingy Theme
Content preview·