#!/bin/bash
#系统初始化脚本, 适用于CentOS[6&7&8]和Ubuntu
GREEN="echo -e \033[1;32m"
RED="echo -e \033[1;31m"
END="\033[0m"
DIS_VERSION=`[ -f /etc/os-release ] && sed -nr '1s/^NAME="(.*)"/\1/p' /etc/os-release 2> /dev/null`
#用来取发行版, CentOS 7,8,Ubuntu都适用, 如果不存在则为6. 因为,6没有/etc/os-release文件, 则默认判断为6
#如果是Ubuntu版本, 取出来的值为Ubuntu, 如果是CentOS版本, 取出来的值为CentOS Linux
#加了错误重定向, 是为了避免在CentOS 6上执行脚本时, 由于没有/etc/os-release文件, 会出现sed 找不到文件报错
OS_VERSION=`sed -nr '2p' /etc/os-release | tr -s ' ' | cut -d ' ' -f1 | cut -d '"' -f2`
#用来取操作系统版本, Ubuntu无需判断,因为已经通过DIS_VERSION和CentOS区分开来了, CentOS 7,8需要判断, CentOS 6用 ! [ -f /etc/os-release ] 判断
#如果是CentOS 7, 取出来的值为7; 如果是CentOS 8, 取出来的值为8
[ `whoami` = "root" ] || { exit 1; ${RED}Please switch to root account to run this script!!!${END}; }
${GREEN}Linux distribution is $DIS_VERSION${END}
${GREEN}OS system is $OS_VERSION${END}
#1. 修改主机名称
Setup_Hostname(){
if [ -f /etc/os-release ]; then
while true; do
read -p "Plese input Hostname (HOSTNAME.DOMAIN): " HOSTNAME
case $HOSTNAME in
*.*)
hostnamectl set-hostname $HOSTNAME
${GREEN}Hostname has been changed to $HOSTNAME!${END}
break
;;
*)
${RED}Wrong Hostname Format, Must Be HOSTNAME.DOMAIN!${END}
esac
done
else
while true; do
read -p "Plese input Hostname (HOSTNAME.DOMAIN): " HOSTNAME
case $HOSTNAME in
*.*)
sed -i.bak "s/^HOSTNAME=.*/HOSTNAME=$HOSTNAME/" /etc/sysconfig/network
${GREEN}Hostname has been changed to $HOSTNAME!${END}
break
;;
*)
${RED}Wrong Hostname Format, Must Be HOSTNAME.DOMAIN!${END}
esac
done
fi
}
#2. 修改命令提示符
CMD_PROMPT(){
echo 'PS1="\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \w\[\e[1;32m\]]\[\e[0m\]\\$"' >> /etc/profile.d/env.sh
[ $? -eq 0 ] && ${GREEN}命令提示符修改成功!$END || ${RED}命令提示符修改失败!$END
}
#3. 修改vim格式
vim_style(){
cat > /root/.vimrc <<EOF
set ts=4
set expandtab
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: David")
call setline(5,"#QQ: 953260716")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName ".expand("%"))
call setline(8,"#URL: https://www.wangxiaoning.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
[ $? -eq 0 ] && ${GREEN}vim格式修改成功!$END || ${RED}vim格式修改失败!$END
}
#4. 修改PATH变量
Setup_PATH(){
! [ -d /data/scripts ] && mkdir -p /data/scripts
! [ -d /data/pkgs ] && mkdir -p /data/pkgs
! [ -d /data/prac ] && mkdir -p /data/prac
echo 'PATH="/data/scripts:$PATH"' >> /etc/profile.d/env.sh
[ $? -eq 0 ] && ${GREEN}PATH变量修改成功!$END || ${RED}PATH变量修改失败!$END
}
#5. 修改网卡名称为eth0
change_eth0(){
if [ -f /etc/os-release ]; then
if [ "${DIS_VERSION}" = Ubuntu ] ;then
net_file=`ls /etc/netplan/`
sed -i.bak '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
grub-mkconfig -o /boot/grub/grub.cfg &> /dev/null
sed -i 's/ens33/eth0/' /etc/netplan/$net_file
[ $? -eq 0 ] && ${GREEN}net_int has been changed!${END} || ${RED}net_int change failed!!!${END}
#netplan apply, 该命令会使网卡配置文件立即生效, 不建议在初始化阶段执行, 会造成ssh中断
else
#CentOS 7&8 修改网卡名称方式一样
sed -i.bak '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
mv /etc/sysconfig/network-scripts/ifcfg-ens* /etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri 's/(NAME=).*/\1eth0/' /etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri 's/(DEVICE=).*/\1eth0/' /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth0
[ $? -eq 0 ] && ${GREEN}net_int has been changed!${END} || ${RED}net_int change failed!!!${END}
fi
else
{GREEN}CentOS 6 has eth0 as default, there is no need to chang{END}
fi
}
#6. 关闭和禁用防火墙
disable_firewall(){
if [ -f /etc/os-release ]; then
if [ "${DIS_VERSION}" = Ubuntu ]; then
ufw disable &> /dev/null
[ $? -eq 0 ] && ${GREEN}Firewall has been disabled!${END} || ${RED}Firewall disable failed!${END}
else
systemctl disable --now firewalld &> /dev/null
[ $? -eq 0 ] && ${GREEN}Firewall has been disabled!${END} || ${RED}Firewall disable failed!${END}
fi
else
chkconfig iptables off
[ $? -eq 0 ] && ${GREEN}Firewall has been disabled!${END} || ${RED}Firewall disable failed!${END}
fi
}
#7. 禁用SElinux
#CentOS 6&7&8都适用; Ubuntu没有SElinux
Disable_SElinux(){
sed -i.bak 's#^SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config
[ $? -eq 0 ] && ${GREEN}SElinux禁用成功!$END || ${RED}SElinux禁用失败!$END
}
#8. 制作阿里云yum源
mirrors_sources(){
if [ "${DIS_VERSION}" = 'Ubuntu' ];then
apt_source=bionic
cat > /etc/apt/sources.list <<EOF
deb http://mirrors.aliyun.com/ubuntu/ $apt_source main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $apt_source main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $apt_source-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $apt_source-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $apt_source-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $apt_source-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $apt_source-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $apt_source-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $apt_source-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $apt_source-backports main restricted universe multiverse
EOF
[ $? -eq 0 ] && ${GREEN}apt repos has been changed!${END} || ${RED}yum repos change failed!!!${END}
elif ! [ -f /etc/os-release ];then
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/CentOS6.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/6/os/x86_64/
https://mirrors.huaweicloud.com/centos/6/os/x86_64/
https://mirrors.163.com/centos/6/os/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/6/x86_64/
https://mirrors.huaweicloud.com/epel/6/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-6
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/6/extras/x86_64/
https://mirrors.huaweicloud.com/centos/6/extras/x86_64/
https://mirrors.163.com/centos/6/extras/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6
EOF
[ $? -eq 0 ] && ${GREEN}yum repos has been changed!${END} || ${RED}yum repos change failed!!!${END}
elif [ ${OS_VERSION} -eq 7 ];then
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/CentOS7.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
https://mirrors.huaweicloud.com/centos/7/os/x86_64/
https://mirrors.163.com/centos/7/os/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/7/os/x86_64/RPM-GPG-KEY-CentOS-7
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-7
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
EOF
[ $? -eq 0 ] && ${GREEN}yum repos has been changed!${END} || ${RED}yum repos change failed!!!${END}
else
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/CentOS8.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/BaseOS/x86_64/os/
https://mirrors.163.com/centos/8/BaseOS/x86_64/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/8/AppStream/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/AppStream/x86_64/os/
https://mirrors.163.com/centos/8/AppStream/x86_64/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
https://mirrors.huaweicloud.com/epel/8/Everything/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/8/extras/x86_64/os/
https://mirrors.huaweicloud.com/centos/8/extras/x86_64/os/
https://mirrors.163.com/centos/8/extras/x86_64/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
EOF
[ $? -eq 0 ] && ${GREEN}yum repos has been changed!${END} || ${RED}yum repos change failed!!!${END}
fi
}
#9. 安装常用软件
install_app(){
if [ -f /etc/os-release ]; then
if [ "${DIS_VERSION}" = Ubuntu ]; then
apt -y install vim curl wget bash-completion net-tools lrzsz lsof tcpdump psmisc &> /dev/null && ${GREEN}install successfully!${END} || ${RED}install failed!${END}
elif [ ${OS_VERSION} -eq 7 ]; then
yum -y install vim-enhanced tcpdump lrzsz tree telnet bash-completion net-tools wget bzip2 lsof tmux man-pages zip unzip gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel &> /dev/null && ${GREEN}install successfully!${END} || ${RED}install failed!${END}
else
yum -y install vim-enhanced tcpdump autofs lrzsz tree telnet ftp lftp redhat-lsb-core bash-completion net-tools postfix wget bzip2 lsof mlocate tmux man-pages &> /dev/null && ${GREEN}install successfully!${END} || ${RED}install failed!${END}
fi
else
yum -y install vim-enhanced tcpdump lrzsz tree telnet net-tools wget bzip2 lsof man-pages zip unzip gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel &> /dev/null && ${GREEN}install successfully!${END} || ${RED}install failed!${END}
fi
}
#10. 创建别名
create_alias(){
if [ "${DIR_VERSION}" = Ubuntu ];then
net_file=`ls /etc/netplan/`
echo "alias apti='apt -y install'" >> /root/.bashrc
echo alias vinet="vim $net_file" >> /root/.bashrc
echo "alias scandisk='echo - - - > /sys/class/scsi_host/host0/scan;echo - - - > /sys/class/scsi_host/host1/scan;echo - - - > /sys/class/scsi_host/host2/scan'" >> /root/.bashrc
else
echo "alias vinet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'" >> /root/.bashrc
echo "alias yumi='yum -y install'" >> /root/.bashrc
echo "alias yump='yum provides'" >> /root/.bashrc
echo "alias scandisk='echo - - - > /sys/class/scsi_host/host0/scan;echo - - - > /sys/class/scsi_host/host1/scan;echo - - - > /sys/class/scsi_host/host2/scan'" >> /root/.bashrc
fi
}
#开始
while true;do
cat <<EOF
0. 全来
1. 修改主机名称
2. 修改命令提示符
3. 修改vim格式
4. 修改PATH变量
5. 修改网卡名称为eth0
6. 关闭和禁用防火墙
7. 禁用SElinux
8. 制作阿里云yum源
9. 安装常用软件
10. 创建别名
11. 退出
EOF
read -p "请选择你想要的操作(0-10): " INPUT
case $INPUT in
0)
Setup_Hostname
CMD_PROMPT
vim_style
Setup_PATH
change_eth0
disable_firewall
Disable_SElinux
mirrors_sources
install_app
create_alias
;;
1)
Setup_Hostname
;;
2)
CMD_PROMPT
;;
3)
vim_style
;;
4)
Setup_PATH
;;
5)
change_eth0
;;
6)
disable_firewall
;;
7)
Disable_SElinux
;;
8)
mirrors_sources
;;
9)
install_app
;;
10)
create_alias
;;
11)
break
;;
*)
${RED}请输入正确的数字${END}
esac
done
网友评论