Centos6&&7系统初始化脚本

#!/bin/bash
# Linux system initialization
# by zjz 2020-07-12 v1.0

os_version=`cat /etc/redhat-release |awk '{print $4}' | awk -F "." '{print $1}'`

repo() {
    
    
        mkdir -p /etc/yum.repos.d/yum
        mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/yum
        curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-$os_version.repo
        curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-$os_version.repo
        echo "Aliyun-repo download OK!!!!"

#       yum clean all && yum makecache
        yum install -y vim ntpdate
        if [ $? -ne 0 ];then
                echo "Aliyun-YUM仓库安装失败,请检测网络连接、DNS是否正常"
                exit
        fi
}

#ntpdate() {
    
    
#       ntpdate ntp.aliyun.com &>/dev/null
#       hwclock -s
#       echo "This is  Beijing time:  `date`"
#}

change-history() {
    
    
        sed -i '/HISTSIZE=/cHISTSIZE=100000' /etc/profile
        grep -q 'HISTTIMEFORMAT' /etc/profile
        if [ $? -eq 0 ];then
                sed -i 's/^export HISTTIMEFORMAT=.*$/export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S  `whoami` "/' /etc/profile
        else
                echo 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S  `whoami` "' >> /etc/profile
        fi
        source /etc/profile
        echo "History command change OK!!!"
}

#5.调整内核参数
kernel() {
    
    
        cat>/etc/sysctl.conf <<-EOF
                net.ipv4.tcp_tw_reuse=1
                net.ipv4.tcp_tw_recycle=0
        EOF
        echo "Kernel command change OK!!!"
}

##########################
case "$os_version" in
7)
##Firewalld
        systemctl disable firewalld
        systemctl stop firewalld
        if [ $? -eq 0 ];then
                echo "1.Firewalld is stop..."
        else
                echo "Opoo,Please check firewalld status."
        fi
##SELINUX
        sed -i "s#SELINUX=enforcing#SELINUX=disabled#" /etc/selinux/config
        setenforce 0
                echo "2.SELINUX is close..."

##Add Aliyun yum source
        repo

##Ntpdate Beijing
        ntpdate ntp.aliyun.com &>/dev/null
        hwclock -s
        echo "This is  Beijing time:  `date`"
##timezone
        timedatectl set-timezone Asia/Shanghai

##history
        change-history

##kernel
        kernel
        ;;

6)
##iptables
        service iptables stop
        chkconfig iptables off
        if [ $? -eq 0 ];then
                echo "1.iptables is stop..."
        else
                echo "Opoo,Please check iptables status."
        fi
##SELINUX
        sed -i "s#SELINUX=enforcing#SELINUX=disabled#" /etc/selinux/config
        setenforce 0
                echo "2.SELINUX is close..."

##Add Aliyun yum source
        repo

##Ntpdate Beijing
        ntpdate

##timezone
        cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

##history
        change-history
        ;;
"")
        #此行表示,什么都没输入就 退出
        echo "Failed to check system version"
        ;;
*)
        #输入的不是1 or 2 ,就回显Input error
        echo "Opoo,error"
        ;;
esac


执行结果:

1.Firewalld is stop...
2.SELINUX is close...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2523  100  2523    0     0  47182      0 --:--:-- --:--:-- --:--:-- 47603
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   664  100   664    0     0    118      0  0:00:05  0:00:05 --:--:--   162
Aliyun-repo download OK!!!!
Loaded plugins: fastestmirror
base                                                                     | 3.6 kB  00:00:00
epel                                                                     | 4.7 kB  00:00:00
extras                                                                   | 2.9 kB  00:00:00
updates                                                                  | 2.9 kB  00:00:00
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package 2:vim-enhanced-7.4.629-6.el7.x86_64 already installed and latest version
Package ntpdate-4.2.6p5-29.el7.centos.2.x86_64 already installed and latest version
Nothing to do
This is  Beijing time:  Sun Jul 12 16:31:53 CST 2020
History command change OK!!!
Kernel command change OK!!!

猜你喜欢

转载自blog.csdn.net/qq_37126850/article/details/107301327
今日推荐