#!/bin/bash
#
#********************************************************************
#Author: Wuvikr
#QQ: 744123155
#Date: 2020-09-30
#FileName? init_for_centos_or_ubuntu.sh
#URL: http://www.wuvikr.top
#Description? The init script
#Copyright (C): 2020 All rights reserved
#********************************************************************
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
trap "echo -e 'e[1;36mThank you for using, goodbye !e[0m'" EXIT
trap "echo -e 'e[1;31m该脚本已经禁用ctrl + c,请按 0 正常退出,谢谢配合 !e[0m'" SIGINT
LinuxVersion=`awk -F '=|"' '/^NAME/{print $3}' /etc/os-release`
if [ "$LinuxVersion" = 'Ubuntu' ];then
Version_ID=`cat /etc/os-release | sed -rn '/VERSION_ID/s#^[^0-9]+(.*)"$#1#p'`
echo "您的机器Linux发行版是Ubuntu $Version_ID!"
if [ "$Version_ID" = '18.04' ];then
apt_source=bionic
net_file=50-cloud-init.yaml
else
apt_source=focal
net_file=00-installer-config.yaml
fi
else
LinuxVersion='CentOS Linux'
Version_ID=`sed -rn 's/^[^0-9]+(.)..*/1/p' /etc/redhat-release`
echo "您的机器Linux发行版是CentOS Linux $Version_ID!"
fi
green='echo -e e[1;32m'
end='e[0m'
#关闭SELinux
disable_selinux(){
sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
${green}SElinux已经禁用,重新启动后生效!$end
}
#关闭防火墙
disable_firewalld(){
if [ "$LinuxVersion" = 'Ubuntu' ];then
ufw disable &> /dev/null
elif [ "$Version_ID" != 6 ];then
systemctl disable --now firewalld &> /dev/null
else
chkconfig iptables off
fi
${green}防火墙已经禁用!$end
}
#修改命令提示符颜色
set_ps1(){
echo -e 'e[1;31m1.红色e[0m'
echo -e 'e[1;32m2.绿色e[0m'
echo -e 'e[1;33m3.黄色e[0m'
echo -e 'e[1;34m4.蓝色e[0m'
echo -e 'e[1;35m5.紫色e[0m'
echo -e 'e[1;36m6.青色e[0m'
echo -e 'e[1;37m7.白色e[0m'
read -p "请选择你想要的颜色[1-7]" PS1_color
if [ "$LinuxVersion" = 'Ubuntu' ];then
sed -ri "/\:\w/s#(^[^]+).*#1\[\e[1;3${PS1_color}m\][\u@\h:\W]\\$ \[\e[0m\]'#" /root/.bashrc
else
echo "PS1='[e[1;3${PS1_color}m][u@h W]\$[e[0m]'" >> /etc/profile.d/env.sh
fi
${green}提示符已经修改成功,重新登录后生效!$end
}
#修改网卡名
set_eth(){
if [ "$LinuxVersion" = 'Ubuntu' ];then
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
elif [ "$Version_ID" != 6 ];then
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-* /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
fi
${green}网卡名称已经修改成功,重新启动后生效!$end
}
#安装常用软件
install_soft(){
if [ "$LinuxVersion" = 'CentOS Linux' ];then
echo -e 'e[1;33m正在安装,请稍候...e[0m'
yum -y install vim curl tree wget bash-completion net-tools lrzsz lsof zip unzip autofs rsync tcpdump psmisc lbzip2 man-pages chrony &> /dev/null && ${green}vim curl tree wget bash-completion net-tools lrzsz lsof zip unzip autofs rsync tcpdump psmisc lbzip2 man-pages chrony 已经安装成功! $end
else
echo -e 'e[1;33m正在安装,请稍候...e[0m'
apt -y install vim curl tree wget bash-completion net-tools lrzsz lsof zip unzip autofs rsync tcpdump psmisc lbzip2 chrony &> /dev/null && ${green}vim curl tree wget bash-completion net-tools lrzsz lsof zip unzip autofs rsync tcpdump psmisc lbzip2 chrony 已经安装成功!$end
fi
}
#添加常用别名
add_alias(){
if [ "$LinuxVersion" = 'CentOS Linux' ];then
echo >> /etc/profile.d/env.sh
echo "alias vinet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'" >> /etc/profile.d/env.sh
echo "alias yumi='yum -y install'" >> /etc/profile.d/env.sh
echo "alias yump='yum provides'" >> /etc/profile.d/env.sh
echo "alias scandisk='echo - - - > /sys/class/scsi_host/host0/scan;echo - - - > /sys/class/scsi_host/host1/scan;echo - - - > /sys/class/scsi_host/host2/scan'" >> /etc/profile.d/env.sh
else
echo "alias apti='apt -y install'" >> /etc/profile.d/env.sh
echo "alias vinet='vim /etc/netplan/$net_file'" >> /etc/profile.d/env.sh
echo "alias scandisk='echo - - - > /sys/class/scsi_host/host0/scan;echo - - - > /sys/class/scsi_host/host1/scan;echo - - - > /sys/class/scsi_host/host2/scan'" >> /etc/profile.d/env.sh
fi
${green}别名添加成功,重新登录后生效!$end
}
#修改镜像源
mirrors_sources(){
if [ "$LinuxVersion" = 'Ubuntu' ];then
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
elif [ "$Version_ID" = 6 ];then
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/CentOS6.repo <<EOF
[base]
name=CentOS6-base
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=CentOS6-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=CentOS6-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
elif [ "$Version_ID" = 7 ];then
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/CentOS7.repo <<EOF
[Packages]
name=Base
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
https://mirrors.huaweicloud.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/
https://mirrors.huaweicloud.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/
https://mirrors.huaweicloud.com/centos/7/extras/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
EOF
elif [ "$Version_ID" = 8 ];then
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
fi
${green}mirrors源修改成功$end
}
#修改vim格式
vim_style(){
cat > /root/.vimrc <<EOF
"保存.vimrc文件时自动重启加载,即让此文件立即生效
autocmd BufWritePost $MYVIMRC source $MYVIMRC
"TAB长度
set tabstop=2
"输入tab制表符时,自动替换成空格
set expandtab
"设置自动缩进长度为2空格
set shiftwidth=2
"设置行号
"set number
"语法高亮
syntax on
"高亮显示括号匹配
set showmatch
"忽略大小写
set ignorecase
"当前行显示下划线
set cursorline
"自动缩进
set autoindent
"搜索到最后匹配的位置后,再次搜索不回到第一个匹配处
set nowrapscan
""定义函数SetTitle,自动插入文件头
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: Wuvikr")
call setline(5,"#QQ: 744123155")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName ".expand("%"))
call setline(8,"#URL: http://www.wuvikr.top")
call setline(9,"#Description The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(11,"#********************************************************************")
call setline(12,"PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin")
call setline(13,"export PATH")
call setline(14,"")
endif
endfunc
"新建文件后,自动定位到文件末尾
autocmd BufNewFile * normal G
"自动补全括号引号
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {}<ESC>i
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
:inoremap < <><ESC>i
:inoremap > <c-r>=ClosePair('>')<CR>
function! ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "<Right>"
else
return a:char
endif
endfunction
EOF
echo "export EDITOR=vim" >> /etc/profile.d/env.sh
${green}vim格式修改成功!$end
}
#主程序入口
while true;do
cat <<EOF
1. 禁用SElinux
2. 关闭防火墙
3. 修改命令提示符
4. 修改网卡名
5. 安装常用软件
6. 添加常用别名
7. 修改mirrors源
8. 修改vim格式
9. 我全都要
0. 退出
EOF
read -p "请选择你想要的操作(0-9): "
case $REPLY in
1)
if [ "$LinuxVersion" = 'Ubuntu' ]
then
echo -e 'e[1;33m您的系统发行版本为Ubuntu,默认没有安装selinux,请重新选择其他操作!e[0m'
continue
fi
disable_selinux
;;
2)
disable_firewalld
;;
3)
set_ps1
;;
4)
if [ "$Version_ID" = 6 ];then
echo -e 'e[1;33m您的系统版本为CentOS 6,不需要修改网卡名,请重新选择其他操作!e[0m'
continue
fi
set_eth
;;
5)
install_soft
;;
6)
add_alias
;;
7)
mirrors_sources
;;
8)
vim_style
;;
9)
if [ "$LinuxVersion" = 'Ubuntu' ];then
disable_firewalld
set_ps1
set_eth
mirrors_sources
install_soft
add_alias
vim_style
else
disable_selinux
disable_firewalld
set_ps1
set_eth
mirrors_sources
install_soft
add_alias
vim_style
fi
;;
0)
break
;;
*)
echo -e "e[1;31m请输入正确的数字e[0m"
esac
done