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