1 Centos
#!/bin/bash
#
#********************************************************************
#Author:zhangzhuo
#QQ: 1191400158
#Date: 2020-12-18
#FileName:reset_Centos_v3.sh
#URL: https://www.zhangzhuo.ltd
#Description:The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
centos_Version(){
Version=`grep -Eo "[0-9].[0-9]" /etc/redhat-release | tr '.' '%' | tr '\n' % |cut -d% -f 1`
#Version=`sed -nr 's/.* ([0-9])\..*/\1/p' /etc/redhat-release`
}
centos_PS1(){
grep PS1 ~/.bash_profile &>/dev/null
if [ $? = 1 ];then
echo 'export PS1="\[\e[1;32m\][\[\e[0m\]\t \[\e[1;33m\]\u\[\e[36m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$"' >>~/.bash_profile
echo -e "\033[1;31m1)提示符配置已经完成请使用su -命令进行生效\033[0m"
else
echo -e "\033[1;31m1)提示符已经配置无需重新配置\033[0m"
fi
}
centos_hostname(){
centos_Version
num=0
while [ $num -ne 1 -a $num -ne 2 ];do
read -p "您是否要自己设置主机名?(1是2否):" num
if [ $num = 1 ];then
read -p "请输入主机名: " hostname
case ${Version} in
8|7)
hostnamectl set-hostname $hostname
echo -e "\033[1;31m2)主机名设置完成\033[0m,\033[1;32m请使用su -命令进行生效\033[0m"
;;
6)
sed -i "/H/s@\(^H.*=\).*$@\1${hostname}" /etc/sysconfig/network
echo -e "\033[1;31m2)主机名设置完成\033[0m,\033[1;32m请重新启动系统\033[0m"
;;
*)
echo -e "\033[1;31m2,3)您的系统不是Centos6, 7, 8!!!\033[0m"
esac
elif [ $num = 2 ];then
case ${Version} in
8|7)
hostnamectl set-hostname centos8
if [ $Version = 7 ];then
hostnamectl set-hostname centos7
fi
echo -e "\033[1;31m2)主机名设置完成\033[0m,\033[1;32m请使用su -命令进行生效\033[0m"
;;
6)
sed -i '/H/s@\(^H.*=\).*$@\1centos6@' /etc/sysconfig/network
echo -e "\033[1;31m2)主机名设置完成\033[0m,\033[1;32m请重新启动系统\033[0m"
;;
*)
echo -e "\033[1;31m2,3)您的系统不是Centos6, 7, 8!!!\033[0m"
esac
else
echo "选择错误请重新选择!!!"
fi
done
}
centos_firewalld(){
centos_Version
case ${Version} in
8|7)
systemctl stop firewalld
systemctl disable firewalld &>/dev/null
echo -e "\033[1;31m3)防火墙关闭完成\033[0m"
;;
6)
service iptables stop
chkconfig iptables off
echo -e "\033[1;31m3)防火墙关闭完成\033[0m"
;;
*)
echo -e "\033[1;31m2,3)您的系统不是Centos6, 7, 8!!!\033[0m"
esac
}
centos_selinux(){
sed -i '/SELINUX=/s@\(^S.*=\).*$@\1disabled@' /etc/selinux/config
echo -e "\033[1;31m3)Selinux关闭完成,请重新启动系统\033[0m"
}
centos_yum(){
ping -c2 baidu.com >/dev/null
if [ $? = 0 ];then
Version=`grep -Eo "[0-9].[0-9]" /etc/redhat-release | tr '.' '%' | tr '\n' % |cut -d% -f 1`
if [ $Version = 8 ];then
mkdir -p /etc/yum.repos.d/yum-Backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/yum-Backup/ &>/dev/null
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo &>/dev/null
echo -e "\033[1;31m4)阿里源设置完成\033[0m, \033[1;32m之前的配置文件存放在/etc/yum.repo.d/yum-Backup目录下\033[0m"
elif [ $Version = 7 ];then
mkdir -p /etc/yum.repos.d/yum-Backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/yum-Backup/ &>/dev/null
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
echo -e "\033[1;31m4)阿里源设置完成\033[0m, \033[1;32m之前的配置文件存放在/etc/yum.repo.d/yum-Backup目录下\033[0m"
elif [ $Version = 6 ];then
mkdir -p /etc/yum.repos.d/yum-Backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/yum-Backup/ &>/dev/null
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null
echo -e "\033[1;31m4)阿里源设置完成\033[0m, \033[1;32m之前的配置文件存放在/etc/yum.repo.d/yum-Backup目录下\033[0m"
fi
else
echo -e "\033[1;31m4)YUM配置失败,请检查网络连接\033[0m"
fi
}
centos_yum_install(){
yum install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump net-tools bc bzip2 zip unzip nfs-utils man-pages apr* redhat-rpm-config bash-completion bash-completion epel-release -y
echo -e "\033[1;31m5)常用软件安装完成\033[0m"
}
centos_vim(){
if [ -e /usr/bin/vim ];then
cat >~/.vimrc<<EOF
set ignorecase
set cursorline
set autoindent
set number
set paste
set et
set ts=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:zhangzhuo")
call setline(5,"#QQ: 1191400158")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName:".expand("%"))
call setline(8,"#URL: https://www.zhangzhuo.ltd")
call setline(9,"#Description:The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(11,"#********************************************************************")
call setline(12,"set -ue")
endif
endfunc
autocmd BufNewFile * normal G
EOF
echo -e "\033[1;31m6)VIM配置完成\033[0m"
else
echo -e "\033[1;34m6)警告:\033[0m\033[1;31m请先安装VIM软件\033[0m"
fi
}
centos_alias(){
cat <<EOF>>~/.bashrc
alias diskadd="echo '- - -' > /sys/class/scsi_host/host0/scan; echo '- - -' > /sys/class/scsi_host/host1/scan ; echo '- - -' > /sys/class/scsi_host/host2/scan"
alias df='df -h'
alias cdnet='cd /etc/sysconfig/network-scripts/'
alias free='free -h'
EOF
echo -e "\033[1;31m7)别名配置完成,\033[0m\033[1;32m 请使用source ~/.bashrc命令进行生\033[0m"
}
centos_net_name(){
grep 'net.ifnames=0 biosdevname=0' /etc/default/grub &>/dev/null
if [ $? = 1 ];then
sed -ri '/GRUB_CMDLINE_LINUX=/s#(")$# net.ifnames=0 biosdevname=0"#' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
mv /etc/sysconfig/network-scripts/ifcfg-`ip a | grep -Eo "[0-9]:.*e.*:" | tr -d ' ' | cut -d: -f2` /etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri '/NAME=/s#(".*")$#"eth0"#' /etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri '/DEVICE=/s#(".*")$#"eth0"#' /etc/sysconfig/network-scripts/ifcfg-eth0
echo -e '\e[1;31m网卡名已经修改完成,重启电脑生效!\e[0m'
else
echo -e '\e[1;31m网卡名已经修改无需重新修改!\e[0m'
fi
}
centos_net_ip(){
while :;do
read -p "请输入要设置的网卡名称: " NET_NAME
{ nmcli connection | grep -E "\b$NET_NAME\b" >/dev/null; } && break || echo "请重新输入正确且存在>的网卡名称!"
done
while :;do
read -p "请输入要修改的IP地址: " IP
{ echo $IP | grep -E "^(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$">/dev/null; } && break || echo "请输入正确的IP地址!"
done
while :;do
read -p "请输入子网掩码(0-32): " MASK
[ $MASK -le 32 ] && break || echo "输入错误请重新输入!"
done
while :;do
read -p "请输入网关: " GATEWAY
{ echo $GATEWAY | grep -E "^(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$">/dev/null; } && break || echo "请输入正确的网关!"
done
nmcli connection modify ${NET_NAME} ipv4.addresses ${IP}/${MASK} ipv4.gateway ${GATEWAY} ipv4.dns 114.114.114.114 && nmcli connection reload >/dev/null && nmcli connection up $NET_NAME >/dev/null && echo "网卡地址配置完成!" || echo "未知错误!请检查!"
}
mail_set(){
yum install -y postfix mailx &>/dev/null || echo "安装邮箱服务失败,请检查YUM源或网络!"
if [ -a /etc/mail.rc ];then
sed -i '$aset from=自己的邮箱账号' /etc/mail.rc
sed -i '$aset smtp=邮箱服务器域名' /etc/mail.rc
sed -i '$aset smtp-auth-user=自己的邮箱账号' /etc/mail.rc
sed -i '$aset smtp-auth-password=密码不是邮箱密码是授权码' /etc/mail.rc
systemctl restart postfix && echo "服务安装启动完成!" || echo "未知错误请检查!"
else
echo "/etc/mail.rc配置文件不存在请检查!"
fi
}
PS3="请选择相应的编号(1-14):"
MENU="
Centos设置终端提示符
CentOS设置主机名
CentOS关闭防火墙
CentOS关闭SLINUX
CentOS1-4全执行
CentOS设置YUN源(阿里源)
CentOS最小安装安装常用软件
Centos设置VIM
CentOS配置别名
CentOS重设网卡名称
CentOS设置网卡IP地址(如果执行完10请先重启在执行此项)
CentOS设置邮箱代理
重启系统
退出
"
select menu in $MENU;do
case $REPLY in
1)
centos_PS1
;;
2)
centos_hostname
;;
3)
centos_firewalld
;;
4)
centos_selinux
;;
5)
centos_PS1
centos_hostname
centos_firewalld
centos_selinux
;;
6)
centos_yum
;;
7)
centos_yum_install
;;
8)
centos_vim
;;
9)
centos_alias
;;
10)
centos_net_name
;;
11)
centos_net_ip
;;
12)
mail_set
;;
13)
init 6
;;
14)
break
;;
*)
echo -e "\e[1;31m输入错误,请输入正确的数字(1-14)!\e[0m"
;;
esac
done
实现的功能:
- 配置 vim 编辑器初始环境
- 初始化提示符
- 设置主机名称支持 centos6,7,8
- 关闭防火墙和 selinux,支持 centos6,7,8
- 设置 yum 源,阿里源支持 centos6,7,8
- 配置别名
- 安装常用软件
- 使用 root 运行不然会导致有些功能无法实现
2 Ubuntu
#!/bin/bash
#
#********************************************************************
#Author:zhangzhuo
#QQ: 1191400158
#Date: 2020-12-18
#FileName:reset_Ubtuntu_v3.sh
#URL: https://www.zhangzhuo.ltd
#Description:The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
ubuntu_version(){
VERSION=`sed -En '/^VERSION_ID=/s@.*"([0-9]+.[0-9]+)".*@\1@p' /etc/os-release`
}
ubuntu_PS1(){
grep PS1 ~/.profile &>/dev/null
if [ $? = 1 ];then
echo 'export PS1="\[\e[1;32m\][\[\e[0m\]\t \[\e[1;33m\]\u\[\e[36m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$"' >>~/.profile
echo -e "\033[1;31m终端提示符配置完成!\033[0m"
else
echo -e "\033[1;31m终端提示符已经配置无需重新配置!\033[0m"
fi
}
ubuntu_hostname(){
ubuntu_version
num=0
while [ $num -ne 1 -a $num -ne 2 ];do
read -p "您是否要自己设置主机名?(1是2否):" num
if [ $num = 1 ];then
read -p "请输入主机名: " hostname
case ${VERSION} in
20.04|18.04)
hostnamectl set-hostname $hostname
echo -e "\033[1;31m主机名设置完成\033[0m,\033[1;32m请使用su -命令进行生效\033[0m"
;;
*)
echo -e "\033[1;31m您的系统不是Ubuntu20.04 18.04!!!\033[0m"
esac
elif [ $num = 2 ];then
case ${VERSION} in
20.04|18.04)
hostnamectl set-hostname ubuntu20-04
if [ $VERSION = 18.04 ];then
hostnamectl set-hostname ubuntu18-04
fi
echo -e "\033[1;31m主机名设置完成\033[0m,\033[1;32m请使用su -命令进行生效\033[0m"
;;
*)
echo -e "\033[1;31m您的系统不是Ubuntu20.04 18.04!!!\033[0m"
esac
else
echo "选择错误请重新选择!!!"
fi
done
}
ubuntu_root(){
read -p "请输入root密码: " ROOTPASSWD
echo -e "${ROOTPASSWD}\n${ROOTPASSWD}" |sudo passwd root &>/dev/null
sed -Ei '/^#PermitRootLogin/s@^#(.*)\ .*@\1 yes@' /etc/ssh/sshd_config
systemctl restart sshd
echo -e '\e[1;31mroot密码已经设置完成,并且可以远程登录\e[0m'
}
ubuntu_apt(){
ping -c2 baidu.com &>/dev/null
if [ $? = 0 ];then
sed -Eni.bak '/^[^#]/s@(^.*\ )(.*)(/ubuntu.*)@\1http://mirrors.aliyun.com\3@p' /etc/apt/sources.list
apt update
echo -e '\e[1;31m阿里源设置完成! \e[0m'
else
sed -Eni.bak '/^[^#]/s@(^.*\ )(.*)(/ubuntu.*)@\1http://mirrors.aliyun.com\3@p' /etc/apt/sources.list
echo -e "\033[1;31m阿里源设置完成,但是请检查你的网络连接!\033[0m"
fi
}
ubuntu_vim(){
if [ -e /usr/bin/vim ];then
cat >~/.vimrc<<EOF
set ignorecase
set cursorline
set autoindent
set number
set paste
set et
set ts=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:zhangzhuo")
call setline(5,"#QQ: 1191400158")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName:".expand("%"))
call setline(8,"#URL: https://www.zhangzhuo.ltd")
call setline(9,"#Description:The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(11,"#********************************************************************")
call setline(12,"set -ue")
endif
endfunc
autocmd BufNewFile * normal G
EOF
echo -e "\033[1;31mVIM配置完成\033[0m"
else
echo -e "\033[1;34m警告:\033[0m\033[1;31请先安装VIM\033[0m"
fi
}
ubuntu_alias(){
cat <<EOF>>~/.bashrc
df='df -h'
EOF
echo -e "\033[1;31m别名配置完成,请使用source ~/.bashrc生效\033[0m"
}
ubuntu_install(){
apt install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc openssh-server iotop unzip zip
echo -e "\033[1;31m常用软件安装完成\033[0m"
}
PS3="请选择相应的编号(1-10):"
MENU="
Ubuntu设置终端提示符
Ubuntu配置主机名(需要以sudo执行脚本)
Ubuntu设置root密码并且允许root远程登录(需要以sudo执行脚本)
Ubuntu设置阿里源(需要以sudo执行脚本)
Ubuntu安装常用软件(需要以sudo执行脚本)
Ubuntu配置VIM
Ubuntu配置别名
执行1,6,7!
重启系统(需要以sudo执行脚本)
退出
"
select menu in $MENU;do
case $REPLY in
1)
ubuntu_PS1
;;
2)
ubuntu_hostname
;;
3)
ubuntu_root
;;
4)
ubuntu_apt
;;
5)
ubuntu_install
;;
6)
ubuntu_vim
;;
7)
ubuntu_alias
;;
8)
ubuntu_PS1
ubuntu_vim
ubuntu_alias
;;
9)
reboot
;;
10)
break
;;
*)
echo -e "\e[1;31m输入错误,请输入正确的数字(1-13)!\e[0m"
;;
esac
done
- 使用场景为第一次安装 Ubuntu 系统进行环境初始化,具体功能在代码里有详情,可根据自己需求二次修改
- 执行时请务必使用 sudo 命令执行脚本
- 执行前系统需拥有 openssh-server 服务也就是 sshd 服务
- 2004 版本设置完毕后提示符与别名在 root 家目录下,具体原因还在解决 1804 版本不存在此问题,可能是 sudo 版本不同导致。
- 支持版本为 1804 与 2004。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于