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

如何修改 Ubuntu 软件源(镜像源)

如何修改 Ubuntu 软件源(镜像源)

前言

在使用 Ubuntu 时,默认的软件源可能速度较慢,影响软件安装和系统更新的效率。我们可以通过修改 sources.list 文件或使用图形界面更换更快的镜像源,提升软件包管理的速度。

本指南适用于大部分 Ubuntu 版本,并提供了适配多个版本的方法,方便以后扩展。


1. 通过图形界面更换软件源

如果你更喜欢使用 GUI(图形界面)来修改软件源,可以按照以下步骤操作:

  1. 打开“软件和更新”

    • 点击 “活动”,在搜索框中输入 “软件和更新”(或 Software & Updates),然后打开它。
  2. 更改下载服务器

    • Ubuntu 软件 选项卡中,找到 下载自,点击下拉菜单,选择 其他站点(Other…)
    • 点击 选择最佳服务器(Select Best Server),系统会自动测试速度最快的镜像,选中后点击 选择服务器(Choose Server)
  3. 应用更改

    • 关闭窗口,系统可能会提示你 重新加载(Reload) 软件包信息,点击确认。

2. 备份原始软件源

在修改软件源之前,建议先备份原始的 sources.list 文件,以便在出现问题时可以恢复:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

这样,如果新的软件源不可用,可以恢复原始状态:

sudo cp /etc/apt/sources.list.bak /etc/apt/sources.list

3. 确定 Ubuntu 版本代号

Ubuntu 使用版本代号来标识其软件源,例如:

版本代号
Ubuntu 24.04noble
Ubuntu 22.04jammy
Ubuntu 20.04focal
Ubuntu 18.04bionic

可以使用以下命令查看系统的代号:

lsb_release -sc

4. 修改 sources.list 文件

使用以下命令打开 sources.list 进行编辑:

sudo nano /etc/apt/sources.list

国内推荐镜像源(适用于不同 Ubuntu 版本)

阿里云镜像源:

deb http://mirrors.aliyun.com/ubuntu/ <版本代号> main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ <版本代号>-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ <版本代号>-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ <版本代号>-backports main restricted universe multiverse

清华大学镜像源:

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ <版本代号> main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ <版本代号>-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ <版本代号>-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ <版本代号>-backports main restricted universe multiverse

中科大镜像源:

deb https://mirrors.ustc.edu.cn/ubuntu/ <版本代号> main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ <版本代号>-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ <版本代号>-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ <版本代号>-backports main restricted universe multiverse

示例:
如果你使用的是 Ubuntu 24.04(noble),那么 sources.list 应该修改为:

deb http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse

在这里插入图片描述


5. 保存并更新软件源

完成修改后,按 Ctrl + X 退出 nano 编辑器,按 Y 保存更改。

然后运行以下命令更新软件源信息:

sudo apt update && sudo apt upgrade -y

6. 自动选择最快的软件源(可选)

如果不确定哪个镜像源最快,可以使用 netselect-aptmirrors 自动选择最佳源:

sudo apt install netselect-apt -y
sudo netselect-apt
sudo apt install apt-mirror
apt-mirror

系统会自动检测并生成 sources.list,然后运行:

sudo apt update

7. 恢复默认 Ubuntu 源(如果遇到问题)

如果新的镜像源不可用或有问题,可以恢复默认源。

官方 Ubuntu 源:

deb http://archive.ubuntu.com/ubuntu <版本代号> main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu <版本代号>-updates main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu <版本代号>-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu <版本代号>-backports main restricted universe multiverse

然后执行:

sudo apt update

总结

  1. 使用图形界面(推荐给新手)或手动修改 sources.list 来更换镜像源。
  2. 备份 sources.list,防止出错。
  3. 确定 Ubuntu 版本代号(如 noblejammy)。
  4. 选择合适的国内镜像源(阿里云、清华、中科大等)。
  5. 修改 sources.list 并保存。
  6. 执行 sudo apt update && sudo apt upgrade -y 确保更新。
  7. 遇到问题可以恢复默认源。

通过这些步骤,你可以有效地优化 Ubuntu 的软件源,提高软件更新和安装的速度。如果你在未来使用其他版本 Ubuntu,也只需替换 <版本代号> 即可轻松适配。


http://www.kler.cn/a/584200.html

相关文章:

  • 初阶数据结构(C语言实现)——5.3 堆的应用(1)——堆排序
  • qt5中使用中文报错error: C2001: 常量中有换行符
  • Python入门教程:从零开始学习Python编程
  • Houdini Labs Building Generator入门学习
  • RestTemplate 发送 JSON 请求时为何要手动序列化对象?
  • 用SpringBoot做一个web小案例实现登录
  • 16天 - 单例模式有哪几种实现?如何保证线程安全?什么是策略模式?一般用在什么场景?什么是模板方法模式?一般用在什么场景?
  • Linux中的基本指令(下)
  • 【文献阅读】Zotero 新手完全教程:安装、使用与插件
  • Python Cookbook-4.2 通过列表推导构建列表
  • 【C++】 —— 笔试刷题day_3
  • C++ Primer Plus第十二章课后习题总结
  • 人工智能与我何干
  • 新闻网页信息抽取
  • OKHttp3 源码阅读 - Kotlin版本
  • IIC通信协议详解与STM32实战指南
  • 如何在Ubuntu上构建编译LLVM和ISPC,以及Ubuntu上ISPC的使用方法
  • Fiora聊天系统本地化部署:Docker搭建与远程在线聊天的实践指南
  • 广告牌倾斜安全监测:保障公共安全的智能化解决方案
  • OpenMCU(三):STM32F103 FreeRTOS移植