Linux安全脚本

本文涉及的产品
运维安全中心(堡垒机),企业双擎版 50资产 7天
运维安全中心(堡垒机),免费版 6个月
简介: #!/bin/bash#set envexport PATH=$PATH:/bin:/sbin:/usr/sbinif [ "$UID" != "0" ]thenecho "please run this script by root.

#!/bin/bash

#set env

export PATH=$PATH:/bin:/sbin:/usr/sbin

if [ "$UID" != "0" ]

then

echo "please run this script by root."

exit 1

fi


#define cmd var

SERVICE=`which service`

CHKCONFIG=`which chkconfig`


yum install -y wget;

#修改yum源

function mod_yum(){

if [ -e /etc/yum.repos.d/CentOS-Base.repo ]

 then

  mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup&&\

  wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

fi

}


#关闭selinux

function close_selinux(){

sed -i 's/SELINUX=enforcing/SELINUX=disabled' /etc/selinux/config

setenforce 0 &>/dev/null

}


#关闭防火墙

function close_iptables(){

/etc/init.d/iptables stop

/etc/init.d/iptables stop

chkconfig iptables off

service firewalld stop

chkconfig firewalld off

}


#关闭不必要的开机启动服务

function lease_service(){

chkconfig | awk '{print "chkconfig",$1,"off"}'|bash

chkconfig | egrep "crond|sshd|network|rsyslog|sysstat"|awk '{print "chkconfig",$1,"on"}'|bash

}


#添加用户

function adduser(){

    #4.add hat and sudo

    if [ `grep -w hat /etc/passwd|wc -l` -lt 1 ]

      then

        useradd hat -g root

        echo geeboo|passwd --stdin hat

        \cp /etc/sudoers /etc/sudoers.ori

        echo "hat ALL=(ALL) NOPASSWD: ALL " >>/etc/sudoers

        tail -1 /etc/sudoers

        visudo -c &>/dev/null

    fi

}


#设置默认字符集为中文

function charset(){

    #5.charset config

    cp /etc/sysconfig/i18n /etc/sysconfig/i18n.ori

    echo 'LANG="zh_CN.UTF-8"'  >/etc/sysconfig/i18n

    source /etc/sysconfig/i18n

    #echo $LANG

}


#时间同步

function time_sync(){

    #6.time sync.

    cron=/var/spool/cron/root

    if [ `grep -w "ntpdate" $cron|wc -l` -lt 1  ]

      then

        echo "#time sync" >>$cron

        echo "*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1" >>$cron

        crontab -l

    fi

}


function com_line_set(){

    #7.command set.

    if [ `egrep "TMOUT|HISTSIZE|HISTFILESIZE" /etc/profile|wc -l` -ge 3  ]

      then

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

        echo "export HISTSIZE=5" >>/etc/profile

        echo "export HISTFILESIZE=5" >>/etc/profile

        . /etc/profile

    fi

    sed -i '/PASS_MAX_DAYS.*/d' /etc/login.defs

    sed -i '/PASS_MIN_LEN.*/d' /etc/login.defs

    sed -i '/PASS_MIN_DAYS.*/d' /etc/login.defs

    sed -i '/PASS_WARN_AGE.*/d' /etc/login.defs

    sed -i '/UMASK.*/d'  /etc/login.defs

    echo "PASS_MAX_DAYS   90" >>/etc/login.defs

    echo "PASS_MIN_LEN    12" >>/etc/login.defs

    echo "PASS_MIN_DAYS   7" >>/etc/login.defs

    echo "PASS_WARN_AGE 30" >>/etc/login.defs

    echo "UMASK 077" >>/etc/login.defs

    sed -i '/minlen.*/d' /etc/security/pwquality.conf

    sed -i '/minclass.*/d' /etc/security/pwquality.conf

    sed -i '/maxrepeat.*/d' /etc/security/pwquality.conf

    sed -i '/maxclassrepeat.*/d' /etc/security/pwquality.conf

    sed -i '/lcredit.*/d' /etc/security/pwquality.conf

    sed -i '/ucredit.*/d' /etc/security/pwquality.conf

    sed -i '/dcredit.*/d' /etc/security/pwquality.conf

    sed -i '/ocredit.*/d' /etc/security/pwquality.conf

    sed -i '/difok.*/d' /etc/security/pwquality.conf

    echo "minlen = 8">>/etc/security/pwquality.conf

    echo "minclass = 1">>/etc/security/pwquality.conf

    echo "maxrepeat = 0">>/etc/security/pwquality.conf

    echo "maxclassrepeat = 4">>/etc/security/pwquality.conf

    echo "lcredit = -1">>/etc/security/pwquality.conf

    echo "ucredit = -1">>/etc/security/pwquality.conf

    echo "dcredit = -1">>/etc/security/pwquality.conf

    echo "ocredit = -1">>/etc/security/pwquality.conf

    echo "difok = 5">>/etc/security/pwquality.conf

}


#设置打开文件数

function open_file_set(){

    #8.increase open file.

    if [ `grep 65535 /etc/security/limits.conf|wc -l` -lt 1 ]

      then

        echo "*               -       nofile          65535 " >>/etc/security/limits.conf

        tail -1 /etc/security/limits.conf

    fi

}


function set_kernel(){

    #9.kernel set.

    if [ `grep kernel_flag /etc/sysctl.conf|wc -l` -lt 1 ]

      then

        cat >>/etc/sysctl.conf<

        #kernel_flag

        net.ipv4.tcp_fin_timeout = 2

        net.ipv4.tcp_tw_reuse = 1

        net.ipv4.tcp_tw_recycle = 1

        net.ipv4.tcp_syncookies = 1

        net.ipv4.tcp_keepalive_time = 600

        net.ipv4.ip_local_port_range = 4000    65000

        net.ipv4.tcp_max_syn_backlog = 16384

        net.ipv4.tcp_max_tw_buckets = 36000

        net.ipv4.route.gc_timeout = 100

        net.ipv4.tcp_syn_retries = 1

        net.ipv4.tcp_synack_retries = 1

        net.core.somaxconn = 16384

        net.core.netdev_max_backlog = 16384

        net.ipv4.tcp_max_orphans = 16384

        net.nf_conntrack_max = 25000000

        net.netfilter.nf_conntrack_max = 25000000

        net.netfilter.nf_conntrack_tcp_timeout_established = 180

        net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120

        net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60

        net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120

EOF

        sysctl -p

    fi

}


#优化SSH

function init_ssh(){

    cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +"%Y-%m-%d_%H-%M-%S"`;

    sed -i "s%#Port 22%Port 52113%" /etc/ssh/sshd_config;

    #sed -i "s%#PermitRootLogin yes%PermitRootLogin no%" /etc/ssh/sshd_config;

    sed -i "s%#PermitEmptyPasswords no%PermitEmptyPasswords no%" /etc/ssh/sshd_config;

    sed -i "s%#UseDNS yes%UseDNS no%" /etc/ssh/sshd_config;

    sed -i "s%GSSAPIAuthentication yes%GSSAPIAuthentication no%" /etc/ssh/sshd_config;

    sed -i "s%GSSAPIAuthentication yes%GSSAPIAuthentication no%" /etc/ssh/sshd_config;

   #sed -i "$a\AllowUsers  hat" /etc/ssh/sshd_config;

   service sshd restart &>/dev/null;

   echo "sshd:192.168.10.0/24" >> /etc/hosts.allow;

   echo "sshd:ALL" >> /etc/hosts.deny;

   iptables -I INPUT -p tcp --dport 52113 -j DROP;

   iptables -I INPUT -p tcp --dport 52113 -s 192.168.10.0/24 -j ACCEPT;

   iptables save;

}


function update_linux(){

    #10.upgrade linux.

    if [ `rpm -qa lrzsz nmap tree dos2unix nc|wc -l` -le 3 ]

      then

        yum install wget lrzsz nmap tree dos2unix nc -y

        #yum update -y

    fi

}

function cha {

chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab

}

function pamtally {

cp /etc/pam.d/sshd /etc/pam.d/sshd.bak

cp /etc/pam.d/login /etc/pam.d/login.bak

##查看所有登陆用户的失败次数pam_tally2

##解锁指定用户pam_tally2 -r -u root

sed -i '/pam_tally2.so.*/d' /etc/pam.d/system-auth

sed -i -e '4a\auth        required        pam_tally2.so  onerr=fail  deny=5  unlock_time=600 even_deny_root root_unlock_time=300' /etc/pam.d/system-auth

sed -i '/pam_tally2.so.*/d' /etc/pam.d/sshd

sed -i -e '2a\auth        required        pam_tally2.so  onerr=fail  deny=5  unlock_time=600 even_deny_root root_unlock_time=300' /etc/pam.d/sshd

}

main(){

cat << EOF

----------------------------------------

|****Please Enter Your Choice:[1-15]****|

----------------------------------------

(1) mod yum 

(2) close selinux

(3) close iptables

(4) least service

(5) add user

(6) charset

(7) time sync

(8) password security

(9) set open file

(10) set kernel

(11) init ssh

(12) update linux

(13) pamtally

(14) chattr

(15) all(except init ssh) 

EOF

read -p "Please enter your choice[1-15]: " input1

case $input1 in

1)

    mod_yum

;;

2)

    close_selinux

;;

3)

    close_iptables

;;

4)

    least_service

;;

5)

    adduser

;;

6)

    charset

;;

7)

    time_sync

;;

8)

    com_line_set

;;

9)

    open_file_set

;;

10)

    set_kernel

;;

11)

    init_ssh

;;

12)

    update_linux

;;

13)

    pamtally

;;

14)

    cha

;;

15)

    mod_yum

     close_selinux

    close_iptables

    least_service

    adduser

    charset

    time_sync

    com_line_set

    open_file_set

    set_kernel

    init_ssh

    update_linux

    pamtally

    cha

;;

*)

   echo "****Please Enter Your Choice:[1-15]****|"

;;

esac

#隐藏系统版本号

#> /etc/issue

#> /etc/issue.net

#锁定关系系统文件

#chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab

#解锁 chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab

#修改chattr的名字 mv /usr/bin/chattr /usr/bin/hat1

#为grub加密

#/sbin/grub-md5-crypt,生成密码,然后将密码加入/etc/grub.conf,password --md5 #密码

#禁止被ping net.ipv4.icmp_echo_ignore_all=1


}

main

目录
相关文章
|
2月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
412 9
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
288 0
Linux系统初始化脚本
|
2月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
298 2
|
2月前
|
安全 Linux iOS开发
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
179 0
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
3月前
|
安全 Linux C++
PVS‑Studio 7.38 for macOS, Linux & Windows - 代码质量安全静态分析
PVS‑Studio 7.38 for macOS, Linux & Windows - 代码质量安全静态分析
177 0
PVS‑Studio 7.38 for macOS, Linux & Windows - 代码质量安全静态分析
|
4月前
|
安全 应用服务中间件 网络安全
在Linux环境部署Flask应用并启用SSL/TLS安全协议
至此,你的Flask应用应该能够通过安全的HTTPS协议提供服务了。记得定期更新SSL证书,Certbot可以帮你自动更新证书。可以设定cronjob以实现这一点。
330 10
|
4月前
|
安全 Linux iOS开发
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
491 0
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
4月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
536 1
|
6月前
|
Java Linux
自定义linux脚本用于快速jar包启动、停止、重启
自定义linux脚本用于快速jar包启动、停止、重启
323 29
|
6月前
|
安全 Linux iOS开发
PVS‑Studio 7.36 for macOS, Linux & Windows - 代码质量安全静态分析
PVS‑Studio 7.36 for macOS, Linux & Windows - 代码质量安全静态分析
143 1
PVS‑Studio 7.36 for macOS, Linux & Windows - 代码质量安全静态分析
下一篇
oss云网关配置