1 | Linux System Administration and IP Services |
---|
2 | |
---|
3 | Linux Commands |
---|
4 | -------------- |
---|
5 | |
---|
6 | # Notes |
---|
7 | |
---|
8 | * Commands preceded with "$" imply that you should execute the command as |
---|
9 | a general user - not as root. |
---|
10 | * Commands preceded with "#" imply that you should be working as root with |
---|
11 | "sudo" |
---|
12 | * Commands with more specific command lines (e.g. "RTR-GW>" or "mysql>") |
---|
13 | imply that you are executing commands on remote equipment, or within |
---|
14 | another program. |
---|
15 | |
---|
16 | 1. Log in as the sysadm user using ssh |
---|
17 | |
---|
18 | username: sysadm |
---|
19 | password: <given in class> |
---|
20 | |
---|
21 | 2. Become the root user |
---|
22 | |
---|
23 | At the command prompt type the following command: |
---|
24 | |
---|
25 | $ sudo -s |
---|
26 | |
---|
27 | Enter your own password when prompted |
---|
28 | |
---|
29 | Now that you are root the command prompt will change. We indicate this using the â#â |
---|
30 | symbol. |
---|
31 | |
---|
32 | You are now the super user - be careful! |
---|
33 | |
---|
34 | Ok, exit the root account: |
---|
35 | |
---|
36 | # exit |
---|
37 | $ |
---|
38 | |
---|
39 | 3. Look at the network configuration of your host |
---|
40 | |
---|
41 | $ cat /etc/network/interfaces |
---|
42 | |
---|
43 | Notice that configuration of your host is done using DHCP. |
---|
44 | "cat" is for "concatenate" and is one way to view what is in a file. |
---|
45 | |
---|
46 | 4. List files: |
---|
47 | |
---|
48 | Use ls to list files: |
---|
49 | |
---|
50 | $ cd [go to your home directory] |
---|
51 | $ ls |
---|
52 | |
---|
53 | Do you see anything? Try this instead: |
---|
54 | |
---|
55 | $ ls -lah |
---|
56 | |
---|
57 | What's inside one of these files? |
---|
58 | |
---|
59 | $ cat .profile |
---|
60 | $ less .profile |
---|
61 | $ clear |
---|
62 | |
---|
63 | Press âqâ to get out of the less display. |
---|
64 | |
---|
65 | If you don't understand what cat, clear or less do, then type: |
---|
66 | |
---|
67 | $ man cat |
---|
68 | $ man clear |
---|
69 | $ man less |
---|
70 | |
---|
71 | 5. Working with the command prompt: |
---|
72 | |
---|
73 | You can recall previous commands by using the up-arrow and down-arrow keys. Give this a try now. |
---|
74 | |
---|
75 | Alternately, try typing this command: |
---|
76 | |
---|
77 | $ history |
---|
78 | |
---|
79 | If you wish to execute one of the commands in the list you saw type: |
---|
80 | |
---|
81 | $ !nn |
---|
82 | |
---|
83 | Where ânnâ is the number of the command in the history list. This is useful if you want to run a past command that was long and/or complicated. |
---|
84 | |
---|
85 | Command completion: |
---|
86 | |
---|
87 | With the bash shell you can auto-complete commands using the tab key. This means, if you type part of a command, once you have a unique string if you press the TAB key the command will complete. If you press the TAB key twice you'll see all your available options. Your instructor will demonstrate this, but give it a try by doing: |
---|
88 | |
---|
89 | $ hist<TAB> |
---|
90 | $ del<TAB><TAB> |
---|
91 | $ rm <TAB><TAB> [Include the space after the ârmâ] |
---|
92 | |
---|
93 | |
---|
94 | 6. Working with pipes: |
---|
95 | |
---|
96 | We saw an example of using pipes when we sorted the contents of our /sbin directory during the presentation. What if you wanted to have this information available in a file and sorted? |
---|
97 | |
---|
98 | $ cd |
---|
99 | $ ls /sbin | sort > sbin.txt |
---|
100 | |
---|
101 | Now view the contents of what is in sbin.txt to verify that this worked. |
---|
102 | |
---|
103 | $ less sbin.txt |
---|
104 | |
---|
105 | Press the "q" key to quit viewing the contents. |
---|
106 | |
---|
107 | |
---|
108 | 7. Finding text strings: |
---|
109 | |
---|
110 | Use the command grep to print lines matching a pattern in a data stream (such as a file). For example, view the entry for the sysadm account in the system passwd file: |
---|
111 | |
---|
112 | $ grep sysadm /etc/passwd |
---|
113 | |
---|
114 | You should see something like: |
---|
115 | |
---|
116 | sysadm:x:1000:1000:System Administrator,,,:/home/sysadm:/bin/bash |
---|
117 | |
---|
118 | The previous items above are: |
---|
119 | |
---|
120 | userid:passwd:uid:gid:Name,extrastuff,,:HomeDir:LoginShell |
---|
121 | |
---|
122 | grep is often used with a pipe to FILTER the output of commands. For instance: |
---|
123 | |
---|
124 | $ history | grep ls |
---|
125 | |
---|
126 | Will display your previous use of the ls command from exercise 2. |
---|
127 | |
---|
128 | Now try this: |
---|
129 | |
---|
130 | $ grep sysadm /etc/shadow |
---|
131 | |
---|
132 | Why doesn't it work? Change it to the following, why does it work now? |
---|
133 | |
---|
134 | $ sudo grep sysadm /etc/shadow |
---|
135 | |
---|
136 | |
---|
137 | 8. Stopping and starting a service |
---|
138 | |
---|
139 | In a web browser go to your machine's home page: |
---|
140 | |
---|
141 | http://vmN.ws.nsrc.org/ |
---|
142 | |
---|
143 | You should see something like "It Works!" on the page. Now, let's stop the web server (Apache) that is installed on your virtual machine. To do this you can do: |
---|
144 | |
---|
145 | $ sudo service apache2 stop |
---|
146 | |
---|
147 | Now reload the web page for your machine. It should indicate that no web server was found. Now let's start the service again: |
---|
148 | |
---|
149 | $ sudo service apache2 start |
---|
150 | |
---|
151 | You can see if a service is running by typing: |
---|
152 | |
---|
153 | $ sudo service apache2 status |
---|
154 | |
---|
155 | If a process ID is displayed, then the service is running, but our next exercise will show you another way to verify this. |
---|
156 | |
---|
157 | |
---|
158 | 9. Finding and stopping processes |
---|
159 | |
---|
160 | If you wish to find something that is running and then stop it you can use the "ps" (process) command with "grep" and "kill". Let's do this by opening two connections to your virtual machine. |
---|
161 | |
---|
162 | a. Using SSH open two terminal connections to your Linux server (user: sysadm machine: vmN.ws.nsrc.org) |
---|
163 | b. One you have opened two terminals go in to one terminal and type: |
---|
164 | |
---|
165 | $ tail -f /var/log/syslog |
---|
166 | |
---|
167 | This will let you look at the end of the syslog log file in real time. If events take place that are logged you will see them as they happen. Now, in your other terminal let's look for this process: |
---|
168 | |
---|
169 | $ ps auxwww | grep tail |
---|
170 | |
---|
171 | The "auxwww" are options to the ps (process) command. The options mean display all process running that belong to you and to other users and provide information about who owns what process. The three "www"'s mean display the entire command line regardless of how long it is and wrap it in your window. |
---|
172 | |
---|
173 | You will likely see something like this: |
---|
174 | |
---|
175 | root 6903 0.0 0.0 7200 612 pts/0 S+ 03:28 0:00 tail -f /var/log/syslog |
---|
176 | root 6986 0.0 0.0 9388 924 pts/1 S+ 03:28 0:00 grep --color=auto tail |
---|
177 | |
---|
178 | You could press "CTRL-C" in the terminal window where the tail command is running, or to stop the process right now you can use the kill command. You need to replace the Process ID (PID) with the process ID number of the tail command running on your machine. In this example the number is "6903". At the command prompt type: |
---|
179 | |
---|
180 | $ kill NNNN |
---|
181 | |
---|
182 | Where NNNN is the PID of your tail process. Once you do this return to the other terminal screen. The "tail -f" process should now have exited and you should see something like: |
---|
183 | |
---|
184 | Terminated |
---|
185 | sysadm@vmN:~$ |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | |
---|