#!/bin/bash #******************************************************************** #Author: HEhandsome #QQ: 2700565402 #Date: 2022-07-01 #FileName: csh_v7.0.sh #email: [email protected] #Description: 路漫漫其修远兮,吾将上下而求索 #******************************************************************** . /etc/os-release 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 } disable_firewalld(){ systemctl disable --now firewalld color "防火墙关闭成功 " 0 } disable_selinux(){ sed -i s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config color "SELINUX关闭完成" 0 reboot } install_software(){ yum -y install bash-completion psmisc lrzsz tree man-pages redhat-lsb-core wget tcpdump ftp rsync vim lsof net-tools iproute git } set_yum_rocky8(){ [ ! -d /data/backup ] && mkdir -p /data/backup mv /etc/yum.repos.d/* /data/backup mkdir /mnt/cdrom mount /dev/sr0 /mnt/cdrom cat > /etc/yum.repos.d/base.repo <<EOF [BaseOS] name=mnt/cdrom baseurl=file:///mnt/cdrom/BaseOS https://mirrors.aliyun.com/rockylinux/\$releasever/BaseOS/x86_64/os/ http://mirrors.163.com/rocky/\$releasever/BaseOS/x86_64/os/ https://mirrors.nju.edu.cn/rocky/\$releasever/BaseOS/x86_64/os/ https://mirrors.sjtug.sjtu.edu.cn/rocky/\$releasever/BaseOS/x86_64/os/ http://mirrors.sdu.edu.cn/rocky/\$releasever/BaseOS/x86_64/os/ gpgcheck=0 [AppStream] name=mnt/cdrom baseurl=file:///mnt/cdrom/AppStream https://mirrors.aliyun.com/rockylinux/\$releasever/AppStream/x86_64/os/ http://mirrors.163.com/rocky/\$releasever/AppStream/x86_64/os/ https://mirrors.nju.edu.cn/rocky/\$releasever/AppStream/x86_64/os/ https://mirrors.sjtug.sjtu.edu.cn/rocky/\$releasever/AppStream/x86_64/os/ http://mirrors.sdu.edu.cn/rocky/\$releasever/AppStream/x86_64/os/ gpgcheck=0 [extras] name=extras baseurl=https://mirrors.aliyun.com/rockylinux/\$releasever/extras/x86_64/os http://mirrors.163.com/rocky/\$releasever/extras/x86_64/os https://mirrors.nju.edu.cn/rocky/\$releasever/extras/x86_64/os https://mirrors.sjtug.sjtu.edu.cn/rocky/\$releasever/extras/x86_64/os http://mirrors.sdu.edu.cn/rocky/\$releasever/extras/x86_64/os gpgcheck=0 EOF yum clean all yum makecache yum repolist if [ $? -eq 0 ];then color "yum源配置完毕 " 0 else color "yum源配置失败 " 1 exit fi cat >> /etc/fstab << EOF /dev/sr0 /mnt/cdrom iso9660 defaults 0 0 EOF } set_epel_rocky8(){ cat > /etc/yum.repos.d/epel.repo <<-EOF [epel] name=epel baseurl=https://mirrors.aliyun.com/epel/\$releasever/Everything/x86_64/ https://mirror.tuna.tsinghua.edu.cn/epel/\$releasever/Everything/x86_64/ https://mirrors.cloud.tencent.com/epel/\$releasever/Everything/x86_64/ https://mirrors.huaweicloud.com/epel/\$releasever/Everything/x86_64/ gpgcheck=0 EOF dnf clean all &> /dev/null dnf repolist &> /dev/null if [ $? -eq 0 ];then color "EPEL源设置完成!" 0 else color "EPEL源设置失败!" 1 fi } set_yum_centos7(){ [ ! -d /data/bak ] && mkdir -p /data/bak mv /etc/yum.repos.d/* /data/bak mkdir /mnt/cdrom mount /dev/sr0 /mnt/cdrom cat > /etc/yum.repos.d/base.repo <<EOF [BaseOS] name=mnt/cdrom baseurl=file:///mnt/cdrom https://mirrors.aliyun.com/centos/\$releasever/os/x86_64/ gpgcheck=0 [AppStream] name=mnt/cdrom baseurl=file:///mnt/cdrom gpgcheck=0 [extras] name=extras baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/x86_64/ gpgcheck=0 EOF yum clean all yum makecache yum repolist if [ $? -eq 0 ];then color "yum源配置完毕 " 0 else color "yum源配置失败 " 0 exit fi } set_epel_centos7(){ cat > /etc/yum.repos.d/epel.repo <<-EOF [epel] name=epel baseurl=https://mirrors.aliyun.com/epel/\$releasever/x86_64/ gpgcheck=0 EOF yum clean all &> /dev/null yum repolist &> /dev/null if [ $? -eq 0 ];then color "EPEL源设置完成!" 0 else color "EPEL源设置失败!" 1 exit fi } revise_IP(){ read -p "输入网卡名: " NET read -p "输入ip: " IP cat > /etc/sysconfig/network-scripts/ifcfg-$NET <<EOF NAME=$NET DEVICE=$NET IPADDR=$IP BOOTPROTO=none PREFIX=24 GATEWAY=10.0.0.2 ONBOOT=yes DNS1=10.0.0.2 DNS2=8.8.8.8 EOF nmcli con reload nmcli con up $NET if [ $? -eq 0 ];then color "网卡配置完成 " 0 else color "网卡配置失败 " 1 exit fi } set_alias(){ cat >> ~/.bashrc <<EOF alias cdnet="cd /etc/sysconfig/network-scripts/" EOF if [ $? -eq 0 ];then color "别名修改成功 " 0 else color "别名修改失败 " 1 exit fi } set_PS1(){ if [ $ID = 'centos' -o $ID = 'rocky' ];then echo "PS1='\[\e[1;36m\][\[\e[34m\]\u\[\e[35m\]@\[\e[32m\]\h\[\e[31m\]\W\[\e[36m\]]\\$\[\e[0m\]'" >> /etc/bashrc exec bash else echo "PS1='\[\e[1;35m\][\[\e[35m\]\u\[\e[35m\]@\[\e[35m\]\h \[\e[36m\]\W\[\e[35m\]]\\$\[\e[0m\]'" >> ~/.bashrc exec bash fi } set_host(){ read -p "请输入主机名: " HOST hostnamectl set-hostname $HOST exec bash color "修改完成" 0 } set_netname1(){ sed -i '/^GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub grub2-mkconfig -o /etc/grub2.cfg if [ $? -eq 0 ];then color "配置完成,重启生效" 0 else color "配置失败" 1 fi } set_netname2(){ sed -i '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub grub-mkconfig -o /boot/grub/grub.cfg if [ $? -eq 0 ];then color "配置完成,重启生效" 0 else color "配置失败" 1 fi } set_vim(){ cat >>~/.vimrc<<EOF set ignorecase 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,"#Author: HE-handsome") call setline(4,"#QQ: 2700565402") call setline(5,"#Date: ".strftime("%Y-%m-%d")) call setline(6,"#FileName: ".expand("%")) call setline(7,"#email: [email protected]") call setline(8,"#Description:路漫漫其修远兮,吾将上下而求索") call setline(9,"#********************************************************************") call setline(10,"") endif endfunc autocmd BufNewFile * normal G EOF color "设置完成" 0 } ntp(){ rm -f /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime echo `date` } install_MySQL(){ yum -y install mysql-server mysql || systemctl start mysqld systemctl status mysqld echo -e "修改二进制日志位置" sleep 2 mkdir -p /data/mysql/logbin chown -R mysql.mysql /data/mysql cat >> /etc/my.cnf <<EOF [mysqld] log_bin=/data/mysql/logbin/mysql-bin EOF color "修改成功" 0 } install_mariadb(){ yum -y insatll mariadb-server || systemctl start mariadb.service systemctl status mariadb.service echo -e "修改二进制日志位置" sleep 2 mkdir -p /data/mysql/logbin chown -R mysql.mysql /data/mysql cat >> /etc/my.cnf <<EOF [mysqld] log_bin=/data/mysql/logbin/mariadb-bin EOF color "修改二进制位置成功" 0 } set_all(){ while :;do echo -e "\E[$[RANDOM%7+31];1m" cat << EOF *************************************************************************** ***** 路漫漫其修远兮 ************************ *** 吾将上下而求索 ******************* ***** 初始化脚本菜单 ************************* ********** ************ ********** ************ ********** 1.关闭防火墙 ************ ********** 2.关闭selinux ************ ********** 3.安装初始化软件 ************ ********** 4.安装yum源适合rocky8.5 ************ ********** 5.安装yum源适合centos7 ************ ********** 6.修改网卡 ************ ********** 7.修改别名 ************ ********** 8.修改主机名颜色 ************ ********** 9.修改主机名 ************ ********** 10.修改网卡名为eth0适合redhat/centos/Rocky ************ ********** 11.修改网卡名为eth0适合ubuntu ************ ********** 12.安装yum_epel适合rocky8 ************ ********** 13.安装yum_epel适合centos7 ************ ********** 14.设置vim ************ ********** 15.时间同步 ************ ********** 0.退出 ************ ********** ************ *************************************************************************** EOF echo -e "\E[0m" read -p "$(echo -e '\e[1;36m 请输入序号: \e[0m')" Menu case $Menu in 1) disable_firewalld ;; 2) disable_selinux ;; 3) install_software ;; 4) set_yum_rocky8 ;; 5) set_yum_centos7 ;; 6) revise_IP ;; 7) set_alias ;; 8) set_PS1 ;; 9) set_host ;; 10) set_netname1 ;; 11) set_netname2 ;; 12) set_epel_rocky8 ;; 13) set_epel_centos7 ;; 14) set_vim ;; 15) ntp ;; 0) exit ;; esac done } install_service(){ while :;do echo -e "\E[$[RANDOM%7+31];1m" cat << EOF ***************************************************** *** 路漫漫其修远兮 *********** ****** 吾将上下而求 索 ***** ******** 初始化linux脚本 *********** ******** *********** ******** 1.安装MySQL *********** ******** 2.安装mariadb *********** ******** 3.安装dns *********** ******** 0.退出 *********** ******** *********** ***************************************************** EOF echo -e "\E[0m" read -p "$(echo -e '\e[1;36m 请输入序号: \e[0m')" Menu case $Menu in 1) install_MySQL ;; 2) install_mariadb ;; 3) install_dns ;; 0) exit ;; esac done } while :;do echo -e "\E[$[RANDOM%7+31];1m" cat << EOF ***************************************************** *** 路漫漫其修远兮 *********** ****** 吾将上下而求索 ***** ********* 初始化linux脚本 *********** ********* *********** ********* 1.系统设置 *********** ********* 2.安装服务 *********** ********* 0.退出 *********** ********* *********** ***************************************************** EOF echo -e "\E[0m" read -p "$(echo -e '\e[1;34m 输入选项: \e[0m')" option case $option in 1) set_all ;; 2) install_service ;; 0) break ;; esac done
标签:脚本,初始化,set,SETCOLOR,color,echo,etc,yum,Linux From: https://www.cnblogs.com/smlience/p/16596535.html