Linux System Administration




       Copyright © GBdirect Ltd 2004
        http://training.gbdirect.co.uk/
             tel: 0870 200 7273
Overview


1   Introduction                                          1


2   Getting Started                                       8


3   Work Effectively on the Unix Command Line            16


4   Process Text Streams Using Text Processing Filters   24


5   Perform Basic File Management                        33


6   Use Unix Streams, Pipes and Redirects                39


7   Search Text Files Using Regular Expressions          44


8   Job Control                                          47


9   Create, Monitor, and Kill Processes                  49


10 Modify Process Execution Priorities                   55


11 Advanced Shell Usage                                  57


12 Filesystem Concepts                                   61


13 Create and Change Hard and Symbolic Links             63


14 Manage File Ownership                                 67


15 Use File Permissions to Control Access to Files       70


16 Create Partitions and Filesystems                     76


17 Control Filesystem Mounting and Unmounting            79


18 Maintain the Integrity of Filesystems                 83



                                                     i
Linux System Administration                                      Overview


19 Find System Files and Place Files in the Correct Location          87


20 Set and View Disk Quotas                                           93


21 Boot the System                                                    97


22 Change Runlevels and Shutdown or Reboot System                    101


23 Use and Manage Local System Documentation                         106


24 Find Linux Documentation on the Internet                          114


25 Tune the User Environment and System Environment Variables        117


26 Configure and Use System Log Files                                 120


27 Automate and Schedule System Administration Tasks                 124


28 Maintain an Effective Data Backup Strategy                        131




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/          ii
Contents

1   Introduction                                                                                                                                                                       1
    1.1    Unix and Linux . . . . . . . . . . . . . . . .             .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   1
    1.2    Unix System Architecture . . . . . . . . . .               .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   1
    1.3    Unix Philosophy . . . . . . . . . . . . . . .              .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   2
    1.4    What is Linux? . . . . . . . . . . . . . . . .             .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   2
    1.5    Using a Linux System . . . . . . . . . . . .               .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   2
    1.6    Linux Command Line . . . . . . . . . . . .                 .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   3
    1.7    Logging Out . . . . . . . . . . . . . . . . .              .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   3
    1.8    Command Syntax . . . . . . . . . . . . . .                 .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   3
    1.9    Files . . . . . . . . . . . . . . . . . . . . .            .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   4
    1.10 Creating Files with cat . . . . . . . . . . .                .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   4
    1.11 Displaying Files’ Contents with cat . . . . .                .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   4
    1.12 Deleting Files with rm . . . . . . . . . . . .               .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   5
    1.13 Unix Command Feedback . . . . . . . . . .                    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   5
    1.14 Copying and Renaming Files with cp and mv                    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   5
    1.15 Filename Completion . . . . . . . . . . . .                  .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   6
    1.16 Command History . . . . . . . . . . . . . .                  .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   6
    1.17 Exercises . . . . . . . . . . . . . . . . . .                .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   6

2   Getting Started                                                                                                                                                                     8
    2.1    Files and Directories . . . . . . . . .   .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    8
    2.2    Examples of Absolute Paths . . . . .      .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    8
    2.3    Current Directory . . . . . . . . . .     .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    9
    2.4    Making and Deleting Directories . .       .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    9
    2.5    Relative Paths . . . . . . . . . . . .    .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    9
    2.6    Special Dot Directories . . . . . . .     .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   10
    2.7    Using Dot Directories in Paths . . . .    .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   10
    2.8    Hidden Files . . . . . . . . . . . . .    .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   10
    2.9    Paths to Home Directories . . . . . .     .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   11
    2.10 Looking for Files in the System . . .       .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   11
    2.11 Running Programs . . . . . . . . . .        .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   11
    2.12 Specifying Multiple Files . . . . . . .     .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12
    2.13 Finding Documentation for Programs          .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   12
    2.14 Specifying Files with Wildcards . . .       .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   13
    2.15 Chaining Programs Together . . . .          .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   13
    2.16 Graphical and Text Interfaces . . . .       .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   13
    2.17 Text Editors . . . . . . . . . . . . .      .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   14
    2.18 Exercises . . . . . . . . . . . . . .       .   .   .   .    .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   14

3   Work Effectively on the Unix Command Line                                                                                                                                          16
    3.1   Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                       16
    3.2   The Bash Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                         16


                                                                     iii
Linux System Administration                                                                                                                                                  Contents


    3.3     Shell Commands . . . . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   17
    3.4     Command-Line Arguments . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   17
    3.5     Syntax of Command-Line Options . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   17
    3.6     Examples of Command-Line Options .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   18
    3.7     Setting Shell Variables . . . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   18
    3.8     Environment Variables . . . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   18
    3.9     Where Programs are Found . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   19
    3.10    Bash Configuration Variables . . . . .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   19
    3.11    Using History . . . . . . . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   19
    3.12    Reusing History Items . . . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   20
    3.13    Retrieving Arguments from the History        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   20
    3.14    Summary of Bash Editing Keys . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   21
    3.15    Combining Commands on One Line .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   21
    3.16    Repeating Commands with for . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   21
    3.17    Command Substitution . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   22
    3.18    Finding Files with locate . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   22
    3.19    Finding Files More Flexibly: find . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   22
    3.20    find Criteria . . . . . . . . . . . . . .    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   23
    3.21    find Actions: Executing Programs . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   23
    3.22    Exercises . . . . . . . . . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   23

4   Process Text Streams Using Text Processing Filters                                                                                                                               24
    4.1   Working with Text Files . . . . . . . . . . . . .                  .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   24
    4.2   Lines of Text . . . . . . . . . . . . . . . . . . .                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   24
    4.3   Filtering Text and Piping . . . . . . . . . . . . .                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   25
    4.4   Displaying Files with less . . . . . . . . . . . .                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   25
    4.5   Counting Words and Lines with wc . . . . . . .                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   25
    4.6   Sorting Lines of Text with sort . . . . . . . . .                  .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   26
    4.7   Removing Duplicate Lines with uniq . . . . . .                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   26
    4.8   Selecting Parts of Lines with cut . . . . . . . .                  .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   26
    4.9   Expanding Tabs to Spaces with expand . . . .                       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   27
    4.10 Using fmt to Format Text Files . . . . . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   27
    4.11 Reading the Start of a File with head . . . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   27
    4.12 Reading the End of a File with tail . . . . . .                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   28
    4.13 Numbering Lines of a File with nl or cat . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   28
    4.14 Dumping Bytes of Binary Data with od . . . . .                      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   28
    4.15 Paginating Text Files with pr . . . . . . . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   29
    4.16 Dividing Files into Chunks with split . . . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   29
    4.17 Using split to Span Disks . . . . . . . . . . .                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   29
    4.18 Reversing Files with tac . . . . . . . . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   29
    4.19 Translating Sets of Characters with tr . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   30
    4.20 tr Examples . . . . . . . . . . . . . . . . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   30
    4.21 Modifying Files with sed . . . . . . . . . . . . .                  .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   30
    4.22 Substituting with sed . . . . . . . . . . . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   30
    4.23 Put Files Side-by-Side with paste . . . . . . .                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   31
    4.24 Performing Database Joins with join . . . . . .                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   31
    4.25 Exercises . . . . . . . . . . . . . . . . . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   31

5   Perform Basic File Management                                                                                                                                                    33
    5.1   Filesystem Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                       33
    5.2   Directory and File Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                       33
    5.3   File Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                      34

Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                                                                                       iv
Linux System Administration                                                                                                                                                                            Contents


    5.4     Going Back to Previous Directories                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   34
    5.5     Filename Completion . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   34
    5.6     Wildcard Patterns . . . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   35
    5.7     Copying Files with cp . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   35
    5.8     Examples of cp . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   35
    5.9     Moving Files with mv . . . . . . . .                           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   36
    5.10    Deleting Files with rm . . . . . . .                           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   36
    5.11    Deleting Files with Peculiar Names                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   36
    5.12    Making Directories with mkdir . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   37
    5.13    Removing Directories with rmdir .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   37
    5.14    Identifying Types of Files . . . . .                           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   37
    5.15    Changing Timestamps with touch                                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   38
    5.16    Exercises . . . . . . . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   38

6   Use Unix Streams, Pipes and Redirects                                                                                                                                                                      39
    6.1   Standard Files . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   39
    6.2   Standard Input . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   39
    6.3   Standard Output . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   40
    6.4   Standard Error . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   40
    6.5   Pipes . . . . . . . . . . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   40
    6.6   Connecting Programs to Files . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   41
    6.7   Appending to Files . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   41
    6.8   Redirecting Multiple Files . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   41
    6.9   Redirection with File Descriptors .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   42
    6.10 Running Programs with xargs . .                                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   42
    6.11 tee . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   42
    6.12 Exercises . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   43

7   Search Text Files Using Regular Expressions                                                                                                                                                                44
    7.1   Searching Files with grep . . . . . . . .                                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   44
    7.2   Pattern Matching . . . . . . . . . . . .                                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   44
    7.3   Matching Repeated Patterns . . . . . .                                       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   45
    7.4   Matching Alternative Patterns . . . . . .                                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   45
    7.5   Extended Regular Expression Syntax . .                                       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   45
    7.6   sed . . . . . . . . . . . . . . . . . . .                                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   45
    7.7   Further Reading . . . . . . . . . . . . .                                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   46
    7.8   Exercises . . . . . . . . . . . . . . . .                                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   46

8   Job Control                                                                                                                                                                                                47
    8.1   Job Control      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   47
    8.2   jobs . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   47
    8.3   fg . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   48
    8.4   bg . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   48
    8.5   Exercises .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   48

9   Create, Monitor, and Kill Processes                                                                                                                                                                        49
    9.1    What is a Process? . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   49
    9.2    Process Properties . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   49
    9.3    Parent and Child Processes . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   50
    9.4    Process Monitoring: ps . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   50
    9.5    ps Options . . . . . . . . . . .                        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   51
    9.6    Process Monitoring: pstree .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   51


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                                                                                                                  v
Linux System Administration                                                                                                                                                                 Contents


    9.7     pstree Options . . . . . . .  . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   51
    9.8     Process Monitoring: top . .   . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   51
    9.9     top Command-Line Options      . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   52
    9.10    top Interactive Commands .    . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   52
    9.11    Signalling Processes . . . . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   52
    9.12    Common Signals for Interactive Use                      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   53
    9.13    Sending Signals: kill . . . . . . .                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   53
    9.14    Sending Signals to Dæmons: pidof                        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   53
    9.15    Exercises . . . . . . . . . . . . . .                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   53

10 Modify Process Execution Priorities                                                                                                                                                              55
   10.1 Concepts . . . . . . . . . . .                  .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   55
   10.2 nice . . . . . . . . . . . . . .                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   55
   10.3 renice . . . . . . . . . . . .                  .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   56
   10.4 Exercises . . . . . . . . . . .                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   56

11 Advanced Shell Usage                                                                                                                                                                             57
   11.1 More About Quoting . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   57
   11.2 Quoting: Single Quotes . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   57
   11.3 Quoting: Backslashes . . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   57
   11.4 Quoting: Double Quotes . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   58
   11.5 Quoting: Combining Quoting Mechanisms                                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   58
   11.6 Recap: Specifying Files with Wildcards . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   58
   11.7 Globbing Files Within Directories . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   59
   11.8 Globbing to Match a Single Character . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   59
   11.9 Globbing to Match Certain Characters . .                                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   59
   11.10 Generating Filenames: {} . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   60
   11.11 Shell Programming . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   60
   11.12 Exercises . . . . . . . . . . . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   60

12 Filesystem Concepts                                                                                                                                                                              61
   12.1 Filesystems . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   61
   12.2 The Unified Filesystem           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   61
   12.3 File Types . . . . . . .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   62
   12.4 Inodes and Directories          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   62

13 Create and Change Hard and Symbolic Links                                                                                                                                                        63
   13.1 Symbolic Links . . . . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   63
   13.2 Examining and Creating Symbolic Links                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   63
   13.3 Hard Links . . . . . . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   64
   13.4 Symlinks and Hard Links Illustrated . . .                           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   64
   13.5 Comparing Symlinks and Hard Links . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   64
   13.6 Examining and Creating Hard Links . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   65
   13.7 Preserving Links . . . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   65
   13.8 Finding Symbolic Links to a File . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   65
   13.9 Finding Hard Links to a File . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   66
   13.10 Exercises . . . . . . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   66

14 Manage File Ownership                                                                                                                                                                            67
   14.1 Users and Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                                        67
   14.2 The Superuser: Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                                         67
   14.3 Changing File Ownership with chown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                                          68


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                                                                                                      vi
Linux System Administration                                                                                                                                                         Contents


    14.4    Changing File Group Ownership with chgrp . . . . . . . .                                        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   68
    14.5    Changing the Ownership of a Directory and Its Contents . .                                      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   68
    14.6    Changing Ownership and Group Ownership Simultaneously                                           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   69
    14.7    Exercises . . . . . . . . . . . . . . . . . . . . . . . . . .                                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   69

15 Use File Permissions to Control Access to Files                                                                                                                                          70
   15.1 Basic Concepts: Permissions on Files . . . . . . . . . . .                                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   70
   15.2 Basic Concepts: Permissions on Directories . . . . . . . .                                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   70
   15.3 Basic Concepts: Permissions for Different Groups of People                                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   71
   15.4 Examining Permissions: ls -l . . . . . . . . . . . . . . .                                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   71
   15.5 Preserving Permissions When Copying Files . . . . . . . .                                           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   71
   15.6 How Permissions are Applied . . . . . . . . . . . . . . . .                                         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   71
   15.7 Changing File and Directory Permissions: chmod . . . . . .                                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   72
   15.8 Specifying Permissions for chmod . . . . . . . . . . . . . .                                        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   72
   15.9 Changing the Permissions of a Directory and Its Contents .                                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   72
   15.10 Special Directory Permissions: ‘Sticky’ . . . . . . . . . . .                                      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   73
   15.11 Special Directory Permissions: Setgid . . . . . . . . . . .                                        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   73
   15.12 Special File Permissions: Setgid . . . . . . . . . . . . . .                                       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   73
   15.13 Special File Permissions: Setuid . . . . . . . . . . . . . .                                       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   74
   15.14 Displaying Unusual Permissions . . . . . . . . . . . . . .                                         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   74
   15.15 Permissions as Numbers . . . . . . . . . . . . . . . . . .                                         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   74
   15.16 Default Permissions: umask . . . . . . . . . . . . . . . . .                                       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   75
   15.17 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . .                                      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   75

16 Create Partitions and Filesystems                                                                                                                                                        76
   16.1 Concepts: Disks and Partitions          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   76
   16.2 Disk Naming . . . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   76
   16.3 Using fdisk . . . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   77
   16.4 Making New Partitions . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   77
   16.5 Changing Partition Types . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   77
   16.6 Making Filesystems with mkfs            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   78
   16.7 Useful Websites . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   78

17 Control Filesystem Mounting and Unmounting                                                                                                                                               79
   17.1 Mounting Filesystems . . . . . . . . . . .                      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   79
   17.2 Mounting a Filesystem: mount . . . . . .                        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   79
   17.3 Mounting Other Filesystems . . . . . . . .                      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   80
   17.4 Unmounting a Filesystem: umount . . . .                         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   80
   17.5 Configuring mount: /etc/fstab . . . . . . .                      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   80
   17.6 Sample /etc/fstab . . . . . . . . . . . . .                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   80
   17.7 Filesystem Types . . . . . . . . . . . . .                      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   81
   17.8 Mount Options . . . . . . . . . . . . . . .                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   81
   17.9 Other Columns in /etc/fstab . . . . . . . .                     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   81
   17.10 Mounting a File . . . . . . . . . . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   82
   17.11 Exercises . . . . . . . . . . . . . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   82

18 Maintain the Integrity of Filesystems                                                                                                                                                    83
   18.1 Filesystem Concepts . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   83
   18.2 Potential Problems . . . . . . .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   83
   18.3 Monitoring Space: df . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   84
   18.4 Monitoring Inodes: df . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   84
   18.5 Monitoring Disk Usage: du . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   85


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                                                                                              vii
Linux System Administration                                                                                                                                                      Contents


    18.6    du Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                       85
    18.7    Finding and Repairing Filesystem Corruption: fsck . . . . . . . . . . . . . . . . . . . . . . . .                                                                            85
    18.8    Running fsck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                         86
    18.9    Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                                        86

