All posts

Linux Essentials for DevOps Engineers

· Pavan Y N · linux, fundamentals, command-line, devops


# title: "Linux Essentials for DevOps Engineers: Complete Guide" date: "2025-10-16" tags: ["linux", "bash", "scripting", "devops"]

Linux Essentials for DevOps Engineers: Complete Guide

Linux is the foundation of modern DevOps infrastructure. This comprehensive guide covers everything you need to master Linux for DevOps success.

# Why Linux for DevOps?

Linux powers the majority of servers, cloud infrastructure, and containers worldwide. Understanding Linux is non-negotiable for DevOps engineers.

Key Advantages:

  • →Open source and free
  • →Stable and secure
  • →Powerful command-line tools
  • →Excellent for automation
  • →Industry standard for servers

# Essential Shell Commands

## Navigation and File Management

code
# List files with details
ls -la

# Change directory
cd /path/to/directory
cd ~  # Home directory
cd -  # Previous directory

# Print working directory
pwd

# Create directory
mkdir new-folder
mkdir -p parent/child/grandchild  # Create nested

# Remove files and directories
rm file.txt
rm -rf directory/  # Recursive force delete

# Copy and move
cp source.txt destination.txt
mv old-name.txt new-name.txt

## File Content Operations

code
# View file contents
cat file.txt
less file.txt  # Paginated view
head -n 10 file.txt  # First 10 lines
tail -n 20 file.txt  # Last 20 lines
tail -f /var/log/app.log  # Follow log file

# Search in files
grep "error" logfile.txt
grep -r "pattern" /path/  # Recursive search
grep -i "case-insensitive" file.txt

# Text processing
sed 's/old/new/g' file.txt  # Replace text
awk '{print $1}' file.txt  # Print first column
cut -d',' -f1,3 data.csv  # Extract columns

# File Permissions

## Understanding Permission System

Linux uses a three-tier permission system:

code
rwxrwxrwx
│││││││││
│││└┴┴┴┴┴─ Others (everyone else)
││└┴┴┴─── Group (users in file's group)
│└┴┴─── Owner (file creator)
└── File type (- file, d directory, l link)

r = read (4)
w = write (2)
x = execute (1)

## Managing Permissions

code
# Change permissions
chmod 755 script.sh  # rwxr-xr-x
chmod u+x script.sh  # Add execute for owner
chmod go-w file.txt  # Remove write for group/others

# Change ownership
chown user:group file.txt
chown -R user:group directory/

# View permissions
ls -l file.txt
stat file.txt  # Detailed info

## Common Permission Patterns

code
644  # rw-r--r--  Files (owner read/write, others read)
755  # rwxr-xr-x  Executables and directories
600  # rw-------  Private files
700  # rwx------  Private executables

# Process Management

## Viewing Processes

code
# List all processes
ps aux
ps -ef

# Real-time process monitor
top
htop  # Enhanced version

# Find specific process
ps aux | grep nginx
pgrep nginx

# Process tree
pstree

## Managing Processes

code
# Start process in background
command &

# Bring to foreground
fg

# Send to background
bg

# Kill processes
kill PID
kill -9 PID  # Force kill
killall process-name
pkill -f pattern

## System Services

code
# SystemD service management
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl enable nginx  # Start on boot
systemctl disable nginx

# View service logs
journalctl -u nginx
journalctl -u nginx -f  # Follow logs
journalctl -u nginx --since today

# Bash Scripting Fundamentals

## Basic Script Structure

code
#!/bin/bash
# Shebang line - specifies interpreter

# Variables
NAME="DevOps"
COUNT=10

# Output
echo "Hello, $NAME"
echo "Count: $COUNT"

# Command substitution
CURRENT_DATE=$(date +%Y-%m-%d)
FILES=$(ls -1 | wc -l)

## Conditionals

code
# If statement
if [ -f "file.txt" ]; then
    echo "File exists"
elif [ -d "directory" ]; then
    echo "Directory exists"
else
    echo "Neither exists"
fi

# File tests
-f  # File exists
-d  # Directory exists
-r  # Readable
-w  # Writable
-x  # Executable
-s  # File not empty

# String comparison
if [ "$VAR" = "value" ]; then
    echo "Match"
fi

# Numeric comparison
if [ $NUM -gt 10 ]; then
    echo "Greater than 10"
fi
# -eq (equal), -ne (not equal), -lt (less than)
# -le (less or equal), -gt (greater), -ge (greater or equal)

## Loops

code
# For loop
for i in {1..5}; do
    echo "Number: $i"
done

# Loop through files
for file in *.txt; do
    echo "Processing: $file"
done

# While loop
COUNT=0
while [ $COUNT -lt 5 ]; do
    echo "Count: $COUNT"
    COUNT=$((COUNT + 1))
done

# Read file line by line
while IFS= read -r line; do
    echo "Line: $line"
done < input.txt

## Functions

code
# Define function
backup_files() {
    local source=$1
    local dest=$2
    
    echo "Backing up $source to $dest"
    cp -r "$source" "$dest"
}

# Call function
backup_files "/data" "/backup"

# Function with return value
get_timestamp() {
    echo $(date +%Y%m%d_%H%M%S)
}

TIMESTAMP=$(get_timestamp)

# Log Analysis and Troubleshooting

## Viewing Logs

code
# System logs
tail -f /var/log/syslog
tail -f /var/log/messages

# Application logs
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log

# Journal logs
journalctl -xe  # Recent errors
journalctl -p err  # Error priority
journalctl --since "1 hour ago"

## Log Analysis

code
# Count occurrences
grep "ERROR" app.log | wc -l

# Find unique IPs
awk '{print $1}' access.log | sort | uniq -c | sort -rn

# Extract specific fields
grep "ERROR" app.log | awk '{print $1, $2, $5}'

# Filter by time range
sed -n '/2024-01-15 10:00/,/2024-01-15 11:00/p' app.log

# Networking Basics

code
# Check network interfaces
ip addr show
ifconfig

# Test connectivity
ping google.com
ping -c 4 8.8.8.8

# DNS lookup
nslookup google.com
dig google.com

# Port scanning
netstat -tulpn  # Listening ports
ss -tulpn  # Modern alternative

# Test port connectivity
telnet hostname 80
nc -zv hostname 80

# Download files
wget https://example.com/file.zip
curl -O https://example.com/file.zip

# Disk and System Management

code
# Disk usage
df -h  # Filesystem usage
du -sh /path  # Directory size
du -h --max-depth=1 /path  # Subdirectory sizes

# Find large files
find / -type f -size +100M

# System information
uname -a  # System info
uptime  # System uptime
free -h  # Memory usage
lscpu  # CPU info

# Practical DevOps Scripts

## Deployment Script

code
#!/bin/bash
set -e  # Exit on error

APP_NAME="myapp"
VERSION=$1

if [ -z "$VERSION" ]; then
    echo "Usage: $0 <version>"
    exit 1
fi

echo "Deploying $APP_NAME version $VERSION"

# Pull latest code
git pull origin main

# Build application
docker build -t $APP_NAME:$VERSION .

# Stop old container
docker stop $APP_NAME || true
docker rm $APP_NAME || true

# Start new container
docker run -d --name $APP_NAME \
    -p 8080:8080 \
    $APP_NAME:$VERSION

echo "Deployment complete!"

## Backup Script

code
#!/bin/bash

BACKUP_DIR="/backup"
SOURCE_DIR="/data"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="backup_$DATE.tar.gz"

# Create backup
tar -czf "$BACKUP_DIR/$BACKUP_FILE" "$SOURCE_DIR"

# Keep only last 7 backups
cd "$BACKUP_DIR"
ls -t backup_*.tar.gz | tail -n +8 | xargs rm -f

echo "Backup created: $BACKUP_FILE"

# Best Practices

  1. •Use absolute paths in scripts
  2. •Always quote variables: "$VAR"
  3. •Check command success: if [ $? -eq 0 ]
  4. •Use set -e to exit on errors
  5. •Add logging to scripts
  6. •Test scripts in safe environment first
  7. •Use version control for scripts
  8. •Document your scripts with comments

# Conclusion

Mastering Linux is essential for DevOps success. Practice these commands daily, write automation scripts, and continuously expand your Linux knowledge.

# Next Steps

  1. •Set up a Linux VM for practice
  2. •Write daily automation scripts
  3. •Learn advanced bash scripting
  4. •Explore system administration
  5. •Study Linux security practices

Happy Linux learning! 🐧