Basic To Advance Quick Guide to Linux Shell Scripting for DevOps Engineers.

Basic To Advance Quick Guide to Linux Shell Scripting for DevOps Engineers.

#90 days of DevOps challenge- (Day4,5,6,7)

Day4:- What is Shell Scripting,Shell and Kernel?

❇️What is Shell Scripting?

Shell scripting is the process of writing code using shell commands to automate tasks or processes in a DevOps environment. Essentially, it allows you to execute a series of commands automatically without having to manually input each command every time you need it. This can save a lot of time and effort for developers, system administrators, and other IT professionals who are responsible for managing complex systems.

For example, let's say you have a web application that needs to be deployed on multiple servers. Instead of manually logging into each server and running the deployment commands one by one, you can write a shell script that will automate this task. The script can connect to each server, run the necessary commands, and even send notifications when the deployment is complete.

Another example is automating backups. If you have a database that needs to be backed up regularly, you can create a shell script that will run the backup command and save the backup file in a specific location. You can then schedule the script to run at regular intervals, ensuring that your data is always backed up without requiring manual intervention.

Overall, shell scripting is a powerful tool for DevOps professionals, allowing them to automate repetitive tasks, increase efficiency, and reduce errors.

❇️Shell and Kernel: -

The kernel is the core component of an operating system. It is responsible for managing system resources such as the CPU, memory, and input/output devices. The kernel acts as a bridge between software applications and hardware components, allowing them to communicate and work together.

For example, when you plug in a USB drive, the kernel detects it and makes it available to the rest of the system. When you open a file, the kernel reads it from the hard drive and sends it to the application that requested it. In essence, the kernel is the mediator between the hardware and software of your computer.

On the other hand, a shell is a command line interface that allows users to interact with the operating system. It is a program that takes user commands and executes them by communicating with the kernel. The shell provides a way for users to access the underlying features of the operating system, such as launching programs, creating files, and manipulating data.

For example, when you type ls in a shell, you're asking the shell to list the files in the current directory. The shell then communicates with the kernel to retrieve the information and displays it back to you. Similarly, when you run a script or program, the shell passes the instructions to the kernel, which in turn executes them.

❇️What is #!/bin/bash?

#!/bin/bash is called a "shebang" or "hashbang" and it is used in Unix/Linux operating systems to indicate to the system what interpreter should be used to run the script. In this case, #! indicates that the following path /bin/bash specifies that the script should be interpreted using the Bash shell.

❇️Can we write #!/bin/sh as well?

Yes, you can also use #!/bin/sh it instead of #!/bin/bash. The difference is that #!/bin/sh specifies that the script should be interpreted using the default system shell, which may or may not be Bash. On some systems, /bin/sh may point to Bash or to another shell entirely, such as the Bourne shell (sh).

❇️Write a Shell Script that prints I will complete #90DaysOofDevOps challenge :-

#!/bin/bash

echo "I will complete #90DaysOfDevOps challenge"

❇️Write a Shell Script to take user input, input from arguments, and print the variables:-

#!/bin/bash

# Take user input
echo "Please enter your name: "
read name

# Print out variables
echo "Your name is: $name"

❇️Write an Example of If else in Shell Scripting by comparing 2 numbers:-

#!/bin/bash

# Set the values of two variables
num1=07
num2=99

# Compare the values using if-else statements
if [ $num1 -gt $num2 ]
then
    echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]
then
    echo "$num1 is less than $num2"
else
    echo "$num1 is equal to $num2"
fi

Day5:- Advanced Linux Shell Scripting for DevOps Engineers with User management

❇️write a bash script createDirectories.sh that creates a specified number of directories with a dynamic directory name: -

#!/bin/bash

