Linux
Overview
Linux is the backbone of modern DevOps infrastructure, providing a stable, open-source operating system for servers, containers, and cloud platforms.
Core Linux Concepts
File System Hierarchy
/
├── /bin → Essential executables
├── /etc → Configuration files
├── /home → User home directories
├── /var → Variable data (logs, cache)
├── /tmp → Temporary files
├── /usr → User programs and libraries
├── /opt → Optional software packages
└── /root → Root user home directory
File Permissions
# Symbolic representation: rwxrwxrwx (owner-group-others)
# Numeric: 755 = rwxr-xr-x
chmod 755 script.sh # Make executable
chmod 644 config.conf # Read/write for owner, read for others
chown user:group file.txt # Change ownership
Essential Linux Commands
| Category |
Command |
Purpose |
Example |
| Navigation |
cd |
Change directory |
cd /home/user/projects |
| Navigation |
pwd |
Print working directory |
pwd |
| File Listing |
ls -la |
List detailed files |
ls -la /etc |
| File Creation |
touch |
Create empty file |
touch new_file.txt |
| File Content |
cat |
Display file content |
cat config.yml |
| File Search |
find |
Search files |
find / -name "*.log" |
| Text Search |
grep |
Search text patterns |
grep -r "error" /var/log |
| File Copy |
cp |
Copy files/directories |
cp -r src/ dest/ |
| File Move |
mv |
Move/rename files |
mv old.txt new.txt |
| File Delete |
rm |
Remove files |
rm -rf /tmp/old_data |
| Disk Usage |
du -sh |
Directory size |
du -sh /var/log |
| Disk Space |
df -h |
Filesystem usage |
df -h |
User & Group Management
User Management
# Create new user
sudo useradd -m -s /bin/bash john
# Add user to group
sudo usermod -aG docker john
# Set user password
sudo passwd john
# Delete user
sudo userdel -r john
# List all users
cat /etc/passwd
Group Management
# Create group
sudo groupadd developers
# Add user to group
sudo usermod -aG developers john
# List groups
cat /etc/group
# Groups for current user
groups
System Administration
Service Management
# Start service
sudo systemctl start nginx
# Stop service
sudo systemctl stop nginx
# Restart service
sudo systemctl restart nginx
# Enable service on boot
sudo systemctl enable nginx
# Check service status
sudo systemctl status nginx
# List all services
systemctl list-units --type=service
Package Management
Debian/Ubuntu (apt)
# Update package list
sudo apt update
# Install package
sudo apt install git curl wget
# Remove package
sudo apt remove git
# Upgrade all packages
sudo apt upgrade
# Search for package
apt search nginx
Red Hat/CentOS (yum/dnf)
# Install package
sudo yum install git
# Remove package
sudo yum remove git
# List installed packages
yum list installed
# Update packages
sudo yum update
Process Management
# List running processes
ps aux
# Display processes in tree format
pstree
# Kill process by PID
kill -9 1234
# Kill process by name
pkill -f nginx
# Display top processes
top
# Send signal to process
kill -SIGTERM 1234
Network Administration
Network Configuration
# Display network interfaces
ip addr show
# Check IP configuration
ifconfig
# Display routing table
ip route show
route -n
# Test connectivity
ping 8.8.8.8
# DNS lookup
nslookup google.com
dig google.com
# Check open ports
sudo netstat -tlnp
ss -tlnp
Firewall Management
# Check UFW status
sudo ufw status
# Enable UFW
sudo ufw enable
# Allow SSH
sudo ufw allow 22/tcp
# Allow HTTP/HTTPS
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Deny service
sudo ufw deny 8080/tcp
# List rules
sudo ufw show added
File and Text Processing
Text Editing
# Edit file with nano
nano config.txt
# Edit file with vim
vim config.txt
# Quick edit with sed
sed -i 's/old_text/new_text/g' file.txt
# View file with less
less large_file.log
# Display first 10 lines
head -10 file.txt
# Display last 10 lines
tail -10 file.txt
# Follow file in real-time
tail -f /var/log/syslog
Text Processing
# Count lines, words, characters
wc -l file.txt
# Sort lines
sort file.txt
# Remove duplicates
sort file.txt | uniq
# Print specific columns
awk '{print $1, $3}' file.txt
# Replace text
sed 's/pattern/replacement/g' file.txt
Permissions & Ownership
| Permission |
Number |
Meaning |
| Read (r) |
4 |
View file contents |
| Write (w) |
2 |
Modify file contents |
| Execute (x) |
1 |
Run file/access directory |
Permission Examples
# Owner: rwx (7), Group: rx (5), Others: rx (5)
chmod 755 script.sh
# Owner: rw (6), Group: rw (6), Others: (0)
chmod 660 config.txt
# Add execute for group
chmod g+x script.sh
# Remove write for others
chmod o-w file.txt
# Recursive permission change
chmod -R 755 /var/www/html
Shell Scripting Basics
Simple Script
#!/bin/bash
# Define variables
NAME="DevOps Team"
VERSION="1.0"
# Display message
echo "Welcome to $NAME"
# Conditional statement
if [ -f "config.txt" ]; then
echo "Config file exists"
else
echo "Config file not found"
fi
# Loop through files
for file in *.log; do
echo "Processing $file"
done
# Function definition
backup_data() {
echo "Backing up data..."
tar -czf backup.tar.gz /data
}
backup_data
Script Execution
# Make script executable
chmod +x script.sh
# Run script
./script.sh
# Run with bash explicitly
bash script.sh
# Run in background
./script.sh &
# Run with nohup (ignore hangup)
nohup ./script.sh &
System Monitoring
# Display system uptime
uptime
# View system info
uname -a
# Check CPU info
lscpu
# Check memory usage
free -h
# Monitor real-time system stats
top
# View system logs
journalctl -xe
# Check disk I/O
iostat
# Monitor network traffic
iftop
nethogs
Security Best Practices
| Practice |
Command |
Purpose |
| SSH Key Auth |
ssh-keygen -t rsa -b 4096 |
Secure authentication |
| Disable Root Login |
PermitRootLogin no in sshd_config |
Reduce attack surface |
| Fail2Ban |
sudo apt install fail2ban |
Prevent brute force |
| SELinux/AppArmor |
Enable security module |
Mandatory access control |
| Regular Updates |
sudo apt update && apt upgrade |
Security patches |
| Audit Logging |
auditctl -w /etc/passwd |
Monitor changes |
Cron Jobs
Schedule Automated Tasks
# Edit crontab
crontab -e
# List user's cron jobs
crontab -l
# Remove all cron jobs
crontab -r
# Cron syntax
# MIN HOUR DAY MONTH WEEKDAY COMMAND
# 0 0 1 1 * /backup.sh
Common Cron Examples
| Schedule |
Cron Expression |
Purpose |
| Every minute |
* * * * * command |
Frequent checks |
| Every hour |
0 * * * * command |
Hourly tasks |
| Daily at 2 AM |
0 2 * * * command |
Daily backup |
| Weekly (Sunday) |
0 0 * * 0 command |
Weekly maintenance |
| Monthly (1st) |
0 0 1 * * command |
Monthly report |
Advanced Topics
System Tuning
# Display kernel parameters
sysctl -a
# Set kernel parameter
sudo sysctl -w net.ipv4.ip_forward=1
# Make persistent
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p
Log Management
# View system logs
journalctl
# Follow logs in real-time
journalctl -f
# Filter by priority
journalctl -p err
# Export logs
journalctl --since "2 days ago" > export.log
Systemd Services
# Create custom service
sudo nano /etc/systemd/system/myapp.service
# Reload systemd
sudo systemctl daemon-reload
# Enable service
sudo systemctl enable myapp
# Start service
sudo systemctl start myapp
Summary Table: Essential Linux Skills
| Skill |
Importance |
Tools |
| File Navigation |
Critical |
cd, ls, pwd |
| User Management |
Critical |
useradd, usermod, passwd |
| Package Management |
Critical |
apt, yum |
| Service Management |
High |
systemctl, service |
| Network Config |
High |
ip, ifconfig, netstat |
| Process Management |
High |
ps, kill, top |
| Shell Scripting |
High |
bash, sh |
| Log Analysis |
Medium |
grep, tail, journalctl |
| Security |
High |
ssh, chmod, firewall |
Resources