-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinuxforDevOps.txt
More file actions
1347 lines (793 loc) · 26.1 KB
/
LinuxforDevOps.txt
File metadata and controls
1347 lines (793 loc) · 26.1 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
======================================
Linux OS + Shell Scripting
======================================
=> Operating System is a platform which is used to communicate with computers
=> Operating System acts as mediator between users and computers
=> Operating System is mandatory to use a computer
=> There is no use of computer which doesn't have Operating System
=> Using Operating System we can run applications in Computer
Ex: Notepad, Calc, Paint, Browser, VLC Player
=> There are several Operating Systems available in the market
1) Windows OS
2) Linux OS
3) Mac OS
============
Windows OS
============
-> Developed by Microsoft Company
-> It is GUI Based (Graphical User interface)
-> Easy to use
-> Highly recommended for personal use
Ex: Watching Movies, Playing Games, Browsing, Storing personal data ....
-> Security features are very less (we need to install Anti Virus softwares)
-> Windows is Licensed Operating System.
-> Windows is single user based OS
==========
Linux OS
==========
-> It is free and open source operating system
-> Multi User Based Operating System (multiple users can access at a time)
-> Anti Virus S/w not required for Linux Machines
-> Highly recommended for Server Environments
Ex: Database, Jenkins, SonarQube, Nexus, Docker, k8s.....
-> Linux is CLI based (Command Line Interface)
=================
History of Linux
=================
-> Developed by Linus Torvalds
-> Earlier "Linus Torvalds" used Unix OS and he faced some challenges with Unix OS and he reported to company to change Unix OS but Company rejected his suggetions.
-> Linus Torvalds started developing his own operating by using "Minux OS"
(Li) nus + Mi (nux) ===> Linux
-> After developing Linux OS "Linux Torvalds" released Linux OS into market for free of cost along with source code.
-> Several Companies downloaded Linux OS source code and modified according to their requirement and released into market with their own brand names. Those are called as Linux Distributions / Flavours .
1) Amazon Linux
2) Ubuntu Linux
3) CentOS Linux
4) Debian Linux
5) Fedora Linux
6) SUSE Linux
7) Kali Linux
8) RED HAT Linux etc..
1) What is Operating System
2) What is Windows OS
3) Linux OS
4) Linux History
5) Linux Distributions
==================================
Environment Setup to Learn Linux
=================================
1) Create Account in AWS Cloud & Login into that
2) Create Linux Virtual Machine using EC2 service
3) Connect with Linux VM using MobaXterm software (Windows Users)
4) Practice Linux Commands
===============
Linux Commands
===============
whoami : Displays logged in username
date : displays current date
cal : displays calendar
clear : Clear screen
pwd : Display present working directory
cd : change directory
mkdir : To create directory (folder)
rmdir : To remove empty directory
Note: To remove non-empty directory we will use below command
rm -r <dirname>
ls : List files & directories available in present working directory
ls -l : Long listing of the files & directories in alphabetical order
ls -lr : Long listing of the files & directories in reverse of alphabetical order
ls -lt : Display latest files on top
ls -ltr : Display old files on top
ls -la : To display hidden files
ls -li : To display files with inode numbers
mv : To rename/move files & directories
touch : To create empty files
rm : Remove file
cat : To create files with data, append data to file and display file content
cat > f1.txt : Create new file with data
cat f1.txt : Print file content from top to bottom
cat -n f1.txt : Print file content with line numbers
cat >> f1.txt : Append data to existing file
tac : To print file content from bottom to top
tac f1.txt
cp : Top copy data from one file to another file
cp f1.txt f2.txt
Note: How to copy more than one file data to another file
cat m1.txt m2.txt > m3.txt : Copy two files data to 3rd file
wc : word count (it will display no.oflines, no.of words and no.of characters in given file)
wc m3.txt
head : Display first 10 lines of the file
head devops.txt (print first 10 lines)
head -n 15 devops.txt (print first 15 lines)
head -n 30 devops.tx (print first 30 lines)
tail : Display last 10 lines of the file
tail devops.txt (print last 10 lines)
tail -n 15 devops.txt (print last 15 lines)
tail -n 30 devops.tx (print last 30 lines)
tail -n +5 devops.txt (print from 5th line to last line)
=======================
Text Editors in Linux
=======================
vi : visual editor
=> Using vi we can edit files in linux
$ vi linux.txt
Insert mode : To insert/modify data we should go to insert mode (press i)
Esc Mode : After modifications completed we should press Esc key
Save and Close : Press :wq to save the changes and close the file.
Close without Saving : press :q! for closing file without saving the changes
============
SED command
===========
=> SED stands for Stream Editor
=> SED command is used for replacement or substitution
=> We can replace one word with another word using SED command without opening file
$ sed 's/linux/unix/' linux.txt
Note: by default sed command will give output to terminal but it won't make changes to original file. To make changes to original file we need to use -i in sed command
$ sed -i 's/linux/unix/' linux.txt
# delete last line of the file
$ sed -i '$d' linux.txt
# Delete second line of the file
$ sed -i '2d' linux.txt
# Delete from second line to till last line
$ sed -i '2, $d' linux.txt
=============
grep command
=============
=> Grep stands for global regular expression print
=> It is used for file content search based on given pattern
# Pattern Matching print
$ grep "keyword" filename
# Pattern Matching by ignoring case sensitive
$ grep -i 'devops' mydata.txt
# Print matching pattern lines along with line numbers
$ grep -n 'DEVOPS' mydata.txt
# invert match (print lines which doesn't have given pattern)
$ grep -v 'DEVOPS' mydata.txt
================================
Working with Zip files in Linux
================================
Syntax to create To zip file : $ zip <zip-file-name> <files-to-add>
# Create zip file (all .txt files will be added to notes.zip file)
$ zip notes *.txt
# Print content of zip file
$ zip -sf notes.zip
# Add new file to existing zip
$ zip notes f1.txt
# Delete file from zip
$ zip -d notes f1.txt
# Unzip the files from zip file
$ unzip <zip-file-name>
# Create zip file with encryption (set password)
$ zip -e <filename> <files-to-add>
###### Note: man command is used to get documentation about linux commands #####
$ man vi
$ man ls
$ man zip
##########################
02 - Aug - 2023 (Wednesday)
###########################
===========================
locate and find commands
==========================
=> locate and find commands are used for file search
=> locate command will search for the files in locate database.
$ sudo yum install locate
$ locate 'apache'
=> find command is used for more advanced search in linux os
# search for the file under home directory
$ sudo find /home -name f1.txt
# find empty files under home directory
$ sudo find /home -type f -empty
# find empty directories under home directory
$ sudo find /home -type d -empty
========================
Users & Groups in Linux
========================
=> Linux is Multi User Based OS
=> We can create Multiple User accounts in Linux Machine
=> In Every Linux Machine by default one 'root' user account will be available
root user => super user / Administrator
Note: Root user will have all priviliges to perform any operation.
=> When we launch EC2 instance using "Amazon Linux AMI" we will get 'ec2-user' by default.
Note: For every user one home directory will be created in Linux machine under 'home' directory
ec2-user => /home/ec2-user
ashok => /home/ashok
raju => /home/raju
rani => /home/rani
=> To get user account information we can use 'id' command in linux machine
$ id <uname>
# switch to root user account
$ sudo su -
# logout from root user account
$ exit
====================================
Working with User Accounts in Linux
===================================
# swith to root user
$ sudo su -
# Create New User Account
$ useradd <uname>
# Set Password for user account
$ passwd <uname>
# Display users we have created
$ cat /etc/passwd
# Delete User (it won't delete user home directory)
$ userdel <uname>
# Delete user along with user home directory
$ userdel <uname> --remove
# switch to user account
$ su <uname>
=====================================
Working with User Groups in Linux
=====================================
# Display all user-groups available in Linux
$ cat /etc/group
Note: When we create user in linux machine, it will create user account + user-group
# Create New User-Group
$ groupadd <group-name>
# Add user to user-group
$ usermod -aG <group-name> <user-name>
# Remove user from user-group
$ gpasswd -d <user-name> <group-name>
# Display users belongs to user-group
$ sudo lid -g <group-name>
# Rename user-group
$ groupmod -g <new-name> <existing-name>
# Rename user
$ usermod -l <new-name> <existing-name>
# Delete User Group
$ groupdel <group-name>
################
03-Aug-2023(Thu)
################
==========================
File Permissions In Linux
==========================
=> In Linux VM everything is treated as file (Ex: normal files, directory files)
=> We can secure our files using File Permissions
=> We have 3 types of file permissions
1) READ (r)
2) WRITE (w)
3) EXECUTE (x)
=> File Permissions are divided into 3 sections
1) User Permissions (rwx)
2) OWNER Group Permissions(rwx)
3) OTHERS Permissions (rwx)
Ex: rwxrwxrwx
=> To change file permissions in linux we can use 'chmod' command
Note: "+" is used to add permission and " - " is used to remove permission
# Adding write permission for user on f1.txt file
$ chmod u+w f1.txt
# Adding execute permission for group
$ chmod g+x f1.txt
# Remove write permission for group
$ chmod g-w f1.txt
# Add write and execute permission for others
$ chmod o+wx f1.txt
# Remove all permissions for others
$ chmod o-rwx f1.txt
===================================
File Permissions in Numeric Format
===================================
0 : No Permission
1 : Execute
2 : Write
3 : Write + Execute => 2 + 1
4 : Read
5 : Read + Execute => 4 + 1
6 : Read + Write => 4 + 2
7 : Read + Write + Execute => 4 + 2 + 1
$ chmod 777 f1.xt
$ chmod 445 f1.txt
$ chmod 655 f1.txt
$ chmod 444 f1.txt
Q-1) What are default file permissions in Linux in numeric format ?
Q-2) What are default directory permissions in Linux in numeric format ?
============================
Working with chown command
===========================
=> chown command is used to change file owner and file group
# making ashok as owner for f1.txt file
$ sudo chown ashok f1.txt
# making dbteam as owner group for f1.txt file
$ sudo chown :dbteam f1.txt
# Changing owner and group at a time
$ sudo chown ashok:ashok f1.txt
Note: We can use userid and group id also in chown command instead of names.
#################
04-Aug-2023
###############
===========================
Hard Links and Soft Links
===========================
=> In Linux we can create Link files (similar to shortcuts in windows)
=> We have 2 types of links in Linux
1) Hard Link
2) Soft Link
---------------------------
Syntax To create Hard Link
--------------------------
$ ln <orginal-file> <link-file>
$ touch m1.txt
$ ln m1.txt m10.txt
$ cat > m1.txt
$ cat m10.txt
$ ls -li
Note-1: original file and link file will have same inode number
Note-2: When we write data to original file then it will reflect in link file also.
Note-3: When we delete original file link file is not effected (hard link)
---------------------------
Syntax To create soft Link
---------------------------
$ ln -s <original-file> <link-file>
$ touch f1.txt
$ ln -s f1.txt f10.txt
$ ls -li
$ cat > f1.txt
$ cat > f10.txt
$ rm f1.txt
$ ls -li
Note: When we remove original file then soft link file will become dangling. We can't access it.
=============================
Process Management in Linux
============================
=> Process represents running application
=> In Windows machine we can see running processes using "Task Manager"
=> In Linux machine we can see running process using ps command
$ ps
$ top
=> To kill process in linux machine we will use kill command
$ kill -9 <process-id>
=====================
Networking Commands
=====================
# To get ip address of machine
$ ifconfig
# To check connectivity
$ ping <ip>
$ ping www.google.com
$ ping www.facebook.com
# To download files using url
$ wget <url>
$ wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.78/bin/apache-tomcat-9.0.78.zip
# To send HTTP Request to URL
$ curl <url>
========================
Utility Commands
========================
# Display memory details
$ free
# Install htop command
$ sudo yum insall htop
# memory details with dynamic view
$ htop
# Display OS information
$ cat /etc/os-release
# Display Kernel Version
$ uname -r
# Check all Users Login history In Linux
$ lastlog
# Check Specific User login history
$ last <uname>
# Get last 3 login details
$ last -3
===========================
Package Managers in Linux
===========================
=> Package Managers are used to install softwares in Linux Machines
=> We have several package managers in linux
1) yum (Yellow Dog updater & modified)
Ex: Amazon Linux, RED HAT Linux, CentOS
2) apt (Advanced Packaged Tool)
Ex: Ubuntu
3) dnf (Dandified YUM package)
Ex: It is replacing yum in latest versions of centos
# Install Java in Amazon Linux
$ sudo yum install java
# Check Java version
$ java -version
# Install Maven in Amazon Linux
$ sudo yum install maven
# Check Maven version
$ mvn -v
# Install git client in Amazon Linux
$ sudo yum install git
# Check Git version
$ git -v
# check path of packages we installed
$ whereis java
$ whereis mvn
$ whereis git
=========================
What is a Sudoers File?
========================
=> A Sudoers file is just like any other file on a Linux Operating System.
=> It plays a vital role in managing what a “User” or “Users in a Group” can do on the system.
======================
Why the name Sudoers?
======================
=> The word Sudo is the abbreviation of the term “Super User Do”.
=> The users with Sudo permission are called as Sudo Users.
=> Management of all these sudo users is done through a file called as Sudoers File.
=> This file also lives in the /etc directory just like any other configuration file.
$ sudo cat /etc/sudoers
##### User Privilege Specification #####
root ALL = (ALL:ALL) ALL
Note: ‘#’ indicates comment in the file, OS will ignore this line while executing.
Syntax : User <space> OnHost = (Runas-User:Group) <space> Commands
Example: root ALL = (ALL:ALL) ALL
Read it as — User Root can Run Any Command as Any User from Any Group on Any Host.
→ The first ALL is used to define HOSTS. We can define Hostname/Ip-Address instead of ALL. ALL means any host.
→ Second ALL : Third ALL is User:Group. Instead of ALL we can define User or User with the group like User:Group. ALL:ALL means All users and All groups.
→ Last ALL is the Command. Instead of ALL, we can define a command or set of commands. ALL means all commands.
#### To put it very simple, it is “who where = (as_whom) what”. ####
=> Tnother example to clearly understand the fields in the syntax:
Example : ashok ALL = (root) /usr/bin/cat /etc/shadow
Read this as — User “ashok” can Run the command “/usr/bin/cat /etc/shadow” as ROOT user on all the HOSTS.
## Edit sudoers file
$ sudo visudo
## close sudeors file
$ ctrl + x
=================================
What is .bashrc file in linux ?
=================================
=> It is used to configure environment variables in Linux Machines
Ex: Java Path, Maven Path etc....
=> For every user account one .bashrc file will be available under user home directory
=> .bashrc file is hidden file. To see this we need to execute 'ls -la'
=========================
What is .ssh in linux ?
==========================
=> This is hidden directory which contains authorized_keys (pem file public key)
=> We can get our authorized_key using below command
$ cat .ssh/authorized_keys
1) What is Operating System
2) Windows OS
3) Linux OS
4) Linux Distributions (200+)
5) Linux Virtual Machine Setup in AWS
6) Connect with Linux VM using MobaXterm
7) Connect with Linux VM using Putty
8) Linux commands
9) Working with Directories
10) Working with Files
11) Text Editors (vi & sed)
12) Pattern Matching (grep)
13) head & tail
15) User Management
16) Group Management
17) File Permissions (chmod)
18) Ownership (chown)
19) find and locate
20) Working with zip files
21) Network commands
22) Package Managers
23) Sudoers file
24) .bashrc file
25) .ssh Authorized keys
====================
Linux Architecture
====================
1) Applications / Commands
2) Shell : It is mediator between application/users and kernel. It will verify command syntax.
3) Kernel : It is mediator between Shell and Linux Hardware components
4) Hardware
=================
Shell Scripting
=================
=> Scripting means writing set of commands in a file and executing them.
=> Scripting is used to automate our daily routine tasks in the project.
Ex: delete temp files, take files backup, archive log files, monitor servers etc..
=> Shell script file will have .sh extension
=> We can execute shell script file like below
$ sh <file-name>
=========
Script-1
========
whoami
pwd
date
ls -l
touch f2.txt
ls -l
=========
Script-2
========
echo "Welcome to Ashok IT";
echo "DevOps Course is Running"
=========
Script-3
========
echo "Enter Your Name";
read name
echo "Good Evening $name"
=========
Script-4
========
echo "Enter first number"
read a
echo "Enter second number"
read b
let sum=$(($a+$b))
echo "Sum is $sum"
========
1) Variables : To store the data
2) Command Line Arguments : To supply input for script file
3) Control Statements : To control execution of script file
3.1) if - elif
3.2) for loop
3.3) while loop
4) functions : To divide large task into smaller tasks
===========
Variables
===========
=> Variables are used to store the data
=> Variables are key-value pairs
a=10
b=20
name=ashok
=> Variables are divided into 2 types
1) Environment Variables / System Variables
$ echo $USER
$ echo $SHELL
2) User Defined Variables
a=10
b=20
name=ashok
===================
Variable Rules
===================
1) We should not use special symbools like -, @, # etc in variable name
2) Variable name shouldn't start with digit
Note: It is recommended to take variable name as UPPERCASE character in scripting.
=======================
Command Line Arguments
=======================
=> The arguments which we will pass to script file at the time of execution
ex:
$ sh script5.sh ashok it
=> We can access command line arguments in script file like below
$# => No.of Args supplied
$0 => Script file name
$1 => First Cmd Argument
$2 => Second CMD Argument
$3 => Third cmd argument
$* => All Cmd arguments
============
Script-6
============
echo "No.of Args Supplied : $#"
echo "First Arg : $1"
echo "Second Arg: $2"
echo "All Args : $*"
##### Execution :: $ sh script6.sh ashok it
==========
Script-7
=========
echo "First Arg : $1"
echo "Second Arg: $2"
let result=$(($1*$2))
echo "Multiply Result : $result"
##### Execution :: $ sh script7.sh 2 4
====================
Control Statements
====================
=> Script will execute from top to bottom in forward direction (this is default behaviour)
=> If we want to control script execution flow then we have to use "Control Statements"
=======================
Conditional statements