02 - Linux Running Notes
02 - Linux Running Notes
1) Windows OS
2) Linux OS
3) Mac OS
============
Windows OS
============
Ex: Watching Movies, Playing Games, Browsing, Storing personal data ....
-> Security features are very less (we need to install Anti Virus softwares)
==========
Linux OS
==========
-> Multi User Based Operating System (multiple users can access at a time)
-> 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"
-> 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..
2) What is Windows OS
3) Linux OS
4) Linux History
5) Linux Distributions
==================================
Environment Setup to Learn Linux
=================================
===============
Linux Commands
===============
cd : change directory
rm -r <dirname>
rm : Remove file
cat : To create files with data, append data to file and display file content
tac f1.txt
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
=======================
Text Editors in Linux
=======================
vi : visual editor
$ vi linux.txt
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
===========
=> 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
=============
grep command
=============
================================
Working with Zip files in Linux
================================
# Create zip file (all .txt files will be added to notes.zip file)
$ zip notes *.txt
###### 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.
$ locate 'apache'
========================
Users & Groups in Linux
========================
=> In Every Linux Machine by default one 'root' user account will be available
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
=> To get user account information we can use 'id' command in linux machine
$ id <uname>
====================================
Working with User Accounts in Linux
===================================
=====================================
Working with User Groups in Linux
=====================================
$ cat /etc/group
Note: When we create user in linux machine, it will create user account + user-
group
# Rename user-group
$ groupmod -g <new-name> <existing-name>
# Rename user
$ usermod -l <new-name> <existing-name>
################
03-Aug-2023(Thu)
################
==========================
File Permissions In Linux
==========================
=> In Linux VM everything is treated as file (Ex: normal files, directory files)
1) READ (r)
2) WRITE (w)
3) EXECUTE (x)
Ex: rwxrwxrwx
===================================
File Permissions in Numeric Format
===================================
0 : No Permission
1 : Execute
2 : Write
4 : Read
============================
Working with chown command
===========================
=> chown command is used to change file owner and file group
Note: We can use userid and group id also in chown command instead of names.
#################
04-Aug-2023
###############
===========================
Hard Links and Soft Links
===========================
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 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
$ 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
============================
=> In Windows machine we can see running processes using "Task Manager"
$ ps
$ top
$ kill -9 <process-id>
=====================
Networking Commands
=====================
# To check connectivity
$ ping <ip>
$ ping www.google.com
$ ping www.facebook.com
========================
Utility Commands
========================
# Display OS information
$ cat /etc/os-release
===========================
Package Managers in Linux
===========================
Ex: Ubuntu
=========================
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.
Note: ‘#’ indicates comment in the file, OS will ignore this line while executing.
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.
Read this as — User “ashok” can Run the command “/usr/bin/cat /etc/shadow” as ROOT
user on all the HOSTS.
=================================
What is .bashrc file in linux ?
=================================
=> 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)
$ cat .ssh/authorized_keys
====================
Linux Architecture
====================
1) Applications / Commands
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..
$ sh <file-name>
=========
Script-1
========
whoami
pwd
date
ls -l
touch f2.txt
ls -l
=========
Script-2
========
=========
Script-3
========
echo "Enter Your Name";
read name
=========
Script-4
========
read a
read b
let sum=$(($a+$b))
========
3.1) if - elif
3.2) for loop
3.3) while loop
===========
Variables
===========
a=10
b=20
name=ashok
$ echo $USER
$ echo $SHELL
===================
Variable Rules
===================
=======================
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
============
Script-6
============
==========
Script-7
=========
let result=$(($1*$2))
====================
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
=======================
=> Conditional statements are used to execute commands based on given condition
Syntax:
if [ condition ]
then
stmts;
else
stmts;
fi
=> As per above syntax if given condition satisifed then if statments will execute
othewise else statements will execute.
Note: If we want to check more than one conditon then "if - elif - else - fi"
============
script - 8
============
====================
Looping Statements
====================
=> Loops are used to execute statements / commands multiple times
Ex: print "hello" message for 1000 times (we can't write echo "hello" for 1000
times).
===================================
for loop example (1 to 10 numbers)
===================================
============================
print numbers from 10 to 1
============================
for((i=10; i>=1; i--))
do
echo "$i"
done
===========
while loop
===========
i=10
while [ $i -ge 1 ]
do
echo "$i"
let i--;
done
i=1
while [ $i -le 10 ]
do
echo "$i"
let i++
done
===========
Functions
===========
=> The big task can be divided into smaller tasks using functions.
-------
Syntax
-------
===================================================================================
Q) Write shell script to take file name as input and print content of that file
===================================================================================
---------------------------
approach-1 (read command)
---------------------------
function readFileData(){
echo "enter file name"
read filename
echo "#### file reading start #####"
cat $filename
echo "##### file reading end #####"
}
#calling function
readFileData
----------------------
approach-2 (cmd arg)
----------------------
filename=$1
function readFileData(){
echo "#### file reading start #####"
cat $filename
echo "##### file reading end #####"
}
#calling function
readFileData
===========================================================================
Q) Check presence of given file, if it is not available create that file
============================================================================
# file checking
read filename
if [ -f "$filename" ] ; then
echo "File alredy exist"
else
echo "File not available, hence creating"
touch $filename
===================================================================================
Q) Check presence of given directory, if it is not available create that directory
===================================================================================
# file checking
read dirname
if [ -d "$dirname" ] ; then
echo "Directory alredy exist"
else
echo "Directory not available, hence creating"
mkdir $dirname
===========================
Shell Script Assignments
===========================
Task-1 : Take a number from user and check given number is +ve or -ve
Task-2 : Take a number from user and check given number is even or odd
Task-3 : Take a number from user and check given number is prime number or not
Task-4 : Take a number from user and print multiplication table of given number
like below
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
..
5 * 10 = 50
=========
Summary
=========
1) What is Shell
2) What is Kernel
3) What is Scripting
4) Why we need Scripting
5) Writing Shell Script files (.sh extension)
6) Executing shell script files ($ sh filename)
7) Variables
8) Taking input using shell script
9) Commandline arguments
10) Conditional Statements (if-elif-else-fi)
11) Working with Loops (for and while)
====================
CRON JOBS in Linux
====================
==========================
What is CROND in Linux?
==========================
=> CROND will check for cron schedules every minute. If any job is scheduled then
crond will execute that job based on given schedule.
=================
CRON JOB Syntax
=================
* * * * * <command/script-file>
=> Fift * will represent day of week ( 0 - 6 or Mon, Tues, Wed... Sun)
========================
Sample CRON Expressions
=======================
=========================
What is crontab file ?
=========================
=> In Linux machine, for every user account one crontab file will be available.
$ crontab -e
$ crontab -l
$ crontab -r
==================
CRON Job practice
==================
$ vi task.sh
touch /home/ubuntu/f1.txt
touch /home/ubunut/f2.txt
$ chmod +x task.sh
$ crontab -e
$ ls - l