-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSysAdmin.txt
More file actions
1351 lines (1005 loc) · 41.4 KB
/
SysAdmin.txt
File metadata and controls
1351 lines (1005 loc) · 41.4 KB
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
@@@@@@@@@@@@@@@@@@@@@ ALL THINGS SYSADMIN/DEVOPS RELATED @@@@@@@@@@@@@@@@@@@@@@@
top - Process Activity
vmstat - System Activity, H/W info
w - Who logged on
ps - Displays processes
pgrep
free - Memory Usage
iostat - Average CPU Load, Disk Activity
sar - System Activity Reporter
mpstat - Multiprocessor Usage
pmap - Process Memory Usage
netstat - Networks stats
ss - Utility to investigate sockets, similar to netstat
iptraf - Realtime network traffic
tcpdump -
strace - System Calls
/proc - Various Kernel Stats
nagios - Server and Network Monitoring
FAN - Fully Automated Nagios
nmap - Port Scanning
lsof - list open files, network connections and more.
ntop
dig - Swiss Army Knife for DNS querying.
pidstat
mpstat
sar
dstat
ngrep
mitmproxy
opensnoop
eBPF
perf
cpuid - Dump detailed info about CPUs
@@@@@@@@@@@@@@@ PMAP PROCESS MEMORY @@@@@@@@@@@@@@@@@@
$ pmap PID
@@@@@@@@@@@@@@@ CRONTAB/CRONJOB @@@@@@@@@@@@@@@@@@
Format of cron job:
min(0-59) hour(0-24) day_of_month(1-31) month(1-12) day_of_week(0-6) cmd_to_run
0 = Sunday
6 = Saturday
7 = Sunday again
- Asterisk means every/all instances.
- Manpages
$ man crontab
- Display contents of crontab file
$ crontab -l
- Edit current users cron jobs:
$ crontab -e
- To remove all cron jobs
$ crontab -r
Examples:
---------
- Run cron job every minute
* * * * * cmd_to_run
- Run cron job every 5th minute
*/5 * * * * cmd_to_run
- Every quarter hour
*/15 * * * * cmd_to_run
- Every half hour
30 * * * * cmd_to_run
or
*/30 * * * * cmd_to_run
- Run every hour, 5 mins and 10 mins past every hour
0,5,10 * * * * cmd_to_run
- Run every hour
0 * * * * cmd_to_run
- Run every 2 hours
0 */2 * * * cmd_to_run
- Run a job everyday (at 00:00)
0 0 * * * cmd_to_run
- Run a job everyday at 3am
0 3 * * * cmd_to_run
- Run a job every Sunday (at 00:00)
0 0 * * SUN cmd_to_run
or
0 0 * * 0 cmd_to_run
- Run a job every weekday only
0 0 * * 1-5 cmd_to_run
- Run a job every month (1st day)
0 0 1 * * cmd_to_run
- Run a job at 16:15 on 1st day of month
15 16 1 * * cmd_to_run
- Run a job every quarter i.e., 1st day of every 3rd month
0 0 1 */3 * cmd_to_run
- Run a job on specific month at a specific time
5 0 * 4 * cmd_to_run
- Run a job every 6 months
0 0 1 */6 * cmd_to_run
- Run a job every year
0 0 1 1 * cmd_to_run
Following strings can also be used to define job
We can also use the following strings to define job.
------
@reboot Run once, at startup.
@yearly Run once a year.
@annually (same as @yearly).
@monthly Run once a month.
@weekly Run once a week.
@daily Run once a day.
@midnight (same as @daily).
@hourly Run once an hour.
- Run a job everytime server is rebooted
@reboot cmd_to_run
Crontab Sytax Generators
----
https://crontab.guru/
https://crontab-generator.org/
[1] https://www.ostechnix.com/a-beginners-guide-to-cron-jobs/
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+ How to check linux flavor/version
Method 1:
$ cat /etc/*-release
Method 2:
$ lsb_release -a # displays LSB (Linux Standard Base) and
# distribution specific info.
Method 3:
$ cat /proc/version
---------------------------
+ How to find out kernel version
$ uname -a
$ uname -mrs
---------------------------
@@@@@@@@@@@@@@@@@ SYSTEM PERFORMANCE CHECK @@@@@@@@@@@@@@
// Notes from Brendan Gregg's videos
----- First 60 seconds investigation ------
uptime
dmesg | tail
vmstat 1
mpstat -P ALL 1
pidstat 1
iostat -xz 1
free -m
sar -n DEV 1 // network throughput
sar -n TCP,ETCP 1
top
-------- Detailed -------------
- Performance is slow. What to do?
- 4 types of tools
* Observability Tools: No impact on system.
+ Basic: vmstat, iostat, mpstat, ps, top ...
+ Intermediate: tcpdump, netstat, nicstat, pidstat, sar, ...
+ Advanced: ss, slaptop, perf_events, ...
* Benchmarking Tools: Affects the system.
* Tuning:
* Static performance tuning:
Basic Tools >>>>>>>>
- uptime
Get a quick overview on load averages.
- top (or htop or atop)
%CPU is summed across all CPUs
Can miss short-lived processes. 'atop' wont.
- ps
has lots of flags and custom fields can be set.
- vmstat
'r' is runnable tasks
Gives high level CPU summary
- iostat (ATT Unix system)
block I/O (disk) stats.
- mpstat ($ mpstat -P ALL 1)
multi processor stats
look for unbalanced (hot) CPUs, which means one thread is hogging CPU.
- free -m
'buffers': block device I/O cache
'cached': virtual page cache
Intermediate Tools >>>>>>>
- strace (system call tracer)
$ strace -tttT -p 313
* Translates syscall args. Very helpful for solving system usage issues.
strace is based on ptrace. Has massive overhead.
Can slow target by > 100x. Use extreme caution.
- tcpdump
sniff packets for post analysis
- netstat. Note that `netstat` belongs to `net-tools` package in linux which is
deprecated in favor or `ip` command suite. Use `nstat` which belongs to
`iproute2` package.
-s Various protocol stats
-i Interface stats
-r route table
-p process details
-c per-second interval
- netstat statistics
# All connections on port 80
$ netstat -anp | grep :80
# Help
$ netstat -h
netstat -s Display statistics
netstat -st Display TCP statistics
netstat -su Display UDP statistics
- netstat listening
netstat -ltunp All Listening ports
netstat -ltn Listening TCP ports
netstat -lun Listening UDP ports
netstat -lx Listening Unix ports
- netstat connections
netstat -a All connections
netstat -at All TCP connections
netstat -au All UDP connections
- netstat networks
netstat -i Show network interfaces
netstat -ie Show network interfaces extended info
- netstat routing
netstat -r Show routing table
netstat -rn Show routing table, don't resolve hosts
- `nstat` cheat sheet. `nstat` is tool for monitoring kernel SNMP and network
interface stats and part of iproute2 package.
$ nstat
# kernel
IpInReceives 12057 0.0
IpInAddrErrors 1 0.0
IpInDelivers 12000 0.0
IpOutRequests 11060 0.0
IpOutNoRoutes 252 0.0
TcpActiveOpens 36 0.0
TcpEstabResets 3 0.0
TcpInSegs 11804 0.0
TcpOutSegs 10960 0.0
TcpRetransSegs 3 0.0
TcpOutRsts 29 0.0
UdpInDatagrams 196 0.0
UdpOutDatagrams 246 0.0
Ip6InReceives 18 0.0
Ip6OutRequests 31 0.0
Ip6OutNoRoutes 248 0.0
Ip6InMcastPkts 18 0.0
Ip6OutMcastPkts 31 0.0
Ip6InOctets 1715 0.0
Ip6OutOctets 2791 0.0
Ip6InMcastOctets 1715 0.0
Ip6OutMcastOctets 2791 0.0
Ip6InNoECTPkts 18 0.0
Icmp6OutMsgs 13 0.0
Icmp6OutRouterSolicits 3 0.0
Icmp6OutNeighborSolicits 2 0.0
Icmp6OutMLDv2Reports 8 0.0
Icmp6OutType133 3 0.0
Icmp6OutType135 2 0.0
Icmp6OutType143 8 0.0
Udp6OutDatagrams 18 0.0
TcpExtTW 19 0.0
TcpExtDelayedACKs 37 0.0
TcpExtTCPHPHits 10981 0.0
TcpExtTCPPureAcks 142 0.0
TcpExtTCPHPAcks 267 0.0
TcpExtTCPLossUndo 3 0.0
TcpExtTCPTimeouts 3 0.0
TcpExtTCPAbortOnClose 3 0.0
TcpExtTCPRcvCoalesce 327 0.0
TcpExtTCPAutoCorking 5 0.0
TcpExtTCPSynRetrans 3 0.0
TcpExtTCPOrigDataSent 390 0.0
TcpExtTCPHystartTrainDetect 1 0.0
TcpExtTCPHystartTrainCwnd 17 0.0
TcpExtTCPWinProbe 1 0.0
TcpExtTCPDelivered 424 0.0
IpExtInMcastPkts 41 0.0
IpExtOutMcastPkts 47 0.0
IpExtInOctets 54131913 0.0
IpExtOutOctets 829632 0.0
IpExtInMcastOctets 3902 0.0
IpExtOutMcastOctets 4142 0.0
IpExtInNoECTPkts 48401 0.0
- This resource has details about the fields
- https://www.kernel.org/doc/html/latest/networking/snmp_counter.html
- https://loicpefferkorn.net/2018/09/linux-network-statistics-reference/
- nstat options
-a, --ignore
Dump absolute values of counters. The default calculates the increments
since the previous run.
-z, --zeroes
Dump all zero counters. They are ignored by default.
-r, --reset
Resets the history, thus excluding averages since the last run.
-d, --scan x
Run in daemon mode to collect statistics. x is an interval between
measurements in seconds.
-t, --interval x
Where x is the interval to average rates. Default value is 60s.
- pidstat
Very useful process stats, eg, by-thread, disk I/O
- swapon -s
Show swap device usage.
- lsof
- sar (System Activity Reporter)
Archive or Live Mode
Advanced Tools >>>>>>>
- ss
Info about socket stats
- iptraf
Nice histogram about network pkt size per interface
- iotop
- slabtop
Kernel slab allocator memory usage
- pcstat (available on Github)
- perf_events
multi tool with many capabilities
CPU profiling
PMC profiling (Performance Monitoring Counters) - very low level
static and dynamic tracing
- tiptop
front end to PMC
- rdmsr
* Model Specific Registers (MSRs), unlike PMCs, can be read by default in Xen
guests: timestamp clock, temp, power ...
* Use rdmsr from the msr-tools package to read them.
More Advanced Tools >>>>>>>
ltrace Library call tracer
ethtool Mostly interface tuning; some stats
snmpget SNMP network host statistics
lldptool Can get LLDP broadcast stats
blktrace Block I/O event tracer
/proc Many raw kernel counters
pmu-tools On- and off-core CPU counter tools
Benchmarking >>>>>>>
- lmbench
* CPU, memory and kernel micro-benchmarks
- fio
* File System or disk I/O micro-benchmarking
- pchar
* traceroute with bandwidth per hop
- ethtool -S eth0
root@webserver1-2:~# ethtool -S eth0
NIC statistics:
rx_queue_0_packets: 1383000
rx_queue_0_bytes: 88404885
rx_queue_0_drops: 0
rx_queue_0_xdp_packets: 0
rx_queue_0_xdp_tx: 0
rx_queue_0_xdp_redirects: 0
rx_queue_0_xdp_drops: 0
rx_queue_0_kicks: 22
tx_queue_0_packets: 6000
tx_queue_0_bytes: 423757
tx_queue_0_xdp_tx: 0
tx_queue_0_xdp_tx_drops: 0
tx_queue_0_kicks: 5719
root@webserver1-2:~# ip -s link show ens4
3: ens4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether fa:16:3e:be:77:d2 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped missed mcast
88408725 1383060 0 0 0 0
TX: bytes packets errors dropped carrier collsns
423757 6000 0 0 0 0
altname enp0s4
Tuning Tools >>>>>>>
- Read Slides [4]
Profiling Tools >>>>>
- Read Slides [4]
[1] https://www.youtube.com/watch?v=ZdVpKx6Wmc8
[2] https://www.youtube.com/watch?v=FJW8nGV4jxY
[3]
[4]
http://cdn.oreillystatic.com/en/assets/1/event/122/Linux%20perf%20tools%20Presentation.pdf
[5] https://github.com/brendangregg/msr-cloud-tools
@@@@@@@@@@@@@@@ rsync commands @@@@@@@@@@@@@@@@@@@@@@@
-v: verbose
-r: recursive copy of data. Doesn't keep permission or timestamp
-a: archive.
Features of this flag:
* Preserves timestamp, file permissions, user & group ownership
* Copies files recursively
* Copies symlinks
+ -a flag is superset of -r flag, looks like
-z: compress data
-h: Output numbers in human readable format
rsync [options] <src> <dst>
Copy/Sync a File on a Local Computer
$ rsync -zvh backup.tar /tmp/backups/
Copy/Sync a Directory on Local Computer
$ rsync -avzh /root/rpmpkgs /tmp/backups/
Copy a Directory from Local Server to a Remote Server
$ rsync -avz rpmpkgs/ root@192.168.0.101:/home/
Copy/Sync a Remote Directory to a Local Machine
$ rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms
Copy a File from a Remote Server to a Local Server with SSH
$ rsync -avzhe ssh --progress root@192.168.0.100:/root/install.log /tmp/
Excluding Dirs
---
To exclude directories, use --exclude=<relative-path> flag. Note that the
directory is relative to the source
$ rsync -a --exclude=/some/location/ /path/to/source /path/to/target
Excluded dir is here: /path/to/source/some/location/
Reference:
[1] https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/
[2]
https://unix.stackexchange.com/questions/2161/rsync-filter-copying-one-pattern-only/2503#2503
[3]
@@@@@@@@@@@@@@@@@@@@@ TAR commands @@@@@@@@@@@@@@@@@@@@@@@
+ 'tar' command examples [2]
// Note that '-' is a mess and may or may not be used.
1) Create tar Archive File
$ tar -cvf example.tar /home/example/
c - Create a new .tar archive file.
v - Verbose
f - File name is provided (example.tar)
2) Create tar.gz Archive File
$ tar -cvzf example.tar.gz /home/example/
OR
$ tar -cvzf example.tgz /home/example/
3) Create a tar.bz2 Archive File
$ tar -cvfj example.tar.bz2 /home/example/
OR
$ tar -cvfj example.tar.tbz /home/example/
OR
$ tar -cvfj example.tar.tb2 /home/example/
4) Untar tar (or tar.gz or tar.bz2) Archive File
// NOTE: Untar in current directory
$ tar -xvf example.tar
OR
$ tar -xvf example.tar -C /destination/directory/
$ tar -xvf example.tar.gz
$ tar -xvf example.tar.bz2
5) List Content of tar (or tar.gz or tar.bz2) Archive File
$ tar -tvf example.tar
$ tar -tvf example.tar.gz
$ tar -tvf example.tar.bz2
6) Untar one or more files from tar Archive File
$ tar -xvf example.tar 1.txt [2.txt ...] //1.txt is part of the archive.
OR
$ tar --extract --file=example.tar 1.txt
7) Untar one or more files from tar.gz
$ tar -zxvf example.tar.gz 1.txt
OR
$ tar --extract --file=example.tar.gz 1.txt
8) Untar one or more files from tar.bz2
$ tar -jcvf example.tar.bz2 1.txt
OR
$ tar --extract --file=example.tar.bz2 1.txt
9) Extract group of files using Wildcard
$ tar -xvf example.tar --wildcards '*.c'
$ tar -zxvf example.tar.gz --wildcards '*.c'
$ tar -jxvf example.tar.bz2 --wildcards '*.c'
10) Add files or directories to tar Archive
$ tar -rvf example.tar 1.txt
OR
$ tar -rvf example.tar php //php is a directory here
// tar command CANNOT add files/directories to .tar.gz and tar.bz2
// compressed files.
---------------------------
@@@@@@@@@@@@@@@@@@@@@ FIND commands @@@@@@@@@@@@@@@@@@@@@@@
Find Command Usage [3]
Part I – Basic Find Commands for Finding Files with Names
1. Find Files Using Name in Current Directory
Find all the files whose name is tecmint.txt in a current working
directory.
$ find . -name tecmint.txt
2. Find Files Under Home Directory
Find all the files under /home directory with name tecmint.txt.
$ find /home -name tecmint.txt
3. Find Files Using Name and Ignoring Case
Find all the files whose name is tecmint.txt and contains both capital and
small letters in /home directory.
$ find /home -iname tecmint.txt
4. Find Directories Using Name
Find all directories whose name is Tecmint in / directory.
$ find / -type d -name Tecmint
5. Find PHP Files Using Name
Find all php files whose name is tecmint.php in a current working
directory.
$ find . -type f -name tecmint.php
6. Find all PHP Files in Directory
Find all php files in a directory.
$ find . -type f -name "*.php"
Part II – Find Files Based on their Permissions
7. Find Files With 777 Permissions
Find all the files whose permissions are 777.
$ find . -type f -perm 0777 -print
8. Find Files Without 777 Permissions
Find all the files without permission 777.
$ find / -type f ! -perm 777
9. Find SGID Files with 644 Permissions
Find all the SGID bit files whose permissions set to 644.
$ find / -perm 2644
10. Find Sticky Bit Files with 551 Permissions
Find all the Sticky Bit set files whose permission are 551.
$ find / -perm 1551
11. Find SUID Files
Find all SUID set files.
$ find / -perm /u=s
12. Find SGID Files
Find all SGID set files.
$ find / -perm /g+s
13. Find Read Only Files
Find all Read Only files.
$ find / -perm /u=r
14. Find Executable Files
Find all Executable files.
$ find / -perm /a=x
15. Find Files with 777 Permissions and Chmod to 644
Find all 777 permission files and use chmod command to set permissions to
644.
$ find / -type f -perm 0777 -print -exec chmod 644 {} \;
16. Find Directories with 777 Permissions and Chmod to 755
Find all 777 permission directories and use chmod command to set
permissions to 755.
$ find / -type d -perm 777 -print -exec chmod 755 {} \;
17. Find and remove single File
To find a single file called tecmint.txt and remove it.
$ find . -type f -name "tecmint.txt" -exec rm -f {} \;
18. Find and remove Multiple File
To find and remove multiple files such as .mp3 or .txt, then use.
$ find . -type f -name "*.txt" -exec rm -f {} \;
OR
$ find . -type f -name "*.mp3" -exec rm -f {} \;
19. Find all Empty Files
To file all empty files under certain path.
$ find /tmp -type f -empty
20. Find all Empty Directories
To file all empty directories under certain path.
$ find /tmp -type d -empty
21. File all Hidden Files
To find all hidden files, use below command.
$ find /tmp -type f -name ".*"
Part III – Search Files Based On Owners and Groups
22. Find Single File Based on User
To find all or single file called tecmint.txt under / root directory of
owner root.
$ find / -user root -name tecmint.txt
23. Find all Files Based on User
To find all files that belongs to user Tecmint under /home directory.
$ find /home -user tecmint
24. Find all Files Based on Group
To find all files that belongs to group Developer under /home directory.
$ find /home -group developer
25. Find Particular Files of User
To find all .txt files of user Tecmint under /home directory.
$ find /home -user tecmint -iname "*.txt"
Part IV – Find Files and Directories Based on Date and Time
26. Find Last 50 Days Modified Files
To find all the files which are modified 50 days back.
$ find / -mtime 50
27. Find Last 50 Days Accessed Files
To find all the files which are accessed 50 days back.
$ find / -atime 50
28. Find Last 50-100 Days Modified Files
To find all the files which are modified more than 50 days back and less
than 100 days.
$ find / -mtime +50 –mtime -100
29. Find Changed Files in Last 1 Hour
To find all the files which are changed in last 1 hour.
$ find / -cmin -60
30. Find Modified Files in Last 1 Hour
To find all the files which are modified in last 1 hour.
$ find / -mmin -60
31. Find Accessed Files in Last 1 Hour
To find all the files which are accessed in last 1 hour.
$ find / -amin -60
Part V – Find Files and Directories Based on Size
32. Find 50MB Files
To find all 50MB files, use.
$ find / -size 50M
33. Find Size between 50MB – 100MB
To find all the files which are greater than 50MB and less than 100MB.
$ find / -size +50M -size -100M
34. Find and Delete 100MB Files
To find all 100MB files and delete them using one single command.
$ find / -size +100M -exec rm -rf {} \;
35. Find Specific Files and Delete
Find all .mp3 files with more than 10MB and delete them using one single
command.
$ find / -type f -name *.mp3 -size +10M -exec rm {} \;
Part VI - Exclude directories during search [4]
@@@@@@@@@@@@@@@@@@@@@ CUT commands @@@@@@@@@@@@@@@@@@@@@@@
$ cut -c1 data.txt // Select (or cut) 1st character/column of chars
$ cut -c1-4 data.txt // Select 1 to 4 chars
$ cut -c-6 data.txt // Select first 6 chars
$ cut -c4- data.txt // Select from 4th char to end
$ cut -f 2 data.txt // Select 2nd field (TAB is default delim)
$ cut -f 1 -d ':' data.txt // Delim is ':' now
$ cut -f 1,4 data.txt // Select 1 and 4th field
$ cut -f 1-4 data.txt // 1 to 4 fields
$ cut -f 2- data.txt // 2nd to end fields
$ cut -f 1,4-6,8 data.txt // Get 1, 4 to 6 and 8th fields
Now it's intuitive. Playing around with chars (c) and field (f) flags
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
LINUX NETWORKING COMMANDS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ipcalc
------
$ ipcalc 192.168.0.1/255.255.128.0
Address: 192.168.0.1 11000000.10101000.0 0000000.00000001
Netmask: 255.255.128.0 = 17 11111111.11111111.1 0000000.00000000
Wildcard: 0.0.127.255 00000000.00000000.0 1111111.11111111
=>
Network: 192.168.0.0/17 11000000.10101000.0 0000000.00000000
HostMin: 192.168.0.1 11000000.10101000.0 0000000.00000001
HostMax: 192.168.127.254 11000000.10101000.0 1111111.11111110
Broadcast: 192.168.127.255 11000000.10101000.0 1111111.11111111
Hosts/Net: 32766 Class C, Private Internet
iproute2 commands [5]
--------
show all addresses
$ ip address show
show address for a single intf
$ ip address show ${interface name}
show running intf
$ ip address show up
show statically configured address
$ ip address show [dev ${interface}] permanent
show dynamically configured address
$ ip address show [dev ${interface}] dynamic
Add an address to an interface
$ ip address add ${address}/${mask} dev ${intf name}
Ex:
ip address add 192.0.2.10/27 dev eth0
ip address add 2001:db8:1::/48 dev tun10
Add an address with human-readable description
$ ip address add ${address}/${mask} dev ${interface name} label ${interface name}:${description}
Ex:
$ ip address add 192.0.2.1/24 dev eth0 label eth0:my_wan_address
Delete an address
$ ip address delete ${address}/${prefix} dev ${interface name}
Remove all addresses from an interface
$ ip address flush dev ${interface name}
View all routes
$ ip route
$ ip route show
View routes to a network and all its subnets
$ ip route show to root ${address}/${mask}
$ ip route show to root 192.168.0.0/24
View routes to a network and all its supernets
$ ip route show to match ${address}/${mask}
$ ip route show to match 192.168.0.0/24
View routes to exact subnet
$ ip route show to exact ${address}/${mask}
View route actually used by kernel
$ ip route get ${address}/${mask}
View route cache
$ ip route show cached
Add a route via gateway
$ ip route add ${address}/${mask} via ${next hop}
$ ip route add 192.0.2.128/25 via 192.0.2.1
Add a route via interface
$ ip route add ${address}/${mask} dev ${interface name}
Change/Replace/Delete a route
$ ip route change ${rest of route statement}
$ ip route replace ${rest of route statement}
$ ip route delete ${rest of route statement}
Default route
$ ip route add default via ${address}/${mask}
$ ip route add default dev ${interface name}
Blackhole routes
$ ip route add blackhole ${address}/${mask}
Special routes
$ ip route add unreachable ${address}/${mask}
$ ip route add prohibit ${address}/${mask}
$ ip route add throw ${address}/${mask}
Routes with different metric
$ ip route add ${address}/${mask} via ${gateway} metric ${number}
$ ip route add 192.168.2.0/24 via 10.0.1.1 metric 5
$ ip route add 192.168.2.0 dev ppp0 metric 10
Multipath routing
$ ip route add ${addresss}/${mask} nexthop via ${gateway 1} weight ${number}
nexthop via ${gateway 2} weight ${number}
$ ip route add default nexthop via 192.168.1.1 weight 1 nexthop dev ppp0 weight 10
Show info about all links
$ ip link show
$ ip link list
Show info about specific link
$ ip link show dev ${intf name}
Bring up/down a link
$ ip link set dev ${intf name} up
$ ip link set dev ${intf name} down
Set human-readable link description
$ ip link set dev ${intf name} alias "${description}"
$ ip link set dev eth0 alias "LAN interface"
Rename an interface
$ ip link set dev ${old name} name ${new name}
Change link layer address (usually MAC)
$ ip link set dev ${intf name} address ${address}
Change link MTU
$ ip link set dev ${intf name} mtu ${MTU value}
Delete link
$ ip link delete dev ${intf name}
Enable or disable multicast on an interface
$ ip link set ${interface name} multicast on
$ ip link set ${interface name} multicast off
Enable or disable ARP on an interface
$ ip link set ${interface name} arp on
$ ip link set ${interface name} arp off
Create a VLAN intf
$ ip link add name ${VLAN interface name} link ${parent interface name} type vlan id ${tag}
$ ip link add name eth0.110 link eth0 type vlan id 110
QinQ
$ ip link add name ${service interface} link ${physical interface} type vlan proto 802.1ad id ${service tag}
$ ip link add name ${client interface} link ${service interface} type vlan proto 802.1q id ${client tag}
Ex:
$ ip link add name eth0.100 link eth0 type vlan proto 802.1ad id 100 # Create service tag interface
$ ip link add name eth0.100.200 link eth0.100 type vlan proto 802.1q id 200 # Create client tag interface
Create pseudo-ethernet (aka macvlan) interface
$ ip link add name ${macvlan interface name} link ${parent interface} type macvlan
$ ip link add name peth0 link eth0 type macvlan
Create dummy intf
$ ip link add name ${dummy intf name} type dummy
Create a bridge intf
$ ip link add name ${bridge name} type bridge
Add an intf to bridge
$ ip link set dev ${intf name} master ${bridge name}
Remove intf from bridge
$ ip link set dev ${intf name} nomaster
Create a bonding intf
$ ip link add name ${name} type bond
// read documentation as bonding is extensive topic
Create an Intermediate Functional Block interface (IFB)
$ ip link add ${intf name} type ifb
// RTFM for more. It's extensive topic.
Network Namespace Management
---------
Create a namespace
$ ip netns add ${namespace name}
List existing namespaces
$ ip netns list
Delete namespace
$ ip netns delete ${namespace name}
Run a process inside a namespace
$ ip netns exec ${namespace name} ${command}
$ ip netns exec foo /bin/sh
List all processes assigned to a namespace
$ ip netns pids ${namespace name}
Identify process' primary namespace
$ ip netns identify ${pid}
Assign network intf to a namespace
$ ip link set dev ${intf name} netns ${namespace name}
Connect one namespace to another
# look up the reference link
Monitor network namespace subsystem events
$ ip netns monitor
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PING/PING6
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/* pinging a loopback addr. The -I is essential to ping loop-back intf */
$ ping -c 5 -I eth0 <ipv6-loop-back-addr>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
strace Examples/Notes
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// Trace certain syscalls
$ strace -e trace=socket, sendmsg ./a.out
// Trace a running process
$ strace -p [PID]
// Trace child processes
$ strace -f ./a.out