linux系统加固脚本

#########################################//以下是锁定系统不需要登录的账号
cp /etc/passwd /etc/passwd.`date +%F`
zhanghao="adm lp mail uucp operator games gopher ftp nobody nobody4 noaccess listen webservd rpm dbus avahi mailnull smmsp nscd vcsa rpc rpcuser nfs sshd pcap ntp haldaemon distcache apache webalizer squid xfs gdm sabayon named "
for zh in $zhanghao
do
passwd -l $zh
done
echo "Lock useless users.......................OK"
sleep 1
#################################################################
cp /etc/profile /etc/profiel.`date +%F`
echo "TMOUT=1800" >>/etc/profile  #设置30分钟无活动自动退出,可自行设置
echo "set autologout=30 >> /etc/csh.cshrc"
sleep 1
###############################
cp /etc/sysctl.conf /etc/sysctl.conf.`date +%F`
cat >> /etc/sysctl.conf << endf  #优化内核参数调整
net.ipv4.tcp_max_syn_backlog = 3000
net.ipv4.conf.lo.accept_source_route = 0
net.ipv6.conf.usb0.accept_redirects = 0
net.ipv6.conf.bond0.accept_redirects = 0
net.ipv6.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv6.conf.lo.accept_redirects = 0
net.ipv4.conf.usb0.accept_redirects = 0
net.ipv4.conf.bond0.accept_redirects = 0
net.ipv4.conf.lo.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.usb0.send_redirects = 0
net.ipv4.conf.bond0.send_redirects = 0
net.ipv4.conf.lo.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.usb0.log_martians = 1
net.ipv4.conf.bond0.log_martians = 1
net.ipv4.conf.lo.log_martians = 1
net.ipv4.conf.default.log_martians = 1
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.usb0.arp_filter = 1
net.ipv4.conf.bond0.arp_filter = 1
net.ipv4.conf.lo.arp_filter = 1
net.ipv4.conf.lo.rp_filter = 1
net.ipv4.conf.default.arp_filter = 1
net.ipv4.conf.all.arp_filter = 1
net.ipv4.conf.all.rp_filter = 1
endf
sysctl -p
echo "Adjust the kernel parameters!......................OK!"
sleep 1
#############################################
#关闭不必要的服务
SERVICES="amanda chargen chargen-udp cups cups-lpd daytime daytime-udp echo echo-udp eklogin ekrb5-telnet finger gssftp imap imaps ipop2 ipop3 klogin
krb5-telnet kshell ktalk ntalk rexec rlogin rsh rsync talk tcpmux-server telnet tftp time-dgram time-stream uucp nfslock"

for serv in $SERVICES
do
chkconfig --level 345 $serv off
done
echo "Close useless services.........................ok"
sleep 1
#################################################################
#口令策略
cp /etc/login.defs /etc/login.defs.`date +%F`
DIR=/etc
echo "正在修改/etc/login.defs..."
sleep 1
#检查用户口令最长有效时间
max=`cat $DIR/login.defs |grep ^PASS_MAX_DAYS |awk ‘{print $2}‘`
if [ $max != 90 ];then
    sed -i ‘/^PASS_MAX_DAYS/s/‘"$max"‘/90/g‘ $DIR/login.defs
fi

##PASS_MIN_DAYS 检查用户口令最短有效时间
min=`cat $DIR/login.defs |grep ^PASS_MIN_DAYS |awk ‘{print $2}‘`
if [ $min != 30 ];then
    sed -i ‘/^PASS_MIN_DAYS/s/‘"$min"‘/30/g‘ $DIR/login.defs
fi

##PASS_MIN_LEN 检查用户口令最短长度
len=`cat $DIR/login.defs |grep ^PASS_MIN_LEN |awk ‘{print $2}‘`
if [ $len != 8 ];then
    sed -i ‘/^PASS_MIN_LEN/s/‘"$len"‘/8/g‘ $DIR/login.defs
fi

##PASS_WARN_AGE
warn=`cat $DIR/login.defs |grep ^PASS_WARN_AGE | awk ‘{print $2}‘`
if [ $warn != 30 ];then
    sed -i ‘/^PASS_WARN_AGE/s/‘"$warn"‘/30/g‘ $DIR/login.defs
