ubuntu一键自动换源脚本

# ubuntu 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


aptAlibaba(){
aptubuntu22="
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
"

aptubuntu21="
deb http://mirrors.aliyun.com/ubuntu/ hirsute main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ hirsute-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ hirsute-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ hirsute-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ hirsute-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ hirsute main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ hirsute-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ hirsute-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ hirsute-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ hirsute-backports main restricted universe multiverse
"

aptubuntu20="
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

# deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
"

aptubuntu18="
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

# deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
"
aptubuntu16="
deb http://mirrors.aliyun.com/ubuntu/ xenial main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main

deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main

deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe

deb http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe
"

aptubuntu14="
deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

## Not recommended
# deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
"

}

aptupdateqinghua(){
# 清华 apt 源:http://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

# 注意:定义变量的时候引号需要换行,否则原本的换行将被替换为空格
aptubuntu22="
# 默认注释了源码镜像以提高 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="
# 默认注释了源码镜像以提高 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="
# 默认注释了源码镜像以提高 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="
# 默认注释了源码镜像以提高 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="
# 默认注释了源码镜像以提高 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
  sudo timedatectl set-timezone Asia/Shanghai
  sudo ntpdate -u cn.pool.ntp.org
}

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 "选择目的地源: 阿里云1  清华2  腾讯3  华为4  网易5  任意按键跳过配置 apt 源 "  ; read -p "请选择:" readset

  case $readset in
    "1")  echo "当前切换到阿里云源" ; aptAlibaba  ;;
    "2")  echo "当前切换到清华源,较慢" ; aptupdateqinghua ;;
    "3")  echo "当前切换到腾讯云源"    ; aptAlibaba ; releaseApt=1 ; destAptUrl="mirrors.tencent.com" ;;
    "4")  echo "当前切换到华为云源"    ; aptAlibaba ; releaseApt=1 ; destAptUrl="repo.huaweicloud.com" ;;
    "5")  echo "当前切换到网易163源"   ; aptAlibaba ; releaseApt=1 ; destAptUrl="mirrors.163.com" ;;

    *)    echo "我已经配置 apt 源。正在继续下面任务... " ;;
  esac

  case $version in 
    "22") echo "$aptubuntu22" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu22 apt源  " ;;
    "21") echo "$aptubuntu21" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu21 apt源  " ;;
    "20") echo "$aptubuntu20" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu20 apt源  " ;;
    "18") echo "$aptubuntu18" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu18 apt源  " ;;
    "16") echo "$aptubuntu16" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu16 apt源  " ;;
    "14") echo "$aptubuntu14" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu14 apt源  " ;;
  esac

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

}

sudo_install(){

if [ "$USER" != "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 超级权限,继续往下配置..."

    sudo touch /etc/apt/sources.list
    sudo chmod 777 -R /etc/apt/*  
    if [ ! -e /etc/apt/sources.list.bak ]
    then 
      sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
      echo "备份文件 在: /etc/apt/sources.list.bak"
    else
      echo "已经存在备份文件:/etc/apt/sources.list.bak"
    fi

    echo "" >  /etc/apt/sources.list ; ubuntu_release_setApt 
    # 文件已经附加了权限,删除后文件权限为默认,而不为 777 。如果删除重写,记得重新附加权限 777 
    sudo rm -rf /var/lib/dpkg/updates/*
    # 解决  sudo dpkg --configure -a  # to correct the problem
    ubuntu_ntpdate

    sudo apt update ; sudo apt update ; sudo apt upgrade -y ; 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
    done    
    
    sudo apt update ; sudo apt update ; sudo apt upgrade -y ; 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 "指令序号: 1 配置ubuntu 的apt 仓库源;   2 安装 kvm 虚拟机;   3 跳过此步骤,往下继续"
  read -p "请输入数字:"  selectRun

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

sudo apt-get --fix-broken install
# 解决问题:Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution)

 

版权声明:
作者:tianya
链接:https://tya.zone/p/45.html
来源:天涯博客
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
海报
ubuntu一键自动换源脚本
# ubuntu 22/21/20/18/16/14 自动切换 apt 阿里云/清华/腾讯/华为/网易163 源; update 更新,自动部署可 kvm 虚拟化,安装google 浏览器;安装 mp4 播放器 ……
<<上一篇
下一篇>>