From ff83fa9e7408bc88e2404848ab27d52d0ab66b15 Mon Sep 17 00:00:00 2001 From: ert Date: Mon, 22 Sep 2025 05:40:12 +0000 Subject: [PATCH] first commit --- README.md | 123 +++++++++++++ ubuntu_manager.sh | 459 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 582 insertions(+) create mode 100644 README.md create mode 100755 ubuntu_manager.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..62c3cde --- /dev/null +++ b/README.md @@ -0,0 +1,123 @@ +# Ubuntu 24.04 换源和升级工具 + +这是一个专为 Ubuntu 24.04 LTS 设计的换源和系统升级脚本,提供了友好的菜单界面和完整的功能。 + +## 功能特性 + +- 🔄 **多镜像源支持**:支持阿里云、清华大学、中科大、华为云等国内镜像源 +- 🚀 **一键系统升级**:自动更新软件包列表、升级系统、清理无用包 +- 📋 **菜单式操作**:直观的交互界面,操作简单 +- 🔒 **安全备份**:自动备份原始源配置,支持一键恢复 +- 🧹 **清理重复源**:避免重复源配置,确保系统稳定 +- ✅ **错误处理**:完善的错误检查和用户提示 + +## 使用方法 + +### 1. 赋予执行权限 +```bash +chmod +x ubuntu_manager.sh +``` + +### 2. 运行脚本(需要root权限) +```bash +sudo ./ubuntu_manager.sh +``` + +### 3. 选择操作 +脚本会显示以下菜单选项: + +``` +================================================ + Ubuntu 24.04 换源和升级工具 +================================================ + +1. 换源到阿里云镜像 +2. 换源到清华大学镜像 +3. 换源到中科大镜像 +4. 换源到华为云镜像 +5. 系统升级 +6. 显示当前软件源 +7. 恢复原始软件源 +8. 一键换源并升级(推荐阿里云) +0. 退出 + +================================================ +``` + +## 功能说明 + +### 换源功能(选项1-4) +- 自动备份当前的 sources.list 文件 +- 完全替换为选定的镜像源配置 +- 清理 sources.list.d 目录中的其他源文件(重命名为 .disabled) +- 自动更新软件包列表 + +### 系统升级功能(选项5) +- 更新软件包列表 +- 升级已安装的软件包 +- 执行完整系统升级(包括内核) +- 清理不需要的软件包 +- 检查是否需要重启系统 + +### 显示当前软件源(选项6) +- 显示当前 sources.list 文件的有效配置 +- 过滤注释和空行,只显示实际的源配置 + +### 恢复原始软件源(选项7) +- 列出所有可用的备份文件 +- 允许用户选择要恢复的备份 +- 自动更新软件包列表 + +### 一键换源并升级(选项8) +- 自动换源到阿里云镜像(推荐) +- 完成换源后自动执行系统升级 +- 适合新系统初始化使用 + +## 支持的镜像源 + +| 镜像源 | 地址 | 特点 | +|--------|------|------| +| 阿里云 | mirrors.aliyun.com | 速度快,稳定性好 | +| 清华大学 | mirrors.tuna.tsinghua.edu.cn | 教育网友好,更新及时 | +| 中科大 | mirrors.ustc.edu.cn | 老牌镜像,可靠性高 | +| 华为云 | mirrors.huaweicloud.com | 企业级服务,速度稳定 | + +## 注意事项 + +1. **权限要求**:脚本需要 root 权限运行,请使用 `sudo` 执行 +2. **系统版本**:专为 Ubuntu 24.04 LTS 设计,其他版本可能需要调整 +3. **网络连接**:确保网络连接正常,以便下载更新 +4. **备份重要性**:脚本会自动备份,但建议在重要系统上先手动备份 +5. **重启提醒**:系统升级后可能需要重启,请注意保存工作 + +## 故障排除 + +### 权限错误 +```bash +# 如果遇到权限错误,确保使用 sudo +sudo ./ubuntu_manager.sh +``` + +### 网络连接问题 +```bash +# 测试网络连接 +ping -c 4 mirrors.aliyun.com +``` + +### 恢复到官方源 +如果需要恢复到 Ubuntu 官方源,可以使用脚本的恢复功能,或手动编辑: +```bash +sudo cp /etc/apt/sources.list.backup.* /etc/apt/sources.list +sudo apt update +``` + +## 版本信息 + +- **版本**:1.0 +- **适用系统**:Ubuntu 24.04 LTS +- **作者**:SOLO Coding +- **更新日期**:2024年 + +## 许可证 + +本脚本遵循 MIT 许可证,可自由使用和修改。 \ No newline at end of file diff --git a/ubuntu_manager.sh b/ubuntu_manager.sh new file mode 100755 index 0000000..8f94efa --- /dev/null +++ b/ubuntu_manager.sh @@ -0,0 +1,459 @@ +#!/bin/bash + +# Ubuntu 24.04 换源和自动升级脚本 +# 作者: SOLO Coding +# 版本: 1.0 +# 适用于: Ubuntu 24.04 LTS + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${BLUE}[INFO]${NC} $1" +} + +log_success() { + echo -e "${GREEN}[SUCCESS]${NC} $1" +} + +log_warning() { + echo -e "${YELLOW}[WARNING]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 检查是否为root用户 +check_root() { + if [[ $EUID -ne 0 ]]; then + log_error "此脚本需要root权限运行,请使用 sudo 执行" + exit 1 + fi +} + +# 检查Ubuntu版本 +check_ubuntu_version() { + if ! grep -q "Ubuntu 24.04" /etc/os-release; then + log_warning "检测到非Ubuntu 24.04系统,脚本可能无法正常工作" + read -p "是否继续执行?(y/N): " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + exit 1 + fi + fi +} + +# 备份原始sources.list +backup_sources() { + local backup_file="/etc/apt/sources.list.backup.$(date +%Y%m%d_%H%M%S)" + if [[ -f /etc/apt/sources.list ]]; then + cp /etc/apt/sources.list "$backup_file" + log_success "已备份原始sources.list到: $backup_file" + fi +} + +# 彻底清理所有第三方源文件 +clean_all_sources() { + log_info "正在彻底清理所有第三方源文件..." + + # 清理sources.list.d目录中的所有源文件 + if [[ -d /etc/apt/sources.list.d ]]; then + # 备份sources.list.d目录 + local sources_d_backup="/etc/apt/sources.list.d.backup.$(date +%Y%m%d_%H%M%S)" + cp -r /etc/apt/sources.list.d "$sources_d_backup" 2>/dev/null || true + log_info "已备份sources.list.d目录到: $sources_d_backup" + + # 禁用所有第三方源文件 + find /etc/apt/sources.list.d -name "*.list" -exec mv {} {}.disabled \; 2>/dev/null || true + find /etc/apt/sources.list.d -name "*.sources" -exec mv {} {}.disabled \; 2>/dev/null || true + + log_success "已清理sources.list.d目录中的所有第三方源文件" + fi + + # 清理可能的缓存 + rm -rf /var/lib/apt/lists/* 2>/dev/null || true + log_info "已清理APT缓存" +} + +# 验证当前源配置 +verify_single_source() { + local expected_mirror="$1" + local source_name="$2" + + log_info "正在验证 $source_name 源配置..." + + # 检查sources.list文件 + if [[ ! -f /etc/apt/sources.list ]]; then + log_error "sources.list文件不存在" + return 1 + fi + + # 统计不同镜像源的数量 + local active_sources=$(grep -v '^#' /etc/apt/sources.list | grep -v '^$' | grep -c '^deb' || echo "0") + local expected_sources=$(grep -v '^#' /etc/apt/sources.list | grep -v '^$' | grep -c "$expected_mirror" || echo "0") + + if [[ $active_sources -eq $expected_sources && $expected_sources -gt 0 ]]; then + log_success "源验证通过:当前使用的是纯净的 $source_name 镜像源(共 $expected_sources 个源)" + return 0 + else + log_warning "源验证失败:检测到混合源配置(总源数:$active_sources,$source_name 源数:$expected_sources)" + return 1 + fi +} + +# 换源到阿里云镜像 +change_to_aliyun() { + log_info "正在切换到阿里云镜像源..." + backup_sources + + # 彻底清理所有第三方源 + clean_all_sources + + # 清空并重写sources.list文件 + cat > /etc/apt/sources.list << 'EOF' +# ============================================================ +# 阿里云Ubuntu 24.04 LTS镜像源 - 纯净单一源配置 +# 此文件由ubuntu_manager.sh自动生成,请勿手动编辑 +# 镜像提供商: 阿里云 (Alibaba Cloud) +# 镜像地址: http://mirrors.aliyun.com/ubuntu/ +# 配置时间: $(date '+%Y-%m-%d %H:%M:%S') +# ============================================================ + +# 主要软件仓库 +deb http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse + +# 安全更新仓库 +deb http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse + +# 推荐更新仓库 +deb http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse + +# 后移植软件仓库 +deb http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse +EOF + + log_success "已切换到阿里云镜像源" + + # 验证源配置 + if verify_single_source "mirrors.aliyun.com" "阿里云"; then + update_package_list + else + log_error "源配置验证失败,请检查配置" + return 1 + fi +} + +# 换源到清华大学镜像 +change_to_tsinghua() { + log_info "正在切换到清华大学镜像源..." + backup_sources + + # 彻底清理所有第三方源 + clean_all_sources + + # 清空并重写sources.list文件 + cat > /etc/apt/sources.list << 'EOF' +# ============================================================ +# 清华大学Ubuntu 24.04 LTS镜像源 - 纯净单一源配置 +# 此文件由ubuntu_manager.sh自动生成,请勿手动编辑 +# 镜像提供商: 清华大学 (Tsinghua University) +# 镜像地址: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ +# 配置时间: $(date '+%Y-%m-%d %H:%M:%S') +# ============================================================ + +# 主要软件仓库 +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse + +# 安全更新仓库 +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-security main restricted universe multiverse + +# 推荐更新仓库 +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse + +# 后移植软件仓库 +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse +EOF + + log_success "已切换到清华大学镜像源" + + # 验证源配置 + if verify_single_source "mirrors.tuna.tsinghua.edu.cn" "清华大学"; then + update_package_list + else + log_error "源配置验证失败,请检查配置" + return 1 + fi +} + +# 换源到中科大镜像 +change_to_ustc() { + log_info "正在切换到中科大镜像源..." + backup_sources + + # 彻底清理所有第三方源 + clean_all_sources + + # 清空并重写sources.list文件 + cat > /etc/apt/sources.list << 'EOF' +# ============================================================ +# 中科大Ubuntu 24.04 LTS镜像源 - 纯净单一源配置 +# 此文件由ubuntu_manager.sh自动生成,请勿手动编辑 +# 镜像提供商: 中国科学技术大学 (USTC) +# 镜像地址: https://mirrors.ustc.edu.cn/ubuntu/ +# 配置时间: $(date '+%Y-%m-%d %H:%M:%S') +# ============================================================ + +# 主要软件仓库 +deb https://mirrors.ustc.edu.cn/ubuntu/ noble main restricted universe multiverse + +# 安全更新仓库 +deb https://mirrors.ustc.edu.cn/ubuntu/ noble-security main restricted universe multiverse + +# 推荐更新仓库 +deb https://mirrors.ustc.edu.cn/ubuntu/ noble-updates main restricted universe multiverse + +# 后移植软件仓库 +deb https://mirrors.ustc.edu.cn/ubuntu/ noble-backports main restricted universe multiverse +EOF + + log_success "已切换到中科大镜像源" + + # 验证源配置 + if verify_single_source "mirrors.ustc.edu.cn" "中科大"; then + update_package_list + else + log_error "源配置验证失败,请检查配置" + return 1 + fi +} + +# 换源到华为云镜像 +change_to_huawei() { + log_info "正在切换到华为云镜像源..." + backup_sources + + # 彻底清理所有第三方源 + clean_all_sources + + # 清空并重写sources.list文件 + cat > /etc/apt/sources.list << 'EOF' +# ============================================================ +# 华为云Ubuntu 24.04 LTS镜像源 - 纯净单一源配置 +# 此文件由ubuntu_manager.sh自动生成,请勿手动编辑 +# 镜像提供商: 华为云 (Huawei Cloud) +# 镜像地址: https://mirrors.huaweicloud.com/ubuntu/ +# 配置时间: $(date '+%Y-%m-%d %H:%M:%S') +# ============================================================ + +# 主要软件仓库 +deb https://mirrors.huaweicloud.com/ubuntu/ noble main restricted universe multiverse + +# 安全更新仓库 +deb https://mirrors.huaweicloud.com/ubuntu/ noble-security main restricted universe multiverse + +# 推荐更新仓库 +deb https://mirrors.huaweicloud.com/ubuntu/ noble-updates main restricted universe multiverse + +# 后移植软件仓库 +deb https://mirrors.huaweicloud.com/ubuntu/ noble-backports main restricted universe multiverse +EOF + + log_success "已切换到华为云镜像源" + + # 验证源配置 + if verify_single_source "mirrors.huaweicloud.com" "华为云"; then + update_package_list + else + log_error "源配置验证失败,请检查配置" + return 1 + fi +} + +# 更新软件包列表 +update_package_list() { + log_info "正在更新软件包列表..." + if apt update; then + log_success "软件包列表更新成功" + else + log_error "软件包列表更新失败" + return 1 + fi +} + +# 系统升级 +system_upgrade() { + log_info "开始系统升级..." + + # 更新软件包列表 + if ! update_package_list; then + return 1 + fi + + # 升级已安装的软件包 + log_info "正在升级已安装的软件包..." + if apt upgrade -y; then + log_success "软件包升级完成" + else + log_error "软件包升级失败" + return 1 + fi + + # 完整系统升级(包括内核等) + log_info "正在进行完整系统升级..." + if apt full-upgrade -y; then + log_success "完整系统升级完成" + else + log_error "完整系统升级失败" + return 1 + fi + + # 清理不需要的软件包 + log_info "正在清理不需要的软件包..." + apt autoremove -y + apt autoclean + + log_success "系统升级完成!" + + # 检查是否需要重启 + if [[ -f /var/run/reboot-required ]]; then + log_warning "系统升级完成,建议重启系统以应用所有更改" + read -p "是否现在重启?(y/N): " -n 1 -r + echo + if [[ $REPLY =~ ^[Yy]$ ]]; then + log_info "系统将在5秒后重启..." + sleep 5 + reboot + fi + fi +} + +# 显示当前源信息 +show_current_sources() { + log_info "当前软件源配置:" + echo "----------------------------------------" + if [[ -f /etc/apt/sources.list ]]; then + grep -v '^#' /etc/apt/sources.list | grep -v '^$' + else + log_warning "未找到sources.list文件" + fi + echo "----------------------------------------" +} + +# 恢复原始源 +restore_original_sources() { + log_info "查找备份文件..." + local backup_files=($(ls /etc/apt/sources.list.backup.* 2>/dev/null | sort -r)) + + if [[ ${#backup_files[@]} -eq 0 ]]; then + log_error "未找到备份文件" + return 1 + fi + + echo "找到以下备份文件:" + for i in "${!backup_files[@]}"; do + echo "$((i+1)). ${backup_files[i]}" + done + + read -p "请选择要恢复的备份文件编号 (1-${#backup_files[@]}): " choice + + if [[ $choice -ge 1 && $choice -le ${#backup_files[@]} ]]; then + local selected_backup="${backup_files[$((choice-1))]}" + cp "$selected_backup" /etc/apt/sources.list + log_success "已恢复备份: $selected_backup" + update_package_list + else + log_error "无效的选择" + return 1 + fi +} + +# 显示主菜单 +show_menu() { + clear + echo -e "${BLUE}================================================${NC}" + echo -e "${BLUE} Ubuntu 24.04 换源和升级工具${NC}" + echo -e "${BLUE}================================================${NC}" + echo + echo -e "${GREEN}1.${NC} 换源到阿里云镜像" + echo -e "${GREEN}2.${NC} 换源到清华大学镜像" + echo -e "${GREEN}3.${NC} 换源到中科大镜像" + echo -e "${GREEN}4.${NC} 换源到华为云镜像" + echo -e "${GREEN}5.${NC} 系统升级" + echo -e "${GREEN}6.${NC} 显示当前软件源" + echo -e "${GREEN}7.${NC} 恢复原始软件源" + echo -e "${GREEN}8.${NC} 一键换源并升级(推荐阿里云)" + echo -e "${GREEN}0.${NC} 退出" + echo + echo -e "${BLUE}================================================${NC}" +} + +# 一键换源并升级 +quick_setup() { + log_info "开始一键换源并升级..." + change_to_aliyun + if [[ $? -eq 0 ]]; then + system_upgrade + else + log_error "换源失败,跳过升级步骤" + fi +} + +# 主程序 +main() { + # 检查权限和系统版本 + check_root + check_ubuntu_version + + while true; do + show_menu + read -p "请选择操作 (0-8): " choice + + case $choice in + 1) + change_to_aliyun + ;; + 2) + change_to_tsinghua + ;; + 3) + change_to_ustc + ;; + 4) + change_to_huawei + ;; + 5) + system_upgrade + ;; + 6) + show_current_sources + ;; + 7) + restore_original_sources + ;; + 8) + quick_setup + ;; + 0) + log_info "感谢使用,再见!" + exit 0 + ;; + *) + log_error "无效的选择,请重新输入" + ;; + esac + + echo + read -p "按回车键继续..." -r + done +} + +# 脚本入口点 +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + main "$@" +fi \ No newline at end of file