19 Find System Files and Place Files in the Correct Location                                                                                                                             87
   19.1 Unix Filesystem Layout . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   87
   19.2 The Filesystem Hierarchy Standard . . . . . . . . . .                                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   87
   19.3 Shareable and Non-Shareable Data . . . . . . . . .                                   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   88
   19.4 Static and Dynamic Data . . . . . . . . . . . . . . .                                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   88
   19.5 Overview of the FHS . . . . . . . . . . . . . . . . .                                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   88
   19.6 FHS: Installed Software . . . . . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   89
   19.7 FHS: Other Directories Under /usr . . . . . . . . . .                                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   89
   19.8 FHS: Directories Under /var . . . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   89
   19.9 FHS: Other Directories . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   90
   19.10 FHS: Other Directories . . . . . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   90
   19.11 Finding Programs with which . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   90
   19.12 The type Built-in Command . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   90
   19.13 Checking for Shell Builtins with type . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   91
   19.14 Updating the locate Database . . . . . . . . . . . .                                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   91
   19.15 updatedb.conf . . . . . . . . . . . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   91
   19.16 whatis . . . . . . . . . . . . . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   92
   19.17 Finding Manpages with apropos . . . . . . . . . . .                                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   92
   19.18 Web Resources . . . . . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   92
   19.19 Exercises . . . . . . . . . . . . . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   92

20 Set and View Disk Quotas                                                                                                                                                              93
   20.1 What are Quotas? . . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   93
   20.2 Hard and Soft Limits . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   93
   20.3 Per-User and Per-Group Quotas .              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   94
   20.4 Block and Inode Limits . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   94
   20.5 Displaying Quota Limits: quota . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   94
   20.6 Options in /etc/fstab . . . . . . . .        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   94
   20.7 Enabling Quota: quotaon . . . . .            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   95
   20.8 Changing Quota Limits: setquota              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   95
   20.9 edquota . . . . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   95
   20.10 repquota . . . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   96

21 Boot the System                                                                                                                                                                        97
   21.1 Boot Loaders . . . . . . . . . . . . . .                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    97
   21.2 LILO . . . . . . . . . . . . . . . . . . .               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    97
   21.3 Sample lilo.conf File . . . . . . . . . . .              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    98
   21.4 Selecting What to Boot . . . . . . . . .                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    98
   21.5 Other Ways of Starting Linux . . . . . .                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    98
   21.6 Specifying Kernel Parameters . . . . . .                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    99
   21.7 Specifying Kernel Parameters in lilo.conf                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    99
   21.8 Useful Kernel Parameters . . . . . . . .                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    99
   21.9 Boot Messages . . . . . . . . . . . . .                  .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .    99
   21.10 Kernel Modules . . . . . . . . . . . . .                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   100
   21.11 Exercises . . . . . . . . . . . . . . . .               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   100

22 Change Runlevels and Shutdown or Reboot System                                                                                                                                        101


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                                                                                            viii
Linux System Administration                                                                                                                                              Contents


    22.1    Understanding Runlevels . . . . . . . . . . .            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   101
    22.2    Typical Runlevels . . . . . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   101
    22.3    Single-User Mode and sulogin . . . . . . .               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   102
    22.4    Shutting Down and Restarting the System . .              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   102
    22.5    Setting the Default Runlevel . . . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   102
    22.6    Selecting a Different Runlevel at Bootup . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   103
    22.7    Determining the Current Runlevel . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   103
    22.8    Switching Runlevel . . . . . . . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   103
    22.9    Services in Each Runlevel: the init.d Directory          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   104
    22.10   Symbolic Links in rcN.d . . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   104
    22.11   Starting or Stopping Individual Services . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   104
    22.12   Exercises . . . . . . . . . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   104

23 Use and Manage Local System Documentation                                                                                                                                     106
   23.1 Manual Pages . . . . . . . . . . . . . . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   106
   23.2 Navigating Within Manual Pages . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   106
   23.3 Sections of a Manual Page . . . . . . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   107
   23.4 Sections of the Manual . . . . . . . . . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   107
   23.5 Manual Section Numbering . . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   108
   23.6 Determining Available Manpages with whatis . . . . . . .                                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   108
   23.7 Printing Manual Pages . . . . . . . . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   108
   23.8 Searching for Manpages with apropos . . . . . . . . . . .                                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   109
   23.9 Displaying All Manpages of a Particular Name with man -a                                 .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   109
   23.10 Searching the Content of All Manpages with man -K . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   109
   23.11 Finding the Right Manual Page . . . . . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   110
   23.12 Help on Shell Builtins . . . . . . . . . . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   110
   23.13 Location of Manual Pages . . . . . . . . . . . . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   110
   23.14 Info Pages . . . . . . . . . . . . . . . . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   111
   23.15 Navigating Within Info Pages . . . . . . . . . . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   111
   23.16 Documentation in /usr/share/doc/ . . . . . . . . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   111
   23.17 Contents of /usr/share/doc . . . . . . . . . . . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   112
   23.18 Interrogating Commands for Help . . . . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   112
   23.19 Finding Documentation . . . . . . . . . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   112
   23.20 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . .                           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   112

24 Find Linux Documentation on the Internet                                                                                                                                      114
   24.1 The Linux Documentation Project . . . . . .              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   114
   24.2 HOWTOs . . . . . . . . . . . . . . . . . .               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   114
   24.3 Obtaining HOWTOs . . . . . . . . . . . . .               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   115
   24.4 Vendor- and Application-Specific Web Sites                .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   115
   24.5 Usenet Newsgroups . . . . . . . . . . . . .              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   115
   24.6 FAQs . . . . . . . . . . . . . . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   116
   24.7 Local Help . . . . . . . . . . . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   116

25 Tune the User Environment and System Environment Variables                                                                                                                    117
   25.1 Configuration Files . . . . . . . . . . . . . . . . . . . . . .                           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   117
   25.2 Shell Configuration Files . . . . . . . . . . . . . . . . . . .                           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   117
   25.3 Changing Environment Variables . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   118
   25.4 Changing the Prompt . . . . . . . . . . . . . . . . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   118
   25.5 Shell Aliases . . . . . . . . . . . . . . . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   118
   25.6 Setting Up Home Directories for New Accounts . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   119
   25.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   119


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                                                                                    ix
Linux System Administration                                                                                                                                                    Contents


26 Configure and Use System Log Files                                                                                                                                                   120
   26.1 syslog . . . . . . . . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   120
   26.2 /etc/syslog.conf . . . . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   120
   26.3 Sample /etc/syslog.conf . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   121
   26.4 Reconfiguring syslog . . . . . . . .            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   121
   26.5 Examining Logs: less and grep . .              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   121
   26.6 Examining Logs in Real Time: tail              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   122
   26.7 Log Rotation . . . . . . . . . . . . .         .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   122
   26.8 Sample /etc/logrotate.conf . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   122
   26.9 Exercises . . . . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   123

27 Automate and Schedule System Administration Tasks                                                                                                                                   124
   27.1 Running Commands in the Future . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   124
   27.2 At Commands . . . . . . . . . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   124
   27.3 Commands Run by the At Dæmon . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   125
   27.4 At Command Specification . . . . . . . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   125
   27.5 Opening Windows from At Commands . . . . . .                               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   125
   27.6 At Command Date & Time Specification . . . . .                              .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   126
   27.7 Managing At Commands . . . . . . . . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   126
   27.8 Simple Cron Job Specification . . . . . . . . . .                           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   126
   27.9 More Complex Cron Job Specification . . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   127
   27.10 Crontab Format . . . . . . . . . . . . . . . . . .                        .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   127
   27.11 Crontab Date & Time Specification . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   128
   27.12 More Complex Crontab Dates & Times . . . . . .                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   128
   27.13 /etc/crontab . . . . . . . . . . . . . . . . . . . .                      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   128
   27.14 User Crontabs . . . . . . . . . . . . . . . . . . .                       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   129
   27.15 Cron Job Output . . . . . . . . . . . . . . . . . .                       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   129
   27.16 At Command and Cron Job Permissions . . . . .                             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   129
   27.17 Exercises . . . . . . . . . . . . . . . . . . . . .                       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   130

28 Maintain an Effective Data Backup Strategy                                                                                                                                          131
   28.1 Reasons for Backup . . . . . . . . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   131
   28.2 Backup Media . . . . . . . . . . . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   131
   28.3 Types of Backup . . . . . . . . . . . .            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   132
   28.4 Backup Strategy . . . . . . . . . . . .            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   132
   28.5 Archiving Files with tar . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   132
   28.6 Creating Archives with tar . . . . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   133
   28.7 Listing the Files in tar Archives . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   133
   28.8 Extracting Files from tar Archives . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   133
   28.9 Device Files for Accessing Tapes . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   134
   28.10 Using tar for Backups . . . . . . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   134
   28.11 Controlling Tape Drives with mt . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   134
   28.12 Deciding What to Backup . . . . . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   135
   28.13 What Not to Backup . . . . . . . . . .            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   135
   28.14 Scripting Backup . . . . . . . . . . . .          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   135
   28.15 Other Backup Software . . . . . . . .             .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   135
   28.16 Exercises . . . . . . . . . . . . . . .           .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   136




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                                                                                           x
Module 1


Introduction

1.1   Unix and Linux
  s Linux is based on Unix
       q Unix philosophy
       q Unix commands
       q Unix standards and conventions
  s There is some variation between Unix operating systems
       q Especially regarding system administration
       q Often Linux-specific things in these areas


1.2   Unix System Architecture



                                          applications
                           programs




                                      X                      shell


                                              kernel

                                             hardware


  s The shell and the window environment are programs
  s Programs’ only access to hardware is via the kernel




                                                         1
Linux System Administration                                                                 Module 1. Introduction


1.3    Unix Philosophy
   s Multi-user
         q A user needs an account to use a computer
         q Each user must log in
         q Complete separation of different users’ files and configuration settings
   s Small components
         q Each component should perform a single task
         q Multiple components can be combined and chained together for more complex tasks
         q An individual component can be subsituted for another, without affecting other
             components



1.4    What is Linux?
   s Linux kernel
         q Developed by Linus Torvalds
         q Strictly speaking, ‘Linux’ is just the kernel
   s Associated utilities
         q Standard tools found on (nearly) all Linux systems
         q Many important parts come from the GNU project
               s   Free Software Foundation’s project to make a free Unix
               s   Some claim the OS as a whole should be ‘GNU/Linux’
   s Linux distributions
         q Kernel plus utilities plus other tools, packaged up for end users
         q Generally with installation program
         q Distributors include: Red Hat, Debian, SuSE, Mandrake


1.5    Using a Linux System
   s Login prompt displayed
         q When Linux first loads after booting the computer
         q After another user has logged out
   s Need to enter a username and password
   s The login prompt may be graphical or simple text
   s If text, logging in will present a shell
   s If graphical, logging in will present a desktop
         q Some combination of mousing and keystrokes will make a terminal window appear
         q A shell runs in the terminal window


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                  2
Linux System Administration                                                                    Module 1. Introduction


1.6    Linux Command Line
   s The shell is where commands are invoked
   s A command is typed at a shell prompt
         q Prompt usually ends in a dollar sign ($)
   s After typing a command press Enter to invoke it
         q The shell will try to obey the command
         q Another prompt will appear
   s Example:
      $ date
      Thu Jun 14 12:28:05 BST 2001
      $
         q The dollar represents the prompt in this course — do not type it


1.7    Logging Out
   s To exit from the shell, use the exit command
   s Pressing Ctrl+D at the shell prompt will also quit the shell
   s Quitting all programs should log you out
         q If in a text-only single-shell environment, exiting the shell should be sufficient
         q In a window environment, the window manager should have a log out command for this
             purpose
   s After logging out, a new login prompt should be displayed


1.8 Command Syntax
   s Most commands take parameters
         q Some commands require them
         q Parameters are also known as arguments
         q For example, echo simply displays its arguments:
             $ echo

             $ echo Hello there
             Hello there
   s Commands are case-sensitive
         q Usually lower-case
      $ echo whisper
      whisper
      $ ECHO SHOUT
      bash: ECHO: command not found


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                     3
Linux System Administration                                                                   Module 1. Introduction


1.9    Files
   s Data can be stored in a file
   s Each file has a filename
         q A label referring to a particular file
         q Permitted characters include letters, digits, hyphens (-), underscores (_), and dots (.)
         q Case-sensitive — NewsCrew.mov is a different file from NewScrew.mov
   s The ls command lists the names of files


1.10     Creating Files with cat
   s There are many ways of creating a file
   s One of the simplest is with the cat command:
       $ cat > shopping_list
       cucumber
       bread
       yoghurts
       fish fingers
   s Note the greater-than sign (>) — this is necessary to create the file
   s The text typed is written to a file with the specified name
   s Press Ctrl+D after a line-break to denote the end of the file
         q The next shell prompt is displayed
   s ls demonstrates the existence of the new file


1.11     Displaying Files’ Contents with cat
   s There are many ways of viewing the contents of a file
   s One of the simplest is with the cat command:
       $ cat shopping_list
       cucumber
       bread
       yoghurts
       fish fingers
   s Note that no greater-than sign is used
   s The text in the file is displayed immediately:
         q Starting on the line after the command
         q Before the next shell prompt




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                    4
Linux System Administration                                                            Module 1. Introduction


1.12     Deleting Files with rm
   s To delete a file, use the rm (‘remove’) command
   s Simply pass the name of the file to be deleted as an argument:
       $ rm shopping_list
   s The file and its contents are removed
         q There is no recycle bin
         q There is no ‘unrm’ command
   s The ls command can be used to confirm the deletion


1.13     Unix Command Feedback
   s Typically, succesful commands do not give any output
   s Messages are displayed in the case of errors
   s The rm command is typical
         q If it manages to delete the specified file, it does so silently
         q There is no ‘File shopping_list has been removed’ message
         q But if the command fails for whatever reason, a message is displayed
   s The silence can be be off-putting for beginners
   s It is standard behaviour, and doesn’t take long to get used to


1.14     Copying and Renaming Files with cp and mv
   s To copy the contents of a file into another file, use the cp command:
       $ cp CV.pdf old-CV.pdf
   s To rename a file use the mv (‘move’) command:
       $ mv commitee_minutes.txt committee_minutes.txt
         q Similar to using cp then rm
   s For both commands, the existing name is specified as the first argument and the new name as
       the second
         q If a file with the new name already exists, it is overwritten




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                             5
Linux System Administration                                                                   Module 1. Introduction


1.15      Filename Completion
   s The shell can making typing filenames easier
   s Once an unambiguous prefix has been typed, pressing Tab will automatically ‘type’ the rest
   s For example, after typing this:
        $ rm sho
        pressing Tab may turn it into this:
        $ rm shopping_list
   s This also works with command names
          q For example, da may be completed to date if no other commands start ‘da’


1.16      Command History
   s Often it is desired to repeat a previously-executed command
   s The shell keeps a command history for this purpose
          q Use the Up and Down cursor keys to scroll through the list of previous commands
          q Press Enter to execute the displayed command
   s Commands can also be edited before being run
          q Particularly useful for fixing a typo in the previous command
          q The Left and Right cursor keys navigate across a command
          q Extra characters can be typed at any point
          q Backspace deletes characters to the left of the cursor
          q Del and Ctrl+D delete characters to the right
               s   Take care not to log out by holding down Ctrl+D too long




1.17      Exercises

   1.     a. Log in.
          b. Log out.
          c. Log in again. Open a terminal window, to start a shell.
          d. Exit from the shell; the terminal window will close.
          e. Start another shell. Enter each of the following commands in turn.
               s date
               s whoami
               s hostname
               s uname



Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                    6
Linux System Administration                                                                       Module 1. Introduction


              s uptime
   2.    a. Use the ls command to see if you have any files.
         b. Create a new file using the cat command as follows:
             $ cat > hello.txt
             Hello world!
             This is a text file.
             Press Enter at the end of the last line, then Ctrl+D to denote the end of the file.
         c. Use ls again to verify that the new file exists.
         d. Display the contents of the file.
         e. Display the file again, but use the cursor keys to execute the same command again without having to
            retype it.
   3.    a. Create a second file. Call it secret-of-the-universe, and put in whatever content you deem appropriate.
         b. Check its creation with ls.
         c. Display the contents of this file. Minimise the typing needed to do this:
              s Scroll back through the command history to the command you used to create the file.
              s Change that command to display secret-of-the-universe instead of creating it.
   4. After each of the following steps, use ls and cat to verify what has happened.
         a. Copy secret-of-the-universe to a new file called answer.txt. Use Tab to avoid typing the existing file’s
            name in full.
         b. Now copy hello.txt to answer.txt. What’s happened now?
         c. Delete the original file, hello.txt.
         d. Rename answer.txt to message.
         e. Try asking rm to delete a file called missing. What happens?
          f. Try copying secret-of-the-universe again, but don’t specify a filename to which to copy. What happens
             now?




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                        7
Module 2


Getting Started

2.1   Files and Directories
  s A directory is a collection of files and/or other directories
       q Because a directory can contain other directories, we get a directory hierarchy
  s The ‘top level’ of the hierarchy is the root directory
  s Files and directories can be named by a path
       q Shows programs how to find their way to the file
       q The root directory is referred to as /
       q Other directories are referred to by name, and their names are separated by slashes (/)
  s If a path refers to a directory it can end in /
       q Usually an extra slash at the end of a path makes no difference


2.2   Examples of Absolute Paths
  s An absolute path starts at the root of the directory hierarchy, and names directories under it:
      /etc/hostname
       q Meaning the file called hostname in the directory etc in the root directory
  s We can use ls to list files in a specific directory by specifying the absolute path:
      $ ls /usr/share/doc/




                                                        8
Linux System Administration                                                                 Module 2. Getting Started


2.3    Current Directory
   s Your shell has a current directory — the directory in which you are currently working
   s Commands like ls use the current directory if none is specified
   s Use the pwd (print working directory) command to see what your current directory is:
      $ pwd
      /home/fred
   s Change the current directory with cd:
      $ cd /mnt/cdrom
      $ pwd
      /mnt/cdrom
   s Use cd without specifying a path to get back to your home directory


2.4    Making and Deleting Directories
   s The mkdir command makes new, empty, directories
   s For example, to make a directory for storing company accounts:
      $ mkdir Accounts
   s To delete an empty directory, use rmdir:
      $ rmdir OldAccounts
   s Use rm with the -r (recursive) option to delete directories and all the files they contain:
      $ rm -r OldAccounts
   s Be careful — rm can be a dangerous tool if misused


2.5    Relative Paths
   s Paths don’t have to start from the root directory
         q A path which doesn’t start with / is a relative path
         q It is relative to some other directory, usually the current directory
   s For example, the following sets of directory changes both end up in the same directory:
      $ cd /usr/share/doc

      $ cd /
      $ cd usr
      $ cd share/doc
   s Relative paths specify files inside directories in the same way as absolute ones




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                     9
Linux System Administration                                                                 Module 2. Getting Started


2.6    Special Dot Directories
   s Every directory contains two special filenames which help making relative paths:
         q The directory .. points to the parent directory
               s   ls .. will list the files in the parent directory
         q For example, if we start from /home/fred:
             $ cd ..
             $ pwd
             /home
             $ cd ..
             $ pwd
             /
   s The special directory . points to the directory it is in
         q So ./foo is the same file as foo


2.7    Using Dot Directories in Paths
   s The special .. and . directories can be used in paths just like any other directory name:
      $ cd ../other-dir/
         q Meaning “the directory other-dir in the parent directory of the current directory”
   s It is common to see .. used to ‘go back’ several directories from the current directory:
      $ ls ../../../../far-away-directory/
   s The . directory is most commonly used on its own, to mean “the current directory”


2.8    Hidden Files
   s The special . and .. directories don’t show up when you do ls
         q They are hidden files
   s Simple rule: files whose names start with . are considered ‘hidden’
   s Make ls display all files, even the hidden ones, by giving it the -a (all) option:
      $ ls -a
      .    ..          .bashrc        .profile         report.doc
   s Hidden files are often used for configuration files
         q Usually found in a user’s home directory
   s You can still read hidden files — they just don’t get listed by ls by default




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                    10
Linux System Administration                                                              Module 2. Getting Started


2.9    Paths to Home Directories
   s The symbol ˜ (tilde) is an abbreviation for your home directory
         q So for user ‘fred’, the following are equivalent:
             $ cd /home/fred/documents/
             $ cd ˜/documents/
   s The ˜ is expanded by the shell, so programs only see the complete path
   s You can get the paths to other users’ home directories using ˜, for example:
       $ cat ˜alice/notes.txt
   s The following are all the same for user ‘fred’:
       $ cd
       $ cd ˜
       $ cd /home/fred


2.10     Looking for Files in the System
   s The command locate lists files which contain the text you give
   s For example, to find files whose name contains the word ‘mkdir’:
       $ locate mkdir
       /usr/man/man1/mkdir.1.gz
       /usr/man/man2/mkdir.2.gz
       /bin/mkdir
       ...
   s locate is useful for finding files when you don’t know exactly what they will be called, or where
       they are stored
   s For many users, graphical tools make it easier to navigate the filesystem
         q Also make file management simpler


2.11     Running Programs
   s Programs under Linux are files, stored in directories like /bin and /usr/bin
         q Run them from the shell, simply by typing their name
   s Many programs take options, which are added after their name and prefixed with -
   s For example, the -l option to ls gives more information, including the size of files and the date
       they were last modified:
       $ ls -l
       drwxrwxr-x         2 fred       users         4096 Jan 21 10:57 Accounts
       -rw-rw-r--         1 fred       users          345 Jan 21 10:57 notes.txt
       -rw-r--r--         1 fred       users         3255 Jan 21 10:57 report.txt
   s Many programs accept filenames after the options
         q Specify multiple files by separating them with spaces


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                 11
Linux System Administration                                                               Module 2. Getting Started


2.12     Specifying Multiple Files
   s Most programs can be given a list of files
         q For example, to delete several files at once:
             $ rm oldnotes.txt tmp.txt stuff.doc
         q To make several directories in one go:
             $ mkdir Accounts Reports
   s The original use of cat was to join multiple files together
         q For example, to list two files, one after another:
             $ cat notes.txt morenotes.txt
   s If a filename contains spaces, or characters which are interpreted by the shell (such as *), put
       single quotes around them:
       $ rm ’Beatles - Strawberry Fields.mp3’
       $ cat ’* important notes.txt *’


2.13     Finding Documentation for Programs
   s Use the man command to read the manual for a program
   s The manual for a program is called its man page
         q Other things, like file formats and library functions also have man pages
   s To read a man page, specify the name of the program to man:
       $ man mkdir
   s To quit from the man page viewer press q
   s Man pages for programs usually have the following information:
         q A description of what it does
         q A list of options it accepts
         q Other information, such as the name of the author




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                  12
Linux System Administration                                                               Module 2. Getting Started


2.14     Specifying Files with Wildcards
   s Use the * wildcard to specify multiple filenames to a program:
       $ ls -l *.txt
       -rw-rw-r--  1 fred              users           108 Nov 16 13:06 report.txt
       -rw-rw-r--  1 fred              users           345 Jan 18 08:56 notes.txt
   s The shell expands the wildcard, and passes the full list of files to the program
   s Just using * on its own will expand to all the files in the current directory:
       $ rm *
         q (All the files, that is, except the hidden ones)
   s Names with wildcards in are called globs, and the process of expanding them is called
       globbing



2.15     Chaining Programs Together
   s The who command lists the users currently logged in
   s The wc command counts bytes, words, and lines in its input
   s We combine them to count how many users are logged in:
       $ who | wc -l
   s The | symbol makes a pipe between the two programs
         q The output of who is fed into wc
   s The -l option makes wc print only the number of lines
   s Another example, to join all the text files together and count the words, lines and characters in
       the result:
       $ cat *.txt | wc


2.16     Graphical and Text Interfaces
   s Most modern desktop Linux systems provide a graphical user interface (GUI)
   s Linux systems use the X window system to provide graphics
         q X is just another program, not built into Linux
         q Usually X is started automatically when the computer boots
   s Linux can be used without a GUI, just using a command line
   s Use Ctrl+Alt+F1 to switch to a text console — logging in works as it does in X
         q Use Ctrl+Alt+F2, Ctrl+Alt+F3, etc., to switch between virtual terminals — usually
             about 6 are provided
         q Use Ctrl+Alt+F7, or whatever is after the virtual terminals, to switch back to X




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                  13
Linux System Administration                                                                    Module 2. Getting Started


2.17      Text Editors
   s Text editors are for editing plain text files
          q Don’t provide advanced formatting like word processors
          q Extremely important — manipulating text is Unix’s raison d’être
   s The most popular editors are Emacs and Vim, both of which are very sophisticated, but take
        time to learn
   s Simpler editors include Nano, Pico, Kedit and Gnotepad
   s Some programs run a text editor for you
          q They use the $EDITOR variable to decide which editor to use
          q Usually it is set to vi, but it can be changed
          q Another example of the component philosophy




2.18      Exercises

   1.     a. Use the pwd command to find out what directory you are in.
          b. If you are not in your home directory (/home/USERNAME) then use cd without any arguments to go
             there, and do pwd again.
          c. Use cd to visit the root directory, and list the files there. You should see home among the list.
          d. Change into the directory called home and again list the files present. There should be one directory
             for each user, including the user you are logged in as (you can use whoami to check that).
          e. Change into your home directory to confirm that you have gotten back to where you started.
   2.     a. Create a text file in your home directory called shakespeare, containing the following text:
             Shall I compare thee to a summer’s day?
             Thou art more lovely and more temperate
          b. Rename it to sonnet-18.txt.
          c. Make a new directory in your home directory, called poetry.
          d. Move the poem file into the new directory.
          e. Try to find a graphical directory-browsing program, and find your home directory with it. You should also
             be able to use it to explore some of the system directories.
           f. Find a text editor program and use it to display and edit the sonnet.
   3.     a. From your home directory, list the files in the directory /usr/share.
          b. Change to that directory, and use pwd to check that you are in the right place. List the files in the current
             directory again, and then list the files in the directory called doc.
          c. Next list the files in the parent directory, and the directory above that.
          d. Try the following command, and make sure you understand the result:
             $ echo ˜
          e. Use cat to display the contents of a text file which resides in your home directory (create one if you


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                        14
Linux System Administration                                                                     Module 2. Getting Started


             haven’t already), using the ˜/ syntax to refer to it. It shouldn’t matter what your current directory is when
             you run the command.
   4.    a. Use the hostname command, with no options, to print the hostname of the machine you are using.
         b. Use man to display some documentation on the hostname command. Find out how to make it print the
            IP address of the machine instead of the hostname. You will need to scroll down the manpage to the
            ‘Options’ section.
         c. Use the locate command to find files whose name contains the text ‘hostname’. Which of the filenames
            printed contain the actual hostname program itself? Try running it by entering the program’s absolute
            path to check that you really have found it.
   5.    a. The * wildcard on its own is expanded by the shell to a list of all the files in the current directory. Use
            the echo command to see the result (but make sure you are in a directory with a few files or directories
            first)
         b. Use quoting to make echo print out an actual * symbol.
         c. Augment the poetry directory you created earlier with another file, sonnet-29.txt:
             When in disgrace with Fortune and men’s eyes,
             I all alone beweep my outcast state,
         d. Use the cat command to display both of the poems, using a wildcard.
         e. Finally, use the rm command to delete the poetry directory and the poems in it.




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                         15
Module 3


Work Effectively on the Unix Command Line

3.1   Shells
  s A shell provides an interface between the user and the operating system kernel
  s Either a command interpreter or a graphical user interface
  s Traditional Unix shells are command-line interfaces (CLIs)
  s Usually started automatically when you log in or open a terminal

                              User




                                        Shell




                                                    Kernel




3.2 The Bash Shell
  s Linux’s most popular command interpreter is called bash
       q The Bourne-Again Shell
       q More sophisticated than the original sh by Steve Bourne
       q Can be run as sh, as a replacement for the original Unix shell
  s Gives you a prompt and waits for a command to be entered
  s Although this course concentrates on Bash, the shell tcsh is also popular
       q Based on the design of the older C Shell (csh)




                                                     16
Linux System Administration                                      Module 3. Work Effectively on the Unix Command Line


3.3    Shell Commands
   s Shell commands entered consist of words
         q Separated by spaces (whitespace)
         q The first word is the command to run
         q Subsequent words are options or arguments to the command
   s For several reasons, some commands are built into the shell itself
         q Called builtins
         q Only a small number of commands are builtins, most are separate programs


3.4    Command-Line Arguments
   s The words after the command name are passed to a command as a list of arguments
   s Most commands group these words into two categories:
         q Options, usually starting with one or two hyphens
         q Filenames, directories, etc., on which to operate
   s The options usually come first, but for most commands they do not need to
   s There is a special option ‘--’ which indicates the end of the options
         q Nothing after the double hyphen is treated as an option, even if it starts with -


3.5    Syntax of Command-Line Options
   s Most Unix commands have a consistent syntax for options:
         q Single letter options start with a hyphen, e.g., -B
         q Less cryptic options are whole words or phrases, and start with two hyphens, for example
           --ignore-backups
   s Some options themselves take arguments
         q Usually the argument is the next word: sort -o output_file
   s A few programs use different styles of command-line options
         q For example, long options (not single letters) sometimes start with a single - rather
           than --




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                   17
Linux System Administration                                          Module 3. Work Effectively on the Unix Command Line


3.6    Examples of Command-Line Options
   s List all the files in the current directory:
      $ ls
   s List the files in the ‘long format’ (giving more information):
      $ ls -l
   s List full information about some specific files:
      $ ls -l notes.txt report.txt
   s List full information about all the .txt files:
      $ ls -l *.txt
   s List all files in long format, even the hidden ones:
      $ ls -l -a
      $ ls -la


3.7    Setting Shell Variables
   s Shell variables can be used to store temporary values
   s Set a shell variable’s value as follows:
      $ files="notes.txt report.txt"
         q The double quotes are needed because the value contains a space
         q Easiest to put them in all the time
   s Print out the value of a shell variable with the echo command:
      $ echo $files
         q The dollar ($) tells the shell to insert the variable’s value into the command line
   s Use the set command (with no arguments) to list all the shell variables


3.8    Environment Variables
   s Shell variables are private to the shell
   s A special type of shell variables called environment variables are passed to programs run
      from the shell
   s A program’s environment is the set of environment variables it can access
         q In Bash, use export to export a shell variable into the environment:
             $ files="notes.txt report.txt"
             $ export files
         q Or combine those into one line:
             $ export files="notes.txt report.txt"
   s The env command lists environment variables



Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                       18
Linux System Administration                                      Module 3. Work Effectively on the Unix Command Line


3.9    Where Programs are Found
   s The location of a program can be specified explicitly:
         q ./sample runs the sample program in the current directory
         q /bin/ls runs the ls command in the /bin directory
   s Otherwise, the shell looks in standard places for the program
         q The variable called PATH lists the directories to search in
         q Directory names are separated by colon, for example:
             $ echo $PATH
             /bin:/usr/bin:/usr/local/bin
         q So running whoami will run /bin/whoami or /usr/bin/whoami or /usr/local/bin/whoami
             (whichever is found first)



3.10     Bash Configuration Variables
   s Some variables contain information which Bash itself uses
         q The variable called PS1 (Prompt String 1) specifies how to display the shell prompt
   s Use the echo command with a $ sign before a varable name to see its value, e.g.
       $ echo $PS1
       [u@h W]$
   s The special characters u, h and W represent shell variables containing, respectively, your
       user/login name, machine’s hostname and current working directory, i.e.,
         q $USER, $HOSTNAME, $PWD


3.11     Using History
   s Previously executed commands can be edited with the Up or Ctrl+P keys
   s This allows old commands to be executed again without re-entering
   s Bash stores a history of old commands in memory
         q Use the built-in command history to display the lines remembered
         q History is stored between sessions in the file ˜/.bash_history
   s Bash uses the readline library to read input from the user
         q Allows Emacs-like editing of the command line
         q Left and Right cursor keys and Delete work as expected




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                   19
Linux System Administration                                      Module 3. Work Effectively on the Unix Command Line


3.12     Reusing History Items
   s Previous commands can be used to build new commands, using history expansion
   s Use !! to refer to the previous command, for example:
       $ rm index.html
       $ echo !!
       echo rm index.html
       rm index.html
   s More often useful is !string , which inserts the most recent command which started with
     string
         q Useful for repeating particular commands without modification:
             $ ls *.txt
             notes.txt report.txt
             $ !ls
             ls *.txt
             notes.txt report.txt


3.13     Retrieving Arguments from the History
   s The event designator !$ refers to the last argument of the previous command:
       $ ls -l long_file_name.html
       -rw-r--r-- 1 jeff users 11170 Oct 31 10:47 long_file_name.html
       $ rm !$
       rm long_file_name.html
   s Similarly, !ˆ refers to the first argument
   s A command of the form ˆstringˆreplacementˆ replaces the first occurrence of string
     with replacement in the previous command, and runs it:
       $ echo $HOTSNAME

       $ ˆTSˆSTˆ
       echo $HOSTNAME
       tiger




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                   20
Linux System Administration                                      Module 3. Work Effectively on the Unix Command Line


3.14     Summary of Bash Editing Keys
   s These are the basic editing commands by default:
         q Right — move cursor to the right
         q Left — move cursor to the left
         q Up — previous history line
         q Down — next history line
         q Ctrl+A — move to start of line
         q Ctrl+E — move to end of line
         q Ctrl+D — delete current character
   s There are alternative keys, as for the Emacs editor, which can be more comfortable to use than
       the cursor keys
   s There are other, less often used keys, which are documented in the bash man page (section
       ‘Readline’)



3.15     Combining Commands on One Line
   s You can write multiple commands on one line by separating them with ;
   s Useful when the first command might take a long time:
       time-consuming-program; ls
   s Alternatively, use && to arrange for subsequent commands to run only if earlier ones
       succeeded:
       time-consuming-potentially-failing-program && ls


