CentOS 6初始化环境脚本

  1. #!/bin/bash
  2. #SHELL ENV
  3. BASECOLOR="\E[1;46m"
  4. WARNCOLOR="\E[1;5;31m"
  5. ENDCOLOR="\E[0m"
  6. function color()
  7. {
  8. RES_COL=60;
  9. MOVE_TO_COL="echo -en \\033[${RES_COL}G";
  10. SETCOLOR_SUCCESS="echo -en \\033[1;32m";
  11. SETCOLOR_FAILURE="echo -en \\033[1;31m";
  12. SETCOLOR_WARNING="echo -en \\033[1;33m";
  13. SETCOLOR_NORMAL="echo -en \E[0m";
  14. echo -n "$1" && $MOVE_TO_COL;
  15. echo -n "[";
  16. if [ $2 = "success" -o $2 = "0" ] ;then
  17. ${SETCOLOR_SUCCESS};
  18. echo -n $" OK ";
  19. elif [ $2 = "failure" -o $2 = "1" ] ;then
  20. ${SETCOLOR_FAILURE};
  21. echo -n $"FAILED";
  22. else
  23. ${SETCOLOR_WARNING};
  24. echo -n $"WARNING";
  25. fi
  26. ${SETCOLOR_NORMAL};
  27. echo -n "]";
  28. echo
  29. }
  30. #Step1:关闭SELinux功能
  31. function set_selinux()
  32. {
  33. setenforce 0
  34. sed -i '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config
  35. color "关闭SELinux功能成功" 0
  36. }
  37. #Step2:关闭防火墙功能
  38. function set_firewalld()
  39. {
  40. service iptables stop
  41. chkconfig iptables off
  42. color "关闭防火墙功能成功" 0
  43. }
  44. #Step3:设置光盘源
  45. function set_yum()
  46. {
  47. CDROM=$(lsblk | grep sr0 &> /dev/null ; echo $?)
  48. if [ $CDROM -eq 0 ] ;then
  49. color "系统光盘已挂载,开始设置光盘源" 0
  50. else
  51. { color "系统光盘未挂载,无法设置光盘源" 1 ; exit ; }
  52. fi
  53. mkdir -p /mnt/cdrom ; mount /dev/sr0 /mnt/cdrom
  54. mkdir /etc/yum.repos.d/repobak ; mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repobak
  55. cat > /etc/yum.repos.d/CentOS-ISO.repo <<EOF
  56. [CentOS-ISO]
  57. name=CentOS-ISO
  58. baseurl=file:///mnt/cdrom
  59. enabled=1
  60. gpgcheck=0
  61. EOF
  62. yum repolist
  63. color "设置光盘源成功" 0
  64. }
  65. #Stpe4:关闭NetworkManager功能
  66. function set_network_manager()
  67. {
  68. service NetworkManager stop
  69. chkconfig NetworkManager off
  70. color "关闭NetworkManager功能成功" 0
  71. }
  72. #Step5:设置操作系统常用别名
  73. function set_alias() {
  74. echo -e "${BASECOLOR}Starting set alias......${ENDCOLOR}"
  75. echo "" >> /etc/bashrc
  76. cat >> /etc/bashrc <<-'EOF'
  77. # Bash Alias Starting
  78. alias cdnet='cd /etc/sysconfig/network-scripts/'
  79. alias cdyum='cd /etc/yum.repos.d/'
  80. alias nm='systemctl restart NetworkManager'
  81. alias disepel='sed -ri /enabled/s/enabled=1/enabled=0/ /etc/yum.repos.d/base.repo'
  82. alias egrep='egrep --color=auto'
  83. alias epel='sed -ri /enabled/s/enabled=0/enabled=1/ /etc/yum.repos.d/base.repo'
  84. alias fgrep='fgrep --color=auto'
  85. alias i.='ls -d .* --color=auto'
  86. alias lh='ls -lh --color=auto'
  87. alias more='more -d'
  88. alias p='poweroff'
  89. alias restart='systemctl restart '
  90. alias restartnet='systemctl restart network'
  91. alias scandisk='echo "- - -" > /sys/class/scsi_host/host0/scan;\
  92. echo "- - -" > /sys/class/scsi_host/host1/scan;\
  93. echo "- - -" > /sys/class/scsi_host/host2/scan'
  94. alias vie0='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
  95. alias vie1='vim /etc/sysconfig/network-scripts/ifcfg-eth1'
  96. alias xzfgrep='xzfgrep --color=auto'
  97. alias xzgrep=' xzgrep --color=auto'
  98. alias yr='yum remove'
  99. alias yy='yum -y install'
  100. alias zegrep='zegrep --color=auto'
  101. alias zfgrep='zfgrep --color=auto'
  102. alias zgrep='zgrep --color=auto'
  103. alias more='more -d'
  104. # Bash Alias End
  105. EOF
  106. echo -e "${BASECOLOR}Alias set finished......${ENDCOLOR}"
  107. color "设置操作系统常用别名功能成功" 0
  108. }
  109. set_selinux
  110. set_network_manager
  111. set_firewalld
  112. set_yum
  113. set_alias

