Debian Reference

Osamu Aoki

This book is free; you may redistribute it and/or modify it under the terms of the GNU General Public License of any version compliant to the Debian Free Software Guidelines (DFSG).

Abstract

This Debian Reference (v2) (2009-01-17 17:51:59 UTC) is intended to provide a broad overview of the Debian system as a post-installation user's guide. It covers many aspects of system administration through shell-command examples for non-developers.


Table of Contents

1. Preface
1.1. Disclaimer
1.2. What is Debian
1.3. About this document
1.3.1. Guiding rules
1.3.2. Prerequisites
1.3.3. Conventions
1.3.4. The popcon
1.3.5. The package size
1.3.6. Bug reports
1.4. Some quotes for new users
2. GNU/Linux tutorials
2.1. Console basics
2.1.1. The shell prompt
2.1.2. The shell prompt under X
2.1.3. The root account
2.1.4. The root shell prompt
2.1.5. GUI system administration tools
2.1.6. Virtual consoles
2.1.7. How to leave the command prompt
2.1.8. How to shutdown the system
2.1.9. Recovering a sane console
2.1.10. Additional package suggestions for the newbie
2.1.11. An extra user account
2.1.12. sudo configuration
2.1.13. Play time
2.2. Unix-like filesystem
2.2.1. Unix file basics
2.2.2. Filesystem internals
2.2.3. The filesystem permission system
2.2.4. Control of permissions for newly created files: umask
2.2.5. Permissions for groups of users (group)
2.2.6. Timestamps
2.2.7. Links
2.2.8. Named pipes (FIFOs)
2.2.9. Sockets
2.2.10. Device files
2.2.11. Special device files
2.2.12. procfs and sysfs
2.3. Midnight Commander (MC)
2.3.1. Customization of MC
2.3.2. Starting MC
2.3.3. File manager in MC
2.3.4. Command-line tricks in MC
2.3.5. The internal editor in MC
2.3.6. The internal viewer in MC
2.3.7. Auto-start features of MC
2.3.8. FTP virtual filesystem of MC
2.4. The basic Unix-like work environment
2.4.1. The login shell
2.4.2. Customizing bash
2.4.3. Special key strokes
2.4.4. Unix style mouse operations
2.4.5. The pager
2.4.6. The text editor
2.4.7. Setting a default text editor
2.4.8. Customizing vim
2.4.9. Recording the shell activities
2.4.10. Basic Unix commands
2.5. The simple shell command
2.5.1. Command execution and environment variable
2.5.1.1. LANG variable
2.5.1.2. PATH variable
2.5.1.3. HOME variable
2.5.2. Command line options
2.5.3. Shell glob
2.5.4. Return value of the command
2.5.5. Typical command sequences and shell redirection
2.5.6. Command alias
2.6. Unix-like text processing
2.6.1. Unix text tools
2.6.2. Regular expressions
2.6.3. Replacement expressions
2.6.4. Extract data from text file table
2.6.5. Script snippets for piping commands
2.6.6. Perl one liner for the regular-expression substitution
3. Debian package management
3.1. Debian package management prerequisites
3.1.1. Package configuration
3.1.2. Basic precautions
3.1.3. Life with eternal upgrades
3.1.4. Debian archive basics
3.1.5. Package dependencies
3.1.6. The event flow of the package management
3.1.7. First response to package management troubles
3.2. Basic package management operations
3.2.1. Basic package management operations with commandline
3.2.2. Interactive use of aptitude
3.2.3. Key bindings of aptitude
3.2.4. Package views under aptitude
3.2.5. Search method options with aptitude
3.2.6. The aptitude regex formula
3.2.7. Dependency resolution of aptitude
3.2.8. Package activity logs
3.2.9. Aptitude advantages
3.3. Examples of aptitude operations
3.3.1. List packages with regex matching package name
3.3.2. Browse with the regex matching
3.3.3. Purge removed packages for good
3.3.4. Tidy auto/manual install status
3.3.5. System wide upgrade with aptitude
3.4. Advanced package management operations
3.4.1. Advanced management operations with commandline
3.4.2. Verify installed package files
3.4.3. Safeguard for package problems
3.4.4. Search on the package meta data
3.5. Debian package management internals
3.5.1. Archive meta data
3.5.2. Top level Release file and authenticity
3.5.3. Archive level Release files
3.5.4. Fetching of the meta data for the package
3.5.5. The package state for APT
3.5.6. The package state for aptitude
3.5.7. The local copies of the fetched packages
3.5.8. The Debian package file name
3.5.9. The dpkg command
3.5.10. The update-alternative command
3.5.11. The dpkg-statoverride command
3.5.12. The dpkg-divert command
3.6. Recovery from a broken system
3.6.1. Incompatibility with old user configuration
3.6.2. Different packages with overlapped files
3.6.3. Fixing broken package script
3.6.4. Rescue using the dpkg command
3.6.5. Recover package selection data
3.7. Tips for the package management
3.7.1. How to pick Debian packages
3.7.2. Packages from mixed source of archives
3.7.3. Tweaking candidate version
3.7.4. Volatile and Backports.org
3.7.5. Emergency downgrading
3.7.6. Who uploaded the package?
3.7.7. The equivs package
3.7.8. Port a package to the stable system
3.7.9. Proxy server for APT
3.7.10. Small public package archive
3.7.11. Record/copy system configuration
3.7.12. Convert or install an alien binary package
3.7.13. Extract package without dpkg
3.7.14. More readings for the package management
4. The system initialization
4.1. An overview of the boot strap process
4.2. Stage 1: the BIOS
4.3. Stage 2: the boot loader
4.4. Stage 3: the mini-Debian system
4.5. Stage 4: the normal Debian system
4.5.1. The meaning of the runlevel
4.5.2. The configuration of the runlevel
4.5.3. The runlevel management example
4.5.4. The default parameter for each init script
4.5.5. The hostname
4.5.6. Network interface initialization
4.5.7. Network service initialization
4.5.8. The system message
4.5.9. The kernel message
4.5.10. The udev system
4.5.11. The kernel module initialization
5. Authentication
5.1. Normal Unix authentication
5.2. Manage account and password information
5.3. Good password
5.4. Creating encrypted password
5.5. PAM and NSS
5.5.1. Configuration files accessed by the PAM and NSS
5.5.2. The modern centralized system management
5.5.3. "Why GNU su does not support the wheel group"
5.5.4. Stricter password rule
5.6. Other access controls
5.6.1. sudo
5.6.2. SELinux
5.6.3. Restricting access to some server services
5.7. Security of authentication
5.7.1. Secure password over the Internet
5.7.2. Secure Shell
5.7.3. Extra security measures for the Internet
5.7.4. Securing the root password
6. Network setup
6.1. The basic network infrastructure
6.1.1. The domain name
6.1.2. The hostname resolution
6.1.3. The network interface name
6.1.4. The network address range for the LAN
6.1.5. The network configuration infrastructure
6.1.6. The network device support
6.2. The network connection method
6.2.1. The DHCP connection with the Ethernet
6.2.2. The static IP connection with the Ethernet
6.2.3. The PPP connection with pppconfig
6.2.4. The alternative PPP connection with wvdialconf
6.2.5. The PPPoE connection with pppoeconf
6.3. The basic network configuration with ifupdown
6.3.1. The command syntax simplified
6.3.2. The basic syntax of /etc/network/interfaces
6.3.3. The loopback network interface
6.3.4. The network interface served by the DHCP
6.3.5. The network interface with the static IP
6.3.6. The basics of wireless LAN interface
6.3.7. The wireless LAN interface with WEP
6.3.8. The wireless LAN interface with WPA/WPA2
6.3.9. The PPP connection
6.3.10. The alternative PPP connection
6.3.11. The PPPoE connection
6.3.12. The network configuration state of ifupdown
6.3.13. The basic network reconfiguration
6.3.14. The ifupdown-extra package
6.4. The advanced network configuration with ifupdown
6.4.1. The ifplugd
6.4.2. The ifmetric
6.4.3. The virtual interface
6.4.4. The advanced command syntax
6.4.5. The mapping stanza
6.4.6. The manually switchable network configuration
6.4.7. Scripting with the ifupdown
6.4.8. Mapping with guessnet
6.5. The network configuration for desktop
6.5.1. Automatic network configuration
6.5.2. GUI network configuration tools
6.6. The low level network configuration
6.6.1. Iproute2 commands
6.6.2. Safe lower level network operations
6.7. Network optimization
6.7.1. Finding optimal MTU
6.7.2. Setting MTU
6.7.3. WAN TCP optimization
6.8. Netfilter
7. Network applications
7.1. Web browsers
7.1.1. Browser configuration
7.2. The mail system
7.2.1. Modern mail service basics
7.2.2. Basic mail software choice
7.2.3. The mail configuration strategy for workstation
7.2.3.1. The configuration of exim4
7.2.3.2. The configuration of postfix with SASL
7.2.3.3. The mail address configuration
7.2.4. Tips for managing the mail
7.2.4.1. Basic MTA operations
7.2.4.2. Basic MUA -- Mutt
7.2.4.3. Redeliver mbox contents
7.2.5. Choices of software for the mail
7.2.5.1. MTA
7.2.5.2. MUA
7.2.5.3. The remote mail retrieval and forward utility
7.2.5.4. MDA
7.2.5.5. POP3/IMAP4 server
7.3. The print server and utility
7.4. The remote access server and utility (SSH)
7.4.1. Basics of SSH
7.4.2. Port forwarding for SMTP/POP3 tunneling
7.4.3. Connecting with fewer passwords -- RSA
7.4.4. Dealing with alien SSH clients
7.4.5. Setting up ssh-agent
7.4.6. Troubleshooting SSH
7.5. Other network application servers
7.6. Other network application clients
7.7. The diagnosis of the system daemons
8. The X window system
8.1. Key packages
8.2. Setting up desktop environment
8.2.1. Debian menu
8.2.2. Freedesktop.org menu
8.2.3. Debian menu under GNOME desktop environment
8.3. The server/client relationship
8.4. The X server
8.4.1. The (re)configuration of the X server
8.4.2. The connection methods to the X server
8.5. Starting the X window system
8.5.1. Starting X session with gdm
8.5.2. Customizing the X session (classic method)
8.5.3. Customizing the X session (new method)
8.5.4. Connecting a remote X client via SSH
8.5.5. Secure X terminal via Internet
8.6. Fonts in the X window
8.6.1. Basic fonts
8.6.2. Additional fonts
8.6.3. CJK fonts
8.7. X applications
8.7.1. X office applications
8.7.2. X utility applications
8.8. The X trivia
8.8.1. Keymaps and pointer button mappings in X
8.8.2. The classic X clients
8.8.3. The X terminal emulator -- xterm
8.8.4. Running X clients as root
9. The I18N and L10N
9.1. The keyboard input
9.1.1. The input method support with scim
9.1.2. An example for Japanese
9.1.3. Disabling the input method
9.2. The display output
9.3. The locale
9.3.1. Basics of encoding
9.3.2. Rationale for UTF-8 locale
9.3.3. The reconfiguration of the locale
9.3.4. Configuring system with LANG
9.3.5. Specific locale only under X Window
9.3.6. Filename encoding
9.3.7. Localized messages and translated documentation
9.3.8. Effects of locale
10. System tips
10.1. The screen program
10.1.1. The use scenario for the screen command
10.1.2. Key bindings for the screen command
10.1.3. A screen-like program for X window system
10.2. Data recording and presentation
10.2.1. The log daemon
10.2.2. Log analyzer
10.2.3. Recording the shell activities cleanly
10.2.4. Customized display of text data
10.2.5. Colorized shell echo
10.2.6. Colorized commands
10.2.7. Recording the graphic image of an X application
10.2.8. Recording changes in configuration files
10.3. Data storage tips
10.3.1. Partition configuration
10.3.2. Accessing partition using UUID
10.3.3. Filesystem configuration
10.3.4. Filesystem creation and integrity check
10.3.5. Optimization of filesystem by mount options
10.3.6. Optimization of filesystem via superblock
10.3.7. Optimization of harddisk
10.3.8. Expand usable storage space via LVM
10.3.9. Expand usable storage space by mounting another partition
10.3.10. Expand usable storage space using symlink
10.3.11. Expand usable storage space using aufs
10.4. Data encryption tips
10.4.1. Removable disk encryption with dm-crypt/LUKS
10.4.2. Encrypted swap partition with dm-crypt
10.4.3. Automatically encrypting files with eCryptfs
10.4.4. Automatically mounting eCryptfs
10.5. Monitoring, controlling, and starting program activities
10.5.1. Time a process
10.5.2. The scheduling priority
10.5.3. The ps command
10.5.4. The top command
10.5.5. List files opened by a process
10.5.6. Trace program activities
10.5.7. Identify processes using files or sockets
10.5.8. Repeating a command with a constant interval
10.5.9. Repeating a command looping over files
10.5.10. Starting a program from GUI
10.5.11. Customizing program to be started
10.5.12. Kill a process
10.5.13. Schedule tasks once
10.5.14. Schedule tasks regularly
10.5.15. Alt-SysRq
10.6. System maintenance tips
10.6.1. Who is logged on
10.6.2. Warn everyone
10.6.3. The hardware identification
10.6.4. The hardware configuration
10.6.5. System and hardware time
10.6.6. The terminal configuration
10.6.7. The sound infrastructure
10.6.8. Disable the screen saver
10.6.9. Disable the sound (beep)
10.6.10. Memory usage
10.6.11. System security and integrity check
10.7. The kernel
10.7.1. Linux kernel 2.6
10.7.2. Kernel headers
10.7.3. Kernel and module compile
10.7.4. Kernel source compile: Debian standard method
10.7.5. Module source compile: Debian standard method
10.7.6. Kernel source compile: classic method
10.7.7. Non-free hardware drivers
10.8. The chroot
10.8.1. Run a different Debian distribution with chroot
10.8.2. Setting up login for chroot
10.8.3. Setting up X for chroot
10.8.4. Run other distributions with chroot
10.8.5. Build packages under chroot
10.8.6. Other virtualization tools
11. Data management
11.1. Sharing, copying, and archiving
11.1.1. Archive and compression tools
11.1.2. Copy and synchronization tools
11.1.3. Idioms for the archive
11.1.4. Idioms for the copy
11.1.5. Idioms for the selection of files
11.1.6. Backup and recovery
11.1.7. Backup utility suites
11.1.8. An example script for the system backup
11.1.9. A copy script for the data backup
11.1.10. Removable mass storage device
11.1.11. Sharing data via network
11.1.12. Archive media
11.2. The binary data
11.2.1. Make the disk image file
11.2.2. Writing directly to the disk
11.2.3. View and edit binary data
11.2.4. Mount the disk image file
11.2.5. Manipulating files without mounting disk
11.2.6. Make the ISO9660 image file
11.2.7. Writing directly to the CD/DVD-R/RW
11.2.8. Mount the ISO9660 image file
11.2.9. Split a large file into small files
11.2.10. Clear file contents
11.2.11. Dummy files
11.2.12. Erase entire harddisk
11.2.13. Undelete deleted but still open file
11.2.14. Searching all hardlinks
11.2.15. Invisible disk space consumption
11.3. Data security infrastructure
11.3.1. Key management for Gnupg
11.3.2. Using GnuPG with files
11.3.3. Using GnuPG with Mutt
11.3.4. Using GnuPG with Vim
11.3.5. The MD5 sum
11.4. Source code merge tools
11.4.1. Extract differences for source files
11.4.2. Merge updates for source files
11.4.3. 3 way merge updates
11.5. Version control systems
11.5.1. Native VCS commands
11.5.2. CVS
11.5.2.1. Installing a CVS server
11.5.2.2. Use local CVS server
11.5.2.3. Use remote CVS pserver
11.5.2.4. Anonymous CVS (download only)
11.5.2.5. Use remote CVS through ssh
11.5.2.6. Create a new CVS archive
11.5.2.7. Work with CVS
11.5.2.8. Export files from CVS
11.5.2.9. Administer CVS
11.5.2.10. File permissions in repository
11.5.2.11. Execution bit
11.5.3. Subversion
11.5.3.1. Installing a Subversion server
11.5.3.2. Setting up a repository
11.5.3.3. Configuring Apache2
11.5.3.4. Subversion usage examples
11.5.3.5. Create a new Subversion archive
11.5.3.6. Working with Subversion
11.5.4. Git
11.5.4.1. Before using Git
11.5.4.2. Git references
11.5.4.3. Git commands
11.5.4.4. Git for recording configuration history
12. Data conversion
12.1. Text data conversion tools
12.1.1. To convert a text file with iconv
12.1.2. To convert file names with iconv
12.1.3. EOL conversion
12.1.4. TAB conversion
12.1.5. Editors with auto-conversion
12.1.6. Plain text extraction
12.1.7. Highlighting and formatting plain text data
12.2. XML data
12.2.1. Basic hints for XML
12.2.2. XML processing
12.2.3. The XML data extraction
12.3. Printable data
12.3.1. The Ghostscript
12.3.2. Merge two PS or PDF files
12.3.3. Printable data utilities
12.3.4. Printing with CUPS
12.4. Type setting
12.4.1. roff typesetting
12.4.2. TeX/LaTeX
12.4.3. Pretty print a manual page
12.4.4. Creating a manual page
12.5. The mail data conversion
12.5.1. Mail data basics
12.6. Graphic data tools
12.7. Miscellaneous data conversion
13. Programming
13.1. The shell script
13.1.1. POSIX shell compatibility
13.1.2. Shell parameters
13.1.3. Shell conditionals
13.1.4. Shell loops
13.1.5. The shell command-line processing sequence
13.1.6. Utility programs for shell script
13.1.7. Shell script dialog
13.1.8. Shell script example with zenity
13.2. Make
13.3. C
13.3.1. Simple C program (gcc)
13.3.2. Debugging with gdb
13.3.2.1. Basic gdb execution
13.3.2.2. Debugging the Debian package
13.3.2.3. Obtaining backtrace
13.3.2.4. Advanced gdb commands
13.3.2.5. Debugging X Errors
13.3.3. Check dependency on libraries
13.3.4. Debugging with memory leak detection tools
13.3.5. Disassemble binary
13.3.6. Flex -- a better Lex
13.3.7. Bison -- a better Yacc
13.4. Autoconf
13.4.1. Compile and install a program
13.4.2. Uninstall program
13.5. Perl short script madness
13.6. Web
13.7. Static code analysis tools
13.8. The source code translation
13.9. Making Debian package
A. Appendix
A.1. The Debian maze
A.2. Copyright history
A.3. Document format

List of Tables

2.1. List of interesting text-mode program packages.
2.2. List of informative documentation packages.
2.3. List of usage of key directories.
2.4. List of the first character of "ls -l" output
2.5. The numeric mode for file permissions in chmod(1) commands.
2.6. The umask value examples.
2.7. List of example system-provided groups for file access.
2.8. List of notable system provided groups for particular command executions.
2.9. List of types of timestamps.
2.10. The device types.
2.11. List of special device files.
2.12. The key bindings of MC.
2.13. The reaction to the enter key in MC.
2.14. List of shell programs.
2.15. List of key bindings for bash.
2.16. The Unix style mouse operations.
2.17. List of basic Unix commands.
2.18. The 3 parts of locale value.
2.19. List of locale recommendations.
2.20. List of HOME values.
2.21. The shell glob patterns.
2.22. Command exit code.
2.23. The shell command idioms.
2.24. The predefined file descriptors.
2.25. The metacharacters for BRE and ERE.
2.26. The replacement expression.
2.27. The script snippets for piping commands.
2.28. The comparison of ed vs perl for in-place editing.
3.1. List of Debian package management tools
3.2. Lists of Debian archive sites.
3.3. The lists of Debian archive components.
3.4. The relationship between suite and codename.
3.5. List of package dependencies.
3.6. List of key web site to resolving problems with a specific package.
3.7. Package management operations with commandline using aptitude and apt-get / apt-cache.
3.8. Notable command options for "aptitude".
3.9. List of key bindings for aptitude.
3.10. Views for aptitude.
3.11. The categorization of standard aptitude views.
3.12. List of the aptitude regex formula.
3.13. The log files for package activities.
3.14. List of advanced package management operations.
3.15. The content of the Debian archive meta data.
3.16. The name structures of the Debian packages.
3.17. The usable characters for each component in the Debian package names.
3.18. The notable files for dpkg.
3.19. List of essential default Pin-Priority values.
3.20. List of the proxy tools specially for Debian archive
4.1. List of boot loaders.
4.2. The meaning of GRUB parameters.
4.3. List of runlevels and meanings.
4.4. List of kernel error levels.
5.1. The 3 important configuration files for pam_unix.o.
5.2. The second entry content of /etc/passwd.
5.3. List of commands to manage account information.
5.4. List of tools to generate password.
5.5. List of notable PAM and NSS systems.
5.6. List of configuration files accessed by the PAM.
5.7. List of insecure and secure services and ports.
5.8. List of tools to provide extra security measures.
6.1. List of network address ranges.
6.2. List of network configuration tools.
6.3. List of network connection types and connection paths.
6.4. List of network connection configurations.
6.5. List of network connection acronyms.
6.6. List of configuration files for the PPP connection with pppconfig.
6.7. List of configuration files for the PPP connection with wvdialconf.
6.8. List of configuration files for the PPPoE connection with pppoeconf.
6.9. List of basic network configuration commands with ifupdown.
6.10. List of stanzas in /e/n/i
6.11. List of acronyms for WLAN.
6.12. List of terminology for network devices.
6.13. List of advanced network configuration commands with ifupdown.
6.14. List of environment variables passed by the ifupdown system
6.15. Translation table from obsolete net-tools commands to new iproute2 commands.
6.16. List of lower level network commands.
6.17. List of network optimization tools.
6.18. Basic guide lines of the optimal MTU value
6.19. List of firewall tools.
7.1. List of web browsers.
7.2. List of browser plugin packages.
7.3. List of popular mail system for workstation.
7.4. List of important postfix manual pages
7.5. List of mail address related configuration files.
7.6. List of basic MTA operation.
7.7. List of MTA.
7.8. List of MUA.
7.9. List of remote mail retrieval and forward utilities.
7.10. List of MDA.
7.11. List of POP3/IMAP4 servers.
7.12. List of print servers and utilities.
7.13. List of remote access server and utilities.
7.14. List of SSH authentication protocols and methods.
7.15. List of SSH configuration files.
7.16. List of SSH client startup examples.
7.17. List of free SSH clients for other platforms.
7.18. List of other network application servers.
7.19. List of network application clients.
7.20. List of popular RFCs.
8.1. List of key (meta)packages for X window.
8.2. List of server/client terminology.
8.3. List of connection methods to the X server.
8.4. Table of packages to support X window font systems.
8.5. Table of corresponding PostScript Type 1 fonts.
8.6. Table of corresponding TrueType fonts.
8.7. Table of key words used in CJK font names to indicate font types.
8.8. List of basic X office applications
8.9. List of basic X utility applications
9.1. List of keyboard reconfiguration methods.
9.2. List of input method supports with scim.
10.1. List of programs to support interrupted network connections.
10.2. List of key bindings for screen.
10.3. List of system log analyzers.
10.4. List of graphic image manipulation tools.
10.5. List of packages to record configuration history in VCS.
10.6. List of disk partition management packages
10.7. List of filesystem management packages
10.8. List of data encryption utilities.
10.9. List of tools for monitoring and controlling program activities
10.10. List of nice values for the scheduling priority.
10.11. List of ps command styles.
10.12. List of commands for top.
10.13. List of frequently used signals for kill command.
10.14. List of SAK command keys.
10.15. List of hardware identification tools.
10.16. List of hardware configuration tools.
10.17. List of sound packages
10.18. List of commands for disabling the screen saver.
10.19. List of memory sizes reported.
10.20. List of tools for system security and integrity check
10.21. List of key packages to be installed for the kernel recompilation on the Debian system
10.22. List of virtualization tools
11.1. List of archive and compression tools.
11.2. List of copy and synchronization tools.
11.3. List of backup suite utilities.
11.4. List of packages which permit normal users to mount removable devices without a matching /etc/fstab entry.
11.5. List of filesystem choices for removable storage devices with typical usage scenarios.
11.6. List of the network service to chose with the typical usage scenario.
11.7. List of packages which view and edit binary data.
11.8. List of packages to manipulate files without mounting.
11.9. List of data security infrastructure tools.
11.10. List of GNU Privacy Guard commands for the key management
11.11. List of the meaning of trust code.
11.12. List of gnu privacy guard commands on files
11.13. List of source code merge tools.
11.14. List of version control system tools.
11.15. Comparison of native VCS commands.
11.16. Assumption for the CVS archive.
11.17. Notable options for CVS commands (use as first argument(s) to cvs).
11.18. List of git packages and commands.
12.1. List of text data conversion tools.
12.2. List of encoding values and their usage.
12.3. List of EOL conversion tools.
12.4. List of TAB conversion commands from bsdmainutils and coreutils packages.
12.5. List of tools to extract plain text data.
12.6. List of tools to highlight plain text data.
12.7. List of predefined entities for XML.
12.8. List of XML tools.
12.9. List of DSSL tools.
12.10. List of XML data extraction tools.
12.11. List of XML pretty print tools.
12.12. List of Ghostscript PostScript interpreters.
12.13. List of printable data utilities.
12.14. List of type setting tools.
12.15. List of packages to help creating the manpage.
12.16. List of packages to help mail data conversion.
12.17. List of graphic data tools.
12.18. List of miscellaneous data conversion tools.
13.1. List of packages to help programing.
13.2. List of bashizms.
13.3. List of shell parameters.
13.4. List of shell parameter expansions.
13.5. List of key shell parameter substitutions.
13.6. List of file comparison operators in the conditional expression.
13.7. List of string comparison operators in the conditional expression.
13.8. List of user interface programs.
13.9. List of make automatic variables.
13.10. List of make variable expansions.
13.11. List of advanced gdb commands
13.12. List of memory leak detection tools
13.13. List of Yacc-compatible LALR parser generators
13.14. List of tools for static code analysis
13.15. List of source code translation tools.

