1. Introduction
Bash scripting is a powerful tool for system administrators, enabling automation of routine tasks, system monitoring, and management of Linux/Unix systems. This guide covers essential Bash scripting techniques for system administration.
2. User Management
Automate user creation, modification, and deletion:
#!/bin/bash
# Create a new user
create_user() {
username=$1
useradd -m -s /bin/bash "$username"
echo "User $username created."
}
# Delete a user
delete_user() {
username=$1
userdel -r "$username"
echo "User $username deleted."
}
# Main menu
while true; do
echo "1. Create user"
echo "2. Delete user"
echo "3. Exit"
read -p "Choose an option: " choice
case $choice in
1) read -p "Enter username: " username; create_user "$username" ;;
2) read -p "Enter username: " username; delete_user "$username" ;;
3) exit 0 ;;
*) echo "Invalid option" ;;
esac
done
3. System Monitoring
Monitor system resources and log the results:
#!/bin/bash
log_file="/var/log/system_monitor.log"
while true; do
timestamp=$(date "+%Y-%m-%d %H:%M:%S")
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
memory_usage=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')
disk_usage=$(df -h | awk '$NF=="/"{printf "%s", $5}')
echo "$timestamp - CPU: $cpu_usage%, Memory: $memory_usage, Disk: $disk_usage" >> "$log_file"
sleep 300 # Wait for 5 minutes
done
4. Backup Management
Create a simple backup script:
#!/bin/bash
source_dir="/path/to/source"
backup_dir="/path/to/backup"
date=$(date +%Y-%m-%d)
backup_file="backup_$date.tar.gz"
# Create backup
tar -czf "$backup_dir/$backup_file" "$source_dir"
# Remove backups older than 30 days
find "$backup_dir" -name "backup_*.tar.gz" -mtime +30 -delete
echo "Backup completed: $backup_file"
5. Service Management
Manage system services:
#!/bin/bash
manage_service() {
service=$1
action=$2
sudo systemctl $action $service
}
# Usage
manage_service "apache2" "restart"
manage_service "mysql" "status"
6. Log Analysis
Analyze system logs for specific patterns:
#!/bin/bash
log_file="/var/log/syslog"
search_pattern="error"
grep -i "$search_pattern" "$log_file" | while read -r line; do
echo "$(date): $line" >> "/var/log/error_summary.log"
done
7. Network Monitoring
Monitor network connections and alert on high usage:
#!/bin/bash
threshold=1000 # Set your desired threshold
while true; do
connections=$(netstat -an | grep -c ESTABLISHED)
if [ $connections -gt $threshold ]; then
echo "High network usage detected: $connections connections" | mail -s "Network Alert"
[email protected]
fi
sleep 300 # Check every 5 minutes
done
Tip:
Always test your scripts in a safe environment before deploying them on production systems. Use error handling and logging to make your scripts more robust and easier to debug.
8. Automated Updates
Create a script to automate system updates:
#!/bin/bash
log_file="/var/log/system_updates.log"
echo "Starting system update at $(date)" >> "$log_file"
# Update package list
apt update >> "$log_file" 2>&1
# Upgrade packages
apt upgrade -y >> "$log_file" 2>&1
echo "System update completed at $(date)" >> "$log_file"
Further Learning
To enhance your system administration skills with Bash, consider exploring: