linux服务器安全加固shell脚本代码

 更新时间:2013年06月11日 15:05:34   作者:  
有时候安装完服务器以后,需要一些安全设置,这段脚本就是为了安全加固所写,需要的朋友可以参考下
复制代码 代码如下:

#!/bin/sh
# desc: setup linux system security
# author:coralzd
# powered by www.freebsdsystem.org
# version 0.1.2 written by 2011.05.03
#account setup

passwd -l xfs
passwd -l news
passwd -l nscd
passwd -l dbus
passwd -l vcsa
passwd -l games
passwd -l nobody
passwd -l avahi
passwd -l haldaemon
passwd -l gopher
passwd -l ftp
passwd -l mailnull
passwd -l pcap
passwd -l mail
passwd -l shutdown
passwd -l halt
passwd -l uucp
passwd -l operator
passwd -l sync
passwd -l adm
passwd -l lp

# chattr /etc/passwd /etc/shadow
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/group
chattr +i /etc/gshadow
# add continue input failure 3 ,passwd unlock time 5 minite
sed -i 's#auth        required      pam_env.so#auth        required      pam_env.so\nauth       required       pam_tally.so  onerr=fail deny=3 unlock_time=300\nauth           required     /lib/security/$ISA/pam_tally.so onerr=fail deny=3 unlock_time=300#' /etc/pam.d/system-auth
# system timeout 5 minite auto logout
echo "TMOUT=300" >>/etc/profile

# will system save history command list to 10
sed -i "s/HISTSIZE=1000/HISTSIZE=10/" /etc/profile

# enable /etc/profile go!
source /etc/profile

# add syncookie enable /etc/sysctl.conf
echo "net.ipv4.tcp_syncookies=1" >> /etc/sysctl.conf

sysctl -p # exec sysctl.conf enable
# optimizer sshd_config

sed -i "s/#MaxAuthTries 6/MaxAuthTries 6/" /etc/ssh/sshd_config
sed -i  "s/#UseDNS yes/UseDNS no/" /etc/ssh/sshd_config

# limit chmod important commands
chmod 700 /bin/ping
chmod 700 /usr/bin/finger
chmod 700 /usr/bin/who
chmod 700 /usr/bin/w
chmod 700 /usr/bin/locate
chmod 700 /usr/bin/whereis
chmod 700 /sbin/ifconfig
chmod 700 /usr/bin/pico
chmod 700 /bin/vi
chmod 700 /usr/bin/which
chmod 700 /usr/bin/gcc
chmod 700 /usr/bin/make
chmod 700 /bin/rpm

# history security

chattr +a /root/.bash_history
chattr +i /root/.bash_history

# write important command md5
cat > list << "EOF" &&
/bin/ping
/bin/finger
/usr/bin/who
/usr/bin/w
/usr/bin/locate
/usr/bin/whereis
/sbin/ifconfig
/bin/pico
/bin/vi
/usr/bin/vim
/usr/bin/which
/usr/bin/gcc
/usr/bin/make
/bin/rpm
EOF

for i in `cat list`
do
   if [ ! -x $i ];then
   echo "$i not found,no md5sum!"
  else
   md5sum $i >> /var/log/`hostname`.log
  fi
done
rm -f list

相关文章

  • shell中set指令的用法详解

    shell中set指令的用法详解

    这篇文章主要介绍了shell中set指令的用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Linux下的fdisk指令用法场景分析

    Linux下的fdisk指令用法场景分析

    在Linux系统中,对磁盘进行分区是管理和利用磁盘空间的重要任务之一,本篇将以通俗易懂的方式,详细介绍fdisk指令的前世今生、功能、用法和应用场景,帮助读者全面了解fdisk指令,并掌握其在磁盘管理和问题排查方面的应用,需要的朋友可以参考下
    2024-01-01
  • Shell踢人的三种方法

    Shell踢人的三种方法

    这篇文章主要介绍了Shell踢人的三种方法,首先用w查看当前登录用户,运行w命令的是当前用户,需要保留,其他一概踢掉,需要的朋友可以参考下
    2014-12-12
  • Linux中cp命令的使用详解

    Linux中cp命令的使用详解

    cp 命令是 Linux 中一个重要的命令,你可能经常会用到它,所以本文主要来和大家讲讲cp命令的具体使用,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-08-08
  • 利用shell脚本进行批量处理文件后缀名

    利用shell脚本进行批量处理文件后缀名

    这篇文章主要给大家介绍了关于如何使用shell脚本进行批量处理文件后缀名,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2023-12-12
  • Linux 中shell脚本设置开头固定格式的实现方法

    Linux 中shell脚本设置开头固定格式的实现方法

    这篇文章主要介绍了Linux 中shell脚本设置开头固定格式的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • 一个强大的网络分析shell脚本分享(实时流量、连接统计)

    一个强大的网络分析shell脚本分享(实时流量、连接统计)

    这篇文章主要介绍了一个强大的网络分析shell脚本分享,此脚本包含实时流量统计、连接统计强大功能,需要的朋友可以参考下
    2014-12-12
  • Linux禁止普通用户su至root的解决方法

    Linux禁止普通用户su至root的解决方法

    很多朋友不清楚linux如何禁止普通用户su到root,这里需要修改两个配置文件,具体详细配置大家通过本文了解下吧
    2017-08-08
  • Linux下Shell脚本中几种基本命令的替换区别

    Linux下Shell脚本中几种基本命令的替换区别

    最近因为工作需要,需要编写shell脚本。编写大量shell脚本时,累计了大量经验,也让自己开始迷糊几种函数输出调用的区别。后面和前辈们请教了一下这个问题,大致明白了区别。这里分享给大家,希望对大家能有所收获。下面来一起看看吧。
    2016-10-10
  • 利用kernel提供的接口打印进程号(pid)

    利用kernel提供的接口打印进程号(pid)

    我们知道linux是模块化的内核。实现模块、利用kernel提供的接口,首先了解写模块的基本框架。下面的c文件就是最基本的框架,当然还有怎样添加一些符号变量,利用的module_param,没用上
    2013-11-11

最新评论