Chapter 1. Preface

This Debian Reference (version 2) (2009-01-17 17:51:59 UTC) is intended to provide a broad overview of Debian system administration as a post-installation user guide.

The target reader is someone who is willing to learn shell scripts but who is not ready to read all the C sources to figure out how the GNU/Linux system works.

1.1. Disclaimer

All warranties are disclaimed. All trademarks are property of their respective trademark owners.

The Debian system itself is a moving target. This makes its documentation difficult to be current and correct. Although the current unstable version of Debian system was used as the basis for writing this, some contents may be already outdated by the time you read this.

Please treat this document as the secondary reference. This document does not replace any authoritative guides. The author and contributors do not take responsibility for consequences of errors, omissions or ambiguity in this document.

1.2. What is Debian

The Debian Project is an association of individuals who have made common cause to create a free operating system. It's distribution is characterized by:

Free Software pieces in Debian come from GNU, Linux, BSD, X, Berkeley DB, ISC, Apache, Ghostscript, Common Unix Printing System , Samba, GNOME, KDE, Mozilla, OpenOffice.org, Vim, TeX, LaTeX, DocBook, Perl, Python, Tcl, Java, Ruby, PHP, MySQL and many more independent free software projects. Debian integrates this diversity of Free Software into one system.

1.3. About this document

1.3.1. Guiding rules

Following guiding rules were followed while compiling this document:

  • don't explain in details what is well documented elsewhere (in the judgment of the author)

  • Provide overview and skip corner cases (Big Picture)

  • Keep It Short and Simple. (KISS)

  • Focus on non-GUI tools and consoles. (Use shell examples)

  • Do not reinvent the wheel. (Use pointers to the existing references)

  • Be objective. (Use popcon etc.)

I tried to elucidate hierarchical aspects and lower levels of the system.

1.3.2. Prerequisites

You are required to seek help from (in approximate order of importance, starting with the most important sources):

[Note] Note

For detailed documentation, you may need to install the corresponding documentation package named with "-doc" as its suffix.

1.3.3. Conventions

This document refers to some command names followed by the number in parenthesis , such as bash(1). This means that you are encouraged to obtain information from the manpage by typing the following on the shell command prompt:

$ man 1 bash 

This document provides information through the following simplified presentation style with bash(1) shell command examples and bullets:

# <command in root account>
$ <command in user account>
  • <description of action>

These shell prompts distinguish account used and correspond to PS1='\$' and PS2=' ' environment variables. These values are chosen for the sake of readability of this document and are not typical on actual installed system.

[Note] Note

See the meaning of the PS1 and PS2 environment variables in the manpage of bash(1) command.

1.3.4. The popcon

The popcon data was presented as the objective measure for the popularity of each package. It was downloaded on 2009-01-18 and contains the total submission of 79097 reports over 91823 binary packages and 20 architectures.

[Note] Note

Please note that the amd64 unstable archive contains only 24396 packages currently. The popcon data contains reports from many old system installations.

The popcon number preceded with "V:" for "votes" is calculated by 100 * (the popcon submissions for the package executed recently on the PC)/(the total popcon submissions).

The popcon number preceded with "I:" for "installs" is calculated by 100 * (the popcon submissions for the package installed on the PC)/(the total popcon submissions).

[Note] Note

The popcon figures should not be considered as absolute measures of the importance of packages. There are many factors which can skew statistics. For example, some system participating popcon may have mounted directories such as /bin with "noatime" option for system performance improvement and effectively disabled "vote" from such system.

1.3.5. The package size

The package size data was also presented as the objective measure for each package. It is based on the "Installed-Size:" reported by "apt-cache show " command (currently on amd64 architecture for the unstable release). The reported size is in KB (unit for 1024 bytes).

[Note] Note

A package with a small numerical package size may indicate that the package in the unstable release has a dummy package which installs other packages with significant contents by the dependency. The dummy package enables a smooth transition or split of the package.

1.3.6. Bug reports

Please file bug reports on "debian-reference" package using reportbug(1) if you find any issues. For simple spell errors and grammar corrections, you may alternatively edit the source text available as Debian Reference wiki page at http://wiki.debian.org/DebianReference and tell me to update the document.

1.4. Some quotes for new users

Here are some interesting quotes from the Debian mailing list which may help enlighten new users:

  • "This is Unix. It gives you enough rope to hang yourself." --- Miquel van Smoorenburg <miquels at cistron.nl>

  • "Unix IS user friendly... It's just selective about who its friends are." --- Tollef Fog Heen <tollef at add.no>

Chapter 2. GNU/Linux tutorials

I think learning a computer system is like learning a new foreign language. Although tutorial books and documentation are helpful, you have to practice it yourself. In order to help you get started smoothly, I will elaborate a few basic points.

The powerful design of Debian GNU/Linux comes from the Unix operating system, i.e., a multiuser, multitasking operating system. You must learn to take advantage of the power of these features and the similarities between Unix and GNU/Linux.

Don't shy away from Unix oriented texts and don't rely solely on GNU/Linux texts, as this will rob you of much useful information.

"Rute User's Tutorial and Exposition", in the Debian non-free archive as rutebook package (popcon: I:0.2), provides a good online resource to the generic system administration.

[Note] Note

If you have been using any Unix-like system for a while with command line tools, you probably know everything I explain here. Please use this as a reality check and refresher.

2.1. Console basics

2.1.1. The shell prompt

Upon starting the system, you are presented with the character based login screen if you did not install X Window System with the display manager such as gdm. Suppose your hostname is foo, the login prompt looks like:

foo login:

If you did install a GUI environment such as Gnome of KDE, then you can get to a prompt by Ctrl-Alt-F1, and you can return to the GUI screen via Alt-F7 (see Virtual Consoles below for more information).

Following what you selected during the installation process, you type your username, e.g. penguin, and press the Enter-key, then type your password and press the Enter-key again.

[Note] Note

Following the Unix tradition, the username and password of the Debian system are case sensitive. The username is usually chosen only from the lowercase.

Now you are in the shell. The shell interprets your commands. The system starts with the greeting message stored in /etc/motd (Message Of The Day) and with the command prompt as:

Debian GNU/Linux lenny/sid foo tty1
foo login: penguin
Password:
Last login: Sun Apr 22 09:29:34 2007 on tty1
Linux snoopy 2.6.20-1-amd64 #1 SMP Sun Apr 15 20:25:49 UTC 2007 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
foo:~$

Here, the main part of the greeting message can be customized by editing the /etc/motd.tail file. The first line is generated from the system information using "uname -snrvm".

2.1.2. The shell prompt under X

If you installed X Window System with a display manager such as Gnome's gdm by selecting "Desktop environment" task during the installation, you will be presented with the graphical login screen upon starting your system. You type your username and your password to login to the non-privileged user account. Use tab to navigate between username and password, or use the mouse and primary click.

You can gain the shell prompt under X by starting a x-terminal-emulator program such as gnome-terminal, rxvt or xterm. Under the Gnome Desktop environment, clicking "Applications" -> "Accessories" -> "Terminal" does the trick.

You can also see the section below Section 2.1.6, “Virtual consoles”.

Under some other Desktop systems (like fluxbox), there may be no obvious starting point for the menu. If this happens, just try (right) clicking the center of the screen and hope for a menu to pop-up.

2.1.3. The root account

The root account is also called superuser or privileged user. From this account, you can perform the following system administration activities:

  • read, write, and remove any files on the system irrespective of their file permissions

  • set file ownership and permission of any files on the system

  • set the password of any non-privileged users on the system

  • login to any accounts without their passwords

This unlimited power of root account requires you to be considerate and responsible when using it.

[Note] Note

The file permission of the file (including hardware devices such as CD-ROM etc. which are just another file for the Debian system) may render them unusable or inaccessible by non-root users. Although the use of root account is a quick way to test this kind of situation, the resolution of this situation should be done through proper setting of the access permission and the group.

[Warning] Warning

Never share the root password with others.

2.1.4. The root shell prompt

Here are a few basic methods to gain the root shell prompt by using the root password:

  • At the character based login prompt, you simply type root.

  • Under the Gnome Desktop environment, click "Applications" -> "Accessories" -> "Root Terminal".

  • From any user shell prompt, type "su -l". (This does not preserve the environment of the current user)

  • From any user shell prompt, type "su". (This preserves most of the environment of the current user)

2.1.5. GUI system administration tools

When your desktop menu does not start GUI system administration tools automatically with the appropriate privilege, you can start them from the root shell prompt of the X terminal emulator, such as gnome-terminal, rxvt, or xterm. See Section 2.1.4, “The root shell prompt”.

[Warning] Warning

Never start the X server under the root account by typing in root to the prompt of the display manager such as gdm because it is considered unsafe (insecure), even when you plan to perform administrative activities. The entire X architecture is considered insecure if run as root. You must always use the lowest privilege level possible, like a regular user account.

[Warning] Warning

Never run untrusted remote GUI program under X window when critical information is displayed since it may eavesdrop your X screen.

2.1.6. Virtual consoles

In the default Debian system, there are six switchable VT100-like character consoles available to start the command shell directly on the Linux host. Unless you are in a GUI environment, you can switch between the virtual consoles by pressing the Left-Alt-key and one of the F1--F6 keys simultaneously. Each character console allows independent login to the account and offers the multiuser environment. This multiuser environment is a great Unix feature, and very addictive.

If you are under the X Window System, you gain access to the character console 1 by pressing Ctrl-Alt-F1 key, i.e., the left-Ctrl-key, the left-Alt-key, and the F1-key are pressed together. You can get back to the X Window System, normally running on the virtual console 7, by pressing Alt-F7.

You can alternatively change to another virtual console, e.g. to the console 1, by the command:

# chvt 1

2.1.7. How to leave the command prompt

You type Ctrl-D, i.e., the left-Ctrl-key and the d-key pressed together, at the command prompt to close the shell activity. If you are at the character console, you will return to the login prompt with this. Even though these control characters are referred as "control D" with the upper case, you do not need to press the Shift-key. The short hand expression, ^D, is also used for Ctrl-D. Alternately, you can type "exit".

If you are at x-terminal-emulator, you can close x-terminal-emulator window with this.

2.1.8. How to shutdown the system

Just like any other modern OS where the file operation involves caching data in memory for improved performance, the Debian system needs the proper shutdown procedure before power can safely be turned off. This is to maintain the integrity of files, by forcing all changes in memory to be written to disk. If the software power control is available, the shutdown procedure automatically turns off power of the system. (Otherwise, you may have to press power button for few seconds after the shutdown procedure.)

Under the normal multiuser mode, use following from the root command prompt to shutdown the system:

# shutdown -h now

Under the single-user mode, use following from the root command prompt to shutdown the system:

# poweroff -i -f

Alternatively, you may type Ctrl-Alt-Delete (The left-Ctrl-key, the left-Alt-Key, and the Delete are pressed together) to shutdown if /etc/inittab contains "ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -h now" in it. See manpage for inittab(5) for details.

2.1.9. Recovering a sane console

When the screen goes berserk after doing some funny things such as "cat <some-binary-file>", type "reset" at the command prompt. You may not be able to see the command echoed as you type. You may also issue "clear" to clean up the screen.

2.1.10. Additional package suggestions for the newbie

Although even the minimal installation of the Debian system without any desktop environment tasks provides the basic Unix functionality, it is a good idea to install few additional commandline and curses based character terminal packages such as mc and vim with aptitude for beginners to get started. From the shell prompt as root:

# aptitude update
...
# aptitude install mc vim sudo
...

If you already had these packages installed, nothing will be installed.

Table 2.1. List of interesting text-mode program packages.

package

popcon

size

description

mc

V:10, I:25

6328

A text-mode full-screen file manager

vim

V:12, I:29

1736

Unix text editor Vi IMproved, a programmers text editor (standard version)

vim-tiny

V:16, I:89

828

Unix text editor Vi IMproved, a programmers text editor (compact version)

emacs21

V:4, I:9

8176

GNU project Emacs, the Lisp based extensible text editor (version 21)

emacs22

V:2, I:4

10992

GNU project Emacs, the Lisp based extensible text editor (version 22)

w3m

V:20, I:85

1968

Text-mode WWW browsers

gpm

V:3, I:5

564

The Unix style cut-and-paste on the text console (daemon)


It may be a good idea to read some informative documentations.

Table 2.2. List of informative documentation packages.

package

popcon

size

description

doc-debian

I:84

376

Debian Project documentation, (Debian FAQ) and other documents

debian-policy

I:3

2732

Debian Policy Manual and related documents

developers-reference

I:1.4

1308

Guidelines and information for Debian developers

maint-guide

I:1.0

644

Debian New Maintainers' Guide

debian-history

I:0.4

2544

History of the Debian Project

debian-faq

I:20

1190

Debian FAQ

doc-linux-text

I:83

8616

Linux HOWTOs and FAQ (text)

doc-linux-html

I:1.2

62564

Linux HOWTOs and FAQ (html)

sysadmin-guide

I:0.4

964

The Linux System Administrators' Guide

rutebook

I:0.2

8264

Linux: Rute User's Tutorial and Exposition (non-free)


You can install some of these packages by issuing the following command from the root shell prompt:

# aptitude install package_name

2.1.11. An extra user account

If you do not want to use your main user account for the following training activities, you can create a training user account, e.g. fish. Type at root shell prompt:

# adduser fish
  • answer all the questions

This will create a new account named as fish. After your practice, you can remove this user account and its home directory by:

# deluser --remove-home fish

2.1.12. sudo configuration

For the typical single user workstation such as the desktop Debian system on the laptop PC, it is common to deploy simple configuration of sudo(8) as follows to let the non-privileged user, e.g. penguin, to gain administrative privilege just with his user password (not with the root password).

# echo "penguin  ALL=(ALL) ALL" >> /etc/sudoers

Since the sudo(8) command normally use normal user's environment variables to execute commands and normal user's "$PATH" variable is set to "/usr/local/bin:/usr/bin:/bin:/usr/games" by the system default, you should also adjust "$PATH" variable as:

$ echo 'PATH=$PATH:/usr/sbin:/sbin' >> ~/.bashrc

This trick should only be used for the single user workstation which you administer and where you are the only user.

[Warning] Warning

Do not set up accounts of regular users on multiuser workstation like this because it would be very bad for system security.

[Caution] Caution

The password and the account of the penguin in the above example requires as much protection as the root password and the root account.

[Caution] Caution

Administrative privilege in this context belongs to someone authorized to perform the system administration task on the workstation. Never give some manager in the Admin department of your company or your boss such privilege unless they are authorized and capable.

[Note] Note

For providing access privilege to limited devices and limited files, you should consider to use group to provide limited access instead of using the root privilege via sudo(8).

[Note] Note

With more thoughtful and careful configuration, the sudo facility may provide means to grant limited administrative privileges to other users on a shared system without sharing the root password. This can help with accountability with hosts with multiple administrators so you can tell who did what. On the other hand, you might not want anyone else to have such privileges.

2.1.13. Play time

Now you are ready to play with the Debian system without risks as long as you use the non-privileged user account.

This is because the Debian system is, even after the default installation, configured with the proper file permissions which prevent non-privileged users from damaging the system. Of course, there may still be some holes which can be exploited but those who worry about these issues should not be reading this section but should be reading Securing Debian Manual.

We will review basic Unix filesystem theory first. Then we will learn the Debian system with the easy way using Midnight Commander (MC) and with the proper Unix-like ways.

2.2. Unix-like filesystem

In GNU/Linux and other Unix-like operating systems, files are organized into directories. All files and directories are arranged in one big tree, the file hierarchy, rooted at /. It's called a tree because if you draw the file system, it looks like a tree (upside down).

These files and directories can be spread out over several devices. The mount(8) command serves to attach the file system found on some device to the big file tree. Conversely, the umount(8) command will detach it again. On recent Linux kernels, mount(8) operation can bind part of the file hierarchy somewhere else or can mount filesystem as shared, private, slave, or unbindable. Supported mount options for each filesystem are available in /share/doc/linux-doc-2.6.*/Documentation/filesystems/.

Directories on Unix systems are called folders on some other systems. Please also note that there is no concept for drive such as A: on any Unix system. There is one file system, and everything is included. This is a huge advantage compared to Windows, so consider yourself lucky.

2.2.1. Unix file basics

Here are Unix file basics:

  • Filenames are case sensitive. That is, MYFILE and MyFile are different files.

  • The root directory is referred to as simply /. Don't confuse this with the root user or the home directory for the root user: /root. Note that both entities are commonly referred to as "root". The context of the usage should make it clear which is meant.

  • Every directory has a name which can contain any letters or symbols except /. The root directory is an exception; its name is / (pronounced "slash" or "the root directory") and it cannot be renamed.

  • Each file or directory is designated by a fully-qualified filename, absolute filename, or path, giving the sequence of directories which must be passed through to reach it. The three terms are synonymous.

  • All fully-qualified filenames begin with the / directory, and there's a / between each directory or file in the filename. The first / is the top level directory, and the other /'s separate successive subdirectories, until we reach the last entry which is the name of the actual file. The words used here can be confusing. Take the following fully-qualified filename as an example: /usr/share/keytables/us.map.gz. However, people will also refer to its basename us.map.gz alone as a filename.

  • The root directory has a number of branches, such as /etc/ and /usr/. These subdirectories in turn branch into still more subdirectories, such as /etc/init.d/ and /usr/local/. The whole thing viewed collectively is called the directory tree. You can think of an absolute filename as a route from the base of the tree (/) to the end of some branch (a file). You will also hear people talk about the directory tree as if it were a family tree: thus subdirectories have parents, and a path shows the complete ancestry of a file. There are also relative paths that begin somewhere other than the root directory. You should remember that the directory ../ refers to the parent directory. This also applies to other directory like structures, such as data structures or other tree hierarchical organized entities.

  • There's no special directory path name component that corresponds to a physical device, such as your hard disk. This differs from RT-11, CP/M, OpenVMS, MS-DOS, AmigaOS, and Microsoft Windows, where the path contains a device name such as C:\. (However, directory entries do exist that refer to physical devices as a part of the normal filesystem. See Section 2.2.2, “Filesystem internals”.)

[Note] Note

While you can use almost any letters or symbols in a file name, in practice it is a bad idea to do so. It is better to avoid any characters that often have special meanings on the command line, including spaces, tabs, newlines, and other special characters: { } ( ) [ ] ' ` " \ / > < | ; ! # & ^ * % @ $ . If you want to separate words in a name, good choices are the period, hyphen, and underscore. You could also capitalize each word, "LikeThis". Experienced Linux users tend to avoid spaces in filenames.

[Note] Note

The word path is used not only for fully-qualified filename as above but also for the command search path. The intended meaning is usually clear from the context.

The detailed best practices for the file hierarchy are described in the Filesystem Hierarchy Standard (/usr/share/doc/debian-policy/fhs/fhs-2.3.txt.gz and hier(7)). You should remember the following facts as the starter:

Table 2.3. List of usage of key directories.

directory

usage

/

A simple / represents the root directory.

/etc/

This is the place for the system wide configuration files.

/var/log/

This is the place for the system log files.

/home/

This is the directory which contains all the home directories for all non-privileged users.


2.2.2. Filesystem internals

Following the Unix tradition, the Debian GNU/Linux system provides the filesystem under which physical data on harddisks and other storage devices reside, and the interaction with the hardware devices such as console screens and remote serial consoles are represented in an unified manner [under /dev/].

Each file, directory, named pipe (a way two programs can share data), or physical device on a Debian GNU/Linux system has a data structure called an inode which describes its associated attributes such as the user who owns it (owner), the group that it belongs to, the time last accessed, etc. If you are really interested, see /usr/include/linux/fs.h for the exact definition of struct inode in the Debian GNU/Linux system. The idea of representing just about everything in the file system was a Unix innovation, and modern Linux kernels have developed this idea ever further. Now, even information about processes running in the computer can be found in the file system.