3.16     Repeating Commands with for
   s Commands can be repeated several times using for
         q Structure: for varname in list; do commands...; done
   s For example, to rename all .txt files to .txt.old:
       $ for file in *.txt;
       > do
       >   mv -v $file $file.old;
       > done
       barbie.txt -> barbie.txt.old
       food.txt -> food.txt.old
       quirks.txt -> quirks.txt.old
   s The command above could also be written on a single line




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                   21
Linux System Administration                                      Module 3. Work Effectively on the Unix Command Line


3.17     Command Substitution
   s Command substitution allows the output of one command to be used as arguments to
       another
   s For example, use the locate command to find all files called manual.html and print information
     about them with ls:
       $ ls -l $(locate manual.html)
       $ ls -l ‘locate manual.html‘
   s The punctuation marks on the second form are opening single quote characters, called
       backticks
         q The $() form is usually preferred, but backticks are widely used
   s Line breaks in the output are converted to spaces
   s Another example: use vi to edit the last of the files found:
       $ vi $(locate manual.html | tail -1)


3.18     Finding Files with locate
   s The locate command is a simple and fast way to find files
   s For example, to find files relating to the email program mutt:
       $ locate mutt
   s The locate command searches a database of filenames
         q The database needs to be updated regularly
         q Usually this is done automatically with cron
         q But locate will not find files created since the last update
   s The -i option makes the search case-insensitive
   s -r treats the pattern as a regular expression, rather than a simple string


3.19     Finding Files More Flexibly: find
   s locate only finds files by name
   s find can find files by any combination of a wide number of criteria, including name
   s Structure: find directories               criteria
   s Simplest possible example: find .
   s Finding files with a simple criterion:
       $ find . -name manual.html
       Looks for files under the current directory whose name is manual.html
   s The criteria always begin with a single hyphen, even though they have long names




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                   22
Linux System Administration                                         Module 3. Work Effectively on the Unix Command Line


3.20      find Criteria
   s find accepts many different criteria; two of the most useful are:
          q -name pattern: selects files whose name matches the shell-style wildcard pattern
          q -type d, -type f: select directories or plain files, respectively
   s You can have complex selections involving ‘and’, ‘or’, and ‘not’


3.21      find Actions: Executing Programs
   s find lets you specify an action for each file found; the default action is simply to print out the
        name
          q You can alternatively write that explicitly as -print
   s Other actions include executing a program; for example, to delete all files whose name starts
        with manual:
        find . -name ’manual*’ -exec rm ’{}’ ’;’
   s The command rm ’{}’ is run for each file, with ’{}’ replaced by the filename
   s The {} and ; are required by find, but must be quoted to protect them from the shell




3.22      Exercises

   1.     a. Use the df command to display the amount of used and available space on your hard drive.
          b. Check the man page for df, and use it to find an option to the command which will display the free
             space in a more human-friendly form. Try both the single-letter and long-style options.
          c. Run the shell, bash, and see what happens. Remember that you were already running it to start with.
             Try leaving the shell you have started with the exit command.
   2.     a. Try ls with the -a and -A options. What is the difference between them?
          b. Write a for loop which goes through all the files in a directory and prints out their names with echo. If
             you write the whole thing on one line, then it will be easy to repeat it using the command line history.
          c. Change the loop so that it goes through the names of the people in the room (which needn’t be the
             names of files) and print greetings to them.
          d. Of course, a simpler way to print a list of filenames is echo *. Why might this be useful, when we
             usually use the ls command?
   3.     a. Use the find command to list all the files and directories under your home directory. Try the -type d
             and -type f criteria to show just files and just directories.
          b. Use locate to find files whose name contains the string ‘bashbug’. Try the same search with find,
             looking over all files on the system. You’ll need to use the * wildcard at the end of the pattern to match
             files with extensions.
          c. Find out what the find criterion -iname does.




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                      23
Module 4


Process Text Streams Using Text Processing
Filters

4.1   Working with Text Files
  s Unix-like systems are designed to manipulate text very well
  s The same techniques can be used with plain text, or text-based formats
       q Most Unix configuration files are plain text
  s Text is usually in the ASCII character set
       q Non-English text might use the ISO-8859 character sets
       q Unicode is better, but unfortunately many Linux command-line utilities don’t (directly)
           support it yet



4.2   Lines of Text
  s Text files are naturally divided into lines
  s In Linux a line ends in a line feed character
       q Character number 10, hexadecimal 0x0A
  s Other operating systems use different combinations
       q Windows and DOS use a carriage return followed by a line feed
       q Macintosh systems use only a carriage return
       q Programs are available to convert between the various formats




                                                       24
Linux System Administration                                      Module 4. Process Text Streams Using Text Processing Filters


4.3    Filtering Text and Piping
   s The Unix philosophy: use small programs, and link them together as needed
   s Each tool should be good at one specific job
   s Join programs together with pipes
         q Indicated with the pipe character: |
         q The first program prints text to its standard output
         q That gets fed into the second program’s standard input
   s For example, to connect the output of echo to the input of wc:
      $ echo "count these words, boy" | wc


4.4    Displaying Files with less
   s If a file is too long to fit in the terminal, display it with less:
      $ less README
   s less also makes it easy to clear the terminal of other things, so is useful even for small files
   s Often used on the end of a pipe line, especially when it is not known how long the output will be:
      $ wc *.txt | less
   s Doesn’t choke on strange characters, so it won’t mess up your terminal (unlike cat)


4.5    Counting Words and Lines with wc
   s wc counts characters, words and lines in a file
   s If used with multiple files, outputs counts for each file, and a combined total
   s Options:
         q -c output character count
         q -l output line count
         q -w output word count
         q Default is -clw
   s Examples: display word count for essay.txt:
      $ wc -w essay.txt
   s Display the total number of lines in several text files:
      $ wc -l *.txt




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                            25
Linux System Administration                                      Module 4. Process Text Streams Using Text Processing Filters


4.6    Sorting Lines of Text with sort
   s The sort filter reads lines of text and prints them sorted into order
   s For example, to sort a list of words into dictionary order:
      $ sort words > sorted-words
   s The -f option makes the sorting case-insensitive
   s The -n option sorts numerically, rather than lexicographically


4.7 Removing Duplicate Lines with uniq
   s Use uniq to find unique lines in a file
         q Removes consecutive duplicate lines
         q Usually give it sorted input, to remove all duplicates
   s Example: find out how many unique words are in a dictionary:
      $ sort /usr/dict/words | uniq | wc -w
   s sort has a -u option to do this, without using a separate program:
      $ sort -u /usr/dict/words | wc -w
   s sort | uniq can do more than sort -u, though:
         q uniq -c counts how many times each line appeared
         q uniq -u prints only unique lines
         q uniq -d prints only duplicated lines


4.8    Selecting Parts of Lines with cut
   s Used to select columns or fields from each line of input
   s Select a range of
         q Characters, with -c
         q Fields, with -f
   s Field separator specified with -d (defaults to tab)
   s A range is written as start and end position: e.g., 3-5
         q Either can be omitted
         q The first character or field is numbered 1, not 0
   s Example: select usernames of logged in users:
      $ who | cut -d" " -f1 | sort -u




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                            26
Linux System Administration                                      Module 4. Process Text Streams Using Text Processing Filters


4.9    Expanding Tabs to Spaces with expand
   s Used to replace tabs with spaces in files
   s Tab size (maximum number of spaces for each tab) can be set with -t number
         q Default tab size is 8
   s To only change tabs at the beginning of lines, use -i
   s Example: change all tabs in foo.txt to three spaces, display it to the screen:
       $ expand -t 3 foo.txt
       $ expand -3 foo.txt


4.10     Using fmt to Format Text Files
   s Arranges words nicely into lines of consistent length
   s Use -u to convert to uniform spacing
         q One space between words, two between sentences
   s Use -w width to set the maximum line width in characters
         q Defaults to 75
   s Example: change the line length of notes.txt to a maximum of 70 characters, and display it on
       the screen:
       $ fmt -w 70 notes.txt | less


4.11     Reading the Start of a File with head
   s Prints the top of its input, and discards the rest
   s Set the number of lines to print with -n lines or -lines
         q Defaults to ten lines
   s View the headers of a HTML document called homepage.html:
       $ head homepage.html
   s Print the first line of a text file (two alternatives):
       $ head -n 1 notes.txt
       $ head -1 notes.txt




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                            27
Linux System Administration                                      Module 4. Process Text Streams Using Text Processing Filters


4.12     Reading the End of a File with tail
   s Similar to head, but prints lines at the end of a file
   s The -f option watches the file forever
         q Continually updates the display as new entries are appended to the end of the file
         q Kill it with Ctrl+C
   s The option -n is the same as in head (number of lines to print)
   s Example: monitor HTTP requests on a webserver:
       $ tail -f /var/log/httpd/access_log


4.13     Numbering Lines of a File with nl or cat
   s Display the input with line numbers against each line
   s There are options to finely control the formating
   s By default, blank lines aren’t numbered
         q The option -ba numbers every line
         q cat -n also numbers lines, including blank ones


4.14 Dumping Bytes of Binary Data with od
   s Prints the numeric values of the bytes in a file
   s Useful for studying files with non-text characters
   s By default, prints two-byte words in octal
   s Specify an alternative with the -t option
         q Give a letter to indicate base: o for octal, x for hexadecimal, u for unsigned decimal, etc.
         q Can be followed by the number of bytes per word
         q Add z to show ASCII equivalents alongside the numbers
         q A useful format is given by od -t x1z — hexadecimal, one byte words, with ASCII
   s Alternatives to od include xxd and hexdump




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                            28
Linux System Administration                                      Module 4. Process Text Streams Using Text Processing Filters


4.15     Paginating Text Files with pr
   s Convert a text file into paginated text, with headers and page fills
   s Rarely useful for modern printers
   s Options:
         q -d double spaced output
         q -h header change from the default header to header
         q -l lines change the default lines on a page from 66 to lines
         q -o width set (‘offset’) the left margin to width
   s Example:
       $ pr -h "My Thesis" thesis.txt | lpr


4.16     Dividing Files into Chunks with split
   s Splits files into equal-sized segments
   s Syntax: split [options] [input] [output-prefix]
   s Use -l n to split a file into n-line chunks
   s Use -b n to split into chunks of n bytes each
   s Output files are named using the specified output name with aa, ab, ac, etc., added to the end
       of the prefix
   s Example: Split essay.txt into 30-line files, and save the output to files short_aa, short_ab, etc:
       $ split -l 30 essay.txt short_


4.17     Using split to Span Disks
   s If a file is too big to fit on a single floppy, Zip or CD-ROM disk, it can be split into small enough
       chunks
   s Use the -b option, and with the k and m sufixes to give the chunk size in kilobytes or megabytes
   s For example, to split the file database.tar.gz into pieces small enough to fit on Zip disks:
       $ split -b 90m database.tar.gz zip-
   s Use cat to put the pieces back together:
       $ cat zip-* > database.tar.gz


4.18     Reversing Files with tac
   s Similar to cat, but in reverse
   s Prints the last line of the input first, the penultimate line second, and so on
   s Example: show a list of logins and logouts, but with the most recent events at the end:
       $ last | tac

Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                            29
Linux System Administration                                      Module 4. Process Text Streams Using Text Processing Filters


4.19     Translating Sets of Characters with tr
   s tr translates one set of characters to another
   s Usage: tr start-set             end-set
   s Replaces all characters in start-set with the corresponding characters in end-set
   s Cannot accept a file as an argument, but uses the standard input and output
   s Options:
         q -d deletes characters in start-set instead of translating them
         q -s replaces sequences of identical characters with just one (squeezes them)


4.20     tr Examples
   s Replace all uppercase characters in input-file with lowercase characters (two alternatives):
       $ cat input-file | tr A-Z a-z
       $ tr A-Z a-z < input-file
   s Delete all occurrences of z in story.txt:
       $ cat story.txt | tr -d z
   s Run together each sequence of repeated f characters in lullaby.txt to with just one f:
       $ tr -s f < lullaby.txt


4.21     Modifying Files with sed
   s sed uses a simple script to process each line of a file
   s Specify the script file with -f filename
   s Or give individual commands with -e command
   s For example, if you have a script called spelling.sed which corrects your most common
       mistakes, you can feed a file through it:
       $ sed -f spelling.sed < report.txt > corrected.txt


4.22     Substituting with sed
   s Use the s/pattern/replacement/ command to substitute text matching the pattern
     with the replacement
         q Add the /g modifier to replace every occurrence on each line, rather than just the first one
   s For example, replace ‘thru’ with ‘through’:
       $ sed -e ’s/thru/through/g’ input-file > output-file
   s sed has more complicated facilities which allow commands to be executed conditionally
         q Can be used as a very basic (but unpleasantly difficult!) programming language




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                            30
Linux System Administration                                      Module 4. Process Text Streams Using Text Processing Filters


4.23     Put Files Side-by-Side with paste
   s paste takes lines from two or more files and puts them in columns of the output
   s Use -d char to set the delimiter between fields in the output
         q The default is tab
         q Giving -d more than one character sets different delimiters between each pair of columns
   s Example: assign passwords to users, separating them with a colon:
        $ paste -d: usernames passwords > .htpasswd


4.24     Performing Database Joins with join
   s Does a database-style ‘inner join’ on two tables, stored in text files
   s The -t option sets the field delimiter
         q By default, fields are separated by any number of spaces or tabs
   s Example: show details of suppliers and their products:
        $ join suppliers.txt products.txt | less
   s The input files must be sorted!
   s This command is rarely used — databases have this facility built in




4.25     Exercises

   1.    a. Type in the example on the cut slide to display a list of users logged in. (Try just who on its own first to
            see what is happening.)
         b. Arrange for the list of usernames in who’s output to be sorted, and remove any duplicates.
         c. Try the command last to display a record of login sessions, and then try reversing it with tac. Which
            is more useful? What if you pipe the output into less?
         d. Use sed to correct the misspelling ‘enviroment’ to ‘environment’. Use it on a test file, containing a few
            lines of text, to check it. Does it work if the misspelling occurs more than once on the same line?
         e. Use nl to number the lines in the output of the previous question.
   2.    a. Try making an empty file and using tail -f to monitor it. Then add lines to it from a different terminal,
            using a command like this:
             $ echo "testing" >>filename
         b. Once you have written some lines into your file, use tr to display it with all occurances of the letters
            A–F changed to the numbers 0–5.
         c. Try looking at the binary for the ls command (/bin/ls) with less. You can use the -f option to force it to
            display the file, even though it isn’t text.
         d. Try viewing the same binary with od. Try it in its default mode, as well as with the options shown on the
            slide for outputting in hexadecimal.
   3.    a. Use the split command to split the binary of the ls command into 1Kb chunks. You might want to

Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                            31
Linux System Administration                                      Module 4. Process Text Streams Using Text Processing Filters


             create a directory especially for the split files, so that it can all be easily deleted later.
         b. Put your split ls command back together again, and run it to make sure it still works. You will have to
            make sure you are running the new copy of it, for example ./my_ls, and make sure that the program is
            marked as ‘executable’ to run it, with the following command:
             $ chmod a+rx my_ls




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                            32
Module 5


Perform Basic File Management

5.1   Filesystem Objects
  s A file is a place to store data: a possibly-empty sequence of bytes
  s A directory is a collection of files and other directories
  s Directories are organized in a hierarchy, with the root directory at the top
  s The root directory is referred to as /

                                                   /



                                     home/                       bin/




                            jeff/                      cp               rm



5.2 Directory and File Names
  s Files and directories are organized into a filesystem
  s Refer to files in directories and sub-directories by separating their names with /, for example:
      /bin/ls
      /usr/share/dict/words
      /home/jeff/recipe
  s Paths to files either start at / (absolute) or from some ‘current’ directory




                                                            33
Linux System Administration                                                Module 5. Perform Basic File Management


5.3    File Extensions
   s It’s common to put an extension, beginning with a dot, on the end of a filename
   s The extension can indicate the type of the file:
          .txt             Text file
          .gif             Graphics Interchange Format image
          .jpg             Joint Photographic Experts Group image
          .mp3             MPEG-2 Layer 3 audio
          .gz              Compressed file
          .tar             Unix ‘tape archive’ file
          .tar.gz, .tgz    Compressed archive file

   s On Unix and Linux, file extensions are just a convention
         q The kernel just treats them as a normal part of the name
         q A few programs use extensions to determine the type of a file


5.4    Going Back to Previous Directories
   s The pushd command takes you to another directory, like cd
         q But also saves the current directory, so that you can go back later
   s For example, to visit Fred’s home directory, and then go back to where you started from:
      $ pushd ˜fred
      $ cd Work
      $ ls
      ...
      $ popd
   s popd takes you back to the directory where you last did pushd
   s dirs will list the directories you can pop back to


