#!/bin/bash # 设置设备IP后使用此脚本 # 可以先 yum install lrzsz -y 然后使用 rz 将此脚本上传到设备上, chmod +x centosinit.sh 给此脚本执行权限,再 ./centosinit.sh 进行第三方包和软件的安装 # 检测当前系统是否为 CentOS 7 系列,若是则自动备份 yum 源并用内嵌的华为云 CentOS-Base.repo 替换 if [ -f /etc/redhat-release ] && grep -q "CentOS" /etc/redhat-release && grep -qE "release 7\." /etc/redhat-release; then echo "检测到 CentOS 7 系列,开始检查 yum 源..." if [ -f /etc/yum.repos.d/CentOS-Base.repo ] && grep -q "repo.huaweicloud.com" /etc/yum.repos.d/CentOS-Base.repo; then echo "yum 源已替换为华为云镜像,无需重复替换" else # 备份原有 yum 源 if [ -f /etc/yum.repos.d/CentOS-Base.repo ]; then echo "备份原有 yum 源 CentOS-Base.repo ..." cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak if [ $? -ne 0 ]; then echo "备份 yum 源失败" exit 1 fi fi # 写入内嵌的华为云 CentOS-Base.repo echo "写入新的 CentOS-Base.repo ..." cat > /etc/yum.repos.d/CentOS-Base.repo <<'REPOEOF' # CentOS-Base.repo # # The mirror system uses the connecting IP address of the client and the # update status of each mirror to pick mirrors that are updated to and # geographically close to the client. You should use this for CentOS updates # unless you are manually picking other mirrors. # # If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead. # # [base] name=CentOS-$releasever - Base - repo.huaweicloud.com baseurl=https://repo.huaweicloud.com/centos/$releasever/os/$basearch/ #mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os gpgcheck=1 gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7 #released updates [updates] name=CentOS-$releasever - Updates - repo.huaweicloud.com baseurl=https://repo.huaweicloud.com/centos/$releasever/updates/$basearch/ #mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates gpgcheck=1 gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that may be useful [extras] name=CentOS-$releasever - Extras - repo.huaweicloud.com baseurl=https://repo.huaweicloud.com/centos/$releasever/extras/$basearch/ #mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras gpgcheck=1 gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that extend functionality of existing packages [centosplus] name=CentOS-$releasever - Plus - repo.huaweicloud.com baseurl=https://repo.huaweicloud.com/centos/$releasever/centosplus/$basearch/ #mirrorlist=https://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus gpgcheck=1 enabled=0 gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7 REPOEOF if [ $? -ne 0 ]; then echo "写入 CentOS-Base.repo 失败" exit 1 fi # 清理旧的 yum 缓存,避免仍指向失效镜像 echo "清理 yum 缓存..." yum clean all rm -rf /var/cache/yum/* fi else echo "非 CentOS 7 系列,跳过 yum 源替换" fi echo "update now, this may take a minute..." yum upgrade -y if [ $? -ne 0 ]; then echo "yum upgrade failed" exit 1 fi echo "install fonts now" yum groupinstall -y "Fonts" if [ $? -ne 0 ]; then echo "install fonts failed" exit 1 fi packages=( net-tools # lrzsz # tcpdump # libaio # psmisc # lsof # bridge-utils # wget # strace # cryptsetup # net-snmp-utils # freeradius-utils # unzip # socat # traceroute # sysstat # iotop # gtk3 # vim # telnet # docker # java # java-devel # unixODBC # ) for(( i=0; i<${#packages[@]}; i++)) do echo "install ${packages[i]} now" yum install -y ${packages[i]} if [ $? -ne 0 ]; then echo "install ${packages[i]} failed" exit 1 fi done #兆芯设备安装麒麟23年server系统时,需要安装指定版本内核才不会导致系统网络io异常 if nkvers | grep -q "x86_64" && nkvers | grep -q "4.19.90" && lscpu | grep -q "ZHAOXIN"; then yum install -y kernel-4.19.90-52.42.v2207.ky10 else echo "非特定设备与系统组合,不需要安装指定版本内核" fi echo "Initialization completed, everything is ok!"