Linux for DevOps Engineers

Linux for DevOps Engineers

The Linux is a type of operating system that is similar to Unix, and it is built upon the Linux Kernel. The Linux Kernel is like the brain of the operating system because it manages how the computer interacts with its hardware and resources. It makes sure everything works smoothly and efficiently. It is open source.

But the Linux Kernel alone is not enough to make a complete operating system. To create a full and functional system, the Linux Kernel is combined with a collection of software packages and utilities, which are together called Linux distributions. These distributions make the Linux Operating System ready for users to run their applications and perform tasks on their computers securely and effectively. Linux distributions come in different flavors, each tailored to suit the specific needs and preferences of users.

Architecture of Linux

Linux architecture has the following components:

Linux Architecture

Easy Commands

  • whoami → Current user

  • touch → to create file

  • ls → to list directory

  • sudo → super user permission

  • pwd → present working directory

  • mkdir → to create a directory

  • cd → change directory

  • mv <source> <destination> → to move any file

  • clear → to clear the bash/terminal or ctrl+L

  • cat filename → to view contents of file

  • echo "your text" → to print something

  • ls -la → list contents of directory with file info

  • nano file_name → to write something to a file.

    To exit nano: ctrl+ x > press y if want to save or press n for no > press enter.

  • history → to all your previously executed commands

  • rm filename → to delete the file (be careful before deleting)

Intermediate Commands

  • SSH → Secured Shell Protocol (Connecting one machine from another machine in secure environment is called SSH)

PuTTY

ssh -i “private-key” username@public-ip
  • Update and Install
# Update
sudo apt-get udpate
# Install
sudo apt-get install docker.io -y

You have observed after finishing every task it shows processing triggers for man-db.

Here man db is manual database which is just like manual which contain data of that command.

Suppose you want to learn everything about ls command, run command:

man ls

Similarly, you can search anything using man

man ssh
Quick tips
If you want to complete any command, just write its two letter and press tab. Eg. chmod is a command for change mode you have to type ch and press tab, it will get auto complete.
  • Copy - Syntax: cp <source> <destination>
cp /home/ubuntu/file.txt /home/ubuntu/newfolder/
  • Server Copy (scp) → Copying anything from local to server

Syntax: sudo scp -i <private-key> <source> <destination>:path

Make sure you are in same directory where your private key is there

sudo scp -i "key.pem" local_file.txt ubuntu@ec2-54-72-184-240.eu-west-1.compute.amazonaws.com:/home/ubuntu/new_folder/

Now if you want to copy something from server to local then also you can run this like

sudo scp -i "key.pem" ubuntu@ec2-54-72-184-240.eu-west-1.compute.amazonaws.com:/home/ubuntu/new_folder/aws.txt .

Note: when you are copying anything from server to local then also you have to run the above in local only.

Shell Scripting

  1. If-elif
#!/bin/bash 

a=100
b=200
c=300

<< comment
-ge = greater than equal to
-gt = greater than
-lt = less than
-le = less than equal to
comment

if [[ $a -gt $b && $a -gt $c ]]
then
    echo "a is biggest"
elif [[ $b -gt $a && $b -gt $c ]]
then
    echo "b is biggest"
else
    echo "c is biggest"
fi
  1. Loops
#!/bin/bash

for ((i==0;i<10;i++))
do
    echo "$i"
done

To create multiple files of same name and different numbering

touch file{1..10}.txt

Now if we want to iterate to every file

#!/bin/bash

for File in *.txt # 8.txt means iterate everything containing .txt
do
    echo "$File"
done
  1. Function

Create a script to create user with password

#!/bin/bash

add_user(){
user = $1
pass = $2

sudo useradd -m -p $pass $user && echo "Successfully added user"
}

#main
add_user harshit har@123

Backup Script

#!/bin/bash

src = /home/ubuntu/scripts # Jiska backup lena hai
dest = /home/ubuntu/backup # Jha pe backup lena hai

curr_timestamp = $(date "%Y-%m-%d-%H-%M-%S") # year-month-date-hour-minute-second
backup_file = $dest/$curr_timestamp.tgz # naming and converting backup file into zipped form

echo "Taking backup on $curr_timestamp"

tar czf $backup_file $src

echo "Backup complete"

Memory commands

free → to check memory status of RAM

top → CPU stands for Central Processing Unit, it means a place where all processes run, so to get top process list use top command.

df -h → Check Disk memory space

Filesystem Size Used Avail Use% Mounted on

AWK commands

  • AWK is a scripting language in itself,

  • It is used to sort and fetch a specific data from a source.

Suppose you want to show only filesystem column in df -h command

df -h | awk '{print $1}'

Suppose you want to print 5th column as well

df -h | awk '{print $1 " " $5}'

While loop

#!/bin/bash

df -h | awk '{print $5 " " $1}' | while read output;
do
    echo "Disk Detail: $output"
done

Cut command

Suppose in df -h outputs you want to see used% data without the symbol “%“ then you can use cut command there.

#!/bin/bash

df -h | awk '{print $5 " " $1}' | while read output;
do
    #echo "Disk Detail: $output"
    usage = $(echo $ouput | awk '{print $1}' | cut -d'%' -f1) # -d cuts the % and f1 tag prints the output line by line
    echo $usage
done

Now we have to make a script which indicates alert when memory is used above 90%.

#!/bin/bash

df -h | awk '{print $5 " " $1}' | while read output;
do
    usage = $(echo $ouput | awk '{print $1}' | cut -d'%' -f1) 
    file_sys = $(echo $ouput | awk '{print $2}') 
    if [ $usage -ge 90 ]
    then 
        echo "CRITICAL for $file_sys"
    fi
done

Using variables:

#!/bin/bash
alert = 90
df -h | awk '{print $5 " " $1}' | while read output;
do
    usage = $(echo $ouput | awk '{print $1}' | cut -d'%' -f1) 
    file_sys = $(echo $ouput | awk '{print $2}') 
    if [ $usage -ge $alert ]
    then 
        echo "CRITICAL for $file_sys"
    fi
done