How to create rpm package

Install required tools

rpm -qa | grep rpmdevtools
yum list | grep rpmdevtools
yum install rpmdevtools

Create directory structure required for rpm build if not exist

rpmdev-setuptree
rpmbuild/
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS

Create source package under SOURCES

mkdir hello-0.0.1
cd hello-0.0.1

hello.sh

#!/bin/sh
echo "Hello world"
tar -cvzf hello-0.0.1.tar.gz hello-0.0.1

Create spec file and make necessary changes

cd ~/rpmbuild/SPECS
rpmdev-newspec hello.spec
Name:           hello
Version:        0.0.1
Release:        1%{?dist}
Summary:        A simple hello world script
BuildArch:      noarch

License:        GPL
Source0:        %{name}-%{version}.tar.gz

Requires:       bash

%description
A demo RPM build

%prep
%setup -q

%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/opt/hello
cp * $RPM_BUILD_ROOT/opt/hello

%clean
rm -rf $RPM_BUILD_ROOT

%files
/opt/hello/*

%postun
rm -vrf /opt/hello

%changelog
* Sun Feb 04 2024 
- First version being packaged

Now directory structure should look like this

/home/tux/rpmbuild/
├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
│   └── hello-0.0.1.tar.gz
├── SPECS
│   └── hello.spec
└── SRPMS

Build rpm using following command

rpmbuild -ba SPECS/hello.spec

where -b is build a is for both source and binary
if you want only binary then put -bb flag
if you want only source then put -bs flag

Install the rpm using following command

sudo rpm -ivh ~/rpmbuild/RPMS/noarch/hello-0.0.1-1.el8.noarch.rpm

Verify rpm installed successfully using following command

rpm -qi hello
tree /opt/hello

The %changelog entry of a package can be viewed, too:

rpm -q hello –changelog

Uninstall or erase installed rpm

sudo rpm -q | grep hello
sudo rpm -ve hello

Troubleshoot

To extract files and directories from rpm

rpm2cpio hello-0.0.1-1.el8.noarch.rpm | cpio -idmv

Linux Crash Course

Day 1: Introduction to Linux

Topics:

  1. Linux operating system
    Explanation: Learn about the Linux operating system, its features, and its popularity among developers and system administrators.
  2. Basic Linux commands
    Explanation: Understand essential Linux commands such as ls (list files and directories), cd (change directory), pwd (print working directory), and mkdir (make directory).
  3. File and directory management
    Explanation: Learn how to create, delete, move, and rename files and directories using commands like touch, rm, mv, and cp.
  4. File permissions and ownership
    Explanation: Understand the concept of file permissions and how to set permissions using chmod, as well as changing file ownership using chown.
  5. File manipulation and text editing
    Explanation: Explore commands like cat (concatenate and display file contents), less (view file contents page by page), and nano (a basic text editor).

Assignments:

  1. Create a directory named “my_files” and navigate into it.
    Answer: mkdir my_files (to create the directory) and cd my_files (to navigate into it).
  2. Create a file named “my_text.txt” and write some text into it.
    Answer: touch my_text.txt (to create the file) and nano my_text.txt (to open the file in the nano text editor and write the text).
  3. List all the files and directories in the current directory.
    Answer: ls (to list files and directories).
  4. Rename the file “my_text.txt” to “new_text.txt”.
    Answer: mv my_text.txt new_text.txt (to rename the file).
  5. Delete the directory “my_files” and all its contents.
    Answer: rm -r my_files (to remove the directory and its contents).

Day 2: Working with Files and Permissions

Topics:

  1. File manipulation commands
    Explanation: Explore more file manipulation commands such as head (display the beginning of a file), tail (display the end of a file), and wc (word count).
  2. File permissions and access modes
    Explanation: Understand the three levels of file permissions (owner, group, others) and how to set permissions using symbolic notation (rwx) and numeric notation (777).
  3. Changing file ownership and group
    Explanation: Learn how to change the owner and group of a file using the chown and chgrp commands.
  4. File compression and archiving
    Explanation: Discover commands like tar (archive files) and gzip (compress files) to manage large file collections efficiently.
  5. File searching and filtering
    Explanation: Learn about commands like find (search for files and directories) and grep (search for text patterns in files).

Assignments:

  1. Create a file named “sample.txt” and write some content into it.
    Answer: touch sample.txt (to create the file) and nano sample.txt (to open the file in the nano text editor and write the content).
  2. Display the first 5 lines of the file “sample.txt”.
    Answer: head -n 5 sample.txt (to display the first 5 lines).
  3. Set the file permissions of “sample.txt” to read and write for the owner only.
    Answer: chmod 600 sample.txt (to set the permissions).
  4. Change the owner of “sample.txt

” to a different user.
Answer: chown username sample.txt (to change the owner to the specified username).

  1. Archive the file “sample.txt” and compress it into a single file.
    Answer: tar -czvf sample.tar.gz sample.txt (to create the archive and compress it).

Day 3: Introduction to Vim Editor

Topics:

  1. Introduction to Vim
    Explanation: Understand the basics of the Vim editor, including its modes (Normal, Insert, Visual), navigation, and command execution.
  2. Opening and saving files
    Explanation: Learn how to open files in Vim, make changes, and save them using commands like :e (open), :w (save), and :q (quit).
  3. Moving and navigating within files
    Explanation: Explore various movement commands like h (left), j (down), k (up), l (right), and using line numbers to jump to specific locations.
  4. Editing and modifying text
    Explanation: Understand text editing commands like i (insert), o (open a new line), x (delete a character), and yy (copy a line).
  5. Undo and redo operations
    Explanation: Learn how to undo and redo changes made in Vim using the u (undo) and Ctrl+R (redo) commands.

Assignments:

  1. Open the file “my_file.txt” in Vim and navigate to the end of the file.
    Answer: vim my_file.txt (to open the file) and G (to move to the end of the file).
  2. Insert a new line after the current line and write some text into it.
    Answer: Press o (to open a new line) and start typing the desired text.
  3. Delete the current line in Vim.
    Answer: Press dd (to delete the current line).
  4. Copy the current line in Vim and paste it below the current line.
    Answer: Press yy (to copy the current line) and then p (to paste it below).
  5. Save the changes made to the file and exit Vim.
    Answer: Press :wq (to save and quit).

Day 4: Advanced Vim Editing

Topics:

  1. Visual mode in Vim
    Explanation: Learn how to select and manipulate blocks of text using Visual mode, including commands like v (characterwise), V (linewise), and Ctrl+V (blockwise).
  2. Searching and replacing text
    Explanation: Discover how to search for specific text patterns using / (forward search) and ? (backward search), as well as replacing text using :s (substitute).
  3. Advanced editing commands
    Explanation: Explore advanced editing commands like x (delete a character), r (replace a character), J (join lines), and . (repeat the last command).
  4. Advanced movement and navigation
    Explanation: Learn advanced movement commands like w (jump to the beginning of the next word), b (jump to the beginning of the previous word), and gg (jump to the first line).
  5. Split windows and tabs in Vim
    Explanation: Understand how to split the Vim editor window vertically and horizontally using commands like :split and :vsplit, and navigate between tabs.

Assignments:

  1. Open the file “my_notes.txt” in Vim and search for the word “important”.
    Answer: vim my_notes.txt (to open the file) and /important (to search for the word “important”).
  2. Replace all occurrences of the word “old” with “new” in the current line.
    Answer: :s/old/new/ (to perform the substitution).
  3. Join the current line with the line below it.
    Answer: Press J (to join the lines).
  4. Split the Vim editor window vertically.
    Answer: :vsplit (to split the window vertically).
  5. Move the cursor to the beginning of the next word in Vim.
    Answer: Press w (to jump to the beginning of the next word).

Day 5: Vim Customization and Advanced Topics

Topics:

  1. Vim configuration files
    Explanation: Understand the .vimrc file and how to customize Vim settings, key mappings, and plugins.
  2. Customizing Vim colorschemes
    Explanation: Learn how to change the colorscheme in Vim to enhance the visual appearance and readability of your code.
  3. Advanced Vim features and plugins
    Explanation: Explore advanced Vim features like macros, multiple cursors (using plugins), and code completion (using plugins).
  4. Vim navigation shortcuts
    Explanation: Discover useful navigation shortcuts like Ctrl+U (scroll half a page up), Ctrl+D (scroll half a page down), and gg (jump to the first line).
  5. Vim documentation and help
    Explanation: Learn how to access Vim documentation and help resources, including built-in help pages and online resources.

Assignments:

  1. Customize the Vim settings by adding the following lines to your .vimrc file:
  • Set the tab width to 4 spaces.
  • Enable line numbers.
    Answer: Open the .vimrc file in Vim (vim ~/.vimrc) and add the desired configurations.
  1. Change the colorscheme in Vim to a different one.
    Answer: In Vim, type :colorscheme <colorscheme_name> to change the colorscheme.
  2. Create a macro in Vim that inserts a specific code snippet.
    Answer: Record the macro using q<register> and replay it using @<register>.
  3. Install a plugin in Vim for code completion or any other desired functionality.
    Answer: Install the desired plugin using a plugin manager like Vundle or Pathogen.
  4. Access the Vim built-in help and find information on a specific Vim command or feature.
    Answer: In Vim, type :help <command_or_feature> to access the built-in help pages.

Shell Scripting

Apologies for the oversight. Here are the shell scripting assignments along with sample answers for each day:

Day 1:

  1. Write a shell script that takes two numbers as input and prints their sum.
#!/bin/bash

echo "Enter the first number: "
read num1

echo "Enter the second number: "
read num2

sum=$((num1 + num2))
echo "The sum is: $sum"
  1. Create a shell script that reads a filename from the user and checks if it exists in the current directory. If the file exists, display a message confirming its existence; otherwise, display an error message.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -e "$filename" ]; then
    echo "The file '$filename' exists in the current directory."
else
    echo "Error: The file '$filename' does not exist in the current directory."
fi
  1. Write a shell script that reads a string from the user and prints it in reverse order.
#!/bin/bash

echo "Enter a string: "
read input_string

reversed_string=$(echo "$input_string" | rev)
echo "Reversed string: $reversed_string"
  1. Create a script that takes a directory name as input and lists all the files in that directory.
#!/bin/bash

echo "Enter a directory name: "
read directory

if [ -d "$directory" ]; then
    echo "Files in $directory:"
    ls "$directory"
else
    echo "Error: '$directory' is not a valid directory."
fi
  1. Write a shell script that generates a random number between 1 and 100 and asks the user to guess the number. Provide appropriate feedback based on the user’s guess.
#!/bin/bash

random_number=$((RANDOM % 100 + 1))

echo "Guess the number between 1 and 100: "
read user_guess

if [ "$user_guess" -eq "$random_number" ]; then
    echo "Congratulations! You guessed the correct number."
elif [ "$user_guess" -lt "$random_number" ]; then
    echo "Try again. The number is higher than your guess."
else
    echo "Try again. The number is lower than your guess."
fi

Day 2:

  1. Write a shell script that takes a filename as input and checks if it is a regular file or a directory.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -f "$filename" ]; then
    echo "'$filename' is a regular file."
elif [ -d "$filename" ]; then
    echo "'$filename' is a directory."
else
    echo "Error: '$filename' is neither a regular file nor a directory."
fi
  1. Create a script that takes a file containing a list of numbers and calculates their sum.
#!/bin/bash

sum=0
while read -r num; do
    sum=$((sum + num))
done < "$1"

echo "Sum of numbers in the file: $sum"
  1. Write a shell script that renames all files in a directory with a specific extension to have a prefix “backup_” followed by the original filename.
#!/bin/bash

echo "Enter the directory name: "
read directory

echo "Enter the file extension to rename: "
read ext

for file in "$directory"/*."$ext"; do
    filename=$(basename "$file")
    mv "$file" "$directory/backup_$filename"
done
  1. Create a script that reads a file and counts the number of lines, words, and characters in it.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -f "$filename" ]; then
    line_count=$(wc -l < "$filename")
    word_count=$(wc -w < "$filename")
    char_count=$(wc -c < "$filename")

    echo "Number of lines: $line_count"
    echo "Number of words: $word_count"
    echo "Number of characters: $char_count"
else
    echo "Error: '$filename' is not a valid file."
fi
  1. Write a shell script that takes a number as input and prints all the prime numbers less than or equal to that number.
#!/bin/bash

echo "Enter a number: "
read num

if [ "$num" -lt 2 ]; then
    echo "There are no prime numbers less than 2."
else
    echo "Prime numbers less than or equal to $num:"
    for ((i = 2; i <= num; i++)); do
        is_prime=1
        for ((j = 2; j <= i / 2; j++)); do
            if ((i % j == 0)); then
                is_prime=0
                break
            fi
        done
        if [ "$is_prime" -eq 1 ]; then
            echo "$i"
        fi
    done
fi

Day 3:

  1. Create a shell script that takes a directory name as input and finds all the subdirectories within it.
#!/bin/bash

echo "Enter a directory name: "
read directory

if [ -d "$directory" ]; then
    echo "Subdirectories in '$directory':"
    find "$directory" -type d
else
    echo "Error: '$directory' is not a valid directory."
fi
  1. Write a script that reads a file and removes all the empty lines from it.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -f "$filename" ]; then
    sed -i '/^[[:space:]]*$/d' "$filename"
    echo "Empty lines removed from '$filename'."
else
    echo "Error: '$filename' is not a valid file."
fi
  1. Create a shell script that takes a string as input and checks if it is a palindrome.
#!/bin/bash

echo "Enter a string: "
read input_string

reverse_string=$(echo "$input_string" | rev)

if [ "$input_string" = "$reverse_string" ]; then
    echo "The string is a palindrome."
else
    echo "The string is not a palindrome."
fi
  1. Write a shell script that reads a number as input and checks if it is even or odd.
#!/bin/bash

echo "Enter a number: "
read num

if ((num % 2 == 0)); then
    echo "$num is an even number."
else
    echo "$num is an odd number."
fi
  1. Create a shell script that prints the current date and time.
#!/bin/bash

current_date=$(date +"%Y-%m-%d")
current_time=$(date +"%H:%M:%S")

echo "Current date: $current_date"
echo "Current time: $current_time"

Day 4:

  1. Write a shell script that takes a directory name as input and deletes all the files in that directory with a “.tmp” extension.
#!/bin/bash

echo "Enter a directory name: "
read directory

if [ -d "$directory" ]; then
    find "$directory" -type f -name "*.tmp" -delete
    echo "All '.tmp' files in '$directory' deleted."
else
    echo "Error: '$directory' is not a valid directory."
fi
  1. Create a script that reads a file and replaces all occurrences of a word with another word.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -f "$filename" ]; then
    echo "Enter the word to replace: "
    read old_word

    echo "Enter the new word: "
    read new_word

    sed -i "s/$old_word/$new_word/g" "$filename"
    echo "Occurrences of '$old_word' replaced with '$new_word' in '$filename'."
else
    echo "Error: '$filename' is not a valid file."
fi
  1. Write a shell script that reads a number as input and checks if it is prime.
#!/bin/bash

echo "Enter a number: "
read num

if [ "$num" -lt 2 ]; then
    echo "The number must be greater than or equal to 2 to check for primality."
else
    is_prime=1
    for ((i = 2; i <= num / 2; i++)); do
        if ((num % i == 0)); then
            is_prime=0
            break
        fi
    done

    if [ "$is_prime" -eq 1 ]; then
        echo "$num is a prime number."
    else
        echo "$num is not a prime number."
    fi
fi
  1. Create a shell script that takes a directory name as input and counts the number of files and subdirectories in it.
#!/bin/bash

echo "Enter a directory name: "
read directory

if [ -d "$directory" ]; then
    file_count=$(find "$directory" -type f | wc -l)
    dir_count=$(find "$directory" -type d | wc -l)

    echo "Number of files: $file_count"
    echo "Number of subdirectories: $dir_count"
else
    echo "Error: '$directory' is not a valid directory."
fi
  1. Write a shell script that reads a string as input and converts it to uppercase.
#!/bin/bash

echo "Enter a string: "
read input_string

upper_case_string=$(echo "$input_string" | tr '[:lower:]' '[:upper:]')

echo "Uppercase string: $upper_case_string"

Day 5:

  1. Create a shell script that takes two numbers as input and swaps their values.
#!/bin/bash

echo "Enter the first number: "
read num1

echo "Enter the second number: "
read num2

echo "Before swapping: num1 = $num1, num2 = $num2"

# Swapping using a temporary variable
temp=$num1
num1=$num2
num2=$temp

echo "After swapping: num1 = $num1, num2 = $num2"
  1. Write a script that takes a file and sorts its lines in ascending order.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -f "$filename" ]; then
    sort "$filename" > sorted_"$filename"
    echo "Lines in '$filename' sorted in ascending order and saved to 'sorted_$filename'."
else
    echo "Error: '$filename' is not a valid file."
fi
  1. Create a shell script that reads a directory name as input and prints the names of the 10 largest files in it.
#!/bin/bash

echo "Enter a directory name: "
read directory

if [ -d "$directory" ]; then
    echo "The 10 largest files in '$directory':"
    du -ah "$directory" | sort -rh | head -n 10
else
    echo "Error: '$directory' is not a valid directory."
fi
  1. Write a shell script that takes a list of filenames as arguments and checks if all of them exist in the current directory.
#!/bin/bash

for filename in "$@"; do
    if [ ! -e "$filename" ]; then
        echo "Error: '$filename' does not exist in the current directory."
    else
        echo "'$filename' exists in the current directory."
    fi
done
  1. Create a shell script that calculates the factorial of a given number.
#!/bin/bash

echo "Enter a number: "
read num

factorial=1

for ((i = 1; i <= num; i++)); do
    factorial=$((factorial * i))
done

echo "Factorial of $num is: $factorial"

Nginx

create project and verify with normal url

mkdir /var/www/html/myproject

vim index.html

<h1>Hello World</h1>

vim index.php

<?php

phpinfo();

Create server block conf file

vim /etc/nginx/conf.d/myproject.conf

server {
  listen 8082 default_server;
  server_name _;

  index index.php index.html;

  root /var/www/html/myproject;

  location ~ \.php$
  {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
  }
}

Test nginx configuration

nginx -t

restart nginx

sudo service nginx restart
OR
sudo systemctl restart nginx

For Laravel Project
Specify path till public directory

User Group and Permission

User

List all users

cat /etc/passwd

List specific user

cat /etc/passwd | grep username
OR
grep username /etc/passwd

Create new user

useradd username

Delete existing user

userdel username

Set user password or update password

passwd username

You can use shortcut to add new user. Using following command you can create user set password and create home directory for newly added user

adduser username
ls -l /home/username

Rename existing user

usermod --login new-name old-name

Group

List all groups

cat /etc/group

List specific group

cat /etc/group | grep groupname
cat /etc/group | grep ^groupname
grep groupname /etc/group
grep ^groupname /etc/group

Add new group

groupadd group-name

Delete existing group

groupdel group-name

List all members of specific group

getent group group-name

Add new member in specific groups

usermod -a -G group-name1,group-name2,... user-name

Check in which groups user exist

groups user-name

Remove user from specific group

gpasswd -d user-name group-name

Permission

LIST FILES AND DIRECTORIES

OWNER – FIRST 3 FLAGS SHOWS OWNER’S PERMISSION

GROUP- MIDDLE 3 FLAGS SHOWS GROUP’S PERMISSION

OTHERS- LAST 3 FLAGS SHOWS OTHER’S PERMISSION

Permission List

  • 7 – 111 => read write execute
  • 6 – 110 => read write –
  • 5 – 101 => read – execute
  • 4 – 100 => read – –
  • 3 – 011 => – write execute
  • 2 – 010 => – write –
  • 1 – 001 => – – execute
  • 0 – 000 => no permission

How to change permission

chmod -Rf 777 path-to-file-or-directory

How to give only specific permission

#only executable
chmod -Rf +x path-to-file-or-directory

#only writable
chmod -Rf +w path-to-file-or-directory

#only readable
chmod -Rf +r path-to-file-or-directory

How to give permission to only specific role

#only owner
chmod -Rf u+x path-to-file-or-directory

#only group
chmod -Rf g+x path-to-file-or-directory

#only others
chmod -Rf o+x path-to-file-or-directory

Ownership

FIRST FLAG IN ROLE IS FOR OWNER

SECOND FLAG IN ROLE IS FOR GROUP

How to change ownership of any file or directory

chown -Rf user:group path-to-file-or-directory

Change attribute read only

chattr +a file.txt
  • chattr: This is a command in Linux used to change file attributes on a file system.
  • +a: This option sets the “append-only” attribute on the specified file. When this attribute is set, the file can only be opened in append mode for writing. Existing data in the file cannot be modified or removed. This attribute is often used to prevent accidental deletion or modification of important files.
  • to revert use -a option for chattr

Install Jenkins on Ubuntu

sudo apt update
sudo apt install default-jdk default-jre
javac
wget -q -O - https://pkg.jenkins.io/debian/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"
sudo apt update
sudo apt install jenkins
sudo service jenkins start
sudo service jenkins status

Open Browser and hit following url

http://localhost:8080/

To view default initial password

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Install recommended Plugins. Make sure Git Plugin is installed

Username: admin
Password: Shift + 5
Full name: Administrator

Jenkins URL: http://localhost:8080/
Or
http://youripaddress:8080/
Or
http://yourdomain:8080/

Create New Item (New project for build)

Create release_project.sh file

rsync -av --exclude-from='.releaseignore' <src_dir_path> <dest_dir_path>

Add files and directories entries in .releaseignore file to skip rsync

node_modules
.git
private_documents

After creating freestyle project if you face any permission issue then try one of the following solutions

sudo su -
vim /etc/sudoers

Add following entry at end of file

#add jenkins as sudoer
jenkins        ALL=(ALL)       NOPASSWD: ALL

OR add user to the group

sudo usermod -a -G sudo jenkins

Ref:
medium.com
stackoverflow

vim cheat sheet

to open file

vim filename

copy/cut paste

yy => p
dd => p
v => select lines to copy => y => goto line where need to paste => p
v => select lines to cut => d => goto line where need to paste => p

to undo and redo

Esc
u u u u 

Esc
Ctrl + R Ctrl + R

to open file in read mode

vim -R filename

to open file on specific line number

vim filename +10

insert mode

i
o

escape / command mode

Esc

to write file

:w
:wq
:x

to minimize vim editor

Ctrl + Z

to see minimized files in vim

jobs

to see background job

bg

to open specific jobs or bring it to foreground

fg 1
fg +
fg -

search any word in vim editor

/word
/word\c
/word\C
#then press N or Shift + N

search and replace word

:%s/word/replacewith/i
:%s/word/replacewith/g

go to specific line

:10
:1
:$

show/hide line numbers in vim editor

:set number
:set nonumber

set tabstop

:set tabstop=4

set font color

:colorscheme murphy

vimdiff difference between 2 files

vimdiff file1 file2
#difference put
dp
#difference obtain
do