当前位置: 首页 > 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
    
http://www.lryc.cn/news/576581.html

相关文章:

  • InnoDB的undo日志涉及的页结构
  • C语言二级指针与多级指针
  • 国内公司把数据湖做成了数据库
  • uni-app项目实战笔记27--uniapp搜索页面的实现
  • 手势-handpose的pipeline介绍
  • nt!IoSynchronousPageWrite函数分析之atapi!IdeReadWrite----非常重要
  • 视频序列中的帧间匹配技术 FrameMatcher 详解
  • 智能制造——56页2025 智慧工厂解决方案【附全文阅读】
  • zookeeper Curator(3):Watch事件监听
  • 从单体架构到微服务:微服务架构演进与实践
  • 从台式电脑硬件架构看前后端分离开发模式
  • Spring Boot 3 多数据源改造全流程:Druid、HikariCP 与 dynamic-datasource 实战总结
  • 内网横向-工作流
  • 典型工程应用三
  • [rootme:ctf all the day]Ubuntu 8.04week wp
  • python 项目利用uv管理python包依赖
  • phpstudy 可以按照mysql 数据库
  • cf 禁止http/1.0和http/1.1的访问 是否会更安全?
  • 《自动控制原理 》- 第 1 章 自动控制的基本原理与方式
  • Confluence-测试用例执行规范
  • srs-gb28181 与 SRS 5.0 对 GB28181 国标支持
  • Learning to Prompt for Continual Learning
  • python基础21(2025.6.28)_全栈爬取_车168以及诗词名句案例
  • AUTOSAR图解==>AUTOSAR_AP_EXP_SOVD
  • Linux快速查找文件
  • JVM 之双亲委派机制与打破双亲委派
  • 【安卓Sensor框架-2】应用注册Sensor 流程
  • Everything
  • 深入解析 Electron 核心模块:构建跨平台桌面应用的关键
  • day45 Tensor board使用介绍