Ubuntu 国内镜像源配置指南(多版本常用镜像地址都有)

Ubuntu 国内镜像源配置指南(含 24.04+ DEB822 格式)

一、重要说明:Ubuntu 版本差异

关键变化

Ubuntu 版本 配置方式 文件位置 格式
24.04 LTS 及之后 DEB822 格式(新) /etc/apt/sources.list.d/ubuntu.sources YAML-like 格式
22.04 LTS 及之前 传统格式 /etc/apt/sources.list 每行一个源

二、国内主流镜像源汇总

1. 清华大学镜像源(推荐)

bash 复制代码
# 主站
https://mirrors.tuna.tsinghua.edu.cn/ubuntu/
# 帮助文档
https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

# 特点:更新及时,覆盖全面,教育网速度快

2. 阿里云镜像源(推荐)

bash 复制代码
# 主站
https://mirrors.aliyun.com/ubuntu/
# 帮助文档
https://developer.aliyun.com/mirror/ubuntu/?spm=a2c6h.25603864.0.0.7ed250ffJieSIB

# 特点:速度快,稳定性高,适合阿里云服务器

3. 腾讯云镜像源

bash 复制代码
# 主站
https://mirrors.tencent.com/ubuntu/
# 帮助文档
https://mirrors.tencent.com/help/ubuntu.html

# 特点:适合腾讯云服务器,速度稳定

4. 中国科学技术大学镜像源

bash 复制代码
# 主站
https://mirrors.ustc.edu.cn/ubuntu/
# 帮助文档
https://mirrors.ustc.edu.cn/help/ubuntu.html#__tabbed_4_2

# 特点:学术机构运营,更新及时

5. 网易镜像源

bash 复制代码
# 主站
https://mirrors.163.com/ubuntu/
# 帮助文档
https://mirrors.163.com/.help/ubuntu.html

# 特点:老牌镜像源,稳定性好

三、完整替换方案(分版本)

方案 A:Ubuntu 24.04+(DEB822 格式)

步骤 1:检查并备份原有配置
bash 复制代码
# 检查当前配置文件位置
ls -la /etc/apt/sources.list.d/

# 备份原有配置
sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak

# 验证备份
ls -la /etc/apt/sources.list.d/
步骤 2:替换为国内镜像源(DEB822 格式)
使用清华大学镜像
bash 复制代码
sudo tee /etc/apt/sources.list.d/ubuntu.sources << 'EOF'
# Ubuntu 24.04 LTS (Noble Numbat) - Tsinghua Mirror
Types: deb deb-src
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble noble-updates noble-backports noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
使用阿里云镜像
bash 复制代码
sudo tee /etc/apt/sources.list.d/ubuntu.sources << 'EOF'
# Ubuntu 24.04 LTS (Noble Numbat) - Aliyun Mirror
Types: deb deb-src
URIs: https://mirrors.aliyun.com/ubuntu
Suites: noble noble-updates noble-backports noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
使用腾讯云镜像
bash 复制代码
sudo tee /etc/apt/sources.list.d/ubuntu.sources << 'EOF'
# Ubuntu 24.04 LTS (Noble Numbat) - Tencent Cloud Mirror
Types: deb deb-src
URIs: https://mirrors.tencent.com/ubuntu
Suites: noble noble-updates noble-backports noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
步骤 3:更新并验证
bash 复制代码
# 更新软件包索引
sudo apt update

# 更新已安装的软件包
sudo apt upgrade -y

# 验证配置
cat /etc/apt/sources.list.d/ubuntu.sources

方案 B:Ubuntu 22.04/20.04(传统格式)

步骤 1:备份原配置
bash 复制代码
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
步骤 2:替换为国内镜像源
使用清华大学镜像(22.04 LTS)
bash 复制代码
sudo tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
EOF
使用清华大学镜像(20.04 LTS)
bash 复制代码
sudo tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
EOF

四、DEB822 格式详解(重要)

传统格式 vs DEB822 格式对比

