0% found this document useful (0 votes)
54 views106 pages

Linux Ulitimate Guide Cheat Sheet Edition Pd3ywv

The document is a comprehensive guide to Linux, covering its history, distributions, and key components. It includes sections on basic and advanced commands, shell scripting, system administration, and practical examples, along with cheat sheets for quick reference. The guide is structured into multiple parts, each focusing on different aspects of Linux, making it suitable for both beginners and advanced users.

Uploaded by

nithish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views106 pages

Linux Ulitimate Guide Cheat Sheet Edition Pd3ywv

The document is a comprehensive guide to Linux, covering its history, distributions, and key components. It includes sections on basic and advanced commands, shell scripting, system administration, and practical examples, along with cheat sheets for quick reference. The guide is structured into multiple parts, each focusing on different aspects of Linux, making it suitable for both beginners and advanced users.

Uploaded by

nithish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 106

Linux Ultimate Guide

PART I: Introduction to Linux .................................................................................................. 4


Chapter 1: Overview of Linux ........................................................................................... 4
Chapter 2: Getting Started with Linux ............................................................................... 6
Chapter 3: Linux Environment .......................................................................................... 9
PART II: Basic Linux Commands ........................................................................................... 12
Chapter 4: File and Directory Management..................................................................... 12
Cheat Sheet: ..................................................................................................................... 13
Chapter 5: File Permissions and Ownership .................................................................... 14
Linux File Permissions Cheat Sheet ................................................................................ 15
Chapter 6: Basic Text Processing .................................................................................... 17
Linux Text Processing Commands Cheat Sheet .............................................................. 18
Chapter 7: Networking Basics ......................................................................................... 19
Linux networking commands cheat sheet ........................................................................ 20
Chapter 8: System Monitoring and Process Management ............................................... 22
Linux System Monitoring Commands Cheat Sheet......................................................... 23
PART III: Advanced Linux Commands .................................................................................. 25
Chapter 9: Advanced File Operations .............................................................................. 25
Cheat Sheet for find, sed, awk, and diff Commands .............................................. 26
Chapter 10: Advanced Networking ................................................................................. 27
Cheat Sheet for SSH, ip, and netstat Commands ..................................................... 28
Chapter 11: System Administration ................................................................................. 29
Cheat Sheet for User Management, systemctl, and apt-get Commands ....... 30
Chapter 12: Advanced Scripting ...................................................................................... 31
Cheat Sheet for Bash Scripting and Bash Debugging Commands ................. 33
PART IV: Shell Scripting ........................................................................................................ 35
Chapter 13: Introduction to Shell Scripting ..................................................................... 35
Chapter 14: Getting Started with Shell Scripting ............................................................ 35
Chapter 15: Variables and User Input .............................................................................. 37
Shell Script Variables Cheat Sheet .................................................................................. 37
Chapter 16: Control Structures ........................................................................................ 39
Chapter 17: Functions ...................................................................................................... 41
Cheat Sheet: Shell Script Functions ................................................................................. 41
Chapter 18: Error Handling ............................................................................................. 42
Chapter 19: Advanced Scripting Techniques .................................................................. 43

1
Chapter 20: Automating System Tasks............................................................................ 45
Chapter 21: Real-world Examples and Case Studies ....................................................... 45
Cron Job Syntax Cheat Sheet ........................................................................................... 47
PART V: System Administration ............................................................................................ 48
Chapter 22: User and Group Management ...................................................................... 48
User Management, Group Management, and File Permissions Cheat Sheet .................. 49
Chapter 23: Package Management................................................................................... 50
APT, YUM, and SNAP Commands Cheat Sheet ............................................................ 51
Chapter 24: Disk Management ........................................................................................ 52
Disk Usage, Disk Partitioning, and Mounting Filesystems Commands Cheat Sheet ...... 53
Chapter 25: Network Management .................................................................................. 54
Network Configuration, Network Monitoring, and Nmap Commands Cheat Sheet ....... 55
Chapter 26: System Monitoring and Performance Tuning .............................................. 56
System Monitoring, Htop, and Performance Tuning Commands Cheat Sheet ............... 57
Chapter 27: Security and Access Control ........................................................................ 58
SSH Configuration, UFW, and SELinux Commands Cheat Sheet ................................. 59
Chapter 28: Case Studies and Real-world Applications .................................................. 61
PART VI: Advanced Topics .................................................................................................... 62
Chapter 29: Advanced File System Management ............................................................ 62
RAID and LVM Commands Cheat Sheet ........................................................................ 64
Chapter 30: Kernel Tuning and Optimization ................................................................. 65
sysctl Commands Cheat Sheet ......................................................................................... 66
Chapter 31: Virtualization and Containers ...................................................................... 67
Docker Commands Cheat Sheet ...................................................................................... 69
Chapter 32: High Availability and Load Balancing ........................................................ 70
Chapter 33: Security and Hardening ................................................................................ 72
SELinux Commands Cheat Sheet .................................................................................... 73
Chapter 34: Troubleshooting and Optimization .............................................................. 74
PART VII: Case Studies and Practical Examples.................................................................... 78
Chapter 36: Automating Daily System Tasks .................................................................. 78
Cron Expressions and Mail Command Options Cheat Sheet .......................................... 78
Chapter 37: Network Administration............................................................................... 79
iftop and nmap Command Cheat Sheet ........................................................................... 79
Chapter 38: Web Server Administration.......................................................................... 80
Chapter 39: Database Management ................................................................................. 82
Chapter 40: Security and Compliance ............................................................................. 84
Chapter 41: Advanced Scripting and Automation ........................................................... 86

2
PART VIII: Cheat Sheets and Resources ................................................................................ 88
Chapter 42: Basic Linux Commands Cheat Sheet ........................................................... 88
Chapter 43: Advanced Linux Commands Cheat Sheet .................................................... 89
Chapter 44: Shell Scripting Cheat Sheet .......................................................................... 89
Chapter 45: Network Administration Cheat Sheet .......................................................... 91
Chapter 46: System Administration Cheat Sheet............................................................. 91
Chapter 47: Security and Compliance Cheat Sheet ......................................................... 93
Chapter 48: Performance Tuning Cheat Sheet................................................................. 93
Chapter 49: Scripting and Automation Tools Cheat Sheet .............................................. 95
Chapter 50: Additional Resources ................................................................................... 96
PART IX: Appendices ............................................................................................................. 97
Chapter 51: Appendix A: Mathematical Foundations ..................................................... 97
Chapter 52: Appendix B: Shell Scripting Reference ....................................................... 98
Chapter 53: Appendix C: Linux Resources ..................................................................... 99
Chapter 54: Appendix D: Glossary of Terms ................................................................ 100

3
PART I: Introduction to Linux
Chapter 1: Overview of Linux

History and Evolution of Linux

• A Brief History from Unix to Linux


o Unix, developed in the 1960s and 1970s, laid the foundation for modern
operating systems.
o The GNU Project began in the 1980s with the goal of creating a free Unix-like
operating system.
o Linus Torvalds released the first version of the Linux kernel in 1991.
o Linux evolved through contributions from developers worldwide, becoming a
robust and versatile OS.

bash

# View the current Linux kernel version


uname -r

• Key Milestones in Linux Development


o 1991: Linux kernel version 0.01 released.
o 1992: Linux becomes GPL-licensed, attracting more developers.
o 2003: Release of Fedora, a community-supported distribution by Red Hat.
o 2012: Introduction of systemd, a system and service manager.

bash

# Check for systemd version


systemctl --version

• Linux Distributions and Their Purposes


o Distributions (distros) customize the Linux kernel with different software
packages and configurations to serve various needs.

Linux Distributions

• Popular Distributions: Ubuntu, Fedora, CentOS, Debian


o Ubuntu: User-friendly, ideal for desktops and beginners.
o Fedora: Cutting-edge features, often used by developers.
o CentOS: Stability-focused, commonly used in servers.
o Debian: Stability and freedom, a base for many other distributions.

bash

# Check the distribution name and version


lsb_release -a

4
• Differences and Use Cases for Each Distribution
o Ubuntu: Great for everyday use, multimedia, and general-purpose computing.
o Fedora: Preferred for development environments and testing new features.
o CentOS: Suited for enterprise environments requiring long-term support.
o Debian: Used in environments where stability and software freedom are
prioritized.

bash

# Example of installing a package in different distributions


# Ubuntu/Debian
sudo apt-get install <package_name>

# Fedora
sudo dnf install <package_name>

# CentOS
sudo yum install <package_name>

Linux Kernel

• Overview of the Linux Kernel


o The core component of the Linux operating system, managing hardware
resources and system processes.
• Key Components and Functions of the Kernel
o Process Management: Handles process creation, scheduling, and termination.
o Memory Management: Manages system memory, including allocation and
deallocation.
o File Systems: Provides access to various file systems like ext4, xfs, and btrfs.
o Device Drivers: Interfaces with hardware components like disks, networks,
and graphics cards.

bash

# Display the kernel ring buffer


dmesg

# View loaded kernel modules


lsmod

• How the Kernel Interacts with Hardware and Software


o The kernel acts as a bridge between applications and hardware, handling
input/output operations, resource allocation, and security enforcement.

bash

# Display hardware information


lshw

# Check CPU information


lscpu

5
Chapter 2: Getting Started with Linux

Introduction

This chapter covers the essentials of getting started with Linux, including installing and
configuring Linux on various platforms, understanding the Linux directory structure, and
basic navigation and file management commands.

Installing Linux

• Installing Ubuntu on Windows Using WSL


1. Enable WSL
▪ Open PowerShell as Administrator.
▪ Run the following command:

powershell

dism.exe /online /enable-feature /featurename:Microsoft-


Windows-Subsystem-Linux /all /norestart

2. Install a Linux Distribution


▪ Open Microsoft Store and search for "Ubuntu".
▪ Select Ubuntu and click "Get" to install it.
3. Set Up Ubuntu
▪ Launch Ubuntu from the Start menu.
▪ Follow the prompts to set up a new user account.

bash

# Verify WSL installation


wsl --list --verbose

• Installing Ubuntu from a USB Drive


1. Create a Bootable USB Drive
▪ Download the Ubuntu ISO from the official site.
▪ Use a tool like Rufus to create a bootable USB drive.
2. Boot from USB
▪ Restart your computer and enter the BIOS/UEFI settings.
▪ Set the USB drive as the first boot device.
▪ Save and exit the BIOS/UEFI settings.
3. Install Ubuntu
▪ Follow the on-screen instructions to install Ubuntu.

bash

# Example of using dd command to create a bootable USB on Linux


sudo dd if=~/Downloads/ubuntu.iso of=/dev/sdX bs=4M
sync

6
• Installing Ubuntu on a Virtual Machine
1. Download and Install VirtualBox
▪ Download VirtualBox from the official site.
▪ Install VirtualBox following the on-screen instructions.
2. Create a New Virtual Machine
▪ Open VirtualBox and click "New".
▪ Follow the prompts to create a new virtual machine, selecting Ubuntu
as the OS.
3. Install Ubuntu
▪ Start the virtual machine and follow the on-screen instructions to
install Ubuntu.

bash

# Check the version of VirtualBox


vboxmanage --version

Understanding the Linux Directory Structure

• Key Directories and Their Purposes


o /: Root directory.
o /bin: Essential user binaries.
o /boot: Boot loader files.
o /dev: Device files.
o /etc: Configuration files.
o /home: User home directories.
o /lib: Shared libraries.
o /media: Mount points for removable media.
o /mnt: Temporary mount points.
o /opt: Optional software packages.
o /proc: Process and kernel information.
o /root: Root user's home directory.
o /run: Application state files.
o /sbin: System binaries.
o /srv: Data for services.
o /tmp: Temporary files.
o /usr: User binaries and applications.
o /var: Variable files like logs.

bash

# List root directory contents


ls /

7
Basic Navigation and File Management Commands

• Navigation Commands
o pwd: Print working directory.
o ls: List directory contents.
o cd: Change directory.

bash

# Example of navigation commands


pwd
ls -l
cd /home/user

• File Management Commands


o cp: Copy files or directories.
o mv: Move or rename files or directories.
o rm: Remove files or directories.
o mkdir: Create a directory.

bash

# Example of file management commands


cp file1.txt file2.txt
mv file1.txt /home/user/documents/
rm file2.txt
mkdir new_directory

8
Chapter 3: Linux Environment

Introduction

This chapter explores the Linux environment, focusing on the shell as the primary interface
for interacting with the system. We'll discuss different shell types, shell features, scripting
basics, and how to write and execute simple scripts.

The Linux Shell

Introduction to Different Shells: Bash, Zsh, Fish

• Bash (Bourne Again Shell)


o Default shell for many Linux distributions.
o Combines features of the original Bourne shell (sh) with additional
improvements.
o Known for its wide adoption and extensive scripting capabilities.

bash

# Check current shell


echo $SHELL

o Example of Bash prompt customization:

bash

# Customize Bash prompt


PS1="\u@\h:\w$ "

• Zsh (Z Shell)
o An extended Bourne shell with many improvements.
o Features include powerful scripting language, enhanced completion, spell
checking, and themes.
o Highly customizable with frameworks like Oh My Zsh.

bash

# Install and switch to Zsh


sudo apt install zsh
chsh -s $(which zsh)

o Example of using Oh My Zsh:

bash

# Install Oh My Zsh
sh -c "$(curl -fsSL
https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/instal
l.sh)"

9
• Fish (Friendly Interactive Shell)
o User-friendly shell with helpful command suggestions and intuitive syntax.
o Focuses on user experience and ease of use.
o Includes features like autosuggestions and syntax highlighting out of the box.

bash

# Install Fish shell


sudo apt install fish

o Example of Fish shell usage:

fish

# Run Fish shell


fish
Shell Features and Scripting Basics

• Common Features of Shells


o Command history: Allows you to recall and edit previous commands.
o Command completion: Automatically completes command names and file
paths.
o Environment variables: Variables that affect the way processes run on a
system.

bash

# View command history in Bash


history
bash

# Enable command completion in Bash


bind 'set show-all-if-ambiguous on'

• Scripting Basics
o Shebang: The #! line at the beginning of a script specifying the interpreter.

bash

# Example shebang line for a Bash script


#!/bin/bash

o Variables: Store data for use in scripts.

bash

# Declare a variable in Bash


MY_VAR="Hello, World!"
echo $MY_VAR

o Control Structures: Conditional statements and loops.

bash

# If statement in Bash

10
if [ "$MY_VAR" == "Hello, World!" ]; then
echo "The variable is set to 'Hello, World!'"
fi
bash

# For loop in Bash


for i in {1..5}; do
echo "Number $i"
done
Writing and Executing Simple Scripts

• Creating a Simple Script


