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

所属分类: 脚本专栏 / linux shell 阅读数: 617
收藏 0 赞 0 分享
复制代码 代码如下:

#!/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
更多精彩内容其他人还在看

linux Shell入门:掌握Linux,OS X,Unix的Shell环境

这篇文章主要介绍了linux Shell入门:掌握Linux,OS X,Unix的Shell环境 ,需要的朋友可以参考下
收藏 0 赞 0 分享

shell 1>&2 2>&1 &>filename重定向的含义和区别

这篇文章主要介绍了shell 1>&2 2>&1 &>filename重定向的含义和区别,需要的朋友可以参考下
收藏 0 赞 0 分享

ssh远程执行命令方法和Shell脚本实例

这篇文章主要介绍了ssh远程执行命令方法和Shell脚本实例,本文讲解了ssh执行远程操作方法和远程执行命令shell脚本示例,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell中的${}、##和%%使用范例

这篇文章主要介绍了Shell中的${}、##和%%使用范例,本文给出了不同情况下得到的结果,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本一次读取文件中一行的2种写法

这篇文章主要介绍了Shell脚本一次读取文件中一行的2种写法,本文还同时讲解了Shell读取文本文件的2种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本逐行读取文本文件(不改变文本格式)

这篇文章主要介绍了Shell脚本逐行读取文本文件,本文着重探讨不改变文本格式的方法读取出文件内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本配合iptables屏蔽来自某个国家的IP访问

这篇文章主要介绍了Shell脚本配合iptables屏蔽来自某个国家的IP访问,本文利用IPdeny的IP数据,然后用Shell脚本导入iptables实现屏蔽IP访问,需要的朋友可以参考下
收藏 0 赞 0 分享

Bash脚本内置的调试方法技巧

这篇文章主要介绍了Bash脚本内置的调试方法技巧,本文介绍了调试技巧和几个调试技巧,比如输出行号的方法、只调试某段程序的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell、Perl、Python、PHP访问 MySQL 数据库代码实例

这篇文章主要介绍了Shell、Perl、Python、PHP访问 MySQL 数据库代码实例,本文分别给出这几种语言访问Mysql数据的代码实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本计算字符串长度和判断字符串为空小技巧

这篇文章主要介绍了Shell脚本计算字符串长度和判断字符串为空小技巧,本文分别给出计算字符串长度和判断字符串为空各3种实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多