1、批量创建用户
#!/bin/bash
read -p "请输入要创建的用户名(空格隔开):" user_list
for user in $user_list;do
if ! id $user &> /dev/null;then
#给用户设置随机密码
pass=`echo $RANDOM | md5sum | cut -c 1-6`
useradd $user
echo $pass | passwd --stdin $user
echo "$user $pass" >> /opt/user_file
else
echo "$user user already exists!!!"
fi
done
2、资源检查
#!/bin/bash
cpu_check(){
cpu_util=`vmstat | awk 'NR==3 {print $13+$14"%"}'`
iowait=`vmstat | awk 'NR==3 {print $16"%"}'`
echo "---CPU使用情况---" && echo "cpu的使用率和等待磁盘IO响应使用率分别是: $cpu_util $iowait"
}
memory_check(){
memory_total=`free -h | awk 'NR==2 {print $2}'`
memory_used=`free -h | awk 'NR==2 {print $3}'`
memory_available=`free -h | awk 'NR==2 {print $NF}'`
echo "---内存使用情况---" && echo "内存总大小:$memory_total,已使用:$memory_used,剩余:$memory_available"
}
disk_check(){
Fs=`df -h | awk '/^\/dev/ {print $1}'`
echo "---磁盘使用情况---"
for i in $Fs;do
fs=`df -h | awk -v x=$i '$1==x {print $1}'`
mounted=`df -h | awk -v x=$i '$1==x {print $NF}'`
size=`df -h | awk -v x=$i '$1==x {print $2}'`
used=`df -h | awk -v x=$i '$1==x {print $3}'`
used_percent=`df -h | awk -v x=$i '$1==x {print $5}'`
echo "Filesystem $fs挂载点为: $mounted,总大小: $size,已使用: $used,使用率: $used_percent"
done
}
tcp_status(){
tcp_status=`ss -antp | awk 'NR!=1 {x[$1]++}END{for(i in x)print i":"x[i]}'`
echo "---TCP连接状态---" && echo "$tcp_status"
}
cpu_check
memory_check
disk_check
tcp_status
3、系统配置初始化
#!/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
else
echo "已配置过超时时间"
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
5、网卡流量监控
#!/bin/bash
#执行脚本时请指定要监控的网卡名称
eth=$1
echo "---------网卡进出流量监控中---------"
echo "--IN----------OUT--"
while true;do
#第一次取网卡进出的流量
IN_1=`cat /proc/net/dev | grep $eth | awk '{print $2}'`
OUT_1=`cat /proc/net/dev | grep $eth | awk '{print $10}'`
sleep 1
#第二次取网卡进出的流量
IN_2=`cat /proc/net/dev | grep $eth | awk '{print $2}'`
OUT_2=`cat /proc/net/dev | grep $eth | awk '{print $10}'`
#第二次减第一次网卡的差,不断循环,单位为 kb/s
IN=`printf "%.2f%s" "$((($IN_2-$IN_1)/1024))" "KB/s"`
OUT=`printf "%.2f%s" "$((($OUT_2-$OUT_1)/1024))" "KB/s"`
echo "$IN $OUT"
sleep 1
done
版权声明:本文为a913909626原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。