o Create a new file with a .sh extension.

bash

# Create a script file


nano my_script.sh

o Add the shebang and some commands.

bash

#!/bin/bash
echo "This is a simple script."
MY_VAR="Hello, World!"
echo $MY_VAR

• Making the Script Executable


o Change the file permissions to make it executable.

bash

# Make the script executable


chmod +x my_script.sh

• Executing the Script


o Run the script from the command line.

bash

# Execute the script


./my_script.sh

11
PART II: Basic Linux Commands
Chapter 4: File and Directory Management

• Navigating the Filesystem


o Commands: ls, cd, pwd

bash

# List files and directories


ls -l
# Change directory
cd /home/user
# Print working directory
pwd

o Explanation: ls lists files and directories, cd changes the current directory,


pwd prints the current directory path.

• Managing Files
o Commands: touch, cp, mv, rm

bash

# Create a new file


touch newfile.txt
# Copy a file
cp newfile.txt copyfile.txt
# Move or rename a file
mv copyfile.txt renamedfile.txt
# Remove a file
rm renamedfile.txt

o Explanation: touch creates a new file, cp copies a file, mv moves or renames a


file, rm removes a file.

• Managing Directories
o Commands: mkdir, rmdir, rm -r

bash

# Create a new directory


mkdir newdir
# Remove an empty directory
rmdir newdir
# Remove a directory and its contents
rm -r newdir

o Explanation: mkdir creates a new directory, rmdir removes an empty


directory, rm -r removes a directory and its contents.

12
Cheat Sheet:

Command Description Example


ls List directory contents ls <br> ls -l <br> ls -a
cd Change directory cd /path/to/directory <br> cd .. <br> cd ~
pwd Print working directory pwd
tree Display directory tree tree
structure
locate Find files by name locate filename
which Locate a command which ls
man Display manual page man ls
for a command
clear Clear the terminal clear
screen
touch Create a new empty touch newfile.txt
file
cp Copy files or cp source_file destination_file <br> cp -r
directories source_directory destination_directory
mv Move or rename files mv old_name new_name <br> mv
or directories /path/to/source /path/to/destination
rm Remove files or rm file.txt <br> rm -r directory_name
directories
cat Concatenate and cat file.txt
display file contents
more View file contents one more file.txt
screen at a time
less View file contents with less file.txt
backward movement
head Display the first lines head -n 10 file.txt
of a file
tail Display the last lines of tail -n 10 file.txt <br> tail -f
a file logfile.txt
chmod Change file chmod 755 script.sh
permissions
chown Change file owner and chown user:group file.txt
group
ln Create links between ln -s source_file symbolic_link
files
mkdir Create a new directory mkdir new_directory
rmdir Remove an empty rmdir empty_directory
directory
rm -r Remove a directory rm -r directory_to_remove
and its contents
find Search for files in a find /path -name filename
directory hierarchy
du Estimate file space du -sh /path/to/directory
usage
df Display free disk space df -h

13
Chapter 5: File Permissions and Ownership

• Understanding File Permissions


o Explanation of rwx (read, write, execute) permissions for user, group, and
others.
o Example: -rwxr-xr--
o Commands: ls -l

bash

# List files with detailed information


ls -l

• Changing File Permissions


o Commands: chmod

bash

# Change permissions using symbolic mode


chmod u+x myfile.txt
chmod g-w myfile.txt
# Change permissions using numeric mode
chmod 755 myfile.txt

o Explanation: chmod changes file permissions. Symbolic mode (e.g., u+x) adds
execute permission for the user. Numeric mode (e.g., 755) sets permissions
directly.
• Changing File Ownership
o Commands: chown, chgrp

bash

# Change file owner


chown newuser myfile.txt
# Change file group
chgrp newgroup myfile.txt

o Explanation: chown changes the owner of a file, chgrp changes the group of a
file.

14
Linux File Permissions Cheat Sheet

Command Description Example


ls -l List files with permissions ls -l
chmod Change file permissions chmod 755 file.txt
chown Change file owner chown user file.txt
chgrp Change file group chgrp group file.txt
umask Set default file permissions umask 022

Understanding File Permissions

Symbol Meaning
r Read permission
w Write permission
x Execute permission
- No permission

File Permission Representation

Symbol Description Example


- Regular file -rw-r--r--
d Directory drwxr-xr-x
l Symbolic link lrwxrwxrwx

Numerical Representation of Permissions

Number Permission Binary


0 No permission 000
1 Execute only 001
2 Write only 010
3 Write and execute 011
4 Read only 100
5 Read and execute 101
6 Read and write 110
7 Read, write, and execute 111

15
Changing Permissions with chmod

Command Description Example


chmod 755 Owner can read, write, and execute; chmod 755 file.txt
file
others can read and execute
chmod u+x Add execute permission for the owner chmod u+x script.sh
file
chmod g-w Remove write permission for the group chmod g-w file.txt
file
chmod o+r Add read permission for others chmod o+r file.txt
file
chmod a-r Remove read permission for all chmod a-r secret.txt
file
chmod -R 755 Change permissions recursively chmod -R 755
dir /path/to/directory

Changing Ownership with chown

Command Description Example


chown user file Change owner of a file chown alice file.txt
chown user:group Change owner and group of chown alice:developers
file file.txt
a file
chown -R user dir Change ownership chown -R bob
/path/to/directory
recursively

Changing Group Ownership with chgrp

Command Description Example


chgrp group Change group ownership of a chgrp developers file.txt
file
file
chgrp -R group Change group ownership chgrp -R staff
dir /path/to/directory
recursively

Setting Default Permissions with umask

Command Description Example


umask 022 Set default permissions for new files and directories umask 022
umask Display current umask setting umask

16
Chapter 6: Basic Text Processing

• Viewing File Contents


o Commands: cat, less, more

bash

# View file contents


cat myfile.txt
# View file contents one page at a time
less myfile.txt
more myfile.txt

o Explanation: cat displays file contents, less and more allow paginated
viewing.
• Searching Within Files
o Commands: grep

bash

# Search for a pattern in a file


grep "search_term" myfile.txt

o Explanation: grep searches for a specific pattern in a file.


• Manipulating Text Files
o Commands: head, tail, cut, sort

bash

# View the first 10 lines of a file


head myfile.txt
# View the last 10 lines of a file
tail myfile.txt
# Extract specific columns from a file
cut -d"," -f1,3 myfile.csv
# Sort the contents of a file
sort myfile.txt

o Explanation: head and tail view the beginning and end of a file, cut extracts
columns, sort sorts file contents.

17
Linux Text Processing Commands Cheat Sheet

Command Description Example


