Advanced Linux Summary 1723323191
Advanced Linux Summary 1723323191
Over this Summary, I've covered a wide range of topics related to Linux and Shell
scripting. I started with Mastering the command Line, including file system structure,
basic commands and command-line help. Next, we delved into networking concepts
such as network interfaces, configuration, tools and troubleshooting. Next, I explored
the file system hierarchy, including directory structure, permissions, file management
and system files. Finally, I covered Shell scripting, focusing on script structure,
variables, control structures, functions, file operations and advanced scripting
techniques.
Each summary has been designed to provide a thorough understanding of these topics,
offering both theoretical insights and practical use of the commands.
• Environment Variables:
o export VAR=value: Set environment variables.
o printenv: Display environment variables.
o env: Run a program in a modified environment.
• Shell Initialization Files:
o .bashrc, .bash_profile, .profile: Customize shell environments.
o source [file]: Apply changes from a shell script or config file without logging
out.
2. Shell Scripting
• Control Structures:
o if, elif, else, fi: Conditional execution.
o case, esac: Multi-way branching.
o for, while, until, do, done: Loop structures.
• Functions:
o function_name() { commands; }: Define a function.
o return [value]: Exit a function with a return value.
o local var_name=value: Define local variables in functions.
• Text Processing:
o grep [pattern] [file]: Search for patterns in files.
o sed 's/old/new/g' [file]: Stream editor for basic text transformations.
o awk '{print $1, $3}' [file]: Pattern scanning and processing language.
o cut -d[delimiter] -f[field] [file]: Remove sections from each line.
• Automation and Scheduling:
o cron: Schedule tasks to run periodically (crontab -e to edit cron jobs).
o at [time] [command]: Schedule one-time tasks.
o sleep [time]: Delay execution for a specified time.
• File Permissions:
o chmod [permissions] [file]: Change file permissions (e.g., chmod 755).
o chown [user]:[group] [file]: Change file owner and group.
o umask [mask]: Set default file permissions.
• Special Permissions:
o SUID, SGID, Sticky Bit: Advanced permission settings (chmod u+s).
• File Compression and Archiving:
o tar -czvf [archive.tar.gz] [files]: Create a compressed archive.
o tar -xvzf [archive.tar.gz]: Extract a compressed archive.
o zip [archive.zip] [files] / unzip [archive.zip]: Create/extract ZIP files.
• Symbolic Links:
o ln -s [target] [link]: Create a symbolic (soft) link.
4. Filesystem Management
• Disk Usage:
o df -h: Display disk space usage.
o du -sh [directory]: Display the size of a directory.
• Mounting and Unmounting:
o mount [device] [mount-point]: Mount a filesystem.
o umount [device/mount-point]: Unmount a filesystem.
o lsblk: List information about block devices.
• File Systems:
o mkfs.ext4 [device]: Create an ext4 filesystem.
o fsck [device]: Check and repair a filesystem.
o tune2fs: Adjust filesystem parameters.
System Administration
5. Process Management
• Process Control:
o ps aux: Display all running processes.
o top / htop: Interactive process viewer.
o kill [PID]: Terminate a process by PID.
o killall [process_name]: Terminate all instances of a process.
o bg / fg: Background and foreground process control.
• Process Priority:
o nice -n [priority] [command]: Start a process with a given priority.
o renice [priority] -p [PID]: Change the priority of a running process.
• User Management:
o useradd / usermod / userdel: Add, modify, delete users.
o passwd [username]: Change user passwords.
o whoami: Display current username.
• Group Management:
o groupadd / groupmod / groupdel: Add, modify, delete groups.
o usermod -aG [group] [user]: Add a user to a group.
• User Session Management:
o who / w: Display logged-in users.
o last: Show login history.
o su [user]: Switch to another user.
7. Package Management
• Debian-based Distributions:
o apt-get update / apt-get upgrade: Update package lists and upgrade installed
packages.
o apt-get install [package]: Install a package.
o dpkg -i [package.deb]: Install a package from a .deb file.
• Red Hat-based Distributions:
o yum update / yum upgrade: Update package lists and upgrade installed
packages.
o yum install [package]: Install a package.
o rpm -ivh [package.rpm]: Install a package from an .rpm file.
• Universal Package Managers:
o snap install [package]: Install a snap package.
o flatpak install [package]: Install a flatpak package.
• Network Configuration:
o ifconfig / ip addr: Display or configure network interfaces.
o ping [host]: Test connectivity to a host.
o netstat -tuln: Display active connections and listening ports.
o traceroute [host]: Trace the route to a host.
o ss -tuln: View open sockets.
• SSH (Secure Shell):
o ssh [user]@[host]: Connect to a remote host via SSH.
o scp [file] [user]@[host]:[destination]: Securely copy files between hosts.
o rsync -avz [source] [destination]: Synchronize files and directories.
• Firewall Management:
o ufw enable / ufw disable: Enable/disable the UFW firewall.
o ufw allow [port] / ufw deny [port]: Allow/deny traffic on a port.
o iptables -L: List firewall rules.
• Network Diagnostics:
o dig [domain]: Query DNS servers.
o nslookup [domain]: DNS lookup utility.
o nmap [host]: Network exploration and security auditing tool.
9. Security Practices
• User Permissions:
o sudo [command]: Execute a command as the superuser.
o visudo: Edit the sudoers file.
• File Encryption:
o gpg -c [file]: Encrypt a file with GPG.
o gpg -d [file.gpg]: Decrypt a GPG-encrypted file.
• System Auditing:
o auditctl -l: List active audit rules.
o ausearch -i: Search audit logs.
• SELinux:
o getenforce / setenforce [enforcing|permissive|disabled]: Get or set SELinux mode.
o semanage fcontext -a -t [type] '[file]': Set file security context.
• System Load:
o uptime: Display system uptime and load average.
o vmstat: Report virtual memory statistics.
o iostat: Report CPU and I/O statistics.
• Resource Usage:
o free -h: Display memory usage.
o df -h: Report file system disk space usage.
o sar: Collect, report, and save system activity information.
• Network Performance:
o iperf: Network bandwidth measurement tool.
o ethtool: Display or change ethernet device settings.
o tcpdump: Packet analyzer for network traffic.
• Log Files:
o /var/log/syslog: System log messages.
o /var/log/auth.log: Authentication log.
o /var/log/kern.log: Kernel log messages.
• Log Monitoring:
o tail -f /var/log/syslog: Follow live log file updates.
o journalctl: Query systemd journal logs.
o logger [message]: Add a message to the system log.
• Backup Tools:
o rsync -av --delete [source] [destination]: Synchronize and backup directories.
o tar -cvpzf [backup.tar.gz] [directories]: Create a full backup.
• Disk Imaging:
o dd if=[input] of=[output]: Create a disk image.
o partclone: Partition cloning and imaging tool.
• Recovery Techniques:
o fsck /dev/[device]: Repair a filesystem.
o testdisk: Recover lost partitions and files.
13. Virtualization
• Virtual Machines:
o virt-manager: GUI tool to manage virtual machines (KVM/QEMU).
o virsh: Command-line interface to manage virtual machines.
• Containers:
o docker ps: List running containers.
o docker images: List available images.
o docker run -d [image]: Run a container in detached mode.
• Container Orchestration:
o kubectl get pods: List Kubernetes pods.
o kubectl logs [pod]: View logs of a Kubernetes pod.
o kubectl apply -f [file.yaml]: Apply a Kubernetes configuration.
Troubleshooting & Advanced Topics
14. Troubleshooting Techniques
• System Diagnostics:
o dmesg: Print kernel ring buffer messages.
o strace [command]: Trace system calls and signals.
o lsof: List open files and the processes that opened them.
• Boot Issues:
o grub2-install: Reinstall GRUB bootloader.
o systemctl rescue: Boot into rescue mode.
o systemctl default: Switch back to the default target (usually graphical).
• Kernel Compilation:
o make menuconfig: Configure the Linux kernel.
o make bzImage: Compile the Linux kernel.
o make modules / make modules_install: Compile and install kernel modules.
• Kernel Modules:
o lsmod: List loaded kernel modules.
o modprobe [module]: Load a kernel module.
o rmmod [module]: Remove a kernel module.
• System Tuning:
o sysctl -p: Apply kernel parameter changes.
o ulimit -n [value]: Set the maximum number of open file descriptors.
o echo 1 > /proc/sys/vm/drop_caches: Clear the system cache.
2. Network Protocols
3. Network Services
5. Network Diagnostics
6. Network Configuration
7. Network Security
• Firewall Management:
o iptables: User-space utility to configure the IP packet filter rules of the
Linux kernel.
▪ Example: iptables -A INPUT -p tcp --dport 22 -j ACCEPT (allow SSH)
o firewalld: Dynamic firewall management tool.
▪ Example: firewall-cmd --add-port=22/tcp --permanent (open port 22 for
SSH)
• SELinux:
o getenforce: Check the current SELinux mode.
▪ Example: getenforce
o setenforce: Change SELinux mode.
▪ Example: setenforce 0 (set SELinux to permissive mode)
• VPN Configuration:
o openvpn: Set up and manage OpenVPN connections.
▪ Example: openvpn --config [config-file]
o strongSwan: IPsec-based VPN solutions.
▪ Example: ipsec status (check VPN status)
• Network Isolation:
o ip netns add [namespace]: Create a network namespace.
o ip netns exec [namespace] [command]: Run a command within a network
namespace.
o ip link set [interface] netns [namespace]: Move an interface to a network
namespace.
• Bridging:
o brctl: Bridge control.
▪ Example: brctl addbr [bridge-name] (create a bridge)
o ip link set [interface] master [bridge-name]: Add an interface to a bridge.
• Bonding:
o bonding: Aggregate multiple network interfaces into a single bond.
▪ Example: Configure /etc/modprobe.d/bonding.conf and
/etc/sysconfig/network-scripts/ifcfg-bond0.
• Synchronize Time:
o ntpdate [server]: Manually synchronize time with an NTP server.
o chrony: Modern replacement for NTP.
▪ Example: chronyc sources (list NTP sources).
• TLS/SSL:
o openssl: Generate and manage TLS/SSL certificates.
▪ Example: openssl req -new -x509 -keyout server.key -out server.crt -days 365
17. Setting Up a Basic Web Server
• Apache/Nginx:
o Apache: sudo apt-get install apache2 / sudo systemctl start apache2
o Nginx: sudo apt-get install nginx / sudo systemctl start nginx
• OpenVPN:
o Server Configuration: openvpn --config /etc/openvpn/server.conf
o Client Configuration: openvpn --config /etc/openvpn/client.conf
• Creating Files:
o touch [file]: Create an empty file or update timestamp.
o echo 'text' > [file]: Create a file with specific content.
o cat > [file]: Create a file and enter content interactively.
• Viewing Files:
o cat [file]: Concatenate and display file content.
o more [file] / less [file]: View file content page-by-page.
o head [file]: Display the first 10 lines of a file.
o tail [file]: Display the last 10 lines of a file.
• Editing Files:
o nano [file] / vi [file] / vim [file]: Text editors for modifying files.
• Copying, Moving, and Renaming:
o cp [source] [destination]: Copy files or directories.
o mv [source] [destination]: Move or rename files or directories.
o rsync [source] [destination]: Synchronize files and directories.
• Removing Files and Directories:
o rm [file]: Remove files.
o rm -r [directory]: Remove directories and their contents.
o find [directory] -type f -name [pattern] -exec rm {} \;: Find and remove files
matching a pattern.
• Viewing Permissions:
o ls -l [file]: Display file permissions, ownership, and other details.
• Changing Permissions:
o chmod [mode] [file]: Change file permissions.
▪ Example: chmod 755 [file] (owner can read/write/execute, others can
read/execute).
o chmod u+x [file]: Add execute permission for the file owner.
• Changing Ownership:
o chown [owner][:group] [file]: Change file owner and group.
▪ Example: chown user:group [file]
• Special Permissions:
o Setuid (s): Execute file with the file owner’s privileges.
▪ Example: chmod u+s [file]
o Setgid (s): Execute file with the group’s privileges or ensure new files
inherit group ownership.
▪ Example: chmod g+s [directory]
o Sticky Bit (t): Ensure only file owners can delete their files in a directory.
▪ Example: chmod +t [directory]
• Disk Usage:
o df -h: Display disk space usage in a human-readable format.
o du -sh [directory]: Show disk usage of a directory and its contents.
• File System Checks and Repairs:
o fsck [device]: Check and repair file systems.
o tune2fs -l [device]: Display file system information.
• Mounting and Unmounting:
o mount [device] [mount-point]: Mount a filesystem.
o umount [mount-point]: Unmount a filesystem.
o mount -t [type] [device] [mount-point]: Specify filesystem type (e.g., ext4, ntfs).
• Creating and Formatting Filesystems:
o mkfs -t [type] [device]: Create a filesystem (e.g., mkfs.ext4 /dev/sda1).
o mkfs.ext4 [device]: Format a partition with the ext4 filesystem.
o parted / fdisk: Partition management tools.
• Basic Search:
o find [directory] -name [pattern]: Find files by name.
o locate [pattern]: Find files using a pre-built index (requires updatedb).
o grep [pattern] [file]: Search for a pattern within a file.
• Advanced Search:
o find [directory] -type f -size +100M: Find files larger than 100 MB.
o grep -r [pattern] [directory]: Recursively search for a pattern in files.
o ack [pattern] [directory]: Search for patterns using ack, an advanced text
search tool.
• File Hashing:
o md5sum [file]: Generate MD5 hash of a file.
o sha256sum [file]: Generate SHA-256 hash of a file.
• File Auditing:
o auditd: Audit daemon for monitoring file access and modifications.
o ausearch: Search audit logs.
• SELinux and AppArmor:
o SELinux:
▪ getenforce: Check SELinux mode.
▪ setenforce 0: Set SELinux to permissive mode.
o AppArmor:
▪ aa-status: Check AppArmor status and profiles.
▪ aa-enforce [profile]: Enforce an AppArmor profile.
• Bash (Bourne Again Shell): Default shell in many Linux distributions, known for
its features and scripting capabilities.
• sh (Bourne Shell): Traditional Unix shell, simple and portable.
• zsh (Z Shell): Advanced shell with enhanced features like improved tab
completion.
• ksh (Korn Shell): Combines features of Bourne Shell and C Shell.
• Fish (Friendly Interactive Shell): User-friendly shell with an emphasis on
interactive use.
• Echo:
o echo "text": Display text on the screen.
• Variables:
o Definition: Store data to be used later.
o Syntax: variable_name=value
o Usage: echo $variable_name
o Example: name="John"; echo "Hello, $name"
• Comments:
o Single-line Comment: # This is a comment
o Multi-line Comment: : 'This is a multi-line comment'
• Conditionals:
o if [ condition ]; then ... fi: Basic conditional statement.
o Example:
Loops:
for i in {1..5}; do
echo "Number $i"
done
5. Functions
function_name() {
commands
}
• Example:
greet() {
echo "Hello, $1"
}
greet "Alice"
• Reading Input:
o read variable_name: Read user input.
o Example: read -p "Enter your name: " name
• Redirecting Output:
o >: Redirect output to a file, overwriting.
o >>: Append output to a file.
o Example: echo "Text" > file.txt
• Pipes:
o Usage: Chain commands by passing the output of one as input to another.
o Example: ls -l | grep "file"
• Error Handling:
o Standard Error: 2> (e.g., command 2> error.log)
o Redirecting Both Outputs: command > output.log 2>&1
7. Arrays
9. File Operations
• File Existence:
o -e: Check if file exists.
o Example: if [ -e file.txt ]; then echo "File exists"; fi
• File Type:
o -f: Regular file, -d: Directory, -s: Non-empty file.
o Example: if [ -d /path/to/dir ]; then echo "Directory exists"; fi
• Handling Files:
o cp, mv, rm: Copy, move, and remove files.
o Example: cp file1.txt file2.txt
• Background Processes:
o command &: Run a command in the background.
o jobs: List background jobs.
o fg %job_number: Bring a background job to the foreground.
• Process IDs:
o $$: Process ID of the script.
o $!: Process ID of the last background command.
• Killing Processes:
o kill [PID]: Terminate a process by ID.
o killall [process_name]: Terminate all instances of a process.
o pkill [pattern]: Terminate processes matching a pattern.
• Error Checking:
o set -e: Exit script on error.
o trap 'commands' ERR: Execute commands on error.
• Debugging:
o set -x: Print commands and their arguments as they are executed.
o bash -n [script.sh]: Check syntax without executing.
• Portability:
o Write portable scripts by adhering to POSIX standards.
• Documentation:
o Use comments and help functions to document scripts.
A. Regular Expressions
D. User Interaction
E. Command Substitution
F. Arithmetic Operations
• Syntax: $((expression))
• Example: result=$((5 + 3))
G. Network Operations
• Automating Backups:
#!/bin/bash
backup_dir="/backup/$(date +%F)"
mkdir -p $backup_dir
cp -r /important_data/* $backup_dir
18. Log Management
Rotate Logs:
#!/bin/bash
log_file="/var/log/myapp.log"
max_size=10485760 # 10 MB
#!/bin/bash
for user in user1 user2 user3; do
useradd $user
echo "$user:password" | chpasswd
done
20. Monitoring and Alerts
#!/bin/bash
threshold=90
used_space=$(df / | grep / | awk '{ print $5 }' |
sed 's/%//g')