linux内核高级优化脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux内核高级优化脚本相关的知识,希望对你有一定的参考价值。

linux内核高级优化脚本
可以用于hadoop生态圈环境的组件安装 linux内核调优
具体的每一步的解释请参考 03搭建cdh
备注:使用与centos6和centos7两个系统版本执行

[[email protected] scripts]# pwd
/opt/scripts
[[email protected] scripts]# ll
total 12
-rw-r--r-- 1 root root 3837 Apr 22 14:34 adlinuxopt.sh
-rw-r--r-- 1 root root 7215 Apr 22 13:32 initialization.sh
[[email protected] scripts]# cat adlinuxopt.sh 
#!/bin/bash
#adlinuxopt scripts
#author majihui 
#date 20190422
#by Advanced linux optimization for hadoop

echo "在执行此脚本之前需要先执行 initialization.sh 等待5s确认"
echo "若没执行请 ctrl+c 退出执行"
sleep 1
echo "1S"
sleep 2
echo "2S"
sleep 3
echo "3S"
sleep 4
echo "4S"
sleep 5
echo "5S"

if [ ! -d "/opt/scripts" ]; then
 mkdir -p /opt/scripts
fi

echo "等待1s:#1/判断centos7还是centos6系统"
sleep 1
VERSION=`cat /etc/redhat-release|awk -F " " ‘{print $3}‘|awk -F "." ‘{print $1}‘`
if [ "$VERSION" == "6" ];then
VERSION=‘6‘
echo "centos6"
else
VERSION=‘7‘
echo "centos7"
fi

echo "-----------------------------华丽分隔符----------------------------------------"
echo "#2/调整Linux磁盘缓存和性能"
if [ "`egrep "vm.dirty_background_ratio = 5" /etc/sysctl.conf|wc -l`" == "0" ];then
        echo "vm.dirty_background_ratio = 5" >> /etc/sysctl.conf
else
        echo "Linux磁盘缓存和性能 vm.dirty_background_ratio 设置成功或者之前已经设置过了"
fi

if [ "`egrep "vm.dirty_ratio = 10" /etc/sysctl.conf|wc -l`" == "0" ];then
        echo "vm.dirty_ratio = 10" >> /etc/sysctl.conf
        sysctl -p
else
        echo "Linux磁盘缓存和性能 vm.dirty_ratio设置成功或者之前已经设置过了"
fi

echo "-----------------------------华丽分隔符----------------------------------------"
echo "#3/是否禁用ipv6"
echo "等待3秒:"
sleep 3
cat << EOF
        **********************
        1.[disable ipv6]
        2.[no disable ipv6]
        3.[exit]
    pls input the num you want:
        **********************
EOF
read -t 30 -p "pls input the num you want:" a 
[ -n "`echo $a|sed ‘s#[0-9]##g‘`" ] && {
         echo "Input error"
        exit 1
}
iffuncation(){
if [ $a -eq 1 ];then
        echo "disable ipv6"
        echo "等待3S"
        sleep 3
        if [ "`egrep "net.ipv6.conf.all.disable_ipv6=1" /etc/sysctl.conf|wc -l`" == "0" ];then
                echo "net.ipv6.conf.all.disable_ipv6=1" >> /etc/sysctl.conf
        fi
        if [ "`egrep "NETWORKING_IPV6=no" /etc/sysconfig/network|wc -l`" == "0" ];then
                echo "NETWORKING_IPV6=no" >> /etc/sysconfig/network
        fi
        pathnetwork=`ls /etc/sysconfig/network-scripts/ifcfg-e*`
        echo NETWORKING_IPV6=no >> $pathnetwork
        sysctl -p
        echo "等待3S"
        sleep 3
elif [ $a -eq 2 ];then
        echo "no disable ipv6"
elif [ $a -eq 3 ];then
        exit 1
else
        echo "Input error"
        exit 1
fi
}
iffuncation

