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程序退出状态码的命令详解

    Shell程序退出状态码的命令详解

    在本篇文章当中主要给大家介绍一个shell的小知识——状态码。这是当我们的程序退出的时候,子进程会将自己程序的退出码传递给父进程,有时候我们可以利用这一操作做一些进程退出之后的事情,比如当程序执行失败或者被某个信号杀死我们就可以了解到,然后做出对应的措施
    2022-10-10
  • Linux下的自动化构建工具之make/makefile的用法详解

    Linux下的自动化构建工具之make/makefile的用法详解

    这篇文章主要为大家详细介绍了Linux下的自动化构建工具之make/makefile的区别与使用,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-10-10
  • PowerShell实现简单的grep功能

    PowerShell实现简单的grep功能

    下面的PS脚本针对目录和文件进行了区分,借用Select-String命令,实现了内容查找,并显示查找到的文件和匹配内容所在行号。感兴趣的朋友一起看看吧
    2017-10-10
  • Shell脚本实用的六个技巧示例

    Shell脚本实用的六个技巧示例

    本文介绍Shell脚本实用的六个技巧示例:使用Shell脚本实现自动化备份、使用Shell脚本实现定时任务、使用Shell脚本实现远程登录服务器、使用Shell脚本自动化部署应用、使用Shell脚本实现快速部署开发环境、使用Shell脚本实现快速检查服务器性能
    2023-11-11
  • Window11 WSL2 添加静态IP的脚本

    Window11 WSL2 添加静态IP的脚本

    这篇文章主要介绍了Window11 WSL2 添加静态IP的脚本方法,通过一段脚本给 Windows 端的"vEthernet (WSL)"加了IPv4 172.18.16.1,给 Linux 端的 eth0 添加 IPv4 172.18.16.1,在使用前,请按需调整网络接口的名称和IP地址,需要的朋友可以参考下
    2023-01-01
  • Linux中SELinux、Shell简介、touch命令的应用小结

    Linux中SELinux、Shell简介、touch命令的应用小结

    SELinux(Security-Enhanced Linux)是美国国家安全局(NSA)对于强制访问控制的实现,是Linux历史上最杰出的新安全子系统,这篇文章主要介绍了Linux中SELinux、Shell简介、touch命令的应用知识总结,需要的朋友可以参考下
    2023-02-02
  • 在linux上定期执行命令、脚本(cron,crontab,anacron)

    在linux上定期执行命令、脚本(cron,crontab,anacron)

    在linux下,如果想要在未来的某个时刻执行某个任务,并且在未来的每一个这样的时刻里都要执行这个任务。这篇文章主要介绍了在linux上定期执行命令、脚本(cron,crontab,anacron)的相关知识,需要的朋友可以参考下
    2018-07-07
  • shell for循环与数组应用介绍

    shell for循环与数组应用介绍

    先举一个例子,shell for循环读取一个文件,把每一行赋值给一个数组的变量,然后介绍shell数组的相关知识,有需要的朋友可以参考下
    2013-02-02
  • linux shell 编程之函数使用详解

    linux shell 编程之函数使用详解

    使用linux的shell编程,可以说函数是非常重要的内容,也是在编写各类shell脚本的时候经常用到的,这篇文章主要介绍了linux shell 编程之函数使用,需要的朋友可以参考下
    2022-10-10
  • Shell编程指南

    Shell编程指南

    这里汇总了一下关于shell编程的相关知识点,由简入深,非常的不错,这里推荐给大家
    2014-12-12

最新评论