Linux Basics To Advance Indetailed With Pratical Execution
Linux Basics To Advance Indetailed With Pratical Execution
What is Infrastructure
=========================
Servers
Database
Storage
Security
Monitoring
Network
===========
AWS Cloud
==========
Started in 2006
190+ Countries
Global Infrastructure
33 Regions (Geographical locations)
=> We can create free tier acc in AWS for learning purpose
===========================
===========================
=> Users will communicate with computers using Operating System (OS)
Windows OS
============
Ex: Play Games, Watch Movies, Internet Browsing, Store data, Online Classes...
=======================
=======================
=================
Linux OS History
=================
=> Initially Linus Torvalds was using Unix OS and found some challenges in that and
informed to that company but they did not accept his suggestions.
=> Linus started doing research and he found Minux OS is similar to his ideas.
=> He has taken Minux OS and made few changes to that and released into market as
Linux OS.
================================
Linux Distributions / Flavours
================================
=> Many companies downloaded Linux OS code and modified according to their
requirement and released into market with different names. Those are called as Linux
Distributions.
Ex: Amazon Linux, Red Hat Linux, Ubuntu Linux, Cent OS Linux, SuSe linux....
==========
Summary
==========
2) Windows OS
3) Linux OS
4) Linux OS History
5) Linux Distributions
=====================
=====================
==================
==================
ls : list content
$ ls (display files in present working directory)
cd : change directory
$ rm *.txt
$ rm a*.txt
$ rm -rf <dir-name>
$ mv <present-name> <new-name>
$ mv <present-location> <new-location>
cat : To create file with data + append data to existing file + view file data
$ cat f1.txt
$ cat -n f1.txt
1 clear
2 ls
3 mkdir devops
4 ls
6 ls
8 rmdir aws
9 clear
10 ls
11 rmdir linux
12 ls
13 rmdir devops
14 ls
15 mkdir devops
16 ls
17 pwd
18 clear
19 pwd
20 cd ..
21 pwd
22 ls
23 cd ec2-user
24 cd ..
25 clear
26 ls
27 cd ec2-user
28 pwd
29 clear
30 ls
31 touch ashokit.txt
32 ls
34 ls
35 clear
36 ls
37 ls -l
38 ls
39 ls -l
40 clear
41 ls -l
43 ls
44 ls -l
45 clear
46 ls
47 ls -l
48 ls -lr
49 clear
50 pwd
51 ls
52 ls -l
53 ls -rl
54 clear
55 ls -lt
56 ls -ltr
57 clear
58 ls -l
59 ls -la
60 clear
61 ls
62 ls -l
63 ls -la
64 clear
65 pwd
66 ls -l
67 cd devops
68 pwd
70 ls -l
71 pwd
72 clear
73 ls -l
74 pwd
75 ls -l
76 rm git.txt
77 ls -l
78 clear
79 ls -l
80 rm jenkins.txt
81 ls -l
82 pwd
83 cd ..
84 pwd
85 ls -l
86 clear
87 ls -l
88 rm m1.txt
89 ls -l
90 ls -l devops
91 clear
92 ls -l devops
93 ls -l
94 ls -l devops
95 rmdir devops
96 clear
97 rm -rf devops
98 ls -l
99 clear
100 ls -l
102 ls -l
103 cd devops
105 ls -l
106 clear
107 ls -l
108 pwd
109 cd ..
110 pwd
111 l -l
112 ls -l
113 clear
114 ls -l
115 ls -l devops
117 rm devops
118 clear
119 rm -r devops
120 ls -l
121 clear
122 ls -l
123 rm *.txt
124 ls -l
125 ls -la
126 clear
127 ls -l
129 ls -l
131 ls -l
132 clear
133 ls -l
135 ls -l
136 ls -l data
137 clear
138 ls -l
139 ls -l data
141 ls -l
142 ls -l data
143 clear
144 ls -l
145 ls -l data
147 ls -l
149 ls -l
151 ls -l data
152 clear
153 ls -l
155 ls -l
156 clear
161 clear
162 ls -l
164 ls -l
169 clear
170 cat aws.txt
175 clear
181 clear
184 clear
185 history
================================================
$ cp f1.txt f2.txt
Note: To copy more than one file data into another file we will use cat command
$ tac f1.txt
$ rev f1.txt
$ head f1.txt
$ tail f1.txt
$ grep -n 'aws' f1.txt (print lines having aws with line number)
$ grep -v 'aws' f1.txt (pring lines which doesn't have aws keyword)
$ grep 'java' * (search for java keyword in all the files of pwd)
=======
tac
cp
cat
head
tail
grep
wc
diff
==========
=======================
=======================
=> Using 'vi' we can create new files and we can modify existing file data
2) insert mode (to edit the file ) ---> press 'i' in keyboard
3) esc mode (to comeout from insert mode) --> press 'esc' in keyboard
===================================
===================================
====================================
===================================
=============
SED command
=============
=> Using SED command we can perform operations on the file without opening the file.
===========================
===========================
=> Within one linux machine we can create multiple user accounts
=> Multiple users can acces single linux machine and can perform multi tasking
# create user
$ cat /etc/passwd
# switch user
$ sudo su <uname>
# Go to logged in user home directory
$ cd ~
# Delete user
===========================
===========================
=> When we create user in linux, for every user one user group also will be created with
the given username
$ cat /etc/group
$ id <username>
# delete group
=================================
=================================
=> Using this file we can control which user can run command as a superuser.
Note: We should be very careful while working with sudoers file. If we do any mistakes in
sudoers file then system will be crashed.
$ sudo visudo
=> After making changes to close sudoers file => ( CTRL + X + Y + Enter)
========================================================
========================================================
=> TO enable password based authentication we need to set the value as yes.
$ sudo vi /etc/ssh/sshd_config
============================================================
============================================================
$ sudo visudo
Step-5) Enabled PwdBasedAuthentication in 'sshd_config' file
$ sudo vi /etc/ssh/sshd_config
==========================
==========================
r => read
w => write
x => execute
====================================
====================================
0 => No permission
1 => Execute
2 => Write
4 => Read
# ugo+x
# ugo+w
$ chmod 7 f1.txt
Ans) 644
Ans) 755
==============
chown command
==============
# changing owner
# changing owner-group
============================================
========================
=======================
=> find and locate commands are used for file location search
=> find command will search for files in entire linux file system based on given path
$ locate apache
==================================
=================================
$ unzip ashokit.zip
=====================
Networking commands
=====================
$ ping www.google.com
$ ping 192.168.1.1
wget : It is used to download files from internet
$ wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.19/bin/apache-tomcat-
10.1.19.zip
$ curl https://type.fit/api/quotes
$ ifconfig
====================================================
====================================================
====================
=====================
=> The awk command is a versatile text processing tool available Linux.
=> It allows you to manipulate and extract data from structured text files, usually in a
columnar format.
=> awk takes input, processes it line by line, and performs actions based on specified
patterns and rules.
========================
========================
=> Inode is one unique number that will be assigned for every file in linux.
=> Linux will use inode number to map our files with its name in the linux db.
=> We can use below command to check inode number of the files
$ ls -li
==========================
===========================
=> In linux we can create link files ( similar to shorcut files in windows )
1) Hard Link
2) Soft Link
--------------------------------
--------------------------------
$ ln <orginal-file> <link-file>
Ex:
$ touch m1.txt
$ ln m1.txt m11.txt
$ ls -li
Note: m1.txt and m11.txt files are having same inode number
Note: If we write some data to m1.txt that data will reflect in m11.txt file also
------------------------------
------------------------------
$ ln -s <orginal-file> <soft-link-file>
Ex:
$ touch s1.txt
$ ln -s s1.txt s11.txt
$ ls -li
Note: Original file and soft link file having different inode numbers
Note: Data writing in original file will reflect in soft link file also
$ rm s1.txt
Note: When we remove original file then soft link file will become dangling file. We can't
access that file.
=========
Revision
=========
pwd
whoami
date
cal
cd
mkdir
rmdir
touch
ls -ltr
cat
tac
rev
mv
diff
cp
wc
man
head
tail
vi
sed
useradd
passwd
id
userdel
usermod
groupadd
groupdel
sudoers file
sshd_config file
chmod
chown
find
ping
wget
curl
ifconfig
zip
unzip
free
top
htop
awk
ln
Ans) $ uname -r
Ans) $ ps aux
Ans) $ netstat
==========================
==========================
=> Package Managers are used to install / update / manage software packages in linux
machines.
======================================
======================================
# install webserver
# start webserver
$ sudo vi index.html
=> To access our webserver we need to enable 80 port number in security group
inbound rules.
=============================
=============================
=> It is a part of the systemd system and service manager, which has become the
standard init system for many Linux distributions.
====================================================
systemctl list-unit-files --type=service: Lists all available services, both enabled and
disabled.
==============================================
==================================
==================================
# set hostname
# re-start session
$ exit
===================================
===================================
# update hostname in below file
$ sudo vi /etc/hostname
#restart the vm
=====================
Linux Architecture
=====================
2) Text Editors
3) Text filters
4) Users Management
5) File Permissions
6) File Ownership
7) Archieves
8) Networking
9) suderos file
10) sshd_config
====================
Linux Architecture
====================
1) hardware
2) kernel
3) shell
=> shell is a mediator between user and kernal. Shell will process our commands.
=> Kernal is a program which reads shell commands and gives to hardware
components.
=> When we execute any linux command, shell will read our command it will translate
our command into kernel understabale format.
=> kernal s/w will convert our command into linux machine hardware understanable
format.
=====================
What is Scripting ?
=====================
=> Scripting means set of commands we are keeping in a file for execution.
whoami
pwd
date
cal
ls -l
Note: instead of executing these commands one after other manually we can keep
them inside a file and we can execute that file which is called as Scripting.
=> The process of executing script file using shell is called as Shell Scripting.
=> Shell scripting is used to automate our daily routine work in the project.
Note: Shell script files we will create using .sh extension.
$ echo $SHELL
$ cat /etc/shells
===================================================
====================================================
============================
============================
=> sha-bang is used to specify which shell we should use to process our script file.
syntax: #! /bin/bash
#! /bin/bash
read uname
#! /bin/bash
read fname
read lname
===========
Variables
===========
=> Variables are used to store the values
a = 10
b = 20
name = ashokit
age = 30
=> The variables which are already defined and using by our system are called as
System variables.
$ echo $SHELL
$ echo $USER
$ echo $PATH
Note: We can access all the environmental variables using below command
$ env
=> The variables which we are creating for our requirement are called as 'User Defined
Variables'.
name = ashok
id = 101
age = 25
gender = male
$ echo $VARIABLE_NAME
$ export course=devops
$ echo $course
# unset variable
$ unset variable_name
Note: If we use export command in terminal for setting variables then those variables
will be removed once we close our terminal. These are called temporary variables.
===================================
=> We will use .bashrc file to set variables permanently for the user.
$ cat .bashrc
$ vi .bashrc
course=devops
trainer=ashok
$ source .bashrc
# Access variables
$ echo $course
$ echo $trainer
Note: In linux machine, every user will contain their own .bashrc file.
================================================
=================================================
$ cat /etc/profile
Note: If we add variables in /etc/profile then those variables applicable for all users in
linux vm.
================
Variables Rules
================
Ex: - , @, #
=============
Operators
=============
========================
Arithematic Operations
========================
#! /bin/bash
read FNUM
read SNUM
=======================
Comparision Operators
=======================
Equal : ==
Not Equal : !=
========================
Conditional Statements
========================
Syntax :
if [ condition-1 ]; then
// stmt-1
else
// stmt-3
fi
#! /bin/bash
read N1
read N2
echo "Equal"
else
fi
read AGE
else
fi
#! /bin/bash
read N1
else
fi
====================
Looping Statements
===================
=================
================
do
// stmts
done
================================================
===============================================
#! /bin/bash
do
echo "$i"
done
================================================
================================================
do
echo "$i"
done
=============
While Loop
=============
=> While loop is used to execute statements until condition is true
============================
============================
#! /bin/bash
N=1
while [ $N -le 10 ]
do
echo "$N"
let N++
done
============================
============================
N=10;
while [ $N -gt 0 ]
do
echo "$N"
let N--
done
==========================
==========================
N=10;
while [ $N -gt 0 ]
do
echo "$N"
done
======================
Functions / Methods
======================
=> Using functions we can divide big task into multiple small tasks.
---------
syntax
---------
# creating function
function functionName( ) {
// function body
functionName
#! /bin/bash
function welcome(){
}
welcome
=================================================================
Q) Write a function which will read filename from user and print content of that file.
#! /bin/bash
function doWork(){
read fname
cat $fname
doWork
=====================================================================
Q) Write a function which will read filename from user and check file is already present
or not. If file is not present then create that file. If file is already present then print
content of that file.
#! /bin/bash
function fileOps(){
read fname
if [ -f "$fname" ]; then
cat $fname
else
touch $fname
fi
fileOps
======================================================================
=======================
=======================
=> cmd args are used to supply values to script file at the time of execution.
$ sh task.sh 10 20 30
=> cmd args are we can access in script file like below...
================================
#! /bin/bash
echo "==========="
==================================
#! /bin/bash
result=$(($1+$2))
=============================
$ sh <filename> 10 20
===========================
===========================
2*1=2
2*2=4
...
2 * 10 = 20
=====================
What is Scheduling ?
=====================
=========
usecase
=========
Note: Instead of human executing script for every 5 minutes, we can automate script
execution using CRON job.
==================
What is CROND ?
=================
=> Every minute, CROND will be checking for CRON Jobs Schedule for the execution.
================
================
Syntax : * * * * * <script-file>
======================
======================
Run for every 15 mins => */15 * * * * <script-file>
========================
========================
$ crontab -e
$ crontab -l
$ crontab -r
===================
===================
$ sudo systemctl status cron
====================
CRONJOB Practicals
====================
$ vi task.sh
touch /home/ubuntu/f1.txt
touch /home/ubuntu/f2.txt
$ chmod +x task.sh
$ crontab -e
$ ls -l
======================================================================
==
=============
Summary
=============
1) What is Shell
2) What is Kernal
3) What is Scripting
4) Shell Scripting
6) Programming Vs Scripting
7) What is sha-bang ?
8) Variables
11) Operators
14) Functions
======================================
=======================================
Redirecting output to a log file in Linux is a common practice and can be achieved using
the > or >> operators. Here's how:
$ ls > directory_listing.txt
====================================================
====================================================
=======================================
Q-1) Write shell script with logging
=======================================
#! /bin/bash
LOG_FILE=myapp.log
log_message(){
local message=$1
#exit 1
# Simulate error
mkdirs aws
ls abc
================================================
================================================
#!/bin/bash
# Backup script
backup_dir="/path/to/backup"
source_dir="/path/to/source"
==============================================
==============================================
#!/bin/bash
df -h
free -h
==========================================================
==========================================================
mywebapp/
├── config
│ └── config.yaml
├── docs
│ └── README.md
├── src
│ ├── app.js
│ ├── index.html
│ └── main.css
└── tests
└── test.js
===============================================================
#! /bin/bash
PROJECT_NAME="mywebapp"
ROOT_DIR=$(pwd)
create_project(){
mkdir $1
mkdir $1/src
mkdir $1/tests
mkdir $1/docs
mkdir $1/config
touch $1/config/config.yaml
touch $1/docs/README.md
touch $1/src/app.js
touch $1/src/main.css
touch $1/src/index.html
touch $1/tests/test.js
create_project $ROOT_DIR/$PROJECT_NAME
===========================================
============================================
#!/bin/bash
logfile="/var/log/syslog"