[figure: Header; Body; Footer; Margin Notes; 8; 7; 1; 3; 10; 9; 11; 2; 4; 5; 6]

1  one inch + \hoffset      2  one inch + \voffset
3  \oddsidemargin = 53pt    4  \topmargin = 23pt
5  \headheight = 12pt       6  \headsep = 40pt
7  \textheight = 650pt      8  \textwidth = 400pt
9  \marginparsep = 7pt     10  \marginparwidth = 61pt
11  \footskip = 25pt            \marginparpush = 5pt (not shown)
   \hoffset = 0pt              \voffset = 0pt
   \paperwidth = 597pt         \paperheight = 845pt

Linux Training Materials Project

GBdirect Limited

27 Park Drive Bradford, BD9 4DS

West Yorkshire tel: +44 (0)1274 772277 linux@gbdirect.co.uk

October 9, 2000

Contents

1 Overview 1

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Generic Features of Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2 Linux — The Kernel of a System . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3 Fundamental Characteristics of Linux . . . . . . . . . . . . . . . . . . . . . . 4 1.4 Multiuser Multitasking and Time-sharing . . . . . . . . . . . . . . . . . . . . 5 1.5 Protected memory mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.6 Multiple Filesystem Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.7 The Many Faces of a GNU/Linux System . . . . . . . . . . . . . . . . . . . 8 1.8 The Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.9 Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.10 Filename Extensions and File Types . . . . . . . . . . . . . . . . . . . . . . 11 1.11 Hidden Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.12 The Shell (bash) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.13 Key Features of the Bash Shell . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.14 Interacting with a Linux ‘Terminal’ . . . . . . . . . . . . . . . . . . . . . . . . 15 1.15 Software Tools: The UNIX Philosophy . . . . . . . . . . . . . . . . . . . . . 16 1.16 Tasks/Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.17 Process Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.18 Re-directing I/O to and from Files . . . . . . . . . . . . . . . . . . . . . . . . 19 1.19 Re-directing I/O to and from Files (continued) . . . . . . . . . . . . . . . . . 20 1.20 Pipes & Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.21 Linux as a Programming Environment . . . . . . . . . . . . . . . . . . . . . 22 1.22 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.23 TCP/IP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.24 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.25 Using the man pages (On-Line Manual) . . . . . . . . . . . . . . . . . . . . 26 1.26 Overview Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.27 Overview Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

