Linux服务器(Debian系)包含UOS安全相关巡检shell脚本

bash 复制代码
#!/bin/bash

# Define output file
current_date=$(date "+%Y%m%d") # Gets the current date in YYYYMMDD format
output_file="server_security_inspection_report_${current_date}.txt"

# Empty the file initially
echo '' > $output_file

# 获取巡检时间 (Get the inspection time)
inspection_time=$(date "+%Y-%m-%d %H:%M:%S")

# 获取主机IP地址 (Get the host IP address)
host_ip=$(hostname -I | awk '{print $1}')

# 获取最后六次Session记录 (Get the last six session records)
last_logins=$(last -n 6 -w)

echo "===============================================" > $output_file
echo "-- 巡检时间:$inspection_time" >> $output_file
echo "-- 主机IP地址:$host_ip" >> $output_file
echo "-- 最后六次Session记录:" >> $output_file
echo "$last_logins" >> $output_file

# 获取防火墙状态 (Get the firewall status)
# UOS (Debian/Ubuntu-based) 通常使用 UFW,如果使用其他方式需要调整此部分脚本
firewall_status=$(ufw status | head -n 1)
echo "-- 防火墙状态:$firewall_status" >> $output_file
ufw_status=$(ufw status | grep Status | awk '{print $2}')
if [ "$ufw_status" != "active" ]; then
    firewall_open_ports_and_service="防火墙未激活"
else
    # 获取开放端口或服务, 逗号分隔
    firewall_open_ports_and_service=$(ufw status | grep ALLOW | awk '{print $1}' | sort | uniq | paste -sd, - )
fi
echo "-- 防火墙开放端口/服务:$firewall_open_ports_and_service" >> $output_file


# 密码有效期策略,脚本中username即用户名需要根据实际使用进行修改
password_policy=$(chage -l username 2>/dev/null || echo "chage 命令执行失败,请检查是否修改脚本中指定用户名(默认username需要改为实际使用用户名)")
password_max_days=$(grep -w "PASS_MAX_DAYS" /etc/login.defs | grep -v ^#)
echo "-- 密码有效期策略:$password_max_days" >> $output_file
echo "-- 指定用户有效期(非root):" >> $output_file
echo "$password_policy" >> $output_file

# 账户锁定策略 (Account lockout policy)
echo "-- 账户锁定策略:" >> $output_file
grep "pam_tally2\|pam_faillock" /etc/pam.d/common-auth >> $output_file

# 密码强度策略 (Password strength policy)
echo "-- 密码强度策略:" >> "$output_file"
grep "pam_cracklib.so\|pam_pwquality.so" /etc/pam.d/common-password >> "$output_file"

echo "===============================================" >> $output_file

# 显示报告 (Display the report)
cat $output_file

巡检结果输出样例:

相关推荐
杰 .11 小时前
Linux工具使用
linux·服务器
Gc9umsbL111 小时前
零基础学Linux:21天从“命令小白”到独立部署服务器
linux·运维·服务器
测试员周周11 小时前
【AI测试功能5】AI功能测试的“黄金数据集“构建指南:从0到1搭建质量评估体系
运维·服务器·开发语言·人工智能·python·功能测试·集成测试
骑着骆驼写程序12 小时前
Ubuntu上部署前端项目报500错误
linux·运维·ubuntu
小娄~~13 小时前
进程间通信
linux·运维·服务器
企业网盘服务谷雨网络13 小时前
自建服务器还是云存储?企业存储选型没有标准答案
服务器·数据安全·云存储·企业云盘·企业资产
祁_z13 小时前
LangSmith 实操指南「Agent 可观测性系统」
java·服务器
实心儿儿13 小时前
Linux —— 库的制作和原理(2)
linux·运维·服务器
运维全栈笔记13 小时前
Docker一键部署Immich:自建私有云相册,照片视频备份无忧
linux·服务器·网络·docker·容器
yyuuuzz13 小时前
企业出海中的技术稳定性问题梳理
运维·服务器·网络·github·aws