首页 > 系统相关 >Linux初始化脚本

Linux初始化脚本

时间:2022-08-17 19:45:08浏览次数:55  
标签:脚本 初始化 set SETCOLOR color echo etc yum Linux

#!/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

相关文章

  • 如何在Windows 10上使用Kali Linux应用程序
    Linux操作系统在一些企业中越来越受欢迎。有许多发行版,包括Mint、Ubuntu和Kali。Kali是最著名的Linux发行版之一,渗透测试人员通常使用它。KaliLinux配备了一系列免费的开......
  • Unity获取脚本的CustomEditor(自定义编辑)数据
    在此之前,粗略的介绍下CustomEditor(自定义编辑)。Unity对于我们创建的Mono脚本提供了属性面板的展示和修改。默认情况下,Inspector面板中会显示当前脚本类的公开字段(publi......
  • Linux-进程管理(1)Linux进程睡眠状态 disk sleep (kill -9 杀不掉D状态进程)
    参考文档Linux进程睡眠状态disksleep1.Linux进程状态Running(R):运行或将要运行Interruptible(S):被阻断而等待一个事件,可能会被一个信号**Uninterruptible(D):被阻......
  • linux 中awk命令跳出外层循环的方法
     001、(base)root@PC1:/home/test4#lsa.txt(base)root@PC1:/home/test4#cata.txt##测试数据##1##2##34i6y#kkmma97......
  • 在linux服务器上搭建FTP服务器
    一、在Linux服务器上安装vsftpyum-yinstallvsftpd二、编辑vsftp.conf文件 参数作用listen=[YES|NO]是否以独立运......
  • linux常用命令
    linux常用基础命令目录环境绝对路径、相对路径绝对路径路径的全称:C:\ProgramFiles\InternetExplorer命令用法:命令空格参数空格文件cd:切换目录cd./:当前目......
  • Rocky Linux8升级9随记
    发现RockyLinux已经升级了9.0版本,看着自己用着的8.5版本,跃跃欲试,于是就索性升级了。两者的支持年限没有太大的差别,先说我的想法:升不升级无所谓。并不是9.0有什么特别牛......
  • linux 中sed命令 P和p的区别
     001:p:输出缓冲区中的所有内容。P:输出缓冲区中一个个换行符之前的内容(base)root@PC1:/home/test4#lsa.txt(base)root@PC1:/home/test4#cata.txt12345......
  • Linux同步网络时间
    CentOS服务器1、获取当前系统时间#date2、安装ntp#yum-yinstallntp3、修改时区#ln-sf/usr/share/zoneinfo/Asia/Shanghai/etc/localtime4、同步网络时间#ntpd......
  • linux系统目录及作用
    /binbin是binary的缩写。这个目录沿袭了UNIX系统的结构,存放着使用者最经常使用的命令。例如cp、ls、cat等等。/boot这里存放的是启动Linux时使用的一些核心文件。/devdev是......