5.5    Filename Completion
   s Modern shells help you type the names of files and directories by completing partial names
   s Type the start of the name (enough to make it unambiguous) and press Tab
   s For an ambiguous name (there are several possible completions), the shell can list the options:
         q For Bash, type Tab twice in succession
         q For C shells, type Ctrl+D
   s Both of these shells will automatically escape spaces and special characters in the filenames




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                 34
Linux System Administration                                                Module 5. Perform Basic File Management


5.6    Wildcard Patterns
   s Give commands multiple files by specifying patterns
   s Use the symbol * to match any part of a filename:
      $ ls *.txt
      accounts.txt        letter.txt       report.txt
   s Just * produces the names of all files in the current directory
   s The wildcard ? matches exactly one character:
      $ rm -v data.?
      removing data.1
      removing data.2
      removing data.3
   s Note: wildcards are turned into filenames by the shell, so the program you pass them to can’t
      tell that those names came from wildcard expansion



5.7    Copying Files with cp
   s Syntax: cp [options] source-file                    destination-file
   s Copy multiple files into a directory: cp files           directory
   s Common options:
         q -f, force overwriting of destination files
         q -i, interactively prompt before overwriting files
         q -a, archive, copy the contents of directories recursively


5.8    Examples of cp
   s Copy /etc/smb.conf to the current directory:
      $ cp /etc/smb.conf .
   s Create an identical copy of a directory called work, and call it work-backup:
      $ cp -a work work-backup
   s Copy all the GIF and JPEG images in the current directory into images:
      $ cp *.gif *.jpeg images/




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                 35
Linux System Administration                                                Module 5. Perform Basic File Management


5.9    Moving Files with mv
   s mv can rename files or directories, or move them to different directories
   s It is equivalent to copying and then deleting
         q But is usually much faster
   s Options:
         q -f, force overwrite, even if target already exists
         q -i, ask user interactively before overwriting files
   s For example, to rename poetry.txt to poems.txt:
       $ mv poetry.txt poems.txt
   s To move everything in the current directory somewhere else:
       $ mv * ˜/old-stuff/


5.10     Deleting Files with rm
   s rm deletes (‘removes’) the specified files
   s You must have write permission for the directory the file is in to remove it
   s Use carefully if you are logged in as root!
   s Options:
         q -f, delete write-protected files without prompting
         q -i, interactive — ask the user before deleting files
         q -r, recursively delete files and directories
   s For example, clean out everything in /tmp, without prompting to delete each file:
       $ rm -rf /tmp/*


5.11     Deleting Files with Peculiar Names
   s Some files have names which make them hard to delete
   s Files that begin with a minus sign:
       $ rm ./-filename
       $ rm -- -filename
   s Files that contain peculiar characters — perhaps characters that you can’t actually type on your
       keyboard:
         q Write a wildcard pattern that matches only the name you want to delete:
             $ rm -i ./name-with-funny-characters*
         q The ./ forces it to be in the current directory
         q Using the -i option to rm makes sure that you won’t delete anything else by accident




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                 36
Linux System Administration                                                     Module 5. Perform Basic File Management


5.12        Making Directories with mkdir
   s Syntax: mkdir directory-names
   s Options:
            q -p, create intervening parent directories if they don’t already exist
            q -m mode, set the access permissions to mode
   s For example, create a directory called mystuff in your home directory with permissions so that
       only you can write, but eveyone can read it:
       $ mkdir -m 755 ˜/mystuff
   s Create a directory tree in /tmp using one command with three subdirectories called one, two
       and three:
       $ mkdir -p /tmp/one/two/three


5.13        Removing Directories with rmdir
   s rmdir deletes empty directories, so the files inside must be deleted first
   s For example, to delete the images directory:
       $ rm images/*
       $ rmdir images
   s For non-empty directories, use rm -r directory
   s The -p option to rmdir removes the complete path, if there are no other files and directories in
       it
            q These commands are equivalent:
               $ rmdir -p a/b/c
               $ rmdir a/b/c a/b         a


5.14        Identifying Types of Files
   s The data in files comes in various different formats (executable programs, text files, etc.)
   s The file command will try to identify the type of a file:
       $ file /bin/bash
       /bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1,
       dynamically linked (uses shared libs), stripped
   s It also provides extra information about some types of file
   s Useful to find out whether a program is actually a script:
       $ file /usr/bin/zless
       /usr/bin/zless: Bourne shell script text
   s If file doesn’t know about a specific format, it will guess:
       $ file /etc/passwd
       /etc/passwd: ASCII text


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                      37
Linux System Administration                                                  Module 5. Perform Basic File Management


5.15     Changing Timestamps with touch
   s Changes the access and modification times of files
   s Creates files that didn’t already exist
   s Options:
         q -a, change only the access time
         q -m, change only the modification time
         q -t [YYYY]MMDDhhmm[.ss], set the timestamp of the file to the specified date and
             time
         q GNU touch has a -d option, which accepts times in a more flexible format
   s For example, change the time stamp on homework to January 20 2001, 5:59p.m.
        $ touch -t 200101201759 homework




5.16     Exercises

   1.    a. Use cd to go to your home directory, and create a new directory there called dog.
         b. Create another directory within that one called cat, and another within that called mouse.
         c. Remove all three directories. You can either remove them one at a time, or all at once.
         d. If you can delete directories with rm -r, what is the point of using rmdir for empty directories?
         e. Try creating the dog/cat/mouse directory structure with a single command.
   2.    a. Copy the file /etc/passwd to your home directory, and then use cat to see what’s in it.
         b. Rename it to users using the mv command.
         c. Make a directory called programs and copy everything from /bin into it.
         d. Delete all the files in the programs directory.
         e. Delete the empty programs directory and the users file.
   3.    a. The touch command can be used to create new empty files. Try that now, picking a name for the new
            file:
             $ touch baked-beans
         b. Get details about the file using the ls command:
             $ ls -l baked-beans
         c. Wait for a minute, and then try the previous two steps again, and see what changes. What happens
            when we don’t specify a time to touch?
         d. Try setting the timestamp on the file to a value in the future.
         e. When you’re finished with it, delete the file.




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                   38
Module 6


Use Unix Streams, Pipes and Redirects

6.1   Standard Files
  s Processes are connected to three standard files

               Standard input
                                                             Program




                                                                  Standard       Standard
                                                                   output          error




  s Many programs open other files as well


6.2   Standard Input
  s Programs can read data from their standard input file
  s Abbreviated to stdin
  s By default, this reads from the keyboard
  s Characters typed into an interactive program (e.g., a text editor) go to stdin




                                                        39
Linux System Administration                                       Module 6. Use Unix Streams, Pipes and Redirects


6.3    Standard Output
   s Programs can write data to their standard output file
   s Abbreviated to stdout
   s Used for a program’s normal output
   s By default this is printed on the terminal


6.4 Standard Error
   s Programs can write data to their standard error output
   s Standard error is similar to standard output, but used for error and warning messages
   s Abbreviated to stderr
   s Useful to separate program output from any program errors
   s By default this is written to your terminal
         q So it gets ‘mixed in’ with the standard output


6.5    Pipes
   s A pipe channels the output of one program to the input of another
         q Allows programs to be chained together
         q Programs in the chain run concurrently
   s Use the vertical bar: |
         q Sometimes known as the ‘pipe’ character
   s Programs don’t need to do anything special to use pipes
         q They read from stdin and write to stdout as normal
   s For example, pipe the output of echo into the program rev (which reverses each line of its
      input):
      $ echo Happy Birthday! | rev
      !yadhtriB yppaH




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                40
Linux System Administration                                       Module 6. Use Unix Streams, Pipes and Redirects


6.6    Connecting Programs to Files
   s Redirection connects a program to a named file
   s The < symbol indicates the file to read input from:
      $ wc < thesis.txt
         q The file specified becomes the program’s standard input
   s The > symbol indicates the file to write output to:
      $ who > users.txt
         q The program’s standard output goes into the file
         q If the file already exists, it is overwritten
   s Both can be used at the same time:
      $ filter < input-file > output-file


6.7 Appending to Files
   s Use >> to append to a file:
      $ date >> log.txt
         q Appends the standard output of the program to the end of an existing file
         q If the file doesn’t already exist, it is created


6.8    Redirecting Multiple Files
   s Open files have numbers, called file descriptors
   s These can be used with redirection
   s The three standard files always have the same numbers:
          Name                  Descriptor
          Standard input        0
          Standard output       1
          Standard error        2




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                41
Linux System Administration                                              Module 6. Use Unix Streams, Pipes and Redirects


6.9    Redirection with File Descriptors
   s Redirection normally works with stdin and stdout
   s Specify different files by putting the file descriptor number before the redirection symbol:
         q To redirect the standard error to a file:
             $ program 2> file
         q To combine standard error with standard output:
             $ program > file 2>&1
         q To save both output streams:
             $ program > stdout.txt 2> stderr.txt
   s The descriptors 3–9 can be connected to normal files, and are mainly used in shell scripts


6.10     Running Programs with xargs
   s xargs reads pieces of text and runs another program with them as its arguments
         q Usually its input is a list of filenames to give to a file processing program
   s Syntax: xargs command [initial args]
   s Use -l n to use n items each time the command is run
         q The default is 1
   s xargs is very often used with input piped from find
   s Example: if there are too many files in a directory to delete in one go, use xargs to delete them
       ten at a time:
       $ find /tmp/rubbish/ | xargs -l10 rm -f


6.11     tee
   s The tee program makes a ‘T-junction’ in a pipeline
   s It copies data from stdin to stdout, and also to a file
   s Like > and | combined
   s For example, to save details of everyone’s logins, and save Bob’s logins in a separate file:
       $ last | tee everyone.txt | grep bob > bob.txt

                             PIPE                   PIPE                REDIRECT
                 last                    tee                     grep               bob.txt




                                    everyone.txt




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                       42
Linux System Administration                                         Module 6. Use Unix Streams, Pipes and Redirects


6.12     Exercises

   1.    a. Try the example on the ‘Pipes’ slide, using rev to reverse some text.
         b. Try replacing the echo command with some other commands which produce output (e.g., whoami).
         c. What happens when you replace rev with cat? You might like to try running cat with no arguments
            and entering some text.
   2.    a. Run the command ls --color in a directory with a few files and directories. Some Linux distributions
            have ls set up to always use the --color option in normal circumstances, but in this case we will give
            it explicitly.
         b. Try running the same command, but pipe the output into another program (e.g., cat or less). You
            should spot two differences in the output. ls detects whether its output is going straight to a terminal
            (to be viewed by a human directly) or into a pipe (to be read by another program).




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                   43
Module 7


Search Text Files Using Regular Expressions

7.1   Searching Files with grep
  s grep prints lines from files which match a pattern
  s For example, to find an entry in the password file /etc/passwd relating to the user ‘nancy’:
      $ grep nancy /etc/passwd
  s grep has a few useful options:
        q -i makes the matching case-insensitive
        q -r searches through files in specified directories, recursively
        q -l prints just the names of files which contain matching lines
        q -c prints the count of matches in each file
        q -n numbers the matching lines in the output
        q -v rev erses the test, printing lines which don’t match


7.2   Pattern Matching
  s Use grep to find patterns, as well as simple strings
  s Patterns are expressed as regular expressions
  s Certain punctuation characters have special meanings
  s For example this might be a better way to search for Nancy’s entry in the password file:
      $ grep ’ˆnancy’ /etc/passwd
        q The caret (ˆ) anchors the pattern to the start of the line
  s In the same way, $ acts as an anchor when it appears at the end of a string, making the
      pattern match only at the end of a line




                                                        44
Linux System Administration                                      Module 7. Search Text Files Using Regular Expressions


7.3    Matching Repeated Patterns
   s Some regexp special characters are also special to the shell, and so need to be protected with
      quotes or backslashes
   s We can match a repeating pattern by adding a modifier:
      $ grep -i ’continued.*’
   s Dot (.) on its own would match any character, so to match an actual dot we escape it with 
   s The * modifier matches the preceding character zero or more times
   s Similarly, the + modifier matches one or more times


7.4    Matching Alternative Patterns
   s Multiple subpatterns can be provided as alternatives, separated with |, for example:
      $ grep ’fish|chips|pies’ food.txt
   s The previous command finds lines which match at least one of the words
   s Use (...) to enforce precedence:
      $ grep -i ’(cream|fish|birthday) cakes’ delicacies.txt
   s Use square brackets to build a character class:
      $ grep ’[Jj]oe [Bb]loggs’ staff.txt
   s Any single character from the class matches; and ranges of characters can be expressed as
     ‘a-z’



7.5 Extended Regular Expression Syntax
   s egrep runs grep in a different mode
         q Same as grep -E
   s Special characters don’t have to be marked with 
         q So + is written +, (...) is written (...), etc
         q In extended regexps, + is a literal +


7.6    sed
   s sed reads input lines, runs editing-style commands on them, and writes them to stdout
   s sed uses regular expressions as patterns in substitutions
         q sed regular expressions use the same syntax as grep
   s For example, to used sed to put # at the start of each line:
      $ sed -e ’s/ˆ/#/’ < input.txt > output.txt
   s sed has simple substitution and translation facilities, but can also be used like a programming
      language


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                     45
Linux System Administration                                      Module 7. Search Text Files Using Regular Expressions


7.7     Further Reading
   s man 7 regex
   s Sed and Awk, 2nd edition, by Dale Dougherty and Arnold Robbins, 1997
   s The Sed FAQ, http://www.dbnet.ece.ntua.gr/˜george/sed/sedfaq.html
   s The original Sed user manual (1978), http://www.urc.bl.ac.yu/manuals/progunix/sed.txt




7.8 Exercises

   1.    a. Use grep to find information about the HTTP protocol in the file /etc/services.
         b. Usually this file contains some comments, starting with the ‘#’ symbol. Use grep with the -v option to
            ignore lines starting with ‘#’ and look at the rest of the file in less.
         c. Add another use of grep -v to your pipeline to remove blank lines (which match the pattern ˆ$).
         d. Use sed (also in the same pipeline) to remove the information after the ‘/’ symbol on each line, leaving
            just the names of the protocols and their port numbers.




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                     46
Module 8


Job Control

8.1   Job Control
  s Most shells offer job control
       q The ability to stop, restart, and background a running process
  s The shell lets you put & on the end of a command line to start it in the background
  s Or you can hit Ctrl+Z to suspend a running foreground job
  s Suspended and backgrounded jobs are given numbers by the shell
  s These numbers can be given to shell job-control built-in commands
  s Job-control commands include jobs, fg, and bg


8.2   jobs
  s The jobs builtin prints a listing of active jobs and their job numbers:
      $ jobs
      [1]- Stopped                          vim index.html
      [2]    Running                        netscape &
      [3]+ Stopped                          man ls
  s Job numbers are given in square brackets
       q But when you use them with other job-control builtins, you need to write them with percent
         signs, for example %1
  s The jobs marked + and - may be accessed as %+ or %- as well as by number
       q %+ is the shell’s idea of the current job — the most recently active job
       q %- is the previous current job




                                                        47
Linux System Administration                                                                     Module 8. Job Control


8.3     fg
   s Brings a backgrounded job into the foreground
   s Re-starts a suspended job, running it in the foreground
   s fg %1 will foreground job number 1
   s fg with no arguments will operate on the current job


8.4     bg
   s Re-starts a suspended job, running it in the background
   s bg %1 will background job number 1
   s bg with no arguments will operate on the current job
   s For example, after running gv and suspending it with Ctrl+Z, use bg to start it running again in
        the background




8.5 Exercises

   1.     a. Start a process by running man bash and suspend it with Ctrl+Z.
          b. Run xclock in the background, using &.
          c. Use jobs to list the backgrounded and stopped processes.
          d. Use the fg command to bring man into the foreground, and quit from it as normal.
          e. Use fg to foreground xclock, and terminate it with Ctrl+C.
          f. Run xclock again, but this time without &. It should be running in the foreground (so you can’t use the
             shell). Try suspending it with Ctrl+Z and see what happens. To properly put it into the background,
             use bg.




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                    48
Module 9


Create, Monitor, and Kill Processes

9.1   What is a Process?
  s The kernel considers each program running on your system to be a process
  s A process ‘lives’ as it executes, with a lifetime that may be short or long
  s A process is said to ‘die’ when it terminates
  s The kernel identifies each process by a number known as a process id, or pid
  s The kernel keeps track of various properties of each process


9.2   Process Properties
  s A process has a user id (uid) and a group id (gid) which together specify what permissions it
      has
  s A process has a parent process id (ppid) — the pid of the process which created it
        q The kernel starts an init process with pid 1 at boot-up
        q Every other process is a descendant of pid 1
  s Each process has its own working directory, initially inherited from its parent process
  s There is an environment for each process — a collection of named environment variables and
      their associated values
        q A process’s environment is normally inherited from its parent process




                                                        49
Linux System Administration                                              Module 9. Create, Monitor, and Kill Processes


9.3    Parent and Child Processes
   s The init process is the ancestor of all other processes:

                                  init

                                                     bash

                                                     bash           vi

                                                   apache

                                                                 apache

                                                                 apache

                                                                 apache

   s (Apache starts many child processes so that they can serve HTTP requests at the same time)


9.4 Process Monitoring: ps
   s The ps command gives a snapshot of the processes running on a system at a given moment in
      time
   s Very flexible in what it shows, and how:
         q Normally shows a fairly brief summary of each process
         q Normally shows only processes which are both owned by the current user and attached
             to a terminal
   s Unfortunately, it doesn’t use standard option syntax
   s Instead it uses a mixture of options with one of three syntaxes:
         q Traditional BSD ps: a single letter with no hyphen
         q Unix98 ps: a single letter preceded by a hyphen
         q GNU: a word or phrase preceded by two hyphens




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                     50
Linux System Administration                                                    Module 9. Create, Monitor, and Kill Processes


9.5     ps Options
   s ps has many options
   s Some of the most commonly used are:
          Option           Description
          a                Show processes owned by other users
          f                Display process ancestors in a tree-like format
          u                Use the ‘user’ output format, showing user names and process
                           start times
          w                Use a wider output format. Normally each line of output is
                           truncated; each use of the w option makes the ‘window’ wider
          x                Include processes which have no controlling terminal
          -e               Show information on all processes
          -l               Use a ‘long’ output format
          -f               Use a ‘full’ output format
          -C cmd           Show only processes named cmd
          -U user          Show only processes owned by user



9.6     Process Monitoring: pstree
   s Displays a snapshot of running processes
   s Always uses a tree-like display, like ps f
         q But by default shows only the name of each command
   s Normally shows all processes
         q Specify a pid as an argument to show a specific process and its descendants
         q Specify a user name as an argument to show process trees owned by that user


9.7     pstree Options
   Option       Description
   -a           Display commands’ arguments
   -c           Don’t compact identical subtrees
   -G           Attempt to use terminal-specific line-drawing characters
   -h           Highlight the ancestors of the current process
   -n           Sort processes numerically by pid, rather than alphabetically by
                name
   -p           Include pids in the output



9.8     Process Monitoring: top
   s Shows full-screen, continuously-updated snapshots of process activity
         q Waits a short period of time between each snapshot to give the illusion of real-time
              monitoring
   s Processes are displayed in descending order of how much processor time they’re using
   s Also displays system uptime, load average, CPU status, and memory information

Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                           51
Linux System Administration                                                 Module 9. Create, Monitor, and Kill Processes


9.9      top Command-Line Options
   Option           Description
   -b               Batch mode — send snapshots to standard output
   -n num           Exit after displaying num snapshots
   -d delay         Wait delay seconds between each snapshot
   -i               Ignore idle processes
   -s               Disable interactive commands which could be dangerous if run by
                    the superuser



9.10     top Interactive Commands
   Key          Behaviour
   q            Quit the program
   Ctrl+L       Repaint the screen
   h            Show a help screen
   k            Prompts for a pid and a signal, and sends that signal to that
                process
   n            Prompts for the number of processes to show information; 0 (the
                default) means to show as many as will fit
   r            Change the priority (‘niceness’) of a process
   s            Change the number of seconds to delay between updates. The
                number may include fractions of a second (0.5, for example)



9.11     Signalling Processes
   s A process can be sent a signal by the kernel or by another process
   s Each signal is a very simple message:
         q A small whole number
         q With a mnemonic name
   s Signal names are all-capitals, like INT
         q They are often written with SIG as part of the name: SIGINT
   s Some signals are treated specially by the kernel; others have a conventional meaning
   s There are about 30 signals available, not all of which are very useful




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                        52
Linux System Administration                                                    Module 9. Create, Monitor, and Kill Processes


9.12      Common Signals for Interactive Use
   s The command kill -l lists all signals
   s The following are the most commonly used:
   Name           Number    Meaning
   INT                 2    Interrupt — stop running. Sent by the kernel when
                            you press Ctrl+C in a terminal.
   TERM               15    “Please terminate.” Used to ask a process to exit
                            gracefully.
   KILL                9    “Die!” Forces the process to stop running; it is given
                            no opportunity to clean up after itself.
   TSTP               18    Requests the process to stop itself temporarily. Sent
                            by the kernel when you press Ctrl+Z in a terminal.
   HUP                 1    Hang up. Sent by the kernel when you log out, or
                            disconnect a modem. Conventionally used by many
                            dæmons as an instruction to re-read a configuration
                            file.



9.13      Sending Signals: kill
   s The kill command is used to send a signal to a process
          q Not just to terminate a running process!
   s It is a normal executable command, but many shells also provide it as a built-in
   s Use kill -HUP pid or kill -s HUP pid to send a SIGHUP to the process with that pid
   s If you miss out the signal name, kill will send a SIGTERM
   s You can specify more than one pid to signal all those processes


9.14      Sending Signals to Dæmons: pidof
   s On Unix systems, long-lived processes that provide some service are often referred to as
        dæmons
   s Dæmons typically have a configuration file (usually under /etc) which affects their behaviour
   s Many dæmons read their configuration file only at startup
   s If the configuration changes, you have to explicitly tell the dæmon by sending it a SIGHUP signal
   s You can sometimes use pidof to find the dæmon’s pid; for example, to tell the inetd dæmon
        to reload its configuration, run:
        $ kill -HUP $(pidof /usr/sbin/inetd)
        as root




9.15      Exercises

   1.     a. Use top to show the processes running on your machine.



Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                           53
Linux System Administration                                              Module 9. Create, Monitor, and Kill Processes


         b. Make top sort by memory usage, so that the most memory-hungry processes appear at the top.
         c. Restrict the display to show only processes owned by you.
         d. Try killing one of your processes (make sure it’s nothing important).
         e. Display a list of all the processes running on the machine using ps (displaying the full command line for
            them).
          f. Get the same listing as a tree, using both ps and pstree.
         g. Have ps sort the output by system time used.




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                     54
Module 10


Modify Process Execution Priorities

10.1     Concepts
  s Not all tasks require the same amount of execution time
  s Linux has the concept of execution priority to deal with this
  s Process priority is dynamically altered by the kernel
  s You can view the current priority by looking at top or ps -l and looking at the PRI column
  s The priority can be biased using nice
         q The current bias can be seen in the NI column in top


10.2     nice
  s Starts a program with a given priority bias
  s Peculiar name: ‘nicer’ processes require fewer resources
  s Niceness ranges from +19 (very nice) to −20 (not very nice)
  s Non-root users can only specify values from 1 to 19; the root user can specify the full range of
       values
  s Default niceness when using nice is 10
  s To run a command at increased niceness (lower priority):
       $ nice -10 long-running-command &
       $ nice -n 10 long-running-command &
  s To run a command at decreased niceness (higher priority):
       $ nice --15 important-command &
       $ nice -n -15 important-command &




                                                       55
Linux System Administration                                                Module 10. Modify Process Execution Priorities


10.3     renice
   s renice changes the niceness of existing processes
   s Non-root users are only permitted to increase a process’s niceness
   s To set the process with pid 2984 to the maximum niceness (lower priority):
        $ renice 20 2984
         q The niceness is just a number: no extra - sign
   s To set the process with pid 3598 to a lower niceness (higher priority):
        $ renice -15 3598
   s You can also change the niceness of all a user’s processes:
        $ renice 15 -u mikeb




10.4     Exercises

   1.    a. Create the following shell script, called forever, in your home directory:
             #!/bin/sh
             while [ 1 ]; do
                echo hello... >/dev/null;
             done
             Make it executable and run it in the background as follows:
             $ chmod a+rx forever
             $ ./forever &
         b. Use ps -l to check the script’s nice level
         c. Run the script with nice and give it a niceness of 15. Try running it alongside a less nice version, and
            see what the difference is in top
         d. Try using nice or renice to make a process’ niceness less than 0




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                        56
Module 11


Advanced Shell Usage

11.1    More About Quoting
  s The shell actually has three different quoting mechanisms:
        q Single quotes
        q Backslashes
        q Double quotes


11.2    Quoting: Single Quotes
  s Putting single quotes round something protects it from special interpretation by the shell:
       $ xmms ’Tom Lehrer - Poisoning Pigeons in the Park.mp3’
       $ rm ’b*lls and whistles’
  s But single quotes (obviously) don’t protect single quotes themselves
        q So you can’t quote something like She said, "Don’t go." with only single quotes


11.3    Quoting: Backslashes
  s You can put a backslash  in front of any single character to turn off its special meaning:
       $ echo M&S
       $ xmms Suzanne Vega - Tom’s Diner.mp3
       $ mail -s C:MSDOS.SYS windows-user@example.com




                                                       57
Linux System Administration                                                      Module 11. Advanced Shell Usage


11.4     Quoting: Double Quotes
   s Putting double quotes round something protects most things within it from interpretation by the
       shell
         q A dollar sign $ retains its special interpretation
         q As do backticks “
         q ! can’t be escaped in double quotes
   s A backslash can be used within double quotes to selectively disable the special interpretation
     of $, ‘ and :
       $ mail -s "C:MSDOS.SYS" windows-user@example.com
       $ echo "It cost $price US$"
   s Putting a backslash in front of anything else gives you both characters:
       $ echo "*/"
       */