特性 传统格式(sources.list) DEB822 格式(.sources)
格式 每行一个源 YAML 风格的键值对
文件位置 /etc/apt/sources.list /etc/apt/sources.list.d/*.sources
可读性 一般 更好
扩展性 较差 更好

DEB822 格式字段说明

yaml 复制代码
# Ubuntu 24.04 LTS - DEB822 格式示例
Types: deb deb-src                    # 包类型:二进制包、源码包
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu  # 镜像源地址
Suites: noble noble-updates noble-backports noble-security  # 发布版本
Components: main restricted universe multiverse    # 组件
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg  # GPG 密钥文件
字段 说明 示例
Types 包类型 deb 二进制包,deb-src 源码包
URIs 镜像源地址 https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites 版本套件 noble, noble-updates, noble-security
Components 组件 main, restricted, universe, multiverse
Signed-By GPG 签名密钥 /usr/share/keyrings/ubuntu-archive-keyring.gpg

Suites 版本代号

Ubuntu 版本 代号
24.04 LTS noble
22.04 LTS jammy
20.04 LTS focal

五、如何确定使用哪种格式

方法 1:检查 Ubuntu 版本

bash 复制代码
# 查看 Ubuntu 版本
lsb_release -cs

# 输出结果说明:
# noble  → Ubuntu 24.04+ → 使用 DEB822 格式
# jammy  → Ubuntu 22.04  → 使用传统格式
# focal  → Ubuntu 20.04  → 使用传统格式

方法 2:检查配置文件是否存在

bash 复制代码
# 检查是否存在 DEB822 格式文件
ls -la /etc/apt/sources.list.d/ubuntu.sources

# 如果文件存在 → 使用 DEB822 格式
# 如果不存在 → 使用传统 sources.list 格式

六、恢复备份(万一出错)

DEB822 格式恢复

bash 复制代码
# 恢复备份文件
sudo cp /etc/apt/sources.list.d/ubuntu.sources.bak /etc/apt/sources.list.d/ubuntu.sources

# 更新
sudo apt update

传统格式恢复

bash 复制代码
# 恢复备份文件
sudo cp /etc/apt/sources.list.bak /etc/apt/sources.list

# 更新
sudo apt update

七、完整示例(自动适配版本)

一键配置脚本

bash 复制代码
#!/bin/bash

# 检查 Ubuntu 版本
VERSION=$(lsb_release -cs)

# 备份原配置
if [ -f /etc/apt/sources.list.d/ubuntu.sources ]; then
    # DEB822 格式(Ubuntu 24.04+)
    echo "检测到 Ubuntu 24.04+,使用 DEB822 格式..."
    sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak
    
    # 写入新配置
    sudo tee /etc/apt/sources.list.d/ubuntu.sources << 'EOF'
Types: deb deb-src
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: VERSION VERSION-updates VERSION-backports VERSION-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
    
    # 替换版本代号
    sudo sed -i "s/VERSION/$VERSION/g" /etc/apt/sources.list.d/ubuntu.sources
else
    # 传统格式(Ubuntu 22.04 及之前)
    echo "检测到 Ubuntu 22.04/20.04,使用传统格式..."
    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
    
    # 写入新配置
    sudo tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ VERSION main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ VERSION main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ VERSION-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ VERSION-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ VERSION-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ VERSION-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ VERSION-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ VERSION-security main restricted universe multiverse
EOF
    
    # 替换版本代号
    sudo sed -i "s/VERSION/$VERSION/g" /etc/apt/sources.list
fi

# 更新
sudo apt update
echo "配置完成!"

八、命令详解与举一反三

1. tee 命令详解

bash 复制代码
sudo tee /path/to/file << 'EOF'
内容
EOF

作用 :将 EOF 之间的内容写入指定文件

举一反三:

  • 写多个文件:tee file1 file2
  • 追加模式:tee -a 追加内容而不覆盖

2. sed 命令详解

bash 复制代码
# 替换字符串
sed -i 's/原内容/新内容/g' filename

作用:批量替换文件中的内容

举一反三:

  • s/ 替换操作符
  • g 全局替换(替换所有匹配项)
  • 也适用于配置其他软件源

3. 版本检测命令

bash 复制代码
lsb_release -cs

作用:获取 Ubuntu 版本代号

举一反三:

  • 也用于脚本中自动适配版本
  • 类似命令:cat /etc/os-release

九、常见问题

问题 1:DEB822 格式不生效

bash 复制代码
# 原因:配置文件格式错误
# 检查:
ls -la /etc/apt/sources.list.d/

# 验证格式:
sudo apt update  # 看是否有错误提示

问题 2:两种格式混用

注意:Ubuntu 24.04+ 会优先读取 DEB822 格式,传统格式可能被忽略

问题 3:GPG 密钥问题

bash 复制代码
# 重新导入密钥
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <密钥ID>

十、总结

Ubuntu 版本 配置方式 配置文件 推荐操作
24.04 LTS+ DEB822 /etc/apt/sources.list.d/ubuntu.sources 使用方案 A
22.04 LTS 传统 /etc/apt/sources.list 使用方案 B
20.04 LTS 传统 /etc/apt/sources.list 使用方案 B

重要提醒:Ubuntu 24.04 是第一个使用 DEB822 格式的 LTS 版本,配置方式完全不同,一定注意区分!

相关推荐
律宏阔1 小时前
WSL Docker 端口明明空闲,但就是绑不上端口
linux·windows
律宏阔1 小时前
WSL 突然断网,无法 ping 内网或外网
linux·windows
穷人小水滴1 小时前
用容器编译 VirtualBox 虚拟机软件 (ArchLinux, podman)
linux·容器·virtualbox
乱码三千1 小时前
如何优雅地直连无公网 IP 的远程 GPU 服务器
linux·人工智能·程序员
yunwei371 小时前
使用 eBPF 跟踪 Nginx 请求
linux·后端·性能优化
yunwei371 小时前
使用 eBPF 跟踪 MySQL 查询
linux·后端·性能优化
yunwei371 小时前
eBPF 示例教程:使用 XDP 捕获 TCP 信息
linux·后端·性能优化
judezh1 小时前
api 容器一直 unhealthy?我把一个 Agent 运行时的健康检查逐条拆了,查出四个问题
运维·docker
GeW2 小时前
制造业高端化,为什么必须重估Linux和数据库?
linux
GeW2 小时前
工业场景中的Linux与数据库选型:高端化转型的技术底座
linux