This abstract and unified representation of physical entities and internal processes is very powerful since this allows us to use the same command for the same kind of operation on many totally different devices. It is even possible to change the way the kernel works by writing data to special files that are linked to running processes.

All of your files could be on one disk --- or you could have 20 disks, some of them connected to a different computer elsewhere on the network. You can't tell just by looking at the directory tree, and nearly all commands work just the same way no matter what physical device(s) your files are really on. [This is a good thing. Trust us.] Of course, methods do exist whereby you can tell what devices map to which physical or network devices. mount with no arguments will show how storage is mapped to physical or network devices.

2.2.3. The filesystem permission system

The filesystem permissions of Unix-like system are defined for three categories of affected users:

  • the user who owns the file (u),

  • other users in the group which the file belongs to (g), and

  • all other users (o) also referred to as "world" and "everyone".

For the file, each corresponding permission allows:

  • read (r): to examine contents of the file,

  • write (w): to modify the file, and

  • execute (x): to run the file as a command.

For the directory, each corresponding permission allows:

  • read (r): to list contents of the directory,

  • write (w): to add or remove files in the directory, and

  • execute (x): to access files in the directory.

Here, execute permission on the directory means not only to allow reading of files in its directory but also to allow viewing their attributes, such as the size and the modification time.

To display permission information (and more) for files and directories, ls(1) is used. When ls invoked with the -l option, it displays the following information in the order given:

  • the type of file (first character)

  • the file's access permissions (the next nine characters, consisting of three characters each for user, group, and other in this order)

  • the number of hard links to the file

  • the name of the user who owns the file

  • the name of the group which the file belongs to

  • the size of the file in characters (bytes)

  • the date and time of the file (mtime)

  • the name of the file.

Table 2.4. List of the first character of "ls -l" output

character

meaning

-

normal file

d

directory

l

symlink

c

character device node

b

block device node

p

named pipe

s

socket


To change the owner of the file, chown(1) is used from the root account. To change the group of the file, chgrp(1) is used from the file's owner or root account. To change file and directory access permissions, chmod(1) is used from the file's owner or root account. Basic syntax to manipulate foo file is:

# chown <newowner> foo
# chgrp <newgroup> foo
# chmod  [ugoa][+-=][rwxXst][,...] foo

For example, in order to make a directory tree to be owned by a user foo and shared by a group bar, issue the following commands from the root account:

# cd /some/location/
# chown -R foo:bar .
# chmod -R ug+rwX,o=rX .

