Tools: basic OS commands

Tools: basic OS commands

Table of Contents

1. Linux Basics & File System

What is Linux?

Linux Distributions

Choosing a Distribution — Key Questions

Package Management (apt — Debian/Ubuntu)

Linux File System Hierarchy

OS and Kernel Information

2. Linux Commands & Navigation

Command Structure

The sudo Command

Directory Navigation

Absolute vs Relative Paths

Viewing Files

Creating & Manipulating Files

Searching for Files

The find Command

The locate Command

which & whereis

I/O Redirection & Pipes

Process Management

Getting Help

3. File Permissions & Ownership

Three Types of Owners

Three Types of Permissions

Octal Permission Table (MEMORIZE THIS!)

Reading Permission Strings

chmod — Change Permissions

chown — Change Ownership

⭐ DevOps Golden Rules for Permissions

4. Users & Groups Management

User Management Commands

Group Management Commands

Important Files

5. SSH (Secure Shell)

What is SSH?

Basic SSH Connection

SSH Service Control

SSH Configuration File

SSH Key-Based Authentication

SSH Config File (Client Side)

Non-Interactive SSH Commands

Disabling Root Login & Password Auth

6. Apache Web Server

Installation & Service Management

Apache runs on Port 80 (HTTP) or Port 443 (HTTPS)

Apache Default Paths

Deploying a Static Website

Virtual Hosts (Multiple Websites)

7. Nginx Web Server

Installation & Service Management

Nginx Key Paths

Nginx Virtual Host (Server Block)

8. Port Debugging & Troubleshooting

Common Scenario: Port Conflict

Debugging Commands

Fixing Port Conflicts Step by Step

Common Web Server Errors & Fixes

9. Bash Scripting

Script Basics

Variables

Command Line Arguments

Exit Codes

Conditionals (if/else)

Case Statement (for Flags)

For Loops

The tar Command (Archives/Backups)

The awk Command

Heredoc (Writing Multi-line Content)

Redirection in Scripts

Lab Script Examples

Script 1: backup.sh — Automated Directory Backup

Script 2: Virtual Host Automator

Script 3: wserver.sh — Nginx Install/Remove with Flags

Script 4: system_health.sh — HTML Report

Script 5: systeminfo.sh

10. Docker Containers

What is Docker?

Key Concepts

Verify Docker Installation

Docker Container Lifecycle

Managing Containers

Managing Images

Attach vs Exec (IMPORTANT DIFFERENCE!)

Container Logs & Inspect

Port Mapping (-p)

Volume Mounts (-v) / Bind Mounts

Complete Docker Command Reference

12. Quick Reference Cheat Sheet

systemctl Commands

Key File Paths

Permission Quick Reference

Docker Quick Reference

Bash Scripting Quick Reference Focus: Terminal-first, step-by-step command explanations for practical exam prep Linux is an open-source operating system kernel. A Linux Distribution (distro) is a collection of software built around the Linux kernel, all licensed under open-source or GPL. 90% of all cloud infrastructure is powered by Linux, making it the foundation of DevOps. 🔥 EXAM TIP: Always run apt update BEFORE apt install. Update refreshes the package list, upgrade actually updates the software. Linux organizes everything in a tree structure starting from / (root): 🔥 EXAM TIP: Know these paths by heart: /etc/ssh/sshd_config, /var/www/html, /var/log/apache2/, /etc/apache2/sites-available/ A Linux command has three parts: command [options] [arguments] sudo (superuser do) lets you run commands with root (administrator) privileges: The find command searches the filesystem in real-time. Extremely useful in DevOps for automation, cleanup, backups, and CI/CD scripts. 📝 NOTE: The {} is a placeholder filled with each file found. The ';' or \; terminates the -exec command. 🔥 EXAM TIP: > overwrites, >> appends. 2>&1 redirects errors to same place as stdout. The pipe | chains commands together. 📝 NOTE: You can only kill your own processes (unless you are root). 🔥 EXAM TIP: If a web page shows '403 Forbidden', first check permissions and ownership! Owner should be www-data, dirs 755, files 644. 📝 NOTE: The -aG flag is critical! Without -a, the user gets REMOVED from all other groups. Always use -aG together. SSH (Secure Shell) allows you to connect and log into remote systems securely. It encrypts ALL communication between client and server. SSH runs on port 22 by default. The server must have sshd (SSH daemon) installed and running. 🔥 EXAM TIP: systemctl enable makes service start on boot. systemctl start starts it NOW. They are different! You often need BOTH. The main SSH server config file is: /etc/ssh/sshd_config Key-based auth is more secure than passwords. It uses a pair of keys: 📝 NOTE: The remote server stores public keys in ~/.ssh/authorized_keys (one key per line). If permissions on this file are wrong, SSH will REFUSE the key and fall back to password auth. Create ~/.ssh/config to avoid typing username and IP every time: Run commands on a remote server without opening a full session: 🔥 EXAM TIP: Non-interactive SSH is key for scripting — you can loop over servers: for server in list; do ssh server "command"; done Virtual hosts let you run MULTIPLE websites on one server, each on a different port or domain. Content of mysite.conf: 🔥 EXAM TIP: sites-available = config files stored here. sites-enabled = symlinks to active configs. Use a2ensite/a2dissite to enable/disable. 📝 NOTE: Both Apache and Nginx use port 80 by default. They CANNOT run on the same port at the same time! If Apache fails to start with "Address already in use" error, another process (like Nginx) is already using port 80. 🔥 EXAM TIP: NO SPACES around = when assigning variables! NAME='value' is correct. NAME = 'value' is WRONG and will cause an error. Common test operators: Docker is a platform that lets you package, ship, and run applications in isolated environments called containers. A container is like a lightweight virtual machine that shares the host OS kernel. Think of it as: your app + all its dependencies, packaged together so it runs the same everywhere. 🔥 EXAM TIP: docker run ubuntu exits immediately because there is no long-running process. Use sleep, -it bash, or -d to keep it alive. 📝 NOTE: Use exec when you want to inspect a running container without affecting it. Use attach to reconnect to the main process. Port mapping connects a port on your HOST machine to a port inside the container. 🔥 EXAM TIP: Format is -p HOST_PORT:CONTAINER_PORT. The HOST port is what you type in the browser. The CONTAINER port is what the app listens on inside. Volumes let you share files between your host machine and the container. 📝 NOTE: The --rm flag automatically removes the container when it exits. Great for temporary containers. — list, create, add to sudo, switch user, create group Good luck on your exam! Practice these commands in the terminal — reading is not enough, you need muscle memory! 🚀 Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Command

Copy

