Linux服务器(RedHat、CentOS系)安全相关巡检shell脚本

提示:巡检脚本可以使用crontab定时执行,人工根据执行结束时间点统一收集报告文件即可。

bash 复制代码
#!/bin/bash

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

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

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


# 获取最后六次Session记录
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
# 获取防火墙状态
firewall_status=$(systemctl is-active firewalld)
echo "-- 防火墙状态:$firewall_status" >> $output_file

# 获取防火墙开放端口
if [ "$firewall_status" = "active" ]; then
    firewall_open_ports=$(firewall-cmd --list-ports)
    firewall_open_services=$(firewall-cmd --list-services)
else
    firewall_open_ports="防火墙未激活"
    firewall_open_services="防火墙未激活"
fi
echo "-- 防火墙开放端口:$firewall_open_ports" >> $output_file
echo "-- 防火墙开放服务:$firewall_open_services" >> $output_file

# 密码有效期策略,脚本中username即用户名需要根据实际使用进行修改****************
password_policy=$(chage -l 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_lock_policy=$(grep -w "pam_tally2" /etc/pam.d/system-auth)
echo "-- 账户锁定策略:" >> $output_file
echo "  $account_lock_policy" >> $output_file

# 密码强度策略
echo "-- 密码强度策略:" >> "$output_file"
grep "pam_cracklib.so" /etc/pam.d/system-auth >> "$output_file"

echo "===============================================" >> $output_file
# 显示报告
cat $output_file
相关推荐
CertiK1 小时前
IBW 2025: CertiK首席商务官出席,探讨AI与Web3融合带来的安全挑战
人工智能·安全·web3
xuanzdhc1 小时前
Linux 基础IO
linux·运维·服务器
愚润求学1 小时前
【Linux】网络基础
linux·运维·网络
bantinghy2 小时前
Linux进程单例模式运行
linux·服务器·单例模式
m0_738120722 小时前
玄机——某学校系统中挖矿病毒应急排查
网络·安全·web安全
小和尚同志3 小时前
29.4k!使用 1Panel 来管理你的服务器吧
linux·运维
帽儿山的枪手3 小时前
为什么Linux需要3种NAT地址转换?一探究竟
linux·网络协议·安全
shadon1789 天前
回答 如何通过inode client的SSLVPN登录之后,访问需要通过域名才能打开的服务
linux
AWS官方合作商9 天前
AWS ACM 重磅上线:公有 SSL/TLS 证书现可导出,突破 AWS 边界! (突出新功能的重要性和突破性)
服务器·https·ssl·aws
小米里的大麦9 天前
014 Linux 2.6内核进程调度队列(了解)
linux·运维·驱动开发