📄 脚本文档:switch_mirrors.sh
1. 使用环境
硬件:Orange Pi 5 Plus(RK3588)
操作系统:Armbian 26.5.1 (Resolute),基于 Ubuntu 26.04
软件源格式:DEB822(
.sources文件),位于/etc/apt/sources.list.d/架构:
arm64(aarch64)
2. 脚本功能
自动备份原有源配置文件(带时间戳)
用户可选择国内镜像源(清华/阿里云/华为云/中科大/腾讯云/官方源)
正确替换
ubuntu.sources和armbian.sources自动执行
apt update验证避开阿里云 Armbian 源 404 问题(阿里云选项仅修改 Ubuntu 源)
使用
printf写入,避免 heredoc 格式错误
3. 使用限制
仅适用于使用 DEB822 格式的 Armbian/Ubuntu 系统(如当前环境)。传统
sources.list格式的系统不适用。仅适用于 Ubuntu 26.04 (resolute) 及相近版本(因为
resolute是开发代号,部分镜像站可能同步延迟,若出现 404 需自行调整Suites值)。腾讯云选项不提供 Armbian 镜像,会保留官方源。
需要 root 权限(脚本内部使用
sudo,但建议以 root 执行)。
4. 脚本源码(带注释)
bash
#!/bin/bash
# ============================================================
# 文件名: switch_mirrors.sh
# 描述: 一键切换 Orange Pi 5 Plus (Armbian) 国内镜像源
# 作者: 用户定制
# 适用: Armbian 26.5.1 (resolute) / Ubuntu 26.04
# 用法: sudo bash switch_mirrors.sh
# ============================================================
set -e # 遇到错误立即退出
# 颜色定义(用于美化输出)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# ---------- 1. 备份原有配置 ----------
BACKUP_DIR="/etc/apt/sources.list.d/backup.$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"
cp /etc/apt/sources.list.d/*.sources "$BACKUP_DIR/" 2>/dev/null || true
echo -e "${GREEN}>>> 已备份原有源配置到 $BACKUP_DIR${NC}"
# ---------- 2. 显示菜单并读取用户选择 ----------
echo -e "${YELLOW}请选择要切换的国内镜像源:${NC}"
echo "1) 清华大学 (tuna) - 推荐(同时更换 Ubuntu 和 Armbian 源)"
echo "2) 阿里云 (aliyun) - 仅更换 Ubuntu 源(Armbian 源保持官方,避免 404)"
echo "3) 华为云 (huaweicloud) - 同时更换 Ubuntu 和 Armbian 源"
echo "4) 中科大 (ustc) - 同时更换 Ubuntu 和 Armbian 源"
echo "5) 腾讯云 (tencent) - 仅更换 Ubuntu 源(Armbian 源保持官方)"
echo "6) 恢复原始配置(取消切换)"
read -p "请输入选项 [1-6]: " choice
# ---------- 3. 根据选项设置镜像 URL ----------
case $choice in
1)
UBUNTU_URI="https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports"
ARMBIAN_URI="https://mirrors.tuna.tsinghua.edu.cn/armbian"
MODIFY_ARMBIAN=1
;;
2)
UBUNTU_URI="https://mirrors.aliyun.com/ubuntu-ports"
MODIFY_ARMBIAN=0
echo -e "${YELLOW}注意:阿里云 Armbian 源不稳定,将保留官方源(http://apt.armbian.com)${NC}"
;;
3)
UBUNTU_URI="https://mirrors.huaweicloud.com/ubuntu-ports"
ARMBIAN_URI="https://mirrors.huaweicloud.com/armbian"
MODIFY_ARMBIAN=1
;;
4)
UBUNTU_URI="https://mirrors.ustc.edu.cn/ubuntu-ports"
ARMBIAN_URI="https://mirrors.ustc.edu.cn/armbian"
MODIFY_ARMBIAN=1
;;
5)
UBUNTU_URI="https://mirrors.tencent.com/ubuntu-ports"
MODIFY_ARMBIAN=0
echo -e "${YELLOW}腾讯云未提供 Armbian 镜像,将保留官方源${NC}"
;;
6)
echo -e "${RED}已取消操作,未做任何更改。${NC}"
exit 0
;;
*)
echo -e "${RED}无效选项,退出。${NC}"
exit 1
;;
esac
# ---------- 4. 修改 Ubuntu 源 ----------
echo -e "${GREEN}>>> 配置 Ubuntu 源...${NC}"
printf "Types: deb\nURIs: %s\nSuites: resolute resolute-security resolute-updates resolute-backports\nComponents: main restricted universe multiverse\nSigned-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg\n" "$UBUNTU_URI" > /etc/apt/sources.list.d/ubuntu.sources
# ---------- 5. 修改 Armbian 源(如果需要) ----------
if [ "$MODIFY_ARMBIAN" -eq 1 ]; then
echo -e "${GREEN}>>> 配置 Armbian 源...${NC}"
printf "Types: deb\nURIs: %s\nSuites: resolute\nComponents: main resolute-utils resolute-desktop\nSigned-By: /usr/share/keyrings/armbian-archive-keyring.gpg\n" "$ARMBIAN_URI" > /etc/apt/sources.list.d/armbian.sources
else
echo -e "${YELLOW}>>> 跳过修改 Armbian 源,保持原有配置(官方源)${NC}"
fi
# armbian-config.sources 指向 GitHub,无国内镜像,保留不动
echo -e "${GREEN}>>> armbian-config.sources 未作修改${NC}"
# ---------- 6. 更新软件源 ----------
echo -e "${GREEN}>>> 执行 apt update ...${NC}"
apt update
# ---------- 7. 完成提示 ----------
echo -e "${GREEN}✅ 镜像源切换完成!${NC}"
echo -e "${YELLOW}提示:若要安装闭源 rknpu 驱动,请先运行 armbian-config 切换到 vendor 内核。${NC}"5. 使用方法
将上述脚本保存为
switch_mirrors.sh(例如放在/root/或/home/liao/)。赋予执行权限:
bash
chmod +x switch_mirrors.sh以 root 或 sudo 执行:
bash
sudo ./switch_mirrors.sh根据菜单输入数字,等待脚本自动完成。
6. 恢复原始源
如果需要恢复切换前的源配置,可以执行:
bash
# 找到最近的备份目录
ls -td /etc/apt/sources.list.d/backup.* | head -1 | xargs -I {} cp {}/* /etc/apt/sources.list.d/
apt update7. 常见问题
8. 注意事项
本脚本不会修改
/etc/apt/sources.list(传统格式),因为 Armbian 默认使用 DEB822 格式。如果将来 Armbian 升级到新版本(如 27.x),需要将脚本中的
resolute替换为新版本代号。建议在更换源后,执行
apt upgrade -y更新系统软件包。