Making Ubuntu Usable - Optimizing Your System - Hacking Ubuntu (2007)

Hacking Ubuntu (2007)

Part I: Optimizing Your System

Chapter 2: Making Ubuntu Usable

The basic install of Ubuntu provides a usable system. But usable is not the same as optimal. When you first log into the system, there are some things that you will definitely want to change, like the colors (nobody likes brown-seriously, what can brown do for you?). This chapter discusses options for making the desktop and workspace environment more usable.

Logging in for the First Time

When you first log into Ubuntu, a few things will get your attention, like the login music. It's this increasing tone with twinkles that sound like an orchestra tuning up. And the desktop color scheme: brown. Before you can call the system usable, you will need to change these and other things.

Changing the Startup Music

The startup music is one of the easiest items to change. From the menu at the top, select System Image from book Preferences Image from book Sound. This brings up the Sound Preferences applet (see Figure 2-1). From here, you can assign sounds to system events. For example, I have a blood-curdling scream for error messages, and a telephone ringing for question dialog boxes.

Image from book
Figure 2-1: The Sound Preferences applet


New sound selections are not always used immediately. If you find that your new sounds are not immediately used, log out and log back in. This seems to be a bug in the Gnome Sound Preferences applet.

Converting Audio Files

You may select any WAV audio file for any of the audio events. If you want to use a different audio format, such as MP3 or OGG, you will need to convert it to a WAV file. The easiest way to convert audio files is with sox-the universal sound exchanger.

1. The sox package comes from the universe repository, but this repository is not enabled by default. As root, you will need to edit /etc/apt/sources.list and uncomment the two universe lines. These should be near the top of the file and look similar to:

2. deb dapper universe

3. deb-src dapper universe

You will also need to update the repository cache:

sudo apt-get update


The /etc/apt/sources.list file and the apt-get process are detailed in Chapter 4.

4. Install sox if it is not already installed.

5. sudo apt-get install sox

6. Use sox to convert the audio file. By default, sox determines file types by the file extension.

7. sox fileIN.mp3 fileOUT.wav

8. Test the sound file using the play command.

9. play fileOUT.wav

10. From the Sound Preferences applet, select the pull-down menu for the desired system sound, for example, Log in and click Select sounds file…

11. Select the WAV file you just created.


Although sox comes from the universe repository, lame comes from the multiverse repository. You will need to uncomment the multiverse lines in /etc/apt/sources.list and run sudo apt-get update before you can install it. See Chapter 4 for more details.


The sox application is a great tool for converting and modifying sound files, but it does not support all formats for all functions. In particular, even though it can read MP3 files, it cannot be used to create MP3 files without additional libraries. To resolve this constraint, you can use lame package to encode MP3 files from WAV files.

Just as GNU is a recursive acronym "GNU is Not Unix", LAME uses a recursive name: LAME Ain't an MP3 Encoder. Ignoring the name, lame is a powerful tool for creating MP3 files. First, make sure the package is installed: sudo apt-get install lame. Then, convert your WAV file to an MP3 file: lame fileIN.wav fileOUT.mp3.

The MP3 file from LAME cannot be used for system sounds (because system sounds do not support MP3 files) but can be used by other audio applications and portable music devices.

Modifying Audio Files

Most WAV files are larger than a few seconds, and you probably do not want a three-minute song playing every time you click a button. The sox program can be used to trim audio files, but you will need to know the starting time for the sound you want and the length. For example, to start 3 seconds into the file and keep a 2.31 second duration, use:

sox fileIN.mp3 fileOUT.wav trim 3 2.31

Trim times can be written in a variety of formats. You can specify seconds (and fractions of a second) or actual time values. The value 125.6 can be written as 2:05.6 or 0:2:5.6. The notations indicate seconds (with fractions), minutes, and hours. Also, if you do not specify the duration, then it goes to the end of the file.

The sox command can also add effects such as echos, high-pass and low-pass filtering, stretching, and reversing. For a full list of functions, refer to the sox man page (man sox).

Changing the Background

The default background for Ubuntu Dapper Drake is a brown screen with a bright highlight. As my fashionable coworker tells me, "Nobody likes brown." So the background must be changed. The System Image from bookPreferences Image from book Desktop Background menu item brings up the Desktop Background Preferences applet.


You can also bring up the Desktop Background Preferences applet by right-clicking the desktop background and selecting Change Desktop Background from the popup menu.

Although Ubuntu does include some very colorful backgrounds, you can always add your own. Unlike the Sound Preferences, where you are limited to one type of audio file format, the desktop supports many graphic formats. GIF, JPEG, BMP, PCX, TIF, and PNG are all acceptable formats. Besides selecting an alternate image, you can also select whether the image should be tiled, stretched, or centered, or have a border color. In Figure 2-2, I selected my Safe Computing logo, centered, with a vertical gradient background. Since my image is a GIF with a transparent background, the desktop's color scheme blends well with the picture.

Image from book
Figure 2-2: The Hacker Factor Safe-Computing logo as a background with a gradient color


Even though the desktop supports many different graphical formats, there are some graphics that it does not support. In particular, the desktop background displays static pictures. Animated GIFs are not animated, MPEG videos do not work as backgrounds, nor do Adobe Flash movies. In Chapter 9, you'll see how to create animated backgrounds.


If you need to crop or modify your picture, the Dapper Desktop installation automatically includes Gimp, a powerful graphics editor.

Changing the Background As-Needed

The picture and color on the desktop does not need to be static. You can write a simple program to change the picture (or color) as needed. The basic commands are:

gconftool-2 -t str --set /desktop/gnome/background/picture_filename \


gconftool-2 -t str --set /desktop/gnome/background/primary_color "#AABBCC"

gconftool-2 -t str --set /desktop/gnome/background/secondary_color "#112233"

The Gnome Desktop Manager (GDM) keeps a set of configuration parameters, similar to the Microsoft Windows registry. Although the gconf-editor command enables you to interactively view and edit the values (similar to the Windows regedit command), and the gconftool-2 command enables you to edit values from the command line. Within the registry, the picture_filename field stores the name of the background image (in this example, /path/picture.gif). Be sure to specify the full path to the image. As soon as you change the image in the registry, the image on the background changes.

