#!/bin/bash # ================================================================= # Debian 12 (bookworm) APT 源自动配置脚本(阿里云镜像) # 功能: # 1. 备份现有的 sources.list 文件 # 2. 使用阿里云镜像源覆盖 sources.list # 3. 执行 apt update # 4. 安装 curl # ================================================================= SOURCES_FILE="/etc/apt/sources.list" BACKUP_FILE="${SOURCES_FILE}.bak.$(date +%Y%m%d-%H%M%S)" read -r -d '' NEW_SOURCES_CONTENT << 'EOF' # ===================================================== # Debian 12 (bookworm) - Aliyun Mirrors # ===================================================== deb https://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware # deb-src https://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware deb https://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware # deb-src https://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware deb https://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware # deb-src https://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware EOF echo "===== [0/4] 检查运行权限 =====" if [ "$(id -u)" -ne 0 ]; then echo "错误:此脚本必须以 root 用户身份运行。" >&2 echo "请使用 'sudo bash update_sources_debian12.sh' 执行。" >&2 exit 1 fi echo "权限检查通过。" echo "" echo "===== [1/4] 备份原始源文件 =====" if [ -f "$SOURCES_FILE" ]; then cp "$SOURCES_FILE" "$BACKUP_FILE" echo "原始文件已备份到: $BACKUP_FILE" else echo "原始文件不存在,将创建新文件。" fi echo "" echo "===== [2/4] 写入新的阿里云镜像源 =====" echo "$NEW_SOURCES_CONTENT" > "$SOURCES_FILE" echo "新的源配置已写入 $SOURCES_FILE" echo "" echo "===== [3/4] 执行 apt update =====" apt-get update if [ $? -ne 0 ]; then echo "错误:apt update 执行失败!" >&2 echo "原始 sources.list 已备份在:$BACKUP_FILE" >&2 exit 1 fi echo "软件包列表更新成功。" echo "" echo "===== [4/4] 安装 curl =====" apt-get install -y curl if ! command -v curl >/dev/null 2>&1; then echo "错误:curl 安装失败!" >&2 exit 1 fi echo "Curl 已成功安装!" curl --version echo "" echo "脚本执行完毕,Debian 12 阿里云源配置成功!"