11.5     Quoting: Combining Quoting Mechanisms
   s You can build up an argument for a command by combining several chunks of
       differently-quoted text
   s Just put the chunks next to each other with no intervening whitespace:
       $ echo "double-quoted"’.single-quoted.’unquoted
       double-quoted.single-quoted.unquoted
       $ echo ’She said, "Don’’’t go."’
       She said, "Don’t go."
   s Rarely needed — the last example is probably better written as:
       $ echo "She said, "Don’t go.""


11.6     Recap: Specifying Files with Wildcards
   s * in a glob pattern can stand for any sequence of characters:
       $ ls -l *.txt
       -rw-rw-r--    1 fred            users           108 Nov 16 13:06 report.txt
       -rw-rw-r--    1 fred            users           345 Jan 18 08:56 notes.txt
   s * on its own expands to all files in the current directory
   s Glob expansion is done by the shell
         q So a program can’t tell when the user ran it with a glob as an argument




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                               58
Linux System Administration                                                         Module 11. Advanced Shell Usage


11.7     Globbing Files Within Directories
   s You can use globs to get filenames within directories:
       $ ls Accounts/199*.txt
       Accounts/1997.txt Accounts/1998.txt Accounts/1999.txt
       $ ls ../images/*.gif
       ../images/logo.gif ../images/emblem.gif
   s You can also use globs to expand names of intervening directories:
       $ cd /usr/man && ls man*/lp*
       man1/lpq.1.gz man1/lprm.1.gz                  man4/lp.4.gz     man8/lpd.8.gz
       man1/lpr.1.gz man1/lptest.1.gz                man8/lpc.8.gz


11.8     Globbing to Match a Single Character
   s * matches any sequence of characters
   s To match any single character, use ?:
       $ ls ?ouse.txt
       Matches mouse.txt and house.txt, but not grouse.txt
   s Can be useful for making sure that you only match files of at least a certain length:
       $ rm ???*.txt
       Matches any file ending in .txt that has at least three characters before the dot



11.9 Globbing to Match Certain Characters
   s Instead of matching any single character, we can arrange to match any of a given group of
       characters
   s *.[ch] matches any file ending in .c or .h
   s *[0-9].txt matches any text file with a digit before the dot
   s You can use a caret as the first thing in the brackets to match any character that isn’t one of the
       listed ones
   s [ˆa-z]*.jpg matches any JPEG file that doesn’t begin with a lower-case letter
   s To match any hidden file except the . and .. directories: .[ˆ.]*




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                  59
Linux System Administration                                                              Module 11. Advanced Shell Usage


11.10       Generating Filenames: {}
   s You can use braces {} to generate filenames:
        $ mkdir -p Accounts/200{1,2}
        $ mkdir Accounts/200{1,2}/{0{1,2,3,4,5,6,7,8,9},1{0,1,2}}
   s You could even combine those two lines:
        $ mkdir -p Accounts/200{1,2}/{0{1,2,3,4,5,6,7,8,9},1{0,1,2}}
   s Or combine brace expansion with quoting:
        $ echo ’Hello ’{world,Mum}!
        Hello world! Hello Mum!
   s Braces can be used for generating any strings, not just filenames
   s Distinctly different from ordinary glob expansion — the words generated don’t need to be
        names of existing files or directories



11.11       Shell Programming
   s The shell is designed to be both:
          q A convenient environment to type commands into
          q A simple programming language
   s Any command that can be typed at the command line can be put into a file — and vice versa
   s Programming features include variables, loops (including for), and even shell functions
   s The Unix component approach makes it very easy to write shell scripts to perform fairly
        complex tasks
   s Common application domains for shell scripting include:
          q Text processing
          q Automation of system administration tasks




11.12       Exercises

   1.     a. Print out the following message: *** SALE $$$ ***.
          b. Try escaping the same string using single quotes, double quotes and backslashes.
          c. Echo the message ‘quoting isn’t simple’, escaping the spaces by putting single quotes around it.
          d. Use the glob pattern .[ˆ.]* to list all the hidden files in your home directory.
          e. To find out what shells are available on your system, list the programs in /bin whose names end in sh.
           f. Use [] brackets to list all the files in /usr/bin with names starting with a, b or c.




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                       60
Module 12


Filesystem Concepts

12.1     Filesystems
  s Some confusion surrounds the use of the term ‘filesystem’
  s Commonly used to refer to two distinct concepts
         1. The hierarchy of directories and files which humans use to organise data on a system
            (‘unified filesystem’)
         2. The formatting system which the kernel uses to store blocks of data on physical media
            such as disks (‘filesystem types’)



12.2     The Unified Filesystem
  s Unix and Linux systems have a unified filesystem
         q Any file, on any disk drive or network share, can be accessed through a name beginning
            with /
  s The unified filesystem is made up of one or more individual filesystems (‘branches’ of the
       unified hierarchy)
         q Each individual filesystem has its own root
         q That root can be grafted onto any directory in the unified filesystem
         q The directory where an individual filesystem is grafted into the unified filesystem is the
            individual filesystem’s mount point
  s An individual filesystem lives on a physical device (such as a disk drive), though not
       necessarily on the same computer




                                                        61
Linux System Administration                                                           Module 12. Filesystem Concepts


12.3     File Types
   s Files directly contain data
   s Directories provide a hierarchy of files: they can contain both files and other directories
   s Files and directories are both file types
   s Other file types exist, including device special files:
         q Device files provide a way of asking the kernel for access to a given physical device
         q The data that the device file seems to contain is actually the raw sequence of bytes or
             sectors on the device itself
         q Device files are by convention stored under the /dev directory


12.4     Inodes and Directories
   s An inode is the data structure that describes a file on an individual filesystem
   s It contains information about the file, including its type (file/directory/device), size, modification
       time, permissions, etc.
   s You can regard an inode as being the file itself
   s The inodes within an individual filesystem are numbered
         q An inode number is sometimes called an ‘inum’
   s Note that a file’s name is stored not in its inode, but in a directory
         q A directory is stored on disk as a list of file and directory names
         q Each name has an inode number associated with it
         q Separating names from inodes means that you can have multiple directory entries
             referring to the same file




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                   62
Module 13


Create and Change Hard and Symbolic Links

13.1     Symbolic Links
  s A symbolic link (or symlink) is a pseudo-file which behaves as an alternative name for some
       other file or directory
  s The ‘contents’ of the symlink are the real name pointed to
  s When you try to use a file name including a symlink, the kernel replaces the symlink
       component with its ‘contents’ and starts again
  s Symlinks allow you to keep a file (or directory) in one place, but pretend it lives in another
         q For example, to ensure that an obsolete name continues to work for older software
         q Or to spread data from a single filesystem hierarchy over multiple disk partitions


13.2 Examining and Creating Symbolic Links
  s ls -l shows where a symbolic link points to:
       $ ls -l /usr/tmp
       lrwxrwxrwx 1 root root 30 Sep 26             2000 /usr/tmp -> /var/tmp
  s ls can also be made to list symlinks in a different colour to other files, or to suffix their names
    with ‘@’
  s A symlink is created with the ln -s command
  s Its syntax is similar to cp — the original name comes first, then the name you want to create:
       $ ln -s real-file file-link
       $ ln -s real-dir dir-link
       $ ls -l file-link dir-link
       lrwxrwxrwx 1 bob bob     9 Jan 11 15:22 file-link -> real-file
       lrwxrwxrwx 1 bob bob     8 Jan 11 15:22 dir-link -> real-dir




                                                        63
Linux System Administration                                        Module 13. Create and Change Hard and Symbolic Links


13.3     Hard Links
   s Where symlinks refer to other files by name, a hard link refers to another file by inode number
         q An inode is the data structure that describes a file on disk
         q It contains information about the file, including its type (file/directory/device), modification
             time, permissions, etc.
   s A directory entry contains a name and an inode number
         q So a file’s name is not considered to be part of the file itself
   s You get a hard link when different directory entries on a filesystem refer to the same inode
       number



13.4     Symlinks and Hard Links Illustrated
   s A symbolic link refers to filename, which in turn refers to an inode:

                           directory                                        inode
                             entry

                                                                            data
                           directory
                             entry


   s A hard link is a normal directory entry, referring directly to an inode:

                           directory
                             entry                                          inode


                           directory                                        data
                             entry



13.5 Comparing Symlinks and Hard Links
   Symlinks                                       Hard links
   Symlinks are distinctly different from         Multiple hard-link style names for the
   normal files, so we can distinguish a           same file are indistinguishable; the term
   symlink from the original it points to         ‘hard link’ is merely conventional
   Symlinks can point to any type of file          Hard links may not point to a directory
   (normal file, directory, device file, symlink,   (or, on some non-Linux systems, to a
   etc.)                                          symlink)
   Symlinks refer to names, so they can           Hard links work by inode number, so they
   point to files on other filesystems              can only work within a single filesystem
   Conversely, if you rename or delete the        Renaming or deleting the ‘original’ file
   original file pointed to by a symlink, the      pointed to by a hard link has no effect on
   symlink gets broken                            the hard link
   Symlinks may take up additional disk           Hard links only need as much disk space
   space (to store the name pointed to)           as a directory entry


Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                      64
Linux System Administration                                      Module 13. Create and Change Hard and Symbolic Links


13.6     Examining and Creating Hard Links
   s Use the ln command to create a hard link
   s Don’t use the -s option when creating hard links
   s As when creating symlinks, the order of the arguments to ln mimics cp:
       $ ls -l *.dtd
       -rw-r--r--    1       anna   anna           11170 Dec     9 14:11 module.dtd
       $ ln module.dtd       chapter.dtd
       $ ls -l *.dtd
       -rw-r--r--    2       anna      anna        11170 Dec     9 14:11 chapter.dtd
       -rw-r--r--    2       anna      anna        11170 Dec     9 14:11 module.dtd
   s Notice that the link count in the listing increases to 2
   s The two names are now indistinguishable
         q Deleting or renaming one doesn’t affect the other


13.7     Preserving Links
   s Commands that operate on files often take options to specify whether links are followed
   s The tar command notices when two files it’s archiving are hard links to each other, and stores
       that fact correctly
   s By default tar also stores symlinks in archives
         q Use the -h option (--dereference) to instead store the file pointed to
   s The cp command by default ignores both hard links and symlinks
         q Use the -d option (--no-dereference) to preserve all links
         q Use the -R option (--recursive) when copying recursively to ensure that symlinks are
               preserved
         q The -a option (--archive) implies both -d and -R


13.8     Finding Symbolic Links to a File
   s The find command has a -lname option which searches for symbolic links containing some
       text:
       $ find / -lname ’*file’ -printf ’%p -> %ln’
   s This command prints the names and destinations of all symbolic links whose destination ends
     in file
   s Be aware that running find over the entire filesystem is very disk-intensive!




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                    65
Linux System Administration                                       Module 13. Create and Change Hard and Symbolic Links


13.9      Finding Hard Links to a File
   s Hard links can be found by searching for directory entries with a given inode number
   s First, identify the filesystem and inode number of the file you’re interested in:
        $ df module.dtd
        Filesystem         1k-blocks                  Used Available Use% Mounted on
        /dev/sdb3           13647416               5241196   7712972 40% /home
        $ ls -i module.dtd
         245713 module.dtd
   s Then use find’s -inum option to look for directory entries in that filesystem with that inode
        number:
        $ find /home -xdev -inum 245713
   s The -xdev option prevents find from recursing down into other filesystems




13.10      Exercises

   1.     a. Make a temporary directory and change into it.
         b. Make some test files as follows:
             $ echo "oranges and lemons" > fruit
             $ echo spuds > veg
          c. Make a symbolic link called starch to the veg file.
         d. Make a hard link called citrus to the appropriate file, and check that it has the same inode number.
         e. Delete the original fruit file and check that citrus still contains the text.
          f. Delete the original veg file and try to look at the contents of starch. Use ls to check the symlink.
   2.     a. Try to see what the following loop does, and then create some .htm files and try it:
             $ for htm in *.htm; do
             >    ln -s $htm ${htm}l;
             > done
         b. Make a symlink called dir to a directory (such as /etc).
          c. Try the following commands to display the link and compare the results:
             $ ls -l dir
             $ ls -l dir/




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                     66
Module 14


Manage File Ownership

14.1     Users and Groups
  s Anyone using a Linux computer is a user
  s The system keeps track of different users, by username
         q Security features allow different users to have different privileges
  s Users can belong to groups, allowing security to be managed for collections of people with
       different requirements
  s Use su to switch to a different user
         q Quicker than logging off and back on again
  s su prompts you for the user’s password:
       $ su - bob
       Password:
       The - option makes su behave as if you’ve logged in as that user



14.2 The Superuser: Root
  s Every Linux system has a user called ‘root’
  s The root user is all-powerful
         q Can access any files
  s The root user account should only be used for system administration, such as installing
       software
  s When logged in as root, the shell prompt usually ends in #
  s Usually best to use su for working as root:
       $ whoami
       fred
       $ su -
       Password:
       # whoami
       root


                                                         67
Linux System Administration                                                  Module 14. Manage File Ownership


14.3     Changing File Ownership with chown
   s The chown command changes the ownership of files or directories
   s Simple usage:
       # chown aaronc logfile.txt
   s Makes logfile.txt be owned by the user aaronc
   s Specify any number of files or directories
   s Only the superuser can change the ownership of a file
         q This is a security feature — quotas, set-uid


14.4     Changing File Group Ownership with chgrp
   s The chgrp command changes the group ownership of files or directories
   s Simple usage:
       # chgrp staff report.txt
   s Makes staff be the group owner of the file logfile.txt
   s As for chown, specify any number of files or directories
   s The superuser may change the group ownership of any file to any group
   s The owner of a file may change its group ownership
         q But only to a group of which the owner is a member


14.5 Changing the Ownership of a Directory and Its Contents
   s A common requirement is to change the ownership of a directory and its contents
   s Both chown and chgrp accept a -R option:
       # chgrp -R staff shared-directory
   s Mnemonic: ‘recursive’
   s Changes the group ownership of shared-directory to staff
         q And its contents
         q And its subdirectories, recursively
   s Changing user ownership (superuser only):
       # chown -R root /usr/local/share/misc/




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                            68
Linux System Administration                                                    Module 14. Manage File Ownership


14.6     Changing Ownership and Group Ownership Simultaneously
   s The chown command can change the user-owner and group-owner of a file simultaneously:
        # chown aaronc:www-docs public_html/interesting.html
   s Changes the user owner to aaronc and the group owner to www-docs
   s Can use the -R option as normal
   s A dot (.) may be used instead of a colon:
        # chown -R aaronc.www-docs /www/intranet/people/aaronc/




14.7     Exercises

   1.    a. Find out who owns the file /bin/ls and who owns your home directory (in /home).
         b. Log on as root, and create an empty file with touch. The user and group owners should be ‘root’ —
            check with ls.
         c. Change the owner of the file to be ‘users’.
         d. Change the group owner to be any non-root user.
         e. Change both of the owners back to being ‘root’ with a single command.




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                              69
Module 15


Use File Permissions to Control Access to
Files

15.1     Basic Concepts: Permissions on Files
  s Three types of permissions on files, each denoted by a letter
  s A permission represents an action that can be done on the file:
          Permission      Letter   Description
          Read            r        Permission to read the data stored in the file
          Write           w        Permission to write new data to the file, to truncate
                                   the file, or to overwrite existing data
          Execute         x        Permission to attempt to execute the contents of the
                                   file as a program

  s Occasionally referred to as ‘permission bits’
  s Note that for scripts, you need both execute permission and read permission
         q The script interpreter (which runs with your permissions) needs to be able to read the
            script from the file



15.2 Basic Concepts: Permissions on Directories
  s The r, w, x permissions also have a meaning for directories
  s The meanings for directories are slightly different:
          Permission      Letter   Description
          Read            r        Permission to get a listing of the directory
          Write           w        Permission to create, delete, or rename files (or
                                   subdirectories) within the directory
          Execute         x        Permission to change to the directory, or to use the
                                   directory as an intermediate part of a path to a file

  s The difference between read and execute on directories is specious — having one but not the
       other is almost never what you want




                                                           70
Linux System Administration                                      Module 15. Use File Permissions to Control Access to Files


15.3     Basic Concepts: Permissions for Different Groups of People
   s As well as having different types of permission, we can apply different sets of permissions to
       different sets of people
   s A file (or directory) has an owner and a group owner
   s The r, w, x permissions are specified separately for the owner, for the group owner, and for
       everyone else (the ‘world’)



15.4 Examining Permissions: ls -l
   s The ls -l command allows you to look at the permissions on a file:
       $ ls -l
       drwxr-x---         9 aaronc        staff          4096 Oct 12 12:57 accounts
       -rw-rw-r--         1 aaronc        staff         11170 Dec 9 14:11 report.txt
   s The third and fourth columns are the owner and group-owner
   s The first column is the permissions:
         q One character for the file type: d for directories, - for plain files
         q Three characters of rwx permissions for the owner (or a dash if the permission isn’t
             available)
         q Three characters of rwx permissions for the group owner
         q Three characters of rwx permissions for everyone else


15.5     Preserving Permissions When Copying Files
   s By default, the cp command makes no attempt to preserve permissions (and other attributes
       like timestamps)
   s You can use the -p option to preserve permissions and timestamps:
       $ cp -p important.txt important.txt.orig
   s Alternatively, the -a option preserves all information possible, including permissions and
       timestamps



15.6     How Permissions are Applied
   s If you own a file, the per-owner permissions apply to you
   s Otherwise, if you are in the group that group-owns the file, the per-group permissions apply to
       you
   s If neither of those is the case, the for-everyone-else permissions apply to you




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                          71
Linux System Administration                                      Module 15. Use File Permissions to Control Access to Files


15.7     Changing File and Directory Permissions: chmod
   s The chmod command changes the permissions of a file or directory
         q A file’s permissions may be changed only by its owner or by the superuser
   s chmod takes an argument describing the new permissions
         q Can be specified in many flexible (but correspondingly complex) ways
   s Simple example:
       $ chmod a+x new-program
       adds (+) executable permission (x) for all users (a) on the file new-program



15.8 Specifying Permissions for chmod
   s Permissions can be set using letters in the following format:
       [ugoa][+=-][rwxX]
   s The first letters indicate who to set permissions for:
         q u for the file’s owner, g for the group owner, o for other users, or a for all users
   s = sets permissions for files, + adds permissions to those already set, and - removes
       permissions
   s The final letters indicate which of the r, w, x permissions to set
         q Or use capital X to set the x permission, but only for directories and already-executable
             files



15.9     Changing the Permissions of a Directory and Its Contents
   s A common requirement is to change the permissions of a directory and its contents
   s chmod accepts a -R option:
       $ chmod -R g+rwX,o+rX public-directory
   s Mnemonic: ‘recursive’
   s Adds rwx permissions on public-directory for the group owner, and adds rx permissions on it
       for everyone else
         q And any subdirectories, recursively
         q Any any contained executable files
         q Contained non-executable files have rw permissions added for the group owner, and
           r permission for everyone else




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                          72
Linux System Administration                                      Module 15. Use File Permissions to Control Access to Files


15.10      Special Directory Permissions: ‘Sticky’
   s The /tmp directory must be world-writable, so that anyone may create temporary files within it
   s But that would normally mean that anyone may delete any files within it — obviously a security
      hole
   s A directory may have ‘sticky’ permission:
         q Only a file’s owner may delete it from a sticky directory
   s Expressed with a t (mnemonic: temporary directory) in a listing:
      $ ls -l -d /tmp
      drwxrwxrwt   30 root                root          11264 Dec 21 09:35 /tmp
   s Enable ‘sticky’ permission with:
      # chmod +t /data/tmp


15.11      Special Directory Permissions: Setgid
   s If a directory is setgid (‘set group-id’), files created within it acquire the group ownership of the
      directory
         q And directories created within it acquire both the group ownership and setgid permission
   s Useful for a shared directory where all users working on its files are in a given group
   s Expressed with an s in ‘group’ position in a listing:
      $ ls -l -d /data/projects
      drwxrwsr-x   16 root   staff                  4096 Oct 19 13:14 /data/projects
   s Enable setgid with:
      # chmod g+s /data/projects


15.12      Special File Permissions: Setgid
   s Setgid permission may also be applied to executable files
   s A process run from a setgid file acquires the group id of the file
   s Note: Linux doesn’t directly allow scripts to be setgid — only compiled programs
   s Useful if you want a program to be able to (for example) edit some files that have a given group
      owner
         q Without letting individual users access those files directly




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                          73
Linux System Administration                                          Module 15. Use File Permissions to Control Access to Files


15.13      Special File Permissions: Setuid
   s Files may also have a setuid (‘set user-id’) permission
   s Equivalent to setgid: a process run from a setuid file acquires the user id of the file
   s As with setgid, Linux doesn’t allow scripts to be setuid
   s Expressed with an s in ‘user’ position in a listing:
      $ ls -l /usr/bin/passwd
      -r-s--x--x   1 root   root                12244 Feb        7     2000 /usr/bin/passwd
   s Enable setuid with:
      # chmod u+s /usr/local/bin/program


15.14      Displaying Unusual Permissions
   s Use ls -l to display file permissions
         q Setuid and Setgid permissions are shown by an s in the user and group execute positions
         q The sticky bit is shown by a t in the ‘other’ execute position
   s The letters s and t cover up the execute bits
         q But you can still tell whether the execute bits are set
         q Lowercase s or t indicates that execute is enabled (i.e., there is an x behind the letter)
         q Uppercase S or T indicates that execute is disabled (there is a - behind the letter)


15.15      Permissions as Numbers
   s Sometimes you will find numbers referring to sets of permissions
   s Calculate the number by adding one or more of the following together:
          4000      Setuid                     40     Readable by group
                                                      owner
          2000      Setgid                     20     Writable by group
                                                      owner
          1000      ‘Sticky’                   10     Executable by group
                                                      owner
           400      Readable by owner           4     Readable by anyone
           200      Writable by owner           2     Writable by anyone
           100      Executable by owner         1     Executable by
                                                      anyone

   s You may use numerical permissions with chmod:
      $ chmod 664 *.txt
      is equivalent to:
      $ chmod ug=rw,o=r *.txt




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                              74
Linux System Administration                                      Module 15. Use File Permissions to Control Access to Files


15.16       Default Permissions: umask
   s The umask command allows you to affect the default permissions on files and directories you
        create:
        $ umask 002
   s The argument is calculated by adding together the numeric values for the rwx permissions you
        don’t want on new files and directories
          q This example has just 2 — avoid world-writable, but turn everything else on
   s Other common umask values:
          q 022 — avoid world- and group-writable, allow everything else
          q 027 — avoid group-writable, and allow no permissions for anyone else
   s You normally want to put a call to umask in your shell’s startup file




15.17       Exercises

   1.     a. Find out what permissions are set on your home directory (as a normal user). Can other users access
             files inside it?
          b. If your home directory is only accessible to you, then change the permissions to allow other people to
             read files inside it, otherwise change it so that they can’t.
          c. Check the permissions on /bin and /bin/ls and satisfy yourself that they are reasonable.
          d. Check the permissions available on /etc/passwd and /etc/shadow.
          e. Write one command which would allow people to browse through your home directory and any subdi-
             rectories inside it and read all the files.




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                          75
Module 16


Create Partitions and Filesystems

16.1   Concepts: Disks and Partitions
  s A hard disk provides a single large storage space
  s Usually split into partitions
       q Information about partitions is stored in the partition table
       q Linux defaults to using partition tables compatible with Microsoft Windows
       q For compatibility with Windows, at most four primary partitions can be made
       q But they can be extended partitions, which can themselves be split into smaller logical
          partitions
            s   Extended partitions have their own partition table to store information about logical
                partitions



16.2   Disk Naming
  s The device files for IDE hard drives are /dev/hda to /dev/hdd
       q hda and hdb are the drives on the first IDE channel, hdc and hdd the ones on the second
          channel
       q The first drive on each channel is the IDE ‘master’, and the second is the IDE ‘slave’
  s Primary partitions are numbered from 1–4
  s Logical partitions are numbered from 5
  s The devices /dev/hda, etc., refer to whole hard disks, not partitions
       q Add the partition number to refer to a specific partition
       q For example, /dev/hda1 is the first partition on the first IDE disk
  s SCSI disks are named /dev/sda, /dev/sdb, etc




                                                        76
