Agenda: commands-exercises.md

File commands-exercises.md, 5.0 KB (added by andy, 5 years ago)
Line 
1% Linux System Administration and IP Services
2
3# Notes
4
5* Commands preceded with "$" imply that you should execute the command as
6  a general user - not as root.
7* Commands preceded with "#" imply that you should be working as root
8  with "sudo"
9* Commands with more specific command lines (e.g. "RTR-GW>" or "mysql>")
10  imply that you are executing commands on remote equipment, or within
11  another program.
12
13# Exercise
14
15## Log in as the nsrc user
16
17If you have been allocated a virtual machine by the instructor,
18you will log in as SSH. If this is a machine running inside VirtualBox
19on your laptop, you will probably log in directly on the console.
20
21~~~
22username: nsrc
23password: <given in class>
24~~~
25
26## Become the root user
27
28At the command prompt type the following command:
29
30~~~
31$ sudo -s
32~~~
33
34Enter the class user's password when prompted
35
36Now that you are root the command prompt will change. We indicate this
37using the "#" symbol.
38
39You are now the super user - be careful!
40
41Ok, exit the root account:
42
43~~~
44# exit
45$
46~~~
47
48## Look at the network configuration of your host
49
50~~~
51$ cat /etc/network/interfaces
52~~~
53
54The IP configuration of your host is either done using DHCP, or configured
55statically. Which is it in your case ?
56
57"cat" is for "concatenate" and is one way to view what is in a file.
58
59## List files
60
61Use `ls` to list files:
62
63~~~
64$ cd            [go to your home directory]
65$ ls
66~~~
67
68Do you see anything? Try this instead:
69
70~~~
71$ ls -lah
72~~~
73
74What's inside one of these files?
75
76~~~
77$ cat .profile
78$ less .profile
79~~~
80
81Press `q` to get out of the less display.
82
83Another command:
84
85~~~
86$ clear
87~~~
88
89If you don't understand what cat, clear or less do, then type:
90
91~~~
92$ man cat
93$ man clear
94$ man less
95~~~
96
97## Working with the command prompt
98
99You can recall previous commands by using the up-arrow and down-arrow
100keys. Give this a try now.
101
102Alternately, try typing this command:
103
104~~~
105$ history
106~~~
107
108If you wish to execute one of the commands in the list you saw type:
109
110~~~
111$ !nn
112~~~
113
114Where `nn` is the number of the command in the history list. This
115is useful if you want to run a past command that was long and/or
116complicated.
117
118## Command completion
119
120With the bash shell you can auto-complete commands using the tab key.
121This means, if you type part of a command, once you have a unique string
122if you press the TAB key the command will complete. If you press the TAB
123key twice you'll see all your available options. Your instructor will
124demonstrate this, but give it a try by doing:
125
126~~~
127$ hist<TAB>
128$ del<TAB><TAB>
129$ rm <TAB><TAB>         [Include the space after the "rm"]
130~~~
131
132## Working with pipes
133
134We saw an example of using pipes when we sorted the contents of our
135/sbin directory during the presentation. What if you wanted to have this
136information available in a file and sorted?
137
138~~~
139$ cd
140$ ls /sbin | sort > sbin.txt
141~~~
142
143Now view the contents of what is in sbin.txt to verify that this worked.
144
145~~~
146$ less sbin.txt
147~~~
148
149Press the "q" key to quit viewing the contents.
150
151## Finding text strings
152
153Use the command grep to print lines matching a pattern in a data stream
154(such as a file). For example, view the entry for the nsrc account in
155the system passwd file:
156
157~~~
158$ grep nsrc /etc/passwd
159~~~
160
161You should see something like:
162
163~~~
164nsrc:x:1000:1000:System Administrator,,,:/home/nsrc:/bin/bash
165~~~
166
167The previous items above are:
168
169~~~
170userid:passwd:uid:gid:Name,extrastuff,,:HomeDir:LoginShell
171~~~
172
173grep is often used with a pipe to FILTER the output of commands. For instance:
174
175~~~
176$ history | grep ls
177~~~
178
179Will display your previous use of the ls command from exercise 2.
180
181## Editing the command line revisited
182
183It is particularly useful to realize that you can edit a command just as
184you would a line of text in a file. For instance, you can:
185
186- Use your back-arrow (left) and forward-arrow (right) keys to change
187  text in a command.
188- Use the Home and End keys to go to the start and the end of a command:
189
190~~~
191+ ctrl-a = start
192+ ctrl-e = end
193~~~
194
195- NOTE: you do not need to go to the end of a command before pressing
196  `<ENTER>` to execute the command.
197- You can use the history command with grep to find a previous command.
198  You can copy and paste this command, then edit it to make adjustments.
199  For long commands this can save considerable time.
200- To terminate a command without executing it press ctrl-c
201
202- Alternatively you can use the reverse-search feature of bash:
203
204    1. Press `<CTRL>-R`
205    2. type the term you are searching for.
206    3. Press `<CTRL>-R` again to cycle through all occurrences of the
207       term in your history.
208    4. Press the right or left-arrow, HOME or END key to start editing the command.
209
210Let's give some of these editing rules a try:
211
212~~~
213$ ls -lah /usr/lib/ | grep postfix
214~~~
215
216Then, let's look for postfix
217
218`<CTRL>-R`, type `postfix`, then press left arrow. Edit the previous
219command (which you should now have) and change `/usr/lib/` to
220`/usr/sbin/`. Use the left+right arrow key to move, and backspace to
221erase. You should now have:
222
223~~~
224$ ls -lah /usr/sbin/ | grep postfix
225~~~
226
227With your cursor just past the `/` in `/sbin/`, press <ENTER> to execute
228the command.