2 Basic Shell 33

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2.2 Getting around the command line . . . . . . . . . . . . . . . . . . . . . . . . 35 2.3 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2.4 Plumbing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.5 Plumbing (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 2.6 Output Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.7 Input Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.8 Combining Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2.9 Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2.10 Background Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.11 Background Processes (continued) . . . . . . . . . . . . . . . . . . . . . . . 44 2.12 Background Processes and nohup . . . . . . . . . . . . . . . . . . . . . . . 45 2.13 Command Grouping and Sub-shells . . . . . . . . . . . . . . . . . . . . . . 46 2.14 Process Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 2.15 Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2.16 Signals (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 2.17 Background Processes: top . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 2.18 Filename Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 2.19 Quoting Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 2.20 Shell built-in commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 2.21 Basic Shell Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 2.22 Basic Shell Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

3 Basic Tools 57

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.2 Using Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3.3 The On-Line Manual (man) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3.4 Finding Files the Long Way (find) . . . . . . . . . . . . . . . . . . . . . . . 61 3.5 Locate Files (locate) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3.6 View and Concatenate Files (cat) . . . . . . . . . . . . . . . . . . . . . . . . 63 3.7 View Large Files & Output (less) . . . . . . . . . . . . . . . . . . . . . . . . 64 3.8 Viewing Parts of Files (head and tail) . . . . . . . . . . . . . . . . . . . . . 65 3.9 Listing File Information (ls) . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 3.10 File Classification (file) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.11 Count Words, Lines, Characters (wc) . . . . . . . . . . . . . . . . . . . . . . 68 3.12 Differences Between Files (diff) . . . . . . . . . . . . . . . . . . . . . . . . 69 3.13 Compare Binary Files (cmp) . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 3.14 Regular Expression Searches (grep) . . . . . . . . . . . . . . . . . . . . . . 72 3.15 grep examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 3.16 Sort and Merge Files (sort) . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 3.17 sort Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 3.18 Display Unique Lines (uniq) . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 3.19 Split Files (split) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 3.20 Splitting Files by Context (csplit) . . . . . . . . . . . . . . . . . . . . . . . 78 3.21 Compression Utilities (gzip) . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 3.22 Store and Retrieve Archives (tar) . . . . . . . . . . . . . . . . . . . . . . . . 80 3.23 Translating Characters (tr) . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 3.24 Examples of tr Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 3.25 Execute programs at specified times (at) . . . . . . . . . . . . . . . . . . . . 83 3.26 Options and commands related to at . . . . . . . . . . . . . . . . . . . . . . 84 3.27 Running commands regularly (crontab) . . . . . . . . . . . . . . . . . . . . 85 3.28 Evaluate expressions (expr) . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 3.29 Linux Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 3.30 Printing documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 3.31 Main Printing Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 3.32 Using lpr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 3.33 Using lpq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 3.34 Using lprm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 3.35 Basic Tools Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 3.36 Basic Tools Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 4 More Tools 98

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 4.2 Displaying System Processes (top) . . . . . . . . . . . . . . . . . . . . . . . 100 4.3 Options and Interactive Commands for top . . . . . . . . . . . . . . . . . . 101 4.4 Reporting process status (ps) . . . . . . . . . . . . . . . . . . . . . . . . . . 102 4.5 Options for Reporting process status (ps) . . . . . . . . . . . . . . . . . . . 103 4.6 Finding Files using specified criteria (find) . . . . . . . . . . . . . . . . . . 105 4.7 Criteria used in find expressions . . . . . . . . . . . . . . . . . . . . . . . . 106 4.8 Examples of using (find) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 4.9 Reporting virtual memory statistics (vmstat) . . . . . . . . . . . . . . . . . . 108 4.10 Output from vmstat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 4.11 free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 4.12 ldd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 4.13 uptime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 4.14 xargs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 4.15 Options to xargs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 4.16 Positioning filenames with xargs . . . . . . . . . . . . . . . . . . . . . . . . 115 4.17 cpio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 4.18 gzip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 4.19 Unzipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 4.20 tar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 4.21 Raw devices and tar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 4.22 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 4.23 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

5 Basic Filesystem 123

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 5.1 Filesystem Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 5.2 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 5.3 Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 5.4 Directory Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 5.5 Pathnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 5.6 Current Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 5.7 Dot (.) and DotDot(..) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 5.8 Moving and Copying Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 5.9 Removing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 5.10 Operations on Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 5.11 Inodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 5.12 Inodes (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 5.13 Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 5.14 Hard links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 5.15 Soft links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 5.16 Soft links (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 5.17 Access Control and UID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 5.18 Categories of Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . 141 5.19 Access Control — Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 5.20 Changing Access Permission: chmod . . . . . . . . . . . . . . . . . . . . . . 143 5.21 chmod symbolically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 5.22 chmod numerically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 5.23 umask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 5.24 Special Files — /dev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 5.25 Special Files — /proc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 5.26 Filesystem Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 5.27 /etc/fstab — Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 5.28 Mounting Additional Volumes . . . . . . . . . . . . . . . . . . . . . . . . . . 151 5.29 Mounting shared filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . 152 5.30 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 5.31 Filesystem Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 5.32 Filesystem Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

6 Introduction to Editing With vi 157

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 6.1 Text editors under Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 6.2 vi and your terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 6.3 vi screen layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 6.4 Opening files with vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 6.5 vi Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 6.6 Saving, changing file and quitting . . . . . . . . . . . . . . . . . . . . . . . . 163 6.7 Moving around in command mode . . . . . . . . . . . . . . . . . . . . . . . 164 6.8 Numeric Prefixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 6.9 Further Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 6.10 Further Movement — Example . . . . . . . . . . . . . . . . . . . . . . . . . 167 6.11 Movement by lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 6.12 Movement by lines — Examples . . . . . . . . . . . . . . . . . . . . . . . . 169 6.13 Inserting text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 6.14 i command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 6.15 Multiple Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 6.16 Deleting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 6.17 Changing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 6.18 Copy and Paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 6.19 Finding your place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 6.20 Miscellaneous Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 6.21 Search and replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 6.22 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 6.23 Regular Expression Conventions . . . . . . . . . . . . . . . . . . . . . . . . 180 6.24 Regular Expression Examples . . . . . . . . . . . . . . . . . . . . . . . . . . 181 6.25 Regular Expression Replacement . . . . . . . . . . . . . . . . . . . . . . . . 182 6.26 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 6.27 vi Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 6.28 vi Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

7 Basic X-Windows 188

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 7.1 What X-Windows Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 7.2 X Needs Window Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 7.3 Window Managers Are Applications . . . . . . . . . . . . . . . . . . . . . . 191 7.4 Desktop Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 7.5 Starting X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 7.6 Stopping X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 7.7 Running Shells (Xterms) Under X . . . . . . . . . . . . . . . . . . . . . . . . 195 7.8 Running Applications from an xterm . . . . . . . . . . . . . . . . . . . . . . 196 7.9 Running Applications from a window manager . . . . . . . . . . . . . . . . . 197 7.10 Configuring X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 7.11 Basic X Hardware Configuration . . . . . . . . . . . . . . . . . . . . . . . . 199 7.12 Basic X Software Configuration . . . . . . . . . . . . . . . . . . . . . . . . . 200 7.13 Networked X — The Client-Server Relationship . . . . . . . . . . . . . . . . 201 7.14 Principles of Running Remote X Apps . . . . . . . . . . . . . . . . . . . . . 202 7.15 How to Run Remote X Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 7.16 Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 7.17 Better Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 7.18 Basic X Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

8 Fundamentals of TCP/IP 208

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 8.1 Fundamentals of TCP/IP Networking . . . . . . . . . . . . . . . . . . . . . . 209 8.2 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 8.3 Recap of basic IP Concepts — Components . . . . . . . . . . . . . . . . . . 211 8.4 IP versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 8.5 Packets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 8.6 Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 8.7 Addresses (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 8.8 Netmasks and subnetting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 8.9 Netmasks with /xx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 8.10 Transferring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 8.11 Hosts & Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 8.12 Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 8.13 Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 8.14 Ports cont.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 8.15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 8.16 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

9 Practical TCP/IP 226

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 9.1 Ping Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 9.2 Network Statistics (netstat) in Practice . . . . . . . . . . . . . . . . . . . . 228 9.3 netstat (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 9.4 netstat — Further Examples . . . . . . . . . . . . . . . . . . . . . . . . . . 230 9.5 Network Traffic (tcpdump) in Practice . . . . . . . . . . . . . . . . . . . . . . 231 9.6 tcpdump Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 9.7 tcpdump Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 9.8 Firewalling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 9.9 Basic Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 9.10 Basic Theory (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 9.11 ipchains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 9.12 ipchains Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 9.13 ipchains Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 9.14 Options For Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 9.15 ipchains — Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 9.16 Removing Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 9.17 Implementing ipchains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 9.18 Save and restore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 9.19 ipchains setup script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 9.20 Real World ipchains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 9.21 Interface Configuration and Management . . . . . . . . . . . . . . . . . . . 247 9.22 Point-and-Click Interface Administration . . . . . . . . . . . . . . . . . . . . 248

9.23 /etc/sysconfig/network-scripts . . . . . . . . . . . . . . . . . . . . . . 249

9.24 ifcfg-ethx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 9.25 Altering An Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 9.26 Adding an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 9.27 The ‘Proper’ Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 9.28 Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 9.29 The Secure Shell in Practice (ssh) . . . . . . . . . . . . . . . . . . . . . . . 255 9.30 Secure Copying in Practice (scp) . . . . . . . . . . . . . . . . . . . . . . . . 256 9.31 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 9.32 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 9.33 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

10 Shared File Systems 260

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 10.1 NFS (Network File System) . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 10.2 NFS Basics … continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 10.3 Exporting File Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 10.4 Viewing exports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 10.5 Importing File Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 10.6 Samba . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 10.7 Samba Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 10.8 Samba Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 10.9 Access to Files and Printers . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 10.10 Testing Samba . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 10.11 Smbclient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 10.12 Samba configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 10.13 Testing Samba . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

Notes on Testing Samba . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 10.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 10.15 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

11 Apache Basics 278

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 11.1 What is Apache? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 11.2 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 11.3 How Apache Listens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 11.4 Configuration File(s) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 11.5 Key Configuration Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 11.6 ServerRoot, DocumentRoot . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 11.7 ServerAdmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 11.8 BindAddress, and Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 11.9 Listen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 11.10 User and Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 11.11 Apache Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 11.12 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 11.13 Customizable Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 11.14 CustomLog examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 11.15 Example Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 11.16 Basic Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 11.17 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

12 Apache 297

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 12.1 Two sites and more . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 12.2 Virtual Hosting Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 12.3 Name-based hosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 12.4 Name-based hosting (continued) . . . . . . . . . . . . . . . . . . . . . . . . 301 12.5 Block Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 12.6 Block Directives (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 12.7 DirectoryMatch, et al. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 12.8 Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 12.9 Access Control (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 12.10 Authorisation Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 12.11 Authorisation Files (continued) . . . . . . . . . . . . . . . . . . . . . . . . . 308 12.12 Other useful directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 12.13 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 12.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 12.15 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312

13 Key Configuration Files 314

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 13.1 /etc/passwd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 13.2 /etc/passwd (continued) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 13.3 Editing /etc/passwd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 13.4 Other Changes To /etc/passwd . . . . . . . . . . . . . . . . . . . . . . . . . 318 13.5 /etc/group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 13.6 Editing /etc/group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 13.7 Important Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 13.8 Shadow Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 13.9 /etc/shadow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 13.10 Scheduling Jobs (Cron) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 13.11 /etc/crontab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 13.12 run-parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 13.13 logrotate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 13.14 Module Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 13.15 Modules Configuration — ‘Options’ . . . . . . . . . . . . . . . . . . . . . . . 329 13.16 Mounting Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 13.17 Runlevels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 13.18 Single User Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 13.19 Multi User Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 13.20 Starting up and Shutting down . . . . . . . . . . . . . . . . . . . . . . . . . 334 13.21 Changing runlevel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 13.22 Initscripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 13.23 rcn.d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 13.24 Initscripts — An example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 13.25 Restarting Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 13.26 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 13.27 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341

Module 1

Overview

Objectives

Having completed this module, you will have an overview of a Linux system, including its:

• Underlying philosophy

• System layering — kernel vs. applications • Core services

• Multiuser and timesharing facilities

• File System

• Network Services

• Desktop and X windowing system 1.1 Generic Features of Unix

• Component-based systems

• Very popular with technically skilled

• Not ‘solution’ oriented

• Building blocks not the building

• Highly network-aware

• Robust, powerful, reliable 1.2 Linux — The Kernel of a System

Compilers  ^{DBMS}  ^{Text}^{Editors}  ^{Mailers}  ^{Graphics}^{Apps} Shells & Utilities  X Windowing System Kernel Hardware

Figure 1.1: kernel-layering

• What is called Linux is actually a collection of components from many sources

– freely copiable, under ‘open source’ licences

• Linux is, strictly, just the kernel which provides:

– A common interface between user process and hardware

– Minimal functions to user applications, i.e. system calls

– Scheduling 1.3 Fundamental Characteristics of Linux

• Multi-tasking

• Multi-user access

• Multi-processor

• Architecture independence

• POSIX 1003.1 plus basic System V and BSD

• Protected memory mode

• Multiple filesystem types

• Comprehensive networking (TCP/IP and others)

• Multiple executable formats (MS-DOS, iBCS UNIX, SCO, etc)

1.4 Multiuser Multitasking and Time-sharing

• Designed as a multi-user system

– Each user’s shells, apps and commands are separate processes

– Number of simultaneous users limited only by: ∗ CPU speed and available memory

∗ Min. response times required by users/apps • Multi-tasking:

– Many jobs can be under way at the same time

– Jobs truly simultaneous on multi-cpu

• Time-sharing: A single cpu is shared by all processes

– Processes exec briefly, passing cpu to others

– Process switches occur in miliseconds or less

– Kernel gives process a sense of total control 1.5 Protected memory mode

• Uses the processor’s protection mechanisms

• Prevent access to memory already allocated to kernel or other processes

• Bad programs can’t crash the system – Theoretically 1.6 Multiple Filesystem Types

• Native FS is ext2 (Second Extended File System) – File names up to 255 chars

– More secure than conventional UNIX

• Others include:

– MS-DOS (FAT16), VFAT, FAT32

– ISO9660 (CD-ROM)

– HPFS (OS/2)

– NTFS (Windows NT)

– UPS, SysV and other proprietory UNIX

– NFS (Unix network file system)

– SMB / CIFS (MS Windows file sharing)

1.7 The Many Faces of a GNU/Linux System

• The user may see up to five aspects of Linux: – the filesystem

– processes

– the shell

– the X windowing system

– Inter-Process Communication (IPC)

• The system is very highly configurable

• Different users may experience totally different views of the same system

• Multiple simultaneous users are normal

– Linux is designed from the ground up as a multi-user system, NOT a ‘personal’ system 1.8 The Filesystem

• The filesystem contains all data in the system

• A name in the filesystem can refer to:

– a data file, which can be:

∗ a plain file

∗ a directory

– a device (disk, tape etc.)

– internal memory

– OS information (the proc system)

• Directories are groups of files – Grouped in hierarchical trees

• Files are fully specified with their pathname

• An original Unix structure; copied by most OSs 1.9 Filenames

• Maximum length depends on filesystem type – Most allow up to 255 characters

• Can use almost any character in a filename, but avoid ambiguity by sticking to:

– (A-Z) Uppercase letters

– (a-z) Lowercase letters

– (0-9) Numbers

– (.) Full-stop

– (,) Comma

– ( ) Underscore

– (-) Hyphen

• Should convey meaningful info about contents

• Type longer filenames using completion for: – Filenames

– Pathnames

– Commands 1.10 Filename Extensions and File Types

• Filenames don’t determine other attributes of file, i.e. do not, automatically, cause command interpreters to treat them in a particular way

• However:

– Extensions can enable meaningful naming and automatic file manipulation

– C compilers and some other programs do depend on specific file extensions to carry out particular tasks

• Common conventions for extensions:

Filename Meaning of Extension
program.c C programming source file
program.o Object code
program.sh Shell executable
letter.txt  Text file of a letter
letter.ps  Postscript version of same letter file
letter.ps.gz gzip compressed version of same
letter.tgz  tar archive of same compressed by gzip
letter.tar.gz Another, more common, way of naming *.tgz
letter.Z  Same file compressed with outdated compress utility

Table 1.1: Common conventions for filename extensions

1.11 Hidden Filenames

• Filenames beginning with a full-stop are hidden

• Typically used:

– To hide personal configuration files

– To avoid cluttering dirs with rarely used files

• Every dir contains 2 special hidden files:

. The current directory file
.. The parent directory file

1.12 The Shell (bash)

• A shell is a program that you interact with

• Can be any program, but is normally a command interpreter

• A command interpreter is usually started when you log in (but this is just one way)

• The ‘standard’ Linux command interpreter is a Bourne shell look-alike called bash^{1}

• The command line syntax provided by bash enables manipulation of files & processes

• The command-line frightens beginners but is the preferred home of the skilled

1Bash has more functions than true Bourne shells; incorporating most of the innovations added by the C and Korn shells. Bash functions and flags differ between implementations of UNIX and Linux. The version of bash in current Linux releases tends to be the most fully functional Bourne shell around.

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

1.13 Key Features of the Bash Shell

• Command history

• Command aliasing

• Shell scripting

• Filename completion

• Command completion

• Command line editing (emacs and vi styles)

• Job control

• Key Bindings

• Directory stacking

• Tilde directory notation

• Help function, e.g.

   $ help history
   history: history [n] [ [-awrn] [filename]]
       Display the history list with line numbers.  Lines listed with
       with a `*' have been modified.  Argument of N says to list only
       the last N lines.  Argument `-w' means to write out the current
       history file;

1.14 Interacting with a Linux ‘Terminal’

• Linux can support any number of ‘terminal’ types

– nowadays, monitor/keyboard combinations

– previously, dumb terminals

– occasionally, printers (debugging servers)

• Most will use the console or a windowed terminal, but if not:

– Linux usually keeps a database of terminal capabilities in /etc/termcap^{2}

– If your terminal type is not recorded in /etc/termcap, you’ll have problems running certain programs e.g.

∗ cursor driven apps (top, linuxconf, vi etc)

– The environmental variable TERM tells programs what terminal type you are using

2AT&T flavours of UNIX use /usr/lib/terminfo to store the same information and Linux can, if necessary.

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

1.15 Software Tools: The UNIX Philosophy

• True UNIX-like systems treat programs as tools

– Each tool should:

∗ Do just one thing well

∗ Be generic (untied to specific applications)

– For new jobs, build new tools

– (Re-)combine, don’t complicate old tools

• Linux can do this because it has:

– two simple objects:

∗ the file

∗ the process

– simple methods of connecting:

∗ processes to files

∗ processes to processes 1.16 Tasks/Processes

• A program is an executable object, stored in a file

• A process is an executing object, i.e. ^{3} – an instance of a program currently being run

• Existing processes can ‘fork’ to create other processes

– the only way to make new processes

• A user may run multiple copies of same program

• Multiple users may run single/multiple copies

• System tracks ownership and permission

3Processes are often called tasks, as in ‘multi-tasking’

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

1.17 Process Communication

• Processes may need to co-operate by – sharing files

– signalling events

– direct transfer of data

– pipelines (data streams)

– synchronising with each other

• Linux provides facilities for:

– signals

– shared memory

– pipes, both named and unnamed

– semaphores

– and others

• Processes may use network connections for communication, permitting client-server model

– Common for shared services like printing 1.18 Re-directing I/O to and from Files

• Most processes will take input from the keyboard and output to the screen

• Both input and output streams can be re-directed to/from files

• Output to a file (creating or overwriting):

  $ ls > my-system.txt

• Appending output to a file: $ who >>

  my-system.txt

1.19 Re-directing I/O to and from Files (continued)

• Take input from one file, output to another: $

   sort < /etc/passwd > pwd.sorted

1.20 Pipes & Tools

• Linux tools act as filters:

– taking data from input streams, modifying it, sending it elsewhere

– expecting data to come from other tools

– producing output which any other tool can

process, e.g. ASCII text

• One tool’s output is connected to another’s input: – Indirectly, via a file created by the first tool

– Directly, via a pipe or pipeline

• For example, to page through a reverse-sorted version of your password file on screen:

$ sort -r < /etc/passwd | less

1.21 Linux as a Programming Environment

• Hierarchical Filestore

• Extensive set of powerful tools – for software production, admin and support

• A common system interface – only one set of procedures to learn

• Processes interface with anonymous files – programs output to files or devices identically

• Modular architecture provides for a completely customised OS, e.g.

– An OS dedicated solely to graphics rendering

– A general-purpose system on one floppy

• Flexible user interface allows for uniquely customised programming environments 1.22 Networking

• Linux is a network operating system.

• The Internet network protocols (TCP/IP) are implemented in the kernel

• Although other media are supported (e.g. radio, infra-red), links are usually across:

– Ethernet

– Serial Line (Point-to-point)

• Proprietory file/print serving protocols supported: – Appletalk

– DECNET

– IPX / Novell Netware

– SMB / CIFS (MS Windows/NT)

1.23 TCP/IP

• A suite of Internet-standard protocols and apps for managing data transfers

• Depicted as a ‘stack’

– hardware and transport control protocols at the bottom

– user applications (e.g. browsers) at the top

• Client-server apps provide facilities for:

– Remote login

– File transfer

– Resource sharing (e.g. expensive peripherals)

– Remote command execution

– Email (internet/intranet/extranet)

– Web browsing 1.24 Documentation

• Copious, but fragmented and/or duplicated

Programmer’s Manual

/usr/man

The classic ‘man pages’, first stop for skilled users, worth learning

info pages hypertext browsable texts, often identical or updated versions of man pages

/usr/doc/program-name ascii/html docs installed with the named program Howtos Tutorials on Linux-related topics, available on-line if installed (usually in /usr/doc)

www Recently-released programs are usually documented on authorised web sites, many (including older tools) are documented by third-party sites

Table 1.2: Sources of Linux Documentation

• Linux man pages divided into sections:

1. User Commands

2. System calls

3. Subroutines (inc library routines)

4. Devices (inc network interfaces)

5. File Formats

6. Games

7. Miscellaneous

8. System Administration

• The apropos command word searches the description line in man pages. Thus:

apropos printer

will find man pages relating to printers, e.g.

lp (4)   - line printer devices
lpd (8)  - line printer spooler daemon
lprm (1) - remove jobs from the line printer spooling queue

1.25 Using the man pages (On-Line Manual)

• Use man to see man pages on a named command, e.g

  $ man date

• The result should be something like:

  DATE(1)
  NAME date - print or set the system date and time
  SYNOPSIS
  date [-u] [-d datestr] [-s datestr] [--utc]
  [--universal] [--date=datestr] [--set=datestr]
  [--help] [--version] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]

• DATE(1) Shows page is in manual section 1

• To view a page from a certain section use:

  $ man -S section-number command-name

• Square brackets surround optional arguments

  ls  [-abcdfgiklmnpqrstuxABCFGLNQRSUX1]

1.26 Overview Exercises

1. Logging in

(a) Practice logging in by typing your username and password in response to the Login: and Password: prompts, e.g. ^{4}

Login:  mikeb
Password:
$

Once you have logged in, the bash prompt ($) is printed; indicating the shell is ready to take commands.

(b) Log out, by typing exit at the $ prompt. You should get a new Login: prompt, at which you can login again.

2. Changing password

(a) Set yourself a new password using the passwd command. Run the command by typing passwd, followed by a <RETURN> .

3. Navigating Man Pages

(a) Type man man to open the man page which details how to use the man command

(b) Press the h (help) key, which opens a “Summary of Less Commands”, including all the keystrokes you need to navigate a man page

(c) Make sure you can quit this page (by typing q) and quit the man page (by typing q again). When you get back to the shell prompt, repeat the first 2 steps to open the “Summary of Less Commands” from the man man page.

(d) Use the “Summary of Less Commands” to make sure you know how to do the following bits of navigation inside a man page:

i. Move to the top and bottom of the man page ii. Move up and down one screen of text iii. Move up and down one line of text iv. Search forward for a pattern (e.g. a word) v. Search backwards for a pattern vi. Repeat a forward pattern search using one key vii. Repeat a backward pattern search using one key viii. Move to a specific line number

(e) With a partner, test each other on how well you can navigate the man man page, e.g. set each other target locations or words to go to.

4. Invoking the Right Man Pages

(a) Using the man man page, find the command string you need to use to get the following:

i. A list of man pages whose description lines contain details about the ‘whatis’ database

4A <RETURN> is required after each input, to tell the shell that you have finished typing and it should start processing your request. Note that the password is not displayed on the screen; to keep it secret

ii. A list of man pages containing the string ‘cdrom’ ^{5} iii. A list of man pages from a specific section (e.g. 1) of the manual, whose

description lines contain ‘print’

(b) Practice using these flags to find and view man pages which deal with computer

keywords your partner sets for you (and vice versa), e.g. i. bitmap formats like jpg, gif, xpm, bmp ii. communications concepts like modem, serial, telnet, pcmcia, ppp iii. filesystems like NFS, ext2, FAT, vfat, msdos, samba

5. Finding Out About Your System and Users

(a) Type the following commands. Identify what each of them tells you about your

system.

  i. $ whoami
 ii. $ who am i
 iii. $ users
 iv. $ who
 v. $ w
 vi. $ date
vii. $ cal 8 1999
viii. $ cal 9 1752 ^{6}
 ix. $ df
 x. $ which man
 xi. $ type man
xii. $ whereis less
xiii. $ help cd
xiv. $ time sleep 2

(b) Use the appropriate man page, to check that you have interpreted the screen output correctly

6. Creating New Files

(a) Try creating a new empty file in your home directory using the touch command, e.g.

$ touch filename

(b) Get the file details on filename using this command:

$ ls -l filename

(c) Wait 1 minute, then repeat the previous two steps, i.e.

$ touch filename
$ ls -l filename

i. Which of the file details have changed? ii. What does this tell you about the purpose of touch? Check the man page if

you are unsure.

(d) Creating new files using re-direction

5Actually running this sort of search can take a long time, given that many systems contain over 500 man pages, some of which are very long. 6You should notice something very strange about the output from this string. The cal utility is perfectly functional, so what’s wrong?

i. Create a new file containing the output from the df command, using re-direction, e.g.

       $ df > diskspace.txt

ii. Ask a partner to create new files, with appropriate filenames, containing output from the commands used in the questions on “Finding Out About Your System and Users”.

7. Appending information to files

(a) With a partner, choose several of the system information commands whose outputs may have changed since you completed the previous question. Practice appending the updated information to the file which contains the earlier output.

(b) Create a file containing output from w, then append the output from date to it, i.e. time-stamp the output data.

8. Using Simple Pipes

(a) Pipe the output from who through the sort command to reverse its order.

(b) Sort your /etc/passwd file alphabetically and send the output to a new file

   (passwd.sorted).

(c) Find out what wc does from its man page, then use it at the end of a pipe to analyse the output from other utlities.

(d) Repeat the last step, limiting wc to counting words only

1.27 Overview Solutions

2. Changing password

(a) Responses will vary from system to system, depending on whether or not good password practice is enforced.

3. Navigating Man Pages

(b) It is possible that some Linux distributions won’t use less to display man pages. If that is the case, try to find out how you navigate under that setup and answer the same questions about it.

(d) Keystrokes for basic man page navigation:

Instruction  Keystroke(s)
Top of man page  g < ESC-<
Bottom of man page  G > ESC->
Forward one screen  f ^F ^V SPACE
Backward one screen  b ^B ESC-v
Up one line  y ^Y k ^K ^P

Down one line  e ^E j ^N RETURN

pattern Search forward  /pattern
pattern Search backward  ?pattern
Repeat pattern Search forward  n
Repeat pattern Search backward N
Move to nth line  ng

Table 1.3: Keystrokes for basic man page navigation

N.B. Several different keystrokes can be used for the same movement. This is common in UNIX tools designed to operate from any keyboard. less always has a single key method. Multi-key methods are shown without spaces between them.

4. Invoking the Right Man Pages

(a) i. $ man -k whatis

or, slightly differently:

$ man -f whatis
ii. $ man -K cdrom

iii. There is no easy way to do this yet. Later on you will learn about grep which will allow you to filter the output of man -k print to see only the information you require.

(b) Practice using these flags to find and view man pages which deal with computer keywords your partner sets for you (and vice versa), e.g.

i. e.g. $ man -K jpg
ii. e.g. $ man -K modem
iii. e.g. $ man -K NFS

5. Finding Out About Your System and Users

(a) The listed command strings tell you about:

(b) See Table 1.4

6. Creating new files Command string

Output

$ whoami

Your username

$ who am i

Your username plus machine(s) and terminal you are on

$ users

Usernames of currently logged on users

$ who

Who is logged on, when and where

$ w

Who’s logged on, when, where, what process and what system resources they are using

$ date

Current date and time, can set date/time

$ cal 8 1999

Calendar for August 1999

$ cal 9 1752

Calendar for September 1752. Strange because 12 days were ‘lost’ in the transition from Gregorian to Julian calendars

$ df

Disk free, i.e. summarises disk usage

$ which man

Full file and path name for the man executable file

$ type man

Much the same as which man

$ whereis less Locates the less executable and its man page

$ help cd

Very brief help notes on the cd command. N.B. help only works on very few built-in commands

$ time sleep 2 The sleep command puts itself to sleep for 2 seconds. The time command then times the whole process and provides other data on the operation of the sleep command

Table 1.4: Output from basic system information commands

(a) Your output should be something like:

$ touch filename.txt

(b) Your output should be something like:

$ ls -l filename.txt
-rw-rw-r--   1 davef  davef  0 Jul 21 17:59 filename.txt

(c) Your output should now be something like:

$ touch filename.txt
$ ls -l filename.txt
-rw-rw-r--   1 davef  davef  0 Jul 21 17:59 filename.txt
$ touch filename.txt
$ ls -l filename.txt
-rw-rw-r--   1 davef  davef  0 Jul 21 18:01 filename.txt

i. The time stamp has changed ii. The real purpose of touch is to change time stamps, but it is handy for

creating new empty files

(d) i. Reading diskspace.txt should produce something like this:

$ cat test.txt
Filesystem     Used Available Capacity Mounted on
/dev/hda1     65571   406394     14%   /
/dev/hdc1   5030416   650563     89%   /backup
/dev/hda5   2000097   857401     70%   /home
/dev/hda7     14289   457676      3%   /tmp
/dev/hda6   1136861   741727     61%   /usr
/dev/hdb     653004        0    100%   /mnt/cdrom

7. Appending information to files

(b) Your screen should look something like this:

$ w > test.txt
$ date >> test.txt
$ cat test.txt
  6:36pm  up 16 days, 23:07,  4 users,  load average: 1.03, 1.08, 1.02
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU  WHAT
davef    ttyp7    oakleigh:0.0      9:39am  0.00s  2.58s  0.02s  w
mikeb    ttyp4    kebab            Tue 3pm 23:07   0.20s  0.13s  -bash
davef    ttyp2    oakleigh          9:04am 15:34   7:24   0.07s  -bash
davef    ttypb    oakleigh          3:02pm  3:15m  3:00m  0.08s  -bash
Wed Jul 21 18:36:39 BST 1999

8. Using Simple Pipes

(a) $ who | sort -r
(b) sort /etc/passwd > passwd

or

$ cat /etc/passwd | sort > passwd.sorted

(c) wc prints the number of lines, words, and bytes in files. To get these details for your /etc/mime.types file, you could do the following:

$ cat /etc/mime.types | wc
    291     524    7751

i.e. 275 lines, 488 words, and 7373 bytes ^{7} Another way of doing the same thing without a pipe is

$ wc /etc/mime.types
    291     524    7751 /etc/mime.types

(d) E.g.

$ cat /etc/mime.types | wc -w
    524

We can get a similar result by typing:

$ wc -w /etc/mime.types

7N.B. wc only counts whitespace-separated words

Basic Shell

Objectives

On completion of this module, you should be able to understand and use the Linux shell to create and combine tools. Topics covered include:

• An overview of the command line

• The software tools model

• File names and types

• Shell programming

• Command scripts

• Job control

• I/O — pipes and redirection 2.1 Introduction

• The standard command line interpreter under

Linux is bash (/bin/bash or /bin/sh)

• An enhanced version of the classic Bourne shell

• Shares most features of other shells (C, Korn, etc) and has some more advanced features

– ‘Plumbing’ — transparent redirection and pipes

– Background processes

– Process suspension, resumption, termination

– Filename completion and wildcard generation

– History 2.2 Getting around the command line

• You can use the cursor keys to move around and edit the current line^{1}

• By default, bash uses emacs-like keystrokes for navigation and editing. Here are 4 examples:

Keystroke  Action

   ^a

Move to the beginning of the line

   ^e

Move to the end of the line

   ^k

Delete to the end of the line

   ^w

Delete the previous ’word’

• To choose emacs or vi-like keystrokes:

  $ set -o emacs
  $ set -o vi

• bash man page gives details of all keystrokes

1This may not work on badly-configured systems

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

2.3 History

• Bash remembers used commands (in a ‘history’)

• Old commands are retrievable in different ways

• Repeat the previous command by typing !!

• Execute the nth previous command by typing !-n

• Typing !string repeats the last command beginning with string

• To view your history command by command, use the up and down cursor keys

• View your history at any time by typing history

• History is a very useful feature, if used well – Incrementally searchable using CTRL-R 2.4 Plumbing

• Processes typically start with three files open:

Name  Descriptor

Standard input 0 Standard output 1 Standard error 2

• Later we see how to refer to their file descriptors

• These are normally connected to the keyboard and your command-line terminal 2.5 Plumbing (continued)

• Data can be redirected by the shell – Transparently to the process concerned

– Any or all streams can be redirected

– You can redirect to/from a file or to/from

another process

• Redirection to a process is known as ‘piping’ 2.6 Output Redirection

• Redirection of output is done using ’>’

• For example:

$ command > output

Creates the file output (or overwrites it if it already exists) and places the standard output from command into it

• We can append to a file rather than overwriting it by using >>

• > and >> are actually shorthands for 1> and 1>>

• Error output can be redirected using 2> or 2>>

$ command 2> error-out

2.7 Input Redirection

• < redirects standard input from a file, e.g.

  $ command < input

• command will now take the contents of the file input as its input

• This could also be written as 0<

• Consistent with > and 1> 2.8 Combining Redirection

• Redirect more than one descriptor by giving more than one redirection, e.g.

$ command 1> output 2> error

• Group redirections using the >& operator, e.g.

$ command 1> output 2>&1

– Output to the file called output ( > output )

– Send errors to the same place as the standard output ( 2>&1 )

• The order of these is very important

• The redirections are evaluated left-to-right, e.g. the following differs from the previous example

$ command 2>&1 > output

– It sends error to the normal output and normal output to the file called output 2.9 Pipelines

• You can output to another process with ‘|’ – Known as the pipe symbol

• A pipe connects the output of one process to the input of another

• The data waiting to be transferred is buffered

• The processes run concurrently

• Linux ensures that the processes keep in step

• For example:

  $ sort document | uniq | mail lee

2.10 Background Processes

• Most commands run to completion before you get your shell prompt back

• A ‘background’ process continues while you get your prompt back immediately

• To launch a process in the background place & at the end of the line, e.g.

$ sort /var/log/maillog > output &

• Unless you use redirection (plumbing), output and error continue to appear on your terminal

• Input is disconnected, so typing goes to the shell, not to the background process

• If a process needs user input, and can’t take it from a file, it is ’stopped’

– It won’t resume until brought to the foreground to receive input

• You should normally start background processes with their output and error redirected to a file, e.g.

$ sort big_file > output 2> error_output &

2.11 Background Processes (continued)

• Running processes can be put in the background

– Suspend the process by typing ^Z in the terminal that the process is running in

– Put the process in the background using bg

• Bring a process back to the foreground using fg

• fg and bg operate on the most recent process by default

– Change to a process, by job number or name

• jobs displays current shell processes:

  $ jobs
  [1]+  Stopped (tty output)    top
  $ fg %1

2.12 Background Processes and nohup

• Sometimes it is necessary to start a process and leave it running when you log out

• If your shell is killed, any background processes will also be lost

• nohup gets round this by detaching the process from the terminal

• Always redirect output and error with nohup, e.g.

  $ nohup sort bigfile > out 2>&1 err.out

• If you don’t redirect them then they will end up in

  ./nohup.out and ./nohup.err

2.13 Command Grouping and Sub-shells

• bash can execute multiple commands on a line

• Sequential commands are separated by ‘;’

  $ sort data ; mail lee < sorted_data

• It’s possible to launch a sub-shell to execute a command or group of commands

– Put commands in parentheses, e.g.

    $ (command1 ; command2)

• Can also put a subshell in background, e.g.

  $ (command1 ; command2) &

2.14 Process Management

• ps (process status) prints info about a users’ processes:

  PID   TTY STAT TIME COMMAND
  22074 p0  S    0:02 Eterm -t trans
  22075 p0  S    2:13 emacs -bg black
  22081 p0  S    0:00 asclock
  22590 p5  R    0:00 ps

• jobs only prints info about processes belonging to the current shell

• wait postpones shell until process is finished – Usually given a process id as an argument

– If no argument is given it waits until all the

shell’s processes have terminated

• kill is used to send signals to processes – Can terminate background processes

• Some processes use signals to trigger tasks, e.g. log rotation, re-reading config files, etc 2.15 Signals

• kill can be given a signal name or number

• There are a variety of signals :

SIGHUP 1 Hangup detected on controlling terminal or death of controlling process

SIGINT 2

Interrupt from keyboard

SIGQUIT 3

Quit from keyboard

SIGKILL 9 Kill signal

SIGTERM 15

Termination signal

SIGUSR1 30

User-defined signal 1

10 16

SIGUSR2 31

User-defined signal 2

12 17

2.16 Signals (continued)

• Unless specified, kill sends a SIGTERM which causes most processes to terminate

• If a process is unresponsive, it can be forcibly killed by sending it SIGKILL

  $ kill -9 1512
  1512: Terminated

or

  $ kill -KILL 1512
  1512: Terminated

• Can only signal your own processes – Superuser can signal all 2.17 Background Processes: top

• top displays the processes running on a machine

• Results can be sorted in various ways

• Options:

– See man top for full details, including command-line options

– Inside top use h for help on interactive options

• Typical output:

PID USER   PRI  NI  SIZE  RSS SHARE STAT LIB %CPU %MEM  TIME COMMAND
22594 user    10   0   736  736   556 R     0  8.2  0.5   0:00 top
  1 root     0   0   144   96    76 S     0  0.0  0.0   0:03 init
  2 root     0   0     0    0     0 SW    0  0.0  0.0   0:19 kflushd
  3 root   -12 -12     0    0     0 SW<   0  0.0  0.0   2:42 kswapd
486 root     5   5  3160 2356   804 S N   0  0.0  1.8   0:03 mysqld
869 root     0   0    68   12    12 S     0  0.0  0.0   0:00 mingetty
838 www      0   0 11468 6280   488 S     0  0.0  4.9   4:14 squid
 48 root     0   0   100   80    48 S     0  0.0  0.0   0:01 kerneld
230 root     0   0   384  372   272 S     0  0.0  0.2   0:12 syslogd
239 root     0   0   164  120    72 S     0  0.0  0.0   0:00 klogd
250 daemon   0   0   164  132    88 S     0  0.0  0.1   0:00 atd
261 root     0   0   192  160   112 S     0  0.0  0.1   0:01 crond
272 bin      0   0   244  224   168 S     0  0.0  0.1   0:00 portmap
283 root     0   0   572  296   248 S     0  0.0  0.2   0:17 snmpd
295 root     1   0   136   88    60 S     0  0.0  0.0   0:00 inetd
306 root     0   0   516  488   224 S     0  0.0  0.3   0:06 named
317 root     0   0   124   56    48 S     0  0.0  0.0   0:00 lpd

• N.B. top is not available on all unices 2.18 Filename Generation

• Some characters are ‘special’ to the shell

Chars Meaning

*

Matches any string, including the null string

?

Matches any single character

[...]

Matches any one of the enclosed characters. A pair of characters separated by a minus sign denotes a range. Any character lexically between those two characters, inclusive, is matched. If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched. A ‘-’ or ‘]’ may be matched by including it as the first or last character in the set.

Table 2.1: Special characters under bash

• Special characters can be used to match filenames, e.g. to show files beginning with f

$ echo f*

• To show files starting with ‘f’, followed by a vowel:

$ echo f[aeiou]*

2.19 Quoting Mechanisms

• Sometimes it’s necessary to ignore a character’s special meaning

• Use a backslash (\) to quote a special character, e.g. to list a file called f*

  $ ls f\*

• To quote a longer string, enclose it in quotes :

' disable all interpretation

  "

disable filename generation and blank space interpretation 2.20 Shell built-in commands

• Some commands must be built in to the shell, because they can’t be executed independently

– cd, if executed independently would change its own directory, not that of your shell

– umask, would change its umask, not the shell’s

– logout
– history

• Other commands are built in for speed e.g.

– pwd
– echo

2.21 Basic Shell Exercises

1. Redirection

(a) Try typing the following commands exactly as they appear here:

$ cat > newfile 2> newfile.error
$ car > newfile 2> newfile.error
$ cat > newfile 2>&1
$ car > newfile 2>&1
$ cat < newfile
$ echo foo | cat > newfile 2>&1
$ ech foo | cat > newfile 2>&1
$ echo foo | car > newfile 2>&1
$ (ech foo | cat) > newfile 2>&1

Make sure you understand what happens in each case, ask the tutor if you are note sure.

2. Filename expansion and Quoting

(a) Do the following in the /bin directory

i. List all filenames with exactly three characters. ii. List all filenames with exactly three characters in which the second

character is a vowel. iii. List all filenames with a, b, c, or d as the last character. iv. Construct a command to print the number of filenames consisting of exactly

three characters. v. Construct a command to print the total number of files with exactly two,

three or four characters in their name. (You may find the wc utility useful here, check man wc for more information.)

(b) Compare the effect of the following commands:

echo $HOME
echo "$HOME"
echo '$HOME'
echo *
echo "*"
echo '*'
echo $HOME/*
echo "$HOME/*"
echo '$HOME/*'

(c) Change back to your home directory and try to create a file with the name *. Was this a sensible thing to do? How would you delete it? (Be very careful!)

(d) Create a file called --file. Try to remove this. Use the rmman page to help you.

3. Background processes and nohup

(a) Start the command sort /dev/random in the background in your current shell

(b) Bring it back to the foreground and terminate it by typing ^C

(c) Start it again, and once more so that you have two copies running in the background (d) Bring them to the foreground and terminate them in the order you started them

(e) Start the same command in the background, and terminate it using kill

4. Grouped commands

Compare the following command sequences, and make sure you understand the differences :

(a) cd /tmp
   cd /usr; ls
   pwd
(b) cd /tmp
   (cd /usr; ls)
   pwd
(c) sleep 5; sleep 5 &
(d) (sleep 5; sleep 5) &

Check you can use your history to get at and repeat any of the commands you have typed.

2.22 Basic Shell Solutions

1. N/A

2. Filename expansion and Quoting

(a) These are solutions which will do the job, there may be other ways of acheiving the same thing

i. ls ??? ii. ls ?[aeiou]? iii. ls *[abcd] iv. ls ??? — wc -l v. (ls ?? ; ls ??? ; ls ????) — wc -l

(b) When not quoted $HOME gives the name of your home directory. This is variable substitution. We can see that this substition still happens inside ” quotes, but not inside ’ quotes. echo * expands to all the filenames in the current directory. This is filename generation and doesn’t happen in either quotes.

(c) It is not a wise choice to name anything with a filename containing special characters. You can delete the file safely using either:

  • rm "*"
  • rm '*'

(d) You can delete the file by using rm -- --file

3. Background processes and nohup

(a) $ sort /dev/random &
(b) $ fg
^C
(c) $ sort /dev/random &
[1] 26409
$ sort /dev/random &
[2] 26410
(d) $ jobs
[1]-  Running                 sort /dev/random &
[2]+  Running                 sort /dev/random &
$ fg %1
sort /dev/random
^C
$ fg %2
sort /dev/random
^C
$
(e) $ sort /dev/random &
[1] 26462
$ kill -9 26462

4. N/A

Basic Tools

Objectives

At the end of this section, you will be able to:

• Use the most frequently used Linux tools to: – Find files

– Get information about commands

– View file contents

– Get information about files

– Operate on file contents

– Do simple text manipulation

– Schedule jobs

• Combine tools to solve problems

• Understand and use the Linux printing subsystem 3.1 Introduction

The basic Linux command-line utilities dealt with here, are:

Finding files  find

locate

Getting info about commands man
Viewing file contents  cat

less
head/tail

Getting information about a file ls

file
wc
diff
cmp

Operating on file contents  grep

sort
uniq
split/csplit
tar
gzip/bzip2

Simple text manipulation  tr

expr

Scheduling jobs  at

crontab

Table 3.1: Basic Linux utilities

3.2 Using Tools

• Typical Linux systems contain over 400 command-line tools

• Tools are combined (via pipes and redirection) to solve specific problems

• Most tools have a standard syntax:

    $ command [options] [files ...  ]

• Some arguments must be quoted

• Standard input often read if no filename given

• Most tools can take several filename arguments

• Desktop/windowing environments may provide graphical wrappers to some tools

• Serious Linux administrators and users know the key command-lines well

• The terms ‘command’ and ‘tool’ are used interchangeably here 3.3 The On-Line Manual (man)

• Most commands have an associated man page

• Accessed by typing:

$ man command[s]

• Brings up a page of information usually detailing: – command name, section number, description

– syntax

– options

– version information

– location of configuration files

– other related commands

– examples of usage

– known bugs (if any …)

3.4 Finding Files the Long Way (find)

• find searches the filesystem in real time;

– Makes disks work hard

• Can find files by name, type, size, dates, e.g

– To find all files ending with .jpg under the current directory:

find .  -name "*.jpg"

– To find all filenames ending in .jpg and modified in the last 8 days below /etc

find /etc -name "*.jpg" -mtime -8

• Tests can be combined with -o and negated with !, for example:

– To find all filenames not ending in .jpg or modified in the last 8 days under /etc

find .  !  -name "*.jpg" -o -mtime -8

• Can execute commands on the files it finds. The name of the file found is placed in {} ^{1}

find . -name "*.gif" -exec ls -l {} \;

1This is not a resource friendly way of doing things. xargs may be better

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

3.5 Locate Files (locate)

• locate searches a periodically-updated database of the filesystem(s)

– Not available on all systems

– Very fast, but DB needs regular updating, e.g.

$ updatedb

– Usually updated nightly automatically

• Won’t be there on a fresh install

• Won’t show files created since last database update

• Given the command ‘locate string’, locate will show all files containing string in their full pathname, e.g.

$ locate logrotate
/usr/man/man8/logrotate.8
/usr/sbin/logrotate
/etc/logrotate.d
/etc/logrotate.d/cron
/etc/logrotate.d/syslog
/etc/logrotate.d/linuxconf
/etc/logrotate.d/ftpd
/etc/logrotate.d/samba
/etc/cron.daily/logrotate
/etc/logrotate.conf

3.6 View and Concatenate Files (cat)

• Displays and/or joins (con catenates) files

• Sends the content of named file(s) to standard output

• If no filename is given, it reads from standard input and writes to standard output

• Given more than one filename, it displays each file’s contents sequentially, i.e, joins them

• Example:

    $ cat file1 file2 file3 > all files

3.7 View Large Files & Output (less)

• less displays the contents of file(s) in a controlled way on stdout

– Usually, one page at a time

– Like UNIX/DOS command more, on steroids

• You can search for patterns in the file

• It allows you to move quickly to any point (backwards or forwards)

• Similar usage to more, vi, and lynx:

Action  Keystokes
Top of page  g < ESC-<

Bottom of page

                             G > ESC->

Forward one screen f ^F ^V SPACE Backward one screen

                             b ^B ESC-v

Up one line  y ^Y k ^K ^P

Down one line  e ^E j ^N RETURN

pattern Search forward

                             /pattern

pattern Search backward

                             ?pattern

Repeat pattern Search forward

                             n

Repeat pattern Search backward

                             N

Move to nth line

                             ng

!command Execute the shell command with $SHELL

—Xcommand Pipe file between current pos & mark X to shell command

v Edit the current file with $VISUAL or $EDITOR

Table 3.2: Commands within less

3.8 Viewing Parts of Files (head and tail)

• head displays the first few lines of a file

• tail displays the last few lines of a file

• You can specify how many lines are displayed

– To display only the first 4 lines:

    $ head -4 filename

• tail -f often used to monitor growing files 3.9 Listing File Information (ls)

• Without any options, ls lists files in the current directory

• By default all files starting with . (dot) aren’t shown

• The most common options to ls include:

Flag Option

-l Long (detailed) listing of file info, including: size,

ownership, permissions and type -a Show all files, including hidden ones -F Highlight directories and executables with / and @

respectively -R Recursively list subdirectories -t Sort list by last modification time -u Sort list by last access time (with -t) -X Sort list by file eXtension -r Reverse order of listing -d Show directory information not directory contents

Table 3.3: Common options to ls

• For example:

  $ ls -lrt

show files in reverse order based on their modification time 3.10 File Classification (file)

• file displays the type of data contained in named file(s)

• Results not always correct

• Uses list of magic numbers and keywords in /etc/magic to determine file type ^{2}

• Classifications include: executable, archive, C program, ASCII text, JPEG image …

• Syntax:

  file [-vbczL] [-f filename] [-m magicfiles]
  file...

2The magic numbers file can be /usr/share/magic on some systems

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

3.11 Count Words, Lines, Characters (wc)

• wc displays the number of lines, words,^{3} and characters in a file

Flag Option

-l Only displays the number of lines -w Only displays the number of ‘words’ -c Only displays the number of characters

Table 3.4: Options to the wc command

3A ‘word’, in this context, is a character string surrounded by SPACEs, TABs, NEWLINEs, or a combination of them.

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

3.12 Differences Between Files (diff)

• diff displays the difference between two text files, line-by-line

• Output from diff can be confusing

• For example, given the files text1 and text2:

text1: This is a temprary test to check the diff utility

text2: This is a temporary test to check the diff utility.

1. A simple line-by-line comparison:

$ diff text1 text2
1c1
< This is a temprary test
---
> This is a temporary test
3c3
< utility
---
> utility.

2. Using the context output format (-c):

$ diff -c text1 text2
*** text1       Mon Apr 19 14:46:25 1999
--- text2       Mon Apr 19 14:46:05 1999
***************
*** 1,3 ****
! This is a temprary test
  to check the diff
! utility
--- 1,3 ----
! This is a temporary test
  to check the diff
! utility.

3. Using the unified output format (-u): (Most common)

$ diff -u text1 text2
--- text1       Mon Apr 19 14:46:25 1999
+++ text2       Mon Apr 19 14:46:05 1999
@@ -1,3 +1,3 @@
-This is a temprary test
+This is a temporary test
 to check the diff
-utility
+utility.

3.13 Compare Binary Files (cmp)

• Displays differences between 2 binary files

• Locates the byte and line number of the first difference

• Can show all differences if required, e.g.

  cmp -l file1 file2

• cmp -s suppresses output and returns exit status – 0 if the files are identical

– 1 if the files differ

– 2 if an error has occurred

• Often used in shell scripts 3.14 Regular Expression Searches (grep)

• Search for regular expressions in file(s)

i.e “ g lobally find regular e xpressions and p rint”

• Usage:

grep [options] search-pattern files

• Reads standard input if no filenames are given

• Matching lines are printed to standard output

• Popular options:

Flag Option
-i   Ignore case

-l List only filenames containing the expression -v Reverse sense of test, i.e. find non-matching

lines -w Word search, i.e. match whole word -E Extended regular expression search (more

complex patterns), egrep similar -F Fixed string pattern search, same as fgrep

Table 3.5: Popular grep options

3.15 grep examples

• Search for a user in the password file

$ grep lee /etc/passwd
lee:x:500:500:Lee Willis:/home/lee:/bin/bash

• Search for events in a log

$ grep connect /var/log/secure
Mar  8 14:42:53 rafters in.telnetd[579]: connect from 192.168.0.129
Mar 13 10:27:40 rafters in.telnetd[724]: connect from 192.168.0.139
Mar 13 16:54:24 rafters in.telnetd[2135]: connect from 192.168.0.139
Mar 20 10:54:38 rafters in.telnetd[816]: connect from 127.0.0.1

• Search for a function in source code

$ grep "int main" *.cpp
checkrefint.cpp:int mainbit();
checkrefint.cpp:int mainbit(){
do_maint.cpp:int maint(Form &Myform){
expire.cpp:int main(void){
maint_login.cpp:int maint_login(Form &) {

3.16 Sort and Merge Files (sort)

• sort and/or merge files

• Acts as a filter without file arguments

• Sorts entire lines lexically, by default

• Alternative sort orders:

Flag Option

-n Numerical order -r Reverse order

Table 3.6: Alternative sort orders

• Other popular options:

Flag  Option

-b Blanks (TAB, SPACE) ignored -f Fold lowercase to upper before sorting -i Ignore non-printable characters -m Merge files, without checking if sorted -tx Set field delimiter in file as x -u Unique, outputs repeat lines once only +POS1 [-POS2] Specify a field in each line as a sorting

key, starting at POS1 and ending at POS2 (or NEWLINE). Fields and character positions are numbered starting at 0

-k POS1[,POS2] Alternative syntax for specifying sorting keys. Positions are numbered starting at 1

Table 3.7: Popular sort options

3.17 sort Examples

Consider /etc/passwd which typically contains lines in the following format:

username:password:UID:GID:Realname:Homedirectory:shell

• To sort by username:

   $ sort -t:  -f +0 -1 /etc/passwd

• To sort numerically by user ID:

   $ sort -t:  -n +2 -3 /etc/passwd

• To sort by real name within group ID:

   $ sort -t:  +3n -4 +4f -5 /etc/passwd

3.18 Display Unique Lines (uniq)

• Removes all but one of successively repeated lines

• Acts on standard input, often piped from sort

• Most popular options:

Flag Option

-c Count duplications and prepend number to each

output line -d Duplicated lines only are displayed -u Unique lines only are displayed -n Ignore the first n fields +n Ignore the first n characters -w Specify the number of chars to compare

Table 3.8: Popular uniq options

• Example:

3.19 Split Files (split)

• Split a file into pieces

• Outputs sections to new files or standard output

• Creates files named prefixaa, prefixab,

prefixac . . .

• Main split options:

Flag Option

-ln Put n lines of the input file into each output file -bn Put n bytes of the input file into each output file -Cn Put as many complete lines of the input file as is

possible into the output file, up to n bytes

Table 3.9: Main split options

3.20 Splitting Files by Context (csplit)

• Splits file into sections determined by context (patterns or regular extressions)

• Syntax:

   csplit [-f prefix] [-b suffix] [-n digits] filename pattern...

• Main csplit arguments:

Argument  Instruction

/regexp/[offset] Split the file at occurrence of regexp. The line after the optional offset (‘+’ or ‘-’ followed by a number) begins next bit of input

{repeat-count} Repeat the previous pattern split n times. Substitute an asterisk for n to repeat until the input is exausted

-fstring Use string as prefix of output filename -bstring Use string as suffix of output filename -nn Use output filenames n digits long

Table 3.10: Main csplit arguments

3.21 Compression Utilities (gzip)

• Linux has many compression utilities; gzip dominates thanks to integration with tar

• Takes files or standard input and compress them to file(s) or standard output

• Uses lossless compression, so safe on any file

• Key gzip options:

Flag  Option

-r Recursive compression of subdirectories -d Decompress (same as gunzip) -[1-9] Fast or best compression, where 1 is fastest

and 9 is most intense compression

Table 3.11: Key gzip options

• bzip2 has better compression ratios and is now becoming widely used for distributing software.

3.22 Store and Retrieve Archives (tar)

• Originally designed to make tape archives

• Takes a group of files and creates one big file containing their contents and details

• Widely used for:

– Compression with gzip^{4}

– Maintaining Linux file details (permissions, dates, ownership etc) on inferior filesystems

– Bundling file trees for distribution (called a tarball)

• Key tar options:

Flag Option

-c Create a new archive -r Append files to an existing archive -x Extract the contents from an archive -z Create/Open gzip compressed tar file(s) -f Filename of the file or device to hold the archive -P Pathnames are absolute

Table 3.12: Key tar options

• To create a gzip compressed archive of /etc:

  $ tar -czf /home/username/filename.tar.gz /etc

• To extract the file in the current directory:

  $ tar -xzf filename.tar.gz

4The gzip compression option is not available on all UNIX versions of tar.

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

3.23 Translating Characters (tr)

• Translate characters in standard input into different characters in output

• Syntax:

  tr [options] [string1 [string2]]

• Characters in string1 are replaced by the corresponding character in string2

   $ tr `abc` `123`
   abcdad
   123dld

• Character position in both strings matters

• Both strings should be the same length ^{5}

5If string1 is shorter than string2, the extra characters at the end of string2 are ignored. If string1 is longer, GNU tr follows BSD in padding string2 to the length of string1, by repeating the last character. With the -t option it follows AT&T by truncating string1 to the length of string2.

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

3.24 Examples of tr Usage

• To replace all vowels in the input with spaces

$ tr 'aeiou' '     '

• Using character ranges to translate all lower case letters into their upper case equivalents

$ tr '[a-z]' '[A-Z]'

• Using the asterisk * to replace all 10 digits with the letter same ‘n’

$ tr '[0-9]' '[n*10]'

• Use the -c option (complement) to replace all characters in string1 which don’t belong in a range

$ tr -cs '[a-zA-Z0-9]' '[\n*]'

N.B. This puts every word on a line by itself, by converting all non-alphanumeric characters to newlines, then ‘squeezing’ repeated newlines (with -s) into a single newline.

3.25 Execute programs at specified times (at)

• at executes a shell script at a specified time

• Syntax:

  $ at [options] time [date] +increment

• stdin is scanned for commands to execute, e.g.

  at 2300
  at> fetchmail
  <CTRL>+D

• Some more examples of how to specify time and date:

  at 11pm
  at 1am
  at 5am tomorrow
  at 08:00 Sep 12
  at now
  at now + 4 hours
  at now + 1 day
  at now + 2 months
  at now + 3 years
  at 5:30pm Thursday
  at teatime

• Commands are executed in the current environment at the given time

• stdout and stderr are sent as mail 3.26 Options and commands related to at

• at belongs to a family of utilities for managing time-specified commands

Command  Purpose

atq

Display list of queued at commands

atrm

Remove queued at commands

batch

Schedule jobs at low CPU loading

• All of these can be run as at options:

Option  Purpose

-l

Display list of queued at commands

-d

Remove queued at commands

-b

Schedule jobs at low CPU loading

-ffile

Specify script file in command-line

-m

Send mail after running at, whatever

              the stdout or stderr

• The use of at is controlled by /etc/at.allow

and /etc/at.deny^{6}

6UNIX NOTE: On System V these live in /usr/lib/cron/

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

3.27 Running commands regularly (crontab)

• crontab lets you submit job lists at regular times using the crond daemon

• 2 syntax formats:

$ crontab filename
$ crontab [-u username] [options]

• Options, etc:

Command  Purpose

crontab myfile Install contents of myfile (stdin if no file specified) in

appropriate directory

crontab -r

Remove the crontab for the current user

crontab -l

List (on stdout) current user’s crontab. (might be useful for editing a cron table)

crontab -e

Run a text editor on your crontab file

Table 3.13: crontab usage

• Examples of Crontab Entries

01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
# LW poll client1  for mail
0 10,12,16,18 * * 1-5 root /www/bin/client1_poll
LW 16/09/1998 - Hylafax cron stuff ...
0 0 * * *  root /usr/local/sbin/faxqclean
10 0 * * * fax /usr/local/sbin/faxcron -info 7
[0-59/10] 9-18 * * 1-5 root /usr/local/bin/domail

3.28 Evaluate expressions (expr)

• expr is used to evaluate expressions, like bash arithmetic expressions, e.g., echo $((1 + 2))

• Takes arguments and operators on the command line

• Prints the result

• Returns zero or non zero depending on the result; can be tested with shell

• Watch out for special meaning to shell of characters like * and < or >

  $ expr 1 + 2
  3
  $ expr 6 \* 7 #must escape the '*'
  42
  $ if expr 1+2 != 3 >/dev/null
  > then
  > echo yes
  > else
  > echo no
  > fi
  no

• Has some string manipulation facilities too

  $ expr index abcdefg d
  4
  $ expr substr abcdefghijk 3 3
  cde

• Sometimes used in shell scripts for looping

  i=0; while expr $i != 20 >/dev/null
  do echo $i; i=`expr $i + 1`; done

3.29 Linux Printing

• Completely network-oriented

• Any printer can be made available to any client (machine and application)

• All print jobs are sent to a queue

• Queues can be viewed, edited, maintained from anywhere

– Subject to permission

• Formatted files can be sent straight to queues i.e. no ‘device drivers’

• Printer configuration via text file /etc/printcap,

  see man -S5 printcap

3.30 Printing documents

• Printing may be ‘dumb’ – Data dumped straight to printer

• You get BAD results if formatting is wrong

• Your setup may be smart – Autodetect data formats and convert

• Older UNIX mainly dumb

• Modern Linux pretty smart — selects filters and transforms data streams if possible 3.31 Main Printing Tools

• lpr sends job to the queue for a named printer

• lpq returns info about jobs in a queue

• lprm removes unwanted jobs from a queue

• lpc enables system administrator to control the operation of the printing system

– see man lpc for details

• Desktop environments may offer “drag ‘n’ drop”, visual facilities, etc 3.32 Using lpr

• Syntax:

  lpr [options] file ...

• Main Options:

Flag Options

-P Name of the printer to send the job to -n Print n copies of the document -m Send mail on completion

Table 3.14: Main lpr options

• Example: ^{7}

 $ lpr -Pdjrmt -2 filetypes.txt

7The multiple copies feature of lpr was broken in several versions, you should check you have an up to date copy before relying on this feature

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

3.33 Using lpq

• Syntax:

  lpq [options]

• Options:

Flag Options

-P Name of the printer/queue to interrogate -l Get info on each file within a job

Table 3.15: lpq options

• Example:

  $ lpq -Pdjrmt -l

3.34 Using lprm

• Syntax:

 lprm [options]

• Options:

Flag Options

-P Remove jobs from named printer/queue

- Remove all jobs belonging to yourself user Remove all jobs belonging to user n Remove job number n

Table 3.16: lprm options

• Example:

 $ lprm -Pdjrmt -davef

3.35 Basic Tools Exercises

1. Find and Locate Files

Using find or locate

(a) Display all the filenames under /usr/sbin.

(b) Display all the filenames under /usr/sbin begining with a lowercase ‘c’.

(c) Repeat the previous question, but translate the output to uppercase.

(d) Display all the files under /usr/sbin which are over 5k in size in uppercase.

2. Display Parts of Files

(a) Display the first 10 lines of the file /etc/mime.types

(b) Display the last 10 lines of /etc/mime.types

(c) Display the first 25 lines of /etc/mime.types

(d) Display /etc/mime.types one screen at a time

(e) While viewing /etc/mime.types page-by-page, search for ‘html’

3. Classify, Count and Compare Files

(a) Find out what file types you have in the following directories:

     i. /etc
     ii. /usr/bin

(b) Repeat the previous question, but this time:

i. Re-direct /etc listing to new file filetypes.txt ii. Append the listing for /usr/bin to filetypes.txt

(c) Build a tool (i.e. write a command) to find out how many files are in the /usr/bin directory.

(d) Create two new files from listings of 2 user’s home directories, then find the differences between them.

(e) If you are feeling adventurous, use diff and patch to make the two files created in the last question identical. (Check man patch)

(f) If you are feeling very adventurous, use diff and ed to make the two files created in the last question identical. (Check man ed)

4. Regular Expressions Using the filetypes.txt file that you created before, do the following:

(a) List all the lines that contain ‘directory’

(b) List all the lines that don’t contain ‘directory’.

(c) Find out how many files are directories, then find out how many aren’t.

(d) Why does the following give an error message (try redirecting the output to /dev/null so you can see the error).

   grep ASCII text filetypes.txt

5. Sorting (a) Sort the filetypes.txt file into reverse alphabetical order on the first field.

You may notice that capital and lowercase letters are sorted independently, e.g. ‘A’ comes before ‘a’.

(b) Repeat the first sorting exercise but ignoring case differences

(c) Sort the filetypes.txt files into alphabetical order on the second field (the file type).

(d) Find out how many English text files are listed in the filetypes.txt file.

3.36 Basic Tools Solutions

1. Find and Locate Files

(a) Either

$ find /usr/sbin

or

$ locate /usr/sbin/*

(b) Either

$ find /usr/sbin/c*

or

$ locate /usr/sbin/c*

(c) Either

$ find /usr/sbin/c* | tr '[a-z]' '[A-Z]'

or

$ locate /usr/sbin/c* | tr '[a-z]' '[A-Z]'
(d) $ find /usr/sbin -size 5k | tr '[a-z]' '[A-Z]'

2. Display Parts of Files

(a) Use

$ head /etc/mime.types

(b) Use

$ tail /etc/mime.types

(c) Use

$ head -n 25 /etc/mime.types

(d) Use one of the following

$ less /etc/mime.types

or

$ more /etc/mime.types

(e) While in one of the above, type

/html <RETURN>

3. Classify, Count and Compare Files

(a) Use these commands:

  i. $ file /etc/*
 ii. $ file /usr/bin/*

(b) Use these commands

  i. $ file /etc/* > filetypes.txt

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

 ii. $ file /usr/bin/* >> filetypes.txt

(c) The easiest solution is

$ ls -l /usr/bin | wc -l

(d) Something like this:

$ ls /home/davef > test1.txt
$ ls /home/mikeb > test2.txt
$ diff test1.txt test2.txt

(e) This technique is very important in software development and software packaging:

$ diff -u test1.txt test2.txt > test.patch
$ patch < test.patch
patching file test1.txt

(f) This solution uses the --ed option to diff, and the ed line-editor.

$ diff --ed test1.txt test2.txt > differences.txt
$ cat >> differences.txt
w
q
<CONTROL>d
$ cat differences.txt | ed test1.txt

You could substitute the sed stream editor to pipe everything through one line.

4. Regular Expressions

(a) Use

$ grep directory filetypes.txt

(b) Use

$ grep -v directory filetypes.txt

(c) Use either

$ grep -c directory filetypes.txt
$ grep directory filetypes.txt | wc -l

then

$ grep -cv directory filetypes.txt
$ grep -v directory filetypes.txt | wc -l

(d) Without escaping the space between ASCII and text the shell assumes the pattern has ended and takes text as a filename to look for; hence the error about a non-existing file. grep continues, however, showing lines that match ”ASCII” in the files it can find. What you probably wanted was:

$ grep "ASCII text" filetypes.txt

5. Sorting

(a) Use

$ sort -r filetypes.txt

(b) Use

$ sort -fr filetypes.txt

(c) Presuming the second column starts at character position 25

$ sort -k 1.25 filetypes.txt
(d) $ grep -c "English text" filetypes.txt

More Tools

Objectives

Having completed this module you should be able use the following tools appropriately:

• top
• ps
• find
• vmstat
• free
• ldd
• uptime
• xargs
• cpio
• tar
• gzip

4.1 Introduction

Tools covered in this module have these functions:

Command Function

top

display top CPU processes

ps

display process status

find

find files in a directory hierarchy

vmstat

display virtual memory statistics

free

display free and used memory

ldd

display shared library dependencies

uptime

display system uptime

xargs

build and exec commands from stdin

cpio

copy files to and from archives

tar

create and extract *.tar archive files

gzip

create and extract *.gz archive files

Table 4.1: More tools and their functions

4.2 Displaying System Processes (top)

• Displays ongoing processor activity in real time ^{1}

• Shows processes for all users, unlike ps

• Has several modes:

– Secure Mode, disables potentially dangerous interactive commands

– Cummulative Mode, shows time for a process and its dead children

– No-idle Mode, ignores idle or zombie processes

• Typical output may be:

PID USER   PRI  NI  SIZE  RSS SHARE STAT LIB %CPU %MEM  TIME COMMAND
22594 user    10   0   736  736   556 R     0  8.2  0.5   0:00 top
  1 root     0   0   144   96    76 S     0  0.0  0.0   0:03 init
  2 root     0   0     0    0     0 SW    0  0.0  0.0   0:19 kflushd
  3 root   -12 -12     0    0     0 SW<   0  0.0  0.0   2:42 kswapd
486 root     5   5  3160 2356   804 S N   0  0.0  1.8   0:03 mysqld
869 root     0   0    68   12    12 S     0  0.0  0.0   0:00 mingetty
838 www      0   0 11468 6280   488 S     0  0.0  4.9   4:14 squid
 48 root     0   0   100   80    48 S     0  0.0  0.0   0:01 kerneld
230 root     0   0   384  372   272 S     0  0.0  0.2   0:12 syslogd
239 root     0   0   164  120    72 S     0  0.0  0.0   0:00 klogd
250 daemon   0   0   164  132    88 S     0  0.0  0.1   0:00 atd
261 root     0   0   192  160   112 S     0  0.0  0.1   0:01 crond
272 bin      0   0   244  224   168 S     0  0.0  0.1   0:00 portmap
283 root     0   0   572  296   248 S     0  0.0  0.2   0:17 snmpd
295 root     1   0   136   88    60 S     0  0.0  0.0   0:00 inetd
306 root     0   0   516  488   224 S     0  0.0  0.3   0:06 named
317 root     0   0   124   56    48 S     0  0.0  0.0   0:00 lpd

1N.B. top is not available on all UNIX

4.3 Options and Interactive Commands for top

Significant command-line options for top include:

Option  Function

-d

delay between screen updates (seconds)

-q

Refresh without any delay.

-S

Specifies cumulative mode

-s             Secure mode

-i

Non-idle mode

-c

Show full command line instead of command name

Table 4.2: Command line options for top

The key interactive commands are:

Command  Function

<SPACE>

Update display

fF

add and remove fields

oO

Change order of displayed fields

h or ?

Help on interactive commands

S

Toggle cumulative mode

i

Toggle display of idle proceses

c

Toggle display of command name/line

l

Toggle display of load average

m

Toggle display of memory information

t

Toggle display of summary information

k

Kill a task (with any signal)

r              Renice a task

P

Sort by CPU usage

M

Sort by resident memory usage

T

Sort by time / cumulative time

u

Show only a specific user

n or #

Set the number of process to show

W

Write configuration file /.toprc

q              Quit

Table 4.3: Interctive commands for top

4.4 Reporting process status (ps)

• ps prints info about a user’s processes :

PID   TTY STAT TIME COMMAND
22074 p0  S    0:02 Eterm -t trans
22075 p0  S    2:13 emacs -bg black
22081 p0  S    0:00 asclock
22590 p5  R    0:00 ps

• Unlike jobs which only prints info about processes belonging to the current shell

• Various display formats:

– Long format (l), e.g.

 FLAGS   UID   PID  PPID PRI  NI  SIZE  RSS  WCHAN   STA TTY TIME COMMAND
     0   512 19665 19653   0   0  7000 5616  12d63d  S   p2  0:19 /usr/bin/emacs
100000   512 23656 23652   0   0  1076  548  1897da  S   p5  0:00 /usr/bin/less -is

– User format (u), e.g.

USER    PID %CPU %MEM  SIZE   RSS TTY STAT START   TIME COMMAND
davef 19665  0.0  2.1  7000  5616  p2 S    09:06   0:20 /usr/bin/emacs
davef 23656  0.0  0.2  1076   548  p5 S    14:36   0:00 /usr/bin/less -is

– Jobs format (j), e.g.

 PPID   PID  PGID   SID TTY TPGID  STAT  UID   TIME COMMAND
19653 19665 19665 19652  p2 19653  S     512   0:20 /usr/bin/emacs
23652 23656 23650 19679  p5 23650  S     512   0:00 /usr/bin/less -is

– Virtual Memory format (v), e.g.

  PID TTY STAT TIME  PAGEIN TSIZ DSIZ  RSS   LIM %MEM COMMAND
23656  p5 S    0:00     125   66 1009  548    xx  0.2 /usr/bin/less -is
19665  p2 S    0:21    1645  829 6170 5620    xx  2.1 /usr/bin/emacs

4.5 Options for Reporting process status (ps)

More options to ps:

• Show memory info (m)

  PID TTY MAJFLT MINFLT  TRS  DRS  SIZE SWAP  RSS SHRD LIB  DT COMMAND
23656  p5    125     25   72  476   548    0  548  424   0  31 /usr/bin/less -is
19665  p2   1648   2613 2120 3672  5792    0 5792 2584   0 802 /usr/bin/emacs

• Show ‘family tree’ (f) for processes, e.g.

  PID TTY STAT TIME COMMAND
19652  p2 S    0:00 /bin/login -h oakleigh gbdirect.co.uk -p
19653  p2 S    0:00  \_-bash
19664  p2 S    3:02      \_/home/lee/bin/emacs -f gnus-no-server
19665  p2 S    0:23      \_/usr/bin/emacs
19668  p2 S    3:41      \_/usr/lib/netscape/netscape-communicator
19681  p2 S    0:00          \_(dns helper)

Try pstree also.

• Show environment after command line:

 PID TTY STAT TIME COMMAND
19653 p2 S    0:00 -bash DISPLAY=oakleigh:0.0 TERM=xterm HOME=/home/davef PATH=

• Sort ps results by specific fields: ^{2}

2You can get et a full print-out of the sorting options by giving the erroneous command string ps -O+

Option  Function  Option  Function
-Ou           user  -Oc           cmd

-OU           uid  -Op           pid

-OP           pppid  -Ot           tty
-Oo or ?       session  -Ok           utime
-OK           stime  -Oj           cutime
-OJ           cstime  -Oy           priority
-OT           start time  -Or           rss
-Ov           vsize  -Os           size
-OC           pcpu  -OS           share

Table 4.4: CLI options for sorting ps results

4.6 Finding Files using specified criteria (find)

• find searches your filesystem for files matching certain criteria

• Can match on name, owner, size, modification/access time, name

– and many others

• Can execute commands on files it finds

• Commonly used to archive sets of files, or clear out old files 4.7 Criteria used in find expressions

• Basic syntax:

find base_directory search_criteria

-name string

Filename matches string (Shell metacharacters included)

-mtime value

Modification time matches value

-user UID/username Owner matches UID or username -size size

Size of the file matches size

-perm -/+ mode Permissions of the file match mode -type t File is of type t (f — normal file, x — executable file etc. See man page for full details)

• The values to match are very flexible, e.g. to find all files below /home/lee that were last modified less than 36 ^{3} hours ago:

$ find /home/lee -mtime -1.5

• Find all files below current directory greater than 1000k in size and with permissions 664

(-rw-rw-r---)
$ find . -size +1000k -perm 664 -exec ls -l {} \;

31.5 multiplied by 24 hours

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

4.8 Examples of using (find)

• Find all files ending in .jpg under current dir:

$ find .  -name "*.jpg"

• Find filenames ending in .jpg and modified in the last 8 days below /etc

$ find /etc -name "*.jpg" -mtime -8

• Combine tests with -o and negate with !, e.g:

– To find all filenames not ending in .jpg or modified in the last 8 days

  $ find .  !  -name "*.jpg" -o -mtime -8

• Execute commands on files found. For example, to find then gzip-compress tar files:

$ find . -name "*.tar" -exec gzip {} \;

i.e. found filenames substitute for {} above ^{4}

• N.B. find searches the filesystem in real time; making disks work hard

4Piping the find results to xargs (section 4.14 ) is a more efficent approach

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

4.9 Reporting virtual memory statistics

   (vmstat)

• vmstat is used to identify system bottlenecks

• Reports on processes, memory, paging, block IO, interrupts (traps), and cpu activity

• SYNTAX:

    vmstat [-n] [-V] [delay [count]]

• If no delay specified gives averages since last reboot

– Otherwise updates every delay seconds

– Shows averages since last report

• count is the number of updates to give – If no count is specified, just keeps on running

• Option -n causes header display only once ^{5}

5Allegedly! Some versions do not implement this correctly

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

4.10 Output from vmstat

• Output has cryptic headings :

Section Head Field Description

Procs r no. of runnable processes b no. of processes sleeping w no. of processes swapped out but otherwise

runnable

Memory swpd virtual memory used (kb)

free idle memory (kb) buff memory used as buffers (kb)

Swap si memory swapped in from disk (kb/s)

so memory swapped out to disk (kb/s)

IO bi Blocks sent to a block device (blocks/s)

bo Blocks received from a block device

(blocks/s)

System in interrupts per second, inc the clock

cs context switches per second

CPU us user time (as % of total CPU time) sy system time (as % of total CPU time) id idle time (as % of total CPU time)

Table 4.5: Field descriptions for vmstat output

4.11 free

• Another tool to examine memory status

• Displays in kilobytes by default

  -k

Output in kilobytes

  -m

Output in megabytes

  -b

Output in bytes

  -s x

Poll every x seconds

  -t

Display a ‘total’ line

• Simpler alternative to vmstat

• May be useful if vmstat not available

• Quick check on overall memory usage 4.12 ldd

• ldd prints out library dependencies for a given executable

   $ ldd /bin/bash
           libtermcap.so.2 => /lib/libtermcap.so.2 (0x40003000)
           libc.so.6 => /lib/libc.so.6 (0x40006000)
           /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x00000000)

• /bin/bash requires three libraries to run correctly

• Very useful debugging tool

• Also needed for setting up restricted (chroot’d) environments 4.13 uptime

• uptime prints out basic information about performace of system

$ uptime
4:26pm  up 165 days, 23:23,  9 users,  load average: 0.23, 0.13, 0.10

• Shows :

– Current Time

– Time since last reboot (Days, Hours:Minutes)

– Number of logged-in connections

– Load average past minute, past 5 minutes and past fifteen minutes 4.14 xargs

• Constructs and executes command-lines from information given on standard input

• Commonly used in conjunction with find

• Syntax:

| xargs command

• N.B. Data is normally piped to xargs

• Example: Delete all files named core not modified in the past 8 days

$ find / -name "core" -mtime +8 | xargs rm -f

• This is preferred to

$ find / -name "core" -mtime +8 -exec rm -f {} \;

as it spawns less processes, therefore easing the load on the machine 4.15 Options to xargs

-p

Interactive mode

-t

Verbose mode

-n num

Limit arguments used to command

• Verbose mode will print out the commands it executes

• Interactive mode prints out the command-line and awaits confirmation before executing

• -n tells xargs to use at most num arguments to the command you are running with xargs

• Example:

  $ find / -name "core" -mtime +8 | xargs -p -n 2 rm -f
  rm -f /home/lee/core /home/davef/core ?...y
  rm -f /usr/local/bin/core /root/core ?...y
  $

4.16 Positioning filenames with xargs

• By default xargs places the filenames at the end of the command you give

• If they need to be somewhere else you can use

  replace

• Put {} at the point that you want the filenames inserted

• Example:

  $ find /var/log -mtime -0.5 | xargs --replace cp {} /tmp

4.17 cpio

• Similar to tar

• Creates archives of files

• Operates in copy-in or copy-out mode – Copy-out mode writes archives

– Copy-in extracts from them

• Takes filelist on standard input – Not given on the command line

-i

Copy-in mode

-o

Copy-out mode

-A

Append to an archive

-F file

Use file instead of standard input/output

-H format

Use archive format format

4.18 gzip

• Compresses data taken from stdin or a file

• ‘Lossless’ compression

– Safe on any file

• Various compression levels, 1-9

1 — Fast, less compression 9 — Slower, more compression

• By default takes filename and turns it into

  filename.gz
  $ ls foo*
  foobar
  $ gzip -9 foobar
  $ ls foo*
  foobar.gz

– Replaces original file

• Can write compressed data to stdout

  $ ls foo*
  foobar
  $ gzip -9 -c foobar > foobar.gz
  $ ls foo*
  foobar     foobar.gz

– Leaves original file intact 4.19 Unzipping

• To unzip a file there are two methods

  – gunzip
  – gzip -d

• Really one file linked to two names

• Uncompress all files in the current directory

  $ gzip -d *.gz

• gzip uses Lempel-Ziv coding

• Can also unpack files created with zip, compress

  and pack

4.20 tar

• tar creates archives of files

• Used for transferring files between machines – Or from place to place

• Options:

-x

Extract from an archive

-c

Create archive

-t

List files in an archive

-v          Be verbose

-z

Compress/decompress archive with

gzip
-d

Find differences between archive and the filesystem

-f

Operate on a file not a tape

• tar all files ending in .tex into a gzipped ^{6}

archive called alltex.tar.gz
$ tar zcvf alltex.tar.gz *.tex

• Check which files were included

$ tar tvzf alltex.tar.gz

• Extract them again

$ tar xzvf alltex.tar.gz

6Integrated gzipping is not available on all versions of tar

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

4.21 Raw devices and tar

• Originally designed to talk to magnetic tapes

• Can still write to raw devices

• Useful to maximize space – Fit 1.44Mb of data on a floppy

– Don’t need any space for filesystem information

  $ tar zc filelist > /dev/fd0

• Extract it again

  $ tar zx < /dev/fd0

4.22 Exercises

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

2. Make top sort the list by memory usage.

3. Try killing a process, a good example would be your top process itself!

4. Use top to re-nice a process so that it gets more, or less CPU time.

5. Find a full list of every process on your machine and their full command name using

   ps.

6. Get the same view but tell ps to display the output in ‘family tree’ mode.

7. Request that ps sort it’s output by system time used

8. Display all the filenames under /usr/sbin using find.

9. Display all the filenames under /usr/sbin begining with a lowercase ‘c’.

10. Display all the files under /usr/sbin which are over 5k in size in uppercase.

11. Set vmstat running in a spare terminal updating every 5 seconds.

12. Set up free in another window doing the same thing.

13. Use ldd to find out what libraries some common applications use:

   (a) /sbin/halt
   (b) /usr/bin/gnome-session
    (c) /usr/bin/ee

14. Practice using xargs by finding sets of files and performing simple (Non-destructive!) operations on them e.g.

(a) Find all files in the file system modified in the last 24 hours and make copies of them in a directory called backup in your home directory

(b) Find all files over 5000k and make copies of them in the backup directory

(c) Find all files ending in .txt and compress them using gzip. NOTE: You will

need to use the -n option to xargs for this. Why?

15. Use cpio and tar to create an archive of the files you’ve copied in to the backup directory. Which do you think is easier to use? Learn one … forget the other!

16. Write an archive of /etc to a floppy as a raw archive

4.23 Solutions

1. Simply starting top from the command line will show all processes

2. Hitting M will sort the list by memory usage

3. Find your top process in the list (It will probably be near the head of a CPU-sorted list. Press k. You will be prompted for a process number, give the number from the PID column adjacent to your top process. You are then prompted for a signal to send the process. The default (15) should kill top and return you to your shell prompt

4. Again find your top process, then press r. You are prompted for the PID and a nice value. Unless you are super-user you can only lower the process priority (Give it a higher nice value).

5. $ ps auxw

Shows a full list of processes and doesn’t truncate the command-line to fit on one output line

6. $ ps auxwf

Identical to the above but show the family-tree view

7. Adding OK to the first command line should sort the output by system time

$ ps auxw OK
8. $ find /usr/sbin -type f
9. $ find /usr/sbin -type f -name "c*"
10. $ find /usr/sbin -type f -size +5k | tr [a-z] [A-Z]
11. $ vmstat 5
12. $ free -s 5
13. (a) $ ldd /sbin/halt
 (b) $ ldd /usr/bin/gnome-session
 (c) $ ldd /usr/bin/ee
14. (a) $ find / -mtime -1 | xargs --replace cp {} ~/backup
 (b) $ find / -size +5000k | xargs --replace cp {} ~/backup
 (c) $ find / -name "*.txt" | xargs -n 1 gzip

If we didn’t use -n 1 then xargs would produce command lines like

$ gzip /etc/foo.txt /var/log/bar.txt moo.txt

Due to the way gzip works this will not produce the desired effect

15. The command lines would be

$ find ~/backup -type f | cpio -o > cpio_archive

or

$ tar zcvf tar_archive.tar.gz ~/backup
16. $ tar zcv /etc > /dev/fd0

Basic Filesystem

Objectives

• After completing this section, you will be able to: – Understand a typical Linux filesystem

– Navigate the file hierarchy

– Manipulate files and directories

– Handle access control

– Deal with ‘special files’ and links 5.1 Filesystem Overview

• Linux uses ext2 as its native filesystem – Also supports many other types

• All data stored on a Linux system is a file

• Ext2 file names can be 1 to 255 characters long – Only / and nul are disallowed

• Non-native filesystems have different features

• Ext2 sees only two basic types of files:

– directories

– files

• Other specialised types exist (FIFOs, and ‘special files’), these are covered later 5.2 Files

• Linux imposes no structure on files

• All files are accessible at the byte level

• Individual files have a maximum size of around 2Gb (in an ext2 filesystem)

• They have a minimum size of 0 bytes

• Files can be extended after creation

• Filename extensions such as .exe and .bat are unnecessary

• Executable files are simply marked as such using file permissions (see later)

5.3 Directories

• Directories are files that list other files – Can be normal files or directories

– Enables a hierarchy to be built

• Each directory entry consists of two parts: a file name and an inode number

(An inode is roughly a pointer to a file, see below)

Filename

Inode number

.  512
..  500
bin  17324

basic linux.tex

24567

• The topmost directory is always called / – Called the fileystem ‘root’

• Directory information can only be changed by Linux itself

– Ensures a proper structure is maintained 5.4 Directory Hierarchy

• By tradition several directories have specialised rôles

/bin

executable commands

/sbin

executable commands regarding important system functions

/etc

system configuration files

/lib

shared libraries

/dev

peripheral devices

/tmp

temporary files

/mnt

to mount external devices

/var

odds and ends, e.g. logs, status and lock files, spooling files

/proc

system information

/usr/bin

further executable files

/usr/sbin

further system-important executable files

/usr/lib

further libraries

• User-installed programs typically go under the

/usr/local hierachy

• /mnt is not always present, it is merely a convenience to place all ‘mounted’ devices under one place 5.5 Pathnames

[figure: File1; File2; lee; mike; home; /]

• Files can be referred to by relative or absolute pathnames

• Absolute pathnames begin with /

/usr/sbin/httpd
/usr/local/bin/safe-mysqld

• The absolute pathname refers to one file only

• A relative pathname does not begin with / and describes the path from the current directory to find a file, e.g.

sbin/httpd
bin/safe-mysqld

5.6 Current Directory

• When you log in your shell is placed in your home directory

  – Typically /home/username^{1}

– Superuser typically has /root for a home

directory

• ~ is a synonym for /home/username

• cd changes your current directory

– Typing cd path changes your current

directory to path

• path can be absolute or relative

• Without arguments cd changes to your home directory

• pwd tells you the current directory

1Home directories are sometimes under /usr/username on some older systems

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

5.7 Dot (.) and DotDot(..)

• Directories always contain two entries ”.” and ”..”

  .

Current directory

  ..

Parent directory

• Used for relative pathnames and navigation

• Example:

   $ mv file ..

Move file to the parent directory

   $ du .

Display space used by files in current directory and below

   $ du ..

Display space used by files in parent directory and below

   $ ./a.out

Execute the file a.out in the current directory

• This last row above shows execution of the particular file that is in the current directory

– If we had simply typed a.out then our PATH environment variable would be used to search for the file

– It may execute another a.out instead of the one we actually want 5.8 Moving and Copying Files

• The mv command is used to move files:

mv [options] source dest
mv [options] source... directory

e.g.

$ mv oldname newname
$ mv somefile ..

• The cp command is used to copy files:

cp [options] source dest
cp [options] source... directory

e.g.

$ cp thisfile newfile
$ cp file1 file2 file3 /tmp

– A very important option is -a, which preserves permissions and symbolic links.

– Can even use cp -ax to clone a hard disk

partition.

• Full details of applicable options and usage can be found by typing man mv or man cp 5.9 Removing Files

• Files are removed using the rm command:

rm [options] file

e.g.

$ rm thisfile thatfile
rm: remove `thisfile'? y
rm: remove `thatfile'? y
$

• Most notable among the options are :

 rm -f

Force removal, without confirmation

 rm -r

Recursively delete files e.g.

$ rm -r somedir

• Again, full details are available by typing man rm

• Removing a file is not considered an operation on the file

– It is an operation on the directory

– Filenames are merely links (Explained below)

5.10 Operations on Directories

mkdir

Create a new directory

rmdir

Remove a directory

ls

List the contents of a directory

• These commands can take many arguments

• mkdir can be told to create the whole pathname of directories if they don’t exist, e.g.

$ mkdir -p /home/lee/new1/test/directory

Will create the directories

/home/lee/new1 and /home/lee/new1/test as
well as /home/lee/new1/test/directory

if they don’t already exist

• ls arguments control what information is shown and how it’s sorted

– Some are explained later, consult man ls for full details 5.11 Inodes

• Each file is represented by an inode^{2}

• An inode contains information about:

– File type (ordinary, directory, FIFO, block device etc.)

– Owner ID (user the file belongs to)

– Size (in bytes)

– Access, creation, and modification times

– Group ID (group the file belongs to)

– File permissions

– Mapping of the file contents (data sectors)

• Inode layout and location varies with filesystem type

2The term inode was invented by Dennis Ritchie of AT&T. He admits to forgetting why he chose that name.

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

5.12 Inodes (continued)

• ls -i displays inode numbers of entries, e.g.

200808 Ext2fs-0.1-14.html   542726 include
200795 Ext2fs-0.1-2.html    188447 info
200797 Ext2fs-0.1-4.html    333831 ldap
200802 Ext2fs-0.1-8.html    329729 man
200803 Ext2fs-0.1-9.html    278533 misc
200793 Ext2fs-0.1.html      428042 nsmail
204802 systemprogramming

• stat prints the inode contents for files inc. permissions, size, links, access times etc.

$ stat /home/lee
File: "/home/lee"
Size: 4096         Filetype: Directory
Mode: (0755/drwxr-xr-x) Uid:(504/lee) Gid:(502/lee)
Device:  3,0   Inode: 200705    Links: 30
Access: Thu May 27 10:54:55 1999(00000.00:01:43)
Modify: Thu May 27 10:44:41 1999(00000.00:11:57)
Change: Thu May 27 10:44:41 1999(00000.00:11:57)

5.13 Links

• More than one filename can refer to an inode – These file names are links to the file

• ln creates links to files – Creates hard links by default

– ln -s creates symbolic or soft links

• Erasing a file just removes its directory entry

– The file is only lost when all entries for it have

been removed

• Crucially : A filename is not the file – The inode is the file

– All names are simply links (references) to the inode

– Vague resemblance to Windows’ ‘shortcuts’ 5.14 Hard links

• A hard link is merely a directory entry with the relevant inode number

• Consider the following – Start with:

     $ ls -li
     428175 -rw-rw-r-- ... 4 May 26 13:18 test

– We create a hard link:

     $ ln test hl
     $ ls -li
     428175 -rw-rw-r-- ... 4 May 26 13:18 hl
     428175 -rw-rw-r-- ... 4 May 26 13:18 test

• N.B. Hard links cannot cross filesystems

• Inode numbers are filesystem specific 5.15 Soft links

• Soft links store the pathname of the linked file

• This means they can cross filesystems

• Adding a soft link :

$ ln -s test sl
$ ls -li
428175 -rw-rw-r-- ... 4 May 26 13:18 hl
428176 lrwxrwxrwx ... 4 May 26 13:19 sl -> test
428175 -rw-rw-r-- ... 4 May 26 13:18 test

5.16 Soft links (continued)

• If we replace the test file with another then the symbolic link still works, but the hard one still points to the old file!

$ mv ../test2 test
$ ls -li
428175 -rw-rw-r-- ...  5 May 26 13:45 hl
428176 lrwxrwxrwx ...  4 May 26 13:19 sl -> test
428178 -rw-rw-r-- ... 15 May 26 13:47 test

5.17 Access Control and UID

• File access can be limited to specific users

• Super user(s) can override access control

• Access control is set by user and group ID

• Each user has a user-id (UID) and one or more group-ids GIDs)

• Processes have an associated UID and GID

– Inherited from the user who created the

process

• They can however can be changed:

– Processes are known as set-user ID (setuid) if they set their own user ID

– or set-group ID (setgid) if they set their own group ID 5.18 Categories of Access Control

• There are three categories of access

read  r
write  w
execute  x

• These may be specified for three sets of users: – User

– Group

– Everyone 5.19 Access Control — Example

• ls -l shows the access permissions, e.g.

$ ls -l
-rw-rw-r-- 1 www    www    x_windows.tex
lrwxrwxrwx 1 lee    lee    img -> ../linux/img/
-rw-rw-r-- 1 lee    lee    test.log

5.20 Changing Access Permission: chmod

• Only the owner of a file (or the super-user) may alter its access permissions

• chmod (change mode) changes access permissions

– Works in two ways, symbolically or numerically

– Symbolically is easier to remember (for most)

5.21 chmod symbolically

• Select who you want to change permissions for

(u=user, g=group, o=others, a=all)

• Decide whether you want to grant a

permission(+), remove(-), or set(=) it

• Take the permission that you want to change

(r=read, w=write, x=execute)

• Example :

$ chmod gu+w filename

Adds write permission for user and group

• You can make several changes by separating the settings with commas, e.g.

$ chmod a-w,gu=rw filename

Removes write permission for all, then grants it for the user and group 5.22 chmod numerically

• Once you know this it is often quicker

• A number represents each permission type

4

read permission 2

write permission 1

execute permission

• Add up the permission numbers you want for each user group (owner, group, all) and supply these to chmod

• Example:

$ chmod 755 filename

grants all permissions to the owner (4+2+1), and read and execute (4+1) to group and all others

5.23 umask

• Files begin with a default access setting

– Specified by a user’s umask setting

• This only works numerically

• Unlike chmod, specified permissions are turned off

• With a umask setting of 000 files are created with permissions rw-rw-rw- (666)

• Default umask (on Red Hat systems) is 002 which means files are typically created rw-r--r-- (664) ^{3} e.g.

  $ umask
  002
  $ touch foo
  $ ls -l foo
  -rw-rw-r-- 1 lee  lee     0 Feb 10 17:17 foo
  $ umask 222
  $ touch foo2
  $ ls -l foo2
  -r--r--r-- 1 lee  lee     0 Feb 10 17:17 foo2

3This is the case on Redhat systems where users typically belong to a group of their own; other distributions will probably use a default umask of 022.

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

5.24 Special Files — /dev

• Files under /dev typically represent devices attached to your computer

• Programs can open and close them and read from and write to them — as with regular files

• Kernel code handles exactly how these work

• Two types – Block — Disk drives, tape drives, CDROMs

– Character — Printers, modems, etc.

5.25 Special Files — /proc

• The section of the filesystem called /proc doesn’t contain real files

• It contains system status information

• For example:

Location  Information

/proc/[number] On specific running processes. See

man proc for details

/proc/meminfo How much memory is in your system

and how much is being used

/proc/cpuinfo What CPU(s) you are currently using /proc/filesystems Filesystems your kernel supports /proc/kcore

An image of your physical memory

/proc/net

Network status of your machine

/proc/pci

PCI devices found at initialization

/proc/sys Details on kernel variables, e.g.

– Maximum number of files we can open (file-max)

– Number of files currently open (file-nr)

– The uptime of the system (uptime)

Table 5.1: System Information from /proc

5.26 Filesystem Structure

Multi-Volume Filesystems

• The filesystem can be held on several devices

• Large disks can be divided into partitions – This creates several logical devices

• A basic Linux system must be present on /

• Other parts of the fs may be mounted at any time

• The main ones are mounted at boot time

• This is controlled by the /etc/fstab file which says which volumes are mounted where 5.27 /etc/fstab — Example

Logical Volume Mount Point FS type Options Dump

Check order

/dev/hda1  /  ext2  defaults  1  1
/dev/hda5  /home  ext2  defaults  1  2
/dev/hda7  /tmp  ext2  defaults  1  2
/dev/hda6  /usr  ext2  defaults  1  2
/dev/hda8  swap  swap  defaults  0  0
/dev/fd0  /mnt/floppy  ext2  noauto  0  0
/dev/cdrom  /mnt/cdrom  iso9660  noauto,ro  0  0

\\kashmir\c /mnt/kashmir smbfs guest 0

0

landlord:/var/admin /var/admin nfs defaults 0

0

landlord:/home/lee /home/lee/LANDLORD nfs defaults 0 0

5.28 Mounting Additional Volumes

• To mount a filesystem use mount, e.g.

mount /dev/cdrom /mnt/cdrom

• Mounts the filesystem /dev/cdrom in the

directory /mnt/cdrom

• cd /mnt/cdrom changes directory to the root of the CDROM’s filesystem

• To unmount use umount name where name is either the filesystem name or the mount point:

umount /dev/cdrom
umount /mnt/cdrom

• N.B. - A filesystem can only be unmounted when it is no longer in use. ‘In use’ includes :

– Having any file on that filesystem open

– Having a shell in a directory on that filesystem

• Use the lsof (“list open files”) program to identify and kill processes that prevent unmounting a file.

5.29 Mounting shared filesystems

• NFS filesystems can be mounted with

mount -t nfs hostname:path mount-point

• Example:

mount -t nfs landlord:/backup /mnt/backup

• Share files from MS-Windows machines using SAMBA

• This is a free implementation of the Windows file-sharing protocols, e.g.

mount -t smbfs '\\ntbox\c' /mnt/ntbox

• N.B. Linux does not use the ‘drive letter’ concept at all

– Drives and shares integrate seamlessly into the filename tree 5.30 Summary

• The primary Linux filesystem is Ext2

• It has a tree-like hierarchy of directories

• Directories merely contain pointers to files (inodes)

• inodes contain all the information about a file

• Can have multiple links to the same file

• Read/Write access is controlled per file

• Creation/Deletion of files is controlled by permissions of the directory

• Several filesystems can be mounted to create the directory hierarchy 5.31 Filesystem Exercises

1. Basic navigation

(a) Log in and use pwd to discover what the full path of your home directory is.

(b) Change directory to /bin and then /tmp. Use pwd to check you got there each time.

(c) When in /tmp type cd .. and use pwd to find out where you end up.

(d) What is the parent directory of the root of the filesystem? Why is this so?

(e) Move back to your home directory. Think of three ways you can do this.

2. Directories

(a) Start in your home directory and create a directory called new

(b) Change to the new directory and create a directory called newer

(c) Go to your home directory. Now create a directory under newer called newerstill There are two ways to do this what are they? (Hint: You don’t have to change directories to solve this.)

(d) Remove all the directories that you’ve just created, there are several ways to do this.

(e) Create the same directory structure with one command.

3. Links

(a) Create a file called test in your home directory (Typing echo 123> test should do this). Now create a hard link to test called h test and a symbolic link to test called s test

(b) Find out the inode number of the files. Check you understand why they are what they are.

(c) Remove the original file called test. Can you still get at the contents of the original file?

(d) What happens if you try cat s test? Make sure you understand the distinction between h test, and s test

(e) Try to make a hard link to your home directory. Why does this fail?

4. /proc

(a) Use the files in /proc to find out how much memory your system has and what processor it is running on.

(b) Find out what PCI devices are attached to your machine.

(c) Find out what environment variables are set for your currently running shell using the information in /proc. Hint you can get the process-id of your shell using $$

(d) Whether or not your machine is doing IP forwarding is stored in the file /proc/sys/net/ipv4/ip forward. You can cat this file, a value of 1 means that IP forwarding is turned on. Find out whether or not your machine will forward IP.

(e) Find out how many files are currently open on your system.

5.32 Filesystem Solutions

1. Basic Navigation

(a) You will probably see

   $ pwd
   /home/username

where username is the name you log in with.

(b) Check that the output of pwd is /bin and /tmp

(c) You should end up in / This is the root of the filesystem.

(d) The parent of / is itself. You can use ls -lia to show that the inode numbers for . and .. are the same when you are in /

(e) You can move back to your home directory by using any of the following:

     i. cd

ii. cd ∼

    iii. cd ∼username

iv. cd /home/username (Provided your home directory lives under /home)

2. Directories

(a) $ cd
   $ mkdir new
(b) $ cd new
   $ mkdir newer
(c) $ cd

then either :

     i. $ cd new/newer
       $ mkdir newerstill
     ii. $ mkdir new/newer/newerstill

(d) Any of the following will work :

     i. $ cd
       $ cd new/newer
       $ rmdir newerstill
       $ cd ..
       $ rmdir newer
       $ cd ..
       $ rmdir new
     ii. $ cd
       $ rmdir -p new/newer/newerstill

iii. $ cd

       $ rm -fr new/
(e) $ cd
   $ mkdir -p new/newer/newerstill

3. Links

(a) This is achieved as follows:

$ ln test h_test
$ ln -s test s_test

(b) ls -li should show that the inode number of the original file and h test are identical h test is another name for the original file. The inode number for s test will be different. It is a separate file that contains information about the location of the file it is a link to.

(c) After you created the hard link the original file had two names test and h test. You have removed test but until all names for a file have been removed it is still accessible. In this case you can do cat h test to see the contents of the file.

(d) This should fail with a No such file or directory message. s test contained a pointer to the file test not the inode number. There is no longer a file named test so this cannot work. Hard links reference a file by its inode number, symbolic links reference it by its name

(e) This is not allowed, as it could stop the filesystem being strictly hierarchical.

4. /proc

(a) cat /proc/meminfo should show you memory usage.

(b) cat /proc/pci gives a list of all PCI devices.

(c) cat /proc/$$/environ will give a list of the environment of your current shell. Each variable is delimited with the nul character (decimal 0). The following will show the output with one variable per line :

$ cat /proc/$$/environ | tr '\verb|\|000' '\verb|\|n'

(d) See The Exercise -

(e) cat /proc/sys/fs/file-nr

Introduction to Editing With vi

Objectives

In this section, you will learn how to:

• Use the vi editor to view, create and edit files – the vi screen layout

– move round in files

– replace, insert and change text

– search files 6.1 Text editors under Linux

• There are a number of text-editors available

• vi is on virtually every Linux distribution

• Also comes with 99% of Unix systems

• Everyone should have a basic understanding

• vi is like Linux

– Has some very complex and powerful functions that can make your life easier

– However, you don’t have to know everything; you get by knowing the basics

– Shares key bindings with many utilities

• We’ll just cover the basics here, vi is too big to cover everything!

6.2 vi and your terminal

• vi is fundamentally text-based

– Graphical adaptations are available (gvim)

• Needs to know your terminal’s capabilities

– May not function if your terminal is misconfigured

– Check your TERM environment variable

– Terminal capabilities are listed in

/etc/termcap

– Generally not an issue except with Windows 98 telnet client. When use windows telnet,

always type export TERM=vt100 before

starting vi and other such programs, or they will not work properly.

6.3 vi screen layout

• Lines containing simply a ~ show that you are past the end of the file and there is nothing here.

• The terminal’s bottom line is the status line – Shows status messages

– Where you type some commands (The ‘ed’/‘ex’ command set, explained later)

   This is a test document
   Some lines of text here
   One, two, three
   four, five, six
   ~
   ~
   ~
   ~
   ~
   ~
   "test" 5 lines, 77 characters written

6.4 Opening files with vi

• Launch vi by typing its name on command line

• With no arguments vi starts with an un-named and empty buffer

• vifilename opens a specific file

• If you don’t have write permission on a file the status line will tell you :

  "/etc/aliases" [readonly] 152 lines, 3215 characters

• If there is no such file status line will say something like :

  "some_filename" [New File]

6.5 vi Modes

• Unlike many editors vi does not always insert what you type into the file

• Has several ‘modes’

– Only one is responsible for inserting text into

the current file

• vi has 3 modes: ^{1}

command mode Moving the cursor, searching and

manipulating existing text

insert mode Entering new text

‘:’ (‘ed’) mode File manipulation, advanced searching and substitution

• vi starts in command mode

• Return to command mode at any time by hitting

  <ESC>

1Some people refer to ”ex” instead of ”ed”. They are the same thing

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

6.6 Saving, changing file and quitting

• When you open a file, a copy of it is opened into memory

• Any changes you make apply to this copy only

• File on disk only changes if you explicitly say so

• To save (or write) a file you must be in command-mode, then type :w

• Can save your file under a new name, e.g.

    :w newfilename

• To quit vi type :q

• vi normally prompts you if you have unsaved work

• To quit without saving your work type :q!

• ZZ will save your work and then quit 6.7 Moving around in command mode

• Many ways to move around a document

• You must be in command mode for the following : – On ‘friendly’ terminals you can use arrow keys

– Arrow keys are sometimes unavailable on some terminals so vi has some alternatives

k h l j

• Although ‘awkward’ at first, these make your life easier

– Always work, regardless of system type

– Fingers stay on the ‘home’ keys 6.8 Numeric Prefixes

• Key concept: ‘numeric prefixes’ or ‘multipliers’

– Vastly improves the usefulness of many commands

• To supply a prefix simply type the number before the command

– vi will then perform the command the specified number of times.

• Note: In subsequent examples a small box indicates the position of the cursor

• Starting with

The quick brown
fox jumped over
the lazy dog

and pressing 2l will result in

The quick brown
fox jumped over
the lazy dog

6.9 Further Movement

• vi also allows movements by units other than characters.

• Moving by pages :

Key Result

^f

Forward one screenful

^b

Back one screenful

^u

Forward half a screenful

^d

Back half a screenful

• Moving by ‘words’ :

Key Result

w

Go to beginning of next word

e

Go to end of next word

b

Go to start of previous word

• For these commands punctuation is not counted as part of a word

• The commands W, E and B act the same but do include punctuation in words

• NOTE: Case is important to vi commands, b and B are different commands!

– The upper and lower case versions of commands are usually related 6.10 Further Movement — Example

• From

This, he said, is an example

Key Result

w

     This, he said, is an example
W

     This, he said, is an example
e

     This, he said, is an example
E

     This, he said, is an example
b

     This, he said, is an example
B

     This, he said, is an example

• As with virtually all commands these may be given a numeric prefix

• From the original start-point :

Key Result

2w

     This, he said, is an example
2W

     This, he said, is an example
2e

     This, he said, is an example
2E

     This, he said, is an example
2b

     This, he said, is an example
2B

     This, he said, is an example

• It’s not necessary to know these, but they make life a lot easier when you get used to them!

6.11 Movement by lines

• What if we want to get to the beginning of the next line^{2}?

• Commands to move to line start/end:

Key Result

$

Move to the end of the current line

0

Move to start of current line

^

Move to first character of line

• Moving to start of a previous or subsequent line

Key  Result

+

Move to beginning of the next line

-

Move to beginning of the previous line

2A ‘line’ is the set of characters contained between newline characters, not necessarily what appears on one line in your terminal

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

6.12 Movement by lines — Examples

• In each case here, we start from:

This, he said,
is a most
interesting example

Key  Result

+ <RET>

This, he said,
is a most
interesting example
-

This, he said,
is a most
interesting example
0 or ^

This, he said,
is a most
interesting example
$

This, he said,
is a most
interesting example

6.13 Inserting text

• You probably want more from a text editor than the ability to move a cursor!

• At the bare minimum you need to be able to insert text into a file

• Don’t worry, vi does this with ease

• As with everything else, though, there’s more than one way

• Again, while this may seem confusing, you only need to know the bare minimum

• But, the more you know, the easier your life becomes!

6.14 i command

• The i command inserts text before the cursor

• This places vi into ‘insert’ mode

• Anything you type now is treated as text to insert into the file rather than as a command

• You leave insert mode by typing <ESC>

• This is insertion at its simplest!

• To insert text after the cursor we use the a (append) command

• Also :

Key Result

   A

Append at the end of the line

   I

Insert at the beginning of the line

   o

Create blank line below cursor for insertion

   O

Create blank line above cursor for insertion

• If your cursor keys work then you may move around the line while in insert mode

• You can delete characters from the current insertion using backspace 6.15 Multiple Insertion

• Insertion commands can take numeric prefixes

• The result may be surprising!

• Consider the following sequence of keypresses (from command mode) in an empty document

  5i&nbsp;

• The result will be :

  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

6.16 Deleting Text

• vi has a vast array of commands for deleting text

• The ‘odd-one-out’ is x which deletes the character under cursor

• The rest of the deletion commands are based-around the easy to remember d command

• d on its own does nothing

• You have to tell it how much to delete

• The amount to delete is given by the keys you used when studying movement

Example:

Key Result

   dw

Delete to the beginning of the next ‘word’

   3dw

Delete 3 ‘words’

   de

Delete to the end of the ‘word’

   db

Delete everything before cursor to the beginning of the ‘word’

   d$

Delete to the end of the line

   d0

Delete to the beginning of the line

• Two more special cases :

Key Result

   dd

Delete the entire line

   D

Delete to the end of the line

6.17 Changing Text

• Now we know everything we need to know to delete text, insert new text and save changes

• vi however likes to give us choices!

• If we find a word that is wrong, we can delete it and insert the replacement

• We’re actually ‘changing’ the word

• vi has a family of commands for just this, all starting with c

• Similar to deletion, i.e. you can use cw to change a word, c$ to change to the end of the line, or 3cw to change three words

• What actually happens is that the designated amount is deleted and you are placed in insert mode

Key Result

   cw

Change a word

   3cw

Change 3 words

   c$

Change to the end of the line

   c0

Change to the beginning of the line

6.18 Copy and Paste

• We’re still missing the ability to copy a piece of text and paste it somewhere else

• vi does support this, but it calls it ‘yanking’ and putting

• All ‘yanking’ commands are prefixed with a y and follow the same rules as before, i.e. yw, y$, 3yw

• yy and Y yank a whole line and the rest of a line, respectively

• Paste text using p or P – p pastes text after the cursor

– Uppercase P pastes it before

• Deleted text is also considered to be yanked – xp will transpose two characters 6.19 Finding your place

• You can search through a file using /

• You will get a / as a prompt on your status line

• To search for the string exam type

  /exam
  and press <RETURN>

• If vi found your search string it will move the screen to a relevant place and highlight it

• n will skip to the next occurence; N to the previous

• Search backwards by using ? instead of / 6.20 Miscellaneous Commands

• vi has a number of commands that don’t really fit anywhere else

• ~ toggles the case of character under cursor

• . repeats the last action

• u undoes the last action – Linux vi supports multilevel undo

– Standard vi does not

• J Join the current and following line 6.21 Search and replace

• vi can also replace the words it finds

• Basic form is:

    s/searchfor/replacewith/modifier

• By default it only changes one occurence per line, and only checks the current line

– If we tag the g modifier on the end it will

replace all matches on the current line

• If we use a range^{3} we can search and replace a specified part of a document, e.g.

– To search and replace from lines 10 to 15

inclusive:

      :10,15 s/foo/bar/g

– To search and replace on the whole document

      :1,$ s/foo/bar/g

3Not explained here, this is an advanced topic

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

6.22 Regular Expressions

• Sometimes it’s desireable to search for a word ‘fuzzily’

• You may know the start of a word, or the end – Or both, but not the bit in the middle!

• Regular expressions can come in useful here

• Can be used in normal searches or ‘search and replace’ commands 6.23 Regular Expression Conventions

• Lots of things in Linux use regular expressions

– Not all ‘exactly’ the same

– 95% similar though

• Defines certain special characters

Character Result

   .

Match any character

   [a-z]

Match any character in the range a to z

   *

Match the preceeding character zero or more times

   ^

Match the beginning of a line

   $

Match the end of a line

   \<

Match the beginning of a word

   \>

Match the end of a word

• Strictly speaking * can apply to more than one character

– We won’t cover that here 6.24 Regular Expression Examples

• Suppose we want to find all words ending in ent

• We could read the entire document to check for ent by hand

– Takes far too long

– We’d probably still miss some

– Easier to get the computer to do it

• We could do a search using /ent<RET>

– Unfortunately that would also match words

beginning with ent or with ent in the middle • /ent\> will jump to the next word that ends with

  ent

6.25 Regular Expression Replacement

• We can also use regular expressions in the search section of search and replace commands, e.g.

 s/\<foo/bar/g

will replace all occurences of foo at the beginning of a word with bar

Introduction to Editing With vi  183

6.26 Help

• vi on Linux has very extensive online help.

• There is an interactive tutorial too; try it out.

6.27 vi Exercises

1. Recognizing vi

(a) Start up vi with no filename to see what it looks like

(b) Exit vi and then start it again with the file /etc/passwd

(c) What can you tell about the file from this screen?

2. Getting used to vi

(a) Start vi with the the file /etc/passwd again

(b) Practise the basic movement commands on the file

(c) Check you can use both the cursors and hjkl to move around

(d) Check the other movement commands work as expected

3. Creating with vi

(a) Start vi with the filename vi test. This should be a new file

(b) Insert your name into the file and then save it and leave vi

(c) Open the file again and check it still contains your name

(d) Next add some more names to the file, one on each line

(e) Go to a name roughly half way down your list. Check you can insert a name on the line above, and on the line below

(f) Check you can append to the end of lines and insert at the beginning of lines

4. Movement and Multipliers

(a) Check you can move through your file using combinations of the movement keys and numeric prefixes. For example

i. Move 3 lines down at a time ii. Move 2 words along iii. Move to the beginning of the second line below your cursor

5. Deleting with vi (a) Try deleting various entities (Words, lines, characters) from your file

(b) Check that these work with the numeric prefixes

(c) You should be able to achieve all of the following i. Delete a word ii. Delete to the end of the line iii. Delete to the beginning of the line iv. Delete the whole line v. Delete 2 lines at once vi. Delete 2 words at once (Either including or excluding punctuation)

6. Changes with vi

(a) Repeat the exercises given for delete but do changes instead of deletions 7. Yanking and Pasting

(a) Copy the first line of your file and paste it so that it becomes the last line

(b) Paste it back at the top of the file

(c) Place the cursor at the very beginning of the file and try the following keystrokes

     i. yyjyyp
     ii. 2yyp

(d) What was the difference and can you suggest why this may be?

(e) Check that text deleted can be pasted back

8. Miscellaneous

(a) Place the cursor at the beginning of the file and try the following command sequence:

   yyp...

Explain the result

(b) Place the cursor over a letter on the middle of a word. What happens when you type xp?

(c) Join all the lines of your file into one long line. Check that the movement commands regarding lines work on actual lines rather than the lines as seen on your screen

6.28 vi Solutions

1. Recognizing vi

(a) Check you understand where the status line is, and what the ~ characters mean (b) :q should exit vi. If you want to make sure you’re in command mode press

<ESC> first. vi /etc/passwd will start vi with /etc/passwd opened

(c) vi should tell you that this file is read only. This is because you don’t have sufficient permissions to change the file. vi should also tell you how many lines and characters are in the file.

2. Getting used to vi

(a) vi /etc/passwd

(b) You should be fairly comfortable with the various navigation methods such as moving left, right, up and down, to the end or beginning of the line and moving up and down by intervals of pages and half pages.

3. Creating with vi

(a) vi vi test. The status line should tell you that it is a new file and each line on the main screen should begin with a ~ indicating lack of content

(b) To insert my name simply type:

imy name<ESC>

There are several ways to save and exit:

i. :w followed by :q

 ii. :wq

iii. ZZ

(c) vi vi test and check that the text you entered is there. If not try again.

(d) There are several ways to do this:

i. When inserting using i you may type RETURN to insert a newline character. it is possible therefore to start with the cursor at the beginning of the file and type:

    iname1<RET>name2<RET>name3<RET> and so on

ii. Typing o or O will open a new line for insertion

(e) You should check that you understand which of o and O inserts above, and which below the current line

(f) Appending to the end of a line can be done using either:

  i. $atext to append<ESC>
 ii. Atext to append<ESC>

Inserting at the beginning can be done using any of:

  i. ^itext to insert<ESC>
 ii. 0itext to insert<ESC>
 iii. Itext to insert<ESC>

4. Movement and Multipliers

(a) You should practice moving around using the movement characters with the numerical prefixes

  i. 3j, 3+, or 3<RET>

ii. 3e, or 2w

 iii. 2+, or 2<RET>

5. Deleting with vi

(a) You should make sure that the various deleting methods work as you expected. If they surprise you, try to work out how they do work.

(b) Again check you understand the various possibilities.

(c) The following represent only possible solutions:

  i. dw

ii. d$ iii. d0 iv. dd

 v. 2dd
 vi. 2dw

6. Changing with vi

(a) The answers for this are the same as for delete except substituting c for d in each case.

7. Yanking and Pasting

(a) Move to the first line of the file and type yy, then move to the end of the file and type p

(b) Move back to the top line of the file and type P which will paste it above the current line.

(c) Check you can tell the difference between the two commands.

(d) The ‘Yank buffer’ only holds the contents of one yank operation. Both sets of keypresses yank the line we start on and the line below. However the first does this as two seperate operations and the ‘yank buffer’ only remembers the most recent. The second example yanks two lines at once, therefore placing both in the yank buffer.

(e) You should try ddp and check that the text appears after being deleted.

8. Miscellaneous

(a) . repeats the last action. In this case it is a paste operation. It could equally well have been an insert, change word or delete operation.

(b) The xp command pair is useful for transposing letters.

(c) Starting at the top of your file pressing J will join the following line to the current line. Repeat this until the entire file is on one line. Pressing one of the down a line keys (Such as j, + or <RET> should have no effect despite the illusion that there is more than one line.

Basic X-Windows

Objectives

• On completion, you should be able to:

– Understand the basic concepts behind networked X windowing

– start and stop X

– run shells and user applications under X – set preferences for X

– change window managers and desktops – use X over a network 7.1 What X-Windows Is

X is a windowing system

• Provides the basic graphic functions for Linux

• Designed to provide windowing to any workstation across a network, regardless of OS

• Operates on a client-server model

• Is an application, i.e. not a part of the OS

• The standard Linux X server is Xfree86, commercial alternatives include:

– Metro-X

– Accelerated-X 7.2 X Needs Window Managers

• Window managers provide the controls which allow you manipulate all graphic apps, e.g.

– move, size and stick

– open and close

– maximize, minimize, iconize

– title bars

• Determine the ‘look and feel’ of X, e.g.

– Win95

– Motif

– Next Step

• Can provide ‘virtual desktops’ 7.3 Window Managers Are Applications

• Linux distributions contain many window managers, e.g.

Manager  Description

sawfish

Used with Gnome, lightweight, programmable

fvwm2

Motif-like look

fvwm2

Win95-like configuration for fvwm2, Red Hat default

twm

Bare-bones Tab WM

olwm

Open Look (Sun)

olvwm

Virtual Screen Open Look

WindowMaker

Next Step look, fast and lean

enlightenment Gnome-compatible WM,

powerful, rich, heavy weight

wm2               Ultra-lean

• Window managers are X applications, thus:

– change manager without re-starting X

– change X behaviour without re-start 7.4 Desktop Environments

• X + WM alone don’t provide everything expected of modern desktops, e.g.

– completely integrated drag and drop

– universal access to a clip board

• Desktop Environments bring these facilities to Linux, bundling:

– desktop-capable window manager

– URL-based file manager

– facilities to share clipboard and other data between optimized apps (inc. object linking)

• Linux currently has 3 main desktop environments:

– CDE … the original commercial UNIX standard

– KDE

– GNOME — now adopted by Sun Microsystems as the window desktop for Solaris. Helix Gnome is excelent and is

  available from http://www.helixcode.com.

7.5 Starting X

• Many possibilities

– You may be using the graphical xdm tool which does it for you

– or login to command prompt then type

$ startx

if .xinitrc is not setup:

  $ xinit
  $ window manager filename

7.6 Stopping X

• Stopping:

<CTRL>+<ALT>+<BACKSPACE>

• Use the window manager’s menus

• If you started via xinit, type the following in the startup xterm:

$ exit

• If all these fail, switch to another virtual terminal using the following keys, then kill X from the command prompt:

<CTRL>+<ALT>+<F1> ...  <F4>

7.7 Running Shells (Xterms) Under X

• Even under X, the most productive way to work is often via the command line (i.e. a shell)

• The standard way to access a shell prompt under X is via a terminal emulator called an xterm^{1}

• An xterm shell behaves like a non-X shell, except that you can cut and paste between it and X applications

• Any number of xterms can be open at the same time

• Using ssh (or if you are desperate, telnet) the xterms can provide shells to any number of other hosts

• To start an xterm:

– From an already open xterm:

  $ xterm &

– From a window manager menu (invariably top-level)

1Linux provides other terminal emulators for specialised hosts, but they are rarely necessary. There is also another category of emulators that provide advanced features such as transparent terminals etc.

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

7.8 Running Applications from an xterm

• Character-based apps:

– Run exactly as they would outside X, unless

the xterm itself has been misconfigured

• X applications:

– Type the program’s file name at the prompt: ^{2}

  $ filename &

2The ampersand allows the application to run independently of the shell

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

7.9 Running Applications from a window manager

• Every window manager provides simple menubased access to applications

• Application Menus are usually accessible by

  clicking Mouse Button 1 on:

– Buttons set into a task bar

– The desktop background (root window)

7.10 Configuring X

• Default installations of Linux provide a fully functional setup for using graphic X apps

• 2 different types of X configuration that system administrators or users may need to change:

• Basic configuration of screen, mouse, keyboard behaviour, fonts

– Could be a course in itself (classic O’Reilly manual fills a bookshelf)

– Configuration files best edited via config tools (see next Section 7.11)

• Behaviour of desktop objects (windows, icons, taskbars, xterms)

– Window manager dependent

– Best configured via window manager preferences 7.11 Basic X Hardware Configuration

• Basic configuration for hardware is defined in the

XF86Config file, located in /etc/X11^{3}

• XF86Config is easier to edit using the following tools:

– Xconfigurator … Red Hat tool sets monitor, card, screen mode, colour depth and resolution with probing

– XF86Setup … an X application which edits most basic hardware preferences (Mouse, Keyboard, Card, Monitor, Graphic Modes)

– xf86config a character-based application which prompts for the same settings

– mouseconfig … Red Hat tool sets the mouse type with probing. Useful for setting 2-button mice to emulate 3-button types by simultaneous clicking on both buttons

3On some older systems you may find the X configuration in

/usr/lib/X11,/usr/X11R6/lib/X11 or /var/X11R6/lib/

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

7.12 Basic X Software Configuration

• Under X, the user can configure every conceivable aspect of graphic display

• Users may need to change:

– Screen font sizes, styles, familes

– Pointer behaviour

– Screen colours

– Window manager

• All desktop environments and many window managers provide graphic tools for changing these configurations

• They can be set, on a system-wide or per-user basis, in the following two files:

• .xinitrc

– to set the default window manager and style to be used by the startx command

• ∼/.Xresources or ∼/.Xdefaults

– for fonts, pointers, colours, etc

– Read this file after changing it with xrdb

  ∼/.Xresources

7.13 Networked X — The Client-Server Relationship

• X works in a client-server relationship

• The client is a user application (e.g. netscape) which needs X services to display itself on a given screen

• The server is the application which provides these services, e.g. Xfree86

• On a single-user Linux system, both apps reside on the same system

• On a networked Linux system the user can run an X application which is installed on a remote system but see it displayed on the local monitor, i.e.

– The client application (e.g. netscape) is remote and the X server (e.g. Xfree86) is local 7.14 Principles of Running Remote X Apps

• The most common use for networked X is to run client apps which are installed on remote hosts

• Reasons for running X apps on remote hosts:

– Using graphical tools to administer a remote machine

– No local installation of the app

– Local processing or memory are insufficient

– No local access to data 7.15 How to Run Remote X Apps

The right way (and simplest way) to do this is to use

openssh

• Simply type:

$ slogin  remotehost

• … then type any commands to start a graphical epplication in the terminal window; it just works.

The wrong way:

• Start the local x server:

$ startx

• Enable (dangerous) lack of authentication

$ xhost +remote-hostname

• Open a telnet connection to the remote host:

$ telnet remote-hostname

• Set the your $DISPLAY environment variable on the remote host so that applications re-direct their graphic output to your local monitor:

$ export DISPLAY=oakleigh:0

7.16 Authentication

• Xservers only allow authenticated hosts to connect

• The right way to connect to a remote host is to use openssh. All the other methods described here are difficult, insecure, troublesome… So don’t use them.

• On a trusted LAN you might use xhost in an xterm

$ xhost +beehive

• Or edit /etc/X0.hosts (0 refers to display 0):

$ cat /etc/X0.hosts
landlord
kebab
samosa

• This is dangerous

– Allows hosts to grab your mouse and keyboard

• Only use in a trusted environment 7.17 Better Authentication

• Can use cookie-based authentication

• Done for you if using xdm or ssh

• Clients look in ∼/.Xauthority for cookies to feed to server

• Server must be started with appropriate argument

– Reads its ∼/.Xauthority file

• Server only looks when started – Too late to change once running

• Both server and clients must use the same cookies

– Involves merging ∼/.Xauthority files using

    xauth

• Hard to manage — most resort to xdm for local access or ssh for remote access.

• Documentation is not very penetrable; ssh is much simpler to use, and is secure.

7.18 Basic X Exercises

1. Figure out how to get an X session running

2. In an xterm window type 'xterm' — what happens?

3. In an xterm window type

export DISPLAY=xyz xterm

what happens?

4. Start up another xterm.

(a) Type:

    echo hello

You should get ‘hello’ echoed.

(b) Select the echo hello text so that it highlights — do this by clicking the first mouse button and dragging.

(c) Move the mouse to another xterm window; click into it to make it active if necessary.

(d) You should be able to paste the selected text by clicking the middle mouse button (3 button mouse) or simultaneously clicking both buttons on a 2 button mouse. Try it and see.

5. Find another machine on the same network. Use xhost to tell it to accept connections from your machine. Start an xterm on your machine but tell it (using the DISPLAY variable) to display on the remote machine.

6. Go to the /usr/X11R6/bin directory. Try these commands:

xeyes
xsnow
xfishtank
xbill
xdemineur
xclock

and any others that you like the idea of.

Fundamentals of TCP/IP

Objectives

This module is intended as an introduction to the the basic concepts of IP networking. By the end of it you should understand:

• The history and uses of various protocols

• How subnetting and netmasks work

• About interfaces

• The use of ports 8.1 Fundamentals of TCP/IP Networking

Key concepts:

• Packets

• TCP vs UDP

• Services

• Subnetting inc /xx form

• Routing 8.2 History

• Developed by ARPA for university & military research

• Robust, reliable, wide area network protocol, system-independent

• Will route traffic around network outages (if routing protocols used)

• Came into widespread use in mid-late 1970s

• Popularity hugely helped by free availability of the BSD Unix implementation

i.e. the pre-Linux reference platform • Now the standard protocol — the Internet based totally upon it 8.3 Recap of basic IP Concepts — Components

• Properly, The Internet Protocol Suite (IP Suite)

• Usually erroneously referred to as TCP/IP

• Consists of numerous protocols

• IP is used to encapsulate:

– TCP (Transmission Control Protocol)

– UDP (User Datagram Protocol)

– ICMP (Internet Control Message Protocol)

– other routing & management protocols 8.4 IP versions

• Currently at Version 4 (IPV4) – Entire Internet based on IPV4

– Quickly running out of spare numbers

• IPV6 well standardised – Important improvements

– Currently in miniscule use

– Migration will occur eventually

– Support already in Linux 8.5 Packets

• All data transferred in packets (datagrams)

• Each packet contains various flags & admin information

– Source address (32 bits)

– Destination address (32 bits)

• Addresses identify hosts – Usually an interface on a host

• Addresses are the basis of packet routing

• Packets can be split reassembled, differentially routed, arrive out-of-order or just get lost

• Higher-level protocols (e.g. TCP) add sequencing reliability, flow control etc.

8.6 Addresses

• Addresses shown in ‘dotted decimal’ — break into 4 bytes

– 192.168.0.129

• Four address families – Class A 0.x.x.x–127.x.x.x

– Class B 128.x.x.x–191.x.x.x

– Class C 192.x.x.x–223.x.x.x

– ’reserved’ 224.x.x.x

• Class A network 127 is special – Refers to the current network (any network)

– Current host is always 127.0.0.1

– ‘loopback’ address 8.7 Addresses (continued)

• Addresses identify:

– Network (used for routing between networks)

– Hosts on a particular network ∗ Class A 8 network bits, 24 host bits

∗ Class B 16 network bits, 16 host bits

∗ Class C 24 network bits, 8 host bits

• In all networks, host-parts of all zeros (0) and all ones (255) are reserved

– Host-part zero refers to the network itself

– Host-part all ones is ’broadcast’ address (all hosts)

8.8 Netmasks and subnetting

• Netmasks split host and network part of address

• Says which machines can be reached directly

• Example:

Netmask 255     .255     .255     .0
Binary  11111111.11111111.11111111.00000000
IP      192     .168     .0       .129
Binary  11000000.10101000.00000000.10000001

• To work out the network part

Netmask  11111111.11111111.11111111.00000000
IP       11000000.10101000.00000000.10000001
--------------------------------------------
Result   11000000.10101000.00000000.00000000
--------------------------------------------
           192       168      0         0

• To work out the host part

Netmask  11111111.11111111.11111111.00000000
IP       11000000.10101000.00000000.10000001
--------------------------------------------
Result   00000000.00000000.00000000.10000001
--------------------------------------------
            0         0       0        129

8.9 Netmasks with /xx

• Sometimes see IP addresses given as :

192.168.0.129/24

• /xx is another form of netmask. Says that the left-most xx bits specify the network

– i.e. /24 means 24 ‘1s’, a netmask of:

  11111111.11111111.11111111.00000000

or

  255.255.255.0

• Was rare, but becoming more common

• Examples:

– 10.0.0.0 is network 10

– 192.168.5.0 is network 192.168.5

– 10.255.255.255 is broadcast on network 10

– 192.168.5.255 is broadcast on network 192.168.5 8.10 Transferring Data

• IP allows datagrams to be sent and routed between hosts

• Contains no application-level data

• Data part will be one of UDP, TCP, ICMP etc.

• TCP is ‘session’ oriented data, used for long-lived connections

• UDP used for fire-and-forget messages

• ICMP used for control & testing, not seen by most applications or users

• Examples:

– Email transferred using SMTP over TCP, (maybe many bytes, order important)

– Web pages use HTTP over TCP

– UDP more obscure, used for NFS

– ICMP: ‘ping’ utility, used to test visibility 8.11 Hosts & Interfaces

• Hosts are individual computers/systems

• Each host has one or more interfaces

– Each interface is a point of connection to a

network (often a NIC or modem)

• Many hosts have a single interface, so the address is the host

• May have more than one interface – Interfaces could be on different networks

– Can act as routers, forwarding packets

• Each ‘interface’ will have a single address 8.12 Routing

• Hosts receive packets on one or more interfaces

• Check to see if packet is for current host – If so, deliver to the UDP/TCP etc mechanisms

• Otherwise

– If routing enabled ^{1} ∗ Forward packet to appropriate host

– Routing based on internal routing table

– Manipulated by route command ∗ Superuser only

1Often referred to as ‘IP forwarding’

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

8.13 Ports

• Not enough just to deliver packets to hosts

• Deliver to correct applications on the host – Hosts presumed to be multitasking

• UDP & TCP both include port numbers – 16 bit numbers (0–65535)

– Each UDP/TCP packet contains source & destination port

– sourceport/sourceaddress & destinationport/destinationaddress uniquely identify a conversation 8.14 Ports cont..

• Many ‘well known’ ports published for client-server applications

• See /etc/services under Linux – TCP/25 — SMTP mail

– TCP/23 — telnet (remote terminal access)

– TCP/80 — HTTP (web protocol)

• Unix-like systems reserve ports below 1024 for super-user

• Ordinary users cannot run ‘special’ services without authorisation

• This cannot be trusted in other environments, such as Windows 8.15 Exercises

1. Using ifconfig, explore the interfaces available on your current system

2. Discover the IP addresses of some other machines on your network and check that you can ping them all. What class (A, B or C) of network are they on?

3. From the man page for ping, discover how to set a regular ping running every five seconds. Then investigate how you can send extra-long ping packets (try sending a ping longer than 2K bytes).

4. What ports and protocols are used to run the following services?

• Telnet

• SMTP

• Printer

• Talk

5. What happens if you telnet to various ports? (Try 25 or 110)

6. Use this fact to discover what mail system your machine runs, and see if it runs a webserver (Port 80)

8.16 Solutions

1. ifconfig by default shows a list of the currently configured interfaces including the IP addresses and netmasks.

2. ping 10.0.0.2 will send pings to the 10.0.0.2 interface provided routing is set up correctly. You should be able to find out what class of network you are on from the IP address. See section 8.6 for details.

3. To send a regular ping every 5 seconds use

$ ping -i 5

To alter the packet size you use the -s option to give a size in bytes

$ ping -s 3000

4. The following values are taken from /etc/services

Port  Service
23  Telnet
25  SMTP
515  Printer
517  Talk

5. You should be able to talk directly to the daemon at the other end, e.g.

$ telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK POP3 localhost v4.47 server ready
USER lee
+OK User name accepted, password please
PASS ********
+OK Mailbox open, 2 messages
RETR 1
+OK 332 octets
Return-Path: <lee>
Received: (from lee@localhost)
        by gbdirect.co.uk (8.8.7/8.8.7) id LAA12997
        for lee@localhost; Mon, 14 Feb 2000 11:39:19 GMT
Date: Mon, 14 Feb 2000 11:39:19 GMT
From: Lee <lee@gbdirect.co.uk>
Message-Id: <200002141139.LAA12997@gbdirect.co.uk>
To: lee@gbdirect.co.uk
Subject: Test
Status: RO
This is a test.
.
QUIT
+OK Sayonara
Connection closed by foreign host.

6. You can sometimes find out what webserver a site is using by telnetting to port 80 and requesting the headers of the main page, e.g.

$ telnet www.bbc.co.uk 80
Trying 212.58.224.31...
Connected to www.bbc.net.uk.
Escape character is '^]'.
HEAD / HTTP/1.0
HTTP/1.1 302 Moved Temporarily
Date: Mon, 14 Feb 2000 11:43:06 GMT
Server: Apache/1.3.1 (Unix)
Location: http://www.bbc.co.uk/home/today/
Connection: close
Content-Type: text/html
Connection closed by foreign host.

Practical TCP/IP

Objectives

After completing this module you should be able to understand and utilise:

• Firewalling principles

• Basic firewalling with ipchains

• Network/routing debugging procedures

• Interface configuration under Linux

• The secure shell (sshd, ssh, and scp)

9.1 Ping Protocols

• ping used to test network/host availability

• A little about its implementation – Uses ICMP protocol

– Send requests of type echo-request

– Receives answer echo-reply 9.2 Network Statistics (netstat) in Practice

• Show network status; many options

• Most useful: -r and -n flags (show routes, numeric addresses only)

   $ netstat -rn
   Kernel IP routing table
   Destination   Gateway       Genmask        Flags  MSS Window irtt Iface
   192.168.0.0   0.0.0.0       255.255.255.0  U     1500 0         0 eth0
   202.80.80.0   192.168.0.1   255.255.255.0  UG    1500 0         0 eth0
   192.100.100.0 192.168.0.1   255.255.255.0  UG    1500 0         0 eth0
   194.217.100.0 192.168.0.143 255.255.255.0  UG    1500 0         0 eth0
   192.168.1.0   192.168.0.1   255.255.255.0  UG    1500 0         0 eth0
   192.168.3.0   192.168.0.1   255.255.255.0  UG    1500 0         0 eth0
   127.0.0.0     0.0.0.0       255.0.0.0      U     3584 0         0 lo

• Note ‘gateway‘ above

– route to networks 202.80.80.0, 192.100.100.0, 192.168.1.0 and 192.168.3.0 use gateway 192.168.0.1

– 192.168.0.1 is a gateway (router) which knows how to access those networks

– route to network 194.217.100.0 is via gateway at 192.168.0.143

• Often see destination of 0.0.0.0

– ‘default‘ route

– send all otherwise unrouteable packets to designated gateway

• Iface column shows which interface will be used

• Note interface for 127.0.0.0 — ‘loopback‘ interface; the host itself 9.3 netstat (continued)

• Also show information about connected sockets

• netstat -a shows no. of active connections (useful for seeing system load)

• Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address        Foreign Address    State
tcp        0      0 *:6000               *:*                LISTEN
tcp        0      0 linux.gazcl:domain   *:*                LISTEN
tcp        0      0 localhost:domain     *:*                LISTEN
tcp        0      0 *:linuxconf          *:*                LISTEN
tcp        0      0 *:auth               *:*                LISTEN
tcp        0      0 *:finger             *:*                LISTEN
raw        0      0 *:icmp               *:*                7
raw        0      0 *:tcp                *:*                7
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  1      [ ]         STREAM     CONNECTED     8937   @00000082
unix  1      [ ]         STREAM     CONNECTED     8906   @0000007b
unix  1      [ ]         STREAM     CONNECTED     8933   @00000081
unix  0      [ ACC ]     STREAM     LISTENING     327    /dev/log
unix  1      [ ]         STREAM     CONNECTED     8949   @00000086
unix  1      [ ]         STREAM     CONNECTED     8926   @0000007f
unix  1      [ ]         STREAM     CONNECTED     8644   @00000059

9.4 netstat — Further Examples

• Configured interfaces

 • netstat -i
     Kernel Interface table
     Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
     eth0 1500   0     0      0      0      0  3107      0      0      0 BRU
     lo   3924   0  1035      0      0      0  1035      0      0      0 LRU

• netstat -p shows processes listening on each socket ^{1}

– Includes PID

– Useful to kill processes hogging key ports

$ netstat -pn
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address  Foreign Address  State       PID/name
tcp        0      0 10.0.0.1:1025  10.0.0.2:telnet  ESTABLISHED 443/telnet
tcp        0      0 10.0.0.1:1024  10.0.0.3:telnet  ESTABLISHED 442/telnet
tcp        0      0 10.0.0.1:1023  10.0.0.4:ssh     ESTABLISHED 432/ssh
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags  Type    State      I-Node PID/Program name  Path
unix  1      [ ]    STREAM  CONNECTED  662    432/ssh           @00000037
unix  1      [ ]    STREAM  CONNECTED  591    388/login -- lee  @0000002f

1Only supported in more recent versions

9.5 Network Traffic (tcpdump) in Practice

• Used to monitor network traffic – Need sufficient privilege to monitor devices

• Can show only particular information – Traffic to/from a particular host

– Traffic on a certain port

– Certain types of traffic, e.g. TCP, ARP, UDP

• Very configurable – Decide what you want to do

– Then look at manual page 9.6 tcpdump Options

• Some options

-i Says which network interface to show -n Print IP addresses not names -N Don’t print domain name of address -t Don’t print timestamp -q Show only minimal output (quiet) -v Verbose info (time-to-live etc.)

9.7 tcpdump Examples

$ tcpdump dst host 192.168.0.143 -i eth0 -n -t
tcpdump: listening on eth0
192.168.0.131 > 192.168.0.143: icmp: echo request
192.168.0.131 > 192.168.0.143: icmp: echo request
arp who-has 192.168.0.143 tell 192.168.0.131
192.168.0.131 > 192.168.0.143: icmp: echo request
192.168.0.131.1026 > 192.168.0.143.telnet: S 73945916:73945916(0) win 32120
 <mss 1460,sackOK,timestamp 238586[|tcp]> (DF)
192.168.0.131.1026 > 192.168.0.143.telnet: . ack 3134108710 win 32120 (DF)
192.168.0.131.1026 > 192.168.0.143.telnet: P 0:27(27) ack 1 win 32120 (DF)
192.168.0.131.1026 > 192.168.0.143.telnet: . ack 13 win 32120 (DF)
192.168.0.131.1026 > 192.168.0.143.telnet: P 27:35(108) ack 5 win 32120 (DF)
192.168.0.131.1026 > 192.168.0.143.telnet: P 35:38(3) ack 55 win 32120 (DF)
192.168.0.131.1026 > 192.168.0.143.telnet: P 38:41(3) ack 125 win 32120 (DF)
192.168.0.131.1026 > 192.168.0.143.telnet: . ack 132 win 32120 (DF)
192.168.0.131.1026 > 192.168.0.143.telnet: P 41:42(1) ack 132 win 32120 (DF)

$ tcpdump dst host 192.168.0.143 -i eth0 -N -q
tcpdump: listening on eth0
09:56:32.947997 landlord.mysql > samosa.5660: tcp 166 (DF)
09:56:32.955822 landlord.mysql > samosa.5660: tcp 166 (DF)
09:56:32.963597 landlord.mysql > samosa.5660: tcp 182 (DF)
09:56:32.970917 landlord.mysql > samosa.5660: tcp 166 (DF)
09:56:32.979341 landlord.mysql > samosa.5660: tcp 166 (DF)
09:56:32.987218 landlord.mysql > samosa.5660: tcp 166 (DF)
09:56:32.995902 landlord.mysql > samosa.5660: tcp 555 (DF)

9.8 Firewalling

• Allows you to protect your machine – As well as machines behind them

• Checks packet headers before acting on them – Can ignore, reject or accept packets

– Makes decision based on source, destination, or packet type

∗ Or a combination

• Set up using ipchains under kernel 2.2 – Older kernels used ipfwadm 9.9 Basic Theory

• Two main considerations – Port Filtering

– Host Filtering

• Block services you don’t need

• Limit services you do need to specific machines/networks 9.10 Basic Theory (continued)

• Firewalling can be done with inetd

– /etc/hosts.allow
– /etc/hosts.deny
– /etc/inetd.conf

• Flaw in inetd would still let things through

• Best to drop the packets as soon as possible – Kernel-level filtering

9.11 ipchains

• Packet filtering set up using ipchains

• All the filtering is done in the kernel

  – Not by ipchains

– ipchains just sets up/modifies the rules

• All packets entering and leaving are examined ^{2}

2Including loopback traffic which conceptually leaves the machine

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

9.12 ipchains Details

• Every packet goes through one or more ‘chains’ – A ‘chain’ is a set of rules

– Rules can accept, reject, or deny a packet

∗ Can also send it to another chain

• Three default chains, input, output, forward

– If a packet passes through a default chain without matching:

∗ Fate is determined by the chains policy ∗ Can be Accept, deny, or reject

– If it reaches the end of a user defined chain

∗ Carries on where it left off

• forward is for IP masquerading systems – Not covered here 9.13 ipchains Options

• Dealing with chains:

-N Create a new chain -X Delete an empty chain -P Change the policy for a chain -L List the rules in a chain -F Flush (delete) all rules from a chain

• Dealing with rules:

-A Append a rule to a chain -D Delete a single rule from a chain -I Insert a rule at some point in a chain 9.14 Options For Rules

• Use the following to specify packets to match

-s Source address -d Destination address

-p  Protocol (TCP, UDP, ICMP)

-j chain Jump to chain/action

--sport  Source Port

--dport Destination Port 9.15 ipchains — Examples

• In most cases default chains will be sufficient

• To block all ping requests to our machine:

$ ipchains -A input -p icmp -s 0.0.0.0/0 \
> --icmp-type echo-request -j DENY
$ ipchains -L input
Chain input (policy ACCEPT):
target  prot opt     source       destination    ports
DENY    icmp ------  anywhere     anywhere       echo-request

• To block outgoing ping packets:

$ ipchains -A output -p icmp -d 0.0.0.0/0 \
> --icmp-type echo-request -j DENY
$ ipchains -L output
Chain output (policy ACCEPT):
target  prot opt     source       destination    ports
DENY    icmp ------  anywhere     anywhere       echo-request
$ ping -c1 landlord
PING landlord.gbdirect.co.uk (192.168.0.129): 56 data bytes
ping: sendto: Operation not permitted
ping: wrote landlord.gbdirect.co.uk 64 chars, ret=-1
--- landlord.gbdirect.co.uk ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss

• Very simple examples but they show the theory 9.16 Removing Rules

• Rules can be removed by number, e.g. to delete the first rule in the input chain:

  $ ipchains -D input 1

• or definition, e.g. delete the first matching rule:

  $ ipchains -D output -p icmp -d 0.0.0.0/0 --icmp-type
    echo-request -j DENY

• To clear an entire chain use:

  $ ipchains -F chainname

• If no chainname is given, it clears all chains 9.17 Implementing ipchains

• The rules are normally set up in the machines ‘init scripts’

• Typically by creating a script in init.d that is run just before networking starts

– Example in section 9.19

• Ensure you flush existing rules first (just in case):

    $ ipchains -F

• Generally start with the DENY rules then add what you want

• Maximum security 9.18 Save and restore

• Often useful to create a firewalling ‘config file’

• ipchains-save outputs a text file you can store

[Setup firewall rules as you want]
$ ipchains-save > /etc/ip.rules
Saving `input'.
Saving `forward'.
Saving `output'.
$

• Can reinitialise your firewalling with ipchains-restore and your ‘config file’, e.g.

$ ipchains-restore < /etc/ip.rules
$

• Usually done in a startup script 9.19 ipchains setup script

• A sample script may look like:

#! /bin/sh
# Script to control packet filtering.
# If no rules, do nothing.
# Altered from the ipchains HOWTO
[ -f /etc/ipchains.rules ] || exit 0
case "$1" in
start)
echo -n "Turning on packet filtering:"
/sbin/ipchains-restore < /etc/ipchains.rules || exit 1
echo "."
;;
stop)
echo -n "Turning off packet filtering:"
/sbin/ipchains -X
/sbin/ipchains -F
/sbin/ipchains -P input ACCEPT
/sbin/ipchains -P output ACCEPT
/sbin/ipchains -P forward ACCEPT
echo "."
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0

9.20 Real World ipchains

• Connect out to a host but not in

  $ ipchains -A input -d 192.168.0.131/32 -p TCP -y -j DENY

• -y limits matching to packets with the SYN bit set – Used when establishing connections

• No-one can open a connection from

  192.168.0.131

– Can still connect to it from here … 9.21 Interface Configuration and Management

• An interface is a point of connection to a network

• Usually a single device – Network card

– PPP link

• A device can have more than one interface – Referred to as ‘aliases’

– Commonly used for virtual web sites 9.22 Point-and-Click Interface Administration

• Number of ways to add/edit interface details – Linuxconf

  – Redhat control-panel

– ‘By hand!’

• For most cases you can probably use one of the two graphical methods

• Useful to understand the configuration files behind it all

• See the Linux Network Administrator’s Guide at

  http://www.linuxdoc.org/LDP/nag2/ for much

there is to know about this.

9.23 /etc/sysconfig/network-scripts

• Directory containing scripts and config files ^{3}

• ifup & ifdown activate/deactivate an interface

• Argument specifies interface to act on, e.g.

  $ ifdown eth0

• ifcfg-eth* are config files for each interface

– Should be numbered sequentially from 0 ∗ ifcfg-eth0 is the first interface

– Files ending in :n (where n is a number) are aliases

∗ ifcfg-eth0:0 is the first alias for the first interface

3This applies to RedHat only, you should see section 9.27 for information on other distributions

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

9.24 ifcfg-ethx

• Describes characteristics of a given interface

– What device it should be known as (DEVICE)

– IP address, network, and netmask (IPADDR,

  NETWORK, NETMASK)

– Whether it is activated at boot time (ONBOOT)

– Whether it can be controlled by normal users

  (USERCTL)

• Example:

DEVICE=eth0
IPADDR=192.168.0.129
NETMASK=255.255.255.0
NETWORK=192.168.0.0
BROADCAST=192.168.0.255
ONBOOT=yes
BOOTPROTO=none
USERCTL=no

9.25 Altering An Interface

• It is perfectly allowable to alter interfaces while the system is running

• Requires only minimal disruption to network connectivity

– Not a reboot

• Two simple steps 1. Make alterations (by hand or through GUI)

2. Restart networking

• Networking is just another service

  – /etc/rc.d/init.d/network restart
  – /etc/init.d/network restart

9.26 Adding an Interface

• Adding an alias is even easier!

1. Add the alias

2. Activate it

• Example: Add the following to

/etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
USERCTL=no
ONBOOT=yes
BOOTPROTO=none
BROADCAST=192.168.0.255
NETWORK=192.168.0.0
NETMASK=255.255.255.0
IPADDR=192.168.0.141

• Then execute

$ ifup eth0:0

9.27 The ‘Proper’ Way

• Previous examples use scripts (not always provided)

• You can do everything manually

• Add an alias:

  /sbin/ifconfig eth0:0 192.168.0.128

• Check with ifconfig that it succeeded

• Setup routing to that interface:

  /sbin/route add -host 192.168.0.128 dev eth0:0

• Removing an alias:

   – /sbin/ifconfig eth0:0 down
   – /sbin/route del 192.168.0.128

• Adding an interface is similar …

• Probably want to add a route to the entire network not just the host

  /sbin/route add -net 192.168.0.0 netmask
  255.255.255.0 dev eth1

9.28 Drivers

• Network drivers invariably handled by kernel modules

– PCI NE2000 card handled by ne2k-pci.o

• Kernel cannot tell which module should be used by which interface

– module loader uses lines from

/etc/modules.conf (older:
/etc/conf.modules), e.g.,
alias eth0 ne
alias eth1 ne
options ne io=0x320,0x340 irq=2,12

– Above says that interfaces eth0 and eth1 handled by ne module (NE2000 ISA)

– Options line is module-specific; permits port/IRQ specification if not autodetected: this is common for old ISA cards.

9.29 The Secure Shell in Practice (ssh)

• How you use ssh varies across systems

• Some require stricter authentication than others

• For example, within a secure environment it may not require a password

– Works on ‘trusted host’ concept

– Much better than rsh due to encryption and

server key authentication

• Can often be used as a drop-in replacement for

rsh or telnet

• Has numerous advantages … – Sets up forwarding of X connections

– Can compress the data sent

– No passwords sent in plain text (and hence trivially read by others)

– Not trivially hijacked using hunt (from

  http://lin.fsid.cvut.cz/~kra/) and other

such tools 9.30 Secure Copying in Practice (scp)

• Replacement for rcp

• Much more secure – Encrypts all traffic

– Uses same authentication as ssh

• Can copy local to remote, remote to local or remote to remote

• Example:

  $ scp localfilename user@remotehost:remotefilename

9.31 Summary

• Wide range of network utilities available – Both maintenance and user-orientated

• Very flexible system – Can be hard to setup/maintain

• Pros outweigh cons

• Common jobs become second nature 9.32 Exercises

1. Network tools

(a) Use netstat -rn to investigate the routes on your network. Explain each line of entry to a colleague.

(b) Read the man page for tcpdump. Use it to monitor traffic on your host’s network interface whilst other hosts are pinging each other.

2. ipchains

(a) Use ipchains to set up the following configurations. In each case you should first set up the system by hand, check it. Then set it up so that the firewall rules are in place when the machine reboots.

i. Block all incoming ICMP packets ii. Block only incoming ICMP ‘echo-request’ packets iii. Block all incoming telnet connections iv. Block all telnet connections v. Block all outgoing web requests (Port 80)

3. Network configuration

(a) Using one of the admin tools (linuxconf or control-panel etc.) add an alias on your network interface so that your host can masquerade as some other host. DO NOT DO THIS IF YOU ARE NOT SURE YOU ARE USING A SPARE IP ADDRESS. Investigate what ifconfig and netstat -rn now report. Check that you can ping the alias from another host on the network.

(b) If possible, fit an extra network card to one of the hosts (host b) and configure it to be on a different network. Check it can be pinged from its own host. Go to another host (host a) on the original network and add a route to host b’s new interface, using as a gateway host b’s original network interface. Check that you can ping it and then use traceroute to see the path taken by packets. Host b will have to have IPV4 forwarding enabled for this to work. Ask the tutor about which machine will be set up for this.

9.33 Solutions

1. (a) If you don’t understand the output check section 9.2 or the netstat manpage

(b) tcpdump -i eth0 should monitor all network traffic. If you want to see the traffic

       to a particular host use tcpdump dst host 10.0.0.3

2. (a) The following are the list of rules needed to satisfy each situation. You should flush the chains before each one (ipchains -F).

        i. ipchains -A input -p icmp -j DENY
        ii. ipchains -A input -p icmp --icmp-type echo-request -j DENY
        iii. ipchains -A input -p tcp -d 127.0.0.1 --dport telnet -j DENY
          ipchains -A input -p tcp -d 192.168.0.131 --dport telnet -j
          DENY
        iv. ipchains -A output -p tcp -d 0/0 --dport telnet -j DENYipchains
          -A input -p tcp -s 0/0 --dport telnet -j DENY
        v. ipchains -A output -p tcp -d 0/0 --dport www -j DENY

3. (a) -

(b) Ask the tutor for details.

Shared File Systems

Objectives

After completing this chapter you should be able to:

• Understand basic remote file and print sharing • Appreciate the pros and cons of Samba and NFS • Install Samba and NFS servers

• Configure basic Samba and NFS services

• Access remote resources using SMB and NFS 10.1 NFS (Network File System)

• NFS developed by Sun Microsystems (early 80’s)

• Native method for file sharing between Unix/Linux systems

• Stateless protocol – Means server keeps no state

– Renders server crashes ‘easily recoverable’

• Should be compatible with all Unix-like systems

• Best in trusted environment, not highly secure

• Best where all user/group IDs are same

• Often used with Network Information Services (NIS) to synchronise user/group IDs 10.2 NFS Basics … continued

• Systems are clients, servers or both

• Clients import shared filesystems

• Servers export shared filesystems

• Servers easy to implement via network daemons

• Clients require kernel modifications

• Linux systems normally work as both already

• NFS is NOT Unix/Linux specific (e.g. PC-NFS)

10.3 Exporting File Systems

• Exporting handled by daemons rpc.nfsd and

mountd

• Must be running for NFS export to work

• Exported file systems listed in /etc/exports, format is:

fsname hostname(flags) [hostname(flags)]

• Example:

/tmp *.blah.co.uk(ro)

Exports /tmp to all systems belonging to domain read-only ^{1}

• Important flags:

– ro (read only)

– rw (read/write)

– all squash (map all uid/gid to something)

– anonuid (specify user ID to map to)

– anongid (specify group ID to map to)

• After changing /etc/exports, restart NFS

exportfs -av

or

killall -HUP rpc.nfsd
killall -HUP mount

1For full detail on flags use man exports

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

or

   /etc/rc.d/init.d/nfs restart

10.4 Viewing exports

• Use showmount:

  $ showmount -e
  $ showmount -e hostname
  Export list for landlord.gbdirect.co.uk:
  /usr/local/gbdirect/cvsroot roti.gbdirect.co.uk
  /home/adamg                 roti.gbdirect.co.uk
  /home/andylong              along2.gbdirect.co.uk
  /home/mikeb                 kebab.gbdirect.co.uk
  /mnt/cdrom                  <anon clnt>

• NFS uses a portmapper to handle requests

• This must be running (and you must have access to it) to use NFS

• Check that hosts.allow contains an entry to permit you access, e.g.

  – portmap:  ALL

or

  – portmap:  my.ip.network.

10.5 Importing File Systems

• Mount a remotely exported directory

• Usually have to be superuser

  $ mount hostname:/sharename /local/directory

• If successful, the export named /sharename on host hostname is mounted on your mountpoint

  /local/directory

• Files accessed just as if local

• Remote host must be exporting the directory

• You must have access permission

• Your local mountpoint must exist

• Exactly like mounting a device 10.6 Samba

• Implementation of Server Message Block protocol (SMB)

– Core of Microsoft’s file and print sharing

– Now ‘re-invented’ as CIFS

• Developed in Australia by Andrew Tridgell et al

• Info, sources, distributions at www.samba.org

• High performance – competitive with NT

• Server is purely application code – Not part of the OS

• Can be a client – Requires OS support

– Client module (smbfs) not part of Samba 10.7 Samba Installation

• Will vary — may come preinstalled, may come as RPMs or similar

• Key components are nmbd and smbd

– nmbd is the name services daemon; mostly fit-and-forget

– smbd is the samba server; listens for

connections and then forks one copy per client • Other tools & utilities exist, e.g. smbclient

• Configuration file is /etc/smb.conf

• Later versions come with the Samba Web Administration Tool (swat); listens on port 901 10.8 Samba Basics

• Most likely started as daemons in init scripts

• Can be run-on-demand via inetd, but unlikely – Gives poor performance

• Exclusively uses TCP/IP. Microsoft clients need to be configured for it — they may use NETBEUI

• Permits:

– full file sharing, browsing and domain controller services

– full access to printers

– extensive customising 10.9 Access to Files and Printers

• Linux and Win/NT access controls don’t match

• Various options can be set

• Attempts to match logged-on Windows Username to Linux user names and passwords

• Modern versions use encrypted passwords — takes some setting up (see documentation)

• Has concept of ‘guest’ users — may map to ‘nobody’ on Linux

• Take a look in your smb.conf file and read man

  smb.conf

10.10 Testing Samba

• Use smbclient (see screen dump below)

• May need to provide a password

• Check DIAGNOSIS.TXT from distribution (usually installed at /usr/doc/samba) if you have problems

$ smbclient -L localhost
Added interface ip=192.168.0.129 bcast=192.168.0.255
nmask=255.255.255.0
Password:
Domain=[GBDIRECT]     OS=[Unix]    Server=[Samba 2.0.3]
Sharename       Type    Comment
--------        ---     -------
www             Disk    WWW     files
software        Disk    Installable     Software
tmp             Disk    Temporary       file    space
admin           Disk    GBdirect        admin   files
printers        Printer All     Printers
IPC$            IPC     IPC     Service (Samba  Server)
okirmt          Printer
txtdj           Printer
djrmt           Printer
fax             Printer
Server          Comment
--------       -------
LANDLORD        Samba   Server
Workgroup       Master
--------       -------
GBDIRECT        LANDLORD
WORKGROUP       KEBAB

10.11 Smbclient

• Numerous options:

smbclient servicename  [password]  [-s
smb.conf]  [-B  IP addr]  [-O socket options][-
R name resolve order] [-M Net-BIOS name] [-i
scope] [-N] [-n  NetBIOS  name]  [-d  debu-
glevel] [-P] [-p port] [-l log basename] [-h]
[-I dest IP][-E] [-U username] [-L NetBIOS
name]  [-t  terminal  code][-m  max  protocol]
[-W workgroup] [-T<c|x>IXFqgbNan] [-Ddirectory]
[-c command string]

• Example:

$ smbclient //landlord/admin
Added interface ip=192.168.0.129
bcast=192.168.0.255 nmask=255.255.255.0
Password: xxxxx
Domain=[GBDIRECT] OS=[Unix] Server=[Samba 2.0.3]
smb: \> ls
  q3.dir                 85  Tue Jun 29 13:01:44 1999
  actwin2         D       0  Sun Mar  7 22:01:28 1999
  courses         D       0  Wed May 12 10:02:20 1999
  cvs             D       0  Mon Mar 22 12:36:13 1999
  domreg          D       0  Tue Sep  1 10:14:12 1998
  finance         D       0  Thu Jul  1 12:33:49 1999
  informat        D       0  Wed Jun 23 09:56:34 1999
  julie           D       0  Fri Jul  2 10:06:43 1999
............. etc ...........

10.12 Samba configuration File

• Three sections to smb.conf – global

– directories

– printers, if enabled, will export the printers

    known in /etc/printcap

• Far too much detail to go into here

• Lots of help in the HOWTO files

– Usually under

    /usr/doc/samba-versionnumber, e.g.
    /usr/doc/samba-2.0.5a

• Read the man pages

• Via the web

• and others 10.13 Testing Samba

• Use testparm and smbstatus

– testparm is used before starting Samba to check that smb.conf is ok

– smbstatus reports status of Samba, all connected clients and file share modes

Notes on Testing Samba

• Note that Samba is a server implementation only

• Cannot be used by Linux to import shared files, only export them

• Some Linuxes have import facilities too — but requires kernel support (smbfs module)

10.14 Exercises

1. NFS (a) Set up your local host so you can use showmount to show exported directories.

(b) Find other hosts on your network which list exports.

(c) Set up your host to export /tmp

(d) Go to some other system and mount the exported /tmp

(e) Play with file access on the mountpoint!, e.g. Try accessing files you normally wouldn’t have access to, creating files and seeing what the ownership and permissions are on the local copy.

2. Samba (a) Locate the file DIAGNOSIS.txt

(b) Read through it, then carefully work through all of its instructions to check your Samba installation.

(c) Run testparm on your current smb.conf, pipe the output through less to see the results.

(d) Run smbstatus and explain to your neighbour what the results mean.

(e) Set up a share so that your /etc directory is exported read-only and test it with

   smbclient.

(f) Figure out how to export users’ home directories and get a colleague to test your work.

10.15 Solutions

1. NFS

(a) You should ensure that the portmap and nfs services are running before using

showmount

(b) You can give showmount a hostname to query, e.g.

$ /usr/sbin/showmount -e somehost
Export list for somehost:
/home/adamg                 roti.gbdirect.co.uk
/home/lee                   rafters.gbdirect.co.uk
/backup                     <anon clnt>
/home/james                 oakleigh.gbdirect.co.uk
/mnt/cdrom                  <anon clnt>

(c) You should add the following to /etc/exports:

/tmp    *(ro)

(d) -

(e) -

2. Samba

(a) The file should be in /usr/doc/samba-x.xx/docs/textdocs

(b) You should carry out all the test given to reach a working samba system

(c) Check that your smb.conf is correct

(d) Check the various smb manpages (smb.conf, smbd, smbstatus to see what the output means

(e) You should add the following to your smb.conf, and restart samba with

/etc/rc.d/init.d/smb restart:
[etcshare]
        path = /etc
        comment = Shared etc directory
        writeable = no
        browseable = yes

(f) You should ensure that the homes share is uncommented in smb.conf, and restart samba if necessary. You can test this by using:

$ smbclient '\\localhost\username'
added interface ip=192.168.0.135 bcast=192.168.0.255 nmask=255.255.255.0
Password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 2.0.6]
smb: \> dir
.muttrc          H  1387  Thu Jan 27 11:45:21 2000
.addressbook.lu  H  2285  Mon Jan 24 14:37:29 2000
.procmailrc      H    38  Mon Jan 24 18:11:04 2000
.newsrc.eld      H  1151  Tue Jan 25 13:24:44 2000
.mail_aliases    H    56  Thu Jan 27 16:52:13 2000
Desktop          D     0  Tue Feb  8 15:48:40 2000
.opera          DH     0  Thu Jan 27 12:53:23 2000
.balsarc         H  1391  Wed Feb  9 14:27:36 2000
.mozilla        DH     0  Wed Feb  9 10:27:15 2000
ltculogo.gif        8202  Wed Feb  9 11:49:06 2000
LANDLORD         D     0  Wed Feb 16 14:49:16 2000
nltculogo.xcf      53886  Wed Feb  9 12:54:53 2000
nltculogo.gif       8398  Wed Feb  9 13:09:16 2000

Module 11

Apache Basics

Objectives On completion of this module you should be able to:

• Install the Apache webserver

• Perform basic configuration 11.1 What is Apache?

• Apache is the most widely-used web-server^{1}

• Listens for requests and hands something back

• Normally the contents of a file – Possibly the result of a program

• Designed to be stable and configurable – Fast at serving synamic content

– Use kernel based http server for static content for maximum speed

162.26% of all servers as of September 2000 (Netcraft — http://www.

netcraft.com/Survey/Reports/200009/platform.html)

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

11.2 Installation

• Basic installation is easy

• You may be able to install from your distribution – Most come with Apache

• Otherwise just follow the download instructions from the official site

– http://www.apache.org/

• Then follow the instructions in the INSTALL file

– Normally just

  $ ./configure
  $ make
  $ make install

– If you have problems check the docs

– Available at http://www.apache.org/docs

11.3 How Apache Listens

• Apache runs several processes at any one time – Parent and several children

• Parent ‘watches over’ the children – Tracks how many are answering requests

– Spawns more if free processes drop below a certain point

– Kills spare processes if there are lots free

• Configure child numbers using MinSpareServers and MaxSpareServers directives

– Default is reasonable for a small business

– Tune it for busier sites 11.4 Configuration File(s)

• If compiled from source, Apache installs in

/usr/local/apache

– Earlier versions installed under

  /usr/local/etc/httpd

– Your distribution may differ again … ^{2}

• Configuration file is called httpd.conf

– Older versions use

  ∗ httpd.conf
  ∗ srm.conf
  ∗ access.conf

• Controls what requests Apache answers – and how …

2Redhat installs config files under /etc/httpd and the sample web pages and logs directories under /home/httpd

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

11.5 Key Configuration Directives

• Wide range of configuration directives

• For a very basic server you need at least the following:

– ServerRoot

– DocumentRoot

– ServerAdmin

– BindAddress

– Port

– Listen

– User

– Group 11.6 ServerRoot, DocumentRoot

• Tells Apache where its files live

• ServerRoot tells Apache where its conf and logs directories live

– Not always necessary

– Good practice to have it

• DocumentRoot tells Apache where to look for documents to serve up

• Requested filenames are appended to this

• If you have

DocumentRoot /var/www/html

then a request to

http://www.domain.co.uk/foo.html
points to the file /var/www/html/foo.html

11.7 ServerAdmin

• Apache sometimes can’t complete requests

• In these cases it serves up an error page

• ServerAdmin is given as a contact address

• Usually set to something like

  webmaster@tycm.vtc.edu.hk

– You should of course ensure that it is a valid

email address

• Possible to specify a different error page – Doesn’t have to use ServerAdmin 11.8 BindAddress, and Port

• Tells Apache which requests to answer

• By default Apache listens to every IP address on your machine

– But only to the port given by the Port directive

• BindAddress 192.168.0.1 tells Apache to ignore

anything that doesn’t come in on 192.168.0.1

• Port 8080 ignores all but the specified port

• You can use more than one Port directive, e.g.

  Port 80
  Port 8080

• If you don’t specify a port then a default is used^{3}

• You can only use one BindAddress!

3This is usually 80, but if you are using a binary package then bear in mind whoever compiled your package may have chosen a different value

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

11.9 Listen

• Listen is a replacement for BindAddress and Port

• Given IP:port or just port, e.g.

Listen 192.168.0.1:8080

will answer requests on the IP address 192.168.0.1 and port 8080 and no others

• To answer requests to all valid IP addresses, but only a certain port (e.g. 80) use:

Listen 80

• Can use more than one Listen directive

• Should be used instead of BindAddress and Port in new servers 11.10 User and Group

• Apache should normally be started as root – So it can change the user ID of the children

– These should not run as root

• User and Group directives say what user/group the children should run as

– Important security feature

• Should be set to something that has no real power on your system

– Most people use user and group nobody

• Web documents should be readable by this user

• Nothing should be writeable except log files 11.11 Apache Processes

• Looking at a process list^{4} you can see – The parent

root S Jul  4  0:37 /usr/local/apache/bin/httpd -d /www

– The children

nobody  S  10:11  0:00 /usr/local/apache/bin/httpd -d /www
nobody  S  10:20  0:00 /usr/local/apache/bin/httpd -d /www
nobody  S  10:58  0:00 /usr/local/apache/bin/httpd -d /www
nobody  S  10:58  0:00 /usr/local/apache/bin/httpd -d /www
nobody  S  11:06  0:00 /usr/local/apache/bin/httpd -d /www
nobody  S  11:13  0:00 /usr/local/apache/bin/httpd -d /www

• Spare processes don’t use processor time

– They are ‘sleeping’

• They do use memory, however

– Negligible for a default Apache

– Watch carefully the more modules you add!

– Particularly, mod perl adds a heavy memory requirement.

4Some fields from the ps output have been left out to aid clarity

11.12 Logging

• Apache can log information about accesses

• Use the TransferLog and ErrorLog directives

• TransferLog logs/access log

will log all requests in the file

  ServerRoot/logs/access log

• If the filename starts with a / then it is treated as a proper pathname, not appended to ServerRoot

• ErrorLog is similar but controls where error messages go

– Useful for debugging CGI scripts and misconfigurations

– Check here first if Apache won’t start 11.13 Customizable Logging

• Customizable logs available with CustomLog

CustomLog filename format-string

• format-string consists of ‘% directives’ and/or text

• % directives include:

%b

Bytes sent, excluding HTTP headers

%f              Filename

%{headername}i The contents of headername: header

in the request

%P

The process ID of the child that serviced the request

%r

First line of request

%t

Time, in common log format time format

%T

The time taken to serve the request, in seconds

%u

Remote username (may be bogus if return status (%s) is 401)

%U

The URL path requested

%v

The ServerName of the server answering the request

11.14 CustomLog examples

• To log the referer information in the file

  ServerRoot/logs/referer
   CustomLog logs/referer "%r Refered by: %{Referer}i"

• % directives can be conditional on reply status

   CustomLog logs/referer "%r Refered by: %200,304,302{Referer}i"

– Logs the refering page only on status 200,304,302 ^{5}

• For full details consult the Apache documentation – Gives list of all possible % directives

5For full details consult the Apache documentation

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

11.15 Example Configuration

• A sample configuration file could look like this :

ServerRoot /usr/local/apache
DocumentRoot /usr/local/apache/htdocs
ServerAdmin webmaster@domain.co.uk
Listen 192.168.0.131:80
User nobody
Group nobody
ErrorLog /usr/local/apache/logs/error_log

• We recommend starting with the default httpd.conf rather than from scratch

– Correctly configures many things for you

• The default is well annotated

– Everything after a # character is a comment

– Ignored by Apache

• Apache can check the syntax of its configuration

 – httpd -t
 – apachectl configtest

… if you installed apachectl on your system.

11.16 Basic Exercises

1. Apache Installation (a) Find out if Apache is installed on your machine … if not, install it.

(b) Check Apache is running on your system.

i. You should be able to point your web browser at http://127.0.0.1/ to

check this

     ii. You might have to try http://127.0.0.1:8080/

(c) If Apache is not running, start it

     i. Run /usr/local/apache/bin/apachectl --help for information

(d) If Apache still doesn’t appear to be running, find its configuration and log files

and try to fix the error.

2. Basic configuration (a) Familiarise yourself with the httpd.conf file.

(b) How would you change the directory where the log files are kept?.

(c) How would you change the ’root’ for documents?

(d) How would you enable symbolic links to be followed on the cgi-bin directory? Warning: this is a really bad idea!

(e) Make your site only accessible on Port 8080

(f) Now make it only accessible on the IP address 127.0.0.1, and port 80

(g) Make the changes and check them.

(h) Place the following line in your /etc/hosts file:

       IP_ADDRESS www.test.co.uk www

where IP ADDRESS is the IP address of your machine. You should now be able to

   browse http://www.test.co.uk/

3. Logging

(a) Take a look at the access logs and familiarise yourself with the information they contain.

(b) Set up a custom log to give the time of the request, the request, referer, and number of bytes sent, as well as the time taken to serve the request.

(c) Alter your custom log to show the time taken and bytes sent only if a 200 status response occured.

11.17 Solutions

1. Apache Installation

(a) If Apache is not installed you should be able to install it off a RedHat CD by

   mounting the CD and typing rpm -ivh /mnt/cdrom/RedHat/RPMS/apache.rpm

(b) There are several ways to check this. One is to telnet to port 80 of your machine and see if you get a response.

i. This should work for a default RedHat install, though the port number that Apache first listens on changes in various different packaging so you should try both 80 and 8080.

(c) You can start Apache one of two ways (Which may be the same on some machines!)

• /etc/rc.d/init.d/httpd start

• somepath/apachectl start ^{6}

(d) If you can’t work out why Apache isn’t running ask the tutor for assistance.

2. Basic configuration

(a) You should make sure that you understand everything in the httpd.conf including those sections that are commented out.

(b) Alter the CustomLog and ErrorLog directives to change where the log files are kept, e.g.

       ErrorLog /var/log/myerrorlog
       CustomLog /var/log/myaccesslog common

(c) The ’root’ for documents is specified by the DocumentRoot directive, e.g.

       DocumentRoot /path/to/my/web/documents

(d) You can enable symbolic links by adding Options +ExecCGI FollowSymLinks to the <Directory> section for your cgi-bin, e.g.

       <Directory /path/to/my/web/documents/cgi-bin>
       Options +ExecCGI FollowSymLinks
       </Directory>

(e) Add/Change the Port directive in your httpd.conf file to read Port 8080

 (f) Add the following to your httpd.conf: Listen 127.0.0.1:80

(g) Restart the server and try to access it on both port 80 and 8080. Check that it only works as you expect and fetches documents from the correct place.

(h) Check that you can browse http://www.test.co.uk

6You may have to dig a little to find where this script is

3. Logging

(a) Make sure you understand what each of the columns in the access logs is for. Try tailing the logs as you browse your webserver

(b) The following should create a file newlogformat which holds the desired log format.

LogFormat "%t %U %{Referer}i %b %T" newlog
CustomLog logs/newlogformat newlog

(c) Change your LogFormat line to

LogFormat "$t %U %{Referer}i %200b %200T" newlog
CustomLog logs/newlogformat newlog

Apache

Objectives

On completion of this module you should be able to:

• Set up virtual hosts on the Apache webserver • Use access controls

\underline{Apache 298}

• Set up basic authentication 12.1 Two sites and more …

• Apache can serve multiple sites easily

• Known as ‘Virtual Hosting’, e.g.

  <VirtualHost 192.168.0.2>
  DocumentRoot /www/web.test2/docs
  ServerName www.test2.co.uk
  ServerAdmin www@test2.co.uk
  ErrorLog /www/web.test2/logs/error_log
  TransferLog /www/web.test2/logs/access_log
  </VirtualHost>

to make apache answer requests to address

  192.168.0.2 from /www/web.test2/docs

• Your machine must answer to this address^{1}

• Apache must be listening on the address

– Listen 80 will make Apache answer to all available addresses on port 80

• This is known as IP-based virtual hosting

1If you don’t know how to set up IP aliases ask the instructor

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

12.2 Virtual Hosting Options

• IP-based – Each site must have a unique, IP address

– Uses up valuable IP addresses

– Site accessible by all browsers

• Name-based – Sites share an IP address

– Useful if short of available addresses

– Some browsers may have problems

• Most use IP-based hosting where possible

• Ensures maximum accessibility

• However, Internic are now requiring all sites to use name based hosting as much as possible to reduce drain on IP addresses.

• Name based hosting doesn’t work with SSL encryption.

12.3 Name-based hosting

• Name-based hosting looks like:

NameVirtualHost 192.168.0.3
<VirtualHost foo.domain.co.uk>
ServerAdmin foomaster@domain.co.uk
DocumentRoot /www/foo/docs
ErrorLog /www/foo/logs/error_log
TransferLog /www/foo/logs/access_log
</VirtualHost>
<VirtualHost bar.domain.co.uk>
ServerAdmin barmaster@domain.co.uk
DocumentRoot /www/bar/docs
ErrorLog /www/bar/logs/error_log
TransferLog /www/bar/logs/access_log
</VirtualHost>

12.4 Name-based hosting (continued)

• NameVirtualHost tells Apache that an IP address

can serve multiple hosts

• VirtualHost sections describe how documents for each site are served

– Apache must be able to resolve the names in the <VirtualHost> directives to the IP address

• Apache looks at the Host: header to decide which documents to serve

– Not sent by all browsers

• Requests on other IP addresses will be processed as normal

• Can use both IP-based and name-based hosting 12.5 Block Directives

• Apache has several block directives

– Limit enclosed directives to apply to a certain set of ‘things’

• <VirtualHost> is a block directive

– Enclosed directives apply only to that virtual host

• Others are:

<Directory> ...  </Directory>
<DirectoryMatch> ...
</DirectoryMatch>
<Files> ...  </Files>
<FilesMatch> ...  </FilesMatch>
<Location> ...  </Location>
<LocationMatch> ...  </LocationMatch>

12.6 Block Directives (continued)

• <Directory name> Limits the enclosed directives to apply to everything below the directory name

– name can be anywhere on the filesystem

– Independent of DocumentRoot

• <Location name> is similar but is a URL path rather than a filesystem path

• <Files name> limits directives to files called name – Path of the file is irrelevant

– Only checks the file name, not its location 12.7 DirectoryMatch, et al.

• DirectoryMatch, FilesMatch and
LocationMatch are similar

– Accept regular expressions as arguments, e.g.

<FilesMatch .*\.cgi>
...
</FilesMatch>

• More flexible

• Need more thought to match only intended files 12.8 Access Control

• Create a file in the directory to be protected

  – Usually .htaccess or .acl

– Can be anything

• Example:

  AuthType        Basic
  AuthName        "Members Only"
  AuthUserFile    /www/conf/auth/auth.user
  AuthGroupFile   /www/conf/auth/auth.group
  require group   testgroup
  require user    testuser

• Only the user testuser, or a user in the group testgroup, may access files in this directory

• Validation is done on the files

  /www/conf/auth/auth.user

and

  /www/conf/auth/auth.group

12.9 Access Control (continued)

• Access control is off by default – Unnecessary for most sites

• Switched on by:

  AccessFileName .acl
  <Directory /www/web.test2>
  AllowOverride AuthConfig
  </Directory>

• AccessFileName identifies which filename(s) constitute an Access Control File

• Every directory in the request path is checked for a relevant file

• AllowOverride says that Access Control files can override authorisation directives only

– Can have other values

– Change behaviours through your .acl file

– See Apache docs for further details

• Note that there is quite an overhead if turn on AllowOverride, since web server has to search for this file through each directory in the entire path of each document it fetches.

12.10 Authorisation Files

• Authorisation files are very straightforward

• Group file is groupname:  userlist

• For example:

  firstgroup: user1 user2 user3
  secondgroup: user2 user3 user4
  othergroup: user4 user5 user6

• Listed users belong to that group

• Must create this file by hand 12.11 Authorisation Files (continued)

• User file is a little more complicated

• Format is username:encryptedpassword

• For example:

  testuser:6SlrYaxUFml

• Create/edit this with htpasswd – Part of the Apache distribution

– Give it an authorisation file and a username

  $ htpasswd /www/conf/auth/auth.user newuser
  New password:
  Re-type new password:
  Adding password for user newuser

12.12 Other useful directives

• There are around 200 Apache directives

– More if you add modules e.g. mod ssl

• The previous ones are the essentials

• Some other useful directives are given below:

Directive  Action

Redirect url-path
new-url

Redirect Requests to url-path to

                     new-url

RewriteRule pattern

new-pattern

Rewrite requests, replace pattern with

                     new-pattern

AddEncoding type ext Serve up documents with extension ext

with encoding type type

ForceType type Force all documents to be served up with

MIME type type

HostNameLookups on—off—double Whether to do DNS lookups for logging purposes

ExpiresDefault Set the default expiry time of documents

12.13 Examples

Redirect permanent /ents/theatre/fab-gere http://www.fabgere.com
Redirect /gbdirect/logo.gif http://www.gbdirect.co.uk/logo.gif
Redirect permanent /gbdirect http://www.gbdirect.co.uk/
RewriteEngine on
RewriteRule ^/linuxtraining.*\.htm /ltcu_moved.htm
<Location /LTCU>
AddEncoding x-gzip gz
</Location>
<Location /LTCU-plain>
ForceType text/plain
</Location>
HostNameLookups off
<Location /LTCU>
ExpiresDefault "access plus 1 month"
ExpiresByType text/html "access plus 1 week"
</Location>

12.14 Exercises

1. IP based hosting

(a) Start with the default installation file and add an IP based virtual host:

i. Add an IP alias for your machine (Ensure it doesn’t clash with any others on

your network!) ii. Create a dummy index page so you will be able to tell the difference

between your two sites. iii. Set up Apache to serve this site and check from a browser that everything

works (for both sites) as you expected.

2. Name based hosting

(a) Set up your apache so that it will serve the same sites but on a single IP address (Name-based virtual hosting).

3. Access control

(a) Create two directories on one of your sites and set up access controls so that anyone can see the main index page, testuser can see the first directory and anyone in group testgroup can see the second.

12.15 Solutions

1. IP based hosting

(a) The first thing that you will have to do is set up an IP alias for your machine so that it has two distinct IP addresses. Yu might find it easiest to use the Redhat control-panel for this. If you aren’t sure how to achieve this ask the instructor. A list of spare IP addresses will be made available. An example from a working multi-hosted Apache is given below

Listen 192.168.0.3:80
Listen 192.168.0.2:80
<VirtualHost 192.168.0.3>
ServerAdmin webmaster@gbdirect.co.uk
DocumentRoot /home/www/web.llord/docs
ServerName llord.gbdirect.co.uk
ErrorLog /home/www/web.llord/logs/error-log
TransferLog /home/www/web.llord/logs/access-log
</VirtualHost>
<VirtualHost 192.168.0.2>
ServerAdmin webmaster@gbdirect.co.uk
DocumentRoot /home/www/web.trainingpages/docs
ServerName trainingpages.gbdirect.co.uk
ErrorLog /home/www/web.trainingpages/logs/error-log
TransferLog /home/www/web.trainingpages/logs/access-log
</VirtualHost>

2. Name based hosting

(a) An equivalent example using name-based hosting would be:

NameVirtualHost 192.168.0.2
<VirtualHost llord.gbdirect.co.uk>
ServerAdmin webmaster@gbdirect.co.uk
DocumentRoot /home/www/web.llord/docs
ServerName llord.gbdirect.co.uk
ErrorLog /home/www/web.llord/logs/error-log
TransferLog /home/www/web.llord/logs/access-log
</VirtualHost>
<VirtualHost trainingpages.gbdirect.co.uk>
ServerAdmin webmaster@gbdirect.co.uk
DocumentRoot /home/www/web.trainingpages/docs
ServerName trainingpages.gbdirect.co.uk
ErrorLog /home/www/web.trainingpages/logs/error-log
TransferLog /home/www/web.trainingpages/logs/access-log
</VirtualHost>

Note that the two names given llord.gbdirect.co.uk and trainingpages.gbdirect.co.uk should both resolve to 192.168.0.2

3. Access Control

(a) You should create a file called .htaccess in both directories, the first should be:

AuthType        Basic
AuthName        "First Directory"
AuthUserFile    /www/conf/auth/auth.user
AuthGroupFile   /www/conf/auth/auth.group
require user   testuser

and the second should be:

AuthType        Basic
AuthName        "Second Directory"
AuthUserFile    /www/conf/auth/auth.user
AuthGroupFile   /www/conf/auth/auth.group
require group   testgroup

Key Configuration Files

Objectives

After completing this module, you should be able to configure the following:

• The password files /etc/passwd, /etc/shadow

• The group file /etc/group

• cron management /etc/crontab
• Kernel modules (/etc/conf.modules)

• Filesystem mounting (/etc/fstab and

  /etc/exports)

• System startup and shutdown scripts

13.1 /etc/passwd

• Stores information about users – Password (on some systems)

– Id, and primary group

– Finger information

– Home directory

– Default shell 13.2 /etc/passwd (continued)

• Colon-separated fields, e.g.

lee:Df18jed/nienysd:501:501:Lee Willis,Rm 1,013 567,013 765:/home/lee:/bin/bash

• First field is the username

• Second is the encrypted password ^{1}

• Third and fourth fields give the user ID and the primary group ID respectively

• Finger information is a comma separated list of information about a user

– Typically stores full name, office room, office

phone number and home phone number

• The sixth field is the user’s home directory

• The user’s default shell is given by the last field

1On systems which support shadow passwords this will just be an x, see 13.8 for an explanation

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

13.3 Editing /etc/passwd

• You should never edit /etc/passwd directly – Can lose information on multi-user systems

• Use the passwd command

• Normal users simply type passwd – Prompted for old password

– Type new password twice (to avoid typos)

• Superuser can change anyone’s password

passwd username

– Enters only the new password

– Don’t have to know the old password

• Superuser may also disable/enable accounts

– passwd -l username disables or locks an account

– passwd -u username unlocks the account 13.4 Other Changes To /etc/passwd

• chfn allows you to change the finger information for a user e.g.

   $ chfn -f "Lee Willis" -o "Room 1" -p "01234 5678" -h "0123 45678"

• chsh -s shell lets you change your default shell

– Must be listed in /etc/shells

– chsh --list-shells will give a list of valid values

• Example:

  $ chsh --list-shells
  /bin/bash
  /bin/sh
  /bin/ash
  /bin/bsh
  /bin/tcsh
  /bin/csh
  $ chsh -s /bin/tcsh lee
  Changing shell for lee.
  Password:
  Shell changed.

• Note: Both chfn and chsh require you to give your password

13.5 /etc/group

• Effective control of file access is one of the strengths of Linux/Unix

• One aspect of this is the concept of groups

• Users belong to one or more of these groups

• Access to files can be granted or denied on the basis of group priveleges

• Group membership is controlled by the file

  /etc/group

13.6 Editing /etc/group

• Like /etc/passwd shouldn’t be edited directly

• Tools can change it and ensure locking

• To create a group with ID gid and name gname:

    $ groupadd -g gid gname

• To change name of group gname to newname:

    $ groupmod -n newname gname

• usermod changes the groups a user belongs to, e.g. to add the user lee to groups www,

    project, and tempgroup:
     $ usermod -G www,project,tempgroup lee

• N.B. It also removes him from any groups not listed (excluding his primary group)

• usermod can also change the information in

  /etc/passwd

– Can only be run by the superuser 13.7 Important Note

• Changing user information shouldn’t be undertaken lightly

• There are a number of restraints on changing usernames, IDs, and group IDs

• You can’t change name while a user is logged in

• You can’t change ID while user has processes running

• See man usermod and man groupmod for others

• Mostly common sense 13.8 Shadow Passwords

• shadow passwords are a security feature

– Normal users could get others’ passwords if encrypted versions were readable

– Some information in /etc/passwd needs to be

readable, but Passwords don’t!

• Solution:

– Keep everything except passwords in

  /etc/passwd

– Password field contains just a single ’x’

• Encrypted passwords are stored in /etc/shadow – Only readable by superuser

13.9 /etc/shadow

• /etc/shadow also stores other information

• Mainly password expiry information

• Can force users to change their password

• Most important benefit is increased security

• All modern systems should use shadow passwords 13.10 Scheduling Jobs (Cron)

• cron schedules jobs to run at times; specified in

  the file /etc/crontab
   SHELL=/bin/bash
   PATH=/sbin:/bin:/usr/sbin:/usr/bin
   MAILTO=root
   HOME=/
   # run-parts
   01 * * * * root run-parts /etc/cron.hourly
   02 4 * * * root run-parts /etc/cron.daily
   5,35 * * * 1-5 root /usr/local/bin/domail

• The first section sets environment variables

• Cron jobs run when the current time/date matches a crontab entry

• The first 5 fields in /etc/crontab are

  minute hour day_of_month month day_of_week

• * Matches all possible values

• Commas separate sets of values within a field

• Ranges can also be specified, e.g. [1-5]

• Can also specifiy steps, e.g. [0-59/5]

13.11 /etc/crontab

• /etc/crontab also specifies what user the job runs as, e.g.

01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily

• Second line runs the command run-parts

/etc/cron.daily as root at 4:02am every day
• To run the command /usr/local/bin/domail as

root at 10 minutes past and 40 minutes past the hour, between 9am and 6pm on weekdays:

10,40 9-18 * * 0-5 mail /usr/local/bin/domail

13.12 run-parts

• run-parts is a script designed for use with cron

• Runs all the programs in the specified directory

• Allows administrators to easily add jobs

– Simply place an executable script/program in

the correct directory

• N.B. Not a standard cron feature

13.13 logrotate

• Log rotation is normally handled by logrotate

• Run by cron, which reads /etc/logrotate.conf for configuration

• Example:

$ cat /etc/logrotate.conf
#Rotate the logs weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# send errors to root
errors root
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own lastlog or wtmp --- we'll rotate them here
/var/log/wtmp {
    monthly
    rotate 1
}
/var/log/lastlog {
    monthly
    rotate 1
}

13.14 Module Configuration

• The Linux kernel can be modular in nature

• Needs to know which devices use which drivers

• /etc/conf.modules contains this information ^{2}

• Typical file may look like :

  alias eth0 ne2k-pci
  alias eth1 3c509

• States that the device eth0 requires the module

  ne2k-pci, and eth1 requires 3c509

2Warning, on some systems this is /etc/modules.conf

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

13.15 Modules Configuration — ‘Options’

• Some modules allow you to specify options

• Mainly used for ISA peripherals, e.g. to provide I/O and IRQ information:

alias eth0 ne
options ne irq=10

Specifies that eth0 requires the module ne which should be passed the argument irq=10

• Can also specify actions to be executed when loading unloading modules, e.g.

pre-install pcmcia_core /etc/rc.d/init.d/pcmcia start
Run /etc/rc.d/init.d/pcmcia start before

loading the pcmcia core module 13.16 Mounting Filesystems

• Linux can store its files on multiple disks

• It decides what part of the filesystem each of these lives on using /etc/fstab

Logical Volume Mount Point FS type Options Dump

Check order

/dev/hda1  /  ext2  defaults  1  1
/dev/hda5  /home  ext2  defaults  1  2
/dev/hda7  /tmp  ext2  defaults  1  2
/dev/hda6  /usr  ext2  defaults  1  2
/dev/hda8  swap  swap  defaults  0  0
/dev/fd0  /mnt/floppy  ext2  noauto  0  0
/dev/cdrom  /mnt/cdrom  iso9660  noauto,ro  0  0

\\kashmir\c /mnt/kashmir smbfs guest 0

0

landlord:/var/admin /var/admin nfs defaults 0

0

landlord:/home/lee /home/lee/LANDLORD nfs defaults 0 0

13.17 Runlevels

• Linux has several modes of operation

• Referred to as runlevels

• Most common are:

0  Initial boot
1  Single User Mode
2 and above Multi-user mode

• Apply to most UNIX/Linux, but some allocate different numbers to graphical mode/login ^{3}

• Unfortunately, the app and daemons run automatically at each level vary greatly

• A good justification for the Linux Standard Base

• See your distribution documentation for details

3Red Hat uses 5 for graphic mode, but others don’t.

\begin{matrix}c \\ ◯ \end{matrix} GBdirect Ltd, 2000  www.linuxtraining.co.uk

13.18 Single User Mode

• Mainly used for diagnostic purposes

• Starts only a subset of the possible services, e.g. – No networking

– No mail services

– No name lookup services

  ∗ Except /etc/hosts

– No file-sharing services etc 13.19 Multi User Mode

• The ‘normal’ operating state

• All configured services are running

• Multiple users can log in

• /sbin/runlevel shows the previous and current runlevel of your machine 13.20 Starting up and Shutting down

• Only the superuser can shutdown or reboot

• halt will shut down the machine totally – For safety you should type /sbin/halt

• Makes sure all processes are stopped

• Stops services cleanly

• Writes unsaved data to the disk – ‘Syncing’

• /sbin/reboot will shut down cleanly and reboot 13.21 Changing runlevel

• It is sometimes necessary to change runlevel

• Rare, but useful to know

• You can instruct a system to change runlevel using the telinit command

• Example:

  $ telinit 5

• Changes to runlevel 5

• telinit 1 takes the system down to single user mode 13.22 Initscripts

• The precise behaviour of each of the runlevels is controlled by initscripts

• Control which services run in each runlevel

• Live in init.d – On Debian it’s in /etc/init.d

  – On Redhat it’s /etc/rc.d/init.d

• Each file here is a script that can be called with

  an argument, start, stop, or restart

13.23 rcn.d

• The contents of the directories rcn.d control which services start and stop in runlevel n

• The directories hold symbolic links to the files in

  init.d

• The links are named informatively

• To start service abc you would create a link typically named Sxxabc, to init.d/abc

• The xx specifies the order to run the scripts, e.g. S00foo will be run before S90foo

• Links that stop a service are of the form Kxxabc 13.24 Initscripts — An example

• Consider the following: ^{4}

lee @ 12:22:08 /etc/rc.d/rc3.d ls -l S*
lrwxrwxrwx ... S01kerneld -> ../init.d/kerneld
lrwxrwxrwx ... S10network -> ../init.d/network
lrwxrwxrwx ... S15nfsfs -> ../init.d/nfsfs
lrwxrwxrwx ... S20random -> ../init.d/random
lrwxrwxrwx ... S30syslog -> ../init.d/syslog
lrwxrwxrwx ... S40atd -> ../init.d/atd
lrwxrwxrwx ... S40crond -> ../init.d/crond
lrwxrwxrwx ... S40portmap -> ../init.d/portmap
lrwxrwxrwx ... S40snmpd -> ../init.d/snmpd
lrwxrwxrwx ... S45pcmcia -> ../init.d/pcmcia
lrwxrwxrwx ... S50inet -> ../init.d/inee
lrwxrwxrwx ... S55named -> ../init.d/named

• We can see that the first thing started is kerneld, followed by network services, nfs services, etc

• There are also a series of Kxxyyy scripts which shut down the services in a sensible order

4Unimportant information has been removed from the screen dump so do not be alarmed if this doesn’t look like you’d expect!

13.25 Restarting Services

• Can be necessary to restart a particular service, e.g. so it can re-read a modified configuration file

• This can be done without a complete reboot

• It must, however, be done by the superuser

• To restart samba (smb) we can do the following:

  $ cd /etc/rc.d/init.d
  $ ./smb restart

13.26 Exercises

1. Passwords (a) Find out whether your machine is using standard or shadow passwords?

2. Users

(a) Add a new user (useradd) and set them up with the correct Full Name,

password, home directory. Set their default shell to csh

3. Groups (a) Create a new group and add your user to this group

(b) Now remove both the user and the group. How would you ensure that all files

belonging to that user have been removed?

4. Scheduling

(a) Add a cron job to eject your CDROM drive at 5 minutes past every hour and put

it back in at ten minutes past the hour

5. Mounting

(a) Set up your fstab so that

   $ mount /dev/cdrom

will automatically mount your CD drive under /mnt/cdrom

6. Runlevels

(a) Switch your machine between runlevels 3 and 5. What is happening? What happens if you change to runlevel 6?

(b) Make sure your machine runs the same set of services in both runlevels

7. Stop, Start and Restart Services (a) Check you can stop, start, or restart services

(b) Can you do this as a normal (ie non-root) user?

13.27 Solutions

1. Passwords

(a) Your machine will have an /etc/shadow file if it is using shadow passwords. The password field will be set to an ’x’ in /etc/passwd.

2. Users

(a) The following would set the details for the user Lee Willis

$ useradd leewillis
$ passwd leewillis
Changing password for user leewillis
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully
$ chfn -f "Lee Willis" leewillis
Changing finger information for leewillis.
Finger information changed.
$ chsh -s /bin/csh leewillis
Changing shell for leewillis.
Shell changed.

The home directory should be set up properly (/home/leewillis), if not you can change it with

$ usermod -d /home/leewillis leewillis

3. Groups

(a) $ groupadd newgroup
$ usermod -G newgroup leewillis

(b) To remove the group, the user and the user’s home directory

$ groupdel newgroup
$ userdel -r leewillis

There are a few important points here! Firstly there may still be files in the filesystem belonging to that user. To locate them all you should have done

find / -user leewillis -exec rm -f {} \;

prior to removing the user. You should also have located all files belonging to the group and re-parented and/or removed them before removing the group

4. Scheduling

(a) The following lines should acheive the desired effect

05 * * * * root eject /dev/cdrom
10 * * * * root eject -t /dev/cdrom

5. Mounting

(a) The entry should look like

/dev/cdrom       /mnt/cdrom       iso9660 noauto,ro     0 0

6. Runlevels (a) You can change runlevels by using telinit 5 and telinit 3. All non-relevant

services are stopped and the new ones started each time you change runlevel. Runlevel 6 reboots the machine!

(b) You should ensure that the directory listings for /etc/rc.d/rc3.5 and /etc/rc.d/rc5.d are the same. This should ensure that the same services are started/stopped when entering either runlevel.

7. Start, Stop and Restart services

(a) -

(b) -