🐧 Linux Administration Guide

Master Linux system administration, shell scripting, networking, security, and performance tuning for DevOps excellence.

1. Overview

Linux is the backbone of modern DevOps infrastructure. This comprehensive guide covers everything from basic commands to advanced system administration, automation, and troubleshooting.

What You'll Learn:
  • Essential Linux commands and file system navigation
  • User and permission management
  • Shell scripting and automation
  • Networking and security configuration
  • Performance monitoring and troubleshooting
  • Package management and system updates

2. Essential Linux Commands

2.1 File System Navigation

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

# List files
ls                      # List files
ls -la                  # List all with details
ls -lh                  # Human readable sizes
ls -lt                  # Sort by modification time

# Print working directory
pwd

# Create directories
mkdir new_directory
mkdir -p path/to/nested/directory

# Remove files and directories
rm file.txt
rm -rf directory/       # Remove recursively (DANGEROUS!)

# Copy and move
cp source.txt destination.txt
cp -r source_dir/ destination_dir/
mv old_name.txt new_name.txt
mv file.txt /new/location/

2.2 File Content Operations

# View file contents
cat file.txt            # Display entire file
less file.txt           # Page through file
head file.txt           # First 10 lines
tail file.txt           # Last 10 lines
tail -f logfile.txt     # Follow file in real-time

# Search within files
grep "pattern" file.txt
grep -r "pattern" directory/
grep -i "pattern" file.txt      # Case insensitive

# Text processing
wc file.txt             # Word count
wc -l file.txt          # Line count
sort file.txt           # Sort lines
uniq file.txt           # Remove duplicates
cut -d',' -f1,3 file.csv    # Extract columns

2.3 System Information

# System information
uname -a                # System info
hostname                # Host name
uptime                  # System uptime
whoami                  # Current user
w                       # Who is logged in

# Resource usage
top                     # Process monitor
htop                    # Better process monitor
free -h                 # Memory usage
df -h                   # Disk usage
du -sh directory/       # Directory size

# Process management
ps aux                  # All processes
ps aux | grep nginx
kill <PID>              # Kill process
killall process_name    # Kill by name
pkill pattern           # Kill by pattern

3. User and Permission Management

3.1 User Management

# Add user
sudo useradd -m -s /bin/bash username
sudo passwd username

# Modify user
sudo usermod -aG sudo username      # Add to sudo group
sudo usermod -s /bin/zsh username   # Change shell

# Delete user
sudo userdel username
sudo userdel -r username            # Remove home directory

# View users
cat /etc/passwd
id username
groups username

3.2 File Permissions

# Permission format: rwxrwxrwx (owner, group, others)
# r=read(4), w=write(2), x=execute(1)

# Change permissions
chmod 755 script.sh     # rwxr-xr-x
chmod +x script.sh      # Add execute
chmod -w file.txt       # Remove write
chmod u+x,g+r file      # User execute, group read

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

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

3.3 Sudo and Root Access

# Run command as root
sudo command

# Switch to root
sudo -i
sudo su

# Edit sudoers file
sudo visudo

# Run as different user
sudo -u username command

# Sudo without password (add to /etc/sudoers)
username ALL=(ALL) NOPASSWD:ALL

4. Shell Scripting

4.1 Basic Script Structure

#!/bin/bash
# Script description

# Variables
NAME="DevOps"
COUNT=10

# Echo output
echo "Hello, $NAME"
echo "Count: $COUNT"

# Command substitution
CURRENT_DATE=$(date +%Y-%m-%d)
USER_COUNT=$(who | wc -l)

# Conditional
if [ $COUNT -gt 5 ]; then
    echo "Count is greater than 5"
elif [ $COUNT -eq 5 ]; then
    echo "Count equals 5"
else
    echo "Count is less than 5"
fi

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

while [ $COUNT -gt 0 ]; do
    echo "Countdown: $COUNT"
    COUNT=$((COUNT - 1))
    sleep 1
done

# Functions
function greet() {
    local name=$1
    echo "Hello, $name!"
}

greet "World"

4.2 Practical Examples

#!/bin/bash
# Backup script

SOURCE_DIR="/var/www"
BACKUP_DIR="/backups"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="backup_${DATE}.tar.gz"

# Create backup
echo "Starting backup..."
tar -czf ${BACKUP_DIR}/${BACKUP_FILE} ${SOURCE_DIR}

# Check if successful
if [ $? -eq 0 ]; then
    echo "Backup completed: ${BACKUP_FILE}"
    
    # Remove backups older than 7 days
    find ${BACKUP_DIR} -name "backup_*.tar.gz" -mtime +7 -delete
    echo "Old backups cleaned up"
else
    echo "Backup failed!"
    exit 1
fi

5. Networking

5.1 Network Configuration

# View network interfaces
ip addr show
ip a
ifconfig                # Legacy command

# View routing table
ip route show
route -n