CentOS 7初始化环境脚本

#!/bin/bash
#Initialize the system script

#SHELL ENV
BASECOLOR="\E[1;46m"
WARNCOLOR="\E[1;5;31m"
ENDCOLOR="\E[0m"

#yum install -y cowsay

#SHELL Function
function set_alias() {
echo -e "${BASECOLOR}Starting set alias......${ENDCOLOR}"
echo "" >> /etc/bashrc
cat >> /etc/bashrc <<-'EOF'
# Bash Alias Starting
alias cdnet='cd /etc/sysconfig/network-scripts/'
alias cdyum='cd /etc/yum.repos.d/'
alias nm='systemctl restart NetworkManager'
alias disepel='sed -ri /enabled/s/enabled=1/enabled=0/ /etc/yum.repos.d/base.repo'
alias egrep='egrep --color=auto'
alias epel='sed -ri /enabled/s/enabled=0/enabled=1/ /etc/yum.repos.d/base.repo'
alias fgrep='fgrep --color=auto'
alias i.='ls -d .* --color=auto'
alias lh='ls -lh --color=auto'
alias more='more -d'
alias p='poweroff'
alias restart='systemctl restart '
alias restartnet='systemctl restart network'
alias scandisk='echo "- - -" > /sys/class/scsi_host/host0/scan;\
                echo "- - -" > /sys/class/scsi_host/host1/scan;\
                echo "- - -" > /sys/class/scsi_host/host2/scan'
alias vie0='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
alias vie1='vim /etc/sysconfig/network-scripts/ifcfg-eth1'
alias xzfgrep='xzfgrep --color=auto'
alias xzgrep=' xzgrep --color=auto'
alias yr='yum remove'
alias yy='yum -y install'
alias zegrep='zegrep --color=auto'
alias zfgrep='zfgrep --color=auto'
alias zgrep='zgrep --color=auto'
alias more='more -d'
# Bash Alias End
EOF
echo -e "${BASECOLOR}Alias set finished......${ENDCOLOR}"
}

