Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >debian 12/11/10 自动切换 apt 阿里云/清华/腾讯/华为/网易163 源

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

原创
作者头像
eisc
修改于 2024-07-03 06:25:54
修改于 2024-07-03 06:25:54
1.4K0
举报
代码语言:shell
AI代码解释
复制
#!/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 







原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
树莓派换源
bullseye可以理解为树莓派的系统代号(10,11,12都不同,一定要看好自己系统是什么版本)
云未归来
2025/07/16
1250
树莓派换源
ubuntu 24/23/22/21/20/18/16/14 自动切换 apt 阿里云/清华/腾讯/华为/网易163 源
eisc
2024/06/14
7950
Docker安装kali
a1765e8e381e为你的镜像id,-t 让 docker 分配一个伪终端并绑定到容器的标准输入上 ,-p 指定映射端口,如60001端口映射到虚拟机的 5901 端口,-d 保持后台运行, -c 执行一些命令
sakurajiamai
2022/01/02
3K0
Docker安装kali
树莓派4B搭建homeassistant 开源物联网平台
树莓派官网:https://www.raspberrypi.com/software/operating-systems/
用户8913398
2023/03/05
2.4K0
树莓派4B搭建homeassistant 开源物联网平台
Debian 10 Buster 国内常用镜像源
随着 Debian Buster 10.3 的发布,Debian Buster 日趋稳定。如果在生产上使用了 Debian 9 的系统,现在是可以安全的,灰度升级到 Debian Buster 做前期验证了。这里我们整理一下国内较知名的镜像站点,共大家在生产环境中使用。
Debian中国
2020/02/25
68.3K0
PVE安装和初始设置
PVE: 全称Proxmox Virtual Environment,开源的虚拟化管理平台。它基于Debian Linux操作系统,并集成了KVM和LXC两种虚拟化技术,可以帮助用户快速搭建和管理虚拟化环境。
六月河
2023/07/24
4.8K0
PVE安装和初始设置
常见的dockerfile汇总
python FROM python:3.7-alpine WORKDIR /code ENV FLASK_APP=app.py ENV FLASK_RUN_HOST=0.0.0.0 COPY requirements.txt requirements.txt RUN pip install -r requirements.txt EXPOSE 5000 COPY . . CMD ["flask", "run"] php FROM php:7.2.34-fpm-alpine WORKDIR /app ENV
章工运维
2023/05/19
3700
debian中科大替换教程
一般情况下,将 /etc/apt/sources.list 文件中 Debian 默认的源地址 http://deb.debian.org/ (opens new window) 替换为 http://mirrors.ustc.edu.cn (opens new window) 即可。
章工运维
2023/05/19
8350
基于Debian10.10的开源蜜罐T-Pot 20.06.2安装
蜜罐技术本质上是一种对攻击方进行欺骗的技术,通过布置一些作为诱饵的主机、网络服务或者信息,诱使攻击方对它们实施攻击,从而可以对攻击行为进行捕获和分析,了解攻击方所使用的工具与方法,推测攻击意图和动机,能够让防御方清晰地了解他们所面对的安全威胁,并通过技术和管理手段来增强实际系统的安全防护能力。
yuanfan2012
2021/08/06
7540
基于Debian10.10的开源蜜罐T-Pot 20.06.2安装
我在学校里学习kali linux的日子
近期我得到了一个宝书,作为一个安全圈的老油条(吹水王)我是很兴奋的,这个书张这个样子。
天钧
2019/11/30
1.5K0
腾讯云轻量国内机dd网络在线自定义重装debian
-----------------------------------------------------
软件UP
2021/04/01
2.6K1
腾讯云轻量国内机dd网络在线自定义重装debian
换源系列
注意事项 不支持Arm架构电脑 apt update && apt install apt-transport-https ca-certificates mv /etc/apt/sources.list /etc/apt/sources.list.bak apt update && apt upgrade apt install net-tools iputils-ping vim wget git unzip zip proxychains -y export https_proxy=http://19
XRSec
2022/02/11
2.7K0
树莓派4B安装64位系统 以及基础配置
(在写之前 Imager提示 会擦去sd的所有内容,那好像这个工具也就对sd进行了格式化,前面不需要先格式化也可以。而且Imager 里面提供了很多树莓派的系统可以在这里面下载 也可以提前下载好)
全栈程序员站长
2022/09/01
2.7K0
树莓派4B安装64位系统 以及基础配置
Debian9安装163镜像源
Debian 9(代号为 Stretch),镜像源已由原来的jessie更新为了stretch,为了安全起见需要先备份,再进行配置新的镜像源。修改debian9镜像源方法如下:
Tacc
2022/05/05
2.7K0
Debian9安装163镜像源
树莓派4B安装PyTorch1.10
编译 torch 需要花费大量的内存,在低于 2g 或以下内存的树莓派上,可以通过增加虚拟内存来防止OOM,4g 或 8g 的版本的树莓派可跳过这步。
小锋学长生活大爆炸
2021/11/30
1.2K0
树莓派4B安装PyTorch1.10
更换 PVE7 软件仓库源和 CT模板(LXC)源为国内源
PVE7 安装后默认配置的 apt 软件源和 CT(LXC)容器模板源均是官方默认的,国内使用性能不佳,建议替换为 清华 Tuna 提供的国内镜像源,速度将有一个较大的提升。
宋天伦
2023/10/20
2.2K0
Debian 操作系统安装文档
ufw(Uncomplicated Firewall)是一个简单易用的防火墙管理工具,基于 iptables。下面是一些常见的命令和简单配置步骤,适用于 Debian 系统。
Linux运维技术之路
2025/01/22
2000
Debian 操作系统安装文档
debian更改源和卸载无用服务
初次使用Debian系统呢,上来我们一般要先执行这些操作,更新源或者更换源,删除无用的端口占用服务,还我们一个干净的系统,以下自己的一些使用教程,顺便记录下笔记。
无敌小菜鸟
2021/12/10
3.8K1
debian更改源和卸载无用服务
010.Debian系统基本操作
debian默认不允许使用root用户远程登录,需要修改/etc/ssh/sshd_config文件:
CoderJed
2020/05/04
2K0
magicbook 与debian的故事
安装很慢的原因 选择 高级选项里的exp install安装时,去掉安全源更新。因为安全源是国外的。你选了国内镜像也是从国外下载的。
impdx-忧郁的派大星
2020/11/03
9680
相关推荐
树莓派换源
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档