| 1 | % Linux System Administration and IP Services |
|---|
| 2 | |
|---|
| 3 | # Linux Commands - part 2 |
|---|
| 4 | |
|---|
| 5 | # Notes |
|---|
| 6 | |
|---|
| 7 | * Commands preceded with "$" imply that you should execute the command as |
|---|
| 8 | a general user - not as root. |
|---|
| 9 | * Commands preceded with "#" imply that you should be working as root |
|---|
| 10 | with "sudo" |
|---|
| 11 | * Commands with more specific command lines (e.g. "RTR-GW>" or "mysql>") |
|---|
| 12 | imply that you are executing commands on remote equipment, or within |
|---|
| 13 | another program. |
|---|
| 14 | |
|---|
| 15 | # Exercise |
|---|
| 16 | |
|---|
| 17 | ## Install a new software package (Apache) |
|---|
| 18 | |
|---|
| 19 | We will now install the Apache web server on your machine: |
|---|
| 20 | |
|---|
| 21 | ~~~ |
|---|
| 22 | $ sudo apt-get install apache2 |
|---|
| 23 | ~~~ |
|---|
| 24 | |
|---|
| 25 | Say "Y" or "Yes" if prompted. Once the apt program finishes you will have the Apache web server running on your machine. |
|---|
| 26 | |
|---|
| 27 | To test this, open a web browser go to your machine's home page: |
|---|
| 28 | |
|---|
| 29 | ~~~ |
|---|
| 30 | http://vmN.ws.nsrc.org/ |
|---|
| 31 | ~~~ |
|---|
| 32 | |
|---|
| 33 | ... where "N" is the number of your machine. |
|---|
| 34 | |
|---|
| 35 | You should see something like "It Works!" on the page. |
|---|
| 36 | |
|---|
| 37 | If not, you may need to create a simple web page: |
|---|
| 38 | |
|---|
| 39 | ~~~ |
|---|
| 40 | $ sudo -s |
|---|
| 41 | # echo "Hello, world" > /var/www/index.html |
|---|
| 42 | # exit |
|---|
| 43 | $ |
|---|
| 44 | ~~~ |
|---|
| 45 | |
|---|
| 46 | Try reloading the web page again (http://vmN.ws.nsrc.org/) |
|---|
| 47 | |
|---|
| 48 | ## Stopping and starting a service |
|---|
| 49 | |
|---|
| 50 | Now, let's stop the web server (Apache) that is installed on your |
|---|
| 51 | virtual machine. To do this you can do: |
|---|
| 52 | |
|---|
| 53 | ~~~ |
|---|
| 54 | $ sudo service apache2 stop |
|---|
| 55 | ~~~ |
|---|
| 56 | |
|---|
| 57 | In your web browser, try and reload the web page for your machine. |
|---|
| 58 | It should indicate that no web server was found. Now let's start the |
|---|
| 59 | service again: |
|---|
| 60 | |
|---|
| 61 | ~~~ |
|---|
| 62 | $ sudo service apache2 start |
|---|
| 63 | ~~~ |
|---|
| 64 | |
|---|
| 65 | You can see if a service is running by typing: |
|---|
| 66 | |
|---|
| 67 | ~~~ |
|---|
| 68 | $ sudo service apache2 status |
|---|
| 69 | ~~~ |
|---|
| 70 | |
|---|
| 71 | If a process ID is displayed, then the service is running, but our next |
|---|
| 72 | exercise will show you another way to verify this. |
|---|
| 73 | |
|---|
| 74 | # Finding and stopping processes |
|---|
| 75 | |
|---|
| 76 | If you wish to find something that is running and then stop it you can |
|---|
| 77 | use the "ps" (process) command with "grep" and "kill". Let's do this by |
|---|
| 78 | opening two connections to your virtual machine. |
|---|
| 79 | |
|---|
| 80 | a. Using SSH, open two terminal connections to your Linux server |
|---|
| 81 | - user: `sysadm` |
|---|
| 82 | - machine: `vmN.ws.nsrc.org` |
|---|
| 83 | |
|---|
| 84 | b. Once you have opened two terminals go in to one terminal and type: |
|---|
| 85 | |
|---|
| 86 | ~~~ |
|---|
| 87 | $ tail -f /var/log/syslog |
|---|
| 88 | ~~~ |
|---|
| 89 | |
|---|
| 90 | This will let you look at the end of the syslog log file in real time. |
|---|
| 91 | If events take place that are logged you will see them as they happen. |
|---|
| 92 | Now, in your other terminal let's look for this process: |
|---|
| 93 | |
|---|
| 94 | ~~~ |
|---|
| 95 | $ ps aux | grep tail |
|---|
| 96 | ~~~ |
|---|
| 97 | |
|---|
| 98 | The "aux" are options to the ps (process) command. The options mean |
|---|
| 99 | display all process running that belong to you and to other users and |
|---|
| 100 | provide information about who owns what process. |
|---|
| 101 | |
|---|
| 102 | You will likely see something like this: |
|---|
| 103 | |
|---|
| 104 | ~~~ |
|---|
| 105 | sysadm 5200 0.0 0.0 3764 540 pts/6 S+ 13:50 0:00 tail -f /var/log/syslog |
|---|
| 106 | sysadm 5208 0.0 0.0 3908 820 pts/5 S+ 13:50 0:00 grep --color=auto tail |
|---|
| 107 | ~~~ |
|---|
| 108 | |
|---|
| 109 | Question: what is process 5208 above, and why are we seeing it ? |
|---|
| 110 | |
|---|
| 111 | Tip: you can add the "w" option to enable "wide output", and you can |
|---|
| 112 | use "w" twice ("ww") to display the entire command line regardless of how |
|---|
| 113 | long it is and wrap it in your window. |
|---|
| 114 | |
|---|
| 115 | Try it! |
|---|
| 116 | |
|---|
| 117 | ~~~ |
|---|
| 118 | $ ps auxww | grep tail |
|---|
| 119 | ~~~ |
|---|
| 120 | |
|---|
| 121 | You could press "CTRL-C" in the terminal window where the tail command |
|---|
| 122 | is running, or to stop the process right now you can use the kill |
|---|
| 123 | command. You need to replace the Process ID (PID) with the process ID |
|---|
| 124 | number of the tail command running on your machine. In this example the |
|---|
| 125 | number is "5200". At the command prompt type: |
|---|
| 126 | |
|---|
| 127 | ~~~ |
|---|
| 128 | $ kill NNNN |
|---|
| 129 | ~~~ |
|---|
| 130 | |
|---|
| 131 | ... where NNNN is the PID of your tail process. Once you do this return |
|---|
| 132 | to the other terminal screen. The "tail -f" process should now have |
|---|
| 133 | exited and you should see something like: |
|---|
| 134 | |
|---|
| 135 | ~~~ |
|---|
| 136 | Terminated |
|---|
| 137 | sysadmt@vmN:~$ |
|---|
| 138 | ~~~ |
|---|
| 139 | |
|---|
| 140 | That's it for now! |
|---|