Linux System Administration                                                Module 16. Create Partitions and Filesystems


16.3     Using fdisk
   s The fdisk command is used to create, delete and change the partitions on a disk
   s Give fdisk the name of the disk to edit, for example:
       # fdisk /dev/hda
   s fdisk reads one-letter commands from the user
         q Type m to get a list of commands
         q Use p to show what partitions currently exist
         q Use q to quit without altering anything
         q Use w to quit and write the changes
               s   Use with caution, and triple-check what you’re doing!



16.4     Making New Partitions
   s Create new partitions with the n command
         q Choose whether to make a primary, extended or logical partition
         q Choose which number to assign it
   s fdisk asks where to put the start and end of the partition
         q The default values make the partition as big as possible
         q The desired size can be specified in megabytes, e.g., +250M
   s Changes to the partition table are only written when the w command is given


16.5     Changing Partition Types
   s Each partition has a type code, which is a number
   s The fdisk command l shows a list of known types
   s The command t changes the type of an existing partition
         q Enter the type code at the prompt
   s Linux partitions are usually of type ‘Linux native’ (type 83)
   s Other operating systems might use other types of partition, many of which can be understood
       by Linux




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                      77
Linux System Administration                                                Module 16. Create Partitions and Filesystems


16.6     Making Filesystems with mkfs
   s The mkfs command initializes a filesystem on a new partition
         q Warning: any old data on the partition will be lost
         q For example, to make an ext2 filesystem on /dev/hda2:
             # mkfs -t ext2 -c /dev/hda2
         q -t sets the filesystem type to make, and -c checks for bad blocks on the disk
   s mkfs uses other programs to make specific types of filesystem, such as mke2fs and mkdosfs


16.7 Useful Websites
   s Tutorial on making partitions —
       http://www.linuxnewbie.org/nhf/intel/installation/fdisk_nhf/Fdisk.html
   s Linux Partition HOWTO — http://www.linuxdoc.org/HOWTO/mini/Partition/
   s Table of fdisk commands and partition types —
       http://wwwinfo.cern.ch/pdp/as/linux/fdisk/index.html




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                      78
Module 17


Control Filesystem Mounting and Unmounting

17.1     Mounting Filesystems
  s As far as many parts of a Linux system are concerned, a partition contains entirely arbitrary
       data
  s When installing, you set things up so that a partition contains a filesystem — a way of
       organising data into files and directories
  s One filesystem is made the root filesystem: the root directory on that filesystem becomes the
       directory named /
  s Other filesystems can be mounted: the root directory of that filesystem is grafted onto a
       directory of the root filesystem
         q This arranges for every file in every mounted filesystem to be accessible from a single
              unified name space
  s The directory grafted onto is called the mount point


17.2     Mounting a Filesystem: mount
  s ‘Important’ filesystems are mounted at boot-up; other filesystems can be mounted or
       unmounted at any time
  s The mount command mounts a filesystem
         q You usually need to have root permission to mount a filesystem
  s mount makes it easy to mount filesystems configured by the system administrator
  s For example, many systems are configured so that
       $ mount /mnt/cdrom
       will mount the contents of the machine’s CD-ROM drive under the directory /mnt/cdrom




                                                       79
Linux System Administration                                      Module 17. Control Filesystem Mounting and Unmounting


17.3     Mounting Other Filesystems
   s mount /dev/sdb3 /mnt/extra mounts the filesystem stored in the /dev/sdb3 device on the
       mount point /mnt/extra
   s You may occasionally need to specify the filesystem type explicitly:
       # mount -t vfat /dev/hdd1 /mnt/windows
         q Allowable filesystem types are listed in the mount(8) manpage
   s To see a list of the filesystems currently mounted, run mount without any options


17.4     Unmounting a Filesystem: umount
   s A filesystem can be unmounted with umount
         q Note the spelling!
   s umount /mnt/extra unmounts whatever is on the /mnt/extra mount point
   s umount /dev/sdb3 unmounts the filesystem in the /dev/sdb3 device, wherever it is mounted
   s You normally need to have root permission to unmount a filesystem
   s It’s also impossible to unmount a ‘busy’ filesystem
         q A filesystem is busy if a process has a file on it open
         q Or if a process has a directory within it as its current directory


17.5 Configuring mount: /etc/fstab
   s The /etc/fstab file contains information about filesystems that are known to the system
       administrator
         q Specifying a filesystem in /etc/fstab makes it possible to use its mount point as the only
           argument to mount
   s /etc/fstab also configures which filesystems should be mounted at boot-up
   s Each line in /etc/fstab describes one filesystem
   s Six columns on each line


17.6     Sample /etc/fstab
   s A sample /etc/fstab file:
       # device        mount-point       type       options        (dump)       pass-no
       /dev/hda3       /                 ext2       defaults            1             1
       /dev/hda1       /boot             ext2       defaults            1             2
       /dev/hda5       /usr              ext2       defaults            1             2
       /dev/hdb1       /usr/local        ext2       defaults            1             2
       /dev/hdb2       /home             ext2       defaults            1             2
       none            /proc             proc       defaults            0             0
       /dev/scd0       /mnt/cdrom        iso9660    noauto,users,ro     0             0
       /dev/fd0        /mnt/floppy       auto       noauto,users        0             0

Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                     80
Linux System Administration                                       Module 17. Control Filesystem Mounting and Unmounting


17.7     Filesystem Types
   s The most common filesystem types are:
          Type          Usage
          ext2          The standard Linux filesystem
          iso9660       The filesystem used on CD-ROMs
          proc          Not a real filesystem, so uses none as the device. Used as a way
                        for the kernel to report system information to user processes
          vfat          The filesystem used by Windows 95
          auto          Not a real filesystem type. Used as a way of asking the mount
                        command to probe for various filesystem types, particularly for
                        removable media
   s Networked filesystems include nfs (Unix-specific) and smbfs (Windows or Samba)
   s Other, less common types exist; see mount(8)


17.8     Mount Options
   s Comma-separated options in /etc/fstab
   s Alternatively, use comma-separated options with -o on the mount command line
   s Common mount options:
          Option       Description
          noauto       In /etc/fstab, prevents the filesystem being mounted at bootup.
                       Useful for removable media
          ro           Mount the filesystem read-only
          users        Let non-root users mount and unmount this filesystem
          user         Like users, but non-root users can only unmount filesystems that
                       they themselves mounted

   s Other less common mount options exist, as well as many options for individual filesystem types
     — see mount(8)



17.9 Other Columns in /etc/fstab
   s The fifth column is called dump
         q Used by the dump and restore backup utilities
         q Few people use those tools
         q Just use 1 for normal filesystems, and 0 for removable filesystems
   s The sixth column is called pass-no
         q Controls the order in which automatically-mounted filesystems are checked by fsck
         q Use 1 for the root filesystem
         q Use 0 for filesystems that aren’t mounted at boot-up
         q Use 2 for other filesystems




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                      81
Linux System Administration                                      Module 17. Control Filesystem Mounting and Unmounting


17.10      Mounting a File
   s Using loop devices, Linux can mount a filesystem stored in a normal file, instead of a disk
   s Useful for testing images of CD-ROMs before burning them to disk
   s For example, to create a filesystem of roughly floppy-disk size:
        # dd if=/dev/zero of=disk.img bs=1024 count=1400
        # mke2fs -F disk.img
   s To mount the file so that its contents is accessible through /mnt/disk :
        # mount -o loop disk.img /mnt/disk




17.11      Exercises

   1.    a. Use mount to find out which filesystems are mounted.
         b. Check the /etc/fstab file to see whether the floppy drive is configured properly, and find out what its
            mount point is set to.
         c. Mount a floppy disk at the default mount point.
         d. Copy a file onto the floppy disk. Does Linux write it immediately? Unmount the floppy to ensure that
            everything on it is properly written, and it is safe to remove.
         e. Try the commands on the last slide to mount a file, and try copying some files into it. Try using the df
            command to see how much space is available in the file. Unmount /mnt/disk as you would any other
            filesystem.




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                     82
Module 18


Maintain the Integrity of Filesystems

18.1   Filesystem Concepts
  s The files stored on a disk partition are organised into a filesystem
  s There are several filesystem types; the common Linux one is called ext2
  s A filesystem contains a fixed number of inodes
       q An inode is the data structure that describes a file on disk
       q It contains information about the file, including its type (file/directory/device), modification
          time, permissions, etc.
  s A file name refers to an inode, not to the file directly
       q This allows hard links: many file names referring to the same inode


18.2   Potential Problems
  s Over time, an active filesystem can develop problems:
       q It can fill up, causing individual programs or even the entire system to fail
       q It can become corrupted, perhaps due to a power failure or a system crash
       q It can run out of space for inodes, so no new files or directories can be created
  s Monitoring and checking filesystems regularly can help prevent and correct problems like these




                                                        83
Linux System Administration                                          Module 18. Maintain the Integrity of Filesystems


18.3     Monitoring Space: df
   s Run df with no arguments to get a listing of free space on all mounted filesystems
   s Usually better to use the -h option, which displays space in human-readable units:
       $ df -h
       Filesystem                     Size    Used Avail Use% Mounted on
       /dev/hda8                      248M     52M 183M 22% /
       /dev/hda1                       15M    5.6M 9.1M 38% /boot
       /dev/hda6                       13G    5.0G 7.4G 41% /home
       /dev/hda5                       13G    4.6G 7.8G 37% /usr
       /dev/hda7                      248M    125M 110M 53% /var
   s The Use% column shows what percentage of the filesystem is in use
   s You can give df directories as extra arguments to make it show space on the filesystems those
       directories are mounted on



18.4     Monitoring Inodes: df
   s Filesystems rarely run out of inodes, but it would be possible if the filesystem contains many
       small files
   s Run df -i to get information on inode usage on all mounted filesystems:
       $ df -i
       Filesystem                    Inodes       IUsed   IFree IUse% Mounted on
       /dev/hda8                      65736        8411   57325   13% /
       /dev/hda1                       4160          30    4130    1% /boot
       /dev/hda6                    1733312      169727 1563585   10% /home
       /dev/hda5                    1733312      138626 1594686    8% /usr
       /dev/hda7                      65736        1324   64412    2% /var
   s In this example, every filesystem has used a smaller percentage of its inodes (IUse%) than of
       its file space
         q This is a good sign!




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                    84
Linux System Administration                                                     Module 18. Maintain the Integrity of Filesystems


18.5     Monitoring Disk Usage: du
   s df shows a summary of the free space on a partition
   s du, on the other hand, shows information about disk space used in a directory tree
   s Takes one or more directories on the command line:
        $ du /usr/share/vim
        2156    /usr/share/vim/vim58/doc
        2460    /usr/share/vim/vim58/syntax
        36      /usr/share/vim/vim58/tutor
        16      /usr/share/vim/vim58/macros/hanoi
        16      /usr/share/vim/vim58/macros/life
        40      /usr/share/vim/vim58/macros/maze
        20      /usr/share/vim/vim58/macros/urm
        156     /usr/share/vim/vim58/macros
        100     /usr/share/vim/vim58/tools
        5036    /usr/share/vim/vim58
        5040    /usr/share/vim


18.6     du Options
   Option       Description
   -a           Show all files, not just directories
   -c           Print a cumulative total for all directories named on the command line
   -h           Print disk usage in human-readable units
   -s           Print only a summary for each directory named on the command line
   -S           Make the size reported for a directory be the size of only the files in
                that directory, not the total including the sizes of its subdirectories



18.7     Finding and Repairing Filesystem Corruption: fsck
   s Sometimes filesystems do become corrupted
         q Perhaps there was a power failure
         q Or maybe your kernel version has a bug in it
   s The fsck program checks the integrity of a filesystem
         q And can make repairs if necessary
   s Actually has two main parts:
         q A ‘driver program’, fsck, which handles any filesystem type
         q One ‘backend program’ for each specific filesystem type
   s The backend program for ext2 is e2fsck, but it is always invoked through fsck




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                               85
Linux System Administration                                            Module 18. Maintain the Integrity of Filesystems


18.8     Running fsck
   s fsck is normally run at system startup
         q So it gets run automatically if the system was shut down uncleanly
   s It can also be run manually:
        # fsck /dev/sdb3
         q Interactively asks whether to fix problems as they are found
   s Use -f to force checking the filesystem, even if fsck thinks it was cleanly umounted
   s Use -y to automatically answer ‘yes’ to any question
   s Usually a bad idea to run fsck on a mounted filesystem!




18.9 Exercises

   1.    a. Check the free disk space on the computer.
         b. Display just the usage information for the partition that contains /usr/. Display this in human-readable
            units.
         c. Look at the free space and inodes of the partition of /var/tmp first. Then run these commands:
             $ mkdir /var/tmp/foo
             $ seq -f ’/var/tmp/foo/bar-%04.f’ 0 2000 | xargs touch
             What has happened? Look at the free space and inodes again.
             Remove the files when you have finished.
   2. Go into the /var/ directory. Run each of the following commands as root, and explain the difference in their
      output:
         a. # du
         b. # du -h
         c. # du -h *
         d. # du -hs
         e. # du -hs *
          f. # du -hsS *
         g. # du -hsc *
         h. # du -bsc *




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                      86
Module 19


Find System Files and Place Files in the
Correct Location

19.1   Unix Filesystem Layout
  s Many common directory names are abbreviated versions of real words
  s Traditional structure which has developed over many years
       q Most system files have their proper place
       q Programs rely on them being in the correct place
       q Users familiar with Unix directory structure can find their way around any Unix or Linux
          system
  s But a user’s home directory can be structured however they want


19.2   The Filesystem Hierarchy Standard
  s Started as an attempt to standardise Linux filesystem layouts
       q Called the FSSTND when the first version was published in 1994
  s Widely accepted by distributors
       q But few Linux systems are 100% compliant yet
  s Intended to avoid fragmentation of Linux distributions
  s Renamed to the File Hierarchy Standard, or FHS
  s Now intended to apply to all Unix-like operating systems




                                                      87
Linux System Administration                            Module 19. Find System Files and Place Files in the Correct Location


19.3     Shareable and Non-Shareable Data
   s Some files can be shared between multiple computers, using networked filesystems such as
       NFS
         q This can save space, although cheap hard drives mean that this is not so important now
         q More importantly, it can help to centralise administration for a network
   s Usually programs, email and home directories are all shareable
   s Log files and machine-specific configuration files are not shareable


19.4     Static and Dynamic Data
   s Some files hardly ever need to be changed, while others change all the time
   s It can help to store static files separately from those which regularly change:
         q The static files can be on a partition mounted read-only (such as a CD-ROM)
   s Programs and libraries are usually static (except when new software is installed)
   s Home directories and status files are usually more variable


19.5     Overview of the FHS
   bin
   boot
   dev               / contains essential files needed to boot the system
   etc
   lib
                     /usr contains other packaged software
   mnt
   opt
   sbin              /usr/local contains non−packaged software
   tmp
   usr
      bin
      include
      info
      lib
      local
         bin, sbin, and so on
      man
      sbin
      share
   var




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                          88
Linux System Administration                            Module 19. Find System Files and Place Files in the Correct Location


19.6     FHS: Installed Software
   s Programs are usually found in the bin and sbin directories
         q These are found in /, /usr and /usr/local
   s sbin is used for programs likely to be useful to system administrators rather than to general
       users (mail dæmon, web server, etc.)
   s These directories are named after binaries
         q Most programs in them are binaries (compiled programs), although some are
               human-readable scripts
   s Libraries are stored in directories called lib, found in the same places as bin
         q These directories should be listed in /etc/ld.so.conf


19.7     FHS: Other Directories Under /usr
   s /usr/include contains header files used by C/C++ programs
   s /usr/X11R6 contains files used by the X Window system, including programs, libraries,
       configuration files and documentation
   s /usr/local is where software is installed when it is compiled from source code rather than
       installed as a package
   s /usr/share contains files which are not specific to the architecture of a machine, e.g., fonts and
       icons
         q Theoretically could be shared between different types of machine over a network
   s /usr/src often contains the source code for the Linux kernel
         q Usually kept in a directory such as linux-2.2.20, with a symbolic link to it called linux


19.8     FHS: Directories Under /var
   s /var/run contains pid files (process-id files for currently-running dæmon programs)
         q Also contains utmp, a record of user logins
   s /var/mail or /var/spool/mail is where each user’s email is queued up until it is deleted or saved
   s /var/log contains logs produced by various programs, including syslog
   s /var/cache contains data generated by programs which is cached to save time
         q Cached data can be regenerated if deleted




Copyright © 2004 GBdirect Ltd. http://training.gbdirect.co.uk/                                                          89