cat Concatenate and display file content cat file.txt
tac Concatenate and display file content in tac file.txt
reverse
nl Number lines in a file nl file.txt
more View file content interactively more file.txt
less View file content interactively with more less file.txt
options
head Display the first few lines of a file head -n 10 file.txt
tail Display the last few lines of a file tail -n 10 file.txt
cut Remove sections from each line of files cut -d',' -f1,3 file.csv
paste Merge lines of files paste file1.txt file2.txt
sort Sort lines of text files sort file.txt
uniq Report or omit repeated lines uniq file.txt
wc Print newline, word, and byte counts for wc -l file.txt
each file
grep Print lines matching a pattern grep "pattern" file.txt
sed Stream editor for filtering and transforming sed 's/old/new/g'
file.txt
text
awk Pattern scanning and processing language awk '{print $1, $3}'
file.txt
tr Translate or delete characters `echo "text"
fmt Format text to a specified width fmt -w 80 file.txt
pr Convert text files for printing pr -h "Header" file.txt
fold Wrap each input line to fit in specified fold -w 80 file.txt
width
join Join lines of two files on a common field join file1.txt file2.txt
comm Compare two sorted files line by line comm file1.txt file2.txt
diff Compare files line by line diff file1.txt file2.txt
col Filter reverse line feeds col -b file.txt
column Columnate input column -t file.txt
split Split a file into pieces split -l 1000 file.txt
csplit Split a file into context-determined pieces csplit file.txt
'/pattern/' '{*}'
expand Convert tabs to spaces expand file.txt
unexpand Convert spaces to tabs unexpand file.txt
strings Print the printable character sequences in strings file.bin
files
tee Read from standard input and write to `ls -l
standard output and files
xargs Build and execute command lines from `find . -type f -name "*.txt"
standard input

18
Chapter 7: Networking Basics

• Checking Network Configuration


o Commands: ifconfig, ip

bash

# Display network configuration


ifconfig
ip addr show

o Explanation: ifconfig and ip show network interface configuration.


• Testing Network Connectivity
o Commands: ping, traceroute

bash

# Ping a host
ping google.com
# Trace the route to a host
traceroute google.com

o Explanation: ping tests connectivity to a host, traceroute shows the route


taken to a host.
• Transferring Files
o Commands: scp, rsync

bash

# Securely copy a file to a remote host


scp myfile.txt user@remote_host:/path/to/destination
# Synchronize files between local and remote hosts
rsync -avz myfile.txt user@remote_host:/path/to/destination

o Explanation: scp securely copies files, rsync synchronizes files between


hosts.

19
Linux networking commands cheat sheet

Command Description Example


ping Send ICMP ping google.com
ECHO_REQUEST to
network hosts
ifconfig Configure network ifconfig eth0
interfaces
ip Show/manipulate routing, ip addr show
devices, policy routing,
and tunnels
traceroute Print the route packets take traceroute google.com
to the network host
netstat Print network connections, netstat -tuln
routing tables, interface
statistics, masquerade
connections, and multicast
memberships
ss Another utility to ss -tuln
investigate sockets
nslookup Query Internet name nslookup google.com
servers interactively
dig DNS lookup dig google.com
route Show/manipulate the IP route -n
routing table
hostname Show or set the system’s hostname
hostname
whois Perform WHOIS lookups whois google.com
curl Transfer data from or to a curl http://example.com
server
wget Non-interactive network wget http://example.com/file.zip
downloader
ftp File Transfer Protocol ftp ftp.example.com
scp Secure copy (remote file scp file.txt
user@remote:/path/to/destination
copy program)
rsync Remote file and directory rsync -avz file.txt
user@remote:/path/to/destination
synchronization
nmap Network exploration tool nmap -sP 192.168.1.0/24
and security/port scanner
tcpdump Command-line packet tcpdump -i eth0
analyzer
ethtool Display or change ethtool eth0
Ethernet device settings
nmcli Command-line client for nmcli device status
NetworkManager
iwconfig Configure wireless iwconfig wlan0
network interfaces

20
wpa_supplicant Wi-Fi Protected Access wpa_supplicant -B -i wlan0 -c
/etc/wpa_supplicant.conf
client and IEEE 802.1X
supplicant
ip a Show all network ip a
interfaces and their
addresses
ip link Show/manage network ip link show
devices
ip route Show/manipulate the ip route show
routing table
netcat (nc) Utility for reading from nc -zv 192.168.1.1 22
and writing to network
connections using TCP or
UDP
arp Manipulate the system arp -a
ARP cache
mtr Network diagnostic tool mtr google.com
combining ping and
traceroute
iptables Administration tool for iptables -L
IPv4 packet filtering and
NAT

21
Chapter 8: System Monitoring and Process Management

• Monitoring System Performance


o Commands: top, htop, vmstat

bash

# Display system performance statistics


top
htop
vmstat

o Explanation: top and htop show real-time system performance, vmstat


provides system performance statistics.
• Managing Processes
o Commands: ps, kill, pkill

bash

# Display currently running processes


ps aux
# Kill a process by PID
kill 1234
# Kill processes by name
pkill process_name

o Explanation: ps lists processes, kill terminates a process by its PID, pkill


terminates processes by name.
• Scheduling Tasks
o Commands: crontab, at

bash

# Edit crontab file to schedule tasks


crontab -e
# Schedule a one-time task
echo "mycommand" | at 10:00

o Explanation: crontab schedules recurring tasks, at schedules one-time tasks.

22
Linux System Monitoring Commands Cheat Sheet

Command Description Example


top Display real-time system information top
htop Enhanced version of top htop
vmstat Report virtual memory statistics vmstat 5
iostat Report CPU and I/O statistics iostat
free Display memory usage free -h
df Report file system disk space usage df -h
du Estimate file space usage du -sh
/home/user
ps Report a snapshot of current processes ps aux
pstree Display a tree of processes pstree
netstat Network statistics netstat -tuln
ss Another utility to investigate sockets ss -tuln
iftop Display bandwidth usage on an interface iftop
nload Display network usage nload
sar Collect, report, or save system activity sar -u 5
uptime Show how long the system has been running uptime
dstat Versatile resource statistics dstat
glances Cross-platform system monitoring tool glances
mpstat Report CPU usage mpstat
pidstat Report statistics per PID pidstat
iotop Display I/O usage by processes iotop
ncdu Disk usage analyzer with an ncurses interface ncdu
atop Advanced system and process monitor atop
lsof List open files lsof
watch Execute a program periodically, showing output watch -n 1 df -h
fullscreen
sysstat Collection of performance monitoring tools sysstat

23
Additional Elements

Case Studies

• Case Study: Automating File Backup


o Write a script to automate daily file backups using rsync.

bash

#!/bin/bash
# Daily backup script
rsync -avz /source/directory /backup/directory

Diagrams

• Linux Filesystem Hierarchy


o Search: "Linux filesystem hierarchy diagram"
• Network Configuration Diagram
o Search: "Linux network configuration diagram"
• Process Management Flowchart
o Search: "Linux process management flowchart"

Summary

This expanded section on Basic Linux Commands provides comprehensive coverage of


essential Linux operations, from file and directory management to system monitoring and
process management. Each chapter includes practical examples, cheat sheets, and relevant
diagrams to facilitate learning and application.

24
PART III: Advanced Linux Commands
Chapter 9: Advanced File Operations

• Finding Files and Directories


o Commands: find, locate

bash

# Find files by name


find / -name "myfile.txt"
# Find files by type
find / -type d -name "mydir"
# Locate files using the locate database
locate myfile.txt

o Explanation: find searches for files and directories based on various criteria,
locate searches for files using a prebuilt database.

• Manipulating File Contents


o Commands: sed, awk

bash

# Replace text in a file using sed


sed 's/oldtext/newtext/g' myfile.txt
# Extract and manipulate text using awk
awk '{print $1, $3}' myfile.txt

o Explanation: sed performs text replacements in files, awk is a powerful text


processing tool for extracting and manipulating text.

• Comparing Files
o Commands: diff, cmp

bash

# Compare two files


diff file1.txt file2.txt
# Compare binary files
cmp file1.bin file2.bin

o Explanation: diff shows differences between text files, cmp compares binary
files byte by byte.

25
Cheat Sheet for find, sed, awk, and diff Commands

Command Description Example


find Search for files in a find /home/user -name
"*.txt"
directory hierarchy
find /path -type f Find all files find /home/user -type f
find /path -type d Find all directories find /home/user -type d
find /path -size +100M Find files larger than find /home/user -size
+100M
100MB
find /path -mtime -7 Find files modified in the find /home/user -mtime -7
last 7 days
find /path -exec Execute a command on find /home/user -name
command {} \; "*.log" -exec rm {} \;
each file found
find /path -perm 644 Find files with specific find /home/user -perm 644
permissions
sed 's/old/new/g' file Replace all occurrences of sed 's/foo/bar/g'
file.txt
'old' with 'new' in a file
sed -n '1,5p' file Print lines 1 to 5 of a file sed -n '1,5p' file.txt
sed 'd' file Delete lines matching the sed '/pattern/d' file.txt
pattern
awk '{print $1}' file Print the first column of a awk '{print $1}' file.txt
file
awk '/pattern/ {print Print lines matching the awk '/foo/ {print $0}'
$0}' file file.txt
pattern
awk 'BEGIN {FS=":"} Set field separator and awk 'BEGIN {FS=":"}
{print $1}' file {print $1}' /etc/passwd
print first column
diff file1 file2 Compare two files line by diff file1.txt file2.txt
line
diff -u file1 file2 Unified diff format diff -u file1.txt
file2.txt
diff -r dir1 dir2 Compare directories diff -r dir1 dir2
recursively
cmp file1 file2 Compare two files byte by cmp file1.txt file2.txt
byte
comm file1 file2 Compare two sorted files comm file1.txt file2.txt
line by line
sdiff file1 file2 Side-by-side comparison sdiff file1.txt file2.txt
of two files

26
Chapter 10: Advanced Networking

• Network Configuration
o Commands: ifconfig, ip

bash

# Display network interfaces and configuration


ifconfig
ip addr show
# Assign an IP address to an interface
sudo ip addr add 192.168.1.100/24 dev eth0

o Explanation: ifconfig and ip commands are used for configuring network


interfaces.

• Network Troubleshooting
o Commands: netstat, ss

bash

# Display network connections and routing tables


netstat -tuln
# Display socket statistics
ss -tuln

o Explanation: netstat and ss provide information about network connections


and listening sockets.

• Secure Shell (SSH)


o Commands: ssh, scp

bash

# Connect to a remote host


ssh user@remote_host
# Copy files securely between hosts
scp user@remote_host:/path/to/remote/file
/path/to/local/destination

o Explanation: ssh allows secure access to remote systems, scp securely copies
files between systems.

27
Cheat Sheet for SSH, ip, and netstat Commands

Command Description Example


ssh user@host Connect to a ssh [email protected]
remote host
ssh -i /path/to/key user@host Connect using a ssh -i ~/.ssh/id_rsa
specific key [email protected]
ssh -p port user@host Connect using a ssh -p 2222
specific port [email protected]
ssh -L Local port ssh -L
local_port:host:remote_port forwarding 8080:localhost:80
user@host [email protected]
ssh -R Remote port ssh -R
remote_port:host:local_port forwarding 8080:localhost:80
user@host [email protected]
ssh-copy-id user@host Copy your public ssh-copy-id
key to a remote [email protected]
host
ip addr show Display all IP ip addr show
addresses and
interfaces
ip link set dev eth0 up Bring an interface ip link set dev eth0
up up
ip link set dev eth0 down Bring an interface ip link set dev eth0
down down
ip route show Show the routing ip route show
table
ip route add default via Add a default ip route add default
gateway gateway via 192.168.1.1
ip link show Display all network ip link show
interfaces
ip neigh show Display the ARP ip neigh show
table
ip addr add IP/dev Add an IP address ip addr add
to an interface 192.168.1.2/24 dev
eth0
netstat -tuln Show listening netstat -tuln
ports
netstat -r Display the routing netstat -r
table
netstat -i Show network netstat -i
interfaces
netstat -s Display network netstat -s
statistics
netstat -anp Show all netstat -anp
connections and
listening ports
netstat -rn Display the kernel netstat -rn
routing table in
numeric form
netstat -g Display multicast netstat -g
group memberships

28
Chapter 11: System Administration

• Managing Users and Groups


o Commands: useradd, usermod, groupadd

bash

# Add a new user


sudo useradd newuser
# Modify an existing user
sudo usermod -aG sudo newuser
# Add a new group
sudo groupadd newgroup

o Explanation: useradd creates a new user, usermod modifies existing user


accounts, groupadd creates a new group.

• Managing Services and Daemons


o Commands: systemctl, service

bash

# Start a service
sudo systemctl start nginx
# Stop a service
sudo systemctl stop nginx
# Check status of a service
sudo systemctl status nginx

o Explanation: systemctl and service commands manage system services and


daemons.

• Package Management
o Commands: apt-get, yum

bash

# Update package lists


sudo apt-get update
# Install a package
sudo apt-get install vim
# Remove a package
sudo apt-get remove vim

o Explanation: apt-get and yum are package management tools for installing,
updating, and removing software packages.

29
Cheat Sheet for User Management, systemctl, and apt-get Commands

Command Description Example


useradd username Add a new user useradd alice
userdel username Delete a user userdel alice
usermod -aG group Add a user to a group usermod -aG sudo alice
username
passwd username Change a user's password passwd alice
chage -l username List a user's password aging chage -l alice
information
groupadd groupname Add a new group groupadd developers
groupdel groupname Delete a group groupdel developers
id username Display user ID and group ID id alice
information
systemctl start Start a service systemctl start
service apache2
systemctl stop service Stop a service systemctl stop apache2
systemctl restart Restart a service systemctl restart
service apache2
systemctl status Check the status of a service systemctl status
service apache2
systemctl enable Enable a service to start on systemctl enable
service apache2
boot
systemctl disable Disable a service from starting systemctl disable
service apache2
on boot
systemctl is-active Check if a service is active systemctl is-active
service apache2
systemctl is-enabled Check if a service is enabled systemctl is-enabled
service apache2
systemctl list-units - List all active services systemctl list-units -
-type=service -type=service
apt-get update Update the package list apt-get update
apt-get upgrade Upgrade all installed packages apt-get upgrade
apt-get install Install a package apt-get install vim
package
apt-get remove package Remove a package apt-get remove vim
apt-get autoremove Remove unnecessary packages apt-get autoremove
apt-get clean Clean up the local repository apt-get clean
of retrieved package files
apt-cache search Search for a package in the apt-cache search vim
package
repository
apt-cache show package Display package details apt-cache show vim

30
Chapter 12: Advanced Scripting

• Bash Scripting
o Example Script: Backup Script

bash

#!/bin/bash
# Backup script

# Define backup source and destination


SRC="/home/user/data"
DEST="/backup/data"

# Perform backup
rsync -avz $SRC $DEST

o Explanation: This script uses rsync to perform a backup of user data.

• Using Variables and Functions


o Example Script: Using Variables

bash

#!/bin/bash
# Script to demonstrate variables

NAME="John Doe"
echo "Hello, $NAME"

o Explanation: This script demonstrates the use of variables in a bash script.


• Control Structures and Loops
o Example Script: Loop Example

bash

#!/bin/bash
# Loop example

for i in {1..5}
do
echo "Iteration $i"
done

o Explanation: This script uses a for loop to iterate and print numbers from 1 to
5.

31
• Debugging Scripts
o Commands: bash -x, set -x

bash

# Run script with debugging enabled


bash -x myscript.sh
# Enable debugging within a script
set -x
# Your script code here
set +x

o Explanation: bash -x runs a script in debug mode, set -x enables debugging


within a script, and set +x disables it.

Additional Elements

Case Studies

• Case Study: Automating Server Maintenance


o Write a script to automate server updates and reboots.

bash

#!/bin/bash
# Server maintenance script

# Update package lists


sudo apt-get update
# Upgrade packages
sudo apt-get upgrade -y
# Reboot the server
sudo reboot

Diagrams

• Advanced Networking Diagram


o Search: "Linux advanced networking diagram"
• User and Group Management Workflow
o Search: "Linux user management workflow"
• Service Management Flowchart
o Search: "systemctl flowchart"

32
Cheat Sheet for Bash Scripting and Bash Debugging Commands

Command Description Example


#!/bin/bash Shebang to specify #!/bin/bash
the script's
interpreter
echo "message" Print a message to echo "Hello, World!"
the terminal
variable=value Define a variable name="Alice"
echo $variable Access a variable echo $name
read variable Read user input into read user_input
a variable
if [ condition ]; then If statement if [ $age -ge 18 ]; then echo
... fi "Adult"; fi
for var in list; do ... For loop for i in {1..5}; do echo $i;
done done
while [ condition ]; do While loop while [ $count -le 5 ]; do
... done echo $count;
count=$((count+1)); done
case variable in ... Case statement case $1 in start) echo
esac "Starting";; stop) echo
"Stopping";; esac
function name { ... } Define a function function greet { echo "Hello,
$1"; }
$? Exit status of the last if [ $? -eq 0 ]; then echo
"Success"; fi
command
$(command) Command today=$(date)
substitution
$(<file) Read a file into a content=$(<file.txt)
variable
> file Redirect output to a echo "Hello" > output.txt
file
>> file Append output to a echo "World" >> output.txt
file
2> file Redirect error output ls /nonexistent 2> error.txt
to a file
&> Redirect both stdout command &> output.txt
and stderr
set -e Exit immediately if a set -e
command exits with
a non-zero status
set -u Treat unset variables set -u
as an error when
substituting
set -x Print commands and set -x
their arguments as
they are executed
set -v Print shell input lines set -v
as they are read
trap 'commands' SIGNAL Trap signals and trap 'echo "Interrupted!"'
INT
execute commands

33
PS4='+ Customize the PS4='+ $BASH_SOURCE:$LINENO:
$BASH_SOURCE:$LINENO: ' '
debugging prompt
bash -x script.sh Execute a script in bash -x myscript.sh
debug mode
bash -n script.sh Check a script for bash -n myscript.sh
syntax errors without
executing it
exec 3>&1 4>&2 Save file descriptors exec 3>&1 4>&2
1 and 2
exec 1>output.log Redirect stdout and exec 1>output.log 2>error.log
2>error.log
stderr to files
exec 1>&3 2>&4 Restore file exec 1>&3 2>&4
descriptors 1 and 2
select var in list; do Select loop for select opt in Start Stop
... done Exit; do echo $opt; done
creating simple
menus

Summary

This expanded section on Advanced Linux Commands covers a wide range of advanced
topics, from sophisticated file operations and networking to system administration and
advanced scripting. Each chapter includes detailed explanations, practical examples, cheat
sheets, and diagrams to enhance understanding and application.

34
PART IV: Shell Scripting
Chapter 13: Introduction to Shell Scripting

• What is Shell Scripting?


o Overview: Shell scripting is a way to automate tasks in a Unix/Linux
environment by writing scripts in a shell language like bash.
• Benefits of Shell Scripting
o Benefits include task automation, time savings, repeatability, and error
reduction.

Chapter 14: Getting Started with Shell Scripting

• Writing Your First Script


o Example Script: Hello World

bash

#!/bin/bash
echo "Hello, World!"

o Explanation: This is the simplest shell script that prints "Hello, World!" to the
terminal.
• Making a Script Executable
o Command: chmod

bash

chmod +x script.sh

o Explanation: chmod +x makes the script executable.

35
Basic Shell Commands Cheat Sheet and Basic Shell Script Structure

Command Example Description


Basic Shell
Commands
List Directory ls -l Lists files and directories with
Contents detailed information.
Change Directory cd /path/to/directory Changes the current working
directory.
Print Working pwd Displays the current directory
Directory path.
Copy File cp source.txt Copies a file from source to
destination.txt
destination.
Move/Rename File mv oldname.txt newname.txt Moves or renames a file.
Remove File rm file.txt Deletes a specified file.
Remove Directory rm -r directory Deletes a directory and its
contents.
Create Directory mkdir new_directory Creates a new directory.
Display File cat file.txt Displays the contents of a file.
Contents
Search in Files grep "pattern" file.txt Searches for a pattern in a file.
Basic Shell Script
Structure
Shebang Line #!/bin/bash Specifies the script interpreter.
Comment # This is a comment Adds comments to the script.
Variable VAR=value Assigns a value to a variable.
Assignment
Echo Statement echo "Hello, World!" Prints text to the terminal.
Read User Input read VAR Reads input from the user.
If Statement if [ condition ]; then Executes commands based on
commands; fi
a condition.
For Loop for i in {1..5}; do Iterates over a sequence of
commands; done
values.
While Loop while [ condition ]; do Repeats commands while a
commands; done
condition is true.
Function Definition function name { commands; } Defines a reusable function.
Function Call name Calls a defined function.
Exit Script exit 0 Exits the script with a status
code.

36
Chapter 15: Variables and User Input

• Using Variables
o Example Script: Simple Variable

bash

#!/bin/bash
NAME="John"
echo "Hello, $NAME"

o Explanation: Variables store data that can be used later in the script.

• Reading User Input


o Example Script: Read User Input

bash

#!/bin/bash
echo "Enter your name:"
read NAME
echo "Hello, $NAME"

o Explanation: read allows you to take input from the user.

Shell Script Variables Cheat Sheet

Command Description Example


variable=value Define a variable name="Alice"
echo $variable Access a variable echo $name
${variable} Access a variable echo ${name}
readonly variable Make a variable readonly PI=3.14
read-only
unset variable Remove a unset name
variable
variable=$(command) Command today=$(date)
substitution
variable=$(<file) Read file into content=$(<file.txt)
variable
var1=value1; var2=value2 Multiple variable first="John"; last="Doe"
assignment in
one line
${#variable} Length of a echo ${#name}
variable
${variable:offset:length} Substring echo ${name:0:3}
extraction

37
${variable/pattern/replacement} String echo ${name/Al/Bob}
replacement
export variable Export a variable export
PATH=$PATH:/new/path
to the
environment
$variable Access a variable echo $name
$variable/default Use default value echo ${name:-"Default"}
if variable is
unset
${variable:=default} Set variable to echo ${name:="Default"}
default if unset
${variable:+replacement} Use replacement echo ${name:+"Hello,
$name"}
if variable is set
${variable:?error_message} Display error echo ${name:?"Name is
not set"}
message if
variable is unset
`var=$(echo $variable tr 'a-z' 'A-Z')` Transform variable content to
uppercase
$1, $2, ... Positional echo "First arg: $1"
parameters
(arguments)
$@ All arguments echo "All args: $@"
$# Number of echo "Number of args:
$#"
arguments
$? Exit status of last echo "Last command exit
status: $?"
command
$$ Process ID of the echo "Script PID: $$"
script
$! PID of the last sleep 10 & echo "Last
background job PID: $!"
background job
$_ Last argument of echo "Last argument: $_"
the previous
command

38
Chapter 16: Control Structures

• Conditional Statements
o Example Script: If-Else Statement

bash

#!/bin/bash
if [ $1 -gt 100 ]
then
echo "The number is greater than 100"
else
echo "The number is less than or equal to 100"
fi

o Explanation: if statements allow you to execute code based on conditions.

• Loops
o Example Script: For Loop

bash

#!/bin/bash
for i in {1..5}
do
echo "Iteration $i"
done

o Example Script: While Loop

bash

#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 5 ]
do
echo "Counter: $COUNTER"
let COUNTER=COUNTER+1
done

o Explanation: Loops allow you to repeat code multiple times.

39
Shell Script Conditional Statements Cheat Sheet

Command Example Description


if [ condition ]; if [ -f "file.txt" ]; then Checks if a file exists and
then ... fi echo "File exists"; fi
prints a message.
if [ condition ]; if [ -d "dir" ]; then echo Checks if a directory
then ... else ... fi "Directory exists"; else
exists and prints
echo "Directory does not
exist"; fi appropriate messages.
if [ condition1 ]; if [ $var -eq 1 ]; then Checks multiple
then ... elif [ echo "Var is 1"; elif [
conditions and executes
condition2 ]; then $var -eq 2 ]; then echo
... fi "Var is 2"; fi corresponding
commands.
case case $var in 1) echo "Var Simplifies checking a
is 1";; 2) echo "Var is
variable against multiple
2";; esac
values.
&& [ -f "file.txt" ] && echo Executes the command
"File exists"
after && only if the
condition is true.
` `

Shell Script Loops Cheat Sheet

Command Example Description


for
loop for i in {1..5}; do echo "Welcome $i"; Iterates over a range of
done
numbers and prints a
message.
for loop for i in apple banana cherry; do echo Iterates over a list of items
"Fruit: $i"; done
(list) and prints each one.
while loop while [ $count -le 5 ]; do echo Executes commands as
"Count: $count"; count=$((count + 1));
long as a condition is true.
done
until loop until [ $count -gt 5 ]; do echo Executes commands until a
"Count: $count"; count=$((count + 1));
condition is true.
done
break for i in {1..5}; do if [ $i -eq 3 ]; Exits the loop when the
then break; fi; echo "Welcome $i";
done
condition is met.
continue for i in {1..5}; do if [ $i -eq 3 ]; Skips the current iteration
then continue; fi; echo "Welcome $i";
done
and continues with the
next.

40
Chapter 17: Functions

• Creating and Using Functions


o Example Script: Basic Function

bash

#!/bin/bash
greet() {
echo "Hello, $1"
}
greet "World"

o Explanation: Functions encapsulate code for reuse and better organization.

Cheat Sheet: Shell Script Functions

Shell Script Functions Cheat Sheet

Command Example Description


Defining a my_function() { echo "Hello, Defines a function named
World!"; }
function my_function that prints
"Hello, World!".
Calling a my_function Calls the previously defined
function function my_function.
Function with greet() { echo "Hello, $1!"; } Defines a function greet that
parameters takes a parameter and prints
"Hello, [parameter]".
Calling a greet "Alice" Calls the greet function with
function with "Alice" as the parameter.
parameters
Function sum() { return $(($1 + $2)); } Defines a function sum that
returning a adds two numbers and returns
value the result.
Capturing a sum 2 3; result=$?; echo Calls the sum function with 2
$result
function's and 3 as parameters, captures
return value the return value, and prints it.
Local variables my_func() { local var="I'm Defines a function my_func
local"; echo $var; }
in a function with a local variable var.
Recursive factorial() { if [ $1 -le 1 ]; Defines a recursive function
then echo 1; else echo $(( $1 *
function factorial that computes the
$(factorial $(( $1 - 1 ))) ));
fi } factorial of a number.
Function with a is_even() { if [ $(($1 % 2)) - Defines a function is_even
eq 0 ]; then return 0; else
return that checks if a number is
return 1; fi }
statement even and returns 0 if true, 1 if
false.

41
Chapter 18: Error Handling

• Basic Error Handling


o Example Script: Error Handling with Exit Status

bash

#!/bin/bash
cp /nonexistent/file /tmp/
if [ $? -ne 0 ]; then
echo "Error occurred while copying file"
fi

o Explanation: $? checks the exit status of the last command.

Shell Script Error Handling Cheat Sheet

Command Example Description


Exit on error set -e Terminates the script if any command
returns a non-zero status.
Trap errors trap 'echo "Error at Runs a command (e.g., printing an
line $LINENO"' ERR
error message) when any command
returns a non-zero status.
Try-catch-like `command
error handling
Using if for error if ! command; then Executes a command and, if it fails,
echo "Command failed";
handling fi
prints an error message.
Error handling in `my_function() { command
functions
Custom error `command
messages
Exit status of the command; echo $? Executes a command and prints the
last command exit status of the last command.
Using && for command1 && command2 Executes command2 only if command1
conditional succeeds.
execution
Using ` ` for conditional execution

