ubuntu 24/23/22/21/20/18/16/14 自动切换 apt 阿里云/清华/腾讯/华为/网易163 源

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


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

aptubuntu24="
# ubuntu24
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse
"

aptubuntu23="
# ubuntu23
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ lunar main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ lunar main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ lunar-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ lunar-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ lunar-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ lunar-backports main restricted universe multiverse

# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ lunar-security main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ lunar-security main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ lunar-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ lunar-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ lunar-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ lunar-proposed main restricted universe multiverse
"

aptubuntu22="
# ubuntu22
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
"

aptubuntu21="
# ubuntu21
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-security main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-proposed main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ impish-proposed main restricted universe multiverse
"

aptubuntu20="
# ubuntu20
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
"

aptubuntu18="
# ubuntu18
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
"

aptubuntu16="
# ubuntu16
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
"

aptubuntu14="
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse
"
}

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)
}

ubuntu_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.huawei  5.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 "当前切换到华为云源"     ; aptupdateqinghua ; releaseApt=1 ; destAptUrl="repo.huaweicloud.com" ;;
    "5")  echo "当前切换到网易163源"    ; aptupdateqinghua ; releaseApt=1 ; destAptUrl="mirrors.163.com" ;;
    *)    echo "我已经配置 apt 源。正在继续下面任务... " ;;
  esac

  case $version in 
    "24") echo "$aptubuntu24" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu $version  apt源  " ;;
    "23") echo "$aptubuntu23" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu $version  apt源  " ;;
    "22") echo "$aptubuntu22" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu $version  apt源  " ;;
    "21") echo "$aptubuntu21" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu $version  apt源  " ;;
    "20") echo "$aptubuntu20" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu $version  apt源  " ;;
    "18") echo "$aptubuntu18" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu $version  apt源  " ;;
    "16") echo "$aptubuntu16" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu $version  apt源  " ;;
    "14") echo "$aptubuntu14" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu $version  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 ; ubuntu_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

  ubuntu_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
}

ubuntu_aptupdate(){
  echo "select:     1.config ubuntu 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(){
  ubuntu_aptupdate
  apt_install

  echo "如果需要更新所有内核软件执行: sudo apt upgrade -y "
  echo "do you update kernel :    sudo apt upgrade -y " 
}
main


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