Linux for DevOps (Beginners)
1. Linux Fundamentals
1.1 What is Open Source?
Open-source software has source code freely available for anyone to view, modify, and distribute. Linux is built on open-source principles.
| Key Points | Description |
|---|
| Source code | Publicly accessible |
| Development | Community-driven |
| License | Free to use, modify, and redistribute |
| Examples | Linux, Python, Firefox, Android |
1.2 What is Linux?
Linux is a free, open-source operating system kernel created by Linus Torvalds in 1991. It powers many devices from smartphones to supercomputers.
| Aspect | Details |
|---|
| Kernel vs OS | Linux is a kernel, not a full OS |
| Full OS Components | Linux Kernel + GNU tools + package manager + desktop environment |
| Popular Distributions (Distros) | Ubuntu, Fedora, Debian, CentOS, Arch Linux |
| Usage | Servers, cloud, Android, IoT, supercomputers |
1.3 Linux vs UNIX
| System | Description | Notes |
|---|
| Linux | Free, open-source kernel by Linus Torvalds (1991) | Community-developed |
| UNIX | Proprietary OS from Bell Labs (1969) | Not free |
| macOS | UNIX-based (BSD), proprietary by Apple | UNIX-based, proprietary |
| Similarities | Both follow POSIX standards | Linux inspired by UNIX |
1.4 Why Use Linux?
- Free and open source — no licensing costs.
- Highly secure — fewer viruses, strong permission model.
- Stable and reliable — servers can run for years without reboot.
- Lightweight — runs on old hardware.
- Industry standard — most servers, cloud, and DevOps tools run on Linux.
- Customizable — choose your own components and desktop environment.
1.5 Linux vs Windows
| Feature | Linux | Windows |
|---|
| Cost | Free | Requires license |
| Security | Strong permissions, fewer viruses | More vulnerable, requires antivirus |
| Usage | Dominates servers | Dominates desktops |
| CLI vs GUI | CLI-first | GUI-first |
| File Systems | ext4, XFS | NTFS, FAT32 |
| Software Install | Package managers (apt, yum) | Installers (.exe) |
1.6 What is a Kernel?
The kernel is the core of the operating system. It manages hardware resources and allows software to communicate with hardware.
| Functions | Description |
|---|
| Manages | CPU, memory, I/O devices |
| Provides | System calls for programs to request services |
| Acts as | Bridge between applications and hardware |
| Linux Kernel Type | Monolithic (all services run in kernel space) |
2. Installing Ubuntu via WSL (Windows Subsystem for Linux)
WSL allows running a Linux terminal on Windows without a VM.
| Steps | Commands/Actions |
|---|
| Open PowerShell as Administrator | Run: wsl --install |
| Restart PC | Restart your computer |
| Open Ubuntu | From Start Menu |
| Initial Setup | Set username and password on first launch |
Useful WSL Commands
| Command | Description |
|---|
wsl --list --verbose | List installed distros with details |
wsl --set-default-version 2 | Set WSL version 2 as default |
explorer.exe . | Open Linux files in Windows Explorer |
3. Basic Terminal Commands
3.1 Navigation
| Command | Description |
|---|
pwd | Print working directory |
ls | List files and directories |
ls -la | List all files including hidden with details |
cd <dir> | Change directory |
cd .. | Go up one directory level |
cd ~ | Go to home directory |
cd / | Go to root directory |
3.2 File and Directory Operations
| Command | Description |
|---|
mkdir <name> | Create a new directory |
mkdir -p a/b/c | Create nested directories |
touch <file> | Create empty file or update timestamp |
rm <file> | Remove a file |
rm -r <dir> | Remove directory and contents recursively |
rm -zf <dir> | Force remove without prompts (use carefully!) |
cp <src> <dst> | Copy a file |
mv <src> <dst> | Move or rename a file |
cat <file> | Display file contents |
less <file> | Scroll through file contents |
nano <file> | Open file in nano editor |
vim <file> | Open file in vim editor |
3.3 File Permissions — chmod
| Symbol | Value | Permission |
|---|
| r | 4 | Read |
| w | 2 | Write |
| x | 1 | Execute |
| Command | Effect |
|---|
chmod 755 file | Owner: rwx, Group: r-x, Others: r-x |
chmod 644 file | Owner: rw-, Group: r--, Others: r-- |
chmod +x file | Add execute permission for all |
chmod -w file | Remove write permission |
3.4 Links
| Command | Description |
|---|
ln <src> <link> | Create hard link (same inode) |
ln -s <src> <link> | Create symbolic (soft) link |
alias ll='ls -la' | Create command alias (session only) |
Note: Deleting original breaks soft link but not hard link.
4. File Handling & Text Processing
4.1 head & tail
| Command | Description |
|---|
head <file> | Show first 10 lines |
head -n 20 <file> | Show first 20 lines |
tail <file> | Show last 10 lines |
tail -f <file> | Follow file output live (logs) |
4.2 wc — Word Count
| Command | Description |
|---|
wc <file> | Show lines, words, bytes |
wc -l <file> | Count lines only |
wc -w <file> | Count words only |
wc -c <file> | Count bytes/characters only |
4.3 sort
| Command | Description |
|---|
sort <file> | Sort lines alphabetically |
sort -r <file> | Sort in reverse order |
sort -n <file> | Sort numerically |
sort -u <file> | Sort and remove duplicates |
sort -k2 <file> | Sort by 2nd column |
4.4 grep — Search Text
| Command | Description |
|---|
grep 'pattern' file | Search for pattern in file |
grep -i 'pattern' file | Case-insensitive search |
grep -r 'pattern' dir/ | Recursive search in directory |
grep -n 'pattern' file | Show line numbers with results |
grep -v 'pattern' file | Show lines NOT matching pattern |
grep -c 'pattern' file | Count matching lines |
| `grep -E 'p1 | p2' file` |
4.5 find — File Search
| Command | Description |
|---|
find . -name '*.txt' | Find all .txt files in current directory |
find / -name 'file.log' | Find file across entire system |
find . -type d | Find only directories |
find . -type f | Find only regular files |
find . -mtime -7 | Find files modified in last 7 days |
find . -size +10M | Find files larger than 10MB |
4.6 awk — Text Processing
| Command | Description |
|---|
awk '{print $1}' file | Print first column |
awk '{print $1, $3}' file | Print columns 1 and 3 |
awk -F',' '{print $2}' file | Use comma as delimiter (CSV) |
awk 'NR == 5' file | Print line number 5 |
awk '{sum+=$1} END {print sum}' file | Sum all values in column 1 |
5. Disk Management & Archives
| Command | Description |
|---|
df -h | Show disk space usage (human-readable) |
du -sh <dir> | Show size of directory |
du -h --max-depth=1 | Show sizes of subdirectories |
lsblk | List block devices (disks, partitions) |
5.1 Zip / Unzip
| Command | Description |
|---|
zip archive.zip file1 file2 | Create a zip archive |
zip -r archive.zip dir/ | Zip an entire directory |
unzip archive.zip | Extract zip archive |
unzip -l archive.zip | List contents without extracting |
5.2 tar — Tape Archive
| Command | Description |
|---|
tar -cvf archive.tar dir/ | Create tar archive |
tar -xvf archive.tar | Extract tar archive |
tar -czvf archive.tar.gz dir/ | Create compressed tar.gz archive |
tar -xzvf archive.tar.gz | Extract tar.gz archive |
tar -tf archive.tar | List contents of tar file |
6. Process Management
6.1 View Processes
| Command | Description |
|---|
ps | List processes for current user |
ps aux | List all processes with details |
| `ps aux | grep nginx` |
top | Interactive real-time viewer |
htop | Improved interactive process viewer |
pgrep <name> | Find process ID by name |
6.2 Killing Processes
| Command | Description |
|---|
kill <PID> | Send SIGTERM (graceful stop) |
kill -9 <PID> | Send SIGKILL (force stop) |
killall <name> | Kill all processes by name |
pkill <name> | Kill process by pattern match |
6.3 Background Jobs & nohup
| Command | Description |
|---|
command & | Run command in background |
jobs | List background jobs |
fg %1 | Bring job 1 to foreground |
bg %1 | Resume job 1 in background |
Ctrl + Z | Suspend current foreground job |
nohup command & | Run command immune to hangups (survives logout) |
nohup command > out.log 2>&1 & | Run nohup and save output to file |
disown %1 | Remove job from shell job table |
7. Remote Access
7.1 SSH — Secure Shell
| Command | Description |
|---|
ssh user@host | Connect to remote server |
ssh user@192.168.1.10 | Connect via IP address |
ssh -p 2222 user@host | Connect on custom port |
ssh-keygen | Generate SSH key pair |
ssh-copy-id user@host | Copy public key to remote server |
exit | Disconnect SSH session |
7.2 SCP — Secure Copy
| Command | Description |
|---|
scp file.txt user@host:/path/ | Copy file TO remote server |
scp user@host:/path/file.txt . | Copy file FROM remote server |
scp -r dir/ user@host:/path/ | Copy directory recursively |
scp -P 2222 file user@host:/path/ | Use custom SSH port |
7.3 GUI Tools
- PuTTY — SSH/Telnet client for Windows
- WinSCP — GUI file transfer for Windows (SFTP/SCP)
- FileZilla — Cross-platform FTP/SFTP client
8. Package Management
8.1 apt-get (Debian/Ubuntu)
| Command | Description |
|---|
sudo apt-get update | Update package list from repos |
sudo apt-get upgrade | Upgrade all installed packages |
sudo apt-get install <pkg> | Install a package |
sudo apt-get remove <pkg> | Remove a package |
sudo apt-get autoremove | Remove unused dependencies |
apt-cache search <keyword> | Search for a package |
dpkg -l | List all installed packages |
8.2 wget — Download Files
| Command | Description |
|---|
wget <URL> | Download file from URL |
wget -O output.zip <URL> | Download with custom filename |
wget -c <URL> | Resume interrupted download |
wget -r <URL> | Recursively download website |
9. Scheduling & Automation
9.1 Cron Jobs
Cron is a time-based scheduler to run tasks automatically.
| Command | Description |
|---|
crontab -e | Edit cron jobs |
crontab -l | List current cron jobs |
crontab -r | Remove all cron jobs |
Cron Syntax
Examples
| Schedule | Command | Description |
|---|
0 2 * * * /backup.sh | Run backup daily at 2 AM | |
*/5 * * * * /check.sh | Run every 5 minutes | |
0 9 * * 1 /report.sh | Run every Monday at 9 AM | |
@reboot /startup.sh | Run at system startup | |
9.2 Apache Airflow (Overview)
- Platform for authoring, scheduling, monitoring workflows.
- Uses Python DAGs (Directed Acyclic Graphs).
- Better suited than cron for complex, dependent tasks.
- Provides web UI for monitoring.
- Supports retries, alerts, and dependency management.
10. Shell Scripting
10.1 Getting Started
#!/bin/bash — Shebang line specifying interpreter.echo — Print to terminal.chmod +x — Make script executable../script.sh — Run script.
10.2 Variables
- Assign variable (no spaces around
=). - Access variable with
$. - Safer with
${}. - Read input from user.
10.3 Conditionals
| Operator | Description |
|---|
-eq | Equal |
-ne | Not equal |
-gt | Greater than |
-lt | Less than |
-ge | Greater than or equal |
-le | Less than or equal |
-z | String is empty |
-f file | File exists and is regular |
-d dir | Directory exists |
10.4 Loops
10.5 Advanced Scripting Tips
- Use
set -e to exit on any error. - Use
set -x for debug mode (prints commands). - Always quote variables
"${var}" to handle spaces. - Use
[[ ]] for advanced test features. - Trap signals for cleanup:
trap 'cleanup' EXIT.
11. Quick Reference Cheat Sheet
| Command Group | Commands / Description |
|---|
| Navigation Basics | pwd, ls, cd |
| File Operations | mkdir, touch, rm, cp, mv |
| View File Contents | cat, less, head, tail |
| Search & Text Processing | grep, find, awk, sort, wc |
| Permissions Management | chmod, chown |
| Links and Aliases | ln -s, alias |
| Process Management | ps, top, kill, nohup |
| Disk and Archives | df, du, tar, zip |
| Remote Access | ssh, scp |
| Package Management | apt-get, wget |
| Scheduling Tasks | crontab -e |
| Shell Scripting | #!/bin/bash, chmod +x |