Linux學習筆記7:初始化腳本

適合於剛裝好的系統進行一些初始化的配置:

#!/bin/bash
#初始化配置腳本
BEGINCOLOR="\e[1;33m"
ENDCOLOR="\e[0m"
BASEARCH="x86_64"
NIC_NAME="ens33"
VAR=`grep -o "[0-9]\+" /etc/redhat-release|head -1`
echo -e "${BEGINCOLOR}啓動腳本,檢測到系統是Centos$VAR${ENDCOLOR}"
if [ $VAR -eq 6 ];then
                NIC_NAME="eth0"
fi
#配置個性化shell腳本風格
echo -e "${BEGINCOLOR}開始shell腳本風格...${ENDCOLOR}"
cat > ~/.vimrc << EOF
set ignorecase
set cursorline
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
                if expand("%:e") == 'sh'
                call setline(1,"#!/bin/bash") 
                call setline(2,"#") 
                call setline(3,"#********************************************************************") 
                call setline(4,"#Author:                chenxu") 
                call setline(5,"#QQ:                    314236848") 
                call setline(6,"#Date:                  ".strftime("%Y-%m-%d"))
                call setline(7,"#FileName:             ".expand("%"))
                call setline(8,"#URL:                   http://www.magedu.com")
                call setline(9,"#Description:          The test script") 
                call setline(10,"#Copyright (C):        ".strftime("%Y")." All rights reserved")
                call setline(11,"#********************************************************************") 
                call setline(12,"") 
                endif
endfunc
autocmd BufNewFile * normal G
EOF
echo -e "${BEGINCOLOR}shell已配置,開始配置別名...${ENDCOLOR}"
#添加幾個別名
cat >> ~/.bashrc << EOF
alias viens="vim /etc/sysconfig/network-scripts/ifcfg-$NIC_NAME"
alias scandisk="echo '- - -' > /sys/class/scsi_host/host0/scan ;echo '- - -' > /sys/class/scsi_host/host1/scan ;echo '- - -' > /sys/class/scsi_host/host2/scan "
EOF
echo -e "${BEGINCOLOR}別名已添加,開始修改yum...${ENDCOLOR}"
#修改yum源
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo << EOF
[base]
name=aliyun base
baseurl='https://mirrors.aliyun.com/centos/$VAR/os/$BASEARCH/'
                'https://mirrors.tuna.tsinghua.edu.cn/centos/$VAR/os/x86_64/'
#baseurl=https://mirrors.aliyun.com/centos/7/updates/x86_64/
gpgcheck=0
gpgkey='https://mirrors.aliyun.com/centos/$VAR/os/x86_64/RPM-GPG-KEY-CentOS-$VAR'
enabled = 1
#enabled=1
EOF

cat > /etc/yum.repos.d/local.repo << EOF
[local]
name=local
baseurl='http://172.22.5.7/centos/$VAR/os/$BASEARCH/'
gpgcheck=0
EOF

cat > /etc/yum.repos.d/epel.repo << EOF
[epel]
name=epel base
baseurl='https://mirrors.aliyun.com/epel/$VAR/x86_64/'
gpgcheck=0
gpgkey='https://mirrors.aliyun.com/centos/$VAR/os/x86_64/RPM-GPG-KEY-CentOS-$VAR'
enabled=1
EOF
echo -e "${BEGINCOLOR}yum源配置完成,開始配置PATH變量...${ENDCOLOR}"
#修改PATH和PS1
echo 'PATH=/script:$PATH' > /etc/profile.d/script.sh
echo 'PS1="\[\e[1;37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[36;40m\]\w\[\e[0m\]]\\$ "' > /etc/profile.d/env.sh
echo -e "${BEGINCOLOR}PATH配置完成,開始修改防火牆配置...${ENDCOLOR}"
#關閉防火牆和selinux
if [ $VAR -eq 6 ];then
                service iptables stop &> /dev/null
                chkconfig iptables off &> /dev/null
                sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null
elif [ $VAR -eq 7 ];then
                systemctl stop firewalld &> /dev/null
                systemctl disable firewalld &> /dev/null
                sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null

fi
echo -e "${BEGINCOLOR}已關閉防火牆和selinux!${ENDCOLOR}"
echo -e "${BEGINCOLOR}初始化完成!須要重啓selinux才能生效${ENDCOLOR}"
exit
相關文章
相關標籤/搜索