linux-shell-scripting について
linux-shell-scriptingは、Linuxのシステム管理と自動化に使えるbashスクリプトのテンプレート集です。システム管理者やセキュリティ運用担当者向けに、バックアップ、監視、ログ分析、リモート実行などを扱います。
主な機能
- 01ファイルとデータベースのバックアップ
- 02CPU、ディスク、システム状態の監視
- 03ユーザーとパスワードの管理
- 04ログとWebサーバーログの分析
- 05ネットワーク確認とタスクスケジュール
- 06暗号化、クリーンアップ、リモート実行
活用シーン
- 01定期バックアップとローテーションの自動化
- 02システムリソース使用状況の追跡
- 03システムログからのエラー抽出
- 04メンテナンスの予約とリモートスクリプトの実行
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ zebbern/ claude-code-guide --skill linux-shell-scriptingインストール後、Claude Code で/linux-shell-scriptingを入力して読み込まれたか確認します。
詳細
Linux 本番環境向けシェルスクリプト
目的
バックアップ、監視、ユーザー管理、ログ分析、自動化など、一般的な Linux システム管理タスク向けの本番環境対応シェルスクリプトテンプレートを提供します。これらのスクリプトは、セキュリティ運用およびペネトレーションテスト環境の構成要素として利用できます。
前提条件
必要な環境
- Linux/Unix システム(bash シェル)
- タスクに対する適切な権限
- 必要なユーティリティのインストール(rsync、openssl など)
必要な知識
- bash スクリプトの基礎
- Linux のファイルシステム構造
- システム管理の概念
出力と成果物
- バックアップソリューション - ファイルおよびデータベースの自動バックアップ
- 監視スクリプト - リソース使用状況の追跡
- 自動化ツール - スケジュールされたタスクの実行
- セキュリティスクリプト - パスワード管理、暗号化
主要なワークフロー
フェーズ 1: ファイルバックアップスクリプト
基本的なディレクトリバックアップ
#!/bin/bash
backup_dir="/path/to/backup"
source_dir="/path/to/source"
# Create a timestamped backup of the source directory
tar -czf "$backup_dir/backup_$(date +%Y%m%d_%H%M%S).tar.gz" "$source_dir"
echo "Backup completed: backup_$(date +%Y%m%d_%H%M%S).tar.gz"
リモートサーバーバックアップ
#!/bin/bash
source_dir="/path/to/source"
remote_server="user@remoteserver:/path/to/backup"
# Backup files/directories to a remote server using rsync
rsync -avz --progress "$source_dir" "$remote_server"
echo "Files backed up to remote server."
バックアップローテーションスクリプト
#!/bin/bash
backup_dir="/path/to/backups"
max_backups=5
# Rotate backups by deleting the oldest if more than max_backups
while [ $(ls -1 "$backup_dir" | wc -l) -gt "$max_backups" ]; do
oldest_backup=$(ls -1t "$backup_dir" | tail -n 1)
rm -r "$backup_dir/$oldest_backup"
echo "Removed old backup: $oldest_backup"
done
echo "Backup rotation completed."
データベースバックアップスクリプト
#!/bin/bash
database_name="your_database"
db_user="username"
db_pass="password"
output_file="database_backup_$(date +%Y%m%d).sql"
# Perform database backup using mysqldump
mysqldump -u "$db_user" -p"$db_pass" "$database_name" > "$output_file"
gzip "$output_file"
echo "Database backup created: $output_file.gz"
フェーズ 2: システム監視スクリプト
CPU 使用率モニター
#!/bin/bash
threshold=90
# Monitor CPU usage and trigger alert if threshold exceeded
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d. -f1)
if [ "$cpu_usage" -gt "$threshold" ]; then
echo "ALERT: High CPU usage detected: $cpu_usage%"
# Add notification logic (email, slack, etc.)
# mail -s "CPU Alert" [email protected] <<< "CPU usage: $cpu_usage%"
fi
ディスク容量モニター
#!/bin/bash
threshold=90
partition="/dev/sda1"
# Monitor disk usage and trigger alert if threshold exceeded
disk_usage=$(df -h | grep "$partition" | awk '{print $5}' | cut -d% -f1)
if [ "$disk_usage" -gt "$threshold" ]; then
echo "ALERT: High disk usage detected: $disk_usage%"
# Add alert/notification logic here
fi
CPU 使用率ロガー
#!/bin/bash
output_file="cpu_usage_log.txt"
# Log current CPU usage to a file with timestamp
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
cpu_usage=$(top -bn1 | grep 'Cpu(s)' | awk '{print $2}' | cut -d. -f1)
echo "$timestamp - CPU Usage: $cpu_usage%" >> "$output_file"
echo "CPU usage logged."
システムヘルスチェック
#!/bin/bash
output_file="system_health_check.txt"
# Perform system health check and save results to a file
{
echo "System Health Check - $(date)"
echo "================================"
echo ""
echo "Uptime:"
uptime
echo ""
echo "Load Average:"
cat /proc/loadavg
echo ""
echo "Memory Usage:"
free -h
echo ""
echo "Disk Usage:"
df -h
echo ""
echo "Top Processes:"
ps aux --sort=-%cpu | head -10
} > "$output_file"
echo "System health check saved to $output_file"
フェーズ 3: ユーザー管理スクリプト
ユーザーアカウント作成
#!/bin/bash
username="newuser"
# Check if user exists; if not, create new user
if id "$username" &>/dev/null; then
echo "User $username already exists."
else
useradd -m -s /bin/bash "$username"
echo "User $username created."
# Set password interactively
passwd "$username"
fi
パスワード有効期限チェッカー
#!/bin/bash
output_file="password_expiry_report.txt"
# Check password expiry for users with bash shell
echo "Password Expiry Report - $(date)" > "$output_file"
echo "=================================" >> "$output_file"
IFS=$'\n'
for user in $(grep "/bin/bash" /etc/passwd | cut -d: -f1); do
password_expires=$(chage -l "$user" 2>/dev/null | grep "Password expires" | awk -F: '{print $2}')
echo "User: $user - Password Expires: $password_expires" >> "$output_file"
done
unset IFS
echo "Password expiry report saved to $output_file"
フェーズ 4: セキュリティスクリプト
パスワードジェネレーター
#!/bin/bash
length=${1:-16}
# Generate a random password
password=$(openssl rand -base64 48 | tr -dc 'a-zA-Z0-9!@#$%^&*' | head -c"$length")
echo "Generated password: $password"
ファイル暗号化スクリプト
#!/bin/bash
file="$1"
action="${2:-encrypt}"
if [ -z "$file" ]; then
echo "Usage: $0 <file> [encrypt|decrypt]"
exit 1
fi
if [ "$action" == "encrypt" ]; then
# Encrypt file using AES-256-CBC
openssl enc -aes-256-cbc -salt -pbkdf2 -in "$file" -out "$file.enc"
echo "File encrypted: $file.enc"
elif [ "$action" == "decrypt" ]; then
# Decrypt file
output_file="${file%.enc}"
openssl enc -aes-256-cbc -d -pbkdf2 -in "$file" -out "$output_file"
echo "File decrypted: $output_file"
fi
フェーズ 5: ログ分析スクリプト
エラーログ抽出ツール
#!/bin/bash
logfile="${1:-/var/log/syslog}"
output_file="error_log_$(date +%Y%m%d).txt"
# Extract lines with "ERROR" from the log file
grep -i "error\|fail\|critical" "$logfile" > "$output_file"
echo "Error log created: $output_file"
echo "Total errors found: $(wc -l < "$output_file")"
Web サーバーログアナライザー
#!/bin/bash
log_file="${1:-/var/log/apache2/access.log}"
echo "Web Server Log Analysis"
echo "========================"
echo ""
echo "Top 10 IP Addresses:"
awk '{print $1}' "$log_file" | sort | uniq -c | sort -rn | head -10
echo ""
echo "Top 10 Requested URLs:"
awk '{print $7}' "$log_file" | sort | uniq -c | sort -rn | head -10
echo ""
echo "HTTP Status Code Distribution:"
awk '{print $9}' "$log_file" | sort | uniq -c | sort -rn
フェーズ 6: ネットワークスクリプト
ネットワーク接続チェッカー
#!/bin/bash
hosts=("8.8.8.8" "1.1.1.1" "google.com")
echo "Network Connectivity Check"
echo "=========================="
for host in "${hosts[@]}"; do
if ping -c 1 -W 2 "$host" &>/dev/null; then
echo "[UP] $host is reachable"
else
echo "[DOWN] $host is unreachable"
fi
done
Web サイト稼働時間チェッカー
#!/bin/bash
websites=("https://google.com" "https://github.com")
log_file="uptime_log.txt"
echo "Website Uptime Check - $(date)" >> "$log_file"
for website in "${websites[@]}"; do
if curl --output /dev/null --silent --head --fail --max-time 10 "$website"; then
echo "[UP] $website is accessible" | tee -a "$log_file"
else
echo "[DOWN] $website is inaccessible" | tee -a "$log_file"
fi
done
ネットワークインターフェース情報
#!/bin/bash
interface="${1:-eth0}"
echo "Network Interface Information: $interface"
echo "========================================="
ip addr show "$interface" 2>/dev/null || ifconfig "$interface" 2>/dev/null
echo ""
echo "Routing Table:"
ip route | grep "$interface"
フェーズ 7: 自動化スクリプト
パッケージの自動インストール
#!/bin/bash
packages=("vim" "htop" "curl" "wget" "git")
echo "Installing packages..."
for package in "${packages[@]}"; do
if dpkg -l | grep -q "^ii $package"; then
echo "[SKIP] $package is already installed"
else
sudo apt-get install -y "$package"
echo "[INSTALLED] $package"
fi
done
echo "Package installation completed."
タスクスケジューラー(Cron 設定)
#!/bin/bash
scheduled_task="/path/to/your_script.sh"
schedule_time="0 2 * * *" # Run at 2 AM daily
# Add task to crontab
(crontab -l 2>/dev/null; echo "$schedule_time $scheduled_task") | crontab -
echo "Task scheduled: $schedule_time $scheduled_task"
サービス再起動スクリプト
#!/bin/bash
service_name="${1:-apache2}"
# Restart a specified service
if systemctl is-active --quiet "$service_name"; then
echo "Restarting $service_name..."
sudo systemctl restart "$service_name"
echo "Service $service_name restarted."
else
echo "Service $service_name is not running. Starting..."
sudo systemctl start "$service_name"
echo "Service $service_name started."
fi
フェーズ 8: ファイル操作
ディレクトリ同期
#!/bin/bash
source_dir="/path/to/source"
destination_dir="/path/to/destination"
# Synchronize directories using rsync
rsync -avz --delete "$source_dir/" "$destination_dir/"
echo "Directories synchronized successfully."
データクリーンアップスクリプト
#!/bin/bash
directory="${1:-/tmp}"
days="${2:-7}"
echo "Cleaning files older than $days days in $directory"
# Remove files older than specified days
find "$directory" -type f -mtime +"$days" -exec rm -v {} \;
echo "Cleanup completed."
フォルダサイズチェッカー
#!/bin/bash
folder_path="${1:-.}"
echo "Folder Size Analysis: $folder_path"
echo "===================================="
# Display sizes of subdirectories sorted by size
du -sh "$folder_path"/* 2>/dev/null | sort -rh | head -20
echo ""
echo "Total size:"
du -sh "$folder_path"
フェーズ 9: システム情報
システム情報コレクター
#!/bin/bash
output_file="system_info_$(hostname)_$(date +%Y%m%d).txt"
{
echo "System Information Report"
echo "Generated: $(date)"
echo "========================="
echo ""
echo "Hostname: $(hostname)"
echo "OS: $(uname -a)"
echo ""
echo "CPU Info:"
lscpu | grep -E "Model name|CPU\(s\)|Thread"
echo ""
echo "Memory:"
free -h
echo ""
echo "Disk Space:"
df -h
echo ""
echo "Network Interfaces:"
ip -br addr
echo ""
echo "Logged In Users:"
who
} > "$output_file"
echo "System info saved to $output_file"
フェーズ 10: Git と開発
Git リポジトリアップデーター
#!/bin/bash
git_repos=("/path/to/repo1" "/path/to/repo2")
for repo in "${git_repos[@]}"; do
if [ -d "$repo/.git" ]; then
echo "Updating repository: $repo"
cd "$repo"
git fetch --all
git pull origin "$(git branch --show-current)"
echo "Updated: $repo"
else
echo "Not a git repository: $repo"
fi
done
echo "All repositories updated."
リモートスクリプト実行
#!/bin/bash
remote_server="${1:-user@remote-server}"
remote_script="${2:-/path/to/remote/script.sh}"
# Execute a script on a remote server via SSH
ssh "$remote_server" "bash -s" < "$remote_script"
echo "Remote script executed on $remote_server"
クイックリファレンス
よく使うスクリプトパターン
| パターン | 目的 |
|---|---|
#!/bin/bash | bash 用のシェバン |
$(date +%Y%m%d) | 日付のフォーマット |
$((expression)) | 算術演算 |
${var:-default} | デフォルト値 |
"$@" | すべての引数 |
便利なコマンド
| コマンド | 目的 |
|---|---|
chmod +x script.sh | 実行可能にする |
./script.sh | スクリプトを実行 |
nohup ./script.sh & | バックグラウンドで実行 |
crontab -e | cron ジョブを編集 |
source script.sh | 現在のシェルで実行 |
Cron の形式
Minute(0-59) Hour(0-23) Day(1-31) Month(1-12) Weekday(0-7, 0/7=Sun)
制約と制限事項
- スクリプトは必ず本番環境以外で先にテストしてください
- エラーを避けるため絶対パスを使用してください
- スペースを正しく扱うために変数はクォートしてください
- 多くのスクリプトは root/sudo 権限が必要です
- デバッグには
bash -x script.shを使用してください
コメント
Markdown 対応