【17 个实用 shell 脚本,建议收藏】1、服务器系统配置初始化
#/bin/bash# 安装系统性能分析工具及其他yum install gcc make autoconf vim sysstat net-tools IOStat iftop iotp wget lrzsz lsof unzip openssh-clients net-tool vim ntpdate -y# 设置时区并同步时间ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeif ! crontab -l |grep ntpdate &>/dev/null ; then(echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab fi # 禁用selinuxsed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config # 关闭防火墙if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; thensystemctl stop firewalldsystemctl disable firewalldelif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; thenservice iptables stopchkconfig iptables offfi # 历史命令显示操作时间if ! grep HISTTIMEFORMAT /etc/bashrc; thenecho 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S`whoami` "' >> /etc/bashrcfi # SSH超时时间if ! grep "TMOUT=600" /etc/profile &>/dev/null; thenecho "export TMOUT=600" >> /etc/profilefi # 禁止root远程登录 切记给系统添加普通用户 , 给su到root的权限sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config # 禁止定时任务向发送邮件sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab# 设置最大打开文件数if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; thencat >> /etc/security/limits.conf << EOF* soft nofile 65535* hard nofile 65535EOFfi # 系统内核优化cat >> /etc/sysctl.conf << EOFnet.ipv4.tcp_syncookies = 1net.ipv4.tcp_max_tw_buckets = 20480net.ipv4.tcp_max_syn_backlog = 20480net.core.netdev_max_backlog = 262144net.ipv4.tcp_fin_timeout = 20EOF # 减少SWAP使用echo "0" > /proc/sys/vm/swAppiness
2、批量创建多个用户并设置密码#!/bin/bashUSER_LIST=$@USER_FILE=./user.infofor USER in $USER_LIST;do if ! id $USER &>/dev/null; thenPASS=$(echo $RANDOM |md5sum |cut -c 1-8)useradd $USERecho $PASS | passwd --stdin $USER &>/dev/nullecho "$USER$PASS" >> $USER_FILEecho "$USER User create successful." elseecho "$USER User already exists!" fidone
3、一键查看服务器利用率#!/bin/bashfunction cpu(){util=$(vmstat | awk '{if(NR==3)print $13+$14}') iowait=$(vmstat | awk '{if(NR==3)print $16}') echo "CPU -使用率:${util}% ,等待磁盘IO相应使用率:${iowait}:${iowait}%" }function memory (){total=`free -m |awk '{if(NR==2)printf "%.1f",$2/1024}'`used=`free -m |awk '{if(NR==2) printf "%.1f",($2-$NF)/1024}'`available=`free -m |awk '{if(NR==2) printf "%.1f",$NF/1024}'`echo "内存 - 总大小: ${total}G , 使用: ${used}G , 剩余: ${available}G"}disk(){fs=$(df -h |awk '/^/dev/{print $1}')for p in $fs; domounted=$(df -h |awk '$1=="'$p'"{print $NF}')size=$(df -h |awk '$1=="'$p'"{print $2}')used=$(df -h |awk '$1=="'$p'"{print $3}')used_percent=$(df -h |awk '$1=="'$p'"{print $5}')echo "硬盘 - 挂载点: $mounted , 总大小: $size , 使用: $used , 使用率: $used_percent"done }function tcp_status() {summary=$(ss -antp |awk '{status[$1]++}END{for(i in status) printf i":"status[i]" "}')echo "TCP连接状态 - $summary"}cpumemorydisktcp_status
4、找出占用CPU 内存过高的进程#!/bin/bashecho "-------------------CUP占用前10排序--------------------------------"ps -eo user,pid,pcpu,pmem,args --sort=-pcpu|head -n 10echo "-------------------内存占用前10排序--------------------------------"ps -eo user,pid,pcpu,pmem,args --sort=-pmem|head -n 10
5、查看网卡的实时流量#!/bin/basheth0=$1echo-e"流量进入--流量传出"while true; do old_in=$(cat /proc/net/dev |grep $eth0 |awk '{print $2}') old_out=$(cat /proc/net/dev |grep $eth0 |awk '{print $10}') sleep 1 new_in=$(cat /proc/net/dev |grep $eth0 |awk '{print $2}') new_out=$(cat /proc/net/dev |grep $eth0 |awk '{print $10}') in=$(printf "%.1f%s" "$((($new_in-$old_in)/1024))" "KB/s") out=$(printf "%.1f%s" "$((($new_out-$old_out)/1024))" "KB/s") echo "$in $out"done
6、监控多台服务器磁盘利用率脚本#!/bin/bashHOST_INFO=host.infofor IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do #取出用户名和端口USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO) #创建临时文件 , 保存信息TMP_FILE=/tmp/disk.tmp #通过公钥登录获取主机磁盘信息ssh -p $PORT $USER@$IP 'df -h' > $TMP_FILE #分析磁盘占用空间USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^/dev/{print $NF,int($5)}' $TMP_FILE) #循环磁盘列表 , 进行判断for USE_RATE in $USE_RATE_LIST; do#取出等号(=)右边的值 挂载点名称PART_NAME=${USE_RATE%=*}#取出等号(=)左边的值磁盘利用率USE_RATE=${USE_RATE#*=}#进行判断if [ $USE_RATE -ge 80 ]; thenecho "Warning: $PART_NAME Partition usage $USE_RATE%!"echo "服务器$IP的磁盘空间占用过高 , 请及时处理" | mail -s "空间不足警告" 你的qq@qq.comelseecho "服务器$IP的$PART_NAME目录空间良好"fidonedone
推荐阅读
- 分享程序员可以接单的六个网站,副业刚需
- 当家里网络WiFi信号弱,延迟高,学会这几个小技巧,轻松告别烦恼
- 往复式剃须刀和旋转式剃须刀哪个好
- 英德红茶哪个牌子好,甘草与黑豆熬茶的功效和作用
- 电脑浏览器哪个好
- 你知道4种类型的生存打火机吗?选择户外打火机要注意这6个特性
- 专业级战术手电,照明防身只需这一个
- 只需这一个设置,让你网速飞起来
- 7个最好的Go语言Web框架
- 头条99天收入1万,新手必看的8个建议,可以让你快速实现经济独立