Module 21: Linux and Scripting
CompTIA A+ Core 2 (220-1102)
1. Linux Basics
Linux is an open-source, Unix-like OS; the kernel plus tools form a distribution (distro) such as Ubuntu, Debian, Fedora, Red Hat/CentOS, Mint, Arch
Used heavily on servers, embedded devices, Android (Linux kernel), and desktops
Desktop environments: GNOME, KDE, Xfce; the shell (usually bash) is the command interpreter
File systems: ext4 (most common), XFS, Btrfs; can read NTFS/FAT with drivers
Package managers install software:
Family | Tool | Package type |
|---|---|---|
Debian/Ubuntu |
|
|
Red Hat/Fedora |
|
|
Arch |
| |
Universal | Snap, Flatpak |
2. Directory Structure (Filesystem Hierarchy)
Path | Contents |
|---|---|
| Root of the whole file system |
| User home directories |
| The root user's home |
| Essential commands, system admin commands |
| Configuration files |
| Variable data: logs ( |
| Temporary files |
| User programs and libraries |
| Device files |
| Virtual process and kernel info |
| Mount points for drives |
| Kernel and boot loader files |
| Optional software |
Everything is a file; paths use
/and are case-sensitive~= home directory,.= current directory,..= parent directory
3. Essential Commands
Navigation and Files
Command | Purpose |
|---|---|
| Print working directory |
| List files (long, hidden, both) |
| Change directory |
| Create / remove directory |
| Create empty file or update timestamp |
| Copy (recursive for folders) |
| Move or rename |
| Delete ( |
| View files; |
| Text editors |
| Search by name/attributes |
| Search text patterns; |
| Manual page; |
| Symbolic link |
| Archive/compress |
| Text processing |
| Shell utilities |
Pipes and Redirection
|sends output of one command to another:ls | grep txt>writes output to a file (overwrites);>>appends;<reads input;2>redirects errors
Users, Permissions, and Admin
Command | Purpose |
|---|---|
| Run a command with superuser rights |
| Switch user |
| Show current user |
| Change a password |
| Manage accounts and groups |
| Change permissions |
| Change owner / group |
| Power control |
System and Process Management
Command | Purpose |
|---|---|
| View processes |
| Stop processes |
| Disk space on file systems |
| Folder size |
| Memory usage |
| System info |
| Load and run time |
| Disks and mounts |
| Manage services (systemd) |
| View system logs |
| Kernel/hardware messages |
| Schedule recurring jobs |
Networking
ip addr (or ifconfig), ping, traceroute, netstat / ss, nslookup / dig, curl, wget, ssh user@host, scp, iwconfig
4. Permissions
ls -l output: -rwxr-xr-- 1 user group 4096 date file
First character:
-file,ddirectory,llinkThen three triplets: owner / group / others, each r (read) w (write) x (execute)
Permission | Number |
|---|---|
read | 4 |
write | 2 |
execute | 1 |
chmod 755 file→ owner rwx, group r-x, others r-xchmod 644 file→ owner rw-, group r--, others r--chmod 600→ owner only;chmod 777→ everyone full (avoid)Symbolic:
chmod u+x file,chmod go-w fileDirectories need x to be entered
Root (UID 0) is the superuser; use
sudorather than logging in as root
5. Backup, Updates, and Software
Back up with
rsync,tar,cp -a, Timeshift (snapshots), or cloud tools; schedule with cronUpdates:
sudo apt update && sudo apt upgrade(Debian/Ubuntu);sudo dnf upgrade(Fedora/RHEL)Update vs. upgrade (apt):
updaterefreshes the package list;upgradeinstalls newer versionsInstall:
sudo apt install package; remove:sudo apt remove packageVerify downloads with checksums and install from trusted repositories
6. Linux Security
Use strong passwords, limit sudo, keep systems patched
Firewalls:
iptables,ufw(Ubuntu),firewalldSSH with keys (
ssh-keygen), disable root loginSELinux / AppArmor enforce access policy
Encryption: LUKS for disks
Check logs in
/var/log(auth.log, syslog)Run services with least privilege
7. Troubleshooting Linux
Problem | Approach |
|---|---|
Won't boot | Check GRUB boot loader, use recovery mode / live USB, |
Disk full |
|
Service down |
|
No network |
|
Permission denied |
|
High CPU/memory |
|
Package errors | Update lists, fix broken packages, check repos |
Kernel panic | Boot an older kernel from GRUB |
Forgotten password | Recovery mode or live media |
GRUB is the common boot loader;
fsckrepairs file systems (run on unmounted volumes)
8. Scripting Basics
What Scripting Is For
Automates repeated tasks (backups, user creation, log checks, updates, cleanups, inventory) and reduces human error.
Common Script Types and Extensions
Language | Extension | Platform | Notes |
|---|---|---|---|
Batch |
| Windows | Runs in CMD; simple automation |
PowerShell |
| Windows (and cross-platform) | Object-based, cmdlets ( |
Shell (Bash) |
| Linux/macOS | Starts with a shebang |
Python |
| Cross-platform | Readable, popular for automation |
JavaScript |
| Web/Node | Browser and server scripts |
VBScript |
| Windows | Legacy |
Bash Script Essentials
#!/bin/bash
# This is a comment
name="Student" # variable (no spaces around =)
echo "Hello, $name" # output; $ reads a variable
if [ -f /etc/hosts ]; then
echo "File exists"
else
echo "Missing"
fi
for i in 1 2 3; do
echo "Number $i"
doneMake executable:
chmod +x script.sh, run with./script.shComments start with
#(Bash/Python),REMor::(Batch),#(PowerShell)Exit codes:
0= success; non-zero = error ($?holds the last code)
Core Programming Concepts (exam level)
Concept | Meaning |
|---|---|
Variable | Named storage for a value |
Constant | A value that doesn't change |
Data types | String, integer, float, Boolean, array/list |
Operators | Math ( |
Conditionals |
|
Loops |
|
Functions | Reusable named blocks |
Comments | Notes ignored by the interpreter |
Arguments / parameters | Inputs to a script |
Environment variables | System-wide values (e.g., |
Error handling | Check exit codes, use try/catch (Python/PowerShell) |
Batch and PowerShell Snippets
@echo off
echo Hello
pauseGet-Process | Where-Object { $_.CPU -gt 100 }
Get-Service | Sort-Object StatusPowerShell execution policy controls whether scripts may run (
Set-ExecutionPolicy)Windows tools to schedule scripts: Task Scheduler; Linux: cron
Script Safety Considerations
Test in a safe environment first
Beware of unintentional infinite loops, wrong paths, destructive commands (
rm -rf,del /s)Don't hard-code passwords; protect credentials
Run least privilege; review scripts from unknown sources (malware often arrives as scripts)
Document scripts and keep them in version control
9. Linux Cheat Sheet by Task
I want to… | Command |
|---|---|
See where I am |
|
List hidden files |
|
Make a folder |
|
Copy a folder |
|
Rename a file |
|
Read a log live |
|
Find text in files |
|
Check free disk |
|
Check memory |
|
Find the IP address |
|
Kill a stuck process |
|
Install software |
|
Make a file executable |
|
Run as admin |
|
Additional Textbook Topics: Uses for Scripting
Automate repetitive tasks: backups, user creation, cleanup, installs
Configuration and deployment: set up many machines the same way
Monitoring and reports: check disk space, services, and logs
Restart services, rename or move files in bulk, map drives at logon (login scripts)
Scheduled jobs: Task Scheduler (Windows), cron (Linux/macOS)
Reduce errors and save time, but test first and avoid hard-coded passwords
Quick-Reference Exam Points
Linux is case-sensitive;
/is root;~is homechmod: r=4, w=2, x=1; 755 and 644 are classics;chownchanges owner>overwrites,>>appends,|pipesapt updaterefreshes lists;apt upgradeinstalls;sudo= temporary adminps/topview processes,killends them,df= disk space,grep= search textShebang
#!/bin/bashstarts a shell script; extensions:.bat, .ps1, .sh, .py, .js, .vbsShell scripts: variables,
if, loops (for/while), functions, commentsfsckrepairs file systems; GRUB is the boot loaderDon't hard-code credentials in scripts; test before running on production