Similar to picture_filename, the primary_color and secondary_color fields are used to control the background color. If you only have a solid background, then the color is defined by primary_color. The secondary_color is only used with a color gradient. Colors are represented by three hex bytes that denote red, green, and blue. Black is #000000, white is #FFFFFF, and a nice light blue is #2255FF. As with the background image, as soon as you change the color in the registry, the color on the background changes.

Using Informative Colors

The background color does not need to be a static color. You can create a script to change the color based on system events. For example, you can monitor a log file and change the background to red when something serious happens. To give you an example, Listing 2-1 is a simple Perl script that changes the background color to represent the system load.


The results from the script in Listing 2-1 will only be visible if you have some portion of the desktop background displaying the primary background color. If you have a graphic that covers the whole desktop, then you will not see anything.

Listing 2-1: Script to Change Background Color with Load


# Color the background based on system load

my $Key = "/desktop/gnome/background/primary_color";

my $Load; # system load

my $R, $G, $B; # Red, Green, Blue colors

while ( 1 )


$Load='uptime'; # get the current load

# format is "time duration, users, load average: 0.00, 0.00, 0.00"

# remove everything except the first load value

$Load =~ s/.*: //;

# load values: 1 minute, 5 minute, 15 minute averages

# these are colored: 1=Red, 5=Green, 15=Blue

($R,$G,$B) = split(', ',$Load);

# scale up to the range 0-255, but cap it at 255

$R = $R * 255; if ($R > 255) { $R = 255; }

$G = $G * 255; if ($G > 255) { $G = 255; }

$B = $B * 255; if ($B > 255) { $B = 255; }

# convert to hex

$Load = sprintf "%02X%02X%02X",$R,$G,$B;

# set the color

system("gconftool-2 -t str --set $Key '#$Load'");

sleep 15; # Update 4 times per minute



This script will change the background color every 15 seconds based on the system load. A reddish color indicates that the system is very active. Yellow indicates an active process that has been running at least 5 minutes; white means at least 15 minutes. If no processes are raising the system load, then the colors will cycle from green to blue to black. If you want to know when the system is busy, a sudden change in the background color should get your attention.

Changing the Fonts

The default font for the Ubuntu desktop is Sans at 10pt. You can change the font used by the desktop through the System Image from book Preferences Image from book Font applet. Changing the fonts immediately changes what is displayed on the desktop.

Changing the DPI

Different output devices render the fonts at different resolutions. Increasing the font size will make it appear larger on all devices including the screen and printer. On the other hand, if you change the rendering resolution for a single device, then the same font size appears different.

The default desktop fonts are rendered at 96 dots per inch (dpi). You can change the dpi value by opening the System Image from book Preferences Image from book Font applet and clicking the Details button.

Although the Font applet is good for one-time changes, it cannot be easily automated. The dpi value can also be set through the gconf-edit registry under /desktop/gnome/font_ rendering/dpi. This value can be adjusted to match your screen. For example, a monitor that is 14 inches across at 1024×768 has approximately 73 horizontal pixels per inch, while a 20-inch wide screen at the same resolution has 51 pixels per inch. The default desktop assumes 1024×768, with 10 inches across. If you change the screen resolution or use a different size monitor, then fonts may appear larger or smaller.

To change the dpi value, use:

gconftool-2 -t float --set /desktop/gnome/font_rendering/dpi 96

Values larger than 96 will make the fonts appear larger (but not change the actual font size), whereas a smaller number decreases the rendered size. In general, changing the dpi setting only changes how fonts are rendered on the screen. It does not change the size of the application windows nor impact how documents will print.

Helping with Big Fonts

I have a couple of co-workers who wear reading glasses when sitting at the computer. More than once, they have come over to my work area and had to run back to get their glasses. To help them (and tease them), I created a Grandpa Mode macro that increases the screen font size-just for them. Permanently setting up the ability to use Grandpa Mode as desired requires four commands. The first two define custom commands that change the dpi value. The second two bind the commands to key sequences.

gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_7 \

'gconftool-2 -t float --set /desktop/gnome/font_rendering/dpi 200'

gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_8 \

'gconftool-2 -t float --set /desktop/gnome/font_rendering/dpi 96'

gconftool-2 -t str --set /apps/metacity/global_keybindings/run_command_7 \


gconftool-2 -t str --set /apps/metacity/global_keybindings/run_command_8 \


Now pressing Ctrl+F7 changes the resolution to 200 dpi and make the fonts appear large (and coworkers can read it without glasses). Ctrl+F8 returns the screen to the system default of 96 dpi (see Figure 2-3).

Image from book
Figure 2-3: The desktop and Gnome terminal as seen at 96 dpi (left) and 200 dpi (right). Both are taken on the same 1280×1024 monitor.


Changing the rendering resolution only alters fonts rendered by the desktop. For example, the Gnome terminal (Applications Image from book Accessories Image from book Terminal) changes size, but an xterm window will not. Also, some applications, such as xchat, use hard-coded window heights, so the tops or bottoms of large fonts may get cut off.

If you want to remove Grandpa Mode, you can use the gconftool-2 –unset option:

# reset default dpi

gconftool-2 -t float --set /desktop/gnome/font_rendering/dpi 96

# unset key mappings

gconftool-2 --unset /apps/metacity/keybinding_commands/command_7

gconftool-2 --unset /apps/metacity/keybinding_commands/command_8

gconftool-2 --unset /apps/metacity/global_keybindings/run_command_7

gconftool-2 --unset /apps/metacity/global_keybindings/run_command_8

Tuning the Shell

Most of these Ubuntu hacks require you to use the command line and that means using the GNU Bourne-Again Shell (bash). Bash has replaced shells such as sh, csh, and tcsh on most Linux distributions. When you open up a command prompt or terminal window, the default shell is bash. Bash uses the same syntax as the original Bourne Shell (sh) but has many more features for usability. For example, the up and down arrows can be used to scroll through the shell history.

Bash also enables you to edit command lines in either vi or emacs mode.

set -o vi # enable vi-mode

set -o emacs # enable emacs-mode

The default edit mode under Ubuntu is emacs. If you want to specify the mode (rather than inherit default settings), then add the appropriate set command to the end of your .bashrc file. This way, it will be set whenever you log in or open a new terminal window.


