1 | Track 2 Workshop |
---|
2 | PacNOG 10 |
---|
3 | |
---|
4 | System Administration |
---|
5 | --------------------- |
---|
6 | |
---|
7 | 1. Reboot your system |
---|
8 | |
---|
9 | To restart your system, you could use: |
---|
10 | |
---|
11 | $ sudo shutdown -r TIME |
---|
12 | |
---|
13 | ... where time can be a day, hour, minute... |
---|
14 | |
---|
15 | Or you could try and reboot your machine *NOW*: |
---|
16 | |
---|
17 | $ sudo shutdown -r now |
---|
18 | |
---|
19 | The "-r" means reboot. Another command for doing this is "reboot". |
---|
20 | |
---|
21 | To stop a machine you can do: |
---|
22 | |
---|
23 | # halt -p |
---|
24 | |
---|
25 | or |
---|
26 | |
---|
27 | # shutdown -h -P now |
---|
28 | |
---|
29 | Be careful when using halt on remote systems! |
---|
30 | |
---|
31 | |
---|
32 | 2. Go to single user mode - DON'T ACTUALLY DO THIS, YOU WILL LOSE |
---|
33 | Network conectivity to your machine! |
---|
34 | |
---|
35 | *IF* you wanted to go into single user mode, you could do: |
---|
36 | |
---|
37 | $ sudo init 1 |
---|
38 | |
---|
39 | This would take you down to single user mode. |
---|
40 | You can only log in as the root user, and only from the console. |
---|
41 | |
---|
42 | Some systems will require you to enter your password to access the |
---|
43 | shell. |
---|
44 | |
---|
45 | If you then wanted to go back to multi-user mode type: |
---|
46 | |
---|
47 | # exit |
---|
48 | |
---|
49 | 3. Figure out how your machine has been partitioned |
---|
50 | |
---|
51 | You want to display free disk space, or "df": |
---|
52 | |
---|
53 | $ df -h |
---|
54 | |
---|
55 | Use: |
---|
56 | |
---|
57 | $ man df |
---|
58 | |
---|
59 | to understand what the "-h" option does. |
---|
60 | |
---|
61 | Look in /etc/fstab. This is where file systems are mounted in Linux. Read the |
---|
62 | man page on this file: |
---|
63 | |
---|
64 | $ man fstab |
---|
65 | |
---|
66 | Notice that defined file systems are pointing to /dev/sda*. Have a look |
---|
67 | at these files: |
---|
68 | |
---|
69 | $ ls -lah /dev/sda* |
---|
70 | $ file /dev/sda* |
---|
71 | |
---|
72 | What type of files are these? |
---|
73 | |
---|
74 | |
---|
75 | 4. Create a new group |
---|
76 | |
---|
77 | In a terminal window first do: |
---|
78 | |
---|
79 | $ sudo groupadd track2 |
---|
80 | |
---|
81 | Now lets add some members to this group: |
---|
82 | |
---|
83 | $ sudo vigr |
---|
84 | |
---|
85 | You will now be in the /etc/group file using the default EDITOR - vi or |
---|
86 | joe, or... |
---|
87 | |
---|
88 | Find the entry for "track2" - Easiest way is to press the "/" key, type |
---|
89 | "track2" and press <ENTER> |
---|
90 | |
---|
91 | /track2 [press <ENTER>] |
---|
92 | |
---|
93 | You will be on this line: |
---|
94 | |
---|
95 | track2:x:1002: |
---|
96 | |
---|
97 | Change the line to look like: |
---|
98 | |
---|
99 | track2:x:1002:sysadm |
---|
100 | |
---|
101 | And save the file and quit. |
---|
102 | |
---|
103 | |
---|
104 | Verify that your sysadm user is now in the track2 group: |
---|
105 | |
---|
106 | $ su - sysadm |
---|
107 | $ groups |
---|
108 | |
---|
109 | |
---|
110 | 5. Editing the command line revisited: |
---|
111 | |
---|
112 | We did this exercise in the introduction to Linux session. We will |
---|
113 | repeat it here. |
---|
114 | |
---|
115 | It is particularly useful to realize that you can edit a command just as |
---|
116 | you would a line of text in a file. |
---|
117 | |
---|
118 | For instance, you can: |
---|
119 | |
---|
120 | - Use your back-arrow and forward-arrow keys to change text in a command. |
---|
121 | Use the Home and End keys to go to the start and the end of a command. |
---|
122 | |
---|
123 | Note: you do not need to go to the end of a command before pressing |
---|
124 | <ENTER> to execute the command. You can use the history command with |
---|
125 | grep to find a previous command. For long commands this can save |
---|
126 | considerable time. |
---|
127 | |
---|
128 | You you can also use the reverse-search feature of bash: |
---|
129 | |
---|
130 | 1.) Press ctrl-r |
---|
131 | 2.) type the term you are searching for |
---|
132 | 3.) Press ctrl-r to cycle through all occurrences of the term in your history |
---|
133 | 4.) Press the right or left-arrow, HOME or END key to start editing the command. |
---|
134 | |
---|
135 | First, let's make sure we have a command to play with. Do the following command: |
---|
136 | |
---|
137 | $ grep sysadm /etc/passwd |
---|
138 | |
---|
139 | Let's give some of these editing rules a try. Instead of searching for |
---|
140 | the sysadm user in the /etc/passwd file we'll search for the root user. |
---|
141 | |
---|
142 | 1.) Press 'ctrl-r' |
---|
143 | 2.) type sysadm |
---|
144 | 3.) If necessary press ctrl-r again until you see the original command |
---|
145 | you used to search for the sysadm user in the /etc/password file |
---|
146 | ("grep sysadm /etc/passwd"). |
---|
147 | |
---|
148 | 4.) Use the arrows keys to move in the command and change sysadm to root. |
---|
149 | You should now have the following on your command line: |
---|
150 | |
---|
151 | $ grep root /etc/passwd |
---|
152 | |
---|
153 | With your cursor positioned over just past the 't' in 'root', press |
---|
154 | <ENTER> to execute the command. |
---|
155 | |
---|
156 | You should see: |
---|
157 | |
---|
158 | root:x:0:0:root:/root:/bin/bash |
---|
159 | |
---|
160 | That's it. You can practice repeating the above with other parts of |
---|
161 | commands you have already executed |
---|
162 | |
---|
163 | (ls, grep, cat, etc.). |
---|
164 | |
---|
165 | 5. Installing a package |
---|
166 | |
---|
167 | We're going to use the 'lsof' command in the next exercise, so let's install |
---|
168 | it: |
---|
169 | |
---|
170 | $ sudo apt-get install lsof |
---|
171 | |
---|
172 | 6. Using the pipe command |
---|
173 | |
---|
174 | Let's run a command with multiple lines of output: |
---|
175 | |
---|
176 | $ sudo lsof -i |
---|
177 | |
---|
178 | Find the Process ID (PID) of one of the ssh server that is running on |
---|
179 | your machine: |
---|
180 | |
---|
181 | COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME |
---|
182 | sshd 988 root 3u IPv4 5040 0t0 TCP *:ssh (LISTEN) |
---|
183 | sshd 988 root 4u IPv6 5042 0t0 TCP *:ssh (LISTEN) |
---|
184 | |
---|
185 | Your PID will be different. Now let's look at all the associated files |
---|
186 | with this process: |
---|
187 | |
---|
188 | $ sudo lsof -p PID |
---|
189 | |
---|
190 | (replace PID with the number you got from the "lsof -i" command |
---|
191 | above, for example 988 here) |
---|
192 | |
---|
193 | There are a fair number of files that are "open" by the ssh server, or |
---|
194 | daemon - the sshd command. |
---|
195 | |
---|
196 | Let's look at more interesting output from the prior command: |
---|
197 | |
---|
198 | $ sudo lsof -p PID | grep REG |
---|
199 | |
---|
200 | Now let's see how many files the sshd program has open: |
---|
201 | |
---|
202 | $ sudo lsof -p PID | grep REG | wc -l |
---|
203 | |
---|
204 | To see what these commands are doing read: |
---|
205 | |
---|
206 | $ man lsof |
---|
207 | $ man grep |
---|
208 | $ man wc |
---|
209 | |
---|
210 | |
---|
211 | 7. Use the top command |
---|
212 | |
---|
213 | The top command let's us see the status of our system at a quick |
---|
214 | glance. To use top simply do: |
---|
215 | |
---|
216 | $ top |
---|
217 | |
---|
218 | The item at the top of list of running processes is the process using |
---|
219 | the most CPU resources. |
---|
220 | |
---|
221 | Open a new SSH connection to your PC. In that window type: |
---|
222 | |
---|
223 | $ ls -lahR / |
---|
224 | |
---|
225 | Now in the other window where top is running you should start to see the "ls" |
---|
226 | process listed using some amount of your total CPU. |
---|
227 | |
---|
228 | At the top of the top window you'll see something like: |
---|
229 | |
---|
230 | top - 08:10:23 up 16:14, 2 users, load average: 0.00, 0.00, 0.00 |
---|
231 | Tasks: 91 total, 1 running, 90 sleeping, 0 stopped, 0 zombie |
---|
232 | Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st |
---|
233 | Mem: 1016348k total, 568532k used, 447816k free, 91904k buffers |
---|
234 | Swap: 2973688k total, 0k used, 2973688k free, 325908k cached |
---|
235 | |
---|
236 | This is a good, quick way to see how much RAM, Virtual memory, CPU, |
---|
237 | total running processes, etc. that your machine has, and is using. |
---|
238 | |
---|
239 | You can adjust the output of top as it is running. Exit from top by |
---|
240 | typing "q" and then do: |
---|
241 | |
---|
242 | $ man top |
---|
243 | |
---|
244 | Now run top again and change what it is displaying interactively. |
---|
245 | |
---|
246 | All the information in top is part of a dynamic file system located in |
---|
247 | /proc. As an example do the following: |
---|
248 | |
---|
249 | $ cd /proc |
---|
250 | $ ls |
---|
251 | |
---|
252 | The numbered directories correspond to actual Process IDs of processes |
---|
253 | that are running. Look at the file meminfo: |
---|
254 | |
---|
255 | $ less meminfo |
---|
256 | |
---|
257 | Note that it includes your total RAM. Top uses this file to get this |
---|
258 | information. Same for cpuinfo, loadavg, uptime, etc. |
---|
259 | |
---|
260 | If you want to know what command was executed to start a number process |
---|
261 | you can type (for instance): |
---|
262 | |
---|
263 | $ less /proc/1/cmdline |
---|
264 | |
---|
265 | You'll see that the first process started on the system is init. |
---|
266 | |
---|
267 | 8. A couple more useful tools |
---|
268 | |
---|
269 | $ sudo apt-get install ifstat |
---|
270 | |
---|
271 | $ ifstat |
---|
272 | |
---|
273 | ... what does ifstat do ? |
---|
274 | |
---|
275 | $ vmstat -a 1 |
---|
276 | |
---|
277 | ... what does vmstat do ? |
---|