LPI 101 Linux Training Course Notes: Leading Edge Business Solutions
LPI 101 Linux Training Course Notes: Leading Edge Business Solutions
LPI 101 Linux Training Course Notes: Leading Edge Business Solutions
LPIC topics
LPIC topic 1.101.1 Configure Fundamental BIOS Settings [1]....................................................19
LPIC topic 1.101.3 Configure modem and sound cards [1]..........................................................28
LPIC topic 1.101.4 Setup SCSI Devices [1].................................................................................33
LPIC topic 1.101.5 Setup different PC expansion cards [3].........................................................38
LPIC topic 1.101.6 Configure Communication Devices [1].........................................................46
LPIC topic 1.101.7 USB hardware [1]..........................................................................................50
LPIC topic 1.102.1 Design hard disk layout [5]............................................................................54
LPIC topic 1.102.2 Install a boot manager [1]..............................................................................59
LPIC topic 1.102.3 Make and install programs from source [5]...................................................65
LPIC topic 1.102.4 Manage shared libraries [3]...........................................................................70
LPIC topic 1.102.5 Use Debian package management [8]............................................................73
LPIC topic 1.102.6 Use Red Hat Package Manager (RPM) [8]....................................................76
LPIC topic 1.103.1 Work on the command line [5]......................................................................81
LPIC topic 1.103.2 Process text streams using filters [6].............................................................89
LPIC topic 1.103.3 Perform basic file management [3]..............................................................102
LPIC topic 1.103.4 Use streams, pipes, and redirects [5]...........................................................110
LPIC topic 1.103.5 Create, monitor, and kill processes [5]........................................................118
LPIC topic 1.103.6 Modify process execution priorities [3].......................................................126
LPIC topic 1.103.7 Regular expressions [3]...............................................................................130
LPIC topic 1.103.8 Perform basic file editing operations using vi [1]........................................135
LPIC topic 1.104.1 Create partitions and filesystems [3]...........................................................139
LPIC topic 1.104.2 Maintain the integrity of filesystems [3]......................................................143
LPIC topic 1.104.3 Control mounting and unmounting filesystems [3].....................................150
LPIC topic 1.104.4 Managing disk quota [3]..............................................................................154
LPIC topic 1.104.5 Use file permissions to control access to files [5].......................................159
LPIC topic 1.104.6 Manage file ownership [1]...........................................................................166
LPIC topic 1.104.7 Create and change hard and symbolic links [1]...........................................169
LPIC topic 1.104.8 Find system files and place files in the correct location [5]........................173
LPIC topic 1.110.1 Install & Configure XFree86 [5]..................................................................179
LPIC topic 1.110.2 Setup a display manager [3].........................................................................188
LPIC topic 1.110.4 Install & Customize a Window Manager Environment [5].........................193
Table of Contents
1 Foreword..........................................................................................................................................10
2 Legal notices....................................................................................................................................14
2.1 About these notes.....................................................................................................................14
2.2 Copyright notice .....................................................................................................................14
2.3 GNU Free Documentation License..........................................................................................14
3 BIOS Settings..................................................................................................................................19
LPIC topic 1.101.1 Configure Fundamental BIOS Settings [1]
3.1 BIOS architecture....................................................................................................................19
3.2 Changing BIOS configuration.................................................................................................20
12.5 Review...................................................................................................................................71
13 Debian package management........................................................................................................73
LPIC topic 1.102.5 Use Debian package management [8]
13.1 Debian and .deb.....................................................................................................................73
13.2 apt...........................................................................................................................................74
13.3 Review...................................................................................................................................74
14 RPM Redhat package manager..................................................................................................76
LPIC topic 1.102.6 Use Red Hat Package Manager (RPM) [8]
14.1 Purpose of RPM.....................................................................................................................76
14.2 RPM database........................................................................................................................77
14.3 RPM functions.......................................................................................................................77
14.3.1 Querying the database and package files
14.3.2 Install and upgrading packages
14.3.3 Erase a package
14.4 RPM integrity checking.........................................................................................................79
14.5 Review...................................................................................................................................79
15 Work on the command line...........................................................................................................81
LPIC topic 1.103.1 Work on the command line [5]
15.1 Command line overview........................................................................................................81
15.2 Command line structure.........................................................................................................82
15.3 Environment variables...........................................................................................................83
15.4 Editing commands and command history..............................................................................85
15.5 $PATH...................................................................................................................................85
15.6 Command substitution $(...) and `...`.....................................................................................85
15.7 Recursive commands.............................................................................................................85
15.7.1 -r switch
15.7.2 cp -r
15.7.3 chown and chmod
15.7.4 grep -r
15.7.5 find for recursive commands
15.8 Bash session...........................................................................................................................87
15.9 Man pages..............................................................................................................................87
15.10 Review.................................................................................................................................87
16 Text filters.....................................................................................................................................89
LPIC topic 1.103.2 Process text streams using filters [6]
16.1 Introduction............................................................................................................................89
16.2 Input and output redirection...................................................................................................90
16.3 Selecting parts of a file..........................................................................................................91
16.4 Sorting....................................................................................................................................94
16.5 Manipulation..........................................................................................................................95
16.6 Formatting..............................................................................................................................98
16.7 Review.................................................................................................................................100
17 File management.........................................................................................................................102
LPIC topic 1.103.3 Perform basic file management [3]
17.1 Files, directories and ls........................................................................................................102
17.2 File globbing (wildcards).....................................................................................................103
17.3 Directories and files.............................................................................................................104
23.3 Review.................................................................................................................................141
24 fsck..............................................................................................................................................143
LPIC topic 1.104.2 Maintain the integrity of filesystems [3]
24.1 Disk space............................................................................................................................143
24.1.1 df disk free
24.1.2 du
24.2 Detecting and correcting errors............................................................................................145
24.2.1 fsck
24.2.2 ext2
24.2.3 reiserfs
24.3 Review.................................................................................................................................149
25 Mounting.....................................................................................................................................150
LPIC topic 1.104.3 Control mounting and unmounting filesystems [3]
25.1 mount...................................................................................................................................150
25.2 fstab......................................................................................................................................150
25.3 Options for mount................................................................................................................151
25.4 Removable media................................................................................................................152
25.5 Review.................................................................................................................................152
26 Quotas..........................................................................................................................................154
LPIC topic 1.104.4 Managing disk quota [3]
26.1 Overview..............................................................................................................................154
26.2 Enabling Quotas...................................................................................................................154
26.3 Setting quotas.......................................................................................................................155
26.4 Reporting with repquota .....................................................................................................157
26.5 Review.................................................................................................................................157
27 Permissions..................................................................................................................................159
LPIC topic 1.104.5 Use file permissions to control access to files [5]
27.1 Ownership and permissions.................................................................................................159
27.2 chmod...................................................................................................................................160
27.3 File types..............................................................................................................................162
27.4 umask...................................................................................................................................163
27.5 Ext2 attributes......................................................................................................................164
27.6 Review.................................................................................................................................165
28 File ownership.............................................................................................................................166
LPIC topic 1.104.6 Manage file ownership [1]
28.1 File ownership......................................................................................................................166
28.2 Default group.......................................................................................................................167
28.3 Review.................................................................................................................................168
29 Links............................................................................................................................................169
LPIC topic 1.104.7 Create and change hard and symbolic links [1]
29.1 Hard links.............................................................................................................................169
29.2 Symbolic links ....................................................................................................................170
29.3 Review.................................................................................................................................171
30 Finding files.................................................................................................................................173
LPIC topic 1.104.8 Find system files and place files in the correct location [5]
30.1 Filesystem hierarchy standard.............................................................................................173
30.2 find ......................................................................................................................................175
30.3 locate....................................................................................................................................175
30.4 slocate..................................................................................................................................176
30.5 Finding files with whereis....................................................................................................176
30.6 Finding programs with which..............................................................................................177
30.7 Review.................................................................................................................................177
31 XFree86.......................................................................................................................................179
LPIC topic 1.110.1 Install & Configure XFree86 [5]
31.1 X11 architecture...................................................................................................................179
31.2 X server................................................................................................................................180
31.3 Configuration file.................................................................................................................182
31.3.1 Version 3.x
31.3.2 Version 4.x
31.4 Video card and monitor tuning............................................................................................185
31.5 Installing fonts.....................................................................................................................185
31.6 X font server........................................................................................................................186
31.7 Review.................................................................................................................................187
32 X display manager ......................................................................................................................188
LPIC topic 1.110.2 Setup a display manager [3]
32.1 What is a display manager...................................................................................................188
32.2 Runlevels and display managers..........................................................................................189
32.3 Configuring XDM................................................................................................................189
32.4 Configuring KDM................................................................................................................190
32.5 Configuring GDM................................................................................................................190
32.6 Connecting to a remote display manager.............................................................................191
32.7 Review.................................................................................................................................192
33 GUI environment.........................................................................................................................193
LPIC topic 1.110.4 Install & Customize a Window Manager Environment [5]
33.1 Window managers...............................................................................................................193
33.2 .xinitrc and the system-wide window manager...................................................................194
33.3 X applications......................................................................................................................194
33.4 X terminal emulators...........................................................................................................195
33.5 X application library dependencies......................................................................................195
33.6 Remote applications.............................................................................................................197
33.6.1 DISPLAY environment variable
33.6.2 Host based access control
33.6.3 MIT cookie access control
33.7 Review.................................................................................................................................198
34 Glossary.......................................................................................................................................200
35 Index............................................................................................................................................207
1 Foreword
10
1 Foreword
A is for awk, which runs like a snail, and
B is for biff, which reads all your mail.
C is for cc, as hackers recall, while
D is for dd, the command that does all.
E is for emacs, which rebinds your keys, and
F is for fsck, which rebuilds your trees.
G is for grep, a clever detective, while
H is for halt, which may seem defective.
I is for indent, which rarely amuses, and
J is for join, which nobody uses.
K is for kill, which makes you the boss, while
L is for lex, which is missing from DOS.
M is for more, from which less was begot, and
N is for nice, which it really is not.
O is for od, which prints out things nice, while
P is for passwd, which reads in strings twice.
Q is for quota, a Berkeley-type fable, and
R is for ranlib, for sorting ar table.
S is for spell, which attempts to belittle, while
T is for true, which does very little.
U is for uniq, which is used after sort, and
V is for vi, which is hard to abort.
W is for whoami, which tells you your name, while
X is, well, X, of dubious fame.
Y is for yes, which makes an impression, and
Z is for zcat, which handles compression.
THE ABC'S OF UNIX
/usr/share/games/fortune/songs-poems
This course material is based on the objectives for the Linux Professionals Institutes LPI 101
examination (specifically, release 2 of the objectives). The course is intended to provide you with
the basic skills required for operating and administering Linux systems.
At every good training course the student should come away with some paper in his hand, to file in
the company filing cabinet. A really excellent course will include some knowledge and practical
ability in the student's head as well. We hope to achieve at least the first with these notes. The
second is up to the instructor.
Goal of this course
This course aims to equip you with the knowledge to be able to pass the LPI 101 examination
(release 2). We hope that in the course of doing this course you will acquire the skills that go with
an understanding of how Linux works.
Target audience
1 Foreword
11
People who wish to write the LPIC 101 exam, as part of the LPIC Level 1 certification.
Technically inclined people who wish to become familiar with Linux, particularly with a view to
administering a Linux system.
People wishing to take this course will probably fit the following profile
You should have basic skills for using Linux, including command line usage. A minimum of 3
months of experience is recommended. Completion of an introductory Linux course is an
alternative to this experience.
You are a system administrator or hold a similar technical position (or you would like a job like
that).
You are interested in technical things and the fascinating little details that make your computer
behave strangely.
You want to know how things work specifically how Linux works, and are willing to spend
some time finding out.
You have practical administrative experience with other computer systems.
You already have some practical familiarity with using Linux. You have probably installed
Linux and have used it without gaining a complete understanding of many functions.
We recommend that this course be followed by professional people who have completed their
secondary education, and possibly an additional qualification. It is preferable that you already hold
a position in which you can use Linux on a day to day basis.
Flow of instruction
Each section in the notes is structured as an independent entity. Each section covers a single LPIC
topic. Each section is structured as follows:
LPIC objectives
Introductory material
Detailed material
Review material (quiz questions and assignments).
Some of the sections are more demanding than others, and the certification does not weight all of
the sections equally.
There are a number of ways to approach this material:
Over a number of weeks, as a self-study course with some classroom time. This approach is
recommended for novices.
Over two weeks, covering all the sections in some detail.
Over a single week, as an instructor-led course. This approach crams a lot of material into a
small time, and is recommended for experienced Linux users as a pre-exam cram.
The following order of study is recommended, especially if you have not previously studied the
material (i.e. for first-time users). This is not the order in which the material appears in the
manuals.
1. Topic 103 GNU & Unix Commands
2. Topic 101 Hardware & Architecture
3. Topic 102 Linux Installation & Package Management
4. Topic 104 Devices, Linux Filesystems, Filesystem Hierarchy Standard
1 Foreword
12
5. Topic 110 X
If you do the material over two weeks, the material can be used in the following arrangement:
101 Part A Tools and Utilities
What you need for this course part time over 8 weeks
What you need for this course instructor led over 1 week
Typographic conventions
Command names and example of command are printed in boldface. So for example, ls -la is used
for printing a list of files in the current directory, and pwd prints the current working directory.
1 And when we say his, we mean her if the student happens to be female.
The student is encouraged to try these example commands on his1 computer, as the results may
differ from one system to the next. Often the output shown is incomplete, and a valuable learning
experience awaits the person bold enough to type the bold text.
v
X
v
%X
$ &
EdX
v $ t &
8 % 3
S e ( v $ t w v $ t
yxH ` u
$
%v @%C 3 $
% 3 $"X
$
` G @3 "X
$
@3 "X X X
X
U i f R e ( v $ t w v $ t
@4ByxH ` u
f R e ( v $ t w v $ t
ByxH ` u
a C
b%h$ s
r DphH2db `
V U q i g f e ca
X W V T S R Q I # # C G $ C 3 A
YU 8%P5 3 BH8@FED$ B0
# 6 $ # # $ 3 1
8@987%544 20
In this particular case, it means that you can tell ls to list a particular directory.
Interactive command sessions are shown in a block
( & $ # !
)%'%"
Syntax explanations are shown like this.
1 Foreword
13
2 Legal notices
14
2 Legal notices
2.1 About these notes
Gentle reader, we hope that these notes provide a wonderful learning experience for you. We trust
that you will be kind enough to point out to us the typos, stylistic faults and gross errors in the text.
Also, if you make changes to these notes, or produce them in an alternative format, please do send
us a copy of your revisions.
These notes have been written with the LPI's objectives and criteria for approved training materials
in mind. The modular design makes it possible to build up a course following LPI objectives from
a selection of topics. If you would like to contribute towards the certification of these notes and the
release of other free documentation please contact us.
Bound copies of our LPI 101 and LPI 102 training manuals can be purchased from Leading Edge
Business Solutions (Pty) Ltd see www.ledge.co.za. We offer training courses based on this
material. The contact address for queries related to these notes is lpinotes@ledge.co.za.
2 Legal notices
15
2 Legal notices
16
2. VERBATIM COPYING
You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this
License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all
copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to
obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept
compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the
conditions in section 3.
You may also lend copies, under the same conditions stated above, and you may publicly display copies.
3. COPYING IN QUANTITY
If you publish printed copies (or copies in media that commonly have printed covers) of the Document, numbering more
than 100, and the Document's license notice requires Cover Texts, you must enclose the copies in covers that carry,
clearly and legibly, all these Cover Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on the back
cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must
present the full title with all words of the title equally prominent and visible. You may add other material on the covers
in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy
these conditions, can be treated as verbatim copying in other respects.
If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as many as fit
reasonably) on the actual cover, and continue the rest onto adjacent pages.
If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include a
machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a computernetwork location from which the general network-using public has access to download using public-standard network
protocols a complete Transparent copy of the Document, free of added material. If you use the latter option, you must
take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent
copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque
copy (directly or through your agents or retailers) of that edition to the public.
It is requested, but not required, that you contact the authors of the Document well before redistributing any large
number of copies, to give them a chance to provide you with an updated version of the Document.
4. MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3 above,
provided that you release the Modified Version under precisely this License, with the Modified Version filling the role
of the Document, thus licensing distribution and modification of the Modified Version to whoever possesses a copy of
it. In addition, you must do these things in the Modified Version:
A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous
versions (which should, if there were any, be listed in the History section of the Document). You may use the same title
as a previous version if the original publisher of that version gives permission.
B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modifications in
the Modified Version, together with at least five of the principal authors of the Document (all of its principal authors, if
it has fewer than five), unless they release you from this requirement.
C. State on the Title page the name of the publisher of the Modified Version, as the publisher.
D. Preserve all the copyright notices of the Document.
E. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices.
F. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modified
Version under the terms of this License, in the form shown in the Addendum below.
G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Document's
license notice.
H. Include an unaltered copy of this License.
I. Preserve the section Entitled "History", Preserve its Title, and add to it an item stating at least the title, year, new
authors, and publisher of the Modified Version as given on the Title Page. If there is no section Entitled "History" in the
2 Legal notices
17
Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add
an item describing the Modified Version as stated in the previous sentence.
J. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document,
and likewise the network locations given in the Document for previous versions it was based on. These may be placed in
the "History" section. You may omit a network location for a work that was published at least four years before the
Document itself, or if the original publisher of the version it refers to gives permission.
K. For any section Entitled "Acknowledgements" or "Dedications", Preserve the Title of the section, and preserve in the
section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein.
L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the
equivalent are not considered part of the section titles.
M. Delete any section Entitled "Endorsements". Such a section may not be included in the Modified Version.
N. Do not retitle any existing section to be Entitled "Endorsements" or to conflict in title with any Invariant Section.
O. Preserve any Warranty Disclaimers.
If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain
no material copied from the Document, you may at your option designate some or all of these sections as invariant. To
do this, add their titles to the list of Invariant Sections in the Modified Version's license notice. These titles must be
distinct from any other section titles.
You may add a section Entitled "Endorsements", provided it contains nothing but endorsements of your Modified
Version by various parties--for example, statements of peer review or that the text has been approved by an organization
as the authoritative definition of a standard.
You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover
Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover Text and one of
Back-Cover Text may be added by (or through arrangements made by) any one entity. If the Document already includes
a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on
behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher
that added the old one.
The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity
for or to assert or imply endorsement of any Modified Version.
5. COMBINING DOCUMENTS
You may combine the Document with other documents released under this License, under the terms defined in section 4
above for modified versions, provided that you include in the combination all of the Invariant Sections of all of the
original documents, unmodified, and list them all as Invariant Sections of your combined work in its license notice, and
that you preserve all their Warranty Disclaimers.
The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be
replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the
title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of
that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant
Sections in the license notice of the combined work.
In the combination, you must combine any sections Entitled "History" in the various original documents, forming one
section Entitled "History"; likewise combine any sections Entitled "Acknowledgements", and any sections Entitled
"Dedications". You must delete all sections Entitled "Endorsements."
6. COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other documents released under this License, and replace
the individual copies of this License in the various documents with a single copy that is included in the collection,
provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects.
You may extract a single document from such a collection, and distribute it individually under this License, provided
you insert a copy of this License into the extracted document, and follow this License in all other respects regarding
2 Legal notices
18
3 BIOS Settings
19
3 BIOS Settings
> valerie kernel: mtrr: your CPUs had inconsistent variable MTRR settings
> valerie kernel: mtrr: probably your BIOS does not setup all CPUs
It indicates your bios authors can't read standards. Thats a quite normal state of
affairs, so common that the kernel cleans up after them
Alan Cox on linux-kernel
This chapter is on the configuration of the BIOS. Although this chapter is mainly information, you
will need to know how to start up your Linux system and execute commands and read files using
less in order to make progress.
3 BIOS Settings
20
The BIOS function to read a sector of data from disk is accessed via a software interrupt, interrupt
13h (hexadecimal). To read from the disk using interrupt 13h, you specify the exact head, cylinder
and sector numbers. The limits on these are shown in the table.
Parameter
Cylinder
Bits
Maximum
10
1024
2 Well, when we wrote this, a 80Gb disk was large. It might not be large anymore.
3 BIOS Settings
Parameter
Bits
Maximum
Head
256
Sector
64
Total
24
21
16.7 million
The location of the data is specified with 24 bits, which means that you can access a maximum of
16.7 million sectors. With a sector size of 512 bytes, you can access a hard disk of up to 8.4 GB.
There is a problem, however. It is unusual that a disk will have the number of heads and cylinders
that happens to match the limits imposed by the design of interrupt 13h. The first fix for this
problem was to employ various translation modes which shuffle bits between the head and sector
addresses. Doing this meant that it was only necessary to change the BIOS not to change the
operating systems. However, disk sizes soon passed 8.4 GB.
To handle disks larger than 8.4 GB an extended version of the interrupt 13h functions allows the
location of data to to be specified using 64 bits, meaning that it should work until disk sizes hit 9.4
x 1021 bytes, which probably won't happen this year. This is called Logical Block Addressing (LBA
mode). When using LBA mode, cylinder addresses are a single number, and there is no such thing
as cylinders, sectors and heads, except for compatibility with older operating systems.
Normal mode (CHS)
In this mode, the location of data on the disk is described in terms of the Cylinder, Head and Sector3
at which the data resides.
The BIOS interface for reading from the disk is via a software interrupt, interrupt 13h (13
hexadecimal). The limits for the cylinder, head and sector numbers do not correspond with the
limits for the IDE drive interface, although they were adequate for older disk drives.
Interface
Cylinders
Heads
Sectors
IDE/ATA
65536
16
256
1024
256
64
1024
16
64
In Normal mode, the cylinder, head and sector numbers requested when interrupt 13h is called are
used as-is.
One of the results of this arrangement is that Normal mode cannot handle disks larger than 504Mb
(that is 1024 cylinders, 16 heads and 64 sectors).
Large mode
If the disk size is between 504 Mb and 8Gb, a horrible hack exists to make the disk addressable,
without requiring changes to the software. You may notice that the Heads parameter of the
interrupt 13h service has 4 bits of extra capacity that will never be used with an IDE disk. Large
mode shifts one, two or four bits from the cylinder specification to the head specification. Shifting
4 bits means that you can point to 16 times as much data. It's a horrible hack, but it means you can
have disks as large as 8Gb.
Because the translation mechanism is not easily predictable, the BIOS may not access the sectors
you intend when the cylinder you ask for is above 1024.
3 Hard disks may have a number of electromagnetic read-write heads. Each head can move across the rotating disk
platter to a cylinder and wait for a given sector to fly past.
3 BIOS Settings
22
The disk also looks rather different to what it would in Normal mode.
The various modes of accessing a disk are subject to the following limitations.
Method
Cylinders
Bits
504Mb
1024
24
Interrupt 13h
8.4Gb
1024
24
8.4Gb
16384
24
ATA interface
128Gb
28
32
LBA mode
32
A kernel patch for the 2.4.18 kernel allows it to address disks with up to 48 bits.
The original for reading and writing an IDE disk is called programmed IO. The choice of PIO
mode determines the speed at which the CPU reads data from the IDE interface. Mode 0 is the
slowest, and mode 4 is the fastest.
DMA modes
DMA is the technique where the IDE interface writes data to memory by direct memory access.
This requires little intervention by the CPU, which can spend its time doing more productive work.
Newer hard disks and motherboards do support this. However, if you connect a new hard disk into
an older motherboard, you may have to manually specify that you do not wish to use DMA to avoid
triggering hardware bugs.
If a BIOS supports setting the DMA mode, Mode 0 is the slowest and Mode 5 is the fastest (and
possibly most error-prone).
32 bit transfer
IDE interfaces always use 16 bit transfer. The 32 bit transfer mode setting that you will find in
some BIOS configuration programs refers to transfer on the PCI bus, rather than to the actual
3 BIOS Settings
23
Block mode involves transferring up to 32 blocks of data between the IDE interface and memory
without monitoring by the CPU. On some older motherboards does not work correctly, but the
BIOS still provides an option to enable it.
The BIOS will generally offer two settings related to an on-board display adapter.
Enable / disable if you have a display card which you prefer to the on-board one, you will want
to choose to use it.
Memory to allocate on-board display adapters will use the top 4MB to 16MB (or more) of your
system memory. You can set this amount in the BIOS settings.
Network card
If there is an on-board network card, you may want to enable or disable it.
Printer and communications port
Usually a built-in parallel port will allow you to choose the IO address and interrupt to be used for
the port.
Port
LPT1
LPT2
LPT3
COM1
COM2
COM3
COM4
IO address
0x378
0x278
0x3BC
0x3F8
0x2F8
0x3E8
0x2E8
Interrupt
Linux device
/dev/lp0
/dev/lp1
/dev/lp2
3 BIOS Settings
24
PCI devices. Devices on a PCI bus can share interrupts with each other.
ISA Plug and play devices the ISA plug and play protocol is designed to allow the BIOS to
allocate resources to a device and configure it to use them. It is usually better to let Linux
configure ISA Plug and Play devices.
3.8.1 /proc/{ioports,interrupts,dma,pci}
The /proc file system contains a number of virtual files which show the state of your hardware.
/proc/ioports
This shows the port numbers which are in use by devices for which a kernel module is loaded. If
two devices use the same IO ports, then it is unlikely that they will work together. One of them
needs to be reconfigured either by changing jumper settings, plug and play settings, or
reconfiguring internal peripherals.
G ` a
e Eda
$ &
e "ga
G
P@"Eda
$ G $ &
ka
$ v
8@ka
&
%'da
"ga
G
Eda
$ &
f Q Q V Q
" EF
`
` !
`
` ! m `
f
` f
` % ! m ` %
j ` % ! j ` %
f
f
f
f
` % ! %
j % ! j %
l f
l f
l ` ! `
l l l
!
`
` ! `
` !
` ! $
m
m
` !
l l
` !
j
j
` !
h
` i !
f e
` % !
` !
U V e ca
D2db `
Older versions of the Linux kernel used /proc/pci to list the devices plugged into the PCI bus.
3.8.2 lspci
That machine does not have any notable DMA assignments. Its problems are clearly caused by
other factors.
$ $ a
n
U S V Q U V e ca
8 F D2db `
DMA channels enable fast data transfer which does not involve the CPU.
/proc/dma
The example above is for a single CPU machine. On this particular machine, the USB controller
and the VIA sound controll.er share interrupt 5.
s8s
t
s8s
t
s8s
t
s8s
t
s8s
t
s t
8s
e f m
Xz%# t xbE e x 8j Xz%# t bE e
x
em "6gxz 3 "'"
$
!
$ $
$
8@v
&
%'
s8s
t
s8s
t
s t
8s
@"% e @~ s
a
a t
h
! 1
a
{ if
h l
! 1
a
j j
e
e
! 1
a
e f m
r e 0 q 8j
{ 8
f
! 1
a
! 1
a m
! 1
a h
! 1
a e
e { 8
f
! 1
a
l h h j
'i hh
! 1
a
| s s
f V Q U V e ca
"g W 7EF D2db `
This file shows the interrupt assignments which are in use by devices supported by the kernel and
the loaded modules. If two devices use the same interrupt this is not necessarily a problem.
Devices on a PCI bus will often share interrupts, and this causes no problems. ISA devices cannot
easily share interrupts, since the ISA bus uses edge triggering, while sharing interrupts requires
level triggering.
/proc/interrupts
The I/O port ranges are listed in hexadecimal. Some of the devices are integrated into the
motherboard (such as the DMA controller). The kernel tracks their port numbers so that it can
prevent unexpected interference in their operation (e.g. by user space programs).
t s ~
j m e
( 8s %w 8G H8G w j s m 1 vz%# t y88
u X
x
t s ~
j m e
( 8s %w 8G H8G w j s m 1 vz%# t y88
u X
x
j
0
j
0
e
0
e
0
$
s j m e
s w l { %w j s m 1 vz%# t y88
#
u X
x
$
s j m e
#
s w l { %w j s m 1 vz%# t y88
u X
x
7t
# 3 2't va
1 w u
# 3 2't va
1 w u
s t s
"$ Ps a
s t s
"$ Ps a
s t s
"$ Ps a
s t s
"$ Ps a
e $ 6
m "ga f
l
# 3 2't va
1 w u
e $ 6
m "ga f
f
# 3 2't va
1 w u
na ` e
na e
l
3 2't va
1 w u
3 "'"ka `
!
3 2't va
1 w u
e $ 6
m "ga `
`
~ t } | X
"| s gz%# t y88 #
x
$
m
m
` m ! m
l m m
` j ! j
`
` 8@! 8
`
` m @! m
`
` @!
`
` @!
8 f
!
l
f 8 f
!
! f
ff ! f
! m e
! e
` e ! e
!
` !
!
3 BIOS Settings
25
1. You can either disable the parallel port in the integrated peripherals section, or you can change
its settings so that it does not use IRQ 5. You also need to reserve IRQ 5 for the ISA bus.
2. Explained in the text
3. Always
4. Halt on keyboard, etc.
5. pci, dma, interrupts
6. lspci and lspnp (for newer kernels)
Answers to quiz questions
1. Set up your BIOS so that your system will boot up even if the keyboard and screen are absent.
Test whether the system does actually boot up.
2. Configure your BIOS so that your system will not boot up at all, and then fix the problem. What
did you change? Are there other settings that can cause problems?
3. Make a list of the IRQ's which are available on your PC.
Assignment
1. You have a system with an ISA bus and an internal modem requiring IRQ 5. The BIOS assigns
IRQ5 to the on-board parallel port. What problem will result (if any). How do you make the
modem and parallel port work simultaneously?
2. What is the significance of hard disk cylinder 1024 to the BIOS?
3. When is it better to use LBA mode?
4. What are the error handling settings supported by some BIOSes
5. Which files in /proc show the Linux view of the BIOS?
6. Which commands list PCI and ISA peripherals?
7. Why is dmesg significant for diagnosing BIOS problems?
Quiz questions
3.9 Review
The messages displayed during boot up correspond to the kernel initialising the devices installed in
the system. The messages are usually appended to /var/log/messages, and can be displayed with
dmesg after the system has booted up.
26
3 BIOS Settings
7. dmesg shows kernel messages, and the kernel talks to the BIOS as it boots up.
27
28
# G & & 3
88'8H%D 1
1
& &
%D 3 B@8PBE w
# $ & &
% U
"@8R pg D2db `
W S i
V e ca
29
Q
7R
@9 Pdbd8@ % W
U Q i R W
W V f W f U W R W g U S
yydbH'%%27%4 84W
7
8S U
i W S Q R U
hE ' B 8 W D%F '
# $ & # 1
@8BE ` w @8 U
&
%& ` B%P$ 3 Fb t
# v
r
a
#9 5H%D# w
# $ C
3
3 BY@ 1
# & #
%@9 3 s
This same interface is supported by almost all external modems, including ISDN terminal adapters
which pretend to be analogue modems. (An ISDN terminal adapter is not a modem, since it does
digital to digital translation rather than digital to analogue modulation / demodulation).
G v $ & G a $
@8 7%72E# 8'py%@
a $ #
f bEC 8
a @"7dx yh Y7 @8nx ' b@" %
e h a $ $
6 a $
f
h h j
Y t x m ` % b s x w y a 1 w gx 8x "@@
a
a
|
#
~ 6
U i W S R U W f W
@9 "@8%'"'f 0 ) 4$ %w
(
If you have an internal modem with a known port and IRQ setting, you would set the port and IRQ
like this.
l Y t x m % b s x yh a 1 w gx e "@@
f
j
a
a
|
~ 6
W S i R U W f W f I
F"@8Bh2%'"'i 7" 0 ) 4$ %w
(
f nx
4nx
nx
4nx
f
m e
m f
m ` e
m ` f
8gx8
G x r
8gxr f
G x
8gxr e
G x
8gxr
G x
@s
@s
@s
@s
q
f "@@
~ 6
e "@@
~ 6
"@@
~ 6
"@@
~ 6
You will notice that there are two interrupts shared between four ports. This means that the serial
ports will only work correctly if level-triggered interrupts are used, or if additional interrupt lines
are configured. IRQ 5 is a good choice, since the Linux parallel port driver uses polling by default
rather than using an interrupt.
4.1.3 Winmodems4
It can be useful for an internal modem to run at the fastest possible speed, 115200 bps. Some older
applications do not request a connection at speeds higher than 38400 bps. When the application
requests 38400 bps, the actual baud rate will be set to 115200 if the spd_vhi flag is given.
f
h h j
3 6 8'py%@ Y t x m ` % b s x w y a 1 w gx e "@@
G a $
x a
a
|
~ 6
S f W S i R U W f W f I
"h2F"@8Bh2%'"'i 7" 0 da "@ `
c $
There are a number of devices which are called Winmodems including host-based, HCF-, HSP-,
4 Winmodem is a trademark of US Robotics.
30
HSF-, controllerless, host-controlled, and soft modems. Winmodems rely on vendor software to act
as true modems. While software for some versions of Microsoft Windows is available, drivers for
Linux are not always. Many USB modems are Winmodems.
Linux support for Winmodems is coordinated by www.linmodems.org. Winmodems
Require relatively high CPU speeds to operate reliably (better than 400MHz) (although this is
not problematic on modern PC's)
May require closed source software to operate. As a result you may be tied in to a specific
kernel version.
You will generally have to load a kernel module to make a Winmodem work. This may require
some research and recompiling the Linux kernel.
For kernel 2.2 and before, you use isapnptools. pnpdump dumps all the possible combinations of
the devices. You then edit the file it produces, and it becomes your /etc/isapnp.conf file.
T Q V U f W S I f
% Y@EV Dg 7"hi 0
$q { @s | @s q
{ l l e m 1
# t
e
h Dl @B ` H%u 0
e f a $ # #
A ~ 6 $
w
! "
! s # s j w H87 s !9%@ t ~ B0
! ! #
X "84%vx X "%u 0
# 6 # u
#
{ l l e m &
1
X m 7" 3 nx { 8 %n @s t %u 0
& v
$ ~ x
#
m
r
E 2 j f 9 24m 2 ` B%@ha $ 50
# $ q
s
31
r
r
3 1 s
r 4@%w q
# #
"% ` 8@B 0
# # G #
l l e m 1 ' w
r'%12 w 0( m { @s ( q
)
r m m f
r @% ~ w u52 t yq e 7t q
q r & ~
& ~
r f f
r % ~ w u5r e t yq 7t q
q
& ~
e e
j
rr ~ w u5r t yq 7t q
q
r
r l % w } s q w q
r
r f % w } s q w q
r r r o
i 2 q { t q 1 t q
0
!
! " w !9%@ t ~ w
! ! #
The corresponding line for modprobe specifies the parameters to load the sound card
module.
m m
@f !Evx f 6 vx l 5 Edx f !Edx { 6nx e 6'98@@%&
j $ &
5 & f
5 G &
5 $ &
5 e
5 G
m m
@f !Evx f 6 vx l 5 Edx f !Edx { 6nx e 6'B"8@
j $ &
5 & f
5 G &
5 $ &
5 e
5 # G
Once /etc/isapnp.conf exists, you can configure the card(s) to the chosen settings by running isapnp,
and then load the sound module.
I f W Q S Q
hpI 7
T Q V U f W U f I f
% Y@EV 4@E 7"hi 7
This is the electronic equivalent of physically changing the jumpers on the sound card to select IO
ports, interrupts and DMA channels.
Kernel 2.4
Kernel 2.4 introduces a kernel module isa-pnp which handles the configuration of ISA PNP devices
automagically. If the module you are using has not been updated to use isa-pnp configuration, you
will have to configure it with pnpdump and isapnp.
4.4 Review
Quiz questions
1. What are the differences between PPP, SLIP and CSLIP? (This is part of the LPIC objectives...)
2. What is the aim of the plug 'n play standard (PnP)?
3. What do you need to do to establish which IRQ, IO Port and DMA settings a PnP compatible
card uses?
4. Which program sets up an ISA PnP sound card?
5. Which modems are generally compatible with Linux?
6. Why is it necessary to set the serial port speed to 115200 baud?
Assignment
1. PPP is newer, and supports a number of options and dynamically configures to the lowest
common denominator. SLIP requires matching configurations at both ends. CSLIP is
compressed SLIP.
2.
3.
4.
5.
6.
32
5 SCSI devices
33
5 SCSI devices
SCSI, n. [Small Computer System Interface] A bus-independent standard for
system-level interfacing between a computer and intelligent devices. Typically
annotated in literature with 'sexy' (/sek'see/), 'sissy' (/sis'ee/), and 'scuzzy'
(/skuh'zee/) as pronunciation guides the last being the overwhelmingly
predominant form, much to the dismay of the designers and their marketing
people. One can usually assume that a person who pronounces it /S-C-S-I/ is
clueless.
Jargon File 4.2.0
It will help you to have a system with SCSI devices for this chapter. You can set up IDE-SCSI by
adding hdc=ide-scsi or similar to the kernel command line for your CDROM device.
Each device on a SCSI chain must have a unique SCSI ID. This must be set manually, i.e. by
changing jumpers or switches5.
5 An exception to this is SCA disks which set their own SCSI ID by black magic. The SCA connector includes power,
SCSI ID, and the SCSI bus all in one connector. SCA drives are used for RAID and "hot swap" situations.
Each SCSI adapter (or at least its driver) has an entry in /proc such as /proc/scsi/aic7xxx/0. The
e a # "6@ t ~ s ~ t ~ w
X Y@
a 6
f% a # "6@ t ~ s ~ t ~ w
% Y@
f a 6
j | !
~ 78 @! @s
a b
j a
#
| { "} %
~ #
a b
a
#
@! @s
a G
b8 1
az% 78s a %u
t
#
t z$ s pb@88}
a # # 3
a
!
8 w
a G
b8 1
az%
t a %u
#
t z$ s pb@88}
a # # 3
a
a 6 $
y8"@B 3 w
f V f f V f V Q U
E'i'E'iF V 0
In the directory /proc/scsi you will find useful information about the SCSI subsystem.
A very useful entry is /proc/scsi/scsi which lists the attached SCSI devices.
5.2.2 /proc/scsi
Note that grub does not use the Linux disk naming scheme hd0 means the first hard
disk, and not particularly an IDE hard disk.
{ X e %8 84%
X ! # #
5% w 56P { XX e !E& 8Fv
9 # 6 #
r j x 3 q
t ~ s p ` P
~ &
l @%@6
$ 6 5
# ! $
7%
{ XX e %8 86%
! # 5 #
8!87
# 5 $
{ X e E& 86@E'
X ! 9 # 6 5 $ &
# X
` b8@ 0
lilo.conf (or grub.conf) must specify that this particular initial root disk is loaded on
startup:
i S Q Q I S r
287 " @"7E@P @"7" 0
Q Q I S
PV 0
$ ' G $ $ 3 $
' l (87@8 8 B$
# X &
` y8%%@@
In order to communicate with your SCSI disks, you need to load the appropriate kernel modules, or
compile support for your SCSI host adapter into the kernel. If your root file system is on a SCSI
disk, you will need to modify /etc/modules.conf to contain a line specifying your SCSI host
adapter's driver before you run mkinitrd.
o o o o
o o o o
oooo
1 2 4 8
5 SCSI devices
34
st
SCSI tape
Used with tar, cpio and friends, and mt (magnetic tape) for control
sr
SCSI CD ROM Audio and data CD ROMs
or scd
sd
SCSI disk
Name Meaning
An example of a Linux SCSI device name is /dev/sda, which is the first SCSI hard disk. Linux
SCSI supports SCSI devices in addition to disks.
5.2.3 Devices
The contents of the file depend on the lower level driver. For some drivers, parameters can be set
by writing to this file.
r
88%C 48 q " ` "%P 1
# $ $
# $ $
h
r yh q
h
q $ x
h
# a #
@"kxr yh i xr yh q ny%@9 ` "% 1
x
# # $
h
@ ` dz@% 8 X h 4" ` "%P%788$ "P"@
` x
$ # $ # ~ C
# 6
r a j a a hq
h j # $ $
r C l h h
88%F 'm 48 'h l q h " ` "%P 1
# $ $ l l
e q $ x
e # a #
r h e q @"kxr m i xr m q ny%@9 ` "% 1
x
# # $
m @ `` dz@% 8 X 44" ` "%P%78 A "P"@
x
$ # $ # ~
# 6
r a a a hq
a $
y@~
1 x x x x x x x x x x x x x x x )
a %@"#H@8 3 l 5 ` "@H8G 3 8@BP% w
# $
$
6
G $
h h h h h
h
h
h h h h h h h h h h h h h h h h
1 hhEe x hhEe x Ee x Ee x hEe x hEe x hEe x hhEe x Ee x Ee x Ee x Ee x Ee x Ee x Ee x Ee )
$
a %@"H@8 3 l 5 ` 7%H"@ PA@$ 1
# $ #
$ $ 6
m a 3 8@ @@$ 1 %$ `
G
y%@ @$ 1 A
a $
y%@ "7%P@@$ 1
a $
$ #
y%@ "7%D%"|
a $
$ # $
a $
$ # # #
` ` `
` y%@ "7%P
$ # # $ # $
"7%ka "% 1
#
h
i b A s 87 w
a
#
G $
j b A s ~ 7t
m
a
#
m % y88G # t
e f j a
h
hEe @$ s x j A }gx h w
$
x e 87 w Evx 87 w y" s ~
6
$
6
a
Y t
a
$ # a
"7%kz~ 7t t ~ s B87 w
~ G $
s n@ ` s s 587 w
~ a #
~ G $
@ b@% t 8w s
a $
s t s
A %"|
$
G $
87 w
X # $ #
b@"PH `
#
s
G $ $ 3
87H@8 Dt ~ s B%"| { e ! w } w
~ $
h a
"7$ a
"7$ a
a 87 t ~ s ~
G $ w
a $% @ ` s 87 w
#
#
G $
3 ~
w % 1 % l Ew
s t
s s s t
~ 1w 1 ~ 78 % l Ew
%$ ` P"7%2 81
$ # s
a #
y"8G " s
G &
s t G $
l Ew 87 w
X e X f { X X h a "8487%
# 6 6
V U f V f V Q U
% %'E'iF V 0
driver name can be something like aic7xxx or BusLogic. The digit is the bus number, and is
relevant if you have more than one SCSI bus in your system.
5 SCSI devices
35
5 SCSI devices
Name Meaning
sg
36
Usage
SCSI generic
character
interface
@8| r 1 q ~ t ~ s 5 `
~
w s
@" !% " 8 s
$E w
&
"7$%#
l
a %E t ~ s
# $ # & 1
a # v
%
t
a 7Bt ~ s
# #
"% ` $ `
~587 w @88}
G $
3 s
s @%$ Ft ~ s ~
~ G $
587 w @88}
# t " t ~ s ~
a 8 B 4
& |
t
a 7Bt ~ s H
~
#
"8G "@
6
5.4 Review
Quiz questions
1.
2.
3.
4.
5 SCSI devices
37
1. Configure your BIOS to boot Linux from a chosen SCSI disk, without relying on a floppy or
IDE disk.
2. Find out which SCSI bus format is the most popular in your country.
3. Find out what ide-scsi is, and why it is used.
Answers to quiz questions
1. SCSI removes load from the CPU, as it is a well-designed protocol. As a result, SCSI works
faster.
2. Far too many.
3. They're better.
4. The ID uniquely identifies each device (or controller) on the SCSI chain.
5. Manually usually by changing jumpers, but occasionally via software.
6. Set the SCSI boot device to the disk, and set the BIOS to boot off the SCSI disk. Install a boot
loader on the SCSI disk.
7. /proc/scsi/scsi
6 PC cards
38
6 PC cards
Is there an API or other means to determine what video card, namely the chipset,
that the user has installed on his machine?
On a modern X86 machine use the PCI/AGP bus data. On a PS/2 use the MCA
bus data. On nubus use the nubus probe data. On old style ISA bus PCs don a
large pointy hat and spend several years reading arcane and forbidden scrolls
Alan Cox on hardware probing
For this chapter you should know how to log in as root and execute commands. It will help you to
have a system with unusual ISA and PCI cards to experiment with.
6 A bus is also a long motor vehicle for carrying passengers, usually along a fixed route. The fixed route part is the
reason that the term is used in computers.
6 PC cards
39
Interrupt requests (IRQs) when a peripheral requires CPU attention, it issues an interrupt
request. The CPU, in response to the request, stops what it is doing, and speaks to the relevant
peripheral (usually to retrieve data from it, or to send more data to it). Busses generally have a
number of interrupt request lines, which enables the kernel to determine which peripheral caused
the interrupt. If two peripherals share an interrupt request line, it is possible that they will trigger
an interrupt request simultaneously. The kernel must respond to both devices for proper
operation. Shared interrupts work when interrupts are Level triggered (e.g. on the PCI bus) and
not possible when interrupts are edge triggered (e.g. on the ISA bus).
DMA channels Direct Memory Access Using a DMA channel allows a peripheral to copy
data directly into or out of the computer's memory. This means that the CPU is not intimately
involved with the data transfer once it is in motion. Use of a DMA channel allows a device to
perform direct memory access. It is generally not possible to share DMA channels between
peripherals.
e f f 6 # C # v # | a &
%yh a %f "@Hg@E'E
6
r @f @uq
3 kz%# t x %Ep"7$ a H 3
# X
# & ~ & 6
# #
i f i V f R I f
h56dDD2%hE 7"hi
~
%
X { a
0 da "@#
c $
lspci can show more information. Here's lspci displaying information for a specific slot:
h
r @uq
6
1
w
# X
# & ~ & 6
# #
s
6 #
r yh @uq s w l { %w
j m e
j s m 1 vz%# t y88 # 3 2't va HB%'% h X
l
u X
x 1 w u # $ $ &
r
t s ~
j m e
6@u2( 8s %w 8G H8G w j s m 1 vz%# t y88 # 3 2't vb %4X
q
u X
x 1 w u a l
l
r
%$ @uq "gz%# t y88 # 3 2't va s "| f X
6 ~ | X
x 1 w u #
~
l
r
%$ @uq "gz%# t y88 # 3 2't va s "| e X
6 ~ | X
x 1 w u #
~
j
r @uq 7t
6
s t
j m e
l
@E" s@8s hh s m 1 vz%# t y88 # 3 2't vb$ ` 5 7t X
$
u X
x 1 w u a
#
r H@uq
6
j m e
l
( 3 58G H8G w j s m 1 vz%# t y88 # 3 2't vb % w ~ t X
~ ~
u X
x 1 w u a
s
f
h j f f j f
u X
x 1 w u a
V f R I f c $
%hE 7"hiD6da "w
f f B f f
( 8 6 8 1 C
B
a
a
a
a
a
a
a
a
a
`
Happily, lspci can display the names of the devices by cross referencing against /usr/share/pci.ids
which contains a list of vendors and their products. Even if the vendor ID is not listed, lspci will
tell you what type of device it is.
To list the components on the PCI bus, you can look at the file /proc/pci or use the lspci command.
h
r @uq
6
r @f @uq
6
6
r yh @uq
r H@uq
6
r
%$ @uq
6
r
%$ @uq
6
j
r @uq
6
r H@uq
6
f
r %
6
@uq
e
e
{
m h
hif
l h
'if
m f
@%f
m f
@%f
'i
l h
j m j
h f
i8m
h f
i%
ab
e
a m
j
a
j
a
j
a
j
a
j
a
j
a
j
a
j
a
a %
f
e a
e
a
a
m j
a %
f
a %
f
a
a
j
j
a
a
j
i V f R
'2%hE
s
$
s
$
s
$
s
$
s
$
s
$
s
$
s
$
s
$
$
s
I f
7"hi
X
X {
h X
l
l
X
f X
l
e X
l
X
l
X
l
X
X
0 da
c
a
a
a
a
a
a
a
a
a
$
"
Each device on the PCI bus is plugged into a specific slot, and identifies itself by manufacturer and
device number:
h
r @uq
6
r @f @uq
6
6
r yh @uq
r H@uq
6
r
%$ @uq
6
r
%$ @uq
6
j
r @uq
6
r H@uq
6
f
r %
6
@uq
e
e
{
m h
hif
l h
'if
m f
@%f
m f
@%f
'i
l h
j m j
h f
i8m
h f
i%
f $
ab a % s
e e
e $
a m a s
j
a a s
$
j
m j $
a a s
j
f
a a % s
$
j
f
a a % s
$
j
$
a a s
j
j $
a a s
j a
j $
a s
j
j $
a a s
i V f R I f c
'2%hE 7"hiD6da
X
X {
h X
l
l
X
f X
l
e X
l
X
l
X
l
X
X
$
"w
a
a
a
a
a
a
a
a
a
`
Each device on the PCI bus is plugged into a specific slot, and identifies itself by manufacturer and
device number:
lspci list PCI devices
6 PC cards
40
A Plug and Play (PnP) device is very similar to a card which is configured with jumpers except
that the jumpers are missing. ISA PnP devices are automagically configured by the BIOS or the
S Q f
hR 0 da "
c $
g b t ` Q W S Q f
W Q S
kc %54h7
usq7%i` kfP@E@` CD@ CD7a@` @T 0 da "
b t p rp T h g b Q S c e bc V d bc b Q
c $
So we learn that the network card is not at address 0x220. To try every conceivable
address, we can do something like this
To change the settings that an ISA card is using generally requires changing the jumpers on the
card. The section following this one explains how to change the settings on an ISA card which
supports ISA Plug'n Play. With a few exceptions, the kernel modules supporting ISA cards have to
be configured with specific parameters. Most drivers are capable of detecting interrupt values, so
the critical values to assign are the I/O addresses.
The best way to assign values is to read the relevant documentation and set the jumpers according
to the free resources on your PC. However, you can sometimes guess. Here's an example of
loading the kernel module for a NE-2000 Ethernet Adapter. The NE2000 driver requires the 8390
driver:
X `
i( ` m m $ t
E X e 6# # #E X f 7%8 b"7"7$ s @E
e 5 # $
$
5
X $ G
$
X t
e
{ a m "@ s a
e e 6 t s
e $
a % ` x {
# #
6
"@dx
X
xr { H@uq
6
E E E E E E E E E E E E E E E E E E E E E E U U T I Q R Q I H F E E E E E E E E E E E E E E E E E E E E E
4!S4%0PGE
s
"
s
"
a #
b ` 8"@ s
6 t s
V V Q U
%F V 0 da "
c $
This is quite similar to the information in /proc/pci (the listing below is a partial listing).
"8F%@%EP% s ( yh Fy8"7"7$ s
a $ G
# 6 # & $ # $ C
( j h 5 9 (
xB hEe 6 )"7$ 6# @ % " p 2 ""G
$ #
$
# # $
j h 5 9 r G ! # # x e
$
( hEe 6 E"7$ 3 ` 7%kb"7! f q B %%
&
j h 5 9
$
( hEe 6 m B488G t
e
t x f 7%nz@E@5%vx @E"gy%@
# $ x 6 & & $ & a $
6 PC cards
41
For each CPU /proc/interrupts shows the interrupts which are in use by the kernel, and the number
of times each interrupt has triggered.
/proc/interrupts
The following kernel interface files in /proc display the assigned bus resources:
r
iB q 7" s @B 0
& v 3 X X X $ #
r
r
3 1 s
r 4@%w q
l l e s ' w
r
'%1 ttt 8% 3 B { yh s f s f 0( j { yh 81 ( q
v #
&
)
& ~
e
j
r r
iB s } s 5r ~ w u5r t yq 7t q
q
q
0
j a
b%8@8$ 7t ` H8
&
0
j # & $ $
88 %# @H@" 7t
& & $
0
% 8H@" 7t ' E
f
$ $
& & #
0
8H@" 7t '%E
e
$ $
j 6 $
0
8#48$ 7t " 8B"@P8
r r r o
i 2 q f t q 1 t q
0
rE%$ ` 9q 8G H87 "@9 db 3 @"}
#
6 # x
0
e
X h x x x { x l x h x f t
s
0
l ` m s H"@B"7" s
6 $ G &
$q j { yh 81 | @s q
l l e s
# t
r
iB q 7" s @B 0
& v 3 X X X $ #
r
r
3 1 s
r 4@%w q 0
l l e s ' w
r
'%1 ttt 8% 3 B { yh s f s f 0( j { yh 81 ( q
v #
&
)
& ~
e
j
r r
iB s } s 5r ~ w u5r t yq 7t q 0
q
q
0
j a
b%8@8$ 7t ` H8
&
0
j # & $ $
88 %# @H@" 7t
& & $
0
% 8H@" 7t ' E
f
$ $
& & #
0
8H@" 7t '%E
e
$ $
j 6 $
0
8#48$ 7t " 8B"@P8
r r r o
i 2 q f t q 1 t q 0
0
rE%$ ` 9q 8G H87 "@9 db 3 @"}
#
6 # x
0
e
X h x x x { x l x h x f t
s
0
l ` m s H"@B"7" s
6 $ G &
$q j { yh 81 | @s q
l l e s
# t
operating system.
Before the ISA PnP standard was devised, many hardware manufacturers configured their
hardware's I/O addresses using special MS-DOS based utility programs. The isapnp program
replaces this functionality.
To configure a PnP device:
Use pnpdump > /etc/isapnp.conf to determine possible configuration settings for the device.
Edit /etc/isapnp.conf and remove comments (optionally change the parameters from the defaults)
Run isapnp /etc/isapnp.conf to set the parameters the device actually uses.
Load a kernel module for the device.
The part of the file /etc/isapnp.conf which needs to be edited looks something like this:
6 PC cards
42
EF V 0 dz"
W Q V Q U
ca $ $
In this particular configuration, it will not work well to add an additional card requiring the address
0x210, since that is in use (by a 3c509 network adapter).
The iomem interface is provided from kernel 2.4. It shows 32 bit memory-mapped I/O allocations:
f8hh a { f% "@ s a
h
6 t s
` s a
# t s
C # G
%4G 7$ na
r $ q $
Eyi%@pa
na
o $ 6
@8ga
r $ q $
Eyi%@pa
3 da
$ # G
2G 7$ na
s # B{ yh f a
s
na
G ` a
e Eda
$ &
e "ga
G
P@"Eda
$ G $ &
ka
$ v
8@ka
&
%'da
"ga
G
Eda
$ &
f Q Q V Q
" EF
` !
`
` ! m `
{ l $ ! { l $
f
` f
` % ! m ` %
j ` % ! j ` %
f
f
f
f
` % ! %
e
` e
` ! m `
h e
e
` E !
f f e f f e
!
e
e
` !
l ` ! `
`
` ! `
` !
` ! $
m
m
` !
l l
` !
j
j
` !
h
` i !
f e
` % !
` !
U
V 0 dz"
ca $ $
The I/O ports which are in use by kernel drivers are displayed by the ioports interface
/proc/ioports and iomem
a
z~ t
a
s
l h f
l h f
y%8j e j y%8j e
a 8
a t
H ! 88w ! 7t
h
s t s
a
H ! 88w ! 7t yh
j e { @f
j
s t s j e
a
dxz 3 "'"D8@! 88w ! 7t hh {
h m j
! 6 s t s m
a
s t s
P ! 88w ! 7t
a m
$ $ s t
8s ! 1 P
a e
e
l h
'
8@P ! 88w ! 7t
$ v s t s
a
m f
j e
& s t s
%'B ! 88w ! 7t l { @8 { hh
a
| s s
| s s
f V Q U
"g W 7EF V 0 4"7 3 w
( G G
SMP processors display more interesting information, such as the allocation of interrupts
to the processors.
s t
8s ! 1
s t
8s ! 1
e @l
j f
s t
j j
j
@"% e @~ s 8s ! 1
3 dxr e 0 q 8j
e f m
x
s t
e f m
l e j
Xz%# t xbE e x 8j Xz%# t bE e 8s ! 1
x
s t
8s ! 1
em "6gxz 3 "'" 8s ! 1
s t
!
s t
$ $
8s ! 1
s t
e e l f
f
$
8@v 8s ! 1
s t
f m
l
&
%' 8s ! 1
| s s
f V Q U
"g W 7EF V 0 27w
( G G #
a
a t
h
a
a
e
a
a
a m
a h
a e
a
a
6 PC cards
43
6 PC cards
44
` `
86 @8ka ` ! `
` ` ` ` ` ` `
{ m a ffh "@ s a ` l ` ! `
f
6 t s
` ` ` ` `
e h m h e
$ $ # B
Pva 7 ! hh E
$
m h e
# B
Bva hh E !
w 9@E7na ` !
& ~
` ` ` ` `
9@E7na ` ! `
& ~
` ` ` `
4Eva ` l !
u
` `
$ $
p w 4Eva ` ! $
u
` ` `
w 9@E7na ` { !
& ~
` ` `
The PCI device in the above listing is a VGA display adapter. Memory mapped devices include
network adapters, PCMCIA, ACPI support and video ROM.
/proc/dma
The dma interface shows the DMA assignments in use for the ISA bus.
j $ # ~
@%na h
$ $ a
n
m @%na
$ # ~
U S V Q U
8 F V 0 dz"
ca $ $
6.8 Review
Quiz questions
1. Experiment with lpsci and pnpdump (perhaps lspnp) on your PC. Write an explanation of how
the output from the commands corresponds with the contents of /proc/interrupts, /proc/dma and /
proc/ioports.
2. Obtain an expansion card (such as a network card) and install it in your computer. Explain what
effect merely plugging in the card has on the BIOS, and on the output of various diagnostic
commands. Load an appropriate kernel module for the card, and write an explanation of the
effect this has on the output of the diagnostic commands.
Answers to quiz questions
1. An interrupt indicates that a peripheral requires CPU attention i.e. reading or writing data.
2. An IO address is used on a bus to identify a particular peripheral or a component of a peripheral.
3. A DMA channel is used for coordinating data transfers between peripherals and memory. This
frees the CPU for more productive work.
4. Either one or both of the devices does not work. It is possible that one of the devices may
function unreliably.
5. On a bus which uses level triggered interrupts it is possible to share interrupt values. On the ISA
bus and the EISA bus interrupt sharing is not possible. On the PCI bus, interrupt sharing is the
6 PC cards
norm.
45
7 Device configuration
46
7 Device configuration
Some of my readers ask me what a Serial Port is.
The answer is: I don't know.
Is it some kind of wine you have with breakfast?
the fortune cookie database (no attribution)
ISDN, DSL and PPP are all ways of getting connected from where you are to where you want to be.
Note that some of the objectives in this section are handled in the chapter on modem and sound card
hardware (namely the usage of setserial and modem compatibility requirements). By the end of this
chapter, you should know enough to set up a communications controller, but not necessarily to how
to use it for networking.
7 Device configuration
47
Server
Client
networking subsystem
networking subsystem
pppd
pppd
Modem
Modem
8 8
~
e { 2 "@
e @"B@"
The chat script /etc/ppp/chat-myisp provides the modem initialisation string and details
how to dial the ISP's number.
'
S%# t
1
@s @s
h h h
' hhhhh (B
$ '
' " 8!(B
$ '
'S$y'
' '
%Ch$ s %'$%@E' 1 w
& # |
1 w
1 w
1 w
1 w
1 w
To supply the user name and password, you need an entry in either /etc/ppp/chap-secrets or
/etc/ppp/pap-secrets, depending on the authentication method your ISP uses. You can put
the entry in both if you choose.
'
S@%@"'
' e @"y'
7 Device configuration
48
When you run pppd call myisp, the output is added to /var/log/messages (dependant on the
configuration of your syslogd).
The kernel module that supports most internal ISDN terminal adapters is the hisax module, which
supports the hisax ISDN chipset manufactured by Siemens.
External ISDN terminal adapters
There are a number of external ISDN terminal adapters available which emulate the functions of an
analogue modem. To connect one of these, you usually have to specify the correct initialisation
string. This may be one of the following (but consult the .INF file on the Windows driver disk if in
doubt):
X X
X
3 F8%8@F"FB@H B0
~ | # $
& & v # t X X
%PnX 5H%D5@98 p0
$ # v C # 6 t
s s 1
5 s w
1
f w
7.4 DSL
DSL uses a technique called PPP over Ethernet, or pppoe. For DSL to work, you need a properly
configured ethernet card, and then you run
( # G G G G ( # G G G G
E"8@ 8dFiE"8@ 8d
G G G
8
G G G
@8F8G
ifconfig shows the currently configured interfaces. If one of the interfaces is ppp0 or similar, then
the ppp connection is active. If ppp0 is absent, the connection is inactive.
route -n
When a PPP connection is active, there will be a route to the remote side of the PPP connection.
Usually there will be a route to the network on the other side. It is a common PPP error to have a
default route configured to your local network when starting PPP. In this situation, PPP does not
override the default route with its own.
ping
7 Device configuration
49
interfering firewall).
7.6 Review
Quiz questions
1.
2.
3.
4.
5.
6.
7.
Assignment
1. Obtain a modem and a telephone line and then set up an actual network connection to your own
ISP. Check that you can browse the web using a web browser such as lynx or w3m. Make notes
about how you configured the modem.
2. The Linux pppoe daemon, pppoed7 provides the same functionality as a DSL modem. Set up a
pppoed server and connect to it using pppoe from another computer in the network. Diagnose
the connection using the techniques described in this chapter. Does the server route packets for
you?
Answers to quiz questions
1.
2.
3.
4.
5.
6.
7.
7 This question is strictly for those genius types who are too smart for their own good. You guys must learn how to
hide your talents a bit so the rest of us can feel better.
8 USB hardware
50
8 USB hardware
Looks nice to me but about the only way you are likely to get Linus to take in
kernel debugging patches is to turn them into hex and disguise them as USB
firmware ;)
Alan Cox's guide on submitting Linux patches, today
chapter #3, kernel debuggers
For this chapter, it will be helpful to have some USB hardware to experiment with. Get a USB
scanner, a USB memory stick, a USB camera or anything.
8 USB hardware
51
usb-ohci.o The Open Host Controller Interface (OHCI, by Compaq) (used by the Nvidia
chipset)
usb-uhci.o The Universal Host Controller Interface (UHCI, by Intel) standard (simpler
hardware, more complex software, marginally higher CPU load). (Used by the VIA chipset, and,
of course, Intel.)
To determine which controller you have in a particular computer, you use lspci. The particular
controller in the systems shown below is a UHCI controller.
` 8 5%$
! Gq r
#
s
6
@uq
t t
l f e
~ |
"P 8s w m
r t } |
@( s d ` 8 q
! G
e
# $
%8G s # t a s "| e X a
#
~
I f g i
h p h2%hE 7"hi2da " 3
W i V f R I f e c $ $
As USB becomes increasingly popular, you are likely to come across the following kernel modules
which support specific classes of USB devices:
mousedev USB mouse or pointer
keybdev USB keyboard
usb-storage CD-ROM, hard disk, or ZIP drives
audio USB sound cards
joydev USB joystick
All of these rely on the usbcore.o and either usb-uhci.o or usb-ohci.o.
f
| f | f
$
a
a
7t a "@
l 6
7t a "@
6
7t a "@
6
I f g f R I f
hhE 7"hi 0
"
"
"
e
ca G $
db87 3
f
| f | f
X
~ |
"@"
G G
8 1 87% 8
3
m
#
a 87% 8 "@
G
6
A e nz%# t % 3 7% s w e a h 8 7t a "@ "
l 6
~ X
$ G
$
0 db87 3
i S U W
hi d8DphhR
f i I f g f
ca G $
R U
v
Ga & l j #
'""b%%@ 86
h #
Ga &
'""b%%@ yi 86
6 ta & e
@8@b%%@ f l 86
#
a & e
3 b%%@ f l 86
#
l #
6 ta & e {
@8@b%%@ { h 86
l #
a & e {
3 b%%@ { h 86
e m #
$ ! a &
%@"'"b%%@ 86
h #
v C #a G
%b87% % { { i 86
h #
$ Ga &
"78 8b%%@ { { i 86
v C #a G
%b87% % { 86
#
%89@ P7T 0 8& '" @z%78
W f U T W i Q S W S
a $
The vendor directory specifies vendor and product ID's and the corresponding module that
should be loaded.
# #a &
b%%@ {
$
3 "b%%@
a &
$
m $
$ ! a &
@%@"'"b%%@
m $
a &
%& @b%%@
m $
%& b%%@
a &
$a & $
b%%@
& $a & e $
'b%%@
l $
# Ga & f
%b%%@ %
# Ga & e l $
%b%%@
# Ga & l $
%b%%@
6 a & e f $
@@"%& b%%@ %
a & e f $
3 b%%@ %
6 va & f $
@8@b%%@ %
a & f $
3 b%%@ %
%89@4%%B7T 0 8& '" @z%78
W f U T W i f f U R V S
a $
The class/ directory specifies which module should be loaded for the various classes of
USB device:
#
86
#
` X 8& '"
# X $
` bG
@8 3
$
i f
04R 0 8& '" @z%78
a $
If your system does not use the kernel based /sbin/hotplug, you are probably using usbmgr, and
your kernel is probably version 2.2. You will probably also not have lsusb installed.
usbmgr is user-mode daemon which loads and unloads USB kernel modules. When USB devices
connect into or disconnect from a USB hub, usbmgr does the following, based on its configuration
directory /etc/usbmgr/
Loads and unloads Linux kernel modules.
Execute a file to setup USB devices.
Here's what you find in the /etc/usbmgr directory:
8.4 usbmgr
f
e 8@ e
# # $ ~ $
s
e X
f
% s
$ ` E'
# $
6
"@ "
% s
# u
%
$ s E
9 ~ v
$
6
s "@
$
s "@
~ 6
$ 6
s "@
8 USB hardware
52
8 USB hardware
53
8.5 /sbin/hotplug
When a USB device (or a PCMCIA card, etc.) is plugged in kernel 2.4 this triggers a chain of
events:
The kernel runs the program /sbin/hotplug.
/sbin/hotplug runs the appropriate script from /etc/hotplug/*.agent.
If the device is USB, /etc/hotplug/usb.agent runs usbmodules to find out which kernel modules
may be able to manage interfaces on currently plugged in USB devices.
What /etc/hotplug/usb.agent does is more or less the equivalent of this:
W Q
S
' & e ' v ! ! G
S%%77(4H8@@%&
Q S r s u e 6 ! ! q e & Q
gi t 2"@48%%& '"u)72%%4@T
8.6 Review
Quiz questions
1. Connect a USB device on your computer, such as a mouse, a hard disk, scanner or memory stick.
Write down the name and model number of the device, and identify which kernel modules are
loaded when you plug the device in. If no kernel modules are loaded, verify that hotplug or
usbmgr is configured and running, and whether the device is supported.
2. Configure your computer to use the device, i.e. configure X to use your mouse, or mount your
USB disk, or set up xsane for your hard disk or gphoto for your camera.
Answers to quiz questions
9 Partitioning disks
54
9 Partitioning disks
Nine megs for the secretaries fair,
Seven megs for the hackers scarce,
Five megs for the grads in smoky lairs,
Three megs for system source
One disk to rule them all,
One disk to bind them,
One disk to hold the files
And in the darkness grind 'em.
In order to keep your computer neat, disk space is divided into areas called partitions.
Partitioning a disk makes it possible to allocate parts of the disk to various operating systems and
specific purposes. For beginners, partitioning disks is the most complicated part of installing a
Linux system.
9 Partitioning disks
55
The Linux disk naming scheme identifies each disk and each partition on each disk. IDE disk
names are prefixed hd (for hard disk) and numbered a, b, c, d (and perhaps e, f
and more). SCSI disks are prefixed sd (for SCSI disk). Partitions are numbered, starting at 1
and up to 32 (if you have 32 partitions, you need help).
Here are a few examples of disk and partition names:
Device name
Meaning
/dev/hda
/dev/hdb
/dev/hdc5
/dev/hdd2
/dev/sda
/dev/sdd9
Extended partitions
The original MS DOS partition table was limited to four partitions8. To work around this limitation,
the current scheme allows additional logical partitions accomodated in a single extended
partition. If there are more than 4 partitions, one of the first four partitions will be an extended
partition.
/dev/hda (the disk)
hda1
hda2
hda4
extended partition
hda5
ext2fs
/boot
vfat
/mnt/windows
hda6
hda7
swap
reiserfs
/
unused
9 Partitioning disks
56
For dual booting, it is generally best to install the other operating system before you install Linux.
Doing this makes it difficult for the other operating system to delete your carefully installed Linux
system by mistake. Also, Linux is able to handle a number of strange partitioning schemes used by
other operating systems.
When you install an operating system, it is good to decide how much disk space you will require for
your operations on that operating system. Once the file systems have been created, and filled with
important data, it is difficult or impossible to resize them.
/boot within 1024 cylinders
Older BIOS machines struggle to access data beyond cylinder 1024. If the Linux kernel and the
boot loader are stored in a partition above the 1024 cylinder point, it is possible that the BIOS may
not be able to load the operating system at all. To avoid this problem, create a boot partition,
around 16Mb to 32Mb large which is entirely below the 1024 cylinder point.
The filesystem for the boot partition is usually set to ext2fs. LILO can have trouble with reiserfs
partitions if the notail option is not provided when the filesystem is created.
/var, /home and / partitions
It is possible to install an entire Linux installation in one partition. This partition is mounted at the
mount point /. It can be useful to create a partition for /var to limit the size to which variable data
can grow (particularly spool files). If the disk becomes full, the system can become non-functional,
so limiting the impact to the /var directory is beneficial. A fair size for /var is 200Mb to 500Mb,
depending on the application(s) that will be used. For similar reasons /home is often given its own
dedicated partition.
Swap space
The rule of thumb9 for swap space is that you should configure two times as much swap space as
you have RAM. Once you have filled up all your computer's memory twice, things aren't going to
be working so well, and having some swap space available allows you to continue running without
causing application failures.
I I I T e
ana$ym8lmE
I I T e H I R F e R k R
m8lfW68af@a0ajE
d H I I R e R d H I Q Q F U I I R
0iih0gfw0(!S6%!a$!PawE
U S W S f T W r
P 8 0
W Q U S W S Q
PFg 0
W Q S r
P% 0
9.2.2 fstab
Once you have created partitions, you create filesystems in them using mkfs.
9 Uncle Ed's rule of thumb says you should never use your thumb for a rule, because you'll either draw skew, or you'll
hit it with a hammer.
9 Partitioning disks
57
When your system boots, the filesystems listed in /etc/fstab are mounted automatically, and become
part of the system. The contents of /etc/fstab show the file systems that are available on your
computer.
B
e
2
8%$
8%$
8%$
8%$
`
`
`
`
G $ C
7Eh
f
f
f
G $ C
7Eh
$ 6
"
&
% 3
e
$
j
$
f
$
l
$
3 @
6
3 @
6
3 @
6
3 @
6
W W T
@4e
U f f T T Q U
@%y"7EW P8@%yf 0
W W T
@4e
f i
p @%yf 0
Q U
f f Q U
@U%y"7EW H @%yf 0
@U%yf"7fEW H@U%y 7"hie
f r I f
7P`%g QV2r'8`fIB@U%yf"7fEW `68QBQ76T 5De
T
W
W S ` S S
W W T
@4e
f i
p @%yf 0
Q U
U i W U V I f
@8@8 7"hi 0
U V f I f
@'% 7"hi 0
After this, the /dev/vg devices are available for formatting and using as filesystems.
9.3 Review
Quiz questions
Design a partitioning layout for a mail server which acts as a file server as well. One of the
concerns of the system administrator is that too much incoming mail must not cause the file server
9 Partitioning disks
58
system to stop functioning, and that filling up the file server should not cause the mail server to stop
functioning. The disk to be used is 12Gb in size and has 1467 cylinders.
Answers to quiz questions
1. /dev/hdc
2. It is faster, and it can be used when without a filesystem being mounted beforehand.
3. When you have an old or buggy BIOS. It should end before cylinder 1024.
10 Boot managers
59
10 Boot managers
Marvelous! The super-user's going to boot me! What a finely tuned response to
the situation!
fortune database
In order to run Linux, it must be loaded into memory. The program that loads Linux into memory
is called a boot manager.
Read up on editing files with vi (maybe run vimtutor) in preparation for this chapter.
Data area
Microsoft operating systems (FDISK /MBR in MS-DOS) install a partition sector loader which
does the following:
Check which partition is marked as active.
Load the sector from the start of that partition and run it.
10 Boot managers
60
If you install a Linux boot loader, you will usually install it on /dev/hda. If you install it on /
dev/hda1 (the first sector of the first partition), then you require a partition sector loader that load
the code on your boot sector.
Master boot record
/dev/hda2
10.2 LILO
LILO is the (almost) original LInux LOader. It does not depend on you using a specific filesystem,
it can boot Linux kernel images from floppy disks, and it can act as a bootmanager for other
operating systems.
LILO works something like this:
You create the configuration file /etc/lilo.conf
You run /sbin/lilo
/sbin/lilo maps out the sectors on the disk that contain the data needed for booting (the kernel,
the configuration options)
/sbin/lilo maps out the locations of the disk that contain the data needed for booting (the kernel,
the configuration options). This map is stored in /boot/map.
/sbin/lilo installs the boot loader on the disk, and configures it with the location of /boot/map.
One of the important consequences of the way LILO works is that the location of data on the disk
may not change after /boot/map has been created. This means that if you modify the configuration
10 Boot managers
61
in /etc/lilo.conf, install a kernel upgrade, or rename files and directories, you need to run /sbin/lilo
again before things work as expected.
10.2.1 /etc/lilo.conf
lilo.conf determines the configuration of the boot loader installed by /sbin/lilo. Here's an example
of the contents of /etc/lilo.conf:
$ #
@$ 8%@ 86@$ 8%&
& 5
X 5 $ #
b8 8!@"
G $ & 5 G $
7E@ 87E&
$
3 @68
6 5
86%$ `
# 5
5 &
yh 6%'
G &
8G
T Q V Q R R W U
% EV F V 0
' 5 5 ' 5 # G G
% w 6y687$
# ! $
7%
m ! # 5 #
8&'X ! XX e %8 86%
8!87
# 5 $
m X ! 9 # 6 5 $ &
! X e E& 86@E'
C# A !87
5 $
$ # G
%8@
3 @% 3
$
6 5
The first part of the file contains items which apply to the entire LILO configuration:
prompt this means that LILO presents the boot up menu, even if nobody presses the Shift key
during booting up.
timeout=50 50 corresponds to 5 seconds. This allows the user 50 seconds to choose a menu
item, or to change the kernel command line.
default=linux if there is no response for the 5 seconds, the image defined below at label=linux
is loaded into memory.
boot=/dev/hda the first stage boot loader is installed here (as the Master boot record on the first
IDE disk). If this is replaced with /dev/fd0 then /sbin/lilo installs the first stage loader on the
boot sector of the floppy disk. This option can be overridden by running /sbin/lilo as lilo -b /
dev/fd0.
linear This causes LILO to use linear sector addresses instead of sector/head/cylinder addresses
to pinpoint the location of the data files. While this is slightly more reliable with older hardware,
the system may not boot if you install the disk in a different computer (with different BIOS
behaviour). For large disks in combination with an older BIOS, /sbin/lilo may generate
references to parts of the disk that are inaccessible when the system is booted.
These global parameters aren't quite as exciting as the others they could have been left out, in fact,
as they are unchanged from their default settings:
message=/boot/message the contents of this file are displayed when the system boots. In more
recent versions of LILO, this file contains the instructions for the graphical boot menus used by
many distributions.
map=/boot/map this is the file in which the locations of the kernel, and LILO itself are stored.
Generally one does not change this.
install=/boot/boot.b This file contains the data which /sbin/lilo installs in the boot sector.
10 Boot managers
62
This is the part of the file that specifies the loading of a Linux kernel:
' 5 5 ' 5 # G G
% w 6y687$
# ! $
7%
m ! # 5 #
8&'X ! XX e %8 86%
8!87
# 5 $
m X ! 9 # 6 5 $ &
! X e E& 86@E'
Occasionally, you will want to return to the state of your system as it was before LILO wrecked it.
Happily, LILO keeps a copy of the original sector of each device you install it on (except perhaps
for your floppy disks). If you ask LILO to uninstall itself, it simply copies this sector back to the
device, and the system returns to it's original state.
g i Q R
'R
0 db `
ca
You can also uninstall LILO by reinstalling the boot loader of another operating system. To
reinstall the default boot loader for DOS / Windows 95..ME, the command is
Apa s
b o
If you install LILO on a floppy disk, that floppy disk is able to boot only the computer from which
it was installed. Installing on a floppy makes it possible for you to boot Linux without interferring
with the operation of other operating systems:
S T W S I i Q R
@"4'R
0 db `
ca
10 Boot managers
63
The -b command line option overrides the value specified for boot= in /etc/lilo.conf.
C# A !87
5 $
$ # G
%8@
3 @% 3
$
6 5
Most operating systems install their own boot loader at the start of their data partition. Once this
code gets running, the system will start up as designed. All LILO needs to know is which operating
systems can be booted in this way.
If configuring LILO seems hard to you, you can configure boot.ini on a Windows NT system to
load the Linux loader from its partition. If you are using Windows NT, you probably know how to
do this. You must just be sure to install LILO in a Linux partition, rather than all over the master
boot record.
10.3 GRUB
GRUB is the GNU Grand Unified Boot loader. GRUB provides a number of functions which PC
BIOS programs do not:
Provides fully-featured command line and graphical interfaces
Understanding partitions (both DOS and BSD style)
Understanding most file systems
Booting of Linux, BSD and Multiboot-compliant kernels (such as GNU Mach)
GRUB understands enough about partitions and filesystems to find its configuration file grub.conf
without using a map file. GRUB can also boot systems without a configuration file, if the operator
is sufficiently skilled.
10.3.1 /boot/grub/grub.conf
This is the configuration file for GRUB.
$ & # ! G G ` ` !
E"@ B` E&
9 X
%& G
` 5 G $ 5 5
` 8& 79% w 6P X m X
&
8'X X m
X { ! m X e %#8 84%
X
! #
$ & 5 G G
E# @ `
{ ! XX e !#E& 6 8Fv
m
9
#
r j x 3 q
m
G7$Dr X m X { ! XX e q 84$"F4
#
}
X 3 "G'% 8hr j x 3 qx6@E' 3 "'
$
5 $ & $ G
6%'
5 &
6%$ `
5
V U S W S S T W S W S
D@"B 0 H"@ `
c $
S T W S ` T Q g R Q Q I ` S
@"68E7"@6T 5S 0 H"@ `
c $
That will copy the kernel image to the floppy, and set the root device encoded in the kernel to /
dev/hda3. The floppy disk is a bootable kernel image. The only problem is that many Linux
distributions require a initial root disk (usually /boot/initrd), so this method is not universally
10 Boot managers
64
successful.
Quiz questions
1. What must be done after editing /etc/lilo.conf to make changes take effect?
2. What is the difference between LILO and GRUB?
3. How would you go about booting up a Linux installation where no boot loader has been
installed?
Assignment
1. Change your boot loader from LILO to GRUB. Install the boot loader on your first hard disk can
create an appropriate configuration file. You should have a rescue system handy when you do
this.
2. Install LILO as your first boot loader (e.g. on /dev/hda), and configure it to load GRUB (e.g.
from /dev/hda3), which in turn loads Linux. This will give you experience in configuring both
GRUB and LILO.
Answers to quiz questions
1. Run lilo
2. LILO records absolute sector addresses for the files it loads, while GRUB understand how to
read various filesystems.
3. a) Provide a floppy disk with a kernel image, and supply the correct root device using rdev. b)
Boot off a floppy containing GRUB, and enter the correct parameters to locate the kernel and
root device. c) Boot off a distribution or rescue CDROM, but specify a specific root device on
the kernel command line. d) Boot a rescue system, mount the installed system (e.g. mount /
dev/hda4 /mnt), and change root to the mounted system (chroot /mnt) and then install a boot
loader on a floppy disk or on the hard disk.
65
LPIC topic 1.102.3 Make and install programs from source [5]
Weight: 5
Objective:
Candidates should be able to build and install an executable program from source. This
objective includes being able to unpack a file of sources. Candidates should be able to make
simple customizations to the Makefile, for example changing paths or adding extra include
directories.
Key files, terms, and utilities include:
gunzip
Uncompressing a file using the gzip algorithm
gzip
Compressing a file using the gzip algorithm
bzip2
Use the bzip2 algorithm to do compression
tar
Program for tape archives, and source archives
configure
What you run before make
make
What you run after configure
U V i Q f Q Q T i U t $ w
y rb@E E @4%'Pq"@"
U V i Q f Q Q g t $ w
8s y rb@E E @7 E@4q"@"
U V i Q f Q Q T i U t $ w
8s y rb@E E @4%hPq"@"
If the file is bzip2 compressed, you can use bunzip2 or the -j switch to tar.
X v $ # G $ # G
y8%4H ` 8'94"8@B 3 H ` 8%$ `
X $ $ 6
y8"7%"% ` @"P 3 ` B%@ ` F"87% 8H ` 8F~ X w @ w u
&
# G
C
| u 5
s x X X $ $ 6 # & # # $
$ &
p 3 9 ` 8'pxz~ w s x s bq 8"7%"F%"6 H# @ H 1
G x r X X X #
X X X ( | u 5 u X X X t 1 s
ix w @ w Fi( 7"
# X a
@ ` 8Bb@$ E|
X &
y@E' ` F8%2 EDB87HB@%" 3 F8 @ ` nb @ ` v
# v # $ & G $ $ $ v $ G
# #
R W i i W g T Q V
7%'@% e
The behaviour of GNU autoconf configure scripts can be customized by passing command line
options. Here are some highlights from the help of your average configure script:
`
`
`
`
`
`
Most source code is distributed with at least some documentation, and often with too much
documentation. Usually a file named INSTALL or README is included in the tarball, which
describes how to compile the program and the required software and hardware.
Often a program will be distributed with a Makefile. This file contains instructions on how to
compile the program. make relies on these instructions and, in turn, runs the compiler to convert
the source code to binary code.
GNU programs are usually distributed with a script named configure. The task of this script is to
determine the correct contents for and to create the Makefile so that the software can be built.
One of the problems during building software is that often the required libraries and header files are
missing from your system. These need to be installed. On rpm based systems, the -devel package
usually contains the appropriate libraries. You can, of course, download and install the required
libraries too.
If you download a package from the internet, you may find that the following commands produce
an installed version of it for you.
v
%P!
X v
b%
r Q Q W r U e ca
@"B@82db `
If you can't get used to the idea of using cc directly, you can get make to do it for you:
$ j f l
E @f
X v
b% e a e
h
E @f
$ j f l
$ l l h
E '
X v
b% e a e
h
v h l
%4a 4e
v $
t
v $
t
! ! ! C ! C
%EE!
% !
v $
t
v $
t
!
C C
t r Q Q U i f R e ca
@"PR 4B2db `
V@"4@"B7D2db `
r Q Q r Q Q Q i V V e ca
v $
t
v $
t
! ! ! C ! C
%EE!
V r Q Q U i f R e ca
@"PR 4B2db `
66
Source code packages for other Unix systems often work with Linux. However, occasionally there
is a change in a directory name, or a file name which causes a compilation failure. The file to edit
# #
C # v
` T W i i W g T Q V
878'@% e
f g U f T Q V
7b% 2 De
If you have previously run configure, you often have to remove the cached results to get it
to work correctly. (Note that the output of configure is a few pages longer than shown
here).
` T W i i W g T Q V
878'@%
The default installation prefix is /usr/local, so the programs end up in /usr/local/bin, the man pages
in /usr/local/man, etc. You may prefer to install with a different prefix.
X # $ & $ # $ # $ #
y"88%'%P@"# 3 E'% F48%%7H ` B7 3
C & $ G # $ $ #
G
#
Hdb @ ` v FE8 3 H 3 B%8@BF8"7%"F@8 3 B@E|
$ &
6 $ $ 6
#
X X x $
G G o
~ w %s s
%97 " B@"#
$ # $ #
#
$5#48%%7H87$ 3 8 ` 9%97 " !nbdy%@ `
$ 6
! X X x $
v #
~ w 8
#
$
%@ ` " s
G &
~ w s
a $ $ 6 # & # # $ # # &
y8"7%"F%"6 P ` H%~
(
E885 # w
! $ # ! ! $ &
"7%4H%'$ hq
#8@H#H 1
#
! $ # !
@"7%!
| 1w %# ( # w 5w | w "7%!
1
! $ # !
r 5 1
E# % | w
| w "7$ !
1
! !
a $
y8 %8G
$ #
| w %HP
1
# #
t 5 # !
()%8 s 8 ` Ps
# t
$ 3
" %%!
t 5 # !
(x#"'% t s 8"7 HEp@E'
$ # & $ &
" %"'!
t 5 # !
( # t
x" s 8"7 H@"
$
" %"7!
a # $ $ #
y8%H@"H 3 ` 2%B
# # #
X C # G
8"8@B 3 B@"kz584
x #
X } e
5 ` 7! v %@"5 `
G !
# $ #
x ` G7!! v "kz87" v $ 3 H 3 ` 4@"5$
# $
#
G # $ $ # #
` 8'5 3 zdY78@87" v "@87" v
G # $
X $
x # $
#
48 ` 3 PB@"BEvz@"HE& v b%$ ` 9
$ $ # C $ # v $
x
t
( s
#
48 `
#
48 `
# # G ! $ $ #
8@ 3 %B@"
( $
87"
# # G # ! $ $ #
8@ 3 %B@"
# $ G
B7 3 3 97"'
$ G
t
s
t
s 5 ` 7 !
G ! !
t
s
t
s 5 ` 7!
G !
a # $ $
y8%H@"# t
G ! !
7 3 nx 3 !
a % @ ` s
# $ #
11 Installing from source code
67
Find, download a source package of your own choice, and install it on your system. If you cannot
find any other download and install the game rocksndiamonds. You can install any libraries
Assignment
1.
2.
3.
4.
5.
6.
What is the sequence of commands for unpacking and installing a GNU program from source?
Why must make install be run as root? Are there exceptions to this?
Why should configure and make generally not be run as root?
What options to configure are used to change the installation directory?
How does one add an include directory to a Makefile?
Which file is generated by configure?
Quiz questions
11.6 Review
After these changes the program compiles with the new settings. A side note for this particular
program, it was necessary to run xmkmf before running make, which was explained in the
INSTALL file (quite similar to configure for programs using autoconf).
t |
j 5 7~ w
# $ & 5 1 w s
EFq} @w
# 5 t
"Py " t
$ 5 t ~
@87" By "1 %
t |
j 5 7~ w
# $ & 5 1 w s
EFq} @w
# 5 t
"Py " t
& $ 5 t ~
8%'87" By "1 %
And while we are at it, we'll install in /usr/local/bin rather than in /usr/games (which
doesn't exist on my system). So this ...
t s
vXb b@88'S " u w ~ HG B 8
X t 1 t
) e 5
t
X t 1 t
b@88'S " u w ~ HG @s ~
) e 5
t & $ 5 t
@888%'8 $ 3 %7" By " u w ~
t
vXb b@88'S " u w ~ HG
X t 1 t
) e 5
t
X t 1 t
b@88'S " u w ~ HG
) e 5
t & $ 5 t
@88788%'87" By "
s
B 8
@s ~
u w ~
The file /usr/games/lib/jetset/jetset.score.lock is not the ideal value, and we would prefer to
use /usr/share/games/jetset. The file to edit is actually the Imakefile, but we can do as well
by editing Makefile, and changing this ...
8G # t )dz xbE&
( X $ $ a v $
X $ $
bdz v ` xbE&
#
a v $
X $ $ X $ $ !
zBdzBB!
' o t & $ '
6@88788%'87" ho
5 t" u w ~ D%b b@88 @88788%'87" h% B 8 !
! ' o v X X t t & $ ' o 5 t s
1 s w
8A !
'6 b@88 @88788%'87" h% @s ~ !
o
X t t & $ ' o 5 t
s
u !
~ !
s
h
5 1 s | 8 !
s | ~ 7t '~ 5 s | ~ % 5 s | ~ @ !
s !
s
s
j m
{ 5
s | ~ t ~ % 9 { f { % s | ~ 7" t ~ % ! @f ! !
#
#
%8 j 8 7" t !
G G j m 5 G & j m $ &
87"7! j 8%7E'! @f 85 3 %E'! e B8
!
W r U
@8e
68
69
required by the application from the -devel packages that ship with your distribution. Write
instructions on how to install the application from source on your system, including a list of library
software you installed.
Answers to quiz questions
12 Shared libraries
70
12 Shared libraries
Weiner's Law of Libraries:
There are no answers, only cross references.
fortune cookie database
r q e b@X 878B5 e b@X 878
X
# !
X
# !
e
r q j b@z"7878B5 j b@z"7
X X
X X
e
r m q e b@z87878B5 e b@z87
X X
X X
e
r q e b@Y7E87878B5 e b@Y7E87
X X G $ &
X X G $ &
f U I I S S R e ca
h%8 7"PB2db `
The left hand column indicates what the program would like to be linked to (usually no
path name given), and the right hand column shows what the program will actually be
linked to (full path) (often a symbolic link...)
% % !
@X f { X e X e "7878 e j
! e
G h j l e f
@~ 8
! !
C
Q f V i V I R I R i f R e ca
' Pp y "7E 7EDR 4B2db `
j b@z"7878 l a
X X
C C C
' 7E 7EDR 4B2db `
Q f V I R I R i f R e ca
@X f { X e X e "79!!
!
12 Shared libraries
71
12.5 Review
Quiz questions
1.
2.
3.
4.
5.
6.
Assignment
1. Find where the glibc library files are store on your computer.
2. Install either Mozilla or OpenOffice.org. Find the dynamic libraries that are loaded as part of the
application and determine whether these are in the regular library path. Which environment
variables are set during the startup of the application, and what is the actual executable file for
the application?
3. Download and install the a binary of a program dynamically linked against libc5 (or a version of
glibc that you do not have) and make it work. (You will have to download the libraries it links
against, and make them work.)
Answers to quiz questions
1. This makes it possible to use new programs together with programs which use older libraries.
2. Including the code from shared libraries would make them bulky, and the system works perfectly
well with the code being shared.
3. This variable can be set to the name of a library which overrides functions from the standard
libraries.
4. It's the search path for the system libraries. It determines which directories are searched when a
dynamic executable is loaded.
12 Shared libraries
72
5. /lib contains libraries required by the system commands in /bin. /usr/lib contains most system
libraries. A complete list of additional directories is in /etc/ld.so.conf, largely for X and its
applications.
73
The
Here's how it looks in practice. The particular FTP server in the example lists Debian Linux i386
and Debian hurd-i386 packages.
j
@dX m@f X e ! e X j X e E'86 e GEe
& !
! ! ! ! ! C
%%E!
h
# # # $ $ G
BB8@ w y
h l e
j x
r l { x E x x x j { q %D87 $ s % e
6
# # l e
f R
H@ `
G
h
86 E@@8"@D B s X B yEe
# $ & G # $
#
S V G
8 BP@ `
74
j m
@dX @f j
j m ! 3
@dX @f 8 j
8dX j
9 `
%X ` dX j
j m
@dX @f X e
8dX X e
9 `
%X ` dX X e
j m
@dX @f j 8X
!
!8X j
!8X j
!8X j
!8X j
! eX j
! eX j
! eX j
jX e
X e 86
X e 86
X e 86
X e 86
X e 86
X e 86
X e 86
& !
E'86
e
e
e
e
e
e
e
e
e
e
e
e
e
{
{
j
j
j
{
{
f f e
G w %e
$ e j f
7E j e
G w { h
l f
G w 8j e
z l h j
# yj ' e
z e e
# 4j
z
# Gyh {
e f j h
G w 8hEe
!%%E!
! ! ! ! C
!%%E!
! ! ! ! C
!%%E!
! ! ! ! C
!%%E!
! ! ! ! C
!%%E!
! ! ! ! C
!%%E!
! ! ! ! C
!%%E!
! ! ! ! C
! ! ! ! ! C
%%E!
Reconfiguring packages
After a package has been installed it is usually configured automatically by apt (see further on). If
the configuration is incorrect, dpkg-reconfigure reconfigures an installed package. dpkgreconfigure is most often used for reconfiguring the X server, or the mail server.
13.2 apt
apt maintains the cache of packages that can be installed.
apt-cache search searchstring Show information about packages containing the name given
apt-get update Connect to the apt sources listed in /etc/apt/sources.list and determine the
current versions of cached packages.
apt-get install package install a package listed in the cache. The dependencies for the package
are also installed.
apt-get upgrade upgrade all out of date packages to the current versions as known from the
cache.
For complete information on apt, read to the man pages for apt-get and apt-cache.
13.3 Review
Quiz questions
75
Assignment
76
LPIC topic 1.102.6 Use Red Hat Package Manager (RPM) [8]
Weight:8
Objective
Candidates should be able to perform package management under Linux distributions that use
RPMs for package distribution. This objective includes being able to install, re-install, upgrade,
and remove packages, as well as obtain status and version information on packages. This
objective also includes obtaining package information such as version, status, dependencies,
integrity, and signatures. Candidates should be able to determine what files a package provides,
as well as find which package a specific file comes from.
Key files, terms, and utilities include
/etc/rpmrc
rpm configuration file
/usr/lib/rpm/*
rpm database of installed packages
rpm
rpm command line tool
grep
grep is useful for searching output of rpm -qa
77
R U S W f i
8 'pWp 0 db
ca
rpm -U will install a package if it is not currently installed. If it is installed, it will be upgraded.
rpm -F will upgrade a package provided it is already installed.
Here's an example of upgrading sendmail on a SuSE Linux system
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
{
( 0
C #
7 a
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
{
( 0
X X X # $ G
%"@ s
V i i Q f i
b@SyhB' p 0 & G @a "
$ $ $
e f j
& G X @f X m ! e X E7 hh a { @ 8@f
j m
! C #
6
! ! ! ! ! C
%%E!
V i i Q f U i f
b@SyhFR 4R 0 & G @a "
$ $ $
r X e 7s t # q j b@z"7
X X
r X e 7s t # q j b@z"7
X X
j b@z"7
X X
e b@X
X
# !
87"T Wp D2da "
i e c $
h
hh ! e X e 8"G `
! #
T i e c $
87"9Wp D2da "
X $ t #
zH4@"48@"n 3 3 d@E7p A
x & ~ C #
3 H ` 7%Fp7 X 9 3 B$ 3 @ B 3 "7 ` " s
# C #
#
#
#
$
3
$Hny8dz$ ` 79F%B48"@B@57 1
# $ ~ # $ x x
C # # $ # # $ 6 G C #
a 87% 8
# G
X 3 4E@% s ` B@8B@"2@E7p A # w a
$ & 3
$ & ~ C #
E~
$ & &
C # $ t c #X # X C C C a G
87 @%@%bd@Y@ 3 a
|
!%8b$ 3 z%Y@ 3 " z%# t b"F a
$ 9 & X X $ 9 a G
X
x $ }
$ v
@%$ s
j $ e @ { e
m
B
7t @vx 1 ~ w ~ j e a h a Be @~ % 1 x w 78 w ~ a
f e e G f
} ~
$
%# @~
1 a #
7t vb@"8
{ a
e m
9
~
& G z%7X m ! e X E7 s 4%~ 3 7% # 8%@"& w a
X
! C #
a
G $
# &
G #
& X GX ` $ a }
b$ 3 X ` 8Y ` db@88F%
r $ # #q a $ $
E@"HubP@"# t
{ a hh a Be w j e %vb$ %
e e e
m a
# a
$
@
e X a
X
z%# t b"F a %u
x $ }
#
#
"%u
r $ $ #q a # $
E"7Puy"
C #
7 a
&
%'$
V i i Q f i
b@SyhB7%Wp 0 & G @a "
$ $ $
78
79
e
! j X X m E@
! $ & #
Before the downloaded update is installed, we check whether it was signed by SuSE's
packaging key, and then install it with rpm -F.
X X X $ & # # $ G
zE@@ ` E'%@28|
X X X $ & # # # $ G
zE@%@ ` E'%@28|
00000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
$ & #
E@
6@i b@8 'f 0p 0 db
i R U S W i
ca
e
B h %v& G X hh X { ! j X X m E@
G
& a
j m
! $ & #
6@i b@8 '5r 7p 0 db
i R U S W f f V W V i i
ca
v C $ C C #
%D@H%%B7 p& G
!
0
@iSyih47p 0 da "
Q f W i
c $
m ! e X E7
! C #
Q f i
h4Wp 0 da "
c $
X X X X
@@%@FnX
G
` @F 1 X
X X X
G $ # G
7%@F 1 X
X X X
#
%G 8@F 1 X
X X X
&
%@@ ` 'p& G F2da
u !
e c
|X
rX
h z~
X
h z~
X
h z~
X
$
"
14.5 Review
Quiz questions
1. Which command will list the files that will be installed from the package bogus-10.1.rpm?
2. Which commands will enable you to identify the author of lilo given the file /sbin/lilo was
installed as part of an rpm package?
3. Which flag for rpm -q specifies that you wish to query an installed package by specifying a file
installed by that package?
4. What is the configuration file for rpm?
5. What does the rpm database of installed packages contain?
80
1. Find out which rpm package owns the file /bin/cut. Read the package information for this
package. What other files are included in the package?
2. Verify all rpm packages that are installed on your system. Did you personally make all the
changes that are listed?
3. Use rpm from the command line to install a game that is available on your distribution media.
You should find a game for which a number of libraries are required. Find out what the
dependencies for the package are, and install packages to satisfy the dependencies as well.
4. Download an updated RPM for a package you have installed from your distribution's FTP site.
Check the signature and checksum of the package you downloaded, and upgrade the installed
package to the downloaded version.
Answers to quiz questions
1.
2.
3.
4.
5.
6.
7.
81
UR 4R
i f
WV8'f
T T Q
R U
8
f
R
Here are a couple of examples of commands.
Commands
S
0 )P ` w
(
S
V 0 )$ 3 9 ` w
(
$ 3 %7"
S
0 )$ 3 9 ` w
(
V 0 )$ 3 9 ` w
S
(
$ 3 %7"
S
0 )$ 3 9 ` w
(
V 0 B ` w
S
(
$ 3 %7"
S
0 B ` w
(
VQSWU8hfi'fgPV 0 )P ` w
S
(
S
0 )P ` w
(
The default prompt on some systems only displays the last part of the working directory.
$ 3 %7"
S e a
2$ 3 %7" b
X 22$ 3 %7" b
e a
$ 3 %7"
S
2$ 3 %7" b
e a
XX 24 $ 3 %7" b
e a
$ 3 %7"
S e4 $ 3 %7" b
a
$ 3 %7" 422db
e ca
t &
8 % 3
S e ca
2db
`
`
`
`
`
`
If the prompt ends with # then you are logged in as root. If the prompt ends with $ then you are
logged in as a regular user.
An important concept on the command line is the working directory. The working directory can be
changed using cd (change directory) and displayed using pwd (print working directory). On most
systems, the working directory is displayed as part of the prompt.
File names, by default, are relative to the working directory. Commands like ls and du display
information for the working directory.
0 ()D"7"w
$ $
0 78"zE&
$ 6 a $
e c $
2da "@ `
0 8"b87$
$ 6 a
0 da
c #
The prompt in bash is programmable. The usual values tell you the name of the machine you are
using (sometimes your user name too) and which directory you are in (either the last part of the
directory name, or the whole path name):
The prompt and working directory
82
83
Each command that bash runs may be passed a number of optional parameters (arguments). Each
argument is separated from the next argument by a space (or spaces, or tabs).
Command
argv[2]
la
ls
argv[1]
/bin
Most commands take switches, which is a command line argument which starts with a dash.
command -x A single letter command line option tells a command to behave slightly
differently. For example, to ask ls to use the long format, the command is ls -l. Some of the
more commonly used one-character flags are shown in the table.
Flag
-l
-v
-f
-r
-R
Meaning
Long format
Verbose
Force
recursive
Recursive
Used with
ls
cp
ln, cp, mv, rm
grep
chown, chmod, ls
-xyz For most command which accept single character flags, you can join the flags together in
any order. As a result, the following are equivalent:
--option Many commands allow you to specify options in a more understandable format
(although more keyboard intensive). A common and useful example is --help. Another common
option is verbose.
filename Most commands expect you to specify the name of a file or directory to work with.
These commands include cp, mv and cd. Remember that ../ means the previous directory, ./
means the current directory, and ~/ means your home directory.
U i f R e ca
R 4B2db `
U i i f R e ca
@2R 4B2db `
`
8s r 8D%F2db
U f g V U I T i U e ca
8s fgr U84ip7 8!57R F2db
U
V I W R T i
i i f i i U e ca
I U f V U T V i U e ca
7"8s gr 8ID7F2db
7"I8s U fr V8I4RTip7 8!i'fEI WhiW W7F2db
g U W i
i
W Q i
U V i i U e ca
7R%'i 2db
W i
e ca
t U i i e ca
2W'8VW 7 p D2db
t i e ca
G p D2db
R R U i i f R e ca
@4B2db
U i f R e ca
@4B2db
f W V W f i T Q i
EE'87 7 7AEE'iV
Q V i Y f W V W f W
`
`
`
`
`
`
`
`
D2db `
V e ca
$ 6 5 w w
b"% % 't w u
Other applications which use environment variables for customising their behaviour include less
(the pager), ssh (secure shell), cvs (concurrent versioning system), make (for compiling programs),
man (system manual pages) and startx (uses the WINDOWMANAGER environment variable to
choose the desktop system to use).
#
8%B#
$ #
@P 7
G
R e ca
%7B2db `
b P D2db `
Q W e ca
R
E7` b 2db `
e ca
#
8%B#
$
@ 7
G
R e ca
%7B2db `
$ 6
H"P 1 t s
b A57% b 4 D2db `
t f W g R U
Q V W e ca
The behaviour of many commands can be customised by setting (and exporting) shell variables as
listed on the man pages of the application.
The lpq command shows the print queue for the current printer. The current printer is set
using the PRINTER environment variable.
5 t
X f 8@! f 787" "1
#
8 ` $ 3 % 8 % 3 % t ~ #
vX t & 5
v X
% t8 % 3 az%@!% P7s B 1 #
&
v v 5 ~ t
5 &
% t ~ 1 ~ t }
# w
3 x # ` 7E& xb@E7$E& x azE @E& "5 w
$
! &
# $ 6
3 ""%7~
$ # 5 }
& 5
8@ 1
$
9sXbXb b ` % 1 ~ }
X 5 w
# w } ~
t s
hh 5 78%1 7~
t |
@$ ` 5 w } "1 B
S U W
D2db `
W e ca
The command env displays a list of all the exported environment variables. These are the variables
that are passed from the shell to applications when the applications are executed.
e ca
2db `
~ w B w } ~
B~ @s ~ 7~
u u t f u u Q V W e ca
% %%6A2% %%69 D2db `
u u W f g e ca
% %%6H'2db `
$ v $ ! & # G ~ w B w } ~
"G 7! 3 %# 3 "8@@ 8787" DB~ @s ~ 7~
u u t f u u Q V W e ca
% %%6A2% %%69 D2db `
Y ` e ca
%8 2db
S e ca
2db
S V e ca
D2db
U ` e ca
%8 2db
W g%D87"@9 I%4Q@T5 VUW'f 7
f f W
ER D2db
W e ca
pqPysD!%2db
t
g p ` e ca
} 7 P!%2db
} ` e ca
t Q V W e ca
% D2db
u D2db
t Q V W e ca
t Q V W e ca
D2db
Q V W e ca
4 D2db
D2db
u t Q V W e ca
4 D2db
u Q V W e ca
f f W W f e ca
ER 'H2db
`
`
`
`
`
`
`
`
`
`
`
`
`
`
84
85
15.5 $PATH
When you enter a command, bash searches for the executable program in a number of directories.
The places bash searches are specified in the PATH environment variable.
# # #
# $ #
"@87" a " j 8 7" a "7" a "a "@" % 3
&
D2db `
u t Q V W e ca
One of the places that bash does not search by default is the current directory usually. Some
distributions do include the current directory in the path (spot the difference).
# #
a " 8 % 3 a "@87" a " j 8 7" a "7" a "
# t & # $ #
P4da
u t Q V W
c #
3 ""b% % 3 a
$ # a $ &
S f f U W
y%8V
3 ""b% % 3 a
$ # a $ &
S f f U
y%8V
15.7.1 -r switch
A common switch is -r which means recusive. To understand recursion, you must first understand
86
recursion. Here are some commands that support this kind of recursion:
chown -R change ownership
chmod -R change file mode (permissions)
cp -r copy files and subdirectories.
rm -r remove files and subdirectories
grep -r grep in files and directories.
15.7.2 cp -r
Here's how you can use cp to copy a directory:
Y W i
V P7p V {
This will create a copy of etc in your home directory. Of course it's not a very good copy, since the
permissions will change, and symbolic links will be updated. Often what you want is cp -a (archive
copy) instead of cp -r.
G X @"4@5 3
! # C
! # C
8@8@y8@y2G X @"4@5 3
G X @"B 3 4%$ X `
# C
#
' ' 1 ) # C !
$hGwG X @"B 3 B nX `
#
Using xargs will fail if there are filenames containing whitespace (i.e. spaces, tabs and newlines)
The last example uses shell globbing and will miss all hidden files and only go to three direcories
deep.
chmod is very similar to chown in recursive usage:
g e q %8 7T
Q S Q V f U
S
c
%g e q B 7p 7T
Q S Q V V W W i
S
g e V
Q
i S Q
15.7.4 grep -r
grep includes an option for recusively reading the contents of each file in a directory. There are
other ways to do this as well:
}c } R R g W S } V Q } W V W W i T W i W S
yl" % % y( q 7H9@V P7T
V % y7p
W } V Q } i
W
The first example of using grep -r will fail if there is a FIFO in the /etc directory, unless some
application helpfully closes the other side of the FIFO after grep opens its side.
Using grep with find is safer, but to force grep to print out the names of the files it finds, you need
to specify /dev/null as a dummy file to search.
%89@V P7T
U V f U T W i
W S
h W T W i S Th U
V 9@7u V
c
% B 7H9@V P7T
U V V W W i T W i
W S
87
Using the parameter -type f to find ensures that we only consider regular files. Of course, find can
use many other criteria to identify files, such as their name, permissions, modification date and
more. find is discussed more fully in another chapter.
W I f g V W
7" 7"'P W
# $ & & # # $
E5@E&
# $ & & # $
E5E&
The following sections are used in the Linux man page system
Section 1 User Commands (important commands like ps, ls and ln)
Section 2 System Calls (for programming)
Section 3 Subroutines (for programming)
Section 4 Devices
Section 5 File Formats (like passwd, hosts_access, shadow, inittab)
Section 6 Games
Section 7 Miscellaneous
Section 8 System Administration (like fsck, ping and reboot)
Section 9 Kernel
Section n New
For introductory use, sections 1,5 and 8 provide the most useful information.
15.10 Review
Quiz questions
1. What command will bash attempt to run for the following example:
W W R W Q R R W
@2B7%4% {
2. What is the command to create an environment variable called EXAMPLE and set its value to
"Hello there"?
3. Why will the following code print a blank line?
} W R U W t } Q V
"@8 @ W
} W W Q R R W } `
2%yP8 u g
4. What is the effect of having a "." in the PATH environment variable, and why is this not good
practice?
5. How do you set an environment variable (MYNAME) to contain the contents of a file
88
(/etc/HOSTNAME)?
6. Write a command that will copy the files and subdirectories from /etc to ~/etcbackup.
Assignment
1. Create a directory named testdir in your home directory. Copy /usr/bin/vi to ~/testdir/myeditor
and run it from the command line without modifying the PATH environment variable. Now add
the directory to your PATH environment variable, and verify whether you have modified the
PATH environment variable correctly by running the command myeditor.
2. Use the echo command to show the current value of the SHELL environment variable. What
happens if you run exec $SHELL? (Use echo $$ to show your current process ID before
and after running exec).
3. Set the variable HELLO to the value Hello world. Run a sub-shell (i.e. run bash). Is the value
of HELLO still set? Exit from the sub-shell and run a command which will make HELLO
available to sub-shells.
4. Modify your ~/.profile so that the output of the date command is stored in the shell variable
LOGIN_TIME. Log in at the console and verify whether the variable was set when you logged
in.
5. Read the bash man page. Pay special attention to the SHELL GRAMMAR and QUOTING
sections.
Answers to quiz questions
1. bash runs the command hello with the first parameter help the second me and the third
type .
2. The command is
} W W Q R R W } `
2%yP8 u g
} u t } Q V
a 7 W
}h u
n V F aP8
W U Vh } ` u
g V U W Y W U i
r 8I V 7V D@p V
16 Text filters
89
16 Text filters
cat > /dev/null
16.1 Introduction
Text filters are commands which receive a stream of data in, and send out a stream of modified
data. The filters either select, sort, reformat, or summarise the data that they receive. Text filters
are at the heart of UNIX shell scripting.
When using the text filtering commands, some of the more interesting files to play around with are
these, which you are fairly sure to have on your system:
/etc/services list of TCP/IP service numbers
/etc/passwd list of users on your system
/etc/protocols list of IP based protocols
/etc/profile default login script
/etc/inittab configuration file for init.
16 Text filters
90
Here's a brief rundown of the commands which will be discussed in this chapter.
cat concatenate files (or just show a single file without alteration)
cut cut chosen text out of each line of a file and display it.
expand expand tabs into spaces
fmt reformat a text file with a consistent right margin
head show the first few (10) lines of a file
join join lines of two files on a common field
nl print the file with numbered lines
od octal dump of a file (or hexadecimal).
paste print a number of files side by side
pr format for printing (split into pages or columns and add headers)
sed stream editor (search and replace, append, cut, delete and more)
sort sort in alphabetical order (and numerical order too)
split split a single input into multiple files
tac print the lines of a file from back to front (backwards cat)
tail print the last few lines of a file
tr character translation (e.g. upper case to lower case).
unexpand convert spaces to tabs (unlike expand).
uniq remove duplicate lines from a sorted file
wc word count (and line count, and byte count)
C # # $ # $ $ & G
ED%P"G P@8HPP579"'9@ 7 $ 3 84G
&
G
$ G # & & # t $
@"FP%H7 h %PH 3 ` %& ` "G BH"'pp7"H
# $ G & & & v $
T g Q f p t fp S W f I W f R g R i
k"'hi'7"B q8 W F%Wp De
Effect of redirection
f
m
%7pG @p7E8gz%H ` e a m e a Pl
$ G $ & G a $
G
e
m
%7pG @9 %kz%H ` e a m e a Pl
$ v a $
G
e
m
%7pG @9 E'pz%H ` e a m e a Pl
$ a $
G
Q R Q Q I Q R U S U W
E%P 0 db
ca
w
w
w
`
3
"WRTDWRTP"H D5 db
W R T U V e G & ca
3
W R T U V e G & ca
H D5 db
W R T U V e G & ca
"H D5 db
`
3
3
3
`
W R T W W Q V W e G & ca
2q2 D5 db `
W R T Q R R W Q V W e G & ca
"2q%4 D5 db `
cat can also join a number of files together. This works for text files and binary files.
y
j y
7%%C X 8@88@8'%" ` p
v # # $ # G $ 6
! &
6 # ! # #
h
# ! # #
H"% 8787" H@8
!
yyy
{ e
{ h
@!
0
# #
7 3 %& X
# $ x v # x $ # x
nY7%%C X nY7dX nY7% 3 X 54887% 3
# # G v
0
0
t
5 1 Ew
#"C# 787" a #"a #"'%a "'%7" a "7" } @s
# # 5 1 w
3 %"%7~
# 5 }
I U Q V W U V e ca
@ V F D2db `
cat prints out an entire file.
cat - concatenate
Effect of redirection
command1 | command2
16 Text filters
91
a & $a
a
$
da e a e a
#
a "a a
a a
75g y%8V P
i V i V S f f U W S U W
f a $
a &
a %
# & $
a a "
#
a b
a
0 db `
ca
Cut can be used to select certain columns of the input stream. Columns can be defined by either
their position, or by being separated by field separators.
cut can select a certain set of data:
cut pull out columns
tail can be used to watch a file as it grows. Run the command tail -f /var/log/messages on one
console and then log in on another virtual console.
tail -n 20 file or tail -20 file will show the last 20 lines of a file. tail -c 20 file will show the last 20
characters of a file.
e 3 yE 8G f X X X
h e
&
` B ` @" F8@ w i( @e y 3 9 ` a f a 4l G w
v G G
a j f e
e
&
"6 @
xbt5@"B ` @ 8@pa e % '"gi( h { e P ` @f a { e a 4l G w
$ #
# & $ G a
& v
l
r f X X X q f X X X ` ni( @%e y f @87H ` a e a 4l G w
&
# # a j f e
G G
m
r f X X X q f X X X ` ni( %%e y f @87H ` hh a j e a 4l G w
&
# # a h f e
G G
e e
h
r X X X q X X X ` ni( m e y f @87H ` h a j e a 4l G w
&
# # a
G G
e e
r X X X q X X X ` ni( j e y f @87H ` j a j e a 4l G w
&
# # a
G G
& $ #
%'%P@8 3 X X X
m j
8H# t 86 H$ s i( h P ` a e a 4l G w
$ #
# # #
a h & v
3 P"6
$
j
h
l a hj a e ab$a m a { X X X HB %@s s } b87 3 B ` %f a { a 4l G w
# s w
a G
3 P" l a j a ba m a
h
e a $
$ 6
j
& ` { X X X ` 1 %6 s s } b87 3 B ` f a { a 4l G w
~ |
a G
X
b ` 8@HF8@ { A b87 3 B ` f a { a 4l G w
$ $
a G
f W U f f W Q R U R U
EEE% 0 b ` w
a
tail is just like head, but it shows the tail end of the file. Quite often it is used for viewing the end of
a log file:
tail show the end of a file
l
& e a 4l
e
G w hh
! ! ! ! ! C
%%E!
I U i f
FR 4R 0 db `
ca
I U S W S
Fy d 7 0 db `
V i S U W
ca
One can also use head to extract an exact number of bytes from an input stream (rather than lines).
Here's how to get a copy of the partition sector of a disk (be careful with that redirection).
6
@
$
$
8
#
"
m ea
m ea
h
%f a
h
Ee a
{ a
m
Pl
m
Pl
2
4l
h
B e
G
G
#$
#
$
@
w e { m
w m
l m
zG m e
zyj {
j {
4 j
! !
% %
C
% %
e
! !
C
% %
h
! !
C
% %
f
! !
C
% %
e
! !
C
e f $
e
i S U W i f R e ca
'R 4B2db `
head can print out a specific number of lines from a file or a stream.
`
`
j l
hh a e
j ea m e
l
a e
l
a e
l
a e
l
a e
l
a e
`
jhh
jhh
jhh
jhh
j
hh
`
`
m
a Pl
m
a Pl
m
a Pl
m
a Pl
m
a Pl
m
a Pl
m
a Pl
G
G
G
G
G
G
G
w
w
w
w
w
w
16 Text filters
92
W R T W t i U U V e $ # C ca
"FA%8P D2% db `
e h
$ ! $
"G { a 4l
G w 'i j e
&
&
! ! ! C ! C
%EE!
m l e
$ ! $
"G { a 4l
G w j f
&
&
! ! ! C ! C
%EE!
m l e
$ ! $
7"G { a 4l
G w j f
&
&
! ! ! C ! C
%EE!
m l e
$ $ ! $
"G { a 4l
G w j f
&
&
! ! ! C ! C
%EE!
t i U U i f R e $ # C ca
%8FR 4B2% db `
i U U i W U f i
%88s ' y"78@"@ @9y%'5@"hH2% db `
Q U r V I i R f e $ # C ca
Here's how to use cat to recombine the parts (and using md5sum to check whether the
whole is equal to the sum of the parts).
e h
$ m a 4l
G w 'i j e
&
&
! ! ! C ! C
%EE!
m l e
$ m a 4l
G w j f
&
&
! ! ! C ! C
%EE!
m l e
7$ m a 4l
G w j f
&
&
! ! ! C ! C
%EE!
m l e
$
$ m a 4l
G w j f
&
&
! ! ! C ! C
%EE!
t U i f R e $ # C ca
SFR 4B2% db `
U i W U f i
8s ' y"78@"@ @9y%'5@"hH2% db `
Q U r V I i R f e $ # C ca
9 $ j
%X dX hh X
j h
e
! 9 # ! & # l m e
%%$ "$ f a 4e h%f
&
&
! ! ! C ! C
%EE!
U i W U f i
8s ' y"78@"@ @BR 4B2% db `
Q U U i f R e $ # C ca
Split can split files into more manageable parts, e.g. for FTP uploads. The parts can be recombined
with cat.
m { e
l
l j
hh
$
{ m {
{ f
e
e
$ j e
3 $ h e
e
m
@f
j
j
l
l j
@f
j
j
@$ m e
f
e e
f
j
j
` $ m e
e h
'%f
j
j
$ e
h l
Ee
j
j
$ e
f
m
j e
j
j
$ %e
e j f
f h
%f
j
j
7$ @e
m h
f
j
j
$
$ h e
t V e ca
S42db `
f W V W f W
EE'iV R 5@"hH2db `
i R f e ca
Split can be used to split a file into multiple parts. If you have a desire to print /etc/services you
may want to print it on a printer which prints 66 lines per page.
split split a file into multiple parts
e
a m zE&
a $
a a ` %8G
` C
a l b$ 3
a
a j a 3
# C
a h z%'
a #
l Y7
a a G
a &
a f $
e a e a %
# & $
a a "
#
a b
a
V T i S i V S f f U W S U W
i rHp75g y%8V P 0 db `
ca
cut can also select based on fields which are separated from each other by field separators. This is
useful in dealing with files like /etc/passwd where each field is separated from the others by a
colon.
$ e
E& a a
a a a a
$ 3 a a
a a ja a
%'a a
#
$ 6 a G
"Y7a
m a zE&
a $
` C
` %8G
l a b$ 3
a
# C
3
h a z%'
a #
l a Y7
a a G
16 Text filters
93
l
l
{ { m
f h h j e
h l j h h
h f
4hh hh { ypyh yBiyh y5%yh yh yh B{ 2 m
{ Bhh yh m l f e @HH5f { 2m 2j Ee F844l 22
j
m f l f e f
e e e h
f e
e
T g i Q f T i S i V S f f U W U V e ca
k"'5'Pp75g y%8V F D2db `
Here's the same command pipeline, but we are removing duplicates with uniq before
formatting.
{ { m m l
j e
h l j h h
h f
fhh4hh hh { ypyh yBiyh y5%yh yh yh B{ 2 4l l { j
m l f e @HH5f { 2m 2j Ee F844l 22BBBBB
m f l f e f
e e e h
f e
e
T i Q f T i S i V S f f U W U V e ca
k'5'Pp75g y%8V F D2db `
hh
yh
i Q f W Q S c Q R R W
'5' k%2 u Bkf e dD@ d dD7
t Q V W Q S c e Uc Uc ` U
f e h
3 'Ee
m
3 e e
j
3 { e
3 { m {
j m h
3 y
m f
3 @8 {
3 m {
m
3 j {
l j
3
f
3 @e
Q T e ca
@42db `
sort by default sorts in alphabetical order. This gives slightly strange results as shown
above. To sort in numerical order, use the -n switch.
Q f W Q S c Q R R W
' k%2 u Bkf e dD@ d dD7
t Q V W Q S c e Uc Uc ` U
l
3 {
j
3 j m
f
3 8m
l l
3 l
f
3 e { j
h e h
3 j 'Ee
f h
3 %f {
e m
3
f j
3 l { 8
e
3
4{
Q T e ca
@42db `
# $ $ 6 &
"F89@PB% 3 @%db% 3 %
X &
#
#
7H ` o @8B EDD8@PFB"'9@"798@ ` 9H7"B"6
& # & G G # G G & $
%& ` vD22db `
$ e ca
$ 6 G & G #
"47"2B8@ ` B@"7"'9DG
G # & & #
8@B ED ` o @87H%9@%db% 3 % 3 9@PB84"
# X &
&
6 $ $ #
%& ` kD22db `
$ e ca
tac reverses the order of the lines in the input stream. This is quite useful for sorting backwards.
tac the opposite of cat
16.4 Sorting
e
9%X dX hh X %%$ "$ m e ` 8m j { " l { $
$ j
! 9 # ! & #
e l e f j
$ l
e
` # m e ` 8m j { " l { $
C
e l e f j
$ l
U i W U f i
8s ' y"78@"@ @H"Bhf 2% db `
Q U W R T W g S e $ # C ca
16 Text filters
94
{ { { {
a # # a G G $ 6 a G a a G
a a
a l a { a l { zwa 8 # "'%b878@8'%"Y7a l a Y7
a
{ { { {
a # # a & $ $ 6 a & $a
a a
a l a { a l { zwa 8 # "'%8"a f a $
a
a &
{ { { {
a # # # # & $
a a
a l a { a l { zwa 8 # "'%a "'%a %da e a e a a %
a
# & $
{ { { {
a # # # #
a a
a l a { a l { zwa 8 # "'%a "a "a a a a "
a
#
{ { { {
a a
a l a { a a
j a w } 3 3 % 3 %| l | e a 3 ""b ba a a b
e
6 w
C e } e $ # a a
a
S U W Q S U f W S f f U W i
8hiV Py%8V vp7%Q q 0 db `
ca
Here we are joining /etc/passwd and /etc/shadow based on their first field (the user name).
Since /etc/passwd and /etc/shadow use a colon to separate fields, it is necessary to use the
-t : option.
C # C $ # & # $
%P%ED"G
! $
$ 3 H 3 E'%H
C $ & # $ $
6 # $ 6 $ $
887$5 4"7 8F@"7
S
W Q' WT9SW 'y"57%Q F2db `
f
R
Q f W R T q e ca
%C
# C
# $
"G
$ 3
! $
3 C
$
6 $
887
# $ $
4@"7
SW ' H D2db `
Q f W R T U V e ca
$ #
%E&
# $
"G
$ #
%E&
$
$ & #
E'%$
$
$ 6 $ $
"7 8F@"7
S Q f W R T U V e ca
W 'y"H D2db `
join is capable of combining files based on common fields (similar to the SQL inner join, if you
happen to know SQL).
join join files
f f W
ER g %U 47 v B 2'p@82db `
Q W b u S i
U V i U e ca
tr -d can be used to make normally readable text rather unusable, but it does show that
vowels are not as necessary to the English language as you might have previously
suspected.
g S U Q f Q S S U Q p p S i
Y7" 8 W FG E 8 W gXs7 F2db `
e ca
Here we are using tr -d to delete the Carriage Returns (\r) from a file created with
Windows Notepad.
f f W i u i i U i i u i U U U V i U e ca
ER hEE'8@EDE08H v@8P 2'p@82db `
ROT13 encryption12
W Q S c i U i u } b t } Q V W t } b t }
kfq84E4 va @ %a @
Q S c t
kw7 @4@4e s E@b `
b Q T
C # C a
f f W i u i U U U V i U e ca
ER E8H v@8P 2'p@82db `
f f W i U i u U U V i U e ca
ER 84E4 v@8P 2'p@82db `
tr is usually used for converting upper case to lower case. It can also do other character translations.
tr -d can remove specific characters from a stream.
Translating from UPPER CASE to lower case. We are asking man to use cat as its pager,
instead of using less, and opening up the man page for man itself.
tr character set translation
16.5 Manipulation
16 Text filters
95
G
7H86 3 5 3 H%& 3 G 3 H 3 " e
# C
$
$
pXSHW pS'"5k7" B2db `
W f R i T I f R e ca
3 8y'"6
l
9 $ 9 &
j
"6B@ "P%'%P%P
G & $ # # &
3 H"77H86 3 5 3 H%& 3 f
G G # C
G 3 H 3 "F%'%@" e
$
$ & $ # $
v C $ $
'B@X 3 $ 3 $ 3 %$
p p S W f R V i T
XS '"%5k 7" B2db `
I f R e ca
You can also use i at the end of the s command to make the search case-insensitive.
d delete the line. You need to select the lines as explained in the next paragraph.
|
z p 3 b z "}
C
x
pD@P%y'8mpgXj"y D2db `
W Q S W fp S W f } S W f Q S W } Q V W e ca
|
p 3 b z "}
C
x
p@P%y'8mpgXj"y D2db `
W Q S W fp S W f } S W f Q S W } Q V W e ca
$ C
8Fp 3 b z "}
x
p W Q S W fp S W f } Q W U Q S W } Q V W e ca
@P%y'ag P5gXj"y D2db `
sed is a stream editor. sed does text transformations on an input stream. sed works by making only
one pass over the inputs. A sed program consists of one or more sed commands which are applied
to each line of the input. A command may be prefixed by an address range indicating the lines for
which the command should be performed.
Here is a very abbreviated summary of the sed commands
s/PATTERN/REPLACEMENT/g search and replace. If you add the g at the end, the search
and replace is applied as many times as possible to a single line.
(So as you can see in line 8, the CD ROM in this system is mounted at /mnt/cdrom.)
B
f
8%$ `
B
8%$ `
G $ C
7Eh
B
8%$ `
`
G &
B
8%$ `
G
B
f
8%$ `
&8 @
# &
& 6
8@ m
w w % w l
1 5
$ $
e 3 @ j
G $ C
7Eh
$
6
& 3 %@
6
#
# h
G
#
#
88G@
6
#
# f
f 3 @ e
% 3
&
$
6
5
% w
I U f T W R e a
@78"V "4b `
Ever wanted your printouts to be numbered? If so, then nl is the tool for you!
nl number lines
#
@X
join allows you to specify which particular field to join on, and also which particular fields should
appear in the output (similar to cut)
The script below prints out the permissions (from ls) and file type (from file) for each file
in the current directory. The 9th field from the second file (-2) is used for the join this is
the file name. The actual output (-o) consists of the first field from .fileinfo (1.1), the first
field from .listing (2.1) and the second field from .fileinfo (1.2).
{ { { {
aaa l a { a l {
a
a a
a l
{ { { {
a a
a l a { a l {
a
a a
a
a m a zE&
a $
a l a b$ 3
a
a 3
# C
a h a z%'
a #
16 Text filters
96
# #
8 # "'%
#
"
#
"
#
"
3 ""
$ #
S U W V i S U W p p
D7y i%D@5@8 8sy vy%8V F D2db `
S f f U W U V e ca
expand allows you to specify the size of the tab stops with -t. You can specify a single size
for all tab stops, or you can set the tab stops at specific positions.
m
# # $ & G $ 6
8 # "'%FE@@8'%"
$
E&
l
$ 3 "'%
#
#
"'%
$ 3
h
# #
%'%"
#
"'%
#
%'
l
# #
8 # "'% 878@8'%"
G G $ 6
G
7
f
# #
8 # "'%
& $ $ 6
8"
&
$
e
e
#8 # "'%
#
#
"'% %
# & $
#
8 # "'%
#
#
"
#
"
3 ""
$ #
S U W S U W p p
@8 8sy vy%8V F
S f f U W
$
E&
$ 3
#
%'
G
7
&
$
# & $
%
#
"
U V e ca
D2db `
# a # G $ 6 # & $ $ e
a $
3 "#"b878@8'%"a %2% s a l a a @"7
$ a G G $ 6 # & $ # #
a
w ! G
3 ""a "'%a %$ a e a e a a @%%
$
# # # &
w ! # & $
3 ""a "a "a a a a @%"
$ # # #
w ! #
3 ""b ba a a a @
$ # a a
w !
h%8 'y%8V F D2db `
f U I p S W f S f f U W U V e ca
S U W p u i
yf
You can also use the line matching together with search and replace. Note that the replacement is
only done on lines containing 'bash'.
#
@"7H `
#
H `
6 H `
# #
#
7"784 `
# #
4 `
# #
` 4 `
$ # #
%4 `
6 $ #
@ 3 H `
G # #
87 3 @"H `
#
`
S U W p S Q T b I f g fp S W f
g @BC 7"' i' 7"'P742db
I f g S T e ca
t
t
t
t
t
t
t
t
t
t
`
Instead of using s/PATTERN/REPLACEMENT/ you can use any other character in the
place of the slash e.g. a colon.
p W U fc Q R Q V Q R Q V f
DyDg %%yp
@ D5
S W f } U f Q R Q V } Q V W e ca
'x5%h D2db `
The commands are most useful if you specify a range of lines to which the command applies.
Here's how to specify specific lines for the d command:
/PATTERN/d delete all lines contains the pattern
4d delete line 4
4,10d delete lines 4 to 10
6,$d delete from line 6 to the last line
The most common way of using sed is to specify the editing commands as a command line
argument. You can specify multiple commands separated by semicolons.
3 8y'""6
9 $ 9 & 6
f
8
16 Text filters
97
%P@82D%D%D%4DPE58 4E@
# $ # 6 & & # & & G & v & # v & v & $ & # v $
# v # v v $ G v C $ & $ #
5B%P%& 8 D"75'8@H%'%@8 3 7%H7 2H@
G 9 G 9 # $ G
2'8H@ ` @$ ` 96 9@ BB 3 FE@B%'%E
v C $ G
# G
v G & & $ # # $ &
& $ # # $ & # &
B%'%E@"28% ` BBB 3 H"775 3 H%& 3
$
G G # C
G 3 H 3 E e 3 " 3 "F%'%@"4'BE'%E'! EB@X 3 $ 3 $ 3 %$
$
$
$ & $ # $ v C $ $ & # & & $ $
T I f R e ca
k7" B2db `
16.6 Formatting
$ # & $
%E!
$ # & # $ G ! C # C # $
%ED"7%PF"G
$ 6 $ $ ! ! $
"7 8F@"7%$ 3 H 3 D
C $
$ & # $ $ 6 $ # $ $
E'%H! 8875 4@"7
W R T W R T p ip S i
"D7W 7%85 db `
f U e G & ca
You can specify a delimiter between the files which is not a tab space with -d
$ # & $
%E
%#ED"G
$ & # $
C # C # $
%PF"G
"7 86F@$"7
$ $
! $
$ 3 H 3 D
C $
$ & #
E'%$H$ 8875 4@"7
6 $ # $ $
wiS@U8 W"WTDW9W 7%85 db `
R
R T f U e G & ca
$ # & $
%E
$ # & # $
%ED"G
$ 6 $ $
"7 8F@"7
$ & # $ $
E'%H
"H D5 db `
W R T U V e G & ca
C # C # $
%PF"G
! $
$ 3 H 3 D
C $
6 $ # $ $
8875 4@"7
W R T U V e G & ca
H D5 db `
Using paste is like taking two printouts of two files and sticking the right margin of one to the left
margin of the other. The glue between the files is a tab stop.
paste paste two files together
m f
@8m e m e { f
f
l
hh
VS@U8 Wh%8P 2'p@82db `
g f U I U V i U e ca
m f m e { f
l l
f
l
hh
V f U I U V i U e ca
h%8P 2'p@82db `
If you have spaces and want tabs, you can achieve this with unexpand.
Here we are counting the number of lines, words and characters in the man page of bash.
After piping the output through unexpand and replacing spaces with tabs the size is almost
19kb smaller, although it looks the same.
unexpand spaces to tabs
$ e
E&
$ 3
#
%'
G
7 l
&
$
# & $
% e
h
$
E&
$ 3
#
%'
G
7
&
$
# & $
%
16 Text filters
98
l
m
e
f
m
l
m
e
Q R R W V S i S Q e G & a
%E 7"4D5b `
# o
l
m
e
f
}
m
l
m
e
Q R R W V S i S Q e G & a
%47"4D5b `
l
m
e
f
m
l
e
Q R R W S i S Q e G & a
%"4D5b `
od -t d1 specifies decimal format, with a width of one. The more astute among you will
realise that the character encoding is ASCII.
# o
}
Q R R W V i S Q e G & a
%44D5b `
Using the -t switch tells od to use a specific format type (the default is od -t o2). od -t c
means character format. You will notice that file ends in a newline character (\n).
h j j h e j
h e j j j l e h l j l h e
hiPP'EP' B hEB
Q R R W S Q e G & a
%4D5b `
od's behaviour is rather odd when it is used without any arguments. It prints out the octal
value of two pairs of bytes in the file. Sometimes this is useful, but usually it is not.
% A 8}
Q R R W U V e G & a
%P D5b `
Q R R W } S R Q Q R R W } Q V W e G & a
%Fj%P%y D5b `
od, oddly enough, does not just print out in octal, but in other formats.
od octal dump (and other formats)
fER R 4B2db `
f W
i f R e ca
f f W t f Q W e ca
ER 7EV P2db `
In the good old days, when it was traditional to send reams and reams of printouts to a line printer,
it was nice to add file names as headers and to make sure you didn't print on the fold between
papers. This is what pr does for you.
pr format for printing
# G v C $ G & $ # # $ & # # $
"4'8F%'%E@%P%P@8#
6 & & # & & G & v & # v & v
D%D%D%4DP&
$ & # v $ # v #
E58 4E@55B%v
v $ G v C $ & $ #
%& 8 D"75'8@H%'%@8 3 7%
G 9
G 9 # $ G
7 2H@ 4 2'8H@ `
v C $ G
@$ ` 96 9@ BB 3 FE@
# G
v G &
& # # $ & & $ # # $ & # &
%'$%EP B%'%E@"28%
$
G G # C
` BBB 3 H"775 3 H%& 3
G 3 H 3 E e 3 " 3 "F%'%@"4'$
$
$
$ & $ # $ v C
$ & # & & $ $
E'%E'! EB@X 3 $ 3 $ 3 %$
S U W i T I f R e ca
5k7" B2db `
$
9
%&'$%E82"E'%E& z""B@ "B%2@@ %P@ %
# # $ & G C 6 $ & # X 6 6 G v # # G # $ #
& $ # # &
%'%P%P 3 % 3 HP%'7p@@9@p@ 3 2%@8@
$ # G
$
#
` @9@ "6 D"6 H& G B%HPP%C EP"4'8F%'%E@%#
C
& & G G # G v C $ G & $ # # $ &
16 Text filters
99
100
# o
16 Text filters
j e
j l
j h j
$ j 4l ` 4'h e ` j j Bm
A
}
For each file specified on the command line, wc prints out the number of words, lines and
characters in the file.
dy8@8
# X
v $ X
"y8@8
Cy8@8
$X
8@8
# X
` b@8
$
3 @
3 @
3 @
3 @
3 @
t f Q W V e ca
7EV P2db `
e j
l'%f
h
j {
e
{ e
l
e j
h
j
hEe
j@f
f
f
f
{
wc -l prints just the number of lines, wc -w prints only the number of words and wc -c
prints the number of characters (bytes) in the file. These features allow wc to be used to
count and measure the output from other commands.
e
R R 4B2db `
i V
i f R e ca
Here we are using cat to show the contents of all the files in /bin, and then using wc to add
up the size of those.
e e m j m
l {
V i V t I U V e ca
7 7"F D2db `
16.7 Review
Quiz questions
16 Text filters
101
Assignment
1. Figure out something useful to do with join (other than join -t : /etc/passwd /etc/shadow)
2. Read the man pages for each of the commands discussed in this chapter, and make notes about
options and modes of operation which have not been described.
3. ls /etc/*.* prints a list of files and directories containing a dot in their name. Use sed to remove
the leading /etc/ from the filenames and format the list for presentation using fmt and pr.
4. Create a tabulated list of user names, home directories and shells from /etc/passwd. Use cut to
select the appropriate columns from the password file; use tr to translate colons : to tab
characters; and use expand to format the output.
5. Concatenate all the data in the files in /etc using cat. Sort the lines alphabetically and remove
duplicates. How many lines were there in /etc before you removed duplicates, and how many
were there after you removed duplicates.
6. The command ls -latr prints a directory listing in reverse order, sorted by time. Use the tail
command to show the newest 5 files from this listing. Do it again using the tac and head
commands.
7. Create a file containing a copy of /etc/services with the lines numbered. Split the file you have
created into parts of 1000 lines each named services-aaaa, services-aaab and so forth in your
home directory. Check how many lines are in each file.
Answers to quiz questions
1. head
2. tail
3. cut
4. cat
5. fmt
6. unexpand
7. expand
8. sort
9. sort | uniq
10.tac
11.tr
12.nl
13.sed
14.fmt
15.od
16.pr doesn't none of these commands do mpage does it though.
17.wc -l
17 File management
102
17 File management
Unix is all about files. Everything is a file. Because of this, it's important to be able to shunt your
files around.
Mommy, what happens to your files when you die?
fortune cookie database
! G $
8@"7"G
# G
"8@
dYG "7
X ! G
! G
PG "7
dX C"7
# ! G
PG !"G79!!5#!"7
C
G
X
X
R
gnSd
ef a
ef a
a
{ a
hh a
j
@f a
ee {
ee {
{ 2
{ 2
{ 2
f
F%
e e
4
g0iI
! {
! {
! {
! {
! {
f
! %
j
!
h
! i
!
e
e {
e
! ! ! ! ! ! ! C
E!
e
e2 m
e
! ! ! ! ! C
%%E!
e
e2j {
e
! !
% %
C
e e
2m@f% {
! !
% % !
C
% %
e e
2j {
e
! !
C
f%h
e
C C C
% %
f e
%Be { m
h
f
! !
C
f e
%2j { 4$ h
! ! !
! %
C
m $
j
U i f R
R 4H@b ` "
G G G a w $
V PP6db ` "
W S V ca w $
I R
!P@S6Q
eT H
yS%
Q T k d F e e I
n8!(wnl$6lWaE
In the output of ls, the meaning of the columns from left to right are:
file type and permissions (d directory, l symbolic link, r read, w write, x executable or
searchable)
number of links to the file
user name
group name
17 File management
103
The shell expands the following special characters just in case they are part of a file name.
Filename expansion is only performed if the special characters are in quote
~
echo /etc/hos?
[abc]
Any of a, b, or c
echo /etc/host[s.]
{abc,def,ghi}
echo /etc/hosts.{allow,deny}
G ~
@9 f e
! ! ! ! ! C
%%E!
G
@~9 f e
! ! ! ! ! C
%%E!
% %
j {
f
! !
C
S U i t i u f R e a
R BEw(B4b `
w b f @87Y dy8@8 3
a
G G a # X
G #
"@% ` w y8@8 3
a a C $X
WSQRRU "7E4y @4b `
f f Q u W e a
# z l h
$ 4'%f
! ! ! ! ! C
%%E!
j {
! ! ! C ! C
%EE!
@ e
! ! ! ! ! C
%%E!
$
E { e
e
! ! ! C ! C
%EE!
z l
% 4
! ! ! ! ! C
%%E!
t f Q i f R e a
7ER 4B4b `
C $X
y8@8 3 8@8 3 ` b@8 3
# X
dy8@8 3 "y8@8 3
# X
v $ X
dy8@8
# X
v $ X
"y8@8
Cy8@8
$X
8@8
# X
` b@8
34{ f a { Fe
f
3 a {
j h
3 i a P8
h
e f
3 % a B@f
f h
3 e e
f
s t e
& Xz~8 @" " e e
s t e
~
8 @" " e e
w @s a
l
t f Q Q V W e a
7E4 D4b `
t W S V e ca
PPD2db `
# 3 %@% 7% "X
& # $ G
7% "X
# $ G
# $ G
"X
f X
e X
X
X
S T e G & w v $ t
74y( B ` u
rmdir can be used to remove empty directories. If the directory contains any files and directories,
rmdir will fail to remove it (although rm -r can).
rmdir remove directory
0777
C
002
0775
C
022
0755
C
027
0750
!! %
! !
% %
! !
%
!
C
C C
000
C
B `
3 %79 3
a #
f
e
X
X
X
a Bm
a Bm
a Bm
a Bm
a Bm
# & # $ G
3 %@% 7% "X
7% "X
# $ G
# $ G
"X
f X
e X
X
X
S T e G & w v $ t
74y( B ` u
W Q I g W U i S r e G & w v $ t
87" 'fi hfi@ 8B'%y( B ` u
%@% 7% "G v BHna &
&
# $
$ # # $
v
87" 'fi Ihfi@ WU84%y( B ` u
W Q
g
S r e G & w v $ t
%
G w j {
v $
t
v $
t e
!
C C
%
G w j {
v $
t
v $
t e
!
C C
%
G w j {
v $
t
v $
t e
!
C C
G w j {
"@"
v $ e
t
! !
% %
C
G w j {
v $
t
v $
t h
!
%
C C
$
e
U i f R e G & w v $ t
R 4By( B ` u
V S S S r e G & w v $ t
S%%B%y( B ` u
S S r e G & w v $ t
"%B%y( B ` u
To make a new directory, use mkdir. By default, the parent directory must exist, but with the -p
switch mkdir will create the required parent directories too.
mkdir make directory
X
dX h % dz% @
X X
d@% dz% @
X
dX j % dz% @
8 h a { e
f
e
% e e
@ j {
# z j f f
y@e {
X
dX f % dz% @ dX % dz% @
X
X
dX e % dz% @ dX % dz% @
X
S i8u V P74B2db `
V S V W S i f R e ca
% %
m
! !
C
! ! ! ! ! C
%%E!
17 File management
104
HB `
U t W R T S T i
@P"@%Fp
X E ` H%P
# $ & # &
3 %79 z ` v
a $
U t W R T S
@P"@%2
e G & w v
y( B ` $ 3
e G & w v
Dy( B ` $ 3
G ! ! &
` Y7 3 p v 1
$ # # $ a &
@Hn
e G & w v
Dy( B ` $ 3
rm -f causes rm not to complain if the file you are trying to remove is not present.
` #
C
! C !
7% `
` C# 7% `
! C !
! # !
7
S f R e G & w v
@%By( B ` $ 3
U W R
@tPT"@%2 Dy( B ` $ 3
S e G & w v
$!%C `
!
! # !
7 `
$ ! # !
S f R e G & w v
@%By( B ` $ 3
rm removes files, and also symbolic links and other strange file system objects.
rm remove files
` #
C
! C !
7% `
$% `
! C !
! # !
7 `
$ ! # !
`
{ e
ff
ff
ff
f
f
a Bm
a Bm
a Bm
a Bm
a Bm
G
G
G
G
G
vt
$
vt
$
vt
$
vt
$
v $
t
w
w
w
w
v $
t
! ! ! C ! C
%EE!
v $
t
! ! ! C ! C
%EE!
v $
t
! ! ! C ! C
%EE!
v $
t
! ! ! C ! C
%EE!
v $
t
! ! ! C ! C
%EE!
$
R 4By( B ` u
i f R e w v $ t
` # { e a Bm
C
G w
v $
t
v $
t
! ! ! C ! C
%EE!
$
R 4By( B ` u
i f R e w v $ t
W R T W V e w v $ t
"B g Q Fy( B ` u
` # m e a Bm
C
G w
v $
t
v $
t
! ! ! C ! C
%EE!
$
R 4By( B ` u
i f R e w v $ t
WRT"B g Q Fy( B ` u
W V e w v $ t
$
R 4By( B ` u
i f R e w v $ t
S S V e G & w v $ t
%BDy( B ` u
S S r e G & w v $ t
%B%y( B ` u
touch was originally written to update the time stamp of files. However, due to a programming
error, touch would create an empty file if it did not exist. By the time this error was discovered,
people were using touch to create empty files, and this is now an accepted use of touch.
touch update a file's time stamp
G & w v $ t
X
w v $ t
` u
w v $ t
` u
w v $ t
` u
a %
&
w v $ t
` u
w v $ t
` u
a %
&
w v $ t
` u
S T e G &
74y( B
W U S e G &
84% Dy( B
I g f W U S e G &
hi@ 84% Dy( B
G & #
@8B2 b"G v
a # $
W U S e G &
84% Dy( B
87"W 'i hi@ 84% Dy( B
Q f I g f W U S e G &
G & #
@8B2 b"G v
a # $
W U S e G &
84% Dy( B
V S S S e G & w v $ t
S%%B% Dy( B ` u
S S e G & w v $ t
"%B% Dy( B ` u
17 File management
105
| $ $ # G
rz@X 3 8"@8X v %8@dY7
C 6 a G
# | $ # G
r 3 8"@8X v %8@dY7
C 6 a G
# | $ # G
r 3 %8"@8X v %8@dY7
C 6 a G
I Q V
7" 7" p DyxH ` u
t I i V e( v $ t w v $ t
cp -i interactively asks for feedback before it does something drastic, such as overwriting
an existing file.
'8"@8X v !n '8" v
v C $ # G
!
v C $ #
z$E&'%#E&'! E8"@8X v !nzE'%E'! E8" v
& $ # G
! $ & # & & $ #
z$@X 3 8"@8X v !nz@X 3 8" v
$ # G
! $ $ #
3 8"@8X v !n 3 8" v
$ # G
!
$ #
3 %8"@8X v !n 3 %8" v
$ # G
!
$ #
7"I 7"Bhp DyxH ` u
Q V
t I i V e( v $ t w v $ t
I Q V
7" 7"4 DyxH ` u
t I V e( v $ t w v $ t
I Q V S r e ( v $ t w v $ t
7" B%yxH ` u
cp -v is verbose about the progress of the copying process. In the first example below,
there is no feedback. In the second example, there's a lot of feedback.
C $
%h"G %h"7! ` "@
C $ G ! G
f R e ( v $ t w v $ t
ByxH ` u
S f f U i T Q i
y%8'87 By%8V 4 DyxH ` u
Q V S f f U W V e ( v $ t w v $ t
Here we are copying a file (/etc/passwd) and renaming it to copy-of-passwd.
C $
%h"G
f R e ( v $ t w v $ t
ByxH ` u
S f f U W V e ( v $ t w v $ t
y%8V 4 DyxH ` u
f R e ( v $ t w v $ t
ByxH ` u
cp copy files
Ydy
a G & a
3 ` $ 3
w v
` $ 3
w v
`
#
` $ 3
w v
` $ 3
w v
w v
` $ 3
r v B8@%k
|
6 & a &
rY7% ` v ` @8H% P8@%k
| ! C !
G & $ 6 & a &
|rY7! ` v ` @8H% P8@%k
! #
G & $ 6 & a &
|rb ` C# v ` @8H% P8@%k
G & $ 6 & a &
r v BH 8d
|
# # a &
S i
%D%7p Dy( B ` $ 3
e G & w v
rm -r can be used to remove directories and the files contained in them. Here we are using
-i to specify interactive behaviour (confirmation before removal).
17 File management
106
$ & X v
Ed@7%@
$ X v
% 3 "@7%@
$ X v
` G @3 "@7%@
$ X v
@3 "@7%@
d @8 w @7%@
X $
vX v
G v #
@78dX w @8 w @7%@
$
vX v
$
@8 w @7%@
vX v
vX v
@7%@
v
@7%@
w yh X h "7 @ $ 3 %7"
! G 9 #
s
17.5 find
rz$ 3 $ 3 %'% 3 $ 3 v %8@dY&
|
& $ # #
C 6 a 6
U U W U W S W U V f W V W f
8@8 @8 8@8 PEE'H i yxH ` u
e ( v $ t w v $ t
mv -i, like cp -i, interactively asks for confirmation if a file will be overwritten
C $ G
b%h"X v !nb%h" t v
! C $ G
Sy%8Q 9hi yxH ` u
f f U W q
e ( v $ t w v $ t
C $ G
b%h" t v !nb%h"@ v
! C $ G
Q q2Syff%8V Bhp DyxH ` u
W
U W i
V e ( v $ t w v $ t
bt v !nb uv
! `
W q S W T
Q 2@hi yxH ` u
e ( v $ t w v $ t
S W T S r e ( v $ t w v $ t
@9%yxH ` u
107
17.6 Review
c R R g W S
" % W %7E67R hp q 7Py@8'74e
U R T I
i
W V W W i } V t } W U i S T
g R V f f g S V
7E'i'PDe
Want to grep through the C files in the kernel? That /dev/null is there to make sure that
grep prints out a file name together with the data from the file.
R yl} %
i V }c
b W V W W i p V tp W U i S T
g y q 7pXy@8'74e 8@%7%7" b `
# a
g R V f f g S V e ca
7E'i'PD2db `
Have you ever wondered how many things need to be fixed in the Linux kernel?
#
7%"
% "
# & #
%@"
# & #
# G #
""
ee
ee
ee
e
e
{ e
f
f
e f
w 8 {
l
w
{
w j { m
%
!
%
!
%
!
%7!
! C
%7!
! C
%7!
! C
% %7!
f
e 4%f
# z e f h
! ! C
c S U i f R V W W i i W i I S T e ca
% R 45 7B8p 'p7"P742db `
When find finds a file, the default action is to print out the file's name. The one alternative action
that is quite useful is -exec where find executes the specified program, usually passing the file name
to it. find substitutes the name of the command in the place of the special characters {}. The
command must end with ; which must be quoted so that it is not interpreted by the shell.
find -exec
Test
Meaning
-name
-iname
& $ # ! #
%'%B4 `
-ctime
-mtime
-atime
h j o & ! & #
@f 4%'H% 3 4 `
H%'%'9 8"4 `
! & & ! $ 6 #
o & $ ! G & #
f 4%'4 `
-group, -user
-gid, -uid
!
! G & #
v H v %4 `
& $
! G & #
v E' 3 C v @"4 `
-perm
o & G
987! ` 8@B4 `
G ! #
! & G ! # #
@p875"7" 4 `
-size
o 9 ! #
v yh 4 94 `
-type
G G ! $ 6 6 #
F8@5"H@4 `
$ G
z6"7w
z 3
& $ # ! # # #
%'%5"7" 2"4 `
Example
find is usually used to search based on particular criteria. The more commonly used ones are shown
in the table.
find tests
v X v
%@7%@
17 File management
108
17 File management
109
1. Perform the following instructions, and make a list of the commands you used
1. Make a directory named stuff for the commands below.
2. Copy the following files to your stuff directory: /etc/passwd, /etc/services, /etc/profile and all
files in /etc starting with the letter h or H.
3. Copy the directory /etc/sysconfig and all of its subdirectories and files to your stuff directory.
4. Rename the file hosts to the name network peers.
5. Delete the files host.conf and nsswitch.conf using a wildcard (but don't delete other files
ending in .conf).
6. Make a list of the files in your home directory which are larger than 100kb using find.
7. Remove the sysconfig directory you copied earlier.
2. Ensure that you have a rescue disk handy before doing this assignment. Do not do this
assignment on a system containing valuable data.
Install the stand-alone shell (sash), and review its manual page, particularly the aliasall
command. As root, enter the command 'mv -f /*'. Now recover your system either use a
rescue disk, or run /var/bin/sash. You can pass the parameter init=/var/bin/sash to the kernel if
you boot from LILO, otherwise you will need to use a rescue disk. What happened? Should you
have known? Did you get some valuable experience with the mv command?
Answers to quiz questions
1.
2.
3.
4.
5.
-i
The user that ran the command.
-a
find -mtime +2
find -type f -perm -2001
18 Redirection
110
18 Redirection
Row, row, row your bits, gently down the stream...
/usr/share/games/fortune/computers
Redirection is that property of Unix operating systems that makes them quite useful.
Effect of redirection
h
%f
r b
7"PG
fEW75g A
R Q
W U W W
H
Q R R W
%F
d V e( v $ t w v $ t
d yxH ` u
Read input from the script or the command line. The << operator is traditionally followed
by EOF, but any other letters can be used. When a line is found which consists only of
EOF, that's the end of the input. This is called a here document.
e
e
j
j
V S f f U W U V e ( v $ t w v $ t
y%8V F DyxH ` u
Read input from a program using the pipe redirection. Here cat is piping its output to wc.
e
e
j
j
S f f U W
y%8V d yxH ` u
V e ( v $ t w v $ t
Read input from a file using <. Notice that standard input is anonymous wc does not
know the name of the file which it is reading from.
ey(xH ` u
v $ t w v $ t
m e
F
n8$I
X G
b87"G 3 "gb8@ t
$ x G
$H$ 3 F"% 1 "@FEC t 8%C
# C
X # G $
EB 3 b8@ t nb%C t b8@
# $ & C
x G
$ x #
X G
# $ G
H8@ t b4 3 B8@ t b8}
X #
G
X
V e ( v $ t w v $ t
yxH ` u
Read input from the terminal:
e
j
j
C $ G
%h"@ e
S f f U W V e ( v $ t w v $ t
y%8V PyxH ` u
Standard input is the terminal, by default. The following redirections are possible:
No redirection. Although we are telling wc to examine a specific file in this example, we
are not changing standard input using redirection
Effect of redirection
command1 | command2
18 Redirection
111
# $ # 6 &
$
S U W U U T V U e ( v $ t w v $ t
Y@8V PY@8D8BFyxH ` u
We can append the errors to an existing file ...
Q R U T Q V f f W U T Q V f f T V U e( v $ t w v $ t
b87" %DW2% 'iyiV Pb% 'iy98BFyxH ` u
R R g W S T Q V f f W U T Q V f f T V U e ( v $ t w v $ t
"DW2% 'iyiV Pb% 'iy98BFyxH ` u
Here we are sending the errors to a file. It is quite common to send error text to /dev/null
especially when you know what it says.
6 G &
"F""@2 ` 87B Hka
$
# # &
%H E s a 8@B$ s y8@%@ ` E'%@ a
# G # #
a ! $ #
# # &
%H E s a 8@B$ s y8"7878@ ` E'%@ a
# G # #
a $ G #
#
%
# &
E s a 8@B$ s %@ e G # 3 @ ` E'%@ a
# G # #
a G X # v ! $
#
# # &
%H E s a 8@B$ s bE@E'%# 3 @ ` E'%@ a
# G # #
a &
#
$
$
$
$
$
# $ # 6 &
$
T Q V f f W U T Q V f f T V U e( v $ t w v $ t
% 'iyiV Pb% 'iy98BFyxH ` u
stdin (0)
Shell command
stderr (2)
stdout (1)
By default, Linux processes produce two error streams. Regular output is sent to stdout (standard
output, 1) and errors are sent to stderr (standard error, 2).
V W g e ( v $ t w v $
78yxH ` t
Redirecting to a program. Here the output of uptime is being piped as the input to wc.
G a h
G { a h
w v $ t
` u
w v $ t
` u
$
x
W R T i W
"78 DyxH ` u
U V e ( v $ t w v $ t
` !%&'8G| { a lBm G w ej
v $
t
v $
t
! ! ! C ! C
%EE!
W R T i W
"78 R 4ByxH ` u
i f R e ( v $ t w v $ t
W R T i W
"78 q78yxH ` u
W g e( v $ t w v $ t
Redirection to a file (> means to). If the file already exists, it is overwritten.
e X x e X x e X b@%87H
e
a $ 6 $ $
x
y"@" j x j e a m G & G { a h
W g e ( v $ t w v $ t
78yxH ` u
Standard output is the terminal, by default. The following redirections are possible:
No redirection
112
C !
8E'"G ` 8887% %%# @ ` E'%@
G ! v C #
Q H Q T H e H U R F I H R
P0Sn!W%a@nS@jP(0SvX0WI
d d H Q Q d eH I H d d
nw06%SnXnljPS0R $PW%S0S6W0!6%$8gX0WI
e Q Q k Q R U d H Q Q I R
d d H Q Q d eH I H d d
nw06%SnXnljPS0R !!8SPiPWS6W0!6%$8gX0WI
Q I T H I R I Q U d H Q Q I R
d d H Q Q d eH I H d d
nw06%SnXnljPS0R !66WnS6W0!6%$8gX0WI
Q F R I e U d H Q Q I R
d
n
d H Q Q d eH I H d d
06%SnXnljPS0R 8!60a%!P8yg6W0!6%$8gX0WI
e d k I R eT d U d H Q Q I R
d d H Q Q d eH I H d d
nw06%SnXnljPS0R iS!S6%6W0!6%$8gX0WI
I Q Q d U d H Q Q I R
d d H Q Q d e
nw06%SnXnlH
I H d d
PS0R h!P8yg6W0!6%$8gX0WI 887% %%# @ ` E'%@
I R eT d U d H Q Q I R G ! v C #
#
@ ` E'%@
g W S S U W e( v $ t w v $ t
8I ByxH ` u
g W S g W S T Q V f f W U U T V U e( v $ t w v $ t
8I W28I % 'iyiV PY@8D8PFyxH ` u
The usage below is wrong, and because of this the results are quite strange. tar is opening
the file debug two times, and writing to the file is not synchronised, and some writes
overwrite others (the error output is in italics).
f f W R U W V U f e ( v $ t w v $ t
ER "W84 7HyxH ` u
f f W V i U e ( v $ t w v $ t
ER "WP7FyxH ` u
f f W U f W Q W S W i f U I e ( v $ t w v $ t
ER "Ww7E2r @@"7EV 4'ph%8yxH ` u
These commands send standard error output to the same destination standard output (a
program).
R R g W S R U S W f
"D8 'pS 7 '2"yxH
W
i R R U R R r e ( v $ t
R R g W S R U S W f
"D8 'pS 7 '2"yxH
W
i R R U R R r e ( v $ t
R R g W S S U S g e( v $ t
"PI W yxH
R R g W S $ ! e( v $ t
!2"W F42yxH
R R g W S & $ # # $ & & # e( v $ t
!2"W%'% ` E 3 %78FyxH
R R g W S T Q V f f W U U T V U e ( v $ t
"W"y% 'iyiV PY@8D8PFyxH
w v $ t
` u
w v $ t
` u
w v $ t
` u
w v $ t
` u
w v $ t
` u
w v $ t
` u
The notation 2>&1 means stderr(2) to copy of stdout(1). The following all send standard error
output to the same destination as standard output
command >file 2>&1
command 2>file 1>&2
command &> file
command >& file (this is not the preferred method)
command 2>&1 | another-command
Here are the examples of using these commands
Here we discard the output and the errors from the commands, sending them to /dev/null.
6 G &
"F""@2 ` 87B Hka
$ $
# # &
%H E s a 8@B$ s b 3 % d'"@ a
# G # #
a
X & $ G
$
& $ # & & &
8%'%B8%B ` v 9"@% a
# $ # 6 &
$
6 G &
"F""@2 ` 87B Hka
$ $
# # &
%H E s a 8@B$ s y8@%@ ` E'%@ a
# G # #
a ! $ #
$
# # &
%H E s a 8@B$ s y8"7878@ ` E'%@ a
# G # #
a $ G #
$
#
%
# E s a 8@B$ s %@ e G # 3 @ ` E'%@ a
&
# G # #
a G X # v ! $
#
$
%H E s a 8@B$ s bE@E'%# 3 @ ` E'%@ a
#
# &
# G # #
a &
#
$
& $ # & & &
8%'%B8%B ` v 9"@% a
# $ # 6 &
$
Q R U U V e( v $ t w v $ t
b87" %F DyxH ` u
R
Qb87" %WY@8V PY@8D8BFyxH ` u
U S U W U U T V U e ( v $ t w v $ t
6 G &
"F""@2 ` 87B Hka
$ $
# # &
%H E s a 8@B$ s b 3 % d'"@ a
# G # #
a
X & $ G
$
18 Redirection
113
18 Redirection
114
p i u i i U i p p i u i Up W I I
hEE'8@EyE08d v Ft
Q V W
You can pipe a large number of processes together. Here we are counting how many
unique words appear in the fortune cookie database.
R "'f
i V g Q
p i Up p i up p t c
i8dyqE vhyyf DB e E08@y'f
i u i U fp S W
U V f U p p i W t W g Q T f W U W U f f g f R
%8shp @"yE@8hi' Be
f e
m e
ls lists the files in the fortune cookie directory. grep -v removes the files that have a dot in
their name. xargs runs cat with all of the file names on its command line. cat prints the
contents of the files. sed edits the stream, replacing anything that is not A-Z and a-z with a
space, then removes leading spaces. tr converts everything to lowercase. sort sorts the
words in alphabetical order, and uniq removes the duplicates. wc says how many lines
there were in its input.
If you need to redirect standard error together with standard output, you use the 2>&1 operator.
In the following example, the output of bash -x is linked to the input of less (that's the
pipe). The errors are redirected to the output (2>&1). The regular output is redirected to /
dev/null (but the errors remain redirected).
f f W R R g W S U
ER "A"Ww7f
r @@"7EV 4'ph%8I 0
Q W S W i f U
tee divert a copy to a file
stdout(1)
tee
ls lR /
file1
Interpretation of
ls lR / | tee diverted > file1
diverted
tee is like a tee-piece for pipes13. It allows you to pipe to one or more files and also send output to
13 One difference between tee and a physical tee-piece is that the flow of data is duplicated, and not divided in two like
t ! t 1
% "s |
# # $
%%EC
! #
w
3 s 1
!@s } w
w 1 X @s
~ 1 7% s #
| ~
f R e ( v $ t w v $ t
ByxH ` u
W Q S
P
}iEui8UH va @ h%ya @ F
} b t } Q V W t } } b t }
Q
Skctw7 @4@4yxH ` u
b Q T e ( v $ t w v $ t
$ 3 $ 3 %'%# 3 $ 3 %h"7! ` "@
& $
#
C $ G ! G
8%78@
G
G #
@% 3 $
# G
"@
! & G
` %'8|
f R e ( v $ t w v $ t
ByxH ` u
dX'"G
$
& $
t
This example renames every file in the current directory to an upper case version of its
name. The command whose output is being captured is echo $FILE | tr a-z A-Z14.
h T V h W U S
5 "d@8 %De
h T V h W U W f U I
5 "d@8 '%8e
h T V h i
5 "dfT %Wp De
Command substitution makes it possible to grab the standard output of a command and put it on the
command line of another command. The two forms `command` and $(command) are equivalent.
Note that the quote is a backward quote this is a little difficult to distinguish from the regular
quote (apostrophe), so some people prefer the form $(...).
Rather than using /usr/bin/ftp we use `which ftp` below.
0
0
0
0
Here we are recording the results of loading a kernel module by reading the kernel
message buffer with dmesg. The results are also displayed on the terminal.
Q i Q W Q i R R U f U i W V i i i
R Er @FR 77H@W v"W( E2'"'p De
tee -a does not overwrite the current contents of a file. In scripts tee -a is useful for
appending the results of commands to one or more log files.
In the following example tee catches the output of grep in resultsfile, and wc counts it.
f W
fER 8 8 @%7%8r 84W vWp De
Q U Q T W U V U W U i
8 8 @%7%8r 8 d EBe
Q U Q T W U V U
f f W R
8 8 @%7%8r 8F@Wp De
Q U Q T W U V U U i
Q'fSW Q'4W vy%8V 4h%8B @e
f g W S f f U W f U I W
S Q f
W 'g d 'He
Q f
S Q f g S f f U W f U I W
W 'Fqy%8V 4h%8B @e
tee can be used to catch intermediate results in a command pipeline, such as unsorted data,
or data which will be passed to a pager.
f Q i W V W
EW g 7% g BW vW g 0
Q
T Q W i W V W T Q V
% V '% g BW v% @T 0
standard output.
By default tee works like the redirection > and overwrites any contents in the files specified. If
you want to append to the files, use tee -a.
tee can save the results of a command in a file while you view them on the terminal.
18 Redirection
115
116
# t w
~ % A
#
% !! 1 % 1
# t
w 1 X @s ~ 3 ~
z
w } w } } w } t
w 1 # s
18 Redirection
3 s
t !@s
~ w
A ~ @s ! 8 ! !@s
3 s
The grep and cut commands conspire here to find the name server which is in use. This is
then entered into an environment variable.
e X X e {
X m j
6 % De
u t Q V W
u
haTippypS7i5g VT% V'%Q'EWV WP'E@8B ri` 6 % e
Q R f
W W f W U W h
e X X e {
X m j
ippyp7i5g V% V'%Q'EWV P'E@8B @e
T
S
T Q R f W W W f W U W
e X X e { 86 @8%'%#
X m j
& $
T% 'R%'fEWV P'E@8B @e
Q V Q
W W W f W U W
e XX X e { 86 @8%'%#
m j
& $
& X G &
b"'$ 3 %@
$
T Q V R Q f W W U V
% '%'EV F De
18.7 xargs
xargs expands its standard input to be arguments to the command it is given.
The following commands are equivalent:
W W Q W Q
2@2 2
h W W Q W Q Q V W h
2@2 B nq
%82@2 B W
f U W W Q W Q Q V
xargs is often used to handle a list of file names generated by another command. Here we generate
a list of files with grep, and then read the files with less. The actual command run is less file1
file2 ...
V f U
%8
R R g W S t W Q Q i
"WqV FDR p
W
f f W R f U t W Q R R W i
EP%8V P%R p
W
xargs is frequently used with find. find finds the files, and xargs starts up the command that is
going to handle the file.
Here we find regular files that have not been accessed for more than 30 days, and removes them
P@4%87%'9V e
i f U i
%8V e
f U
W78 @H9@p8P7T
U i T W i S
W U i T W i S
78 @H9@p8P7T
18.8 Review
Quiz questions
1. What are the two methods to capture the output of a process and include it on the command line
of another process?
2. How would you send the output of a process to a file named output and the errors to a file
named errors?
3. What would you type in a script to have the standard input of the command defined in the script?
4. How do you send the output of one process as the input of another?
5. How do you send both standard output and standard error output to another program as its
standard input?
6. How would you use tee to capture the intermediate results of find /etc before sorting them with
sort? (Are there any differences between the unsorted and the sorted versions?)
Assignment
1. Create a list of the files in /var/log and write it to a file named logfiles. Edit this file using vi and
18 Redirection
2.
3.
4.
5.
6.
7.
117
remove /var/log/messages. Use the file listed in logfiles as the command line arguments for the
wc command, and determine the number of lines in the files. Hint: Use xargs.
Write a single command to sort the file /etc/passwd, write the sorted list to two files named
pwsorted and pwsorted2, and then write the last line of the sorted output to the file pwlast with
the command tail -1. Hint: use tee.
The command ls accepts a number of single letter command line options. Use the command for
LETTER in a b c d e f g h i j k l m n o p q r s t u v w x y z ; do ls -$LETTER ; done and extract
the lines from standard error where the string invalid option appears, using grep. Use cut to
extract only the letters which are not supported by ls.
Write a command to set the environment variable ISSUE to the contents of the file /etc/issue.net.
Write the output of all the following commands to the file lotsastuff. The commands are: date ;
id ; hostname ; df ; uname -a ; du -hsc /*. Check that you have captured the output. If you run
the commands you used again, how many copies of the output will appear?
Use the redirection commands together with the filter commands and extract a list of all the users
in /etc/passwd whose shell is /bin/bash. For each user, print out that user's name and home
directory. You can use grep to select the lines and cut to print out the relevant detail.
Work out a method to use tee and tail to pipe the results of a command to two processes
simultaneously. For the source command use ls -lR, and pipe this to grep hosts and wc -l. What
are the limitations of your method? Can the limitations be overcome, and how?
19 Process control
118
19 Process control
Zombie processes are haunting the computer.
BOFH excuse of the day
What is process control? Process control is the active changing of the process based on the results
of process monitoring. Of course, that is in the chemical industry. In your Linux system, it's quite
similar you want to talk to the processes running on your computer.
` f I R R g W S ` T Q Q W W S ` S S e ca
j8"68B76T 5D2db `
m
(
19 Process control
119
Q R R W W
y%B @F D2db `
Q S U g W S U V e ca
A 3 @ % @4
G & # $ 6 $
E%# @5 ` B%@5 ` H
5 6 9 6
% ( e
# # #
o
% h(
# # #
!
f
I Q F2db
q e ca
f m
8 ( e
3 @ % @4
G & # $ 6 $
T e ca
642db `
3 @ % @4
G & # $ 6 $
3 @ % @4
G & # $ 6 $
E%# @5 ` B%@5 ` H
5 6 9 6
68
e ca
2db
8@~ ( e
G G
o
# # #
% h(
!
f
I Q F2db
q e ca
G G
8@~ ( e
o
The command bg makes the most recently executed job continue to run in the background. Like
with fg you can make a specific job run in the background by specifying a job number, e.g. fg 2.
The kill command is able to kill a job based on its job number (instead of its PID) using a
percentage sign to specify the job number:
A 3 @ % @4
G & # $ 6 $
E%# @5 ` B%@5 ` H
5 6 9 6
E%# @5 ` B%@5 ` H
5 6 9 6
A 3 @ % @4
G & # $ 6 $
3 @ % @4
G & # $ 6 $
%E 1 ( e
$ # &
o
e ca
2db
r R R r e ca
x"2db
# # #
% ( e
o
f
I Q F2db
q e ca
%E 1 h(
$ # &
!
e ca
2db
r"2db
R R r e ca
% ( e
# # #
o
# # #
% h(
!
f
I Q F2db
q e ca
`
`
`
`
Notice that the shell prints out the exit status of jobs that terminate unnaturally, but it doesnt
always print this out immediately when the job receives the signal, and usually just before
displaying the prompt.
19 Process control
120
console, or via a network connection. nohup writes the output to a file named nohup.out, unless
you redirect the output yourself.
nohup is generally used when you know that the process you are going to run should continue to
run after your session ends. Quite often nohup is used to wrap commands which will cause your
session to end, such as network reconfiguration commands.
Restoring from a backup can take a while. We dont want to stay logged in while it
happens. Well get the output later by reading the file.
! & G $ 6
@ @824% 3 487@ ` 759G 3 #
6 ! $
If you restart the network while connected over a network link, the stop will work, but the
restart may fail.
$ v C X #
@8B%# db%8@HG 3 #
Here we hang up a phone line, wait a minute, and then dial again. This is the kind of thing
you will do when you are dialed in via the phone line, and you have reconfigured the
modem. Note that nohup only runs a single command here (bash) which in turn runs the
three commands needed to take the line down and bring it back up.
$ 6 C
z8v j
G $ 6 C $ v
@ p8%
!
3 "BG 3 #
$
Here we run a command, top, with its output displayed on /dev/tty8. The command
continues to run even after the session ends.
A program run with nohup is still nominally attached to the terminal or pseudo-terminal on which it
was started. This is shown in the output of ps. To totally detach from the controlling terminal, the
setsid command can be used. Processes that run from setsid should handle their output
appropriately (e.g. by redirection to a file).
To ...
Unix98
BSD
ps ax
ps -A
ps -e
ps u (user format)
ps -f (full listing)
ps -ef
ps -Af
in60Wi0g
i!!0g
iig
iWWW0g
iWWW0g
iWWWWg
ni0g
xi0000x
iWWWisg
W
isx
i 0 f 6
i!00nWiWi
00i0y
g g
!f
0 $
g g
f
pg g
iW
n $%
sx g
nW W0$y
sS g
Wi n0mnWy
S pg
0in 0iW00$
g pg
Wn
W0 W0
x$g
rn i Wynf
pS
i0 iWymin
g s
0W
Wi0
S hi $6W ii!Wi0W0n
in6Wi0i0! iW 0
W0W
W0W
W0W
0ii0y
0iii$
0ii0i
0ii0$
0iinW
0iiWi
0ii0$
W0W0
0$
0iWW
gin600r0
l0n00i
ly
gWW$i
x0iiWi
gin6iin0lii!W0 i
in$sSxi0fgi0n6$jhhnn6f$sx00iWnmin
i!0ninxiWihW00ihW0iiin0iWW6
WgagxWgx!n!fnnnn6g0x 0 ig
top displays the top processes those processes that use up the most CPU or memory.
19.3.2 top
'i 7 {
W W f
W W f
7 {
T U g f
" @8 {
The following options for ps are very useful as well:
ps -w display in wide format with more of the command line.
ps -f display in a forest of processes, where you can see which processes are the parents of
which other processes. This is similar to the output of pstree16.
EG a
3 "" a
$ #
w @s "1
t
a e
e
f a e
1 w 1 ~
1
w 1 ~
e 88G j { j 'Ee
e h
e 88G 2j j
e l
3 1 ~ ~
61 8 & Eu
~
` !4EG a a
3 $"#" a a
s "1
t
e 88G a
e 88G f a
e
{ f
h X X { f
&
l X X lm {
&
t
{ | s s { 78s
~
"|
g f e ca
2db `
H
e
m { f
f
&
eH
lm {
&
t
78s
7t |
T i f e ca
42db `
3 1 t
61 "1 ~ s
lm {
ej
t s
78s
Here we do it
19 Process control
121
19 Process control
122
$ G
"'
~
v
s
1
When top is using cumulative time, then the sub-processes started by a process are included in its
own running time. If you have customised the display to your liking, you can save your settings to
~/.toprc by pressing W.
19.4 Signals
Processes in Linux do not communicate with each other directly, but send each other signals via the
kernel. The most common signal sent is SIGTERM, which means Terminate, unless you know
what to do. This is the reason that the command to send a signal is called kill since the process
that receives the signal usually ends up dead.
The following signals are used quite often. The complete list of Linux signals is in the signal(7)
man page.
Signal
Meaning
Usage
SIGTERM
kill pid
kill -TERM pid
kill -15 pid
SIGHUP
The modem hung up (or reload your configuration file if kill -HUP pid
youre not a terminal application)
kill -1 pid
SIGINT
Ctrl+C
SIGSTOP
Ctrl+Z
kill -STOP pid
SIGCONT
Continue
fg or bg
kill -CONT pid
17 kill is a bash built-in function, primarily so that you dont start up a new process to terminate an existing process.
18 Note that the killall function on some Unix versions kills every process, regardless of its name.
R I I d d I I U R
6W26i(n!E
X $ & # #
` zE@5H ` $
U d H I d d I I U R
0!iW8a26i(n!E
@"7 '2"r {
i R R U R R
R U S W
8 'f '2"r {
i R R U R R
S 7 '2"r {
W
i R R U R R
v G # a #
%8Fkb%
& G # # $
E8FB%8G xr q %
a
#
R R U R R
@"7B"r {
Linux prevents you from sending signals to processes you do not own.
G
@
%E 1
$ # &
$ # &
%E 1
o
( e
!
h(
$ # &
%E 1 ( f
o
BE"r {
W W R f f W R R U R R
l j
@f ( f
@ f {
W W R
j j
@f ( e
R R g W S f W
"PE {
h j
i @f (
R R g W S f W
"PE {
Using killall instead of kill allows you to kill all processes that have a particular name18.
killall kill all process with a particular name
e v
#
d w 4%Pd
G
@ 9 3 C
Experiment with killing process with other signals (e.g. SIGHUP, SIGSTOP and SIGCONT).
Heres a short little script to make your Linux machine behave more like another operating system.
6
%# @% @4
& # $ 6 $
%B
o
(
V i R R
22"r {
e j
@f (
R R g W S Q S U g W S U
"D @F V {
Sometimes you want to kill a process without allowing it to save its work. F
8G 3 %79
8G 3 %79
8G 3 %79
%# @ 88
6
%# @ 88
6
6
%# @ 88
j h q a v
!Dr h%f z%ka 3 "
$
h h q a v
!Dr { %f z%ka 3 "
$
m h h q a v
!Dr h%f z%ka 3 "
$
' V2 q"r {
V V R R
%E 1 ( f
$ # &
o
%E 1 h( e
$ # &
!
$ # &
%E 1
(
V V V R R
' 2 q"r {
h%f ( f
j h
R R g W S f W
"PE {
{ %f ( e
h h
R R g W S f W
"PE {
m h h
h%f (
R R g W S f W
"PE {
kill17 sends a signal to the process or processes listed on the command line. The syntax for kill is
one of these.
kill send a signal
19 Process control
123
19 Process control
124
19.5 Review
Quiz questions
#
%
1.
2.
3.
4.
5.
6.
!
B B%v
s 7B%v
| } ! $
7. What happens if you kill -9 a process which is doing interactive console handling - like man or
vi?
Assignment
xx
Candidate should be able to manage processes. This includes knowing how to run jobs in the
foreground and background, bring a job from the background to the foreground and vice versa,
start a process that will run without being connected to a terminal and signal a program to
continue running after logout. Tasks also include monitoring active processes, selecting and
sorting processes for display, sending signals to processes, killing processes and identifying and
killing X applications that did not terminate after the X session closed.
Key files, terms, and utilities include
&
Run a task in the background
bg
Start a stopped task in the background
jobs
Show background jobs
kill
Send a signal
nohup
Block the process from receiving SIGHUP signals
ps
Process status
top
Interactive process viewing
xx
1. Run the command ls -lR / > ~/listing in the background. Write down two ways in which this
process can be terminated.
2. Run the command find / >& ~/listing2 &. Now run the command kill -STOP %1. What effect
did the command have? What methods are there to terminate the job at this point? How would
you terminate it without allowing it to run further?
3. Log in at a terminal and run the command man man. Log in at another terminal and make a list
of the processes which are involved in the session on the first terminal. Use top to terminate the
pager which man is using, and observe the effect on the other processes.
4. Make a list of the signals which you can use to kill an interactive bash session. Hint: kill $$
sends signal 15 to the current shell, which does not exit.
5. Enter this command 30 times, and document the effect this has on your system's performance
using top, uptime and vmstat 1 10. Make notes about the different ways in which the 30
processes can be removed from memory.
6
%# @88F 8@pG 3 #
19 Process control
125
6. Make a list of processes which are part of your graphical desktop. Kill the X server using
Ctrl+Alt+Backspace and then see which of these processes continue to run after the X server has
terminated.
Answers to quiz questions
1.
2.
3.
4.
5.
6.
7.
20 Nice
126
20 Nice
LPIC topic 1.103.6 Modify process execution priorities [3]
Weight: 3
Objective
Candidates should be able to manage process execution priorities. Tasks include running a
program with higher or lower priority, determining the priority of a process and changing the
priority of a running process.
Key files, terms, and utilities include
nice
Make a command be nice in terms of its CPU usage
ps
Show process status (including niceness of processes)
renice
Make a running process be nice
top
Interactive process monitor (shows niceness, and can renice)
19 The niceness of a process does not prevent it from monopolising available memory, file descriptors, network
bandwidth, hard disk bandwidth and other scarce resources. The following commands are quite useful for finding out
when things are going wrong.
vmstat virtual memory status for diagnosing excessive memory usage. You have to read the man page to
understand the meaning of each column.
ngrep and tcpdump very useful for find out who is using bandwidth.
S U W W f g ` Q f i i f U W f g W V S Q W i f ca
'@%'4%dX'X"X"474F6db ` w
ps can be convinced to display the niceness of each process but seeing how hard it is, I can't think
why you would want to do this. (It's probably not in the exam).
EG
3 "7!
$
3 %#"'%7"
w @s
a
a
a
t
"1
1
w 1
88G hEe
h
88G 8j e
~
f
~
| j e
3 1 1 t
6B78s
f ca w v $
F6db ` t
~
ps (process status) reports niceness of processes in the STAT column. If the process has any
degree of niceness, the status column includes N.
@%%2kx @%%Fd m
G C #
G a e
e
m 7! e 4%F2db
G o # e ca
C #
@%%G2kx @%%Fd m
G a e
em 7! 4%F2db
G
o # e ca
G
@%%2C#kx @%%Fd m
G a e
e
m 7H%F2db
G ! # e ca
e
m (
6 e ca
j%# @88D2db `
nice can only be used to set the priority of processes when they start. renice is used to set the
priority of processes that are already running. renice can zap specific processes, or the processes
owned by a user or a group.
renice +1 -p 14292 make process 14292 just a little nicer
renice +2 -u jack make all of Jack's processes two notches nicer.
renice +3 -g users make the processes whose group is users three notches nicer.
Users can only increase the niceness of their own processes. Root can increase or decrease the
niceness of any process. If a process's niceness is 20, then it will only run when nothing else wants
to.
20.1.2 renice
6
%# @88
$ # &
%E 1 ( f
o
6
%# @88
$ # &
%E 1 h( e
!
6
%# @88
$ # &
%E 1
(
f W R R U R R r e ca
E"2db `
X &
E'
e X &
@"
j
% X &
$
R R g W S f W I g
"DE d 'p"478F2db `
Q f W V W e ca
e
X &
E'
eX &
@"
f
8j e X &
$
R R g W S f W I g
"DE d 'p78F2db `
Q f W e ca
e
l ( f
R R g W S f W e ca
"DE2db `
l ( e
R R g W S f W e ca
"DE2db `
l (
R R g W S f W e ca
"DE2db `
R R g W S f W I g
"DE d D2db `
U V e ca
f W I g W Q S c
EFq k u Bkl e dDV d dD7 @42db `
t Q V W Q S c e Uc U c ` U
Q T e ca
influence the timing). When sort again nicely, sort takes longer to execute (although the
CPU time is similar in both cases).
20 Nice
127
1. Run yes >& /dev/null with some amount of niceness. Increase the amount of niceness using
Assignment
1.
2.
3.
4.
5.
Quiz questions
20.4 Review
t #
nab$"6F j 78s %
l
a # t
b%P 78s
in!nnWi!x0g
iW0!Wg
i0i0i0g
$600g
rpg
in60Wi0g
!i!0g
i!!g
iig
xi000x
iWWW$Ch$
$Ca!
ni0g
000WW!uWg
i 0 f 6
g g
f
g g
f
g g
f
g g
f
g g
f
g g
!f
g g
f
g g
f
g g
g0g
iiin ! $0y0
x g
xin! W0nWii 0
g g
Wi0 0 iWWnf
pg g
Wn
$ $
sx g
xi!nW i iW0i0
in6Wi0i0! iW
6
W0W0
W0W
W0Wx
W0W
W0Wv
$
W0W
W0W
W0W
W0W
6 0iiWi0
6 0ii0i
W0WnWWi
W0Wv6i
6 0iii
0
0$
i!00iiiWW0
0n$000gin60n00$
liWnWiWly
0000n0W gWW$i
x0iiW gin60nW0Wn l0nWy06
i
in$sxWxi0fpgi0n6$jpghnn6fpg0iWnmin
i!0ninxiWihW00i hW0iii0n0iWWu0
WsxgisxxWgx!n!fnnnn6x xa60f0 inWx
top shows the top processes on your computer, i.e. those that use all the resources. top runs in a
console, and displays some summary information at the top, and a sorted list of processes. By a few
deft key presses, you can change the order in which processes are displayed, and quite often find
out why things are not working as planned.
Heres a screen dump of top where user processes are nice.
20.3 top
8!586 @E@db%v
# !
G a #
X X X # # #
% b%v
a #
v " j 8 7" 3 %"
#
#
G $ & G #
7E8"'%
$ #
8"'%7"
$
3
@" 56@!4%$x @"xb%b"F4EG
!
#x G !
3 "7!
$
3 %"'%7"
#
w @s
$ 3 E&
$ 3 E&
$ 3 E&
#
"
$
~
"|
t
%
f8@8 e
m f
@8 e
m f
f f
8 e
{ hh
m
j {
j
j e
f8j e
j
m@8j e
f
j f
@8j e
t
78s
20 Nice
128
20 Nice
129
renice and top. What are the limits of this? What effect does a nice process have on uptime and
vmstat 1 10? How are the nice processes displayed in top and ps?
2. As root run yes >& /dev/null with an increased priority. Increase the priority further using renice
and top. What are the limits of this? How are this high priority processes displayed in top and
ps?
3. Modify your /etc/profile so that all console sessions are made to run more nicely except if the
user is root. Hint: the shell which you want to change the priority of is already running, and its
PID is $$.
Answers to quiz questions
1.
2.
3.
4.
5.
So that the process does not interfere with processes which should be running more urgently.
nice process
nice -n -20 process ... only root
renice +2 pid ... make process 2 notches less important
r for renice
21 Regular expressions
130
21 Regular expressions
regexp /reg'eksp/ n. [UNIX] (alt. `regex' or `reg-ex')
1. Common written and spoken abbreviation for `regular expression', one of the
wildcard patterns used, e.g., by UNIX utilities such as grep(1), sed(1), and
awk(1). These use conventions similar to but more elaborate than those
described under glob. For purposes of this lexicon, it is sufficient to note that
regexps also allow complemented character sets using ^; thus, one can specify
any non-alphabetic character with [^A-Za-z].
2. Name of a well-known PD regexp-handling package in portable C, written by
revered Usenetter Henry Spencer <henry@zoo.toronto.edu>.
the Jargon File version 3.20
Regular expressions were formalised by the logician Stephen Kleene as a method for specifying
syntactic structure precisely. Regular expressions have been a part of Unix for a quarter-century,
and have found their way into many other places. A regular expression is a search pattern written in
a specialized language that is understood by a number of Linux applications. Regular expressions
are used to search through text for phrases according to an exact algorithm.
In this chapter we will look at using two programs using simple regular expressions. We will then
take a slightly more detailed look at regular expressions
Metacharacter
Meaning
Regex
Matches
foo.bar
foodbar
^hi
lines starting hi
21 Regular expressions
Metacharacter
131
Meaning
Regex
Matches
hi$
lines ending hi
[ABC]
Match A or B or C
h[ae]ll
hall, hell
[^DEF]
h[^ae]ll
A[A-Z]T
[A-Z]
For normal regular expressions, the following methods of grouping are available.
Metacharacter
(whatever)
Meaning
Regex
Matches
These are the modifiers used in regular expressions. These may follow a character, a metacharacter,
or a group in parentheses.
Modifier
Meaning
1 or more times
0 or 1 times
{N}
N times
{N,}
N or more times
{N,M}
G
@
By default, grep reads the file(s) or standard input one line at a time and prints the lines that match
the pattern. Here is an example:
X 87% 8BP5 ` 0
# G $ $
C$Xy8@8 3 @D$B%C$ #%8$ 8@8 3 h E& v $ $87HE& v ~ 0
# $
# # G # $
dy8@8 3 @ 0
# X
"7EV F P6db ` t
W S f f Q W U V ca w v $
w s
1
8 s s 4 w a
# $ & ! G
E"@ 3
In the above example the pattern we are searching for is etc. The file we are searching is /
etc/hosts.deny. A simple regular expression merely matches the string given anywhere in the line.
Notice that only the lines containing the string etc were printed!
If grep is invoked without a filename argument it will expect input from stdin.
In this example the output of the ls command is filtered by grep for lines containing host.
C $X
y8@8 3
8@8 3
# X
` b@8 3
f Q W W f R ca w v $
7EB V H6db ` t
G & # X #
dX ` b8@B ` b8@ " h@ ` 7E' o f @8xo 0 (
# X #
o r o G
o G $ &
G Gq
&
@
The next level of complexity is replace only part of the matched expression. This is useful when
the part in the parentheses could contain a number of different values.
This code will generate contents for an inetd.conf file where POP3, IMAP and FTP are
enabled (as well as POP3s, IMAPs). The \1 is replaced with whatever matched between
the parentheses, and the # at the start of the line is not sent to the output.
a 6 $ # &a
z%# @a 3 ""@'y
C $ G
%h"@ " @
sed stands for stream editor. sed is used in the following manner:
sed pattern [filename1 filename2..]
Like grep, sed parses input one line at a time. sed performs basic text transformations on the output
stream. The most-often used purpose of sed is to perform search and replace operations. The
simplest form of these is to search for a fixed regular expression, and replace with constant text.
21.3 sed
The regular expression sbin.*getty matches sbin followed by anything followed by getty
occurring anywhere in the line.
t
l t @@4@ 8"'%7" a '"'8a %f a l p0
6 ! 6 # # C $ G h
t
j t @@4@ 8"'%7" a '"'8a %f a j p0
6 ! 6 # # C $ G h
m ! & # # C $ G h f a
&%& @f @8@"'%7" a '"'8a %e b%& 0
e
86 "@ j { !@@8"'%a '"'8a h a ~ 0
f e
~
! $ # # C $ G
j @9@E@"'%a '"'8a h e a j
f
# & # # C $ G
h @9@E@"'%a '"'8a h e a h
f
# & # # C $ G
f a
# & # # C $ G
@9@E@"'%a '"'8a h e
f @9@E@"'%a '"'8a h e a f
f
# & # # C $ G
e @9@E@"'%a '"'8a h e a e
f
# & # # C $ G
@H@E@"'%a '"'8a h e a
f
$ # ! ! # & # # C $ G
I U W W t I
@@"7EV B@!Y7"hf y6db ` t
W ca w v $
T Q V W W
% S 7EV vp 7 hp @e
p i W
T% S 7EV v ' p @e
Q V W W p
p i W
T% S 7EV v p @e
Q V W W p
p i W
T Q V W W f f W R
% S 7EV EBe
T Q V W W p
% S 7EV v @e
p W
fEWVE'iV vhhp @e
W f W p fp i
W
f W V W f W p fp
EE'iV vh @e
W
f W V W f W f f W R
EE'iV EBe
case insensitive
-w
-v
-r
recursive
-l
G X
87y8@8 3
6 X
7%dy8@8 3
dy8@8 3
# X
21 Regular expressions
132
21 Regular expressions
133
21.4 Review
Quiz questions
X
zi( { ! z
&
&
)B4e
e
2BP&
i( { ! @Xzi( { ! @zi( { ! @zi( { !
X
X
3 7
C | #
%HD
& $ X X $
'wbwY E&
5. Using sed, how can one search for the phrase big brother or big sister in an input stream, and
replace the big with little?
Assignment
1. Write a grep command line to search /etc/passwd for users that have both a home directory under
/home who also have a shell ending in sh (e.g. bash).
2. Write a grep command line which will match the word imap at the beginning of the line in /
etc/services, but not match the line imaps or imap4. How many entries are there for imap?
3. Write a grep command line to match lines in /etc/inetd.conf that do not start with a hash
character #.
4. Write a grep command line which will match ftp and http at the start of a line in /etc/services
followed by tcp later on the same line. How many FTP and HTTP related services exist?
5. Create a file consisting of the columns Match this and But not this from the table below.
Write regular expressions for grep to match the left hand column, but not the middle column.
Match this
en2a3
en22a3
en2b3
en22b3
Hint
Use *
part3a
part4x
part9b
part3A
partA3
partx7
Use []
Part34
part3a
part5x
PartA5
Use []
I H
na$PSW%F
0
| # # $ '
X
zi( { ! z
' %88@# 8h(' h'
&
3
#
&
)B4e
'wB'"7$ 9 w
#
#
e
2BP&
$
"%$ 3 5 ` $ i( { ! @Xzi( { ! @zi( { ! @zi( { !
#
X
X
Sd0R 0
3 7
C | #
%HD
& $ X X $
'wbwY E&
I H F F F 0 x '
na$PSW%F )xa 3 7'
' C # ' C #
S%HH'' 0H S%HB''
' & $ $
x'HBH$ 3 E'
$ &
apples
oranges
anything else
id23x56
id234xy88
id10x32
id9545x452
Use ?
id345x111
id344y231
Use [0-9]
Match this
21 Regular expressions
Hint
134
22 vi
135
22 vi
vi is a visual editor with a difference it's hard to use.
Anyone who thinks UNIX is intuitive should be forced to write 5000 lines of code
using nothing but vi or emacs. AAAAACK!
Discussion in comp.os.linux.misc on the intuitiveness
of commands, especially Emacs
vi is preferred to other editors by system administrators because it is small and fast, will run on any
kind of terminal, yet is very powerful, efficient and flexible. vi's commands enable you to perform
any editing task quickly without having to leave the main keyboard and reach for the arrow keys. It
is the one editor you will find on every UNIX installation.
vi is standard equipment in Linux. For those trained on other editors, vi does take a little getting
used to. This chapter will enable you to do basic editing using vi.
LPIC topic 1.103.8 Perform basic file editing operations using vi [1]
Weight: 1
Objective
Candidate must be able to edit text files using vi. This objective includes vi navigation, basic vi
nodes20, inserting, editing, deleting, copying, and finding text.
Key files, terms, and utilities include
vi
(editor)
/, ?
(search forward, search backward)
h,j,k,l
(left, up, down, right)
G, H, L
(end of file, head of page, end of page)
i, c, d, dd, p, o, a
(insert, change, delete, delete line, paste, add line, append)
ZZ, :w!, :q!, :e!
(save+exit, save, quit, edit file)
:!
(run external command)
22.1 vi modes
Most people are used to editors that have a single mode of operation, namely insert mode, with
additional commands that can be selected from menus (another mode). vi is different in that it has
three modes of operation, and none of these is a menu. vi is mostly used in command mode and
input mode.
Command mode Every pressed key is interpreted as part of a command. This includes the
keys a to z, which is very confusing for beginners. vi starts in Command mode. You can change
from Command mode to Input mode by pressing a key.
All commands may be preceded by a number. This number sets how many times the given
command should be executed. So while dw deletes one word, entering 3dw deletes three words
at once and 10x deletes 10 characters. 20dd deletes 20 lines.
Input mode To enter input mode, you press i (insert), o (add line), a (append) or r (replace). In
input mode, they keys you press are inserted as text, until you press Escape. To change from
Input mode back to Command mode, just press Escape.
22 vi
136
Ex21 mode To enter ex mode, type : followed by the ex mode command. ex mode makes it
possible to interact with the shell in very powerful and sophisticated ways. It is not necessary to
understand most of these, since you can do quite well with only a few elementary commands.
21 vi is the visual editor, while ex is the extended line editor that preceded it (no wysiwyg display). These days ex is
not used a lot outside of scripts. ex mode makes vi behave a lot like ex.
22 vi
137
Key Action
u
undo the last change
x
delete the current character (and puts in buffer)
dd delete the current line (and puts in buffer)
dw delete to the end of the current word (and puts in buffer)
cw change word (rest of the current word is overwritten by the input)
yy copy current line into buffer
p
paste text in buffer after cursor position
P
paste text in buffer before cursor position
J
append following line to current line
.
repeat the last command
Improvements available in some versions of vi
ZZ equivalent to :wq save and exit
ZQ equivalent to :q! - exit without saving
v
Visual mode
V Visual mode selection by lines
22.3 ex mode
In addition to the movement commands, you need to know a few commands for ex mode.
Command Action
Movement commands
:q
quit (if all files are saved)
:q!
quit without saving
:w filename write the file (save). The file name is optional.
:wq
save the file and exit
:x
save the file and exit
:e filename edit another file
:e! filename edit another file without saving the current file
Here are a few more useful ex commands that can make your time with vi more pleasant if you are
using a version of vi that supports them:
:syntax on turn on syntax highlighting
:set smartindent automatic indentation as you type program code
:set wrap show long lines over two or more lines
:set nowrap chop long lines shorter.
22 vi
138
22.5 Review
Quiz questions
1.
2.
3.
4.
5.
6.
7.
8.
What are the vi commands for up, down, left and right?
How do you search for the string The at the beginning of a line?
What is the key to exit insert mode?
What is the meaning of ! in command mode?
How do you delete an entire line?
How does vis cut and paste work?
Explain 3 different ways of getting into insert mode.
What is the quick equivalent for :wq?
Assignment
1. Install vim, run the vimtutor command, and work through the tutorial.
2. Don't use any editor except vi for editing files for one month. If you are using a Windows
system, download and install gvim and use that in preference to your regular editor.
Answers to quiz questions
1.
2.
3.
4.
5.
6.
kjhl
/ ^The
Escape
Dont save
dd
d to delete (e.g. dd for a line, dw for delete word, 6dw for delete 6 words), p for paste (or P for
paste before).
7. i (insert), o (add line), O (add line before), a (append). Perhaps even cw (change word).
8. ZZ
139
23.1 fdisk
fdisk edits the partition table. For each partition, the following information appears in the partition
table:
Active if the partition is marked as active, the default DOS MBR loader will load the boot
record from it during startup. (The default DOS MBR is installed with FDISK /MBR in DOS or
Windows)
Starting offset of the partition where on the disk the partition starts
Size of the partition where the partition ends
Partition type each operating system has a code allocated to its partitions.
In the listing below, the commands that are not used often have been removed.
x X X
r B t
i~ "@ e @~ x ~ "@ ~ bq
B t
#"H
$ G $
# $ # $ & &
HE s
a r G
vx)7 3 ` q E s
& # $ & &
mkfs is the interface through which you can make any kind of filesystem that Linux supports. The
most important option is the -t option which specifies the filesystem type.
23.2 mkfs
j
6
B"@ a
X v # #
y"72%7~
Xb8@P P 3 BH@"P8ED"75P 1
#
$ C $ C # 3
X $
b"7BB 3 F8@"D@2P 1
# v 3
X
Y'"F%@8
# t w
$ # $ G
"7H"F 3 2 a % A
# $ !
3 ED$ `
C
X $ # $ G $ ! r q #
b"7H"FPi9$ s
$ #
58$ 3 "7H"F 1
$ # $ G 3
C a r G
vx)7 3 ` q E s
& # $ & &
After you have modified the partition table, you write it to the disk using w. If there are mounted
partitions on the partition you modify, the kernel will not re-read the partition table, and the changes
do not take effect until the system reboots (or until the partitions are unmounted, and fdisk is run
again).
V axr88H@H248@q 8}
G
a
nxr l ! q 8B$ s
& # #
a r G
kx)7 3 ` q E s
& # $ & &
After creating the partition, we set it's type. This is actually not necessary, since the
default type is 83 (Linux). If we were creating a swap partition, it would be good practice
to set the type to 82 (although not strictly necessary).
h h
Ee
h
axr hhEe %$ ` dx hEe ! q %25 %o2B
B 9 o 9
9
I R U
FTw0H26$PSdyn!Ihn
U d I H d
a
xr %$ ` dx j
l
a r
nx@!
$ 6 #
"F%$ ` 2 E|
%o2H"7H@8
# $
"F%$ ` 2 E|
$ 6 #
! q "7H@"
#
& # #
8B$ s
q
d H I I R e R e R I R
0iih0gfS0a0f@!Pa
# $ # $ & &
HE s
a r G
vx)7 3 ` q E s
& # $ & &
d H I I R e d R I R
0iih0g6a@!Pa
)0iih0g2!PW%lQ
d H I I R e 1 F F R U e
d H I I R e R I F
0iih0gf@!8@%
a
nxr h ! q 8B$ s
& # #
S a r G
gx)7 3 ` q E s
& # $ & &
To create a new partition, use n for new. Here we are deleting a partition, and creating it
again.
r w $q
8
#
#
8
#
G $ C
7Eh 8
#
e 1 w
f h { A
#
&
@E7~
f
8m
f
8m
e
m
m
l h h
o 'h yh
j
e
e e
o e { l
j h e
hEm e
v
"%
l l
l
l
j
j l
l
j e
h h
Ee
#
h 3 @
$ 6
$ 6
@ 3 @
f 3 @
$ 6
e 3 @
$ 6
3 @
$ 6
6
P"@
l l
l
l l
l
hj e
jhEe
h
$
~
h j j
e
88 hh ` F""798%|
# 5 #
h h a $ 6 v
# l
""7 j y"@ 8j y8 3 Ee z 3 @27
x f x $
a r G
vx)7 3 `
&
q
# $ & &
E s
# $ v $
B57BB"7B%C
& # $ G $ #
p@E'B "H$ 3
# # 6 C
8$ 3 9"7$ 9 3 ED%
140
23.3 Review
m
X 6 ! !
b%8@BP5P4 ` e B@E| b@" ` 8%58@ 3 3 vyE7
#
X
& 6
C x $
# l
F8%& e 8@B 3 7EH8EB@E'8 ` 1
6 v $ $ & $ C &
3
# # $ & # # # $ &
da E ` 9p@E'8 ` 4"" 8G 75% A
# $ v #
# a $ # #
dy8"7B9% A
h 'h x j { x 'h e x f { m
f l
l l
a v # v $ v
y""4B@BEG "4" 8G ~
G 288 m e
G #
x G v
& $
v
EG 2" m
%"B@"
5 v $ $
{
l e
@"B8G 79 3 H ` 86 @8 r X h q "" j f
v
v j f h h x # m f j
"" @j y89 @8
r y 6 9%@@%
5 q e 5 9 # & $
r y 6 %
5 q e 5 9 v
8b8@P~
# a G
5 $ &
!87p@E'8
iYp7EC H s
r #x q | $
# $
hFU@RGQ CDIAB%Sdk @
H
Xb"@P8'"Hp@ j
6 $ G v $ #
e ! $
l
r e %E'! m q e X ` e &
v
Q
Fg g 0
g
hf 0
S V
De
U i f R
R 4Be
S V
PDe
575P7EW 8 g 0
Q Q R Q i W R T f
Q
g
hf 0
W R T f f T W r I f
7EW 8 7"hie
W R T f U i f R
7EW 4R 4Be
` Q V r ` f I W R T f
7P%g 2'847EW 68B76T 5De
` T Q Q W W S ` S S
` y ` &
X
v
` @y ` &
X
v
8@& y ` &
X
v
%E& y ` &
# X
v
` y ` &
tX
v
f dy ` &
X
v
e dy ` &
X
v
` y ` &
X
v
` '%y ` &
& $ X
v
` &
v
` @"'%P2db `
v & # e ca
d H I F e H d F d R H Q R H U 9 9
0i8an!Ga@S0an!W6%206R 4 6R 4
U S W S ! Q d H I e H Q U ! e I Q U I ! 3
8 8P0i!%67lWS%m654
f T r
8%
To create a filesystem on /dev/hda1, you would use mkfs something like this
23 fdisk and mkfs
141
142
2. When does the kernel read the partition table on the hard disks?
3. Under what circumstances will the kernel not reread the partition table after running fdisk, and
what error message is displayed?
4. What is the command to make a vfat filesystem on an existing partition, hda3?
Assignment
1. Run fdisk on /dev/hda (or your first hard disk). Make the following changes, but do not save
your changes (unless you want to practice using gpart):
a) Delete all of the partitions one by one using the d command.
b)Create a 64M primary partition, a 256Mb primary partition, and an extended partition
covering the rest of the disk.
c) Divide the extended partition into two equally sized logical partitions.
d)Set the type of the second partition to Linux swap, and set the type of the first logical partition
to Linux LVM.
2. Create one of each of the filesystems discussed above in its a partition. Mount the filesystems,
and copy the /usr tree to the new filesystem. Record the differences in the amount of free disk
space (df) and inodes (df -h) before and after copying the data. Suggest reasons for the
differences between filesystems.
3. Find out what a superblock is, and see what mkfs options each filesystem has with regard to its
superblock.
Answers to quiz questions
1.
2.
3.
4.
24 fsck
143
24 fsck
fsck, <operating system> file system check. The Unix program that checks a file
system for internal consistency and bad blocks etc. and can repair some faults.
fsck is often used after a crash when the file system has been left in an
inconsistent state, e.g. due to incomplete flushing of buffers.
Used on Usenet newsgroup alt.sysadmin.recovery as substitute for **** and
became more main-stream after the Communications Decency Act.
The Free On-line Dictionary of Computing (edited)
$ $
& 3 %@
6
&
% 3
# #
B%
{ @EF"7"6 w @E|
| $ $
{
Wm
{
{ m
{ { m
{ f l
e { e
{ h
m
m
m l
e l l
f
j f
j
e
f { f
m
l j
m j
j h
{ h
m
j
@f { l
e e l
f { f
v !
""7%B
j 3 @
$ 6
#
#
f 3 @
$ 6
l 3 @
$ 6
&
@E'8
T S e ca
8D2db `
f
{ { m
f # e X f
# #
B% { @E"6 w @E|
| $
# mX f
9
~
l 3 @
$ 6
&
@E'8
i T S e ca
'H8D2db `
{f
&
% 3 n8m # l X # e X m
{ # f # f X f
X
# #
B% { @E"6 w @E|
| $
#
#
6{ X l
9
~
f 3 @
$ 6
3 @
$ 6
&
@E'8
i T
'H8S 0 db `
ca
By way of comparison, the figures from df -h are similar, but not quite the same. This is
because of differences between du's estimation of the space used and the actual space used.
HB `
$
$ 6
"
7"
G &
@"7%
6 7%
#
"'%
G
3 %79 a ` h j @
j G
G
8@
# &
@
$%@
&
#
` o @88
78
&
% 3
6
@
8
#
"
#
e
f { l
#
6{ X
{ e
B
X
@
X {
e
B X e
v a
{ m
B
X
j
B
j
B
e
j
# X m
e
j h
B hEe
l
X
{X l
t V f i
P''pg S 0 db `
ca
du will summarise the amount of disk space allocated to files and directories (recursively for the
directories, i.e. including the files in those directories).
The most useful switches for du are these
-h human readable. By default du displays the number of disk sectors used.
-s summarise (show totals for each directory, not detail)
-c count (show a grand total)
24.1.2 du
{
& 3 %@ W
6
{
# #
B% { @E| t
j
B
#
X
t
j
B
#
X
8# t
@E| t
{
e
& 3 %@
6
j
#
# fX
{
m
0l
# #
B% { @E"6 w @E|
| $
e
j
#
6{
9
~
` & 3
e 3 @
$ 6
&
@E'8
"'H8S 0 db
i T
ca
` & 3
e 3 @
$ 6
&
@E'8
i T
'H8S 0 db
ca
Here's df looking at a system with a single reiserfs partition. There are a lot of inodes free.
$ $
& 3 %@
6
&
% 3
# #
B%
{W
{W
{W
j
{ { e
j j m
{ l
f j f
l
j m h
h%f
{ @E| t t
m m h m
y
h { l
e l h
yhh
j { yh
8# t
e e
{ e l
l
h
m e
@E| t
j 3 @
$ 6
#
#
f 3 @
$ 6
l 3 @
$ 6
&
@E'8
H8D2db `
i T S e ca
Most filesystems have a limited number of inodes available for files. These may be
dynamically allocated, but when they run out, you can't make more files.
$ $
& 3 %@
6
&
% 3
{Wm
{
{
m
l
j e
l
'h
e
j l
# X
# X f
# X
m
hh
#
6{ X f
j 3 @
$ 6
#
#
f 3 @
$ 6
24 fsck
144
145
& 3 %@
6
h e
E
h e
E
24 fsck
` & 3
24.2.1 fsck
fsck is the interface for checking any given filesystem. During startup fsck -A is run to check all
the filesytems listed in /etc/fstab. Depending on the filesystem to be checked, fsck will use one of
the following programs.
e2fsck or fsck.ext2 second extended filesystem (ext2)
reiserfsck check a reiser filesystem (this is not possible if the filesystem is mounted, even readonly)
fsck.minix minix filesystem
fsck.msdos FAT filesystem22
fsck.vfat Windows 95 to ME VFAT filesystems
fsck.xfs XFS filesystem check
fsck cannot check a filesystem while the filesystem is in use (mounted read-write). This is because
fsck may make changes to the filesystem which make the kernel behave badly, since the data on the
disk changes in unexpected ways. reiserfsck has the additional restriction that you cannot check a
filesystem which is mounted read-only, which makes it a little difficult to recover from serious
errors without the use of a rescue system.
24.2.2 ext2
e2fsck
e2fsck checks the consistency of the file system. If there are errors, it will make changes to the file
system to correct the errors.
Deleted files which have not been removed from their parent directories are removed. It is quite
common for a file which is in use to be deleted, but to remain open. The problem is fixed
automatically.
The filesystem check may locate files which have become detached from their containing
directory. These files are saved in the lost+found directory in the root of the filesystem (e.g. /
home/lost+found if the filesystem is mounted at /home). The file name used is based on the
inode number, although this is seldom useful, since the file name is missing.
Cross linked sectors are sectors which are allocated to more than one inode (file). This problem
is resolved by copying the sector and allocating one copy to each inode.
Various inconsistencies are eliminated, such as incorrect hard link counts, parent directories not
22 fsck.msdos will destroy data on a vfat filesystem if you are unfortunate enough to try it. The two filesystems are
almost compatible, but using the incorrect filesystem check scrubs off vfat information.
r
EB@"uq
a v
b%""F86 @8
f e h
% a a { 4 w %
m
#
a ` 5 3 H
$ v
e h h h
r
8 3 %& j q '
#
a $ # v 3
z"6 5 s
f e h
% a a { { @ A
a v $
b 3 H@8
j
@f
a # # & & & $
bH%B' E
a # #
bH%
f e l m m $
%B a hh a 4 ED 1
a & C $
b%'B%D@8
f e l m m $
%B a hh a 4 ED 1
a & # & $
b%'B%D@8
yh
a G v
YG 28""F# t
e f j
%
a G
YG 288# t
m l e
j f
a G # & $
YG 288%@@%
m l e
j f
a G v
YG 28"%
j {
a 9 # & $
b 9%@@%
j {
a 9 v
b %
a "F@"
v
j { @Ee
f h
a #
y8H
a v
y""F
f j h
% hEe
a # v
b5"F86 @8
j e
hh
a # v
b5%
e h j h
hhEe
a #
bH# t
8
#
a G
b8@P~ @E'8
&
# #
s
a "7$ 3 8"
6
# $
a $
b@ @E'8
&
$ G
8G 7 @""'
8@ 8#P8G@ ` %t $ 3
6
$ #
a $
y8 ` @E'8
&
r & $ #
%E'%@q
a 0 "@H@E'8
# 6 &
f
h a 8D8@EB@E'8
& # $ & &
| | &
f @ f ` { jm em ! m { @! { hEe ! ` h ` $ e
!
a 7t 2@E'8
6"7$$"67$H# "
a B%D@8
# # & $
6## "
a & $ # & 6 &
b%'%P%82@E'8
e ! $
l
r e %E'! m q e X ` e 8
G &
U S W S i f T W
84'48g S 0 db `
ca
dumpe2fs displays the tunable parameters of the ext2 filesystem as contained in the superblock.
Don't feel bad if some of the output is Greek23 as the BUGS section of the man page explains,
You need to know the physical filesystem structure to understand the output.
dumpe2fs
The debugfs program is an interactive file system debugger. It can be used to examine and change
the state of an ext2 file system. When debugfs is running, you can enter specific commands
detailed in the man page. These can also be entered on the command line with -R command. -w
enables write mode, in which changes may be made to the filesystem.
!
g
S W S
! ! I Q T G E
B765S!Ha4F
f T g W
88I S
matching child directories, the presence of the . and .. directories in each directory.
The following options are useful with e2fsck (or fsck.ext2)
-f force a check, even if the filesystem was cleanly unmounted.
-p preen fix without questions.
-n assume an answer of no to any question, and do a read-only check of the filesystem.
-y assume an answer of yes to any question.
-c check the disk using the badblocks program.
24 fsck
146
24 tune2fs is shorthand for tuna fish, as in I can't tune a piano, but I can tune2fs.
o j
o j
If you ever need to run reiserfsck manually, you may have to use the command reiserfsck --rebuildtree.
24.2.3 reiserfs
m
Xb%8@BP!5P4 ` e B@E| b@" ` 8%58@ 3 3 vyE7
6
!
#
X
& 6
C x $
#
F8%& f 8@B 3 7EH8EB@E'8 ` 1
6
v $ $ & $ C &
3
# a # $ # t # $
dbB%9 s
e ! $
l
r e %E'! m q e X ` e
#
W 7iyEH488 7"hiyxH ` u
f f W R T q i f T W g I f e ( v $ t w v $ t
U U T I Q R Q I H F Q R Q Q U k
!S4%0P46%y%%WajE
iYp7EC H s
r #x q | $
# $
X 6 $ G v $ # &
b"@P8'"Hp@E'8 `
e ! $
l
r e %E'! m q e X ` e &
v
W 7iyEF8 7"hiyxH ` u
f f W R T f T W r I f e ( v $ t w v $ t
F8
o
#
48
o
%g Hh8B W 7iyE68B76T 5DyxH ` u
` Q V ` f I
f f W R T ` T Q Q W W S ` S S e ( v $ t w v $ t
You probably don't want to change these options on your filesystems too often.
-c max-mounts-count how many times it can be mounted without a fsck.
-i 10d the number of days (d) or weeks (w) or months (m) the filesystem can go without a fsck.
-e remount-ro what the kernel should do if it finds an error in this filesystem. Options are
remount-ro (read-only), continue and panic. The default is to continue.
-g group -u user -r reserved-blocks-count which group and user can use the reserved blocks on
the disk. The standard options reserve a few blocks for the user root and the group root so that
the system still functions when the filesystem is full.
-U UUID set the universally unique identifier for the filesystem. This can be used in /etc/fstab
and for locating the journal of a filesystem.
Here is how you add a journal to an existing filesystem using tune2fs. This converts it from ext2 to
ext3.
| | | ! ( & ! v ! $
6
"@ ( 7t B7u)%'% 3 @ 1 uy8y ` u
! ( ( X X X x $ ( &
!
( ! # & ! $ ! ( $ ! & 6 !
x%'@p'u87%8P!u
( # ! #
)%& s ux@"Bu)! ""786 @8Bu
! ( ! ( # v ! !
( # G v ! & !
)%87! ""786 @8H'u( @ ` @""'Buu
$ ! $ G ! ( !
( # G ! $ #
E"8@%t z uu( C & "6 Bu
! ( t ! (
$ # !
( G B@ux"7$ 3 87! "Bu)! 8%'! Eu88 7"hi
! ( 6 ! ( # # & $ & ! f T W g I f
The man page for tune2fs24 describes its usage something like this
tune2fs change ext2 filesystem parameters
m
6## "
m e
r
E2G q
a # #
b5$ 3 G B@"
a 6 $ #
b"@P% z
a # $ #
bB% z
| | $ #
a 7t % z
a 9
b 9# t
a #
bH@"
a v
b8D""F86 @8
24 fsck
147
x `
0 0 0 0 0 0 0 0 0 0
0
# X X # #
d%7~
# X X #
d 3 "
@% x
#
# !
P9D$ s
@% x Bd 3 "
# # X X #
a
y8%B@85 ` %
# v
0000000 8"F ` o @8yq $ f $ 90
0 0 r $ G #
s 0 0 0 0 0 0
a
xr88 ` B@85 ` %yq $ f $ s
# v
h e a #
E b ` "E& 7~
v #
b ` 8%
a #
j
m l b ` 8
a #
# X X #
d 3 "
0 0 0 0 0 0 0 0
0 f $ 90
s 0 0 0 0 0 0
a r # $ &
x%E@hq f $ s
# X X #
d 3 "
0 0 0 0 0 0
0 e $ 90
s 0 0 0 0 0 0
# h
@" h
$ 6 $ h
887 h
# X X #
d 3 "
@%
{ XX m X j X X e X
X X { X X X { X X X { X X X { X X X {
v
dXX ""F"75 ` %
v $ $
# v
0 0 0 0 0 0
0 $ 90
s 0 0 0 0 0 0
h
h @"H9 PEq $ s
# C
a r 6 $
x8887
j
{ ` 8"
#
t
h
h @8 3 2%'4887
# & $ 6 $
yj 8""BPq ""F
h j r v $ $ # v $
# X X #
d 3 "
@B 3 $ 3 ' h y'
8 3 @f { ' h y'
h e f
@% Ef
{
XXX { m XX j X X e X
x `
X
X X { X X X { X X X { X X X {
$
8EC"v" yhjj r8EG7E&"gz%tnx v"48G 7hq "" m "7%~
$ x $ #
v e # G G v
j
@"PvE&"v" m x vdXX G7E"47%2
$
$ & v ! # # $
0 0 0 0 0 0
0 $ 90
s 0 0 0 0 0 0
a $ s
0 0 0 0 0 0 0 0 0 0
0
f
%e f a j e a Fe G w A H@9%@5 ` @
f
$ $ ! ! ! v
0 0 0 0 0 0 0 0 0 0
0
#
` "$ "%B
# # $
X X $ # t # $ G
z%9"7"@
f W a r
Ex88 3 8@BBPu2E8 3 a'% 3 HPBEP8F
G # #q (
| & $ G # # $ C
e d Q
nWqE
e X j X f % ' ` @
G
e d Q
nWqE
v X X # #
d%7~
{ XXXX { m XXXX { j XXX X e X B%9%# t
X { X X X { X X X { ! $ # t # 9 $
e
h
h
e |
'h @$ $ j j ! e $ " hh @! m %f ! { j { ` da 7t |
$ !
xi# q
a r
e d Q
nWqE
# # 6 $ G v $ # &
s "@P8'"Hp@E'8 `
e X j X f % ' ` @
G
24 fsck
148
149
f e m
%2@f a j e a Fe
f
24 fsck
G w A H 3 % `
$ #
0 0 0 0 0 0 0 0 0 0
0
v
` @
24.3 Review
Quiz questions
1.
2.
3.
4.
5.
Assignment
1. Set up an ext2 filesystem in a partition. Set the label of this partition to CRASHME and mount
the filesystem at /mnt/crashme. How large is the partition? How many inodes are available?
How much space is available for files?
2. Copy a large directory tree (e.g. /usr) to your filesystem (cp -a /usr /mnt/crashme). While this
copy process is in process, reboot the computer forcibly by pressing the reset button. Use the
diagnostic commands to examine the effect of doing this.
Now set up a journal for your e2fs partition, and repeat the operation above. Make notes on
what you observe.
3. Repeat the previous assignment with the other filesystems you have available. Do any of these
survive the treatment?
4. Create an ext3 filesystem, and copy some files to it. Use tune2fs to remove the journal from the
ext3 partition and convert it into a ext2 partition.
Answers to quiz questions
1.
2.
3.
4.
25 Mounting
150
25 Mounting
Recursive traversal of loopback mount points.
BOFH excuse of the day
Linux does not have A: for its floppies, C: for its hard disk, D: for another hard disk and H: to Z: for
network connections. Instead all files are arranged in a big tree, the file hierarchy, rooted at /. The
files can be spread out over several devices. The mount command attaches the filesystem found on
a device to the file hierarchy. The umount command detaches filesystems from the file hierarchy.
This chapter is primarily concerned with hard disk partitions and removable media, and omits
mention of the various network filesystems that Linux supports.
25.1 mount
The mount command without any options shows the filesystems which are mounted as part of the
filesystem.
On this system, partitions of the first IDE hard disk /dev/hda are mounted at / and /home.
The additional entries are kernel virtual filesystems (/proc, /dev/pts, and /dev/shm).
C G 6 G G 6 # #
r uq f B8@B% 3 2 f 3 @
C
G & #
$ 6
r uq ` @8'"P8@9'" "@2F ` @8'"
C
6
G G #
6
r uq F8@P2B#
C G G G # #
r uq f B8@22 l 3 @
C
G #
$ 6
Q e ( v $ t # w v $ t
g yxHu
mount is used to add a filesystem to the filesystem.
C
3 @4$ 3 @@ 8P0iy!67lWy
C 6 ! Q d H I e H H ! e I I
Q
g
The mount commands which correspond to the mounts above are these
25.2 fstab
During the boot sequence, the command mount -a is run. mount -a reads /etc/fstab and, for each
line, mounts the appropriate filesystem at the designated mount point.
Q Q R e e T
6gy
Q d H I e
P0iH
8%$ `
I Q Q F
!S6%!U
f
I d H e I dT H
hgrhna
I R
PSiE
5
% w
151
B x db#
9 v # C x $
B x x dbx @"
9 v # C x $ #
B
8%$ `
B
8%$ `
B
8%$ `
B j 6%& x h 68
e 5
5
e
2
8%$ `
$
j { @
j
G $ C
7Eh
`
G &
G
G 6
88@
f
25 Mounting
G@ ` @
G
# &
&8 @
# &
G $ C
7Eh
& 3 %@
6
G
G 6
88@
&
% 3
` @
6
&8@
6
e 3 @
$ 6
#
#
#
#
#
#
f 3 @
$ 6
25 Mounting
152
sync, where any changes are written to the filesystem before processing continues).
remount When the filesysem you mount is already mounted, to change the mount options, you
use -o remount.
defaults equivalent to rw, suid, dev, exec, auto, nouser, and async.
# x $
%'x @"b#
$
G G $ &
@ ` @%@
` @
6
This means that any user can access the floppy disk with any of these commands. The user cannot
specify any options to override the options listed in /etc/fstab.
"%Fg e
Q R T U S W Q
S T W S Q
@"Fg e
"%D@"Fg e
Q R T U S W S T W S Q
The lines for a CD-ROM are typically something like this (from various machines). All of the fstab
lines specify user (so that a regular user can access the media) and noauto (so that the media is not
mounted by root during startup).
B
B
W f g g U Q
'XQ @
Q @U
g
V W W W f g g U Q Q
X'XQ @X
Q @U
g
Q W Q g U Q W f g Q f
XXQ @X'F %'
Q S V
E @
W S Q V W S V
@
@
Q S V
S V f W S
'i
W S Q V W S V W S
Q S V W S
25.5 Review
Quiz questions
1.
2.
3.
4.
Write the full command to mount the first partition of the third scsi disk on the /mnt directory
How would you allow a non root user to mount a cdrom on /mnt/cdrom?
How does one prevent a filesystem from being mounted at bootup?
Who can use the umount command?
Assignment
1. Create an ext2 filesystem on /dev/fd0 using mke2fs. Mount the floppy disk as read-write and
copy /etc/passwd onto the floppy. Remove the floppy disk and set the read-only tag. Configure
your system to mount the floppy disk read only when you boot up. Does it work? If not, make it
work. What can a floppy disk like this be used for? What happens if the floppy disk is not in the
drive when the system is booted?
2. Configure your system so that a regular user can mount and unmount the floppy disk, but ensure
that no executable files on the floppy disk can be run. Test whether your changes work. (You
will need to undo the effects of the previous assignment.)
3. Create a loopback filesystem, and mount it on /mnt/loop. To create the filesystem you can use
the following script or any other method.
` @" ` e &
v
5 #
e 6p EF ` @"5 ` B%@5 ` H
5 9 6
Which option is required to mount in order to use the filesystem you have created?
25 Mounting
153
1.
2.
3.
4.
26 Quotas
154
26 Quotas
I hate quotas.
Ralph Waldo Emerson, when his system administrator
said he couldn't have another 50 Megs
Quotas are the nemesis of disk hungry users and programs. Actually, they cause problems. Quotas
enable the administrator to restrict the amount of disk space available to a given user or a given
group. Quotas are often used on web servers to restrict the size of a user's web directory. Quotas
on mail servers prevent a single user's mail from filling up the disk.
26.1 Overview
In order to use quotas, the kernel must be compiled with quota support. When quotas are enabled,
the kernel notes each operation that causes a change in the amount of disk space allocated to a user
or to a group.
The quota commands are used in the following sequence.
1. Add the usrquota or grpquota option to /etc/fstab for the filesystems that require quota support.
2. Run quotacheck -avug to determine the amount of disk space allocated to each user and group,
and create the quota files quota.user and quota.group in the root directory of the filesystems.
3. Run quotaon to enable quotas.
4. Run edquota to set quotas for users
5. Run repquota to view the usage of quotas.
The quota commands discussed in this section have the following switches in common:
-v verbose
-a all file systems with quotas as listed in /etc/fstab
-u user quotas (the default)
-g group quotas
The edquota command allows the administrator to edit the quota for a user or a group. edquota's
To make these quotas come into effect, we run quotaon -avug. This requires quota support in the
kernel, which may be compiled as a module.
h
j
@"z$8 % 3 a e G w m f
X $
&
! ! ! ! ! C
%%E!
f
l
G Xz8 %& 3 h a e G w j 'h e
$ $
! ! ! ! ! C
%%E!
j
X $ f
@"z$8 hh a 8 e G w e {
! ! ! ! ! C
%%E!
j
G z$8 hh a f8 e G w e {
X $
! ! ! ! ! C
%%E!
t U W Q t U i f
P% P%R 4R 0 db `
ca
# $
f
8 ` f { l 48% l l s a 3
v 3
v $
# )% 3 f 3 @B%na 3
( &
$
6 # # # $ ~
v $
l e v 3
$
6 # # # $ ~
v $
g U i
@8@pr g 0 db `
V W V U Q
ca
Okay, so lets use the -m flag, since there aren't any programs writing to the filesystem. If
you want accurate quotas (usually) you would shut down to single user mode with init
single before running quotacheck, without -m.
X # v
% 3 H% ` 2@ ` 'H@"B5@E'8 `
$
& ! &
#%C&'% @@9@ s b 3 @%P8FP 3 @E8"F
$ G $ G $
X
#
& $ 6 #
@p7%#P% 3 2B%B@E'8 ` %P$ s a 3
! $ & # # & &
# & # #
v
X X X # $ 6 #
y%@2B87$ 9d $ s b%
X $ #
# t w
7"7"@EP@"z8 % 3 4 ` j! % A a 3
$ G $ C X $ $ & $
v
g U i V W V U Q
8@pr g 0
Q U Q U W Q
g sbg %
W f g U Q U W Q Q U Q U W f g U Q U V
P'bg % 4g sbg %P'bg %4 g Q 0
$
$
ca
db `
ca
db `
Now it can't find the file. So let's give it the file. For version 1 quotas, it would be
quota.user and qouta.group. We'll use version 2, aquota.user and aquota.group.
l 3 @2 `
$ 6
3
$
0 db `
ca
0 db `
ca
0 db `
ca
# & # &
%DH@E'8 `
v &
H 3 H5@E'8 `
X 8@P 3 EC
# G $
#
` d $ s a 3
#
v $
g U i V W V U Q
8@pr g 0 db `
ca
G $ C
7Eh
`
G &
G
88@
G 6
f
f
8%$ `
8%$ `
8%$ `
ej 56%& x h 68
5
8%$ `
8%$ `
B
e2
G $ C
7Eh
`
G &
G
88@
G 6
f
f
8%$ `
8%$ `
8%$ `
e 5
j 6%& x h 68
5
Ug % sDg 'g
Q U Q f
U Q U Q f
g % sDg 'g
B
e
2
e
G $ C
7Eh
& 3 %@
6
G
88@
G 6
f
&
% 3
I U f T W U
@78"V F V
I U f T W
@78"V E
e
G $ C
7Eh
& 3 %@
6
G
88@
G 6
f
&
% 3
I U f T W U
@78"V F V
$ 3 @
6
#
#
#
#
#
#
$ 3 @
6
5
% w
0 db `
ca
0 db `
ca
$ 3 @
6
#
#
#
#
#
#
$ 3 @
6
5
% w
0 db `
ca
26 Quotas
155
$
%
&
E'
e
$
8 `
f 3 @2B 3 PE'5%
$
6 # $ & v
f $ 3 @2B 3 PE'H
6 # $ &
a
xr hh %uq 5@"B ` H7
t
$ v
i U Q e( t # t
W9g y)Hw u
yh
f 3 @
j
$ 6
f
l 3 @
$ 6
$%
$ "" @E'8
v
&
t
%uq 5@"B ` H7
$ v
U Q e ( t # t
g y)Hw u
j
E'
&
a r
x hh
m
m
{ f
U Q T d R Q H d d R H
V8!(a0G!S@S0a%SAE
V S T e ( t
74y)H `
{
l l l { j
l
m
m l { e f { e e { 8 h {
e e
f
j m
f f m
l f
el 8j j { @f l m e 8 e 8 h E8
l f
m
h
l f
f j m m
m h l
h e f f
jl e { h
l
j
m { e { m l e e { @8
h h m
m f m
h { 8@8
h
f f f
T P R P d Q Q C
4S(Q66%Sfa I%E
f R e ( t
By)H `
$ v
PH7 m m v na
a j l
# $
X $ &
b 3 PE'H ` @"kb$ ` %vxr f x f
x
C a
X $
b PB ` @"k%Evxr f x f
x # # $ C a
W Q S c p p W i Q V W Q S c
ky' Bk u g Q 2"y)H `
t
V W R e ( t
w u
t
8
e f
e {
e
{
f e
w u
t
3 %
q
q
w u
t
Let's create a stack of files (using touch). We only get so many. So the quota does seem
to be working.
$
B j
X
B j
''pg Dy)H ` w u
V f i
S e ( t
t
j
F8 o
l
#
48 o
$ v
PH7 e v %vb
a j m
# C a
Xb 3 PE'5"4@"kb$ ` %vxr f x f q
$ & v x
C a
X $ v x # # $ C a
b PH"4@"k%Evxr f x f q
r ` f I
'82 u %68B76T 5Dy)H ` w u
t ` T Q Q W W S ` S S e ( t
t
There's not much one can do with 60 blocks it turns out to be 60kbytes on this system.
` @
$ 3
#
8
$ 3
a r
x hh
f
` @
%uq
l 3 @
$ 6
v
""
&
@E'8
t
5@"B ` H7
$ v
The blocks and inodes columns specify the amount of blocks and inodes actually in
use, according to the kernel quota system.
Disk quotas are a really good reason not to get on the wrong side of the system
administrator.
$ 3
` @
#
8
$ 3
a r
x hh
` @
%uq
' } | w 9 $ G &
x@ '& X s h'
c
f 3 @
$ 6
l 3 @
$ 6
v
""
&
@E'8
t
5@"B ` H7
$ v
W q U Q S
Q 4g W 0
156
1. Set up quotas on a filesystem as explained in the notes above. Edit quotas for a user and allow
the user 50Mb more space and 50 more files than the user currently has available. Run the
command dd if=/dev/zero of=dir/$RANDOM.junk bs=1k count=1000 until the quota is reached,
and write down an explanation of the behaviour you see.
2. Set the grace period for users exceeding the soft limit to one minute, and explain the behaviour
of commands when the quota is exceeded.
Assignment
1.
2.
3.
4.
5.
Name 2 Linux filesystems that support quotas, and name one filesystem that does not.
What happens when the soft limit is reached?
What happens when the hard limit is reached?
Who can adjust filesystem quotas?
If changes are made to the filesystem while quotas are not enabled, which command must be
used for quotas to work correctly?
Quiz questions
26.5 Review
e
j
yh
!
!
t
e
m f
%
$
E7 j
@8@f
!
8o
v $
t
j h
hj
8j
!
!
&
%
j
hh
e { h
!
!
@8
!
!
G
7%
!
!
C
#
e
!
!
G
7
f
l
j f
!
!
! !
!!!!!!!!!!!!!
! ! ! ! ! !
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
$% $ 3 ` @
@"
$
% $ 3
` @
@"
@E|
8E&'H
& v
8E'5%
E7 l b%'B% 4# t E7 l b%'B% 2%
$
a & $
$
a & $ v
f 3 @4"@HFH@"B ` 8@F
$
6 6 # $
G
& $ G
% 3 F8@ 0 db `
ca
The repquota command produces a report for user (default or -u) or group (-g) quotas:
i g W {
U Q S
Quotas version 2 allows you to set the grace time during which the soft limit can be exceeded.
f 3 6@2B%8F% 2"4# t
$
# G $ v
f $ 3 6@2B%8F% 4 ` # t
# G $
ax hh %uq 5@"B ` H7
r
t
$ v
i U Q e( t
W9g y)H ` w u
t
V f U I
h%8 Dy)H ` w u
e ( t
t
quota -q can be included in /etc/profile so that the user is notified about problems with her
disk space usage.
If we remove a file, we should have some inodes spare, and some space too.
26 Quotas
157
26 Quotas
158
1. reiserfs does not support quotas in some kernel versions. Ext2, ext3 and others do support
quotas. Read-only file systems do not support quotas.
2. Nothing happens. quota -q begins to display a warning message.
3. The filesystem behaves as if it is full.
4. Only root.
5. quotacheck -avug (or specify the specific filesystem)
25 Kernel patches that implement access control lists for certain filesystems are available. These are not standard
equipment on a Linux installation. Having only one owner and one group makes Linux a simpler and more robust
system.
Every file has one owner and one group25. Only the owner or root can change a file's permissions
$ C #
E@"7"
$ G #
@""7"
C $ G #
%h""7"
Eh""7"
$ & $ G #
v $ t &
8 % 3
G
"@ 3 @
dy8@8 3 @
# X
v $ X
"y8@8 3 @
C $X
y8@8 3 @
8@8 3 @
# X
` b@8 3 @
3 $"P!! e 3 ""
$ #
3 ""
$ #
& $ # $ #
%'%@""
e
e {
e
e
e
e m e
e
e e
h a { Bf
e
e
{ f a { Fe
f
j h
a {
hi a P8
e f
f% a B@f
h
e e
f
ee
ee e
f
e
e { e
e e
% z
l l m
% z
$ m j f h
7E @
l
w j
G w j {
j {
# z l h
$ 4'%f
j {
@ e
$
E { e
z l
% 4
m m e
w j j
m j
w
@
v $
t
v $
t e
4
e
%7% %!
! ! !
% % !
! !
C
! ! !
! ! %!
! !
% % !
C
!!
! ! ! !
C
% %
! !
C
!%%E!
! ! ! ! C
!%EE!
! ! C ! C
!%%E!
! ! ! ! C
!%EE!
! ! C ! C
!%%E!
! ! ! ! C
C C C
% % !
! !
C
% % !
! !
C
Y t R U I f g t U I I t Q W t f U I f g S i f R e( v $ t w v $ t
@%% 7"'88 7"PV 6%8 7"'PR 4ByxH ` u
The items that affect the permissions of a file are displayed when using the ls -l command:
The file type (e.g. a regular file, a directory, a symbolic link, etc). This is the first character on
each line in the output of ls.
The permissions of the file (e.g. rwxr-xr-x).
The user and group of the file.
LPIC topic 1.104.5 Use file permissions to control access to files [5]
Linux restricts access to the filesystem based on one of these ...
The owner of the files
The permissions of the files
The permissions and ownership of the parent directories.
This chapter discusses the permissions of filesystem objects.
Mom
No.
27 Permissions
27 Permissions
159
27 Permissions
160
using chmod. The owner can change the group of the file to any group of which he is a member
(using chgrp).
File permissions are represented with the letters r (read), w (write) and x (execute).
User Group Others
rwx
rwx
rwx
27.2 chmod
chmod changes file access permissions.
Symbolic chmod
The symbolic syntax for chmod allows you to change the permissions of a file for the category user,
group or others.
t (
)@ "( !8"E"8@
C 5 ! o ( $ ( # G
%& 3
Usage
$
user, group, others26 or all. Default is all, but limited by umask value.
5 !
!8o
@
C
And some practice ...
I d d I H d I F U R I R
26$PSy$!4@@!PawE
F U I H U d H Q Q e I T I I Q
a$006%Snl!86%xya$y88qE
d H H U d H Q Q e I d R R H
a0Sn6%(006%Snl!iGS0ya68aaAE
W R T i Q S Q V
7"q De
W R T U i f R
2R 4Be
W R T
5 e De
S Q V
W R T U i f R
2R 4Be
W R T R R g W S V
4"4 De
d H I I T
nai%@a$y88
26 It is rather easy to think you are changing the owner's permissions with o, which actually stands for other.
27 Octal is a base-8 number system, using the digits 0 to 7. Decimal is base 10 (0 to 9), binary base 2 (0 and 1) and
hexadecimal base 16 (0 to 9 and A to F). The reason octal is used is that each permission is represented as a bit
which is either set or unset, and octal conveniently allows you to group three bits into one digit.
#
% !! 1 % 1 a 2 G w j l
v $
t
v $
t
! ! ! ! ! ! ! C
E!
$
E l a 84e G w j {
f e
v $
t
v $
t l
! ! ! ! !
!
C
v # z e
yhh a 4e G w j {
v $
t
v $
t e
! ! ! ! !
!
C
z l
4'h a Bm G w j {
v $
t
v $
t e
! ! ! ! !
!
C
l
~ 1 7% s # a 2 G w 'h
| ~
v $
t
v $
t
! ! ! ! ! ! ! C
E!
l h e
G v
@78 'h a 4e G w j {
v $
t
v $
t f
! ! ! ! !
!
C
A ~ @s ! 8 ! !@s j a Bm G w e
3 s
~ w
v $
t
v $
t
! ! ! ! ! ! ! C
E!
3 s 1 w
!@s } F{ h a 2 G w hh {
j
v $
t
v $
t
! ! ! ! ! ! ! C
E!
j {
$
S U W
R 4R yxH ` u
i f
e ( v $ t w v $ t
r Pshf 0 db `
V U q x g
ca
&
E8G
# # $
B%8G t ` 88 % 3 f` F" E8P8$ 3 nb%& 3
a C $ v $ t &
v # & G # # a
r Y g e h" V 0 db `
V U q
g i Q
i S Q
ca
chmod -R changes the permissions of a directory and its files and subdirectories.
Here we remove permissions from all other users for jack's home directory, and make sure he has
write and execute permissions on his own files. We discover that we can't change the mode of the
immutable file (see the section on ext2fs attributes).
Recursive chmod
Symbolic
Octal
Example
!%%E
! ! ! ! C
! %
! !
C
! ! !
! %
C
0750
/home/user
0751
/etc/ppp/peers
0644
/etc/bashrc
! ! ! ! ! ! C
%E
0640
/etc/aliases.db
%7%
! !
C
% %7
! ! C
4755
/bin/ping
2755
/usr/bin/write
C C C
1777
/tmp
User
Group Others
Every letter in rwx corresponds to a bit: --- is 0, --x is 1, -w- is 2, r-- is 4. The idea is to
add the digits together as well, so -wx is 3 and r-x is 5.
t
!
1 s ( # G
'8 w @8 E"8@
%& 3
The octal mode of chmod sets the file permissions to exactly the value specified as an octal
number27.
Octal chmod
W R T U i f R
2R 4Be
I T d R 1 I 1 R d H
!86%xqS0fw!i2aa0Sn6%y6PPE
d H Q Q
X06%Snle
v t r q i c g f e c a Y X
4usFpi hBbdb`BW
W R T U i f R
2R 4Be
27 Permissions
161
27 Permissions
162
Regular files
G z$
X $
m a
hh { e
j
G w e {
! ! ! ! ! C
%%E!
! e ( v $ t w v $ t
F42yxH ` u
Directories
Directories are identified by a d in the directory listing. The interpretation of permissions for
directories is as follows.
r Read permission means a user can observe the list of files in the directory.
w Write permission means a user can create files in the directory.
x Execute permission means a user can open files inside the directory (provided the user has
permissions to read the actual file).
The following permissions are quite common for a directory:
drwxr-xr-x 0755 The owner can make files in the directory, and others can read the files that
are there.
drwxr-x--- 0750 The group can read from the directory, but not modify it, and all others are
barred.
drwx------ 0700 A directory for the exclusive use of the owner.
drwx--x--x 0711 All users, except the owner, can open files in this directory, if they happen
to know the file names. This permission is suitable for a web server.
For directories files, the set-uid and sticky bits have the following meanings:
27 Permissions
163
Set group-id bit New files created in the directory are given the group id of the directory, rather
than the group id of the process that created them.
Sticky bit Files in the directory can only be deleted by their owner. This permission is used for
the /tmp directory, to prevent denial of service and symbolic link race attacks between users.
Symbolic links
Symbolic links are created with the ln -s command. The owner of the symbolic link is the user that
created the link, but the permissions for a link always look like this.
C $ G ! C $ e f
%h"@B!H%h"G e a 82f
G w
v $
t
v $
t
C C C
S f f U U i f R e ( v $ t w v $ t
y%8FR 4ByxH ` u
S f f U W f i R e ( v $ t w v $ t
y%8V p7ByxH ` u
When chmod is used on a symbolic link, it acts on the file that the link points to.
`
f f
` e a 82f
G w
!5 e a 82f G w
! v # f f
v $
t
v $
t
v#H `
v #
v #
H
f
8hh
C C C
f
8hh
! ! ! ! ! ! ! C
E!
$ ! e ( v $ t w v $ t
42yxH ` u
j %& 3 2yxH ` u
e ( v $ t w v $ t
! # e ( v $ t w v $ t
` 52yxH ` u
3 %4yxH ` u
e ( v $ t w v $ t
The permissions for a symbolic link are therefore largely irrelevant, since they can not be changed.
Having a link pointing to a file does not provide a user any additional permissions to the file.
C ! v # l f
$ 3 %@B!5"& e a 82f
# # &
%H E s a "vb
v # & a $
r R U V e ( v $ t w v $ t
7E P DyxH ` u
G w
v $
t
v $
t
C C C
r R i f R e ( v $ t w v $ t
7E R 4ByxH ` u
r R Q S U f W f i R e ( v $ t w v $ t
7E 28hiV p7ByxH ` u
Pipes, sockets and devices
Named pipes made with mkfifo (p) allow programs to stream information to each other. Both
programs require permissions on the FIFO for this to work, and the sticky bits have no particular
significance.
UNIX domain sockets (file type s) allow programs on a single machine to communicate using
sockets. On Linux UNIX domain sockets respect the permissions of the directory they appear in.
The set-id and sticky bits have no particular significance.
Character devices (file type c) and block devices (file type b) behave like regular files as far as
their permissions are concerned.
27.4 umask
The umask setting of a process determines the permissions with which a file is created by default.
During login an initial value is set usually in /etc/profile.
If the umask is 000, then files are created with permissions of 0666 (rw-rw-rw-) and directories with
permissions of 0777 (rwxrwxrwx). The umask value is subtracted from the permissions that the file
would be created with. Usually umask digits are either 0, 2 or 7.
0 read, write and execute allowed.
2 no read permissions for files or directories.
7 no permissions at all.
The more common values for umask are shown in the table.
C # $
t ` HF%@ `
C h a
%C Ee 2f
% Ee 2f
C h a
h a
C
t ` Ee 2f
q T S
682 0 xH ` w
( $
T S
6q8D e 8 V 0 xH ` w
U
( $
a C
b%h$ s
g f e ( $ w v $ t
hHyxH ` u
#@9 3 CDE&8GFB%8G a $ 3
# # $
q T
68SD e 8 DyxH ` u
U V e ( $ w v $ t
C C ! ! ! ! ! ! ! ! ! ! ! ! !
%@X !
%X !
C ! ! ! ! ! ! ! ! ! ! ! ! !
C ! ! ! ! ! ! ! ! ! ! ! ! !
t ` X !
ByxH ` u
U f R e ( $ w v $ t
v $
t
! ! ! C ! C
%EE!
v $
t
! ! ! C ! C
%EE!
v $
t
! ! ! C ! C
%EE!
R 4ByxH ` u
i f R e ( $ w v $ t
G w
G w
G w
vt
$
vt
$
v $
t
The chattr command manipulates ext2 filesystem attributes. lsattr displays these attributes. The
following attributes can be set on the ext2 filesystem (and not on other filesystems):
i immutable the file cannot be changed, unless the immutable bit is removed first
a append only this file can only be appended to, not rewritten.
s secure deletion after the file is deleted, the blocks that stored its data are blanked out.
Here's an example of setting the immutable flag on a file.
hh
hh
hh
hh
hh
hh
hh
hh
a 2f
a 2f
a 2f
a 2f
a 2f
a 2f
a 2f
a 2f
G
G
G
G
G
G
G
G
w j {
w j {
w j {
w j {
w j {
w j {
w j {
w j {
%% h%84kc 2
u t r f U g Q S
l b
X
j b
X
h b
X
X
b
f b
X
e b
X
b
X
b
X
g Q c
V
e
` 'h
e
` 'h
e
` 'h
e
` 'h
e
` 'h
e
` 'h
e
` 'h
e
` 'h
a 2f
a 2f
a 2f
a 2f
a 2f
a 2f
a 2f
a 2f
G
G
G
G
G
G
G
G
vt
$
v $
t e
! !
!
!
vt
$
v $
t e
!
vt
$
v $
t e
! C
E'!
'!
vt
$
v $
t e
C
vt
$
v $
t e
! !
%
%
vt
$
v $
t e
!
vt
$
v $
t e
! C
E
v $
t
v $
t e
C
t %SBR 4Byx7EP `
S U i f R e ( v $ &
W Q S
kc %% C %S
u t
%% @4yx7EP `
u
Q T e ( v $ &
V
4q997
C C
C C
C C
C C
C C
C C
C C
C C
w v $ t
u
w v $ t
u
v $
t
v $
t
! ! ! ! C ! C
EE!
v $
t
v $
t
! ! ! ! C ! C
EE!
v $
t
v $
t
! C ! ! C ! C
E'EE!
v $
t
v $
t
! C ! ! C ! C
E'EE!
v $
t
v $
t
! ! ! C ! C
%EE!
v $
t
v $
t
! ! ! C ! C
%EE!
vt
$
v $
t
! C ! C ! C
EEE!
vt
$
v $
t
! C ! C ! C
EEE!
t WR4R 4Byx7EP ` u
T i f R e ( v $ & w v $ t
W Q S
kc %% C T
u t W R
%% @4yx7EP ` u
u
Q T e ( v $ & w v $ t
w
w
w
w
w
w
w
%% h%84kc 2
u t r f U g Q S
V
4q997
!!
! ! ! !
!! %
! !
% %
! !
%
!
C
! ! ! ! ! ! ! C
E
! ! ! ! ! ! C
%E
! ! ! ! ! C
%%E
! ! ! C ! C
%EE
C
C
C
C
077
027
022
002
Umask
27 Permissions
Directories
Files
164
27 Permissions
165
# # &
%H E s t ` da 3 "
a C
$
C
t ` 2 0 xHw
( $ #
C C ! ! ! ! ! ! ! ! ! ! ! ! !
%@X !
%X !
C ! ! ! ! ! ! ! ! ! ! ! ! !
C ! ! ! ! ! ! ! ! ! ! ! !
t ` X !
R 0 xH ` w
U f
( $
& G # # $
E8FB%8G t ` v 8@%Pn
a C
6 & # # $ a &
rt ` v ` @8H% P7%D8@%k
| C
G & $ G ! C 6 & a &
27.6 Review
Quiz questions
1.
2.
3.
4.
5.
6.
Assignment
1. Write down the commands do do the following. Create files using touch and set the following
permissions:
file1 -rw-rw-rw-; file2 -rwxrwxrwx; file3 -rwsr-xr-x; file4 -rwx------; file5 ---------x;
file6 -r-xr-Sr-x; file7 -rws------ (that's an entirely useless set-uid bit that is).
Create directories using mkdir and set the following permissions:
dir1 drwxrwxrwx; dir2 drwx--x--x; dir3 drwxrwsr-x; dir4 drwxrwxrwt; dir5 ----------;
dir6 drwxr-xr-x; file7 drwxr-x--2. Run the command chmod -R 000 ~. What happens, and why? How can you fix it? Hint: find
-type d | xargs chmod something {} \; ... repeat ...
3. Use find to create a list of the files on your disk which are world writable.
4. Sort this list into different types of files. Hint: use ls -ld to show the file type ...
Answers to quiz questions
1.
2.
3.
4.
5.
6.
28 File ownership
166
28 File ownership
I really hate this damned machine
I wish that they would sell it.
It never does quite what I want
But only what I tell it.
(All the good quotes were taken)
In Linux, you get your own files. Nobody else can fiddle them, unless you allow them to. This
chapter is about setting the ownership of files.
V f U I
h%8
DyxH ` u
e ( v $ t w v $ t
So root made another one for him (because jack didn't know how to do it himself, I
suspect).
$
% 3 "X
f a
% B
7$EF e
! ! ! ! ! C
%%E!
V f U I
h%8 R 4fR 0 xHw
i
( v $ t #
Vhf%U8I'%hfiV W4 V 0 xHw
R W r
( v $ t #
a C
b%h$ s
g f e ( v $ t w v $ t
hHyxH ` u
Unfortunately, the ownership was incorrect but that was fixed with chown. In fact,
chown can set the group as well.
V f U I
h%8
@" W 0 xHw
( v $ t #
f W f g V U q Q
E'Yr H V 0 xHw
( v $ t #
` yh a j2f G w
v $
t
v $
t
! ! ! C ! C
%EE!
$
R 4B5G 8# db `
i f R e
C G & ca
W R T
5 g Q F5G 8# db `
V e
C G & ca
x v $
r88$ 3 #8q xrityq yh EG riyq yh 68iyq yh 6%
5
v $ t 5 r v $ t 5
S e
B5G 8# db `
C G & ca
2D2db `
W S V e ca
H'%2db `
W i S r e ca
Here we do it with newgrp. newgrp creates a session with a different primary group, which is why
we have to exit.
e ` # j a 2f G w
C
j
$ #
8 3 8 t
v $
! ! ! C ! C
%EE!
` # h a 2f G w
C
j
v $
t
v $
t
! ! ! C ! C
%EE!
% %
X
X h a 2f G w j {
j
v $
t
v $ e
t
! !
C
A user can only change the group to another group of which he is a member. Here jack
discovers he is not a user.
% 3
` G @3
@3
@ 8@3
3
"X
$
"X
$
"X
$
$
"X
f a
% B
m a
hh { e
h a Bm
m h
@f a 2f
$ e
7EF
f
G w 8 e
G w e
h h j
G w
Vhf%U8 r H
I
V U q
"@"
vt
$
vt
$
v $
t
U
R
t f U I
Sh%8
V e ( v $ t w v $ t
8 DyxH ` u
v $
t
! ! ! ! ! C
%%E!
v $
t
! ! ! ! ! ! ! C
E!
v $
t
! ! ! ! ! ! ! C
E!
v $
t
! ! ! ! ! ! ! C
E!
i f
4R 0 xH ` u
( v $ t w v $ t
If jack doesn't like the file's group being users, he can change it.
28 File ownership
167
28 File ownership
168
r88$ 3 #8q xi$yq yh EG iyq yh 68iyq yh 6%
x r v t
5 r v $ t 5 r v $ t 5
S e
B5G 8# db `
C G & ca
W e
@" D5G 8# db `
C G & ca
e ` yh a 2f G w
j
$ #
8 3 8 t
v $
! ! ! ! ! C
%%E!
j
` yh a 2f G w
v $
t
v $
t
! ! ! C ! C
%EE!
$
R 4B5G 8# db `
i f R e
C G & ca
W R T V e
5 g Q F5G 8# db `
C G & ca
r88 3 #8q xri$yq yh EG 88 3 8q 68iyq yh 6%
$
x v t 5
r $ # 5 r v $ t 5
S e
B5G 8# db `
C G & ca
f S U W W Q I W e
E B 5G 8# db `
C G & ca
28.3 Review
Quiz questions
6. Under what circumstances would one use the command chmod 2775 directoryname?
7. Which users may change the group of a file?
8. Which users may change the ownership of a file?
Assignment
1. Create a new user using useradd username, but do not create a home directory for the user.
Manually copy /etc/skel to the users home directory using cp -r /etc/skel ~username/.. Now fix
the ownership of the files you copied so that they belong to the user.
2. As a regular user, use the id command to see which supplementary groups you are a member of.
Create files named after each of the groups you are a member of, with the group set
appropriately. Set the permissions on these files to -rw-rw-----. Log in as a different member of
the group, and see whether you can do the following:
a) Delete the file.
b)Rename the file
c) Edit the file using vi
d)Change the permissions of the file
e) Change the owner of the file
f) Change the group of the file
3. Change the permissions and ownership of your /bin/su so that only members of the group trusted
can use su to become root (you will have to create the group). Check that su still works for
members of the group and for root. Check that users who are not members of the group
trusted cannot use su.
Answers to quiz questions
1. To set the directory to give its group away to all the files created in it.
2. The owner and root.
3. Only root.
29 Links
169
29 Links
And we can always supply them with a program that makes identical files into
links to a single file.
Larry Wall in <199709292012.NAA09616@wall.org>
LPIC topic 1.104.7 Create and change hard and symbolic links [1]
Weight: 1
Objective
Candidates should be able to create and manage hard and symbolic links to a file. This objective
includes the ability to create and identify links, copy files through links, and use linked files to
support system administration tasks.
Key files, terms, and utilities include
ln
Link command for hard and soft (-s) links
$ $ # #
"G P%8@%H
or, more simply, to link to an existing file in another location,
$ # & #
%8@% %@%2
There are a few provisos when making hard links
The permissions and ownership of the new link is the same as the permissions and ownership of
the original file. If a user creates a link to a file he does not own, he will retain his own copy
of that file if the original is deleted.
You can't make hard links to directories (usually but you probably shouldn't want to either.)
Hard links can be identified in the output of ls by the link count, which is the second column (just
after the permissions). For regular files the link count is 1. For files with hard links, the link count
is 2 or more. (The link count for directories is based on the number of files in the directory).
C C
%%B%$
Q R R W U V e ( v # w v $ t
%P DyxH ` u
878WWyq%%g D%84 DyxH ` u
S R Q R W V R R W W U Q V W e ( v # w v $ t
C
%%D8}
W W U V e( v # w v $ t
878G DyxH ` u
l
3 a { Pe
$
7E e
v $
t
v $
t e
! ! ! C ! C
%EE!
l
# a { Pe
7E e
$
v $
t
v $
t e
! ! ! C ! C
%EE!
R 4ByxH ` u
i f R e ( v # w v $ t
W W Q R R W R e( v # w v $ t
878q%B7ByxH ` u
l
3 a { Pe
7$E e
v $
t
v $
t
! ! ! C ! C
%EE!
R 4ByxH ` u
i f R e ( v # w v $ t
Q R R W S R Q Q R R W Q V W e ( v # w v $ t
%Fq%P%4 DyxH ` u
28 Hard links are like cross-linked files in older filesystems with the difference being that the operating system
understands they are supposed to be there.
Symbolic links much like hard links, with the following differences
Symbolic links can cross filesystems
Symbolic links can point to directories
The link is owned by the user that created it
The destination does not have to exist. A symbolic link becomes useless if the original file is
$ # & ! #
%8@% %@%D5
or, more simply, to create a symbolic link to an existing file in another location, where the symbolic
link has the same name as the original file,
$ $ # ! #
"G P%8@%F5
To create a soft link using the ln command the syntax is
m h h
G w h
G w h e
m h h
G w h
G w h e
S f f U W S f f U
8Py%8V Py%8
e %9 j e
! ! ! ! ! ! ! ! ! m
e %%E! j e
! ! ! ! ! C j m
e %9 j e
! ! ! ! ! ! ! ! ! m
e %%E! j e
! ! ! ! ! C j m
i f R e G & w v $ t
8R 4By( B ` u
Using ls -i (show inode numbers) you can see that the link and the original file share the
same inode number, which makes them equivalent.
& # # $
E8GFB%8G $ 3 v 8@%Pn
a C
6 & # # $ a &
&
E8GF#B%8G b%h"G v 8@%Pn
# $
a C $
6 & # # $ a &
Q S U f S f f U T i
8hpy%8Dp Dy( B ` u
e G & w v $ t
# # &
%H E s $ 3 pb
a C a $
Q S U f
8h Dy( B ` u
U V e G & w v $ t
h
h
m h
C$ 3 i a Ee G w hh
e
! ! ! ! ! ! ! !
%!
%Ch$"G hi a hEe G w h e
e
! ! ! ! ! C
%%E!
X a { Pe
7E e { m
$
f
%e %
C C C
QSU8hfpSy%8g R 4By( B ` u
f f U
S i f R e G & w v $ t
Q S U f W R e G & w v $ t
8hiV 47By( B ` u
S f f U W R e G & w v $ t
y%8V 47By( B ` u
This example shows how permissions are shared between files with hard links. (Files
cannot be removed from /tmp because of the sticky bit on the directory permissions).
6 $
"@! 8H"6 # t b%h"@ v
a C $ G
v #
C $ G
db%h"X v H$ 3 na
v #
# $
#
S f f U W R e ( v # w v $ t
y%8V 47ByxH ` u
f a
% B
$
% 3 "X
f% B
a
f a
% B
$ X X
% 3 "X
$
% 3 "X
$ e
7EF
$ #
8 3 8
v $
t e
! ! ! ! ! C
%%E!
$
7EF e
$ #
8 3 8
v $
t e
! ! ! ! ! C
%%E!
V
hf%U8I h%8 R 4ByxH ` u
V f U I U i f R e ( v # w v $ t
e
$
7EF
8$ 3 8
#
v $
t e
! ! ! ! ! C
%%E!
V f U I U i f R e ( v # w v $ t
h%8 R 4ByxH ` u
V f U I
h%8 7ByxH ` u
R e ( v # w v $ t
When using ln to create a link to a file in another directory, specifying the file name is
sufficient.
#
a { Pe
C C
%%B%$
W U V e( v # w v $ t
878WG DyxH ` u
v$t
v $
t
! ! ! C ! C
%EE!
R 4ByxH ` u
i f R e ( v # w v $ t
Q R R W e ( v # w v $ t
%B DyxH ` u
$ e
7E
When one of the files pointing to the data is removed, the data remains accessible via the
other file.
29 Links
170
9
# ! C
2!H%
C !
%2!H 3
87 ` !!
6
9 ! #
G!H
3 2!!5 `
` !H867 `
!
X
X
X
hi
hi
hi
hi
hi
hi
hi
h
i
a { Pe
a { Pe
a { Pe
a { Pe
a { Pe
a { Pe
a { Pe
a { Pe
a { Pe
29.3 Review
There is a limit to the number of symbolic links that Linux will follow before arriving at
the file.
C $ G ! C $ h
%h"@B!H%h"G Ee a { Pe
$
7E
v $
t
v $
t
C C C
S f f U i f R e ( v # w v $ t
y%8R 4ByxH ` u
S f f U W f i R e ( v # w v $ t
y%8V p7ByxH ` u
$ X X
% 3 "X
$ X X
% 3 "X
!B% 3 "X
! $
f a
% B
e a { Pe
$ e
7EF 8 3 8
$ #
v $
t
! ! ! ! ! C
%%E!
7E
$
v $
t
v $
t
C C C
V f U I
h%8 h%8 R 4ByxH ` u
V f U I
i f R e ( v # w v $ t
V f U I
h%8 p7ByxH ` u
f i R e ( v # w v $ t
When using ln to link to a file in another directory, specifying the file name is sufficient.
3 !9
! #
3
3 !9
! #
3
$
7E h
v $
t
v $
t
C C C
R 4ByxH ` u
i f R e ( v # w v $ t
Q R R W e ( v # w v $ t
%B DyxH ` u
C C
%%B%$
Q R R W U V e ( v # w v $ t
%P DyxH ` u
$ e
7E
v $
t
v $
t
! ! ! C ! C
%EE!
l
a { Pe
7E h
$
v $
t
v $
t
C C C
R 4ByxH ` u
i f R e ( v # w v $ t
W W Q R R W f i R e( v # w v $ t
878q%p7ByxH ` u
l
a { Pe
e
7$E
v $
t
v $
t
! ! ! C ! C
%EE!
R 4ByxH ` u
i f R e ( v # w v $ t
Q R R W S R Q Q R R W Q V W e ( v # w v $ t
%Fq%P%4 DyxH ` u
171
29 Links
10.
11.
12.
13.
14.
172
How can you use ls to identify which two files are hard links to each other?
Why does an empty directory show contain 2 links?
Which flag for ln will request it to overwrite an existing file or link?
What happens when you delete a file to which a symbolic link points?
What happens when you delete a file to which a hard link points?
Assignment
1. Create a symbolic link to /etc/profile. Copy the link using cp. Do you get a regular file or a
symbolic link? What option to cp changes this?
2. Create symbolic links in your /etc/skel directory pointing to /usr/share/doc and /etc. What effect
does this have when you add a new user using useradd or adduser? Does the user get a symbolic
link or a regular file?
3. Run the commands below, and find the reason for the output:
! e ( v # w v $ t
42yxH ` u
# $ e r
dr y)P5Hd@ " !yq ` yxH ` u
! $ q q e ! # r r o o $
$
5 $q
e ( v # w v $ t
3 %4yxH ` u
e ( v # w v $ t
Hint: check which of the files you can read...
4. Copy /etc/hosts to your home directory. Create a hard link to this file named link1. Now create
a hard link to link1 named link2. Change the permissions of link1 to 0600. What happens to
the other files? What happens if you change the group of the link link2? What happens to the
original file and to link2 if you delete link1? Explain why the behaviour is as you observed.
5. Create a regular file named symlink using ls > symlink. Now create a symbolic to the current
directory named symlink using a single command. What happens to the previous contents of the
file symlink?
Answers to quiz questions
1.
2.
3.
4.
5.
6.
30 Finding files
173
30 Finding files
Making files is easy under the UNIX operating system. Therefore, users tend to
create numerous files using large amounts of file space. It has been said that the
only standard thing about all UNIX systems is the message-of-the-day telling
users to clean up their files.
System V.2 administrator's guide
With hard disk sizes getting awfully big, it is wonderful to have an orderly arrangement of wheredata-goes. The basic separations are between program code and data, between static data and
dynamic data, and between shareable files and non-shareable files.
LPIC topic 1.104.8 Find system files and place files in the correct
location [5]
Weight: 5
Objective
Candidates should be thoroughly familiar with the Filesystem Hierarchy Standard, including
typical file locations and directory classifications. This objective includes the ability to find files
and commands on a Linux system.
Key files, terms, and utilities include
find
find by searching the filesystem
locate
find by querying the locate database
slocate
secure version of locate (enforce user permissions)
updatedb
update locate's database
whereis
look in common locations for a file
which
find the path name of a command
/etc/updatedb.conf
configuration file for slocate
7"
G &
6 7
#
"'
G
G
8@
#
&
&
% 3
$
"6
6
@ 8 "
#
H4db ` t
f R
ca w v $
29 Other UNIX systems do not enjoy the same level of standardisation as Linux does.
30 Finding files
174
root has a separate directory outside the /home directory to maintain the separation between
users and administrators.
/bin, /sbin, /lib These are the directories containing the essential programs for booting the
system. Program files (binaries) are stored in directories named bin. The main bin directory
contains files which are required by scripts (and also the script interpreter /bin/sh). The sbin
directory contains supervisor binaries, such as network configuration and testing programs. The
essential library files are stored in /lib.
/usr This is the secondary hierarchy of the filesystem. Just about everything ends up in /usr
everything users need while operating the system. In the /usr directory, there are sub-directories
for binaries (/usr/bin), supervisor binaries (/usr/sbin), and library files (/usr/lib). For example, /
usr/bin contains programs, similar to /bin, but not vital to the system's basic operation.
/var Variable files that are modified by programs are stored here including, log files and files
in transit.
/opt Complete add-on applications like netscape and kde tend to end up here. Each program
is stored neatly in its own sub-directory, not overtly interfering with the rest of the system.
/mnt, /cdrom, /floppy These directories are used for file systems that are mounted
temporarily. To access a CD-ROM, you run mount /cdrom and its files appear at /cdrom.
/dev The files in /dev represent devices which may be attached to the system, such as hard
disks, etc. The files are generally character devices and block devices.
/boot Files used by the boot loader (e.g. LILO or GRUB).
/proc Originally for providing an interface to process information, /proc now provides a
general interface to the kernel. The /proc interface is used to set networking and virtual memory
parameters, for example.
In addition to specifying directories, the filesystem hierarchy standard also specifies the presence of
specific files in specific directories. Here are a few ...
/bin cat, chgrp, chmod, chown, cp, date, dd, df, dmesg, echo, false, hostname, kill, ln, login, ls,
mkdir, mknod, more, mount, mv, ps, pwd, rm, rmdir, sed, sh, stty, su, sync, true, umount, uname.
/etc - csh.login, exports, fstab, ftpusers, gateways, gettydefs, group, host.conf, hosts, hosts.allow,
hosts.deny, hosts.equiv, hosts.lpd, inetd.conf, inittab, issue, ld.so.conf, motd, mtab, mtools.conf,
networks, passwd, printcap, profile, protocols, resolv.conf, rpc, securetty, services, shells,
syslog.conf (if the associated programs are installed).
/sbin fastboot, fasthalt, fdisk, fsck, fsck.*, getty, halt, ifconfig, init, mkfs, mkfs.*, mkswap,
reboot, route, swapon, swapoff, update (all of these are optional, but usually do appear).
If a system is FHS compliant, it is possible to mount /usr as a read-only filesystem. This means it is
possible to share a single /usr filesystem using NFS (since there is no conflict between systems
wanting to make modifications). Apart from essential files for booting up, the bulk of the files
making up a standard installation appear in /usr.
/usr/bin, /usr/sbin, /usr/lib Binaries that are not essential to the system starting up appear under
the /usr tree. Again, bin is for everyone, sbin for the supervisor, and lib is for the libraries that
these programs require.
/usr/local this is the third hierarchy, for files which are used only by the local site e.g.
programs compiled from source code, local scripts and such like. /usr/local is often used for
installing plug-in applications like /opt is. In most distributions, /usr/local is untouched by the
30 Finding files
175
The /var hierarchy is for things that change during the running of the system. This includes mail
and printer spool directories, administrative and logging data, and temporary files.
/var/lib state information for programs. Programs such as logrotate, NFS client utilities and the
PCMCIA utilities store their state here.
/var/lock Lock files. Lock files are a way for programs accessing a single resource to make
sure that they don't fight with each other.
/var/spool Various message queues go here, such as printer queues,
/var/log Log files (what has been happening). The most important log files are /
var/log/messages and /var/log/mail or /var/log/maillog.
/var/run Data used by running processes. Mostly this contains files with the current process ID
(PID) of processes (such as /var/run/crond.pid).
/var/tmp This is very similar to /tmp, but files might not be deleted quite as often.
30.2 find
The find command searches the specific directories for files that match your criteria.
( $ ( X X
%%uyX
e
#
`
To find a file on Linux, you often know the name of the file, and something about its location. To
use this information with find you would use the -name criterion, or -iname (case insensitive).
dy8@8 3 %'%94 `
# X
& $ # ! #
The find command is discussed in more detail in the chapter on File Management.
30.3 locate
Although locate is not installed by default on many distributions, it is a useful tool. When locate is
installed and set up, you can use it something like this:
You may notice that locate runs significantly faster than find. The reason for this is that locate does
9 X # $ & # $ & # a
%X y8 E@E@ $ 3 %7" D8" y
f R f W W e ( v $ t w v $ t
P%yxH ` u
The whereis command searches a list of commonly used locations for the file name specified. It
tends to find man pages, configuration files and similar lost objects.
~ 1 w
"} @s s 8G
|
~ s 8G
|
'
S# D ` 8H"H7" Hh!"} @s s
$ G & $ 6 G & G & ' 5 ~ 1 w
|
j $
' j { @H4 ` 4 ` h2 ` 4 ` D~ 88@'!~ s
$
& G
$
# G 6 ' 5
|
T Q V S U S g W U V e ( v $ t w v $ t
% YI W V F DyxH ` u
When slocate updates its database, it uses the file /etc/updatedb.conf. This file explains which
directories and filesystem types should be excluded from the slocate database30.
!@8G "7"
! $ #
$ #
%"7"
$ ! $ #
5!H8"7"
I W XW
S U S g
e
e
e
e
e
e
XW
U V Q R
$
l
$
C C C
e y@f
# z j j $
%7% !
! !
C
l
$
C C C
f 7"'R 4ByxH ` u
U V Q R
I f g i f R e ( v $ t w v $ t
slocate is just like locate, except that it records the permissions and ownership of each file so that
users cannot see files they do not have access to. (locate achieves the same result by running
updatedb as the user nobody when run from the cron.daily script.)
In order to have permissions to read the database, which contains privileged information, slocate is
a set-gid application. If your system has slocate installed, you can still use the locate command,
which is simply a symbolic link to slocate. updatedb is also a link to slocate, but it has the good
sense to update the database when you run it in this way.
30.4 slocate
` C 867$H
#
$
@8G
$
7
3 %
` 87$
C # 6
#
`
G
@
$
"@" 4
$ $
H
G $ # G $
7%F
#E!@s
t 3 s $
# t 3 s $
E!@s
not search the entire filesystem, but searches a database which was created by updatedb. This
works out significantly faster. The one drawback is that the database is not always up to date. If
you try to find a file that was created recently, it will not be in the database.
30 Finding files
176
15.
16.
17.
18.
19.
Quiz questions
30.7 Review
j
j ! h X E& @#
! G
q R V t T i e( v $ t w v $ t
w@%Q B " %y8Wp DyxH ` u
One of the most useful applications for which is together with rpm, to find the package which
provides a specific command.
i S U W i
"7i
p S i Q f x Q S i Q f i i f U R
XWR8h8)7h4%U
'R" Q7i@iip V" 7"I'fgqf%dpD`8 V" %B6db$ 3 t
U R U
f U R U ca w v $
G ! G
Y7H8@d5 3 3 $
C $
" %B6db@"7%t
V f U R U ca w v $
Many distributions define useful or unusual aliases for which. The redhat example below
corresponds with the recommended usage from the which man page.
r #
i"v
# $ # #
# #
HHka 3 3 @"7"
C #
S V V I f g e ( v $ t w v $ t
2 " 7"'yxH ` u
3 "
#
Q 2 " 7"'yxH ` u
V W V I f g e ( v $ t w v $ t
G #
%C "
S B " 7"'yxH ` u
V I f g e ( v $ t w v $ t
#
8"
f R
" 7"'yxH ` u
V I f g e ( v $ t w v $ t
Heres a few examples of using which for satisfying idle curiosity.
# # $ # #
# v $ t & #
"8 % 3 a " j 8 7" a 8 "7" a "@87" a "7" a "
DyxH ` u
u t Q V W e( v $ t w v $ t
When you enter a command, the shell searches the directories listed in the $PATH environment
variable for the program to run. The command which searches for, and prints out the location of the
command which is executed.
3 C
3 C
3 C
3 C
3 C
3 C
3 C
3 C
9 X
%X h y8@8 3 h E@E@ $ 3 %7"
# $ & # $ &
v $ X
"y8@8 3 @D8@8 3 @H dy8@8 3 @9y8@8 3 @ y8@8 3
# X
C $X
a
f f Q f W W e ( v $ t w v $ t
"7E%yxH ` u
9 X C # $ & # $ & C #
%X y 3 @ E@E@ $ 3 %7" D 3 @"7" y 3 C
a
f W W f W W e ( v $ t w v $ t
% %yxH ` u
30 Finding files
177
30 Finding files
178
Assignment
1. Write down where would you expect to find the following files, and them find them using the
tools listed in this chapter.
a) cat (show file contents)
b)ls (show directory listing)
c) traceroute (find route to given network)
d)ifconfig (set up network interface configuration)
e) syslog.conf (configuration file for syslogd)
f) cat.1.gz (man page for the cat command)
g)messages (growing file of various system events)
2. Write a command to find all the files in /etc which are smaller than 12kb. Write a command to
list all files in /etc which are larger than 12kb. Do the two lists comprise all the files in the /etc
directory?
3. Download the filesystem hierarchy standard document from the internet or find it on your
distribution media. Read it and make notes.
Answers to quiz questions
1.
2.
3.
4.
5.
/bin contains files which are essential for booting when /usr may not be available.
The contents of /bin is standardised, but anything can appear in /usr/bin.
When the files have been prepared for local conditions, e.g. customised programs, etc.
boot time vs run time when /usr is available.
which searches the directories in the PATH variable.
31 XFree86
179
31 XFree86
If the designers of X-window built cars, there would be no fewer than five
steering wheels hidden about the cockpit, none of which followed the same
principles but you'd be able to shift gears with your car stereo. Useful feature,
that.
From the programming notebooks of a heretic, 1990.
31 There is an intentional pun here its X Windows for the 80386. Actually, XFree86 is used on more than just 80386
based machines.
31 XFree86
180
Font installation
31.2 X server
The task of configuring an X server means setting up the following:
Software installation (XFree86, version 3.x or 4.x)
Graphics card
Monitor
Keyboard
Mouse
Other input devices (joystick, graphics tablet, etc).
Configuration programs
Things to configure
Keyboard the relevant information is the number of keys (pc101, etc), language (US, UK),
options
s
r s @! % # q @8 3
v
#
x $ & $ #
y8 3 E474 3 # ` t b8@B8 ` H87% 8
X $ v
# G
8F 3 FH$ 3 F88@B8@4E ` 3 ` BB@9@ s
G $ v # C
# $
s
s
s r
s %# t q
s
s
@8 % #
v !
#
@! % #
v e
#
v
@! % #
#
Mouse where the mouse is connected (serial, PS/2 port, USB), type of mouse and the protocol
used (Microsoft, PS/2, imps/2 wheel mouse), number of buttons, 3-button emulation. If you set
up a wheel mouse, the wheel corresponds to the z-axis mapping.
Choosing the mouse protocol
a # C
b@9E `
3 5 `
&
# 3 X G G & $
B@8 s b8@PF@"% ` 8'9@"
G
Monitor older monitors can be destroyed by incorrect settings for horizontal and vertical sync
range. These settings are used as protection against the X server asking the video card to drive
the monitor too hard. Unfortunately, you have to set your own levels of protection when you
configure the system.
The horizontal sync specification is the rate at which the monitor moves from one pixel to
the next.
3 j
B j { h
B m e
B e
6 f
e
B hh e
j h
B hEe
a $ 6 # 6
bH"484B87$ 3 8F9 %%D"6 3 %B8}
& &
& C
By default, XFree86 will detect the amount of video memory you have on your card. Just
occasionally, it cant and you have to tell it manually.
s #
w l % Eu f f
l # t
! #
w t
e '" @ s
# E78 f e
#
$
j 1
j s w E' 3 ! e
$ !
o u
8@ j % 1 f ~
o u
8@ j % 1 f 5 s w ED 3 e
~
$
Graphics card For version 3.x it is necessary to specify the X server to use (although
XF86SVGA supports most of the supported graphics cards). Newer hardware is a great deal
easier to configure and you will not have to enter ClockChip values, which are nearly impossible
to obtain.
The XFree86 server supports an enormous range of graphics cards.
6
Xb@"%&@6@ xb%$ ` B 3 B@"DECP#2 8GF@" z X @@
#
G &
"'$
3 B8789
6 C
` bB4H@"%D 3 B$ 3 B%'%P"@P% `
x # # &
& $ # 6
The mouse is usually connected to one of /dev/ttyS0, /dev/psaux. Whatever the correct
setting is /dev/mouse is generally set up to be a symbolic link to the actual device.
X #
dY
3
| # $ & $ # # $ C
%" f %P"7%BBEP8F
3 EP@82E ` 3 H%h"H@ s
C # # C
C # $ $
X # $ & $ #
y" f %P"7%
8
# & &
$ 3 B%H t zF@"%P7%PH@987$ 3 3
X G & # ! C $ 6
$ G
@"F P 3 H ` F8 s
#
#
X #
Y H 3 B@8 3 HH8D5
& # $ #
v $
E@28C 3 8kzY|
# X ~X
{ { {
f ! h ~ t 8C 3 8kzY|
# X ~X
3 8kzY|
# X ~X
f
e
The country or language specification refers to the keyboard language, rather than the
language you actually speak.
@"% e @~ s
@"%"
r G #
%47! f q @E7H@"%
& ~
r G #
%47! e q "7"H ` @8E
$ G &
X
X f
X e
X
31 XFree86
181
32 For XFree86 version 3.x, the command X can only be run by root. Other users must run Xwrapper.
Module (Dynamic module loading) in version 3.x modules are only used for certain input
' G h #
"@ El 8 ` 8 787" h'
# ~ #
3 $ s
#
e d Q
nW
@8 3 HH"$ ` H%vy"@$ 3 86 @9 ` H 3 `
# v $
# $ C x & G
#
$
#
86 @9 ` 0
$
' l %y'
a
#
3 $ s
#
' ' #
88 (~
" j 8 "B!!
$ 6
#
g 7" % g 'P74B2dz%78
I
f g Q i f R e ca $
" j 8 7"
#
g "2dz%78
V e ca $
The command X is used to start the X server32, and is what ends up running at some stage after
running startx. For version 4.x, each video driver is supplied as an independent module, which is
loaded when X is run. For version 3.x, a large number of video drivers were provided in a single
precompiled binary the most widely used being XF86SVGA. Because of this, it was necessary to
set up X as a link to the correct binary.
For version 4.x, X is a link to XFree86 binary. For version 3.x, X must be set to point to the X
server with compiled-in support for your particular hardware.
Symbolic link /usr/X11R6/bin/X
#$%D%#'2$864#CH84
#
h
y !
! yh
{ ! yh
8
i
The vertical sync range is the rate at which your monitor is capable of drawing the screen.
9 e
8} l w j
9 j
8} hh w j
m
9
8} l w j l
m b8} j w j l
m
x 9
~
A@ EF
m b%H8}
x $ # 9
e
e
~
A
l
m w
w u ~ # }
l
x # 'H%# t % h X m F! h X @f
w u ~
mj l x # 'n h X %f x X %f x h X @f
e
h
h h
r j m
e b"7" s hm h X %f x h X @f
h
h
x $ G &
w u ~
j m x # P8G n X %f ! h X @f
w u $ # $ ~
j w m j x # Dn h X @f
3 ED8@H%%vD8} BB%' 3
C G # & 9 v # #
#
uq
m
$%H8} m w j l
# 9 l
9 j
8} hh w
9
8}
& $
8%@%%$ 3
f
e
31 XFree86
182
' # #
a%%% # '
'
S$ s E% # '
u #
w u ~
' # ''
%%
#
6
"@
6
87%
' # ~ ' #
a(~
Pointer (Pointer configuration) - Mouse or other pointing device configuration. The two most
important parameters in this section are the Protocol parameter and the Device parameter (e.g. /
dev/mouse).
Monitor (Monitor description) one or more Monitor sections provide the specifications of a
monitor and list the video modes which can be used. Important parameters in this section are ...
HorizSync 30-57 the horizontal frequency (kHz) range supported by the monitor
VertRefresh 43-72 the vertical refresh rate (Hz) range supported by the monitor
ModeLine 800x600 ... the exact timing for the mode.
Device (Graphics device description) this defines the video adapter (e.g. VGA display
adapter). In the example below, the graphics card is automatically detected, because support for
it is compiled into the X server.
'
S$ s E'
u
' #
% # '
'
S$ s E% # '
u #
# ~ #
& $
%'$
%'$ %u
& #
#
` t
' 6 ' #
S"@ (~
# ~ #
e d Q
nW
# 6
%'Eo
h j
j j
e
e m
m
j
' #
%' 3 o EeFf% B 2 j
#
Bj { m 2 2 X @f ' j m w86%
h a #
9
8} v e X %f z%' 3 r w %uq 8} hh w j m 0
~
9 j
# 6
%'E7!
e
m h
4e'El2hh2 j4e X hEe ' m j w86%
j j
%#' 3 ! hE'hpe { { m
' #
9
8} v h X @f az%#' 3 E$@p @"# t q 8} j w m j 0
r $ #
9
3 8 ` %u
e
l ! f
l'h ! f
# ~ 9
%7%8}
&
%'$ %
%'$ %u
& #
#
` t
' # ' #
a%%(~
' #
a%%'
' #
% # '
' # #
a%%% # '
'
S ` @8E'
' $ $ G 6
%& @h'
'
8"y'
'
"'
G
j
' m ` '
' $ # $ ~
S'
# ~ #
w
G $
#"G7Eh '&
"# f %
$ &
s
6
"@
' # ' #
a s (~
# ~ #
878 v
$
% v
8% v
' $ B' #
S8@n(~
devices.
ServerFlags (Server flags) this section sets X server options, some of which are helpful in
debugging (e.g. with AllowMouseOpenFail set, the server will start if the mouse is
misconfigured).
Keyboard (Keyboard configuration) Specifies the keyboard input device, parameters and
mapping options.
31 XFree86
183
' "@@h'
~ 6
' 6
S"@ '
' #
a s s '
' &
S@"%'
' #
S@"%D @ ` s '
#
8G
#
8G
6
87%
#
`
' 6
S"@ G # t (~
' #
'8"y'
' $
S878 v '
' "'
G
'
% v '
' jm ` '
'
88% v '
' $ B
S8@ s '
' $ v
S8@y'
' $ B #
S8@% # '
# ~ #
#
8G
#
8G
#
8G
#
8G
6
87%
#
` t
' 6
S"@ G # t (~
' #
ServerLayout section what to include when starting the X server. This section lists the input
and output devices to be used. Each input device is described in its own InputDevice section.
The components of output devices (e.g. graphics board with a Device section, and a monitor
with a Monitor section) are bound together in a Screen section which is listed in the
ServerLayout section.
InputDevice section for a mouse or a keyboard. Heres the InputDevice section for a
Keyboard.
' #
S@"%%
'S@"%D @ `
#
' $ B #
S8@%
'aH%$ `
# ~
' $
S8784%$ `
#'
s '
#'
'
'
d0HiPSnxW0!yPW%lx0Sn!3
I R T U d H U eQ H d
d H I R T U d H U eQ E
0iPSnxW0!yPW%l @ 4
I T H R F F F R
8ySn6
d H I R T U d H d
0iPSnxW0!na
Q d H I e Q Q H H
P0in!G!ai3
d H I e Q H I d
0in!w0W0H
d0HiIn!w0WyWy
e Q H I eR R H
d e Q Q eR
0HiIn!@6PW%GPS
d I I T e
0HienQ!@6PW%j8ld @
6HFaPSnS0
d R
F T H R d
Q R F U
Sin6xn
Q R d I R e F
!gnS$Pg
# ~ #
"@ G # t
6
"@ G # t
6
"@ G # t
6
#
~
#
` t
' $ ~ ' #
S878%86 (~
' # u' #
a%(~
' t (~
' #
'S878%86 (~
$ ~ ' #
' # ~ ' #
a(~
' ' #
88%(~
' # ' #
a%%(~
'a87 w E(~
G $ u ' #
' 6 ' #
S"@ (~
'S"@ G # t (~
6
' #
' ' #
S%%(~
' $ ~ ' #
8%@ 86 (~
' ' #
88 (~
In the configuration file for XFree86 version 4.x there is a new top level section, the
ServerLayout section. The Keyboard and Pointer sections have been replaced by
InputDevice sections. The Device section has been supplemented by module-specific sections,
DRI and VideoAdapter.
# ~ #
# ~ ~ #
"
8%
3 8@
G
'
m
e
' m j y' j m y' j l '
'
' $ G ' # ~
x7"' (" ~
184
185
' h h'
'
S''
'
S ` @8E'
' # G G $ w
"7Eh '& '
' # $ &
8" f %y'
'
s '
31 XFree86
# ~ #
#8G
#8G
#
8G
The ModeLine configuration lines usually are not necessary for configuring XFree86 version 4.x
since the standard VESA modes are built into the server. As a result, the configuration file looks a
great deal simpler, and the ModeLine parameters are omitted.
Controls:
Left, Right, Wider, Narrower adjust the horizontal position and size of the display
Up, Down, Shorter, Taller adjust the vertical position and size of the display.
Test try the settings and see what the effect is.
Auto try the settings as you make changes. This usually leads to an unusable display.
Show - When you click Show xvidtune prints output which is appropriate for the Modeline
parameter in the XF86Config file.
e
#
%' 3 o m j
h
i j
# 6
%'Eo
j
j h m
hi Bj { m m X
' j m '
X l ! X yh a %D%'E6
# $ #
yh X m ! yh X @f a %D%' 3
# $ #
z%'E2 x z%' 3
a # 6 &
a #
&
# a # u # #
%P%%vz%vx %P%%va %u
The corresponding entry in XF86Config has the keyword ModeLine added to the front.
e j h j j
#
%' 3 o m i B 2 j
j h m
hi Bj { 2 m
m
# 6
%'Eo
X ' j m w86%
' #
It is not always possible to tune your display using xvidtune, the usual reason being that the
Horizontal Sync and Vertical Sync ranges are incorrect for your monitor.
! & h h
! ! $ & # ! ! & & ! ! $
Font names can contain the * wildcard, allowing the above to be abbreviated to the equally
obscure version below.
' l %y' 3 $ s
a
#
#
It is quite common to use 127.0.0.1 as the font server, or to use the unix socket,
The use of a font server is configured by specifying tcp and a port number:
` 8
` 8
` 8
` 8
78
78
78
78
j 8
j 8
j 8
j 8
7"
7"
7"
7"
h'
h'
h'
h'
3 $ s
#
3 $ s
#
3 $ s
#
3 $ s
#
' ' #
88 (~
Many X servers may share a single font server. In /etc/X11/XF86Config, the location of fonts is
given in the Files section:
h h
El x El x x "@8%$ `
5 # !
X X X
x e x e x x 0
e 5 9 ! # G !
4 87%$ `
X # # $
b%$ ` 2 3 %&@9@"kx @8D ` 8'9d B$ 3 F8 ` 0
G # #
x G #
h
@"G@ El 8 ` 8 787" @"@ 8 ` 8 787"
#
#
x
x & #
` 8 787" x@%788 ` 8 787" @ E@8 ` 8 787"
5 $ $
B4
#
8 ` ` 3 %@9F 3 "G 0
$
$
G 5 # !
72G@#
r & G
EE8@ 0
%%@H8G s 81 ` H 20
s ` # 1
# |q # #
$ # # # # $ &
4"5E
The font server does the hard work of translating a font from the many available formats to a
bitmap which can be displayed on the screen. Heres the configuration file /etc/X11/fs/config.
! & h h
! ! $ & # ! ! & & ! ! $
! { m @! E'! El ! El ! ! E%E%'%%8@!
h m ! & h h
! { m @! E%E%'%%!
h m ! ! ! ! ! ! ! $ & # ! ! & & ! !
31 XFree86
186
31 XFree86
187
31.7 Review
Quiz questions
1.
2.
3.
4.
Client
Limits of pixel to pixel clock and screen redraw frequency.
To translate vector fonts into bitmaps (pictures) for use by an X server.
Set the font path to include the directory, and create the appropriate directory file.
32 X display manager
188
32 X display manager
I have never seen anything fill up a vacuum so fast and still suck.
Rob Pike, on X.
Steve Jobs said two years ago that X is brain-damaged and it will be gone in two
years. He was half right.
Dennis Ritchie
Dennis Ritchie is twice as bright as Steve Jobs, and only half wrong.
Jim Gettys
(/usr/share/games/fortune/computers)
32 X display manager
189
Display a menu of available machines to log in to (the chooser). This happens when the remote
machine uses X -indirect xdmhost2
Send a XDMCP query to the selected machine to display a login screen on the X server (if the
selected machine is the machine running xdm, then it displays the login screen itself). The query
that is sent is the same as sent by X -query xdmhost2.
A login window is displayed by the display manager on the selected host.
Start a session for the user by running the appropriate window manager.
a # a
b%$ ` %a h b
I U W R g T W
@@"7EV F@"@U 8S @"7 y6da "w `
W c $
Linux distributions use different standard values for the runlevels. Debian is the one distribution
that does use special runlevels for graphical login if you install the display manager it will be
started during boot-up.
Distribution
Console login Display manager
runlevel
runlevel
RedHat and Mandrake
3
5
Caldera
3
5
Slackware
3
4
SuSE Linux version 7.2 and above
3
5
SuSE Linux up to version 7.1
2
3
Debian
2
2
In order to serve remote XDMCP requests, the following line must be adjusted in xdm-config. The
default configuration is that xdm does not request a listening TCP port. kdm shares this
configuration, and is adjusted in the same way.
a $ # $ $ G
b s @8X @%E 7"'
&
3 E%E @%EDBEP8 ` H4 3 BB% s
C $ # &
$ # $ & # $ C
# # & &
3 1 ~
3
8@8B@8 s s s ` @HPda 67t | s
# #
xdm Xresources
When xdm starts up, it loads /etc/X11/xdm/XResources as a X resources database (using xrdb).
This defines the greeting that the system displays, and other interface settings which can be
modified such as the border width.
a 8 o o o o o o b8 s %'%8
#
o a G & & $ # #
1 ~ } 1 t s B% A @8
&
a # #
G @ 8 8 @
&
a X $ # $ $ G
YG @zwX @%E 7"'
In /etc/X11/xdm/xdm-config, a line will appear specifying the script to run to set up the display for
the login window to appear on.
XSetup
One non-obvious thing in the above is that the logoFileName parameter must refer to a XPM file
(X pixmap).
$ s 8
a # #
j
a & #
& G X m E7E& "8 8 @ b%'$ 8
G $ G &
f %ka s @8
#
@ b "78
a # v $ #
{ @ a s 3 8
#
f @ a s 3 8
#
e a 3 A 8%'% 8
& $
# # #
h a 3 A %'% ` 8
& $ #
a 3 A 878
#
m m ! ! $ & # ! ! ! $ 6 ! $ ! a # #
f ! G h h
! % "7! El ! El ! ! E%8787 3 8@nb $ ` 8
f ! G h h
m m ! ! $ & # ! ! ! $ 6 ! $ ! a # G & G #
! % "7! El ! El ! ! E%8787 3 8@nb 878
m m ! ! $ & # ! ! & & ! $ 6 ! $ ! a # #
f ! G h h
! % "7! El ! El ! ! E%E%'87 3 8@nb ` 8
m f ! G h h
! @8 "7! El ! El ! e 8 e E8787 3 8@nb @8
! ! ! $ & # ! ! ! $ 6 ! $ ! a # #
32 X display manager
190
32 X display manager
191
Xaccess
xdm and kdm use the Xaccess file to determine whether a XDMCP request is accepted or refused.
For gdm there is a XDMCP configuration tab in the configuration tool. Xaccess also determines the
fate of indirect requests, which may be diverted to other hosts.
The Xaccess file assumes that you are running a number of application servers, and a number of X
terminals. The key elements in the file are:
hosts machines which run a display manager
terminals machines which run X servers (which want to connect to display managers)
There are two types of request which may be received from a terminal. These are handled
differently in the Xaccess file.
A direct query (-query). For this, the hostss name must appear on a line in Xaccess, or the line *
must appear. In the example the terminals terry, fred and joe can access the display manager.
Usually, a * appears to indicate that any terminal may open up a login window.
t
`
An indirect query (-indirect or -broadcast). In the following example, terry, fred, pam or joe, on
connecting to the display manager, will be offered connections to the three application servers
fasthost, slowhost and officehost.
@8
@8
@8
@8 3
3 ` B@8 3
`
3 ` B@8 3
`
3 ` B@8 3
`
@@8 3 { ~
C
`` B@8 3 9@8 3
C 9@8 3 @$ ` ~
C 9@8 3 @$ ` ~
C
9@8 3 @$ ` ~
}
@$
}
}
}
s
`
s
s
t
@@8 3 {
& $
'"G
`
The most common patterns in the Xaccess files allow access for any host to open a login session, or
get a window. The special name BROADCAST below means that the list of hosts is obtained by
broadcasting on the network for other display managers.
$ # $
@8 3 BH2H@8 3 $
#
#
C # C # $ # $
EP8 BH2H@8 3 $
#
0
1 ~ 8w 4~ } s
w s
As the comments in the file suggest, Xaccess can allow access for only specific hosts. The file is
(at its simplest) a list of machines that can connect to the display manager. Additional lines can be
given to allow access to the host chooser.
%4B@8 H0
C $ ! 3
%47@8 H0
C $ ! 3
C $ $ ! 3
%4B@8 H0
@8 3
!
!
7@8 3
$ !
@8 3
32 X display manager
192
@@8 3 { ~ } s
!
7@8 3 @8 3
$ !
@@8 3 {
This particular configuration in the above will allow only the machines host-a and host-b to connect
to the chooser.
32.7 Review
Quiz questions
1. Why do remote terminals use a display manager, and not just run applications with export
DISPLAY=myname:0?
2. What are the configuration files for the display managers discussed?
3. How does /etc/inittab get used for starting a display manager?
4. How do you change the display manager greeting for xdm?
5. What are bitplanes, and how do they relate to the display manager?
Assignment
1. Change your display manager to xdm, and set up a custom greeting and a custom background
colour.
2. Set up xdm to serve direct and indirect remote sessions. Test whether this works. Once you have
done this, set up the display manager to serve direct and indirect remote sessions for only one
host. Test whether it works, and test that it only works for the one host.
3. Repeat the previous assignment using either kdm or gdm.
4. If you have a second machine available, set it up as a display manager and get the chooser on
both display managers to show the other. Log on to one of the display managers, and then
connect to the other for a login session.
Answers to quiz questions
1. Changing the value of the DISPLAY environment variable assumes that you can log in to the
server that will be running the application. If you cant then you need the display manager to
send the application to you.
2. /etc/X11/xdm/*, /opt/kde3/share/config/kdm (or similar) and /etc/X11/gdm/gdm.conf
3. In some distributions, the runlevels either include or do not include xdm. In some distributions
xdm is started from /etc/inittab for a particular runlevel.
4. Modify the resources /etc/X11/xdm/Xresources
5. Bitplanes are the number of bits per pixel, and you will want to have configured your X server
with the correct values by the time you run xdm.
33 GUI environment
193
33 GUI environment
Hallo. I em ze Viper. I huf kome to vipe your vindows.
The Viper is Coming! (Unpublished work)
33 GUI environment
194
hackedbox The bastard son of blackbox, just in case blackbox wasnt small enough for you.
There is no universal configuration interface for configuring all Linux window managers, but most
window managers have their own dot configuration file or directory in the users home directory.
33.3 X applications
In the original design the X windows system, it was intended that X applications should be
customised by editing X resources33. X resources are simply configuration settings that are loaded
into the X server, usually at startup (using xrdb, but you generally dont worry about that).
In ancient times34, when twm, mwm and olwm were the window managers of choice, window
managers were customised by editing the X resource files. In modern times, editing X resource
files is still the way to customise a number of useful applications using X, including xterm. X
resources make it possible to change these values on the fly as well, without editing the files.
When X starts, the X resources are loaded from the following files into the X server by xinitrc:
1. /usr/X11R6/lib/X11/Xresources system-wide X resources
2. ~/.Xdefaults default resource settings users dont usually edit (only in some distributions)
3. ~/.Xresources resource settings that users do edit
The format of X resource files is as follows:
The name of the resource, followed by a colon, followed by the value.
The name of the resource can be abbreviated with a *, in which case any number of characters
can match (actually, only a complete section name can match).
The name of each resource is in the form application.item, e.g. xterm*font.
You should have different values to these if you run the commands on your server.
I 2db `
S e ca
a %S
$ ~ &
`
`
e
a # 6 $ ~ &
y88687S
j
a 3 E'"7S 1
C $ ~ &
e
a 3 A $ 3 "7S 1
C $ ~ &
j a
b 3 @ 3 "7S 1
$ ~ &
f
a 3 A 87"7S 1
$ ~ &
W ip g WpI 2db `
W
W i
S e ca
I T eI T H R Q I H F
(8lw8y%0Px
33 The xrdb man page says Most X clients use the RESOURCE_MANAGER and SCREEN_RESOURCES properties
to get user preferences about color, fonts, and so on for applications.
34 Ancient times i.e. before last year
$
r
m
r
l
r q b@X
X
e f
r % q j
e
r
Every X application communicates with an X server using the API provided by the X11 library.
This library must be present for the application to work.
e 88@
G 6
e ca
@F2db `
A terminal emulator lets you run console based programs on a virtual console.
You can run a couple of terminal emulators at the same time.
You can set the font and the console size.
Some terminal emulators let you run multiple sessions in a single window.
An X terminal emulator usually has a large scroll-back buffer more than the Linux console.
Here are some of the terminal emulators available for X on Linux.
xterm the original X terminal. Other terminal emulators are based on this one. Some copy its
code.
rxvt a lightweight clone of xterm (based on xvt)
aterm aterm is a pseudo transparency terminal. It is supposed to be resource friendly and has
many features like fading (darkening/lightening of colors when aterm is losing focus) and tint
color (pseudo transparency background color).
konsole KDEs terminal
gterm Gnomes terminal
Each session gets a different virtual terminal. As a curiosity, the tty command can be used to
establish which particular console is active.
`
a # &
b ` S
a # v $ &
b "7S
3
A
33 GUI environment
195
r { q e b@X e 878787" B5 e b@X e 87
X !
X !
r l q
b@X e %@%# 78787" B5 b@X e %@%# 7
X ! # G &
X ! # G &
e
r q
Xb@X e %%@%# 78787" B5 b@X e %%@%# 7
! # G &
X ! # G &
S U W
S W i i W Q I f g S S R e ca
Q @%7% W " @ 7"'PB2db `
j
r { b@X @8 78787" Byb@X @8 7
q X s s 5 X s s
h m h
r yhi b@b78787" Byb@b7
q X X v 5 X X v
l h
r ` 'i q b@b%%78787" B5 b@b%%7
X X & $ $ v
X X & $ $ v
l h
r 'i q b@z7878B5 b@z7
X X
X X
h
r ` i b@X 7878787" Byb@X 787
q X v 5 X v
$
r 8 hi q b@X 7878 j 8 7" B5 b@X 7
X #
X #
f e h
r 'i b@X ` 78787" Byb@X ` 7
q X
v 5 X
v
f
r % b@z%78787" Byb@z%7
q X X v 5 X X v
r { b@b%78787" Byb@b%7
q X X v 5 X X v
f
r 8 q e b@z%78787" B5 e b@z%7
X X $ v
X X $ v
S U W
8 7"'PB2db `
U R U r I f g S S R e ca
r q e b@X 878B5 e b@X 878
X
# !
X
# !
e
r { ` q j b@z"7878B5 j b@z"7
X X
X X
e
r m q j b@& 7878B5 j b@& 7
X X
X X
r $ ` q j b@X 8 7878 j 8 7" B5 j b@X 8 7
X
X
r q j b@b 7878 j 8 7" B5 j b@b 7
X X
X X
r f q j b@z 7878 j 8 7" B5 j b@z 7
X X
X X
r q e b@z87878B5 e b@z87
X X
X X
Xb@X e X "7878787" B5 b@X e X "787
!
X
!
e X !%%@78787" B5 b@X e X %%@7
&
X
! &
q b@X e X %78787" B5 b@X e X %7
X
! v
X
! v
b@X e X %78787" B5 b@X e X %7
X
! v
X
! v
I f g S S R e ca
7 7"'PB2db `
$
r "
r l q
r
r
q
b@X
X
l
e
It is quite common for X applications to interface to the X11 system via a number of additional
libraries. So, for example, KDE applications require the qt libraries, which insulate the application
from the intricacies of talking to X directly.
The following libraries are quite popular for X applications:
qt this is the underlying display library used by KDE applications. Some applications are built
directly on qt.
gtk the gimp toolkit. This is the library originally written for the graphics program gimp, but
now used by a number of other applications.
gnome the gnome user interface has its own set of libraries.
kde the K Desktop Environment also comes with a set of libraries supplying functions to K
applications.
Here are more than just a few examples. You will notice that KDE and gnome are both heavy
interfaces consisting of many interrelated components, while GTK is a light interface.
f
r j % q b@b"G 878787" B5 b@b"G 87
X X $
X X $
r q e b@X 878B5 e b@X 878
X
# !
X
# !
f
r f % q e b@z87878B5 e b@z87
X X
X X
f
r 8 % q b@@ ` ` 78787" B5 b@@ ` ` 7
X X # #
X X # #
e
r $ q j b@z"7878B5 j b@z"7
X X
X X
e
r { q b@X 878787" B5 b@X 87
X G &
X G &
h e
r E q h b@y8@"%78787" B5 h b@y8@"%7
X X #
X X #
P@8@7 'y g 7E 7E % g 'D` 'y g 7R
Q f
I R I R
f g
Q f
I
e l
r q j b@b 7878 j 8 7" B5 j b@b 7
X X
X X
f j
5 X X
r 8 b@& G 7878 j 8 7" Byb@& G 7
q X X
s
r 8 q j b@X t 7878 j 8 7" B5 j b@X t 7
X
X s
r f q j b@'"7878 j 8 7" B5 j b@'"7
X X ~
X X ~
r ` q j b@b 7878 j 8 7" B5 j b@b 7
X X
X X
33 GUI environment
196
197
j
r
h
r @ i q
h
r j i q
h
r ` i q
r l
r
e h
r { 'i q
33 GUI environment
3
t
# X $ G a
Y7"'db@8 3 5 w s ~ "
If the host is localhost, it can be omitted. The screen part is used for X servers which have multiple
monitors attached. If the screen is .0, that part can be omitted as well. The most common value
for the DISPLAY environment variable is to point to the first local display:
u b t Q V W e ca
66 D2db `
W FdX8@662db `
U I ` u b e ca
W gdX8@66P D2db `
c U I ` u b Q W e ca
W gdbdX8@66P D2db `
c U I ` u b Q W e ca
a
Instead of setting the DISPLAY environment variable, you can use the -display switch on
the command line.
U I U R f S i
dX82"h%7p W 2db `
e ca
r v
uYq
86 @9 3 BB5H@8 3 #
# # # $
@H 3 HF8@8 3 7%5
#
# C
86 @9 3 B@"B4"@"DB
# $ $
@H 3 BB@8 3 3 B
@H 3 BB@8 3 3 B
wB0
wB0
wB0
wB0
w
B0
e 7E
f Q
i f Q
57E
7fE7R e 7E
Q R U V Q
f Q
W@87E'57E
U f Q i f Q
W U f Q
@87E e 7E
f Q
{
{
{
{
{
35 A security note if a malicious application connects to your X display, it can do strange things like monitor all
keystrokes, and take snapshots of the screen. Apart from this, one X application generally cannot subvert another.
33 GUI environment
198
(xhost +).
Only the controlling machine can use xhost. The controlling machine is usually the machine
running the X server, or in the case of an X terminal, the login host that supplied the login window.
{ El m j hE hm hh h $
h
j j l l l m h e m h l j
s t # 1
% t B @s ! %w '! 7t a % 8b%'b `
!
# & X & $X
{ El m j hE hm hh h $ % t B @s ! %w '! 7t b `
s t # 1
h
j j l l l m h e m h l j
!
a
f R g U e ca
7@82db `
One can log into another server, and then add these credentials there.
Similarly, as root, you can obtain access to another users display (this is the default
behaviour on some distributions).
W 2db `
e ca
i W W g U } u b t i V U W g U } V i W U W f g g f e ca
B@8x66@5 2@8y7@8 'BhH2db `
` u b Q W e ca
dD66P D2db `
33.7 Review
Quiz questions
1.
2.
3.
4.
5.
6.
7.
8.
Assignment
1. Change the system-wide window manager from KDE to Gnome or from Gnome to KDE.
2. Install icewm, FVWM2 or BlackBox and make this the system-wide window manager.
3. Log into a remote machine using telnet or ssh (without the -X option) and make an X application
such as xterm run on your local display. Do this using both the -display option and the
33 GUI environment
199
DISPLAY environment variable. You can use host based authentication for this exercise.
4. Set up MIT magic cookie access control and test it by repeating the previous assignment.
5. Change the foreground and background colours for xterm by editing your X resources file. You
can import changes to your X resources file with xrdb and then test whether they were
successful.
6. Modify your .xinitrc to run xlock before starting the window manager. (This can be quite useful
for users which are automatically logged in by kdm or gdm).
Answers to quiz questions
1.
2.
3.
4.
5.
6.
7.
8.
34 Glossary
200
34 Glossary
0x00c0ffee
1024
about
ASCII
argument
bash
BIOS
bleeding edge
boot
buffer
bus
cat
character
client
CLUE
CMOS
Hexadecimal numbers are frequently written with the prefix 0x, to indicate the
base of 16. This is the notation used by the C programming language. The digits 0
to 9 are used, and a to f represent digits for 10 to 15. Hexadecimal notation is
convenient for computer applications, because a hexadecimal digit corresponds
neatly to 4 bits.
1024, or 210 is the number of numbers you can represent with 10 binary digits, and
the number of bytes in a kilobyte (kb). A Megabyte (Mb) is similarly 1024 kbytes,
or 1048576 bytes. Hard disk sizes are sometimes quoted in millions of bytes,
rather than in true megabytes to achieve the marketing edge that 4.9% can give.
The first word in many glossaries.
The American standard code for information interchange, a standardised set of
meanings for the bytes 32 to 127. Included in this are the letters A to Z in upper
and lower case, the digits, and most common punctuation symbols. Text files in
Linux are usually encoded with ASCII.
Arguments appear after a command on the command line, and are sometimes
called parameters. If the command is ls -l -a /etc then the command is ls and
the command line arguments are -l, -a and /etc/.
An improved version of the Bourne Shell (sh), known as the Bourne Again Shell.
Basic Input Output System. This was originally intended to be some kind of
hardware independent layer providing kernel-like facilities. For most systems the
BIOS is usually used for loading the operating system which provides these
facilities.
If the leading edge software you choose doesn't work quite right, it might still need
a bit of debugging.
When you turn on your computer, it boots up. The origin of this is that the
computer pulls itself up by its own bootstraps and gets the operating system
running.
A place for storing data in transit. This is a technical term used by program which
occasionally escapes into regular usage. Linux write filesystem changes directly to
the disk, but writes them to buffers which are later written to the disk.
A set of parallel electrical conductors used in a PC for communication between
devices, such as between the CPU and memory and peripherals.
A Linux command that shows the contents of a file. This is the only Linux
command in the glossary, and it's only here because I like cats. All the other
commands are listed in the index.
A letter in the alphabet, a digit, a punctuation symbol anything that can be
represented by one or two bytes (in this sense, characters are rather onedimensional). The most commonly used representation of characters is ASCII.
UTF8 uses more than one byte to represent a single character.
In networking, the side that initiates the connection is the client. The client talks to
a server, which (hopefully) serves it.
A command line user environment, as opposed to a GUI, a graphical user interface.
Complementary Metal-Oxide Semiconductor usually refers to something
managed by the BIOS. (CMOS is not that stuff you get at the bottom of the
ocean).
34 Glossary
comment
201
Comments are written by people in configuration files to explain what the meaning
of the rest of the file is. Configuration file comments usually start with a hash
character (#).
# /etc/hosts.deny
# See `man tcpd and `man 5 hosts_access as well as /etc/hosts.allow
# for a detailed description.
sshd: ALL
console
CPU
database
default
display
distribution
DMA
DNS
DOS
environment
execute
FAQ
Reading configuration file comments is often more instructive than reading other
documentation such as man pages.
A console is a terminal which is directly connected to a server.
Central Processing Unit. Linux originally ran on the Intel 80386 CPU.
A database is an ordered arrangement of computer data. The password file /
etc/passwd is correctly called a database (not all databases are relational databases
using SQL for queries.)
What it is if you don't change it. The default behaviour of cp is not to be verbose,
but telling cp -v overrides the default and makes cp print information about the
files it copies.
A display is strictly just a monitor for viewing computer output. In X windows a
display includes the facilities provided by an X windows server monitor,
keyboard and mouse. X Windows applications allow you to specify a display on
the command line or in the environment.
export DISPLAY=remoteserver:0.0 ; xterm
xterm -display remoteserver:0.0
Because the vast majority of software for Linux is free, it is possible for anyone
with the technical capability to distribute it. A number of distributions exist as a
result, varying primarily in the method of distribution (e.g. CDROM, download),
the method of software packaging (e.g. RPM, deb, tar.gz, cvs, other). Some
distributions are commercial (e.g. RedHat, Mandrake, SuSE), and others are
deliberately non-commercial (e.g. Debian).
Direct Memory Access where your peripherals talk directly to the memory, rather
than via the CPU.
Names on the Internet such as www.disney.com are turned into network addresses
using the DNS system. From the client point of view, you simply need to know
what DNS server to ask for questions of DNS names. On Linux, the DNS
configuration is mostly in /etc/resolv.conf.
Microsoft MS-DOS is an cut-down operating system for the Intel 8088 family of
processors. Microsoft Windows 3.1, 95 and 98 contain designs influenced by MSDOS. DOS is also an acronym for Denial of Service (deliberately making a service
fail).
The environment a program is provided with is a list of variables in the form
NAME=VALUE. When the shell starts a program it passes all the exported
environment variables in the environment. The program may choose to react to the
environment variables it receives.
To start or run a program. Programs start by being executed, and they end when
they are killed.
FAQs or Frequently Asked Questions are lists of questions that are frequently
asked, and sometimes answered. If you have a question about a particular product,
and it has an associated FAQ, you may find an answer in the FAQ.
34 Glossary
field
filesystem
202
A system for arranging files, either in a fixed space, like a partition or a CD ROM,
or referring to the entire Linux filesystem, consisting of a number of mounted
filesystems.
font
A font specifies the shape of each symbol in a script. Vector fonts describe
symbols in terms of the outline. Bitmap fonts specify the exact map of bits for
each letter. Bitmap fonts look rather blocky if they are enlarged.
foobar
A foobar is a widget that does something. Sometimes written as just foo or just
bar. When you can't think of a name for something, foobar is just fine.
fortune
A Chinese fortune cookie is a biscuit containing a text, presumably related to your
future. The fortune program prints out a message which is almost as random as
what you may find in a cookie. Some administrators configure their system to
print a fortune cookie when you log in.
geometry
For computer disks, geometry refers to the coordinate system of finding your data,
the coordinates being the cylinder, sector and head to use. The geometry of a disk
is the number of cylinders, sectors and heads (disk read and write heads). LBA
addressing is more common. This geometry is more like the Cartesian plane than
Euclidean geometry.
GPL
The GNU public licence a licence document which allows you to redistribute
computer software that is copyrighted with the proviso that you must distribute
the source code as well.
group
A group is defined by the contents of /etc/group. Every file on the filesystem and
every process has a group assigned. A user may belong to a number of groups (the
primary group, and a number of secondary groups).
home directory Each user has a directory which is allocated to them for their personal files. For a
user named joe, this will usually be /home/joe, but it is possible to change this.
The user's home directory is specified in /etc/passwd. In the shell, the abbreviation
for your own home directory is ~.
host
A machine connected to a network. Similarly, the term hostname refers to the
name of a host (of a machine).
HOWTO
A HOWTO is a long-ish document explaining how to do something. This
something is often a relatively complex task, such as setting up a firewall, or
configuring modem communications. Mini-HOWTO's are usually only a few
pages long, and not as extensive as a full HOWTO.
I/O
Input and output what the CPU does when it talks to devices. Devices are
generally identified by their I/O address, a number to which only the device
responds.
IDE
Integrated Drive Electronics IDE usually means a hard disk or a CD ROM drive,
which unlike archaic disks, has the electronics integrated into the drive unit.
interrupt
When a device wants to communicate some data to the CPU, it triggers an
interrupt. The CPU interrupts what it is doing, and makes sure that it gets the data
from the device.
34 Glossary
journal
203
34 Glossary
peripheral
permission
pipe
PnP
ppp
proc
process
protocol
rc
recursive
resources
root
rwx
script
SCSI
serial
server
setuid
204
34 Glossary
shell
205
34 Glossary
variable
verbose
wildcard
zzz
206
Something that varies. In programming, variables have names, and the value of the
variable is substituted where the name is used. In the shell, environment variables
are set using VARIABLE=something. After this statement, the shell substitutes
something in the place of $VARIABLE.
A program which is being verbose will print out extra information. For example,
cp file1 file2 will quietly copy the files, while cp -v file1 file2 will print out the
names of the files as they are copied.
A special character which matches any character. For file names, the wildcards
* and ? match anything or a single character. For regular expressions, the only
wildcard is . (matching any character), while .* matches any number of
characters. In card games, the joker card is a wild card.
After reading an entire glossary, you will notice a tendency towards sleepiness.
The other option for this last entry was zebra, but that's not only an animal, but an
implementation of BGP and RIP.
35 Index
207
35 Index
An index is an alphabetical list of words and the places they occur in the text. The list at the front
of the notes was a table of contents.
`...` command substitution, 85 comm, 90
E
command line, 81
/usr/local, 174
e2fsck, 145
options, switches, 83
$(...) command substitution, 85
echo, 84
parameters, 83
1024 cylinders, 56
editing files with vi, 135
command substitution, 115
32 bit transfer, 22
edquota, 155
compiling, 65
A
env command, 84
configure script, 66
environment variables, 83
ADSL, 46
--bindir, 67
etc, 173
apt, 74
--prefix, 67
expand, 97
apt-cache, 74
cp, 106
export, 84
apt-get, 74
cp,
extended partition, 55
aquota.user, 155
recursive, 86
F
B
csplit, 90
fdisk, 55, 139
background processes, 118
Ctrl+A start of line, 85
FDISK for MS-DOS, 59
bash_history, 87
Ctrl+Alt+F1, 195
fg foreground, 119
bg background, 119
Ctrl+C interrupt, 81
file type, 159
bin directory, 174
Ctrl+E end of line, 85
filesystem, 54
BIOS, 19, 20
Ctrl+R history search, 85
Filesystem hierarchy standard
block mode, 23
Ctrl+Z suspend, 81
(FHS), 173
boot directory, 34
cut, 92
find, 107, 175
boot floppy, 62, 63
cut and paste in vi, 137
find,
boot loader, 60
D
recursive, 86
boot partition, 55
dd, 63
floppy, 152
bus, 38
deb packages, 73
fmt, 98
C
Debian, 73
fold, 90
C programming language, 65
debugfs, 146
fonts.alias, 186
cat, 91
depmod, 39
fonts.dir, 186
cc, 65
DESTDIR, 68
fonts.scale, 186
cdrom, 152
dexconf, 180
fortune, 54
cfdisk, 55
df, 143
fsck, 145
chattr, 164
dirname, 115
25.2 fstab, 56, 150
chgrp, 166, 167
disk usage quotas, 154
quotas, 154
chmod, 160
DISPLAY environment
G
chmod,
variable, 197
gdm, 190
recursive, 86
DMA, 22-24, 30, 39
gdmconfig, 190
chown, 166
dpkg, 74
geometry of disks, 20
chown,
drwxr-xr-x permissions, 162
gnome, 196
recursive, 86
DSL, 46
grep, 131
CHS mode, 21
du, 144
grep,
cksum, 90
dual boot, 63
recursive, 86
CLUE, 81
dual booting, 56
grpquota option, 154
CMOS, 19
dumpe2fs, 146
grub, 63
COM1, 23
35 Index
grub.conf, 34, 63
gtk, 196
IDE, 20
ifconfig, 48
ifup, 47
index, 207
initdefault, 189
initrd, 62, 63
insmod, 41
brute force, 41
Install rpm packages, 78
integrated peripherals, 23
interrupts, 24, 30
ioports, 24, 30
IRQ, 23, 29, 39
conflict resolution, 39
isa-pnp, 42
isapnp, 30
isapnp.conf, 30, 42
ISDN, 48
iso9660, 152
Isochronous transfers, 51
ISP, Internet service provider,
46
jobs, 118
join, 95
kdm, 190
kill, 123
killall, 123
kppp, 47
Large mode, 21
LBA mode, 21
LD_LIBRARY_PATH, 71
LD_PRELOAD, 71
ld.so.cache, 71
ld.so.conf, 71
ldconfig, 71
ldd, 70
lib directory, 174
lilo, 60
lilo.conf, 34, 60, 61
linear, 61
29.1 links, 169
hard, 169
soft, 170
ln, 169
locate, 175
logical partition, 54
LPT1, 23
lrwxrwxrwx, 163
ls, 102
permissions, ownership, 159
lsattr, 164
lspci, 25, 40, 51
lsusb, 51
LVM (Logical volume
management), 57
208
nice, 126
nl, 96
nohup, 119
od, 99
partitions,
sizing, 56
paste, 98
PATH environment variable, 85
3.8.1 PCI, 24
card configuration, 39
ping, 48
PIO (Programmed I/O), 22
Plug and Play, 41
pnpdump, 30, 42
Point to point connections, 46
power management, 24
PPP, 47
pppd, 47
chap-secrets, 47
chat, 47
M
messages, 48
make, 66
pap-secrets, 47
Makefile, 66
peers, 47
man pages, 87
pppoe, 48
MBR (Master boot record), 59 pr, 99
md5sum, 90
proc,
messages, 26
bus/usb, 51
mkdir, 104
cmdline, 62
mke2fs, 141
dma, 44
mkfontdir, 186
interrupts, 42
mkfs, 56, 140
iomem, 43
mkinitrd, 34
ioports, 43
mkreiserfs, 141
isapnp, 42
modem, 28
pci, 41
external, 28
scsi, 34
internal, 29
profile, 87
modprobe, 31, 39
prompt, bash, 82
modules.conf, 34, 39
19.3.1 ps, 120
mount, 150
niceness, 127
options, 151
PS1, 84
removable media, 152
ptx, 90
multiboot-compliant kernels,
pwd (print working directory),
63
82
mv, 107
newgrp, 167
qt, 196
35 Index
quota,
command, 156
configuration, 154
editing, 155
grace time, 157
quota_v2 module, 155
quota.user, 155
quotacheck, 155
rdev, 63
Recursive commands, 85
18.1 redirection, 90, 110
command pipelines, 114
standard error , 112
standard input, 111
standard output, 112
tee, 114
refresh rate, 182
regular expressions , 130
reiserfs, 147
reiserfsck, 147
renice, 127
repquota, 157
rm, 105
rmdir, 104
root directory, 173
root partition, 55
route, 48
rpm,
--checksig, 79
-e erase package, 79
-F freshen, 78
-i install, 78
-q query, 77
-U upgrade, 78
-V verify, 79
database, 77
functions, 77
modes of operation, 77
Redhat Package Manager,
76
RS232, 28
rw-r--r-- permissions, 162
booting, 36
ide-scsi, 37
SCSI ID, 33
scsi_hostadapter alias, 34
sda, 35, 55
sed, 96, 132
server configuration,
X Window System, 180
set, 84
set-gid for directories, 167
setserial, 29
setsid, 119
sg0, 36
sha1sum, 90
shared libraries, 70
symbol versions, 71
Shift+PgUp scrolling, 81
SIGCONT, 122
SIGHUP, 119
SIGINT, 119
Signals, 122
SIGSTOP, 119, 122
SIGTERM, 122
slocate, 176
sndconfig, 30
sort, 94
Sound cards, 30
spd_vhi, 29
split, 93
st0, 35
startx, 188
swap,
partition, 55
space, 54
swap space, 56
symbolic links , 170
Tab completion, 81
tac, 94
tail, 92
tar, 65
tar,
tar.gz, 65
text filters, 89
textutils, 90
19.3.2 top, 121
niceness, 128
renice, 128
touch, 105
tr, 95
ttmkfdir, 186
ttyS0, 28
tune2fs, 147
umask, 163
unexpand, 98
uniq, 94
unset, 84
updatedb, 176
urandom, 119
USB, 50
usb-ohci.o, 51
usb-uhci.o, 51
usbcore.o, 51
usbmgr, 52
usr directory, 174
usrquota option, 154
uucp, 28
vgchange, 57
vgscan, 57
vi, 135
vmlinuz, 63
vsync, 182
wc, 100
whereis, 176
which, 115, 177
wildcards, 103
window manager, 193
Winmodems, 29
wvdial, 47
X resources, 194
X terminal, 191
X Window System,
access control, 197
client, 179
font server, 186
fonts, 185
graphics card, 181
Keyboard, 180
Mouse / pointer, 180
protocol, 179
server, 179
209
35 Index
Xresources, 189
xset +fp, 186
XSetup, 190
xterm, 195
xvidtune, 185
zero, 141
210