Linux 主机一键安全整改策略

为防止linux主机被恶意攻击,和受到攻击后能更快定位到源头,需要对linux主机做一些参数配置。

比如禁用root的远程登录、用户多次密码验证失败后被锁、禁止系统账号交互式登录等等。

下面是linux主机安全整改的一些简单介绍,最后会通过脚本一键整改所有项,适用linux主机版本为:Redhat 7和CentOS 7。

本次安全整改项

  • 禁止root用户远程登录;

  • 设置密码复杂度(新建账号和修改密码时生效);

  • 设置密码过期时间90天;

  • 设置命令行界面超时退出(180秒);

  • 设置密码重复使用次数(5次);

  • 禁止系统账号交互式登录;

  • 设置ssh登录警告banner;

  • 禁用不必要的别名;

  • 安装配置记录用户对设备的操作的pacct工具;

  • 配置su命令使用情况记录。

通过以下命令一键安全整改

复制代码

#!/bin/bash

source /etc/profile

echo "---------禁止root用户远程登录----------"

result=`grep "PermitRootLogin no" /etc/ssh/sshd_config|wc -l` ; if [ $result -eq 0 ];then

sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config

sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config

echo "禁止root用户远程登录已修改"

else

echo "禁止root用户远程登录已存在"

fi

echo -e "\n"

echo "---------设置密码复杂度限制----------"

result1=`grep "password requisite pam_cracklib.so" /etc/pam.d/system-auth|wc -l` ; if [ $result1 -eq 0 ];then

echo "password requisite pam_cracklib.so try_first_pass retry=3 minlen=8 lcredit=-1 dcredit=-1 ocredit=-1 ucredit=-1" >> /etc/pam.d/system-auth

echo "密码复杂度限制设置完成"

else

echo "密码复杂度限制已存在"

fi

echo -e "\n"

echo "---------设置用户密码过期时间----------"

result=` cat /etc/login.defs|grep PASS_MAX_DAYS|grep ^[^#]|awk '{print 2}'\` ; if \[ result -gt 90 ];then

sed -i '/PASS_MAX_DAYS/ s/99999/90/' /etc/login.defs

echo "用户密码过期时间设置完成"

else

echo "用户密码过期时间已设置为90天"

fi

echo -e "\n"

echo "---------设置命令行界面超时退出----------"

result=` grep TMOUT /etc/profile |wc -l` ; if [ $result -eq 0 ];then

cp -p /etc/profile /etc/profile_bak

echo "export TMOUT=180">> /etc/profile

echo "命令行界面超时退出已设置"

else

echo "命令行界面超时退出已存在"

fi

echo -e "\n"

echo "---------设置密码重复使用次数----------"

result=` grep remember= /etc/pam.d/system-auth|wc -l` ; if [ $result -eq 0 ];then

sed -i 's/password sufficient pam_unix.so/& remember=5/g' /etc/pam.d/system-auth

echo "密码重复使用次数已设置"

else

echo "密码重复使用次数限制已存在"

fi

echo -e "\n"

echo "禁止系统账号交互式登录"

passwd -l adm

passwd -l daemon

passwd -l bin

passwd -l sys

passwd -l lp

passwd -l uucp

passwd -l nuucp

passwd -l smmsp

echo "禁止系统账号交互式登录已设置"

echo -e "\n"

echo "---------ssh登录前警告banner设置----------"

file="/etc/ssh_banner"

if [ ! -f "$file" ]; then

touch "$file"

chown bin:bin /etc/ssh_banner

chmod 644 /etc/ssh_banner

echo "Authorized only. All activity will be monitored and reported" >> $file

echo "ssh登录前警告banner已设置"

else

echo "ssh登录前警告banner设置已存在"

fi

echo -e "\n"

echo "---------禁用不必要的别名----------"

result=`cat /etc/aliases|grep ^# | grep root |wc -l` ; if [ $result -le 9 ];then

sed -i '/games/ s/^/#/g' /etc/aliases

sed -i '/ingres/ s/^/#/g' /etc/aliases

sed -i '/system/ s/^/#/g' /etc/aliases

sed -i '/toor/ s/^/#/g' /etc/aliases

sed -i '/uucp/ s/^/#/g' /etc/aliases

sed -i '/manager/ s/^/#/g' /etc/aliases

sed -i '/operator/ s/^/#/g' /etc/aliases

sed -i '/decode/ s/^/#/g' /etc/aliases

sed -i '/marc/ s/^/#/g' /etc/aliases

sed -i '/dumper/ s/^/#/g' /etc/aliases

echo "禁用不必要的别名已完成"

else

echo "禁用不必要的别名已存在"

fi

echo -e "\n"

echo "---------配置pacct工具----------"

file="/var/log/pacct"

if [ ! -f "$file" ]; then

yum install psacct

touch /var/log/pacct

accton /var/log/pacct

echo "配置pacct工具已完成"

else

echo "配置pacct工具已存在"

fi

echo "---------配置记录su命令使用情况----------"

result=`grep "authpriv.* /var/log/secure" /etc/rsyslog.conf|wc -l` ; if [ $result -eq 0 ];then

echo "authpriv.* /var/log/secure" >> /etc/rsyslog.conf

echo "配置记录su命令使用情况已完成"

else

echo "记录su命令使用情况已存在"

fi

相关推荐
一只栖枝2 小时前
网络安全 vs 信息安全的本质解析:数据盾牌与网络防线的辩证关系关系
网络·网络安全·信息安全·it·信息安全认证
安全系统学习2 小时前
【网络安全】Mysql注入中锁机制
安全·web安全·网络安全·渗透测试·xss
怦然星动_4 小时前
DHCP中继及动态分配
网络安全
Johny_Zhao6 小时前
Ubuntu系统安装部署Pandawiki智能知识库
linux·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm·pandawiki
游戏开发爱好者87 小时前
iOS App首次启动请求异常调试:一次冷启动链路抓包与初始化流程修复
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_915106327 小时前
频繁迭代下完成iOS App应用上架App Store:一次快速交付项目的完整回顾
websocket·网络协议·tcp/ip·http·网络安全·https·udp
薄荷椰果抹茶10 小时前
【网络安全基础】第一章---引言
安全·网络安全
00后程序员张13 小时前
免Mac上架实战:全平台iOS App上架流程的工具协作经验
websocket·网络协议·tcp/ip·http·网络安全·https·udp
unable code18 小时前
攻防世界-Reverse-insanity
网络安全·ctf·reverse
亚里士多没有德77518 小时前
【CTF-Web环境搭建】kali
网络安全