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