Centos 7 系統優化腳本

#!/bin/bash

#this script is only for CentOS 7.x

#check the OS

platform=`uname -i`

if [ $platform != "x86_64" ];then

echo "this script is only for 64bit Operating System !"

exit 1

fi

echo "the platform is ok"

cat << EOF

+---------------------------------------+

|? your system is CentOS 7 x86_64? ? ? |

|? ? ? start optimizing.......? ? ? ? ? |

+---------------------------------------

EOF

#添加公網DNS地址

cat >> /etc/resolv.conf << "EOF"

nameserver 114.114.114.114

EOF

#Yum源更換為國內阿里源

yum install -y wget vim telnet tree?telnet?dos2unix?sysstat?lrzsz?nc?nmap?zip?unzip tumx libxslt-devel

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

#時區更改

\cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#添加阿里的epel源

#add the epel

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

# rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm

#yum重新建立緩存

yum clean all

yum makecache

#同步時間

yum -y install ntp

/usr/sbin/ntpdate cn.pool.ntp.org

echo "* 4 * * * /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/root

systemctl? restart crond.service

#設置最大打開文件描述符數

echo "ulimit -SHn 102400" >> /etc/rc.local

cat >> /etc/security/limits.conf << "EOF"

*? ? ? ? ? soft? nofile? ? ? 65534

*? ? ? ? ? hard? nofile? ? ? 65534

EOF

#禁用selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

setenforce 0

#關閉防火墻

systemctl disable firewalld.service

systemctl stop firewalld.service

#set ssh

sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config

sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config

systemctl? restart sshd.service

#內核參數優化

cat >> /etc/sysctl.conf << "EOF"

vm.swappiness = 1

net.ipv6.conf.all.disable_ipv6 = 1

net.ipv6.conf.default.disable_ipv6 = 1

net.ipv4.icmp_echo_ignore_broadcasts = 1

net.ipv4.icmp_ignore_bogus_error_responses = 1

net.ipv4.ip_forward = 0

net.ipv4.conf.all.send_redirects = 0

net.ipv4.conf.default.send_redirects = 0

net.ipv4.conf.all.rp_filter = 1

net.ipv4.conf.default.rp_filter = 1

net.ipv4.conf.all.accept_source_route = 0

net.ipv4.conf.default.accept_source_route = 0

kernel.sysrq = 0

kernel.core_uses_pid = 1

net.ipv4.tcp_syncookies = 1

kernel.msgmnb = 65536

kernel.msgmax = 65536

kernel.shmmax = 68719476736

kernel.shmall = 4294967296

net.ipv4.tcp_max_tw_buckets = 6000

net.ipv4.tcp_sack = 1

net.ipv4.tcp_window_scaling = 1

net.ipv4.tcp_rmem = 4096? ? ? ? 87380? 4194304

net.ipv4.tcp_wmem = 4096? ? ? ? 16384? 4194304

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.core.netdev_max_backlog = 262144

net.ipv4.tcp_max_orphans = 3276800

net.ipv4.tcp_max_syn_backlog = 262144

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 1

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_fin_timeout = 1

net.ipv4.tcp_keepalive_time = 30

net.ipv4.ip_local_port_range = 1024? 65535

fs.file-max = 265535

net.ipv4.conf.all.accept_redirects = 0

net.ipv4.conf.default.accept_redirects = 0

net.ipv4.conf.all.secure_redirects = 0

net.ipv4.conf.default.secure_redirects = 0

EOF

/sbin/sysctl -p

#vim定義退格鍵可刪除最后一個字符類型

echo 'alias vi=vim' >> /etc/profile

echo 'stty erase ^H' >> /etc/profile

cat >> /root/.vimrc << "EOF"

set tabstop=4

set shiftwidth=4

set expandtab

syntax on

"set number

EOF

#update soft

#yum -y update

cat << EOF

+-------------------------------------------------+

|? ? ? ? ? ? ? optimizer is done? ? ? ? ? ? ? ? |

|? it's recommond to restart this server !? ? ? |

+-------------------------------------------------+

EOF

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • !/bin/bash ##############################################...
    SkTj閱讀 3,893評論 1 23
  • 后面繼續添加優化 1.修改主機名 #方便標識主機hostnamectl set-hostname newname ...
    杜永龍閱讀 430評論 0 0
  • 作者:馬帥琦 歸檔:day31 時間:2019/4/12 1、什么是集群? 簡單地說,集群就是一堆機器做同一件事,...
    馬帥琦閱讀 246評論 0 0
  • 本腳本是centos 6系統初始化腳本,參考網上已有腳本,以模塊化的形式呈現,可以按照需要方便注釋掉不需要的模塊。...
    think_lonely閱讀 261評論 0 3
  • ##修改yum源,關閉selinux,關閉防火墻,關閉不必要的開機服務,添加用戶,設置默認字符集UTF8 ##時間...
    SkTj閱讀 805評論 0 1