42
Chapter 19: Advanced Scripting Techniques

• Working with Arrays


o Example Script: Array Example

bash

#!/bin/bash
FRUITS=("Apple" "Banana" "Cherry")
for FRUIT in "${FRUITS[@]}"
do
echo "Fruit: $FRUIT"
done

o Explanation: Arrays store multiple values in a single variable.

• String Manipulation
o Example Script: String Operations

bash

#!/bin/bash
STR="Hello World"
echo "Length: ${#STR}"
echo "Substring: ${STR:0:5}"

o Explanation: String manipulation techniques to extract and modify substrings.

Shell Script Arrays Cheat Sheet

Command Example Description


Declare an array=(val1 val2 val3) Declares an array with values
array val1, val2, val3.
Access array ${array[0]} Accesses the first element of the
element array.
All elements ${array[@]} Accesses all elements of the
array.
Length of array ${#array[@]} Returns the number of elements
in the array.
Add element array+=(val4) Adds val4 to the array.
Iterate over for i in "${array[@]}"; do Iterates over and prints each
echo $i; done
array element in the array.
Remove unset array[1] Removes the second element
element from the array.
Replace array[1]=newval Replaces the second element
element with newval.

43
Shell Script String Operations Cheat Sheet

Command Example Description


String length ${#string} Returns the length of the
string.
Substring ${string:pos:len} Extracts a substring from
extraction string starting at pos with
length len.
Substring ${string/old/new} Replaces the first
replacement occurrence of old with new
in string.
Global substring ${string//old/new} Replaces all occurrences of
replacement old with new in string.
Check if string [[ $string == *"substring"* ]] Checks if string contains
contains substring substring.
Concatenate newstring="${string1}${string2}" Concatenates string1 and
strings string2.
Convert to ${string^^} Converts all characters in
uppercase string to uppercase.
Convert to ${string,,} Converts all characters in
lowercase string to lowercase.
Trim leading ${string# } Removes leading
whitespace whitespace from string.
Trim trailing ${string% } Removes trailing
whitespace whitespace from string.

44
Chapter 20: Automating System Tasks

• Scheduled Tasks with Cron


o Example Script: Cron Job

bash

# Run backup script at 2am every day


0 2 * * * /path/to/backup.sh

o Explanation: Cron jobs automate the execution of scripts at specified times.

• Automating Backups
o Example Script: Backup Script

bash

#!/bin/bash
tar -czf /backup/home_$(date +%F).tar.gz /home/user

o Explanation: Automates the backup of user home directory.

Chapter 21: Real-world Examples and Case Studies

• Case Study: Server Maintenance Script


o Example Script: Server Update and Reboot

bash

#!/bin/bash
sudo apt-get update && sudo apt-get upgrade -y
sudo reboot

o Explanation: Script to update and reboot a server.

45
Server maintenance workflow diagram

• Case Study: Log File Management


o Example Script: Log Rotation

bash

#!/bin/bash
LOG_FILE="/var/log/myapp.log"
mv $LOG_FILE $LOG_FILE.$(date +%F)
touch $LOG_FILE

o Explanation: Rotates log files to prevent them from growing indefinitely.

Diagram: Log Rotation Workflow

46
Cron Job Syntax Cheat Sheet

Command Example Description


Schedule a job * * * * * /path/to/script.sh Executes script.sh every
minute.
Minute field 30 * * * * /path/to/script.sh Executes script.sh at minute
30 of every hour.
Hour field 0 8 * * * /path/to/script.sh Executes script.sh at 08:00
AM every day.
Day of month 0 8 1 * * /path/to/script.sh Executes script.sh at 08:00
field AM on the first day of every
month.
Month field 0 8 1 1 * /path/to/script.sh Executes script.sh at 08:00
AM on January 1st.
Day of week field 0 8 * * 1 /path/to/script.sh Executes script.sh at 08:00
AM every Monday.
List all cron jobs crontab -l Lists all the cron jobs for the
current user.
Edit cron jobs crontab -e Opens the cron job editor for the
current user.
Remove all cron crontab -r Removes all cron jobs for the
jobs current user.
Specific user cron crontab -u username -e Edits the cron jobs for username.
jobs
Redirect output to * * * * * /path/to/script.sh Executes script.sh every
file > /path/to/logfile 2>&1 minute and logs output to
logfile.
Environment SHELL=/bin/bash\n* * * * * Uses /bin/bash shell to execute
variable /path/to/script.sh the cron job.
Schedule multiple * * * * * command1 && Executes command1 and then
commands command2 command2 every minute.
Use ranges 0-30/5 * * * * Executes script.sh every 5
/path/to/script.sh minutes between minutes 0 and 30.
Use step values */10 * * * * Executes script.sh every 10
/path/to/script.sh minutes.
Use specific 0 0 1 1,6,12 * Executes script.sh at midnight
months /path/to/script.sh on the 1st of January, June, and
December.
Use specific days 0 0 * * MON,WED,FRI Executes script.sh at midnight
of week /path/to/script.sh on Monday, Wednesday, and
Friday.

Summary

This expanded section on Shell Scripting covers fundamental to advanced topics with
practical examples and scripts. Each chapter includes detailed explanations, commands, cheat
sheets, diagrams, and real-world case studies to ensure a thorough understanding and
application of shell scripting.

47
PART V: System Administration
Chapter 22: User and Group Management

• Creating and Managing Users


o Example Script: Create a New User

bash

#!/bin/bash
sudo useradd -m -s /bin/bash newuser
sudo passwd newuser

o Explanation: useradd creates a new user with a home directory and bash
shell.

• Creating and Managing Groups


o Example Script: Create a New Group

bash

#!/bin/bash
sudo groupadd newgroup
sudo usermod -aG newgroup newuser

o Explanation: groupadd creates a new group and usermod -aG adds the user to
the group.

• Managing Permissions
o Example Script: Change File Permissions

bash

#!/bin/bash
sudo chmod 755 /path/to/file
sudo chown user:group /path/to/file

o Explanation: chmod changes file permissions and chown changes file


ownership.

48
User Management, Group Management, and File Permissions Cheat Sheet

Command Example Description


User Management
Add user sudo adduser Adds a new user with username.
username
Delete user sudo deluser Deletes the user with username.
username
Change user password sudo passwd username Changes the password for
username.
Switch user su - username Switches to the user username.
List all users cut -d: -f1 Lists all users on the system.
/etc/passwd
Group Management
Add group sudo addgroup Adds a new group with groupname.
groupname
Delete group sudo delgroup Deletes the group with groupname.
groupname
Add user to group sudo usermod -aG Adds username to groupname.
groupname username
Remove user from sudo gpasswd -d Removes username from
username groupname
group groupname.
List all groups cut -d: -f1 Lists all groups on the system.
/etc/group
File Permissions
Change file chmod 755 filename Sets the permissions of filename
permissions to 755 (rwxr-xr-x).
Change file owner sudo chown username Changes the owner of filename to
filename
username.
Change file group sudo chown Changes the group of filename to
:groupname filename
groupname.
View file permissions ls -l filename Displays the permissions of
filename.
Set read, write, execute chmod u+rwx filename Adds read, write, and execute
for owner permissions to the owner of
filename.
Remove read chmod g-r filename Removes read permission for the
permission for group group of filename.
Add execute chmod o+x filename Adds execute permission for others
permission for others for filename.
Set permissions chmod -R 755 Sets permissions to 755 for all files
directory
recursively and directories in directory.
Set permissions using chmod u=rw,go=r Sets read/write for owner, read-only
filename
symbolic notation for group and others for filename.

49
Chapter 23: Package Management

• Managing Packages with apt


o Example Script: Install and Update Packages

bash

#!/bin/bash
sudo apt-get update
sudo apt-get install -y package_name
sudo apt-get upgrade -y

o Explanation: apt-get commands for updating, installing, and upgrading


packages.

Managing Packages with yum

o Example Script: Install and Update Packages

bash

#!/bin/bash
sudo yum update
sudo yum install -y package_name
sudo yum upgrade

o Explanation: yum commands for updating, installing, and upgrading packages.

• Managing Packages with snap


o Example Script: Install and Update Packages

bash

#!/bin/bash
sudo snap install package_name
sudo snap refresh

o Explanation: snap commands for installing and refreshing packages.

50
APT, YUM, and SNAP Commands Cheat Sheet

Command Example Description


APT Commands
Update package list sudo apt update Updates the list of available packages and
their versions.
Upgrade installed sudo apt upgrade Installs the newest versions of all installed
packages packages.
Install a package sudo apt install Installs the specified packagename.
packagename
Remove a package sudo apt remove Removes the specified packagename but
packagename leaves configuration files.
Purge a package sudo apt purge Completely removes the packagename,
packagename including configuration files.
Search for a apt search keyword Searches for packages related to keyword.
package
Show package apt show packagename Displays information about packagename.
details
List installed apt list --installed Lists all installed packages.
packages
YUM Commands
Update package list sudo yum check- Checks for available updates for all packages.
update
Upgrade installed sudo yum update Updates all packages to the latest version.
packages
Install a package sudo yum install Installs the specified packagename.
packagename
Remove a package sudo yum remove Removes the specified packagename.
packagename
Search for a yum search keyword Searches for packages related to keyword.
package
Show package yum info packagename Displays information about packagename.
details
List installed yum list installed Lists all installed packages.
packages
Clean cache sudo yum clean all Cleans up the local repository cache.
SNAP Commands
Find a package snap find Searches the Snap Store for packagename.
packagename
Install a package sudo snap install Installs the specified packagename from
packagename the Snap Store.
Remove a package sudo snap remove Removes the specified packagename.
packagename
List installed snaps snap list Lists all installed snaps.
Refresh snaps sudo snap refresh Updates all installed snaps to the latest
version.
Info about a snap snap info Displays information about packagename.
packagename
Revert to previous sudo snap revert Reverts packagename to the previous
version packagename version.
List available snap info Lists the available channels for
channels packagename packagename (e.g., stable, beta).

51
Chapter 24: Disk Management

• Monitoring Disk Usage


o Example Script: Check Disk Usage

bash

#!/bin/bash
df -h
du -sh /path/to/directory

o Explanation: df -h displays disk space usage and du -sh displays directory


size.

• Managing Disk Partitions


o Example Script: Create and Format Partitions

bash

#!/bin/bash
sudo fdisk /dev/sdX
sudo mkfs.ext4 /dev/sdX1

o Explanation: fdisk manages disk partitions and mkfs.ext4 formats partitions.

• Mounting and Unmounting Filesystems


o Example Script: Mount a Filesystem

bash

#!/bin/bash
sudo mount /dev/sdX1 /mnt
sudo umount /mnt

o Explanation: mount attaches a filesystem to the directory tree and umount


detaches it.

52
Disk Usage, Disk Partitioning, and Mounting Filesystems Commands Cheat
Sheet

Command Example Description


Disk Usage Commands
Check disk usage df -h Displays disk space usage in a
human-readable format.
Check directory size du -sh Displays the size of a specified
/path/to/directory
directory.
Check disk usage of all du -ah Displays disk usage of all files
/path/to/directory
files and directories and directories in a specified
path.
Check top largest `du -ah /path/to/directory sort -rh
files/directories
Disk Partitioning
Commands
List disk partitions lsblk Displays information about all
available or the specified block
devices.
Start fdisk to partition sudo fdisk /dev/sdX Opens fdisk utility to create or
a disk modify partitions on
/dev/sdX.
Print partition table sudo fdisk -l /dev/sdX Prints the partition table of
/dev/sdX.
Create a new partition sudo fdisk /dev/sdX then Creates a new partition on
n /dev/sdX using fdisk.
Format a partition sudo mkfs.ext4 /dev/sdX1 Formats the partition
/dev/sdX1 with ext4
filesystem.
Mounting Filesystems
Commands
Mount a filesystem sudo mount /dev/sdX1 Mounts the partition
/mnt/mydrive
/dev/sdX1 to the directory
/mnt/mydrive.
Unmount a filesystem sudo umount /mnt/mydrive Unmounts the filesystem
mounted at /mnt/mydrive.
List mounted mount or df -h Displays all mounted
filesystems filesystems.
Mount all filesystems sudo mount -a Mounts all filesystems
mentioned in /etc/fstab specified in /etc/fstab.
Show filesystem types lsblk -f Lists all block devices with
their filesystems.
Add entry to /etc/fstab `echo '/dev/sdX1 sudo tee -a /etc/fstab`
/mnt/mydrive ext4 defaults 0
2'

53
Chapter 25: Network Management

• Configuring Network Interfaces


o Example Script: Configure a Static IP

bash

#!/bin/bash
sudo ip addr add 192.168.1.100/24 dev eth0
sudo ip link set eth0 up

o Explanation: ip addr add assigns an IP address to an interface and ip link


set activates it.

• Monitoring Network Traffic


o Example Script: Monitor Traffic with iftop

bash

#!/bin/bash
sudo iftop

o Explanation: iftop displays bandwidth usage on an interface.

• Using nmap for Network Scanning


o Example Script: Basic nmap Scan

bash

#!/bin/bash
sudo nmap -sP 192.168.1.0/24

o Explanation: nmap -sP performs a ping scan to discover active devices on a


network.

54
Network Configuration, Network Monitoring, and Nmap Commands Cheat
Sheet

Command Example Description


Network
Configuration
Commands
Display network ifconfig Shows the configuration of all
interfaces network interfaces.
Display network ip addr show Shows IP address information of
interfaces (ip network interfaces.
command)
Bring an interface up sudo ifconfig eth0 up Activates the network interface
eth0.
Bring an interface sudo ifconfig eth0 down Deactivates the network interface
down eth0.
Assign IP address to sudo ifconfig eth0 Sets the IP address and netmask
interface 192.168.1.10 netmask for eth0.
255.255.255.0
Add a default gateway sudo route add default gw Adds a default gateway to the
192.168.1.1 routing table.
Configure DNS servers `echo 'nameserver 8.8.8.8' sudo tee /etc/resolv.conf`
Network Monitoring
Commands
Check current netstat -tuln Displays all listening TCP and
network connections UDP ports.
Monitor network iftop Monitors network traffic by
traffic interface in real-time.
Display routing table netstat -r or route -n Shows the kernel routing table.
Monitor network nload Monitors network bandwidth
bandwidth usage in real-time.
Capture network tcpdump -i eth0 Captures network packets on
packets interface eth0.
Display network ip -s link Shows detailed statistics of
statistics network interfaces.
Nmap Commands
Basic scan of an IP or nmap 192.168.1.1 Scans the specified IP address or
range range.
Scan specific ports nmap -p 22,80,443 Scans specified ports on the
192.168.1.1 target.
Scan all ports nmap -p- 192.168.1.1 Scans all 65535 ports on the
target.
Detect OS and services nmap -A 192.168.1.1 Enables OS detection, version
detection, script scanning, and
traceroute.
Scan multiple targets nmap 192.168.1.1,2,3 Scans multiple IP addresses.
Aggressive scan nmap -T4 -A -v 192.168.1.1 Performs an aggressive scan with
verbose output.
Save scan output nmap -oN output.txt Saves the scan results to a file.
192.168.1.1

55
Chapter 26: System Monitoring and Performance Tuning

• Monitoring System Performance


o Example Script: Monitor with top

bash

#!/bin/bash
top

o Explanation: top displays real-time system performance and process


information.

• Using htop for Enhanced Monitoring


o Example Script: Install and Run htop

bash

#!/bin/bash
sudo apt-get install -y htop
htop

o Explanation: htop provides an enhanced, interactive system-monitoring


interface.

• Tuning System Performance


o Example Script: Modify sysctl Settings

bash

#!/bin/bash
sudo sysctl -w net.ipv4.tcp_syncookies=1
sudo sysctl -p

o Explanation: sysctl modifies kernel parameters at runtime.

56
System Monitoring, Htop, and Performance Tuning Commands Cheat Sheet

Command Example Description


System
Monitoring
Commands
Display system uptime Shows how long the system has
uptime been running and the load average.
Display memory free -h Shows the amount of free and used
usage memory in the system.
Display disk usage df -h Shows the amount of disk space
used and available on filesystems.
Display CPU lscpu Shows detailed information about
information the CPU architecture.
Display processes ps aux Shows a snapshot of all current
processes.
Real-time system top Provides a dynamic view of
monitoring system processes and resource
usage.
Check system logs dmesg Displays the system message
buffer (kernel logs).
Display hardware lshw Provides detailed information
information about all hardware components.
Htop Commands
Start htop htop Launches the htop interactive
process viewer.
Sort by CPU usage Press F6, then select PERCENT_CPU Sorts processes by CPU usage
within htop.
Sort by memory Press F6, then select PERCENT_MEM Sorts processes by memory usage
usage within htop.
Kill a process Press F9, then select signal Sends a signal to terminate a
selected process within htop.
Search for a process Press F3, then type process name Searches for a specific process
within htop.
Filter processes Press F4, then type filter string Filters processes based on the
input string within htop.
Performance Tuning
Commands
Check CPU cpupower frequency-info Displays current CPU frequency
frequency scaling and scaling information.
Set CPU governor sudo cpupower frequency-set -g Sets the CPU frequency governor
performance to "performance".
Adjust swappiness sudo sysctl vm.swappiness=10 Sets the kernel parameter for swap
space usage.
Adjust cache sudo sysctl Sets the kernel parameter for cache
pressure vm.vfs_cache_pressure=50 reclaiming.
Limit process CPU cpulimit -l 50 -p <PID> Limits a process to a maximum of
usage 50% CPU usage.
Monitor I/O iostat -xz 1 Monitors I/O performance and
performance statistics in real-time.
Analyze system perf stat Collects and displays performance
performance data for a given command.
Profile CPU usage perf top Provides a real-time view of CPU
usage and hotspots.

57
Chapter 27: Security and Access Control

• Configuring SSH
o Example Script: Configure SSH Server

bash

#!/bin/bash
sudo apt-get install -y openssh-server
sudo systemctl enable ssh
sudo systemctl start ssh

o Explanation: Installs and starts the SSH server.

• Firewall Management with ufw


o Example Script: Basic ufw Commands

bash

#!/bin/bash
sudo ufw allow 22
sudo ufw enable
sudo ufw status

o Explanation: ufw is a user-friendly front-end for managing iptables firewall


rules.

• Using SELinux for Enhanced Security


o Example Script: Enable SELinux

bash

#!/bin/bash
sudo setenforce 1
sudo getenforce

o Explanation: setenforce enables SELinux in enforcing mode.

58
SSH Configuration, UFW, and SELinux Commands Cheat Sheet

Command Example Description


SSH
Configuration
Commands
Generate SSH ssh-keygen -t rsa -b 4096 -C Generates a new
"[email protected]"
key pair SSH key pair with
RSA 4096-bit
encryption.
Copy SSH ssh-copy-id user@remote_host Copies the SSH
key to remote public key to the
host remote host for
passwordless
login.
Connect to a ssh user@remote_host Connects to a
remote host remote host using
SSH.
SSH config ~/.ssh/config The SSH client
file location configuration file
path.
SSH config Host example\n HostName example.com\n User An example entry
user\n IdentityFile ~/.ssh/id_rsa
file entry in the SSH config
file to simplify
connections.
Disable root PermitRootLogin no (in /etc/ssh/sshd_config) Prevents root user
login from logging in
via SSH.
Change SSH Port 2222 (in /etc/ssh/sshd_config) Changes the
port default SSH port
from 22 to 2222.
Restart SSH sudo systemctl restart sshd Restarts the SSH
service service to apply
configuration
changes.
UFW
Commands
Enable UFW sudo ufw enable Enables the
Uncomplicated
Firewall (UFW).
Disable UFW sudo ufw disable Disables UFW.
Check UFW sudo ufw status Displays the
status current status of
UFW, including
rules.
Allow a port sudo ufw allow 22 Allows traffic on
port 22 (SSH).

59
Deny a port sudo ufw deny 22 Denies traffic on
port 22.
Allow specific sudo ufw allow from 192.168.1.100 Allows traffic
IP from a specific IP
address.
Deny specific sudo ufw deny from 192.168.1.100 Denies traffic
IP from a specific IP
address.
Allow a port sudo ufw allow 3000:4000/tcp Allows traffic on
range a range of ports
(3000-4000) for
TCP.
Delete a rule sudo ufw delete allow 22 Deletes the rule
allowing traffic on
port 22.
SELinux
Commands
Check sestatus Displays the
SELinux current status and
status mode of SELinux.
Set SELinux sudo setenforce 0 Sets SELinux to
mode to permissive mode
permissive temporarily.
Set SELinux sudo setenforce 1 Sets SELinux to
mode to enforcing mode
enforcing temporarily.
Make sudo sed -i Sets SELinux to
's/SELINUX=enforcing/SELINUX=permissive/'
SELinux permissive mode
/etc/selinux/config
permissive permanently by
permanently editing the config
file.
Relabel sudo touch /.autorelabel && sudo reboot Forces SELinux
filesystem to relabel the
filesystem on next
reboot.
List SELinux sudo semanage policy -l Lists all loaded
policies SELinux policies.
Add SELinux sudo semanage port -a -t http_port_t -p Adds a port
tcp 8080
port context context for
SELinux (e.g.,
allowing HTTP
on port 8080).
Modify sudo setsebool -P Changes the state
httpd_can_network_connect on
SELinux of an SELinux
boolean boolean (e.g.,
allowing HTTPD
to connect to the
network).

60
Chapter 28: Case Studies and Real-world Applications

• Case Study: Setting Up a Web Server


o Example Script: Install and Configure Apache

bash

#!/bin/bash
sudo apt-get update
sudo apt-get install -y apache2
sudo systemctl enable apache2
sudo systemctl start apache2

o Explanation: Steps to install and configure Apache web server.

• Case Study: Implementing a Backup System


o Example Script: Automated Backup Script

bash

#!/bin/bash
tar -czf /backup/home_$(date +%F).tar.gz /home/user

o Explanation: Script to back up the home directory.

• Case Study: Network Configuration for a Small Office


o Example Script: Configure DHCP Server

bash

#!/bin/bash
sudo apt-get install -y isc-dhcp-server
sudo systemctl enable isc-dhcp-server
sudo systemctl start isc-dhcp-server

o Explanation: Steps to install and configure a DHCP server.

Summary

This expanded section on System Administration covers essential topics with practical
examples and scripts. Each chapter includes detailed explanations, commands, cheat sheets,
diagrams, and real-world case studies to ensure a comprehensive understanding and
application of system administration tasks.

61
PART VI: Advanced Topics
Chapter 29: Advanced File System Management

• File System Types and Their Uses


o Explanation: Overview of different file systems like ext4, XFS, Btrfs, and
ZFS.

File system comparison chart

• Configuring RAID
o Example Script: Configure RAID 1

bash

#!/bin/bash
sudo mdadm --create --verbose /dev/md0 --level=1 --raid-
devices=2 /dev/sdX /dev/sdY
sudo mkfs.ext4 /dev/md0
sudo mount /dev/md0 /mnt

o Explanation: mdadm creates a RAID array, mkfs.ext4 formats it, and mount
attaches it to the filesystem.

62
• LVM (Logical Volume Management)
o Example Script: Create and Manage LVM

bash

#!/bin/bash
sudo pvcreate /dev/sdX
sudo vgcreate vg0 /dev/sdX
sudo lvcreate -L 10G -n lv0 vg0
sudo mkfs.ext4 /dev/vg0/lv0
sudo mount /dev/vg0/lv0 /mnt

o Explanation: Commands to create a physical volume, volume group, and


logical volume.

63
RAID and LVM Commands Cheat Sheet

Command Example Description


RAID
Commands
Create RAID 0 sudo mdadm --create --verbose /dev/md0 Creates a RAID 0 array with two
--level=0 --raid-devices=2 /dev/sda1 devices.
/dev/sdb1
Create RAID 1 sudo mdadm --create --verbose /dev/md0 Creates a RAID 1 array with two
--level=1 --raid-devices=2 /dev/sda1 devices.
/dev/sdb1
Create RAID 5 sudo mdadm --create --verbose /dev/md0 Creates a RAID 5 array with three
--level=5 --raid-devices=3 /dev/sda1 devices.
/dev/sdb1 /dev/sdc1
Create RAID 6 sudo mdadm --create --verbose /dev/md0 Creates a RAID 6 array with four
--level=6 --raid-devices=4 /dev/sda1 devices.
/dev/sdb1 /dev/sdc1 /dev/sdd1
Create RAID 10 sudo mdadm --create --verbose /dev/md0 Creates a RAID 10 array with four
--level=10 --raid-devices=4 /dev/sda1 devices.
/dev/sdb1 /dev/sdc1 /dev/sdd1
View RAID status cat /proc/mdstat Displays the status of RAID devices.
Detail RAID sudo mdadm --detail /dev/md0 Provides detailed information about a
array specific RAID array.
Stop RAID array sudo mdadm --stop /dev/md0 Stops a specific RAID array.
Remove RAID sudo mdadm --remove /dev/md0 Removes a specific RAID array.
array
Add spare to sudo mdadm --manage /dev/md0 --add Adds a spare device to an existing
RAID /dev/sdc1 RAID array.
LVM Commands
Create physical sudo pvcreate /dev/sda1 Initializes a physical volume for use
volume (PV) by LVM.
Display physical sudo pvs Lists all physical volumes.
volumes
Create volume sudo vgcreate vg1 /dev/sda1 /dev/sdb1 Creates a volume group named vg1
group (VG) with specified physical volumes.
Display volume sudo vgs Lists all volume groups.
groups
Create logical sudo lvcreate -L 10G -n lv1 vg1 Creates a logical volume named lv1
volume (LV) of size 10G in volume group vg1.
Display logical sudo lvs Lists all logical volumes.
volumes
Extend logical sudo lvextend -L +5G /dev/vg1/lv1 Extends the logical volume lv1 by
volume 5G.
Resize filesystem sudo resize2fs /dev/vg1/lv1 Resizes the filesystem on a logical
volume after extending it.
Reduce logical sudo lvreduce -L -5G /dev/vg1/lv1 Reduces the size of logical volume
volume lv1 by 5G (requires unmounting and
filesystem shrinkage first).
Remove logical sudo lvremove /dev/vg1/lv1 Removes the specified logical volume.
volume
Remove volume sudo vgremove vg1 Removes the specified volume group.
group
Remove physical sudo pvremove /dev/sda1 Removes the specified physical
volume volume from LVM control.

64
Chapter 30: Kernel Tuning and Optimization

• Kernel Parameters and sysctl


o Example Script: Tune Kernel Parameters

bash

#!/bin/bash
sudo sysctl -w vm.swappiness=10
sudo sysctl -w net.ipv4.tcp_rmem='4096 87380 6291456'
sudo sysctl -p

o Explanation: Adjust kernel parameters for memory management and TCP


performance.

• Building and Patching the Kernel


o Example Script: Compile and Install a Custom Kernel

bash

#!/bin/bash
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-
5.10.tar.xz
tar -xf linux-5.10.tar.xz
cd linux-5.10
make menuconfig
make -j$(nproc)
sudo make modules_install
sudo make install

o Explanation: Steps to download, configure, compile, and install a custom


kernel.

Kernel compilation workflow diagram

65
sysctl Commands Cheat Sheet

Command Example Description


List all kernel sysctl -a Displays all the current kernel
parameters parameters and their values.
Get value of a sysctl net.ipv4.ip_forward Retrieves the current value of
specific the net.ipv4.ip_forward
parameter parameter.
Set value of a sudo sysctl -w Sets the
net.ipv4.ip_forward=1 net.ipv4.ip_forward
specific
parameter parameter to 1.
Load settings sudo sysctl -p /path/to/file Loads and applies settings
from a from a specified configuration
configuration file.
file
Persist kernel `echo "net.ipv4.ip_forward = 1" sudo tee -a /etc/sysctl.conf`
parameters
Reload sysctl sudo sysctl -p Reloads the configuration
configuration from /etc/sysctl.conf.
Apply a single sudo sysctl -w Changes the hostname to
kernel.hostname="newhostname"
parameter from newhostname.
the command
line
Apply settings sudo sysctl --system Applies all settings from all
from a custom *.conf files in
file /etc/sysctl.d,
/usr/lib/sysctl.d, and
/run/sysctl.d.
Delete a specific sudo sysctl -w Deletes the
net.ipv4.ip_forward="" net.ipv4.ip_forward
parameter
parameter.
Check if a sysctl -n net.ipv4.ip_forward Checks if the
parameter is net.ipv4.ip_forward
present parameter is present and prints
its value.

66
Chapter 31: Virtualization and Containers

• Introduction to Virtualization with KVM


o Example Script: Install and Configure KVM

bash

#!/bin/bash
sudo apt-get install -y qemu-kvm libvirt-daemon-system libvirt-
clients bridge-utils
sudo systemctl enable libvirtd
sudo systemctl start libvirtd
sudo virsh list --all

o Explanation: Commands to install and start KVM for virtualization.

"KVM virtualization architecture diagram"

• Using Docker for Containerization


o Example Script: Basic Docker Commands

bash

#!/bin/bash
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo docker run hello-world
sudo docker ps -a

67
o Explanation: Install Docker, run a test container, and list containers.

• Advanced Docker Usage


o Example Script: Docker Compose

bash

#!/bin/bash
sudo apt-get install -y docker-compose
cat > docker-compose.yml <<EOF
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
EOF
sudo docker-compose up

o Explanation: Use Docker Compose to manage multi-container applications.

68
Docker Commands Cheat Sheet

Command Example Description


List all Docker docker ps Lists all running
containers containers.
List all containers docker ps -a Lists all containers,
(including stopped) both running and
stopped.
Start a container docker start <container_id> Starts a stopped
container.
Stop a container docker stop <container_id> Stops a running
container.
Remove a container docker rm <container_id> Removes a stopped
container.
List all Docker docker images Lists all images on the
images local machine.
Pull an image from docker pull <image_name> Downloads an image
Docker Hub from Docker Hub.
Run a container docker run <image_name> Runs a container from
from an image the specified image.
Run a container docker run -it <image_name> /bin/bash Runs a container and
with an interactive provides an interactive
terminal terminal.
Build an image from docker build -t <image_name> . Builds an image from a
a Dockerfile Dockerfile in the
current directory.
Remove an image docker rmi <image_name> Deletes an image from
the local machine.
Show logs from a docker logs <container_id> Displays the logs from a
container running container.
Execute a command docker exec -it <container_id> Runs a command in an
in a running <command> existing container.
container
List container docker stats Displays resource usage
resource usage statistics for running
statistics containers.
Show detailed docker inspect <container_id or Provides detailed
information about image_name> information about a
Docker objects container or image.
Copy files from docker cp <container_id>:/path/to/file Copies a file from a
container to host /host/path container to the host
machine.
Remove all stopped docker container prune Removes all stopped
containers containers.
Remove all unused docker image prune Deletes all unused
images images to free up space.
Network docker network ls Lists all Docker
management networks.
Volume docker volume ls Lists all Docker
management volumes.
Create a new volume docker volume create <volume_name> Creates a new Docker
volume.
Attach a volume to a docker run -v Attaches a volume to a
container <volume_name>:/path/in/container container at the
<image_name> specified path.

69
Chapter 32: High Availability and Load Balancing

• Setting Up a Load Balancer with HAProxy


o Example Script: Install and Configure HAProxy

bash

#!/bin/bash
sudo apt-get install -y haproxy
sudo tee /etc/haproxy/haproxy.cfg <<EOF
frontend http_front
bind *:80
default_backend http_back

backend http_back
balance roundrobin
server server1 192.168.1.2:80 check
server server2 192.168.1.3:80 check
EOF
sudo systemctl restart haproxy

o Explanation: Configure HAProxy for load balancing HTTP traffic.

HAProxy load balancing diagram

70
• Configuring High Availability with Keepalived
o Example Script: Set Up Keepalived

bash

#!/bin/bash
sudo apt-get install -y keepalived
sudo tee /etc/keepalived/keepalived.conf <<EOF
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.1.100
}
}
EOF
sudo systemctl restart keepalived

o Explanation: Configure Keepalived for high availability with VRRP.

71
Keepalived high availability diagram

Chapter 33: Security and Hardening

• Implementing SELinux Policies


o Example Script: Enable and Configure SELinux

bash

#!/bin/bash
sudo setenforce 1
sudo semanage fcontext -a -t httpd_sys_content_t
'/var/www/html(/.*)?'
sudo restorecon -R /var/www/html

o Explanation: Enable SELinux and set security contexts for web directories.

• Using AppArmor for Application Security


o Example Script: Configure AppArmor Profiles

bash

#!/bin/bash
sudo apt-get install -y apparmor apparmor-utils
sudo aa-enforce /etc/apparmor.d/usr.sbin.nginx
sudo aa-status

o Explanation: Install and enforce AppArmor profiles for application security.

• Advanced Firewall Configuration with iptables


o Example Script: Configure iptables Rules

bash

#!/bin/bash
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -j DROP
sudo iptables-save | sudo tee /etc/iptables/rules.v4

o Explanation: Create and save iptables rules for allowing SSH and HTTP
traffic while dropping all others.

72
SELinux Commands Cheat Sheet

Command Example Description


SELinux Commands
Check SELinux sestatus Displays the current status of
status SELinux.
Enable SELinux setenforce 1 Sets SELinux to enforcing mode.
Disable SELinux setenforce 0 Sets SELinux to permissive mode.
Change SELinux vi /etc/selinux/config Edits the SELinux configuration
mode permanently file to set the mode permanently.
List SELinux getsebool -a Displays all SELinux booleans and
booleans their states.
Change SELinux setsebool Changes the state of an SELinux
<boolean_name> on
boolean boolean.
List SELinux ls -Z Lists SELinux contexts for files and
contexts for files directories.
Change SELinux chcon -t <type> <file> Changes the SELinux context type
context for a file for a file.
Restore default restorecon -Rv Restores the default SELinux
<directory>
SELinux context context for files and directories
recursively.

73
Chapter 34: Troubleshooting and Optimization

• System Log Analysis


o Example Script: Analyze Logs with journalctl

bash

#!/bin/bash
sudo journalctl -u apache2
sudo journalctl --since "2023-01-01" --until "2023-01-31"

o Explanation: Use journalctl to view logs for specific services and time
ranges.

• Performance Monitoring with Performance Co-Pilot (PCP)


o Example Script: Install and Use PCP

bash

#!/bin/bash
sudo apt-get install -y pcp
sudo systemctl start pmcd
sudo pmstat

o Explanation: Install PCP and use pmstat to monitor system performance.

• Memory and CPU Optimization


o Example Script: Optimize Memory Usage

bash

#!/bin/bash
sudo sysctl -w vm.dirty_ratio=10
sudo sysctl -w vm.dirty_background_ratio=5
sudo sysctl -p

o Explanation: Adjust kernel parameters to optimize memory usage.

74
Command Example Description
journalctl
Commands
View all logs journalctl Displays all logs from the
systemd journal.
View logs for a journalctl -u <unit_name> Displays logs for a specific
specific unit systemd unit (e.g., journalctl -
u nginx).
View logs by journalctl -p <priority> Displays logs of a specified
priority priority (e.g., journalctl -p
err).
View logs within journalctl --since "2021- Displays logs within a specific
01-01" --until "2021-01-02"
a time range time range.
Follow live logs journalctl -f Continuously displays new log
entries (similar to tail -f).
View kernel logs journalctl -k Displays only kernel log
messages.
View logs for the journalctl -b Displays logs from the current
current boot boot.
View logs for a journalctl -b -1 Displays logs from the previous
specific boot boot (use -2, -3, etc. for earlier
boots).
Clear all journal journalctl --vacuum-time=1s Deletes all journal logs older than
logs 1 second.
View logs for a journalctl _UID=<user_id> Displays logs for a specific user
specific user by UID.

75
Chapter 35: Real-life Applications and Case Studies

• Case Study: Automating System Maintenance


o Example Script: Automated Maintenance Script

bash

#!/bin/bash
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get autoremove -y
sudo reboot

o Explanation: Script to automate system updates and maintenance.

• Case Study: Setting Up a Secure Web Server


o Example Script: Install and Secure Nginx with Let's Encrypt

bash

#!/bin/bash
sudo apt-get install -y nginx
sudo systemctl start nginx
sudo apt-get install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com
sudo systemctl reload nginx

o Explanation: Steps to install and secure Nginx with SSL/TLS certificates from
Let's Encrypt.

Secure connection using HTTPS on nginx

76
• Case Study: Configuring a VPN Server
o Example Script: Install and Configure OpenVPN

bash

#!/bin/bash
sudo apt-get install -y openvpn easy-rsa
make-cadir ~/openvpn-ca
cd ~/openvpn-ca
source vars
./build-ca
./build-key-server server
./build-dh
openvpn --genkey --secret keys/ta.key
cd keys
sudo cp ca.crt server.crt server.key dh2048.pem ta.key
/etc/openvpn
sudo cp /usr/share/doc/openvpn/examples/sample-config-
files/server.conf.gz /etc/openvpn
cd /etc/openvpn
sudo gzip -d server.conf.gz
sudo systemctl start openvpn@server
sudo systemctl enable openvpn@server

o Explanation: Steps to install and configure an OpenVPN server for secure


remote access.

VPN server configuration diagram

Summary

This expanded section on Advanced Topics delves into sophisticated aspects of Linux
administration, providing practical scripts, explanations, commands, cheat sheets, diagrams,
and case studies. Each chapter equips readers with the necessary knowledge and tools to
handle complex system administration tasks effectively.

77
PART VII: Case Studies and Practical Examples
Chapter 36: Automating Daily System Tasks

• Case Study: Scheduled Backups with Cron


o Example Script: Automate Backups Using cron

bash

#!/bin/bash
crontab -l > mycron
echo "0 2 * * * /usr/bin/rsync -a /home/user/
/backup/home/user/" >> mycron
crontab mycron
rm mycron

o Explanation: This script uses crontab to schedule a daily backup at 2 AM


using rsync.
o Diagram: Cron Job Workflow
▪ Search: "cron job workflow diagram"

• Example: System Health Monitoring with Scripts


o Example Script: Health Check Script

bash

#!/bin/bash
df -h | mail -s "Disk Usage Report" [email protected]
free -m | mail -s "Memory Usage Report" [email protected]

o Explanation: This script checks disk and memory usage and emails the report
to the system admin.

Cron Expressions and Mail Command Options Cheat Sheet

Command/Expression Example Description


Cron Expressions
Minute 0 At minute 0
Hour 0 At the top of the hour (0)
Day of Month 1 On the first day of the month
Month * Every month
Day of Week * Every day of the week
Every 5 minutes */5 * * * * Runs every 5 minutes
Every hour at the 30th minute 30 * * * * Runs every hour at 30 minutes past the hour
Daily at midnight 0 0 * * * Runs daily at midnight
Every Sunday at midnight 0 0 * * 0 Runs every Sunday at midnight
On the 1st of every month at midnight 0 0 1 * * Runs at midnight on the 1st of every month
Every weekday at midnight 0 0 * * 1-5 Runs every weekday at midnight
On January 1st at midnight 0 0 1 1 * Runs at midnight on January 1st
Last day of the month at midnight 0 0 L * * Runs at midnight on the last day of the month

78
Chapter 37: Network Administration

• Case Study: Network Performance Monitoring


o Example Script: Monitor Network Bandwidth with iftop

bash

#!/bin/bash
sudo apt-get install -y iftop
sudo iftop -n -B -i eth0

o Explanation: This script installs iftop and monitors network bandwidth on


interface eth0.

• Example: Network Troubleshooting with nmap


o Example Script: Scan Network for Open Ports

bash

#!/bin/bash
sudo apt-get install -y nmap
nmap -sP 192.168.1.0/24

o Explanation: This script installs nmap and scans the local network for active
hosts and open ports.

iftop and nmap Command Cheat Sheet

Command Example Description


iftop Commands
Start iftop iftop Starts the iftop network bandwidth monitoring
tool.
Specify interface iftop -i eth0 Monitors the eth0 network interface.
Show port iftop -P Displays port numbers as well as hostnames/IP
numbers addresses.
Display in bytes iftop -B Displays bandwidth usage in bytes instead of
bits.
Disable DNS iftop -n Prevents iftop from resolving hostnames,
resolution showing IP addresses only.
Set screen update iftop -t Sets the screen update rate to "text" mode.
rate
Set limit for iftop -L 50 Limits the display to the top 50 lines.
display
Set custom filter iftop -f "port Filters the display to show only traffic on port
80"
80.

79
Chapter 38: Web Server Administration

• Case Study: Setting Up a Secure LAMP Stack


o Example Script: Install and Configure LAMP Stack

bash

#!/bin/bash
sudo apt-get install -y apache2 mysql-server php libapache2-
mod-php php-mysql
sudo systemctl start apache2
sudo mysql_secure_installation

o Explanation: This script installs Apache, MySQL, and PHP, and secures the
MySQL installation.

LAMP stack architecture diagram

• Example: Configuring HTTPS with Let's Encrypt


o Example Script: Secure Apache with Let's Encrypt

bash

#!/bin/bash
sudo apt-get install -y certbot python-certbot-apache
sudo certbot --apache -d example.com
sudo systemctl reload apache2

o Explanation: This script uses Certbot to obtain and install a Let's Encrypt SSL
certificate for Apache.

80
LAMP Stack Commands Cheat Sheet and Certbot Commands Cheat Sheet

Command Example Description


LAMP Stack
Commands
Install Apache sudo apt-get install apache2 Installs the Apache web
server.
Install MySQL sudo apt-get install mysql-server Installs the MySQL
database server.
Install PHP sudo apt-get install php Installs PHP and its
libapache2-mod-php php-mysql
modules.
Restart Apache sudo systemctl restart apache2 Restarts the Apache
service.
Secure MySQL sudo mysql_secure_installation Runs the security script
Installation for MySQL.
Enable Apache sudo a2enmod rewrite Enables the mod_rewrite
mod_rewrite module in Apache.
Check Apache sudo systemctl status apache2 Checks the status of the
Status Apache service.
Check MySQL sudo systemctl status mysql Checks the status of the
Status MySQL service.
Test PHP php -r 'echo "Hello, World!";' Runs a simple PHP
command to test
installation.
Certbot Commands
Install Certbot sudo apt-get install certbot Installs Certbot for
python3-certbot-apache
Apache.
Obtain SSL sudo certbot --apache Obtains and installs an
Certificate SSL certificate for
Apache.
Renew SSL sudo certbot renew Renews the SSL
Certificate certificates.
Test Renewal sudo certbot renew --dry-run Tests the renewal
Process process without making
changes.
List Certificates sudo certbot certificates Lists all managed
certificates.
Delete Certificate sudo certbot delete --cert-name Deletes a specific
example.com
certificate.
Configure sudo crontab -e Edits crontab to
Automatic schedule automatic
Renewal renewals.

81
Chapter 39: Database Management

• Case Study: Optimizing MySQL Performance


o Example Script: MySQL Performance Tuning

bash

#!/bin/bash
sudo mysql -e "SET GLOBAL innodb_buffer_pool_size = 2G;"
sudo mysql -e "SET GLOBAL query_cache_size = 256M;"

o Explanation: This script tunes MySQL performance by adjusting buffer pool


and query cache size.
o Diagram: MySQL Performance Tuning
▪ Search: "MySQL performance tuning diagram"

• Example: Backing Up and Restoring MySQL Databases


o Example Script: MySQL Backup and Restore

bash

#!/bin/bash
mysqldump -u root -p database_name > backup.sql
mysql -u root -p database_name < backup.sql

o Explanation: This script demonstrates how to back up and restore a MySQL


database using mysqldump.

82
MySQL Optimization Commands Cheat Sheet and mysqldump Commands Cheat
Sheet

Command Example Description


MySQL
Optimization
Commands
Check Slow SHOW VARIABLES LIKE 'slow_query%'; Displays slow query
Queries settings.
Enable Slow SET GLOBAL slow_query_log = 'ON'; Enables the slow query
Query Log log.
Optimize Table OPTIMIZE TABLE table_name; Optimizes the
specified table.
Analyze Table ANALYZE TABLE table_name; Analyzes the specified
table for optimization.
Check Table CHECK TABLE table_name; Checks the specified
table for errors.
Repair Table REPAIR TABLE table_name; Repairs the specified
table if needed.
mysqldump
Commands
Backup Database mysqldump -u root -p database_name Backs up a database to
> backup.sql
a SQL file.
Backup All mysqldump -u root -p --all- Backs up all databases
databases > all_databases.sql
Databases to a SQL file.
Backup with `mysqldump -u root -p database_name gzip > backup.sql.gz`
Compression
Restore Database mysql -u root -p database_name < Restores a database
backup.sql
from a SQL file.
Skip Lock Tables mysqldump -u root -p --single- Backs up a database
transaction database_name >
backup.sql
without locking tables.
Exclude mysqldump -u root -p --ignore- Excludes specific
database=database_name > backup.sql
Databases databases from the
backup.

83
Chapter 40: Security and Compliance

• Case Study: Implementing SELinux for Security


o Example Script: Enable and Configure SELinux

bash

#!/bin/bash
sudo setenforce 1
sudo semanage fcontext -a -t httpd_sys_content_t
'/var/www/html(/.*)?'
sudo restorecon -R /var/www/html

o Explanation: This script enables SELinux and configures security contexts for
a web directory.

• Example: Hardening SSH with Best Practices


o Example Script: Secure SSH Configuration

bash

#!/bin/bash
sudo sed -i 's/#PermitRootLogin yes/PermitRootLogin no/'
/etc/ssh/sshd_config
sudo sed -i 's/#PasswordAuthentication
yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo systemctl restart sshd

o Explanation: This script hardens SSH by disabling root login and password
authentication.

84
SELinux Commands Cheat Sheet and SSH Configuration Cheat Sheet

Command Example Description


SELinux
Commands
Check SELinux sestatus Displays the current status
Status of SELinux.
Enable SELinux setenforce 1 Enables SELinux
enforcement mode.
Disable SELinux setenforce 0 Disables SELinux
enforcement mode.
Configure vi /etc/selinux/config Edits the SELinux
SELinux Mode configuration file.
List SELinux ls -Z Lists the SELinux context
Contexts of files and directories.
Change SELinux chcon -R -t httpd_sys_content_t Changes the SELinux
/var/www/html
Context context of a directory.
SSH
Configuration
Commands
Generate SSH ssh-keygen -t rsa -b 4096 -C Generates a new SSH key
"[email protected]"
Key pair.
Copy SSH Key to ssh-copy-id user@server Copies the SSH key to a
Server remote server.
Connect with ssh user@server Connects to a remote
SSH server using SSH.
Edit SSH Config vi ~/.ssh/config Edits the SSH client
configuration file.
Disable Root PermitRootLogin no Disables root login in the
Login SSH server configuration.
Change SSH Port Port 2222 Changes the default SSH
port in the SSH server
configuration.

85
Chapter 41: Advanced Scripting and Automation

• Case Study: Automating System Updates


o Example Script: Update System Packages

bash

#!/bin/bash
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get autoremove -y

o Explanation: This script automates the update and upgrade of system


packages.

• Example: Using ansible for Configuration Management


o Example Script: Basic Ansible Playbook

yaml

---
- hosts: all
tasks:
- name: Install Nginx
apt:
name: nginx
state: present

o Explanation: This playbook installs Nginx on all hosts in the inventory.

Ansible configuration management diagram

86
apt-get Commands Cheat Sheet

Command Example Description


Update Package List sudo apt-get update Updates the list of available
packages.
Upgrade Packages sudo apt-get upgrade Upgrades all installed packages to
the latest versions.
Install Package sudo apt-get install Installs a specified package.
package_name
Remove Package sudo apt-get remove Removes a specified package.
package_name
Purge Package sudo apt-get purge Removes a package and its
package_name
configuration files.
Search for Package apt-cache search Searches for a package by name or
package_name
keyword.
Show Package Info apt-cache show Displays detailed information
package_name
about a package.
Clean Cache sudo apt-get clean Clears out the local repository of
retrieved package files.
Autoremove sudo apt-get Removes unnecessary packages
autoremove
automatically installed.
Check for Broken sudo apt-get check Checks for broken dependencies
Dependencies in the package database.

Summary

This expanded section on Case Studies and Practical Examples provides comprehensive
scripts, explanations, commands, cheat sheets, and diagrams for various real-world scenarios
in Linux administration. Each chapter equips readers with practical knowledge and tools to
automate tasks, manage networks, administer web servers, handle databases, ensure security,
and utilize advanced scripting techniques effectively.

87
PART VIII: Cheat Sheets and Resources
Chapter 42: Basic Linux Commands Cheat Sheet

• Cheat Sheet: Commonly Used Linux Commands


o Explanation: A quick reference guide for basic Linux commands such as ls,
cd, cp, mv, rm, chmod, and chown.
o Example Commands:

bash

ls -la
cd /path/to/directory
cp file1.txt file2.txt
mv file1.txt /path/to/destination/
rm file1.txt
chmod 755 script.sh
chown user:group file1.txt

Linux file system structure diagram

88
Chapter 43: Advanced Linux Commands Cheat Sheet

• Cheat Sheet: Advanced Linux Commands


o Explanation: A quick reference guide for advanced Linux commands such as
grep, awk, sed, find, xargs, tar, and curl.
o Example Commands:

bash

grep "pattern" file.txt


awk '{print $1}' file.txt
sed 's/old/new/g' file.txt
find /path/to/search -name "*.txt"
xargs -n 1 echo < file.txt
tar -cvf archive.tar /path/to/directory
curl -O http://example.com/file.txt

Chapter 44: Shell Scripting Cheat Sheet

• Cheat Sheet: Essential Shell Scripting Commands


o Explanation: A reference guide for shell scripting commands and syntax,
including variables, loops, conditionals, and functions.
o Example Script:

bash

#!/bin/bash
echo "Hello, World!"
for i in {1..5}; do
echo "Iteration $i"
done
if [ -f "file.txt" ]; then
echo "File exists."
fi
function greet {
echo "Hello, $1!"
}
greet "Alice"

89
grep, awk, sed Cheat Sheet

Command Example Description


grep Commands
Search for Pattern grep "pattern" file.txt Searches for a pattern in a file.
Recursive Search grep -r "pattern" /path Recursively searches for a pattern
in a directory.
Case-Insensitive grep -i "pattern" Searches for a pattern in a file,
file.txt
Search case-insensitively.
Show Line grep -n "pattern" Shows line numbers with matched
file.txt
Numbers lines.
awk Commands
Print Column awk '{print $1}' file.txt Prints the first column of a file.
Pattern Matching awk '/pattern/ {print}' Prints lines matching a pattern.
file.txt
Field Separator awk -F, '{print $1}' Uses a comma as the field
file.csv
separator.
Arithmetic awk '{print $1 + $2}' Performs arithmetic on columns.
file.txt
Operations
sed Commands
Find and Replace sed 's/old/new/g' Replaces all occurrences of a
file.txt
pattern.
Delete Line sed '2d' file.txt Deletes the second line of a file.
Insert Line sed '2i\new line' Inserts a line before the second
file.txt
line.
Substitute with sed -i.bak 's/old/new/g' Replaces a pattern and creates a
file.txt
Backup backup.

Shell Scripting Patterns Cheat Sheet

Command Example Description


For Loop for i in {1..5}; do echo $i; done Iterates over a sequence of
numbers.
While Loop while [ $x -le 5 ]; do echo $x; Executes a loop while a
x=$((x+1)); done
condition is true.
If-Else if [ $x -eq 5 ]; then echo "Equal"; Executes code based on a
else echo "Not equal"; fi
Statement condition.
Case case $x in 1) echo "One";; 2) echo Executes code based on a
"Two";; esac
Statement matching pattern.
Function function name { echo "Hello, $1"; } Defines a reusable
Definition function.
Function Call name "World" Calls a defined function
with arguments.

