├── .gitignore
├── LICENSE
├── README.md
├── SUMMARY.md
├── book.json
└── content
├── auth.md
├── file-system.md
├── history.md
├── install_rpm_and_source.md
├── monitor.md
├── process.md
├── rsync.md
├── shell.md
├── ssh.md
├── system-log.md
├── systemd.md
├── vim.md
└── xargs.md
/.gitignore:
--------------------------------------------------------------------------------
1 | # Node rules:
2 | ## Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
3 | .grunt
4 |
5 | ## Dependency directory
6 | ## Commenting this out is preferred by some people, see
7 | ## https://docs.npmjs.com/misc/faq#should-i-check-my-node_modules-folder-into-git
8 | node_modules
9 |
10 | # Book build output
11 | _book
12 |
13 | # eBook build output
14 | *.epub
15 | *.mobi
16 | *.pdf
17 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Web开发者手边的一本CentOS小书
2 | 这本 CentOS 小书是对 CentOS 系统知识的一个整理以及补完,当然对于其它 Linux 发行版本也有很大的参考意义,但的确针对的是目前 CentOS 的最新版本——**CentOS 7**
3 |
4 | ## 注意
5 | 虽然本书打着*整理补完*的旗号,但毕竟只是一本小书,肯定是比不上外面那些大部头的。因此,本书的着重点是一名web开发人员所需要的知识体系。另外,对于一些必要的概念、或是不常用的命令,书中仅提供搜索关键词,作为知识索引提供诸君参考。
6 |
--------------------------------------------------------------------------------
/SUMMARY.md:
--------------------------------------------------------------------------------
1 | # Summary
2 |
3 | * [封面](README.md)
4 | * [第一章:CentOS的系统服务管理系统](content/systemd.md)
5 | * [第二章:CentOS的文件系统](content/file-system.md)
6 | * [第三章:CentOS的进程系统](content/process.md)
7 | * [第四章:CentOS的系统用户与用户组管理](content/auth.md)
8 | * [第五章:Vim的使用](content/vim.md)
9 | * [第六章:安装RPM包或源码包](content/install_rpm_and_source.md)
10 | * [第七章:shell相关](content/shell.md)
11 | * [第八章:神奇的xargs命令](content/xargs.md)
12 | * [第九章:CentOS的历史命令](content/history.md)
13 | * [第十章:监控CentOS的系统状态](content/monitor.md)
14 | * [第十一章:SSH远程登录](content/ssh.md)
15 | * [第十二章:备份工具rsync](content/rsync.md)
16 | * [第十三章:Linux系统日志](content/system-log.md)
--------------------------------------------------------------------------------
/book.json:
--------------------------------------------------------------------------------
1 | {
2 | "author": "Array-Huang ",
3 | "title": "CentOS小书",
4 | "description": "这本 CentOS 小书是对 CentOS 系统知识的一个整理以及补完,当然对于其它 Linux 发行版本也有很大的参考意义,但的确针对的是目前 CentOS 的最新版本——CentOS 7",
5 | "generator": "site",
6 | "links": {
7 | "sidebar": {
8 | "作者博客": "https://segmentfault.com/u/array_huang"
9 | }
10 | }
11 | }
--------------------------------------------------------------------------------
/content/auth.md:
--------------------------------------------------------------------------------
1 | # 第四章:CentOS的系统用户与用户组管理
2 |
3 | ## 更改文件或文件目录的权限
4 | - `groupadd` 增加一个用户组
5 | - `chgrp` 更改所属组
6 | - `chown` 更改所属主,除了可以更改所属用户外,还可更改所属组
7 | - `chmod` 改变用户对文件的读写执行权限,如744
8 | - `umask` 改变新建文件/目录的默认读写执行权限
9 |
10 | ## 如何创建一个不允许登录的账号(仅供某个后台服务使用)
11 | ```
12 | useradd -M -s /sbin/nologin nologinUser
13 | ```
14 | 但需要注意的是:
15 | - `/sbin/nologin`只是不允许系统login,可以使用其他ftp等服务。
16 | - 如果想彻底一点什么服务都禁止掉,可以使用`/bin/false`。
17 |
18 | ## 为系统设置管理员账号,避免使用root账号造成误操作
19 | - 非root用户可使用`sudo`命令来执行本来只能由root用户才能执行的命令。
20 | - 并非所有的非root用户都可以使用`sudo`命令,只有在`/etc/sudoers`里指定的用户才有此能力。
21 | - 授权使用`sudo`命令的用户,需要root用户使用`visudo`命令进行编辑。
22 | - 为方便起见,可以设置一个“管理员用户组”,先授权这个用户组使用`sudo`命令,再把管理员用户的账号加入到这个“管理员用户组”中即可。授权用户组的配置如下:`%wheel ALL=(ALL) ALL`;这个配置便授权给了`wheel`这个用户组。
23 |
24 | ## 添加或删除某个用户组的成员
25 | - 查`gpasswd`。
26 | - 如果图省事,想一次性让某个用户加入多个用户组,可以使用`usermod`。
--------------------------------------------------------------------------------
/content/file-system.md:
--------------------------------------------------------------------------------
1 | # 第二章:CentOS的文件系统
2 |
3 | ## 修改文件的特殊属性
4 | `chattr`,可修改文件的多种特殊属性:
5 | - `a`,增加该属性后,只能追加不能删除,非root用户不能设定该属性
6 | - `c`,自动压缩该文件,读取时会自动解压;
7 | - `i`,增加后,使文件不能被删除、重命名、设定链接接、写入、新增数据
8 |
9 | `lsattr`,该命令用来读取文件或者目录的特殊权限
10 |
11 | ## 在linux下搜一个文件
12 | - `which`,找命令。
13 | - `locate`,针对已生成的全局文件树索引对文件名进行搜索,但使用前需要先安装`mlocate`且执行`updatedb`来生成文件树索引;该命令仅支持按文件名进行搜索。
14 | - `find`,遍历查找指定目录(不指定就针对整个系统进行查找);该命令支持多种筛选条件(可按`与或否`的逻辑关系进行串联)进行查找,如:
15 | - 文件名,通过`-name`和`-iname`参数传入,支持通配符。
16 | - 所属用户,通过`-user`参数传入。
17 | - 所属组,通过`-group`参数传入。
18 | - 文件时间戳的相关属性,通过`-atime`(Access time)/`-ctime`(Change time)/`-mtime`(Modify time)参数传入,其中`-mtime`参数比较常用。
19 | - 文件类型,通过`-type`参数传入。
20 | - 文件大小,通过`-size`参数传入。
21 |
22 | ## 如何动态显示一个不停增加内容的文件
23 | - 使用`tail -f`可实时追踪一个或多个文档的所有更新,这个功能在调试程序时非常好用:
24 | ```
25 | tail -f /var/log/mail.log /var/log/apache/error_log
26 | ```
27 |
28 | ## 查看文件/目录占用磁盘大小
29 | `du -sh filename`,解释:
30 | - `-s`,表示只列出目录本身的数据。
31 | - `-h`,系统自动调节单位。
32 |
33 | ## 压缩和解压缩
34 | ### gzip 压缩工具
35 | - linux下压缩工具有多种,但最常用的是gzip,其它的使用起来也差不多。
36 | - gzip只支持文件的压缩,若要压缩目录,则需要使用下述的`tar`打包工具。
37 | - 压缩直接用`gzip sourcefile`,解压则用`gzip -d zipfile`。
38 | - 使用 gzip 压缩的文件后缀一般为`.gz`。
39 |
40 | ### tar 打包工具
41 | - tar 本身是一个打包工具,并不具有压缩功能,但可以配合压缩工具,一次性完成打包和压缩的任务;通常情况下我们也不会只打包不压缩,所以我们直接记住“一次性打包压缩”的参数即可:
42 | - `tar -czvf distfile sourcedir`,压缩打包sourcedir到disfile。
43 | - `tar -zxvf sourcefile`,解压解包fourcefile到当前目录。
44 | - 解释一下上面命令用到的参数:
45 | - `-z`表示使用 gzip 压缩工具;其实还可使用其它压缩工具(如 bzip2 和 xz),但毕竟最常用的还是 gzip。
46 | - `-c`(`c` for compress)表示压缩打包,`-x`表示解压解包。
47 | - `-v`表示可视化。
48 | - `-f`后面跟文件名(即`-f filename`),表示压缩后的文件名为 filename,或当期需要解压文件 filename。
49 | - tar 除了可以打包目录,还可以指定多个文件打包到一起:`tar -czvf files.tar.gz file1 file2 file3`。
50 | - tar 命令支持查看(但不解压)压缩文件的内容,其参数为`-t`,但需要注意的是必须与`-f`同用,其用法为:`tar -tf file.tar.gz`。
51 |
52 | ### zip 压缩工具
53 | - 对比起上述介绍的 gzip 和 tar,zip 的功能更为强大,它可以压缩(解压)文件和目录。
54 | - 由于 zip 在 windows 系统上比较常用,因此如需与 windows 系统交换文件,可通过 zip 进行压缩,这样两边都可以识别。
55 | - CentOS 默认不带 zip 命令,需要通过`yum install -y zip`进行安装。
56 | - 压缩文件用`zip distfile sourcefile`,压缩目录则用`zip distfile sourcedir`。
57 | - 需要注意的是,当压缩目录下还有二级目录甚至更多级目录时,zip 命令仅仅是把二级目录本身压缩而已,如果想要一并压缩二级目录下的文件及更多级目录,则必须加上`-r`,如`zip -r distfile sourcedir`。
58 | - 解压文件并不用 zip 命令,而是用`unzip`命令,如`unzip file.zip`。
59 | - 除了基本的压缩/解压功能外,zip 还提供更多进阶功能,如:使用密码进行加密;设置压缩级别;添加注释,等等。
--------------------------------------------------------------------------------
/content/history.md:
--------------------------------------------------------------------------------
1 | # 第九章:CentOS的历史命令
2 |
3 | ## 查看(只读)文件内容或其它linux命令执行结果
4 | 实例:查看命令历史使用记录并通过less分页显示,命令:`history | less`
5 |
6 | ## 重新执行历史命令
7 | Linux会把我们执行过的命令记录起来,预设可以记录1000条历史记录,这些记录保存在用户家目录的`.bash_history`文件中。
8 | - 一般来说,我们可以通过按动方向键上下来选择过往执行过的命令,但如果太久远的命令要找就太繁琐了,此时我们便可以充分利用这历史命令记录:先利用`history`找到想重新执行的命令的编号,比如说编号为101的命令为`pwd`,然后再输入`!101`,这样就相当于我们执行了一遍`pwd`命令。
9 | - 另外我们还可以通过`!!`来直接执行上一条命令,不过当然我还是倾向于按方向键上来找到上一条命令。
--------------------------------------------------------------------------------
/content/install_rpm_and_source.md:
--------------------------------------------------------------------------------
1 | ## RPM(Red Hat Package Manager)
2 | - 系统中存在着一个关于RPM的数据库,它记录了安装包以及包与包之间的依赖关系。
3 | - RPM包是预先在Linux机器上编译并打包的文件,安装非常快捷;但它也有一些缺点:
4 | - 安装环境必须与编译时的环境一致或者相当;
5 | - 包与包之间存在着相互依赖的情况下,卸载某个包时,需要先把系统里所有依赖该包的包进行卸载;虽然也可忽略依赖关系进行强制删除,但这样就会导致异常情况的发生。
6 | - 安装RPM包使用命令`rpm -ivh filename`,其中:
7 | - `-i`,表示安装;
8 | - `-v`,表示可视化;
9 | - `-h`,表示显示安装进度;
10 | - 升级RPM包使用命令`rpm -Uvh filename`,其中的`-U`就表示升级。
11 | - 查询rpm包:
12 | - 查询是否已安装某个包使用命令`rpm -q packagename`,如`rpm -q zip`。另外,我们可以通过`rpm -qa`的命令来查询系统中所有已安装的包,并通过`grep`等方式进行二次搜索,如`rpm -qa | grep zip`。
13 | - 查询某个已安装的RPM包的详情:`rpm -qi packagename`,可得到版本号、安装时间、简介等信息。
14 | - 卸载RPM包使用命令为`rpm -e packagename`。
15 |
16 | ## yum 工具
17 | - Yum(Yellow dog Updater,Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。
18 | - 列出所有可用的RPM包:`yum list`,由于数量众多,我们一般会进行二次搜索、筛选,如`yum list | grep zip | head -n 5`;此命令列出的信息里,主要有以下三列:
19 | - 第一列是包名,含平台信息。
20 | - 第二列是最新版本号。
21 | - 第三列是安装信息,如果已安装,则显示`@base`或`@anaconda`;如果未安装则显示`base`或`anaconda`;如果已安装但已有更新版本,则显示`updates`。
22 | - 搜索RPM包的命令是`yum search str`,如`yun search zip`。
23 | - 安装RPM包的命令是`yum install -y packagename`,如`yun install -y zip`,需要注意的是,虽然不加`-y`也是可以正常安装RPM包的,但是不加`-y`的话,如果该RPM包有依赖的包,就会一个一个轮流询问用户是否需要安装,那样子就太繁琐了,不如就加个`-y`全部默认安装,这也正是 yum 的一大特点嘛。
24 | - 卸载RPM包的命令为`yum remove -y packagename`,加`-y`的原因同`yum install`。
25 | - 升级RPM包的命令为`yum update -y packagename`,加`-y`的原因同`yum install`;另有`yum upgrade -y packagename`,作用与`yum update`类似都是更新本地系统里的该RPM包,不同在于`yum update`会先去更新软件支持列表(也称RPM源)。
26 |
27 | ## 安装源码包
28 | 安装源码包有3个主要步骤,分别是`./configure`、`make`、`make install`。
29 |
30 | ### 前置工作
31 | 安装源码包除了上述3个主要步骤,我们还需要前期的一些准备工作:
32 | - 在官方站点下载源码包,并且基于约定俗成,把源码包放到`/usr/local/src`目录。
33 | - 视源码包格式而定,挑选压缩工具进行解压。
34 |
35 | ### ./configure
36 | 这一步骤的主要作用就在于:
37 | - 定制软件安装的功能/配置;
38 | - 检查系统环境以及是否具有编译该源码包所需要的库;
39 | - 生成 Makefile 文件;
40 |
41 | 关于软件可定制的功能/配置,我们可以通过命令`./configure --help`来进行查看,此时实际上并不会真的执行`./configure`,而是显示一个帮助文档。
42 |
43 | 最常用的可配置项莫过于`--prefix`,该配置项的意思是定义软件包的安装路径。
44 |
45 | 在确定好所有配置项后,我们可以执行形如以下的命令:`./configure --prefix=/usr/local/appache2`,此时就开始检测安装环境了,如果有问题,按照提示信息操作(如安装缺失了的库/包)即可。
46 |
47 | 如果执行成功,则可看到已生成了`Makefile`;另外也可以执行`echo $?`来验证操作结果,如果结果是0说明执行成功,否则就没有成功。
48 |
49 | ### make
50 | 生成`Makefile`后,需要进行编译,执行命令`make`,执行后,同样可用`echo $?`来验证操作结果。
51 |
52 | ### make install
53 | 通过`make`成功编译后,我们就可以执行安装了,命令为`make install`,执行后,同样可用`echo $?`来验证操作结果。
54 |
55 | 到此,该源码包便已安装完成了。
--------------------------------------------------------------------------------
/content/monitor.md:
--------------------------------------------------------------------------------
1 | # 第十章:监控CentOS的系统状态
2 |
3 | ## 如何查看系统变量
4 | - 执行`env`可以查看系统的环境变量,如主机的名称、当前用户的SHELL类型、当前用户的家目录、当前系统所使用的语言等。
5 | - 执行`set`可以看到系统当前所有的变量,其中包括了:
6 | - 系统的所有预设变量,这其中既包括了`env`所显示的环境变量,也包含了其它许多预设变量。
7 | - 用户自定义的变量。
8 |
9 | ## 监控系统的状态
10 | ### 使用w命令查看当前系统整体上的负载
11 | 使用`w`命令可以查看当前系统整体上的负载:
12 | ```
13 | # w
14 | 20:33:11 up 309 days, 10:03, 1 user, load average: 0.00, 0.01, 0.05
15 | USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
16 | root pts/0 113.102.224.86 20:33 3.00s 0.00s 0.00s w
17 | ```
18 | 需要关注的是第一行的最后一个部分——**load average**,这里的3个数字分别表示了系统在1分钟/5分钟/15分钟内的平均负载值,值越大说明服务器压力就越大。
19 |
20 | 那么,如何看负载是不是太满了呢?其实这个值是与服务器的物理CPU做对比的,那么只要负载值不超过物理CPU数量即可;如当前服务器有两个CPU,那么就尽量不要让负载值超过2。
21 |
22 | ### 用vmstat命令查看系统具体负载
23 | `vmstat`命令打印的结果共分为6部分:procs、memory、swap、io、system和cpu,其中又有许多的细分字段,这里我们重点关注r、b、si、so、bi、bo、wa字段。
24 |
25 | ```
26 | # vmstat 1 5
27 | procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
28 | r b swpd free buff cache si so bi bo in cs us sy id wa st
29 | 0 0 0 707116 15392 177284 0 0 81 5 110 267 0 0 99 1 0
30 | 0 0 0 707100 15392 177284 0 0 0 0 121 274 1 0 99 0 0
31 | 0 0 0 706712 15392 177284 0 0 0 0 107 254 0 1 99 0 0
32 | 0 0 0 706696 15392 177284 0 0 0 40 94 235 0 0 100 0 0
33 | 0 0 0 706712 15392 177284 0 0 0 0 93 231 0 0 100 0 0
34 | ```
35 |
36 | - r(run):表示正在运行或等待CPU时间片的进程数,**该数值如果长期大于服务器CPU的个数,则说明CPU资源不够用了**。
37 | - b(block):表示等待资源(I/O、内存等)的进程数。举个例子,当磁盘读写非常频繁时,写数据就会非常慢,此时CPU运算很快就结束了,但进程需要把计算的结果写入磁盘,这样进程的任务才算完成,因此这个任务只能慢慢等待磁盘了。**该数值如果长时间大于1,则需要查一下具体是缺的哪项资源**。
38 | - si和so:分别表示由交换区写入内存的数据量以及由内存写入交换区的数据量;**一般情况下,si、so的值都为0,如果si、so的值长期不为0,则表示系统内存不足**,需要借用磁盘上的交换区,由于这往往对系统性能影响极大,因此需要考虑是否增加系统内存。
39 | - bi和bo:分别表示从块设备读取数据的量和往块设备写入数据的量;**如果这两个值很高,那么表示磁盘I/O压力很大**。
40 | - wa:表示I/O等待所占用CPU的时间百分比。**wa值越高,说明I/O等待越严重。如果wa值超过20%,说明I/O等待严重。**
41 |
42 | 另外,`vmstat`命令后可带两个数字,第一个数字表示每多少秒打印一次结果,第二个数字表示总共打印多少次结果;如果只有第一个数字,则会不停地打印结果,直到你终止该命令。
43 |
44 | ### 用top命令显示进程所占的系统资源
45 | `top`命令的结果有很多信息,但我们主要用它来监控进程所占的系统资源。top命令的结果每隔3秒变1次,它的特点是把占用系统资源(CPU、内存、磁盘I/O等)最高的进程放到最前面。
46 | ```
47 | PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
48 | 1 root 20 0 41060 3576 2396 S 0.0 0.4 0:00.89 systemd
49 | 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
50 | 3 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
51 | ```
52 | 这里面我们主要关注RES(所占内存大小)、%CPU、%MEM(占用内存的百分比)、COMMAND这4个字段。
53 |
54 | 另外,如果需要一次性打印系统资源的使用情况,可以使用`top - bn1`。
55 |
56 | ### 监控网卡流量
57 |
58 | #### 使用sar命令查看网卡流量历史记录
59 | 使用`sar`命令前可能需要先进行安装:`yum install -y sysstat`。
60 |
61 | 使用方法是:`sar -n DEV`,第一次使用时会报错,因为还没有生成相应的数据记录。打印出来的结果里有很多字段,我们关注`rxpck/s`和`rxkB/s`。
62 | - `rxpck/s`表示网卡每秒收取的包的数量,如果数值大于4000则考虑是被攻击了。
63 | - `rxkB/s`表示网卡每秒收取的数据量(单位为KB)。
64 |
65 | #### 使用nload命令监控网卡实时流量
66 | 使用`nload`前需进行安装:`yum install -y epel-release;yum install -y nload`。
67 |
68 | 使用起来也很简单,直接使用`nload`命令则可动态显示当前的网卡流入/流出的流量。
69 |
70 | ### 使用free命令查看内存使用状况
71 | 为了检查内存是否够用,除了`vmstat`外,我们还可以使用更直接有效的`free`命令:`free -h`。
72 |
73 | ```
74 | # free -h
75 | total used free shared buff/cache available
76 | Mem: 992M 141M 462M 516K 388M 714M
77 | Swap: 1.0G 0B 1.0G
78 | ```
79 | - total:内存总量,相当于used+free+buff/cache=used+available。
80 | - used:已真正使用的内存量。
81 | - free:剩余(未被分配)的内存量。
82 | - shared:不关注。
83 | - buff/cache:缓解CPU和I/O速度差距所用的内存缓存区,由系统预留出来备用,但如果剩余内存都不够用了,那么这部分也是可以挪用出来供服务来使用的。
84 | - available:可用内存,相当于free+buff/cache。
85 |
86 | ### 使用ps命令查看系统进程
87 | 与`top`命令类似,`ps`命令也是用来查看系统具体进程占用资源的情况;由于`top`命令本身是动态的,而`ps`命令是非动态的(相当于执行命令时的一个快照),因此`ps`命令的功能实际上更接近于`top -bn1`。
88 |
89 | ### 使用netstat命令查看网络情况
90 | `netstat`的功能非常强大,这里举两个实际使用场景:`netstat -lnp`(打印当前系统启动哪些端口)和`netstat -an`(打印网络连接状况)。
91 |
92 | ```
93 | # netstat -an
94 | Proto Recv-Q Send-Q Local Address Foreign Address State
95 | tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
96 | tcp 0 0 172.18.63.215:36492 140.205.140.205:80 ESTABLISHED
97 | ```
98 |
99 | ```
100 | netstat -lnp
101 | Active Internet connections (only servers)
102 | Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
103 | tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1604/mysqld
104 | tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1636/httpd
105 | tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 1639/vsftpd
106 | tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 674/sshd
107 | tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1636/httpd
108 | ```
109 |
--------------------------------------------------------------------------------
/content/process.md:
--------------------------------------------------------------------------------
1 | # 第三章:CentOS的进程系统
2 |
3 | ## 如何暂停以及恢复当前进程的执行
4 | - 使用`Ctrl+D`暂停当前进程。
5 | - 进程被暂停后,使用`fg`把进程恢复到前台继续执行。
6 | - 进程被暂停后,使用`bg`把进程恢复到后台继续执行。
7 | - 如有多个进程被暂停,则可通过`jobs`命令查看其编号,再通过`fg [被暂停进程编号]`或`bg [被暂停进程编号]`,来恢复执行。
8 |
9 | ## 如何让linux命令在后台执行
10 | - 在命令后加上符号`&`即可让linux命令在后台执行,例如`sellp 30 &`。
11 | - 如该linux命令正在前台运行,可使用`Ctrl+D`暂停后,再使用`bg`把进程恢复到后台继续执行。
12 |
13 | ## 如何让后台正在运行的进程转到前台来
14 | 1. 对于所有运行的程序,我们可以用`jobs –l`指令查看,此时记住想要转到前台运行的进程的编号。
15 | 2. 我们可以用`fg %[number]`指令把一个程序调到前台来运行。
16 |
17 | ## 使用kill命令结束一个进程
18 | `kill`命令的语法是`kill 进程的pid`;有时这样并不能终止进程,可以考虑使用`kill -9 进程的pid`,这会强制终止一个进程。
19 |
20 | 进程的pid可以通过`top`命令或`ps`命令进行查看。
--------------------------------------------------------------------------------
/content/rsync.md:
--------------------------------------------------------------------------------
1 | ## 备份工具rsync
2 | rsync是Linux系统下最具代表性的数据备份工具,它具有以下特点:
3 | - 不仅可以远程同步,还可以在本地进行同步。
4 | - 增量更新,减少同步的流量。
5 | - 可以在windows和mac下使用,能够做到跨平台使用。
6 | - 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
7 | - 比较安全,可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
8 |
9 | ### rsync的命令格式
10 | `rsync`命令的格式是:`rsync [OPTION]... SRC DEST`;其中,SRC和DEST既可以取本地目录/文件,也可以取远程目录/文件,如`rsync [OPTION]... SRC [USER@]HOST:DEST`或`rsync [OPTION]... SRC [USER@]HOST::DEST`。
11 |
12 | SRC可以由多个文件组成,以此来实现一次性指定同步多个文件:`rsync [OPTION]... SRC1 SRC2 SRC3 DEST`。
13 |
14 | ### rsync的常用参数
15 | - -a:归档模式,表示以递归方式传输文件,并保持所有属性,它等同于`-rlptgoD`,这是最常用的参数。另外,为灵活起见,`-a`后可跟一个`--no-OPTION`来表示关闭`-rlptgoD`中的任意一个参数,比如`-a--no-l`等同于`-rptgoD`。
16 | - -r:表示以递归模式处理子目录。
17 | - -v:表示打印同步的汇总结果信息。
18 | - -l:表示保留软连接。
19 | - -L:表示如果SRC中含有软连接文件,则取其指向的目标文件同步到DEST(当然软连接就不会同步过去了)。
20 | - --delete:表示删除DST中SRC没有的文件。
21 | - --exclude=PATTERN:表示指定排除不需要传输的文件。
22 | - --progress:表示动态打印rsync同步过程中的状态以及最后的汇总结果信息(即包含了`-V`的效果)。
23 | - -z:表示将会在同步传输过程中压缩。
24 |
25 | ### rsync远程同步
26 | 虽然rsync可以在本地的两个目录中进行同步,但其实rsync更常见的应用场景应该是在两台机器中进行远程同步(不论是通过局域网还是互联网)。
27 |
28 | #### 通过ssh的方式进行远程同步
29 | 其命令格式(注意是只有一个**冒号**)如下:
30 | - `rsync [OPTION]... SRC [USER@]HOST:DEST`
31 | - `rsync [OPTION]... [USER@]HOST:SRC DEST`
32 |
33 | 在输入命令后,接下来的事情其实跟openssh的反馈十分类似:如没有通过密钥认证,则需要输入密码进行认证。认证通过后,接下来的rsync执行结果就跟在本地同步操作的没有两样了。
34 |
35 | #### 通过rsync后台服务(rsyncd)的方式进行远程同步
36 | 这种方式的原理是在远程服务器上建立rsync服务器,并将本机看成是rsync客户端,这样我们就可以利用rsync客户端远程连接(via TCP)rsync服务器进行远程同步的操作了。
37 |
38 | 下面我们分**服务器端**和**客户端**两个部分来解释通过rsync后台服务(rsyncd)的方式进行远程同步的整个过程。
39 |
40 | ##### 服务器端
41 |
42 | ###### rsync配置文件
43 | 启动rsync后台服务rsyncd前,我们需要先准备一份rsyncd的配置文件——`/etc/rsyncd.conf`,该配置可控制rsync的权限、可操作范围、日志、可用模块等方方面面的内容。
44 |
45 | 配置文件由两个部分组成:全局变量和模块变量,但你也可以把模块变量写在全局变量里,作为模块变量的一份默认值。
46 |
47 | 下面列出一些常用的变量,具体的变量列表请查询[官方文档](https://download.samba.org/pub/rsync/rsyncd.conf.html)。
48 |
49 | - 全局变量
50 | - pid file:指定pid文件路径(记录rsyncd的进程ID) 。
51 | - port:指定监听哪个端口,默认是873端口。
52 | - address:指定启动服务的IP,假如你的服务器有多个IP,那么可以指定其中的一个IP来启动rsyncd服务;如果不指定则默认在全部IP上启动
53 | - 模块变量
54 | - path:指定该模块的根目录,客户端在访问该模块时不能超出该根目录的层级范围。另外,此变量可以达成某些骚操作,比如`path=/home/%RSYNC_USER_NAME%`,则当连接服务器端的rsync用户名为**test**时,该模块的根目录就自动设置为`/home/test`,但为了安全起见,不推荐这么干。
55 | - use chroot:值取**true**或**false**,表示在传输文件前,首先`chroot`到**path参数**所指定的目录下,这样做可以获得额外的安全防护;但是缺点是需要以root权限来启动rsyncd,并且,如果同步的内容里有软连接指向模块根目录以外的文件,则不能实现同步。
56 | - max connections:指定该模块最大的连接数,如果有超出此最大连接数的客户端试图连接,则会被告知**try later**;该值默认为0,即没有限制最大连接数。
57 | - log file:指定日志文件的路径,默认使用**syslog**。该日志文件意义非常重大,尤其是在架构rsync服务初期,可能会遇到相当多的问题,需要通过查看日志来定位问题所在。同时,即使为模块设置了**log file**,还是建议给全局设置一份,因为有些权限校验的异常,还是会被记录到全局日志文件里的。
58 | - read only:值取**true**或**false**,决定客户端是否能往服务器端上传/删除文件,默认为**false**。
59 | - write only:值取**true**或**false**,决定客户端是否能从服务器端下载文件。
60 | - list:值取**true**或**false**,决定客户端查询服务器端所有可用模块时是否显示,默认显示。
61 | - uid/gid:决定传输文件时以哪个身份(用户/组)进行传输,这关系到rsyncd是否有相应的文件系统权限来读取或修改文件。如果不设置此参数,则据此逻辑处理:如果rsyncd由超级用户启动,则以**nobody**这个系统用户的身份进行传输;否则,按启动rsyncd的用户身份进行传输。因此,如果想以root/root身份进行传输,则必须设置此参数为`uid=root;gid=root`。
62 | - filter/include from/include/exclude from/exclude:表示同步文件的白名单及黑名单;虽然在rsync客户端使用的`rsync`命令里也有对应的可选参数,但在服务器端设置的话,更具有强制性和隐蔽性(在客户端访问的时候,只会反馈"不存在",而非"不允许访问")。
63 | - auth users:表示身份验证的规则,不设置则表示所有rsync用户都可访问。指定对象的方式有两种:1. 指定用户名,此用户名并不一定要真实存在于Linux系统中;2. 指定用户组名(并在用户组名前加上`@`以区分开用户名),这个方式是依托于Linux系统的,因此客户端连接使用的用户名以及此处指定的用户组都必须是真实存在于Linux系统中。多个对象可用空格或逗号进行分隔(推荐使用逗号,因为如果使用空格来分隔,那么在用户名/组名中带有空格的情况下,会造成混淆)。另外可以使用参数`deny`/`ro`(read only)/`rw`(read write)来具体控制某个用户或某个组的访问权限,如:`auth users = , joe:deny, @Some Group:deny, admin:rw, @RO Group:ro`。
64 | - secrets file:指定一个声明`用户名:用户密码`或`@用户组名:用户组公共密码`的文件,该参数是`auth users`参数的配套,仅在已设置`auth users`参数的情况下生效,且没有默认值。另外,即使你在`auth users`参数中指定了用户组作为对象,也不一定要在本文件中声明`@用户组名:用户组公共密码`,完全可以罗列出用户组中所有用户的`用户名:用户密码`来满足需求。
65 | - strict modes:值取**true**或**false**,且默认值为**true**。如取**true**值,则`secrits file`参数所对应的文件,其文件系统权限必须设定为600。
66 | - hosts allow / hosts deny:表示rsync客户端的白名单及黑名单,可根据IP或IP段进行设置,且支持以空格为分隔符输入多个对象。
67 |
68 | 这里给出一份参考的rsyncd配置文件:
69 | ```
70 | port=873
71 | log file=/var/log/rsync.log
72 | pid file=/var/run/rsyncd.pid
73 |
74 | [test]
75 | path=/root/rsync/test
76 | use chroot=false
77 | max connections=0 # no limitation
78 | read only=false
79 | list=true
80 | uid=root
81 | gid=root
82 | auth users=rsyncuser
83 | secrets file=/etc/rsyncd.passwd
84 | ```
85 | ###### 启动rsyncd
86 | 启动方式有以下两种:
87 | - 独立的服务:`rsync --daemon --config=/etc/rsyncd.conf`。
88 | - 由**inetd**在监听到请求时再行启动,这里不作阐述。
89 |
90 | ###### rsyncd自启
91 | 为保证rsyncd的可用性(比如在系统异常重启后依然可用),需要把rsyncd设置为开机自启,具体方法请查询`chkconfig`或`systemd`。
92 |
93 | ##### 客户端
94 |
95 | ##### 命令格式
96 | 在这种方式下的命令格式稍有区别:`rsync [OPTION]... RSYNCUSER@HOST::[MODULE[/SRC]] DEST`。多个对象的命令也有所区别:`rsync [OPTION]... RSYNCUSER@HOST::[MODULE1[/SRC1]] ::[MODULE2[/SRC2]] ::[MODULE3[/SRC3]] DEST`
97 |
98 | ##### 关于用户身份验证
99 | 若rsyncd设置了`auth users`,那么客户端在连接的时候就需要提供相应rsync用户的密码了:
100 | - 直接用`rsync [OPTION]... RSYNCUSER@HOST::[MODULE[/SRC]] DEST`命令,接下来会提示需要输入密码。
101 | - 指定存放rsync用户密码的文件路径:`rsync -avL --password-file=/etc/rsyncd.passwd RSYNCUSER@HOST::[MODULE[/SRC]],此密码文件中,就只放密码:
102 | ```
103 | # cat /etc/rsyncd.passwd
104 | test123
105 | ```
106 |
107 | ### rsync进阶运用
108 |
109 | #### rsync配合inotify实现文件目录实时同步
110 | 需求假设是这样的:
111 | - 服务器A随着程序的执行,不断产生新的日志。
112 | - 服务器B和C需要把服务器A上的日志拉取过来,并需要保证实时性。
113 |
114 | 如果我们用crond轮询来启动rsync发起同步,不仅实时性得不到保证,另外也很可能造成资源的浪费。
115 |
116 | 那么,就有了下面这套方案:
117 | - 在服务器B和C上启动rsyncd。
118 | - 在服务器A上利用inotify监控日志的产生,每逢有新日志产生inotify都会调用我们写好的shell脚本,这样我们就可以在脚本中,把服务器A作为rsync客户端,远程连接服务器B和C,达到把服务器A上新产生的日志推送到服务器B和C上的目的。
119 |
120 |
--------------------------------------------------------------------------------
/content/shell.md:
--------------------------------------------------------------------------------
1 | # 第七章:shell相关
2 |
3 | ## 区分登录/非登录shell,以及交互式/非交互式shell
4 | ### 登录/非登录shell
5 | - 登录shell(login shell)指的是需要用户输入用户名和密码才能进去的shell,这是比较常用的。
6 | - 非登录shell(non-login shell)则相反,不需要用户输入用户名和密码,例如:
7 | - 直接命令`bash`(不带`--login`参数)就是打开一个新的非登录shell。
8 | - 在图形界面(如Gnome或KDE)中打开一个“终端”(terminal)窗口程序也是属于打开了一个非登录shell。
9 |
10 | ### 交互式/非交互式shell
11 | - 交互式shell(interactive shell)指的是shell等待自然人用户在终端上的输入,并且按照用户输入的命令立即执行且返回执行结果,这种操作模式被称为“交互式模式”,是我们最直观、最常用的操作方式。
12 | - 非交互式shell(non-interactive shell)针对的并非自然人用户,它通常是以 shell script (即以 shell 的语法所写成的程序代码段)的方式执行。
13 | - 运行shell脚本程序时,系统将创建一个子shell。此时,系统中将有两个shell,一个是登录时系统启动的shell,另一个是系统为运行脚本程序创建的shell。
14 | - 当一个脚本程序运行完毕,它的脚本shell将终止,可以返回到执行该脚本之前的shell。
15 | - 从这种意义上来说,用户可以有许多 shell,每个shell都是由某个shell(称为父shell)派生的。
16 |
17 | ### 不同类型shell初始化时所执行的startup脚本不一样
18 | > 对于Bash来说,登录shell(包括交互式登录shell和使用`–-login`选项的非交互shell),它会首先读取和执行`/etc/profile`全局配置文件中的命令,然后依次查找`~/.bash_profile`、`~/.bash_login` 和 `~/.profile`这三个配置文件,读取和执行这三个中的第一个存在且可读的文件中命令。除非被`–noprofile`选项禁止了。另外,由于`~/.bash_profile`一般也会带有执行`~/.bashrc`的代码段,因此`~/.bashrc`的内容也会被执行。
19 |
20 | > 在非登录shell里,只读取 `~/.bashrc` (和 `/etc/bash.bashrc`、`/etc/bashrc` )文件,不同的发行版里面可能有所不同。
21 |
22 | 从上可得,无论是登录shell还是非登录shell,最终都会执行`~/.bashrc`。
23 |
24 | ## 如何让变量突破shell脚本程序的上下文限制:`export`与`source`
25 | 在子 shell 中定义的变量只在该子 shell 内有效。如果在一个 shell 脚本程序中定义了一个变量,当该脚本程序运行时,这个定义的变量只是该脚本程序内的一个局部变量,其他的 shell 不能引用它。
26 |
27 | 有两种方法可以使子 shell 中的变量突破上下文限制,在其它 shell 中也能使用:
28 | - 在脚本代码中使用`export`语句可以对已定义的变量进行输出,如下:
29 | ```
30 | a=2333
31 | export a
32 | ```
33 | `export`命令将使系统在创建每一个新的子 shell 时定义这个变量的一个拷贝。这个过程称之为变量输出。
34 | - 在交互式 shell 中使用`source`命令来执行脚本程序,系统将不会生成新的子 shell 来执行脚本程序,而是直接在当前的交互式 shell 中执行;那么,脚本程序中所有定义(或改变)的变量,都将在当前 shell 中起效。
35 |
36 |
37 | ## 如何配置Linux用户的工作环境,并在下次登录依然保持有效
38 | 当我们直接在命令行里执行配置工作环境的命令,如`alias`,赋值自定义变量等操作时,这些配置仅适用于当前登录的会话,若当前用户退出系统,则这些配置均失效。
39 |
40 | 首先需要明确的是,既然针对的是“Linux用户的工作环境”,那么实际上我们执行的是**登录交互式shell**;因此,若想要这些工作环境的配置在下次登录依然保持有效,可以这样做:
41 |
42 |
43 | - “全局配置”,指的是为当前系统的每一个用户都设置一致的工作环境配置。全局配置的方式是:在`/etc/profile`文件中进行编辑或添加命令语句。
44 | - “每个用户私有配置”,指的是针对Linux中具体某个用户进行特定的工作环境配置,其方式为:在`~/.bash_profile`中进行编辑或添加命令语句。
45 |
46 | ## shell变量规则
47 | - 赋值变量的格式为`a=b`,其中a为变量名,b为赋给变量的值,需要注意的是,等号两边不能有空格。
48 | - 变量名只能由字母、数字以及下划线组成,而且不能以数字开头。
49 | - 当变量内容带有特殊字符(如空格)时,需要加上单引号,如`myname='array huang'`;如果变量内容中本来就含有单引号,此时就需要加双引号了,如`myname="'array huang'"`。
50 | - 如变量中需要用到linux命令的执行结果,可以使用反引号,如
51 | ```
52 | pwdResult=`pwd`
53 | ```
54 | - 拼接变量的方法如下:`myname="$firstName"Huang`
55 |
56 | ## `grep`命令和`egrep`命令不一样的地方
57 | `egrep`是`grep`的扩展版本,因此在功能上前者比后者更为强大,下面介绍它们不同的地方:
58 | - `egrep`支持更多的正则表达式用法:
59 | - `?`(表示0个或1个指定的字符);
60 | - `+`(表示至少1个指定的字符);
61 | - `|`(逻辑或连接符),如`'a|b'`,指的是匹配含有 a **或** b 的字符串;
62 | - `()`表示把多个字符合为1个整体来进行表达,通常搭配其它正则表达式符号来使用,如`'r(oo|at)o'`,指的是匹配含有 rooo 或 rato 的字符串;
63 | - 在`{}`的用法上,`grep`的使用比较麻烦,需要加上转义字符`\`,如`'o\{2\}'`(表示匹配含有 oo 的字符串);而`egrep`的使用则自然得多,同样的正则表达式,直接使用`'o{2}'`即可。
64 |
65 | ## shell脚本
66 |
67 | ### shell脚本的创建和执行
68 | - shell脚本应以`#! /bin/bash`开头来表明该脚本使用的是 bash 语法,如果不写,也能正常运行,但就不符合编码规范了。
69 | - shell脚本的执行有两种方法:
70 | - 执行命令`sh ./file.sh`,并且可以考虑加上`-x`参数,这样就能查看脚本每一步执行的命令与结果了,非常利于调试程序。
71 | - 直接执行`./file.sh`,这种方法使用的前提是用户拥有脚本的执行权限(即**x**权限),需要注意的是新建脚本文件时一般是没有执行权限的,需要通过`chmod`进行修改。
72 |
73 | 因此一般我们直接用第一种方法即可。
74 |
75 | ### 脚本常用命令及语法
76 | 在 shell 脚本中,命令可看作是一般程序语言中的预设函数。
77 |
78 | #### 时间
79 | `date`,时间格式化和简单的计算(如“一天前”、“一小时后”等)。
80 | - 命令(函数)的结果可以通过反引号来赋值给变量,如
81 | ```
82 | nowDate=`date +"%Y-%m-%d"`
83 | ```
84 |
85 | #### 数学运算
86 | - 数学运算有特别的语法:
87 | - 用`$[]`给括起来(只支持整数运算),如:
88 | ```
89 | a=1
90 | b=2
91 | c=$[$a+$b+3]
92 | echo $c // 结果是6
93 | ```
94 | -
95 | - 使用`let`(只支持整数运算):
96 |
97 | ```
98 | var=1
99 | let "var+=1"
100 | echo $var
101 | ```
102 | -
103 | - 使用`(())`(只支持整数运算):
104 | ```
105 | var=1
106 | ((var+=1))
107 | echo $var
108 | ```
109 | -
110 | - 使用bc(可以进行浮点数计算):
111 | ```
112 | var=1
113 | var=`echo "$var+1"|bc`
114 | echo $var
115 | ```
116 | 其原理是:bc是linux下的一个简单计算器,支持浮点数计算,在命令行下输入bc即进入计算器程序,而我们想在程序中直接进行浮点数计算时,利用一个简单的管道即可解决问题。
117 |
118 | #### 和用户交互
119 | 使用`read`命令可达到与用户交互的目的,它会把用户输入的字符串作为变量值,如:
120 |
121 | ```
122 | read -p "please input a number:" x # 执行后等待用户输入并按回车确认
123 | echo "$x"
124 | ```
125 |
126 | #### 执行shell脚本时传参
127 | 在执行脚本时,我们可以通过对脚本传参(当然前提是脚本被设计成可以接受参数),来改变脚本的具体行为,以及得到以此计算出来的结果。
128 |
129 | 脚本内获取参数的格式为:$n。(n代表一个数字,,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推……)。
130 |
131 | 举例说明,当前有一个名为**test.sh**的脚本:
132 |
133 | ```
134 | #!/bin/bash
135 |
136 | echo "Shell 输出脚本名称及参数";
137 | echo "执行的脚本名:$0";
138 | echo "第一个参数为:$1";
139 | echo "第二个参数为:$2";
140 | echo "第三个参数为:$3";
141 | ```
142 |
143 | 运行输出:
144 |
145 | ```
146 | $ ./test.sh 1 2 3
147 |
148 | Shell 传递参数实例!
149 | 执行的文件名:./test.sh
150 | 第一个参数为:1
151 | 第二个参数为:2
152 | 第三个参数为:3
153 | ```
154 |
155 | #### 逻辑判断
156 | 共有以下3种语法:
157 | - 不带`else`:
158 | ```
159 | if [判断语句]; then
160 | command
161 | fi
162 | ```
163 | - 带`else`:
164 | ```
165 | if [判断语句]; then
166 | command
167 | else
168 | command
169 | fi
170 | ```
171 | - 带`elif`:
172 | ```
173 | if [判断语句]; then
174 | command
175 | elif [判断语句]; then
176 | command
177 | else
178 | command
179 | fi
180 | ```
181 |
182 | ##### if语句
183 | ###### 字符串判断
184 |
185 | - str1 = str2 当两个串有相同内容、长度时为真
186 | - str1 != str2 当串str1和str2不等时为真
187 | - -n str1 当串的长度大于0时为真(串非空)
188 | - -z str1 当串的长度为0时为真(空串)
189 | - str1 当串str1为非空时为真
190 |
191 | ###### 数字的判断
192 |
193 | - int1 -eq int2 两数相等为真
194 | - int1 -ne int2 两数不等为真
195 | - int1 -gt int2 int1大于int2为真
196 | - int1 -ge int2 int1大于等于int2为真
197 | - int1 -lt int2 int1小于int2为真
198 | - int1 -le int2 int1小于等于int2为真
199 |
200 | ###### 文件的判断
201 |
202 | - -r file 用户可读为真
203 | - -w file 用户可写为真
204 | - -x file 用户可执行为真
205 | - -f file 文件为正规文件为真
206 | - -d file 文件为目录为真
207 | - -c file 文件为字符特殊文件为真
208 | - -b file 文件为块特殊文件为真
209 | - -s file 文件大小非0时为真
210 | - -t file 当文件描述符(默认为1)指定的设备为终端时为真
211 |
212 | ###### 复杂逻辑判断
213 |
214 | - && 与
215 | - || 或
216 | - ! 非
217 |
218 | 举例:
219 | ```
220 | a=10
221 | if [$a -lt 1] || [$a -gt 5]; then
222 | echo ok; # 输出ok
223 | fi
224 | ```
225 |
226 | #### case语句
227 | 语法如下:
228 | ```bash
229 | case 变量 in
230 | value1)
231 | command
232 | ;;
233 | value2)
234 | command
235 | ;;
236 | *)
237 | command
238 | ;;
239 | esac
240 | ```
241 | 结合执行shell脚本时传入的参数,可以实现功能的切换:
242 | ```bash
243 | #!/bin/bash
244 | case $1 in
245 | start|s) ## |表示or,在这里表示匹配start或s均可
246 | echo service is running
247 | ;;
248 | stop)
249 | echo service is stoped
250 | ;;
251 | reload)
252 | echo service is reload
253 | ;;
254 | *)
255 | echo xxxxx
256 | ;;
257 | esac
258 | ```
259 |
260 | #### for循环
261 | 语法如下:
262 | ```
263 | for 变量名 in 循环的条件; do
264 | command
265 | done
266 | ```
267 |
268 | ##### 循环条件
269 | - 数字递进:`((i=1;i<=10;i++))`
270 | - 列举一组字符串或数字,以空格分隔:`1 2 3 aaa bbb`
271 | - 命令执行的结果:
272 | ```bash
273 | for i in `ls`; do
274 | echo $i is file name\!
275 | done
276 | ```
277 | - 变量的值:
278 | ```bash
279 | list="rootfs usr data data2"
280 | for i in $list; do
281 | echo $i is appoint
282 | done
283 | ```
284 | - 执行脚本时传入的参数:
285 | ```bash
286 | #!/bin/bash
287 |
288 | echo "number of arguments is $#" # $#表示参数的个数
289 |
290 | echo "What you input is: "
291 |
292 | for argument in "$@"; do # $@表示参数列表,此处也可以使用$*代替,$*则把所有的参数当作一个字符串
293 | echo "$argument"
294 | done
295 | ```
296 |
297 | #### while循环
298 | 语法:
299 | ```
300 | while [循环(判断)的条件]; do
301 | command
302 | done
303 | ```
304 |
305 | 注意此处的***循环(判断)的条件***与`if`语句中的判断条件是一样的。
306 |
307 | #### 函数
308 | ##### 函数定义语法:
309 | ```bash
310 | function 函数名() {
311 | command
312 | [return] # 可return也可不return
313 | }
314 | ```
315 |
316 | ##### 函数的调用方法是:
317 | ```bash
318 | #!/bin/bash
319 | function show() {
320 | echo "hello , you are calling the function"
321 | }
322 | echo "first time call the function"
323 | show
324 | echo "second time call the function"
325 | show
326 | ```
327 |
328 | ##### 如函数需要传入参数,则使用$1、$2……等取用:
329 | ```bash
330 | #!/bin/bash
331 | function show() {
332 | echo "hello , you are calling the function $1"
333 | }
334 | echo "first time call the function"
335 | show first # 输出hello , you are calling the function first
336 | echo "second time call the function"
337 | show second # 输出hello , you are calling the function second
338 | ```
339 |
340 | ##### 函数中的关键字“return”可以放到函数体的任意位置,通常用于返回某些值,Shell在执行到return之后,就停止往下执行,返回到主程序的调用行,return的返回值只能是0~256之间的一个整数,返回值将保存到变量“$?”中。
341 | ```
342 | #!/bin/bash
343 | function test() {
344 | return 0
345 | }
346 |
347 | test
348 | echo "$?" # 输出0
349 | ```
350 |
351 | ##### 如果函数在另外一个文件中,我们该怎么调用它呢?
352 | 我们可以使用`source`命令,比如 show 函数写在了function.sh里面了:
353 | ```bash
354 | source function.sh
355 | show
356 | ```
357 |
358 | ##### 函数的变量作用域
359 | 默认情况下,变量具有全局作用域,如果想把它设置为局部作用域,可以在其前加入local
360 |
361 | 例如:
362 | ```bash
363 | local a=hello
364 | ```
365 | 使用局部变量,使得函数在执行完毕后,自动释放变量所占用的内存空间,从而减少系统资源的消耗,在运行大型的程序时,定义和使用局部变量尤为重要。
366 |
367 | ##### 函数的嵌套
368 | 函数可以进行嵌套,实例:
369 | ```bash
370 | #!/bin/bash
371 | function first() {
372 | function second() {
373 | function third() {
374 | echo "------this is third"
375 | }
376 | echo "this is the second"
377 | third
378 | }
379 | echo "this is the first"
380 | second
381 | }
382 |
383 | echo "start..."
384 | first
385 | ```
386 |
387 | #### shell脚本中的中断和继续
388 |
389 | ##### break
390 | 与其它程序语言一样,`break`是用来跳出当前的循环的,`break`后可加数字,如`break 2`表示跳出两层的循环。
391 |
392 | ##### continue
393 | 与其它程序语言一样。
394 |
395 | ##### exit
396 | 退出脚本,或者更准确地说,是退出当前运行脚本的shell(既然运行环境关闭了,那么脚本当然也不能继续执行下去了)。
397 |
398 | 在函数中使用`exit`,是会退出整个脚本的,因此若只是想退出函数,请使用`return`。
399 |
400 | `exit`后可接数字作为状态码,如`exit 0`;一般来说,**0**表示脚本执行成功,其它状态码则表示各种可能的异常情况
401 |
402 | ## 别名
403 | 我们可以通过`alias`命令把一个常用的并且很长的指令另取名为一个简单易记的指令;如果不想用了,还可以使用`unalias`来取消。
404 |
405 | - 使用`alias`命令,可以列出系统当前所有的别名。
406 | - 使用`alias [命令别名]=['具体的命令']`可以自定义别名,例如:`alias aming='pwd'`。
407 | - 使用`unalias`可以取消别名,例如`unalias aming`。
408 |
409 | ## 重定向
410 | 重定向分为“输入重定向”和“输出重定向”,重定向一般通过在命令间插入特定的符号来实现。
411 |
412 | - `command1 > file1`,这个命令执行command1然后将执行后输出的内容存入file1。注意任何file1内的已经存在的内容将被新内容替代。如果要将新内容添加在文件末尾,请使用>>操作符。
413 | - `command1 < file1`,执行command1,使用file1作为用来替代键盘的输入源。
414 | - `command1 < infile > outfile`,同时替换输入和输出,执行command1,从文件infile读取内容,然后将输出写入到outfile中。
415 |
416 | 另外,上述的“输出内容”仅指命令执行的结果,若命令执行中出现错误,则错误信息需要另行处理:
417 |
418 | - `command1 2> file1`,执行command1,然后将标准错误输出重定向到文件file1。
419 | - 另外一个很有用的功能是将标准错误输出融合到标准输出中去,这样错误信息可以和其他普通的输出信息一起处理。例如:`command > file 2>&1`,这表示将command执行后的结果与错误信息均写入到file里。
420 |
421 | ## 管道符
422 | 管道符的标识为`|`,它用于将前一个指令的输出作为后一个指令的输入,格式如下:`command1 | command2`,举例说明:`yum list | grep zip | head -n 5`。
--------------------------------------------------------------------------------
/content/ssh.md:
--------------------------------------------------------------------------------
1 | ## SSH远程登录
2 | Linux系统通过sshd(ssh daemon)服务实现远程登录的功能,其默认端口是22,此服务为Linux系统预装,并预设开机自启,因此不需要额外设置便能够实现Linux远程登录。
3 |
4 | ### Linux系统上的ssh客户端——openssh
5 | Windosw系统上有许多软件可以实现ssh远程登录,比如说putty、SecureCRT、Xshell等,那么,我们在Linux系统上,应该使用哪个ssh客户端呢?这里推荐使用**openssh-clients**(简称**openssh**)。
6 |
7 | ### ssh的校验登录方式
8 | ssh支持两种方法进行远程校验登录:使用密码登录和使用密钥登录。
9 |
10 | #### 使用密码登录
11 | 使用命令`ssh user@host`,如`ssh root@192.168.80.128`,系统会进一步提示输入密码,正确输入后,你已经成功登录到远程服务器上了,你的一切操作都跟本地操作无异。
12 |
13 | #### 使用密钥认证登录
14 | 由于密码登录存在易泄露、易破解的问题,因此一般主张使用ssh远程登录时,仅使用密钥进行登录,并关闭密码远程登录的权限。
15 |
16 | 所谓密钥认证,实际上是使用了一对加密的字符串:公钥(public key)和私钥(private key),其一般使用**RSA**算法生成。公钥用于加密,任何人都可以看到其内容;而私钥用于解密,只有拥有者才能看到其内容;那么显而易见,即使坏人拿到公钥,也无法解读加密后的内容,因此可以保证内容的保密性。
17 |
18 | 下面介绍具体的操作:
19 | 1. 为本地本用户账号生成一个密钥对:`ssh-keygen`,系统会进一步提示你输入密钥存放的目标目录,按回车键保持默认即可(默认为`~/.ssh`);接下来会提示你输入密钥的密码,一般留空即可(直接按回车键);此时会告知你密钥对已成功生成。
20 | 2. 查看刚生成的公钥内容:`cat ~/.ssh/id_rsa.pub`。
21 | 3. 方法有两个:
22 | - 使用命令`ssh-copy-id user@host`即可把本地的公钥复制到远程服务器的**authorized_keys**文件上,并给相应的目录和文件设置好合适的权限,详情请看这里[SSH-COPY-ID](https://www.ssh.com/ssh/copy-id)。
23 | - 复制公钥的全部内容,粘贴到远程服务器的`~/.ssh/authorized_keys`里。远程服务器上如果不存在这个**authorized_keys**文件,则直接新建一个;如果已存在,则注意需要另起一行,加到此文件末尾。这一个过程也可使用以下命令进行操作:`$ ssh user@host 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub`。需要注意,使用此手动方法,需要相应的设置好**authorized_keys**文件的权限,推荐为**600**。
24 | 4. 大功告成,从此你使用ssh远程登录,就不需要再输入密码了。
25 |
26 | ### 使用ssh远程执行命令并获得执行结果
27 |
28 | #### 例子1:复制公钥
29 | 上述的`$ ssh user@host 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub`就是一个很好的例子:
30 | 1. `ssh user@host`,表示登录远程主机;
31 | 2. 单引号中的`mkdir .ssh && cat >> .ssh/authorized_keys`,表示登录后在远程shell上执行的命令;
32 | 3. `mkdir -p .ssh`的作用是:如果用户主目录中的.ssh目录不存在,就创建一个;
33 | 4. `cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub`的作用是:将本地的公钥文件`~/.ssh/id_rsa.pub`,重定向追加到远程文件**authorized_keys**的末尾。
34 |
35 | #### 例子2:查看远程主机是否运行进程httpd
36 | ```
37 | $ ssh user@host 'ps ax | grep [h]ttpd'
38 | ```
39 |
40 | ### 退出当前ssh会话
41 | 使用`exit`命令或`logout`命令即可。
--------------------------------------------------------------------------------
/content/system-log.md:
--------------------------------------------------------------------------------
1 | ## Linux系统日志
2 |
3 | ### 核心系统日志文件——/var/log/messages
4 | Linux的核心系统日志文件是`/var/log/messages`,它包含了以下内容:
5 | - 系统启动时的引导消息
6 | - I/O错误
7 | - 网络错误
8 | - 其它系统运行时发送的错误
9 | - 单纯的操作记录
10 |
11 | `/var/log/messages`是由**rsyslogd**这个守护进程生成的,如果**rsyslogd**被停止了,则系统将不会生成新的`/var/log/messages`。
12 |
13 | ### 安全日志
14 |
15 | #### 使用`last`命令查看Linux系统的登录日志
16 | ```
17 | # last | head
18 | root pts/0 192.168.80.1 Mon Feb 5 11:28 still logged in
19 | root tty1 Mon Feb 5 11:26 still logged in
20 | reboot system boot 3.10.0-693.11.1. Mon Feb 5 11:26 - 11:28 (00:01)
21 | root tty1 Sun Feb 4 11:34 - crash (23:51)
22 | ```
23 | 上面的字段从左到右依次为账户名称、登录终端、登录客户端IP、登录时间段。
24 |
25 | 以上面这4条截取的日志来分析,它们是按时间从新到旧进行排列的,因此我们先从第四条开始分析。第四条日志其实是我在虚拟机以root账户登录后直接关闭虚拟机,因此我们可以发现,账户名称是root,登录终端是tty1(相当于本地登录了),没有客户端IP(因为是本地登录),登录时间段的末尾是**crash**(因为我不是正常的关机)。
26 |
27 | 根据上面的逻辑继续来分析就非常清晰了:
28 | - 第3条是我重新开机虚拟机的日志;
29 | - 第2条是开机成功后,我在本地使用root账户登录;
30 | - 第1条是我在本物理机(windows系统),开SecureCRT,同样使用root账户,进行远程登录(虽然两个系统在同一台物理机上,但虚拟机是相对独立的,因此其实看作这是两台机器,所以也就是远程登录而非本地登录了)。
31 |
32 | 另外再介绍一个与登录信息有关的日志文件——`/var/log/secure`:
33 | ```
34 | Feb 5 11:28:14 localhost sshd[1462]: pam_unix(sshd:session): session opened for user root by (uid=0)
35 | Feb 5 11:28:14 localhost sshd[1462]: Accepted password for root from 192.168.80.1 port 53703 ssh2
36 | Feb 5 11:26:46 localhost login: ROOT LOGIN ON tty1
37 | Feb 5 11:26:46 localhost login: pam_unix(login:session): session opened for user root by LOGIN(uid=0)
38 | Feb 5 11:26:37 localhost sshd[1102]: Server listening on :: port 22.
39 | Feb 5 11:26:37 localhost sshd[1102]: Server listening on 0.0.0.0 port 22.
40 | Feb 5 11:26:35 localhost polkitd[535]: Acquired the name org.freedesktop.PolicyKit1 on the system bus
41 | Feb 5 11:26:35 localhost polkitd[535]: Finished loading, compiling and executing 2 rules
42 | Feb 5 11:26:35 localhost polkitd[535]: Loading rules from directory /usr/share/polkit-1/rules.d
43 | Feb 5 11:26:35 localhost polkitd[535]: Loading rules from directory /etc/polkit-1/rules.d
44 | Feb 4 21:36:42 localhost sshd[1434]: pam_unix(sshd:session): session closed for user root
45 | ```
46 | 对照着`last`命令的结果,`/var/log/secure`是不是挺容易看明白的呢?
--------------------------------------------------------------------------------
/content/systemd.md:
--------------------------------------------------------------------------------
1 | # 第一章:CentOS的系统服务管理系统
2 |
3 | ## Linux系统服务管理
4 | 从CentOS7开始,CentOS的服务管理工具由SysV改为了systemd,但即使是在CentOS7里,也依然可以使用`chkconfig`这个原本出现在SysV里的命令。
5 |
6 | Systemd的设计目标是,为系统的启动和管理提供一套完整的解决方案。
7 |
8 | ### chkconfig服务管理工具
9 |
10 | #### 罗列chkconfig所管理的服务
11 | 使用`chkconfig --list`命令可以列出所有的服务及其在每个级别(run level)下的自启状态。
12 | ```
13 | netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
14 | network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
15 | ```
16 | 这里我们只关心第3级和第5级:第3级表示完整的多用户模式,是标准的运行级,也即我们平常最常用的文字模式;第5级表示图形界面的管理模式。
17 |
18 | 需要注意的是,在CentOS7中,`chkconfig`只保留极少量的SysV服务,其它服务请使用systemd进行管理。
19 |
20 | #### 使用chkconfig更改某服务在某级别下的自启状态
21 | 例如,使用`chkconfig --level 345 network off`即可关闭network这个服务在第3/4/5级中的自启;另外如果不传入参数`--level`,则默认针对级别2/3/4/5操作。
22 |
23 | #### 为chkconfig添加/删除管理的服务项
24 | 简单例如:
25 | ```
26 | # chkconfig --del network
27 | # chkconfig --add network
28 | ```
29 |
30 | ### systemd服务管理工具
31 |
32 | #### 罗列systemd所管理的服务
33 | 使用`systemctl list-units --all --type=service`:
34 | ```
35 | # systemctl list-units --all --type=service
36 | UNIT LOAD ACTIVE SUB DESCRIPTION
37 | aegis.service loaded active running LSB: aegis update.
38 | agentwatch.service loaded active exited SYSV: Starts and stops guest agent
39 | aliyun-util.service loaded active exited Initial Aliyun Jobs
40 | aliyun.service loaded active running Aliyun Service Daemon
41 | ```
42 |
43 | 这些服务对应的启动脚本文件保存在`/usr/lib/systemd/system`。
44 |
45 | #### systemd的基本概念
46 | systemd把系统的各项资源(包括各个服务、设备等)都看作是**unit**,unit有许多种类,我们目前关心的是**service**和**target**。这里的service并不是什么新概念,因此只解释一下target:target是多个unit的组合,启动一个target也就相当于启动其中包含的所有unit;SysV中的run level在systemd里被target所取代,例如系统以多用户文字模式(runlevel 3)启动时,就会启动**multi-user.target**,而以图形界面模式(runlevel 5)启动时,则会启动**graphical.target**;target之间并非互斥的,因此可以同时启动多个target。
47 |
48 | 我们可以用`systemctl list-dependencies multi-user.target`来列举multi-user.target所包含的内容:
49 | ```
50 | # systemctl list-dependencies multi-user.target
51 | multi-user.target
52 | ● ├─aegis.service
53 | ● ├─agentwatch.service
54 | ● ├─aliyun-util.service
55 | ● ├─aliyun.service
56 | ● ├─brandbot.path
57 | ● ├─crond.service
58 | ● ├─dbus.service
59 | ● ├─network.service
60 | ● ├─ntpd.service
61 | ● ├─plymouth-quit-wait.service
62 | ● ├─plymouth-quit.service
63 | ● ├─rc-local.service
64 | ● ├─rsyslog.service
65 | ● ├─sshd.service
66 | ● ├─sysstat.service
67 | ● ├─systemd-ask-password-wall.path
68 | ● ├─systemd-logind.service
69 | ● ├─systemd-readahead-collect.service
70 | ● ├─systemd-readahead-replay.service
71 | ● ├─systemd-update-utmp-runlevel.service
72 | ● ├─systemd-user-sessions.service
73 | ● ├─basic.target
74 | ● │ ├─microcode.service
75 | ● │ ├─rhel-autorelabel-mark.service
76 | ● │ ├─rhel-autorelabel.service
77 | ● │ ├─rhel-configure.service
78 | ● │ ├─rhel-dmesg.service
79 | ● │ ├─rhel-loadmodules.service
80 | ● │ ├─paths.target
81 | ● │ ├─slices.target
82 | ● │ │ ├─-.slice
83 | ● │ │ └─system.slice
84 | ● │ ├─sockets.target
85 | ● │ │ ├─dbus.socket
86 | ● │ │ ├─systemd-initctl.socket
87 | ● │ │ ├─systemd-journald.socket
88 | ● │ │ ├─systemd-shutdownd.socket
89 | ● │ │ ├─systemd-udevd-control.socket
90 | ● │ │ └─systemd-udevd-kernel.socket
91 | ● │ ├─sysinit.target
92 | ● │ │ ├─dev-hugepages.mount
93 | ● │ │ ├─dev-mqueue.mount
94 | ● │ │ ├─kmod-static-nodes.service
95 | ● │ │ ├─ldconfig.service
96 | ● │ │ ├─plymouth-read-write.service
97 | ● │ │ ├─plymouth-start.service
98 | ```
99 |
100 | 可以看出这其中就包含了不少target,比如**basic.target**,因此target是可以嵌套的。
101 |
102 | #### systemd常用命令
103 | ```
104 | # systemctl enable crond.service // 让某个服务开机自启(.service可以省略)
105 | # systemctl disable crond // 不让开机自启
106 | # systemctl status crond // 查看服务状态
107 | # systemctl start crond // 启动某个服务
108 | # systemctl stop crond // 停止某个服务
109 | # systemctl restart crond //重启某个服务
110 | # systemctl reload * # 重新加载服务配置文件
111 | # systemctl is-enabled crond // 查询服务是否开机启动
112 | ```
113 |
114 | ## systemd功能介绍
115 | Systemd 是 Linux 的系统工具,用来启动守护进程,已成为大多数发行版的标准配置。
116 |
117 | 它的设计目标是,为系统的启动和管理提供一套完整的解决方案。
118 | 根据 Linux 惯例,字母d是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。
119 |
120 | 从CentOS7开始,CentOS的服务管理工具由SysV改为了systemd,但即使是在CentOS7里,也依然可以使用`chkconfig`这个原本出现在SysV里的命令。
--------------------------------------------------------------------------------
/content/vim.md:
--------------------------------------------------------------------------------
1 | # Vim的使用
2 |
3 | ## vim基本操作
4 |
5 | ### 移动光标
6 | 下面操作均需处在一般模式(默认的模式)下:
7 | - `h``j``k``l`分别为“左”“下”“上”“右”
8 | - 翻半页:`Ctrl + d`(`d` for down),`Ctrl + u`(`u` for up)。
9 | - 翻一页:`Ctrl + f`(`f` for front),`Ctrl + b`(`b` for back)。
10 | - `gg`表示移到到首行。
11 | - `G`表示移动到尾行。
12 | - `nG`(`n`指的是数字)表示移动到第n行;一般用于根据程序错误提示信息进行 bug fix。
13 | - `0`表示移到光标所在行的行首; $表示移动到光标所在行的行尾。
14 |
15 | ### 复制剪切粘贴
16 |
17 | - 按`yy`复制光标所在行。
18 | - 按`dd`剪切光标所在行,如果光剪切不粘贴,那就相当于删除。
19 | - 按`p`将复制/剪切的内容粘贴至光标后,因为光标是在具体字符的位置上,所以实际是在该字符的后面;整行的复制粘贴在游标的下一行。
20 |
21 | #### v模式相关
22 | - 按`v`切换到“高亮选择模式”,移动光标进行选择。
23 | - 在`v模式`下,按`y`(`y` for yank)复制高亮选择的内容。
24 | - 在`v模式`下,按`d`剪切高亮选择的内容,如果光剪切不粘贴,那就相当于删除。
25 |
26 | ### 撤销、重做
27 | 以下仅讨论vim下的操作(vi的操作稍有不同):
28 | - 按`u`进行撤销,可多次撤销。
29 | - 按`Ctrl + r`(`r` for redo)进行重做,可多次重做。
30 |
31 | ### 进入编辑模式
32 | 下面所有操作均需在一般模式下执行:
33 | - `i`,在当前光标所在字符前插入。
34 | - `o`,在当前光标所在行的下一行插入新的一行。
35 | - `O`,在当前光标所在行的上一行插入新的一行。
36 |
37 | ### 查找和替换字符串
38 | 下面所有操作均需在一般模式下执行:
39 | - `/word`,向下查找一个字符串word,查找后按`n`看下一匹配结果,按`N`看上一匹配结果。
40 | - `?word`,向上查找一个字符串word,查找后按`n`看下一匹配结果,按`N`看上一匹配结果。
41 | - `:n1,n2s/word1/word2/g`,在n1和n2行之间查找word1并替换为word2,其中n1、n2皆可取数字,另外n2可取$表示最后一行。
42 |
43 | ### 行号相关
44 | - `:set nu`表示显示行号。
45 | - `:set nonu`表示不显示行号。
46 |
47 | ### 保存及退出
48 | - `:w`,保存文本。
49 | - `:q`,退出vim。
50 | - `:w!`,强制保存,在root用户下,即使文本只读也可以完成保存。
51 | - `:q!`,强制退出,所有改动不生效。
52 | - `:wq`,保存并退出。
--------------------------------------------------------------------------------
/content/xargs.md:
--------------------------------------------------------------------------------
1 | # 神奇的xargs命令
2 |
3 | ## xargs命令:将stdin转换成传入其它命令的参数
4 | `xargs`命令的作用在于给别的命令传递参数,其一般配合管道符`|`来使用,把前一命令的stdout作为自己的stdin,再转换成`command line`形式的参数传给其它命令。
5 |
6 | ### xargs命令的语法
7 | 其一般出现的形式如下:
8 | ```
9 | OtherCommand [options] | xargs [options] [TargetCommand [options]]
10 | ```
11 | 如:
12 | ```
13 | find /tmp -name "*.log" -type f -print | xargs /bin/rm -f
14 | ```
15 | 上面这是`xargs`命令的常用场景,配合`find`命令,找到`/tmp`目录下所有日志文件并予以删除。
16 |
17 | ### xargs命令的意义
18 | - 虽然管道能把别的命令的stdout作为下个命令的stdin传入,但毕竟并非所有的命令都接受stdin的,如`ls`;比较常见接受stdin的命令有`cat`、`less`;而`xargs`命令能转化stdin的命令正好弥补了这些不接受stdin的命令的不足。
19 | - 对于大数据量的操作来说,如上面的例子,一次性删除大量文件,若直接使用`rm -f /tmp/*.log`,很可能会报错`/bin/rm Argument list too long`,而如果我们用上`xargs`命令,`xargs`会帮我们把待删的文件分批交给`rm`命令来执行。
20 | - 某些命令针对`xargs`调用的方式进行了优化,达到更进一步的效果,如:
21 | ```bash
22 | # ls | xargs ls
23 | file1 file2 file3
24 |
25 | dir1:
26 | file4
27 |
28 | dir2:
29 | file5 file6 file7
30 | ```
31 |
32 | ### xargs命令的工作原理
33 | 想了解`xargs`命令的工作原理,其实很简单;`xargs`命令在不指定目标命令时,其默认目标命令实际上是`echo`:
34 | ```
35 | # ls ./ | xargs echo
36 | file1 file2 file3
37 | # ls ./ | xargs
38 | file1 file2 file3
39 | # ls ./
40 | file1 file2 file3
41 | ```
42 | 从以上命令的执行结果我们可以看到,`xargs`命令实际上就是**将所有空格、制表符和分行符都替换为空格并压缩到一行上显示,这一整行将作为一个字符串传入到目标命令中**。
43 |
44 | 以下两个命令实际上是等价的:
45 | ```bash
46 | # ls ./ | xargs echo
47 | file1 file2 file3
48 | # echo 'file1 file2 file3'
49 | file1 file2 file3
50 | ```
51 |
52 | 明白了`xargs`命令的工作原理,那么其实它所支持的options也很好理解,实际上就是如何将stdin传来的结果转化成不同的字符串,如处理分隔符的问题、转化成多个字符串分批执行的问题。
53 |
--------------------------------------------------------------------------------