当然,请注意以下是20个常用的Linux服务器检查脚本示例。这些脚本可以用于自动化运行并检查服务器的各个方面,以确保服务器的稳定性和安全性。
- 检查系统负载:
#!/bin/bash
load=$(uptime | awk -F 'load average:' '{print $2}' | cut -d, -f1)
if (( $(echo "$load > 1.0" | bc -l) )); then
echo "High load alert! Current load: $load"
else
echo "Load is normal. Current load: $load"
fi
- 检查内存使用情况:
#!/bin/bash
memory=$(free -m | awk '/Mem:/ {print $3}')
if (( memory > 80 )); then
echo "High memory usage alert! Current usage: $memory MB"
else
echo "Memory usage is normal. Current usage: $memory MB"
fi
- 检查磁盘空间:
#!/bin/bash
disk=$(df -h | awk '$NF=="/"{print $(NF-1)}')
if (( $(echo "$disk > 90" | bc -l) )); then
echo "Low disk space alert! Current usage: $disk%"
else
echo "Disk space is sufficient. Current usage: $disk%"
fi
- 检查网络连接数:
#!/bin/bash
connections=$(netstat -an | grep -c ESTABLISHED)
echo "Current established connections: $connections"
- 检查日志文件:
#!/bin/bash
if grep -q "ERROR" /var/log/syslog; then
echo "Error found in syslog!"
else
echo "No errors found in syslog."
fi
- 检查CPU使用情况:
#!/bin/bash
cpu=$(top -bn1 | awk '/%Cpu/{print $2}')
if (( $(echo "$cpu > 80" | bc -l) )); then
echo "High CPU usage alert! Current usage: $cpu%"
else
echo "CPU usage is normal. Current usage: $cpu%"
fi
- 检查关键服务状态:
#!/bin/bash
services=("apache2" "mysql" "ssh")
for service in "${services[@]}"; do
if systemctl is-active --quiet $service; then
echo "$service is running."
else
echo "$service is not running!"
fi
done
- 检查防火墙规则:
#!/bin/bash
rules=$(iptables -L INPUT -n | grep -c DROP)
echo "Current iptables rules: $rules"
- 检查计划任务:
#!/bin/bash
tasks=$(crontab -l)
if [ -z "$tasks" ]; then
echo "No scheduled tasks found."
else
echo "Scheduled tasks:"
echo "$tasks"
fi
- 检查SSH配置:
#!/bin/bash
ssh_config="/etc/ssh/sshd_config"
if grep -q "PasswordAuthentication no" $ssh_config && grep -q "PermitRootLogin no" $ssh_config; then
echo "SSH configuration is secure."
else
echo "SSH configuration is not secure!"
fi
- 检查系统更新:
#!/bin/bash
updates=$(apt list --upgradable 2>/dev/null | grep -c upgradable)
echo "Available system updates: $updates"
- 检查安全补丁:
#!/bin/bash
security_updates=$(apt list --upgradable 2>/dev/null | grep -c security)
if (( security_updates > 0 )); then
echo "Security updates available: $security_updates"
else
echo "No security updates available."
fi
- 检查密码策略:
#!/bin/bash
password_policy=$(grep -E "PASS_MAX_DAYS|PASS_MIN_DAYS|PASS_WARN_AGE" /etc/login.defs)
echo "Password policy settings:"
echo "$password_policy"
- 检查僵尸进程:
#!/bin/bash
zombies=$(ps aux | grep Z | grep -v grep)
if [ -n "$zombies" ]; then
echo "Zombie processes found:"
echo "$zombies"
else
echo "No zombie processes found."
fi
- 检查系统时间同步:
#!/bin/bash
time_synced=$(timedatectl show --property=NTPSynchronized --value)
if [ "$time_synced" = "yes" ]; then
echo "System time is synchronized."
else
echo "System time is not synchronized!"
fi
- 检查远程登录权限:
#!/bin/bash
allow=$(grep -v "^#" /etc/hosts.allow)
deny=$(grep -v "^#" /etc/hosts.deny)
echo "Allowed hosts:"
echo "$allow"
echo "Denied hosts:"
echo "$deny"
- 检查文件完整性:
#!/bin/bash
if ! [ -x "$(command -v tripwire)" ]; then
echo "Tripwire is not installed. Please install it for file integrity checking."
else
echo "Checking file integrity with Tripwire..."
# Run Tripwire command here
fi
- 检查用户权限:
#!/bin/bash
users=$(cat /etc/passwd | cut -d: -f1)
for user in $users; do
sudo -lU $user >/dev/null 2>&1 && echo "User $user has sudo privileges." || echo "User $user does not have sudo privileges."
done
- 检查运行的服务:
#!/bin/bash
running_services=$(systemctl list-units --type=service --state=running --no-pager)
if [ -n "$running_services" ]; then
echo "Running services:"
echo "$running_services"
else
echo "No running services found."
fi
- 定期备份:
#!/bin/bash
backup_dir="/path/to/backup/directory"
if ! [ -d "$backup_dir" ]; then
echo "Backup directory does not exist!"
else
echo "Running backup..."
# Run backup command here
fi
这些脚本示例可以根据您的需求进行修改和调整。请确保在执行脚本之前对其进行适当的测试,并根据您的服务器配置进行必要的修改。
服务器租用托管,机房租用托管,主机租用托管,https://www.e1idc.com