# DNS configuration
cat /etc/resolv.conf

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

# Trace route
traceroute google.com
mtr google.com          # Better traceroute

# Check open ports
netstat -tulpn
ss -tulpn               # Modern alternative
lsof -i :80             # Check specific port

5.2 Firewall (UFW/iptables)

# UFW (Ubuntu)
sudo ufw status
sudo ufw enable
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw deny 3306
sudo ufw delete allow 80

# iptables
sudo iptables -L        # List rules
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 80 -j ACCEPT  # Delete

5.3 SSH Configuration

# Generate SSH key
ssh-keygen -t ed25519 -C "[email protected]"

# Copy key to server
ssh-copy-id user@server

# SSH config (~/.ssh/config)
Host myserver
    HostName 192.168.1.100
    User ubuntu
    Port 22
    IdentityFile ~/.ssh/id_ed25519

# Connect
ssh myserver

# SSH tunneling
ssh -L 8080:localhost:80 user@server      # Local forward
ssh -R 8080:localhost:80 user@server      # Remote forward

6. Package Management

6.1 Ubuntu/Debian (APT)

# Update package list
sudo apt update

# Upgrade packages
sudo apt upgrade
sudo apt full-upgrade

# Install package
sudo apt install nginx
sudo apt install -y docker.io      # Auto yes

# Remove package
sudo apt remove nginx
sudo apt purge nginx               # Remove with config
sudo apt autoremove                # Remove unused dependencies

# Search packages
apt search nginx
apt show nginx

# List installed
apt list --installed

6.2 RHEL/CentOS (YUM/DNF)

# Update packages
sudo yum update
sudo dnf update

# Install package
sudo yum install nginx
sudo dnf install docker

# Remove package
sudo yum remove nginx

# Search packages
yum search nginx
dnf search nginx

# List installed
yum list installed
dnf list installed

7. Service Management (systemd)

7.1 Service Control

# Start/stop/restart service
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx

# Enable/disable on boot
sudo systemctl enable nginx
sudo systemctl disable nginx

# Check status
sudo systemctl status nginx
systemctl is-active nginx
systemctl is-enabled nginx

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

7.2 Creating a Service

# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
User=appuser
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/start.sh
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Then reload and start:

sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp

8. System Monitoring and Performance

8.1 Resource Monitoring

# CPU usage
top
htop
mpstat              # Per-CPU statistics
sar -u 1 10         # CPU usage every 1 sec, 10 times

# Memory
free -h
vmstat 1 10

# Disk I/O
iostat
iotop

# Network
iftop
nethogs

# All in one
glances

8.2 Log Management

# 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

# Journalctl
journalctl -xe                  # Recent logs with explanation
journalctl -p err               # Only errors
journalctl --since "1 hour ago"
journalctl --until "2024-01-01"

# Log rotation
cat /etc/logrotate.conf

9. Security Best Practices

Security Checklist:
  • ✅ Keep system updated: sudo apt update && sudo apt upgrade
  • ✅ Use SSH keys instead of passwords
  • ✅ Disable root login: Edit /etc/ssh/sshd_configPermitRootLogin no
  • ✅ Enable firewall: sudo ufw enable
  • ✅ Use strong passwords: sudo passwd username
  • ✅ Install fail2ban: sudo apt install fail2ban
  • ✅ Regular backups and monitoring
  • ✅ Minimal package installation
  • ✅ Use sudo instead of root account
  • ✅ Enable SELinux/AppArmor

9.1 SSH Hardening

# Edit /etc/ssh/sshd_config
Port 2222                       # Change default port
PermitRootLogin no
PasswordAuthentication no       # Use keys only
PubkeyAuthentication yes
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2

# Restart SSH
sudo systemctl restart sshd

9.2 Fail2Ban Configuration

# Install
sudo apt install fail2ban

# Configuration (/etc/fail2ban/jail.local)
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3

[sshd]
enabled = true
port = 2222

# Start service
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

# Check status
sudo fail2ban-client status
sudo fail2ban-client status sshd

10. Troubleshooting

10.1 Common Issues

Issue Diagnosis Solution
Service won't start systemctl status service
journalctl -u service
Check logs, verify config, check permissions
Disk full df -h
du -sh /*
Clean logs, remove old packages, find large files
High CPU top
ps aux --sort=-%cpu
Identify process, optimize or restart
Network issues ping
traceroute
ss -tulpn
Check connectivity, firewall, DNS
Permission denied ls -l file
namei -l path
Fix permissions with chmod/chown

10.2 Useful Diagnostic Commands

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

# Find files modified in last 24 hours
find /var/log -type f -mtime -1

# Check for zombie processes
ps aux | grep 'Z'

# System resource limits
ulimit -a

# Open files by process
lsof -p <PID>

# Check for errors in logs
dmesg | grep -i error
journalctl -p err -xe

11. Additional Resources