There are three more special permission bits:

  • set user ID (s or S instead of user's x),

  • set group ID (s or S instead of group's x), and

  • sticky bit (t or T instead of other's x).

Here the output of ls -l for these bits is capitalized if execution bits hidden by these outputs are unset.

Setting set user ID on an executable file allows a user to execute the executable file with the owner ID of the file (for example root). Similarly, setting set group ID on an executable file allows a user to execute the executable file with the group ID of the file (for example root). Because these settings can cause security risks, enabling them requires extra caution.

Setting set group ID on a directory enables the BSD-like file creation scheme where all files created in the directory belong to the group of the directory.

Setting the sticky bit on a directory prevents a file in the directory from being removed by a user who is not the owner of the file. In order to secure the contents of a file in world-writable directories such as /tmp or in group-writable directories, one must not only set write permission off for the file but also set the sticky bit on the directory. Otherwise, the file can be removed and a new file can be created with the same name by any user who has write access to the directory.

Here are a few interesting examples of the file permissions.

$ ls -l /etc/passwd /etc/shadow /dev/ppp /usr/sbin/exim4
crw------- 1 root root   108, 0 2007-04-29 07:00 /dev/ppp
-rw-r--r-- 1 root root     1427 2007-04-16 00:19 /etc/passwd
-rw-r----- 1 root shadow    943 2007-04-16 00:19 /etc/shadow
-rwsr-xr-x 1 root root   700056 2007-04-22 05:29 /usr/sbin/exim4
$ ls -ld /tmp /var/tmp /usr/local /var/mail /usr/src
drwxrwxrwt 10 root root  4096 2007-04-29 07:59 /tmp
drwxrwsr-x 10 root staff 4096 2007-03-24 18:48 /usr/local
drwxrwsr-x  4 root src   4096 2007-04-27 00:31 /usr/src
drwxrwsr-x  2 root mail  4096 2007-03-28 23:33 /var/mail
drwxrwxrwt  2 root root  4096 2007-04-29 07:11 /var/tmp

There is an alternative numeric mode to describe file permissions in chmod(1) commands. This numeric mode uses 3 to 4 digit wide octal (radix=8) numbers.

Table 2.5. The numeric mode for file permissions in chmod(1) commands.

digit

meaning

1st optional digit

sum of set user ID (=4), set group ID (=2), and sticky bit (=1)

2nd digit

sum of read (=4), write (=2), and execute (=1) permissions for user

3rd digit

ditto for group

4th digit

ditto for other


This sounds complicated but it is actually quite simple. If you look at the first few (2-10) columns from "ls -l" command output and read it as a binary (radix=2) representation of file permissions ("-" being "0" and "rwx" being "1"), the last 3 digit of the numeric mode value should make sense as an octal (radix=8) representation of file permissions to you. For example, try:

$ touch foo bar
$ chmod u=rw,go=r foo
$ chmod 644 bar
$ ls -l foo bar
-rw-r--r-- 1 penguin penguin 17 2007-04-29 08:22 bar
-rw-r--r-- 1 penguin penguin 12 2007-04-29 08:22 foo
[Tip] Tip

If you need to access information displayed by "ls -l" in shell script, you should use pertinent commands such as test(1), stat(1) and readlink(1). The shell builtin such as "[" or "test" may be used too.

2.2.4. Control of permissions for newly created files: umask

What permissions are applied to a newly created file or directory is restricted by the umask shell built-in command. See dash(1), bash(1), and builtins(7).

 (file permission) = (requested file permission) & ~(umask value)

Table 2.6. The umask value examples.

umask

usage

file permission created

directory permission created

0022

writable only by the user

-rw-r--r--

-rwxr-xr-x

0002

writable by the group

-rw-rw-r--

-rwxrwxr-x


The Debian system uses a user private group (UPG) scheme as its default. A UPG is created whenever a new user is added to the system. A UPG has the same name as the user for which it was created and that user is the only member of the UPG. UPGs makes it is safe to set umask to 0002 since every user has their own private group. (In some Unix variants, it is quite common to setup all normal users belonging to a single users group and is good idea to set umask to 0022 for security in such cases.)

2.2.5. Permissions for groups of users (group)

In order to make the group permission to be applied to a particular user, that user needs to be made a member of the group using "sudo vigr".

[Note] Note

Alternatively, you may dynamically add users to groups during the authentication process by adding "auth optional pam_group.so" line to /etc/pam.d/common-auth and setting /etc/security/group.conf. (See more: Chapter 5, Authentication.)

The hardware devices are just another kind of file on the Debian system. If you have problems accessing devices such as CD-ROM and USB memory stick from a user account, you should make that user a member of the relevant group.

Some notable system-provided groups allow their members to access particular files and devices without root privilege.

Table 2.7. List of example system-provided groups for file access.

group

accessible files and devices

dialout

Full and direct access to serial ports. (reconfigure modem, dial anywhere, etc.)

dip

"Dialup IP", enough to run ppp or dip commands.

cdrom

CD-ROM, DVD+/-RW drives.

audio

An audio device.

video

A video device.

scanner

Scanner(s).

adm

System monitoring logs.

staff

Some directories for junior administrative work: /usr/local, /home .


Some notable system provided groups allow their members to execute particular commands without root privilege.

Table 2.8. List of notable system provided groups for particular command executions.

group

accessible commands

sudo

execute sudo without their password.

lpadmin

execute commands to add, modify, and remove printers from printer databases.

plugdev

execute pmount(1) for removable devices such as USB memories.


For the full listing of the system provided users and groups, see the recent version of the "Users and Groups" (/usr/share/doc/base-passwd/users-and-groups.html) document provided by the base-passwd package.

See manpages of passwd(5), group(5), shadow(5), group(5), vipw(8), vigr(8), and pam_group(8) for the management commands of the user and group system.

2.2.6. Timestamps

There are three types of timestamps for a GNU/Linux file.

Table 2.9. List of types of timestamps.

type

meaning

mtime

the file modification time (ls -l)

ctime

the file status change time (ls -lc)

atime

the last file access time (ls -lu)


Note that ctime is not file creation time.

  • Overwriting a file will change all of the mtime, ctime, and atime attributes of the file.

  • Changing permission or owner of a file will change the ctime and atime attributes of the file.

  • Reading a file will change the atime of the file.

Note that even simply reading a file on the Debian system will normally cause a file write operation to update atime information in the inode. Mounting a filesystem with the noatime option will let the system skip this operation and will result in faster file access for the read. This is often recommended for laptops, because it reduces hard drive activity and saves power. See mount(8).

Use touch(1) command to change timestamps of existing files.

For timestamps, the ls command outputs different strings under the modern English locale (en_US.UTF-8) from under the old one (C).

$ LANG=en_US.UTF-8  ls -l foo
-rw-r--r-- 1 penguin penguin 3 2008-03-05 00:47 foo
$ LANG=C  ls -l foo
-rw-r--r-- 1 penguin penguin 3 Mar  5 00:47 foo

2.2.7. Links

There are two methods of associating a file foo with a different filename bar.

  • a hard link is a duplicate name for an existing file ("ln foo bar"),

  • a symbolic link, or "symlink", is a special file that points to another file by name ("ln -s foo bar").

See the following example for the changes in link counts and the subtle differences in the result of the rm command.

$ echo "Original Content" > foo
$ ls -li foo
2398521 -rw-r--r-- 1 penguin penguin 17 2007-04-29 08:15 foo
$ ln foo bar     # hard link
$ ln -s foo baz  # symlink
$ ls -li foo bar baz
2398521 -rw-r--r-- 2 penguin penguin 17 2007-04-29 08:15 bar
2398538 lrwxrwxrwx 1 penguin penguin  3 2007-04-29 08:16 baz -> foo
2398521 -rw-r--r-- 2 penguin penguin 17 2007-04-29 08:15 foo
$ rm foo
$ echo "New Content" > foo
$ ls -li foo bar baz
2398521 -rw-r--r-- 1 penguin penguin 17 2007-04-29 08:15 bar
2398538 lrwxrwxrwx 1 penguin penguin  3 2007-04-29 08:16 baz -> foo
2398540 -rw-r--r-- 1 penguin penguin 12 2007-04-29 08:17 foo
$ cat bar
Original Content
$ cat baz
New Content

The hardlink can be made within the same file system and shares the same inode number which the "-i" option with ls command reveals.

The symlink always has nominal file access permissions of "rwxrwxrwx", as shown in the above example, with the effective access permissions dictated by the permissions of the file that it points to.

[Caution] Caution

It is generally good idea not to create complicated symbolic links or hardlinks at all unless you have a very good reason. It may cause nightmares where the logical combination of the symbolic links results in loops in the filesystem.

[Note] Note

It is generally preferable to use symbolic links rather than hardlinks unless you have a good reason for using a hardlink.

The "." directory links to the directory that it appears in, thus the link count of any new directory starts at 2. The ".." directory links to the parent directory, thus the link count of the directory increases with the addition of new subdirectories.

If you are just moving to Linux from Windows, it will soon become clear how well-designed the file-name linking of Unix is, compared with the nearest Windows equivalent of "shortcuts". Because it is implemented in the file system, applications can't see any difference between a linked file and the original. In the case of hardlinks, there really is no difference.

2.2.8. Named pipes (FIFOs)

A named pipe is a file that acts like a pipe. You put something into the file, and it comes out the other end. Thus it's called a FIFO, or First-In-First-Out: the first thing you put in the pipe is the first thing to come out the other end.

If you write to a named pipe, the process which is writing to the pipe doesn't terminate until the information being written is read from the pipe. If you read from a named pipe, the reading process waits until there is something to read before terminating. The size of the pipe is always zero --- it does not store data, it just links two processes like the shell "|". However, since this pipe has a name, the two processes don't have to be on the same command line or even be run by the same user. Pipes were a very influential innovation of Unix.

You can try it by doing the following:

$ cd; mkfifo mypipe
$ echo "hello" >mypipe & # put into background
[1] 8022
$ ls -l mypipe
prw-r--r-- 1 penguin penguin 0 2007-04-29 08:25 mypipe
$ cat mypipe
hello
[1]+  Done                    echo "hello" >mypipe
$ ls mypipe
mypipe
$ rm mypipe

2.2.9. Sockets

Sockets are used extensively by all the Internet communication, databases, and the operating system itself. It is similar to the named pipe (FIFO) and allows processes to exchange information even between different computers. For the socket, those processes do not need to be running at the same time nor to be running as the children of the same ancestor process. This is the endpoint for the inter process communication (IPC). The exchange of information may occur over the network between different hosts. The two most common ones are the Internet socket and the Unix domain socket.

[Tip] Tip

"netstat -an" will provide a very useful overview of the sockets that are open on a given system.

2.2.10. Device files

Device files refer to physical or virtual devices on your system, such as your hard disk, video card, screen, or keyboard. An example of a virtual device is the console, represented by /dev/console.

Table 2.10. The device types.

device type

meaning

character device

This can be accessed one character at a time, that is, the smallest unit of data which can be written to or read from the device is a character (byte).

block device

This must be accessed in larger units called blocks, which contain a number of characters. Your hard disk is a block device.


You can read and write device files, though the file may well contain binary data which may be an incomprehensible-to-humans gibberish. Writing data directly to these files is sometimes useful for the troubleshooting of hardware connections. For example, you can dump a text file to the printer device /dev/lp0 or send modem commands to the appropriate serial port /dev/ttyS0. But, unless this is done carefully, it may cause a major disaster. So be cautious.

[Note] Note

For the normal access to a printer, use the lp(1) command.

The device node number are displayed by executing ls as:

$ ls -l /dev/hda /dev/ttyS0 /dev/zero
brw-rw---- 1 root cdrom   3,  0 2007-04-29 07:00 /dev/hda
crw-rw---- 1 root dialout 4, 64 2007-04-29 07:00 /dev/ttyS0
crw-rw-rw- 1 root root    1,  5 2007-04-29 07:00 /dev/zero

Here,

  • /dev/hda has the major device number 3 and the minor device number 0. This is read/write accessible by the user who belongs to disk group,

  • /dev/ttyS0 has the major device number 4 and the minor device number 64. This is read/write accessible by the user who belongs to dialout group, and

  • /dev/zero has the major device number 1 and the minor device number 5. This is read/write accessible by anyone.

In the Linux 2.6 system, the filesystem under /dev is automatically populated by the udev(7) mechanism.

2.2.11. Special device files

There are some special device files.

Table 2.11. List of special device files.

device file

action

response

/dev/null

read

it returns "end-of-file (EOF) character".

/dev/null

write

it is a bottomless data dump pit.

/dev/zero

read

it returns "the \0 (NUL) character" (not the same as the number zero ASCII).

/dev/random

read

it returns random characters from a true random number generator, delivering real entropy. (slow)

/dev/urandom

read

it returns random characters from a cryptographically secure pseudorandom number generator.

/dev/full

write

it returns the disk-full (ENOSPC) error.


These are frequently used in conjunction with the shell redirection (see Section 2.5.5, “Typical command sequences and shell redirection”).

2.2.12. procfs and sysfs

The procfs and sysfs mounted on /proc and /sys are the pseudo-filesystem and expose internal data structures of the kernel to the userspace. In other word, these entries are virtual, meaning that they act as a convenient window into the operation of the operating system.

The directory /proc contains (among other things) one subdirectory for each process running on the system, which is named after the process ID (PID).

System utilities that access process information, such as ps, get their information from this directory structure.

The directories under /proc/sys/ contain interface to change certain kernel parameters at run time. (You may do the same through specialized command sysctl(8) or its preload/configuration file /etc/sysctrl.conf.)

[Note] Note

The Linux kernel may complain "Too many open files". You can fix avoid this by executing "echo "65536" > /proc/sys/fs/file-max" from the root shell to increase file-max value.

People frequently panic when they notice one file in particular - /proc/kcore - which is generally huge. This is (more or less) a copy of the contents of your computer's memory. It's used to debug the kernel. It is a virtual file that points to computer memory, so don't worry about its size.

The directory under /sys contains exported kernel data structures, their attributes, and the linkages between them. It also contains interface to change certain kernel parameters at run time.

See proc.txt(.gz), sysfs.txt(.gz) and other related documents in the Linux kernel documentation (/usr/share/doc/linux-doc-2.6.*/Documentation/filesystems/*) provided by the linux-doc-2.6.* package.

2.3. Midnight Commander (MC)

Midnight Commander (MC) is a GNU "Swiss army knife" for the Linux console and other terminal environments. This gives newbie a menu driven console experience which is much easier to learn than standard Unix commands.

You may need to install the Midnight Commander package which is titled mc.

 sudo aptitude install mc                                                                                                   

Use the mc command to explore the Debian system. This is the best way to learn. Please explore few interesting locations just using the cursor keys and Enter key:

  • /etc and its subdirectories.

  • /var/log and its subdirectories.

  • /usr/share/doc and its subdirectories.

  • /sbin and /bin

2.3.1. Customization of MC

In order to make MC to change working directory upon exit and cd to the frequently used directories, I suggest to modify ~/.bashrc to include:

. /usr/share/mc/bin/mc.sh                                                                                                        

See mc(1) (under the "-P" option) for the reason. (If you do not understand what exactly I am talking here, you can do this later.)

2.3.2. Starting MC

MC can be started by:

$ mc

MC takes care of all file operations through its menu, requiring minimal user effort. Just press F1 to get the help screen (see next paragraph if this doesn't work). You can play with MC just by pressing cursor-keys and function-keys.

In some consoles such as gnome-terminal, key strokes of function-keys may be stolen by the console program. You can disable these features by "Edit" -> "Keyboard Shortcuts" for gnome-terminal. In particular, consider removing the mapping of F1 to Gnome Terminal Help; doing so will allow F1 to show Midnight Commander help.

If you encounter character encoding problem which displays garbage characters, adding "-a" to MC's command line may help prevent problems.

If this doesn't clear up your display problems with MC, see Section 10.6.6, “The terminal configuration”.

2.3.3. File manager in MC

The default is two directory panels containing file lists. Another useful mode is to set the right window to "information" to see file access privilege information, etc. Following are some essential keystrokes. With the gpm daemon running, one can use a mouse, too. (Make sure to press the shift-key to obtain the normal behavior of cut and paste in MC.)

Table 2.12. The key bindings of MC.

key

key binding

F1

Help menu

F3

Internal file viewer

F4

Internal editor

F9

Activate pull down menu

F10

Exit Midnight Commander

Tab

Move between two windows

Insert or Ctrl-T

Mark file for a multiple-file operation such as copy

Del

Delete file (be careful---set MC to safe delete mode)

Cursor keys

Self-explanatory


2.3.4. Command-line tricks in MC

  • Any cd command will change the directory shown on the selected screen.

  • Ctrl-Enter or Alt-Enter will copy a filename to the command line. Use this with the cp or mv command together with command-line editing.

  • Alt-Tab will show shell filename expansion choices.

  • One can specify the starting directory for both windows as arguments to MC; for example, mc /etc /root.

  • Esc + n-key == Fn (i.e., Esc + 1 = F1, etc.; Esc + 0 = F10)

  • Pressing Esc before the key has the same effect as pressing the Alt and the key together.; i.e., type Esc + c for Alt-C. Esc is called meta-key and sometimes noted as "M-"

2.3.5. The internal editor in MC

The internal editor has an interesting cut-and-paste scheme. Pressing F3 marks the start of a selection, a second F3 marks the end of selection and highlights the selection. Then you can move your cursor. If you press F6, the selected area will be moved to the cursor location. If you press F5, the selected area will be copied and inserted at the cursor location. F2 will save the file. F10 will get you out. Most cursor keys work intuitively.

This editor can be directly started on a file:

$ mc -e filename_to_edit
$ mcedit filename_to_edit

This is not a multi-window editor, but one can use multiple Linux consoles to achieve the same effect. To copy between windows, use Alt-F<n> keys to switch virtual consoles and use "File->Insert file" or "File->Copy to file" to move a portion of a file to another file.

This internal editor can be replaced with any external editor of choice.

Also, many programs use the environment variables EDITOR or VISUAL to decide which editor or viewer to use. If you are uncomfortable with vim or nano initially, you may set these to mcedit by adding these lines to ~/.bashrc:

...
export EDITOR=mcedit
export VISUAL=mcedit
...

I do recommend setting these to vim if possible.

If you are uncomfortable with vim, you can keep using mcedit for most system maintenance tasks.

2.3.6. The internal viewer in MC

Very smart viewer. This is a great tool for searching words in documents. I always use this for files in the /usr/share/doc directory. This is the fastest way to browse through masses of Linux information. This viewer can be directly started like so:

$ mc -v path/to/filename_to_view
$ mcview path/to/filename_to_view

2.3.7. Auto-start features of MC

Press Enter on a file, and the appropriate program will handle the content of the file (see Section 10.5.11, “Customizing program to be started”). This is a very convenient MC feature.

Table 2.13. The reaction to the enter key in MC.

file type

reaction to enter key

executable file

Execute command

man file

Pipe content to viewer software

html file

Pipe content to web browser

tar.gz.deb file

Browse its contents as if subdirectory


In order to allow these viewer and virtual file features to function, viewable files should not be set as executable. Change their status using the chmod command or via the MC file menu.

2.3.8. FTP virtual filesystem of MC

MC can be used to access files over the Internet using FTP. Go to the menu by pressing F9, then type "p" to activate the FTP virtual filesystem. Enter a URL in the form "username:passwd@hostname.domainname", which will retrieve a remote directory that appears like a local one.

Try "http.us.debian.org/debian" as the URL and browse the Debian archive.

2.4. The basic Unix-like work environment

Although MC enables you to do almost everything, it is very important for you to learn how to use the command line tools invoked from the shell prompt and become familiar with the Unix-like work environment.

2.4.1. The login shell

You can select your login shell with the chsh command.

Table 2.14. List of shell programs.

package

popcon

size

POSIX shell

description

bash

V:87, I:99

1300

Yes

The GNU Bourne Again SHell. (de facto standard)

tcsh

V:11, I:83

736

No

TENEX C Shell, an enhanced version of Berkeley csh.

dash

V:3, I:13

236

Yes

The Debian Almquist Shell. Good for shell script.

zsh

V:2, I:5

12504

Yes

The standard shell with many enhancements.

pdksh

V:0.3, I:1.3

464

Yes

A public domain version of the Korn shell.

csh

V:0.4, I:1.5

400

No

OpenBSD C Shell, a version of Berkeley csh.

sash

V:0.3, I:1.1

836

Yes

Stand-alone shell with built-in commands. (Not meant for standard /bin/sh.)

ksh

V:0.3, I:1.5

2540

Yes

The real, AT&T version of the Korn shell.

rc

V:0.08, I:0.5

204

No

An implementation of the AT&T Plan 9 shell.

posh

V:0.02, I:0.14

232

Yes

Policy-compliant Ordinary SHell. A pdksh derivative.

es

V:0.02, I:0.06

304

No

An extensible shell based on the AT&T Plan 9 shell: rc.


In this tutorial chapter, the interactive shell always means bash.

2.4.2. Customizing bash

You can customize bash behavior by ~/.bashrc. For example, I added followings to ~/.bashrc:

# CD upon exiting MC
. /usr/share/mc/bin/mc.sh

# set CDPATH to good one
CDPATH=.:/usr/share/doc:~/Desktop/src:~/Desktop:~
export CDPATH

PATH="${PATH}":/usr/sbin:/sbin
# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
  PATH=~/bin:"${PATH}"
fi
export PATH

EDITOR=vim
export EDITOR
[Tip] Tip

You can find more bash customization tips, such as Section 10.2.6, “Colorized commands”, in Chapter 10, System tips.

2.4.3. Special key strokes

In the Unix-like environment, there are few key strokes which have special meanings. Please note that on a normal Linux character console, only the left-hand Ctrl and Alt keys work as expected. Here are few notable key strokes to remember.

Table 2.15. List of key bindings for bash.

key

key binding

Ctrl-U

Erase line before cursor.

Ctrl-H

Erase a character before cursor.

Ctrl-D

Terminate input. (exit shell if you are using shell)

Ctrl-C

Terminate a running program.

Ctrl-Z

Temporarily stop program by moving it to the background job

Ctrl-S

Halt output to screen.

Ctrl-Q

Reactivate output to screen.

Ctrl-Alt-Del

Reboot/halt the system, see manpage for inittab.

Left-Alt-key (optionally, Windows-key)

Meta-key for Emacs and the similar UI.

Up-arrow

Start command history search under bash.

Ctrl-R

Start incremental command history search under bash.

Tab

Complete input of the filename to the command line under bash.

Ctrl-V Tab

Input Tab without expansion to the command line under bash.


[Tip] Tip

The terminal feature of Ctrl-S can be disabled using stty(1) command.

2.4.4. Unix style mouse operations

The Unix style mouse operations are based on the 3 button mouse system.

Table 2.16. The Unix style mouse operations.

action

response

Left-click-and-drag mouse

Select and copy to the clipboard.

Left-click

Select the start of selection.

Right-click

Select the end of selection and copy to the clipboard.

Middle-click

Paste clipboard at the cursor.


The center wheel on the modern wheel mouse is considered middle mouse button and can be used for middle-click. Clicking left and right mouse buttons together serves as the middle-click under the 2 button mouse system situation. In order to use a mouse in the Linux character console, you need to have gpm running as daemon.

2.4.5. The pager

The less program is the enhanced pager (file content browser). Hit "h" for help. It can do much more than more. This less command can be supercharged by executing eval $(lesspipe) or eval $(lessfile) in the shell startup script. See more in /usr/share/doc/lessf/LESSOPEN. The -R option allows raw character output and enables ANSI color escape sequences. See less(1).

2.4.6. The text editor

You should become proficient in one of the variants of Vim or Emacs programs which are popular in the Unix-like system.

I think getting used to Vim commands is the right thing to do, since Vi-editor is always there in the Linux/Unix world. (Actually, original vi or new nvi are the programs you find everywhere. I chose Vim instead for newbie since it offers you help through F1 key while it is similar enough and more powerful.)

If you chose either Emacs or XEmacs instead as your choice of the editor, that is another good choice indeed, particularly for programming. Emacs has a plethora of other features as well, including functioning as a newsreader, directory editor, mail program, etc.. When used for programming or editing shell scripts, it intelligently recognizes the format of what you are working on, and tries to provide assistance. Some people maintain that the only program they need on Linux is Emacs. Ten minutes learning Emacs now can save hours later. Having the gnu Emacs manual for reference when learning Emacs is highly recommended.

All these programs usually come with tutoring program for you to learn them by practice. Start Vim by typing "vim" and press F1-key. You should at least read the first 35 lines. Then do the online training course by moving cursor to |tutor| and pressing Ctrl-].

[Note] Note

Good editors, such as Vim and Emacs, can be used to handle UTF-8 and other exotic encoding texts correctly with proper option in the x-terminal-emulator on X under UTF-8 locale with proper font settings. Please refer to their documentation on multibyte text.

2.4.7. Setting a default text editor

Debian comes with a number of different editors. We recommend to install the vim package, as mentioned above.

Debian provides unified access to the system default editor via command /usr/bin/editor so other programs (e.g., reportbug(1)) can invoke it. You can change it by:

$ sudo update-alternatives --config editor

The choice /usr/bin/vim.basic is the recommendation for newbies by the author. This supports syntax highlighting.

[Tip] Tip

Many programs use the environment variables "EDITOR" or "VISUAL" to decide which editor to use (see Section 2.3.5, “The internal editor in MC” and Section 10.5.11, “Customizing program to be started”).

2.4.8. Customizing vim

You can customize vim behavior by ~/.vimrc. For example, I use:

" -------------------------------
" Local configuration
"
set nocompatible
set nopaste
set pastetoggle=<f2>
syn on
if $USER == "root"
 set nomodeline
 set noswapfile
else
 set modeline
 set swapfile
endif
" filler to avoid the line above being recognized as a modeline
" filler
" filler

2.4.9. Recording the shell activities

The output of the shell command may roll off your screen and may be lost forever. It is good practice to log shell activities into the file for you to review them later. This kind of record is essential when you perform any system administration tasks.

The basic method of recording the shell activity is to run it under the script(1) command.

$ script
Script started, file is typescript
  • do whatever shell commands ...

  • press Ctrl-D to exit script.

$ vim typescript

See Section 10.2.3, “Recording the shell activities cleanly” .

2.4.10. Basic Unix commands

Let's learn the basic Unix commands. Here I use "Unix" in its generic sense. Any Unix clone OSs usually offer the equivalent commands. The Debian system is no exception. Do not worry if some commands do not work as you wish now. If alias is used in the shell, its corresponding command outputs are different. These examples are not meant to be executed in this order.

Try all the following commands from the non-privileged user account:

Table 2.17. List of basic Unix commands.

command

description

pwd

Display name of current/working directory.

whoami

Display current user name.

id

Display current user identity (name, uid, gid, and associated groups).

file <foo>

Display a type of file for the file <foo>.

type -p <commandname>

Display a file location of command <commandname>.

which <commandname>

, ,

type <commandname>

Display information on command <commandname>.

apropos <key-word>

Find commands related to <key-word>.

man -k <key-word>

, ,

whatis <commandname>

Display one line explanation on command <commandname>.

man -a <commandname>

Display explanation on command <commandname>. (Unix style)

info <commandname>

Display rather long explanation on command <commandname>. (GNU style)

ls

List contents of directory. (non-dot files and directories)

ls -a

List contents of directory. (all files and directories)

ls -A

List contents of directory. (almost all files and directories, i.e., skip ".." and ".")

ls -la

List all contents of directory with detail information.

ls -lai

List all contents of directory with inode number and detail information.

ls -d

List all directories under the current directory.

tree

Display file tree contents.

lsof <foo>

List open status of file <foo>.

lsof -p <pid>

List files opened by PID=<pid>.

mkdir <foo>

Make a new directory <foo> in the current directory.

rmdir <foo>

Remove a directory <foo> in the current directory.

cd <foo>

Change directory to the directory <foo> in the current directory or in the directory listed in the variable CDPATH.

cd /

Change directory to the root directory.

cd

Change directory to the current user's home directory.

cd /<foo>

Change directory to the absolute path directory /<foo>.

cd ..

Change directory to the parent directory.

cd ~<foo>

Change directory to the home directory of the user <foo>.

cd -

Change directory to the previous directory.

</etc/motd pager

Display contents of /etc/motd using the default pager.

touch <junkfile>

Create a empty file <junkfile>.

cp <foo> <bar>

Copy a existing file <foo> to a new file <bar>.

rm <junkfile>

Remove a file <junkfile>.

mv <foo> <bar>

Rename an existing file <foo> to a new name <bar>. The directory <bar> must not exist.

mv <foo> <bar>

Move an existing file <foo> to a new location <bar>/<foo>. The directory <bar> must exist.

mv <foo> <bar>/<baz>

Move an existing file <foo> to a new location with a new name <bar>/<baz>. The directory <bar> must exist but the directory <bar>/<baz> must not exist.

chmod 600 <foo>

Make an existing file <foo> to be non-readable and non-writable by the other people. (non-executable for all)

chmod 644 <foo>

Make an existing file <foo> to be readable but non-writable by the other people. (non-executable for all)

chmod 755 <foo>

Make an existing file <foo> to be readable but non-writable by the other people. (executable for all)

find . -name <pattern>

find matching filenames using shell <pattern>. (slower)

locate -d . <pattern>

find matching filenames using shell <pattern>. (quicker using regularly generated database)

grep -e "<pattern>" *.html

Find a "<pattern>" in all of the files ending with ".html" in current directory and display them all.

top

Display process information using full screen. Type "q" to quit.

ps aux | pager

Display information on all the running processes using BSD style output.

ps -ef | pager

Display information on all the running processes using Unix system-V style output.

ps aux | grep -e "[e]xim4*"

Display all processes running exim or exim4.

ps axf | pager

Display information on all the running processes with ASCII art output.

kill <1234>

Kill a process identified by the process ID: <1234>.

gzip <foo>

Compress <foo> to create <foo>.gz using the Lempel-Ziv coding (LZ77).

gunzip <foo>.gz

Decompress <foo>.gz to create <foo>.

bzip2 <foo>

Compress <foo> to create <foo>.bz2 using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. (Better compression than gzip)

bunzip2 <foo>.bz2

Decompress <foo>.bz2 to create <foo>.

tar -xvf <foo.tar>

Extract files from <foo>.tar archive.

tar -xvzf <foo>.tar.gz

Extract files from gzipped <foo>.tar.gz archive.

tar -xvf -j <foo.tar.bz2>

Extract files from <foo>.tar.bz2 archive.

tar -cvf <foo>.tar <bar>/

Archive contents of folder <bar>/ in <foo>.tar archive.

tar -cvzf <foo>.tar.gz <bar>/

Archive contents of folder <bar>/ in compressed <foo>.tar.gz archive.

tar -cvjf <foo>.tar.bz2 <bar>/

Archive contents of folder <bar>/ in <foo>.tar.bz2 archive.

zcat README.gz | pager

Display contents of compressed README.gz using the default pager.

zcat README.gz > foo

Create a file foo with the decompressed content of README.gz.

zcat README.gz >> foo

Append the decompressed content of README.gz to the end of the file foo. (If it does not exist, create it first.)


[Note] Note

Unix has a tradition to hide filenames which start with ".". They are traditionally files that contain configuration information and user preferences.

[Note] Note

For cd command, see manpage of builtins(7).

[Note] Note

The default pager of the bare bone Debian system is more which cannot scroll back. By installing less package using command line "aptitude install less", less becomes default pager and you can scroll back with cursor keys.

[Note] Note

The "[" and "]" in the regular expression of the "ps aux | grep -e "[e]xim4*"" command above enable grep to avoid matching itself. The "4*" in the regular expression means 0 or more repeats of character "4" thus enables grep to match both "exim" and "exim4". Although "*" is used in the shell filename glob and the regular expression, their meanings are different in the regular expression. Learn the regular expression from the manpage of grep(1).

Please traverse directories and peek into the system using the above commands as training. If you have questions on any of the console commands, please make sure to read the manual page. For example, these commands are the good start:

$ man man
$ man bash
$ man builtins
$ man grep
$ man ls

The style of man pages may be a little hard to get used to, because they are rather terse, particularly the older, very traditional ones. But once you get used to it, you come to appreciate their succinctness.

Please note that many Unix-like commands including ones from GNU and BSD will display brief help information if you invoke them in one of the following ways (or without any arguments in some cases):

$ <commandname> --help
$ <commandname> -h

2.5. The simple shell command

Now you have some feel on how to use the Debian system. Let's look deep into the mechanism of the command execution in the Debian system. Here, I have simplified reality for the newbie. See manpages for bash(1) for the exact explanation.

A simple command is a sequence of

  1. variable assignments (optional)

  2. command name

  3. arguments (optional)

  4. redirections (optional: > , >> , < , << , etc.)

  5. control operator (optional: && , || ; <newline> , ; , & , ( , ) )

2.5.1. Command execution and environment variable

The values of some environment variables change the behavior of some Unix commands.

The default values of environment variables are initially set by the PAM system and then some of them may be reset by some application programs:

  • the display manager such as gdm, and

  • the shell in its start up codes bash_profile and .bashrc.

2.5.1.1. LANG variable

The full locale value given to LANG variable consists of 3 parts: xx_YY.ZZZZ.


For the language codes and country codes, see pertinent description in the info gettext.

For the codeset on the modern Debian system, you should always set it to UTF-8 unless you specifically want to use the historic one with good reason and background knowledge.

For fine details of the locale configuration, see: Section 9.3, “The locale” .

[Note] Note

The "LANG=en_US" is not "LANG=C" nor "LANG=en_US.UTF-8". It is "LANG=en_US.ISO-8859-1" (see: Section 9.3.1, “Basics of encoding”).

Table 2.19. List of locale recommendations.

Language (area)

locale recommendation

English(USA)

en_US.UTF-8

English(Great_Britain)

en_GB.UTF-8

French(France)

fr_FR.UTF-8

German(Germany)

de_DE.UTF-8

Italian(Italy)

it_IT.UTF-8

Spanish(Spain)

es_ES.UTF-8

Catalan(Spain)

ca_ES.UTF-8

Swedish(Sweden)

sv_SE.UTF-8

Portuguese(Brasil)

pt_BR.UTF-8

Russian(Russia)

ru_RU.UTF-8

Chinese(P.R._of_China)

zh_CN.UTF-8

Chinese(Taiwan_R.O.C.)

zh_TW.UTF-8

Japanese(Japan)

ja_JP.UTF-8

Korean(Republic_of_Korea)

ko_KR.UTF-8

Vietnamese(Vietnam)

vi_VN.UTF-8


Typical command execution uses a shell line sequence like the following:

$ date
Sun Jun  3 10:27:39 JST 2007
$ LANG=fr_FR.UTF-8 date
dimanche 3 juin 2007, 10:27:33 (UTC+0900)

Here, the program date is executed in the foreground job. The environment variable "LANG" is:

  • system default locale (such as "en_US.UTF-8" depending on your configuration) for the first command

  • set to "fr_FR.UTF-8" (French UTF-8 locale assuming it is available on your system) for the second command

Most command executions usually do not have preceding environment variable definition. For the above example, you can alternatively execute:

$ LANG=fr_FR.UTF-8
$ date
dimanche 3 juin 2007, 10:27:33 (UTC+0900)

As you can see here, the output of command is affected by the environment variable to produce French output. If you want the environment variable to be inherited to the subprocesses (e.g., when calling shell script), you need to "export" it instead by using:

$ export LANG
[Tip] Tip

When filing a bug report, running the command under "LANG=en_US.UTF-8" is good idea if you use non-English environment.

See locale(5) and locale(7) for LANG and related environment variables.

[Note] Note

I recommend you to configure the system environment just by the LANG variable and to stay away from LC_* variables unless it is absolutely needed.

2.5.1.2. PATH variable

When you type a command into the shell, the shell searches the command in the list of directories contained in the PATH environment variable. The value of the PATH environment variable is also called the shell's search path.

In the default Debian installation, the PATH environment variable of user accounts may not include /sbin/. For example, The ifconfig command needs to be issued with full path as /sbin/ifconfig.

You can change the PATH environment variable by ~/.bash_profile or ~/.bashrc files.

2.5.1.3. HOME variable

Many commands stores user specific configuration in the home directory and changes their behavior by their contents. The home directory is identified by the environment variable: HOME:

Table 2.20. List of HOME values.

situation

value of HOME

program run by the init process (daemon)

/

program run from the normal root shell

/root

program run from the normal user shell

/home/<normal_user>

program run from the normal user GUI desktop menu

/home/<normal_user>

program run as root with "sudo program"

/home/<normal_user>

program run as root with "sudo -H program"

/root


2.5.2. Command line options

Some commands take arguments. Arguments starting with "-" or "--" are called options and control the behavior of the command.

$ date
Mon Oct 27 23:02:09 CET 2003
$ date -R
Mon, 27 Oct 2003 23:02:40 +0100

Here the command-line argument "-R" changes the date command behavior to output RFC-2822 compliant date string.

2.5.3. Shell glob

Often you want a command to work with a group of files without typing all of them. The filename expansion pattern using the shell glob, (sometimes referred as wildcards), facilitate this need.

Table 2.21. The shell glob patterns.

shell glob pattern

match

*

This matches filename (segment) not started with ".".

.*

This matches filename (segment) started with ".".

?

This matches exactly one character.

[...]

This matches exactly one character with any character enclosed in brackets.

[a-z]

This matches exactly one character with any character between "a" and "z".

[^...]

This matches exactly one character other than any character enclosed in brackets (excluding "^").


For example, try the following and think for yourself:

$ mkdir junk; cd junk; .[^.]*touch 1.txt 2.txt 3.c 4.h .5.txt ..6.txt
$ echo *.txt
1.txt 2.txt
$ echo *
1.txt 2.txt 3.c 4.h
$ echo *.[hc]
3.c 4.h
$ echo .*
. .. .5.txt ..6.txt
$ echo .*[^.]*
.5.txt ..6.txt
$ echo [^1-3]*
4.h
$ cd ..; rm -rf junk

See "man 7 glob" for more.

[Note] Note

Unlike normal filename expansion by the shell, the shell pattern "*" tested in the find command with -name test etc., matches the initial "." of the filename. (New POSIX feature)

[Note] Note

BASH can be tweaked to change its glob behavior with its shopt builtin options such as dotglob, noglob, nocaseglob, nullglob, nocaseglob, extglob, etc. See bash(1).

2.5.4. Return value of the command

Each command returns its exit status as the return value.

Table 2.22. Command exit code.

command exit state

numeric return value

logical return value

command executed successfully.

$? = 0

TRUE

command exited with error.

$? != 0

FALSE


Thus:

$ [ 1 = 1 ] ; echo $?
0
$ [ 1 = 2 ] ; echo $?
1
[Note] Note

Please note that, in the logical context for the shell, success is treated as the logical TRUE which has 0 (zero) as its value. This is somewhat non-intuitive and needs to be reminded here.

2.5.5. Typical command sequences and shell redirection

Let's try to remember following shell command idioms.

Table 2.23. The shell command idioms.

command idiom (type in one line)

effects

command &

The command is executed in the subshell in the background.

command1 | command2

The standard output of command1 is piped to the standard input of command2 . Both commands may be running concurrently.

command1 2>&1 | command2

Both standard output and standard error of command1 are piped to the standard input of command2. Both commands may be running concurrently.

command1 ; command2

The command1 and command2 are executed sequentially.

command1 && command2

The command1 is executed. If successful, command2 is also executed sequentially. Return success if both command1 and command2 are successful.

command1 || command2

The command1 is executed. If not successful, command2 is also executed sequentially. Return success if command1 or command2 are successful.

command > foo

Redirect standard output of command to a file foo. (overwrite)

command 2> foo

Redirect standard error of command to a file foo. (overwrite)

command >> foo

Redirect standard output of command to a file foo. (append)

command 2>> foo

Redirect standard error of command to a file foo. (append)

command > foo 2>&1

Redirect both standard output and standard error of command to a file foo.

command < foo

Redirect standard input of command to a file foo.

command << delimiter

Redirect standard input of command to the following lines until delimiter is met. (Here documents)

command <<- delimiter

Redirect standard input of command to the following lines until delimiter is met. The leading tab characters are stripped from input lines. (Here documents)


The Debian system is a multi-tasking system. Background jobs allow users to run multiple programs in a single shell. The management of the background process involves the shell built-ins: jobs, fg, bg, and kill. Please read the sections of the bash(1) manpage under "SIGNALS", and "JOB CONTROL", and the builtins(1) manpage.

Let's try simple examples of redirection:

$ </etc/motd pager

$ pager </etc/motd

$ pager /etc/motd

$ cat /etc/motd | pager

Although all 4 examples display the same thing, the last example runs an extra cat command and wastes resources with no reason.

The shell allows you to open files using the exec built-in with an arbitrary file descriptor.

$ echo Hello >foo
$ exec 3<foo 4>bar  # open files
$ cat <&3 >&4       # redirect stdin to 3, stdout to 4
$ exec 3<&- 4>&-    # close files
$ cat bar
Hello

Here, "n<&-" and "n>&-" mean to close the file descriptor "n".

The file descriptor 0-2 are predefined:

Table 2.24. The predefined file descriptors.

device

description

file descriptor

stdin

standard input

0

stdout

standard output

1

stderr

standard error

2


2.5.6. Command alias

You can set an alias for the frequently used command. For example:

$ alias la='ls -la'

Now, la works as a short hand for "ls -la" which lists all files in the long listing format.

You can list any existing aliases:

$ alias

You can identity exact path or identity of the command using type command. For example:

$ type ls
ls is hashed (/bin/ls)
$ type la
la is aliased to ls -la
$ type echo
echo is a shell builtin
$ type file
file is /usr/bin/file

Here ls was recently searched while file was not, thus ls is "hashed", i.e., the shell has an internal record for the quick access to the location of the ls command.

2.6. Unix-like text processing

In Unix-like work environment, text processing is done by piping text through chains of standard text processing tools. This was another crucial Unix innovation.

2.6.1. Unix text tools

There are few standard text processing tools which are used very often on the Unix-like system.

  • No regular expression is used:
    • cat(1) concatenates files and outputs the whole content.

    • tac(1) concatenates files and outputs in reverse.

    • cut(1) selects parts of lines and outputs.

    • head(1) outputs the first part of files.

    • tail(1) outputs the last part of files.

    • sort(1) sorts lines of text files.

    • uniq(1) removes duplicate lines from a sorted file.

    • tr(1) translates or deletes characters.

    • diff(1) compares files line by line.

  • Basic regular expression (BRE) is used:

    • grep(1) matches text with the pattern.

    • ed(1) is a primitive line editor.

    • sed(1) is a stream editor.

    • vim(1) is a screen editor.

    • emacs(1) is a screen editor. (somewhat extended BRE)

  • Extended regular expression (ERE) is used:

    • egrep(1) matches text with pattern.

    • awk(1) does simple text processing.

    • tcl does every conceivable type of text processing: re_syntax(3). Often used with tk.

    • perl(1) does text processing, is capable of network programming, and much more. perlre(1).

    • pcregrep(1) from the pcregrep package matches text with Perl Compatible Regular Expressions (PCRE) pattern.

    • python with re module does every conceivable text processing. See /usr/share/doc/python/html/index.html.

If you are not sure what exactly these commands do, please use "man command" to figure it out by yourself.

[Note] Note

Sort order and range expression are locale dependent. If you wish to obtain traditional behavior for a command, use C locale instead of UTF-8 ones by prepnding command with "LANG=C" (see Section 2.5.1.1, “LANG variable” and Section 9.3, “The locale”).

[Note] Note

Perl regular expressions (perlre(1)), Perl Compatible Regular Expressions (PCRE), and Python regular expressions offered by re module have many common extensions to the normal ERE.

2.6.2. Regular expressions

Regular expressions are used in many text processing tools. They are analogous to the shell globs, but they are both more complicated and more powerful.

The regular expression describes the matching pattern and is made up of text characters and metacharacters.

The metacharacter is just a character with a special meaning. There are 2 major styles, BRE and ERE, depending on the text tools as described above.

Table 2.25. The metacharacters for BRE and ERE.

BRE

ERE

The meaning of the regular expression

\ . [ ] ^ $ *

\ . [ ] ^ $ *

common metacharacters

\+ \? \( \) \{ \} \|

BRE only "\" quoted metacharacters

+ ? ( ) { } |

ERE only non-"\" quoted metacharacters

c

c

This matches the non-metacharacter "c".

\c

\c

This sequence matches the literal character "c" even if "c" is metacharacter by itself.

.

.

This matches any character including newline.

^

^

This matches the beginning of a string.

$

$

This matches the end of a string.

\<

\<

This matches the beginning of a word.

\>

\>

This matches the end of a word.

\[abc...\]

[abc...]

This character list matches any of the characters "abc...".

\[^abc...\]

[^abc...]

This negated character list matches any of the characters except "abc...".

r*

r*

This matches zero or more regular expressions identified by "r".

r\+

r+

This matches one or more regular expressions identified by "r".

r\?

r?

This matches zero or one regular expressions identified by "r".

r1\|r2

r1|r2

This matches one of the regular expressions identified by "r1" or "r2".

\(r1\|r2\)

(r1|r2)

This matches one of the regular expressions identified by "r1" or "r2" and treats it as a bracketed regular expression.


The regular expression of emacs is basically BRE but has been extended to treat "+"and "?" as the metacharacters as in ERE. Thus, there are no needs to quote them with "\" in the regular expression of emacs.

For example, grep can be used to perform the text search using the regular expression:

$ egrep 'GNU.*LICENSE|Yoyodyne' /usr/share/common-licenses/GPL
GNU GENERAL PUBLIC LICENSE
GNU GENERAL PUBLIC LICENSE
Yoyodyne, Inc., hereby disclaims all copyright interest in the program

2.6.3. Replacement expressions

For the replacement expression, following characters have special meanings:

Table 2.26. The replacement expression.

character

meaning

&

This represents what the regular expression matched. (use \& in emacs)

\n

This represents what the n-th _bracketed_ regular expression matched. ("n" being number)


For Perl replacement string, $n is used instead of \n and & has no special meaning.

For example:

$ echo zzz1abc2efg3hij4 | \
sed -e 's/\(1[a-z]*\)[0-9]*\(.*\)$/=&=/'
zzz=1abc2efg3hij4=
$ echo zzz1abc2efg3hij4 | \
sed -e 's/\(1[a-z]*\)[0-9]*\(.*\)$/\2===\1/'
zzzefg3hij4===1abc
$ echo zzz1abc2efg3hij4 | \
perl -pe 's/(1[a-z]*)[0-9]*(.*)$/$2===$1/'
zzzefg3hij4===1abc
$ echo zzz1abc2efg3hij4 | \
perl -pe 's/(1[a-z]*)[0-9]*(.*)$/=&=/'
zzz=&=

Here please pay extra attention to the style of the bracketed regular expression and how the matched strings are used in the text replacement process on different tools.

These regular expressions can be used for the cursor movements and the text replacement actions in the editors too.

The back slash "\" at the end of line in the shell commandline escapes newline as a white space character and continues shell command line input to the next line.

Please read all the related manual pages to learn these commands.

2.6.4. Extract data from text file table

Let's consider a text file called DPL in which some pre-2004 Debian project leader's names and their initiation days are listed in a space-separated format.

Ian     Murdock   August  1993
Bruce   Perens    April   1996
Ian     Jackson   January 1998
Wichert Akkerman  January 1999
Ben     Collins   April   2001
Bdale   Garbee    April   2002
Martin  Michlmayr March   2003

Awk is frequently used to extract data from these types of files.

$ awk '{ print $3 }' <DPL                   # month started
August
April
January
January
April
April
March
$ awk '($1=="Ian") { print }' <DPL          # DPL called Ian
Ian     Murdock   August  1993
Ian     Jackson   January 1998
$ awk '($2=="Perens") { print $3,$4 }' <DPL # When Perens started
April 1996

Shells such as Bash can be also used to parse this kind of file:

$ while read first last month year; do
    echo $month
  done <DPL
  • same output as the first Awk example.

Here, read built-in command uses the characters in $IFS (internal field separators) to split lines into words.

If you change IFS to ":", you can parse /etc/passwd with shell nicely:

$ oldIFS="$IFS"   # save old value
$ IFS=":"
$ while read user password uid gid rest_of_line; do
    if [ "$user" = "osamu" ]; then
      echo "$user's ID is $uid"
    fi
  done < /etc/passwd
osamu's ID is 1000
$ IFS="$oldIFS"   # restore old value

(If Awk is used to do the equivalent, use "FS=":"" to set the field separator.)

IFS is also used by the shell to split results of parameter expansion, command substitution, and arithmetic expansion. These do not occur within double or single quoted words. The default value of IFS is <space>, <tab>, and <newline> combined.

Be careful about using this shell IFS tricks. Strange things may happen, when shell interprets some parts of the script as its input.

$ IFS=":,"                        # use ":" and "," as IFS
$ echo IFS=$IFS,   IFS="$IFS"     # echo is a Bash built-in
IFS=  , IFS=:,
$ date -R                         # just a command output
Sat, 23 Aug 2003 08:30:15 +0200
$ echo $(date -R)                 # sub shell --> input to main shell
Sat  23 Aug 2003 08 30 36 +0200
$ unset IFS                       # reset IFS to the default
$ echo $(date -R)
Sat, 23 Aug 2003 08:30:50 +0200

2.6.5. Script snippets for piping commands

The following scripts will do nice things as a part of a pipe.

Table 2.27. The script snippets for piping commands.

script snippet (type in one line)

effect

find /usr -print

find all files under /usr (see Section 11.1.5, “Idioms for the selection of files”)

seq 1 100

print 1 to 100

| xargs -n 1 <command>

run command repeatedly with each item from pipe as its argument (see Section 10.5.9, “Repeating a command looping over files”)

| xargs -n 1 echo

split white-space-separated items from pipe into lines

| xargs echo

merge all lines from pipe into a line

| grep -e <regex_pattern>

extract lines from pipe containing <regex_pattern>

| grep -v -e <regex_pattern>

extract lines from pipe not containing <regex_pattern>

| cut -d: -f3 -

extract third field from pipe separated by ":" (passwd file etc.)

| awk '{ print $3 }'

extract third field from pipe separated by whitespaces

| awk -F'\t' '{ print $3 }'

extract third field from pipe separated by tab

| col -bx

remove backspace and expand tabs to spaces

| expand -

expand tabs

| sort| uniq

sort and remove duplicates

| tr 'A-Z' 'a-z'

convert uppercase to lowercase

| tr -d '\n'

concatenate lines into one line

| tr -d '\r'

remove CR

| sed 's/^/# /'

add "#" to the start of each line

| sed 's/\.ext//g'

remove ".ext"

| sed -n -e 2p

print the second line

| head -n 2 -

print the first 2 lines

| tail -n 2 -

print the last 2 lines


When using the shell interactive mode becomes too complicated, please consider to write a shell script (see: Section 13.1, “The shell script”).

2.6.6. Perl one liner for the regular-expression substitution

The following execution of perl(1) one liner command will replace all instances of FROM_REGEX with TO_TEXT in all of the files <target_file> ...:

$ perl -i -p -e 's/FROM_REGEX/TO_TEXT/g;' <target_file> ...

"-i" is for "in-place editing", "-p" is for "implicit loop over <target_file> ...". If the substitution is complex, you can make recovery from errors easier by using the parameter "-i.bak" instead of "-i"; this will keep each original file, adding ".bak" as a file extension.

[Note] Note

Although this is somewhat waste of the resource, this is used frequently to change file contents across the whole directly with minimal typing.

You can do the similar with ed(1) command too.

$ ed <target_file> <<EOF
,s/FROM_REGEX/TO_TEXT/g
w
q
EOF

Here, the ed commands are practically the same command as the vi command-mode command.

Table 2.28. The comparison of ed vs perl for in-place editing.

command

type

argument

regex

script

ed

lighter and faster

works on one file

BRE

read from stdin

perl

heavier and slower

works on multiple files

ERE

can be as a part of the argument


Chapter 3. Debian package management

Debian is a volunteer organization which builds consistent distributions of pre-compiled binary packages of free software and distributes them from its archive.

The Debian archive is offered by many remote mirror sites for access through HTTP and FTP methods. It is also available as CD-ROM/DVD.

The Debian package management system, when used properly, offers the user to install consistent sets of binary packages to the system from the archive. Currently, there are 24396 packages available for the amd64 architecture.

The Debian package management system has a rich history and many choices for the front end user program and back end archive access method to be used. Currently, we recommend aptitude as the main front end program for the Debian package management activity.

Table 3.1. List of Debian package management tools

package

popcon

size

description

aptitude

V:23, I:98

9808

terminal-based package manager (current standard, front-end for apt)

apt

V:85, I:99

5216

Advanced Packaging Tool, front-end for dpkg providing "http", "ftp", and "file" archive access methods (apt-get/apt-cache commands included)

tasksel

V:6, I:94

884

tool for selecting tasks for installation on Debian system (front-end for apt)

dselect

V:5, I:82

2192

terminal-based package manager (previous standard, front-end for apt and other old access methods)

dpkg

V:85, I:99

7340

package management system for Debian

dpkg-ftp

V:0.10, I:0.5

136

older ftp method for dselect

synaptic

V:20, I:52

5952

graphical package manager (Gnome front-end for apt)

gnome-apt

V:0.2, I:1.4

604

graphical package manager (Gnome front-end for apt)

kpackage

V:5, I:14

1496

graphical package manager (KDE front-end for apt)

apt-utils

V:49, I:99

456

APT utility programs: apt-extracttemplates(1), apt-ftparchive(1), and apt-sortpkgs(1)

apt-listchanges

V:3, I:6

264

package change history notification tool

apt-listbugs

V:1.6, I:2

436

lists critical bugs before each APT installation

apt-file

V:1.8, I:8

172

APT package searching utility -- command-line interface

apt-rdepends

V:0.18, I:0.9

92

recursively lists package dependencies


[Note] Note

The annoying bug #411123 for the mixed use of aptitude and apt-get commands has been resolved. If this kept you from using aptitude, please reconsider.

3.1. Debian package management prerequisites

3.1.1. Package configuration

Here are some key points for package configuration on the Debian system:

  • The manual configuration by the system administrator is respected. In other words, the package configuration system makes no intrusive configuration for the sake of convenience.

  • Each package comes with its own configuration script with standardized user interface called debconf(7) to help initial installation process of the package.

  • Debian Developers try their best to make your upgrade experience flawless with package configuration scripts.

  • Full functionalities of packaged software are available to the system administrator. But ones with security risks are disabled in the default installation.

  • If you manually activate a service with some security risks, you are responsible for the risk containment.

  • Esoteric configuration may be manually enabled by the system administrator. This may creates interference with popular generic helper programs for the system configuration.

3.1.2. Basic precautions

[Warning] Warning

Do not install packages from random mixture of suites. It will likely break the package consistency which requires deep system management knowledge, such as compiler ABI, library version, interpreter features, etc.

The newbie Debian system administrator should stay with the stable release of Debian while applying only security updates. I mean that some of the valid actions are better avoided, as a precaution, until you understand the Debian system very well:

  • Do not include testing or unstable in /etc/apt/sources.list,

  • Do not mix standard Debian with other non-Debian archives such as Ubuntu in /etc/apt/sources.list,

  • Do not create /etc/apt/preferences,

  • Do not change default behavior of package management tools through configuration files without knowing their full impacts,

  • Do not install random packages by "dpkg -i <random_package>",

  • Do not ever install random packages by "dpkg --force-all -i <random_package>",

  • Do not erase or alter files in /var/lib/dpkg/, or

  • Do not overwrite system files by installing software programs directly compiled from source. (Install them into /usr/local or /opt.)

The non-compatible effects caused by above actions to the Debian package management system may leave your system unusable.

The serious Debian system administrator who runs mission critical servers, should use extra precautions:

  • Do not install any packages including security updates from Debian without thoroughly testing them with your particular configuration under safe conditions. (Although Debian has been offering an extremely stable system for a long time, you as the system administrator are responsible for your system in the end.)

3.1.3. Life with eternal upgrades

Despite my warnings above, I know many readers of this document wish to run the "testing" or "unstable" suites of Debian as their main Desktop system since they work very well for self-administered Desktop environments. Because they are updated frequently, they offer the latest features.

[Caution] Caution

For your production server, the "stable" suite with the security updates is recommended. The same can be said for desktop PCs on which you can spend limited administration efforts, e.g. for your mother's PC.

It takes no more than simply setting the distribution string in the /etc/apt/sources.list to the suite: "testing" or "unstable"; or the codename: "squeeze" or "sid". This will let you live the life of eternal upgrades.

The use of "testing" or "unstable" is a lot of fun but comes with some risks. Even though the "unstable" suite of Debian system looks very stable for most of the times, there have been some package problems on the "testing" and "unstable" suite of Debian system and a few of them were not so trivial to resolve. It may be quite painful for you. Sometimes, you may have a broken package or missing functionality for a few weeks.

Here are some ideas to ensure quick and easy recovery from bugs in Debian packages:

  • make the system dual bootable by installing the "stable" suite of Debian system to another partition.

  • make the installation CD handy for the rescue boot.

  • consider installing apt-listbugs to check the Debian Bug Tracking System (BTS) information before the upgrade.

  • learn the package system infrastructure enough to work around the problem.

  • create a chroot or similar environment and run the latest system in it in advance. (optional)

(If you can not do any one of these precautionary actions, you are probably not ready for the "testing" and "unstable" suites.)

Enlightenment with the following will save a person from the eternal karmic struggle of upgrade hell and let him reach Debian nirvana.

3.1.4. Debian archive basics

Let's look into the Debian archive from a system user's perspective.

[Tip] Tip

Official policy of the Debian archive is defined at Debian Policy Manual, Chapter 2 - The Debian Archive.

For the typical HTTP access, the archive is specified in the /etc/apt/sources.list file as, e.g. for the current "stable" == "lenny" system:

deb http://ftp.XX.debian.org/debian/ lenny main contrib non-free
deb-src http://ftp.XX.debian.org/debian/ lenny main contrib non-free

deb http://security.debian.org/ lenny/updates main contrib
deb-src http://security.debian.org/ lenny/updates main contrib

Please note "ftp.XX.debian.org" must be replaced with appropriate mirror site URL for your location, for USA "ftp.us.debian.org", which can be found in the list of Debian worldwide mirror sites. The status of these servers can be checked at Debian Mirror Checker site.

Here, I tend to use codename "lenny" instead of suite name "stable" to avoid surprises when the next "stable" is released.

The meaning of this is described in "man 5 sources.list" and key points are:

  • The "deb" line defines for the binary packages.

  • The "deb-src" line defines for the source packages.

  • The 1st argument is the root URL of the Debian archive.

  • The 2nd argument is the distribution: either the suite name or the codename.

  • The 3rd and following arguments are the list of valid archive component names of the Debian archive.

The "deb-src" lines can safely be omitted (or commented out by placing "#" at the start of the line) if it is just for aptitude which does not access source related meta data. It will speed up the updates of the archive meta data. The URL can be "http://", "ftp://", "file://", ....

[Tip] Tip

If "sid" is used in the above example instead of "lenny", the "deb: http://security.debian.org/ ..." line for security updates in the /etc/apt/sources.list is not required. Security updates are only available for "stable" and "testing" (i.e., "lenny" and "squeeze").

Here are the lists of URL of the Debian archive sites and suite or codename used in the configuration file:

Table 3.2. Lists of Debian archive sites.

archive URL

suite (codename)

purpose

http://ftp.XX.debian.org/debian/

stable (lenny)

stable (lenny) release

http://ftp.XX.debian.org/debian/

testing (squeeze)

testing (squeeze) release

http://ftp.XX.debian.org/debian/

unstable (sid)

unstable (sid) release

http://ftp.XX.debian.org/debian/

experimental

experimental pre-release (optional, only for developer)

http://ftp.XX.debian.org/debian/

stable-proposed-updates

Updates for the next stable point release (optional)

http://security.debian.org/

stable/updates

Security updates for stable release (important)

http://security.debian.org/

testing/updates

Security updates for testing release (important)

http://volatile.debian.org/debian-volatile/

volatile

Compatible updates for spam filter and IM clients, etc.

http://volatile.debian.org/debian-volatile/

volatile-sloppy

Non-compatible updates for spam filter, IM clients, etc.

http://backports.org/debian/

lenny-backports

Newer backported packages for lenny. (non-official, optional)


[Tip] Tip

For the Debian system with the "stable" and "testing" suites, it is a good idea to include lines with http://security.debian.org/ in the /etc/apt/sources.list to enable access to the archive for security updates as in the example above.

[Caution] Caution

Only pure stable release with security updates provides the best stability. Running mostly stable release mixed with some packages from testing or unstable release is riskier than running pure unstable release. If you really need the latest version of some programs under stable release, please use packages from the debian-volatile project and http://backports.org (see: Section 3.7.4, “Volatile and Backports.org”) services. These services must be used with extra care.

[Caution] Caution

You should basically list only one of "stable", "testing", or "stable" suites in the "deb" line. If you list any combination of "stable", "testing", and "stable" suites in the "deb" line, APT programs slow down while only the latest archive is effective. Multiple listing makes sense for these when the /etc/apt/preferences file is used with clear objectives (see: Section 3.7.3, “Tweaking candidate version”).

Each Debian archive consists of 3 components. Components are alternatively called categories in "Debian Policy" or areas in "Debian Social Contract". The component is grouped by the compliance to "The Debian Free Software Guidelines " (DFSG):

Table 3.3. The lists of Debian archive components.

component

number of packages

criteria

main

23809

The package is fully compliant to DSFG and does not depend the non-free package.

contrib

211

The package is compliant to the DSFG but depends on the non-free package.

non-free

376

The package is not compliant to the DSFG but distributable and useful.


Here the number of packages in the above is for the amd64 architecture. Strictly speaking, only the main component archive shall be considered the Debian system.

The Debian archive organization can be studied best by pointing your browser to the each archive URL appended with dists or pool.

The distribution is referred by two ways, the suite or codename. The word distribution is alternatively used as the synonym to the suite in many documentations. The relationship between the suite and the codename can be summarized as:

Table 3.4. The relationship between suite and codename.

Timing

suite = "stable"

suite ="testing"

suite ="unstable"

after the "lenny" release

codename = "lenny"

codename = "squeeze"

codename = "sid"

after the "squeeze" release

codename = "squeeze"

codename = "squeeze+1"

codename = "sid"


The history of codenames are described in Debian FAQ: 6.3.1 Which other codenames have been used in the past?

In the stricter Debian archive terminology, the word "section" is specifically used for the categorization of packages by the application area. (Although, the word "main section" may sometimes be used to describe the Debian archive section which provides the main component.)

Every time a new upload is done by the Debian developer (DD) to the "unstable" archive (via incoming processing), DD is required to ensure uploaded packages to be compatible with the latest set of packages in the latest "unstable" archive.

If DD breaks this compatibility intentionally for important library upgrade etc, there is usually announcement to the debian-devel mailing list etc.

Before a set of packages are moved by the Debian archive maintenance script from the "unstable" archive to the "testing" archive, the archive maintenance script not only checks the maturity (about 10 days old) and the status of the RC bug reports for the packages but also tries to ensure them to be compatible with the latest set of packages in the "testing" archive. This process makes the "testing" archive very current and usable.

Through the gradual archive freeze process led by the release team, the "testing" archive will be matured to make it completely consistent and bug free with some manual interventions. Then the new "stable" release is created by assigning the codename for the old "testing" archive to the new "stable" archive and creating the new codename for the new "testing" archive. The initial contents of the new "testing" archive is exactly the same as that of the newly released "stable" archive.

Both the "unstable" and the "testing" archives may suffer temporary glitches due to:

  • broken package upload to the archive (mostly for "unstable"),

  • delay of accepting the new packages to the archive (mostly for "unstable"),

  • archive synchronization timing issue (both for "testing" and "unstable"),

  • manual intervention to the archive such as package removal (more for "testing"), etc.

So if you ever decide to use these archives, you should be able to fix or work around these kinds of glitches.

[Tip] Tip

When tracking the "testing" archive, problem caused by a removed package is usually worked around by installing corresponding package from the "unstable" archive which is uploaded for bug fix.

See Debian Policy Manual for definition of:

3.1.5. Package dependencies

The Debian system offers a consistent set binary packages through its versioned binary dependency declaration mechanism through the control file fields. Here is a bit over simplified definition for them.

Table 3.5. List of package dependencies.

dependency

meaning

Depends

This declares an absolute dependency and all of the packages listed in this field must be installed at the same time or in advance.

Pre-Depends

This is like Depends, except that it requires completed installation of the listed packages in advance.

Recommends

This declares a strong, but not absolute, dependency. Most users would not want the package unless all of the packages listed in this field are installed.

Suggests

This declares a weak dependency. Many users of this package may benefit from installing packages listed in this field but can have reasonable functions without them.

Enhances

This declares a week dependency like Suggests but works in the opposite direction.

Conflicts

This declares an absolute incompatibility. All of the packages listed in this field must be removed to install this package.

Replaces

This is declared when files installed by this package replace files in the listed packages.

Provides

This is declared when this package provide all of the files and functionality in the listed packages.


[Note] Note

Please note that defining, Provides, Conflicts and Replaces simultaneously to an virtual package is the sane configuration. This ensures that only one real package providing this virtual package can be installed at any one time.

The official definition including source dependency can be found in the Policy Manual: Chapter 7 - Declaring relationships between packages.

3.1.6. The event flow of the package management

The simplified event flow of the update is:

  • The local copy of package archive metadata is updated by the remote one(s) as: fetch -> reconstruct

The simplified event flow of the upgrade (safe-upgrade, full-upgrade, upgrade, and dist-upgrade) and the install are:

  1. APT system makes decision on candidate version which is usually the latest available version. (See Section 3.7.3, “Tweaking candidate version” for exception.)

  2. The system administrator makes choice such as upgrade of the entire system or install several new packages with candidate version.

  3. Selected binary packages are processed as: fetch -> unpack -> preinst -> install -> postinst

The package removal process has 2 distinct stages and the simplified event flow of them are:

  • remove : remove all installed files except configuration files as: prerm -> remove -> postrm

  • purge : purge all installed files completely including configuration files as: prerm -> purge -> postrm

Here, I intentionally skipped technical details for the sake of big picture.

3.1.7. First response to package management troubles

You should read the fine official documentation. The first document to read is the Debian specific /usr/share/doc/<package_name>/README.Debian. Other documentation in /usr/share/doc/<package_name>/ should be consulted too. If you set shell as previously discussed, type:

$ cd <package_name>
$ pager README.Debian
$ mc

You may need to install the corresponding documentation package named with "-doc" suffix for detailed information.

If you are experiencing problems with a specific package, make sure to check out these sites first:

Table 3.6. List of key web site to resolving problems with a specific package.

site

command

Home page of the Debian bug tracking system (BTS)

$ sensible-browser http://bugs.debian.org/

The bug report of a known package name.

$ sensible-browser http://bugs.debian.org/<package_name>

The bug report of known bug number.

$ sensible-browser http://bugs.debian.org/<bug_number>


Search Google with search words including "site:debian.org".

When you file a bug report, please use reportbug command.

3.2. Basic package management operations

Aptitude is the current preferred package management tool for the Debian system. It can be used as the commandline alternative to apt-get / apt-cache and also as the full screen interactive package management tool.

For the package management operation which involves package installation or updates package metadata, you need to have root privilege.

3.2.1. Basic package management operations with commandline

Here are package management operations with commandline using aptitude(8) and apt-get(8) /apt-cache(8).

Table 3.7. Package management operations with commandline using aptitude and apt-get / apt-cache.

aptitude syntax

apt-get/apt-cache syntax

description

"aptitude update"

"apt-get update"

Update package archive metadata.

"aptitude install foo"

"apt-get install foo"

Install candidate version of "foo" package with its dependencies.

"aptitude safe-upgrade"

"apt-get upgrade"

Install candidate version of installed packages without removing any other packages.

"aptitude full-upgrade"

"apt-get dist-upgrade <package>"

Install candidate version of installed packages while removing other packages if needed.

"aptitude remove foo

"apt-get remove foo"

Remove "foo" package while leaving its configuration files.

N/A

"apt-get autoremove"

Remove auto-installed packages which is no longer required.

"aptitude purge foo"

"apt-get purge foo"

Purge "foo" package with its configuration files.

"aptitude clean"

"apt-get clean"

Clear out the local repository of retrieved package files completely.

"aptitude autoclean"

"apt-get autoclean"

Clear out the local repository of retrieved package files for outdated packages.

"aptitude show foo"

"apt-cache show <package>"

Display detailed information about "foo" package.

"aptitude search <regex>"

"apt-cache search <regex>"

Search packages which match <regex>.

"aptitude why <regex>"

N/A

Explain the reason why <regex> matching packages should be installed.

"aptitude why-not <regex>"

N/A

Explain the reason why <regex> matching packages can not be installed.


Although it is now safe to mix different package tools on the Debian system, it is best to continue using aptitude as much as possible.

The difference between "safe-upgrade" and "full-upgrade" only appears when new versions of packages stand in different dependency relationships from old versions of those packages. The "aptitude safe-upgrade" command will never install new packages nor remove installed packages.

The "aptitude why <regex>" can list more information by "aptitude -v why <regex>". Similar information can be obtained by "apt-cache rdepends <package>".

When aptitude command is started in the commandline mode and faces some issues such as package conflicts, you can switch to the full screen interactive mode by pressing "e"-key later at the prompt.

You may provide command options right after "aptitude".

Table 3.8. Notable command options for "aptitude".

command option

effects

-s

simulate the result of the command.

-d

download only but no install/upgrade.

-D

show brief explanations before the automatic installations and removals.


See aptitude(8) and the "User's Manual" /usr/share/doc/aptitude/README for more.

[Tip] Tip

The dselect package is still available and was the preferred full screen interactive package management tool in previous releases.

3.2.2. Interactive use of aptitude

For the interactive package management, you start aptitude in interactive mode from the console shell prompt as:

$ sudo aptitude -u
Password:

This will update the local copy of the archive information and display the package list in the full screen with menu. Aptitude places its configuration at $HOME/.aptitude/config.

[Tip] Tip

If you want to use root's configuration instead of user's one, use "sudo -H aptitude ..." instead of "sudo aptitude ..." in the above expression.

[Note] Note

Aptitude automatically sets pending actions as it is started interactively. If you do not like it, you can reset it from menu: "Action" -> "Cancel pending actions".

3.2.3. Key bindings of aptitude

Notable key strokes to browse status of packages and to set "planned action" on them in this full screen mode are:

Table 3.9. List of key bindings for aptitude.

key

key binding

F10 or in a terminal window Ctrl-t

Menu

?

Display help for keystroke (more complete listing)

F10 -> Help -> User's Manual

Display User's Manual

u

Update package archive information

+

Mark the package for the upgrade or the install

-

Mark the package for the remove (keep conffiles)

_

Mark the package for the purge (remove conffiles)

=

Place the package on hold

U

Mark all upgradable packages (function as dist-upgrade)

g

Start downloading and installing selected packages

q

Quit current screen and save changes

x

Quit current screen and discard changes

Enter

View information about a package

C

View a package's changelog

l

Change the limit for the displayed packages

/

Search for the first match

\

Repeat the last search


The file name specification of the command line and the menu prompt after pressing "l" and "/" take the aptitude regex as described below. For the input to the menu prompt and argument to "aptitude search" command, "~n" is prepended to match the package name with the pattern if the input does not start with "~" character.

[Tip] Tip

You need to press "U" to get all the installed packages upgraded to the candidate version in the visual interface. Otherwise only the selected packages and certain packages with versioned dependency to them are upgraded to the candidate version.

3.2.4. Package views under aptitude

In the interactive full screen mode of aptitude(8), packages in the package list are displayed like this by default:

idA   libsmbclient                             -2220kB 3.0.25a-1  3.0.25a-2

Here, this line means from the left as:

  • The "current state" flag (the first letter)

  • The "planned action" flag (the second letter)

  • The "automatic" flag (the third letter)

  • The package name

  • The change in disk space usage attributed to "planned action".

  • The current version of the package.

  • The candidate version of the package.

[Tip] Tip

The full list of flags are given at the bottom of Help screen shown by pressing "?".

The candidate version is chosen according to the current local policy and preferences (see apt_preferences(5)).

Several types of package views are available under the menu "Views":

Table 3.10. Views for aptitude.

view

categorization

status

Package View

See Table 3.11, “The categorization of standard aptitude views. ”. (default)

Good

Audit Recommendations

Packages which are recommended by some installed packages but not yet installed are listed.

Good.

Flat Package List

Packages are listed without categorization (for use with regex).

Good

Debtags Browser

Packages are categorized according to their debtags entries.

Very usable

Categorical Browser

Packages are categorized according to their category.

Deprecated (Use debtags!)


The standard "Package View" categorizes packages somewhat like dselect with few extra features. For switching distribution to a newer one can be achieved basically by

Table 3.11. The categorization of standard aptitude views.

category

organization

"Upgradable Packages"

Organized as section --> component --> package

"New Packages"

, ,

"Installed Packages"

, ,

"Not Installed Packages"

, ,

"Obsolete and Locally Created Packages"

, ,

"Virtual Packages"

You can pick a particular package from a set of packages with the same function.

"Tasks"

You can cherry pick particular packages from a set of packages of a task.


3.2.5. Search method options with aptitude

Aptitude offers several options for you to search packages using its regex formula:

  • "aptitude search '<aptitude_regex>'" to list their installation status, package name and short description.

  • "aptitude show '<package_name>'" to list their installation detailed description.

  • limit view to matching packages: Type "l" in the full screen mode.

  • search the first found package: type "/" in the full screen mode. "n" for find-next, "\" for backward search.

Here, the string for <package_name> is treated as the exact string match to the package name unless it is started explicitly with "~" to be the regex formula.

3.2.6. The aptitude regex formula

The aptitude regex formula is mutt-like extended ERE (see: Section 2.6.2, “Regular expressions”) and the meanings of the aptitude specific special match rule extensions are as below:

Table 3.12. List of the aptitude regex formula.

meaning of the extended match rule

regex formula

match on package name

~n<regex_name>

match on description

~d<regex_description>

match on task name

~t<regex_task>

match on debtag

~G<regex_debtag>

match on maintainer

~m<regex_maintainer>

match on package section

~s<regex_section>

match on package version

~V<regex_version>

match archive

~A{sarge,etch,sid}

match origin

~O{debian,...}

match priority

~p{extra,important,optional,required,standard}

match essential packages

~E

match virtual packages

~v

match new packages

~N

match with pending action

~a{install,upgrade,downgrade,remove,purge,hold,keep}

match installed packages

~i

match installed packages with A-mark (auto installed package)

~M

match installed packages without A-mark (administrator selected package)

~i!~M

match installed and upgradable packages

~U

match removed but not purged packages

~c

match removed, purged or can-be-removed packages

~g

match with broken relation

~B<type>

match broken depends/predepends/conflict packages

~b

match packages whose control files define relation <type> to the <term> package

~D[<type>:]<term>

match packages whose control files define broken relation <type> to the <term> package

~DB[<type>:]<term>

match packages to which the <term> package defines relation <type>

~R[[<type>:]<term>

match packages to which the <term> package defines broken relation <type>

~RB[<type>:]<term>

match packages to which some other installed packages depend on

~R~i

match packages to which no other installed packages depend on

!~R~i

match packages to which some other installed packages depend or recommend on

~R~i|~Rrecommends:~i

match <term> package with filtered version

~S filter <term>

match all packages (true)

~T

match no packages (false)

~F


Here,

  • regex part is the same ERE as the one used in typical Unix-like text tools using "^", ".*", "$" etc. as in egrep(1), awk(1) and perl(1).

  • relation <type> is one of (depends, predepends, recommends, suggests, conflicts, replaces, provides).

  • the default relation type is "depends".

[Tip] Tip

When <regex_pattern> is a null string, place "~T" immediately after the command.

Short cuts:

  • "~P<term>" == "~Dprovides:<term>"

  • "~C<term>" == "~Dconflicts:<term>"

  • "...~W term" == "(...|term)"

Users familiar with mutt will pick up quickly, as mutt was the inspiration for the expression syntax. See "SEARCHING, LIMITING, AND EXPRESSIONS" in the "User's Manual" /usr/share/doc/aptitude/README.

[Note] Note

With the "lenny" version of aptitude(8), the new long form syntax such as "?broken" may be used for regex matching in place for its old short form equivalent "~b". Now space character " " is considered as one of the regex terminating character in addition to tilde character "~". See "User's Manual" for the new long form syntax.

3.2.7. Dependency resolution of aptitude

The selection of a package in aptitude not only pulls in packages which are defined in its "Depends:" list but also defined in the "Recommends:" list if the menu "F10 -> Options -> Dependency handling" is set accordingly. These auto installed packages are removed automatically if they are no longer needed under aptitude.

[Note] Note

Before the "lenny" release, apt-get and other standard APT tools did not offer the autoremove functionality.

3.2.8. Package activity logs

You can check package activity history in the log files.

Table 3.13. The log files for package activities.

file

content

/var/log/dpkg.log

Log of dpkg level activity for all package activities.

/var/log/apt/term.log

Log of generic APT activity.

/var/log/aptitude

Log of aptitude command activity.


In reality, it is not so easy to get meaningful understanding quickly out from these logs. See Section 10.2.8, “Recording changes in configuration files” for easier way.

3.2.9. Aptitude advantages

Aptitude has advantages over other APT based packaging systems (apt-get, apt-cache, synaptic, ...):

  • aptitude removes unused auto installed packages automatically using its own extra layer of package state file (/var/lib/aptitude/pkgstates). (For new "lenny", other APT does the same.)

  • aptitude makes it easy to resolve package conflicts and to add recommended packages.

  • aptitude makes it easy to keep track of obsolete software by listing under "Obsolete and Locally Created Packages".

  • aptitude gives a log of its history in /var/log/aptitude.

  • aptitude offers access to all versions of the package if available.

  • aptitude includes a fairly powerful regex based system for searching particular packages and limiting the package display.

  • aptitude in the full screen mode has su functionality embedded and can be run from normal user until you really need administrative privileges.

For the old "etch" release version, synaptic also gives you the history log; apt-get did not but you can rely on the log of dpkg.

Anyway, aptitude is nice for interactive console use.

3.3. Examples of aptitude operations

Here are few examples of aptitude(8) operations.

3.3.1. List packages with regex matching package name

The following command lists packages with regex matching names.

$ aptitude search '~n(pam|nss).*ldap'
p libnss-ldap - NSS module for using LDAP as a naming service
p libpam-ldap - Pluggable Authentication Module allowing LDAP interfaces

This is quite handy for you to find the exact name of a package.

3.3.2. Browse with the regex matching

The regex "~dipv6" in the "New Flat Package List" view with "l" prompt, limits view to packages with the matching description and let you browse their information interactively.

3.3.3. Purge removed packages for good

You can purge all remaining configuration files of removed packages:

# aptitude search '~c'
  • check results

# aptitude purge '~c'

You may want to do the similar in the interactive mode for fine grained control.

You provide the regex "~c" in the "New Flat Package List" view with "l" prompt. This limits the package view only to regex matched packages, i.e., "removed but not purged". All these regex matched packages can be shown by pressing "[" at top level headings.

Then you press "_" at top level headings such as "Installed Packages". Only regex matched packages under the heading are marked to be purged by this. You can exclude some packages to be purged by pressing "=" interactively for each of them.

This technique is quite handy and works for many other command keys.

3.3.4. Tidy auto/manual install status

Here is how I tidy auto/manual install status for packages (after using non-aptitude package installer etc.):

  • Start aptitude in interactive mode as root.

  • Type "u", "U", "f" and "g" to update and upgrade package list and packages.

  • Type "l" to enter the package tree limit as "~i(~R~i|~Rrecommends:~i)" and type "M" over "Installed Packages" as auto installed.

  • Type "l" to enter the package tree limit as "~prequired|~pimportant|~pstandard|~E" and type "m" over "Installed Packages" as manual installed.

  • Type "l" to enter the package tree limit as "~i!~M" and remove unused package by typing "-" over each of them after exposing them by typing "[" over "Installed Packages".

  • Type "l" to enter the package tree limit as "~i" and type "m" over "Tasks" as manual installed.

  • Exit aptitude.

  • Start "apt-get -s autoremove|less" as root to check what are not used.

  • Restart aptitude in interactive mode and mark needed packages as "m".

  • Restart "apt-get -s autoremove|less" as root to recheck REMOVED contain only expected packages.

  • Start "apt-get autoremove|less" as root to autoremove unused packages.

The "m" action over "Tasks" is an optional one to prevent mass package removal situation in future.

3.3.5. System wide upgrade with aptitude

[Note] Note

When moving to a new release etc, you should consider to perform a clean installation of new system even though Debian is upgradable as described below. This provides you a chance to remove garbages collected and exposes you to the best combination of latest packages. Of course, you should make a full backup of system to a safe place (see Section 11.1.6, “Backup and recovery”) before doing this. I recommend to make a dual boot configuration using different partition to have the smoothest transition.

You can perform system wide upgrade to a newer release by changing contents of the /etc/apt/sources.list file pointing to a new release and running the "aptitude update; aptitude dist-upgrade" command.

To upgrade from "stable" to "testing" or "unstable", you replace "lenny" in the /etc/apt/sources.list example of Section 3.1.4, “Debian archive basics” with "squeeze" or "sid".

In reality, you may face some complications due to some package transition issues, mostly due to package dependencies. The larger the difference of the upgrade, the more likely you face larger troubles. For the transition from the old "stable" archive to the new "stable" after its release, you can read its new Release Notes and follow the exact procedure described in it to minimize troubles.

When you decide to move from "stable" to "testing" before its formal release, there are no Release Notes to help you. The difference between "stable" and "testing" could have grown quite large after the previous "stable" release and makes upgrade situation complicated.

You should make some precautionary moves while gathering latest information from mailing list and using common senses:

  • read previous "Release Notes".

  • back up entire system (especially data and configuration information).

  • have bootable media handy for broken bootloader.

  • inform users on the system well in advance.

  • record upgrade activity with the script(1) command.

  • apply "unmarkauto" to essential packages, e.g., "aptitude unmarkauto vim", to prevent removal.

  • minimize installed packages to reduce chance of package conflicts, e.g., remove desktop task packages.

  • remove the /etc/apt/preferences file. (disable apt-pinning)

  • try to upgrade step wise: "oldstable" --> "stable" --> "testing" --> "unstable".

  • update the /etc/apt/sources.list file to point to new archive only and run "aptitude update".

  • install, optionally, new core packages first, e.g., "aptitude install perl".

  • run the "aptitude dist-upgrade -s" command to assess impact.

  • run the "aptitude dist-upgrade" command.

[Caution] Caution

It is not wise to skip major Debian release when upgrading between "stable" releases.

[Caution] Caution

In previous "Release Notes", GCC, Linux Kernel, initrd-tools, Glibc, Perl, APT tool chain, etc. have required some special attention for system wide upgrade.

For daily upgrade in "unstable", see Section 3.4.3, “Safeguard for package problems”.

3.4. Advanced package management operations

3.4.1. Advanced management operations with commandline

Here are list of other package management operations for which aptitude is too high-level or lacks required functionalities.

Table 3.14. List of advanced package management operations.

action

command

list status of an installed package for the bug report.

"COLUMNS=120 dpkg -l <package_name_pattern>"

list the contents of an installed package.

"dpkg -L <package_name>"

list the manpages for an installed package.

"dpkg -L <package_name> | egrep '/usr/share/man/man.*/.+'"

list installed packages which have matching file name.

"dpkg -S <file_name_pattern>"

list packages in archive which have matching file name.

"apt-file search <file_name_pattern>"

list the contents of matching packages in archive.

"apt-file list <package_name_pattern>"

reconfigure the exact package .

"dpkg-reconfigure <package_name>"

reconfigure the exact package with the most detailed question.

"dpkg-reconfigure -p=low <package_name>"

reconfigure packages from the full screen menu.

"configure-debian"

audit system for partially installed packages.

"dpkg --audit"

configures all partially installed packages.

"dpkg --configure -a"

show available version, priority, and archive information of a binary package.

"apt-cache policy <binary_package_name>"

show available version, archive information of a package.

"apt-cache madison <package_name>"

show source package information of a binary package.

"apt-cache showsrc <binary_package_name>"

install required packages to build package.

"apt-get build-dep <package_name>"

download a source. (from standard archive)

"apt-get source <package_name>"

download a source packages. (from other archive)

"dget <URL for dsc file>"

build a source tree from a set of source packages (*.tar.gz *.diff.gz).

"dpkg-source -x <package_name>_<version>-<debian_version>.dsc"

build package(s) from a local source tree.

"debuild binary"

build a kernel package from a kernel source tree.

"make-kpkg kernel_image"

build a kernel package from a kernel source tree with initramfs enabled.

"make-kpkg --initrd kernel_image"

install a local package to the system.

"dpkg -i <package_name>_<version>-<debian_version>_<arch>.deb"

install local package(s) to the system.

"debi <package_name>_<version>-<debian_version>_<arch>.dsc"

save dpkg level package selection state information

"dpkg --get-selection '*' >selection.txt"

set dpkg level package selection state information

"dpkg --set-selection <selection.txt"


[Caution] Caution

Use of lower level package tools such as "dpkg -i ..." and "debi ..." should be carefully used by the system administrator. It does not automatically take care required package dependencies. Dpkg's commandline options "--force-all" and similar (see dpkg(1)) are intended to be used by experts only. Using them without fully understanding their effects may break your whole system.

Please note:

[Tip] Tip

The source package format described here as a set of source packages (*.tar.gz *.diff.gz) is format 1.0 which is still popular. See more on dpkg-source(1) for other newer formats.

3.4.2. Verify installed package files

The installation of debsums package enables verification of installed package files against MD5sum values in the Packages file with debsums(1) command. See: Section 11.3.5, “The MD5 sum” for how MD5sum works.

[Note] Note

Because MD5sum database may be tampered by the intruder, debsums(1) is of limited use as a security tool. It is only good for checking local modifications by the administrator or damages due to media errors.

3.4.3. Safeguard for package problems

Many user prefer to follow the unstable release of the Debian system for its new features and packages. This makes the system more prone to be hit by the critical package bugs.

The installation of the apt-listbugs package will provide safeguard to the critical bugs by checking Debian BTS automatically for critical bugs when upgrading with APT system.

The installation of the apt-listchanges package will provide important news in NEWS.Debian when upgrading with APT system.

3.4.4. Search on the package meta data

Although visiting Debian site http://packages.debian.org/ facilitates easy ways to search on the package meta data these days, let's look into more traditional ways.

The grep-dctrl(1), grep-status(1), and grep-available(1) commands can be used to search any file which has the general format of a Debian package control file.

The "dpkg -S <file_name_pattern>" can be used search package names which contain files with the matching name installed by dpkg. But this overlooks files created by the maintainer scripts.

If you need to make more elaborate search on the dpkg meta data, you need to run "grep -e regex_pattern *" command in the /var/lib/dpkg/info/ directory. This will let you identify:

  • the package name which installs, creates or modifies particular file which match pattern.

  • the package name which asks the installation query words which match pattern.

If you wish to look up package dependency recursively, you should use apt-rdepends(8).

3.5. Debian package management internals

Let's learn how the Debian package management system works internally. This should help you to create your own solution to some package problems.

3.5.1. Archive meta data

The meta data files are stored under the dist/ on each Debian mirror sites, e.g., ftp://ftp.us.debian.org/debian/. Its archive structure can be browsed by the web browser. There are 6 types of key meta data:

Table 3.15. The content of the Debian archive meta data.

file

location

content

Release

top of distribution

archive description and integrity information

Release.gpg

top of distribution

signature file for Release signed with the archive key

Contents-<architecture>

top of distribution

list of all files for all the packages in the pertinent archive

Release

top of each distribution/component/architecture combination

archive description

Packages

top of each distribution/component/binary-architecture combination

concatenated debian/control for binary packages

Sources

top of each distribution/component/source combination

concatenated debian/control for source packages


In the recent archive, these meta data are stored as the compressed and differential files to reduce network traffic.

3.5.2. Top level Release file and authenticity

Each suites of the Debian archive has a top level Release file, e.g., ftp://ftp.us.debian.org/debian/dists/unstable/Release:

Origin: Debian
Label: Debian
Suite: unstable
Codename: sid
Date: Sat, 26 Jan 2008 20:13:58 UTC
Architectures: alpha amd64 arm hppa hurd-i386 i386 ia64 m68k mips mipsel powerpc s390 sparc
Components: main contrib non-free
Description: Debian x.y Unstable - Not Released
MD5Sum:
 e9f11bc50b12af7927d6583de0a3bd06 22788722 main/binary-alpha/Packages
 43524d07f7fa21b10f472c426db66168  6561398 main/binary-alpha/Packages.gz
...
[Note] Note

Here, you can find my rationale to use the "suite", "codeneme", and "components" in Section 3.1.4, “Debian archive basics”. The "distribution" is used when referring to both "suite" and "codeneme".

The integrity of the top level Release file is verified by cryptographic infrastructure called the secure apt.

  • The cryptographic signature file Release.gpg is created from the authentic top level Release file and the secret Debian archive key.

  • The public Debian archive signing key can be seeded into /etc/apt/trusted.gpg:

  • The secure APT system verifies the integrity of the downloaded top level Release file cryptographically by this Release.gpg file and the public Debian archive key in /etc/apt/trusted.gpg.

The integrity of all the Packages and Sources files are verified by using MD5sum values in its top level Release file. The integrity of all package files are verified by using MD5sum values in the Packages and Sources files. See debsums(1) and Section 3.4.2, “Verify installed package files”.

Since the cryptographic signature verification is very CPU intensive process than the MD5sum value calculation, use of MD5sum value for each package while using cryptographic signature for the top level Release file provides the good security with the performance (see: Section 11.3, “Data security infrastructure”).

3.5.3. Archive level Release files

[Tip] Tip

The archive level Release files are used for the rule of apt_preferences(5).

There are archive level Release files for all archive locations specified by "deb:" line in /etc/apt/sources.list, such as "ftp://ftp.us.debian.org/debian/dists/unstable/main/binary-amd64/Release" or "ftp://ftp.us.debian.org/debian/dists/sid/main/binary-amd64/Release":

Archive: unstable
Component: main
Origin: Debian
Label: Debian
Architecture: amd64
[Caution] Caution

For Archive stanza, suite names ("stable", "testing", "unstable", ...) are used in the Debian archive while codenames ("dapper", "feisty", "gutsy", "hardy", "intrepid", ...) are used in the Ubuntu archive.

For some archives, such as "experimental", "volatile-sloppy", and "lenny-backports", which contain packages which should not be installed automatically, there is an extra line, e.g., "ftp://ftp.us.debian.org/debian/dists/experimental/main/binary-amd64/Release":

Archive: experimental
Component: main
Origin: Debian
Label: Debian
NotAutomatic: yes
Architecture: amd64

Please note that for normal archives without "NotAutomatic: yes", the default Pin-Priority value is 500, while for special archives with "NotAutomatic: yes", the default Pin-Priority value is 1 (see apt_preferences(5) and Section 3.7.3, “Tweaking candidate version”).

3.5.4. Fetching of the meta data for the package

When APT tools, such as aptitude, apt-get, synaptic, apt-file, auto-apt..., are used, we need to update the local copies of the meta data containing the Debian archive information. These local copies have file names corresponding to the specified distribution component and architecture names in the /etc/apt/sources.list (see: Section 3.1.4, “Debian archive basics”) under the /var/lib/apt/lists directory as:

  • ftp.us.debian.org_debian_dists_<distribution>_Release

  • ftp.us.debian.org_debian_dists_<distribution>_Release.gpg

  • ftp.us.debian.org_debian_dists_<distribution>_<section>_binary-<architecture>_Packages

  • ftp.us.debian.org_debian_dists_<distribution>_<section>_source_Sources

  • /var/cache/apt/apt-file/ftp.us.debian.org_debian_dists_<distribution>_Contents-<architecture>.gz (for apt-file)

First 4 are shared by all the pertinent APT commands and updated from command line by "apt-get update" and "aptitude update". The Packages meta data are updated if there is the deb line in /etc/apt/sources.list. The Sources meta data are updated if there is the deb-src line in /etc/apt/sources.list.

The Packages and Sources meta data contain "Filename:" stanza pointing to the file location of the binary and source packages. Currently, they are located under the pool/ directory tree for the improved transition over the releases.

The local copies of Packages meta data can be interactively searched with the help of aptitude. The specialized search command grep-dctrl(1) can search the local copies of Packages and Sources meta data.

The local copyies of Contents-<architecture> files can be updated by "apt-file update" and location is different from other 4 files. See apt-file(1). (The auto-apt uses different location for local caching of Contents-<architecture>.gz as default.)

3.5.5. The package state for APT

In addition to the remotely fetched meta data, the APT tool after "lenny" stores its locally generated installation state information in the /var/lib/apt/extended_states which is used only by all APT tools to track all auto installed packages.

3.5.6. The package state for aptitude

In addition to the remotely fetched meta data, the aptitude command stores its locally generated installation state information in the /var/lib/aptitude/pkgstates which is used only by it.

3.5.7. The local copies of the fetched packages

All the remotely fetched packages via APT mechanism are stored in the /var/cache/apt/packages until they are cleaned.

3.5.8. The Debian package file name

The Debian package files has particular name structures:

Table 3.16. The name structures of the Debian packages.

entity

name structure

The binary package (a.k.a deb)

<package-name>_<epoch>:<upstream-version>-<debian.version>-<architecture>.deb

The binary package for the debian-installer (a.k.a udeb)

<package-name>_<epoch>:<upstream-version>-<debian.version>-<architecture>.udeb

The source package (upstream source)

<package-name>_<epoch>:<upstream-version>-<debian.version>.tar.gz

The source package (Debian changes)

<package-name>_<epoch>:<upstream-version>-<debian.version>.diff.gz

The source package (description)

<package-name>_<epoch>:<upstream-version>-<debian.version>.dsc


where,

Table 3.17. The usable characters for each component in the Debian package names.

component

usable characters (regex)

required

<package-name>

[a-z,A-Z,0-9,.,+,-]+

required

<epoch>:

[0-9]+:

optional

<upstream-version>

[a-z,A-Z,0-9,.,+,-,:]+

required

<debian.version>

[a-z,A-Z,0-9,.,+,~]+

optional


[Note] Note

You can check package version order by dpkg(1), e.g., "dpkg --compare-versions 7.0 gt 7.~pre1 ; echo $?" .

[Note] Note

The debian-installer (d-i) uses udeb as the file extension for its binary package instead of normal deb. An udeb package is a stripped down deb package which removes few non-essential contents such as documentation to save space while relaxing the package policy requirements. Both deb and udeb package share the same package structure. The "u" stands for micro.

3.5.9. The dpkg command

The dpkg is the lowest level tool for the Debian package management. This is very powerful and needs to be used with care.

The fetched package is processed by dpkg in the following order:

  1. unpack the deb file ("ar -x" equivalent)

  2. preinst using debconf

  3. install the package content to the system ("tar -x" equivalent)

  4. postinst using debconf

The debconf system provides standardized user interaction with i18n and l17n supports.

Here dpkg creates following files under /var/lib/dpkg/info/ directory : While installing package called <package_name>, dpkg creates several files and execute scripts.

Table 3.18. The notable files for dpkg.

file

contents

/var/lib/dpkg/info/<package_name>.conffiles

list of user modifiable files.

/var/lib/dpkg/info/<package_name>.list

list of files and directories installed by the package.

/var/lib/dpkg/info/<package_name>.md5sums

list of MD5 hash values for files installed by the package.

/var/lib/dpkg/info/<package_name>.preinst

package script run before the package installation.

/var/lib/dpkg/info/<package_name>.postinst

package script run after the package installation.

/var/lib/dpkg/info/<package_name>.prerm

package script run before the package removal.

/var/lib/dpkg/info/<package_name>.postrm

package script run after the package removal.

/var/lib/dpkg/info/<package_name>.config

package script for debconf system.

/var/lib/dpkg/alternatives/<package_name>

the alternative information used by the update-alternatives command.

/var/lib/dpkg/available

the availability information for all the package.

/var/lib/dpkg/diversions

the diversions information used by the dpkg-divert command.

/var/lib/dpkg/status

the status information for all the packages.

/var/lib/dpkg/status-old

the first-generation backup of the var/lib/dpkg/status file.

/var/backups/dpkg.status*

the second-generation backup and older ones of the var/lib/dpkg/status file.


The last file status is also used by the tools such as "dpkg", "select update" and "apt-get -u dselect-upgrade".

The specialized search command grep-dctrl(1) can search the local copies of status and available meta data.

[Tip] Tip

In the debian-installer environment, the udpkg command is used to open udeb packages. The udpkg is a stripped down version of dpkg command.

3.5.10. The update-alternative command

The Debian system has mechanism to install somewhat overlapping programs peacefully using update-alternatives(8). For example, to make the command vi select to run vim while installing both vim and nvi:

$ ls -l $(type -p vi)
lrwxrwxrwx 1 root root 20 2007-03-24 19:05 /usr/bin/vi -> /etc/alternatives/vi
$ sudo update-alternatives --display vi
...
$ sudo update-alternatives --config vi
  Selection    Command
-----------------------------------------------
      1        /usr/bin/vim
*+    2        /usr/bin/nvi

Enter to keep the default[*], or type selection number: 1

The Debian alternatives system keeps its selection as symlinks in /etc/alternatives/. The selection process uses corresponding file in /var/lib/dpkg/alternatives/.

3.5.11. The dpkg-statoverride command

Stat overrides provided by the dpkg-statoverride(8) command are a way to tell dpkg(1) to use a different owner or mode for a file when a package is installed. If "--update" is specified and file exists, it is immediately set to the new owner and mode.

[Note] Note

I use the word file here, but in reality this can be any filesystem object that dpkg handles, including directories, devices, etc.

[Caution] Caution

The direct alteration of owner or mode for a file owned by the package using chmod or chown commands by the system administrator will be reset by the next upgrade of the package.

3.5.12. The dpkg-divert command

File diversions provided by the dpkg-divert(8) command are a way of forcing dpkg(1) not to install a file into its default location, but to a diverted location. The use of dpkg-divert is meant for the package maintenance scripts. Its use by the system administrator is deprecated.

3.6. Recovery from a broken system

When running "unstable" system, the administrator is expected to recover from broken package management situation.

[Caution] Caution

Some methods described here are high risk actions. You have been warned!

3.6.1. Incompatibility with old user configuration

If a desktop GUI program experienced instability after significant upstream version upgrade, you should suspect interferences with old local configuration files created by it. If it is stable under newly created user account, this hypothesis is confirmed. (This is a bug of packaging and usually avoided by the packager.)

To recover stability, you should move corresponding local configuration files and restart the GUI program. You may need to read old configuration file contents to recover configuration information later. (Do not erase them too quickly.)

3.6.2. Different packages with overlapped files

Archive level package management systems, such as aptitude(8) or apt-get(1), will not even try to install packages with overlapped files using package dependencies (see Section 3.1.5, “Package dependencies”).

Errors by the package maintainer or deployment of inconsistently mixed source of archives (see Section 3.7.2, “Packages from mixed source of archives”) by the system administrator may create situation with incorrectly defined package dependencies. When you install a package with overlapped files using aptitude(8) or apt-get(1) under such situation, dpkg(1) which unpacks package ensures to return error to the calling program without overwriting existing files.

[Caution] Caution

The use of third party packages introduces significant system risks via maintainer scripts which are run with root privilege and can do anything to your system. The dpkg(1) command only protects against overwriting by the unpacking.

You can work around such broken installation by removing the old offending package, <old-package>, first:

$ sudo dpkg -P <old-package>

3.6.3. Fixing broken package script

When a command in the package script returns error for some reason and the script exits with error, the package management system aborts their action and ends up with partially installed packages. When a package contains bugs in its removal scripts, the package may become impossible to remove and quite nasty.

For the package script problem of "<package_name>", you should look for:

  • /var/lib/dpkg/info/<package_name>.preinst

  • /var/lib/dpkg/info/<package_name>.postinst

  • /var/lib/dpkg/info/<package_name>.prerm

  • /var/lib/dpkg/info/<package_name>.postrm

You edit the offending part of the script from the root:

  • to prepend with ": #" or,

  • to append with "|true".

Then configures all partially installed packages by:

# dpkg --configure -a

3.6.4. Rescue using the dpkg command

Since dpkg is very low level package tool, it can function under the very bad situation such as unbootable system without network connection. Let's assume foo package was broken and needs to be replaced.

You may still find cached copies of older version of foo package in /var/cache/apt/archives/ which is bug free. (If not, you can download it from archve of http://snapshot.debian.net/ or copy it from package cache of a functioning machine.)

If you can boot the system, you may install it by:

# dpkg -i /path/to/foo_<old_version>_<arch>.deb

[Tip] Tip

If system breakage is minor, you may alternatively downgrade the whole system as Section 3.7.5, “Emergency downgrading” using the higher level APT system.

If your system is unbootable from harddisk, you should seek other ways to boot it. For example, you can:

  • boot the system using the debian-installer CD in rescue mode,

  • mount the unbootable system on the harddisk to /target,

  • install older version of foo package by:

# dpkg --root /target -i /path/to/foo_<old_version>_<arch>.deb

This second example works even if the dpkg command on the harddisk is broken.

[Tip] Tip

Any linux system started by another system on harddisk, live linux CD, bootable USB-key drive, or netboot can be used similarly to rescue broken system.

If attempting to install a package this way fails due to some dependency violations and you really need to do this as the last resort, you can override dependency using dpkg's --ignore-depends, --force-depends and other options. If you do this, you need to make serious effort to restore proper dependency later. See dpkg(8) for details.

[Note] Note

When your system is seriously broken, you should make a full backup of system to a safe place (see Section 11.1.6, “Backup and recovery”) and should perform a clean installation. This is less time consuming and produces better results in the end.

3.6.5. Recover package selection data

If /var/lib/dpkg/status becomes corrupt for any reason, the Debian system loses package selection data and suffers severely. Look for the old /var/lib/dpkg/status file at /var/lib/dpkg/status-old or /var/backups/dpkg.status.*.

Keeping /var/backups/ in a separate partition may be a good idea since this directory contains lots of important system data.

If everything is gone, I recommend to make fresh re-install after making backup of the system in such serious breakage though. You can still recover some information from directories in /usr/share/doc/ to guide your new installation.

... reinstall minimal (desktop) system
... place old system at /path/to/old/system/
# cd /path/to/old/system/usr/share/doc
# ls -1 >~/ls1.txt
# cd /usr/share/doc
# ls -1 >>~/ls1.txt
# cd
# sort ls1.txt | uniq | less

Then you will be presented with missing package names. (There may be some non-package names such as "texmf".)

3.7. Tips for the package management

3.7.1. How to pick Debian packages

You can seek packages which satisfy your needs with aptitude from the package description or from the list under "Tasks".

When you encounter more than 2 similar packages and wonder which one to install without "trial and error" efforts, you can use some common sense. I consider following points are good indications of preferred packages.

  • essential: yes > no

  • component: main > contrib > non-free

  • priorities: required > important > standard > optional > extra

  • task: package listed in task such as desktop

  • package selected by the dependency package (e.g., python2.4 by python)

  • popcon: higher in the vote and install number

  • changelog: regular updates by the maintainer

  • BTS: No RC bugs (no critical, no grave, and no serious bugs)

  • BTS: responsive maintainer to bug reports

  • BTS: higher number of the recently fixed bugs

  • BTS: lower number of remaining non-wishlist bugs

Debian being a volunteer project with distributed development model, its archive contains many packages with different focus and quality. You must make your own decision what to do with them.

3.7.2. Packages from mixed source of archives

[Caution] Caution

Installing packages from mixed source of archives is not supported by the official Debian distribution except for officially supported particular combinations of archives such as "stable" with security updates and volatile updates.

Here is an example of operations to include specific newer upstream version packages found in "unstable" while tracking "testing" for single occasion:

  • change the /etc/apt/sources.list file temporarily to single "unstable" entry

  • run "aptitude update"

  • run "aptitude install <package-name>"

  • recover the original /etc/apt/sources.list file for "testing"

  • run "aptitude update"

You do not create the /etc/apt/preferences file nor need to worry about apt-pinning with this manual approach. But this is very cumbersome.

[Caution] Caution

When using mixed source of archives, you must ensure compatibility of packages by yourself since the Debian does not guarantee it. If package incompatibility exists, you may break system. You must be able to judge these technical requirements. The use of mixed source of random archives is completely optional operation and its use is not something I encourage you to use.

General rules for installing packages from different archives are:

  • non-binary packages ("Architecture: all") are safer to install.

    • documentation packages: no special requirements

    • interpreter program packages: compatible interpreter must be available

  • completely statically linked binary packages are safe to install.

  • binary packages (non "Architecture: all") usually face many road blocks and unsafe to install.

    • library version compatibility (including "libc")

    • related utility program version compatibility

    • Kernel ABI compatibility

    • C++ ABI compatibility

    • ...

[Note] Note

Except to avoid broken package for a short term, installing binary packages from officially unsupported archives is generally bad idea. This is true even if you use apt-pinning (see Section 3.7.3, “Tweaking candidate version”). You should consider chroot or similar techniques (see Section 10.8, “The chroot”) to run programs from different archives.

3.7.3. Tweaking candidate version

Without the /etc/apt/preferences file, APT system choses the latest available version as the candidate version using the version string. This is the normal state and most recommended usage of APT system. All officially supported combinations of archives do not require the /etc/apt/preferences file since some archives which should not be used as the automatic source of upgrades are marked as NotAutomatic and dealt properly.

[Tip] Tip

The version string comparison rule can be verified with, e.g., "dpkg --compare-versions ver1.1 gt ver1.1~1; echo $?" (see dpkg(1)).

When you install packages from mixed source of archives (see Section 3.7.2, “Packages from mixed source of archives”) regularly, you can automate these complicated operations by creating the /etc/apt/preferences file with proper entries and tweaking the package selection rule for candidate version as described in apt_preferences(5). This is called apt-pinning.

[Warning] Warning

Use of apt-pinning by a novice user is sure call for major troubles. You must avoid using apt-pinning except when you absolutely need it.

[Caution] Caution

When using apt-pinning, you must ensure compatibility of packages by yourself since the Debian does not guarantee it. The apt-pinning is completely optional operation and its use is not something I encourage you to use.

[Caution] Caution

Archive level Release files (see Section 3.5.3, “Archive level Release files”) are used for the rule of apt_preferences(5). Thus apt-pinning works only with "suite" name for normal Debian archives and security Debian archives. (This is different from Ubuntu archives). For example, you can do "Pin: release a=unstable" but can not do "Pin: release a=sid" in the /etc/apt/preferences file.

[Note] Note

Even if you do not create the /etc/apt/preferences file, you can do fairly complex system operations (see Section 3.6.4, “Rescue using the dpkg command” and Section 3.7.2, “Packages from mixed source of archives”) without apt-pinning.

Here is a simplified explanation of apt-pinning technique. Each package has its Pin-Priority value based on the entries in the /etc/apt/preferences file or the default values.

Table 3.19. List of essential default Pin-Priority values.

Pin-Priority

description

990

default value for package from the target release archive

500

default value for package from the normal archive

100

default value for package from the installed package

1

default value for package from the NotAutomatic archive


APT system normally choses highest Pin-Priority upgrading package from the available resources defined in the /etc/apt/sources.list file as the candidate version. This version restriction for upgrading is dropped to enable downgrading if the Pin-Priority of package is larger than 1000 (see Section 3.7.5, “Emergency downgrading”).

The target release can be set:

  • by /etc/apt/apt.conf, e.g., "APT::Default-Release "stable";" line in it, or

  • by "-t" option argument, e.g., "apt-get install -t testing some-package".

The archive level Release file (see Section 3.5.3, “Archive level Release files”) of NotAutomatic archive contains "NotAutomatic: yes".

[Caution] Caution

Although cryptic to read, you should test Pin-Priority situation of your /etc/apt/preferences file using the output of "apt-cache policy <package>".

The Pin-Priority values of <package> from multiple sources are shown by the output of "apt-cache policy <package>":

  • a line started with "Package pin:" lists the package version of pin if association just with <package> is defined, e.g., "Package pin: 0.190",

  • no line with "Package pin:" exists if no association just with <package> is defined,

  • the Pin-Priority value associated just with <package> is listed right side of all version strings, e.g., "0.181 700",

  • 0 is listed right side of all version strings if no association just with <package> is defined, e.g., "0.181 0", and

  • the Pin-Priority values of archives (defined as "Package: *" in the /etc/apt/preferences file) are listed left side of all archive paths, e.g., "200 http://backports.org etch-backports/main Packages".

Here is an example of apt-pinning technique to include specific newer upstream version packages found in "unstable" regularly upgraded while tracking "testing". You list all required archives in the /etc/apt/sources.list file as:

deb http://ftp.us.debian.org/debian/ testing main contrib non-free
deb http://ftp.us.debian.org/debian/ unstable main contrib non-free
deb http://security.debian.org/ testing/updates main contrib

and set the /etc/apt/preferences file as:

Package: *
Pin: release a=testing
Pin-Priority: 500

Package: *
Pin: release a=unstable
Pin-Priority: 200

When you wish to install a package named "<package-name>" with its dependencies from "unstable" archive under this configuration, you issue the following command which switches target release with "-t" option (Pin-Priority of "unstable" becomes 990.):

$ sudo aptitude install -t unstable <package-name>

With this configuration, usual execution of "aptitude upgrade" and "aptitude dist-upgrade" will upgrade packages which were installed from "testing" archive using current "testing" archive and packages which were installed from "unstable" archive using current "unstable" archive.

[Caution] Caution

Be careful not to remove "testing" entry from the /etc/apt/sources.list file. Without "testing" entry in it, APT system will upgrade packages using newer "unstable" archive.

[Tip] Tip

I usually edit the /etc/apt/sources.list file to comment out "unstable" archive entry right after above operation. This avoids slow update process of having too many entries in the /etc/apt/sources.list file although this prevents upgrading packages which were installed from "unstable" archive using current "unstable" archive.

[Tip] Tip

If "Pin-Priority: 20" is used instead of "Pin-Priority: 200" for the /etc/apt/preferences file, already installed packages having Pin-Priority value of 100 will never be upgraded by "unstable" archive even if "testing" entry in the /etc/apt/sources.list file is removed.

If you wish to track particular packages in "unstable" automatically without initial "-t unstable" installation, you must create the /etc/apt/preferences file and explicitly lists all those packages at the top of it as:

Package: <package-1>
Pin: release a=unstable
Pin-Priority: 700

Package: <package-2>

Pin: release a=unstable
Pin-Priority: 700

...

These will set Pin-Priority value for each specific package. For example, in order to track the latest "unstable" version of this "Debian Reference" in English, you should have following entries in the /etc/apt/preferences file:

Package: debian-reference-en
Pin: release a=unstable
Pin-Priority: 700

Package: debian-reference-common
Pin: release a=unstable
Pin-Priority: 700
[Tip] Tip

This apt-pinning technique is valid even when you are tracking "stable" archive. Documentation packages have been always safe to install from "unstable" archive in my experience. (Reminder: This multi-archive tracking is not officially supported feature.)

Here is another example of apt-pinning technique to include specific newer upstream version packages found in "experimental" while tracking "unstable". You list all required archives in the /etc/apt/sources.list file as:

deb http://ftp.us.debian.org/debian/ unstable main contrib non-free
deb http://ftp.us.debian.org/debian/ experimental main contrib non-free
deb http://security.debian.org/ testing/updates main contrib

The default Pin-Priority value for "experimental" archive is always 1 since it is NotAutomatic archive (see Section 3.5.3, “Archive level Release files”). There is no need to set Pin-Priority value in the /etc/apt/preferences file to use experimental archive unless you wish to track particular packages automatically.

3.7.4. Volatile and Backports.org

There are The debian-volatile Project and http://backports.org archives which provide updgrade packages for "stable".

[Warning] Warning

Do not use all packages available in the NotAutomatic archives such as "lenny-backports" and "volatile-sloppy". Use only selected packages which fits your needs.

[Caution] Caution

http://backports.org is a non-Debian archive, although its packages are signed by Debian developers.

[Caution] Caution

Archive level Release files (see Section 3.5.3, “Archive level Release files”) are used for the rule of apt_preferences(5). Thus apt-pinning works only with "code" name for volatile Debian archives. This is different from other Debian archives. For example, you can do "Pin: release a=lenny" but can not do "Pin: release a=stable" in the /etc/apt/preferences file for volatile Debian archives.

Here is an example of apt-pinning technique to include specific newer upstream version packages found in "lenny-backports" while tracking "lenny" and "volatile". You list all required archives in the /etc/apt/sources.list file as:

deb http://ftp.us.debian.org/debian/ lenny main contrib non-free
deb http://security.debian.org/ lenny/updates main contrib
deb http://volatile.debian.org/debian-volatile/ lenny/volatile main contrib non-free
deb http://volatile.debian.org/debian-volatile/ lenny/volatile-sloppy main contrib non-free
deb http://backports.org/debian/ lenny-backports main contrib non-free

The default Pin-Priority value for http://backports.org archive and "volatile-sloppy" are always 1 since they are NotAutomatic archive (see Section 3.5.3, “Archive level Release files”). There is no need to set Pin-Priority value explicitly in the /etc/apt/preferences file just to use for http://backports.org and "volatile-sloppy" archive unless you wish to track packages automatically for next upgrading.

So whenever you wish to install a package named "<package-name>" with its dependency from "lenny-backports" archive, you use following command while switching target release with "-t" option:

$ sudo aptitude install -t lenny-backports <package-name>

If you wish to upgrade particular packages, you must create the /etc/apt/preferences file and explicitly lists all packages in it as:

Package: <package-1>
Pin: release o=Backports.org archive
Pin-Priority: 700

Package: <package-2>
Pin: release o=volatile.debian.org
Pin-Priority: 700

...

Alternatively, with the /etc/apt/preferences file as:

Package: *
Pin: release a=stable , o=Debian
Pin-Priority: 500

Package: *
Pin: release a=lenny, o=volatile.debian.org
Pin-Priority: 500

Package: *
Pin: release a=lenny-backports, o=Backports.org archive
Pin-Priority: 200

Package: *
Pin: release a=lenny-sloppy, o=volatile.debian.org
Pin-Priority: 200

execution of "aptitude upgrade" and "aptitude dist-upgrade" will upgrade packages which were installed from "stable" archive using current "stable" archive and packages which were installed from other archives using current corresponding archive for all archives in the /etc/apt/sources.list file.

3.7.5. Emergency downgrading

[Caution] Caution

Downgrading is not officially supported by the Debian by design. It should be done only as a part of emergency recovery process. Despite of this situation, it is known to work well in many incidents. For critical systems, You should backup all important data on the system after the recovery operation and re-install the new system from the scratch.

You may be lucky to downgrade from newer archive to older archive to recover from broken system upgrade by manipulating candidate version (see: Section 3.7.3, “Tweaking candidate version”). This is lazy alternative to tedious actions of many "dpkg -i <broken-package>_<old-version>.deb" commands (see Section 3.6.4, “Rescue using the dpkg command”).

For downgrading system tracking "unstable" to "testing", change the /etc/apt/sources.list file from:

deb http://ftp.us.debian.org/debian/ sid main contrib non-free

to:

deb http://ftp.us.debian.org/debian/ squeeze main contrib

and set the /etc/apt/preferences file as:

Package: *
Pin: release a=testing
Pin-Priority: 1010

Then run "aptitude dist-upgrade" to force downgrading of packages across the system. You should remove this special /etc/apt/preferences file after the downgrading.

3.7.6. Who uploaded the package?

Although the maintainer name listed in /var/lib/dpkg/available and /usr/share/doc/package_name/changelog provide some information on "who is behind the packaging activity", the actual uploader of the package is somewhat obscure. The who-uploads(1) in devscripts package identifies the actual uploader of Debian source packages.

3.7.7. The equivs package

If you are to compile a program from source to replace the Debian package, it is best to make it into a real local debianized package (*.deb) and use private archive.

If you chose to compile a program from source and to install them under /usr/local instead, you may need to use equivs as a last resort to satisfy the missing package dependency.

Package: equivs
Priority: extra
Section: admin
Description: Circumventing Debian package dependencies
 This is a dummy package which can be used to create Debian
 packages, which only contain dependency information.

3.7.8. Port a package to the stable system

For partial upgrades of the "stable" system, rebuilding a package within its environment using the source package is desirable. This avoids massive package upgrades due to their dependencies. First, add the following entries to the /etc/apt/sources.list of a "stable" system:

deb-src http://http.us.debian.org/debian unstable  main contrib non-free

Then get the required packages and the source to be downloaded and compiled by:

# aptitude update
# aptitude dist-upgrade
# aptitude install fakeroot devscripts build-essential
$ apt-get build-dep foo
$ apt-get source foo
$ cd foo*
  • adjust package if needed.

$ dch -i
  • bump package version, e.g. one appended with "+bp1".

$ debuild
$ cd ..
# debi foo*.changes 

3.7.9. Proxy server for APT

Since mirroring whole subsection of Debian archive wastes disk space and network bandwidth, deployment of a local proxy server for APT is desirable consideration when you administer many systems on LAN. APT can be configure to use web (http) proxy server such as squid (see Section 7.5, “Other network application servers”) as described in apt.conf(5) and in /usr/share/doc/apt/examples/configure-index.gz. The http_proxy environment variable can be used to override proxy server setting in the /etc/apt/apt.conf file.

There are proxy tools specially for Debian archive. You should check BTS before using them.

Table 3.20. List of the proxy tools specially for Debian archive

package

popcon

size

description

URL links

approx

V:0.2, I:0.2

3632

caching proxy server for Debian archive files (compiled OCaml program)

BTS for approx

apt-proxy

V:0.5, I:0.6

428

Debian archive proxy and partial mirror builder (Python program)

BTS for apt-proxy

apt-cacher

V:0.3, I:0.6

312

Caching proxy for Debian package and source files (Perl program)

BTS for apt-cacher

apt-cacher-ng

V:0.09, I:0.13

680

aching proxy for distribution of software packages (compiled C++ program)

BTS for apt-cacher-ng

debtorrent

V:0.15, I:0.2

1173

bittorrent proxy for downloading Debian packages (Python program)

BTS for debtorrent


3.7.10. Small public package archive

Here is an example for creating a small public package archive compatible with the modern secure APT system (see Section 3.5.2, “Top level Release file and authenticity”). Let's assume few things:

  • Account name: foo

  • Host name: www.example.com

  • Required packages: apt-utils, gnupg, and other packages.

  • URL: http://www.example.com/~foo/ displays /home/foo/public_html/index.html

  • Architecture of packages: amd64

One time setup of APT archive on your server system:

  • Create Foo's archive key on server system:

$ ssh foo@www.example.com
$ gpg --gen-key
...
$ gpg -K
...
sec   1024D/3A3CB5A6 2008-08-14
uid                  Foo Bar (ARCHIVE KEY) <foo@www.example.com>
ssb   2048g/6856F4A7 2008-08-14
$ gpg --export -a 3A3CB5A6 >foo.public.key
  • New Foo's archive key is 3A3CB5A6

  • Publish foo.public.key file.

  • Create Foo's archive skeleton:

$ umask 022
$ mkdir -p ~/public_html/debian/pool/main
$ mkdir -p ~/public_html/debian/dists/unstable/main/binary-amd64
$ mkdir -p ~/public_html/debian/dists/unstable/main/source
$ cd ~/public_html/debian
$ cat > dists/unstable/main/binary-amd64/Release << EOF
Archive: unstable
Version: 4.0
Component: main
Origin: Foo
Label: Foo
Architecture: amd64
EOF
$ cat > dists/unstable/main/source/Release << EOF
Archive: unstable
Version: 4.0
Component: main
Origin: Foo
Label: Foo
Architecture: source
EOF
$ cat >aptftp.conf <<EOF
APT::FTPArchive::Release {
  Origin "Foo";
  Label "Foo";
  Suite "unstable";
  Codename "sid";
  Architectures "amd64";
  Components "main";
  Description "Foo's public archive";
};
EOF
$ cat >aptgenerate.conf <<EOF
Dir::ArchiveDir ".";
Dir::CacheDir ".";
TreeDefault::Directory "pool/";
TreeDefault::SrcDirectory "pool/";
Default::Packages::Extensions ".deb";
Default::Packages::Compress ". gzip bzip2";
Default::Sources::Compress "gzip bzip2";
Default::Contents::Compress "gzip bzip2";

BinDirectory "dists/unstable/main/binary-amd64" {
  Packages "dists/unstable/main/binary-amd64/Packages";
  Contents "dists/unstable/Contents-amd64";
  SrcPackages "dists/unstable/main/source/Sources";
};

Tree "dists/unstable" {
  Sections "main";
  Architectures "amd64 source";
};
EOF

Repetitive update of APT archive contents on your server system:

  • Place all package files into ~foo/public_html/debian/pool/main/ by executing "dupload -t foo changes_file" in client while having ~/.dupload.conf containing:

$cfg{'foo'} = {
  fqdn => "www.example.com",
  method => "scpb",
  incoming => "/home/foo/public_html/debian/pool/main",
  # The dinstall on ftp-master sends emails itself
  dinstall_runs => 1,
};

$cfg{'foo'}{postupload}{'changes'} = "
  echo 'cd public_html/debian ;
  apt-ftparchive generate -c=aptftp.conf aptgenerate.conf;
  apt-ftparchive release -c=aptftp.conf dists/unstable >dists/unstable/Release ;
  rm -f dists/unstable/Release.gpg ;
  gpg -u 3A3CB5A6 -bao dists/unstable/Release.gpg dists/unstable/Release'| 
  ssh foo@www.example.com  2>/dev/null ;
  echo 'Package archive created!'";

The postupload hook script initiated by dupload(1) creates updated archive files for each upload.

You can add this small public archive to the apt-line of your client system:

$ sudo bash
# echo "deb http://www.example.com/~foo/debian/ unstable main" \
   >> /etc/apt/sources.list
# apt-key add foo.public.key
[Tip] Tip

If the archive is located on the local file system, you can use "deb file:///home/foo/debian/ ..." instead.

3.7.11. Record/copy system configuration

To make a local copy of the package and debconf selection states:

# dpkg --get-selections '*' > selection.dpkg
# debconf-get-selections    > selection.debconf

Here, '*' makes selection.dpkg to include package entries for "purge" too.

You can transfer these 2 files to another computer, and install there with:

# dselect update
# debconf-set-selections < myselection.debconf
# dpkg --set-selections  < myselection.dpkg
# apt-get -u dselect-upgrade    # or dselect install

If you are thinking about managing many cluster of servers with practically the same configuration, you should consider to use specialized package such as fai to manage the whole system.

3.7.12. Convert or install an alien binary package

The alien command enables the conversion of binary packages provided in Red Hat rpm, Stampede slp, Slackware tgz, and Solaris pkg file formats into a Debian deb package. If you want to use a package from another Linux distribution than the one you have installed on your system, you can use alien to convert it to your preferred package format and install it. alien also supports LSB packages.

The original package needs to be statically linked or its library dependency needs to be satisfied manually. So use this command with great care.

3.7.13. Extract package without dpkg

The current .deb package contents can be extracted without using the dpkg command on any unix-like environment using standard ar and tar commands.

# ar x /path/to/dpkg_<version>_<arch>.deb
# ls
total 24
-rw-r--r-- 1 osamu osamu  1320 2007-05-07 00:11 control.tar.gz
-rw-r--r-- 1 osamu osamu 12837 2007-05-07 00:11 data.tar.gz
-rw-r--r-- 1 osamu osamu     4 2007-05-07 00:11 debian-binary
# mkdir control
# mkdir data
# tar xvzf control.tar.gz -C control
# tar xvzf data.tar.gz -C data

You can also browse package content using the mc command.

3.7.14. More readings for the package management

You should read:

  • manpages for aptitude(8), "dpkg", "man 8 tasksel", "man 8 apt-get", "man 8 apt-config", "man 8 apt-key", "man 5 sources.list", "man 5 apt.conf", and "man 5 apt_preferences";

  • "/usr/share/doc/apt-doc/guide.html/index.html" and "/usr/share/doc/apt-doc/offline.html/index.html" from the apt-doc package;

  • Read /usr/share/doc/aptitude/html/en/index.html from the aptitude-doc-en package.

The official and detailed secondary information on the Debian archive are given by:

The tutorial for building of a Debian package for the common Debian user is given by:

Chapter 4. The system initialization

It is wise for you as the system administrator to know roughly how the Debian system is started and configured. Although the exact details are in the source files of the packages installed and their documentations, it is a bit overwhelming for most of us.

I did my best to provide a quick overview of the key points of the Debian system and their configuration for your reference, based on the current and previous knowledge of mine and others. Since the Debian system is a moving target, the situation over the system may have been changed. Before making any changes to the system, you should refer to the latest documentation for each package.

4.1. An overview of the boot strap process

The computer system undergoes several phases of boot strap processes from the power-on event until it offers the fully functional operating system (OS) to the user.

For simplicity, I will limit discussion to the typical PC platform with the default installation.

The typical boot strap process is like a four-stage rocket. Each stage rocket hands over the system control to the next stage one. Here each stage corresponds to:

  • Stage 1: the BIOS

  • Stage 2: the boot loader

  • Stage 3, the mini-Debian system

  • Stage 4: the normal Debian system

Of course, these can be configured differently. For example, if you compiled your own kernel, you may be skipping the step with the mini-Debian system. So please do not assume this is the case for your system until you check it yourself.

[Note] Note

For non-legacy PC platform such as the SUN or the Macintosh system, the BIOS on ROM and the partition on the disk may be quite different (Section 10.3.1, “Partition configuration”). Please seek the platform specific documentations elsewhere for such a case.

4.2. Stage 1: the BIOS

The BIOS is the 1st stage of the boot process which is started by the power-on event. The BIOS resides on the read only memory (ROM) from the particular memory address to which the program counter of CPU is initialized by the power-on event.

This BIOS performs the basic initialization of the hardware (POST: power on self test) and hands the system control to the next step which you provide. The BIOS is usually provided with the hardware.

The BIOS startup screen usually indicates what key(s) to press to enter the BIOS setup screen to configure the BIOS behavior. Popular keys used are F1, F2, F10, Esc, Ins, and Del. If your BIOS startup screen is hidden by a nice graphics screen, you may press some keys such as Esc to disable this. These keys are highly dependent on the hardware.

The hardware location and the priority of the code started by the BIOS can be selected from the BIOS setup screen. Typically, the first few sectors of the first found selected device (hard disk, floppy disk, CD-ROM, ...) are loaded to the memory and this initial code is executed. This initial code can be:

  • the boot loader code,

  • the kernel code of the stepping stone OS such as FreeDOS, or

  • the kernel code of the target OS if it fits in this small space.

Typically, the system is booted from the specified partition of the primary hard disk partition. The first sector of the hard disk contain the master boot record (MBR). The disk partition information including the boot selection is recorded at the end of this MBR. The first boot loader code executed from the BIOS for the hard disk occupies the rest of this MBR.

4.3. Stage 2: the boot loader

The boot loader is the 2nd stage of the boot process which is started by the BIOS. It loads the system kernel image and the initrd image to the memory and hands control over to them. This initrd image is the root filesystem image and its support depends on the bootloader used.

The Debian system normally uses the Linux kernel as the default system kernel. The initrd image for the current 2.6 Linux kernel is technically the initramfs (initial RAM filesystem) image. The initramfs image is a gzipped cpio archive of files in the root filesystem.

The default install of the Debian system places first-stage GRUB boot loader code into the MBR for the PC platform. There are many boot loaders and configuration options available.

Table 4.1. List of boot loaders.

bootloader

package

popcon

size

initrd

description

GRUB Legacy

grub

V:18, I:91

1884

Supported

This is smart enough to understand disk partitions and file systems such as vfat, ext3, .... (etch default)

GRUB 2

grub-pc

V:0.9, I:1.9

3952

Supported

This is smart enough to understand disk partitions and file systems such as vfat, ext3, .... (new for lenny)

GRUB 2

grub-rescue-pc

V:0.05, I:0.4

2476

Supported

This is GRUB 2 bootable rescue images (CD and floppy) (PC/BIOS version)

Lilo

lilo

V:1.0, I:4

1192

Supported

This relies on the sector locations of data on the hard disk. (Old)

Isolinux

syslinux

V:1.4, I:7

144

Supported

This understands the ISO9660 file system. This is used by the boot CD.

Syslinux

syslinux

V:1.4, I:7

144

Supported

This understands the MSDOS file system (FAT). This is used by the boot floppy.

Loadlin

loadlin

V:0.02, I:0.12

140

Supported

New system is started from the FreeDOS/MSDOS system.

Neil Turton's MBR

mbr

V:1.3, I:7

96

Not supported

This is free software which substitutes MSDOS MBR. This only understands disk partitions.


For GRUB Legacy, the menu configuration file is located at /boot/grub/menu.lst . For example, it has entries like:

title           Debian GNU/Linux
root            (hd0,2)
kernel          /vmlinuz root=/dev/hda3 ro
initrd          /initrd.img

For GRUB 2, the menu configuration file is located at /boot/grub/grub.cfg. It is automatically generated by /usr/sbin/update-grub using templates from /etc/grub.d/* and settings from /etc/default/grub. For example, it has entries like:

menuentry "Debian GNU/Linux" {
        set root=(hd0,3)
        linux /vmlinuz root=/dev/hda3
        initrd /initrd.img
}

For these examples, these GRUB parameters mean:

Table 4.2. The meaning of GRUB parameters.

GRUB parameter

meaning

root

Use 3rd partition on boot disk by setting it as (hd0,2) in GRUB legacy or as (hd0,3) in GRUB 2.

kernel

Use kernel located at /vmlinuz with kernel parameter: "root=/dev/hda3 ro".

initrd

Use initrd/initramfs image located at "/initrd.img".


[Note] Note

The value of the partition number used by GRUB legacy program is one less than normal one used by Linux kernel and utility tools. GRUB 2 program fixes this problem.

[Tip] Tip

UUID (see Section 10.3.2, “Accessing partition using UUID”) may be used to identify a block special device instead of its file name such as "/dev/hda3", e.g."root=UUID=81b289d5-4341-4003-9602-e254a17ac232 ro".

[Tip] Tip

You can start a boot loader from another boot loader using techniques called chain loading.

[Note] Note

Having bootable rescue media (CD or floppy) created from images in the grub-rescue-pc package let you boot your system without functioning bootloader on the harddisk.

See "info grub" and grub-install(8).

4.4. Stage 3: the mini-Debian system

The mini-Debian system is the 3rd stage of the boot process which is started by the boot loader. It runs the system kernel with its root filesystem on the memory. It is an optional preparatory stage of the boot process.

[Note] Note

The term "the mini-Debian system" is coined by the author to describe this 3rd stage boot process for this document. This system is commonly referred as the initrd or initramfs system. Similar system on the memory is used by the Debian Installer.

The /init script is executed as the first program in this root filesystem on the memory. It is a shell script program which initializes the kernel in user space and hands control over to the next stage. This mini-Debian system offers flexibility to the boot process such as adding kernel modules before the main boot process or mounting the root file system as an encrypted one.

You can interrupt this part of the boot process to gain root shell by providing "break=init" etc. to the kernel boot parameter. See the /init script for more break conditions. This shell environment is sophisticated enough to make a good inspection of your machine's hardware.

Commands available in this mini-Debian system are stripped down ones and mainly provided by a GNU tool called busybox.

[Caution] Caution

You need to use "-n" option for mount command when you are on the readonly root file system.

4.5. Stage 4: the normal Debian system

The normal Debian system is the 4th stage of the boot process which is started by the mini-Debian system. The system kernel for the mini-Debian system continues to run in this environment. The root filesystem is switched from the one on the memory to the one on the real harddisk filesystem.

The /sbin/init program is executed as the first program and performs the main boot process. The Debian normally uses the traditional sysvinit scheme with the sysv-rc package. See man 8 init, man 5 inittab, and /usr/share/doc/sysv-rc/README.runlevels.gz for the exact explanation. Following is a simplified overview of this main boot process:

  1. The Debian system goes into runlevel N (none) to initialize the system by following the /etc/inittab description.

  2. The Debian system goes into runlevel S to initialize the system under the single-user mode to complete hardware initialization etc.

  3. The Debian system switches itself to one of the specified multi-user runlevels (2 to 5) to start the system services.

The initial runlevel used for multi-user mode is specified with the "init=" kernel boot parameter or in the "initdefault" line of this /etc/inittab. The Debian system as installed starts at the runlevel 2.

All scripts executed by the init system are located in the directory /etc/init.d/.

[Tip] Tip

For alternative boot mechanism to the sysv-rc package using a single configuration file, see the file-rc package. Both mechanisms are compatible through /etc/init.d/rc, /etc/init.d/rcS, /usr/sbin/update-rc.d, and /usr/sbin/invoke-rc.d scripts.

4.5.1. The meaning of the runlevel

Each runlevel uses a directory for its configuration and has specific meaning:

Table 4.3. List of runlevels and meanings.

runlevel

directory

meaning