首页 > 代码库 > 根据当前机器配置实现内核参数初始化脚本

根据当前机器配置实现内核参数初始化脚本

#对一台新机器根据当前机器配置实现内核参数的初始化
#将脚本加入计划任务后,若机器配置出现更改可根据配置的更改修改内核参数
#!/bin/bash 
Mem=$(free | grep Mem | awk ‘{print $2}‘) #获取系统当前内存大小 
parameter=$(expr $Mem / 64) #计算内核参数配置 
#echo $Mem $parameter 
conf=(net.ipv4.tcp_tw_recycle net.ipv4.tcp_tw_reuse net.ipv4.tcp_syncookies net.ipv4.tcp_syncookies net.ipv4.tcp_synack_retries net.ipv4.tcp_syn_retries net.ipv4.tcp_fin_timeout) 
for i in ${conf[@]} 
do
        determine=$(cat /etc/sysctl.conf | grep $i) 
        if [ -z "$determine" ]; then
                echo "$i = 1" >> /etc/sysctl.conf 
        fi        
done
determine=$(cat /etc/sysctl.conf | grep net.ipv4.tcp_max_tw_buckets) 
if [  -z "$determine" ]; then
        echo "net.ipv4.tcp_max_tw_buckets = 6000" >> /etc/sysctl.conf 
fi
determine=$(cat /etc/sysctl.conf | grep net.ipv4.tcp_keepalive_time) 
if [  -z "$determine" ]; then
        echo "net.ipv4.tcp_keepalive_time = 30" >> /etc/sysctl.conf 
fi
Mem_conf=(net.ipv4.tcp_max_orphans net.ipv4.tcp_max_syn_backlog net.core.somaxconn net.core.netdev_max_backlog) 
for mem_conf in ${Mem_conf[@]} 
do
        determine=$(cat /etc/sysctl.conf | grep $mem_conf) 
        if [ -z "$determine" ]; then
                echo "$mem_conf = $parameter" >> /etc/sysctl.conf 
        else
                sed -i ‘s/‘‘‘$mem_conf‘‘‘ = [0-9]*/‘‘‘$mem_conf‘‘‘ = ‘‘‘$parameter‘‘‘/‘ /etc/sysctl.conf 
        fi
done
sysctl -p 
determine=$(cat /etc/security/limits.d/90-nproc.conf | grep "65535") 
if [  -z "$determine" ]; then
        echo "*               hard    nproc           65535" >> /etc/security/limits.d/90-nproc.conf 
fi
determine=$(cat /etc/security/limits.conf | grep "*               hard    nofile          65535") 
if [  -z "$determine" ]; then
        sed -i ‘/End of file/i\*               hard    nofile          65535‘ /etc/security/limits.conf 
fi
determine=$(cat /etc/security/limits.conf | grep "*               soft    nofile          65535") 
if [  -z "$determine" ]; then
        sed -i ‘/End of file/i\*               soft    nofile          65535‘ /etc/security/limits.conf 
fi
determine=$(cat /etc/security/limits.conf | grep "*               hard    nproc          65535") 
if [  -z "$determine" ]; then
        sed -i ‘/End of file/i\*               hard    nproc          65535‘ /etc/security/limits.conf 
fi
determine=$(cat /etc/security/limits.conf | grep "*               soft    nproc          65535") 
if [  -z "$determine" ]; then
        sed -i ‘/End of file/i\*               soft    nproc          65535‘ /etc/security/limits.conf 
fi
determine=$(cat /etc/profile | grep "export HISTFILESIZE=100000") 
if [ -z "$determine" ]; then
        echo "export HISTFILESIZE=100000" >> /etc/profile
fi
determine=$(cat /etc/profile | grep "export HISTTIMEFORMAT=\"%Y-%m-%d %H:%M:%S \"") 
if [ -z "$determine" ]; then
        echo "export HISTTIMEFORMAT=\"%Y-%m-%d %H:%M:%S \"" >> /etc/profile
fi
source /etc/profile

本文出自 “10946218” 博客,谢绝转载!

根据当前机器配置实现内核参数初始化脚本