You can tell if Bash is using vi or emacs mode by running set -o (with no other options) or with echo $SHELLOPTS.

You may also want to change the default editor for applications by adding one of these lines to your $HOME/.bashrc file:

export EDITOR=vi # enable vi-mode

export EDITOR=emacs # enable emacs-mode

After modifying your $HOME/.bashrc file, you can reload it using: . $HOME/.bashrc. This is called dotting in because the command you are running is a single dot.

Completing Completion

Bash includes file completion so you don't need to type in very long file names. Pressing the Tab key fills out a partial file name with the available files. Pressing Tab twice lists the available files. For example, ls a<TAB> will complete the file name if there is only one file beginning with the letter a. And ls a<TAB><TAB> lists all files beginning with a. In these examples, if you do both <TAB> and <TAB><TAB>and see no file completion, then you have no file names that start with a.

Shell Games

There are a few topics in the computer field that can start passionate arguments on the scale of religious wars. The first topic is "Windows or Unix". Both sides have zealots who are willing to argue this topic to the death. (And anyone who is thinking "What about Macs?" knows the depth of this debate.) Even among Unix users, "Linux versus BSD" can spark heated debates. One of the most violent topics comes from the choice of editors. "Emacs vs. vi" can lead to real bloody battles.

When you need to help someone else at his terminal, it is a common courtesy to ask before switching modes. A vi user who finds himself in emacs mode can become very miffed. Instead, consider creating a subshell while you work. Running bash -o vi or bash -o emacs will open a shell in your favorite mode. Now you can change directories, set aliases, or edit the command line without interfering with the system's owner. When you're done, just exit and the shell closes. This leaves the owner's terminal unchanged.

Under Dapper Drake, the default .bashrc loads the file /etc/bash_completion. This file contains many other completion settings. For example, there is a special completion sequence for the command apt-get. Typing apt-get up<TAB><TAB> displays update and upgrade since those are the available options. This default file (/etc/bash_completion) contains options for many widely used commands.


Older versions of Ubuntu, such as Hoary and Breezy, have the file /etc/bash_completion on the system, but it is not loaded by default. The default .bashrc contains some commented- out lines for loading the completion file.

Awesome Aliases

Aliases are simple shortcuts for commonly typed commands. Usually people add aliases for the ls command to their .bashrc file. For example:

alias l.='ls -d .[a-zA-Z]*' # list all system files

alias ll='ls -l --color=tty' # list using "ls -l"

alias llf='ls -l --full-time' # list using the full timestamp

alias ls='ls -F' # make ls display the file type


If you don't want to use the command alias then preface the command with a backslash. For example ls uses the ls -F alias, but \ls will just run the regular command.

There are a couple of other aliases that I find useful. The first is telco. The telco alias enables me to quickly look up phone numbers. Simply give it the area code and prefix for a phone number in North America and it will provide you with the city, state, and rate center. This is a useful command when you begin to wonder, "What city has phone numbers that begin with 781-202?" (Burlington, Massachusetts).

$ alias telco='whois -h'

$ telco 781-202

Telcodata.US Whois Server


Your command was: 781-202

Data for 781 - 202 follows:

Area-code: 781

Exchange: 202

State: MA


Type: CLEC

Ratecenter: BURLINGTON

Switchtype: WECO 5ESS Host (Digital)



The telco alias logs your IP address. If you use it too often, then you will receive a warning about too much usage. The kind people who run do not want you to abuse their service.

Another alias that I use often is pinger. This alias simply displays a number every five seconds. Many firewalls assume that no traffic after a while indicates a closed connection. If I know I will be walking away from a live connection, I run the pinger alias just to generate traffic and keep the connection alive.

alias pinger='(i=0; while [ 1 ] ; do sleep 5 ; echo $i; ((i=$i+1)); done)'

Fun Functions

Besides storing aliases, the .bashrc file can store shell functions that can be used in place of commands. For example, pinger can also be written in the .bashrc file as:

function pinger() { i=0; while [ 1 ] ; do sleep 5 ; echo $i; ((i=$i+1)); done }

Another function that I use is ccd:

function ccd()


if [ "$1" == "" ] ; then

echo "Usage: ccd location"

elif [ -d "$1" ] ; then

# if it is a directory, go there

cd "$1"

else # must not be a directory

# Cut off filename and cd to containing directory

cd "${1%/*}"



The ccd function works similar to the cd command, except that it does not mind when you paste a line containing a file name. For example, cd ˜/public_html/logo.gif will fail because the target must be a directory. But ccd ˜/public_html/logo.gif will see that logo.gif is a file and place you in the same directory as the file.

Cool Commands

There are two other lesser-known features of Bash. Ctrl+T transposes the last two characters at the cursor. This way, the common typing error sl can be quickly corrected to ls. For other lesser-known Bash commands, see the Bash man page (man bash).

The other is the CDPATH environment variable. The PATH environment variable specifies where to look for commands. This usually looks something like:

export set PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games

When you run a command, the shell first checks if the program exists in /usr/local/bin, then in /usr/sbin, and then /usr/bin, and so on until it is found. The CDPATH variable tells cd where to look when the directory is not in your current directory. For example:

export set CDPATH=.:~:/usr:/etc

If you type cd bin, then the shell will first check for ./bin. If that does not exist, then it will try ˜/bin followed by /usr/bin and /etc/bin. This can come in really handy if you find yourself frequently trying to type cd public_html (or some other directory) when you're in the wrong location.

Using Ubuntu on a PC

The default installation of Ubuntu is optimized for a PC system, so there is not too much that requires modification. Most things that need tuning are physical devices, which are covered in Chapter 3. The only real PC-only functionality that may need tweaking is the use of Ctrl+Alt+Delete.

Trapping Ctrl+Alt+Delete

Different versions of Linux either have the Ctrl+Alt+Delete (CAD) key sequence enabled or disabled. In Ubuntu Dapper Drake, this key sequence is enabled, allowing a quick shutdown and reboot. However, the Gnome desktop intercepts CAD. To reboot, you need to switch to a text window (Ctrl+Alt+F1) and then press CAD.

Since the Gnome desktop intercepts CAD, you can remap this key sequence to run a different command. For example, to bring up the Gnome System Monitor, you can use:

gconftool-2 -t str --set /apps/metacity/global_keybindings/run_command_10 \


gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_10 \


The system monitor enables you to see the running processes and selectively kill applications. This is similar to using CAD under Microsoft Windows to bring up the System Monitor.

Unfortunately, the reboot command runs as root, so you cannot make CAD run /sbin/reboot. However, you can use gksudo (a graphical front-end to sudo) to prompt you for your password and then run reboot as root:

gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_10 \

"gksudo reboot"

Disabling Ctrl+Alt+Delete

Sometimes you may want to prevent CAD from rebooting the system. For example, a critical server may have CAD disabled to prevent someone from playing with the keyboard and cycling the system.

1. Edit the /etc/inittab file.

2. sudo vi /etc/inittab

3. Find the line that says:

4. ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now

This line says, for all init levels (1, 2, … 5), run the shutdown command and reboot now.

5. To disable CAD, comment out the line by inserting # at the beginning of the line.

6. #ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now

7. To alter the CAD action, change the /sbin/shutdown command to run your own program. For example, you may want to send an alert to an administrator or play some Disco music to let the user know that CAD is outdated.

8. ca:12345:ctrlaltdel:/usr/bin/play /home/nealk/disco.mp3 > /dev/null

Only one application can use the audio driver at a time, so this will only play music if nothing else is playing at the same time.

9. After changing the inittab file, reload it using: sudo telinit q.


Unmapped keyboard signals can be lost. If you disable CAD, then you may find that you cannot re-enable it without rebooting the system. But if you change the functionality (without disabling the command) then you do not need to reboot. The same is true for power level signals and Alt-UpArrow.

Tell Init

The init process is the parent of all processes. It manages different run levels and kicks off processes to start and stop at different run levels. For example, run-level 1 is a single-user mode. It contains a minimal number of running processes and is usually used to fix a broken system. Usually Ubuntu operates at run-level 2, supporting multiple users and graphics. To view the current and previous run level, use the runlevel command. (If there was no previous run-level, then N is displayed.)

If you want to tell init to change run levels, use the telinit command. For example, to switch from the current run level to single-user mode, change to a text window (Ctrl+Alt+F1) and use sudo telinit 1 or sudo telinit s.

Running the shutdown command is similar to running sudo telinit 0. Run-level 0 is the shutdown mode.

The file /etc/inittab tells init what to do at each run level, what processes to spawn, and how to handle hardware signals such as Ctrl+Alt+Delete, Alt+UpArrow, and power modes such as low battery. After modifying the inittab, you will need to tell init to re-examine (query) the file. This is done using telinit q.

Tuning Ubuntu on a Macintosh

Many versions of Linux have been ported to the Macintosh, but were not initially developed for the Mac. Ubuntu falls into this category. There are some PC features that do not exist on the Mac, and some Mac features that lack support under Ubuntu.

Using a One-Button Mouse in a Three-Button World

The biggest issue with using Ubuntu on a Mac is the lack of a three-button mouse. Under Ubuntu, the left mouse button performs actions, such as selecting icons and moving windows. The right button brings up menus, and the middle button is used for pasting from the clipboard.


There are many different types of PC mice. Some only have two-buttons. The middle button can be emulated by clicking both the right and left buttons at the same time. Others have a scroll wheel that can be pushed in for use as the third button.

Most Macs include a one-button mouse. If you swap the one-button mouse for a three-button model, it will work fine with Ubuntu, but if you do not want to swap hardware then how do you use the other buttons?

By default, Ubuntu on the Mac (PowerPC platform) maps the F11 and F12 keys to the middle and right mouse buttons. This is configured in the /etc/sysctl.conffile. On the PowerPC installation, this file contains the following additional lines:

# Emulate the middle mouse button with F11 and the right with F12.

dev/mac_hid/mouse_button_emulation = 1

dev/mac_hid/mouse_button2_keycode = 87

dev/mac_hid/mouse_button3_keycode = 88

On a Mac, keycode 87 is F11 and keycode 88 is F12. One easy way to see the keycodes is to use the X Event Tester (xev). This tool opens a window and displays all X-Windows events. If you type in the window, it displays the keycodes (see Figure 2-4).

Image from book
Figure 2-4: The xev application, showing keycodes

Missing Keys and Functionality

PC keyboards usually do not look like Mac keyboards. The standard 104-key PC keyboard contains many extra keys that are not found on a Mac. Some of the missing keys include:

§ Two Alt keys-Every Mac keyboard has a left Alt key (Alt_L), but the right Alt key (Alt_R) is not always present.

§ Two Ctrl keys-While Control_L always exists, Control_R may be missing on some Mac keyboards.

§ System keys-The Print Screen, System Request, Scroll Lock, Pause, and Break keys are missing. With Ubuntu on a PC, the Print Screen button is mapped to the screen capture application. Without this button, you will need to remap the functionality to another key combination.

§ Edit keys-Most PC keyboards have a set of keys for Insert, Delete, Home, End, Page Up, and Page Down. Although Mac keyboards do have Page Up and Page Down, they are not in the same location as a PC keyboard. Different Mac keyboards can have very different keys. For example, the Mac iBook G4 has Home/End while the older iMac G3 keyboard has Home and Help (where Help generates the same keycode as Insert).

§ Windows keys-The Windows keys do not exist on the Mac keyboard (for obvious reasons). Similarly, the Menu key does not exist.

§ Numeric keyboard-Although they are usually not labeled on the Mac, the numeric keyboard does have the same arrows (KP_Up, KP_Down, KP_Left, and KP_Right) and navigation keys as a standard PC keyboard.

Keys Please

Under X-Windows, there are two types of keyboard values: keycodes and keysyms. Keycodes are the actual numeric representation sent by the keyboard when a key or button is pressed. Keysyms are the values assigned to the keycodes. For example, when you press the A key on the keyboard, it generates keycode 73. Keycode 73 is mapped to the keyboard symbol 0x61 (the letter a). The command xmodmap -pk prints the key table, showing the keycode to keysym mapping. For example, here's a section of output from the key table:

55 0xff6a (Help) 0xff6a (Help)

56 0x0030 (0) 0x0029 (parenright)

57 0x0031 (1) 0x0021 (exclam)

58 0x0032 (2) 0x0040 (at)

59 0x0033 (3) 0x0023 (numbersign)

