#!/bin/bash # Debian 国内镜像源更换脚本 # 支持 Debian 9/10/11/12/13 (stretch/buster/bullseye/bookworm/trixie) set -e # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # 检测是否为 root 用户 if [ "$EUID" -ne 0 ]; then echo -e "${RED}错误: 请使用 root 权限运行此脚本${NC}" echo "使用: sudo $0" exit 1 fi # 检测 Debian 版本 detect_debian_version() { if [ -f /etc/debian_version ]; then DEBIAN_VERSION=$(cat /etc/debian_version | cut -d. -f1) if [ -f /etc/os-release ]; then . /etc/os-release CODENAME=$(echo $VERSION_CODENAME | tr '[:upper:]' '[:lower:]') else case $DEBIAN_VERSION in 9) CODENAME="stretch" ;; 10) CODENAME="buster" ;; 11) CODENAME="bullseye" ;; 12) CODENAME="bookworm" ;; 13) CODENAME="trixie" ;; *) CODENAME="bookworm" ;; # 默认使用 bookworm esac fi else echo -e "${RED}错误: 未检测到 Debian 系统${NC}" exit 1 fi } # 显示可用的镜像源 show_mirrors() { echo -e "${YELLOW}可用的国内镜像源:${NC}" echo "1) 阿里云 (aliyun)" echo "2) 清华大学 (tsinghua)" echo "3) 中科大 (ustc)" echo "4) 网易 (163)" echo "5) 华为云 (huawei)" echo "6) 腾讯云 (tencent)" } # 获取镜像源 URL get_mirror_url() { local mirror=$1 local codename=$2 case $mirror in aliyun) echo "https://mirrors.aliyun.com/debian/" ;; tsinghua) echo "https://mirrors.tuna.tsinghua.edu.cn/debian/" ;; ustc) echo "https://mirrors.ustc.edu.cn/debian/" ;; 163) echo "https://mirrors.163.com/debian/" ;; huawei) echo "https://mirrors.huaweicloud.com/debian/" ;; tencent) echo "https://mirrors.cloud.tencent.com/debian/" ;; *) echo "https://mirrors.aliyun.com/debian/" # 默认使用阿里云 ;; esac } # 获取安全更新镜像源 URL get_security_mirror_url() { local mirror=$1 case $mirror in aliyun) echo "https://mirrors.aliyun.com/debian-security/" ;; tsinghua) echo "https://mirrors.tuna.tsinghua.edu.cn/debian-security/" ;; ustc) echo "https://mirrors.ustc.edu.cn/debian-security/" ;; 163) echo "https://mirrors.163.com/debian-security/" ;; huawei) echo "https://mirrors.huaweicloud.com/debian-security/" ;; tencent) echo "https://mirrors.cloud.tencent.com/debian-security/" ;; *) echo "https://mirrors.aliyun.com/debian-security/" # 默认使用阿里云 ;; esac } # 生成 sources.list 内容 generate_sources_list() { local mirror_url=$1 local security_mirror_url=$2 local codename=$3 cat < "$SOURCES_LIST" echo -e "${GREEN}已更新 sources.list${NC}\n" # 更新软件包列表 echo -e "${YELLOW}正在更新软件包列表...${NC}" apt update echo -e "\n${GREEN}✓ 镜像源更换完成!${NC}" echo -e "${GREEN}备份文件位置: ${BACKUP_FILE}${NC}" } # 运行主函数 main