90
Chapter 45: Network Administration Cheat Sheet

• Cheat Sheet: Network Commands and Tools


o Explanation: A reference guide for network-related commands and tools such
as ping, traceroute, netstat, nmap, and ssh.
o Example Commands:

bash

ping -c 4 example.com
traceroute example.com
netstat -tuln
nmap -sP 192.168.1.0/24
ssh [email protected]

Chapter 46: System Administration Cheat Sheet

• Cheat Sheet: System Administration Commands


o Explanation: A reference guide for system administration commands and tools
such as systemctl, journalctl, top, htop, df, du, ps, and kill.
o Example Commands:

bash

systemctl start/stop/restart service


journalctl -u service
top
htop
df -h
du -sh /path/to/directory
ps aux
kill -9 PID

91
nmap SSH Cheat Sheet

Command Example Description


nmap
Commands
Basic Scan nmap 192.168.1.1 Scans the specified IP
address.
Scan Subnet nmap 192.168.1.0/24 Scans all IP addresses in a
subnet.
Service Version nmap -sV 192.168.1.1 Detects versions of
Detection services running on open
ports.
OS Detection nmap -O 192.168.1.1 Attempts to determine the
operating system of the
target.
SSH Commands
Generate SSH ssh-keygen -t rsa -b 4096 -C Generates a new SSH key
Key "[email protected]" pair.
Copy SSH Key ssh-copy-id user@server Copies the SSH key to a
to Server remote server.
Connect with ssh user@server Connects to a remote
SSH server using SSH.
Edit SSH vi ~/.ssh/config Edits the SSH client
Config configuration file.
Disable Root PermitRootLogin no Disables root login in the
Login SSH server configuration.