# Check if number of arguments is correct
if [ $# -ne 3 ]; then
    echo "Usage: $0 <directory_name> <start_number> <end_number>"
    exit 1
fi

# Assign the arguments to variables
dir_name=$1
start_num=$2
end_num=$3

# Loop through the numbers to create directories
for (( i=$start_num; i<=$end_num; i++ ))
do
    dir=${dir_name}_${i}
    mkdir $dir && echo "Directory created: $dir"
done

echo "All directories created successfully!"

❇️Create a Script to backup all your work done till now:-

#!/bin/bash

# Set the backup directory
BACKUP_DIR=/path/to/backup/directory

# Create the backup directory if it doesn't exist
mkdir -p $BACKUP_DIR

# Set the filename for the backup
FILENAME="backup_$(date +%Y-%m-%d_%H-%M-%S).tar.gz"

# Create the tarball of all files in the current directory
tar -czvf $BACKUP_DIR/$FILENAME .

# Print a message indicating the backup has finished and where it was saved
echo "Backup complete! Your file is located at $BACKUP_DIR/$FILENAME"

To use this script, open a terminal window and navigate to the directory where you want to create the backup. Then, save the script as backup.sh (or whatever name you prefer) and make it executable using the following command:

chmod +x backup.sh

Finally, run the script using the following command:

./backup.sh

This will create a compressed tarball of all the files in the current directory and save it to the directory specified by BACKUP_DIR. The filename of the backup will include the current date and time to help you keep track of different backups.

❇️Create a script that creates 2 users and just displays their Usernames:-

# Create first user
sudo useradd -m user1

# Create second user
sudo useradd -m user2

# Display usernames of the two most recently created users
echo "Most recent users:"
tail -n 2 /etc/passwd | cut -d ':' -f 1

The tail -n 2 /etc/passwd the command outputs the last two lines of the /etc/passwd file, which contains information about all users on the system. The output is piped (|) to the cut command, which separates each line using the colon (:) delimiter and outputs only the first field (-f 1), which is the username. The final output will look like this:

Most recent users:
user1
user2

This way, you don't have to hardcode the usernames in your script, and it will automatically display the two most recently created users every time you run the script.

Day6:- File Permissions and Access Control Lists

❇️File Permissions in:-

Every file in Linux has three types of permissions - read, write, and execute - which can be applied to three different groups of users: the owner of the file, the group that owns the file, and everyone else. These permissions are denoted by a combination of letters and symbols that are represented in the output of the ls-l command.

The first character in the output of the ls-l the command indicates the type of the file - whether it is a regular file, directory, symbolic link, or another special file. The next nine characters represent the file permissions. The first three characters indicate the permissions for the owner of the file, the next three for the group that owns the file, and the last three for everyone else.

The three types of permissions are as follows:

  • Read permission (r): Allows a user to open and read the contents of a file or list the contents of a directory.

  • Write permission (w): Allows a user to modify or delete the contents of a file or create, rename, or delete files in a directory.

  • Execute permission (x): Allows a user to run a program or script or enter a directory.

To assign permissions to a file, you can use the chmod command. For example, to give the owner of a file read, write, and execute permissions, and only read and execute permissions to the group and everyone else, you can use the following command:

chmod 750 filename

Here, 7 represents read, write, and execute permissions for the owner of the file (4+2+1), 5 represents read and execute permissions for the group (4+1), and 0 represents no permissions for everyone else.

It is important to note that file permissions can be overridden by the superuser or root user, who has complete control over the system. Therefore, it is essential to practice good security measures, such as limiting access to sensitive files and directories and ensuring that only trusted users have administrative privileges.

In conclusion, understanding file permissions in Linux is critical for maintaining the security of your system. By properly assigning permissions to files and directories, you can ensure that only authorized users can access or modify them.

❇️Access Control Lists (ACLs):-

In addition to the standard file permissions, Linux also supports Access Control Lists (ACLs), which provide more granular control over file and directory access. ACLs allow you to set permissions for specific users or groups on a file or directory, rather than just the owner, group owner, and everyone else.

ACLs can be viewed and modified using the getfacl and setfacl commands. The getfacl command displays the ACLs for a file or directory, while the setfacl command allows you to modify the ACLs.

To use the getfacl command, simply provide the path to the file or directory as an argument. For example, to view the ACLs for a file named example.txt, you would run the following command:

getfacl example.txt

This will output a list of permissions for the file, including any additional ACLs that have been set.

To set an ACL using the setfacl command, you must specify the user or group you wish to grant permission to, along with the type of permission you want to grant. For example, to grant read and write permission to the user "jdoe" for a file named example.txt, you would run the following command:

setfacl -m u:jdoe:rw example.txt

Here, the -m flag indicates that we are modifying the ACLs, while the u:jdoe:rw argument specifies that we are granting read and write permission (rw) to the user jdoe. You can replace u: with g: to grant permission to a group instead of a user.

You can also remove an ACL by using the -x flag followed by the user or group name. For example, to remove the ACL for the user jdoe on example.txt, you would run the following command:

setfacl -x u:jdoe example.txt

ACLs can provide greater flexibility and control over file and directory permissions in Linux. By using the getfacl and setfacl commands, you can easily view and modify ACLs to meet your specific needs.

Day7:- Understanding package manager and systemctl

❇️Install Docker and Jenkins On UBUNTU

Update the apt package index:

sudo apt update

Install Docker by executing the following command:

sudo apt install docker.io

Start the Docker service and enable it to start at boot time:

sudo systemctl start docker
sudo systemctl enable docker

Check if Docker is running:

sudo systemctl status docker

Install Jenkins by adding its repository key:

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

Add the Jenkins Debian package repository to the system:

sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

Update the apt package index again:

sudo apt update

Install Jenkins:

sudo apt install jenkins

Start the Jenkins service and enable it to start at boot time:

sudo systemctl start jenkins
sudo systemctl enable jenkins

Check if Jenkins is running:

sudo systemctl status jenkins

That's it! You have successfully installed Docker and Jenkins on your Ubuntu system using package managers.

❇️Install these tools using package managers on Ubuntu and CentOS.

Docker and Jenkins are two powerful tools that are widely used in the world of software development. Docker helps to create, deploy and run applications by using containers while Jenkins is a popular automation server that facilitates continuous integration and continuous delivery (CI/CD) pipelines. In this article, we will guide you on how to install Docker and Jenkins on Ubuntu and CentOS Linux distributions using package managers.

Installing Docker on Ubuntu:

The first step is to update the package index and install the necessary dependencies using the following commands:

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Next, you need to add Docker's official GPG key and repository by running the following commands:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Once the repository is added, you can proceed with installing Docker using the following command:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

To verify if the installation was successful, run the following command to check the Docker version:

docker --version

Installing Jenkins on Ubuntu:

Before installing Jenkins, you need to install Java Runtime Environment (JRE) using the following command:

sudo apt-get install default-jre-headless

Next, add the Jenkins repository key and source list using the following commands:

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

Update the package index and install Jenkins using the following commands:

sudo apt-get update
sudo apt-get install jenkins

Once the installation is complete, start the Jenkins service and enable it to start at boot time:

sudo systemctl start jenkins
sudo systemctl enable jenkins

To verify if the installation was successful, open a web browser and navigate to http://your_ip_address_or_domain_name:8080. You will be prompted to enter an initial admin password which can be found in the /var/lib/jenkins/secrets/initialAdminPassword file.

Installing Docker on CentOS:

The first step is to install the necessary dependencies using the following command:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Next, add the Docker repository using the following command:

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Finally, install Docker using the following command:

sudo yum install docker-ce docker-ce-cli containerd.io

To verify if the installation was successful, run the following command to check the Docker version:

docker --version

Installing Jenkins on CentOS:

Before installing Jenkins, you need to install Java Development Kit (JDK) using the following command:

sudo yum install java-1.8.0-openjdk-devel

Next, add the Jenkins repository key and source list using the following commands:

sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

Install Jenkins using the following command:

sudo yum install jenkins

Once the installation is complete, start the Jenkins service and enable it to start at boot time:

sudo systemctl start jenkins
sudo systemctl enable jenkins

To verify if the installation was successful, open a web browser and navigate to http://your_ip_address_or_domain_name:8080. You will be prompted to enter an initial admin password which can be found in the /var/lib/jenkins/secrets/initialAdminPassword file.

In conclusion, Docker and Jenkins are two essential tools for modern software development. By following the steps outlined above, you can easily install these tools on your Ubuntu or CentOS Linux distribution using package managers.