$ -weight: 500;">apt -weight: 500;">update # Update the package index (local DB of available repos) -weight: 500;">apt -weight: 500;">upgrade # Upgrade installed packages to latest versions -weight: 500;">apt -weight: 500;">install <pkg_name> # Install a package -weight: 500;">apt -weight: 500;">remove <pkg_name> # Remove a package -weight: 500;">apt search <pkg_name> # Search for a package -weight: 500;">apt show <pkg_name> # Show package details -weight: 500;">apt list --installed # List all installed packages -weight: 500;">apt -weight: 500;">update # Update the package index (local DB of available repos) -weight: 500;">apt -weight: 500;">upgrade # Upgrade installed packages to latest versions -weight: 500;">apt -weight: 500;">install <pkg_name> # Install a package -weight: 500;">apt -weight: 500;">remove <pkg_name> # Remove a package -weight: 500;">apt search <pkg_name> # Search for a package -weight: 500;">apt show <pkg_name> # Show package details -weight: 500;">apt list --installed # List all installed packages -weight: 500;">apt -weight: 500;">update # Update the package index (local DB of available repos) -weight: 500;">apt -weight: 500;">upgrade # Upgrade installed packages to latest versions -weight: 500;">apt -weight: 500;">install <pkg_name> # Install a package -weight: 500;">apt -weight: 500;">remove <pkg_name> # Remove a package -weight: 500;">apt search <pkg_name> # Search for a package -weight: 500;">apt show <pkg_name> # Show package details -weight: 500;">apt list --installed # List all installed packages uname -a # Full system info (kernel name, version, architecture) lsb_release -a # Distribution-specific info cat /etc/os-release # OS release details uname -a # Full system info (kernel name, version, architecture) lsb_release -a # Distribution-specific info cat /etc/os-release # OS release details uname -a # Full system info (kernel name, version, architecture) lsb_release -a # Distribution-specific info cat /etc/os-release # OS release details wc -l devops_course_outline.txt # ^ ^ ^ # | | +-- argument (what to operate on) # | +------- option (modifies behavior, starts with - or --) # +------------ command (program to execute) wc -l devops_course_outline.txt # ^ ^ ^ # | | +-- argument (what to operate on) # | +------- option (modifies behavior, starts with - or --) # +------------ command (program to execute) wc -l devops_course_outline.txt # ^ ^ ^ # | | +-- argument (what to operate on) # | +------- option (modifies behavior, starts with - or --) # +------------ command (program to execute) -weight: 600;">sudo ls -la /root # List root's home directory with elevated privileges -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install nginx # Install software (needs root) -weight: 600;">sudo ls -la /root # List root's home directory with elevated privileges -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install nginx # Install software (needs root) -weight: 600;">sudo ls -la /root # List root's home directory with elevated privileges -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install nginx # Install software (needs root) cd /usr/bin # Absolute path cd ../usr/bin # Relative path cd /usr/bin # Absolute path cd ../usr/bin # Relative path cd /usr/bin # Absolute path cd ../usr/bin # Relative path touch newfile.txt # Create empty file (or -weight: 500;">update timestamp) touch -t 12091600 file.txt # Set specific timestamp (Dec 9, 4PM) echo "hello" > file.txt # Write to file (OVERWRITES!) echo "world" >> file.txt # Append to file cp source.txt dest.txt # Copy file mv old.txt new.txt # Rename/move file rm file.txt # Delete file rm -rf directory/ # Force delete directory and contents touch newfile.txt # Create empty file (or -weight: 500;">update timestamp) touch -t 12091600 file.txt # Set specific timestamp (Dec 9, 4PM) echo "hello" > file.txt # Write to file (OVERWRITES!) echo "world" >> file.txt # Append to file cp source.txt dest.txt # Copy file mv old.txt new.txt # Rename/move file rm file.txt # Delete file rm -rf directory/ # Force delete directory and contents touch newfile.txt # Create empty file (or -weight: 500;">update timestamp) touch -t 12091600 file.txt # Set specific timestamp (Dec 9, 4PM) echo "hello" > file.txt # Write to file (OVERWRITES!) echo "world" >> file.txt # Append to file cp source.txt dest.txt # Copy file mv old.txt new.txt # Rename/move file rm file.txt # Delete file rm -rf directory/ # Force delete directory and contents find /var/log -name "*.log" # Find all .log files in /var/log find /usr -name gcc # Find files/dirs named gcc find /usr -type d -name gcc # Find only DIRECTORIES named gcc find /usr -type f -name gcc # Find only regular FILES named gcc find / -size +10M # Find files larger than 10MB find / -size 0 # Find empty files find / -mtime 3 # Files modified 3 days ago find / -user root # Files owned by root find -name "*.jpg" -exec rm {} ';' # Find and DELETE all .jpg files find / -size +10M -exec command {} ';' # Find large files and run command find /var/log -name "*.log" # Find all .log files in /var/log find /usr -name gcc # Find files/dirs named gcc find /usr -type d -name gcc # Find only DIRECTORIES named gcc find /usr -type f -name gcc # Find only regular FILES named gcc find / -size +10M # Find files larger than 10MB find / -size 0 # Find empty files find / -mtime 3 # Files modified 3 days ago find / -user root # Files owned by root find -name "*.jpg" -exec rm {} ';' # Find and DELETE all .jpg files find / -size +10M -exec command {} ';' # Find large files and run command find /var/log -name "*.log" # Find all .log files in /var/log find /usr -name gcc # Find files/dirs named gcc find /usr -type d -name gcc # Find only DIRECTORIES named gcc find /usr -type f -name gcc # Find only regular FILES named gcc find / -size +10M # Find files larger than 10MB find / -size 0 # Find empty files find / -mtime 3 # Files modified 3 days ago find / -user root # Files owned by root find -name "*.jpg" -exec rm {} ';' # Find and DELETE all .jpg files find / -size +10M -exec command {} ';' # Find large files and run command locate zip # Fast search using pre-built database # Database is updated by 'updatedb' (runs daily automatically) locate zip # Fast search using pre-built database # Database is updated by 'updatedb' (runs daily automatically) locate zip # Fast search using pre-built database # Database is updated by 'updatedb' (runs daily automatically) which python3 # Shows path of executable: /usr/bin/python3 whereis python3 # Shows binary, source, and man page locations which python3 # Shows path of executable: /usr/bin/python3 whereis python3 # Shows binary, source, and man page locations which python3 # Shows path of executable: /usr/bin/python3 whereis python3 # Shows binary, source, and man page locations echo "hello" > file.txt # Redirect stdout to file (overwrite) echo "world" >> file.txt # Redirect stdout to file (append) command < input.txt # Feed file as stdin to command command 2> errors.txt # Redirect stderr to file command > out.txt 2>&1 # Redirect BOTH stdout AND stderr to file cat /etc/passwd | wc -l # Pipe: send output of cat as input to wc cat /etc/passwd | cut -d: -f1 | sort | uniq # Chain multiple pipes echo "hello" > file.txt # Redirect stdout to file (overwrite) echo "world" >> file.txt # Redirect stdout to file (append) command < input.txt # Feed file as stdin to command command 2> errors.txt # Redirect stderr to file command > out.txt 2>&1 # Redirect BOTH stdout AND stderr to file cat /etc/passwd | wc -l # Pipe: send output of cat as input to wc cat /etc/passwd | cut -d: -f1 | sort | uniq # Chain multiple pipes echo "hello" > file.txt # Redirect stdout to file (overwrite) echo "world" >> file.txt # Redirect stdout to file (append) command < input.txt # Feed file as stdin to command command 2> errors.txt # Redirect stderr to file command > out.txt 2>&1 # Redirect BOTH stdout AND stderr to file cat /etc/passwd | wc -l # Pipe: send output of cat as input to wc cat /etc/passwd | cut -d: -f1 | sort | uniq # Chain multiple pipes kill -SIGKILL <PID> # Force kill a process (same as kill -9) kill -9 <PID> # Force kill by PID node server.js & # Run process in background (& suffix) lsof -i :80 # Find what process is using port 80 kill -SIGKILL <PID> # Force kill a process (same as kill -9) kill -9 <PID> # Force kill by PID node server.js & # Run process in background (& suffix) lsof -i :80 # Find what process is using port 80 kill -SIGKILL <PID> # Force kill a process (same as kill -9) kill -9 <PID> # Force kill by PID node server.js & # Run process in background (& suffix) lsof -i :80 # Find what process is using port 80 man apache2 # Full manual page for a command man -k compress # Search man pages by keyword info make # GNU Info documentation (alternative to man) command --help # Quick help/usage summary (fastest option) man apache2 # Full manual page for a command man -k compress # Search man pages by keyword info make # GNU Info documentation (alternative to man) command --help # Quick help/usage summary (fastest option) man apache2 # Full manual page for a command man -k compress # Search man pages by keyword info make # GNU Info documentation (alternative to man) command --help # Quick help/usage summary (fastest option) -rw-r--r-- 1 sajid devops_students 13 2022-05-11 08:29 lecture.txt ^^^^^^^^^ ^ ^ | | +-- group owner | +-------- file owner (user) +-------------------- permission string First char: - = file, d = directory, l = symlink Next 9 chars: rwx for user | rwx for group | rwx for others Example: -rw-r--r-- means: User: rw- (read + write) = 4+2+0 = 6 Group: r-- (read only) = 4+0+0 = 4 Other: r-- (read only) = 4+0+0 = 4 Octal: 644 -rw-r--r-- 1 sajid devops_students 13 2022-05-11 08:29 lecture.txt ^^^^^^^^^ ^ ^ | | +-- group owner | +-------- file owner (user) +-------------------- permission string First char: - = file, d = directory, l = symlink Next 9 chars: rwx for user | rwx for group | rwx for others Example: -rw-r--r-- means: User: rw- (read + write) = 4+2+0 = 6 Group: r-- (read only) = 4+0+0 = 4 Other: r-- (read only) = 4+0+0 = 4 Octal: 644 -rw-r--r-- 1 sajid devops_students 13 2022-05-11 08:29 lecture.txt ^^^^^^^^^ ^ ^ | | +-- group owner | +-------- file owner (user) +-------------------- permission string First char: - = file, d = directory, l = symlink Next 9 chars: rwx for user | rwx for group | rwx for others Example: -rw-r--r-- means: User: rw- (read + write) = 4+2+0 = 6 Group: r-- (read only) = 4+0+0 = 4 Other: r-- (read only) = 4+0+0 = 4 Octal: 644 chmod 644 file.txt # rw-r--r-- (standard file permission) chmod 755 directory/ # rwxr-xr-x (standard directory permission) chmod 700 secret.txt # rwx------ (owner only, full access) chmod 666 file.txt # rw-rw-rw- (everyone can read/write) chmod +x script.sh # Add execute permission for everyone chmod u+x script.sh # Add execute for user only chmod g-w file.txt # Remove write from group chmod o-rwx file.txt # Remove all permissions from others chmod 644 file.txt # rw-r--r-- (standard file permission) chmod 755 directory/ # rwxr-xr-x (standard directory permission) chmod 700 secret.txt # rwx------ (owner only, full access) chmod 666 file.txt # rw-rw-rw- (everyone can read/write) chmod +x script.sh # Add execute permission for everyone chmod u+x script.sh # Add execute for user only chmod g-w file.txt # Remove write from group chmod o-rwx file.txt # Remove all permissions from others chmod 644 file.txt # rw-r--r-- (standard file permission) chmod 755 directory/ # rwxr-xr-x (standard directory permission) chmod 700 secret.txt # rwx------ (owner only, full access) chmod 666 file.txt # rw-rw-rw- (everyone can read/write) chmod +x script.sh # Add execute permission for everyone chmod u+x script.sh # Add execute for user only chmod g-w file.txt # Remove write from group chmod o-rwx file.txt # Remove all permissions from others -weight: 600;">sudo chown root:root file.txt # Change owner AND group to root -weight: 600;">sudo chown daniyal file.txt # Change owner only -weight: 600;">sudo chown daniyal:www-data file.txt # Change owner to daniyal, group to www-data -weight: 600;">sudo chown -R www-data:www-data /var/www/html/ # Recursive (all files inside) -weight: 600;">sudo chown root:root file.txt # Change owner AND group to root -weight: 600;">sudo chown daniyal file.txt # Change owner only -weight: 600;">sudo chown daniyal:www-data file.txt # Change owner to daniyal, group to www-data -weight: 600;">sudo chown -R www-data:www-data /var/www/html/ # Recursive (all files inside) -weight: 600;">sudo chown root:root file.txt # Change owner AND group to root -weight: 600;">sudo chown daniyal file.txt # Change owner only -weight: 600;">sudo chown daniyal:www-data file.txt # Change owner to daniyal, group to www-data -weight: 600;">sudo chown -R www-data:www-data /var/www/html/ # Recursive (all files inside) cat /etc/passwd # List ALL users on the system -weight: 600;">sudo adduser newuser # Create new user (interactive - sets password) -weight: 600;">sudo useradd newuser # Create user (non-interactive - no password set) -weight: 600;">sudo usermod -aG -weight: 600;">sudo newuser # Add user to -weight: 600;">sudo group (give admin rights) -weight: 600;">sudo userdel newuser # Delete a user -weight: 600;">sudo userdel -r newuser # Delete user AND their home directory su - newuser # Switch to another user whoami # Show current username id # Show user ID, group ID, and groups cat /etc/passwd # List ALL users on the system -weight: 600;">sudo adduser newuser # Create new user (interactive - sets password) -weight: 600;">sudo useradd newuser # Create user (non-interactive - no password set) -weight: 600;">sudo usermod -aG -weight: 600;">sudo newuser # Add user to -weight: 600;">sudo group (give admin rights) -weight: 600;">sudo userdel newuser # Delete a user -weight: 600;">sudo userdel -r newuser # Delete user AND their home directory su - newuser # Switch to another user whoami # Show current username id # Show user ID, group ID, and groups cat /etc/passwd # List ALL users on the system -weight: 600;">sudo adduser newuser # Create new user (interactive - sets password) -weight: 600;">sudo useradd newuser # Create user (non-interactive - no password set) -weight: 600;">sudo usermod -aG -weight: 600;">sudo newuser # Add user to -weight: 600;">sudo group (give admin rights) -weight: 600;">sudo userdel newuser # Delete a user -weight: 600;">sudo userdel -r newuser # Delete user AND their home directory su - newuser # Switch to another user whoami # Show current username id # Show user ID, group ID, and groups cat /etc/group # List all groups -weight: 600;">sudo groupadd devopsgroup # Create a new group -weight: 600;">sudo usermod -aG devopsgroup user1 # Add user1 to devopsgroup -weight: 600;">sudo groupdel devopsgroup # Delete a group groups username # Show which groups a user belongs to cat /etc/group # List all groups -weight: 600;">sudo groupadd devopsgroup # Create a new group -weight: 600;">sudo usermod -aG devopsgroup user1 # Add user1 to devopsgroup -weight: 600;">sudo groupdel devopsgroup # Delete a group groups username # Show which groups a user belongs to cat /etc/group # List all groups -weight: 600;">sudo groupadd devopsgroup # Create a new group -weight: 600;">sudo usermod -aG devopsgroup user1 # Add user1 to devopsgroup -weight: 600;">sudo groupdel devopsgroup # Delete a group groups username # Show which groups a user belongs to ssh username@remote_server_ip # Connect to remote machine ssh [email protected] # Example with IP whoami # Verify you are logged in hostname # Verify which machine you are on exit # Disconnect from remote ssh username@remote_server_ip # Connect to remote machine ssh [email protected] # Example with IP whoami # Verify you are logged in hostname # Verify which machine you are on exit # Disconnect from remote ssh username@remote_server_ip # Connect to remote machine ssh [email protected] # Example with IP whoami # Verify you are logged in hostname # Verify which machine you are on exit # Disconnect from remote -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status ssh # Check if SSH is running -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start ssh # Start SSH -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop ssh # Stop SSH -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart ssh # Restart (apply config changes) -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">enable ssh # Start automatically on boot -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">disable ssh # Don't -weight: 500;">start on boot -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status ssh # Check if SSH is running -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start ssh # Start SSH -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop ssh # Stop SSH -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart ssh # Restart (apply config changes) -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">enable ssh # Start automatically on boot -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">disable ssh # Don't -weight: 500;">start on boot -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status ssh # Check if SSH is running -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start ssh # Start SSH -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop ssh # Stop SSH -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart ssh # Restart (apply config changes) -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">enable ssh # Start automatically on boot -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">disable ssh # Don't -weight: 500;">start on boot -weight: 600;">sudo nano /etc/ssh/sshd_config # Edit SSH server configuration # Key settings to know: Port 22 # Default SSH port PermitRootLogin yes/no # Allow/block root login via SSH PasswordAuthentication yes/no # Allow/block password-based login PubkeyAuthentication yes # Allow key-based authentication # IMPORTANT: After ANY change, -weight: 500;">restart SSH! -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart ssh -weight: 600;">sudo nano /etc/ssh/sshd_config # Edit SSH server configuration # Key settings to know: Port 22 # Default SSH port PermitRootLogin yes/no # Allow/block root login via SSH PasswordAuthentication yes/no # Allow/block password-based login PubkeyAuthentication yes # Allow key-based authentication # IMPORTANT: After ANY change, -weight: 500;">restart SSH! -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart ssh -weight: 600;">sudo nano /etc/ssh/sshd_config # Edit SSH server configuration # Key settings to know: Port 22 # Default SSH port PermitRootLogin yes/no # Allow/block root login via SSH PasswordAuthentication yes/no # Allow/block password-based login PubkeyAuthentication yes # Allow key-based authentication # IMPORTANT: After ANY change, -weight: 500;">restart SSH! -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart ssh # STEP 1: Generate key pair on YOUR machine (client) ssh-keygen -t rsa # Press Enter for default location (~/.ssh/id_rsa) # Press Enter for no passphrase (or set one for extra security) # Creates: ~/.ssh/id_rsa (private) and ~/.ssh/id_rsa.pub (public) # STEP 2: Copy public key to the REMOTE server ssh-copy-id username@remote_server # OR manually: cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys # On the remote server # STEP 3: Set correct permissions (CRITICAL!) chmod 700 ~/.ssh # Directory: owner only chmod 600 ~/.ssh/id_rsa # Private key: owner only chmod 600 ~/.ssh/authorized_keys # Auth keys: owner only # STEP 4: Now you can login without password! ssh username@remote_server # STEP 1: Generate key pair on YOUR machine (client) ssh-keygen -t rsa # Press Enter for default location (~/.ssh/id_rsa) # Press Enter for no passphrase (or set one for extra security) # Creates: ~/.ssh/id_rsa (private) and ~/.ssh/id_rsa.pub (public) # STEP 2: Copy public key to the REMOTE server ssh-copy-id username@remote_server # OR manually: cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys # On the remote server # STEP 3: Set correct permissions (CRITICAL!) chmod 700 ~/.ssh # Directory: owner only chmod 600 ~/.ssh/id_rsa # Private key: owner only chmod 600 ~/.ssh/authorized_keys # Auth keys: owner only # STEP 4: Now you can login without password! ssh username@remote_server # STEP 1: Generate key pair on YOUR machine (client) ssh-keygen -t rsa # Press Enter for default location (~/.ssh/id_rsa) # Press Enter for no passphrase (or set one for extra security) # Creates: ~/.ssh/id_rsa (private) and ~/.ssh/id_rsa.pub (public) # STEP 2: Copy public key to the REMOTE server ssh-copy-id username@remote_server # OR manually: cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys # On the remote server # STEP 3: Set correct permissions (CRITICAL!) chmod 700 ~/.ssh # Directory: owner only chmod 600 ~/.ssh/id_rsa # Private key: owner only chmod 600 ~/.ssh/authorized_keys # Auth keys: owner only # STEP 4: Now you can login without password! ssh username@remote_server # File: ~/.ssh/config Host myserver HostName 192.168.1.100 User daniyal IdentityFile ~/.ssh/id_rsa # Now you can just type: ssh myserver # Instead of: ssh [email protected] # File: ~/.ssh/config Host myserver HostName 192.168.1.100 User daniyal IdentityFile ~/.ssh/id_rsa # Now you can just type: ssh myserver # Instead of: ssh [email protected] # File: ~/.ssh/config Host myserver HostName 192.168.1.100 User daniyal IdentityFile ~/.ssh/id_rsa # Now you can just type: ssh myserver # Instead of: ssh [email protected] ssh user@server "whoami" # Run whoami on remote ssh user@server "mkdir ~/test_dir" # Create dir on remote ssh user@server "cat /etc/hostname" # Read remote file ssh user@server "-weight: 500;">systemctl -weight: 500;">restart apache2" # Restart -weight: 500;">service remotely ssh user@server "whoami" # Run whoami on remote ssh user@server "mkdir ~/test_dir" # Create dir on remote ssh user@server "cat /etc/hostname" # Read remote file ssh user@server "-weight: 500;">systemctl -weight: 500;">restart apache2" # Restart -weight: 500;">service remotely ssh user@server "whoami" # Run whoami on remote ssh user@server "mkdir ~/test_dir" # Create dir on remote ssh user@server "cat /etc/hostname" # Read remote file ssh user@server "-weight: 500;">systemctl -weight: 500;">restart apache2" # Restart -weight: 500;">service remotely # Edit /etc/ssh/sshd_config: PermitRootLogin no # Block root from logging in via SSH PasswordAuthentication no # Force key-based auth only # Restart to apply: -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart ssh # Test: trying ssh root@server should now be DENIED # Edit /etc/ssh/sshd_config: PermitRootLogin no # Block root from logging in via SSH PasswordAuthentication no # Force key-based auth only # Restart to apply: -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart ssh # Test: trying ssh root@server should now be DENIED # Edit /etc/ssh/sshd_config: PermitRootLogin no # Block root from logging in via SSH PasswordAuthentication no # Force key-based auth only # Restart to apply: -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart ssh # Test: trying ssh root@server should now be DENIED -weight: 600;">sudo -weight: 500;">apt -weight: 500;">update -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install apache2 # Install Apache -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start apache2 # Start the -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop apache2 # Stop the -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart apache2 # Restart (apply changes) -weight: 600;">sudo -weight: 500;">systemctl reload apache2 # Reload config without downtime -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status apache2 # Check if running -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">enable apache2 # Start on boot -weight: 500;">curl localhost # Test: should show Apache default page -weight: 600;">sudo -weight: 500;">apt -weight: 500;">update -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install apache2 # Install Apache -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start apache2 # Start the -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop apache2 # Stop the -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart apache2 # Restart (apply changes) -weight: 600;">sudo -weight: 500;">systemctl reload apache2 # Reload config without downtime -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status apache2 # Check if running -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">enable apache2 # Start on boot -weight: 500;">curl localhost # Test: should show Apache default page -weight: 600;">sudo -weight: 500;">apt -weight: 500;">update -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install apache2 # Install Apache -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start apache2 # Start the -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop apache2 # Stop the -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart apache2 # Restart (apply changes) -weight: 600;">sudo -weight: 500;">systemctl reload apache2 # Reload config without downtime -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status apache2 # Check if running -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">enable apache2 # Start on boot -weight: 500;">curl localhost # Test: should show Apache default page # Step 1: Clone or copy your website files -weight: 600;">sudo -weight: 500;">git clone https://github.com/user/repo.-weight: 500;">git /var/www/html/mysite # Step 2: Set correct ownership -weight: 600;">sudo chown -R www-data:www-data /var/www/html/mysite # Step 3: Set correct permissions -weight: 600;">sudo chmod -R 755 /var/www/html/mysite/ # Directories -weight: 600;">sudo find /var/www/html/mysite -type f -exec chmod 644 {} \; # Files # Step 4: Verify -weight: 500;">curl localhost # Step 1: Clone or copy your website files -weight: 600;">sudo -weight: 500;">git clone https://github.com/user/repo.-weight: 500;">git /var/www/html/mysite # Step 2: Set correct ownership -weight: 600;">sudo chown -R www-data:www-data /var/www/html/mysite # Step 3: Set correct permissions -weight: 600;">sudo chmod -R 755 /var/www/html/mysite/ # Directories -weight: 600;">sudo find /var/www/html/mysite -type f -exec chmod 644 {} \; # Files # Step 4: Verify -weight: 500;">curl localhost # Step 1: Clone or copy your website files -weight: 600;">sudo -weight: 500;">git clone https://github.com/user/repo.-weight: 500;">git /var/www/html/mysite # Step 2: Set correct ownership -weight: 600;">sudo chown -R www-data:www-data /var/www/html/mysite # Step 3: Set correct permissions -weight: 600;">sudo chmod -R 755 /var/www/html/mysite/ # Directories -weight: 600;">sudo find /var/www/html/mysite -type f -exec chmod 644 {} \; # Files # Step 4: Verify -weight: 500;">curl localhost # Step 1: Create config file in sites-available -weight: 600;">sudo nano /etc/apache2/sites-available/mysite.conf # Step 1: Create config file in sites-available -weight: 600;">sudo nano /etc/apache2/sites-available/mysite.conf # Step 1: Create config file in sites-available -weight: 600;">sudo nano /etc/apache2/sites-available/mysite.conf <VirtualHost *:8080> ServerName mysite.local DocumentRoot /var/www/mysite ErrorLog /var/log/apache2/mysite-error.log CustomLog /var/log/apache2/mysite-access.log combined </VirtualHost> <VirtualHost *:8080> ServerName mysite.local DocumentRoot /var/www/mysite ErrorLog /var/log/apache2/mysite-error.log CustomLog /var/log/apache2/mysite-access.log combined </VirtualHost> <VirtualHost *:8080> ServerName mysite.local DocumentRoot /var/www/mysite ErrorLog /var/log/apache2/mysite-error.log CustomLog /var/log/apache2/mysite-access.log combined </VirtualHost> # Step 2: Add Listen directive for new port # Edit /etc/apache2/ports.conf and add: Listen 8080 # Step 3: Enable the site -weight: 600;">sudo a2ensite mysite.conf # Step 4: Test config and reload -weight: 600;">sudo apache2ctl configtest # Check for syntax errors -weight: 600;">sudo -weight: 500;">systemctl reload apache2 # Step 5: Verify -weight: 500;">curl localhost:8080 # Step 2: Add Listen directive for new port # Edit /etc/apache2/ports.conf and add: Listen 8080 # Step 3: Enable the site -weight: 600;">sudo a2ensite mysite.conf # Step 4: Test config and reload -weight: 600;">sudo apache2ctl configtest # Check for syntax errors -weight: 600;">sudo -weight: 500;">systemctl reload apache2 # Step 5: Verify -weight: 500;">curl localhost:8080 # Step 2: Add Listen directive for new port # Edit /etc/apache2/ports.conf and add: Listen 8080 # Step 3: Enable the site -weight: 600;">sudo a2ensite mysite.conf # Step 4: Test config and reload -weight: 600;">sudo apache2ctl configtest # Check for syntax errors -weight: 600;">sudo -weight: 500;">systemctl reload apache2 # Step 5: Verify -weight: 500;">curl localhost:8080 -weight: 600;">sudo -weight: 500;">apt -weight: 500;">update -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">enable nginx -weight: 500;">curl localhost # Test -weight: 600;">sudo -weight: 500;">apt -weight: 500;">update -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">enable nginx -weight: 500;">curl localhost # Test -weight: 600;">sudo -weight: 500;">apt -weight: 500;">update -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">restart nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status nginx -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">enable nginx -weight: 500;">curl localhost # Test # File: /etc/nginx/sites-available/mysite server { listen 80; server_name mysite.local; root /var/www/mysite; index index.html; location / { try_files $uri $uri/ =404; } } # File: /etc/nginx/sites-available/mysite server { listen 80; server_name mysite.local; root /var/www/mysite; index index.html; location / { try_files $uri $uri/ =404; } } # File: /etc/nginx/sites-available/mysite server { listen 80; server_name mysite.local; root /var/www/mysite; index index.html; location / { try_files $uri $uri/ =404; } } # Enable the site by creating a symlink: -weight: 600;">sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/ # Test config: -weight: 600;">sudo nginx -t # Reload: -weight: 600;">sudo -weight: 500;">systemctl reload nginx # Enable the site by creating a symlink: -weight: 600;">sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/ # Test config: -weight: 600;">sudo nginx -t # Reload: -weight: 600;">sudo -weight: 500;">systemctl reload nginx # Enable the site by creating a symlink: -weight: 600;">sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/ # Test config: -weight: 600;">sudo nginx -t # Reload: -weight: 600;">sudo -weight: 500;">systemctl reload nginx # Check what's using a specific port -weight: 600;">sudo lsof -i :80 # Show process using port 80 # Production-friendly command (all open network connections) -weight: 600;">sudo lsof -i -P -n # -i = open network connections # -P = show port numbers (not -weight: 500;">service names) # -n = don't resolve hostnames # Check -weight: 500;">service logs for errors -weight: 600;">sudo journalctl -xe # Show recent system logs with details -weight: 600;">sudo journalctl -u apache2 # Show only Apache logs # Monitor log files in real-time -weight: 600;">sudo tail -f /var/log/apache2/error.log -weight: 600;">sudo tail -f /var/log/nginx/error.log # Check what's using a specific port -weight: 600;">sudo lsof -i :80 # Show process using port 80 # Production-friendly command (all open network connections) -weight: 600;">sudo lsof -i -P -n # -i = open network connections # -P = show port numbers (not -weight: 500;">service names) # -n = don't resolve hostnames # Check -weight: 500;">service logs for errors -weight: 600;">sudo journalctl -xe # Show recent system logs with details -weight: 600;">sudo journalctl -u apache2 # Show only Apache logs # Monitor log files in real-time -weight: 600;">sudo tail -f /var/log/apache2/error.log -weight: 600;">sudo tail -f /var/log/nginx/error.log # Check what's using a specific port -weight: 600;">sudo lsof -i :80 # Show process using port 80 # Production-friendly command (all open network connections) -weight: 600;">sudo lsof -i -P -n # -i = open network connections # -P = show port numbers (not -weight: 500;">service names) # -n = don't resolve hostnames # Check -weight: 500;">service logs for errors -weight: 600;">sudo journalctl -xe # Show recent system logs with details -weight: 600;">sudo journalctl -u apache2 # Show only Apache logs # Monitor log files in real-time -weight: 600;">sudo tail -f /var/log/apache2/error.log -weight: 600;">sudo tail -f /var/log/nginx/error.log # Step 1: Check which -weight: 500;">service is using port 80 -weight: 600;">sudo lsof -i :80 # Output shows: nginx (PID 1234) # Step 2: Stop the conflicting -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop nginx # Step 3: Start the -weight: 500;">service you want -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start apache2 # Step 4: Verify -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status apache2 -weight: 500;">curl localhost # Step 1: Check which -weight: 500;">service is using port 80 -weight: 600;">sudo lsof -i :80 # Output shows: nginx (PID 1234) # Step 2: Stop the conflicting -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop nginx # Step 3: Start the -weight: 500;">service you want -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start apache2 # Step 4: Verify -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status apache2 -weight: 500;">curl localhost # Step 1: Check which -weight: 500;">service is using port 80 -weight: 600;">sudo lsof -i :80 # Output shows: nginx (PID 1234) # Step 2: Stop the conflicting -weight: 500;">service -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">stop nginx # Step 3: Start the -weight: 500;">service you want -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start apache2 # Step 4: Verify -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">status apache2 -weight: 500;">curl localhost #!/bin/bash # The first line is the SHEBANG — tells the system to use bash # Every script MUST -weight: 500;">start with #!/bin/bash # Make script executable: chmod +x script.sh # Run script: ./script.sh # OR bash script.sh #!/bin/bash # The first line is the SHEBANG — tells the system to use bash # Every script MUST -weight: 500;">start with #!/bin/bash # Make script executable: chmod +x script.sh # Run script: ./script.sh # OR bash script.sh #!/bin/bash # The first line is the SHEBANG — tells the system to use bash # Every script MUST -weight: 500;">start with #!/bin/bash # Make script executable: chmod +x script.sh # Run script: ./script.sh # OR bash script.sh #!/bin/bash NAME="Daniyal" # Assign (NO spaces around =) echo "Hello $NAME" # Use variable with $ echo "Hello ${NAME}" # Curly braces version (safer) # Read user input echo "Enter your name:" read USERNAME # Stores input in USERNAME echo "Welcome $USERNAME" # Command substitution — store command output in variable TODAY=$(date) # Run date command, store result echo "Today is: $TODAY" HOSTNAME=$(hostname) echo "Machine: $HOSTNAME" #!/bin/bash NAME="Daniyal" # Assign (NO spaces around =) echo "Hello $NAME" # Use variable with $ echo "Hello ${NAME}" # Curly braces version (safer) # Read user input echo "Enter your name:" read USERNAME # Stores input in USERNAME echo "Welcome $USERNAME" # Command substitution — store command output in variable TODAY=$(date) # Run date command, store result echo "Today is: $TODAY" HOSTNAME=$(hostname) echo "Machine: $HOSTNAME" #!/bin/bash NAME="Daniyal" # Assign (NO spaces around =) echo "Hello $NAME" # Use variable with $ echo "Hello ${NAME}" # Curly braces version (safer) # Read user input echo "Enter your name:" read USERNAME # Stores input in USERNAME echo "Welcome $USERNAME" # Command substitution — store command output in variable TODAY=$(date) # Run date command, store result echo "Today is: $TODAY" HOSTNAME=$(hostname) echo "Machine: $HOSTNAME" #!/bin/bash # Example: ./myscript.sh hello world echo "Script name: $0" # ./myscript.sh echo "First arg: $1" # hello echo "Second arg: $2" # world echo "Total args: $#" # 2 echo "All args: $@" # hello world #!/bin/bash # Example: ./myscript.sh hello world echo "Script name: $0" # ./myscript.sh echo "First arg: $1" # hello echo "Second arg: $2" # world echo "Total args: $#" # 2 echo "All args: $@" # hello world #!/bin/bash # Example: ./myscript.sh hello world echo "Script name: $0" # ./myscript.sh echo "First arg: $1" # hello echo "Second arg: $2" # world echo "Total args: $#" # 2 echo "All args: $@" # hello world #!/bin/bash # Every command returns an exit code # 0 = SUCCESS, anything else = FAILURE ls /tmp echo $? # 0 (success — /tmp exists) ls /nonexistent echo $? # 2 (failure — directory doesn't exist) # Set your own exit code exit 0 # Exit script with success exit 1 # Exit script with failure #!/bin/bash # Every command returns an exit code # 0 = SUCCESS, anything else = FAILURE ls /tmp echo $? # 0 (success — /tmp exists) ls /nonexistent echo $? # 2 (failure — directory doesn't exist) # Set your own exit code exit 0 # Exit script with success exit 1 # Exit script with failure #!/bin/bash # Every command returns an exit code # 0 = SUCCESS, anything else = FAILURE ls /tmp echo $? # 0 (success — /tmp exists) ls /nonexistent echo $? # 2 (failure — directory doesn't exist) # Set your own exit code exit 0 # Exit script with success exit 1 # Exit script with failure #!/bin/bash # Basic if/else if [ -d "/var/www/html" ]; then echo "Directory exists" else echo "Directory does NOT exist" fi # Check if file exists if [ -f "/etc/ssh/sshd_config" ]; then echo "SSH config found" fi # Compare strings if [ "$1" == "-weight: 500;">install" ]; then echo "Installing..." elif [ "$1" == "-weight: 500;">remove" ]; then echo "Removing..." else echo "Usage: $0 -weight: 500;">install|-weight: 500;">remove" exit 1 fi # Check if command succeeded -weight: 500;">apt -weight: 500;">install nginx -y if [ $? -eq 0 ]; then echo "Installation successful" else echo "Installation FAILED" exit 1 fi #!/bin/bash # Basic if/else if [ -d "/var/www/html" ]; then echo "Directory exists" else echo "Directory does NOT exist" fi # Check if file exists if [ -f "/etc/ssh/sshd_config" ]; then echo "SSH config found" fi # Compare strings if [ "$1" == "-weight: 500;">install" ]; then echo "Installing..." elif [ "$1" == "-weight: 500;">remove" ]; then echo "Removing..." else echo "Usage: $0 -weight: 500;">install|-weight: 500;">remove" exit 1 fi # Check if command succeeded -weight: 500;">apt -weight: 500;">install nginx -y if [ $? -eq 0 ]; then echo "Installation successful" else echo "Installation FAILED" exit 1 fi #!/bin/bash # Basic if/else if [ -d "/var/www/html" ]; then echo "Directory exists" else echo "Directory does NOT exist" fi # Check if file exists if [ -f "/etc/ssh/sshd_config" ]; then echo "SSH config found" fi # Compare strings if [ "$1" == "-weight: 500;">install" ]; then echo "Installing..." elif [ "$1" == "-weight: 500;">remove" ]; then echo "Removing..." else echo "Usage: $0 -weight: 500;">install|-weight: 500;">remove" exit 1 fi # Check if command succeeded -weight: 500;">apt -weight: 500;">install nginx -y if [ $? -eq 0 ]; then echo "Installation successful" else echo "Installation FAILED" exit 1 fi #!/bin/bash # Great for handling command-line flags like -i, -r, -h case "$1" in -i|---weight: 500;">install) echo "Installing..." ;; -r|---weight: 500;">remove) echo "Removing..." ;; -h|--help) echo "Usage: $0 [-i -weight: 500;">install] [-r -weight: 500;">remove] [-h help]" ;; *) echo "Invalid option: $1" echo "Use -h for help" exit 1 ;; esac #!/bin/bash # Great for handling command-line flags like -i, -r, -h case "$1" in -i|---weight: 500;">install) echo "Installing..." ;; -r|---weight: 500;">remove) echo "Removing..." ;; -h|--help) echo "Usage: $0 [-i -weight: 500;">install] [-r -weight: 500;">remove] [-h help]" ;; *) echo "Invalid option: $1" echo "Use -h for help" exit 1 ;; esac #!/bin/bash # Great for handling command-line flags like -i, -r, -h case "$1" in -i|---weight: 500;">install) echo "Installing..." ;; -r|---weight: 500;">remove) echo "Removing..." ;; -h|--help) echo "Usage: $0 [-i -weight: 500;">install] [-r -weight: 500;">remove] [-h help]" ;; *) echo "Invalid option: $1" echo "Use -h for help" exit 1 ;; esac #!/bin/bash # Loop through a list for server in server1 server2 server3; do echo "Restarting $server..." ssh $server "-weight: 500;">systemctl -weight: 500;">restart apache2" done # Loop through files for file in *.sh; do echo "Found script: $file" done # Loop with numbers for i in 1 2 3 4 5; do echo "Number: $i" done # C-style loop for ((i=1; i<=5; i++)); do echo "Count: $i" done #!/bin/bash # Loop through a list for server in server1 server2 server3; do echo "Restarting $server..." ssh $server "-weight: 500;">systemctl -weight: 500;">restart apache2" done # Loop through files for file in *.sh; do echo "Found script: $file" done # Loop with numbers for i in 1 2 3 4 5; do echo "Number: $i" done # C-style loop for ((i=1; i<=5; i++)); do echo "Count: $i" done #!/bin/bash # Loop through a list for server in server1 server2 server3; do echo "Restarting $server..." ssh $server "-weight: 500;">systemctl -weight: 500;">restart apache2" done # Loop through files for file in *.sh; do echo "Found script: $file" done # Loop with numbers for i in 1 2 3 4 5; do echo "Number: $i" done # C-style loop for ((i=1; i<=5; i++)); do echo "Count: $i" done # Create a compressed archive tar -czf backup.tar.gz /path/to/directory # -c = create # -z = compress with gzip # -f = filename follows # Create with timestamp in name tar -czf backup_$(date +%Y%m%d_%H%M%S).tar.gz /var/www/html/ # Extract an archive tar -xzf backup.tar.gz # -x = extract # List contents without extracting tar -tzf backup.tar.gz # Create a compressed archive tar -czf backup.tar.gz /path/to/directory # -c = create # -z = compress with gzip # -f = filename follows # Create with timestamp in name tar -czf backup_$(date +%Y%m%d_%H%M%S).tar.gz /var/www/html/ # Extract an archive tar -xzf backup.tar.gz # -x = extract # List contents without extracting tar -tzf backup.tar.gz # Create a compressed archive tar -czf backup.tar.gz /path/to/directory # -c = create # -z = compress with gzip # -f = filename follows # Create with timestamp in name tar -czf backup_$(date +%Y%m%d_%H%M%S).tar.gz /var/www/html/ # Extract an archive tar -xzf backup.tar.gz # -x = extract # List contents without extracting tar -tzf backup.tar.gz # awk processes text line by line, splitting into fields # Default field separator is whitespace # Print second column of output df -h | awk '{print $2}' # Print specific fields with custom format free -m | awk '/Mem:/ {print "Total: "$2" MB, Used: "$3" MB"}' # Use custom field separator cat /etc/passwd | awk -F: '{print $1}' # Print usernames (field 1, : separator) # Filter lines matching a pattern df -h | awk '!/tmpfs/ {print $0}' # Exclude lines containing tmpfs # awk processes text line by line, splitting into fields # Default field separator is whitespace # Print second column of output df -h | awk '{print $2}' # Print specific fields with custom format free -m | awk '/Mem:/ {print "Total: "$2" MB, Used: "$3" MB"}' # Use custom field separator cat /etc/passwd | awk -F: '{print $1}' # Print usernames (field 1, : separator) # Filter lines matching a pattern df -h | awk '!/tmpfs/ {print $0}' # Exclude lines containing tmpfs # awk processes text line by line, splitting into fields # Default field separator is whitespace # Print second column of output df -h | awk '{print $2}' # Print specific fields with custom format free -m | awk '/Mem:/ {print "Total: "$2" MB, Used: "$3" MB"}' # Use custom field separator cat /etc/passwd | awk -F: '{print $1}' # Print usernames (field 1, : separator) # Filter lines matching a pattern df -h | awk '!/tmpfs/ {print $0}' # Exclude lines containing tmpfs #!/bin/bash # Heredoc writes multi-line content to a file cat << EOF > /var/www/html/index.html <!DOCTYPE html> <html> <head><title>System Report</title></head> <body> <h1>Server: $(hostname)</h1> <p>Date: $(date)</p> </body> </html> EOF # Variables inside heredoc get expanded! # Use << 'EOF' (with quotes) to PREVENT variable expansion #!/bin/bash # Heredoc writes multi-line content to a file cat << EOF > /var/www/html/index.html <!DOCTYPE html> <html> <head><title>System Report</title></head> <body> <h1>Server: $(hostname)</h1> <p>Date: $(date)</p> </body> </html> EOF # Variables inside heredoc get expanded! # Use << 'EOF' (with quotes) to PREVENT variable expansion #!/bin/bash # Heredoc writes multi-line content to a file cat << EOF > /var/www/html/index.html <!DOCTYPE html> <html> <head><title>System Report</title></head> <body> <h1>Server: $(hostname)</h1> <p>Date: $(date)</p> </body> </html> EOF # Variables inside heredoc get expanded! # Use << 'EOF' (with quotes) to PREVENT variable expansion #!/bin/bash # Redirect command output to log file -weight: 500;">apt -weight: 500;">install nginx -y > nginx.log 2>&1 # > nginx.log = redirect stdout to file # 2>&1 = redirect stderr (2) to same place as stdout (1) # Combined: ALL output (normal + errors) goes to nginx.log # Append to log echo "$(date) - Installation complete" >> nginx.log #!/bin/bash # Redirect command output to log file -weight: 500;">apt -weight: 500;">install nginx -y > nginx.log 2>&1 # > nginx.log = redirect stdout to file # 2>&1 = redirect stderr (2) to same place as stdout (1) # Combined: ALL output (normal + errors) goes to nginx.log # Append to log echo "$(date) - Installation complete" >> nginx.log #!/bin/bash # Redirect command output to log file -weight: 500;">apt -weight: 500;">install nginx -y > nginx.log 2>&1 # > nginx.log = redirect stdout to file # 2>&1 = redirect stderr (2) to same place as stdout (1) # Combined: ALL output (normal + errors) goes to nginx.log # Append to log echo "$(date) - Installation complete" >> nginx.log #!/bin/bash echo "Enter directory path to backup:" read DIR_PATH if [ ! -d "$DIR_PATH" ]; then echo "ERROR: Directory $DIR_PATH does not exist!" exit 1 fi TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="backup_${TIMESTAMP}.tar.gz" tar -czf "$BACKUP_FILE" "$DIR_PATH" if [ $? -eq 0 ]; then echo "SUCCESS: Backup created: $BACKUP_FILE" echo "$(date) - SUCCESS - $BACKUP_FILE" >> backup.log else echo "FAILED: Backup failed!" echo "$(date) - FAILED" >> backup.log exit 1 fi #!/bin/bash echo "Enter directory path to backup:" read DIR_PATH if [ ! -d "$DIR_PATH" ]; then echo "ERROR: Directory $DIR_PATH does not exist!" exit 1 fi TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="backup_${TIMESTAMP}.tar.gz" tar -czf "$BACKUP_FILE" "$DIR_PATH" if [ $? -eq 0 ]; then echo "SUCCESS: Backup created: $BACKUP_FILE" echo "$(date) - SUCCESS - $BACKUP_FILE" >> backup.log else echo "FAILED: Backup failed!" echo "$(date) - FAILED" >> backup.log exit 1 fi #!/bin/bash echo "Enter directory path to backup:" read DIR_PATH if [ ! -d "$DIR_PATH" ]; then echo "ERROR: Directory $DIR_PATH does not exist!" exit 1 fi TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="backup_${TIMESTAMP}.tar.gz" tar -czf "$BACKUP_FILE" "$DIR_PATH" if [ $? -eq 0 ]; then echo "SUCCESS: Backup created: $BACKUP_FILE" echo "$(date) - SUCCESS - $BACKUP_FILE" >> backup.log else echo "FAILED: Backup failed!" echo "$(date) - FAILED" >> backup.log exit 1 fi #!/bin/bash echo "Enter domain/directory name:" read DOMAIN -weight: 600;">sudo mkdir -p /var/www/$DOMAIN -weight: 600;">sudo cat << EOF > /var/www/$DOMAIN/index.html <html><body><h1>Welcome to $DOMAIN</h1></body></html> EOF -weight: 600;">sudo cat << EOF > /etc/apache2/sites-available/$DOMAIN.conf <VirtualHost *:80> ServerName $DOMAIN DocumentRoot /var/www/$DOMAIN ErrorLog /var/log/apache2/${DOMAIN}-error.log CustomLog /var/log/apache2/${DOMAIN}-access.log combined </VirtualHost> EOF -weight: 600;">sudo chown -R www-data:www-data /var/www/$DOMAIN -weight: 600;">sudo chmod -R 755 /var/www/$DOMAIN -weight: 600;">sudo a2ensite $DOMAIN.conf -weight: 600;">sudo -weight: 500;">systemctl reload apache2 echo "Website $DOMAIN deployed successfully!" #!/bin/bash echo "Enter domain/directory name:" read DOMAIN -weight: 600;">sudo mkdir -p /var/www/$DOMAIN -weight: 600;">sudo cat << EOF > /var/www/$DOMAIN/index.html <html><body><h1>Welcome to $DOMAIN</h1></body></html> EOF -weight: 600;">sudo cat << EOF > /etc/apache2/sites-available/$DOMAIN.conf <VirtualHost *:80> ServerName $DOMAIN DocumentRoot /var/www/$DOMAIN ErrorLog /var/log/apache2/${DOMAIN}-error.log CustomLog /var/log/apache2/${DOMAIN}-access.log combined </VirtualHost> EOF -weight: 600;">sudo chown -R www-data:www-data /var/www/$DOMAIN -weight: 600;">sudo chmod -R 755 /var/www/$DOMAIN -weight: 600;">sudo a2ensite $DOMAIN.conf -weight: 600;">sudo -weight: 500;">systemctl reload apache2 echo "Website $DOMAIN deployed successfully!" #!/bin/bash echo "Enter domain/directory name:" read DOMAIN -weight: 600;">sudo mkdir -p /var/www/$DOMAIN -weight: 600;">sudo cat << EOF > /var/www/$DOMAIN/index.html <html><body><h1>Welcome to $DOMAIN</h1></body></html> EOF -weight: 600;">sudo cat << EOF > /etc/apache2/sites-available/$DOMAIN.conf <VirtualHost *:80> ServerName $DOMAIN DocumentRoot /var/www/$DOMAIN ErrorLog /var/log/apache2/${DOMAIN}-error.log CustomLog /var/log/apache2/${DOMAIN}-access.log combined </VirtualHost> EOF -weight: 600;">sudo chown -R www-data:www-data /var/www/$DOMAIN -weight: 600;">sudo chmod -R 755 /var/www/$DOMAIN -weight: 600;">sudo a2ensite $DOMAIN.conf -weight: 600;">sudo -weight: 500;">systemctl reload apache2 echo "Website $DOMAIN deployed successfully!" #!/bin/bash case "$1" in -i) if dpkg -l | grep -q nginx; then echo "Nginx is already installed." exit 0 fi -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install nginx -y > nginx.log 2>&1 if [ $? -eq 0 ]; then echo "Nginx installed successfully." else echo "ERROR: Installation failed. Check nginx.log" exit 1 fi ;; -r) if ! dpkg -l | grep -q nginx; then echo "Nginx is not installed." exit 0 fi -weight: 600;">sudo -weight: 500;">apt -weight: 500;">remove nginx -y > nginx.log 2>&1 if [ $? -eq 0 ]; then echo "Nginx removed successfully." else echo "ERROR: Removal failed. Check nginx.log" exit 1 fi ;; *) echo "Usage: $0 [-i -weight: 500;">install] [-r -weight: 500;">remove]" exit 1 ;; esac #!/bin/bash case "$1" in -i) if dpkg -l | grep -q nginx; then echo "Nginx is already installed." exit 0 fi -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install nginx -y > nginx.log 2>&1 if [ $? -eq 0 ]; then echo "Nginx installed successfully." else echo "ERROR: Installation failed. Check nginx.log" exit 1 fi ;; -r) if ! dpkg -l | grep -q nginx; then echo "Nginx is not installed." exit 0 fi -weight: 600;">sudo -weight: 500;">apt -weight: 500;">remove nginx -y > nginx.log 2>&1 if [ $? -eq 0 ]; then echo "Nginx removed successfully." else echo "ERROR: Removal failed. Check nginx.log" exit 1 fi ;; *) echo "Usage: $0 [-i -weight: 500;">install] [-r -weight: 500;">remove]" exit 1 ;; esac #!/bin/bash case "$1" in -i) if dpkg -l | grep -q nginx; then echo "Nginx is already installed." exit 0 fi -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install nginx -y > nginx.log 2>&1 if [ $? -eq 0 ]; then echo "Nginx installed successfully." else echo "ERROR: Installation failed. Check nginx.log" exit 1 fi ;; -r) if ! dpkg -l | grep -q nginx; then echo "Nginx is not installed." exit 0 fi -weight: 600;">sudo -weight: 500;">apt -weight: 500;">remove nginx -y > nginx.log 2>&1 if [ $? -eq 0 ]; then echo "Nginx removed successfully." else echo "ERROR: Removal failed. Check nginx.log" exit 1 fi ;; *) echo "Usage: $0 [-i -weight: 500;">install] [-r -weight: 500;">remove]" exit 1 ;; esac #!/bin/bash TOTAL_MEM=$(free -m | awk '/Mem:/ {print $2}') USED_MEM=$(free -m | awk '/Mem:/ {print $3}') AVAIL_MEM=$(free -m | awk '/Mem:/ {print $7}') TOTAL_SWAP=$(free -m | awk '/Swap:/ {print $2}') USED_SWAP=$(free -m | awk '/Swap:/ {print $3}') DISK_TOTAL=$(df -h --total --exclude-type=tmpfs | awk '/total/ {print $2}') DISK_USED=$(df -h --total --exclude-type=tmpfs | awk '/total/ {print $3}') DISK_AVAIL=$(df -h --total --exclude-type=tmpfs | awk '/total/ {print $4}') cat << EOF > system_health.html <!DOCTYPE html> <html> <head><title>System Health Report</title></head> <body> <h1>System Health Report</h1> <p>Date: $(date)</p> <h2>Memory Usage</h2> <p>Total: ${TOTAL_MEM}MB | Used: ${USED_MEM}MB | Available: ${AVAIL_MEM}MB</p> <h2>Disk Usage</h2> <p>Total: $DISK_TOTAL | Used: $DISK_USED | Available: $DISK_AVAIL</p> </body> </html> EOF -weight: 600;">sudo cp system_health.html /var/www/html/index.html if [ $? -eq 0 ]; then echo "SUCCESS: Report hosted at http://localhost" else echo "ERROR: Failed to deploy report" exit 1 fi #!/bin/bash TOTAL_MEM=$(free -m | awk '/Mem:/ {print $2}') USED_MEM=$(free -m | awk '/Mem:/ {print $3}') AVAIL_MEM=$(free -m | awk '/Mem:/ {print $7}') TOTAL_SWAP=$(free -m | awk '/Swap:/ {print $2}') USED_SWAP=$(free -m | awk '/Swap:/ {print $3}') DISK_TOTAL=$(df -h --total --exclude-type=tmpfs | awk '/total/ {print $2}') DISK_USED=$(df -h --total --exclude-type=tmpfs | awk '/total/ {print $3}') DISK_AVAIL=$(df -h --total --exclude-type=tmpfs | awk '/total/ {print $4}') cat << EOF > system_health.html <!DOCTYPE html> <html> <head><title>System Health Report</title></head> <body> <h1>System Health Report</h1> <p>Date: $(date)</p> <h2>Memory Usage</h2> <p>Total: ${TOTAL_MEM}MB | Used: ${USED_MEM}MB | Available: ${AVAIL_MEM}MB</p> <h2>Disk Usage</h2> <p>Total: $DISK_TOTAL | Used: $DISK_USED | Available: $DISK_AVAIL</p> </body> </html> EOF -weight: 600;">sudo cp system_health.html /var/www/html/index.html if [ $? -eq 0 ]; then echo "SUCCESS: Report hosted at http://localhost" else echo "ERROR: Failed to deploy report" exit 1 fi #!/bin/bash TOTAL_MEM=$(free -m | awk '/Mem:/ {print $2}') USED_MEM=$(free -m | awk '/Mem:/ {print $3}') AVAIL_MEM=$(free -m | awk '/Mem:/ {print $7}') TOTAL_SWAP=$(free -m | awk '/Swap:/ {print $2}') USED_SWAP=$(free -m | awk '/Swap:/ {print $3}') DISK_TOTAL=$(df -h --total --exclude-type=tmpfs | awk '/total/ {print $2}') DISK_USED=$(df -h --total --exclude-type=tmpfs | awk '/total/ {print $3}') DISK_AVAIL=$(df -h --total --exclude-type=tmpfs | awk '/total/ {print $4}') cat << EOF > system_health.html <!DOCTYPE html> <html> <head><title>System Health Report</title></head> <body> <h1>System Health Report</h1> <p>Date: $(date)</p> <h2>Memory Usage</h2> <p>Total: ${TOTAL_MEM}MB | Used: ${USED_MEM}MB | Available: ${AVAIL_MEM}MB</p> <h2>Disk Usage</h2> <p>Total: $DISK_TOTAL | Used: $DISK_USED | Available: $DISK_AVAIL</p> </body> </html> EOF -weight: 600;">sudo cp system_health.html /var/www/html/index.html if [ $? -eq 0 ]; then echo "SUCCESS: Report hosted at http://localhost" else echo "ERROR: Failed to deploy report" exit 1 fi #!/bin/bash echo "Kernel Name: $(uname -s)" echo "Kernel Release: $(uname -r)" echo "Processor: $(uname -p)" echo "OS: $(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2)" EDITOR="nano" echo "Favorite Editor: $EDITOR" echo "Editor Location: $(which $EDITOR)" echo "Documentation: $(whereis $EDITOR)" #!/bin/bash echo "Kernel Name: $(uname -s)" echo "Kernel Release: $(uname -r)" echo "Processor: $(uname -p)" echo "OS: $(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2)" EDITOR="nano" echo "Favorite Editor: $EDITOR" echo "Editor Location: $(which $EDITOR)" echo "Documentation: $(whereis $EDITOR)" #!/bin/bash echo "Kernel Name: $(uname -s)" echo "Kernel Release: $(uname -r)" echo "Processor: $(uname -p)" echo "OS: $(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2)" EDITOR="nano" echo "Favorite Editor: $EDITOR" echo "Editor Location: $(which $EDITOR)" echo "Documentation: $(whereis $EDITOR)" -weight: 500;">docker --version # Check Docker version -weight: 500;">docker version # Detailed client + server version -weight: 500;">docker run hello-world # Test Docker works -weight: 500;">docker --version # Check Docker version -weight: 500;">docker version # Detailed client + server version -weight: 500;">docker run hello-world # Test Docker works -weight: 500;">docker --version # Check Docker version -weight: 500;">docker version # Detailed client + server version -weight: 500;">docker run hello-world # Test Docker works # PULL an image from Docker Hub -weight: 500;">docker pull ubuntu # Download ubuntu image (latest) -weight: 500;">docker pull ubuntu:22.04 # Download specific version -weight: 500;">docker pull nginx # Download nginx image # RUN a container from an image -weight: 500;">docker run ubuntu # Runs and exits immediately (no process) -weight: 500;">docker run ubuntu sleep 5 # Runs, sleeps 5 seconds, then exits # RUN in interactive mode (get a shell inside container) -weight: 500;">docker run -it ubuntu bash # -i=interactive, -t=terminal # You are now INSIDE the container! echo "Hello Docker" ls exit # Exit container (container stops) # RUN with a custom name -weight: 500;">docker run --name myubuntu ubuntu sleep 30 # RUN in detached mode (background) -weight: 500;">docker run -d ubuntu sleep 300 # -d = detached (runs in background) -weight: 500;">docker run -dit --name test ubuntu bash # Detached + interactive # PULL an image from Docker Hub -weight: 500;">docker pull ubuntu # Download ubuntu image (latest) -weight: 500;">docker pull ubuntu:22.04 # Download specific version -weight: 500;">docker pull nginx # Download nginx image # RUN a container from an image -weight: 500;">docker run ubuntu # Runs and exits immediately (no process) -weight: 500;">docker run ubuntu sleep 5 # Runs, sleeps 5 seconds, then exits # RUN in interactive mode (get a shell inside container) -weight: 500;">docker run -it ubuntu bash # -i=interactive, -t=terminal # You are now INSIDE the container! echo "Hello Docker" ls exit # Exit container (container stops) # RUN with a custom name -weight: 500;">docker run --name myubuntu ubuntu sleep 30 # RUN in detached mode (background) -weight: 500;">docker run -d ubuntu sleep 300 # -d = detached (runs in background) -weight: 500;">docker run -dit --name test ubuntu bash # Detached + interactive # PULL an image from Docker Hub -weight: 500;">docker pull ubuntu # Download ubuntu image (latest) -weight: 500;">docker pull ubuntu:22.04 # Download specific version -weight: 500;">docker pull nginx # Download nginx image # RUN a container from an image -weight: 500;">docker run ubuntu # Runs and exits immediately (no process) -weight: 500;">docker run ubuntu sleep 5 # Runs, sleeps 5 seconds, then exits # RUN in interactive mode (get a shell inside container) -weight: 500;">docker run -it ubuntu bash # -i=interactive, -t=terminal # You are now INSIDE the container! echo "Hello Docker" ls exit # Exit container (container stops) # RUN with a custom name -weight: 500;">docker run --name myubuntu ubuntu sleep 30 # RUN in detached mode (background) -weight: 500;">docker run -d ubuntu sleep 300 # -d = detached (runs in background) -weight: 500;">docker run -dit --name test ubuntu bash # Detached + interactive # LIST containers -weight: 500;">docker ps # Show RUNNING containers only -weight: 500;">docker ps -a # Show ALL containers (running + stopped) # STOP a container -weight: 500;">docker -weight: 500;">stop <container_id> # Graceful -weight: 500;">stop -weight: 500;">docker -weight: 500;">stop myubuntu # Stop by name # REMOVE a container -weight: 500;">docker rm <container_id> # Remove stopped container -weight: 500;">docker rm myubuntu # Remove by name # STOP ALL containers at once -weight: 500;">docker -weight: 500;">stop $(-weight: 500;">docker ps -q) # -q = quiet (only IDs) # REMOVE ALL containers -weight: 500;">docker rm $(-weight: 500;">docker ps -aq) # Remove all (running + stopped) # LIST containers -weight: 500;">docker ps # Show RUNNING containers only -weight: 500;">docker ps -a # Show ALL containers (running + stopped) # STOP a container -weight: 500;">docker -weight: 500;">stop <container_id> # Graceful -weight: 500;">stop -weight: 500;">docker -weight: 500;">stop myubuntu # Stop by name # REMOVE a container -weight: 500;">docker rm <container_id> # Remove stopped container -weight: 500;">docker rm myubuntu # Remove by name # STOP ALL containers at once -weight: 500;">docker -weight: 500;">stop $(-weight: 500;">docker ps -q) # -q = quiet (only IDs) # REMOVE ALL containers -weight: 500;">docker rm $(-weight: 500;">docker ps -aq) # Remove all (running + stopped) # LIST containers -weight: 500;">docker ps # Show RUNNING containers only -weight: 500;">docker ps -a # Show ALL containers (running + stopped) # STOP a container -weight: 500;">docker -weight: 500;">stop <container_id> # Graceful -weight: 500;">stop -weight: 500;">docker -weight: 500;">stop myubuntu # Stop by name # REMOVE a container -weight: 500;">docker rm <container_id> # Remove stopped container -weight: 500;">docker rm myubuntu # Remove by name # STOP ALL containers at once -weight: 500;">docker -weight: 500;">stop $(-weight: 500;">docker ps -q) # -q = quiet (only IDs) # REMOVE ALL containers -weight: 500;">docker rm $(-weight: 500;">docker ps -aq) # Remove all (running + stopped) # LIST downloaded images -weight: 500;">docker images # REMOVE an image -weight: 500;">docker rmi ubuntu # Remove ubuntu image -weight: 500;">docker rmi <image_id> # Remove by ID # Note: Must -weight: 500;">remove all containers using that image first! # LIST downloaded images -weight: 500;">docker images # REMOVE an image -weight: 500;">docker rmi ubuntu # Remove ubuntu image -weight: 500;">docker rmi <image_id> # Remove by ID # Note: Must -weight: 500;">remove all containers using that image first! # LIST downloaded images -weight: 500;">docker images # REMOVE an image -weight: 500;">docker rmi ubuntu # Remove ubuntu image -weight: 500;">docker rmi <image_id> # Remove by ID # Note: Must -weight: 500;">remove all containers using that image first! # ATTACH to a running container (connects to main process) -weight: 500;">docker attach test # CTRL+P then CTRL+Q = detach WITHOUT stopping container # If you type 'exit', container STOPS # EXEC runs a NEW process inside a running container -weight: 500;">docker exec -it test bash # If you type 'exit', only the exec process ends # The container keeps running! # ATTACH to a running container (connects to main process) -weight: 500;">docker attach test # CTRL+P then CTRL+Q = detach WITHOUT stopping container # If you type 'exit', container STOPS # EXEC runs a NEW process inside a running container -weight: 500;">docker exec -it test bash # If you type 'exit', only the exec process ends # The container keeps running! # ATTACH to a running container (connects to main process) -weight: 500;">docker attach test # CTRL+P then CTRL+Q = detach WITHOUT stopping container # If you type 'exit', container STOPS # EXEC runs a NEW process inside a running container -weight: 500;">docker exec -it test bash # If you type 'exit', only the exec process ends # The container keeps running! -weight: 500;">docker logs <container_id> # View container logs -weight: 500;">docker logs myubuntu -weight: 500;">docker inspect test # Full container details (JSON) -weight: 500;">docker inspect --format='{{.State.Status}}' test # Get specific info -weight: 500;">docker logs <container_id> # View container logs -weight: 500;">docker logs myubuntu -weight: 500;">docker inspect test # Full container details (JSON) -weight: 500;">docker inspect --format='{{.State.Status}}' test # Get specific info -weight: 500;">docker logs <container_id> # View container logs -weight: 500;">docker logs myubuntu -weight: 500;">docker inspect test # Full container details (JSON) -weight: 500;">docker inspect --format='{{.State.Status}}' test # Get specific info # Map host port 8081 to container port 80 -weight: 500;">docker run -d -p 8081:80 nginx # ^ ^ # | +-- container port (nginx listens on 80) # +-------- host port (access via localhost:8081) # Run multiple web servers on different ports -weight: 500;">docker run -d --name web1 -p 8081:80 ubuntu/apache2 -weight: 500;">docker run -d --name web2 -p 8082:80 ubuntu/apache2 # Access in browser: # http://localhost:8081 --> web1 # http://localhost:8082 --> web2 # Map host port 8081 to container port 80 -weight: 500;">docker run -d -p 8081:80 nginx # ^ ^ # | +-- container port (nginx listens on 80) # +-------- host port (access via localhost:8081) # Run multiple web servers on different ports -weight: 500;">docker run -d --name web1 -p 8081:80 ubuntu/apache2 -weight: 500;">docker run -d --name web2 -p 8082:80 ubuntu/apache2 # Access in browser: # http://localhost:8081 --> web1 # http://localhost:8082 --> web2 # Map host port 8081 to container port 80 -weight: 500;">docker run -d -p 8081:80 nginx # ^ ^ # | +-- container port (nginx listens on 80) # +-------- host port (access via localhost:8081) # Run multiple web servers on different ports -weight: 500;">docker run -d --name web1 -p 8081:80 ubuntu/apache2 -weight: 500;">docker run -d --name web2 -p 8082:80 ubuntu/apache2 # Access in browser: # http://localhost:8081 --> web1 # http://localhost:8082 --> web2 # Mount current directory into container -weight: 500;">docker run -it -v $(pwd):/scripts ubuntu bash # $(pwd) = your current directory on host # /scripts = path inside the container # Mount a specific file into nginx echo '<h1>Hello Docker!</h1>' > index.html -weight: 500;">docker run -d -p 8081:80 -v $(pwd)/index.html:/usr/share/nginx/html/index.html nginx # You have just hosted a simple web app! # Mount a Python script echo 'print("Hello, world!")' > hello.py -weight: 500;">docker run -it --rm -v $(pwd)/hello.py:/hello.py python:3.10-slim bash # Then inside: python hello.py # Mount current directory into container -weight: 500;">docker run -it -v $(pwd):/scripts ubuntu bash # $(pwd) = your current directory on host # /scripts = path inside the container # Mount a specific file into nginx echo '<h1>Hello Docker!</h1>' > index.html -weight: 500;">docker run -d -p 8081:80 -v $(pwd)/index.html:/usr/share/nginx/html/index.html nginx # You have just hosted a simple web app! # Mount a Python script echo 'print("Hello, world!")' > hello.py -weight: 500;">docker run -it --rm -v $(pwd)/hello.py:/hello.py python:3.10-slim bash # Then inside: python hello.py # Mount current directory into container -weight: 500;">docker run -it -v $(pwd):/scripts ubuntu bash # $(pwd) = your current directory on host # /scripts = path inside the container # Mount a specific file into nginx echo '<h1>Hello Docker!</h1>' > index.html -weight: 500;">docker run -d -p 8081:80 -v $(pwd)/index.html:/usr/share/nginx/html/index.html nginx # You have just hosted a simple web app! # Mount a Python script echo 'print("Hello, world!")' > hello.py -weight: 500;">docker run -it --rm -v $(pwd)/hello.py:/hello.py python:3.10-slim bash # Then inside: python hello.py -weight: 500;">systemctl -weight: 500;">start <-weight: 500;">service> # Start a -weight: 500;">service NOW -weight: 500;">systemctl -weight: 500;">stop <-weight: 500;">service> # Stop a -weight: 500;">service NOW -weight: 500;">systemctl -weight: 500;">restart <-weight: 500;">service> # Restart a -weight: 500;">service -weight: 500;">systemctl reload <-weight: 500;">service> # Reload config without stopping -weight: 500;">systemctl -weight: 500;">status <-weight: 500;">service> # Check if -weight: 500;">service is running -weight: 500;">systemctl -weight: 500;">enable <-weight: 500;">service> # Start on boot -weight: 500;">systemctl -weight: 500;">disable <-weight: 500;">service> # Don't -weight: 500;">start on boot -weight: 500;">systemctl -weight: 500;">start <-weight: 500;">service> # Start a -weight: 500;">service NOW -weight: 500;">systemctl -weight: 500;">stop <-weight: 500;">service> # Stop a -weight: 500;">service NOW -weight: 500;">systemctl -weight: 500;">restart <-weight: 500;">service> # Restart a -weight: 500;">service -weight: 500;">systemctl reload <-weight: 500;">service> # Reload config without stopping -weight: 500;">systemctl -weight: 500;">status <-weight: 500;">service> # Check if -weight: 500;">service is running -weight: 500;">systemctl -weight: 500;">enable <-weight: 500;">service> # Start on boot -weight: 500;">systemctl -weight: 500;">disable <-weight: 500;">service> # Don't -weight: 500;">start on boot -weight: 500;">systemctl -weight: 500;">start <-weight: 500;">service> # Start a -weight: 500;">service NOW -weight: 500;">systemctl -weight: 500;">stop <-weight: 500;">service> # Stop a -weight: 500;">service NOW -weight: 500;">systemctl -weight: 500;">restart <-weight: 500;">service> # Restart a -weight: 500;">service -weight: 500;">systemctl reload <-weight: 500;">service> # Reload config without stopping -weight: 500;">systemctl -weight: 500;">status <-weight: 500;">service> # Check if -weight: 500;">service is running -weight: 500;">systemctl -weight: 500;">enable <-weight: 500;">service> # Start on boot -weight: 500;">systemctl -weight: 500;">disable <-weight: 500;">service> # Don't -weight: 500;">start on boot 644 = rw-r--r-- # Standard file permission 755 = rwxr-xr-x # Standard directory / script permission 700 = rwx------ # Private (owner only) 600 = rw------- # SSH keys 777 = rwxrwxrwx # NEVER USE (security risk) 644 = rw-r--r-- # Standard file permission 755 = rwxr-xr-x # Standard directory / script permission 700 = rwx------ # Private (owner only) 600 = rw------- # SSH keys 777 = rwxrwxrwx # NEVER USE (security risk) 644 = rw-r--r-- # Standard file permission 755 = rwxr-xr-x # Standard directory / script permission 700 = rwx------ # Private (owner only) 600 = rw------- # SSH keys 777 = rwxrwxrwx # NEVER USE (security risk) -weight: 500;">docker run -it ubuntu bash # Interactive shell -weight: 500;">docker run -d -p 8080:80 nginx # Background + port map -weight: 500;">docker run -v $(pwd):/app ubuntu # Volume mount -weight: 500;">docker ps -a # List all containers -weight: 500;">docker -weight: 500;">stop $(-weight: 500;">docker ps -q) # Stop all running -weight: 500;">docker rm $(-weight: 500;">docker ps -aq) # Remove all containers -weight: 500;">docker exec -it <id> bash # Shell into running container CTRL+P then CTRL+Q # Detach without stopping -weight: 500;">docker run -it ubuntu bash # Interactive shell -weight: 500;">docker run -d -p 8080:80 nginx # Background + port map -weight: 500;">docker run -v $(pwd):/app ubuntu # Volume mount -weight: 500;">docker ps -a # List all containers -weight: 500;">docker -weight: 500;">stop $(-weight: 500;">docker ps -q) # Stop all running -weight: 500;">docker rm $(-weight: 500;">docker ps -aq) # Remove all containers -weight: 500;">docker exec -it <id> bash # Shell into running container CTRL+P then CTRL+Q # Detach without stopping -weight: 500;">docker run -it ubuntu bash # Interactive shell -weight: 500;">docker run -d -p 8080:80 nginx # Background + port map -weight: 500;">docker run -v $(pwd):/app ubuntu # Volume mount -weight: 500;">docker ps -a # List all containers -weight: 500;">docker -weight: 500;">stop $(-weight: 500;">docker ps -q) # Stop all running -weight: 500;">docker rm $(-weight: 500;">docker ps -aq) # Remove all containers -weight: 500;">docker exec -it <id> bash # Shell into running container CTRL+P then CTRL+Q # Detach without stopping #!/bin/bash # Shebang (always first line) $1 $2 $# $@ $? # Args, count, all, exit code read VAR # Read user input if [ condition ]; then ... fi # Conditional case "$1" in pattern) ... ;; esac # Flag handling for item in list; do ... done # Loop tar -czf name.tar.gz dir/ # Create archive awk '{print $1}' file # Text processing cat << EOF > file ... EOF # Heredoc command > file 2>&1 # Redirect all output #!/bin/bash # Shebang (always first line) $1 $2 $# $@ $? # Args, count, all, exit code read VAR # Read user input if [ condition ]; then ... fi # Conditional case "$1" in pattern) ... ;; esac # Flag handling for item in list; do ... done # Loop tar -czf name.tar.gz dir/ # Create archive awk '{print $1}' file # Text processing cat << EOF > file ... EOF # Heredoc command > file 2>&1 # Redirect all output #!/bin/bash # Shebang (always first line) $1 $2 $# $@ $? # Args, count, all, exit code read VAR # Read user input if [ condition ]; then ... fi # Conditional case "$1" in pattern) ... ;; esac # Flag handling for item in list; do ... done # Loop tar -czf name.tar.gz dir/ # Create archive awk '{print $1}' file # Text processing cat << EOF > file ... EOF # Heredoc command > file 2>&1 # Redirect all output - Linux Basics & File System - Linux Commands & Navigation - File Permissions & Ownership - Users & Groups Management - SSH (Secure Shell) - Apache Web Server - Nginx Web Server - Port Debugging & Troubleshooting - Bash Scripting - Docker Containers - Lab Exercises Summary - Quick Reference Cheat Sheet - Debian-based: Ubuntu, Debian — use .deb packages and -weight: 500;">apt package manager - Red Hat-based: CentOS, Fedora, RHEL — use .rpm packages and -weight: 500;">yum/-weight: 500;">dnf package manager - Technical differences are mainly about package management, software versions, and file locations - Once you grasp those differences, switching between distros becomes painless - What is the main function? (Server or Desktop?) - What packages are important? (web server, database, etc.) - How much disk space is available? - How often are packages updated? - How long is the support cycle? (LTS = Long Term Support) - Do you need long-term stability or cutting-edge software? - Absolute path: Starts from root /. Example: /usr/bin/python3 - Relative path: Starts from current directory. Example: ../usr/bin - User (u): The owner of the file. When you create a file, you become the owner. - Group (g): A collection of users. Useful in multi-user environments. - Other (o): Everyone else on the system. - /etc/passwd — Contains user account info (username:x:UID:GID:info:home:shell) - /etc/shadow — Contains encrypted passwords (only root can read) - /etc/group — Contains group definitions - Private key (~/.ssh/id_rsa): Stays on YOUR machine. NEVER share this! - Public key (~/.ssh/id_rsa.pub): Goes to the REMOTE server. - Default web root: /var/www/html/ (same as Apache) - Config files: /etc/nginx/sites-available/ and /etc/nginx/sites-enabled/ - Main config: /etc/nginx/nginx.conf - Default site config: /etc/nginx/sites-available/default - Image: A blueprint/template for a container (like a class in OOP). Downloaded from Docker Hub. - Container: A running instance of an image (like an object). You can run multiple from one image. - Docker Hub: Online registry of Docker images (like GitHub for images). - Tag: Version of an image. Example: ubuntu:22.04 vs ubuntu:latest