This key table shows that keycode 56 is mapped to the number zero, and when shifted, maps to a right parenthesis.

It is important to realize that not every keyboard can generate every keycode, and not every keycode is mapped to a keysym. Other input devices also generate key- codes. For example, the mouse buttons are keycodes 1, 2, and 3 (for left, middle, and right). Using xmodmap, you can change the key table, swapping keyboard keys or mouse buttons. For example, the command xmodmap -e "pointer = 3 2 1" flips the mouse buttons for a left-handed mouse.


Many Mac keyboards do not have an indicator light to show when NumLock is enabled, so switching between keypad navigation and numeric entry can be confusing.

There are also some keys that exist on the Mac but not on the PC. By default, these are not mapped to anything but can be mapped to other keys.

§ Command keys-On a Macintosh keyboard, there is at least one and possibly two Command keys. These are located on either side of the spacebar and are labeled with a flower pattern. Mac users call this the Apple or Command key. Under Linux, it is called the Super key. The left one (Super_L) is mapped to keycode 115 and the right key (Super_R) is keycode 116.

§ Keypad Equal (=)-On a standard PC keyboard, there is no equal sign on the numeric keypad, but there is one on the Mac. This key is mapped to keycode 157 but is not mapped to any keyboard value.


Specialized keyboards may have additional keys, such as volume up/down, power, and even buttons labeled for e-mail or the Web. Each generates a distinct keycode. Use xev to see the codes. This way, you can map the keys to functions.

Changing Keyboard Layouts

By default, Ubuntu configures itself for a generic 104 key PC keyboard. This is not the same layout as a Macintosh. You can change the configuration to match your keyboard by choosing System Image from book Preferences Image from book Keyboard. This brings up the keyboard configuration applet. From here, you can change the keyboard model to match your Mac keyboard (see Figure 2-5). Changing the keyboard layout changes the keycode to keysym map.

Image from book
Figure 2-5: The Keyboard Preferences applet for changing keyboard models.

Remapping the Command and Alt Keys

One of the biggest distinctions for Mac users is the use of the Command key. Under Mac OS, this key modifier is used for most shortcuts. For example, Command+W closes the window and Command+Q quits an application. Under Ubuntu and on most PC operating systems, the Ctrl key has the same usage (for example, Ctrl+W instead of Command+W). Adding to the confusion, a PC keyboard has Alt next to the spacebar whereas a Mac has the Command keys in that location. What this means is that a Mac user running Ubuntu will need to relearn how to use the Alt key instead of the Command key, or you can just remap the keys.

To remap the Command keys to act as Alt keys, use the xmodmap program. This program maintains a list of keycode mappings (xmodmap -pke) and special key modifiers such as Shift and Control (xmodmap -pm). The easiest way to remap the Command keys is to simply change the modifiers. First, remove the control and command keys, then add the Ctrl keys again.

xmodmap -e "remove control = Control_L Control_R" # unmap control

xmodmap -e "remove mod4 = Super_L Super_R" # unmap old Super keys

xmodmap -e "add control = Super_L Super_R" # map Super to Ctrl

Alternately, you could actually re-map the Command and Ctrl keys.

xmodmap -e "remove control = Control_L Control_R" # unmap Ctrl

xmodmap -e "remove mod4 = Super_L Super_R" # unmap super

xmodmap -e "keycode 115 = Control_L Control_L" # map Command to Alt

xmodmap -e "keycode 116 = Control_R Control_R" # map Command to Alt

xmodmap -e "keycode 64 = Super_L Super_L" # map Alt to Command

xmodmap -e "keycode 113 = Super_R Super_R" # map Alt to Command

xmodmap -e "add control = Control_L Control_R" # map new Ctrl to Ctrl

Mapping the keyboard using xmodmap only creates a temporary change. If you reboot the computer or restart the X-server, the changes will be lost. To make the changes permanent, you can put the changes in ˜/.Xmodmap or /usr/X11/xinit/Xmodmap-depending on whether you want the changes to be per user or system wide:

remove control = Control_L Control_R

keycode 115 = Super_L Super_L

keycode 116 = Super_R Super_R

remove mod4 = Super_L

add control = Super_L Super_R


After creating .xmodmap, you can test it using the command xmodmap .xmodmap. Be sure to correct any errors, otherwise the configuration will not load.


Under the default Dapper installation, Super_R is not defined, so you will need to map it to key- code 116 before using it. Also, Super_L is part of the mod4 modifiers. Use xmodmap -pm to display the modifier list.

Instead of adding the mappings to the .xmodmap file, you can add the xmodmap commands to ˜/.xinitrc or /etc/X11/xinit/xinirrc. You might want to do this anyway since Gnome will prompt you about using a new .xmodmap file each time you log in.

Shas't Nos Funny!

There's an old practical joke where people pry the keys off a keyboard and replace them out of order. A user who is not a touch typist can get really confused when they look for the right letters. The xmodmapcommand can be used for an excellent alternative to this prank. For example, you can add the following commands into the victim's .bashrc file:

xmodmap -e 'keycode 91 = t T'

xmodmap -e 'keycode 92 = s S'

This prank swaps the s and t keys on the keyboard without requiring any physical keyboard modifications. Alternately, if you can pry up and replace the keys on the keyboard, follow it up by remapping the keyboard so it actually works correctly! Your coworkers should get a big laugh out of it!

Tweaking GDM

After you configure the color scheme, fonts, and sound, you'll want to modify the Gnome Desktop Manager (GDM). This means adding buttons and menus for frequently used functions. You may also want to change the overall widget theme and application skins.

Adding a Prompt Button

The toolbars at the top and bottom of the screen are convenient locations for buttons. Buttons give you a quick way to run commands. I usually add buttons for X-terminals since I regularly use the command- line. I also add a button for my favorite games and other applications that I regularly run.

Adding a new button is pretty straightforward: just right-click an empty part of either the top or bottom toolbar and select Add to Panel. The default applet shows you common applications, like Weather Report and Workspace Switcher, that you can drag and drop onto the toolbar. But when the application you want does not exist, you'll need to select the Custom Application Launcher (see Figure 2-6).

Image from book
Figure 2-6: Add to Panel and Custom Application Launcher.

