debian 12/11/10 自动切换 apt 阿里云/清华/腾讯/华为/网易163 源

2024-07-03 14:25:54 浏览数 (3)

代码语言:shell复制
#!/bin/bash
# debian 12/11/10 自动切换 apt 阿里云/清华/腾讯/华为/网易163  源; update 更新,自动部署可 kvm 虚拟化,安装google 浏览器;安装 mp4 播放器
# 执行脚本选择对应的 apt 源码,下面 aptdebian12 为apt 源 内容修改写入到:/etc/apt/sources.list 然后执行 sudo apt update 进行更新
echo "当前需要输入当前用户的密码,需要执行 sudo 命令" ; sudo date


aptupdateqinghua(){
# 清华 apt 源手动选择 debian 版本链接:https://mirrors.tuna.tsinghua.edu.cn/help/debian/
# 注意:定义变量的时候引号需要换行,否则原本的换行将被替换为空格

aptdebian12="
# debian12
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
# deb-src https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
"

aptdebian11="
# debian11
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb https://security.debian.org/debian-security bullseye-security main contrib non-free
# deb-src https://security.debian.org/debian-security bullseye-security main contrib non-free
"

aptdebian10="
# debian10
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb https://security.debian.org/debian-security buster/updates main contrib non-free
# deb-src https://security.debian.org/debian-security buster/updates main contrib non-free
"


}

ubuntu_ntpdate(){
# 更新时间
  sudo apt install ntpdate -y
  sudo timedatectl set-timezone Asia/Shanghai
  sudo ntpdate -u cn.pool.ntp.org
  sudo apt-get --fix-broken install -y
# 问题:Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution)
}

debian_release_setApt(){

  releaseApt=0;
  models=`cat /etc/os-release | grep -w NAME | awk -F""" '{print $2}'`
  version=`cat /etc/os-release | grep VERSION_ID | awk -F""" '{print $2}' | awk -F"." '{print $1}'`
  echo "当前机型为:$models 系统版本: $version "
  echo "select apt update:  1.qinghua   2.Alibaba   3.tencent   4.163  任意按键exit"  ; read -p "select number : " readset

  case $readset in
    "1")  echo "当前切换到清华源,较慢" ; aptupdateqinghua ;;
    "2")  echo "当前切换到阿里云源"     ; aptupdateqinghua ; releaseApt=1 ; destAptUrl="mirrors.aliyun.com" ;;
    "3")  echo "当前切换到腾讯云源"     ; aptupdateqinghua ; releaseApt=1 ; destAptUrl="mirrors.tencent.com" ;;
    "4")  echo "当前切换到网易163源"    ; aptupdateqinghua ; releaseApt=1 ; destAptUrl="mirrors.163.com" ;;
    *)    echo "我已经配置 apt 源。正在继续下面任务... " ;;
  esac

  case $version in 
    "12") echo "$aptdebian12" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptdebian12 apt源  " ;;
    "11") echo "$aptdebian11" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptdebian11 apt源  " ;;
    "10") echo "$aptdebian10" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptdebian10 apt源  " ;;
  esac

  if [ $releaseApt = 1 ]
  then 
    sudo sed -i "s/mirrors.tuna.tsinghua.edu.cn/$destAptUrl/g" /etc/apt/sources.list
  fi

}

