CentOS 6初始化环境脚本
#!/bin/bash
#SHELL ENV
BASECOLOR="\E[1;46m"
WARNCOLOR="\E[1;5;31m"
ENDCOLOR="\E[0m"
function color()
{
RES_COL=60;
MOVE_TO_COL="echo -en \\033[${RES_COL}G";
SETCOLOR_SUCCESS="echo -en \\033[1;32m";
SETCOLOR_FAILURE="echo -en \\033[1;31m";
SETCOLOR_WARNING="echo -en \\033[1;33m";
SETCOLOR_NORMAL="echo -en \E[0m";
echo -n "$1" && $MOVE_TO_COL;
echo -n "[";
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS};
echo -n $" OK ";
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE};
echo -n $"FAILED";
else
${SETCOLOR_WARNING};
echo -n $"WARNING";
fi
${SETCOLOR_NORMAL};
echo -n "]";
echo
}
#Step1:关闭SELinux功能
function set_selinux()
{
setenforce 0
sed -i '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config
color "关闭SELinux功能成功" 0
}
#Step2:关闭防火墙功能
function set_firewalld()
{
service iptables stop
chkconfig iptables off
color "关闭防火墙功能成功" 0
}
#Step3:设置光盘源
function set_yum()
{
CDROM=$(lsblk | grep sr0 &> /dev/null ; echo $?)
if [ $CDROM -eq 0 ] ;then
color "系统光盘已挂载,开始设置光盘源" 0
else
{ color "系统光盘未挂载,无法设置光盘源" 1 ; exit ; }
fi
mkdir -p /mnt/cdrom ; mount /dev/sr0 /mnt/cdrom
mkdir /etc/yum.repos.d/repobak ; mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repobak
cat > /etc/yum.repos.d/CentOS-ISO.repo <<EOF
[CentOS-ISO]
name=CentOS-ISO
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
EOF
yum repolist
color "设置光盘源成功" 0
}
#Stpe4:关闭NetworkManager功能
function set_network_manager()
{
service NetworkManager stop
chkconfig NetworkManager off
color "关闭NetworkManager功能成功" 0
}
#Step5:设置操作系统常用别名
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}"
color "设置操作系统常用别名功能成功" 0
}
set_selinux
set_network_manager
set_firewalld
set_yum
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