#!/bin/bash#减少swap使用,设置权重echo"0" > /proc/sys/vm/swappiness
# 安装相关工具、软件包,dos2unix可以将Windows文件格式转换linux/unix文件
yum -y install vim make wget gcc gcc-c++ net-tools zip sysstat iostat iftop iotp lrzsz dos2unix
# 设置时区并同步网络时间rm-f /etc/localtime
## 同步上海时区
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum -y install chrony &> /dev/null
sed -ri's/.*pool.ntp.org.*/#&/'/etc/chrony.conf
sed -ri'/.*centos.pool.ntp.org.*/a\server ntp1.aliyun.com iburst'/etc/chrony.conf
systemctl start chronyd && systemctl enable chronyd
# 禁用Selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g'/etc/selinux/config
# 关闭防火墙if uname -a | grep el[7-8] &> /dev/null;then
systemctl stop firewalld && systemctl disable firewalld
elif uname -a | grep el6 &> /dev/null;then
service iptables stop && chkconfig iptables off
fi
# history显示操作时间和操作用户if! grep HISTTIMEFORMAT ~/.bashrc;then
echo'export HISTTIMEFORMAT="%F %T `whoami`" ' >> ~/.bashrc
source ~/.bashrc
fi
# SSH超时时间设置,超时自动断开,且设置了用户不能随意更改,更改需要注释readonly TMOUT行if!(grep "export TMOUT"/etc/profile) &> /dev/null && !(grep "set autologout"/etc/csh.cshrc) &> /dev/null;then
echo"export TMOUT=300" >> /etc/profile && echo"readonly TMOUT" >> /etc/profile
source /etc/profile
readonly TMOUT
elseecho"已配置过超时时间"
fi
# 禁止root远程登录
sed -ri'0,/PermitRootLogin.*/s//PermitRootLogin no/'/etc/ssh/sshd_config
if grep [7-8]/etc/redhat-release;then
systemctl restart sshd
elif grep 6 /etc/redhat-release;then
/etc/init.d/sshd restart
fi
# 禁止定时任务发送邮件,定时任务执行后会在/var/mail下面自动生成文件,如果不设置,会占用磁盘空间
sed -i 's/^MAILTO=root/MAILTO=""/'/etc/crontab
# 设置最大打开文件数if! grep "* soft nofile 65535"/etc/security/limits.conf &>/dev/null; then
cat >> /etc/security/limits.conf << EOF
* soft nofile 65535
* hard nofile 65535
EOF
fi
# 系统内核优化cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_tw_buckets = 20480
net.ipv4.tcp_max_syn_backlog = 20480
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_fin_timeout = 20
EOF
4、CPU、memory 消耗 top10
#!/bin/bash#按照CPU使用率排序,会显示pid,cpu,memory,进程echo"------------cpu top 10-------------------"ps-eo pid,pcpu,pmem,args --sort=-pcpu | head
#按照内存使用率排序,会显示pid,cpu,memory,进程echo"------------memory top 10-------------------"ps-eo pid,pcpu,pmem,args --sort=-pmem | head