小工具知识网

当前位置: 首页 > 行业知识 > Linux 主机一键安全整改策略

行业知识

Linux 主机一键安全整改策略

2026-10-26 01:18:43 来源:互联网转载

为防止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

上一篇:直播服务器怎么样设置?

下一篇:服务器管理员怎么查询?