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 | |
---|
17 | If you have been allocated a virtual machine by the instructor, |
---|
18 | you will log in as SSH. If this is a machine running inside VirtualBox |
---|
19 | on your laptop, you will probably log in directly on the console. |
---|
20 | |
---|
21 | ~~~ |
---|
22 | username: nsrc |
---|
23 | password: <given in class> |
---|
24 | ~~~ |
---|
25 | |
---|
26 | ## Become the root user |
---|
27 | |
---|
28 | At the command prompt type the following command: |
---|
29 | |
---|
30 | ~~~ |
---|
31 | $ sudo -s |
---|
32 | ~~~ |
---|
33 | |
---|
34 | Enter the class user's password when prompted |
---|
35 | |
---|
36 | Now that you are root the command prompt will change. We indicate this |
---|
37 | using the "#" symbol. |
---|
38 | |
---|
39 | You are now the super user - be careful! |
---|
40 | |
---|
41 | Ok, 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 | |
---|
54 | The IP configuration of your host is either done using DHCP, or configured |
---|
55 | statically. 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 | |
---|
61 | Use `ls` to list files: |
---|
62 | |
---|
63 | ~~~ |
---|
64 | $ cd [go to your home directory] |
---|
65 | $ ls |
---|
66 | ~~~ |
---|
67 | |
---|
68 | Do you see anything? Try this instead: |
---|
69 | |
---|
70 | ~~~ |
---|
71 | $ ls -lah |
---|
72 | ~~~ |
---|
73 | |
---|
74 | What's inside one of these files? |
---|
75 | |
---|
76 | ~~~ |
---|
77 | $ cat .profile |
---|
78 | $ less .profile |
---|
79 | ~~~ |
---|
80 | |
---|
81 | Press `q` to get out of the less display. |
---|
82 | |
---|
83 | Another command: |
---|
84 | |
---|
85 | ~~~ |
---|
86 | $ clear |
---|
87 | ~~~ |
---|
88 | |
---|
89 | If 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 | |
---|
99 | You can recall previous commands by using the up-arrow and down-arrow |
---|
100 | keys. Give this a try now. |
---|
101 | |
---|
102 | Alternately, try typing this command: |
---|
103 | |
---|
104 | ~~~ |
---|
105 | $ history |
---|
106 | ~~~ |
---|
107 | |
---|
108 | If you wish to execute one of the commands in the list you saw type: |
---|
109 | |
---|
110 | ~~~ |
---|
111 | $ !nn |
---|
112 | ~~~ |
---|
113 | |
---|
114 | Where `nn` is the number of the command in the history list. This |
---|
115 | is useful if you want to run a past command that was long and/or |
---|
116 | complicated. |
---|
117 | |
---|
118 | ## Command completion |
---|
119 | |
---|
120 | With the bash shell you can auto-complete commands using the tab key. |
---|
121 | This means, if you type part of a command, once you have a unique string |
---|
122 | if you press the TAB key the command will complete. If you press the TAB |
---|
123 | key twice you'll see all your available options. Your instructor will |
---|
124 | demonstrate 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 | |
---|
134 | We 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 |
---|
136 | information available in a file and sorted? |
---|
137 | |
---|
138 | ~~~ |
---|
139 | $ cd |
---|
140 | $ ls /sbin | sort > sbin.txt |
---|
141 | ~~~ |
---|
142 | |
---|
143 | Now view the contents of what is in sbin.txt to verify that this worked. |
---|
144 | |
---|
145 | ~~~ |
---|
146 | $ less sbin.txt |
---|
147 | ~~~ |
---|
148 | |
---|
149 | Press the "q" key to quit viewing the contents. |
---|
150 | |
---|
151 | ## Finding text strings |
---|
152 | |
---|
153 | Use 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 |
---|
155 | the system passwd file: |
---|
156 | |
---|
157 | ~~~ |
---|
158 | $ grep nsrc /etc/passwd |
---|
159 | ~~~ |
---|
160 | |
---|
161 | You should see something like: |
---|
162 | |
---|
163 | ~~~ |
---|
164 | nsrc:x:1000:1000:System Administrator,,,:/home/nsrc:/bin/bash |
---|
165 | ~~~ |
---|
166 | |
---|
167 | The previous items above are: |
---|
168 | |
---|
169 | ~~~ |
---|
170 | userid:passwd:uid:gid:Name,extrastuff,,:HomeDir:LoginShell |
---|
171 | ~~~ |
---|
172 | |
---|
173 | grep is often used with a pipe to FILTER the output of commands. For instance: |
---|
174 | |
---|
175 | ~~~ |
---|
176 | $ history | grep ls |
---|
177 | ~~~ |
---|
178 | |
---|
179 | Will display your previous use of the ls command from exercise 2. |
---|
180 | |
---|
181 | ## Editing the command line revisited |
---|
182 | |
---|
183 | It is particularly useful to realize that you can edit a command just as |
---|
184 | you 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 | |
---|
210 | Let's give some of these editing rules a try: |
---|
211 | |
---|
212 | ~~~ |
---|
213 | $ ls -lah /usr/lib/ | grep postfix |
---|
214 | ~~~ |
---|
215 | |
---|
216 | Then, let's look for postfix |
---|
217 | |
---|
218 | `<CTRL>-R`, type `postfix`, then press left arrow. Edit the previous |
---|
219 | command (which you should now have) and change `/usr/lib/` to |
---|
220 | `/usr/sbin/`. Use the left+right arrow key to move, and backspace to |
---|
221 | erase. You should now have: |
---|
222 | |
---|
223 | ~~~ |
---|
224 | $ ls -lah /usr/sbin/ | grep postfix |
---|
225 | ~~~ |
---|
226 | |
---|
227 | With your cursor just past the `/` in `/sbin/`, press <ENTER> to execute |
---|
228 | the command. |
---|