动态 版块 发帖 消息 我的
小绿叶技术博客
小绿叶技术博客
小绿叶技术Lv154   
ubuntu 22/21/20/18/16/14 自动切换 apt 阿里云/清华/腾讯/华为/网易163 源; update 更新,自动部署可 kvm 虚拟化     


#!/bin/bash
# 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


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

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  5.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 
    "23") echo "$aptubuntu23" > /etc/apt/sources.list ; echo "当前机型: $models $version 已经写入 aptubuntu22 apt源  " ;;
    "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.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 超级权限,继续往下配置..."

    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

      if [ $i != root ]
      then
          echo "useradd $i   Please set the password $i: "
          useradd -m -d /home/$i -s /bin/bash $i; passwd $i
      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 "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
}
main


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








 2  已被阅读了1278次  楼主 2022-12-09 13:32:50
回复列表
admin
2F
小绿叶技术Lv154   
ubuntu ISO 官方下载地址:  https://cn.ubuntu.com/download/desktop
 0   2024-04-25 14:22:56  回复

回复:ubuntu 22/21/20/18/16/14 自动切换 apt 阿里云/清华/腾讯/华为/网易163 源; update 更新,自动部署可 kvm 虚拟化

guest
登录之后更精彩~
Powered by HadSky 7.12.10
© 2015-2024 PuYueTian
您的IP:44.200.86.95,2024-05-30 04:21:13,Processed in 0.01957 second(s).
Powered by HadSky
小绿叶技术博客