Ubuntu 24.04 更换国内源教程

Ubuntu 24.04 的软件源配置方式已经发生变化,不再使用传统的 sources.list,而是通过 /etc/apt/sources.list.d/ubuntu.sources 这个文件来管理软件源。对很多用户来说,最常见的需求就是将系统切换为国内镜像源,以提升 apt update 速度和安装效率。

本文将介绍 Ubuntu 24.04 更换国内源的具体方法,并给出常见镜像源配置示例,适合新手直接参考操作。

一、源文件位置

Ubuntu 24.04 之后,系统默认会使用以下配置文件来定义 APT 源:

1
/etc/apt/sources.list.d/ubuntu.sources

如果你要更换国内镜像源,通常需要修改这个文件,或者直接替换成新的配置内容。

二、备份当前源配置

在修改任何系统配置之前,建议先备份当前文件,避免出现配置错误后无法恢复。

1
sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak

如果后续更新失败,也可以用备份文件恢复。

三、直接替换为国内源

如果你不想手动编辑文件,可以直接使用命令下载并覆盖源配置:

1
curl www.meiko.ink/sh/ubuntu.source > /etc/apt/sources.list.d/ubuntu.sources

这类方法适合快速切换到预设好的国内镜像源,操作简单,适合多数 Linux 用户。

四、手动修改 ubuntu.sources

如果你想自己控制源地址,也可以手动编辑这个文件:

1
sudo vim /etc/apt/sources.list.d/ubuntu.sources

例如,使用中科大源时,可以写成下面的格式:

1
2
3
4
5
Types: deb
URIs: http://mirrors.ustc.edu.cn/ubuntu/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

保存后执行更新命令即可生效:

1
sudo apt update

五、常见镜像源示例

1. 中科大源

1
2
3
4
5
Types: deb
URIs: http://mirrors.ustc.edu.cn/ubuntu/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

2. 清华源

1
2
3
4
5
Types: deb
URIs: http://mirrors.tuna.tsinghua.edu.cn/ubuntu/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

3. 阿里云源

1
2
3
4
5
Types: deb
URIs: http://mirrors.aliyun.com/ubuntu/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

4. 网易源

1
2
3
4
5
Types: deb
URIs: http://mirrors.163.com/ubuntu/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

六、更新缓存并检查结果

修改完成后,执行:

1
sudo apt update

如果没有报错,说明源配置已经生效。之后再安装软件或更新系统时,速度通常会明显提升。

七、常见问题

  1. apt update 报错:检查 ubuntu.sources 文件格式是否正确,尤其是 Suites 和 Components 是否和 Ubuntu 24.04 对应。
  2. 访问速度慢:可以切换到更靠近你所在地区的镜像源。
  3. 想恢复原来配置:直接恢复备份文件即可。
1
2
sudo cp /etc/apt/sources.list.d/ubuntu.sources.bak /etc/apt/sources.list.d/ubuntu.sources
sudo apt update

总结来说,Ubuntu 24.04 更换国内源的核心就是修改 /etc/apt/sources.list.d/ubuntu.sources,并选择合适的国内镜像源即可。这样可以更稳定地更新系统软件包,提升日常使用体验。