sudo_install(){

if [ `id -un` != "root" ]
then
  echo "当前不是 root 用户,需要检测 sudo 工具是否安装,并且 获取 sudo "
  a=`sudo apt list --installed | grep sudo | grep now`
  if [ ${#a} -lt 1 ]
  then
    echo " 当前检测到 您没有安装 sudo 超级权限工具,并且当前也不是 root 用户,没有权限更改apt 源,请切换到root 用户执行本shell 按照操作添加用户的sudo 权限"
    exit
  else
    echo "[ ok ] 当前用户  $USER 已经安装 sudo 工具,并且已经配置获得 sudo 超级权限,继续往下配置..."

    aptFileList=( sources.list sources.list.d )
    aptdir=/etc/apt
    sudo chmod 777 -R $aptdir 
    sudo touch $aptdir/sources.list

    for aptfile in ${aptFileList[*]}
    do 
      if [[ ! -e $aptdir/$aptfile.tar ]]
      then 
        if [[ -e $aptdir/$aptfile ]]
        then
            sudo tar -cvf $aptdir/$aptfile.tar $aptdir/$aptfile
            echo "正在备份文件 在:$aptdir/$aptfile.tar"
        else
            echo "[warrior] 文件不存在 $aptdir/$aptfile 但不影响配置" ; sleep 3
        fi
      else
        echo "已经存在备份文件:$aptdir/$aptfile.tar"
      fi
    done
    
    echo "" >  /etc/apt/sources.list ; debian_release_setApt 
    # 文件已经附加了权限,删除后文件权限为默认,而不为 777 。如果删除重写,记得重新附加权限 777 
    sudo rm -rf /var/lib/dpkg/updates/*  /etc/apt/sources.list.d    
    # 删除文件夹1 解决  sudo dpkg --configure -a  # to correct the problem
    # 删除文件夹2 列表源, 原因: 安装google浏览器等等其它软件,会自动增加 apt 源 ,导致 apt update 出现由于没有公钥,无法验证下列签名: NO_PUBKEY E88979FB9B30ACF2
    
    ubuntu_ntpdate

    sudo apt update ; sudo apt update  ; sudo apt list --upgradable  
    echo "切换 apt update 源,更新软件包,和 查看可升级软件包完成"
  fi

else
  echo "[ok] 当前是 root 用户"
  if [ ! -e /etc/apt/sources.list.bak ]
  then 
    cp /etc/apt/sources.list /etc/apt/sources.list.bak
    echo "备份文件 在: /etc/apt/sources.list.bak"
  else
    echo "已经存在备份文件:/etc/apt/sources.list.bak"
  fi

  sudo rm -rf /var/lib/dpkg/updates/*
  # 解决  sudo dpkg --configure -a  # to correct the problem

  debian_release_setApt ; ubuntu_ntpdate ; apt update ; apt install -y sudo 
  if [ ! -e /etc/sudoers.d/stack ]
  then
    touch /etc/sudoers.d/stack 
  fi

  echo "为其他用户添加sudo 权限,多个用户空格个开!"
  read -p "输入:" readuser

    userlist=( root $readuser)
  for i in ${userlist[*]}
    do
      catsudo=`cat /etc/sudoers.d/stack | grep $i` 
      if [ ${#catsudo} -lt 27 ]
      then
        echo "$i ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/stack 
      else
        echo "[ok] 已经添加过用户  $i 的 sudo 权限"
      fi

      if [ $i != root ]
      then
          echo "useradd $i   Please set the password $i: "
          useradd -m -d /home/$i -s /bin/bash $i; passwd $i
          
          read -p " Do you want to disable root login ssh? y/n:  "  rootDisableFlag
          if [ "$rootDisableFlag" = "y" ]
          then
              sudo sed -i "s/PermitRootLogin.*/PermitRootLogin no/g" /etc/ssh/sshd_config
              sudo sed -i "s/#PermitRootLogin.*/PermitRootLogin no/g" /etc/ssh/sshd_config
          fi
      fi
      
    done  
    
    sudo apt update ; sudo apt update ; sudo apt list --upgradable  
    echo "切换 apt update 源,更新软件包,和 查看可升级软件包完成"
  
fi

}


install_kvm(){
  installedkvm=` apt list --installed | grep virtinst` ; Length=${#installedkvm}
    if [ $Length -gt 1 ]
    then
      echo "kvm 已经安装。"
    else 
      echo "正在安装kvm ... "
          sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager -y
          sudo systemctl is-active libvirtd
          # 验证安装,注意:qemu-kvm 更名为:qemu-system-x86  卸载软件命令:sudo apt autoremove namepackage
          
          echo "$USER ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/stack   
          #  授予当前用户sudo超级权限。

          sudo usermod -aG libvirt $USER
          sudo usermod -aG kvm $USER
          # $USER 当前用户名,加入组,能够创建和管理虚拟机,否则无权限。然后注销重新登陆
    fi
}

debian_aptupdate(){
  echo "select:     1.config debian apt update     2.install kvm qemu     3 exit"
  read -p "input number: "  selectRun

  case $selectRun in 
    "1")  sudo_install ;;
    "2")  install_kvm  ;;
    *)    echo "已经跳过此步骤!" ;;
  esac
}

apt_install(){
  echo "install  google , qq , wecat, vscode ?"
  read -p "input: y/n ?: " ifapt
  if [[ $ifapt = y ]]
  then
      echo "[runing] install..."
      wget eisc.cn/file/ubuntu/shell/server/vscode.sh; sudo chmod  x vscode.sh ; ./vscode.sh 
      wget eisc.cn/file/ubuntu/shell/server/weixin.sh; sudo chmod  x weixin.sh ; ./weixin.sh 

  else
      echo "[ok] 取消安装必备软件! "
  fi
}

main(){
  debian_aptupdate
  apt_install

  echo "sudo hostnamectl set-hostname debian  更新debian 主机名后,需要在文件 /etc/hosts 的 127.0.0.1 和 ::1 的末尾增加 更改后的主机名,如: debian  否则 sudo 命令很慢"
  echo "如果需要更新所有内核软件执行: sudo apt upgrade -y "
  echo "do you update kernel :    sudo apt upgrade -y " 
  
}
main


# 一键部署:wget http://eisc.cn/file/ubuntu/shell/server/debian_aptupdate.sh ; chmod  x debian_aptupdate.sh ; ./debian_aptupdate.sh 







0 人点赞