Agenda: intro-ubuntu-exercises.txt

File intro-ubuntu-exercises.txt, 9.2 KB (added by damas, 5 years ago)
Line 
1Linux System Administration and IP Services
2
3Initial Ubuntu System Administration
4------------------------------------
5
6Notes
7------
8
9* Commands preceded with "$" imply that you should execute the command as
10  a general user - not as root.
11* Commands preceded with "#" imply that you should be working as root using
12  "sudo"
13* Commands with more specific command lines (e.g. "RTR-GW>" or "mysql>")
14  imply that you are executing commands on remote equipment, or within
15  another program.
16
171. Find out what's installed
18   -------------------------
19
20Log on to your machine using SSH as the user specified in class.
21
22Once you are logged in, take a look at all the packages installed on your
23system:
24
25        $ dpkg --get-selections
26
27All installed packages fly by on the screen. Let's slow that down:
28
29        $ dpkg --get-selections | less
30
31The "less" command lets you quickly search text. Is the "openssh-server" server
32installed on your machines?  (It should be if you are logged in :)
33
34        Type "/openssh" and press <ENTER>
35
36You should see something like:
37
38        openssh-client                                 install
39        openssh-server                                 install
40
41with the "openssh" text highlighted. Press "q" to exit the less screen.
42
43Another way to see packages is:
44
45        $ dpkg --list | less
46
47Try it!
48
49
50OK, what version of "openssh-server" is installed?
51
52        $ apt-cache policy openssh-server
53
54Or, you could also say:
55
56        $ dpkg --list openssh-server
57
58
592. Find out if a package is available to be installed
60   --------------------------------------------------
61
62You have a local cache of all packages available to be installed from the Ubuntu
63package repositories. You can search this cache using the "apt-cache" command. Before
64you can use apt-cache the first time you need to update your local cache. Let's do this
65now (we did this for you when setting up your machine):
66
67        $ sudo apt-get update
68
69Once this completes we can search for available packages. Let's see if the "ipcalc"
70package is available in our Ubuntu repositories:
71
72        $ apt-cache search ipcalc
73
74It looks like there are three packages matching the name "ipcalc". Try typing:
75
76        $ sudo apt-get install ipcalc
77        [sudo] password for sysadm: .... <- your password
78
79        $ ipcalc 41.93.45.101/24
80
81This is very useful! We'll talk more about what all this means later today or
82tomorrow.
83
84
853. Stopping and starting services
86   ------------------------------
87
88The scripts to run services on your machine are located in /etc/init.d/. By default,
89when Ubuntu installs a package the startup scripts for the package are run and the
90package is configured to automatically run at system startup.
91
92Try viewing the status of the ssh server, stopping and starting the server and
93reloading the server's configuration file (/etc/ssh/sshd_config):
94
95The control script for ssh is here:
96
97        /etc/init.d/ssh
98
99... but it is more common in modern Linux to use the "service" command to control
100services:
101
102        $ service ssh help
103
104You are shown the commands you can perform on the ssh service.
105
106Try to view the status of the ssh server:
107
108        $ sudo service ssh status
109
110Since we are connected using ssh we cannot stop this service. If we did, then you would
111lose your connection and need to go to your machine's console to manually restart the
112service.
113
114is a web server running:
115
116        $ sudo service apache2 status
117
118Yes? Let's look at the default page of your machine's web server:
119
120        $ lynx localhost
121
122Type "q" to exit this text-based web browser (a very powerful tool).
123
124Let's stop the Apache web server:
125
126        $ sudo service apache2 stop
127
128Can you see the web server's default page any more?:
129
130        $ lynx localhost
131
132Let's start the server again:
133
134        $ sudo service apache2 start
135
136And, verify that you can see the page:
137
138        $ lynx localhost
139
140Later today, or tomorrow we'll look at other ways to check for running services.
141
142
1434. Turning a service off
144   ---------------------
145
146If, for some reason, you decide that a currently running service should be turned off
147permanently, but that the software should not be removed, then you need to use the
148update-rc.d utility.
149
150To stop the Apache web server permanently you would do:
151
152        $ sudo update-rc.d apache2 disable
153
154Did you see something like this?
155
156 Disabling system startup links for /etc/init.d/apache2 ...
157 Removing any system startup links for /etc/init.d/apache2 ...
158   /etc/rc0.d/K09apache2
159   /etc/rc1.d/K09apache2
160   /etc/rc2.d/S91apache2
161   /etc/rc3.d/S91apache2
162   /etc/rc4.d/S91apache2
163   /etc/rc5.d/S91apache2
164   /etc/rc6.d/K09apache2
165 Adding system startup for /etc/init.d/apache2 ...
166   /etc/rc0.d/K09apache2 -> ../init.d/apache2
167   /etc/rc1.d/K09apache2 -> ../init.d/apache2
168   /etc/rc6.d/K09apache2 -> ../init.d/apache2
169   /etc/rc2.d/K09apache2 -> ../init.d/apache2
170   /etc/rc3.d/K09apache2 -> ../init.d/apache2
171   /etc/rc4.d/K09apache2 -> ../init.d/apache2
172   /etc/rc5.d/K09apache2 -> ../init.d/apache2
173
174these are logical links in the file system telling it to not run the Apache
175web server at any runlevel the next time the machine starts. If you really did
176not want the Apache web server to be running any more right now, then you would,
177also, need to manually stop the service.
178
179Oops! But, we'll need the web server. Let's re-enable the server:
180
181        $ sudo update-rc.d apache2 enable
182
183Type man update-rc.d for more details on how this works.
184
185
1865. Reboot your system
187   ------------------
188
189To restart your system, you could use:
190
191        $ sudo shutdown -r TIME
192
193... where time can be a day, hour, minute...
194
195Or you could try and reboot your machine *NOW*:
196
197        $ sudo shutdown -r now
198
199The "-r" means reboot.  Another command for doing this is "reboot".
200Go ahead and reboot your machine. You will lose your ssh connection,
201have to wait a few moments and then be able to reconnect to your machine.
202
203To stop a machine you could do (don't do this now!):
204
205        # halt -p
206
207or
208
209        # shutdown -h -P now
210
211Be careful when using halt on remote systems! Don't do this in class. If you
212do let the instructor know and they'll restart your machine.
213
214
2156. Figure out how your machine has been partitioned
216   ------------------------------------------------
217
218You want to display free disk space, or "df":
219
220        $ df -h
221
222Use:
223
224        $ man df
225
226to understand what the "-h" option does.
227
228Look in /etc/fstab. This is where file systems are mounted in Linux. Read the
229man page on this file:
230       
231        $ cat /etc/fstab
232        $ man fstab
233
234Notice that defined file systems are pointing to /dev/vda*. Have a look
235at these files:
236
237        $ ls -lah /dev/vda*
238        $ file /dev/vda*
239
240What type of files are these?
241
2427. Use the top command
243   -------------------
244
245The top command let's us see the status of our system at a quick
246glance. To use top simply do:
247
248        $ top
249
250The item at the top of list of running processes is the process using
251the most CPU resources.
252
253Open a new SSH connection to your PC. In that window type:
254
255        $ ls -lahR /
256
257Now in the other window where top is running you should start to see the "ls"
258process listed using some amount of your total CPU.
259
260At the top of the top window you'll see something like:
261
262        top - 03:17:03 up  1:47,  2 users,  load average: 0.51, 0.19, 0.09
263        Tasks:  79 total,   2 running,  77 sleeping,   0 stopped,   0 zombie
264        Cpu(s):  4.9%us, 10.9%sy,  0.0%ni,  3.6%id, 79.6%wa,  1.0%hi,  0.0%si,  0.0%st 
265        Mem:    508924k total,   491968k used,    16956k free,    59052k buffers
266        Swap:   905208k total,     4584k used,   900624k free,   128712k cached
267
268This is a good, quick way to see how much RAM, Virtual memory, CPU,
269total running processes, etc. that your machine has, and is using.
270
271You can adjust the output of top as it is running. Exit from top by
272typing "q" and then do:
273
274        $ man top
275
276Now run top again and change what it is displaying interactively.
277
278All the information in top is part of a dynamic file system located in
279/proc. As an example do the following:
280
281        $ cd /proc
282        $ ls
283
284The numbered directories correspond to actual Process IDs of processes
285that are running. Look at the file meminfo:
286
287        $ less meminfo
288
289Remember: space bar to go to the next screen of output.
290
291Note that it includes your total RAM. Top uses this file to get this
292information. Same for cpuinfo, loadavg, uptime, etc.
293
294If you want to know what command was executed to start a number process
295you can type (for instance):
296
297        $ less /proc/1/cmdline
298
299You'll see that the first process started on the system is init.
300
301
3028. Viewing your log files in real time
303   -----------------------------------
304
305Now that you have two ssh windows open to your machine do the following:
306
307In one window type:
308
309        $ tail -f /var/log/apache2/access.log
310
311In the other window do
312
313        $ lynx localhost
314
315The "q" to quit, then do:
316
317        $ lynx localhost/junk
318
319Do you see the log messages indicating your access to the main page, and your
320attempt to access localhost/junk, which does not exist. Note the "404" on the
321output line of the message. The number 404 means "Not Found".
322
323Now do:
324
325        $ cd /var/log/apache2
326        $ ls
327
328Note there are several log files.
329
330Look for "404" in the access.log log file:
331
332        $ grep 404 access.log
333
334Now let's make your web busy and watch this in the log file. Be sure you still
335have one ssh window with the tail command running:
336
337        $ sudo tail -f /var/log/apache2/access.log
338
339In the other window do:
340
341        $  while :; do lynx -dump localhost; sleep 1; done
342
343Note the timestamp column in the access.log update each second. When you are done
344go to the window where you are running the "while" loop and press ctrl-c to
345terminate the process.
346
347
348