8) systemctl Journalctl Cheat Sheet

Command Example Description


systemctl
Commands
Start Service sudo systemctl start Starts a specified service.
service_name
Stop Service sudo systemctl stop Stops a specified service.
service_name
Restart Service sudo systemctl restart Restarts a specified service.
service_name
Enable Service sudo systemctl enable Enables a service to start at boot.
service_name
Disable Service sudo systemctl disable Disables a service from starting at
service_name boot.
journalctl Commands
View Logs journalctl Displays system logs.
View Recent Logs journalctl -r Displays logs in reverse order.
View Boot Logs journalctl -b Displays logs from the current
boot.
View Logs for a journalctl -u service_name Displays logs for a specific
Service service.
Follow Logs journalctl -f Continuously displays new log
entries.

92
Chapter 47: Security and Compliance Cheat Sheet

• Cheat Sheet: Security Commands and Best Practices


o Explanation: A reference guide for security-related commands and best
practices such as using iptables, firewalld, fail2ban, selinux, and
configuring SSH.
o Example Commands:

bash

iptables -L
firewall-cmd --list-all
fail2ban-client status
sudo setenforce 1
sudo vi /etc/ssh/sshd_config

Chapter 48: Performance Tuning Cheat Sheet

• Cheat Sheet: Performance Tuning Commands and Tools


