Linux System Administration - Linux Professional Institute (
Linux System Administration - Linux Professional Institute (
Summary Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Work Effectively on the Unix Command Line . . . . . . . . . . . . . 4 Process Text Streams Using Text Processing Filters . . . . . . . . 5 Perform Basic File Management . . . . . . . . . . . . . . . . . . . . . . 6 Use Unix Streams, Pipes and Redirects . . . . . . . . . . . . . . . . . 7 Search Text Files Using Regular Expressions . . . . . . . . . . . . . 8 Job Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Create, Monitor, and Kill Processes . . . . . . . . . . . . . . . . . . . 10 Modify Process Execution Priorities . . . . . . . . . . . . . . . . . . . 11 Advanced Shell Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Filesystem Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Create and Change Hard and Symbolic Links . . . . . . . . . . . . . 14 Manage File Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Use File Permissions to Control Access to Files . . . . . . . . . . . 16 Create Partitions and Filesystems . . . . . . . . . . . . . . . . . . . . 17 Control Filesystem Mounting and Unmounting . . . . . . . . . . . .
1 9 17 26 37 45 51 55 58 64 67 72 75 80 84 91 95
Detailed Contents
1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.20 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 Introduction . . . . . . . . . . . . . . . . . Unix and Linux . . . . . . . . . . . . . . . . Unix System Architecture . . . . . . . . . . Unix Philosophy . . . . . . . . . . . . . . . What is Linux? . . . . . . . . . . . . . . . . Using a Linux System . . . . . . . . . . . . Linux Command Line . . . . . . . . . . . . Logging Out . . . . . . . . . . . . . . . . . Command Syntax . . . . . . . . . . . . . . Files . . . . . . . . . . . . . . . . . . . . . Creating Files with cat . . . . . . . . . . . Displaying Files Contents with cat . . . . . Deleting Files with rm . . . . . . . . . . . . Unix Command Feedback . . . . . . . . . . Copying and Renaming Files with cp and mv Filename Completion . . . . . . . . . . . . Command History . . . . . . . . . . . . . . Exercise 1 . . . . . . . . . . . . . . . . . . Exercise 2 . . . . . . . . . . . . . . . . . . Exercise 3 . . . . . . . . . . . . . . . . . . Exercise 4 . . . . . . . . . . . . . . . . . . Getting Started . . . . . . . . . . . Files and Directories . . . . . . . . . Examples of Absolute Paths . . . . . Current Directory . . . . . . . . . . Making and Deleting Directories . . Relative Paths . . . . . . . . . . . . Special Dot Directories . . . . . . . Hidden Files . . . . . . . . . . . . . Paths to Home Directories . . . . . . Looking for Files in the System . . . Running Programs . . . . . . . . . . Specifying Multiple Files . . . . . . . Finding Documentation for Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 9 10 10 10 11 11 11 12 12 12 13 13 13
ii
2.13 2.14 2.15 2.16 2.17 2.18 2.19 2.20 2.21 3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 3.20 3.21 3.22 3.23 4 4.1 4.2 4.3 4.4 4.5 4.6
Specifying Files with Wildcards Chaining Programs Together . Graphical and Text Interfaces . Text Editors . . . . . . . . . . Exercise 1 . . . . . . . . . . . Exercise 2 . . . . . . . . . . . Exercise 3 . . . . . . . . . . . Exercise 4 . . . . . . . . . . . Exercise 5 . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14 14 14 15 15 15 16 16 16 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 26 27 27 27 28 28 28
Work Effectively on the Unix Command Line Shells . . . . . . . . . . . . . . . . . . . . . . The Bash Shell . . . . . . . . . . . . . . . . . Navigating the Filesystem . . . . . . . . . . . . Command-Line Arguments . . . . . . . . . . . Syntax of Command-Line Options . . . . . . . Examples of Command-Line Options . . . . . . Where Programs are Found . . . . . . . . . . . Setting Shell Variables . . . . . . . . . . . . . . Bash Conguration Variables . . . . . . . . . . Using History . . . . . . . . . . . . . . . . . . Reusing History Items . . . . . . . . . . . . . . Retrieving Arguments from the History . . . . . Summary of Bash Editing Keys . . . . . . . . . Combining Commands on One Line . . . . . . Repeating Commands with for . . . . . . . . . Command Substitution . . . . . . . . . . . . . Finding Files with locate . . . . . . . . . . . . Finding Files More Flexibly: find . . . . . . . . find Criteria . . . . . . . . . . . . . . . . . . . find Actions: Executing Programs . . . . . . . Exercise 1 . . . . . . . . . . . . . . . . . . . . Exercise 2 . . . . . . . . . . . . . . . . . . . . Exercise 3 . . . . . . . . . . . . . . . . . . . .
Process Text Streams Using Text Processing Filters Working with Text Files . . . . . . . . . . . . . . . . . Lines of Text . . . . . . . . . . . . . . . . . . . . . . . Filtering Text and Piping . . . . . . . . . . . . . . . . . Displaying Files with less . . . . . . . . . . . . . . . . Concatenating Files with cat . . . . . . . . . . . . . . Counting Words and Lines with wc . . . . . . . . . . .
iii
4.7 4.8 4.9 4.10 4.11 4.12 4.13 4.14 4.15 4.16 4.17 4.18 4.19 4.20 4.21 4.22 4.23 4.24 4.25 4.26 4.27 4.28 5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11 5.12 5.13 5.14 5.15 5.16 5.17 5.18
Sorting Lines of Text with sort . . . . . Removing Duplicate Lines with uniq . . Selecting Parts of Lines with cut . . . . Expanding Tabs to Spaces with expand Using fmt to Format Text Files . . . . . Reading the Start of a File with head . . Reading the End of a File with tail . . Numbering Lines of a File with nl . . . . Dumping Bytes of Binary Data with od . Paginating Text Files with pr . . . . . . Dividing Files into Chunks with split . . Using split to Span Disks . . . . . . . tac: Backwards cat . . . . . . . . . . . Translating Sets of Characters with tr . tr Examples . . . . . . . . . . . . . . . sed the Stream Editor . . . . . . . . . Substituting with sed . . . . . . . . . . Put Files Side-by-Side with paste . . . Performing Database Joins with join . . Exercise 1 . . . . . . . . . . . . . . . . Exercise 2 . . . . . . . . . . . . . . . . Exercise 3 . . . . . . . . . . . . . . . . Perform Basic File Management Filesystem Objects . . . . . . . . Directory and File Names . . . . . File Extensions . . . . . . . . . . Visiting Directories with cd . . . . Going Back to Previous Directories Filename Completion . . . . . . . Wildcard Patterns . . . . . . . . . Copying Files with cp . . . . . . . Examples of cp . . . . . . . . . . Moving Files with mv . . . . . . . . Deleting Files with rm . . . . . . . Deleting Files with Peculiar Names Making Directories with mkdir . . Removing Directories with rmdir . Identifying Types of Files . . . . . Changing Timestamps with touch Exercise 1 . . . . . . . . . . . . . Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34 34 34 35 35 35 36 37 38 38 38 39 39 39 40 40 40 41 41 41 42 42 42 43 43 43
iv
5.19 Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.20 Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10 6.11 6.12 6.13 7 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 8 8.1 8.2 8.3 8.4 8.5 9 9.1 9.2 9.3 9.4 9.5 9.6 Use Unix Streams, Pipes and Redirects Standard Files . . . . . . . . . . . . . . . Standard Input . . . . . . . . . . . . . . . Standard Output . . . . . . . . . . . . . . Standard Error . . . . . . . . . . . . . . . Pipes . . . . . . . . . . . . . . . . . . . . Connecting Programs to Files . . . . . . . Appending to Files . . . . . . . . . . . . . Redirecting Multiple Files . . . . . . . . . Redirection with File Descriptors . . . . . Running Programs with xargs . . . . . . tee . . . . . . . . . . . . . . . . . . . . Exercise 1 . . . . . . . . . . . . . . . . . Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44 44 45 46 46 46 47 47 47 48 48 48 49 49 49 50 51 52 52 52 53 53 53 54 54 55 56 56 56 57 57 58 59 59 59 60 60 60
Search Text Files Using Regular Expressions Searching Files with grep . . . . . . . . . . . . Pattern Matching . . . . . . . . . . . . . . . . Matching Repeated Patterns . . . . . . . . . . Matching Alternative Patterns . . . . . . . . . . Extended Regular Expression Syntax . . . . . . sed . . . . . . . . . . . . . . . . . . . . . . . Further Reading . . . . . . . . . . . . . . . . . Exercise 1 . . . . . . . . . . . . . . . . . . . . Job Control Job Control jobs . . . . fg . . . . . bg . . . . . Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create, Monitor, and Kill Processes What is a Process? . . . . . . . . . . Process Properties . . . . . . . . . . Parent and Child Processes . . . . . . Process Monitoring: ps . . . . . . . . ps Options . . . . . . . . . . . . . . . Process Monitoring: pstree . . . . .
9.7 9.8 9.9 9.10 9.11 9.12 9.13 9.14 9.15 10 10.1 10.2 10.3 10.4 11 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.10 11.11 11.12 12 12.1 12.2 12.3 12.4 13 13.1 13.2 13.3 13.4 13.5
pstree Options . . . . . . . Process Monitoring: top . . top Command-Line Options top Interactive Commands .
. . . . . . . . . . . . . . . . Signalling Processes . . . . . . . . Common Signals for Interactive Use Sending Signals: kill . . . . . . . Sending Signals to Dmons: pidof Exercise 1 . . . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
61 61 61 62 62 62 63 63 63 64 65 65 65 66 67 68 68 68 69 69 69 70 70 70 71 71 71 72 73 73 73 74 75 76 76 76 77 77
Advanced Shell Usage . . . . . . . . . . More About Quoting . . . . . . . . . . . . Quoting: Single Quotes . . . . . . . . . . Quoting: Backslashes . . . . . . . . . . . Quoting: Double Quotes . . . . . . . . . . Quoting: Combining Quoting Mechanisms Recap: Specifying Files with Wildcards . . Globbing Files Within Directories . . . . . Globbing to Match a Single Character . . . Globbing to Match Certain Characters . . Generating Filenames: {} . . . . . . . . . Shell Programming . . . . . . . . . . . . Exercise 1 . . . . . . . . . . . . . . . . . Filesystem Concepts . Filesystems . . . . . . The Unied Filesystem File Types . . . . . . . Inodes and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create and Change Hard and Symbolic Links Symbolic Links . . . . . . . . . . . . . . . . . . Examining and Creating Symbolic Links . . . . Hard Links . . . . . . . . . . . . . . . . . . . . Symlinks and Hard Links Illustrated . . . . . . . Comparing Symlinks and Hard Links . . . . . .
vi
13.6 13.7 13.8 13.9 13.10 13.11 14 14.1 14.2 14.3 14.4 14.5 14.6 14.7 15 15.1 15.2 15.3 15.4 15.5 15.6 15.7 15.8 15.9 15.10 15.11 15.12 15.13 15.14 15.15 15.16 15.17 16 16.1 16.2 16.3 16.4 16.5 16.6
Examining and Creating Hard Links Preserving Links . . . . . . . . . . Finding Symbolic Links to a File . . Finding Hard Links to a File . . . . Exercise 1 . . . . . . . . . . . . . Exercise 2 . . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
77 78 78 78 79 79 80 81 81 81 82 82 82 83 84 85 85 85 86 86 86 87 87 87 88 88 88 89 89 89 90 90 91 92 92 92 93 93 93
Manage File Ownership . . . . . . . . . . . . . . . . . . Users and Groups . . . . . . . . . . . . . . . . . . . . . . The Superuser: Root . . . . . . . . . . . . . . . . . . . . Changing File Ownership with chown . . . . . . . . . . . . Changing File Group Ownership with chgrp . . . . . . . . Changing the Ownership of a Directory and Its Contents . . Changing Ownership and Group Ownership Simultaneously Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . Use File Permissions to Control Access to Files . . . . Basic Concepts: Permissions on Files . . . . . . . . . . . Basic Concepts: Permissions on Directories . . . . . . . . Basic Concepts: Permissions for Different Groups of People Examining Permissions: ls -l . . . . . . . . . . . . . . . Preserving Permissions When Copying Files . . . . . . . . How Permissions are Applied . . . . . . . . . . . . . . . . Changing File and Directory Permissions: chmod . . . . . . Specifying Permissions for chmod . . . . . . . . . . . . . . Changing the Permissions of a Directory and Its Contents . Special Directory Permissions: Sticky . . . . . . . . . . . Special Directory Permissions: Setgid . . . . . . . . . . . Special File Permissions: Setgid . . . . . . . . . . . . . . Special File Permissions: Setuid . . . . . . . . . . . . . . Displaying Unusual Permissions . . . . . . . . . . . . . . Permissions as Numbers . . . . . . . . . . . . . . . . . . Default Permissions: umask . . . . . . . . . . . . . . . . . Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . Create Partitions and Filesystems Concepts: Disks and Partitions . . . Disk Naming . . . . . . . . . . . . . Using fdisk . . . . . . . . . . . . . Making New Partitions . . . . . . . . Changing Partition Types . . . . . . Making Filesystems with mkfs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
vii
16.7 Useful Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 17.1 17.2 17.3 17.4 17.5 17.6 17.7 17.8 17.9 17.10 17.11 Control Filesystem Mounting and Unmounting Mounting Filesystems . . . . . . . . . . . . . . . Mounting a Filesystem: mount . . . . . . . . . . Mounting Other Filesystems . . . . . . . . . . . . Unmounting a Filesystem: umount . . . . . . . . Conguring mount: /etc/fstab . . . . . . . . . . . Sample /etc/fstab . . . . . . . . . . . . . . . . . Filesystem Types . . . . . . . . . . . . . . . . . Mount Options . . . . . . . . . . . . . . . . . . . Other Columns in /etc/fstab . . . . . . . . . . . . Mounting a File . . . . . . . . . . . . . . . . . . Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
94 95 96 96 96 97 97 97 98 98 98 99 99
viii
Module 1: Introduction
Module 1 Introduction
Module 1: Introduction
programs
s The shell and the window environment are programs s Programs only access to hardware is via the kernel
3. Unix Philosophy
s Multi-user
x A user needs an account to use a computer x Each user must log in x Complete separation of different users les and conguration
settings
s Small components
x Each component should perform a single task x Multiple components can be combined and chained together
for more complex tasks
Module 1: Introduction
4. What is Linux?
s Linux kernel
x Standard tools found on (nearly) all Linux systems x Many GNU utilities
q Written by the Free Software Foundation q Some claim the OS as a whole should be GNU/Linux s Linux distributions
x Kernel plus utilities plus other tools, packaged up for end users x Generally with installation program x Distributors include: Red Hat, Debian, SuSE, Mandrake
x When Linux rst loads after booting the computer x 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
x The shell will try to obey the command x Another prompt will appear
s Example: $ date Thu Jun 14 12:28:05 BST 2001 $
Module 1: Introduction
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
8. Command Syntax
s Most commands take parameters
x Some commands require them x Parameters are also known as arguments x For example, echo simply displays its arguments:
$ echo $ echo Hello there Hello there s Commands are case-sensitive
x Usually lower-case
$ echo whisper whisper $ ECHO SHOUT bash: ECHO: command not found
9. Files
s Data can be stored in a le s Each le has a lename
x A label used to refer to a particular le x Permitted characters include letters, digits, hyphens (-),
underscores ( ), and dots (.)
Module 1: Introduction
x Starting on the line after the command x Before the next shell prompt
Module 1: Introduction
x If it manages to delete the specied le, it does so silently x There is no File shopping list has been removed message x 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 doesnt take long to get used to
Module 1: Introduction
x Use the Up and Down cursor keys to scroll through the list of
previous commands
x x x x x
Particularly useful for xing a typo in the previous command The Left and Right cursor keys navigate across a command Extra characters can be typed at any point
Backspace deletes characters to the left of the cursor Del and Ctrl+D delete characters to the right
q Take care not to log out by holding down Ctrl+D too long
17. Exercise 1
1 Log in. 2 Log out. 3 Log in again. Open a terminal window, to start a shell. 4 Exit from the shell; the terminal window will close. 5 Start another shell. Enter each of the following commands in turn. The dollar ($) represents the prompt do not type it.
s $ date s $ whoami s $ hostname s $ uname s $ uptime
18. Exercise 2
1 Use the ls command to see if you have any les. 2 Create a new le 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 le. 3 Use ls again to verify that the new le exists. 4 Display the contents of the le. 5 Display the le again, but use the cursor keys to execute the same command again without having to retype it.
Module 1: Introduction
19. Exercise 3
1 Create a second le. Call it secret-of-the-universe, and put in whatever content you deem appropriate. 2 Check its creation with ls. 3 Display the contents of this le. Minimise the typing needed to do this:
s Scroll back through the command history to the command you
of creating it.
20. Exercise 4
After each of the following steps, use ls and cat to verify what has happened. 1 Copy secret-of-the-universe to a new le called answer.txt. Use Tab to avoid typing the existing les name in full. 2 Now copy hello.txt to answer.txt. Whats happened now? 3 Delete the original le, hello.txt. 4 Rename answer.txt to message. 5 Try asking rm to delete a le called missing. What happens? 6 Try copying secret-of-the-universe again, but dont specify a lename to which to copy. What happens now?
x Shows programs how to nd their way to the le x The root directory is referred to as / x Other directories are referred to by name, and their names are
separated by /
s If a path refers to a directory it can end in /
called ls:
/bin/ls s The following example will run the ls command, by specifying the
absolute path:
$ ls /usr/share/doc/
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 specied s Use the pwd (print working directory) command to see what your
10
5. Relative Paths
s Paths dont have to start from the root directory
x A path which doesnt start with / is a relative path x It is relative to some other directory, usually the current
directory
s For example, the following sets of directory changes both end up in
absolute ones
relative paths:
11
7. Hidden Files
s The special . and .. directories dont show up when you do ls
s Hidden les are often used for conguration les s You can still read hidden les they just dont get listed by ls by
default
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
lesystem
12
/usr/bin
s Programs are run from the shell, simply by typing their name s Many programs take options, which are added after their name and
4096 Jan 21 10:57 Accounts 345 Jan 21 10:57 notes.txt 3255 Jan 21 10:57 report.txt
x A description of what it does x A list of options which it accepts x Other information, such as the name of the author
GBdirect Ltd 2003 http://training.gbdirect.co.uk/
13
s The shell expands the wildcard, and passes the full list of les to
the program
s Just using * on its own will expand to all the les in the current
directory:
$ rm * s Names with wildcards in are called globs, and the process of
interface (GUI)
s Linux systems use the X window system to provide graphics
x X is just another program, not built-in to Linux x 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
14
x Dont provide advanced formatting, like word processors x Extremely important manipulating text is Unixs raison detre
s The most popular editors are Emacs and Vim, both of which are
x They use the $EDITOR variable to decide which editor to use x Usually it is set to vi, but it can be changed x Another example of the component philosophy
17. Exercise 1
1 Use the pwd command to nd out what directory you are in. 2 If you are not in your home directory (/home/USERNAME) then use cd without any arguments to go there, and do pwd again. 3 Use cd to visit the root directory, and list the les there. You should see home among the list. 4 Change into the directory called home and again list the les present. There should be one directory for each user, including the user you are logged in as (you can use whoami to check that). 5 Change into your home directory to conrm that you have gotten back to where you started.
18. Exercise 2
1 Create a text le in your home directory called shakespear, containing the following text:
Shall I compare thee to a summers day? Thou art more lovely and more temperate
2 Rename it to sonnet-18.txt. 3 Make a new directory in your home directory, called poetry. 4 Move the poem le into the new directory. 5 Try to nd a graphical directory-browsing program, and nd your home directory with it. You should also be able to use it to explore some of the system directories. 6 Find a text editor program and use it to display and edit the sonnet.
15
19. Exercise 3
1 From your home directory, list the les in the directory /usr/share. 2 Change to that directory, and use pwd to check that you are in the right place. List the les in the current directory again, and then list the les in the directory called docs. 3 Next list the les in the parent directory, and the directory above that. 4 Try the following command, and make sure you understand the result: echo 5 Use cat to display the contents of a text le which resides in your home directory (create one if you havent already), using the / syntax to refer to it. It shouldnt matter what your current directory is when you run the command.
20. Exercise 4
1 Use the hostname command, with no options, to print the hostname of the machine you are using. 2 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. 3 Use the locate command to nd les whose name contains the text hostname. Which of the lenames printed contain the actual hostname program itself?
21. Exercise 5
1 The * wildcard on its own is expanded by the shell to a list of all the les in the current directory. Use the echo command to see the result (but make sure you are in a directory with a few les or directories rst) 2 Use quoting to make echo print out an actual * symbol. 3 If you created a poetry directory earlier, augment it with another le, sonnet-29.txt:
When in disgrace with Fortune and mens eyes, I all alone beweep my outcast state,
4 Use the cat command to display both of the poems, using a wildcard. 5 Finally, use the rm command to delete the poetry directory and the poems in it.
GBdirect Ltd 2003 http://training.gbdirect.co.uk/
16
17
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
User
Shell
Kernel
x The Bourne-Again Shell x More sophisticated than the original sh by Steve Bourne x 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
x Separated by spaces (whitespace) x The rst word is the command to run x Subsequent words are options or arguments to the command
GBdirect Ltd 2003 http://training.gbdirect.co.uk/
18
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:
x Options, usually starting with one or two hyphens x Filenames, directories, etc., on which to operate
s The options usually come rst, but for most commands they do not
need to
s There is a special option -- which indicates the end of the options
x Single letter options start with a hyphen, e.g., -B x Less cryptic options are whole words or phrases, and start with
two hyphens, for example --ignore-backups
s Some options themselves take arguments
19
x ./sample runs the sample program in the current directory x /bin/ls runs the ls command in the /bin directory
s Otherwise, the shell looks in standard places for the program
x The variable called $PATH lists the directories to search in x Directory names are separated by colon, for example:
$ echo $PATH /bin:/usr/bin:/usr/local/bin
containing, respectively, your user/login name, machines hostname and current working directory, i.e.,
20
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
x Allows Emacs-like editing of the command line x Left and Right cursor keys, and Delete work as expected
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:
$ 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 rst argument s The pattern stringreplacement replaces the rst
occurrence of string with replacement in the previous command, and runs it:
$ echo $HOTSNAME $ TSST echo $HOSTNAME tiger
GBdirect Ltd 2003 http://training.gbdirect.co.uk/
21
x x x x x x x
Right move cursor to the right Left move cursor to the left Up previous history line Down next history line Ctrl+A move to start of line Ctrl+E move to end of line Ctrl+D delete current character
s There are alternative keys, as for the Emacs editor, which can be
with ;
s Useful when the rst command might take a long time: time-consuming-program; ls s Alternatively, use && to arrange for subsequent commands to run
22
x The $() form is usually preferred, but backticks are widely used
s Newlines are stripped from the output before the substitution s Another example: use vi to edit the last of the les found: vi $(locate manual.html | tail -1)
x The database needs to be updated regularly x Usually this is done automatically with cron x But locate will not nd les 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
including name
s Syntax: find directories criteria s Simplest possible example: find . s Finding les with a simple criterion: $ find . -name manual.html
Looks for les under the current directory whose name is manual.html
s The criteria always begin with a single hyphen, even though they
23
the lename
s The {} and ; are required by find, but must be quoted to protect
21. Exercise 1
1 Use the df command to display the amount of used and available space on your hard drive. 2 Check the man page for df, and use it to nd 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. 3 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 or logout commands.
24
22. Exercise 2
1 Try ls with the -a and -A options. What is the difference between them? 2 Write a for loop which goes through all the les 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. 3 Change the loop so that it goes through the names of the people in the room (which neednt be the names of les) and print greetings to them. 4 Of course, a simpler way to print a list of lenames is echo *. Why might this be useful, when we usually use the ls command?
23. Exercise 3
1 Use the find command to list all the les and directories under your home directory. Try the -type d and -type f criteria to show just les and just directories. 2 Use locate to nd les whose name contains the string bashbug. Try the same search with find, looking over all les on the system. Youll need to use the * wildcard at the end of the pattern to match les with extensions. 3 Find out what the find criterion -iname does.
25
26
formats
x Non-English text might use the ISO-8859 character sets x Unicode is better, but unfortunately many Linux command-line
utilities dont (directly) support it yet
2. Lines of Text
s Text les are naturally divided into lines s In Linux, a line ends in a line feed character
x Windows and DOS use a carriage return followed by a line feed x Macintosh systems use only a carriage return x Programs are available to convert between the various formats
as needed
s Each tool should be good at one specic job s Join programs together with pipes
x Indicated with the pipe character: | x The rst program prints text to its standard output x That gets fed into the second programs standard input
s For example, to connect the output of echo to the input of wc: $ echo "count these words, boy" | wc
27
command line
s More commonly used with just one le to quickly send its contents
to the screen:
$ cat /etc/resolv.conf domain gbdirect.co.uk nameserver 127.0.0.1 nameserver 192.168.100.12
combined total
s Options:
x x x x
-c output character count -l output line count -w output word count 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 les: $ wc -l *.txt
GBdirect Ltd 2003 http://training.gbdirect.co.uk/
28
x Removes consecutive duplicate lines x Usually give it sorted input, to remove all duplicates
s Example: nd 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:
x uniq -c counts how many times each line appeared x uniq -u prints only unique lines x uniq -d prints only duplicated lines
x Either can be missed out x The rst byte, character or eld is numbered 1, not 0
s Example: select usernames of logged in users: $ who | cut -d" " -f1 | sort -u
29
with -t number
x Defaults to 75
s Example: change the line length of notes.txt to a maximum of 70
30
x The option -ba numbers every line x cat -n also numbers lines, including blank ones
x Can be followed by the number of bytes per word x Add z to show ASCII equivalents alongside the numbers x A useful format is given by od -t x1z hexadecimal, one byte
words, with ASCII
s Alternatives to od include xxd and hexdump
31
x -d double spaced output x -h header change from the default header to header x -l lines change the default lines on a page from 66 to
lines x -o width set (offset) the left margin to width
s Example: $ pr -h "My Thesis" thesis.txt | lpr
32
and so on
s Example: show a list of logins and logouts, but with the most recent
characters in end-set
s Cannot accept a le as an argument, but uses the standard input
and output
s Options:
21. tr Examples
s Replace all uppercase characters in input-le with lowercase
33
executed conditionally
of the output
s Use -d char to set the delimiter between elds in the output
x The default is tab x 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
34
26. Exercise 1
1 Type in the example on the cut slide to display a list of users logged in. (Try just who on its own rst to see what is happening.) 2 Arrange for the list of usernames in whos output to be sorted, and remove any duplicates. 3 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? 4 Use sed to correct the misspelling enviroment to environment. Use it on a test le, containing a few lines of text, to check it. Does it work if the misspelling occurs more than once on the same line? 5 Use nl to number the lines in the output of the previous question.
27. Exercise 2
1 Try making an empty le and using tail -f to monitor it. Then add lines to it from a different terminal, using a command like this:
$ echo "testing" >>filename
2 Once you have written some lines into your le, use tr to display it with all occurances of the letters AF changed to the numbers 05. 3 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 le, even though it isnt text. 4 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.
35
28. Exercise 3
1 Use the split command to split the binary of the ls command into 1Kb chunks. You might want to create a directory especially for the split les, so that it can all be easily deleted later. 2 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
36
37
1. Filesystem Objects
s A le is a place to store data: a possibly-empty sequence of bytes s A directory is a collection of les and other directories s Directories are organized in a hierarchy, with the root directory at
the top
home/
bin/
jeff/
cp
rm
directory
3. File Extensions
s Its common to put an extension, beginning with a dot, on the end
of a lename
s The extension indicates the type of the le:
.txt .gif .jpg .mp3 .gz .tar .tar.gz, .tgz Text le Graphics Interchange Format image Joint Photographic Experts Group image MPEG-2 Layer 3 audio Compressed le Unix tape archive le Compressed archive le
38
x But also saves the current directory, so that you can go back
later
s For example, to visit Freds home directory, and then go back to
6. Filename Completion
s Modern shells help you type the names of les and directories by
press Tab
s For an ambiguous name (there are several possible completions),
x For Bash, type Tab twice in succession x For C shells, type Ctrl+D
s Both of these shells will automatically escape spaces and special
39
7. Wildcard Patterns
s Give commands multiple les by specifying patterns s Use the symbol * to match any part of a lename: $ ls *.txt accounts.txt letter.txt report.txt
s Just * produces the names of all les 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 lenames by the shell, so the
program you pass them to cant tell that those names came from wildcard expansion
x -f, force overwriting of destination les x -i, interactively prompt before overwriting les x -a, archive, copy the contents of directories recursively
9. 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/
40
directories
s It is equivalent to copying and then deleting
x -f, force overwrite, even if target already exists x -i, ask user interactively before overwriting les
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/
remove it
s Use carefully if you are logged in as root! s Options:
x -f, delete write-protected les without prompting x -i, interactive ask the user before deleting les x -r, recursively delete les and directories
s For example, clean out everything in /tmp, without prompting to
x Write a wildcard pattern that matches only the name you want
to delete:
$ rm -i ./name-with-funny-characters*
x The ./ forces it to be in the current directory x Using the -i option to rm makes sure that you wont delete
anything else by accident
41
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
rst
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
42
x -a, change only the access time x -m, change only the modication time x -t [YYYY]MMDDhhmm[.ss], set the timestamp of the le to
the specied date and time
2001, 5:59p.m.
$ touch -t 200101201759 homework
17. Exercise 1
1 Just after youve logged in or opened a terminal window, what directory is your current directory? (The pwd command might be useful here.) 2 Change directory to /etc and then /tmp. Each time use pwd to check that you got to the right place, and ls to see what les are there. 3 From /tmp, use cd .. and see where you end up. Then try it again from your working directory. 4 Find out what les are in /bin, without changing your current directory. You should recognise the names of some of the programs stored there. 5 List the les in /usr/bin and /usr/local/bin (if it exists). Use the Tab key to save typing.
18. Exercise 2
1 Use cd to go to your home directory, and create a new directory there called dog. 2 Create another directory within that one called cat, and another within that called mouse. 3 Remove all three directories. You can either remove them one at a time, or all at once. 4 If you can delete directories with rm -r, what is the point of using rmdir for empty directories? 5 Try creating the dog/cat/mouse directory structure with a single command.
43
19. Exercise 3
1 Copy the le /etc/passwd to your home directory, and then use cat to see whats in it. 2 Rename it to users using the mv command. 3 Make a directory called programs and copy everything from /bin into it. 4 Delete all the les in the programs directory. 5 Delete the empty programs directory and the users le.
20. Exercise 4
1 The touch command can be used to create new empty les. Try that now, picking a name for the new le:
$ touch baked-beans
3 Wait for a minute, and then try the previous two steps again, and see what changes. What happens when we dont specify a time to touch? 4 Try setting the timestamp on the le to a value in the future. 5 When youre nished with it, delete the le.
44
45
1. Standard Files
s Processes are connected to three standard les
STANDARD INPUT
PROCESS
STANDARD OUTPUT
STANDARD ERROR
2. Standard Input
s Programs can read data from their standard input le 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
3. Standard Output
s Programs can write data to their standard output le s Abbreviated to stdout s Used for a programs normal output s By default this is printed on the terminal
46
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
5. Pipes
s A pipe channels the output of one program to the input of another
x The programs standard output goes into the le x If the le already exists, it is overwritten
s Both can be used at the same time: $ filter < input-file > output-file
47
7. Appending to Files
s Use >> to append to a le: $ date >> log.txt
48
its arguments
x The default is 1
s xargs is very often used with input piped from find s Example: if there are too many les in a directory to delete in one
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 le s Like > and | combined s For example, to save details of everyones logins, and save Bobs
last
tee
grep
bob.txt
everyone.txt
12. Exercise 1
1 Try the example on the Pipes slide, using rev to reverse some text. 2 Try replacing the echo command with some other commands which produce output (e.g., whoami). 3 What happens when you replace rev with cat? You might like to try running cat with no arguments and entering some text.
49
13. Exercise 2
1 Run the command ls --color in a directory with a few les 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. 2 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).
50
51
x x x x x x
-i makes the matching case-insensitive -r searches through les in specied directories, recursively -l prints just the names of les which contain matching lines -c prints the count of matches in each le -n numbers the matching lines in the output -v rev erses the test, printing lines which dont match
2. Pattern Matching
s Use grep to nd 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 Nancys entry
52
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
x Same as grep -E
s Special characters dont have to be marked with \
6. sed
s sed reads input lines, runs editing-style commands on them, and
53
7. Further Reading
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 user manual (1978),
http://www.urc.bl.ac.yu/manuals/progunix/sed.txt
8. Exercise 1
1 Use grep to nd information about the HTTP protocol in the le /etc/services. 2 Usually this le 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 le in less. 3 Add another use of grep -v to your pipeline to remove blank lines (which match the pattern $). 4 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.
54
55
1. Job Control
s Most shells offer job control
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
2. jobs
s The jobs builtin prints a listing of active jobs and their job numbers: $ jobs [1]- Stopped [2] Running [3]+ Stopped vim index.html netscape & man ls
x 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
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
56
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
5. Exercise 1
1 Start a process by running man bash and suspend it with Ctrl+Z. 2 Run xclock in the background, using &. 3 Use jobs to list the backgrounded and stopped processes. 4 Use the fg command to bring man into the foreground, and quit from it as normal. 5 Use fg to foreground xclock, and terminate it with Ctrl+C. 6 Run xclock again, but this time without &. It should be running in the foreground (so you cant use the shell). Try suspending it with Ctrl+Z and see what happens. To properly put it into the background, use bg.
57
58
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 identies each process by a number known as a
2. Process Properties
s A process has a user id (uid) and a group id (gid) which together
which created it
x The kernel starts an init process with pid 1 at boot-up x Every other process is a descendant of pid 1
s Each process has its own working directory, initially inherited
s (Apache starts many child processes so that they can serve HTTP
59
4. Process Monitoring: ps
s The ps command gives a snapshot of the processes running on a
x Normally shows a fairly brief summary of each process x Normally shows only processes which are both owned by the
current user and attached to a terminal
s Unfortunately, it doesnt use standard option syntax s Instead it uses a mixture of options with one of three syntaxes:
x Traditional BSD ps: a single letter with no hyphen x Unix98 ps: a single letter preceded by a hyphen x GNU: a word or phrase preceded by two hyphens
5. ps Options
s ps has many options s Some of the most commonly used are:
Option Description Show processes owned by other users Display process ancestors in a tree-like format Use the user output format, showing user names and process start times Use a wider output format. Normally each line of output is truncated; each use of the w option makes the window wider Include processes which have no controlling terminal Show information on all processes Use a long output format Use a full output format Show only processes named cmd Show only processes owned by user
a f u w x -e -l -f -C cmd -U user
60
7. pstree Options
Option Description Display commands arguments Dont compact identical subtrees Attempt to use terminal-specic line-drawing characters Highlight the ancestors of the current process Sort processes numerically by pid, rather than alphabetically by name Include pids in the output
-a -c -G -h -n -p
activity
memory information
-b -n num -d delay -i -s
61
q Ctrl+L h k n r s
x 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
62
as a built-in
s Use kill -HUP pid or kill -s HUP pid to send a SIGHUP to the
as root
15. Exercise 1
1 Use top to show the processes running on your machine. 2 Make top sort by memory usage, so that the most memory-hungry processes appear at the top. 3 Restrict the display to show only processes owned by you. 4 Try killing one of your processes (make sure its nothing important). 5 Display a list of all the processes running on the machine using ps (displaying the full command line for them). 6 Get the same listing as a tree, using both ps and pstree. 7 Have ps sort the output by system time used.
63
64
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
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
s Non-root users can only specify values from 1 to 19; the root user
3. renice
s renice changes the niceness of existing processes s Non-root users are only permitted to increase a processs niceness s To set the process with pid 2984 to the maximum niceness (lower
priority):
$ renice 20 2984
priority):
$ renice -15 3598 s You can also change the niceness of all a users processes: $ renice 15 -u mikeb
65
4. Exercise 1
1 Create the following shell script, called forever, in your home directory:
#!/bin/sh while [ 1 ]; do echo hello... >/dev/null; done
2 Use ps -l to check the scripts nice level 3 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 4 Try using nice or renice to make a process niceness less than 0
66
67
themselves
3. Quoting: Backslashes
s You can put a backslash \ in front of any single character to turn off
68
x A dollar sign $ retains its special interpretation x As do backticks x ! cant be escaped in double quotes
s A backslash can be used within double quotes to selectively disable
characters:
$ echo "\*/" \*/
whitespace:
$ echo "double-quoted".single-quoted.unquoted double-quoted.single-quoted.unquoted $ echo She said, "Don\t go." She said, "Dont go." s Rarely needed the last example is probably better written as: $ echo "She said, \"Dont go.\""
s * on its own expands to all les in the current directory s Glob expansion is done by the shell
69
certain length:
$ rm ???*.txt
Matches any le ending in .txt that has at least three characters before the dot
lower-case letter
s To match any hidden le except the . and .. directories: .[.]*
70
12. Exercise 1
1 Print out the following message: *** SALE $$$ ***. 2 Try escaping the same string using single quotes, double quotes and backslashes. 3 Echo the message quoting isnt simple, escaping the spaces by putting single quotes around it. 4 Use the glob pattern .[.]* to list all the hidden les in your home directory. 5 To nd out what shells are available on your system, list the programs in /bin whose names end in sh. 6 Use [] brackets to list all the les in /usr/bin with names starting with a, b or c.
71
72
1. Filesystems
s Some confusion surrounds the use of the term lesystem s Commonly used to refer to two distinct concepts
1 The hierarchy of directories and les which humans use to organise data on a system (unied lesystem) 2 The formatting system which the kernel uses to store blocks of data on physical media such as disks (lesystem types)
x Each individual lesystem has its own root x That root can be grafted onto any directory in the unied
lesystem
3. File Types
s Files directly contain data s Directories provide a hierarchy of les: they can contain both les
73
lesystem
s It contains information about the le, including its type
x A directory is stored on disk as a list of le and directory names x Each name has an inode number associated with it x Separating names from inodes means that you can have
multiple directory entries referring to the same le
74
75
1. Symbolic Links
s A symbolic link (or symlink) is a pseudo-le which behaves as an
replaces the symlink component with its contents and starts again
s Symlinks allow you to keep a le (or directory) in one place, but
3. Hard Links
s Where symlinks refer to other les by name, a hard link refers to
x An inode is the data structure that describes a le on disk x It contains information about the le, including its type
(le/directory/device), modication time, permissions, etc.
s A directory entry contains a name and an inode number
76
inode data
mimics cp:
$ ls -l *.dtd -rw-r--r-1 $ ln module.dtd $ ls -l *.dtd -rw-r--r-2 -rw-r--r-2 anna anna chapter.dtd anna anna 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
77
7. Preserving Links
s Commands that operate on les often take options to specify
x Use the -d option (--no-dereference) to preserve all links x Use the -R option (--recursive) when copying recursively to
ensure that symlinks are preserved
disk-intensive!
interested in:
$ df module.dtd Filesystem 1k-blocks /dev/sdb3 13647416 $ ls -i module.dtd 245713 module.dtd Used Available Use% Mounted on 5241196 7712972 40% /home
s Then use finds -inum option to look for directory entries in that
lesystems
GBdirect Ltd 2003 http://training.gbdirect.co.uk/
78
10. Exercise 1
1 Make a temporary directory and change into it. 2 Make some test les as follows:
$ echo "oranges and lemons" > fruit $ echo spuds > veg
3 Make a symbolic link called starch to the veg le. 4 Make a hard link called citrus to the appropriate le, and check that it has the same inode number. 5 Delete the original fruit le and check that citrus still contains the text. 6 Delete the original veg le and try to look at the contents of starch. Use ls to check the symlink.
11. Exercise 2
1 Try to see what the following loop does, and then create some .htm les and try it:
$ for htm in *.htm; do > ln -s $htm ${htm}l; > done
2 Make a symlink called dir to a directory (such as /etc). 3 Try the following commands to display the link and compare the results:
$ ls -l dir $ ls -l dir/
79
80
81
chgrp
s The chgrp command changes the group ownership of les or
directories
s Simple usage: # chgrp staff report.txt s Makes staff be the group owner of the le logle.txt s As for chown, specify any number of les or directories s The superuser may change the group ownership of any le to any
group
s The owner of a le may change its group ownership
of a le 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/
82
7. Exercise 1
1 Find out who owns the le /bin/ls and who owns your home directory (in /home). 2 Log on as root, and create an empty le with touch. The user and group owners should be root check with ls. 3 Change the owner of the le to be users. 4 Change the group owner to be any non-root user. 5 Change both of the owners back to being root with a single command.
83
84
r w x
s Occasionally referred to as permission bits s Note that for scripts, you need both execute permission and read
permission
r w x
having one but not the other is almost never what you want
85
4. Examining Permissions: ls -l
s The ls -l command allows you to look at the permissions on a le: $ ls -l drwxr-x---rw-rw-r-9 aaronc 1 aaronc staff staff 4096 Oct 12 12:57 accounts 11170 Dec 9 14:11 report.txt
s The third and fourth columns are the owner and group-owner s The rst column is the permissions:
x One character for the le type: d for directories, - for plain les x Three characters of rwx permissions for the owner (or a dash if
the permission isnt available)
x Three characters of rwx permissions for the group owner x Three characters of rwx permissions for everyone else
apply to you
86
adds (+) executable permission (x) for all users (a) on the le new-program
x u for the les owner, g for the group owner, o for other users, or
a for all users
s = sets permissions for les, + adds permissions to those already
x And any subdirectories, recursively x Any any contained executable les x Contained non-executable les have rw permissions added for
the group owner, and r permission for everyone else
GBdirect Ltd 2003 http://training.gbdirect.co.uk/
87
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 s Enable setgid with: # chmod g+s /data/projects 4096 Oct 19 13:14 /data/projects
compiled programs
s Useful if you want a program to be able to (for example) edit some
88
user id of the le
s As with setgid, Linux doesnt 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 s Enable setuid with: # chmod u+s /usr/local/bin/program 12244 Feb 7 2000 /usr/bin/passwd
x But you can still tell whether the execute bits are set x Lowercase s or t indicates that execute is enabled (i.e., there is
an x behind the letter)
together:
4000 2000 1000 400 200 100 Setuid Setgid Sticky Readable by owner Writable by owner Executable by owner 40 20 10 4 2 1 Readable by group owner Writable by group owner Executable by group owner Readable by anyone Writable by anyone Executable by anyone
s You may use numerical permissions with chmod: $ chmod 664 *.txt
is equivalent to:
$ chmod ug=rw,o=r *.txt
GBdirect Ltd 2003 http://training.gbdirect.co.uk/
89
for the rwx permissions you dont want on new les and directories
x 022 avoid world- and group-writable, allow everything else x 027 avoid group-writable, and allow no permissions for
anyone else
s You normally want to put a call to umask in your shells startup le
17. Exercise 1
1 Find out what permissions are set on your home directory (as a normal user). Can other users access les inside it? 2 If your home directory is only accessible to you, then change the permissions to allow other people to read les inside it, otherwise change it so that they cant. 3 Check the permissions on /bin and /bin/ls and satisfy yourself that they are reasonable. 4 Check the permissions available on /etc/passwd and /etc/shadow. 5 Write one command which would allow people to browse through your home directory and any subdirectories inside it and read all the les.
90
91
x Information about partitions is stored in the partition table x Linux defaults to using partition tables compatible with
Microsoft Windows
2. Disk Naming
s The device les for IDE hard drives are /dev/hda to /dev/hdd
x hda and hdb are the drives on the rst IDE channel, hdc and
hdd the ones on the second channel
x The rst drive on each channel is the IDE master, and the
second is the IDE slave
s Primary partitions are numbered from 14 s Logical partitions are numbered from 5 s The devices /dev/hda, etc., refer to whole hard disks, not partitions
x Add the partition number to refer to a specic partition x For example, /dev/hda1 is the rst partition on the rst IDE disk
s SCSI disks are named /dev/sda, /dev/sdb, etc
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
x x x x
Type m to get a list of commands Use p to show what partitions currently exist Use q to quit without altering anything Use w to quit and write the changes
q Use with caution, and triple-check what youre doing!
92
x The default values make the partition as big as possible x The desired size can be specied in megabytes, e.g., +250M
s Changes to the partition table are only written when the w
command is given
x Warning: any old data on the partition will be lost x For example, to make an ext2 lesystem on /dev/hda2:
# mkfs -t ext2 -c /dev/hda2
93
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
94
95
1. Mounting Filesystems
s As far as many parts of a Linux system are concerned, a partition
administrator
s For example, many systems are congured so that $ mount /mnt/cdrom
will mount the contents of the machines CD-ROM drive under the directory /mnt/cdrom
any options
96
point
s umount /dev/sdb3 unmounts the lesystem in the /dev/sdb3
x A lesystem is busy if a process has a le on it open x Or if a process has a directory within it as its current directory
boot-up
s Each line in /etc/fstab describes one lesystem s Six columns on each line
6. Sample /etc/fstab
s A sample /etc/fstab le: # device /dev/hda3 /dev/hda1 /dev/hda5 /dev/hdb1 /dev/hdb2 none /dev/scd0 /dev/fd0 mount-point / /boot /usr /usr/local /home /proc /mnt/cdrom /mnt/floppy type ext2 ext2 ext2 ext2 ext2 proc iso9660 auto options (dump) defaults 1 defaults 1 defaults 1 defaults 1 defaults 1 defaults 0 noauto,users,ro 0 noauto,users 0 pass-no 1 2 2 2 2 0 0 0
97
7. Filesystem Types
s The most common lesystem types are:
Type Usage The standard Linux lesystem The lesystem used on CD-ROMs Not a real lesystem, so uses none as the device. Used as a way for the kernel to report system information to user processes The lesystem used by Windows 95 Not a real lesystem type. Used as a way of asking the mount command to probe for various lesystem types, particularly for removable media
(Windows or Samba)
s Other, less common types exist; see mount(8)
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 In /etc/fstab, prevents the lesystem being mounted at bootup. Useful for removable media Mount the lesystem read-only Let non-root users mount and unmount this lesystem Like users, but non-root users can only unmount lesystems that they themselves mounted
x Used by the dump and restore backup utilities x Few people use those tools x Just use 1 for normal lesystems, and 0 for removable
lesystems
s The sixth column is called pass-no
x Use 1 for the root lesystem x Use 0 for lesystems that arent mounted at boot-up x Use 2 for other lesystems
98
/mnt/disk :
# mount -o loop disk.img /mnt/disk
11. Exercise 1
1 Use mount to nd out which lesystems are mounted. 2 Check the /etc/fstab le to see whether the oppy drive is congured properly, and nd out what its mount point is set to. 3 Mount a oppy disk at the default mount point. 4 Copy a le onto the oppy disk. Does Linux write it immediately? Unmount the oppy to ensure that everything on it is properly written, and it is safe to remove. 5 Try the commands on the last slide to mount a le, and try copying some les into it. Try using the df command to see how much space is available in the le. Unmount /mnt/disk as you would any other lesystem.
99