fi
##口令策略
cp /etc/pam.d/system-auth /etc/pam.d/system-auth.`date +%F`
PASSREQU=$(cat /etc/pam.d/system-auth |grep password |grep requisite)
NEWPASSREQU=‘password    requisite     pam_cracklib.so dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1 minclass=2 minlen=6‘
sed -i ‘s/‘"$PASSREQU"‘/‘"$NEWPASSREQU"‘/g‘ /etc/pam.d/system-auth
PASSSUFF=‘password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok‘
NEWPASSSUFF=‘password    sufficient    pam_unix.so md5 shadow nullok try_first_pass use_authtok remember=5‘
sed -i ‘s/‘"$PASSSUFF"‘/‘"$NEWPASSSUFF"‘/g‘ /etc/pam.d/system-auth
AUTH=‘auth        required      pam_env.so‘
NEXTAUTH=‘auth        required      pam_tally2.so deny=6 onerr=fail no_magic_root unlock_time=120‘
sed -i ‘/‘"$AUTH"‘/a\‘"$NEXTAUTH"‘‘ /etc/pam.d/system-auth

###############################################
sleep 1
echo "正在修改禁止管理员远程登录..."
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +%F`
sed -i ‘s/^#PermitRootLogin yes/PermitRootLogin no/g‘ /etc/ssh/sshd_config
###############################################
#####检查关键敏感文件的权限是否恰当
chmod 400 /etc/shadow
chmod 644 /etc/group
chmod 644 /etc/group
chmod 600 /etc/security
chmod 600 /etc/security
chmod 750 /etc/rc6.d
chmod 750 /etc/rc0.d/
chmod 750 /etc/rc1.d/
chmod 750 /etc/
chmod 750 /etc/rc4.d
chmod 750 /etc/rc5.d/
chmod 750 /etc/rc3.d
chmod 750 /etc/rc.d/init.d/
#帐号与口令-检查是否存在除root之外UID为0的用户
#echo "#检查系统中是否存在其它id为0的用户"
echo "Check if the system have other user‘s id is 0"
echo "#-------------------------------------"
mesg=`awk -F: ‘($3 == 0) { print $1 }‘ /etc/passwd|grep -v root`
if [ -z $mesg ]
then
echo "There don‘t have other user uid=0"
else
echo
echo "$mesg uid=0"
fi
#禁止用户使用ctlraltdel组合键
sed -i ‘s/^start on control-alt-delete/#start on control-alt-delete/g‘ /etc/init/control-alt-delete.conf
#检查ssh协议设置,禁止使用不安全的ssh协议1,只使用协议2
sed -i ‘s/#Protocol 2/Protocol 2/g‘ /etc/ssh/sshd_config
#设置ssh警告Banner
touch /etc/sshbanner
chown bin:bin /etc/sshbanner
chmod 644 /etc/sshbanner
echo " Authorized users only. All activity may be monitored and reported "   >/etc/sshbanner
echo "Banner /etc/sshbanner" >>/etc/ssh/sshd_config
#设置登录成功后警告Banner
echo " Authorized users only. All activity may be monitored and reported " > /etc/motd
##################################################################################
#配置远程日志保存
cp /etc/syslog.conf /etc/syslog.conf.`date +%F`
echo "*.*       @192.168.0.1" >>/etc/syslog.conf
#记录帐户登录日志
touch /var/log/authlog
echo "auht.info       /var/log/authlog" >>/etc/syslog.conf
#存在类似*.err;kern.debug;daemon.notice;       /var/log/messages
echo "*.err;auth.info        /var/adm/messages" >>/etc/syslog.conf
#存在authpriv.info              /var/log/authlog配置
echo "uthpriv.*   /var/log/authlog" >>/etc/syslog.conf
#####################################################################
########################################################
#只允许wheel组使用su
#sed -i ‘N;2iauth sufficient pam_rootok.so‘ /etc/pam.d/su
#sed -i ‘N;2iauth required pam_wheel.so ‘ /etc/pam.d/su
#################################################################
#echo "umask 027" >>/etc/login.defs
#锁定禁止账号交互式登录:修改/etc/shadow文件,用户名后密码列为两个感叹号“!!”;
#sed -ri ‘/mail|lp/[email protected]([[:lower:]]):.*:(1)@\1:!!:\[email protected]‘ /etc/shadow
#chattr +i /etc/passwd
#chattr +i /etc/shadow
#chattr +i /etc/group
#chattr +i /etc/gshadow

时间: 2024-12-12 00:14:15

linux系统加固脚本的相关文章

Linux系统shell脚本编程——生产实战案例

Linux系统shell脚本编程--生产实战案例     在日常的生产环境中,可能会遇到需要批量检查内网目前在线的主机IP地址有哪些,还可能需要检查这些在线的主机哪些端口是开放状态,因此依靠手工来检查是可以实现,但比较费时费力,所以需要结合shell脚本来实现批量检查的功能,那么今天就来做个小小的实验. 1.开发脚本前准备 一般大家都知道,测试主机是否在线,常用的命令无非就是ping.nmap,因此,首先找一个地址来测试下ping命令的效果 [[email protected] scripts]

【安全运维】linux系统加固(第二部分),经测试可行

前言 接续系统加固第一部分未完的内容,第二部分主要涉及关闭多余的服务,用户账户的安全策略,以及内核网络参数的优化等等. <修改系统默认的账户密码策略> 这是通过编辑  /etc/login.defs  文件相关内容实现的.和前面一样的道理,修改前需要备份,然后把"鸡蛋"放在多个"篮子"里: [[email protected] 桌面]# cp -p  /etc/login.defs /etc/login.defs_backup 使用 vim 文本编辑器编

一键linux系统初始化脚本

一.前言一般我们在安装新的系统时,系统的一些默认配置对我们来说是不行的,所以我们要自定义初始化系统. 二.需求1)设置时区并把同步时间加入计划任务2)禁用selinux3)清空原防火墙默认策略只保留ssh4)历史命令显示操作时间级用户5)创建ALL权限用户并禁止root远程登录6)禁止定时任务发送邮件7)设置最大打开文件数8)减少swap使用9)系统内核参数优化10)安装系统性能分析工具及其他常常用工具注意:安装新系统最好最小化安装. 三.脚本正文#!/bin/bash #设置时区并同步时间ln

Linux系统Shell脚本编程

1. shell脚本概念:C语言编写的.命令解释器.编程语言. 是用户使用linux的桥梁. shell脚本语言非常擅长处理文本类型的数据. 2. shell脚本作用:自动化管理.监控管理.日志数据处理.自动数据备份. 3. shell脚本中的成分:注释.命令.shell变量.结构控制语句. 以行为单位  一行一行依次执行. (在shell脚本中可以出现任何在交互方式下可以使用的命令.) 4. 调用shell脚本的两种方式: (1)sh 脚本文件名 (2)./脚本文件名(需要有执行权限) 当执行

Linux服务器企业集群架构部署搭建(二)----linux系统基础脚本优化、内核优化

第四章 linux系统内核优化相关 参考文章: linux内核TCP相关参数解释 http://os.chinaunix.net/a2008/0918/985/000000985483.shtml linux内核参数优化 http://blog.chinaunix.net/uid-29081804-id-3830203.html linux内核调整和内核参数详解 http://blog.csdn.net/cnbird2008/article/details/4419354 linux运维老男孩培

Linux系统检查脚本

一.背景 对登录一个系统,快速查看其系统信息,检查系统各项指标及参数,编写系统快速检查脚本,输出系统信息到脚本运行的logs目录下. 二.脚本 git地址 #!/bin/bash # auth:kaliarch # func:sys info check # version:v1.0 # sys:centos6.x/7.x [ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1 sysversion=$

Linux系统——shell脚本

shell脚本编程 作用:通过命令行解析的方式,自动执行设定好的程序或命令代码.(若将脚本挂到定时任务中,就会自动在非工作时间里自动触发执行程序) Shell脚本文件以".sh"结尾 规范的Shell脚本第一行会指出由哪个程序(解释器)来执行脚本中的内容.在linux bash编程中一般为:#!/bin/bash (表示该脚本运用/bin/bash命令进行解析) Shell的输出用echo命令: Python的输出用print命令 执行脚本的方式: 方法一:/bin/sh是bash的软

Linux系统监控脚本实战

1.局域网网段内主机是否存活脚本ping.sh#!/bin/bash#ping and test hosts of lans is alive!for i in {1..254}dohost=10.57.36.$iping -c3 $host >& /dev/nullif [ $? = 0 ];then   echo "$host is alive!" >> /tmp/hosts.logelse   echo "$host is down!"

linux系统巡检脚本shell实例

#!/bin/sh BACKUP_TIMESTAMP=`date +%Y%m%d` HOSTNAME=`hostname` num=89 ###################核查文件系统opt################ MemUseOpt=`/bin/df -kP | grep /opt | awk -F ' ' '{print $5}' | awk -F '%' ' {print $1}' ` if [ $MemUseOpt -gt $num ] then echo ${BACKUP_