当前位置: 首页 > news >正文

Linux更改国内镜像源

在 Linux 中更换软件源(Repository)是提升软件安装速度和稳定性的重要操作,尤其在中国访问国外官方源可能较慢,建议替换为国内镜像源(如阿里云、清华大学、中科大等)。以下是主流发行版的换源方法:

一、Debian/Ubuntu 系(apt 包管理器)

1. 备份原配置文件
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
2. 编辑源文件
sudo nano /etc/apt/sources.list  # 用 nano 编辑器打开
3. 根据系统版本添加国内镜像源
  • Ubuntu 22.04 (Jammy)

    deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
    
  • Debian 11 (Bullseye)

    deb http://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free
    deb http://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free
    deb http://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free
    deb http://mirrors.ustc.edu.cn/debian-security bullseye-security main contrib non-free
    
4. 更新软件源缓存
sudo apt update  # 更新源索引
sudo apt upgrade  # 升级已安装软件(可选)

二、CentOS/RHEL 系(yum/dnf 包管理器)

1. 备份原配置文件
sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
2. 下载国内镜像源配置
  • CentOS 7

    sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    
  • CentOS 8

    sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
    
3. 生成缓存并更新
sudo yum makecache  # 生成缓存
sudo yum update  # 升级系统(可选)

三、Arch Linux 系(pacman 包管理器)

1. 编辑镜像源配置
sudo nano /etc/pacman.d/mirrorlist
2. 在文件顶部添加中国镜像
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch
Server = https://mirrors.aliyun.com/archlinux/$repo/os/$arch
3. 更新系统
sudo pacman -Syyu  # 刷新源并升级系统

四、其他注意事项

1. 手动选择最快镜像(以 Ubuntu 为例)
# 安装 apt-fast(自动选择最快镜像)
sudo add-apt-repository ppa:apt-fast/stable
sudo apt update && sudo apt install apt-fast -y# 配置 apt-fast 使用 mirrors.aliyun.com
echo 'MIRRORS=( "http://mirrors.aliyun.com/ubuntu/" )' | sudo tee /etc/apt-fast.conf
2. 部分软件单独配置源(以 Docker 为例)
# 添加 Docker 官方源(Ubuntu/Debian)
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
3. 验证源配置是否生效
# 查看当前使用的镜像源(Debian/Ubuntu)
cat /etc/apt/sources.list# 查看已配置的 repo(CentOS)
yum repolist

五、常见问题解决

  1. GPG 密钥验证失败

    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 密钥ID
    
  2. 更新时出现 404 错误
    检查源配置中是否包含不兼容的版本(如 Ubuntu 22.04 使用了 20.04 的源)。

  3. 恢复默认源
    删除自定义配置,重新下载官方配置:

    sudo rm /etc/apt/sources.list  # Debian/Ubuntu
    sudo wget https://mirrors.aliyun.com/repo/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo  # CentOS
    

相关文章:

  • STM32HAL 旋转编码器教程
  • 告别脚本!用浏览器为 AWS CLI 实现真正的 Cognito 单点登录
  • [论文阅读] 软件工程 | 微前端在电商领域的实践:一项案例研究的深度解析
  • COLT_CMDB_linux_zookeeperInfo_20250628.sh
  • Linux离线搭建Redis (centos7)详细操作步骤
  • pytorch 学习-2.线性模型
  • 树莓派学习专题<14>:树莓派4B:从V4L2驱动直接获取h264码流
  • Flutter基础(Isolate)
  • 构建 AI 系统的 4 大 Agentic AI 设计模式
  • ClickHouse基础知识
  • 嵌入式网络通信与物联网协议全解析:Wi-Fi、BLE、LoRa、ZigBee 实战指南
  • golang pprof性能调试工具
  • 内网对抗之信息收集篇
  • Note2.3 机器学习:Adaptive Learning Rate
  • Java 大视界 -- Java 大数据机器学习模型在金融市场高频交易策略优化与风险控制中的应用(327)
  • springboot+Vue逍遥大药房管理系统
  • SmartETL中数据库操作与流程解耦的设计与应用
  • Stewart并联结构两自由度正逆解计算和工作空间仿真
  • 使用asyncio构建高性能网络爬虫
  • for each循环语句