o Explanation: A reference guide for performance tuning commands and tools
such as top, htop, iotop, vmstat, iostat, and sar.
o Example Commands:

bash

top
htop
iotop
vmstat 1
iostat -x 1
sar -u 1

Running htop on a linux machine

93
iptables firewalld fail2ban Cheat Sheet

Command Example Description


iptables
Commands
List Rules sudo iptables -L Lists all current iptables
rules.
Add Rule sudo iptables -A INPUT -p tcp --dport 22 Allows incoming SSH
-j ACCEPT connections.
Delete Rule sudo iptables -D INPUT -p tcp --dport 22 Deletes a specific rule.
-j ACCEPT
Save Rules sudo iptables-save > Saves current rules to a
/etc/iptables/rules.v4 file.
firewalld
Commands
Start firewalld sudo systemctl start firewalld Starts the firewalld
service.
Stop firewalld sudo systemctl stop firewalld Stops the firewalld
service.
Enable firewalld sudo systemctl enable firewalld Enables firewalld to
start at boot.
List Zones sudo firewall-cmd --get-zones Lists available zones.
Add Service to sudo firewall-cmd --zone=public --add- Adds a service to a
Zone service=http --permanent zone.
fail2ban
Commands
Start fail2ban sudo systemctl start fail2ban Starts the fail2ban
service.
Stop fail2ban sudo systemctl stop fail2ban Stops the fail2ban
service.
Enable fail2ban sudo systemctl enable fail2ban Enables fail2ban to start
at boot.
Status of sudo fail2ban-client status Shows the status of
fail2ban fail2ban.
View Jail Status sudo fail2ban-client status jail_name Shows the status of a
specific jail.

