-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux_101.txt
1595 lines (1395 loc) · 69.7 KB
/
linux_101.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Apropos:
# LINUX ADMINISTRATION RECIPES
## External Links: [[{]]
* <https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/>
* <http://www.linuxfromscratch.org/>
* <https://linux.die.net/man/>
* <https://linux.die.net/Linux-CLI/>
* <https://en.wikipedia.org/wiki/Linux_Standard_Base>
* <https://docs.fedoraproject.org/en-US/fedora/f34/system-administrators-guide/>
* <https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/>
[[}]]
# Linux Basics [[{linux.101]]
Linux itself is just an OS kernel in charge of sharing the LIMITED hardware
resources amongst potentially many running tasks and users working simultaneously
on the systems. More preciselly, the main tasks in charge of kernel control are:
* Orchestate(schedule) how much time each running-task is allowed to
run on each CPU before such task is put on stand-by to let
another task proffit from such CPU.
* Assign which process has access to the (limited)
RAM memory on the system and move data on RAM used by stand-by processes
to secondory storage (disks) in case of RAM space shortage.
* Provide support for users and permissions, so that different users will
be able to isolate and protect its data from other users.
Kernel control is transparent to running tasks or processes, so user-space task
will run with the perception that they run alone in its own CPU and with
all available memory for themself. When the kernel puts them on-hold
such tasks will be freezed and once restarted it will NOT notice any
change to the state before being put on-hold. Only the kernel is aware of
the complex trickeries needed to make tasks run in parallel and isolated
from each other.<br>
Other important tasks offered by the kernel are:
* Abstract the running hardware into standarized interfaces, so
user application will not need to work differently with different hardware.
For example an application will ask the kernel to write data to the disk and
the kernel will take care of the internal difference between the miriads
of diferent disk hardware technologies.
* Provide an easy-to-use file-system to work with data on disk, so that apps
can orginized data in terms of files and directories, instead of just
bunch-of-bytes on the hard-disk.
* Provide network communication and support for standard network protocols
like TCP/UP, bluetooth, WiFI, ... so that each app does not need to reimplement
them.
* Provide mechanisms to allow two running tasks to communicate with each other
at will.
## Kernel mode vs User mode
* When the CPU is executing kernel code it's running with elevated privileges.
The software has full control of the hardware and can do "anything" on
the system and access all RAM, disk, network resources at will.
* Standard applications will run in user-mode and will have limited access
to only the RAM memory assigned by the kernel. They will not be able to
inspect the memory of other processes running on the system. In fact they
are not aware of such memory due to the many trickeries done by the kernel
to isolate each task.
## Files, files and more files
Any running process needs some incomming data to work with and
produced new data that must be stored somewhere.
This data can be provided by some storage system (hard-disk,
usb, tape, ...), arrive (continuosly) from the network,
or be generated by another concurrent process.
Linux (UNIX actually) treats all input data sources and
output data sinks as *"file devices"*.
Internally there can be many differences (block devices with
random access vs char devices with just sequential access), but
running processes mostly always use the file methaphor to access
all of them.
Any running process will have 3 devices available "for free":
* STDIN : The standard input file.
* STDOUT: The standard output file
* STDERR: The standard error file
The standard shell provides many utilities to juggle with those
three standard files. In particular it allows to easely forward
the STDOUT output from a running-process to the STDIN input of
another running process using the "|" pipe syntax:
```
| $ command1 | command2 #
| ^
| └─ Send STDOUT output from command1 to
| STDIN input of command2
```
STDOUT and STDERR by default are assigned to the running-process
associated terminal (the console where the user has been logged).
The shell allows also to redirect STDOUT/STDERR to any other
file in our file system. Ex:
```
| $ command1 1>output.log 2>error.log
│ └─────┬────┘ └─────┬───┘
| redirects STDOUT(1) redirects STDERR
| to output.log to error.log
|
| $ command1 1>output.log 2>&1
| └─────┬────┘ └─┬┘
| redirects STDOUT(1) redirects STDERR
| to output.log to STDOUT (&1, aka output.log)
```
[[}]]
# Process model [[{linux.101.process_model,job_control.101,]]
Linux follows a parent-child process model.
Once loaded and initialized during the boot process,
the kernel will launch an initial user-space process in
charge of reading system configuration and (re)start
all other user-space processes that builds a running system.
Normally this initial process is systemd in modern
systems (or initd in older or embedded ones).
Each process can optionally launch new children processes
up to the resource limits established on the running system.
By default a child-process inherits the same user (and so, permissions)
than its parent process. Some processes like the remote
login "sshd" service (ssh is an acronymn for secure-shell) will
change the child-process user/permission to a less privileged
account.
A simplified process-tree of a running-system will look like: [[{doc_has.diagram]]
```
| PROCESS USER PROCESS PARENT-ID
| UNIQUE-ID
| systemd·······································root 1 0
| └─crond·································root 23 1
| |-cupsd·································root 70 1
| |-rtkit-daemon··························rtkit 100 1
| |-sshd··································root 10 1
| | └─sshd·····························mike 300 10
| | └─bash························mike 301 300
| | └─firefox················mike 302 301
| |-systemd·······························alice 705 1
| | └─at-spi-bus-laun···············alice 706 705
| | |···············└─dbus-daemon···alice 707 706
| | |-gnome-terminal················alice 883 705
| | ─bash-+·········alice 884 883
| | └─top·····alice 885 884
| |-systemd-journal·······················root 10 1
| ...
| Notice for example that the same process "bash" runs as a user or another
| ( *mike* or *alice*) depending on the "path"
| followed until the process is executed. [[}]]
```
* The initial sshd running as root user, will span a new sshd child process
with restricted *"mike"* privileges/permissions once the user
has introduced its correct user and password in the remote ssh session, and
from there on, all children will just be able to run with *"mike"*
privileges/permissions.
* Similarly the root systemd process will span a new child process will
restricted *"alice"* privileges/permissions once logged in
the local console, and from there on, all children will just be able to
run with *"alice"* privileges/permissions.
## Executable file vs in-memory process [[{linux.101]]
Applications are stored on disk drives as files or
"bunch-of-instructions and initial data".
When the kernel executes and application it will read the executable file, load
the "bunch-of-instructions" into RAM memory, setup the initial data, assign
restricted privileges and finally allow the program-in-memory to be executed by
any free-available CPU on the system.
[[}]]
## Basic file permissions [[{linux.101,security.aaa]]
Standar file permissions allows to assign different access permissions to
the owner of the file, the group owner of the file and anyone else.
```
| $ ls -l myFileOfInterest.db
|
| -rw?-r-?---? john accountDepartment .... myFileOfInterest.db
| └┼┘│└┼┘│└┼┘│ └┬─┘ └──────┬────────┘
| │ │ │ │ │ │ │ │
| │ │ │ │ │ │ │ └─ group owner
| │ │ │ │ │ │ └──────────── user owner
| │ │ │ │ │ │
| │ │ │ │ │ └──────────────── sticky bit (hidden if not set)
| │ │ │ │ │
| │ │ │ │ └────────────────── permissions allowed to others: read access
| │ │ │ │
| │ │ │ └──────────────────── SUID bit (hidden if not set)
| │ │ │
| │ │ └────────────────────── permissions allowed to group : read access
| │ │
| │ └──────────────────────── SUID bit (hidden if not set)
| │
| └────────────────────────── permissions allowed to user : read&write access
```
The previous command line can be read as:
> Allow read and write permissions to file-owner "john",
> read permissions to group-owner "accountDepartment"
> and no permissions to anyone-else """
```
| ┌──────┬─────────────────────────────┬─────────────────────────────┐
|Permissions │Symbol│ FILE │ DIRECTORY │
|┌───────────┼──────┼─────────────────────────────┼─────────────────────────────┤
|│ read│ r │ Allows to read the content │ Allows to list the files in │
|│ │ │ of the file │ and file-attributes in the │
|│ │ │ │ directory │
|├───────────┼──────┼─────────────────────────────┼─────────────────────────────┤
|│ write│ w │ Allows to write, modify, │ Allows to add and delete │
|│ │ │ append or delete the file │ files into the directory and│
|│ │ │ content. │ modify metadata (access │
|│ │ │ │ or modification time, ...) │
|├───────────┼──────┼─────────────────────────────┼─────────────────────────────┤
|│ execute│ x │ Allows to execute the │ Allows to enter into the │
|│ │ │ program or script │ directory │
|├───────────┼──────┼─────────────────────────────┴─────────────────────────────┤
|│ sticky │ T │ Only the person that created the file/dir. can change it, │
|│ │ │ even if other people have write permissions to file/dir. │
|│ │ │ turn on: $ chmod +t someFileOrDir │
|│ │ │ Normal /tmp (temporary user files) is an sticky directory │
|├───────────┼──────┼───────────────────────────────────────────────────────────┤
|│ suid│ S │ Allow SUID/SGID (switch user/group ID). When executed it │
|│ │ │ it will be executed with creator/group of file, instead of│
|│ │ │ current user. │
|│ │ │ turn on: $ chmod +s someFileOrDir │
|└───────────┴──────┴───────────────────────────────────────────────────────────┘
```
[[}]]
## User Management [[{security.aaa.101,linux.101,]]
|```
| $ useradd -D ← display (D)efault values to apply when creating new uses.
| (Default group / HOME, SHELL, ...)
| Use next flags to update defaults:
| --base-dir BASE_DIR : (default to /home) Ignored if --home-dir set
| --expiredate EXPIRE_DATE
| --inactive INACTIVE : day # after pass.expiration before disabling
| --gid GROUP: existing group name or ID for initial group (when --no-user-group used)
| --shell SHELL
|
| $ addgroup PROJECT01 ← Best pattern when allowing multi-user access to the linux machine.
| Create a new group per project ,then assign permissions to group (vs
| to user). This greately simplifies permissions administration in
| multi-user setups.
|
| $ useradd alice \ ← Create user alice.
| --shell=/usr/bin/git-shell \ ← Assign shell (restricted git-shell in this example)
| --shell=/usr/bin/git-shell \ ← upon succesful login alice will be presented with a git shell *1
| --groups PROJECT01,PROJECT02\ ← Assign PROJECT01 as default group for alice.
| --password ... \ ← create an initial and strong password (or provide alice with ssh keys.
| --no-user-group \ ← Do NOT create a new group (otherwise indicated by --gid)
| --no-create-home \ ← Do not create the /home/alice directory. (Some sort of
| /var/lib/git/PROJECT01 directories already exist with read/write permissions
| for PROJECT01, ... groups)
|
| *1 By default /bin/bash o similar is used. git-shell restricts
| access to just git-related commands.
| Other useful 'useradd' options are:
| --skel SKEL_DIR : skel. dir. to be copied in the user's home directory
| --key KEY=VALUE : Overrides /etc/login.defs defaults
| (UID_MIN, UID_MAX, UMASK, PASS_MAX_DAYS and others).
| Example: -K PASS_MAX_DAYS=-1 can be used when creating
| system account to turn off password ageing, even though
| system account has no password at all.
| --no-log-init : Do not add user to lastlog and faillog databases
| --non-unique : Allow duplicate (non-unique) existing UID in --uid. Most of the times used to
| allow 'root' users with non-root name accounts.
| --system : Create system account (no aging, uid chosen in SYS_UID_MIN-SYS_UID_MAX range)
| --root CHROOTDIR: Apply changes in chrooted directory.
| --uid UID : numerical value for user's ID.
| --user-group : Create group with the same name as user, and use as initial group
| --selinux-user SEUSER : SELinux user for the user's login
```
## su/sudo: Switch user: [[{security.aaa,linux.101,PM.WiP]]
* su and sudo are mostly used to allow temporal root/superuser access
to standard users for administrative tasks like installing new applications,
re-configuring the network, ...
* sudo is ussually considered safer than su. Ubuntu was the first
distribution to allow sudo-only. Others distributions are also
changing to sudo-only as time passes.
* sudo offers also a plugable architecture not offered by su
to provide different authentication and audit mechanisms.
REF:
* https://www.sudo.ws/ (Sudo Home page)
* https://www.sudo.ws/plugins.html (sudo Third-party plugins)
https://linux.die.net/man/1/su
https://linux.die.net/man/8/sudo
* Ex. ussage:
```
| $ sudo vim /etc/passwd # edit /etc/passwd as root
| $ su # Change to root user
```
[[}]]
[[security.aaa.101}]]
[[{101,job_control.task_scheduling,PM.TODO]]
## Job/Process control: Scheduling Tasks
Officially in Linux/UNIX/Posix, a Job or task is a running process.
* cron : program task to be run periodically
* at
* anacron :
Unlike cron(8), it does not assume that the machine is running continuously.
Hence, it can be used on machines that aren't running 24 hours a day.
Anacron checks whether this job has been executed in the last n days.
If not, Anacron runs the job's shell command, after waiting for the
number of minutes specified as the delay parameter.
NOTE/WARN: Only the date , not hour is used.
e.g.: On Debian-based systems, anacron will be activated hourly every day
from 07:30 local time to 23:30 local time through cron job
[[}]]
[[{101.text_utils,use_case.*,PM.low_code]]
# Text Search and processing
## Text view
* Displaying text:
```
$ head -n 20 /path/to/textFile # shows first 20 lines (-n 20). 10 lines by default if -n not provided.
$ tail -n 20 /path/to/textFile # shows last 20 lines (-n 20). 10 lines by default if -n not provided.
$ tail -f /path/to/textFile # shows stream ("f"lush) of lines as they are appended to the file
$ less /path/to/textFile # Views text. Add scroll control backwards and forwards.
# embedded systems use "more", that just scroll forwards.
$ cat /path/to/textFile # dump text content to standard output (STDOUT)
$ cat file1 file2 file3 .... # concatenates files'content and dumps into STDOUT
$ tac file1 file2 file3 .... # concatenates files'content and dumps into STDOUT in reverse order
```
```
| man 1 column:
|$ column -n 140 /usr/share/dict/words # ← One token per line input
|→ A archdeacon effort's mads salient's
|→ A's archdeacon's effortless madwoman salients
|→ ...
|→ archbishop effigy madras salesperson's étude's
|→ archbishop's effigy's madras's salespersons études
|→ archbishopric effluent madrases saleswoman
|
|$ COLUMNS=100 column -t -s, input.csv # ← -t(able):
|1 2 3 -s(eparator): Indicates column sep
|a b c (white space by default)
|x y z
```
[[}]]
[[{]]
## Extract text info/statistics/difference
```
| $ wc /path/to/textFile ← Display total count of words, lines and bytes
| Options:
| -w count only words
| -l count only lines
| -w count only bytes
|
| $ diff file1 file2 ← Compares two text files and output a difference report indicating:
| '> line_in_file2_not_in_file1
| '< line_in_file1_not_in_file2
| $ sdiff ← Similar to diff but with report in colum mode (more human readable)
| $ diff3 ← diff for three files
```
[[}]]
## SORTING FILE CONTENT !!!! [[{]]
```
| $ sort file1 <-·· Sorting alphabetically lines in file
| (-r to reverse, -g for numerical sort)
|
| $ sort file1 \
| -t ':' <-·· Use ':' as separator,
| -k 4 -k 1 sort first by column 4, then column 1.
|
| $ uniq file1 <··· Eliminates duplicate entries from a file
| Commonly used with sort like:
| $ cat file.txt | sort | uniq
| Options: -c: display number of occurances of each duplicate
| -u: list only unique entries
| -d: list only duplicate entries
|
| $ join file1 file2 <-·· Join two lines together assuming they share
| at least one common value on the relevant line,
| skiping lines withouth common value.
```
[[}]]
## Text edit [[{]]
* By default we can use the following tools like:
```
| $ tool inputFile # Apply to given file
| or
| $ command1 ... | tool1 | tool2 | tool3 # Pipe execution output (stdout) as input
| for next command.
|
| CUT CONTENT BY COLUMN:
| $ cut -d "," -f 1,3,7 file1.csv #<· Use "," as column delimiter,
| # show then columns 1,3,7
| $ cut -c 1-50 file.txt #<· show characters 1 to 50 in each line
| $ cut -5, 8, 20- file.txt #<· show characters 1 to 5 and 8 and
| from 20 to the end
|
| $ tr "u" "d" file1 # <· replace characters in file
| $ cat some_file | \
| tr '[A-Z]' '[a-z]' > new_file # <· Ex: Convert all capital letters to lowercase
|
| $ nl file1.txt # <· Display file1.txt to STDOUT prefixing with line numbers
|
| $ sed "s/ *up/ down/g" file1.txt <- Replaces " *up*" by " *down*".
| /g flag: replace all ocurrences (vs only first match).
| NOTE: sed stands for (s)tream (ed)itor. It has lot of powerful flags
| for instance, searching for regular expresions ("complex matches).
```
[[}]]
[[101.text_utils}]]
# Monitoring
## basic process monit/control [[{job_control.101,linux.101,monitoring.jobs,monitoring.i/o]]
* shows list of the processes running:
```
| $ ps -a # Without options: display processes belonging to current
| # user and with a controlling terminal
| # -a : flag to all processes from all users
| # Other common "flags" include:
| # -u: add user names, %cpu usage, and %mem usage,...
| # -x: add also processes without controlling terminals
| # -l: add information including UID and nice value
| # --forest: show process hierarchy.
|
| $ pstree # <·· show parent/children process tree (-p flag show pid)
|
| $ top -n # <·· Display "top processes" (by cpu ussage) and finish
|
| $ top # <·· real-time display processes ordered by memory/CPU/...(as in CPU usage)
|
| Z,B,E,e Global: 'Z' colors; 'B' bold; 'E'/'e' summary/task memory scale
| l,t,m Toggle Summary: 'l' load avg; 't' task/cpu stats; 'm' memory info
| 0,1,2,3,I Toggle: '0' zeros; '1/2/3' cpus or numa node views; 'I' Irix mode
| f,F,X Fields: 'f'/'F' add/remove/order/sort; 'X' increase fixed-width
|
| L,&,<,> . Locate: 'L'/'&' find/again; Move sort column: '<'/'>' left/right
| R,H,V,J . Toggle: 'R' Sort; 'H' Threads; 'V' Forest view; 'J' Num justify
| c,i,S,j . Toggle: 'c' Cmd name/line; 'i' Idle; 'S' Time; 'j' Str justify
| x,y . Toggle highlights: 'x' sort field; 'y' running tasks
| z,b . Toggle: 'z' color/mono; 'b' bold/reverse (only if 'x' or 'y')
| u,U,o,O . Filter by: 'u'/'U' effective/any user; 'o'/'O' other criteria
| n,#,^O . Set: 'n'/'#' max tasks displayed; Show: Ctrl+'O' other filter(s)
| C,... . Toggle scroll coordinates msg for: up,down,left,right,home,end
|
| k,r Manipulate tasks: 'k' kill; 'r' renice
| d or s Set update interval
| W,Y Write configuration file 'W'; Inspect other output 'Y'
| q Quit
|
| $ iotop # ← Simple top-like I/O monitor
| <https://linux.die.net/man/1/iotop>
```
```
$ kill - # <·· Display existing signals (Default to SIGTERM that most of the times
# will just terminate the process "cleanely")
> 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
> 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
> 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
> 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
> 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
> 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
> 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
> 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
> 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
> 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
> 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
> 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
> 63) SIGRTMAX-1 64) SIGRTMAX
$ kill [ -s (signal name)] 'process_id' # <·· Send signal to process.
# kill -9 kills unconditionally
$ killall "process_name" # <·· send signal to all processes matching full name
$ pkill "process_name" # <·· send signal to all processes matching part of the name
$ skill # <·· send a particular signal to command/username/tty
-L --- list the various signals that can be sent
-u --- specify a username;
-p --- process id (followed by the process id)
-c --- command name (this is the same as killall)
-t --- (tty number)
-v --- verbose mode
-i --- interactive mode.
```
* Kill example. PAUSE AND CONTINUE A PROCESS.<br/>
When paused, the process does not consume any resource.
```
$ kill -STOP "pid" # Pauses
$ kill -CONT "pid" # Continues
```
* Change process execution priority
```
| $ nice -20 make # <·· Sets make priority to -20
| # -20 is maximum priority (negative only allowed to root)
| # 20 is the minimum priority.
| $ renice 10 $PID # <·· Changes priority of process identified by <$PID>.
| # Use `ps -a` or `top -n` or ... to fetch the PID value
```
[[}]]
## Disk Space [[{troubleshooting.disk_full]]
```
| $ df -kh # <·· (D)isk (F)ree report.
| # -k : take block-size=1K
| # -h : human-readable, print sizes in powers of 1024 (e.g., 1023M)
|
| $ du -sch dir1 dir2 # <·· (D)isk (U)ssage report for files&directories.
| # -s: summarize. Display only a total for each argument
| # -c: produce grand total
| # -h : human-readable
```
[[}]]
## `pv` Pipe viewer [[{monitoring.pipes,monitoring.i/o,monitoring.101,profiling.storage.FS,storage.profiling]]
* <http://ivarch.com/programs/pv.shtml>
* terminal-based tool for monitoring the progress of data through a pipeline.
("visual hint" about data "travelling" through processes / pipes / network)
* pv provides time elasped, %compl., progressbar, throughput, ETA, ...on STDERR.
Example ussage:
```
| $ sudo apt install pv
|
| $ cat /dev/zero | pv > /dev/null # Check CPU/memory performance [[monitoring.cpu]]
| 2,01GiB 0:00:15 [ 5.38GiB/s] <=>
| $ cat /dev/urandom | pv > /dev/null # Check urandom performance
| 2,01GiB 0:00:15 [ 430MiB/s] <=>
| $ pv file | nc -w 1 somewhere.com 3000 # Check network perf. [[monitoring.network]]
| $ pv -EE /dev/sda > disk-image.img # Check disk image (skip errors)[[monitoring.storage.blocks]]
```
[[monitoring.pipes.pv}]]
* man pv summary
```
| OUTPUT MODIFIERS
| --wait: until first byte has been transferred.
| --delay-start SEC (before showing info)
| --size SIZE: Set total size (if piping from STDIN and size is known)
| --line-mode: (count lines vs counting bytes)
| --null for null terminated.
| --interval SEC: (1 sec by default)
| --width N / --height N: Assume width / height for terminal
| --name NAME: Useful with --cursor for "compilcated pipelines"
| --cursor: Use cursor pos. escape sequences (vs CR)
| --force: (even if STDOUT is not terminal)
| DATA TRANSFER MODIFIERS
| --rate-limit RATE
| --buffer-size BYTES
| --no-splice: Never use splice(2). Ussually more efficient way of
| transferring data from/to pipe than regular read/write.
| but means that the transfer buffer may not be used.
| This prevents -A and -T from working.
| -E/--skip-errors (set twice to only report a read error once per file)
| --stop-at-size
| --watchfd PID[:FD]. Wath FIle Descriptor of process PID, and show its
| progress.
| --remote PID : where PID is an running instance of pv already running.
| Add other commands to such instance.
| --pidfile FILE save PID of (first running) pv instance.
```
[[}]]
[[{PM.TODO]]
## Netdata (Glances++)
* <https://github.com/netdata/netdata>
* (Used by AWS, ..)
* Visual tool to monitor the data "moving around" in our system.
[[PM.TODO}]]
## Glances [[{monitoring.101,PM.low_code,QA.UX,troubleshooting]]
* <https://www.tecmint.com/glances-an-advanced-real-time-system-monitoring-tool-for-linux/>
* <https://github.com/tldr-pages/tldr/blob/master/pages/common/glances.md>
* Alternative to the `top` common
* Linux/MacOS/FreeBSD python command-line, curses-based, using psutils
under the hood.
* It makes easier to find an application/process consuming lots
of system resources by highlighting programs consuming too much
resources and providing maximum of information about the server.
* Allows to define thresholds (careful, warning and critical) in
config files.
* Display info about:
* CPU (user, kernel, idle processes).
* RAM, Swap, Free memory,... etc.
* Average CPU load for past 1min, 5mins and 15 mins.
* Network Download/Upload rates of network connections.
* Total number of processes, active ones, sleeping processes etc.
* Disk I/O related (read or write) speed details
* Currently mounted devices disk usages.
* Top processes CPU/Memory usages, Names and file path of exec
* official packages for Debian/RedHat/...
### USSAGE:
```
| $ glances
| GREEN : OK (everything is fine)
| BLUE : CAREFUL (need attention)
| VIOLET: WARNING (alert)
| RED : CRITICAL (critical)
|
| Shortcuts:
| a – Sort processes automatically
| c – Sort processes by CPU%
| m – Sort processes by MEM%
| p – Sort processes by name
| i – Sort processes by I/O rate
|
| d – Show/hide disk I/O stats ols
| f – Show/hide file system statshddtemp
| n – Show/hide network stats
| s – Show/hide sensors stats
| y – Show/hide hddtemp stats
| l – Show/hide logs
| b – Bytes or bits for network I/Oools
| w – Delete warning logs
| x – Delete warning and critical logs
| x – Delete warning and critical logs
| 1 – Global CPU or per-CPU stats
| h – Show/hide this help screen
| t – View network I/O as combination
| u – View cumulative network I/O
| q – Quit (Esc and Ctrl-C also work)
|
| default thresholds: /etc/glances/glances.conf*
| (careful=50, warning=70 and critical=90)
```
* Client/Server mode:
```
| ON THE SERVER │ ON THE CLIENT
| ────────────────────────────────────┼────────────────────────────────
| │
| # glances -s -B $ADDRESS -p $PORT │ # glances -c -P 172.16.27.56 *
| (0.0.0.0) (61209) │
| Define password for server │
| ...
| Glances server is running on ...
```
[[}]]
[[{MONITORING.I/O,MONITORING.STORAGE,PM.TODO]]
## iostat(CPU, I/O, FS)
* <https://linux.die.net/man/1/iostat>
```
| $ iostat -xt 2 # -x Show extended statistics
| # -t Print time
| avg-cpu: %user %nice %system %iowait %steal %idle
| 4.27 0.00 4.27 2.26 0.00 89.20
|
| Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
| sda 0.00 5.50 0.00 36.00 0.00 5.00 0.00 47.62 0.00 17.55 0.09 0.00 6.55 0.82 0.45
| sdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
| dm-0 0.00 3.00 0.00 12.00 0.00 0.00 0.00 0.00 0.00 123.00 0.37 0.00 4.00 0.50 0.15
| dm-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
| dm-2 0.00 7.50 0.00 30.00 0.00 0.00 0.00 0.00 0.00 0.47 0.00 0.00 4.00 0.40 0.30
| ^^^^ ^^^^ ^^^^ ^^^^^ ^^^^ ^^^^ ^^^^ ^^^^ ^^^^ ^^^^ ^^^^ ^^^^ ^^^^ ^^^^ ^^^^
| read r_await r_await aqu-sz ignore elapsed
| req. avg msec avg msec avg to be time %
| for read for read queue remove during
| requests requests length which
| of req I/O req
| issued were
| issued
| BANDWIDTH
| USSAGE
```
[[}]]
## powertop [[{monitoring.hardware]]
Allows to:
* diagnose device/CPU power consumption issues.
* Tune/control device/CPU power management.
```
| $ sudo powertop # ← Interactive mode if no other option is provided
| $ sudo powertop --auto-tune # ← Callibrate non-interactively
| ^^^^^^^^^^^^^^^^^^^^
| To enable at system boot add next systemd Unit:
| *STEP 1: Create/Edit powertop.service like:*
| $ sudoedit /etc/systemd/system/powertop.service
| (Add next lines)
| + [Unit]
| + Description=Powertop auto-tune
| +
| + [Service]
| + ExecStart=/usr/bin/powertop --auto-tune
| + RemainAfterExit=true
| +
| + [Install]
| + WantedBy=multi-user.target
|
| *STEP 2: Enable the new service like:*
| $ sudo systemctl daemon-reload
| $ sudo systemctl enable powertop
| $ sudo systemctl start powertop
|
| *STEP 3: Check it has run properly*
| $ sudo journalctl -u powertop
| → ...
| → systemd[1]: Started Powertop auto-tune.
| → powertop[4778]: modprobe cpufreq_stats failedLoaded 0 prior measurements
| → powertop[4778]: RAPL device for cpu 0
| → powertop[4778]: RAPL Using PowerCap Sysfs : Domain Mask d
| → powertop[4778]: RAPL device for cpu 0
| → powertop[4778]: RAPL Using PowerCap Sysfs : Domain Mask d
| → powertop[4778]: Devfreq not enabled
| → powertop[4778]: glob returned GLOB_ABORTED
| → powertop[4778]: *Leaving PowerTOP*
|
| OTHER PERTINENT OPTIONS:
| --calibrate : Runs in calibration mode: When running on battery,
| powertop can track power consumption as well as system
| activity.
| When there are enough measurements, powertop can start
| to report power estimates.
| -csv=file : Generate CSV report.
| -html=file : Generate an HTML report.
| -extech=$USBDEV: Use Extech Power Analyzer for analysis
| USBDEV will be a USB adaptor similar to /dev/ttyUSB0
| -iteration=$Num : Number of times to run each test.
| -time[=seconds] : Generate report for specified number of seconds.
| -workload=file : Execute workload file as a part of calibration
| ....
```
[[}]]
[[linux.101.process_model}]]
# Network [[{network]]
[[{configuration.network,troubleshooting.network]]
## NetworkManager
* <https://www.redhat.com/sysadmin/becoming-friends-networkmanager>
* <https://linux.die.net/man/8/networkmanager>
* <https://linux.die.net/man/5/networkmanager.conf>
* <https://linux.die.net/man/1/nmcli>
* <https://linux.die.net/man/8/networkmanager_selinux>
* widespread network configuration daemon
* Managed through cli (nmcli), text-GUI (nmtui) GUI (GNOME,...)
files, web-console (Cockpit) or D-Bus interface.
APIs and a library (libnm) is also provided.
-*NetworkManager allows users and applications to retrieve *
*and modify the network's configuration at the same time, *
*ensuring a consistent and up-to-date view of the network.*
* NetworkManager philosophy:
"...attempts to make networking configuration and operation as
painless and automatic as possible..."
When there is partial or no configuration, NetworkManager checks
the available devices and tries its best to provide connectivity
to the host.
* NetworkManager allows advanced network administrators to
provide their own configuration.
- *NetworkManager Entities*:
```
| - device : represents a network interface ("ip link")
| A NetworkManager devices tracks:
| - If it is managed by NetworkManager
| - The available connections for the device
| - The connection active on the device (if any)
| - connection : represents the full configuration to
| be applied on a device and is just a list of
| properties.
| Properties belonging to the same configuration
| area are grouped into settings:
| Example:
| - ipv4 setting group:
| - addresses
| - gateway
| - routes
|
| ^^^^^^^^^^^^^
| *NETWORK SETUP == activate a connection with a device*
```
```
| $ nmcli device # ← list the devices detected by NetworkManager
| (output will be similar to)
|> DEVICE TYPE STATE CONNECTION
|> enp1s0 ethernet connected ether-enp1s0
|> enp7s0 ethernet disconnected --
|> lo loopback unmanaged --
```
```
| $ nmcli device \ # ← turn off management
| set enp1s0 managed no for enp1s0 device
| (change is not persisted
| and ignored on reboot)
```
```
| $ nmcli # List detailed connections for devices
| enp1s0: connected to enp1s0
| "Red Hat Virtio"
| ethernet (virtio_net), 52:54:00:XX:XX:XX, hw, mtu 1500
| ip4 default
| inet4 192.168.122.225/24
| route4 0.0.0.0/0
| route4 192.168.122.0/24
| inet6 fe80::4923:6a4f:da44:6a1c/64
| route6 fe80::/64
| route6 ff00::/8
| ...
```
```
$ nmcli connection # ← list the available connections
→ NAME UUID TYPE DEVICE
→ ether-enp1s0 23e0d89e-... ethernet enp1s0
→ ...
```
* To deconfigure the associated device, just instruct NetworkManager
to put the connection down. For instance, to deactivate the
ether-enp1s0 connection:
```
| $ nmcli connection \ # ← deactivate connection
| down ether-enp1s0 (deconfigure associated
| device)
|
| $ nmcli connection \ # ← Reactivate
| up ether-enp1s0
|
| $ nmcli connection \ # ← Show connection details
| show ether-enp1s0
| connection.id: *ether-enp1s0* ← human readable name
| connection.uuid: 23e0d89e-...
| connection.stable-id: --
| connection.type: 802-3-ethernet ← ethernet, wifi, bond!!, vpn, ...
| connection.interface-name: enp1s0 ← binds(restrict) to specific device
| connection.autoconnect: yes
| ...
| ipv4.method auto ← one of:
| auto(DHCP)
| manual(static IP in ipv4.addresses),
| disabled, link local, shared
| ipv4.addresses 192.168.1.201/24
| ipv4....
| dhcp4.option[1] broadcast_address = 192.168.1.255
| dhcp4....
| [...]
| (See `man nm-settigs` for full info about available parameters)
```
```
| $ nmcli connection \ ← Permanently change the connection
| modify *ether-enp1s0* \
| ipv4.method manual
| ipv4.addresses 10.10.10.1/24 \
| ipv4.gateway 10.10.10.254 \
| ipv4.dns 10.10.10.254
| $ nmcli connection up ether-enp1s0 ← New settings will only be applied
| on connection (re)activation
|
| $ nmcli connection \ # ← avoid activation by NetworkManager
| modify ether-ens1s0 \ (you would have to activate manually)
| connection.autoconnect no
|
| $ nmcli con add \ ← Create new connection
| type ethernet \ - DHCP will be used if no IPv4 config
| ifname enp0s1 \ is provided.
| con-name *enp0s1_dhcp* \ (ipv4.method defaults to auto)
| autoconnect no - Run interactively with '--ask' option
| $ nmcli con ← Verify new connection
| → NAME UUID TYPE DEVICE
| → ...
| → *enp0s1_dhcp* 64b499cb-... ethernet --
| → ...
|
| $ nmcli con edit ← Interactive editor-mode with inline help will open
```
### nmcli TODO:
* "Many features deserve separate blog posts"
* dispatcher scripts
* connectivity checkers
* split DNS
* MAC address randomization
* hotspot configuration
* automatic configuration.
* Check Network status:
```
| <https://linux.die.net/man/1/nm-online>
| ~:$ nm-online -q ← --timeout=10 (Defaults to 30secs)
| ~:$ echo $? --exit Exit immediately if nm is not running or connecting
| → 0 --quiet Don't print anything
| --wait-for-startup Wait for nm startup instead of a connection
```
### Troubleshooting Network Manager
```
| bypass network manager by manually launching dhcp client:
|
| $ sudo dhclient eth0 # eth0 stands for "Ethernet 0". Name can differ
| execute 'ip link' to show all network devices
```
### ERROR: "Network disconnects after a few seconds"
* Try disabling ModemManager service:
```
| $ sudo systemctl stop ModemManager.service
| $ sudo systemctl disable ModemManager.service
```
[[configuration.network}]]
[[{linux.101,configuration.network,]]
## TCP/IP Utilities
```
| $ host (ip_address|domain_name) # <· Performs DNS lookup of an internet address
| (using the Domain Name System, DNS).
|
| $ dig www.amazon.com # <· Query through (configured) DNS server
| $ dig -x 10.10.10.10 # <· reverse query to DNS
| $ dig www.amazon.com @IP.DNS.1 # <· Query to a given DNS server (vs default one)
| (check man page for more options)
|
| $ wget http://www.mydomain.com/myPage # <· HTTP client
| Options:
| -m: archive/"m"irrow single web-site
| -nc: (no clobber) avoid overwriting local files
|
| $ wget --spider \
| --force-html \
| -i bookmarks.html # <· parse bookmarks.html for links
| (see man page for more info)
|
| $ curl # Script oriented HTTP client. (See curl dedicated page)
| $ curl -M # <· Access the full/huge manual
```
[[}]]
[[{monitoring.network.nethogs,monitoring.jobs,troubleshooting.network]]
## Nethogs: bandwidth "top" per ps
* Nethogs is a command line utility for linux that displays the network
bandwidth used by each application or process in realtime.
```
| $ sudo nethogs *
| ...
| PID USER PROGRAM DEV SENT RECEIVED
| 2367 enlighten/opt/google/chrome/chrome eth0 3.341 20.948 KB/sec
| 2196 enlighten/usr/lib/firefox-7.0.1/fire eth0 0.871 0.422 KB/sec
| 3723 enlighten/usr/bin/pidgin eth0 0.028 0.098 KB/sec
| 2206 enlighten/usr/bin/skype eth0 0.033 0.025 KB/sec
| 2380 enlighten/usr/lib/chromium-browser/c eth0 0.000 0.000 KB/sec
| 0 root unknown TCP 0.000 0.000 KB/sec