因為最近好多公司都更新了CentOS7了,所有現在很多營運都需要重新編寫系統最佳化指令碼了,今天和大家分享一個新編寫的系統最佳化指令碼,如果有什麼問題,大家反應一下,回頭再繼續改。
#!/bin/bash# time:# get kernel versionRELEASEVER=$(rpm -q --qf "%{VERSION}" $(rpm -q --whatprovides redhat-release))# configure yum sourcecd /etc/yum.repos.d/mkdir /etc/yum.repos.d/bakmv /etc/yum.repos/d/*.repo /etc/yum.repos.d/bakif [ $RELEASEVER == 6 ];then wget http://mirrors.163.com/.help/CentOS6-Base-163.repo -O init.repofiif [ $RELEASEVER == 7 ];then wget http://mirrors.163.com/.help/CentOS7-Base-163.repo -O init.repofiyum -y clean allyum check-updata# install base rpm packageyum -y install epel-releaseyum -y install nc vim iftop iotop dstat tcpdumpyum -y install ipmitool bind-libs bind-utilsyum -y install libselinux-python ntpdate# updata rpm package include kernelyum -y updatarm -rf /etc/yum.repos.d/CentOS*# update ulimit configureif [ $RELEASEVER == 6 ];then test -f /etcsecurity/limits.d/90-nproc.conf && rm -rf /etc/security/limits.d/90-nproc.conf && touch /etc/security/limits.d/90=cproc.conffiif [ $RELEASEVER == 7 ];then test -f /etcsecurity/limits.d/20-nproc.conf && rm -rf /etc/security/limits.d/20-nproc.conf && touch /etc/security/limits.d/20=cproc.conffi>/etc/security/limits.confcat >> /etc/security/limits.conf <<EOF* soft nproc 65535* hard nproc 65535* soft nofile 65535* hard nofile 65535EOF# set timezonetest -f /etc/localtime && rm -rf /etc/localtimeln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime# set LANGsed -i 's@LANG=.*$LANG="en_US.UTF-8"$g' /etc/sysconfig/i18n# update timeif [ $RELEASEVER == 6 ];then /usr/sbin/ntpdate pool.ntp.org grep -q ntpdate /var/spool/cron/root if [ $? -ne 0 ];then echo '* * * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null/ 2>&1' /var/spool/cron/root fi /etc/init.d/crond restartfiif [ $RELEASEVER == 7 ];then yum -y install chrony > /etc/chrony.confcat >> /etc/security/limits.conf <<EOFserver pool.ntp.org lburststratumweight 0driftfile /var/lib/chrony/driftrtcsyncmakestep 10 3bindcmdaddress 127.0.0.1bindcmdaddress ::1commandkey 1keyfile /etc/chrony.keysgeneratecommandkeynoclientloglogchange 0.5logdir /var/log/chronyEOFfi# clean iptables default rulesif [ $RELEASEVER == 6 ];then /sbin/iptables -F /etc/init.d/iptables save chkconfig ip6tables offfiif [ $RELEASEVER == 7 ];then systemctl disable firewalldfi# disable unuserd servicechkconfig auditd off# disable ipv6cd /etc/modprobe.d/ && touch ipv6.conf> /etc/modprobe.d/ipv6.confcat >> /etc/modprobe.d/ipv6.conf << EOFalias net-pf-10 offalias ipv6 offEOF# disable iptable nat moudulecd /etc/modprobe.d/ && touch connectiontracking.conf> /etc/modprobe.d/connectiontracking.confcat >>/etc/modprobe.d/connectiontracking.conf <<EOFinstall nf_nat /bin/trueinstall xt_state /bin/trueinstall iptable_nat /bin/trueinstall nf_conntrack /bin/trueinstall nf_defrag_ipv4 /bin/trueinstall nf_conntrack_ipv4 /bin/trueinstall nf_conntrack_ipv6 /bin/trueEOF# disable SELINUXsetenforce 0sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config# update record commandsed -i 's/^HISTSIZE=.*$/HISTSIZE=100000/' /etc/profilegrep -q 'HISTTIMEFORMAT' /etc/profileif [[ $? -eq 0 ]];then sed -i 's/^HISTTIMEFROMAT=.*$/HISTTIMEFORMAT="%F %T "/' /etc/profileelse echo 'HISTTIMEFORMAT="%F %T "/' >> /etc/profilefi# install dnsmasq and update configureyum -y install dnsmasq> /etc/dnsmasq.confcat >> /etc/dnsmasq.conf << EOFlisten-address 127.0.0.1no-dhcp-interface lolog-querieslog-facility=/var/log/dnsmasq.logall-serversno-negcachecache-size=1024dns-firward-max=512EOFif [ $RELEASEVER == 6 ];then /etc/init.d/dnsmasq restartfiif [ $RELEASEVER == 7 ];then systemctl restart dnsmasqfi# update /etc/resolv.conf> /etc/resolv.confcat >> /etc/resolv.conf << EOFsearch xxx.comoptions timeout:1nameserver 127.0.0.1nameserver 8.8.8.8EOF# update /etc/sysctl.confsed -i 's/net.ipv4.tcp_syncookies.*/net.ipv4.tcp_syncookies = 1/g' /etcsysctl.concat >> /etc/sysctl.conf << EOFkernel.core_users_pid=1kernel.core_pattern=/tmp/core-%e-%pfs.suid_dumpable=2net.ipv4.tcp_tw_reuse=1net.ipv4.tcp_tw_recycle=0net.ipv4.tcp_timestamps=1EOFsysctl -p