top htop iotop Cheat Sheet

Command Example Description


top Commands
Start top top Starts the top command-line process viewer.
Sort by Memory Usage top -o %MEM Sorts processes by memory usage.
Kill a Process k then PID Kills a process by PID.
htop Commands
Start htop htop Starts the htop command-line process viewer.
Tree View F5 Toggles tree view for processes.
Search for Process / then name Searches for a process by name.
iotop Commands
Start iotop sudo iotop Starts the iotop I/O process viewer.
Only Active Processes sudo iotop -o Shows only processes with I/O activity.
Sort by Disk Read sudo iotop -d Sorts processes by disk read activity.

94
Chapter 49: Scripting and Automation Tools Cheat Sheet

• Cheat Sheet: Automation Tools and Commands


o Explanation: A reference guide for scripting and automation tools such as
cron, anacron, at, and systemd timers.
o Example Commands:

bash

crontab -e
anacron -d
at now + 5 minutes
systemctl list-timers

cron anacron at Cheat Sheet

Command Example Description


cron Commands
Edit crontab crontab -e Opens the crontab file for editing.
List crontab crontab -l Lists the current user's crontab entries.
Remove crontab crontab -r Removes the current user's crontab.
anacron
Commands
Edit anacrontab sudo vi Edits the anacrontab file.
/etc/anacrontab
Run anacron sudo anacron -s Runs anacron jobs.
List anacron jobs anacron -L Lists scheduled anacron jobs.
at Commands
Schedule Job `echo "command" at 5pm`
List Jobs atq Lists all scheduled at jobs.
Remove Job atrm job_number Removes a scheduled at job by job
number.

95
Chapter 50: Additional Resources

• Resources: Books, Websites, and Communities


o Explanation: A curated list of additional resources for further learning and
support, including books, websites, forums, and online communities.
o Example Resources:
▪ Books:
▪ "The Linux Command Line" by William Shotts
▪ "Linux Pocket Guide" by Daniel J. Barrett
▪ Websites:
▪ Linux Documentation Project
▪ Stack Overflow
▪ Communities:
▪ Reddit: r/linux
▪ LinuxQuestions.org

Summary

This expanded section on Cheat Sheets and Resources provides detailed, quick reference
guides for a variety of Linux commands and tools, complete with scripts, explanations,
commands, cheat sheets, and diagrams for better understanding. Each chapter equips readers
with the essential commands and tools they need to efficiently manage and troubleshoot
Linux systems.

96
PART IX: Appendices
Chapter 51: Appendix A: Mathematical Foundations

• Subsections:
1. Basic Arithmetic and Algebra
▪ Explanation: Fundamental concepts of arithmetic and algebra relevant
to computing and scripting.
▪ Example:

bash

# Simple arithmetic operations in shell


echo $((2 + 3)) # Addition
echo $((5 - 2)) # Subtraction
echo $((3 * 4)) # Multiplication
echo $((10 / 2)) # Division

2. Probability and Statistics


▪ Explanation: Basic probability and statistical concepts used in
performance analysis and system monitoring.
▪ Example:

bash

# Generating random numbers


echo $RANDOM
echo $((RANDOM % 100)) # Random number between 0 and 99

3. Matrices and Vectors


▪ Explanation: Usage of matrices and vectors in computing, particularly
for tasks like graphics rendering or machine learning.
▪ Example:

bash

# Creating a simple matrix using arrays


matrix=([0,0]=1 [0,1]=2 [1,0]=3 [1,1]=4)
echo ${matrix[0,0]} # Accessing matrix element

97
Chapter 52: Appendix B: Shell Scripting Reference

• Subsections:
1. Built-in Shell Variables
▪ Explanation: Common built-in shell variables and their uses.
▪ Example:

bash

echo "Script name: $0"


echo "Number of arguments: $#"
echo "All arguments: $@"

2. Special Characters and Operators


▪ Explanation: Special characters and operators in shell scripting and
their functions.
▪ Example:

bash

echo "Current directory: $(pwd)"


echo "Home directory: $HOME"
echo "Current user: $USER"

3. Regular Expressions
▪ Explanation: Use of regular expressions in shell scripting for pattern
matching.
▪ Example:

bash

grep '^abc' file.txt # Lines starting with 'abc'


grep 'xyz$' file.txt # Lines ending with 'xyz'

Shell Variables Cheat Sheet and Shell Special Characters Cheat Sheet

Command Example Description


Shell Variables
Define Variable VAR=value Defines a new variable.
Access Variable echo $VAR Accesses the value of a variable.
Unset Variable unset VAR Unsets a variable.
Shell Special
Characters
Newline \n Represents a newline character.
Tab \t Represents a tab character.
Escape \ Escapes special characters.
Pipe command1 | Pipes the output of one command into
command2
another.

98
Chapter 53: Appendix C: Linux Resources

• Subsections:
1. Books and Guides
▪ Explanation: Recommended books and guides for further reading on
Linux and shell scripting.
▪ Example:
▪ Books:
▪ "The Linux Command Line" by William Shotts
▪ "Linux Pocket Guide" by Daniel J. Barrett
2. Online Tutorials and Documentation
▪ Explanation: Useful websites and online resources for learning and
troubleshooting.
▪ Example:
▪ Websites:
▪ Linux Documentation Project
▪ Stack Overflow
3. Communities and Forums
▪ Explanation: Online communities and forums where users can ask
questions and share knowledge.
▪ Example:
▪ Communities:
▪ Reddit: r/linux
▪ LinuxQuestions.org
4. Linux Distributions
▪ Explanation: Overview of popular Linux distributions and their
specific uses.
▪ Example:
▪ Distributions:
▪ Ubuntu
▪ Fedora
▪ CentOS

99
Chapter 54: Appendix D: Glossary of Terms

• Subsections:
1. Linux Terminology
▪ Explanation: Definitions of common Linux terms and jargon.
▪ Example:
▪ Kernel: The core component of a Linux operating system,
managing hardware and system processes.
▪ Shell: The interface that allows users to interact with the
operating system via commands.
2. Scripting Terminology
▪ Explanation: Definitions of common scripting terms and jargon.
▪ Example:
▪ Script: A file containing a series of commands that are
executed by the shell.
▪ Variable: A storage location identified by a name that holds
data.
3. Networking Terms
▪ Explanation: Definitions of common networking terms used in Linux
networking.
▪ Example:
▪ IP Address: A unique string of numbers separated by periods
that identifies each computer using the Internet Protocol to
communicate over a network.
▪ Port: A communication endpoint used by applications to
exchange data.
4. System Administration Terms
▪ Explanation: Definitions of common system administration terms.
▪ Example:
▪ Daemon: A background process that runs continuously to
handle periodic service requests.
▪ Cron: A time-based job scheduler in Unix-like operating
systems.

Summary

This expanded section on Appendices provides detailed reference material to supplement the
core content of the book. Each chapter is designed to give readers quick access to essential
information, scripts, explanations, commands, cheat sheets, and diagrams, helping them to
deepen their understanding of Linux and shell scripting.

100
GitLab CI/CD and Shell Scripting Cheatsheet

Command/Variable Action Example


stages Defines the stages: - build - test - deploy
stages of the
pipeline
script Defines the script: - echo "Hello, World!"
script to
execute in a
job
variables Defines variables: DEPLOY_PROXY_PASSWORD:
"your_secret_password"
environment
variables for
a job
$CI_COMMIT_REF_NAME GitLab echo "Branch: $CI_COMMIT_REF_NAME"
predefined
variable for
the branch
name
$CI_JOB_ID GitLab echo "Job ID: $CI_JOB_ID"
predefined
variable for
the job ID
before_script Script to before_script: - apt-get update
execute
before the
main script
after_script Script to after_script: - echo "Job finished"
execute after
the main
script
cache Defines files cache: paths: - node_modules/
to cache
between jobs
artifacts Defines files artifacts: paths: - dist/
to keep after
a job is done
only Defines only: - master
conditions to
limit when
jobs are run
except Defines except: - tags
conditions to
exclude
when jobs
are run
rules More rules: - if: '$CI_COMMIT_REF_NAME ==
"master"'
advanced
rules for job
execution

101
tags Tags to tags: - docker
match jobs to
specific
runners
allow_failure Allows jobs allow_failure: true
to fail
without
failing the
pipeline
dependencies Specifies dependencies: - build
jobs that a
job depends
on
when Defines when: manual
when to run
the job
(on_success
, always,
manual)
retry Defines how retry: 2
many times
to retry a job
on failure
environment Defines environment: production
environment
for a job
$DEPLOY_PROXY_PASSWOR Environment proxy_password="$DEPLOY_PROXY_PASSWOR
D D"
variable
defined in
.gitlab-
ci.yml
if [ -n "$VAR" ] Checks if a if [ -n "$VAR" ]; then echo
"Defined"; fi
variable is
defined and
not null
if [ -z "$VAR" ] Checks if a if [ -z "$VAR" ]; then echo "Not
defined"; fi
variable is
not defined
or is null
echo Prints text to echo "Hello, World!"
the console
#!/bin/bash Shebang to #!/bin/bash (at the top of the script)
specify the
script
interpreter
chmod +x script.sh Makes a chmod +x deploy_script.sh
script
executable
./script.sh Executes a ./deploy_script.sh
script

102
apt-get update Updates the apt-get update
package list
apt-get install Installs a apt-get install -y git
package
export VAR=value Sets an export MY_VAR="value"
environment
variable in a
script
curl -O Downloads a curl -O http://example.com/file.txt
file from a
URL
tar -xzvf Extracts a tar -xzvf archive.tar.gz
tar.gz file
zip -r Creates a zip zip -r archive.zip folder/
archive
scp Copies files scp file.txt user@host:/path/
over SSH
ssh Connects to ssh user@host
a remote host
over SSH
nmap Scans nmap -sP 192.168.0.0/24
network for
hosts and
open ports
ping Checks ping google.com
connectivity
to a host
grep Searches for grep "pattern" file.txt
patterns in
text
sed Stream sed -i 's/old/new/g' file.txt
editor for
modifying
files
awk Text awk '{print $1}' file.txt
processing
and data
extraction
ps aux Lists running ps aux
processes
kill Kills a kill 1234
process by
PID
crontab -e Edits the crontab -e
cron jobs
crontab -l Lists the crontab -l
cron jobs

103
df -h Displays df -h
disk space
usage
du -sh Displays the du -sh /path/to/directory
size of a
directory
top Displays top
running
processes
and system
resource
usage
htop Interactive htop
process
viewer
free -h Displays free -h
memory
usage
ifconfig Displays ifconfig
network
configuration
netstat -tuln Displays netstat -tuln
active
network
connections
iptables Configures iptables -L
the IP packet
filter rules
chmod Changes file chmod 755 script.sh
permissions
chown Changes file chown user:group file.txt
owner and
group
ln -s Creates a ln -s /path/to/file /path/to/link
symbolic
link
find Searches for find /path -name "file.txt"
files and
directories
alias Creates a alias ll="ls -la"
shortcut for a
command
.bashrc Bash ~/.bashrc (edit to add aliases and
configuration environment variables)
file
.zshrc Zsh ~/.zshrc (edit to add aliases and environment
configuration variables)
file
vim Text editor vim file.txt

104
nano Text editor nano file.txt
less Views files less file.txt
one screen at
a time
tail -f Displays the tail -f /var/log/syslog
end of a file
and updates
as it grows
head Displays the head -n 10 file.txt
beginning of
a file
cat Concatenates cat file.txt
and displays
file contents
tee Reads from `echo "Hello"
standard
input and
writes to
standard
output and
files

105

You might also like