The Custom Application Launcher prompts you for the button's name. This is the text that will appear when you hover the mouse over the button. The command is the active element- this is any line that you can enter on a command line. In Figure 2-6, I created an xterm with a dark blue (#000020) background and white text. Rather than opening a terminal on the local system, I use Secure Shell (ssh) to connect to a remote host. This way, I can color-code my terminal windows to specific hosts. You should also choose an icon for your button. Clicking the empty icon button shows you a large system of ready-to-go icons. Alternately, you can create your own; PNG is the best format for this.


The command can be any valid command line. If you want to use pipes or redirection, consider using the bash command. For example: bash -c ‘ls | more’. And be sure to select the Run in Terminal check box. This way, the command will run and you can see the output. Otherwise, it will never open a window (which could be desirable if you don't want to see any text results).

After you create your button, it will appear on the toolbar. You can always right-click the button to change its properties (name, command, icon, and so on). This menu also enables you to move the button along the toolbar, to another toolbar (for example, from the top to the bottom), or to lock the icon so it won't move.


Laptop computers can either use their own screen or an external monitor. These screens are commonly at different resolutions. If you find yourself switching resolutions often, you might want to enable the Lock To Panel property for each toolbar icon. Otherwise, they can move around and get bunched up in the middle of the toolbar.

Buttons are not restricted to the toolbars. They can also be created on the desktop.

1. On the desktop, right-click to bring up the desktop menu.

2. Select Create Launcher. The applet looks just like the menu from the panels.

3. Enter a name and command line for the button, then click OK.

The new button appears on your desktop. Double-clicking the button starts the application.

Adding Panels

The default Dapper user interface has two panels (toolbars): one at the top and one at the bottom. Each panel has a limited amount of screen real estate for placing menus and buttons; you might run out of room. To resolve this, you can add more panels.

1. Right-click any empty space on any panel. This brings up the panel menu.

2. Select New Panel from the menu. A new panel appears.

To move the panel, grab it with the mouse (left button) and move it. It can reside along any screen edge or along another panel. For example, you can easily stack multiple panels along the top edge for adding lots of buttons.

Panels can also have sub-panels, called drawers. A drawer is a pop-out panel where you can store more buttons. To create a drawer:

1. Right-click any empty portion of the panel to bring up the panel menu.

2. Select the Add to Panel option. This brings up the list of available applets.

3. Drag the drawer icon from the Add to Panel window to the panel.

4. Left-click the drawer icon in order to open the drawer.

Clicking the drawer icon once opens it. Clicking it a second time closes it. The open drawer acts as another panel and you can add panel icons to it. The opened drawer is normally three icons tall. However, if you add more icons to add then it will automatically widen.

Adding Menus

The default Dapper desktop contains one text-based top-level menu with three items in it: Applications, Places, and System. In Gnome2, the developers removed the ability to add custom menus. The implementation was reportedly unstable, so it was disabled. Hopefully this will be added to a later version of Ubuntu. In the meantime, you can add custom menus under the existing top-level menu items (Applications and System) by right-clicking the menu and selecting Edit Menus.


Other menu items were also disabled in Dapper. For example, earlier Ubuntu versions had an Applications Image from book System Tools menu for running the Configuration Editor (gconf-edit) and other tools. That menu option is gone under a Dapper clean install, but present if you performed an upgrade to Dapper.

The Gnome Menu items contain can contain two different types of items. First, they can contain additional menus. For example, Applications contains Games (Applications Image from book Games). There is no limit to the depth of the menus; you can have a menu in a menu in a menu.

Second, the Gnome Menu can contain entries. An entry is just like a button but uses text (with an optional graphic) to denote the functionality; buttons only have graphics. Like buttons, entries can run any command on the system.


Only menu items that contain items are displayed, and only empty menus can be deleted.

Although adding menu items is easy, deleting them can be a little confusing (the Gnome interface is not exactly production quality in this regard).

1. Open the Alacarte Menu Editor by right-clicking the menu and selecting Edit Menu.

2. Delete all menu entries by right-clicking each item and selecting Delete. You can delete menus that started off empty, but you cannot delete a menu that you just emptied. The next steps (Steps 3–5) are for deleting an empty menu.

3. Close the Alacarte Menu Editor. (That's right! Close it so you can open it again to delete it!) The menu editor does not update and realize that the menu is empty until you close it.

4. Re-open the Alacarte Menu Editor.

5. Now delete the empty menu by right-clicking and selecting Delete.

Selecting Themes and Skins

The default widgets-window borders, scrollbars, and buttons-that ship with Ubuntu are nice, but they can be made nicer. You can change the desktop theme by selecting System Image from book Preferences Image from book Theme. From this menu you can select from a wide range of themes, color schemes, and control widgets (see Figure 2-7). Besides the existing theme selections, you can choose Theme Details, and modify the controls, window borders, and icons. Your choices here will impact every application you run.

Image from book
Figure 2-7: The Theme Preferences and Theme Details applets

Unfortunately, the theme selection applet does not give as many options as other operating systems. (Yes, Windows and even OS/2 both have more options than Gnome.) But there are ways to tweak aspects that are not available from the Theme Preferences applet.

1. Become root. Open a terminal and type:

2. sudo bash


This starts a command prompt as root. You have the ability to delete and destroy the entire operating system. Be careful and have fun.

3. Go to the directory /usr/share/themes.

4. cd /usr/share/themes

5. Copy your favorite theme to a new directory. This uses the existing theme as a template. For example, if you like the AgingGorilla theme, then use:

6. cp -R AgingGorilla MyTheme

7. In the MyTheme/metacity-1 directory is a file called metacity-theme-1.xml (/usr/share/themes/MyTheme/metacity-1/metacity-theme-1.xml). Edit this file.

The theme file contains XML that describes the theme's name and attributes. By editing this file, you can change colors, border sizes, icons, and more. And this gives you much more editing power for creating a custom theme than other operating systems. As an example, I can set the title bar to have a couple of bars that grow by editing the "title_tile" section and specifying:

<draw_ops name="title_tile">

<line color="#0000ff" x1="0" y1="0" x2="width" y2="0"/>

<line color="#3030f0" x1="0" y1="1" x2="width" y2="1"/>


This sets two alternating horizontal stripes. One is blue (#0000ff) and the other is a grayish blue (#3030f0). If you want to change the icons for close, maximize, minimize, and so on, then you just need to edit the appropriate PNG file in the /usr/share/themes/MyTheme/metacity-1/ directory.

Changes to the active theme take effect immediately. If you change the theme using the System Image from book Preferences Image from book Theme applet, you can re-select your customizations by clicking the Theme Details button and choosing the MyTheme selection.

Navigating Nautilus

Nautilus is the graphical file browser that is incorporated into the GDM. It enables you to graphically view files, navigate directories, and start applications when you double-click icons. Besides appearing as a window with files in it, Nautilus also manages the desktop-the desktop is just another file folder that is open all the way.

You can create a button to quickly start Nautilus.

1. On the top panel, right-click to bring up the menu.

2. Select Add to Panel. This brings up the applet.

3. Click the Custom Application Launcher button.

4. Type Nautilus for the Name field of the button and nautilus (lowercase) as the Command field.

5. Choose an appropriate icon. I like /usr/share/icons/gnome/48x48/apps/ file-manager.tif.

6. Click OK to create the button.

Nautilus has many configurable items that are similar to other operating systems. For example, you can set the background in the Nautilus window to be a color or an image. There is also a side pane (View Image from book Side Pane) that can list information about the directory or show the directory tree.

Beyond these common features, Nautilus offers a few uncommon features, such as emblems, scalable icons, and customized context-sensitive menus.

Configuring Nautilus

Even though Gnome uses Nautilus for directory navigation, Nautilus is not the same as Gnome. This distinction becomes very apparent when customizing the system. Gnome uses gconf-edit to manage all configuration options. Any changes to this registry take effect immediately.

In contrast to Gnome, Nautilus uses a variety of configuration methods, and few take place immediately. To tell Nautilus about changes, your best option is to restart it. The command nautilus -q tells Nautilus to quit. Gnome will immediately restart Nautilus and new configuration changes will be loaded.

Embracing Emblems

Emblems are small accents that you can add to a file or directory icon. For example, critical files can all be tagged with an urgent emblem, and finance files can have a dollar sign on them. Emblems appear at the corner of the icons, so you can easily enable four emblems per file. There are many different ways to enable emblems:

§ Properties-You can right-click an icon and select the properties option. In the properties menu, select the Emblem tab. This enables you to put a check box next to the desired emblem. If you select multiple files, then you can view the preferences and enable all of their emblems at once.

§ Backgrounds and Emblems-Under the Nautilus menu you can select Edit Image from book Background and Emblems. A dialog box appears that enables you to drag over a different background (patterns or colors) for the Nautilus window. There are also icon emblems. Just drag the emblem onto the file icon and it will be set (or if it is already set, then it will be removed).

§ Side Pane-The Nautilus side pane has a pull-down menu that displays the emblems. As with Backgrounds and Emblems, you can drag the emblem onto an icon in order to enable or disable it.

Emblems provide visual cues about a file's purpose, but they have little use beyond human interpretation right now. You cannot automatically select files or sort them by emblem, enable emblems on subdirectories, or programmatically change them (without some serious programming efforts).

Practical Uses for Emblems

Although emblems have no functionality beyond human interpretation, they can be very usable. For example, I have a friend who is a freelance writer. She uses emblems to denote the status of different articles. One emblem denotes a submission, a different emblem marks a sale, and a third indicates work in progress.

Technical Details

Emblems are stored in a set of XML files found in $HOME/.nautilus/metafiles/. There is one XML file per directory, and only customized directories are present. You can edit these XML files to make system changes, but you'll need to restart Nautilus for the change to take effect.

Listing 2-2 shows a simple script for changing emblems. In this case, every icon with the "special" emblem is changed to have an "important" emblem. More complicated scripts can be created to add emblems to specific files, manipulate files according to their emblems, or to set an emblem based on an event. For example, if an "important" icon is more than 48 hours old, then it can be set to "urgent" or an e-mail notification can be generated.

Listing 2-2: Script to Change "Special" Emblems to "Important"


# for each of the meta files...

for i in ~/.nautilus/metafiles/* ; do

# convert any "important" emblems to "special"

# save the result to a temporary file

cat $i | sed -e 's/name="special"/name="important"/g'

# move the temporary file into place

mv $ $i


# restart Nautilus so the changes take effect

nautilus -q

Stretching Icons

One of the coolest features in Nautilus is the ability to stretch icons. You can take any icon on the desktop and scale it-making it very tiny or extremely large. If the icon denotes a text document, then it can be scaled large enough to read the text. To scale an image:

1. For desktop icons, right-click the icon to bring up the icon's menu and select the Stretch Icon menu option. For icons within a Nautilus directory window, select the icon and choose Edit Image from book Stretch Icon from the Nautilus menu bar.

2. When you select Stretch Icon, four blue dots appear on the corners of the icon.

3. Grab one of the blue dots (left mouse button) and drag it. This will stretch or shrink the icon.

One great use for stretching icons is to uncluttered desktops. When I'm working on a project, I may end up having dozens of files on my desktop. Before going to lunch, I can stretch the current file to a larger size-this way, I can find it quickly when I get back. Figure 2-8 shows a couple of stretched icons.

Image from book
Figure 2-8: A regular icon, a shrunk icon, and a stretched icon that is very large. Icons for text files show the file contents.

To un-stretch the icon, bring up the icon's menu and select Restore Icon's Original Size.


Under the previous section on emblems, I mentioned that you can have up to four emblems per icon. That isn't exactly true. Emblems are placed along the sides of the icon. If the icon is stretched to a very large size, then you can have lots of emblems without any overlap.

Stretching Exercises

Stretching icons is still a work in progress for Nautilus. As a result, not all options are available all the time. For example, when an icon is on the desktop, there is a menu option to stretch the icon's size. But the stretch option is not on the icon menu when an icon is selected from inside a Nautilus window. Instead, you need to use Edit Image from book Stretch Icon.

Nautilus enables you to drag and drop a stretched icon into a folder-and it will retain the stretched dimensions. Unfortunately, dragging an icon will lose the Restore Icon's Original Size menu entry. In order to restore the icon's size, you will need to first stretch the icon. This reactivates the Restore menu option.

Technical Details

As with emblems, the stretched value is saved in the $HOME/.nautilus/metafiles/ XML files. Scripts can be created to manipulate the size of the icons. This way, important files can be suddenly made larger. A great use, for example, would be to increase an icon in size right before an online meeting. This way, you can quickly find the files you need. As with emblems, any automated changes to the stretched value require you to restart Nautilus before they take effect.

Adjusting Fonts

The System Image from book Preferences Image from book Font applet enables you to set font types, but not font colors. The default font uses white lettering with a dark shadow. Although this font is nice when you have a dark (or brown) background, it can appear hard to read with a light colored background. Changing the font color under Nautilus requires a $HOME/.gtkrc-2.0 file. In your favorite editor, create (or edit) this file. Add or modify a section for desktop icons:

style "desktop-icon"


NautilusIconContainer::frame_text = 1

NautilusIconContainer::normal_alpha = 0

text[NORMAL] = "#000000"


class "GtkWidget" style "desktop-icon"

In this example, the font color is set to black (#000000). The frame_text value indicates that you are specifying the text format, and the normal_alpha field controls the transparency of the font (0 is not transparent). After editing the file, you can save your changes and restart Nautilus (nautilus -q). The text color matches your setting and (hopefully) offers a higher contrast than the default colors.

Tuning Templates

On the Nautilus menu (including the desktop menu) is an option called Create Document. The Create Document option enables you to make a new file that is based on a template file. By default, there is only one template file-empty file. This creates a zero-sized file just as if you ran the touch command. You can create your own templates by placing files in a Templates directory.

1. Create a directory called Templates in your home directory.

2. mkdir $HOME/Templates

3. Place any template file in this directory. The template file name (without extension) appears in the Create Document menu.

For example, you can start up Open Office (Applications Image from book Office Image from book Word Processor) and save an empty file called WordProcessor.doc in the Templates directory. As soon as you save it, you will have the WordProcessor option listed in the Create Document menu. Selecting this menu option creates a file called WordProcessor.doc, with the name WordProcessor highlighted for renaming.

Scripting Menus

The single most powerful feature of Nautilus is the ability to create context-sensitive menu items. For example, you can create a special menu item that only appears when you select an MP3 file. You can control the types of files, directories, or both where the menu item appears, and whether it appears for one selected item or multiple selections.

To do this, you will first need to install Nautilus Actions:

sudo apt-get install nautilus-actions

Now you can create custom menus. For example, here's a menu for securely deleting files (see Figure 2-9).

1. Run nautilus-actions-config. This brings up the configuration menu.


You can also find this tool on the top menu under System Image from book Preferences Image from book Nautilus Actions Configuration.

2. Click Add to create a new menu script. A new window opens.

3. For the label, enter Secure Delete. This is the text that will appear on the menu.

4. You can add a tooltip, such as Safely delete files. This appears when the mouse hovers over the menu item.

5. Optionally select an icon for this menu item. The icon appears in the menu.

6. Path is the name of the command that is called. Any command that can be used on the command line can be entered here. For this command, use find (or /usr/bin/find) as the Path.

7. The Parameters will be passed to the Path command. An example of the command line is displayed below the Parameter field as you type. The %M parameter becomes the list of selected files. If you click the Legend button, you will see other variables that can be used. For this action, use %M -type f -exec shred -u -f ‘{}‘ \;. This parameter tells find to identify all files and run the command shred, forcefully unlinking (deleting) each file.


The shred command does not securely delete directories. The result is a bunch of empty directories. See the Shred Everything entry in Table 2-1 for a solution to this problem.

Table 2-1: Examples of Useful Custom Menu Commands
Open table as spreadsheet






Command Prompt

. gnometerminal

g–working- directory ='%M'

Folders only, no multiple selection.

Opens a terminal window.

Open in Firefox

. firefox


Both files and folders, no multiple selection

. Keep in mind, Firefox must be opened first; otherwise, nothing may happen.

Shred Everything

. bash

-c ‘find %M -type f -exec shred -u -f "{}" \; ; rm -rf %M’

Files or folders with multiple selection.

. Although we can only run one command, this only runs the bash command. The bash argument translates as two commands.

Convert WAV to MP3

. bash

-c "lame %M %d/‘basename %M .wav‘.mp3"

Only for files ending in .wav, no multiple selections.

. This converts file.wav to file.mp3 in the same directory. Instead of running lame directly, bash is used to create the subshell basename (used to rename the file). Without the basename program, you would end up with a file named file.wav.mp3.

9. Under the Conditions tab, you can select when the menu item appears. You can make the selections based on any combination of file name, Meta type, file or directory, and single or multiple selections. In this case, you should select both files and directories (even through directories are not deleted), and allow multiple file selections.

10. Click OK to add the action to the menu.

11. Click Close to end the Nautilus action application.

12. Restart Nautilus so it loads the new menu item.

Image from book
Figure 2-9: Adding the Secure Delete menu item

nautilus -q

You can test the new menu item by bringing up Nautilus and deleting something. Right-clicking an icon should show the Secure Delete menu item. Selecting the delete item should remove all of the files selected.


Shred overwrites files with garbage before deleting. There is no prompt to check if this is what you really want to do, and anything deleted will be gone forever.

Action scripts can be used to convert files (for example, for any GIF, make it a PNG), e-mail a file (such as a Mail To command), or view a file under the web browser. Actions are limited to one command line, but you can specify a shell script for performing multiple-step commands. Some useful custom menu items are included in Table 2-1.


Only one command can be specified. You cannot use &, |, or ; to join multiple commands.


Some commands, such as shred and lame, may take minutes (or longer) to process very large files. During this time, result files (for example, Convert WAV to MP3 files) may be created even though they are incomplete. Requesting the same task multiple times may result in many running processes, that all interfere with each other. The net result may not be what you want. Instead, wait for the task to finish. See Chapter 7 for different ways to monitor processes.


Just as a home isn't a home until you add furniture, the default installation of Ubuntu is fine for short-term use, but to really be usable over the longer term, it needs to be customized. Changing the background, sounds, fonts, buttons, and themes enables you to configure the desktop for whatever is best for you. For some users, static environments are best, but the user interface supports scripts that can dynamically change the desktop based on the current environment. Informative backgrounds, changing icons, smart menus, and adjustable fonts can really add to the system's usability and your own productivity.