function set_command_prompt() {
FILE="/etc/bashrc"
echo -e "${BASECOLOR}"
    cat <<-'EOF'
    1:(Red)
    2:(Green)
    3:(Yellow)
    4:(Blue)
    5:(Pink)
    6:(Light Blue)
EOF
    echo -e "${ENDCOLOR}"

    echo "" >> ${FILE}
    echo "# System Command Prompt ENV EBGIN" >> ${FILE}
    ENDCOMMAND=`echo '# System Command Prompt ENV END' >> ${FILE}`
    read -p "Command prompt color modification:" OPTIONS
    case ${OPTIONS} in
    1)
        echo 'PS1="\[\e[1;31m\][\u@\h \W]\\$\[\e[0m\]"' >> ${FILE}
        ${ENDCOMMAND}
        ;;
    2)
        echo 'PS1="\[\e[1;32m\][\u@\h \W]\\$\[\e[0m\]"' >> ${FILE}
        ${ENDCOMMAND}
        ;;
    3)
        echo 'PS1="\[\e[1;33m\][\u@\h \W]\\$\[\e[0m\]"' >> ${FILE}
        ${ENDCOMMAND}
        ;;
    4)
        echo 'PS1="\[\e[1;34m\][\u@\h \W]\\$\[\e[0m\]"' >> ${FILE}
        ${ENDCOMMAND}
        ;;
    5)
        echo 'PS1="\[\e[1;35m\][\u@\h \W]\\$\[\e[0m\]"' >> ${FILE}
        ${ENDCOMMAND}
        ;;
    6)
        echo 'PS1="\[\e[1;36m\][\u@\h \W]\\$\[\e[0m\]"' >> ${FILE}
        ${ENDCOMMAND}
        ;;
    *)
        echo -e "${BASECOLOR}Your input options are wrong${ENDCOLOR}"
        ;;
    esac
}

function set_yum() {
echo -e "${BASECOLOR}Starting set Yum Repo......${ENDCOLOR}"

mkdir -pv /etc/yum.repos.d/repobackup &> /dev/null
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repobackup &> /dev/null
# Determine the operating system type
# (currently, CentOS7 and CentOS8 are supported)
OS_VERSION=$(cat /etc/redhat-release|cut -d" " -f4|awk -F"." '{print $1}')
case $OS_VERSION in
7)
cat > /etc/yum.repos.d/CentOS7-all.repo <<END
[CentOS7-Base]
name=CentOS7-Base
baseurl=file:///misc/cd/
        https://mirrors.cloud.tencent.com/centos/\$releasever/os/\$basearch/
        https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/os/\$basearch/
        http://mirrors.163.com/centos/\$releasever/os/\$basearch/
        https://repo.huaweicloud.com/centos/\$releasever/os/\$basearch/
        https://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
enabled=1
gpgcheck=0

[CentOS7-extras]
name=CentOS7-extras
baseurl=https://mirrors.cloud.tencent.com/centos/\$releasever/extras/\$basearch/
        https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/
        http://mirrors.163.com/centos/\$releasever/extras/\$basearch/
        https://repo.huaweicloud.com/centos/\$releasever/extras/\$basearch/
        https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/
enabled=1
gpgcheck=0

[CentOS7-epel]
name=CentOS7-epel
baseurl=https://mirrors.aliyun.com/epel/\$releasever/\$basearch/
        https://repo.huaweicloud.com/epel/\$releasever/\$basearch/
        https://mirrors.tuna.tsinghua.edu.cn/epel/\$releasever/\$basearch/
        https://mirrors.cloud.tencent.com/epel/\$releasever/\$basearch/
enabled=1
gpgcheck=0

[CentOS7-updates]
name=CentOS7-updates
baseurl=https://mirrors.cloud.tencent.com/centos/\$releasever/updates/\$basearch/
        https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/updates/\$basearch/
        http://mirrors.163.com/centos/\$releasever/updates/\$basearch/
        https://repo.huaweicloud.com/centos/\$releasever/updates/\$basearch/
        https://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/
enabled=1
gpgcheck=0
END
;;
8)
cat > /etc/yum.repos.d/CentOS8-all.repo <<END
[CentOS8-vault-BaseOS]
name=CentOS8-vault-BaseOS
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/BaseOS/\$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/BaseOS/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/BaseOS/\$basearch/os/
        https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/BaseOS/\$basearch/os/
        https://repo.huaweicloud.com/centos-vault/8.5.2111/BaseOS/\$basearch/os/
        https://mirrors.cloud.tencent.com/centos-vault/8.5.2111/BaseOS/\$basearch/os/
