Rocky、Almalinux、CentOS、Ubuntu和Debian系统初始化脚本v9版

Rocky、Almalinux、CentOS、Ubuntu和Debian系统初始化脚本

在这里插入图片描述

Shell脚本源码地址:

Gitee:https://gitee.com/raymond9/shell
Github:https://github.com/raymond999999/shell

脚本可以去上面的Gitee或Github代码仓库拉取。

支持的功能和系统:

支持的功能支持的系统
修改网卡名、设置网络(包括设置IP地址、子网掩码位数、网关地址和DNS地址,包括单网卡和双网卡)、设置主机名、设置镜像仓库、Minimal安装建议安装软件、关闭防火墙、禁用SELinux、禁用SWAP、设置系统时区、优化资源限制参数、优化内核参数、优化SSH、更改SSH端口号、设置系统别名、设置vimrc配置文件、安装邮件服务并配置、设置PS1、设置默认文本编辑器为vim、设置history格式、禁用ctrl+alt+del重启、Ubuntu和Debian设置root用户登录、Ubuntu卸载无用软件包、Ubuntu卸载snap。Rocky 8和9、AlmaLinux 8和9、CentOS 7、CentOS Stream 8和9、Ubuntu 18.04/20.04/22.04/24.04、Debian 12

v1和v2版本的区别:

v1和v2版本实现的功能都是一样的,只是实现的方式不同。

v1v2
1.Rocky、Almalinux、CentOS系统修改ip地址采用nmcli命令方式;1.Rocky、Almalinux、CentOS系统修改ip地址采用的是配置文件方式;
2.Rocky、Almalinux、CentOS、Ubuntu和Debian系统修改镜像仓库采用sed直接替换网址方式;2.Rocky、Almalinux、CentOS、Ubuntu和Debian系统修改镜像仓库采用的是配置文件方式;
3.设置系统时区采用timedatectl命令方式。3.设置系统时区采用软链接方式。

版本更新日志:

版本功能
v9版更新内容1.由于CentOS Stream 8 已于 2024 年 5 月 31 日到期, CentOS Linux 7 的生命周期结束日期是 2024 年 6 月 30 日,将CentOS Stream 8和CentOS 7的镜像仓库都改成了centos-vault仓库;把CentOS 7的epel仓库改成了epel-archive仓库;
2.添加了对Ubuntu 24.04系统的支持;(Ubuntu 24.04的变更:网卡配置文件变成了“/etc/netplan/50-cloud-init.yaml”,镜像仓库格式变成了DEB822 格式,ssh服务的服务名变成了ssh;)
3.添加了对Debian 12系统的支持;
4.修改了某些bug。
v8版更新内容1.添加了对AlmaLinux 8和9系统的支持;
2.添加Ubuntu卸载snap的功能;
3.修改了某些bug。
v7版更新内容1.由于v6版修改的比较仓促,其中设置镜像仓库有bug,修复了其中的bug,而且设置镜像仓库可以重复修改;修复了设置ip不能成功的bug;优化了设置系统别名的bug;修复了“优化内核参数”的bug;
2.分别有reset_v7_1版本(镜像仓库采用sed直接替换网址方式;修改ip地址采用nmcli命令方式)和reset_v7_2版本(镜像仓库和修改ip地址采用配置文件方式)。
v6版更新内容1.由于CentOS 6和8官方已经停止支持,也就移除了其相关内容;
2.分别有reset_v6_1版本(镜像仓库采用sed直接替换网址方式;修改ip地址采用nmcli命令方式)和reset_v6_2版本(镜像仓库和修改ip地址采用配置文件方式);
3.reset_v6_1添加了CentOS Stream 9用Perl语言更改镜像源的方法,优化了某些镜像仓库失效的bug,修改了某些bug。
v5版更新内容1.优化了某些镜像仓库失效的bug;
2.CentOS stream 9和Rocky 9修改ip的方式更改,做了相应的修改;
3.分别有reset_v5_1版本(镜像仓库采用sed直接替换网址方式;修改ip地址采用nmcli命令方式)和reset_v5_2版本(镜像仓库和修改ip地址采用配置文件方式);
4.把设置PS1、设置默认文本编辑器为vim和设置history格式单独分开;
5.修改了某些bug。
v4版更新内容1.添加对CentOS stream 9、Rocky 9和Ubuntu 22.04系统的支持;
2.添加Ubuntu 22.04修改IP地址和网关地址、双网卡更改IP地址;
3.添加禁用ctrl+alt+del重启功能;
4.修改了某些bug。
v3版更新内容1.添加双网卡更改IP地址;
2.添加设置系统时区。
v2版更新内容1.添加对CentOS stream 8系统支持,添加了CentOS stream 8镜像仓库;
2.由于CentOS 8已被废弃,修改成centos-vault的历史镜像仓库;
3.优化Ubuntu 20.04禁用swap不生效的问题。
v1版支持功能1.支持CentOS 6/7/8、Ubuntu 18.04/20.04、Rocky 8系统;
2.支持功能禁用SELinux、关闭防火墙、优化SSH、设置系统别名、设置vimrc配置文件、设置软件包仓库、Minimal安装建议安装软件、安装邮件服务并配置邮件、更改SSH端口号、修改网卡名、修改IP地址和网关地址、设置主机名、设置PS1和系统环境变量、禁用SWAP、优化内核参数、优化资源限制参数、Ubuntu设置root用户登录、Ubuntu卸载无用软件包。

reset脚本在使用过程中需要注意的事项:

reset脚本在使用过程中需要注意的事项:

  1. 首先说明,脚本必须在root用户下使用。

    # Rocky、Almalinux、和CentOS默认可以使用root用户登录不用设置,Ubuntu和Debian必须先设置root用户登录。
    # 先安装lrzsz工具,把脚本传上去
    raymond@ubuntu2404:~$ sudo apt -y install lrzsz
    raymond@ubuntu2404:~$ rz -E
    rz waiting to receive.# 使用bash命令运行脚本
    raymond@ubuntu2404:~$ bash reset_v9_1.sh ******************************************************************
    *                     系统初始化脚本菜单                         *
    * 1.修改网卡名                 15.设置系统别名                   *
    * 2.设置网络(单网卡)           16.设置vimrc配置文件              *
    * 3.设置网络(双网卡)           17.安装邮件服务并配置邮件         *
    * 4.设置主机名                 18.设置PS1(请进入选择颜色)        *
    * 5.设置镜像仓库               19.设置默认文本编辑器为vim        *
    * 6.Minimal安装建议安装软件    20.设置history格式                *
    * 7.关闭防火墙                 21.禁用ctrl+alt+del重启           *
    * 8.禁用SELinux                22.Ubuntu和Debian设置root用户登录 *
    * 9.禁用SWAP                   23.Ubuntu卸载无用软件包           *
    * 10.设置系统时区              24.Ubuntu卸载snap                 *
    * 11.优化资源限制参数          25.重启系统                       *
    * 12.优化内核参数              26.关机                           *
    * 13.优化SSH                   27.退出                           *
    * 14.更改SSH端口号                                               *
    ******************************************************************请选择相应的编号(1-27): 22 # 输入22,设置root用户登录
    请输入密码: 123456 # 输入密码
    [sudo] password for raymond: New password: Retype new password: passwd: password updated successfully
    Ubuntu 24.04 root用户登录已设置完成,请重新登录后生效!******************************************************************
    *                     系统初始化脚本菜单                         *
    * 1.修改网卡名                 15.设置系统别名                   *
    * 2.设置网络(单网卡)           16.设置vimrc配置文件              *
    * 3.设置网络(双网卡)           17.安装邮件服务并配置邮件         *
    * 4.设置主机名                 18.设置PS1(请进入选择颜色)        *
    * 5.设置镜像仓库               19.设置默认文本编辑器为vim        *
    * 6.Minimal安装建议安装软件    20.设置history格式                *
    * 7.关闭防火墙                 21.禁用ctrl+alt+del重启           *
    * 8.禁用SELinux                22.Ubuntu和Debian设置root用户登录 *
    * 9.禁用SWAP                   23.Ubuntu卸载无用软件包           *
    * 10.设置系统时区              24.Ubuntu卸载snap                 *
    * 11.优化资源限制参数          25.重启系统                       *
    * 12.优化内核参数              26.关机                           *
    * 13.优化SSH                   27.退出                           *
    * 14.更改SSH端口号                                               *
    ******************************************************************请选择相应的编号(1-27): 27 # 退出脚本# 然后用root用户登录
    [C:\~]$ ssh root:123456@172.31.7.2# 把脚本从普通用户家目录移到root用户家目录,再继续后面步骤。
    root@ubuntu2404:~# mv /home/raymond/reset_v9_1.sh .
    
  2. CentOS Stream 9如果使用v1版本修改镜像源需要注意的地方。

    # 先安装lrzsz工具,把脚本传上去
    [root@centos9 ~]# dnf -y install lrzsz
    [root@centos9 ~]# rz -E
    rz waiting to receive.
    [root@centos9 ~]# ls
    anaconda-ks.cfg  reset_v9_1.sh[root@centos9 ~]# bash reset_v9_1.sh ******************************************************************
    *                     系统初始化脚本菜单                         *
    * 1.修改网卡名                 15.设置系统别名                   *
    * 2.设置网络(单网卡)           16.设置vimrc配置文件              *
    * 3.设置网络(双网卡)           17.安装邮件服务并配置邮件         *
    * 4.设置主机名                 18.设置PS1(请进入选择颜色)        *
    * 5.设置镜像仓库               19.设置默认文本编辑器为vim        *
    * 6.Minimal安装建议安装软件    20.设置history格式                *
    * 7.关闭防火墙                 21.禁用ctrl+alt+del重启           *
    * 8.禁用SELinux                22.Ubuntu和Debian设置root用户登录 *
    * 9.禁用SWAP                   23.Ubuntu卸载无用软件包           *
    * 10.设置系统时区              24.Ubuntu卸载snap                 *
    * 11.优化资源限制参数          25.重启系统                       *
    * 12.优化内核参数              26.关机                           *
    * 13.优化SSH                   27.退出                           *
    * 14.更改SSH端口号                                               *
    ******************************************************************请选择相应的编号(1-27): 51)base仓库
    2)epel仓库
    3)启用CentOS Stream 9 crb仓库
    4)启用CentOS Stream 8 PowerTools仓库
    5)退出请输入镜像源编号(1-5): 1
    由于CentOS Stream 9系统默认镜像源是Perl语言实现的,在更改镜像源之前先确保把'update_mirror.pl'文件和reset脚本放在同一个目录下,否则后面程序会退出,默认的CentOS Stream 9镜像源设置的是阿里云,要修改镜像源,请去'update_mirror.pl'文件里修改url变量!
    缺少update_mirror.pl文件 # 这里提示“缺少update_mirror.pl文件”,上面的提示也写得很清楚,需要把这个文件也传到系统里[root@centos9 ~]# rz -E
    rz waiting to receive.
    [root@centos9 ~]# ls
    anaconda-ks.cfg  reset_v9_1.sh  update_mirror.pl[root@centos9 ~]# bash reset_v9_1.sh ******************************************************************
    *                     系统初始化脚本菜单                         *
    * 1.修改网卡名                 15.设置系统别名                   *
    * 2.设置网络(单网卡)           16.设置vimrc配置文件              *
    * 3.设置网络(双网卡)           17.安装邮件服务并配置邮件         *
    * 4.设置主机名                 18.设置PS1(请进入选择颜色)        *
    * 5.设置镜像仓库               19.设置默认文本编辑器为vim        *
    * 6.Minimal安装建议安装软件    20.设置history格式                *
    * 7.关闭防火墙                 21.禁用ctrl+alt+del重启           *
    * 8.禁用SELinux                22.Ubuntu和Debian设置root用户登录 *
    * 9.禁用SWAP                   23.Ubuntu卸载无用软件包           *
    * 10.设置系统时区              24.Ubuntu卸载snap                 *
    * 11.优化资源限制参数          25.重启系统                       *
    * 12.优化内核参数              26.关机                           *
    * 13.优化SSH                   27.退出                           *
    * 14.更改SSH端口号                                               *
    ******************************************************************请选择相应的编号(1-27): 51)base仓库
    2)epel仓库
    3)启用CentOS Stream 9 crb仓库
    4)启用CentOS Stream 8 PowerTools仓库
    5)退出请输入镜像源编号(1-5): 1 # 选择设置base仓库
    由于CentOS Stream 9系统默认镜像源是Perl语言实现的,在更改镜像源之前先确保把'update_mirror.pl'文件和reset脚本放在同一个目录下,否则后面程序会退出,默认的CentOS Stream 9镜像源设置的是阿里云,要修改镜像源,请去'update_mirror.pl'文件里修改url变量!
    update_mirror.pl文件已准备好,继续后续配置!  # 现在这里提示“update_mirror.pl文件已准备好,继续后续配置!”。
    安装perl工具,请稍等...
    更新镜像源中,请稍等...
    CentOS 9 YUM源设置完成!1)base仓库
    2)epel仓库
    3)启用CentOS Stream 9 crb仓库
    4)启用CentOS Stream 8 PowerTools仓库
    5)退出请输入镜像源编号(1-5): 5 # 退出设置镜像仓库菜单******************************************************************
    *                     系统初始化脚本菜单                         *
    * 1.修改网卡名                 15.设置系统别名                   *
    * 2.设置网络(单网卡)           16.设置vimrc配置文件              *
    * 3.设置网络(双网卡)           17.安装邮件服务并配置邮件         *
    * 4.设置主机名                 18.设置PS1(请进入选择颜色)        *
    * 5.设置镜像仓库               19.设置默认文本编辑器为vim        *
    * 6.Minimal安装建议安装软件    20.设置history格式                *
    * 7.关闭防火墙                 21.禁用ctrl+alt+del重启           *
    * 8.禁用SELinux                22.Ubuntu和Debian设置root用户登录 *
    * 9.禁用SWAP                   23.Ubuntu卸载无用软件包           *
    * 10.设置系统时区              24.Ubuntu卸载snap                 *
    * 11.优化资源限制参数          25.重启系统                       *
    * 12.优化内核参数              26.关机                           *
    * 13.优化SSH                   27.退出                           *
    * 14.更改SSH端口号                                               *
    ******************************************************************请选择相应的编号(1-27): 27 # 退出脚本
    
  3. 其它功能根据需求选择,如果有需要输入的根据提示输入即可,这里不再一一演示。

下面是reset_v9_1脚本内容:

[root@rocky9 ~]# cat reset_v9_1.sh
#!/bin/bash
#
#**********************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2024-11-12
#FileName:      reset_v9_1.sh
#MIRROR:        raymond.blog.csdn.net
#Description:   The reset linux system initialization script supports 
#               “Rocky Linux 8 and 9, Almalinux 8 and 9, CentOS 7, 
#               CentOS Stream 8 and 9, Ubuntu 18.04, 20.04, 22.04 and 24.04, 
#               Debian 12“ operating systems.
#Copyright (C): 2024 All rights reserved
#**********************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'os(){OS_ID=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`OS_NAME=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+) (.*)"$@\2@p' /etc/os-release`OS_RELEASE=`sed -rn '/^VERSION_ID=/s@.*="?([0-9.]+)"?@\1@p' /etc/os-release`OS_RELEASE_VERSION=`sed -rn '/^VERSION_ID=/s@.*="?([0-9]+)\.?.*"?@\1@p' /etc/os-release`
}set_rocky_almalinux_centos_eth(){if [ ${OS_RELEASE_VERSION} == "7" -o ${OS_RELEASE_VERSION} == "8" ];thenETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`if grep -Eqi "(net\.ifnames|biosdevname)" /etc/default/grub;then${COLOR}"${OS_ID} ${OS_RELEASE} 网卡名配置文件已修改,不用修改!"${END}elsesed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0 biosdevname=0"@' /etc/default/grubgrub2-mkconfig -o /boot/grub2/grub.cfg >& /dev/nullmv /etc/sysconfig/network-scripts/ifcfg-${ETHNAME} /etc/sysconfig/network-scripts/ifcfg-eth0${COLOR}"${OS_ID} ${OS_RELEASE} 网卡名已修改成功,10秒后,机器会自动重启!"${END}sleep 10 && shutdown -r nowfi   else${COLOR}"${OS_ID} ${OS_RELEASE} 不能修改网卡名!"${END} fi
}set_ubuntu_debian_eth(){if grep -Eqi "(net\.ifnames|biosdevname)" /etc/default/grub;then${COLOR}"${OS_ID} ${OS_RELEASE} 网卡名配置文件已修改,不用修改!"${END}elsesed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@net.ifnames=0 biosdevname=0"@' /etc/default/grubgrub-mkconfig -o /boot/grub/grub.cfg >& /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} 网卡名已修改成功,请重新启动系统后才能生效!"${END}fi
}set_eth(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenset_rocky_almalinux_centos_ethelseset_ubuntu_debian_ethfi
}check_ip(){local IP=$1VALID_CHECK=$(echo ${IP}|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')if echo ${IP}|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; thenif [ ${VALID_CHECK} == "yes" ]; thenecho "IP ${IP}  available!"return 0elseecho "IP ${IP} not available!"return 1fielseecho "IP format error!"return 1fi
}set_rocky_almalinux_centos_network(){ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`CONNECTION_NAME=`nmcli dev | awk 'NR==2{print $4,$5,$6}'`while true; doread -p "请输入IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdone${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请使用新IP重新登录!"${END}if [ ${OS_RELEASE_VERSION} == "7" -o ${OS_RELEASE_VERSION} == "8" ];thennmcli connection modify "${CONNECTION_NAME}" con-name ${ETHNAME} && \nmcli connection delete ${ETHNAME} >& /dev/null && \nmcli connection add type ethernet con-name ${ETHNAME} ifname ${ETHNAME} ipv4.method manual ipv4.address "${IP}/${PREFIX}" ipv4.gateway "${GATEWAY}" ipv4.dns "${PRIMARY_DNS},${BACKUP_DNS}" autoconnect yes >& /dev/null && \nmcli con reload && nmcli dev up ${ETHNAME} >& /dev/nullelsenmcli connection delete ${ETHNAME} >& /dev/null && \nmcli connection add type ethernet con-name ${ETHNAME} ifname ${ETHNAME} ipv4.method manual ipv4.address "${IP}/${PREFIX}" ipv4.gateway "${GATEWAY}" ipv4.dns "${PRIMARY_DNS},${BACKUP_DNS}" autoconnect yes >& /dev/null && \nmcli con reload && nmcli dev up ${ETHNAME} >& /dev/nullfi
}set_ubuntu_network(){while true; doread -p "请输入IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdoneif [ ${OS_RELEASE_VERSION} == "18" ];thencat > /etc/netplan/01-netcfg.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] gateway4: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
EOFelif [ ${OS_RELEASE_VERSION} == "20" ];thencat > /etc/netplan/00-installer-config.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] gateway4: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
EOFelif [ ${OS_RELEASE_VERSION} == "22" ];thencat > /etc/netplan/00-installer-config.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}]routes:- to: defaultvia: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
EOFelsecat > /etc/netplan/50-cloud-init.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}]routes:- to: defaultvia: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
EOFfi    ${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请重新启动系统后生效!"${END}
}set_debian_network(){ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`while true; doread -p "请输入IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdonesed -ri -e "s/allow-hotplug ${ETHNAME}/auto eth0/g" -e "s/(iface) ${ETHNAME} (inet) dhcp/\1 eth0 \2 static/g" /etc/network/interfacescat >> /etc/network/interfaces <<-EOF
address ${IP}/${PREFIX}
gateway ${GATEWAY}
dns-nameservers ${PRIMARY_DNS} ${BACKUP_DNS}
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请重新启动系统后生效!"${END}
}set_network(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenset_rocky_almalinux_centos_networkelif [ ${OS_ID} == "Ubuntu" ];thenset_ubuntu_networkelseset_debian_networkfi
}set_dual_rocky_almalinux_centos_network(){ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`ETHNAME2=`ip addr | awk -F"[ :]" '/^3/{print $3}'`CONNECTION_NAME1=`nmcli dev | awk 'NR==2{print $4,$5,$6}'`CONNECTION_NAME2=`nmcli dev | awk 'NR==3{print $4,$5,$6}'`while true; doread -p "请输入第一块网卡IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入第二块网卡IP地址: " IP2check_ip ${IP2}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIX2${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请使用新IP重新登录!"${END}if [ ${OS_RELEASE_VERSION} == "7" -o ${OS_RELEASE_VERSION} == "8" ];thennmcli connection modify "${CONNECTION_NAME1}" con-name ${ETHNAME} && nmcli connection delete ${ETHNAME} >& /dev/null && \nmcli connection add type ethernet con-name ${ETHNAME} ifname ${ETHNAME} ipv4.method manual ipv4.address "${IP}/${PREFIX}" ipv4.gateway "${GATEWAY}" ipv4.dns "${PRIMARY_DNS},${BACKUP_DNS}" autoconnect yes >& /dev/null && \nmcli connection modify "${CONNECTION_NAME2}" con-name ${ETHNAME2} && nmcli connection delete ${ETHNAME2} >& /dev/null && \nmcli connection add type ethernet con-name ${ETHNAME2} ifname ${ETHNAME2} ipv4.method manual ipv4.address "${IP2}/${PREFIX2}" autoconnect yes >& /dev/null && \nmcli con reload && nmcli dev up ${ETHNAME} ${ETHNAME2} >& /dev/nullelsenmcli connection delete ${ETHNAME} >& /dev/null && \nmcli connection add type ethernet con-name ${ETHNAME} ifname ${ETHNAME} ipv4.method manual ipv4.address "${IP}/${PREFIX}" ipv4.gateway "${GATEWAY}" ipv4.dns "${PRIMARY_DNS},${BACKUP_DNS}" autoconnect yes >& /dev/null && \nmcli connection modify "${CONNECTION_NAME2}" con-name ${ETHNAME2} && nmcli connection delete ${ETHNAME2} >& /dev/null && \nmcli connection add type ethernet con-name ${ETHNAME2} ifname ${ETHNAME2} ipv4.method manual ipv4.address "${IP2}/${PREFIX2}" autoconnect yes >& /dev/null && \nmcli con reload && nmcli dev up ${ETHNAME} ${ETHNAME2} >& /dev/nullfi
}set_dual_ubuntu_network(){while true; doread -p "请输入第一块网卡IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入第二块网卡IP地址: " IP2check_ip ${IP2}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIX2if [ ${OS_RELEASE_VERSION} == "18" ];thencat > /etc/netplan/01-netcfg.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] gateway4: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]eth1:dhcp4: nodhcp6: noaddresses: [${IP2}/${PREFIX2}] 
EOFelif [ ${OS_RELEASE_VERSION} == "20" ];thencat > /etc/netplan/00-installer-config.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] gateway4: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]eth1:dhcp4: nodhcp6: noaddresses: [${IP2}/${PREFIX2}] 
EOFelif [ ${OS_RELEASE_VERSION} == "22" ];thencat > /etc/netplan/00-installer-config.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] routes:- to: defaultvia: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]eth1:dhcp4: nodhcp6: noaddresses: [${IP2}/${PREFIX2}] 
EOFelsecat > /etc/netplan/50-cloud-init.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] routes:- to: defaultvia: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]eth1:dhcp4: nodhcp6: noaddresses: [${IP2}/${PREFIX2}] 
EOFfi${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请重新启动系统后生效!"${END}
}set_dual_debian_network(){ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`while true; doread -p "请输入第一块网卡IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入第二块网卡IP地址: " IP2check_ip ${IP2}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIX2sed -ri -e "s/allow-hotplug ${ETHNAME}/auto eth0/g" -e "s/(iface) ${ETHNAME} (inet) dhcp/\1 eth0 \2 static/g" /etc/network/interfacescat >> /etc/network/interfaces <<-EOF
address ${IP}/${PREFIX}
gateway ${GATEWAY}
dns-nameservers ${PRIMARY_DNS} ${BACKUP_DNS}auto eth1
iface eth1 inet static
address ${IP2}/${PREFIX2}
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请重新启动系统后生效!"${END}
}set_dual_network(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenset_dual_rocky_almalinux_centos_networkelif [ ${OS_ID} == "Ubuntu" ];thenset_dual_ubuntu_networkelseset_dual_debian_networkfi
}set_hostname(){read -p "请输入主机名: " HOSThostnamectl set-hostname ${HOST}${COLOR}"${OS_ID} ${OS_RELEASE} 主机名设置成功,请重新登录生效!"${END}
}aliyun(){MIRROR=mirrors.aliyun.com
}huawei(){MIRROR=repo.huaweicloud.com
}tencent(){MIRROR=mirrors.tencent.com
}tuna(){MIRROR=mirrors.tuna.tsinghua.edu.cn
}netease(){MIRROR=mirrors.163.com
}sohu(){MIRROR=mirrors.sohu.com
}nju(){MIRROR=mirrors.nju.edu.cn
}ustc(){MIRROR=mirrors.ustc.edu.cn
}sjtu(){MIRROR=mirrors.sjtug.sjtu.edu.cn
}xjtu(){MIRROR=mirrors.xjtu.edu.cn
}bfsu(){MIRROR=mirrors.bfsu.edu.cn
}bjtu(){MIRROR=mirror.bjtu.edu.cn
}pku(){MIRROR=mirrors.pku.edu.cn
}archive_fedora(){MIRROR=archives.fedoraproject.org
}set_yum_rocky_8_9(){MIRROR_URL=`echo ${MIRROR} | awk -F"." '{print $2}'`OLD_MIRROR=$(sed -rn '/^.*baseurl=/s@.*=http.*://(.*)/(.*)/\$releasever/.*/$@\1@p' /etc/yum.repos.d/[Rr]ocky*.repo | head -1)OLD_DIR=$(sed -rn '/^.*baseurl=/s@.*=http.*://(.*)/(.*)/\$releasever/.*/$@\2@p' /etc/yum.repos.d/[Rr]ocky*.repo | head -1)if [ ${MIRROR_URL} == "aliyun" -o ${MIRROR_URL} == "xjtu" ];thenif [ ${OLD_DIR} == '$contentdir' ];thensed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://'${MIRROR}'/rockylinux|g' /etc/yum.repos.d/[Rr]ocky*.repoelif [ ${OLD_DIR} == 'rocky' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/rocky|baseurl=https://'${MIRROR}'/rockylinux|g' /etc/yum.repos.d/[Rr]ocky*.repoelif [ ${OLD_DIR} == 'Rocky' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/Rocky|baseurl=https://'${MIRROR}'/rockylinux|g' /etc/yum.repos.d/[Rr]ocky*.repoelsesed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/rockylinux|baseurl=https://'${MIRROR}'/rockylinux|g' /etc/yum.repos.d/[Rr]ocky*.repofielif [ ${MIRROR_URL} == "sohu" ];thenif [ ${OLD_DIR} == '$contentdir' ];thensed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://'${OLD_MIRROR}'/$contentdir|baseurl=https://'${MIRROR}'/Rocky|g' /etc/yum.repos.d/[Rr]ocky*.repoelif [ ${OLD_DIR} == 'rockylinux' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/rockylinux|baseurl=https://'${MIRROR}'/Rocky|g' /etc/yum.repos.d/[Rr]ocky*.repoelif [ ${OLD_DIR} == 'rocky' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/rocky|baseurl=https://'${MIRROR}'/Rocky|g' /etc/yum.repos.d/[Rr]ocky*.repo elsesed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/Rocky|baseurl=https://'${MIRROR}'/Rocky|g' /etc/yum.repos.d/[Rr]ocky*.repofi	elseif [ ${OLD_DIR} == '$contentdir' ];thensed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://'${OLD_MIRROR}'/$contentdir|baseurl=https://'${MIRROR}'/rocky|g' /etc/yum.repos.d/[Rr]ocky*.repoelif [ ${OLD_DIR} == 'rockylinux' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/rockylinux|baseurl=https://'${MIRROR}'/rocky|g' /etc/yum.repos.d/[Rr]ocky*.repoelif [ ${OLD_DIR} == 'Rocky' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/Rocky|baseurl=https://'${MIRROR}'/rocky|g' /etc/yum.repos.d/[Rr]ocky*.repo elsesed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/rocky|baseurl=https://'${MIRROR}'/rocky|g' /etc/yum.repos.d/[Rr]ocky*.repofifi${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/null && dnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}rocky_8_9_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)网易镜像源
4)搜狐镜像源
5)南京大学镜像源
6)中科大镜像源
7)上海交通大学镜像源
8)西安交通大学镜像源
9)北京大学镜像源
10)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-10): " NUMcase ${NUM} in1)aliyunset_yum_rocky_8_9;;2)tencentset_yum_rocky_8_9;;3)neteaseset_yum_rocky_8_9;;4)sohuset_yum_rocky_8_9;;5)njuset_yum_rocky_8_9;;6)ustcset_yum_rocky_8_9;;7)sjtuset_yum_rocky_8_9;;8)xjtuset_yum_rocky_8_9;;9)pkuset_yum_rocky_8_9;;10)break;;*)${COLOR}"输入错误,请输入正确的数字(1-10)!"${END};;esacdone
}set_devel_rocky_9(){dnf config-manager --set-enabled devel${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/null && dnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} devel源设置完成!"${END}
}set_powertools_rocky_almalinux_centos_8(){dnf config-manager --set-enabled powertools${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/null && dnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} powertools源设置完成!"${END}
}set_yum_almalinux_8_9(){OLD_MIRROR=$(sed -rn '/^.*baseurl=/s@.*=http.*://(.*)/(.*)/\$releasever/.*/$@\1@p' /etc/yum.repos.d/almalinux*.repo | head -1)sed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^# baseurl=https://'${OLD_MIRROR}'|baseurl=https://'${MIRROR}'|g' /etc/yum.repos.d/almalinux*.repo${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/null && dnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}almalinux_8_9_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)南京大学镜像源
4)上海交通大学镜像源
5)北京大学镜像源
6)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-6): " NUMcase ${NUM} in1)aliyunset_yum_almalinux_8_9;;2)tencentset_yum_almalinux_8_9;;3)njuset_yum_almalinux_8_9;;4)sjtuset_yum_almalinux_8_9;;5)pkuset_yum_almalinux_8_9;;6)break;;*)${COLOR}"输入错误,请输入正确的数字(1-6)!"${END};;esacdone
}set_yum_centos_stream_9_perl(){${COLOR}"由于CentOS Stream 9系统默认镜像源是Perl语言实现的,在更改镜像源之前先确保把'update_mirror.pl'文件和reset脚本放在同一个目录下,否则后面程序会退出,默认的CentOS Stream 9镜像源设置的是阿里云,要修改镜像源,请去'update_mirror.pl'文件里修改url变量!"${END}sleep 10PERL_FILE=update_mirror.plif [ ! -e ${PERL_FILE} ];then${COLOR}"缺少${PERL_FILE}文件"${END}exitelse${COLOR}"${PERL_FILE}文件已准备好,继续后续配置!"${END}       firpm -q perl &> /dev/null || { ${COLOR}"安装perl工具,请稍等..."${END};yum -y install perl &> /dev/null; }perl ./update_mirror.pl /etc/yum.repos.d/centos*.repo${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/null && dnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}set_yum_centos_stream_9(){OLD_MIRROR=$(sed -rn '/^.*baseurl=/s@.*=http.*://(.*)/(.*)/\$releasever-stream/.*/$@\1@p' /etc/yum.repos.d/centos*.repo | head -1)sed -i -e 's|^baseurl=https://'${OLD_MIRROR}'|baseurl=https://'${MIRROR}'|g' /etc/yum.repos.d/centos*.repo${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/null && dnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}centos_stream9_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)南京大学镜像源
6)中科大镜像源
7)北京外国语大学镜像源
8)北京大学镜像源
9)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-9): " NUMcase ${NUM} in1)aliyunset_yum_centos_stream_9;;2)huaweiset_yum_centos_stream_9;;3)tencentset_yum_centos_stream_9;;4)tunaset_yum_centos_stream_9;;5)njuset_yum_centos_stream_9;;6)ustcset_yum_centos_stream_9;;7)bfsuset_yum_centos_stream_9;;8)pkuset_yum_centos_stream_9;;9)break;;*)${COLOR}"输入错误,请输入正确的数字(1-9)!"${END};;esacdone
}set_crb_almalinux_centos_9(){dnf config-manager --set-enabled crb${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/null && dnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} crb源设置完成!"${END}
}set_yum_centos_stream_8(){OLD_MIRROR=$(sed -rn '/^.*baseurl=/s@.*=http.*://(.*)/(.*)/\$stream/.*/$@\1@p' /etc/yum.repos.d/CentOS-*.repo | head -1)OLD_DIR=$(sed -rn '/^.*baseurl=/s@.*=http.*://(.*)/(.*)/\$stream/.*/$@\2@p' /etc/yum.repos.d/CentOS-*.repo | head -1)if [ ${OLD_DIR} == '$contentdir' ];thensed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://'${MIRROR}'/centos-vault|g' /etc/yum.repos.d/CentOS-*.repoelsesed -i -e 's|^baseurl=https://'${OLD_MIRROR}'|baseurl=https://'${MIRROR}'|g' /etc/yum.repos.d/CentOS-*.repofi${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/null && dnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}centos_stream_8_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)南京大学镜像源
6)中科大镜像源
7)北京外国语大学镜像源
8)北京大学镜像源
9)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-9): " NUMcase ${NUM} in1)aliyunset_yum_centos_stream_8;;2)huaweiset_yum_centos_stream_8;;3)tencentset_yum_centos_stream_8;;4)tunaset_yum_centos_stream_8;;5)njuset_yum_centos_stream_8;;6)ustcset_yum_centos_stream_8;;7)bfsuset_yum_centos_stream_8;;8)pkuset_yum_centos_stream_8;;9)break;;*)${COLOR}"输入错误,请输入正确的数字(1-9)!"${END};;esacdone
}set_epel_rocky_almalinux_centos_8_9(){rpm -q epel-release &> /dev/null || { ${COLOR}"安装epel-release工具,请稍等..."${END};yum -y install epel-release &> /dev/null; }MIRROR_URL=`echo ${MIRROR} | awk -F"." '{print $2}'`OLD_MIRROR=$(awk -F'/' '/^baseurl=/{print $3}' /etc/yum.repos.d/epel*.repo | head -1)OLD_DIR=$(awk -F'/' '/^baseurl=/{print $4}' /etc/yum.repos.d/epel*.repo | head -1)if [ ${MIRROR_URL} == "sohu" ];thenif grep -Eqi "^#baseurl" /etc/yum.repos.d/epel*.repo;thensed -i.bak -e 's|^metalink=|#metalink=|g' -e 's|^#baseurl=https://download.example/pub/epel|baseurl=https://'${MIRROR}'/fedora-epel|g' /etc/yum.repos.d/epel*.repoelif [ ${OLD_DIR} == 'epel' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/epel|baseurl=https://'${MIRROR}'/fedora-epel|g' /etc/yum.repos.d/epel*.repoelif [ ${OLD_DIR} == 'fedora' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/fedora/epel|baseurl=https://'${MIRROR}'/fedora-epel|g' /etc/yum.repos.d/epel*.repoelsesed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/fedora-epel|baseurl=https://'${MIRROR}'/fedora-epel|g' /etc/yum.repos.d/epel*.repofielif [ ${MIRROR_URL} == "sjtu" ];thenif grep -Eqi "^#baseurl" /etc/yum.repos.d/epel*.repo;thensed -i.bak -e 's|^metalink=|#metalink=|g' -e 's|^#baseurl=https://download.example/pub/epel|baseurl=https://'${MIRROR}'/fedora/epel|g' /etc/yum.repos.d/epel*.repoelif [ ${OLD_DIR} == 'epel' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/epel|baseurl=https://'${MIRROR}'/fedora/epel|g' /etc/yum.repos.d/epel*.repoelif [ ${OLD_DIR} == 'fedora-epel' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/fedora-epel|baseurl=https://'${MIRROR}'/fedora/epel|g' /etc/yum.repos.d/epel*.repoelsesed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/fedora/epel|baseurl=https://'${MIRROR}'/fedora/epel|g' /etc/yum.repos.d/epel*.repofielseif grep -Eqi "^#baseurl" /etc/yum.repos.d/epel*.repo;thensed -i.bak -e 's|^metalink=|#metalink=|g' -e 's|^#baseurl=https://download.example/pub/epel|baseurl=https://'${MIRROR}'/epel|g' /etc/yum.repos.d/epel*.repoelif [ ${OLD_DIR} == 'fedora' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/fedora/epel|baseurl=https://'${MIRROR}'/epel|g' /etc/yum.repos.d/epel*.repoelif [ ${OLD_DIR} == 'fedora-epel' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/fedora-epel|baseurl=https://'${MIRROR}'/epel|g' /etc/yum.repos.d/epel*.repoelsesed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/epel|baseurl=https://'${MIRROR}'/epel|g' /etc/yum.repos.d/epel*.repofifiif [ ${OS_ID} == "Rocky" -o ${OS_ID} == "CentOS" ];thenif [ ${OS_RELEASE_VERSION} == "9" ];thendnf config-manager --set-disabled epel-cisco-openh264fifi${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/null && dnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} EPEL源设置完成!"${END}
}rocky_almalinux_centos_8_9_epel_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)搜狐镜像源
6)南京大学镜像源
7)中科大镜像源
8)上海交通大学镜像源
9)西安交通大学镜像源
9)北京外国语大学镜像源
11)北京大学镜像源
12)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-12): " NUMcase ${NUM} in1)aliyunset_epel_rocky_almalinux_centos_8_9;;2)huaweiset_epel_rocky_almalinux_centos_8_9;;3)tencentset_epel_rocky_almalinux_centos_8_9;;4)tunaset_epel_rocky_almalinux_centos_8_9;;5)sohuset_epel_rocky_almalinux_centos_8_9;;6)njuset_epel_rocky_almalinux_centos_8_9;;7)ustcset_epel_rocky_almalinux_centos_8_9;;8)sjtuset_epel_rocky_almalinux_centos_8_9;;9)xjtuset_epel_rocky_almalinux_centos_8_9;;10)bfsuset_epel_rocky_almalinux_centos_8_9;;11)pkuset_epel_rocky_almalinux_centos_8_9;;12)break;;*)${COLOR}"输入错误,请输入正确的数字(1-12)!"${END};;esacdone
}set_yum_centos_7(){OLD_MIRROR=$(sed -rn '/^.*baseurl=/s@.*=(http.*)://(.*)/(.*)/\$releasever/.*/$@\2@p' /etc/yum.repos.d/CentOS-*.repo | head -1)OS_RELEASE_FULL_VERSION=`cat /etc/centos-release | sed -rn 's/^(CentOS Linux release )(.*)( \(Core\))/\2/p'`if grep -Eqi "^#baseurl" /etc/yum.repos.d/CentOS-*.repo;thensed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://'${MIRROR}'/centos-vault|g' -e "s/\$releasever/${OS_RELEASE_FULL_VERSION}/g" /etc/yum.repos.d/CentOS-*.repoelsesed -i -e 's|^baseurl=https://'${OLD_MIRROR}'|baseurl=https://'${MIRROR}'|g' /etc/yum.repos.d/CentOS-*.repofi${COLOR}"更新镜像源中,请稍等..."${END}yum clean all &> /dev/null && yum makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}centos7_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)南京大学镜像源
6)中科大镜像源
7)北京外国语大学镜像源
8)北京大学镜像源
9)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-9): " NUMcase ${NUM} in1)aliyunset_yum_centos_7;;2)huaweiset_yum_centos_7;;3)tencentset_yum_centos_7;;4)tunaset_yum_centos_7;;5)njuset_yum_centos_7;;6)ustcset_yum_centos_7;;7)bfsuset_yum_centos_7;;8)pkuset_yum_centos_7;;9)break;;*)${COLOR}"输入错误,请输入正确的数字(1-9)!"${END};;esacdone
}set_epel_centos_7(){rpm -q epel-release &> /dev/null || { ${COLOR}"安装epel-release工具,请稍等..."${END};yum -y install epel-release &> /dev/null; }MIRROR_URL=`echo ${MIRROR} | awk -F"." '{print $2}'`OLD_MIRROR=$(awk -F'/' '/^baseurl=/{print $3}' /etc/yum.repos.d/epel*.repo | head -1)OLD_DIR=$(awk -F'/' '/^baseurl=/{print $4}' /etc/yum.repos.d/epel*.repo | head -1)if [ ${MIRROR_URL} == "aliyun" -o ${MIRROR_URL} == "tencent" ];thenif grep -Eqi "^#baseurl" /etc/yum.repos.d/epel*.repo;thensed -i.bak -e 's!^metalink=!#metalink=!g' -e 's!^#baseurl=!baseurl=!g' -e 's!https\?://download\.fedoraproject\.org/pub/epel!https://'${MIRROR}'/epel-archive!g' -e 's!https\?://download\.example/pub/epel!https://'${MIRROR}'/epel!g' /etc/yum.repos.d/epel*.repoelif [ ${OLD_DIR} == 'pub' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/pub/archive/epel|baseurl=https://'${MIRROR}'/epel-archive|g' /etc/yum.repos.d/epel*.repoelsesed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/epel-archive|baseurl=https://'${MIRROR}'/epel-archive|g' /etc/yum.repos.d/epel*.repofielseif grep -Eqi "^#baseurl" /etc/yum.repos.d/epel*.repo;thensed -i.bak -e 's!^metalink=!#metalink=!g' -e 's!^#baseurl=!baseurl=!g' -e 's!https\?://download\.fedoraproject\.org/pub/epel!https://'${MIRROR}'/pub/archive/epel!g' -e 's!https\?://download\.example/pub/epel!https://'${MIRROR}'/epel!g' /etc/yum.repos.d/epel*.repoelif [ ${OLD_DIR} == 'epel-archive' ];thensed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/epel-archive|baseurl=https://'${MIRROR}'/pub/archive/epel|g' /etc/yum.repos.d/epel*.repoelsesed -i -e 's|^baseurl=https://'${OLD_MIRROR}'/pub/archive/epel|baseurl=https://'${MIRROR}'/pub/archive/epel|g' /etc/yum.repos.d/epel*.repofifi${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/null && dnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} EPEL源设置完成!"${END}
}centos7_epel_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)fedora镜像源
4)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-4): " NUMcase ${NUM} in1)aliyunset_epel_centos_7;;2)tencentset_epel_centos_7;;3)archive_fedoraset_epel_centos_7;;4)break;;*)${COLOR}"输入错误,请输入正确的数字(1-4)!"${END};;esacdone
}rocky_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)base仓库
2)epel仓库
3)启用Rocky 9 devel仓库
4)启用Rocky 8 PowerTools仓库
5)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-5): " NUMcase ${NUM} in1)rocky_8_9_base_menu;;2)rocky_almalinux_centos_8_9_epel_menu;;3)if [ ${OS_RELEASE_VERSION} == "9" ];thenset_devel_rocky_9else${COLOR}"${OS_ID} ${OS_RELEASE} 没有devel源,不用设置!"${END}fi;;4)if [ ${OS_RELEASE_VERSION} == "8" ];thenset_powertools_rocky_almalinux_centos_8else${COLOR}"${OS_ID} ${OS_RELEASE} 没有powertools源,不用设置!"${END}fi;;5)break;;*)${COLOR}"输入错误,请输入正确的数字(1-5)!"${END};;esacdone
}almalinux_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)base仓库
2)epel仓库
3)启用AlmaLinux 9 crb仓库
4)启用AlmaLinux 8 PowerTools仓库
5)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-5): " NUMcase ${NUM} in1)almalinux_8_9_base_menu;;2)rocky_almalinux_centos_8_9_epel_menu;;3)if [ ${OS_RELEASE_VERSION} == "9" ];thenset_crb_almalinux_centos_9else${COLOR}"${OS_ID} ${OS_RELEASE} 没有crb源,不用设置!"${END}fi;;4)if [ ${OS_RELEASE_VERSION} == "8" ];thenset_powertools_rocky_almalinux_centos_8else${COLOR}"${OS_ID} ${OS_RELEASE} 没有powertools源,不用设置!"${END}fi;;5)break;;*)${COLOR}"输入错误,请输入正确的数字(1-5)!"${END};;esacdone
}centos_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)base仓库
2)epel仓库
3)启用CentOS Stream 9 crb仓库
4)启用CentOS Stream 8 PowerTools仓库
5)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-5): " NUMcase ${NUM} in1)if [ ${OS_NAME} == "Stream" ];thenif [ ${OS_RELEASE_VERSION} == "8" ];thencentos_stream_8_base_menuelseif grep -Eqi "^baseurl" /etc/yum.repos.d/centos*.repo;thencentos_stream_9_base_menuelseset_yum_centos_stream_9_perlfifielsecentos7_base_menufi;;2)if [ ${OS_RELEASE_VERSION} == "7" ];thencentos_7_epel_menuelserocky_almalinux_centos_8_9_epel_menufi;;3)if [ ${OS_RELEASE_VERSION} == "9" ];thenset_crb_almalinux_centos_9else${COLOR}"${OS_ID} ${OS_RELEASE} 没有crb源,不用设置!"${END}fi;;4)if [ ${OS_RELEASE_VERSION} == "8" ];thenset_powertools_rocky_almalinux_centos_8else${COLOR}"${OS_ID} ${OS_RELEASE} 没有powertools源,不用设置!"${END}fi;;5)break;;*)${COLOR}"输入错误,请输入正确的数字(1-5)!"${END};;esacdone
}set_ubuntu_apt(){if [ ${OS_RELEASE_VERSION} == "18" -o ${OS_RELEASE_VERSION} == "20" -o ${OS_RELEASE_VERSION} == "22" ];thenOLD_MIRROR=`sed -rn "s@^deb http(.*)://(.*)/ubuntu/? $(lsb_release -cs) main.*@\2@p" /etc/apt/sources.list`sed -i.bak 's/'${OLD_MIRROR}'/'${MIRROR}'/g' /etc/apt/sources.listif [ ${OS_RELEASE_VERSION} == "18" ];thenSECURITY_MIRROR=`sed -rn "s@^deb http(.*)://(.*)/ubuntu $(lsb_release -cs)-security main.*@\2@p" /etc/apt/sources.list`sed -i.bak 's/'${SECURITY_MIRROR}'/'${MIRROR}'/g' /etc/apt/sources.listfielsesed -ri "s@^(URIs: )(http.*://)(.*)(/ubuntu).?@\1https://${MIRROR}\4@g" /etc/apt/sources.list.d/ubuntu.sourcesfi${COLOR}"更新镜像源中,请稍等..."${END}apt update &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} APT源设置完成!"${END}
}apt_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)网易镜像源
6)搜狐镜像源
7)南京大学镜像源
8)中科大镜像源
9)上海交通大学镜像源
10)西安交通大学镜像源
11)北京外国语大学镜像源
12)北京交通大学镜像源
13)北京大学镜像源
14)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-14): " NUMcase ${NUM} in1)aliyunset_ubuntu_apt;;2)huaweiset_ubuntu_apt;;3)tencentset_ubuntu_apt;;4)tunaset_ubuntu_apt;;5)neteaseset_ubuntu_apt;;6)sohuset_ubuntu_apt;;7)njuset_ubuntu_apt;;8)ustcset_ubuntu_apt;;9)sjtuset_ubuntu_apt;;10)xjtuset_ubuntu_apt;;11)bfsuset_ubuntu_apt;;12)bjtuset_ubuntu_apt;;13)pkuset_ubuntu_apt;;14)break;;*)${COLOR}"输入错误,请输入正确的数字(1-14)!"${END};;esacdone
}set_debian_apt(){OLD_MIRROR=`sed -rn "s@^deb http(.*)://(.*)/debian/? $(lsb_release -cs) main.*@\2@p" /etc/apt/sources.list`SECURITY_MIRROR=`sed -rn "s@^deb http(.*)://(.*)/debian-security $(lsb_release -cs)-security main.*@\2@p" /etc/apt/sources.list`sed -ri.bak -e 's/'${OLD_MIRROR}'/'${MIRROR}'/g' -e 's/'${SECURITY_MIRROR}'/'${MIRROR}'/g' -e 's/^(deb cdrom.*)/#\1/g' /etc/apt/sources.list${COLOR}"更新镜像源中,请稍等..."${END}apt update &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} APT源设置完成!"${END}
}debian_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)网易镜像源
6)搜狐镜像源
7)南京大学镜像源
8)中科大镜像源
9)上海交通大学镜像源
10)西安交通大学镜像源
11)北京外国语大学镜像源
12)北京交通大学镜像源
13)北京大学镜像源
14)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-14): " NUMcase ${NUM} in1)aliyunset_debian_apt;;2)huaweiset_debian_apt;;3)tencentset_debian_apt;;4)tunaset_debian_apt;;5)neteaseset_debian_apt;;6)sohuset_debian_apt;;7)njuset_debian_apt;;8)ustcset_debian_apt;;9)sjtuset_debian_apt;;10)xjtuset_debian_apt;;11)bfsuset_debian_apt;;12)bjtuset_debian_apt;;13)pkuset_debian_apt;;14)break;;*)${COLOR}"输入错误,请输入正确的数字(1-14)!"${END};;esacdone
}set_mirror_repository(){if [ ${OS_ID} == "Rocky" ];thenrocky_menuelif [ ${OS_ID} == "AlmaLinux" ];thenalmalinux_menuelif [ ${OS_ID} == "CentOS" ];thencentos_menuelif [ ${OS_ID} == "Ubuntu" ];thenapt_menuelsedebian_menufi
}rocky_almalinux_centos_minimal_install(){${COLOR}'开始安装“Minimal安装建议安装软件包”,请稍等......'${END}yum -y 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 wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} Minimal安装建议安装软件包已安装完成!"${END}
}ubuntu_debian_minimal_install(){${COLOR}'开始安装“Minimal安装建议安装软件包”,请稍等......'${END}apt -y 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${COLOR}"${OS_ID} ${OS_RELEASE} Minimal安装建议安装软件包已安装完成!"${END}
}minimal_install(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenrocky_almalinux_centos_minimal_installelseubuntu_debian_minimal_installfi
}disable_firewalls(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenrpm -q firewalld &> /dev/null && { systemctl disable --now firewalld &> /dev/null; ${COLOR}"${OS_ID} ${OS_RELEASE} Firewall防火墙已关闭!"${END}; } || ${COLOR}"${OS_ID} ${OS_RELEASE} iptables防火墙已关闭!"${END}elif [ ${OS_ID} == "Ubuntu" ];thendpkg -s ufw &> /dev/null && { systemctl disable --now ufw &> /dev/null; ${COLOR}"${OS_ID} ${OS_RELEASE} ufw防火墙已关闭!"${END}; } || ${COLOR}"${OS_ID} ${OS_RELEASE}  没有ufw防火墙服务,不用关闭!"${END}else${COLOR}"${OS_ID} ${OS_RELEASE}  没有安装防火墙服务,不用关闭!"${END}fi
}disable_selinux(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenif [ `getenforce` == "Enforcing" ];thensed -ri.bak 's/^(SELINUX=).*/\1disabled/' /etc/selinux/config${COLOR}"${OS_ID} ${OS_RELEASE} SELinux已禁用,请重新启动系统后才能生效!"${END}else${COLOR}"${OS_ID} ${OS_RELEASE} SELinux已被禁用,不用设置!"${END}fielse${COLOR}"${OS_ID} ${OS_RELEASE} SELinux默认没有安装,不用设置!"${END}fi
}set_swap(){sed -ri 's/.*swap.*/#&/' /etc/fstabif [ ${OS_ID} == "Ubuntu" ];thenif [ ${OS_RELEASE_VERSION} == 20 -o ${OS_RELEASE_VERSION} == 22 -o ${OS_RELEASE_VERSION} == 24 ];thenSD_NAME=`lsblk|awk -F"[ └─]" '/SWAP/{printf $3}'`systemctl mask dev-${SD_NAME}.swap &> /dev/nullfifiswapoff -a${COLOR}"${OS_ID} ${OS_RELEASE} 禁用swap成功!"${END}
}set_localtime(){timedatectl set-timezone Asia/Shanghaiecho 'Asia/Shanghai' >/etc/timezoneif [ ${OS_ID} == "Ubuntu" ];thencat >> /etc/default/locale <<-EOF
LC_TIME=en_DK.UTF-8
EOFfi${COLOR}"${OS_ID} ${OS_RELEASE} 系统时区已设置成功,请重启系统后生效!"${END}
}set_limits(){cat >> /etc/security/limits.conf <<-EOF
root     soft   core     unlimited
root     hard   core     unlimited
root     soft   nproc    1000000
root     hard   nproc    1000000
root     soft   nofile   1000000
root     hard   nofile   1000000
root     soft   memlock  32000
root     hard   memlock  32000
root     soft   msgqueue 8192000
root     hard   msgqueue 8192000
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 优化资源限制参数成功!"${END}
}set_kernel(){modprobe  br_netfiltercat > /etc/sysctl.conf <<-EOF
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296# TCP kernel paramater
net.ipv4.tcp_mem = 786432 1048576 1572864
net.ipv4.tcp_rmem = 4096        87380   4194304
net.ipv4.tcp_wmem = 4096        16384   4194304
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_sack = 1# socket buffer
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 20480
net.core.optmem_max = 81920# TCP conn
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_syn_retries = 3
net.ipv4.tcp_retries1 = 3
net.ipv4.tcp_retries2 = 15# tcp conn reuse
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_timestamps = 0net.ipv4.tcp_max_tw_buckets = 20000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syncookies = 1# keepalive conn
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.ip_local_port_range = 10001    65000# swap
vm.overcommit_memory = 0
vm.swappiness = 10#net.ipv4.conf.eth1.rp_filter = 0
#net.ipv4.conf.lo.arp_ignore = 1
#net.ipv4.conf.lo.arp_announce = 2
#net.ipv4.conf.all.arp_ignore = 1
#net.ipv4.conf.all.arp_announce = 2
EOFMAIN_KERNEL=`uname -r | cut -d. -f1`SUB_KERNEL=`uname -r | cut -d. -f2`if [ ${MAIN_KERNEL} -lt "4" -a ${SUB_KERNEL} -lt "12" ];thencat >> /etc/sysctl.conf <<-EOF	
net.ipv4.tcp_tw_recycle = 0
EOFfisysctl -p &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} 优化内核参数成功!"${END}
}optimization_ssh(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thensed -ri.bak -e 's/^#(UseDNS).*/\1 no/' -e 's/^(GSSAPIAuthentication).*/\1 no/' /etc/ssh/sshd_configelsesed -ri.bak -e 's/^#(UseDNS).*/\1 no/' -e 's/^#(GSSAPIAuthentication).*/\1 no/' /etc/ssh/sshd_configfiif [ ${OS_ID} == "Ubuntu" ];thenif [ ${OS_RELEASE_VERSION} == 24 ];thensystemctl restart sshfielsesystemctl restart sshdfi${COLOR}"${OS_ID} ${OS_RELEASE} SSH已优化完成!"${END}
}set_ssh_port(){disable_selinuxdisable_firewallread -p "请输入端口号: " PORTsed -i 's/#Port 22/Port '${PORT}'/' /etc/ssh/sshd_configif [ ${OS_ID} == "Ubuntu" ];thenif [ ${OS_RELEASE_VERSION} == 24 ];thensystemctl restart sshfielsesystemctl restart sshdfi${COLOR}"${OS_ID} ${OS_RELEASE} 更改SSH端口号已完成,请重新登陆后生效!"${END}
}set_rocky_almalinux_centos_alias(){ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`ETHNAME2=`ip addr | awk -F"[ :]" '/^3/{print $3}'`IP_NUM=`ip addr | awk -F"[: ]" '{print $1}' | grep -v '^$' | wc -l`if [ ${IP_NUM} == "2" ];thenif [ ${OS_RELEASE_VERSION} == "7" -o ${OS_RELEASE_VERSION} == "8" ];thencat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/sysconfig/network-scripts"
alias cdrepo="cd /etc/yum.repos.d"
alias vie0="vim /etc/sysconfig/network-scripts/ifcfg-${ETHNAME}"
EOFelsecat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/NetworkManager/system-connections"
alias cdrepo="cd /etc/yum.repos.d"
alias vie0="vim /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection"
EOFfielse	if [ ${OS_RELEASE_VERSION} == "7" -o ${OS_RELEASE_VERSION} == "8" ];thencat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/sysconfig/network-scripts"
alias cdrepo="cd /etc/yum.repos.d"
alias vie0="vim /etc/sysconfig/network-scripts/ifcfg-${ETHNAME}"
alias vie1="vim /etc/sysconfig/network-scripts/ifcfg-${ETHNAME2}"
EOFelsecat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/NetworkManager/system-connections"
alias cdrepo="cd /etc/yum.repos.d"
alias vie0="vim /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection"
alias vie1="vim /etc/NetworkManager/system-connections/${ETHNAME2}.nmconnection"
EOFfifiDISK_NAME=`lsblk|awk -F" " '/disk/{printf $1}' | cut -c1-4`if [ ${DISK_NAME} == "sda" ];thencat >>~/.bashrc <<-EOF
alias scandisk="echo '- - -' > /sys/class/scsi_host/host0/scan;echo '- - -' > /sys/class/scsi_host/host1/scan;echo '- - -' > /sys/class/scsi_host/host2/scan"
EOFfi${COLOR}"${OS_ID} ${OS_RELEASE} 系统别名已设置成功,请重新登陆后生效!"${END}
}set_ubuntu_alias(){cat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/netplan"
alias cdapt="cd /etc/apt"
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 系统别名已设置成功,请重新登陆后生效!"${END}
}set_debian_alias(){cat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/network"
alias cdapt="cd /etc/apt"
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 系统别名已设置成功,请重新登陆后生效!"${END}
}set_alias(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenif grep -Eqi "(.*cdnet|.*cdrepo|.*vie0|.*vie1|.*scandisk)" ~/.bashrc;thensed -i -e '/.*cdnet/d'  -e '/.*cdrepo/d' -e '/.*vie0/d' -e '/.*vie1/d' -e '/.*scandisk/d' ~/.bashrcset_rocky_almalinux_centos_aliaselseset_rocky_almalinux_centos_aliasfielif [ ${OS_ID} == "Ubuntu" ];thenif grep -Eqi "(.*cdnet|.*cdapt)" ~/.bashrc;thensed -i -e '/.*cdnet/d' -e '/.*cdapt/d' ~/.bashrcset_ubuntu_aliaselseset_ubuntu_aliasfielseif grep -Eqi "(.*cdnet|.*cdapt)" ~/.bashrc;thensed -i -e '/.*cdnet/d' -e '/.*cdapt/d' ~/.bashrcset_debian_aliaselseset_debian_aliasfifi
}set_vimrc(){read -p "请输入作者名: " AUTHORread -p "请输入QQ号: " QQread -p "请输入网址: " V_MIRRORcat >~/.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:        ${AUTHOR}")call setline(5,"#QQ:            ${QQ}")call setline(6,"#Date:          ".strftime("%Y-%m-%d"))call setline(7,"#FileName:      ".expand("%"))call setline(8,"#MIRROR:           ${V_MIRROR}")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${COLOR}"${OS_ID} ${OS_RELEASE} vimrc设置完成,请重新系统启动才能生效!"${END}
}set_mail(){                                                                                                 if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenrpm -q postfix &> /dev/null || { yum -y install postfix &> /dev/null; systemctl enable --now postfix &> /dev/null; }rpm -q mailx &> /dev/null || yum -y install mailx &> /dev/nullelsedpkg -s mailutils &> /dev/null || apt -y install mailutilsfiread -p "请输入邮箱地址: " MAILread -p "请输入邮箱授权码: " AUTHSMTP=`echo ${MAIL} |awk -F"@" '{print $2}'`cat >~/.mailrc <<-EOF
set from=${MAIL}
set smtp=smtp.${SMTP}
set smtp-auth-user=${MAIL}
set smtp-auth-password=${AUTH}
set smtp-auth=login
set ssl-verify=ignore
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 邮件设置完成,请重新登录后才能生效!"${END}
}red(){P_COLOR=31
}green(){P_COLOR=32
}yellow(){P_COLOR=33
}blue(){P_COLOR=34
}violet(){P_COLOR=35
}cyan_blue(){P_COLOR=36
}random_color(){P_COLOR="$[RANDOM%7+31]"
}rocky_almalinux_centos_ps1(){C_PS1=$(echo "PS1='\[\e[1;${P_COLOR}m\][\u@\h \W]\\$ \[\e[0m\]'" >> ~/.bashrc)
}ubuntu_debian_ps1(){U_PS1=$(echo 'PS1="\[\e[1;'''${P_COLOR}'''m\]${debian_chroot:+($debian_chroot)}\u@\h:\w\\$ \[\e[0m\]"' >> ~/.bashrc)
}set_ps1_env(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenif grep -Eqi "^PS1" ~/.bashrc;thensed -i '/^PS1/d' ~/.bashrcrocky_almalinux_centos_ps1elserocky_almalinux_centos_ps1fielseif grep -Eqi "^PS1" ~/.bashrc;thensed -i '/^PS1/d' ~/.bashrcubuntu_debian_ps1elseubuntu_debian_ps1fifi
}set_ps1(){TIPS="${COLOR}${OS_ID} ${OS_RELEASE} PS1设置成功,请重新登录生效!${END}"while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)31 红色
2)32 绿色
3)33 黄色
4)34 蓝色
5)35 紫色
6)36 青色
7)随机颜色
8)退出
EOFecho -e '\E[0m'read -p "请输入颜色编号(1-8): " NUMcase ${NUM} in1)redset_ps1_env${TIPS};;2)greenset_ps1_env${TIPS};;3)yellowset_ps1_env${TIPS};;4)blueset_ps1_env${TIPS};;5)violetset_ps1_env${TIPS};;6)cyan_blueset_ps1_env${TIPS};;7)random_colorset_ps1_env${TIPS};;8)break;;*)${COLOR}"输入错误,请输入正确的数字(1-8)!"${END};;esacdone
}set_vim(){echo "export EDITOR=vim" >> ~/.bashrc
}set_vim_env(){if grep -Eqi ".*EDITOR" ~/.bashrc;thensed -i '/.*EDITOR/d' ~/.bashrcset_vimelseset_vimfi${COLOR}"${OS_ID} ${OS_RELEASE} 默认文本编辑器设置成功,请重新登录生效!"${END}
}set_history(){echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bashrc 
}set_history_env(){if grep -Eqi ".*HISTTIMEFORMAT" ~/.bashrc;thensed -i '/.*HISTTIMEFORMAT/d' ~/.bashrcset_historyelseset_historyfi${COLOR}"${OS_ID} ${OS_RELEASE} history格式设置成功,请重新登录生效!"${END}
}disable_restart(){if [ -f /usr/lib/systemd/system/ctrl-alt-del.target ];thencp /usr/lib/systemd/system/ctrl-alt-del.target{,.bak}rm -f /usr/lib/systemd/system/ctrl-alt-del.target${COLOR}"${OS_ID} ${OS_RELEASE} 禁用ctrl+alt+del重启处理成功!"${END}else${COLOR}"${OS_ID} ${OS_RELEASE} 禁用ctrl+alt+del已处理!"${END}fi
}set_ubuntu_debian_root_login(){if [ ${OS_ID} == "Ubuntu" -o ${OS_ID} == "Debian" ];thenread -p "请输入密码: " PASSWORDecho ${PASSWORD} |sudo -S sed -ri 's@#(PermitRootLogin )prohibit-password@\1yes@' /etc/ssh/sshd_configif [ ${OS_ID} == "Ubuntu" ];thenif [ ${OS_RELEASE_VERSION} == 24 ];thensudo systemctl restart sshfielsesudo systemctl restart sshdfisudo -S passwd root <<-EOF
${PASSWORD}
${PASSWORD}
EOF${COLOR}"${OS_ID} ${OS_RELEASE} root用户登录已设置完成,请重新登录后生效!"${END}else${COLOR}"${OS_ID} ${OS_RELEASE} 系统不可用!"${END}fi
}ubuntu_remove(){if [ ${OS_ID} == "Ubuntu" ];thenapt -y purge ufw lxd lxd-client lxcfs liblxc-common${COLOR}"${OS_ID} ${OS_RELEASE} 无用软件包卸载完成!"${END}else${COLOR}"${OS_ID} ${OS_RELEASE} 系统不可用!"${END}fi
}ubuntu_20_22_24_remove_snap(){dpkg -s snapd &> /dev/nullif [ $? -eq 1 ];then ${COLOR}"${OS_ID} ${OS_RELEASE} snap已卸载!"${END}elsesystemctl disable snapd.service && systemctl disable snapd.socket && systemctl disable snapd.seeded.servicesum=$(snap list | awk 'NR>=2{print $1}' | wc -l)while [ $sum -ne 0 ];dofor p in $(snap list | awk 'NR>=2{print $1}'); dosnap remove --purge $pdonesum=$(snap list | awk 'NR>=2{print $1}' | wc -l)doneapt -y autoremove --purge snapdrm -rf ~/snap && sudo rm -rf /snap && rm -rf /var/snap && rm -rf /var/lib/snapd && rm -rf /var/cache/snapdcat > /etc/apt/preferences.d/no-snapd.pref << EOF
Package: snapd
Pin: release a=*
Pin-Priority: -10
EOFapt update${COLOR}"${OS_ID} ${OS_RELEASE} snap卸载完成!"${END}fi
}ubuntu_remove_snap(){if [ ${OS_ID} == "Ubuntu" ];thenif [ ${OS_RELEASE_VERSION} == 20 -o ${OS_RELEASE_VERSION} == 22 -o ${OS_RELEASE_VERSION} == 24 ];thenubuntu_20_22_24_remove_snapelse${COLOR}"${OS_ID} ${OS_RELEASE} 默认没有安装snap!"${END} fielse${COLOR}"${OS_ID} ${OS_RELEASE} 系统不可用!"${END}fi
}menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
******************************************************************
*                     系统初始化脚本菜单                         *
* 1.修改网卡名                 15.设置系统别名                   *
* 2.设置网络(单网卡)           16.设置vimrc配置文件              *
* 3.设置网络(双网卡)           17.安装邮件服务并配置邮件         *
* 4.设置主机名                 18.设置PS1(请进入选择颜色)        *
* 5.设置镜像仓库               19.设置默认文本编辑器为vim        *
* 6.Minimal安装建议安装软件    20.设置history格式                *
* 7.关闭防火墙                 21.禁用ctrl+alt+del重启           *
* 8.禁用SELinux                22.Ubuntu和Debian设置root用户登录 *
* 9.禁用SWAP                   23.Ubuntu卸载无用软件包           *
* 10.设置系统时区              24.Ubuntu卸载snap                 *
* 11.优化资源限制参数          25.重启系统                       *
* 12.优化内核参数              26.关机                           *
* 13.优化SSH                   27.退出                           *
* 14.更改SSH端口号                                               *
******************************************************************
EOFecho -e '\E[0m'read -p "请选择相应的编号(1-27): " choicecase ${choice} in1)set_eth;;2)set_network;;3)set_dual_network;;4)set_hostname;;5)set_mirror_repository;;6)minimal_install;;7)disable_firewalls;;8)disable_selinux;;9)set_swap;;10)set_localtime;;11)set_limits;;12)set_kernel;;13)optimization_ssh;;14)set_ssh_port;;15)set_alias;;16)set_vimrc;;17)set_mail;;18)set_ps1;;19)set_vim_env;;20)set_history_env;;21)disable_restart;;22)set_ubuntu_debian_root_login;;23)ubuntu_remove;;24)ubuntu_remove_snap;;25)reboot;;26)shutdown -h now;;27)break;;*)${COLOR}"输入错误,请输入正确的数字(1-27)!"${END};;esacdone
}main(){osmenu
}main

CentOS Stream 9修改镜像仓库需要下面的perl脚本。

[root@rocky9 ~]# cat update_mirror.pl
#!/usr/bin/perluse strict;
use warnings;
use autodie;# 要修改镜像源,请去修改url变量!
my $url = 'mirrors.aliyun.com';
my $mirrors = "https://$url/centos-stream";if (@ARGV < 1) {die "Usage: $0 <filename1> <filename2> ...\n";
}while (my $filename = shift @ARGV) {my $backup_filename = $filename . '.bak';rename $filename, $backup_filename;open my $input, "<", $backup_filename;open my $output, ">", $filename;while (<$input>) {s/^metalink/# metalink/;if (m/^name/) {my (undef, $repo, $arch) = split /-/;$repo =~ s/^\s+|\s+$//g;($arch = defined $arch ? lc($arch) : '') =~ s/^\s+|\s+$//g;if ($repo =~ /^Extras/) {$_ .= "baseurl=${mirrors}/SIGs/\$releasever-stream/extras" . ($arch eq 'source' ? "/${arch}/" : "/\$basearch/") . "extras-common\n";} else {$_ .= "baseurl=${mirrors}/\$releasever-stream/$repo" . ($arch eq 'source' ? "/" : "/\$basearch/") . ($arch ne '' ? "${arch}/tree/" : "os") . "\n";}}print $output $_;}
}

下面是reset_v9_2脚本内容:

[root@rocky9 ~]# cat reset_v9_2.sh
#!/bin/bash
#
#**********************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2024-11-12
#FileName:      reset_v9_2.sh
#MIRROR:        raymond.blog.csdn.net
#Description:   The reset linux system initialization script supports 
#               “Rocky Linux 8 and 9, Almalinux 8 and 9, CentOS 7, 
#               CentOS Stream 8 and 9, Ubuntu 18.04, 20.04, 22.04 and 24.04, 
#               Debian 12“ operating systems.
#Copyright (C): 2024 All rights reserved
#**********************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'os(){OS_ID=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`OS_NAME=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+) (.*)"$@\2@p' /etc/os-release`OS_RELEASE=`sed -rn '/^VERSION_ID=/s@.*="?([0-9.]+)"?@\1@p' /etc/os-release`OS_RELEASE_VERSION=`sed -rn '/^VERSION_ID=/s@.*="?([0-9]+)\.?.*"?@\1@p' /etc/os-release`
}set_rocky_almalinux_centos_eth(){if [ ${OS_RELEASE_VERSION} == "7" -o ${OS_RELEASE_VERSION} == "8" ];thenETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`if grep -Eqi "(net\.ifnames|biosdevname)" /etc/default/grub;then${COLOR}"${OS_ID} ${OS_RELEASE} 网卡名配置文件已修改,不用修改!"${END}elsesed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0 biosdevname=0"@' /etc/default/grubgrub2-mkconfig -o /boot/grub2/grub.cfg >& /dev/nullmv /etc/sysconfig/network-scripts/ifcfg-${ETHNAME} /etc/sysconfig/network-scripts/ifcfg-eth0${COLOR}"${OS_ID} ${OS_RELEASE} 网卡名已修改成功,10秒后,机器会自动重启!"${END}sleep 10 && shutdown -r nowfi   else${COLOR}"${OS_ID} ${OS_RELEASE} 不能修改网卡名!"${END} fi
}set_ubuntu_debian_eth(){if grep -Eqi "(net\.ifnames|biosdevname)" /etc/default/grub;then${COLOR}"${OS_ID} ${OS_RELEASE} 网卡名配置文件已修改,不用修改!"${END}elsesed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@net.ifnames=0 biosdevname=0"@' /etc/default/grubgrub-mkconfig -o /boot/grub/grub.cfg >& /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} 网卡名已修改成功,请重新启动系统后才能生效!"${END}fi
}set_eth(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenset_rocky_almalinux_centos_ethelseset_ubuntu_debian_ethfi
}check_ip(){local IP=$1VALID_CHECK=$(echo ${IP}|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')if echo ${IP}|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; thenif [ ${VALID_CHECK} == "yes" ]; thenecho "IP ${IP}  available!"return 0elseecho "IP ${IP} not available!"return 1fielseecho "IP format error!"return 1fi
}set_rocky_almalinux_centos_network(){ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`CONNECTION_NAME=`nmcli dev | awk 'NR==2{print $4,$5,$6}'`	while true; doread -p "请输入IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdoneif [ ${OS_RELEASE_VERSION} == "7" -o ${OS_RELEASE_VERSION} == "8" ];thennmcli connection modify "${CONNECTION_NAME}" con-name ${ETHNAME}cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<-EOF
NAME=${ETHNAME}
DEVICE=${ETHNAME}
ONBOOT=yes
BOOTPROTO=none
TYPE=Ethernet
IPADDR=${IP}
PREFIX=${PREFIX}
GATEWAY=${GATEWAY}
DNS1=${PRIMARY_DNS}
DNS2=${BACKUP_DNS}
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,10秒后,机器会自动重启!"${END}sleep 10 && shutdown -r nowelse${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请使用新IP重新登录!"${END}cat > /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection <<-EOF
[connection]
id=${ETHNAME}
type=ethernet
interface-name=${ETHNAME}[ethernet][ipv4]
address1=${IP}/${PREFIX},${GATEWAY}
dns=${PRIMARY_DNS};${BACKUP_DNS};
method=manual[ipv6]
addr-gen-mode=default
method=auto[proxy]
EOFfinmcli con reload && nmcli dev up ${ETHNAME} >& /dev/null
}set_ubuntu_network(){while true; doread -p "请输入IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdoneif [ ${OS_RELEASE_VERSION} == "18" ];thencat > /etc/netplan/01-netcfg.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] gateway4: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
EOFelif [ ${OS_RELEASE_VERSION} == "20" ];thencat > /etc/netplan/00-installer-config.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] gateway4: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
EOFelif [ ${OS_RELEASE_VERSION} == "22" ];thencat > /etc/netplan/00-installer-config.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}]routes:- to: defaultvia: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
EOFelsecat > /etc/netplan/50-cloud-init.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}]routes:- to: defaultvia: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]
EOFfi    ${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请重新启动系统后生效!"${END}
}   set_debian_network(){ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`while true; doread -p "请输入IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdonesed -ri -e "s/allow-hotplug ${ETHNAME}/auto eth0/g" -e "s/(iface) ${ETHNAME} (inet) dhcp/\1 eth0 \2 static/g" /etc/network/interfacescat >> /etc/network/interfaces <<-EOF
address ${IP}/${PREFIX}
gateway ${GATEWAY}
dns-nameservers ${PRIMARY_DNS} ${BACKUP_DNS}
EOF${COLOR}"${OS_ID} ${OS_RELEASE}  网络已设置成功,请重新启动系统后生效!"${END}
}set_network(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenset_rocky_almalinux_centos_networkelif [ ${OS_ID} == "Ubuntu" ];thenset_ubuntu_networkelseset_debian_networkfi
}set_dual_rocky_almalinux_centos_network(){ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`ETHNAME2=`ip addr | awk -F"[ :]" '/^3/{print $3}'`CONNECTION_NAME1=`nmcli dev | awk 'NR==2{print $4,$5,$6}'`CONNECTION_NAME2=`nmcli dev | awk 'NR==3{print $4,$5,$6}'`while true; doread -p "请输入第一块网卡IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入第二块网卡IP地址: " IP2check_ip ${IP2}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIX2if [ ${OS_RELEASE_VERSION} == "7" -o ${OS_RELEASE_VERSION} == "8" ];thennmcli connection modify "${CONNECTION_NAME}" con-name ${ETHNAME}cat > /etc/sysconfig/network-scripts/ifcfg-${ETHNAME} <<-EOF
NAME=${ETHNAME}
DEVICE=${ETHNAME}
ONBOOT=yes
BOOTPROTO=none
TYPE=Ethernet
IPADDR=${IP}
PREFIX=${PREFIX}
GATEWAY=${GATEWAY}
DNS1=${PRIMARY_DNS}
DNS2=${BACKUP_DNS}
EOFnmcli connection modify "${CONNECTION_NAME2}" con-name ${ETHNAME2}cat > /etc/sysconfig/network-scripts/ifcfg-${ETHNAME2} <<-EOF
NAME=${ETHNAME2}
DEVICE=${ETHNAME2}
ONBOOT=yes
BOOTPROTO=none
TYPE=Ethernet
IPADDR=${IP2}
PREFIX=${PREFIX2}
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,10秒后,机器会自动重启!"${END}sleep 10 && shutdown -r nowelse${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请使用新IP重新登录!"${END}cat > /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection <<-EOF
[connection]
id=${ETHNAME}
type=ethernet
interface-name=${ETHNAME}[ethernet][ipv4]
address1=${IP}/${PREFIX},${GATEWAY}
dns=${PRIMARY_DNS};${BACKUP_DNS};
method=manual[ipv6]
addr-gen-mode=default
method=auto[proxy]
EOFnmcli connection modify "${CONNECTION_NAME2}" con-name ${ETHNAME2}cat > /etc/NetworkManager/system-connections/${ETHNAME2}.nmconnection <<-EOF
[connection]
id=${ETHNAME2}
type=ethernet
interface-name=${ETHNAME2}[ethernet][ipv4]
address1=${IP2}/${PREFIX2}
method=manual[ipv6]
addr-gen-mode=default
method=auto[proxy]
EOFchmod 600 /etc/NetworkManager/system-connections/${ETHNAME2}.nmconnectionfinmcli con reload && nmcli dev up ${ETHNAME} ${ETHNAME2} >& /dev/null
}set_dual_ubuntu_network(){while true; doread -p "请输入第一块网卡IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入第二块网卡IP地址: " IP2check_ip ${IP2}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIX2if [ ${OS_RELEASE_VERSION} == "18" ];thencat > /etc/netplan/01-netcfg.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] gateway4: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]eth1:dhcp4: nodhcp6: noaddresses: [${IP2}/${PREFIX2}] 
EOFelif [ ${OS_RELEASE_VERSION} == "20" ];thencat > /etc/netplan/00-installer-config.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] gateway4: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]eth1:dhcp4: nodhcp6: noaddresses: [${IP2}/${PREFIX2}] 
EOFelif [ ${OS_RELEASE_VERSION} == "22" ];thencat > /etc/netplan/00-installer-config.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] routes:- to: defaultvia: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]eth1:dhcp4: nodhcp6: noaddresses: [${IP2}/${PREFIX2}] 
EOFelsecat > /etc/netplan/50-cloud-init.yaml <<-EOF
network:version: 2renderer: networkdethernets:eth0:dhcp4: nodhcp6: noaddresses: [${IP}/${PREFIX}] routes:- to: defaultvia: ${GATEWAY}nameservers:addresses: [${PRIMARY_DNS}, ${BACKUP_DNS}]eth1:dhcp4: nodhcp6: noaddresses: [${IP2}/${PREFIX2}] 
EOFfi${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请重新启动系统后生效!"${END}
}set_dual_debian_network(){ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`while true; doread -p "请输入第一块网卡IP地址: " IPcheck_ip ${IP}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIXwhile true; doread -p "请输入网关地址: " GATEWAYcheck_ip ${GATEWAY}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入主DNS地址(例如:阿里:223.5.5.5,腾讯:119.29.29.29,公共:114.114.114.114,google:8.8.8.8等): " PRIMARY_DNScheck_ip ${PRIMARY_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入备用DNS地址(例如:阿里:223.6.6.6,腾讯:119.28.28.28,公共:114.114.115.115,google:8.8.4.4等): " BACKUP_DNScheck_ip ${BACKUP_DNS}[ $? -eq 0 ] && breakdonewhile true; doread -p "请输入第二块网卡IP地址: " IP2check_ip ${IP2}[ $? -eq 0 ] && breakdoneread -p "请输入子网掩码位数: " PREFIX2sed -ri -e "s/allow-hotplug ${ETHNAME}/auto eth0/g" -e "s/(iface) ${ETHNAME} (inet) dhcp/\1 eth0 \2 static/g" /etc/network/interfacescat >> /etc/network/interfaces <<-EOF
address ${IP}/${PREFIX}
gateway ${GATEWAY}
dns-nameservers ${PRIMARY_DNS} ${BACKUP_DNS}auto eth1
iface eth1 inet static
address ${IP2}/${PREFIX2}
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 网络已设置成功,请重新启动系统后生效!"${END}
}set_dual_network(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenset_dual_rocky_almalinux_centos_networkelif [ ${OS_ID} == "Ubuntu" ];thenset_dual_ubuntu_networkelseset_dual_debian_networkfi
}set_hostname(){read -p "请输入主机名: " HOSThostnamectl set-hostname ${HOST}${COLOR}"${OS_ID} ${OS_RELEASE} 主机名设置成功,请重新登录生效!"${END}
}aliyun(){MIRROR=mirrors.aliyun.com
}huawei(){MIRROR=repo.huaweicloud.com
}tencent(){MIRROR=mirrors.tencent.com
}tuna(){MIRROR=mirrors.tuna.tsinghua.edu.cn
}netease(){MIRROR=mirrors.163.com
}sohu(){MIRROR=mirrors.sohu.com
}nju(){MIRROR=mirrors.nju.edu.cn
}ustc(){MIRROR=mirrors.ustc.edu.cn
}sjtu(){MIRROR=mirrors.sjtug.sjtu.edu.cn
}xjtu(){MIRROR=mirrors.xjtu.edu.cn
}bfsu(){MIRROR=mirrors.bfsu.edu.cn
}bjtu(){MIRROR=mirror.bjtu.edu.cn
}pku(){MIRROR=mirrors.pku.edu.cn
}archive_fedora(){MIRROR=archives.fedoraproject.org
}set_yum_rocky_9(){[ -d /etc/yum.repos.d/backup ] || { mkdir /etc/yum.repos.d/backup; mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup; }MIRROR_URL=`echo ${MIRROR} | awk -F"." '{print $2}'`if [ ${MIRROR_URL} == "aliyun" -o ${MIRROR_URL} == "xjtu" ];thencat > /etc/yum.repos.d/base.repo <<-EOF
[BaseOS]
name=BaseOS
baseurl=https://${MIRROR}/rockylinux/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever[AppStream]
name=AppStream
baseurl=https://${MIRROR}/rockylinux/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever[extras]
name=extras
baseurl=https://${MIRROR}/rockylinux/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever
EOFelif [ ${MIRROR_URL} == "sohu" ];thencat > /etc/yum.repos.d/base.repo <<-EOF
[BaseOS]
name=BaseOS
baseurl=https://${MIRROR}/Rocky/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever[AppStream]
name=AppStream
baseurl=https://${MIRROR}/Rocky/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever[extras]
name=extras
baseurl=https://${MIRROR}/Rocky/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever
EOFelsecat > /etc/yum.repos.d/base.repo <<-EOF
[BaseOS]
name=BaseOS
baseurl=https://${MIRROR}/rocky/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever[AppStream]
name=AppStream
baseurl=https://${MIRROR}/rocky/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever[extras]
name=extras
baseurl=https://${MIRROR}/rocky/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever
EOFfi${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}rocky_9_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)网易镜像源
4)搜狐镜像源
5)南京大学镜像源
6)中科大镜像源
7)上海交通大学镜像源
8)西安交通大学镜像源
9)北京大学镜像源
10)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-10): " NUMcase ${NUM} in1)aliyunset_yum_rocky_9;;2)tencentset_yum_rocky_9;;3)neteaseset_yum_rocky_9;;4)sohuset_yum_rocky_9;;5)njuset_yum_rocky_9;;6)ustcset_yum_rocky_9;;7)sjtuset_yum_rocky_9;;8)xjtuset_yum_rocky_9;;9)pkuset_yum_rocky_9;;10)break;;*)${COLOR}"输入错误,请输入正确的数字(1-10)!"${END};;esacdone
}set_devel_rocky_9(){MIRROR_MIRROR=`echo ${MIRROR} | awk -F"." '{print $2}'`if [ ${MIRROR_MIRROR} == "aliyun" -o ${MIRROR_MIRROR} == "xjtu" ];thencat > /etc/yum.repos.d/devel.repo <<-EOF
[devel]
name=devel
baseurl=https://${MIRROR}/rockylinux/\$releasever/devel/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever
EOFelif [ ${MIRROR_MIRROR} == "sohu" ];thencat > /etc/yum.repos.d/devel.repo <<-EOF
[devel]
name=devel
baseurl=https://${MIRROR}/Rocky/\$releasever/devel/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever
EOFelsecat > /etc/yum.repos.d/devel.repo <<-EOF
[devel]
name=devel
baseurl=https://${MIRROR}/rocky/\$releasever/devel/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-\$releasever
EOFfi${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} devel源设置完成!"${END}
}rocky_9_devel_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)网易镜像源
4)搜狐镜像源
5)南京大学镜像源
6)中科大镜像源
7)上海交通大学镜像源
8)西安交通大学镜像源
9)北京大学镜像源
10)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-10): " NUMcase ${NUM} in1)aliyunset_devel_rocky_9;;2)tencentset_devel_rocky_9;;3)neteaseset_devel_rocky_9;;4)sohuset_devel_rocky_9;;5)njuset_devel_rocky_9;;6)ustcset_devel_rocky_9;;7)sjtuset_devel_rocky_9;;8)xjtuset_devel_rocky_9;;9)pkuset_devel_rocky_9;;10)break;;*)${COLOR}"输入错误,请输入正确的数字(1-10)!"${END};;esacdone
}set_yum_almalinux_9(){[ -d /etc/yum.repos.d/backup ] || { mkdir /etc/yum.repos.d/backup; mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup; }cat > /etc/yum.repos.d/base.repo <<-EOF
[BaseOS]
name=BaseOS
baseurl=https://${MIRROR}/almalinux/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux-9[AppStream]
name=AppStream
baseurl=https://${MIRROR}/almalinux/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux-9[extras]
name=extras
baseurl=https://${MIRROR}/almalinux/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux-9
EOF${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}almalinux_9_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)南京大学镜像源
4)上海交通大学镜像源
5)北京大学镜像源
6)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-6): " NUMcase ${NUM} in1)aliyunset_yum_almalinux_9;;2)tencentset_yum_almalinux_9;;3)njuset_yum_almalinux_9;;4)sjtuset_yum_almalinux_9;;5)pkuset_yum_almalinux_9;;6)break;;*)${COLOR}"输入错误,请输入正确的数字(1-6)!"${END};;esacdone
}set_crb_almalinux_9(){cat > /etc/yum.repos.d/crb.repo <<-EOF
[crb]
name=crb
baseurl=https://${MIRROR}/almalinux/\$releasever/CRB/\$basearch/os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux-9
EOF${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} crb源设置完成!"${END}
}almalinux_9_crb_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)南京大学镜像源
4)上海交通大学镜像源
5)北京大学镜像源
6)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-6): " NUMcase ${NUM} in1)aliyunset_crb_almalinux_9;;2)tencentset_crb_almalinux_9;;3)njuset_crb_almalinux_9;;4)sjtuset_crb_almalinux_9;;5)pkuset_crb_almalinux_9;;6)break;;*)${COLOR}"输入错误,请输入正确的数字(1-6)!"${END};;esacdone
}set_yum_rocky_8(){[ -d /etc/yum.repos.d/backup ] || { mkdir /etc/yum.repos.d/backup; mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup; }MIRROR_URL=`echo ${MIRROR} | awk -F"." '{print $2}'`if [ ${MIRROR_URL} == "aliyun" -o ${MIRROR_URL} == "xjtu" ];thencat > /etc/yum.repos.d/base.repo <<-EOF
[BaseOS]
name=BaseOS
baseurl=https://${MIRROR}/rockylinux/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial[AppStream]
name=AppStream
baseurl=https://${MIRROR}/rockylinux/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial[extras]
name=extras
baseurl=https://${MIRROR}/rockylinux/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
EOFelif [ ${MIRROR_URL} == "sohu" ];thencat > /etc/yum.repos.d/base.repo <<-EOF
[BaseOS]
name=BaseOS
baseurl=https://${MIRROR}/Rocky/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial[AppStream]
name=AppStream
baseurl=https://${MIRROR}/Rocky/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial[extras]
name=extras
baseurl=https://${MIRROR}/Rocky/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
EOFelsecat > /etc/yum.repos.d/base.repo <<-EOF
[BaseOS]
name=BaseOS
baseurl=https://${MIRROR}/rocky/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial[AppStream]
name=AppStream
baseurl=https://${MIRROR}/rocky/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial[extras]
name=extras
baseurl=https://${MIRROR}/rocky/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
EOFfi${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}rocky_8_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)网易镜像源
4)搜狐镜像源
5)南京大学镜像源
6)中科大镜像源
7)上海交通大学镜像源
8)西安交通大学镜像源
9)北京大学镜像源
10)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-10): " NUMcase ${NUM} in1)aliyunset_yum_rocky_8;;2)tencentset_yum_rocky_8;;3)neteaseset_yum_rocky_8;;4)sohuset_yum_rocky_8;;5)njuset_yum_rocky_8;;6)ustcset_yum_rocky_8;;7)sjtuset_yum_rocky_8;;8)xjtuset_yum_rocky_8;;9)pkuset_yum_rocky_8;;10)break;;*)${COLOR}"输入错误,请输入正确的数字(1-10)!"${END};;esacdone
}set_powertools_rocky_8(){MIRROR_URL=`echo ${MIRROR} | awk -F"." '{print $2}'`if [ ${MIRROR_URL} == "aliyun" -o ${MIRROR_URL} == "xjtu" ];thencat > /etc/yum.repos.d/powertools.repo <<-EOF
[PowerTools]
name=PowerTools
baseurl=https://${MIRROR}/rockylinux/\$releasever/PowerTools/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
EOFelif [ ${MIRROR_URL} == "sohu" ];thencat > /etc/yum.repos.d/powertools.repo <<-EOF
[PowerTools]
name=PowerTools
baseurl=https://${MIRROR}/Rocky/\$releasever/PowerTools/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
EOFelsecat > /etc/yum.repos.d/powertools.repo <<-EOF
[PowerTools]
name=PowerTools
baseurl=https://${MIRROR}/rocky/\$releasever/PowerTools/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
EOFfi${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} PowerTools源设置完成!"${END}
}rocky_8_powertools_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)网易镜像源
4)搜狐镜像源
5)南京大学镜像源
6)中科大镜像源
7)上海交通大学镜像源
8)西安交通大学镜像源
9)北京大学镜像源
10)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-10): " NUMcase ${NUM} in1)aliyunset_powertools_rocky_8;;2)tencentset_powertools_rocky_8;;3)neteaseset_powertools_rocky_8;;4)sohuset_powertools_rocky_8;;5)njuset_powertools_rocky_8;;6)ustcset_powertools_rocky_8;;7)sjtuset_powertools_rocky_8;;8)xjtuset_powertools_rocky_8;;9)pkuset_powertools_rocky_8;;10)break;;*)${COLOR}"输入错误,请输入正确的数字(1-10)!"${END};;esacdone
}set_yum_almalinux_8(){[ -d /etc/yum.repos.d/backup ] || { mkdir /etc/yum.repos.d/backup; mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup; }cat > /etc/yum.repos.d/base.repo <<-EOF
[BaseOS]
name=BaseOS
baseurl=https://${MIRROR}/almalinux/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux[AppStream]
name=AppStream
baseurl=https://${MIRROR}/almalinux/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux[extras]
name=extras
baseurl=https://${MIRROR}/almalinux/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux
EOF${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}almalinux_8_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)南京大学镜像源
4)上海交通大学镜像源
5)北京大学镜像源
6)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-6): " NUMcase ${NUM} in1)aliyunset_yum_almalinux_8;;2)tencentset_yum_almalinux_8;;3)njuset_yum_almalinux_8;;4)sjtuset_yum_almalinux_8;;5)pkuset_yum_almalinux_8;;6)break;;*)${COLOR}"输入错误,请输入正确的数字(1-6)!"${END};;esacdone
}set_powertools_almalinux_8(){cat > /etc/yum.repos.d/powertools.repo <<-EOF
[powertools]
name=powertools
baseurl=https://${MIRROR}/almalinux/\$releasever/PowerTools/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux
EOF${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} PowerTools源设置完成!"${END}
}almalinux_8_powertools_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)南京大学镜像源
4)上海交通大学镜像源
5)北京大学镜像源
6)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-6): " NUMcase ${NUM} in1)aliyunset_powertools_almalinux_8;;2)tencentset_powertools_almalinux_8;;3)njuset_powertools_almalinux_8;;4)sjtuset_powertools_almalinux_8;;5)pkuset_powertools_almalinux_8;;6)break;;*)${COLOR}"输入错误,请输入正确的数字(1-6)!"${END};;esacdone
}set_yum_centos_stream_9(){[ -d /etc/yum.repos.d/backup ] || { mkdir /etc/yum.repos.d/backup; mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup; }cat > /etc/yum.repos.d/base.repo <<-EOF
[BaseOS]
name=BaseOS
baseurl=https://${MIRROR}/centos-stream/\$stream/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial[AppStream]
name=AppStream
baseurl=https://${MIRROR}/centos-stream/\$stream/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial[extras-common]
name=extras-common
baseurl=https://${MIRROR}/centos-stream/SIGs/\$stream/extras/\$basearch/extras-common/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}centos_stream_9_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)南京大学镜像源
6)中科大镜像源
7)北京外国语大学镜像源
8)北京大学镜像源
9)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-9): " NUMcase ${NUM} in1)aliyunset_yum_centos_stream_9;;2)huaweiset_yum_centos_stream_9;;3)tencentset_yum_centos_stream_9;;4)tunaset_yum_centos_stream_9;;5)njuset_yum_centos_stream_9;;6)ustcset_yum_centos_stream_9;;7)bfsuset_yum_centos_stream_9;;8)pkuset_yum_centos_stream_9;;9)break;;*)${COLOR}"输入错误,请输入正确的数字(1-9)!"${END};;esacdone
}set_crb_centos_stream_9(){cat > /etc/yum.repos.d/crb.repo <<-EOF
[crb]
name=crb
baseurl=https://${MIRROR}/centos-stream/\$releasever-stream/CRB/\$basearch/os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} crb源设置完成!"${END}
}centos_stream_9_crb_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)南京大学镜像源
6)中科大镜像源
7)北京外国语大学镜像源
8)北京大学镜像源
9)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-9): " NUMcase ${NUM} in1)aliyunset_crb_centos_stream_9;;2)huaweiset_crb_centos_stream_9;;3)tencentset_crb_centos_stream_9;;4)tunaset_crb_centos_stream_9;;5)njuset_crb_centos_stream_9;;6)ustcset_crb_centos_stream_9;;6)bfsuset_crb_centos_stream_9;;			8)pkuset_crb_centos_stream_9;;9)break;;*)${COLOR}"输入错误,请输入正确的数字(1-9)!"${END};;esacdone
}set_yum_centos_stream_8(){[ -d /etc/yum.repos.d/backup ] || { mkdir /etc/yum.repos.d/backup; mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup; }cat > /etc/yum.repos.d/base.repo <<-EOF
[BaseOS]
name=BaseOS
baseurl=https://${MIRROR}/centos-vault/\$stream/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial[AppStream]
name=AppStream
baseurl=https://${MIRROR}/centos-vault/\$stream/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial[extras]
name=extras
baseurl=https://${MIRROR}/centos-vault/\$stream/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}centos_stream_8_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)南京大学镜像源
6)中科大镜像源
7)北京外国语大学镜像源
8)北京大学镜像源
9)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-9): " NUMcase ${NUM} in1)aliyunset_yum_centos_stream_8;;2)huaweiset_yum_centos_stream_8;;3)tencentset_yum_centos_stream_8;;4)tunaset_yum_centos_stream_8;;5)njuset_yum_centos_stream_8;;6)ustcset_yum_centos_stream_8;;6)bfsuset_yum_centos_stream_8;;			8)pkuset_yum_centos_stream_8;;9)break;;*)${COLOR}"输入错误,请输入正确的数字(1-9)!"${END};;esacdone
}set_powertools_centos_stream_8(){cat > /etc/yum.repos.d/powertools.repo <<-EOF
[PowerTools]
name=PowerTools
baseurl=https://${MIRROR}/centos-vault/\$stream/PowerTools/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} PowerTools源设置完成!"${END}
}centos_stream_8_powertools_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)南京大学镜像源
6)中科大镜像源
7)北京外国语大学镜像源
8)北京大学镜像源
9)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-9): " NUMcase ${NUM} in1)aliyunset_powertools_centos_stream_8;;2)huaweiset_powertools_centos_stream_8;;3)tencentset_powertools_centos_stream_8;;4)tunaset_powertools_centos_stream_8;;5)njuset_powertools_centos_stream_8;;6)ustcset_powertools_centos_stream_8;;7)bfsuset_powertools_centos_stream_8;;8)pkuset_powertools_centos_stream_8;;9)break;;*)${COLOR}"输入错误,请输入正确的数字(1-9)!"${END};;esacdone
}set_epel_rocky_almalinux_centos_8_9(){MIRROR_URL=`echo ${MIRROR} | awk -F"." '{print $2}'`if [ ${MIRROR_URL} == "sohu" ];thencat > /etc/yum.repos.d/epel.repo <<-EOF
[epel]
name=epel
baseurl=https://${MIRROR}/fedora-epel/\$releasever/Everything/\$basearch/
gpgcheck=1
gpgkey=https://${MIRROR}/fedora-epel/RPM-GPG-KEY-EPEL-\$releasever
EOFelif [ ${MIRROR_URL} == "sjtu" ];thencat > /etc/yum.repos.d/epel.repo <<-EOF
[epel]
name=epel
baseurl=https://${MIRROR}/fedora/epel/\$releasever/Everything/\$basearch/
gpgcheck=1
gpgkey=https://${MIRROR}/fedora/epel/RPM-GPG-KEY-EPEL-\$releasever
EOFelsecat > /etc/yum.repos.d/epel.repo <<-EOF
[epel]
name=epel
baseurl=https://${MIRROR}/epel/\$releasever/Everything/\$basearch/
gpgcheck=1
gpgkey=https://${MIRROR}/epel/RPM-GPG-KEY-EPEL-\$releasever
EOFfi${COLOR}"更新镜像源中,请稍等..."${END}dnf clean all &> /dev/nulldnf makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} EPEL源设置完成!"${END}
}rocky_almalinux_centos_8_9_epel_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)搜狐镜像源
6)南京大学镜像源
7)中科大镜像源
8)上海交通大学镜像源
9)西安交通大学镜像源
9)北京外国语大学镜像源
11)北京大学镜像源
12)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-12): " NUMcase ${NUM} in1)aliyunset_epel_rocky_almalinux_centos_8_9;;2)huaweiset_epel_rocky_almalinux_centos_8_9;;3)tencentset_epel_rocky_almalinux_centos_8_9;;4)tunaset_epel_rocky_almalinux_centos_8_9;;5)sohuset_epel_rocky_almalinux_centos_8_9;;6)njuset_epel_rocky_almalinux_centos_8_9;;7)ustcset_epel_rocky_almalinux_centos_8_9;;8)sjtuset_epel_rocky_almalinux_centos_8_9;;9)xjtuset_epel_rocky_almalinux_centos_8_9;;10)bfsuset_epel_rocky_almalinux_centos_8_9;;11)pkuset_epel_rocky_almalinux_centos_8_9;;12)break;;*)${COLOR}"输入错误,请输入正确的数字(1-12)!"${END};;esacdone
}set_yum_centos_7(){    [ -d /etc/yum.repos.d/backup ] || { mkdir /etc/yum.repos.d/backup; mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup; }OS_RELEASE_FULL_VERSION=`cat /etc/centos-release | sed -rn 's/^(CentOS Linux release )(.*)( \(Core\))/\2/p'`cat > /etc/yum.repos.d/base.repo <<-EOF
[base]
name=base
baseurl=https://${MIRROR}/centos-vault/${OS_RELEASE_FULL_VERSION}/os/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever[extras]
name=extras
baseurl=https://${MIRROR}/centos-vault/${OS_RELEASE_FULL_VERSION}/extras/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever[updates]
name=updates
baseurl=https://${MIRROR}/centos-vault/${OS_RELEASE_FULL_VERSION}/updates/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
EOF${COLOR}"更新镜像源中,请稍等..."${END}yum clean all &> /dev/nullyum makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} YUM源设置完成!"${END}
}centos_7_base_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)南京大学镜像源
6)中科大镜像源
7)北京外国语大学镜像源
8)北京大学镜像源
9)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-9): " NUMcase ${NUM} in1)aliyunset_yum_centos_7;;2)huaweiset_yum_centos_7;;3)tencentset_yum_centos_7;;4)tunaset_yum_centos_7;;5)njuset_yum_centos_7;;6)ustcset_yum_centos_7;;7)bfsuset_yum_centos_7;;8)pkuset_yum_centos_7;;9)break;;*)${COLOR}"输入错误,请输入正确的数字(1-9)!"${END};;esacdone
}set_epel_centos_7(){MIRROR_URL=`echo ${MIRROR} | awk -F"." '{print $2}'`if [ ${MIRROR_URL} == "aliyun" -o ${MIRROR_URL} == "tencent" ];thencat > /etc/yum.repos.d/epel.repo <<-EOF
[epel]
name=epel
baseurl=https://${MIRROR}/epel-archive/\$releasever/\$basearch/
gpgcheck=1
gpgkey=https://${MIRROR}/epel-archive/RPM-GPG-KEY-EPEL-\$releasever
EOFelsecat > /etc/yum.repos.d/epel.repo <<-EOF
[epel]
name=epel
baseurl=https://${MIRROR}/pub/archive/epel/\$releasever/\$basearch/
gpgcheck=1
gpgkey=https://${MIRROR}/pub/archive/epel/RPM-GPG-KEY-EPEL-\$releasever
EOFfi${COLOR}"更新镜像源中,请稍等..."${END}yum clean all &> /dev/nullyum makecache &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} EPEL源设置完成!"${END}
}centos_7_epel_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)腾讯镜像源
3)fedora镜像源
4)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-4): " NUMcase ${NUM} in1)aliyunset_epel_centos_7;;2)tencentset_epel_centos_7;;3)archive_fedoraset_epel_centos_7;;4)break;;*)${COLOR}"输入错误,请输入正确的数字(1-4)!"${END};;esacdone
}rocky_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)base仓库
2)epel仓库
3)Rocky 9 devel仓库
4)Rocky 8 PowerTools仓库
5)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-5): " NUMcase ${NUM} in1)if [ ${OS_RELEASE_VERSION} == "8" ];thenrocky_8_base_menuelserocky_9_base_menufi;;2)rocky_almalinux_centos_8_9_epel_menu;;3)if [ ${OS_RELEASE_VERSION} == "9" ];thenrocky_9_devel_menuelse${COLOR}"${OS_ID} ${OS_RELEASE} 没有devel源,不用设置!"${END}fi;;4)if [ ${OS_RELEASE_VERSION} == "8" ];thenrocky_8_powertools_menuelse${COLOR}"${OS_ID} ${OS_RELEASE} 没有powertools源,不用设置!"${END}fi;;5)break;;*)${COLOR}"输入错误,请输入正确的数字(1-5)!"${END};;esacdone
}almalinux_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)base仓库
2)epel仓库
3)AlmaLinux 9 crb仓库
4)AlmaLinux 8 PowerTools仓库
5)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-5): " NUMcase ${NUM} in1)if [ ${OS_RELEASE_VERSION} == "8" ];thenalmalinux_8_base_menuelsealmalinux_9_base_menufi;;2)rocky_almalinux_centos_8_9_epel_menu;;3)if [ ${OS_RELEASE_VERSION} == "9" ];thenalmalinux_9_crb_menuelse${COLOR}"${OS_ID} ${OS_RELEASE} 没有crb源,不用设置!"${END}fi;;4)if [ ${OS_RELEASE_VERSION} == "8" ];thenalmalinux_8_powertools_menuelse${COLOR}"${OS_ID} ${OS_RELEASE} 没有powertools源,不用设置!"${END}fi;;5)break;;*)${COLOR}"输入错误,请输入正确的数字(1-5)!"${END};;esacdone
}centos_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)base仓库
2)epel仓库
3)CentOS Stream 9 crb仓库
4)CentOS Stream 8 PowerTools仓库
5)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-5): " NUMcase ${NUM} in1)if [ ${OS_NAME} == "Stream" ];thenif [ ${OS_RELEASE_VERSION} == "8" ];thencentos_stream_8_base_menuelsecentos_stream_9_base_menufielsecentos_7_base_menufi;;2)if [ ${OS_RELEASE_VERSION} == "7" ];thencentos_7_epel_menuelserocky_almalinux_centos_8_9_epel_menufi;;3)if [ ${OS_RELEASE_VERSION} == "9" ];thencentos_stream_9_crb_menuelse${COLOR}"${OS_ID} ${OS_RELEASE} 没有crb源,不用设置!"${END}fi;;4)if [ ${OS_RELEASE_VERSION} == "8" ];thencentos_stream_8_powertools_menuelse${COLOR}"${OS_ID} ${OS_RELEASE} 没有powertools源,不用设置!"${END}fi;;5)break;;*)${COLOR}"输入错误,请输入正确的数字(1-5)!"${END};;esacdone
}set_ubuntu_apt(){if [ ${OS_RELEASE_VERSION} == "18" -o ${OS_RELEASE_VERSION} == "20" -o ${OS_RELEASE_VERSION} == "22" ];thenmv /etc/apt/sources.list /etc/apt/sources.list.bakcat > /etc/apt/sources.list <<-EOF
deb http://${MIRROR}/ubuntu/ $(lsb_release -cs) main restricted universe multiverse
deb-src http://${MIRROR}/ubuntu/ $(lsb_release -cs) main restricted universe multiversedeb http://${MIRROR}/ubuntu/ $(lsb_release -cs)-security main restricted universe multiverse
deb-src http://${MIRROR}/ubuntu/ $(lsb_release -cs)-security main restricted universe multiversedeb http://${MIRROR}/ubuntu/ $(lsb_release -cs)-updates main restricted universe multiverse
deb-src http://${MIRROR}/ubuntu/ $(lsb_release -cs)-updates main restricted universe multiversedeb http://${MIRROR}/ubuntu/ $(lsb_release -cs)-proposed main restricted universe multiverse
deb-src http://${MIRROR}/ubuntu/ $(lsb_release -cs)-proposed main restricted universe multiversedeb http://${MIRROR}/ubuntu/ $(lsb_release -cs)-backports main restricted universe multiverse
deb-src http://${MIRROR}/ubuntu/ $(lsb_release -cs)-backports main restricted universe multiverse
EOFelsemv /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bakcat > /etc/apt/sources.list.d/ubuntu.sources <<-EOF
Types: deb
URIs: https://${MIRROR}/ubuntu
Suites: $(lsb_release -cs) $(lsb_release -cs)-updates $(lsb_release -cs)-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpgTypes: deb
URIs: https://${MIRROR}/ubuntu
Suites: $(lsb_release -cs)-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOFfi${COLOR}"更新镜像源中,请稍等..."${END}apt update &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} APT源设置完成!"${END}
}apt_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)网易镜像源
6)搜狐镜像源
7)南京大学镜像源
8)中科大镜像源
9)上海交通大学镜像源
10)西安交通大学镜像源
11)北京外国语大学镜像源
12)北京交通大学镜像源
13)北京大学镜像源
14)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-14): " NUMcase ${NUM} in1)aliyunset_ubuntu_apt;;2)huaweiset_ubuntu_apt;;3)tencentset_ubuntu_apt;;4)tunaset_ubuntu_apt;;5)neteaseset_ubuntu_apt;;6)sohuset_ubuntu_apt;;7)njuset_ubuntu_apt;;8)ustcset_ubuntu_apt;;9)sjtuset_ubuntu_apt;;10)xjtuset_ubuntu_apt;;11)bfsuset_ubuntu_apt;;12)bjtuset_ubuntu_apt;;13)pkuset_ubuntu_apt;;14)break;;*)${COLOR}"输入错误,请输入正确的数字(1-14)!"${END};;esacdone
}set_debian_apt(){mv /etc/apt/sources.list /etc/apt/sources.list.bakcat > /etc/apt/sources.list <<-EOF
deb https://${MIRROR}/debian/ $(lsb_release -cs) main contrib non-free non-free-firmware
# deb-src https://${MIRROR}/debian/ $(lsb_release -cs) main contrib non-free non-free-firmwaredeb https://${MIRROR}/debian/ $(lsb_release -cs)-updates main contrib non-free non-free-firmware
# deb-src https://${MIRROR}/debian/ $(lsb_release -cs)-updates main contrib non-free non-free-firmwaredeb https://${MIRROR}/debian/ $(lsb_release -cs)-backports main contrib non-free non-free-firmware
# deb-src https://${MIRROR}/debian/ $(lsb_release -cs)-backports main contrib non-free non-free-firmwaredeb https://${MIRROR}/debian-security $(lsb_release -cs)-security main contrib non-free non-free-firmware
# deb-src https://${MIRROR}/debian-security $(lsb_release -cs)-security main contrib non-free non-free-firmware
EOF${COLOR}"更新镜像源中,请稍等..."${END}apt update &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} APT源设置完成!"${END}
}debian_menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)阿里镜像源
2)华为镜像源
3)腾讯镜像源
4)清华镜像源
5)网易镜像源
6)搜狐镜像源
7)南京大学镜像源
8)中科大镜像源
9)上海交通大学镜像源
10)西安交通大学镜像源
11)北京外国语大学镜像源
12)北京交通大学镜像源
13)北京大学镜像源
14)退出
EOFecho -e '\E[0m'read -p "请输入镜像源编号(1-14): " NUMcase ${NUM} in1)aliyunset_debian_apt;;2)huaweiset_debian_apt;;3)tencentset_debian_apt;;4)tunaset_debian_apt;;5)neteaseset_debian_apt;;6)sohuset_debian_apt;;7)njuset_debian_apt;;8)ustcset_debian_apt;;9)sjtuset_debian_apt;;10)xjtuset_debian_apt;;11)bfsuset_debian_apt;;12)bjtuset_debian_apt;;13)pkuset_debian_apt;;14)break;;*)${COLOR}"输入错误,请输入正确的数字(1-14)!"${END};;esacdone
}set_mirror_repository(){if [ ${OS_ID} == "Rocky" ];thenrocky_menuelif [ ${OS_ID} == "AlmaLinux" ];thenalmalinux_menuelif [ ${OS_ID} == "CentOS" ];thencentos_menuelif [ ${OS_ID} == "Ubuntu" ];thenapt_menuelsedebian_menufi
}rocky_almalinux_centos_minimal_install(){${COLOR}'开始安装“Minimal安装建议安装软件包”,请稍等......'${END}yum -y 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 wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} Minimal安装建议安装软件包已安装完成!"${END}
}ubuntu_debian_minimal_install(){${COLOR}'开始安装“Minimal安装建议安装软件包”,请稍等......'${END}apt -y 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${COLOR}"${OS_ID} ${OS_RELEASE} Minimal安装建议安装软件包已安装完成!"${END}
}minimal_install(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenrocky_almalinux_centos_minimal_installelseubuntu_debian_minimal_installfi
}disable_firewalls(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenrpm -q firewalld &> /dev/null && { systemctl disable --now firewalld &> /dev/null; ${COLOR}"${OS_ID} ${OS_RELEASE} Firewall防火墙已关闭!"${END}; } || ${COLOR}"${OS_ID} ${OS_RELEASE} iptables防火墙已关闭!"${END}elif [ ${OS_ID} == "Ubuntu" ];thendpkg -s ufw &> /dev/null && { systemctl disable --now ufw &> /dev/null; ${COLOR}"${OS_ID} ${OS_RELEASE} ufw防火墙已关闭!"${END}; } || ${COLOR}"${OS_ID} ${OS_RELEASE}  没有ufw防火墙服务,不用关闭!"${END}else${COLOR}"${OS_ID} ${OS_RELEASE}  没有安装防火墙服务,不用关闭!"${END}fi
}disable_selinux(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenif [ `getenforce` == "Enforcing" ];thensed -ri.bak 's/^(SELINUX=).*/\1disabled/' /etc/selinux/config${COLOR}"${OS_ID} ${OS_RELEASE} SELinux已禁用,请重新启动系统后才能生效!"${END}else${COLOR}"${OS_ID} ${OS_RELEASE} SELinux已被禁用,不用设置!"${END}fielse${COLOR}"${OS_ID} ${OS_RELEASE} SELinux默认没有安装,不用设置!"${END}fi
}set_swap(){sed -ri 's/.*swap.*/#&/' /etc/fstabif [ ${OS_ID} == "Ubuntu" ];thenif [ ${OS_RELEASE_VERSION} == 20 -o ${OS_RELEASE_VERSION} == 22 -o ${OS_RELEASE_VERSION} == 24 ];thenSD_NAME=`lsblk|awk -F"[ └─]" '/SWAP/{printf $3}'`systemctl mask dev-${SD_NAME}.swap &> /dev/nullfifiswapoff -a${COLOR}"${OS_ID} ${OS_RELEASE} 禁用swap成功!"${END}
}set_localtime(){ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeecho 'Asia/Shanghai' >/etc/timezoneif [ ${OS_ID} == "Ubuntu" ];thencat >> /etc/default/locale <<-EOF
LC_TIME=en_DK.UTF-8
EOFfi${COLOR}"${OS_ID} ${OS_RELEASE} 系统时区已设置成功,请重启系统后生效!"${END}
}set_limits(){cat >> /etc/security/limits.conf <<-EOF
root     soft   core     unlimited
root     hard   core     unlimited
root     soft   nproc    1000000
root     hard   nproc    1000000
root     soft   nofile   1000000
root     hard   nofile   1000000
root     soft   memlock  32000
root     hard   memlock  32000
root     soft   msgqueue 8192000
root     hard   msgqueue 8192000
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 优化资源限制参数成功!"${END}
}set_kernel(){modprobe  br_netfiltercat > /etc/sysctl.conf <<-EOF
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296# TCP kernel paramater
net.ipv4.tcp_mem = 786432 1048576 1572864
net.ipv4.tcp_rmem = 4096        87380   4194304
net.ipv4.tcp_wmem = 4096        16384   4194304
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_sack = 1# socket buffer
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 20480
net.core.optmem_max = 81920# TCP conn
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_syn_retries = 3
net.ipv4.tcp_retries1 = 3
net.ipv4.tcp_retries2 = 15# tcp conn reuse
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_timestamps = 0net.ipv4.tcp_max_tw_buckets = 20000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syncookies = 1# keepalive conn
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.ip_local_port_range = 10001    65000# swap
vm.overcommit_memory = 0
vm.swappiness = 10#net.ipv4.conf.eth1.rp_filter = 0
#net.ipv4.conf.lo.arp_ignore = 1
#net.ipv4.conf.lo.arp_announce = 2
#net.ipv4.conf.all.arp_ignore = 1
#net.ipv4.conf.all.arp_announce = 2
EOFMAIN_KERNEL=`uname -r | cut -d. -f1`SUB_KERNEL=`uname -r | cut -d. -f2`if [ ${MAIN_KERNEL} -lt "4" -a ${SUB_KERNEL} -lt "12" ];thencat >> /etc/sysctl.conf <<-EOF	
net.ipv4.tcp_tw_recycle = 0
EOFfisysctl -p &> /dev/null${COLOR}"${OS_ID} ${OS_RELEASE} 优化内核参数成功!"${END}
}optimization_ssh(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thensed -ri.bak -e 's/^#(UseDNS).*/\1 no/' -e 's/^(GSSAPIAuthentication).*/\1 no/' /etc/ssh/sshd_configelsesed -ri.bak -e 's/^#(UseDNS).*/\1 no/' -e 's/^#(GSSAPIAuthentication).*/\1 no/' /etc/ssh/sshd_configfiif [ ${OS_ID} == "Ubuntu" ];thenif [ ${OS_RELEASE_VERSION} == 24 ];thensystemctl restart sshfielsesystemctl restart sshdfi${COLOR}"${OS_ID} ${OS_RELEASE} SSH已优化完成!"${END}
}set_ssh_port(){disable_selinuxdisable_firewallread -p "请输入端口号: " PORTsed -i 's/#Port 22/Port '${PORT}'/' /etc/ssh/sshd_configif [ ${OS_ID} == "Ubuntu" ];thenif [ ${OS_RELEASE_VERSION} == 24 ];thensystemctl restart sshfielsesystemctl restart sshdfi${COLOR}"${OS_ID} ${OS_RELEASE} 更改SSH端口号已完成,请重新登陆后生效!"${END}
}set_rocky_almalinux_centos_alias(){ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`ETHNAME2=`ip addr | awk -F"[ :]" '/^3/{print $3}'`IP_NUM=`ip addr | awk -F"[: ]" '{print $1}' | grep -v '^$' | wc -l`if [ ${IP_NUM} == "2" ];thenif [ ${OS_RELEASE_VERSION} == "7" -o ${OS_RELEASE_VERSION} == "8" ];thencat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/sysconfig/network-scripts"
alias cdrepo="cd /etc/yum.repos.d"
alias vie0="vim /etc/sysconfig/network-scripts/ifcfg-${ETHNAME}"
EOFelsecat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/NetworkManager/system-connections"
alias cdrepo="cd /etc/yum.repos.d"
alias vie0="vim /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection"
EOFfielse	if [ ${OS_RELEASE_VERSION} == "7" -o ${OS_RELEASE_VERSION} == "8" ];thencat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/sysconfig/network-scripts"
alias cdrepo="cd /etc/yum.repos.d"
alias vie0="vim /etc/sysconfig/network-scripts/ifcfg-${ETHNAME}"
alias vie1="vim /etc/sysconfig/network-scripts/ifcfg-${ETHNAME2}"
EOFelsecat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/NetworkManager/system-connections"
alias cdrepo="cd /etc/yum.repos.d"
alias vie0="vim /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection"
alias vie1="vim /etc/NetworkManager/system-connections/${ETHNAME2}.nmconnection"
EOFfifiDISK_NAME=`lsblk|awk -F" " '/disk/{printf $1}' | cut -c1-4`if [ ${DISK_NAME} == "sda" ];thencat >>~/.bashrc <<-EOF
alias scandisk="echo '- - -' > /sys/class/scsi_host/host0/scan;echo '- - -' > /sys/class/scsi_host/host1/scan;echo '- - -' > /sys/class/scsi_host/host2/scan"
EOFfi${COLOR}"${OS_ID} ${OS_RELEASE} 系统别名已设置成功,请重新登陆后生效!"${END}
}set_ubuntu_alias(){cat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/netplan"
alias cdapt="cd /etc/apt"
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 系统别名已设置成功,请重新登陆后生效!"${END}
}set_debian_alias(){cat >>~/.bashrc <<-EOF
alias cdnet="cd /etc/network"
alias cdapt="cd /etc/apt"
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 系统别名已设置成功,请重新登陆后生效!"${END}
}set_alias(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenif grep -Eqi "(.*cdnet|.*cdrepo|.*vie0|.*vie1|.*scandisk)" ~/.bashrc;thensed -i -e '/.*cdnet/d'  -e '/.*cdrepo/d' -e '/.*vie0/d' -e '/.*vie1/d' -e '/.*scandisk/d' ~/.bashrcset_rocky_almalinux_centos_aliaselseset_rocky_almalinux_centos_aliasfielif [ ${OS_ID} == "Ubuntu" ];thenif grep -Eqi "(.*cdnet|.*cdapt)" ~/.bashrc;thensed -i -e '/.*cdnet/d' -e '/.*cdapt/d' ~/.bashrcset_ubuntu_aliaselseset_ubuntu_aliasfielseif grep -Eqi "(.*cdnet|.*cdapt)" ~/.bashrc;thensed -i -e '/.*cdnet/d' -e '/.*cdapt/d' ~/.bashrcset_debian_aliaselseset_debian_aliasfifi
}set_vimrc(){read -p "请输入作者名: " AUTHORread -p "请输入QQ号: " QQread -p "请输入网址: " V_MIRRORcat >~/.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:        ${AUTHOR}")call setline(5,"#QQ:            ${QQ}")call setline(6,"#Date:          ".strftime("%Y-%m-%d"))call setline(7,"#FileName:      ".expand("%"))call setline(8,"#MIRROR:           ${V_MIRROR}")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${COLOR}"${OS_ID} ${OS_RELEASE} vimrc设置完成,请重新系统启动才能生效!"${END}
}set_mail(){                                                                                                 if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenrpm -q postfix &> /dev/null || { yum -y install postfix &> /dev/null; systemctl enable --now postfix &> /dev/null; }rpm -q mailx &> /dev/null || yum -y install mailx &> /dev/nullelsedpkg -s mailutils &> /dev/null || apt -y install mailutilsfiread -p "请输入邮箱地址: " MAILread -p "请输入邮箱授权码: " AUTHSMTP=`echo ${MAIL} |awk -F"@" '{print $2}'`cat >~/.mailrc <<-EOF
set from=${MAIL}
set smtp=smtp.${SMTP}
set smtp-auth-user=${MAIL}
set smtp-auth-password=${AUTH}
set smtp-auth=login
set ssl-verify=ignore
EOF${COLOR}"${OS_ID} ${OS_RELEASE} 邮件设置完成,请重新登录后才能生效!"${END}
}red(){P_COLOR=31
}green(){P_COLOR=32
}yellow(){P_COLOR=33
}blue(){P_COLOR=34
}violet(){P_COLOR=35
}cyan_blue(){P_COLOR=36
}random_color(){P_COLOR="$[RANDOM%7+31]"
}rocky_almalinux_centos_ps1(){C_PS1=$(echo "PS1='\[\e[1;${P_COLOR}m\][\u@\h \W]\\$ \[\e[0m\]'" >> ~/.bashrc)
}ubuntu_debian_ps1(){U_PS1=$(echo 'PS1="\[\e[1;'''${P_COLOR}'''m\]${debian_chroot:+($debian_chroot)}\u@\h:\w\\$ \[\e[0m\]"' >> ~/.bashrc)
}set_ps1_env(){if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "AlmaLinux" -o ${OS_ID} == "CentOS" ];thenif grep -Eqi "^PS1" ~/.bashrc;thensed -i '/^PS1/d' ~/.bashrcrocky_almalinux_centos_ps1elserocky_almalinux_centos_ps1fielseif grep -Eqi "^PS1" ~/.bashrc;thensed -i '/^PS1/d' ~/.bashrcubuntu_debian_ps1elseubuntu_debian_ps1fifi
}set_ps1(){TIPS="${COLOR}${OS_ID} ${OS_RELEASE} PS1设置成功,请重新登录生效!${END}"while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
1)31 红色
2)32 绿色
3)33 黄色
4)34 蓝色
5)35 紫色
6)36 青色
7)随机颜色
8)退出
EOFecho -e '\E[0m'read -p "请输入颜色编号(1-8): " NUMcase ${NUM} in1)redset_ps1_env${TIPS};;2)greenset_ps1_env${TIPS};;3)yellowset_ps1_env${TIPS};;4)blueset_ps1_env${TIPS};;5)violetset_ps1_env${TIPS};;6)cyan_blueset_ps1_env${TIPS};;7)random_colorset_ps1_env${TIPS};;8)break;;*)${COLOR}"输入错误,请输入正确的数字(1-8)!"${END};;esacdone
}set_vim(){echo "export EDITOR=vim" >> ~/.bashrc
}set_vim_env(){if grep -Eqi ".*EDITOR" ~/.bashrc;thensed -i '/.*EDITOR/d' ~/.bashrcset_vimelseset_vimfi${COLOR}"${OS_ID} ${OS_RELEASE} 默认文本编辑器设置成功,请重新登录生效!"${END}
}set_history(){echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bashrc 
}set_history_env(){if grep -Eqi ".*HISTTIMEFORMAT" ~/.bashrc;thensed -i '/.*HISTTIMEFORMAT/d' ~/.bashrcset_historyelseset_historyfi${COLOR}"${OS_ID} ${OS_RELEASE} history格式设置成功,请重新登录生效!"${END}
}disable_restart(){if [ -f /usr/lib/systemd/system/ctrl-alt-del.target ];thencp /usr/lib/systemd/system/ctrl-alt-del.target{,.bak}rm -f /usr/lib/systemd/system/ctrl-alt-del.target${COLOR}"${OS_ID} ${OS_RELEASE} 禁用ctrl+alt+del重启处理成功!"${END}else${COLOR}"${OS_ID} ${OS_RELEASE} 禁用ctrl+alt+del已处理!"${END}fi
}set_ubuntu_debian_root_login(){if [ ${OS_ID} == "Ubuntu" -o ${OS_ID} == "Debian" ];thenread -p "请输入密码: " PASSWORDecho ${PASSWORD} |sudo -S sed -ri 's@#(PermitRootLogin )prohibit-password@\1yes@' /etc/ssh/sshd_configif [ ${OS_ID} == "Ubuntu" ];thenif [ ${OS_RELEASE_VERSION} == 24 ];thensudo systemctl restart sshfielsesudo systemctl restart sshdfisudo -S passwd root <<-EOF
${PASSWORD}
${PASSWORD}
EOF${COLOR}"${OS_ID} ${OS_RELEASE} root用户登录已设置完成,请重新登录后生效!"${END}else${COLOR}"${OS_ID} ${OS_RELEASE} 系统不可用!"${END}fi
}ubuntu_remove(){if [ ${OS_ID} == "Ubuntu" ];thenapt -y purge ufw lxd lxd-client lxcfs liblxc-common${COLOR}"${OS_ID} ${OS_RELEASE} 无用软件包卸载完成!"${END}else${COLOR}"${OS_ID} ${OS_RELEASE} 系统不可用!"${END}fi
}ubuntu_20_22_24_remove_snap(){dpkg -s snapd &> /dev/nullif [ $? -eq 1 ];then ${COLOR}"${OS_ID} ${OS_RELEASE} snap已卸载!"${END}elsesystemctl disable snapd.service && systemctl disable snapd.socket && systemctl disable snapd.seeded.servicesum=$(snap list | awk 'NR>=2{print $1}' | wc -l)while [ $sum -ne 0 ];dofor p in $(snap list | awk 'NR>=2{print $1}'); dosnap remove --purge $pdonesum=$(snap list | awk 'NR>=2{print $1}' | wc -l)doneapt -y autoremove --purge snapdrm -rf ~/snap && sudo rm -rf /snap && rm -rf /var/snap && rm -rf /var/lib/snapd && rm -rf /var/cache/snapdcat > /etc/apt/preferences.d/no-snapd.pref << EOF
Package: snapd
Pin: release a=*
Pin-Priority: -10
EOFapt update${COLOR}"${OS_ID} ${OS_RELEASE} snap卸载完成!"${END}fi
}ubuntu_remove_snap(){if [ ${OS_ID} == "Ubuntu" ];thenif [ ${OS_RELEASE_VERSION} == 20 -o ${OS_RELEASE_VERSION} == 22 -o ${OS_RELEASE_VERSION} == 24 ];thenubuntu_20_22_24_remove_snapelse${COLOR}"${OS_ID} ${OS_RELEASE} 默认没有安装snap!"${END} fielse${COLOR}"${OS_ID} ${OS_RELEASE} 系统不可用!"${END}fi
}menu(){while true;doecho -e "\E[$[RANDOM%7+31];1m"cat <<-EOF
******************************************************************
*                     系统初始化脚本菜单                         *
* 1.修改网卡名                 15.设置系统别名                   *
* 2.设置网络(单网卡)           16.设置vimrc配置文件              *
* 3.设置网络(双网卡)           17.安装邮件服务并配置邮件         *
* 4.设置主机名                 18.设置PS1(请进入选择颜色)        *
* 5.设置镜像仓库               19.设置默认文本编辑器为vim        *
* 6.Minimal安装建议安装软件    20.设置history格式                *
* 7.关闭防火墙                 21.禁用ctrl+alt+del重启           *
* 8.禁用SELinux                22.Ubuntu和Debian设置root用户登录 *
* 9.禁用SWAP                   23.Ubuntu卸载无用软件包           *
* 10.设置系统时区              24.Ubuntu卸载snap                 *
* 11.优化资源限制参数          25.重启系统                       *
* 12.优化内核参数              26.关机                           *
* 13.优化SSH                   27.退出                           *
* 14.更改SSH端口号                                               *
******************************************************************
EOFecho -e '\E[0m'read -p "请选择相应的编号(1-27): " choicecase ${choice} in1)set_eth;;2)set_network;;3)set_dual_network;;4)set_hostname;;5)set_mirror_repository;;6)minimal_install;;7)disable_firewalls;;8)disable_selinux;;9)set_swap;;10)set_localtime;;11)set_limits;;12)set_kernel;;13)optimization_ssh;;14)set_ssh_port;;15)set_alias;;16)set_vimrc;;17)set_mail;;18)set_ps1;;19)set_vim_env;;20)set_history_env;;21)disable_restart;;22)set_ubuntu_debian_root_login;;23)ubuntu_remove;;24)ubuntu_remove_snap;;25)reboot;;26)shutdown -h now;;27)break;;*)${COLOR}"输入错误,请输入正确的数字(1-27)!"${END};;esacdone
}main(){osmenu
}main

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/471310.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

EXCEL延迟退休公式

如图&#xff1a; A B为手工输入 C2EOMONTH(A2,B2*12) D2EOMONTH(C2,IF(C2>DATEVALUE("2025-1-1"),INT((DATEDIF(DATEVALUE("2025-1-1"),C2,"m")4)/4),0)) E2EOMONTH(A2,B2*12IF(EOMONTH(A2,B2*12)>DATEVALUE("2025-1-1"),INT(…

ARM架构中断与异常向量表机制解析

往期内容 本专栏往期内容&#xff0c;interrtupr子系统&#xff1a; 深入解析Linux内核中断管理&#xff1a;从IRQ描述符到irq domain的设计与实现Linux内核中IRQ Domain的结构、操作及映射机制详解中断描述符irq_desc成员详解Linux 内核中断描述符 (irq_desc) 的初始化与动态分…

论文翻译 | The Capacity for Moral Self-Correction in Large Language Models

摘要 我们测试了一个假设&#xff0c;即通过人类反馈强化学习&#xff08;RLHF&#xff09;训练的语言模型具有“道德自我纠正”的能力——避免产生有害的输出——如果指示这样做的话。我们在三个不同的实验中发现了支持这一假设的有力证据&#xff0c;每个实验都揭示了道德自…

华为云前台用户可挂载数据盘和系统盘是怎么做到的?

用户可以选择磁盘类型和容量&#xff0c;其后台是管理员对接存储设备 1.管理员如何在后台对接存储设备&#xff08;特指业务存储&#xff09; 1.1FusionSphere CPS&#xff08;Cloud Provisionivice&#xff09;云装配服务 它是first node https://10.200.4.159:8890 对接存…

【Excel】身份证号最后一位“X”怎么计算

大多数人身份证号最后一位都是数字&#xff0c;但有个别号码最后一位却是“X"。 如果你查百度&#xff0c;会得到如下答案&#xff1a; 当最后一位编码是10的时候&#xff0c;因为多出一位&#xff0c;所以就用X替换。 可大多数人不知道的是&#xff0c;这个10是怎么来的…

【常见问题解答】远程桌面无法复制粘贴的解决方法

提示:“奔跑吧邓邓子” 的常见问题专栏聚焦于各类技术领域常见问题的解答。涵盖操作系统(如 CentOS、Linux 等)、开发工具(如 Android Studio)、服务器软件(如 Zabbix、JumpServer、RocketMQ 等)以及远程桌面、代码克隆等多种场景。针对如远程桌面无法复制粘贴、Kuberne…

python解析网页上的json数据落地到EXCEL

安装必要的库 import requests import pandas as pd import os import sys import io import urllib3 import json测试数据 网页上的数据结构如下 {"success": true,"code": "CIFM_0000","encode": null,"message": &quo…

change buffer:到底应该选择普通索引还是唯一索引

文章目录 引言第一章&#xff1a;普通索引和唯一索引在查询逻辑与效率上的对比1.1 查询逻辑分析1.2 查询效率对比 第二章&#xff1a;普通索引和唯一索引在更新逻辑与效率上的对比2.1 更新逻辑分析2.2 更新效率对比 第三章&#xff1a;底层原理详解 - 普通索引和唯一索引的区别…

3D编辑器教程:如何实现3D模型多材质定制效果?

想要实现下图这样的产品DIY定制效果&#xff0c;该如何实现&#xff1f; 可以使用51建模网线上3D编辑器的材质替换功能&#xff0c;为产品3D模型每个部位添加多套材质贴图&#xff0c;从而让3D模型在展示时实现DIY定制效果。 具体操作流程如下&#xff1a; 第1步&#xff1a;上…

git入门环境搭建

git下载 git官网地址&#xff1a;https://git-scm.com/ 如果没有魔法的话&#xff0c;官网这个地址能卡死你 这里给个国内的git镜像链接 git历史版本镜像链接 然后一路next 默认路径 默认勾选就行。 今天就写到这吧&#xff0c;11点多了该睡了&#xff0c;&#xff0c;&#x…

Oracle ADB 导入 BANK_GRAPH 的学习数据

Oracle ADB 导入 BANK_GRAPH 的学习数据 1. 下载数据2. 导入数据运行 setconstraints.sql 1. 下载数据 访问 https://github.com/oracle-quickstart/oci-arch-graph/tree/main/terraform/scripts&#xff0c;下载&#xff0c; bank_accounts.csvbank_txns.csvsetconstraints.…

985研一学习日记 - 2024.11.14

一个人内耗&#xff0c;说明他活在过去&#xff1b;一个人焦虑&#xff0c;说明他活在未来。只有当一个人平静时&#xff0c;他才活在现在。 日常 1、起床6:00 2、健身2h 3、LeetCode刷了题 动态规划概念 如果某一问题有很多重叠子问题&#xff0c;使用动态规划是最有效的…

1.两数之和-力扣(LeetCode)

题目&#xff1a; 解题思路&#xff1a; 在解决这个问题之前&#xff0c;首先要明确两个点&#xff1a; 1、参数returnSize的含义是返回答案的大小&#xff08;数目&#xff09;&#xff0c;由于这里的需求是寻找数组中符合条件的两个数&#xff0c;那么当找到这两个数时&#…

【excel】easy excel如何导出动态列

动态也有多重含义&#xff1a;本文将描述两种动态场景下的解决方案 场景一&#xff1a;例如表头第一列固定为动物&#xff0c;且必定有第二列&#xff0c;第二列的表头可能为猫 也可能为狗&#xff1b;这是列数固定&#xff0c;列名不固定的场景&#xff1b; 场景二&#xff1…

〔 MySQL 〕数据类型

目录 1.数据类型分类 2 数值类型 2.1 tinyint类型 2.2 bit类型 2.3 小数类型 2.3.1 float 2.3.2 decimal 3 字符串类型 3.1 char 3.2 varchar 3.3 char和varchar比较 4 日期和时间类型 5 enum和set mysql表中建立属性列&#xff1a; 列名称&#xff0c;类型在后 n…

LlamaIndex

一、大语言模型开发框架 SDK:Software Development Kit,它是一组软件工具和资源的集合,旨在帮助开发者创建、测试、部署和维护应用程序或软件。 所有开发框架(SDK)的核心价值,都是降低开发、维护成本。 大语言模型开发框架的价值,是让开发者可以更方便地开发基于大语言…

【FFmpeg】FFmpeg 函数简介 ③ ( 编解码相关函数 | FFmpeg 源码地址 | FFmpeg 解码器相关 结构体 和 函数 )

文章目录 一、FFmpeg 解码器简介1、解码流程分析2、FFmpeg 编解码器 本质3、FFmpeg 编解码器 ID 和 名称 二、FFmpeg 解码器相关 结构体 / 函数1、AVFormatContext 结构体2、avcodec_find_decoder 函数 - 根据 ID 查找 解码器3、avcodec_find_decoder_by_name 函数 - 根据 名称…

Linux——GPIO输入输出裸机实验

学习了正点原子Linux环境下的GPIO的输入输出的裸机实验学习&#xff0c;现在进行一下小结&#xff1a; 启动文件start.S的编写 .global _start .global _bss_start _bss_start:.word __bss_start.global _bss_end _bss_end:.word __bss_end_start:/*设置处理器进入SVC模式*/m…

zabbix搭建钉钉告警流程

目录 &#x1f324;️zabbix实验规划 &#x1f324;️zabbix实验步骤 &#x1f4d1;1 使用钉钉添加一个自定义的机器人 ​ &#x1f4d1;2在zabbix-server上编写钉钉信息发送脚本&#xff0c;设置钉钉报警媒介 ☁️ 设置钉钉报警媒介​编辑​编辑 ☁️在添加消息模板​编辑​…

Java 多线程(三)—— 死锁

死锁的产生 我们先从简单的死锁最后到难一些的死锁问题开始展开讨论。 首先一个线程&#xff0c;一把锁&#xff0c;因为多次加锁而导致死锁问题&#xff0c;由于Java 的synchronized 实现了可重入锁&#xff0c;因此这个死锁问题就不存在了&#xff0c;意味着当一个线程拥有…