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

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

#!/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

相关推荐

  1. 服务器脚本linux

    2024-03-17 05:30:06       24 阅读
  2. 什么?通过 Prometheus 编写脚本

    2024-03-17 05:30:06       42 阅读
  3. Redis一键脚本

    2024-03-17 05:30:06       7 阅读
  4. AD域服务器指南

    2024-03-17 05:30:06       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-17 05:30:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-17 05:30:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-17 05:30:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-17 05:30:06       18 阅读

热门阅读

  1. K8S下微服务平滑发布的一些思考与总结

    2024-03-17 05:30:06       19 阅读
  2. Spring-Kafka笔记整理

    2024-03-17 05:30:06       18 阅读
  3. ZooKeeper

    ZooKeeper

    2024-03-17 05:30:06      16 阅读
  4. Spring Boot集成mapstruct快速入门指南

    2024-03-17 05:30:06       18 阅读
  5. 封装promise请求方式

    2024-03-17 05:30:06       19 阅读
  6. OLLAMA:如何像云端一样运行本地大语言模型

    2024-03-17 05:30:06       20 阅读
  7. alibaba cloud linux 3 安装 psql 16

    2024-03-17 05:30:06       21 阅读
  8. Python强大的库和框架——TensorFlow

    2024-03-17 05:30:06       18 阅读
  9. springBoot整合Redis(四、整合redis 实现分布式锁)

    2024-03-17 05:30:06       19 阅读