enabled=1
gpgcheck=0

[CentOS8-vault-AppStream]
name=CentOS8-vault-AppStream
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/AppStream/\$basearch/os/
        http://mirrors.aliyuncs.com/centos-vault/8.5.2111/AppStream/\$basearch/os/
        http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/AppStream/\$basearch/os/
        https://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/AppStream/\$basearch/os/
        https://mirrors.cloud.tencent.com/centos-vault/8.5.2111/AppStream/\$basearch/os/
enabled=1
gpgcheck=0

[CentOS8-extras]
name=CentOS8-extras
baseurl=https://mirrors.cloud.tencent.com/centos/\$releasever/extras/\$basearch/os/
        https://repo.huaweicloud.com/centos/\$releasever/extras/\$basearch/os/
        https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/os/
        https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/os/
        https://repo.huaweicloud.com/centos-vault/8.5.2111/AppStream/\$basearch/os/
enabled=1
gpgcheck=0

[CentOS8-epel]
name=CentOS8-epel
baseurl=https://mirrors.aliyun.com/epel/\$releasever/Everything/source/tree/
        https://mirrors.tuna.tsinghua.edu.cn/epel/\$releasever/Everything/source/tree/
        https://repo.huaweicloud.com/epel/\$releasever/Everything/\$basearch/
        https://mirrors.cloud.tencent.com/epel/\$releasever/Everything/\$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
gpgcheck=1
END
;;
*)
  echo "${WARNCOLOR}The operating system was not found${ENDCOLOR}"
  exit
;;
esac
echo -e "${BASECOLOR}Set Yum Repo finished......${ENDCOLOR}"
}

function set_software() {
echo -e "${BASECOLOR}Staring install software......${ENDCOLOR}"
yum install -y gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages cowsay

cowsay "Common software packages such as Tree, FTP, LFTP, and Telnet are installed"
}

function set_firewalld() {
#Check the operating system type
VERSION=$(cat /etc/os-release|grep "^ID="|tr -dc [a-z])
echo -e "${BASECOLOR}Disabling the Firewall${ENDCOLOR}"
case $VERSION in
ubuntu)
    service ufw stop &> /dev/null
    ufw disable &> /dev/null
    echo -e "${BASECOLOR}The Firewall is stop${ENDCOLOR}"
;;
centos)
    NUMBER=$(cat /etc/redhat-release|tr -dc [0-9]|head -c1)
    if [ $NUMBER -le 6 ];then
        service iptables stop
        chkconfig iptables off
    elif [ $NUMBER -ge 7 ];then
        systemctl stop firewalld
        systemctl disable firewalld
    fi
    echo -e "${BASECOLOR}The Firewall is stop${ENDCOLOR}"
    #Close the SELINUX
    echo -e "${BASECOLOR}Disabling the SELINUX${ENDCOLOR}"
    sed -i -r '/^SELINUX=/s#(.*)=.*#\1=disabled#g' /etc/selinux/config
    echo -e "${BASECOLOR}The SELINUX is stop${ENDCOLOR}"
;;
*)
    echo -e "${ENDCOLOR}Sorry, this script does not support this operating system${ENDCOLOR}"
    exit;
;;
esac
}

