Ubuntu

Ubuntu 是基于 Debian 的 Linux 发行版。本页介绍把 apt 软件源切换到阿里云镜像。

配置#

软件源配置文件的位置取决于版本:

  • Ubuntu 24.04 LTS 默认使用 deb822 格式的 /etc/apt/sources.list.d/ubuntu.sources。
  • Ubuntu 22.04 LTS 及更早版本通常使用 /etc/apt/sources.list。

下面的命令在 root shell 下执行。先备份,再选择一个镜像写入。

备份软件源配置#

Ubuntu 24.04 LTS:

sh
cp -a /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak

Ubuntu 22.04 LTS 及更早版本:

sh
cp -a /etc/apt/sources.list /etc/apt/sources.list.bak

使用阿里云公网镜像#

Ubuntu 24.04 LTS 覆盖写入 ubuntu.sources 并更新索引:

sh
cat > /etc/apt/sources.list.d/ubuntu.sources <<'EOF'Types: debURIs: https://mirrors.aliyun.com/ubuntuSuites: noble noble-updates noble-backportsComponents: main restricted universe multiverseSigned-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Types: debURIs: https://mirrors.aliyun.com/ubuntuSuites: noble-securityComponents: main restricted universe multiverseSigned-By: /usr/share/keyrings/ubuntu-archive-keyring.gpgEOF
apt update

Ubuntu 22.04 LTS 及更早版本替换 sources.list 中的地址并更新索引:

sh
sed -i "s@http://.*archive.ubuntu.com@https://mirrors.aliyun.com@g" /etc/apt/sources.listsed -i "s@http://.*security.ubuntu.com@https://mirrors.aliyun.com@g" /etc/apt/sources.listapt update

使用阿里云 ECS VPC 镜像#

在阿里云 ECS 上可以改用 VPC 内网镜像。

Ubuntu 24.04 LTS:

sh
cat > /etc/apt/sources.list.d/ubuntu.sources <<'EOF'Types: debURIs: http://mirrors.cloud.aliyuncs.com/ubuntuSuites: noble noble-updates noble-backportsComponents: main restricted universe multiverseSigned-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Types: debURIs: http://mirrors.cloud.aliyuncs.com/ubuntuSuites: noble-securityComponents: main restricted universe multiverseSigned-By: /usr/share/keyrings/ubuntu-archive-keyring.gpgEOF
apt update

Ubuntu 22.04 LTS 及更早版本:

sh
sed -i "s@http://.*archive.ubuntu.com@http://mirrors.cloud.aliyuncs.com@g" /etc/apt/sources.listsed -i "s@http://.*security.ubuntu.com@http://mirrors.cloud.aliyuncs.com@g" /etc/apt/sources.listapt update