echo "-----------------------------华丽分隔符----------------------------------------"
echo "#4/socket读写缓冲区调优" 
if [ "`egrep "net.core.wmem_default=256960" /etc/sysctl.conf|wc -l`" == "0" ];then
        echo "net.core.wmem_default=256960" >> /etc/sysctl.conf
fi
if [ "`egrep "net.core.rmem_default=256960" /etc/sysctl.conf|wc -l`" == "0" ];then
        echo "net.core.rmem_default=256960" >> /etc/sysctl.conf
fi
if [ "`egrep "net.core.wmem_max=2097152" /etc/sysctl.conf|wc -l`" == "0" ];then
        echo "net.core.wmem_max=2097152" >> /etc/sysctl.conf
fi
if [ "`egrep "net.core.rmem_max=2097152" /etc/sysctl.conf|wc -l`" == "0" ];then
        echo "net.core.rmem_max=2097152" >> /etc/sysctl.conf
fi
if [ "`egrep "net.ipv4.tcp_wmem=8760  256960  4088000" /etc/sysctl.conf|wc -l`" == "0" ];then
        echo "net.ipv4.tcp_wmem=8760  256960  4088000" >> /etc/sysctl.conf
fi
if [ "`egrep "net.ipv4.tcp_rmem=8760  256960  4088000" /etc/sysctl.conf|wc -l`" == "0" ];then
        echo "net.ipv4.tcp_rmem=8760  256960  4088000" >> /etc/sysctl.conf
        sysctl -p
else
        echo "socket读写缓冲区调优 设置成功或者之前已经设置过了"
fi

echo "-----------------------------华丽分隔符----------------------------------------"
echo "#5/预读缓冲调优"
DF=`df |sed -n 2p |awk ‘{print $1}‘`
blockdev --setra 8192 $DF
blockdev --getra $DF

执行结果如下

[[email protected] scripts]# sh adlinuxopt.sh 
在执行此脚本之前需要先执行 initialization.sh 等待5s确认
若没执行请 ctrl+c 退出执行
1S
2S
3S
4S
5S
等待1s:#1/判断centos7还是centos6系统
centos6
-----------------------------华丽分隔符----------------------------------------
#2/调整Linux磁盘缓存和性能
Linux磁盘缓存和性能 vm.dirty_background_ratio 设置成功或者之前已经设置过了
Linux磁盘缓存和性能 vm.dirty_ratio设置成功或者之前已经设置过了
-----------------------------华丽分隔符----------------------------------------
#3/是否禁用ipv6
等待3秒:
        **********************
        1.[disable ipv6]
        2.[no disable ipv6]
        3.[exit]
    pls input the num you want:
        **********************
pls input the num you want:1
disable ipv6
等待3S
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
vm.swappiness = 1
vm.dirty_background_ratio = 5
vm.dirty_ratio = 10
net.ipv6.conf.all.disable_ipv6 = 1
net.core.wmem_default = 256960
net.core.rmem_default = 256960
net.core.wmem_max = 2097152
net.core.rmem_max = 2097152
net.ipv4.tcp_wmem = 8760  256960  4088000
net.ipv4.tcp_rmem = 8760  256960  4088000
等待3S
-----------------------------华丽分隔符----------------------------------------
#4/socket读写缓冲区调优
socket读写缓冲区调优 设置成功或者之前已经设置过了
-----------------------------华丽分隔符----------------------------------------
#5/预读缓冲调优
8192

参考链接为:03搭建cdh 生产环境前的Linux 优化(涉及到Linux内存参数优化)
https://blog.51cto.com/12445535/2365948

以上是关于linux内核高级优化脚本的主要内容,如果未能解决你的问题,请参考以下文章

高级程序员进阶:了解Linux I/O 调度器,优化系统性能

高级Linux SA需要会做的事情

Linux 实现自动安装服务组件以及优化内核参数 (转)

Linux系统最小化安装之后的系统基础环境安装以及内核优化脚本

Linux高级调试与优化——同时抓取coredump和maps文件

Linux内核memcpy的不同实现