function set_network() {
#Check the operating system type
VERSION=$(cat /etc/os-release|grep "^ID="|tr -dc [a-z])
echo -e "${BASECOLOR}Reset Network Card Name${ENDCOLOR}"
case $VERSION in
ubuntu)
sed -i.bak -r '/^GRUB_CMDLINE_LINUX=/s@(.*=).*@\1"net.ifnames=0"@g' /etc/default/grub
grub-mkconfig -o /boot/grub/grub.cfg
cp /etc/netplan/*.yaml{,.bak}
mv /etc/netplan/*.yaml /etc/netplan/eth0.yaml
sed -i -r '/ens33/s#ens33#eth0#ig' /etc/netplan/eth0.yaml
read -p "Restart the operating system for the configuration to take effect(Yes|No) : " CHOICE
while true;do
    case $CHOICE in
        [Yy]|[Yy][Ee][Ss])
            echo -e "${WARNCOLOR}Starting Reboot System(Last time:5 second)${ENDCOLOR}"
            sleep 5
            reboot
        ;;
        [Nn]|[Nn][Oo])
            echo -e "${WARNCOLOR}Restart the system later for the configuration to take effect${ENDCOLOR}"
            break
        ;;
        *)
            echo -e "${WARNCOLOR}Your input is wrong\nPlease re-enter${ENDCOLOR}"
            exit
        ;;
    esac
done
;;
centos)
sed -i.bak -r '/^GRUB_CMDLINE_LINUX=/s#(.*)="(.*)"#\1="\2 net.ifnames=0"#g' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
cp /etc/sysconfig/network-scripts/ifcfg-ens33{,.bak}
mv /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i -r -e '/^UUID=/d' -e '/^NAME=/c\NAME=eth0' -e '/^DEVICE=/c\DEVICE=eth0' /etc/sysconfig/network-scripts/ifcfg-eth0
read -p "Restart the operating system for the configuration to take effect(Yes|No) : " CHOICE
while true;do
    case $CHOICE in
        [Yy]|[Yy][Ee][Ss])
            echo -e "${WARNCOLOR}Starting Reboot System(Last time:5 second)${ENDCOLOR}"
            sleep 5
            reboot
        ;;
        [Nn]|[Nn][Oo])
            echo -e "${WARNCOLOR}Restart the system later for the configuration to take effect${ENDCOLOR}"
            break
        ;;
        *)
            echo -e "${WARNCOLOR}Your input is wrong\nPlease re-enter${ENDCOLOR}"
            exit
        ;;
    esac
done
;;
*)
    echo -e "${ENDCOLOR}Sorry, this script does not support this operating system${ENDCOLOR}"
    exit;
;;
esac
}

function set_vim_format() {
cat > ~/.vimrc <<EOF
set number
set cursorline
set paste
set textwidth=65
set expandtab
set tabstop=4

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:        kubesphere")
    call setline(5,"#QQ:            www.935523993.com")
    call setline(6,"#Date:          ".strftime("%Y-%m-%d"))
    call setline(7,"#FileName:      ".expand("%"))
    call setline(8,"#URL:           http://www.kubesphere.com")
    call setline(9,"#Description:   The script is kubesphere")
    call setline(10,"#Copyright(C): ".strftime("%Y")."All rights reserved")
    call setline(11,"#**************************************************#")
    call setline(12,"")
    endif
endfunc
autocmd BufNewFile * normal G
EOF
}

while true;do
cat <<EOF
(1) 设置命令别名
(2) 设置命令提示符颜色
(3) Yum仓库配置
(4) 安装常用 tree,ftp,lftp,telnet 等包
(5) 配置防火墙(关闭firewalld 和 selinux)
(6) 初始化网卡名(将网卡名改为传统命名方式:eth<Number>)
(7) 设置VIM常用格式设置
(8) 以上全部执行
(9) 退出脚本
EOF
read -p "Please input your hope choice : " CHOICE
case $CHOICE in
1)
    set_alias
;;
2)
    set_command_prompt
;;
3)
    set_yum
;;
4)
    set_yum
    set_software
;;
5)
    set_firewalld
;;
6)
    set_network
;;
7)
    set_vim_format
;;
8)
    set_alias
    set_command_prompt
    set_yum
    set_software
    set_firewalld
    set_network
    set_vim_format
;;
9)
    break
    exit
;;
*)
    echo -e "${WARNCOLOR}Your input number error${ENDCOLOR}"
;;
esac
done