Sunday, September 27, 2009

Linux : A typical set of setup files

A typical set of setup files
7.2.3.1. /etc/profile example
Let's look at some of these config files. First /etc/profile is read, in which important variables such as
PATH, USER and HOSTNAME are set:
debby:~> cat /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# Path manipulation
if [ `id -u` = 0 ] && ! echo $PATH | /bin/grep -q "/sbin" ; then
PATH=/sbin:$PATH
fi
if [ `id -u` = 0 ] && ! echo $PATH | /bin/grep -q "/usr/sbin" ; then
PATH=/usr/sbin:$PATH
fi
if [ `id -u` = 0 ] && ! echo $PATH | /bin/grep -q "/usr/local/sbin"
then
PATH=/usr/local/sbin:$PATH
fi
if ! echo $PATH | /bin/grep -q "/usr/X11R6/bin" ; then
PATH="$PATH:/usr/X11R6/bin"
fi
These lines check the path to set: if root opens a shell (user ID 0), it is checked that /sbin, /usr/sbin and
/usr/local/sbin are in the path. If not, they are added. It is checked for everyone that
/usr/X11R6/bin is in the path.
# No core files by default
ulimit -S -c 0 > /dev/null 2>&1
All trash goes to /dev/null if the user doesn't change this setting.
USER=`id -un`
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
HOSTNAME=`/bin/hostname`
HISTSIZE=1000
Here general variables are assigned their proper values.
if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
INPUTRC=/etc/inputrc
fi
If the variable INPUTRC is not set, and there is no .inputrc in the user's home directory, then the default
input control file is loaded.
Introduction to Linux
Chapter 7. Home sweet /home 117
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
All variables are exported, so that they are available to other programs requesting information about your
environment.
7.2.3.2. The profile.d directory
for i in /etc/profile.d/*.sh ; do
if [ -r $i ]; then
. $i
fi
done
unset i
All readable shell scripts from the /etc/profile.d directory are read and executed. These do things like
enabling color-ls, aliasing vi to vim, setting locales etc. The temporary variable i is unset to prevent it from
disturbing shell behavior later on.
7.2.3.3. .bash_profile example
Then bash looks for a .bash_profile in the user's home directory:
debby:~> cat .bash_profile
#################################################################
# #
# .bash_profile file #
# #
# Executed from the bash shell when you log in. #
# #
#################################################################
source ~/.bashrc
source ~/.bash_login
This very straight forward file instructs your shell to first read ~/.bashrc and then ~/.bash_login.
You will encounter the source built-in shell command regularly when working in a shell environment: it is
used to apply configuration changes to the current environment.
7.2.3.4. .bash_login example
The ~/.bash_login file defines default file protection by setting the umask value, see Section 3.4.2.2.
The ~/.bashrc file is used to define a bunch of user-specific aliases and functions and personal
environment variables. It first reads /etc/bashrc, which describes the default prompt (PS1) and the
default umask value. After that, you can add your own settings. If no ~/.bashrc exists, /etc/bashrc is
read by default.
7.2.3.5. /etc/bashrc example
Your /etc/bashrc file might look like this:
debby:~> cat /etc/bashrc
# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# by default, we want this to get set.
Introduction to Linux
Chapter 7. Home sweet /home 118
# Even for non-interactive, non-login shells.
if [ `id -gn` = `id -un` -a `id -u` -gt 99 ]; then
umask 002
else
umask 022
fi
These lines set the umask value. Then, depending on the type of shell, the prompt is set:
# are we an interactive shell?
if [ "$PS1" ]; then
if [ -x /usr/bin/tput ]; then
if [ "x`tput kbs`" != "x" ]; then
# We can't do this with "dumb" terminal
stty erase `tput kbs`
elif [ -x /usr/bin/wc ]; then
if [ "`tput kbs|wc -c `" -gt 0 ]; then
# We can't do this with "dumb" terminal
stty erase `tput kbs`
fi
fi
fi
case $TERM in
xterm*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
else
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:\
${PWD/$HOME/~}\007"'
fi
;;
*)
[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=\
/etc/sysconfig/bash-prompt-default
;;
esac
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
if [ "x$SHLVL" != "x1" ]; then # We're not a login shell
for i in /etc/profile.d/*.sh; do
if [ -x $i ]; then
. $i
fi
done
fi
fi
7.2.3.6. .bash_logout example
Upon logout, the commands in ~/.bash_logout are executed, which can for instance clear the terminal,
so that you have a clean window upon logging out of a remote session, or upon leaving the system console:
debby:~> cat .bash_logout
# ~/.bash_logout
clear
Let's take a closer look at how these scripts work in the next section. Keep info bash close at hand.
Introduction to Linux
Chapter 7. Home sweet /home 119
7.2.4. The Bash prompt
7.2.4.1. Introduction
The Bash prompt can do much more than displaying such simple information as your user name, the name of
your machine and some indication about the present working directory. We can add other information such as
the current date and time, number of connected users etc.
Before we begin, however, we will save our current prompt in another environment variable:
[jerry@nowhere jerry]$ MYPROMPT=$PS1
[jerry@nowhere jerry]$ echo $MYPROMPT
[\u@\h \W]\$
[jerry@nowhere jerry]$
When we change the prompt now, for example by issuing the command PS1="->", we can always get our
original prompt back with the command PS1=$MYPROMPT. You will, of course, also get it back when you
reconnect, as long as you just fiddle with the prompt on the command line and avoid putting it in a shell
configuration file.
7.2.4.2. Some examples
In order to understand these prompts and the escape sequences used, we refer to the Bash Info or man pages.
export PS1="[\t \j] "
Displays time of day and number of running jobs
·
export PS1="[\d][\u@\h \w] : "
Displays date, user name, host name and current working directory. Note that \W displays only base
names of the present working directory.
·
export PS1="{\!} "
Displays history number for each command.
·
export PS1="\[\033[1;35m\]\u@\h\[\033[0m\] "
Displays user@host in pink.
·
export PS1="\[\033[1;35m\]\u\[\033[0m\] \[\033[1;34m\]\w\[\033[0m\] "
Sets the user name in pink and the present working directory in blue.
·
export PS1="\[\033[1;44m\]$USER is in \w\[\033[0m\] "
Prompt for people who have difficulties seeing the difference between the prompt and what they type.
·
export PS1="\[\033[4;34m\]\u@\h \w \[\033[0m\]"
Underlined prompt.
·
export PS1="\[\033[7;34m\]\u@\h \w \[\033[0m\] "
White characters on a blue background.
·
export PS1="\[\· "\[\033[3;35m\]\u@\h \w \[\033[0m\]\a"
Introduction to Linux
Chapter 7. Home sweet /home 120
Pink prompt in a lighter font that alerts you when your commands have finished.
· export PS1=...
Variables are exported so the subsequently executed commands will also know about the environment. The
prompt configuration line that you want is best put in your shell configuration file, ~/.bashrc.
If you want, prompts can execute shell scripts and behave different under different conditions. You can even
have the prompt play a tune every time you issue a command, although this gets boring pretty soon. More
information can be found in the Bash-Prompt HOWTO.
7.2.5. Shell scripts
7.2.5.1. What are scripts?
A shell script is, as we saw in the shell configuration examples, a text file containing shell commands. When
such a file is used as the first non-option argument when invoking Bash, and neither the -c nor -s option is
supplied, Bash reads and executes commands from the file, then exits. This mode of operation creates a
non-interactive shell. When Bash runs a shell script, it sets the special parameter 0 to the name of the file,
rather than the name of the shell, and the positional parameters (everything following the name of the script)
are set to the remaining arguments, if any are given. If no additional arguments are supplied, the positional
parameters are unset.
A shell script may be made executable by using the chmod command to turn on the execute bit. When Bash
finds such a file while searching the PATH for a command, it spawns a sub-shell to execute it. In other words,
executing
filename ARGUMENTS
is equivalent to executing
bash filename ARGUMENTS
if "filename" is an executable shell script. This sub-shell reinitializes itself, so that the effect is as if a new
shell had been invoked to interpret the script, with the exception that the locations of commands remembered
by the parent (see hash in the Info pages) are retained by the child.
Most versions of UNIX make this a part of the operating system's command execution mechanism. If the first
line of a script begins with the two characters "#!", the remainder of the line specifies an interpreter for the
program. Thus, you can specify bash, awk, perl or some other interpreter or shell and write the rest of the
script file in that language.
The arguments to the interpreter consist of a single optional argument following the interpreter name on the
first line of the script file, followed by the name of the script file, followed by the rest of the arguments. Bash
will perform this action on operating systems that do not handle it themselves.
Bash scripts often begin with
#! /bin/bash
(assuming that Bash has been installed in /bin), since this ensures that Bash will be used to interpret the
script, even if it is executed under another shell.
Introduction to Linux
Chapter 7. Home sweet /home 121
7.2.5.2. Some simple examples
A very simple script consisting of only one command, that says hello to the user executing it:
[jerry@nowhere ~] cat hello.sh
#!/bin/bash
echo "Hello $USER"
The script actually consists of only one command, echo, which uses the value of ($) the USER environment
variable to print a string customized to the user issuing the command.
Another one-liner, used for displaying connected users:
#!/bin/bash
who | cut -d " " -f 1 | sort -u
Here is a script consisting of some more lines, that I use to make backup copies of all files in a directory. The
script first makes a list of all the files in the current directory and puts it in the variable LIST. Then it sets the
name of the copy for each file, and then it copies the file. For each file, a message is printed:
tille:~> cat bin/makebackupfiles.sh
#!/bin/bash
# make copies of all files in a directory
LIST=`ls`
for i in $LIST; do
ORIG=$i
DEST=$i.old
cp $ORIG $DEST
echo "copied $i"
done
Just entering a line like mv * *.old won't work, as you will notice when trying this on a set of test files. An
echo command was added in order to display some activity. echo's are generally useful when a script won't
work: insert one after each doubted step and you will find the error in no time.
The /etc/rc.d/init.d directory contains loads of examples. Let's look at this script that controls the
fictive ICanSeeYou server:
#!/bin/sh
# description: ICanSeeYou allows you to see networked people
# process name: ICanSeeYou
# pidfile: /var/run/ICanSeeYou/ICanSeeYou.pid
# config: /etc/ICanSeeYou.cfg
# Source function library.
. /etc/rc.d/init.d/functions
# See how (with which arguments) we were called.
case "$1" in
start)
echo -n "Starting ICanSeeYou: "
daemon ICanSeeYou
echo
touch /var/lock/subsys/ICanSeeYou
;;
stop)
echo -n "Shutting down ICanSeeYou: "
Introduction to Linux
Chapter 7. Home sweet /home 122
killproc ICanSeeYou
echo
rm -f /var/lock/subsys/ICanSeeYou
rm -f /var/run/ICanSeeYou/ICanSeeYou.pid
;;
status)
status ICanSeeYou
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0
First, with the . command (dot) a set of shell functions, used by almost all shell scripts in
/etc/rc.d/init.d, is loaded. Then a case command is issued, which defines 4 different ways the script
can execute. An example might be ICanSeeYou start. The decision of which case to apply is made by
reading the (first) argument to the script, with the expression $1.
When no compliant input is given, the default case, marked with an asterisk, is applied, upon which the script
gives an error message. The case list is ended with the esac statement. In the start case the server program is
started as a daemon, and a process ID and lock are assigned. In the stop case, the server process is traced
down and stopped, and the lock and the PID are removed. Options, such as the daemon option, and functions
like killproc, are defined in the /etc/rc.d/init.d/functions file. This setup is specific to the
distribution used in this example. The initscripts on your system might use other functions, defined in other
files, or none at all.
Upon success, the script returns an exit code of zero to its parent.
This script is a fine example of using functions, which make the script easier to read and the work done faster.
Note that they use sh instead of bash, to make them useful on a wider range of systems. On a Linux system,
calling bash as sh results in the shell running in POSIX-compliant mode.
The bash man pages contain more information about combining commands, for- and while-loops and regular
expressions, as well as examples. A comprehensible Bash course for system administrators and power users,
with exercises, from the same author as this Introduction to Linux guide, is at
http://tille.garrels.be/training/bash/. Detailed description of Bash features and applications is in the reference
guide Advanced Bash Scripting.
7.3. The graphical environment
7.3.1. Introduction
The average user may not care too much about his login settings, but Linux offers a wide variety of flashy
window and desktop managers for use under X, the graphical environment. The use and configuration of
window managers and desktops is straightforward and may even resemble the standard MS Windows, Apple
or UNIX CDE environment, although many Linux users prefer flashier desktops and fancier window
managers. We won't discuss the user specific configuration here. Just experiment and read the documentation
using the built-in Help functions these managers provide and you will get along fine.
Introduction to Linux
Chapter 7. Home sweet /home 123
We will, however, take a closer look at the underlying system.
7.3.2. The X Window System
The X Window System is a network-transparent window system which runs on a wide range of computing
and graphics machines. X Window System servers run on computers with bitmap displays. The X server
distributes user input to and accepts output requests from several client programs through a variety of different
interprocess communication channels. Although the most common case is for the client programs to be
running on the same machine as the server, clients can be run transparently from other machines (including
machines with different architectures and operating systems) as well. We will learn how to do this in Chapter
10 on networking and remote applications.
X supports overlapping hierarchical sub-windows and text and graphics operations, on both monochrome and
color displays. The number of X client programs that use the X server is quite large. Some of the programs
provided in the core X Consortium distribution include:
· xterm: a terminal emulator
· twm: a minimalistic window manager
· xdm: a display manager
· xconsole: a console redirect program
· bitmap: a bitmap editor
· xauth, xhost and iceauth: access control programs
· xset, xmodmap and many others: user preference setting programs
· xclock: a clock
xlsfonts and others: a font displayer, utilities for listing information about fonts, windows and
displays
·
· xfs: a font server
· ...
We refer again to the man pages of these commands for detailed information. More explanations on available
functions can be found in the Xlib - C language X Interface manual that comes with your X distribution, the X
Window System Protocol specification, and the various manuals and documentation of X toolkits. The
/usr/share/doc directory contains references to these documents and many others.
Many other utilities, window managers, games, toolkits and gadgets are included as user-contributed software
in the X Consortium distribution, or are available using anonymous FTP on the Internet. Good places to start
are http://www.x.org and http://www.xfree.org.
Furthermore, all your graphical applications, such as your browser, your E-mail program, your image viewing
programs, sound playing tools and so on, are all clients to your X server. Note that in normal operation, that is
in graphical mode, X clients and the X server on Linux run on the same machine.
7.3.2.1. Display names
From the user's perspective, every X server has a display name in the form of:
hostname:displaynumber.screennumber
This information is used by the application to determine how it should connect to the X server and which
screen it should use by default (on displays with multiple monitors):
Introduction to Linux
Chapter 7. Home sweet /home 124
hostname: The host name specifies the name of the client machine to which the display is physically
connected. If the host name is not given, the most efficient way of communicating to a server on the
same machine will be used.
·
displaynumber: The phrase "display" is usually used to refer to a collection of monitors that share a
common key board and pointer (mouse, tablet, etc.). Most workstations tend to only have one
keyboard, and therefore, only one display. Larger, multi-user systems, however, frequently have
several displays so that more than one person can be doing graphics work at once. To avoid
confusion, each display on a machine is assigned a display number (beginning at 0) when the X server
for that display is started. The display number must always be given in a display name.
·
screen number: Some displays share a single keyboard and pointer among two or more monitors.
Since each monitor has its own set of windows, each screen is assigned a screen number (beginning at
0) when the X server for that display is started. If the screen number is not given, screen 0 will be
used.
·
On POSIX systems, the default display name is stored in your DISPLAY environment variable. This variable
is set automatically by the xterm terminal emulator. However, when you log into another machine on a
network, you might need to set DISPLAY by hand to point to your display, see Section 10.4.3.2.
More information can be found in the X man pages.
7.3.2.2. Window and desktop managers
The layout of windows on the screen is controlled by special programs called window managers. Although
many window managers will honor geometry specifications as given, others may choose to ignore them
(requiring the user to explicitly draw the window's region on the screen with the pointer, for example).
Since window managers are regular (albeit complex) client programs, a variety of different user interfaces can
be built. The X Consortium distribution comes with a window manager named twm, but most users prefer
something more fancy when system resources permit. Sawfish and Enlightenment are popular examples
which allow each user to have a desktop according to mood and style.
A desktop manager makes use of one window manager or another for arranging your graphical desktop in a
convenient way, with menubars, drop-down menus, informative messages, a clock, a program manager, a file
manager and so on. Among the most popular desktop managers are Gnome and KDE, which both run on
almost any Linux distribution and many other UNIX systems.
KDE applications in Gnome/Gnome applications in KDE
You don't need to start your desktop in KDE in order to be able to run KDE applications. If you have the
KDE libraries installed (the kdelibs package), you can run these applications from the Gnome menus or
start them from a Gnome terminal.
Running Gnome applications in a KDE environment is a bit more tricky, because there is no single set of
base-libraries in Gnome. However, the dependencies and thus extra packages you might have to install
will become clear when running or installing such an application.
7.3.3. X server configuration
The X distribution that used to come with Linux, XFree86, uses the configuration file XF86Config for its
initial setup. This file configures your video card and is searched for in a number of locations, although it is
Introduction to Linux
Chapter 7. Home sweet /home 125
usually in /etc/X11.
If you see that the file /etc/X11/XF86Config is present on your system, a full description can be found
in the Info or man pages about XF86Config.
Because of licensing issues with XFree86, newer systems usually come with the X.Org distribution of the X
server and tools. The main configuration file here is xorg.conf, usually also in /etc/X11. The file
consists of a number of sections that may occur in any order. The sections contain information about your
monitor, your video adaptor, the screen configuration, your keyboard etcetera. As a user, you needn't worry
too much about what is in this file, since everything is normally determined at the time the system is installed.
Should you need to change graphical server settings, however, you can run the configuration tools or edit the
configuration files that set up the infrastructure to use the XFree86 server. See the man pages for more
information; your distribution might have its own tools. Since misconfiguration may result in unreadable
garbage in graphical mode, you may want to make a backup copy of the configuration file before attempting
to change it, just to be on the safe side.
7.4. Region specific settings
7.4.1. Keyboard setup
Setting the keyboard layout is done using the loadkeys command for text consoles. Use your local X
configuration tool or edit the Keyboard section in XF86Config manually to configure the layout for
graphical mode. The XkbdLayout is the one you want to set:
XkbLayout "us"
This is the default. Change it to your local settings by replacing the quoted value with any of the names listed
in the subdirectories of your keymaps directory. If you can't find the keymaps, try displaying their location
on your system issuing the command
locate keymaps
It is possible to combine layout settings, like in this example:
Xkblayout "us,ru"
Make a backup of the /etc/X11/XF86Config file before editing it! You will need to use the root account
to do this.
Log out and reconnect in order to reload X settings.
The Gnome Keyboard Applet enables real-time switching between layouts; no special pemissions are needed
for using this program. KDE has a similar tool for switching between keyboard layouts.
7.4.2. Fonts
Use the setfont tool to load fonts in text mode. Most systems come with a standard inputrc file which
enables combining of characters, such as the French "é" (meta characters). The system admin should then add
the line
Introduction to Linux
Chapter 7. Home sweet /home 126
export INPUTRC="/etc/inputrc"
to the /etc/bashrc file.
7.4.3. Date and time zone
Setting time information is usually done at installation time. After that, it can be kept up to date using an NTP
(Network Time Protocol) client. Most Linux systems run ntpd by default:
debby:~> ps -ef | grep ntpd
ntp 24678 1 0 2002 ? 00:00:33 ntpd -U ntp
You can run ntpdate manually to set the time, on condition that you can reach a time server. The ntpd
daemon should not be running when you adjust the time using ntpdate. Use a time server as argument to the
command:
root@box:~# ntpdate 10.2.5.200
26 Oct 14:35:42 ntpdate[20364]: adjust time server 10.2.5.200 offset
-0.008049 sec
See your system manual and the documentation that comes with the NTP package. Most desktop managers
include tools to set the system time, providing that you have access to the system administrator's account.
For setting the time zone correct, you can use tzconfig or timezone commands. Timezone information is
usually set during the installation of your machine. Many systems have distribution-specific tools to configure
it, see your system documentation.
7.4.4. Language
If you'd rather get your messages from the system in Dutch or French, you may want to set the LANG and
LANGUAGE environment variables, thus enabling locale support for the desired language and eventually the
fonts related to character conventions in that language.
With most graphical login systems, such as gdm or kdm, you have the possibility to configure these language
settings before logging in.
Note that on most systems, the default tends to be en_US.UTF-8 these days. This is not a problem, because
systems where this is the default, will also come with all the programs supporting this encoding. Thus, vi can
edit all the files on your system, cat won't behave strange and so on.
Trouble starts when you connect to an older system not supporting this font encoding, or when you open a
UTF-8 encoded file on a system supporting only 1-byte character fonts. The recode utility might come in
handy to convert files from one character set to another. Read the man pages for an overview of features and
usage. Another solution might be to temporarily work with another encoding definition, by setting the LANG
environment variable:
debby:~> acroread /var/tmp/51434s.pdf
Warning: charset "UTF-8" not supported, using "ISO8859-1".
Aborted
debby:~> set | grep UTF
LANG=en_US.UTF-8
debby:~> export LANG=en_US
Introduction to Linux
Chapter 7. Home sweet /home 127
debby:~> acroread /var/tmp/51434s.pdf
<--new window opens-->
Refer to the Mozilla web site for guidance on how to get Firefox in your language. The OpenOffice.org web
site has information on localization of your OpenOffice.org suite.
7.4.5. Country-specific Information
The list of HOWTOs contains references to Bangla, Belarusian, Chinese, Esperanto, Finnish, Francophone,
Hebrew, Hellenic, Latvian, Polish, Portugese, Serbian, Slovak, Slovenian, Spanish, Thai and Turkish
localization instructions.
7.5. Installing new software
7.5.1. General
Most people are surprised to see that they have a running, usable computer after installing Linux; most
distributions contain ample support for video and network cards, monitors and other external devices, so there
is usually no need to install extra drivers. Also common tools such as office suites, web browsers, E-mail and
other network client programs are included in the main distributions. Even so, an initial installation might not
meet your requirements.
If you just can't find what you need, maybe it is not installed on your system. It may also be that you have the
required software, but it does not do what it is supposed to do. Remember that Linux moves fast, and software
improves on a daily basis. Don't waste your time troubleshooting problems that might already be resolved.
You can update your system or add packages to it at any time you want. Most software comes in packages.
Extra software may be found on your installation CDs or on the Internet. The website of your Linux
distribution is a good place to start looking for additional software and contains instructions about how to
install it on your type of Linux, see Appendix A. Always read the documentation that comes with new
software, and any installation guidelines the package might contain. All software comes with a README file,
which you are very strongly advised to read.
7.5.2. Package formats
7.5.2.1. RPM packages
7.5.2.1.1. What is RPM?
RPM, the RedHat Package Manager, is a powerful package manager that you can use to install, update and
remove packages. It allows you to search for packages and keeps track of the files that come with each
package. A system is built-in so that you can verify the authenticity of packages downloaded from the
Internet. Advanced users can build their own packages with RPM.
An RPM package consists of an archive of files and meta-data used to install and erase the archive files. The
meta-data includes helper scripts, file attributes, and descriptive information about the package. Packages
come in two varieties: binary packages, used to encapsulate software to be installed, and source packages,
containing the source code and recipe necessary to produce binary packages.
Introduction to Linux
Chapter 7. Home sweet /home 128
Many other distributions support RPM packages, among the popular ones RedHat Enterprise Linux, Mandriva
(former Mandrake), Fedora Core and SuSE Linux. Apart from the advice for your distribution, you will want
to read man rpm.
7.5.2.1.2. RPM examples
Most packages are simply installed with the upgrade option, -U, whether the package is already installed or
not. The RPM package contains a complete version of the program, which overwrites existing versions or
installs as a new package. The typical usage is as follows:
rpm -Uvh /path/to/rpm-package(s)
The -v option generates more verbose output, and -h makes rpm print a progress bar:
[root@jupiter tmp]# rpm -Uvh totem-0.99.5-1.fr.i386.rpm
Preparing... ########################################### [100%]
1:totem ########################################### [100%]
[root@jupiter tmp]#
New kernel packages, however, are installed with the install option -i, which does not overwrite existing
version(s) of the package. That way, you will still be able to boot your system with the old kernel if the new
one does not work.
You can also use rpm to check whether a package is installed on your system:
[david@jupiter ~] rpm -qa | grep vim
vim-minimal-6.1-29
vim-X11-6.1-29
vim-enhanced-6.1-29
vim-common-6.1-29
Or you can find out which package contains a certain file or executable:
[david@jupiter ~] rpm -qf /etc/profile
setup-2.5.25-1
[david@jupiter ~] which cat
cat is /bin/cat
[david@jupiter ~] rpm -qf /bin/cat
coreutils-4.5.3-19
Note that you need not have access to administrative privileges in order to use rpm to query the RPM
database. You only need to be root when adding, modifying or deleting packages.
Below is one last example, demonstrating how to uninstall a package using rpm:
[root@jupiter root]# rpm -e totem
[root@jupiter root]#
Note that uninstalling is not that verbose by default, it is normal that you don't see much happening. When in
doubt, use rpm -qa again to verify that the package has been removed.
RPM can do much more than the couple of basic functions we discussed in this introduction; the RPM
HOWTO contains further references.
Introduction to Linux
Chapter 7. Home sweet /home 129
7.5.2.2. DEB (.deb) packages
7.5.2.2.1. What are Debian packages?
This package format is the default on Debian GNU/Linux, where dselect, and, nowadays more common,
aptitude, is the standard tool for managing the packages. It is used to select packages that you want to install
or upgrade, but it will also run during the installation of a Debian system and help you to define the access
method to use, to list available packages and to configure packages.
The Debian web site contains all information you need, including a "dselect Documentation for Beginners".
According to the latest news, the Debian package format is becoming more and more popular. At the time of
this writing, 5 of the top-10 distributions use it. Also apt-get (see Section 7.5.3.2 is becoming extremely
popular, also on non-DEB systems.
7.5.2.2.2. Examples with DEB tools
Checking whether a package is installed is done using the dpkg command. For instance, if you want to know
which version of the Gallery software is installed on your machine:
nghtwsh@gorefest:~$ dpkg -l *gallery*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name Version Description
+++-==============-==============-============================================
ii gallery 1.5-1sarge2 a web-based photo album written in php
The "ii" prefix means the package is installed. Should you see "un" as a prefix, that means that the package is
known in the list that your computer keeps, but that it is not installed.
Searching which package a file belongs to is done using the -S to dpkg:
nghtwsh@gorefest:~$ dpkg -S /bin/cat
coreutils: /bin/cat
More information can be found in the Info pages for dpkg.
7.5.2.3. Source packages
The largest part of Linux programs is Free/Open Source, so source packages are available for these programs.
Source files are needed for compiling your own program version. Sources for a program can be downloaded
from its web site, often as a compressed tarball (program-version.tar.gz or similar). For RPM-based
distributions, the source is often provided in the program-version.src.rpm. Debian, and most
distributions based on it, provide themselves the adapted source which can be obtained using apt-get
source.
Specific requirements, dependencies and installation instructions are provided in the README file. You will
probably need a C compiler, gcc. This GNU C compiler is included in most Linux systems and is ported to
many other platforms.
Introduction to Linux
Chapter 7. Home sweet /home 130
7.5.3. Automating package management and updates
7.5.3.1. General remarks
The first thing you do after installing a new system is applying updates; this applies to all operating systems
and Linux is not different.
The updates for most Linux systems can usually be found on a nearby site mirroring your distribution. Lists of
sites offering this service can be found at your distribution's web site, see Appendix A.
Updates should be applied regularly, daily if possible - but every couple of weeks would be a reasonable start.
You really should try to have the most recent version of your distribution, since Linux changes constantly. As
we said before, new features, improvements and bug fixes are supplied at a steady rhythm, and sometimes
important security problems are addressed.
The good news is that most Linux distributions provide tools so that you don't have to upgrade tens of
packages daily by hand. The following sections give an overview of package manager managers. There is
much more to this subject, even regular updates of source packages is manageable automatically; we only list
the most commonly known systems. Always refer to the documentation for your specific distribution for
advised procedures.
7.5.3.2. APT
The Advanced Package Tool is a management system for software packages. The command line tool for
handling packages is apt-get, which comes with an excellent man page describing how to install and update
packages and how to upgrade singular packages or your entire distribution. APT has its roots in the Debian
GNU/Linux distribution, where it is the default manager for the Debian packages. APT has been ported to
work with RPM packages as well. The main advantage of APT is that it is free and flexible to use. It will
allow you to set up systems similar to the distribution specific (and in some cases commercial) ones listed in
the next sections.
Generally, when first using apt-get, you will need to get an index of the available packages. This is done
using the command
apt-get update
After that, you can use apt-get to upgrade your system:
apt-get upgrade
Do this often, it's an easy way to keep your system up-to-date and thus safe.
Apart from this general usage, apt-get is also very fast for installing individual packages. This is how it
works:
[david@jupiter ~] su - -c "apt-get install xsnow"
Password:
Reading Package Lists... Done
Building Dependency Tree... Done
The following NEW packages will be installed:
xsnow
Introduction to Linux
Chapter 7. Home sweet /home 131
0 packages upgraded, 1 newly installed, 0 removed and 3 not upgraded.
Need to get 33.6kB of archives.
After unpacking 104kB of additional disk space will be used.
Get:1 http://ayo.freshrpms.net redhat/9/i386/os xsnow 1.42-10 [33.6kB]
Fetched 33.6kB in 0s (106kB/s)
Executing RPM (-Uvh)...
Preparing... ########################################### [100%]
1:xsnow ########################################### [100%]
Note the -c option to the su command, which indicates to the root shell to only execute this command, and
then return to the user's environment. This way, you cannot forget to quit the root account.
If there are any dependencies on other packages, apt-get will download and install these supporting packages.
More information can be found in the APT HOWTO.
7.5.3.3. Systems using RPM packages
Update Agent, which originally only supported RedHat RPM packages, is now ported to a wider set of
software, including non-RedHat repositories. This tool provides a complete system for updating the RPM
packages on a RedHat or Fedora Core system. On the command line, type up2date to update your system. On
the desktop, by default a small icon is activated, telleng you whether or not there are updates available for
your system.
Yellowdog's Updater Modified (yum) is another tool that recently became more popular. It is an interactive
but automated update program for installing, updating or removing RPM packages on a system. It is the tool
of choice on Fedora systems.
On SuSE Linux, everything is done with YaST, Yet another Setup Tool, which supports a wide variety of
system administration tasks, among which updating RPM packages. Starting from SuSE Linux 7.1 you can
also upgrade using a web interface and YOU, Yast Online Update.
Mandrake Linux and Mandriva provide so-called URPMI tools, a set of wrapper programs that make
installing new software easier for the user. These tools combine with RPMDrake and MandrakeUpdate to
provide everything needed for smooth install and uninstall of software packages. MandrakeOnline offers an
extended range of services and can automatically notify administrators when updates are available for your
particular Mandrake system. See man urpmi, among others, for more info.
Also the KDE and Gnome desktop suites have their own (graphical) versions of package managers.
7.5.4. Upgrading your kernel
Most Linux installations are fine if you periodically upgrade your distribution. The upgrade procedure will
install a new kernel when needed and make all necessary changes to your system. You should only compile or
install a new kernel manually if you need kernel features that are not supported by the default kernel included
in your Linux distribution.
Whether compiling your own optimized kernel or using a pre-compiled kernel package, install it in
co-existence with the old kernel until you are sure that everything works according to plan.
Then create a dual boot system that will allow you to choose which kernel to boot by updating your boot
loader configuration file grub.conf. This is a simple example:
Introduction to Linux
Chapter 7. Home sweet /home 132
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making config changes.
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, e.g.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/hde8
# initrd /initrd-version.img
#boot=/dev/hde
default=0
timeout=10
splashimage=(hd0,0)/grub/splash.xpm.gz
title Red Hat Linux new (2.4.9-31)
root (hd0,0)
kernel /vmlinuz-2.4.9-31 ro root=/dev/hde8
initrd /initrd-2.4.9-31.img
title old-kernel
root (hd0,0)
kernel /vmlinuz-2.4.9-21 ro root=/dev/hde8
initrd /initrd-2.4.9-21.img
After the new kernel has proven to work, you may remove the lines for the old one from the GRUB config
file, although it is best to wait a couple of days just to be sure.
7.5.5. Installing extra packages from the installation CDs
7.5.5.1. Mounting a CD
This is basically done in the same way as installing packages manually, except that you have to append the
file system of the CD to your machine's file system to make it accessible. On most systems, this will be done
automatically upon insertion of a CD in the drive because the automount daemon is started up at boot time. If
your CD is not made available automatically, issue the mount command in a terminal window. Depending on
your actual system configuration, a line similar to this one will usually do the trick:
mount /dev/cdrom /mnt/cdrom
On some systems, only root can mount removable media; this depends on the configuration.
For automation purposes, the CD drive usually has an entry in /etc/fstab, which lists the file systems and
their mount points, that make up your file system tree. This is such a line:
[david@jupiter ~] grep cdrom /etc/fstab
/dev/cdrom /mnt/cdrom iso9660 noauto,owner,ro 0 0
This indicates that the system will understand the command mount /mnt/cdrom. The noauto option
means that on this system, CDs are not mounted at boot time.
You may even try to right click on the CD icon on your desktop to mount the CD if your file manager doesn't
do it for you. You can check whether it worked issuing the mount command with no arguments:
[david@jupiter ~] mount | grep cdrom
/dev/cdrom on /mnt/cdrom type iso9660 (ro,nosuid,nodev)
Introduction to Linux
Chapter 7. Home sweet /home 133
7.5.5.2. Using the CD
After mounting the CD, you can change directories, usually to the mount point /mnt/cdrom, where you can
access the content of the CD-ROM. Use the same commands for dealing with files and directories as you
would use for files on the hard disk.
7.5.5.3. Ejecting the CD
In order to get the CD out of the drive after you've finished using it, the file system on the CD should be
unused. Even being in one of the subdirectories of the mount point, /mnt/cdrom in our example, will be
considered as "using the file system", so you should get out of there. Do this for instance by typing cd with no
arguments, which will put you back in your home directory. After that, you can either use the command
umount /mnt/cdrom
or
eject cdrom
Blocked drives
NEVER force the drive. The trick with the paperclip is a bad idea, because this will eventually expunge
the CD, but your system will think the CD is still there because normal procedures were not followed.
Chances are likely that you will have to reboot to get the system back in a consistent state.
If you keep getting "device busy" messages, check first that all shell sessions have left the CD file
system and that no graphical applications are using it anymore. When in doubt, use the lsof tool to trace
down the process(es) still using the CD resource.
7.6. Summary
When everything has its place, that means already half the work is done.
While keeping order is important, it is equally important to feel at home in your environment, whether text or
graphical. The text environment is controlled through the shell setup files. The graphical environment is
primarily dependent on the X server configuration, on which a number of other applications are built, such as
window and desktop managers and graphical applications, each with their own config files. You should read
the system and program specific documentation to find out about how to configure them.
Regional settings such as keyboard setup, installing appropriate fonts and language support are best done at
installation time.
Software is managed either automatically or manually using a package system.
The following commands were introduced in this chapter:
Table 7-2. New commands in chapter 7: Making yourself at home
Command Meaning
Introduction to Linux
Chapter 7. Home sweet /home 134
aptitude Manage packages Debian-style.
automount automatically include newly inserted file systems.
dpkg Debian package manager.
dselect Manage packages Debian-style.
loadkeys Load keyboard configuration.
lsof Identify processes.
mount Include a new file system into the existing file system tree.
ntpdate Set the system time and date using a time server.
quota Display information about allowed disk space usage.
recode Convert files to another character set.
rpm Manage RPM packages.
setfont Choose a font.
timezone Set the timezone.
tzconfig Set the timezone.
ulimit Set or display resource limits.
up2date Manage RPM packages.
urpmi Manage RPM packages.
yum Manage RPM packages.
7.7. Exercises
7.7.1. Shell environment
Print out your environment settings. Which variable may be used to store the CPU type of your
machine?
·
Make a script that can say something on the lines of "hello, world." Give it appropriate permissions so
it can be run. Test your script.
·
Create a directory in your home directory and move the script to the new directory. Permanently add
this new directory to your search path. Test that the script can be executed without giving a path to its
actual location.
·
Create subdirectories in your home directory to store various files, for instance a directory music to
keep audio files, a directory documents for your notes, and so on. And use them!
·
· Create a personalized prompt.
· Display limits on resource usage. Can you change them?
· Try to read compressed man pages without decompressing them first.
· Make an alias lll which actually executes ls -la.
· Why does the command tail testfile > testfile not work?
Mount a data CD, such as your Linux installation CD, and have a look around. Don't forget to
unmount when you don't need it anymore.
·
The script from Section 7.2.5.2 is not perfect. It generates errors for files that are directories. Adapt
the script so that it only selects plain files for copying. Use find to make the selection. Do not forget
to make the script executable before you try to run it.
·
Introduction to Linux
Chapter 7. Home sweet /home 135
7.7.2. Graphical environment
· Try all the mouse buttons in different regions (terminal, background, task bar).
· Explore the menus.
· Customize your terminal window.
· Use the mouse buttons to copy and paste text from one terminal to another.
· Find out how to configure your window manager; try different workspaces (virtual screens).
· Add an applet, such as a load monitor, to the task bar.
· Apply a different theme.
Enable the so-called sloppy focus - this is when a window is activated by just moving the mouse over
it, so that you do not need to click the window in order to be able to use it.
·
· Switch to a different window manager.
Log out and select a different session type, like KDE if you were using Gnome before. Repeat the
previous steps.
·
Introduction to Linux
Chapter 7. Home sweet /home 136
Chapter 8. Printers and printing
In this chapter we will learn more about printers and printing files. After reading this part, you
will be able to:
¨ Format documents
¨ Preview documents before sending them to the printer
¨ Choose a good printer that works with your Linux system
¨ Print files and check on printer status
¨ Troubleshoot printing problems
¨ Find necessary documentation to install a printer
8.1. Printing files
8.1.1. Command line printing
8.1.1.1. Getting the file to the printer
Printing from within an application is very easy, selecting the Print option from the menu.
From the command line, use the lp or lpr command.
lp file(s)
lpr file(s)
These commands can read from a pipe, so you can print the output of commands using
command | lp
There are many options available to tune the page layout, the number of copies, the printer that you want to
print to if you have more than one available, paper size, one-side or double-sided printing if your printer
supports this feature, margins and so on. Read the man pages for a complete overview.
8.1.1.2. Status of your print jobs
Once the file is accepted in the print queue, an identification number for the print job is assigned:
davy:~> lp /etc/profile
request id is blob-253 (1 file(s))
To view (query) the print queue, use the lpq or lpstat command. When entered without arguments, it displays
the contents of the default print queue.
davy:~> lpq
blob is ready and printing
Rank Owner Job File(s) Total Size
active davy 253 profile 1024 bytes
davy:~> lpstat
blob-253 davy 1024 Tue 25 Jul 2006 10:20_01 AM CEST
Chapter 8. Printers and printing 137
8.1.1.3. Status of your printer
Which is the default printer on a system that has access to multiple printers?
lpstat -d
davy:~> lpstat -d
system default destination: blob
What is the status of my printer(s)?
lpstat -p
davy:~> lpstat -p
printer blob now printing blob-253. enabled since Jan 01 18:01
8.1.1.4. Removing jobs from the print queue
If you don't like what you see from the status commands, use lprm or cancel to delete jobs.
davy:~> lprm 253
In the graphical environment, you may see a popup window telling you that the job has been canceled.
In larger environments, lpc may be used to control multiple printers. See the Info or man pages on each
command.
There are many GUI print tools used as a front-end to lp, and most graphical applications have a print
function that uses lp. See the built-in Help functions and program specific documentation for more.
Why are there two commands for every task related to printing?
Printing on UNIX and alikes has a long history. There used to be two rather different approaches: the
BSD-style printing and the SystemV-style printing. For compatibility, Linux with CUPS supports the
commands from both styles. Also note that lp does not behave exactly like lpr, lpq has somewhat
different options than lpstat and lprm is almost, but not quite, like cancel. Which one you use is not
important, just pick the commands that you are comfortable with, or that you may know from previous
experiences with UNIX-like systems.
8.1.2. Formatting
8.1.2.1. Tools and languages
If we want to get something sensible out of the printer, files should be formatted first. Apart from an
abundance of formatting software, Linux comes with the basic UNIX formatting tools and languages.
Modern Linux systems support direct printing, without any formatting by the user, of a range of file types:
text, PDF, PostScript and several image formats like PNG, JPEG, BMP and GIF.
For those file formats that do need formatting, Linux comes with a lot of formatting tools, such as the pdf2ps,
fax2ps and a2ps commands, that convert other formats to PostScript. These commands can create files that
can then be used on other systems that don't have all the conversion tools installed.
Introduction to Linux
Chapter 8. Printers and printing 138
Apart from these command line tools there are a lot of graphical word processing programs. Several complete
office suites are available, many are free. These do the formatting automatically upon submission of a print
job. Just to name a few: OpenOffice.org, KOffice, AbiWord, WordPerfect, etc.
The following are common languages in a printing context:
groff: GNU version of the UNIX roff command. It is a front-end to the groff document formatting
system. Normally it runs the troff command and a post-processor appropriate for the selected device.
It allows generation of PostScript files.
·
TeX and the macro package LaTeX: one of the most widely used markup languages on UNIX
systems. Usually invoked as tex, it formats files and outputs a corresponding device-independent
representation of the typeset document.
Technical works are still frequently written in LaTeX because of its support for mathematic formulas,
although efforts are being made at W3C (the World Wide Web Consortium) to include this feature in
other applications.
·
SGML and XML: Free parsers are available for UNIX and Linux. XML is the next generation SGML,
it forms the basis for DocBook XML, a document system (this book is written in XML, for instance).
·
Printing documentation
The man pages contain pre-formatted troff data which has to be formatted before it can roll out of your
printer. Printing is done using the -t option to the man command:
man -t command > man-command.ps
Then print the PostScript file. If a default print destination is configured for your system/account, you
can just issue the command man -t command to send the formatted page to the printer directly.
8.1.2.2. Previewing formatted files
Anything that you can send to the printer can normally be sent to the screen as well. Depending on the file
format, you can use one of these commands:
· PostScript files: with the gv (GhostView) command.
· TeX dvi files: with xdvi, or with KDE's kdvi.
PDF files: xpdf, kpdf, gpdf or Adobe's viewer, acroread, which is also available for free but is not
free software. Adobe's reader supports PDF 1.6, the others only support PDF versions up to 1.5. The
version of a PDF file can be determined using the file command.
·
From within applications, such as Firefox or OpenOffice, you can usually select Print Preview from
one of the menus.
·
8.2. The server side
8.2.1. General
Until a couple of years ago, the choice for Linux users was simple: everyone ran the same old LPD from
BSD's Net-2 code. Then LPRng became more popular, but nowadays most modern Linux distributions use
CUPS, the Common UNIX Printing System. CUPS is an implementation of the Internet Printing Protocol
(IPP), an HTTP-like RFC standard replacement protocol for the venerable (and clunky) LPD protocol. CUPS
Introduction to Linux
Chapter 8. Printers and printing 139
is distributed under the GNU Public License. CUPS is also the default print system on MacOS X.
8.2.2. Graphical printer configuration
Most distributions come with a GUI for configuring networked and local (parallel port or USB) printers. They
let you choose the printer type from a list and allow easy testing. You don't have to bother about syntax and
location of configuration files. Check your system documentation before you attempt installing your printer.
CUPS can also be configured using a web interface that runs on port 631 on your computer. To check if this
feature is enabled, try browsing to localhost:631/help or localhost:631/.
8.2.3. Buying a printer for Linux
As more and more printer vendors make drivers for CUPS available, CUPS will allow easy connection with
almost any printer that you can plug into a serial, parallel, or USB port, plus any printer on the network.
CUPS will ensure a uniform presentation to you and your applications of all different types of printers.
Printers that only come with a Win9x driver could be problematic if they have no other support. Check with
http://linuxprinting.org/ when in doubt.
In the past, your best choice would have been a printer with native PostScript support in the firmware, since
nearly all UNIX or Linux software producing printable output, produces it in PostScript, the publishing
industry's printer control language of choice. PostScript printers are usually a bit more expensive, but it is a
device-independent, open programming language and you're always 100% sure that they will work. These
days, however, the importance of this rule of thumb is dwindling.
8.3. Print problems
In this section, we will discuss what you can do as a user when something goes wrong. We won't discuss any
problems that have to do with the daemon-part of the printing service, as that is a task for system
administrators.
8.3.1. Wrong file
If you print the wrong file, the job may be canceled using the command lprm jobID, where jobID is in the
form printername-printjobnumber (get it from information displayed by lpq or lpstat). This will work when
other jobs are waiting to be printed in this printer's queue. However, you have to be really quick if you are the
only one using this printer, since jobs are usually spooled and send to the printer in only seconds. Once they
arrive on the printer, it is too late to remove jobs using Linux tools.
What you can try in those cases, or in cases where the wrong print driver is configured and only rubbish
comes out of the printer, is power off the printer. However, that might not be the best course of action, as you
might cause paper jams and other irregularities.
8.3.2. My print hasn't come out
Use the lpq command and see if you can spot your job:
Introduction to Linux
Chapter 8. Printers and printing 140
elly:~> lpq
Printer: lp@blob
Queue: 2 printable jobs
Server: pid 29998 active
Unspooler: pid 29999 active
Status: waiting for subserver to exit at 09:43:20.699
Rank Owner/ID Class Job Files Size Time
1 elly@blob+997 A 997 (STDIN) 129 09:42:54
2 elly@blob+22 A 22 /etc/profile 917 09:43:20
Lots of printers have web interfaces these days, which can display status information by typing the printer's IP
address in your web browser:
Figure 8-1. Printer Status through web interface
CUPS web interface versus printer web interface
Introduction to Linux
Chapter 8. Printers and printing 141
Note that this is not the CUPS web interface and only works for printers supporting this feature. Check
the documentation of your printer.
If your job ID is not there and not on the printer, contact your system administrator. If your job ID is listed in
the output, check that the printer is currently printing. If so, just wait, your job will get done in due time.
If the printer is not printing, check that it has paper, check the physical connections to both electricity and data
network. If that's okay, the printer may need restarting. Ask your system admin for advice.
In the case of a network printer, try printing from another host. If the printer is reachable from your own host
(see Chapter 10 for the ping utility), you may try to put the formatted file on it, like file.ps in case of a
PostScript printer, using an FTP client. If that works, your print system is misconfigured. If it doesn't work,
maybe the printer doesn't understand the format you are feeding it.
The GNU/Linux Printing site contains more tips and tricks.
8.4. Summary
The Linux print service comes with a set of printing tools based on the standard UNIX LPD tools, whether it
be the SystemV or BSD implementation. Below is a list of print-related commands.
Table 8-1. New commands in chapter 8: Printing
Command Meaning
lpr or lp Print file
lpq or lpstat Query print queue
lprm or cancel Remove print job
acroread PDF viewer
groff Formatting tool
gv PostScript viewer
printconf Configure printers
xdvi DVI viewer
xpdf PDF viewer
*2ps Convert file to PostScript
8.5. Exercises
Configuring and testing printers involves being in the possession of one, and having access to the root
account. If so, you may try:
· Installing the printer using the GUI on your system.
· Printing a test page using the GUI.
· Printing a test page using the lp command.
Print from within an application, for example Mozilla or OpenOffice, by choosing File->Print from
the menu.
·
Disconnect the printer from the network or the local machine/print-server. What happens when you
try to print something?
·
Introduction to Linux
Chapter 8. Printers and printing 142
The following exercises can be done without printer or root access.
Try to make PostScript files from different source files, (e.g. HTML, PDF, man pages). Test the
results with the gv viewer.
·
· Check that the print daemon is running.
· Print the files anyway. What happens?
· Make a PostScript file using Mozilla. Test it with gv.
· Convert it to PDF format. Test with xpdf.
· How would you go about printing a GIF file from the command line?
Use a2ps to print the /etc/profile file to an output file. Test again with gv. What happens if you
don't specify an output file?
·
Introduction to Linux
Chapter 8. Printers and printing 143
Chapter 9. Fundamental Backup Techniques
Accidents will happen sooner or later. In this chapter, we'll discuss how to get data to a safe
place using other hosts, floppy disks, CD-ROMs and tapes. We will also discuss the most
popular compressing and archiving commands.
Upon completion of this chapter, you will know how to:
¨ Make, query and unpack file archives
¨ Handle floppy disks and make a boot disk for your system
¨ Write CD-ROMs
¨ Make incremental backups
¨ Create Java archives
¨ Find documentation to use other backup devices and programs
¨ Encrypt your data
9.1. Introduction
Although Linux is one of the safest operating systems in existence, and even if it is designed to keep on going,
data can get lost. Data loss is most often the consequence of user errors, but occasionally a system fault, such
as a power or disk failure, is the cause, so it's always a good idea to keep an extra copy of sensitive and/or
important data.
9.1.1. Preparing your data
9.1.1.1. Archiving with tar
In most cases, we will first collect all the data to back up in a single archive file, which we will compress later
on. The process of archiving involves concatenating all listed files and taking out unnecessary blanks. In
Linux, this is commonly done with the tar command. tar was originally designed to archive data on tapes, but
it can also make archives, known as tarballs.
tar has many options, the most important ones are cited below:
· -v: verbose
· -t: test, shows content of a tarball
· -x: extract archive
· -c: create archive
-f archivedevice: use archivedevice as source/destination for the tarball, the device
defaults to the first tape device (usually /dev/st0 or something similar)
·
· -j: filter through bzip2, see Section 9.1.1.2
It is common to leave out the dash-prefix with tar options, as you can see from the examples below.
Use GNU tar for compatibility
The archives made with a proprietary tar version on one system, may be incompatible with tar on
another proprietary system. This may cause much headaches, such as if the archive needs to be recovered
Chapter 9. Fundamental Backup Techniques 144
on a system that doesn't exist anymore. Use the GNU tar version on all systems to prevent your system
admin from bursting into tears. Linux always uses GNU tar. When working on other UNIX machines,
enter tar --help to find out which version you are using. Contact your system admin if you don't see
the word GNU somewhere.
In the example below, an archive is created and unpacked.
gaby:~> ls images/
me+tux.jpg nimf.jpg
gaby:~> tar cvf images-in-a-dir.tar images/
images/
images/nimf.jpg
images/me+tux.jpg
gaby:~> cd images
gaby:~/images> tar cvf images-without-a-dir.tar *.jpg
me+tux.jpg
nimf.jpg
gaby:~/images> cd
gaby:~> ls */*.tar
images/images-without-a-dir.tar
gaby:~> ls *.tar
images-in-a-dir.tar
gaby:~> tar xvf images-in-a-dir.tar
images/
images/nimf.jpg
images/me+tux.jpg
gaby:~> tar tvf images/images-without-dir.tar
-rw-r--r-- gaby/gaby 42888 1999-06-30 20:52:25 me+tux.jpg
-rw-r--r-- gaby/gaby 7578 2000-01-26 12:58:46 nimf.jpg
gaby:~> tar xvf images/images-without-a-dir.tar
me+tux.jpg
nimf.jpg
gaby:~> ls *.jpg
me+tux.jpg nimf.jpg
This example also illustrates the difference between a tarred directory and a bunch of tarred files. It is
advisable to only compress directories, so files don't get spread all over when unpacking the tarball (which
may be on another system, where you may not know which files were already there and which are the ones
from the archive).
When a tape drive is connected to your machine and configured by your system administrator, the file names
ending in .tar are replaced with the tape device name, for example:
tar cvf /dev/tape mail/
The directory mail and all the files it contains are compressed into a file that is written on the tape
immediately. A content listing is displayed because we used the verbose option.
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 145
9.1.1.2. Incremental backups with tar
The tar tool supports the creation of incremental backups, using the -N option. With this option, you can
specify a date, and tar will check modification time of all specified files against this date. If files are changed
more recent than date, they will be included in the backup. The example below uses the timestamp on a
previous archive as the date value. First, the initial archive is created and the timestamp on the initial backup
file is shown. Then a new file is created, upon which we take a new backup, containing only this new file:
jimmy:~> tar cvpf /var/tmp/javaproggies.tar java/*.java
java/btw.java
java/error.java
java/hello.java
java/income2.java
java/income.java
java/inputdevice.java
java/input.java
java/master.java
java/method1.java
java/mood.java
java/moodywaitress.java
java/test3.java
java/TestOne.java
java/TestTwo.java
java/Vehicle.java
jimmy:~> ls -l /var/tmp/javaproggies.tar
-rw-rw-r-- 1 jimmy jimmy 10240 Jan 21 11:58 /var/tmp/javaproggies.tar
jimmy:~> touch java/newprog.java
jimmy:~> tar -N /var/tmp/javaproggies.tar \
-cvp /var/tmp/incremental1-javaproggies.tar java/*.java 2> /dev/null
java/newprog.java
jimmy:~> cd /var/tmp/
jimmy:~> tar xvf incremental1-javaproggies.tar
java/newprog.java
Standard errors are redirected to /dev/null. If you don't do this, tar will print a message for each
unchanged file, telling you it won't be dumped.
This way of working has the disadvantage that it looks at timestamps on files. Say that you download an
archive into the directory containing your backups, and the archive contains files that have been created two
years ago. When checking the timestamps of those files against the timestamp on the initial archive, the new
files will actually seem old to tar, and will not be included in an incremental backup made using the -N
option.
A better choice would be the -g option, which will create a list of files to backup. When making incremental
backups, files are checked against this list. This is how it works:
jimmy:~> tar cvpf work-20030121.tar -g snapshot-20030121 work/
work/
work/file1
work/file2
work/file3
jimmy:~> file snapshot-20030121
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 146
snapshot-20030121: ASCII text
The next day, user jimmy works on file3 a bit more, and creates file4. At the end of the day, he makes a
new backup:
jimmy:~> tar cvpf work-20030122.tar -g snapshot-20030121 work/
work/
work/file3
work/file4
These are some very simple examples, but you could also use this kind of command in a cronjob (see Section
4.4.4), which specifies for instance a snapshot file for the weekly backup and one for the daily backup.
Snapshot files should be replaced when taking full backups, in that case.
More information can be found in the tar documentation.
The real stuff
As you could probably notice, tar is OK when we are talking about a simple directory, a set of files that
belongs together. There are tools that are easier to manage, however, when you want to archive entire
partitions or disks or larger projects. We just explain about tar here because it is a very popular tool for
distributing archives. It will happen quite often that you need to install a software that comes in a
so-called "compressed tarball". See Section 9.3 for an easier way to perform regular backups.
9.1.1.3. Compressing and unpacking with gzip or bzip2
Data, including tarballs, can be compressed using zip tools. The gzip command will add the suffix .gz to the
file name and remove the original file.
jimmy:~> ls -la | grep tar
-rw-rw-r-- 1 jimmy jimmy 61440 Jun 6 14:08 images-without-dir.tar
jimmy:~> gzip images-without-dir.tar
jimmy:~> ls -la images-without-dir.tar.gz
-rw-rw-r-- 1 jimmy jimmy 50562 Jun 6 14:08 images-without-dir.tar.gz
Uncompress gzipped files with the -d option.
bzip2 works in a similar way, but uses an improved compression algorithm, thus creating smaller files. See
the bzip2 info pages for more.
Linux software packages are often distributed in a gzipped tarball. The sensible thing to do after unpacking
that kind of archives is find the README and read it. It will generally contain guidelines to installing the
package.
The GNU tar command is aware of gzipped files. Use the command
tar zxvf file.tar.gz
for unzipping and untarring .tar.gz or .tgz files. Use
tar jxvf file.tar.bz2
for unpacking tar archives that were compressed with bzip2.
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 147
9.1.1.4. Java archives
The GNU project provides us with the jar tool for creating Java archives. It is a Java application that
combines multiple files into a single JAR archive file. While also being a general purpose archiving and
compression tool, based on ZIP and the ZLIB compression format, jar was mainly designed to facilitate the
packing of Java code, applets and/or applications in a single file. When combined in a single archive, the
components of a Java application, can be downloaded much faster.
Unlike tar, jar compresses by default, independent from other tools - because it is basically the Java version
of zip. In addition, it allows individual entries in an archive to be signed by the author, so that origins can be
authenticated.
The syntax is almost identical as for the tar command, we refer to info jar for specific differences.
tar, jar and symbolic links
One noteworthy feature not really mentioned in the standard documentation is that jar will follow
symbolic links. Data to which these links are pointing will be included in the archive. The default in tar
is to only backup the symbolic link, but this behavior can be changed using the -h to tar.
9.1.1.5. Transporting your data
Saving copies of your data on another host is a simple but accurate way of making backups. See Chapter 10
for more information on scp, ftp and more.
In the next section we'll discuss local backup devices.
9.2. Moving your data to a backup device
9.2.1. Making a copy on a floppy disk
9.2.1.1. Formatting the floppy
On most Linux systems, users have access to the floppy disk device. The name of the device may vary
depending on the size and number of floppy drives, contact your system admin if you are unsure. On some
systems, there will likely be a link /dev/floppy pointing to the right device, probably /dev/fd0 (the
auto-detecting floppy device) or /dev/fd0H1440 (set for 1,44MB floppies).
fdformat is the low-level floppy disk formatting tool. It has the device name of the floppy disk as an option.
fdformat will display an error when the floppy is write-protected.
emma:~> fdformat /dev/fd0H1440
Double-sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.
Formatting ... done
Verifying ... done
emma:~>
The mformat command (from the mtools package) is used to create DOS-compatible floppies which can then
be accessed using the mcopy, mdir and other m-commands.
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 148
Graphical tools are also available.
Figure 9-1. Floppy formatter
After the floppy is formatted, it can be mounted into the file system and accessed as a normal, be it small,
directory, usually via the /mnt/floppy entry.
Should you need it, install the mkbootdisk utility, which makes a floppy from which the current system can
boot.
9.2.1.2. Using the dd command to dump data
The dd command can be used to put data on a disk, or get it off again, depending on the given input and
output devices. An example:
gaby:~> dd if=images-without-dir.tar.gz of=/dev/fd0H1440
98+1 records in
98+1 records out
gaby~> dd if=/dev/fd0H1440 of=/var/tmp/images.tar.gz
2880+0 records in
2880+0 records out
gaby:~> ls /var/tmp/images*
/var/tmp/images.tar.gz
Note that the dumping is done on an unmounted device. Floppies created using this method will not be
mountable in the file system, but it is of course the way to go for creating boot or rescue disks. For more
information on the possibilities of dd, read the man pages.
This tool is part of the GNU coreutils package.
Dumping disks
The dd command can also be used to make a raw dump of an entire hard disk.
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 149
9.2.2. Making a copy with a CD-writer
On some systems users are allowed to use the CD-writer device. Your data will need to be formatted first. Use
the mkisofs command to do this in the directory containing the files you want to backup. Check with df that
enough disk space is available, because a new file about the same size as the entire current directory will be
created:
[rose@blob recordables] df -h .
Filesystem Size Used Avail Use% Mounted on
/dev/hde5 19G 15G 3.2G 82% /home
[rose@blob recordables] du -h -s .
325M .
[rose@blob recordables] mkisofs -J -r -o cd.iso .
<--snap-->
making a lot of conversions
<--/snap-->
98.95% done, estimate finish Fri Apr 5 13:54:25 2002
Total translation table size: 0
Total rockridge attributes bytes: 35971
Total directory bytes: 94208
Path table size(bytes): 452
Max brk space used 37e84
166768 extents written (325 Mb)
The -J and -r options are used to make the CD-ROM mountable on different systems, see the man pages for
more. After that, the CD can be created using the cdrecord tool with appropriate options:
[rose@blob recordables] cdrecord -dev 0,0,0 -speed=8 cd.iso
Cdrecord 1.10 (i686-pc-linux-gnu) (C) 1995-2001 Joerg Schilling
scsidev: '0,0,0'
scsibus: 0 target: 0 lun: 0
Linux sg driver version: 3.1.20
Using libscg version 'schily-0.5'
Device type : Removable CD-ROM
Version : 0
Response Format: 1
Vendor_info : 'HP '
Identification : 'CD-Writer+ 8100 '
Revision : '1.0g'
Device seems to be: Generic mmc CD-RW.
Using generic SCSI-3/mmc CD-R driver (mmc_cdr).
Driver flags : SWABAUDIO
Starting to write CD/DVD at speed 4 in write mode for single session.
Last chance to quit, starting real write in 0 seconds.
Operation starts.
Depending on your CD-writer, you now have the time to smoke^H^H^H^H^H eat a healthy piece of fruit
and/or get a cup of coffee. Upon finishing the job, you will get a confirmation message:
Track 01: Total bytes read/written: 341540864/341540864
(166768 sectors).
There are some graphical tools available to make it easier on you. One of the popular ones is xcdroast, which
is freely available from the X-CD-Roast web site and is included on most systems and in the GNU directory.
Both the KDE and Gnome desktop managers have facilities to make your own CDs.
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 150
9.2.3. Backups on/from jazz drives, USB devices and other removables
These devices are usually mounted into the file system. After the mount procedure, they are accessed as
normal directories, so you can use the standard commands for manipulating files.
In the example below, images are copied from a USB camera to the hard disk:
robin:~> mount /mnt/camera
robin:~> mount | grep camera
/dev/sda1 on /mnt/camera type vfat (rw,nosuid,nodev)
If the camera is the only USB storage device that you ever connect to your system, this is safe. But keep in
mind that USB devices are assigned entries in /dev as they are connected to the system. Thus, if you first
connect a USB stick to your system, it will be on the /dev/sda entry, and if you connect your camera after
that, it will be assigned to /dev/sdb - provided that you do not have any SCSI disks, which are also on
/dev/sd*. On newer systems, since kernel 2.6, a hotplug system called HAL (Hardware Abstraction Layer)
ensures that users don't have to deal with this burden. If you want to check where your device is, type dmesg
after inserting it.
You can now copy the files:
robin:~> cp -R /mnt/camera/* images/
robin:~> umount /mnt/camera
Likewise, a jazz drive may be mounted on /mnt/jazz.
Appropriate lines should be added in /etc/modules.conf and /etc/fstab to make this work. Refer
to specific hardware HOWTOs for more information. On systems with a 2.6.x kernel or higher, you may also
want to check the man pages for modprobe and modprobe.conf.
9.2.4. Backing up data using a tape device
This is done using tar (see above). The mt tool is used for controlling the magnetic tape device, like
/dev/st0. Entire books have been written about tape backup, therefore, refer to our reading-list in
Appendix B for more information. Keep in mind that databases might need other backup procedures because
of their architecture.
The appropriate backup commands are usually put in one of the cron directories in order to have them
executed on a regular basis. In larger environments, the freely available Amanda backup suite or a commercial
solution may be implemented to back up multiple machines. Working with tapes, however, is a system
administration task beyond the scope of this document.
9.2.5. Tools from your distribution
Most Linux distributions offer their own tools for making your life easy. A short overview:
· SuSE: YaST now includes expanded backup and restore modules.
RedHat: the File Roller tool provides visual management of (compressed) archives. They seem to be
in favour of the X-CD-Roast tool for moving backups to an external device.
·
· Mandrake: X-CD-Roast.
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 151
Most distributions come with the BSD dump and restore utilities for making backups of ext2 and
ext3 file systems. This tool can write to a variety of devices and literally dumps the file(s) or file
system bit per bit onto the specified device. Like dd, this allows for backing up special file types such
as the ones in /dev.
·
9.3. Using rsync
9.3.1. Introduction
The rsync program is a fast and flexible tool for remote backup. It is common on UNIX and UNIX-like
systems, easy to configure and use in scripts. While the r in rsync stands for "remote", you do not need to take
this all too literally. Your "remote" device might just as well be a USB storage device or another partition on
your hard disk, you do not need to have two separated machines.
9.3.2. An example: rsync to a USB storage device
As discussed in Section 3.1.2.3, we will first have to mount the device. Possibly, this should be done as root:
root@theserver# mkdir /mnt/usbstore
root@theserver# mount -t vfat /dev/sda1 /mnt/usbstore
Userfriendly
More and more distributions give access to removable devices for non-prilileged users and mount USB
devices, CD-ROMs and other removable devices automatically.
Note that this guideline requires USB support to be installed on your system. See the USB Guide for help if
this does not work. Check with dmesg that /dev/sda1 is indeed the device to mount.
Then you can start the actual backup, for instance of the /home/karl directory:
karl@theserver:~> rsync -avz /home/karl/ /mnt/usbstore
As usual, refer to the man pages for more.
9.4. Encryption
9.4.1. General remarks
9.4.1.1. Why should you encrypt data?
Encryption is synonym to secrecy. In the context of backups, encryption can be very useful, for instance if
you need to leave your backed up data in a place where you can not control access, such as the server of your
provider.
Apart from that, encryption can be applied to E-mails as well: normally, mail is not encrypted and it is often
sent in the open over the netwerk or the Internet. If your message contains sensitive information, better
encrypt it.
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 152
9.4.1.2. GNU Privacy Guard
On Linux systems you will find GnuPG, the GNU Privacy Guard, which is a suite of programs that are
compatible with the PGP (Pretty Good Privacy) tools that are commercially available.
In this guide we will only discuss the very simple usage of the encryption tools and show what you will need
in order to generate an encryption key and use it to encrypt data for yourself, which you can then safely store
in a public place. More advanced usage directions can be found in the man pages of the various commands.
9.4.2. Generate a key
Before you can start encrypting your data, you need to create a pair of keys. The pair consists of a private and
a public key. You can send the public key to correspondents, who can use it to encrypt data for you, which
you decrypt with your private key. You always keep the private key, never share it with somebody else, or
they will be able to decrypt data that is only destined for you. Just to make sure that no accidents happen, the
private key is protected with a password. The key pair is created using this command:
willy@ubuntu:~$ gpg --key-gen
gpg (GnuPG) 1.4.2.2; Copyright (C) 2005 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.
gpg: directory `/home/willy.gnupg' created
gpg: new configuration file `/home/willy/.gnupg/gpg.conf' created
gpg: WARNING: options in `/home/willy/.gnupg/gpg.conf' are not yet
active during this run
gpg: keyring `/home/willy/.gnupg/secring.gpg' created
gpg: keyring `/home/willy/.gnupg/pubring.gpg' created
Please select what kind of key you want:
(1) DSA and Elgamal (default)
(2) DSA (sign only)
(5) RSA (sign only)
Your selection? 1
DSA keypair will have 1024 bits.
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 4096
Requested keysize is 4096 bits
Please specify how long the key should be valid.
0 = key does not expire
= key expires in n days
w = key expires in n weeks
m = key expires in n month
y = key expires in n years
Key is valid for? (0) 0
Key does not expire at all
Is this correct? (y/N) y
You need a user ID to identify your key; the software constructs the
user ID from the Real Name, Comment and Email Address in this form:
"Heinrich Heine (Der Dichter) "
Real name: Willy De Wandel
Email address: wdw@mvg.vl
Comment: Willem
You selected this USER-ID:
"Willy De Wandel (Willem) "
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 153
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.
Passphrase:
Now enetr your password. This can be a phrase, the longer, the better, the only condition is that you should be
able to remember it at all times. For verification, you need to enter the same phrase again.
Now the key pair is generated by a program that spawns random numbers and that is, among other factors, fed
with the activity data of the system. So it is a good idea to start some programs now, to move the mouse
cursor or to type some random characters in a terminal window. That way, the chances to generate a number
that contains lots of different digits will be much bigger and the key will be more difficult to crack.
9.4.3. About your key
When your key has been created, you will get a message about the fingerprint. This is a sequence of 40
hexadecimal numbers, which is so long that it is very, very hard to generate the same key twice, on any
computer. You can be rather sure that this is a unique sequence. The short form of this key consists of your
name, followed by the last 8 hexadecimal numbers.
You can get information about your key as follows:
willy@ubuntu:~$ gpg --list-keys
/home/willy/.gnupg/pubring.gpg
------------------------------
pub 1024D/BF5C3DBB 2006-08-08
uid Willy De Wandel (Willem)
sub 4096g/A3449CF7 2006-08-08
The key ID of this key is "BF5C3DBB". You can send your key ID and your name to a key server, so that
other people can get this info about you and use it to encrypt data for you. Alternatively, you can send your
public key directly to the people who need it. The public part of your key is the long series of numbers that
you see when using the --export option to the gpg command:
gpg --export -a
However, as far is this guide is concerned, we assume that you only need your key in order to encrypt and
decrypt data for yourself. Read the gpg man pages if you want to know more.
9.4.4. Encrypt data
Now you can encrypt a .tar archive or a compressed archive, prior to saving it to a backup medium or
transporting it to the backup server. Use the gpg command like this:
gpg -e -r (part of) uid archive
The -e option tells gpg to encrypt, the -r option indicates who to encrypt for. Keep in mind that only only
the user name(s) following this -r option will be able to decrypt the data again. An example:
willy@ubuntu:~$ gpg -e -r Willy /var/tmp/home-willy-20060808.tar
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 154
9.4.5. Decrypting files
Using the -d option, you can decrypt files that have been encrypted for you. The data will scroll over your
screen, but an encrypted copy will remain on disk. So for file formats other than plain text, you will want to
save the decrypted data, so that you can view them with the appropriate program. This is done using the -o
option to the gpg command:
willy@ubuntu:~$ gpg -d -o /var/tmp/home-willy-decrypt.tar /var/tmp/home-willy-20060808.tar.gpg
You need a passphrase to unlock the secret key for
user: "Willy De Wandel (Willem) "
4096 ELG-E key, ID A3449CF7, created 2006-08-08 (main key ID BF5C3DBB)
gpg: encrypted with 4096-bit ELG-E key, ID A3449CF7, created 2006-08-08
"Willy De Wandel (Willem) "
No password = no data
If you can not remember your password, the data is lost. Not even the system administrator will be able
to decrypt the data. That is why a copy of important keys is sometimes kept in a sealed vault in a bank.
9.5. Summary
Here's a list of the commands involving file backup:
Table 9-1. New commands in chapter 9: Backup
Command Meaning
bzip2 A block-sorting file compressor.
cdrecord Record audio or data Compact Disks from a master.
dd Convert and copy a file
fdformat Low-level formats a floppy disk.
gpg Encrypt and decrypt data.
gzip Compress or expand files.
mcopy Copy MSDOS files to/from UNIX.
mdir Display an MSDOS directory.
mformat Add an MSDOS file system to a low-level formatted floppy disk.
mkbootdisk Creates a stand-alone boot floppy for the running system.
mount Mount a file system (integrate it with the current file system by connecting it to a mount point).
rsync Synchronize directories.
tar Tape archiving utility, also used for making archives on disk instead of on tape.
umount Unmount file systems.
Introduction to Linux
Chapter 9. Fundamental Backup Techniques 155

No comments: