在 Ubuntu 中,使用 ISO 镜像作为 APT 的安装源是一种在离线环境中安装和更新软件包的有效方法。首先,需要下载相应的 Ubuntu ISO 镜像并将其挂载到系统中。然后,通过修改 APT 的源列表文件 /etc/apt/sources.list,将软件包的来源指向已挂载的 ISO 镜像路径。具体步骤包括挂载 ISO 镜像到 /media/cdrom 目录,编辑 sources.list 文件以添加 deb file:///media/cdrom/ focal main restricted 等条目。最后,运行 sudo apt update 来刷新包列表,从而使系统能够从 ISO 镜像中安装和更新软件包。这种方法在没有网络连接的情况下尤其有用,可以确保系统软件的安装和更新顺利进行。

博主博客

一、创建挂载目录

1.1 创建 iso 文件夹

sudo mkdir /mnt/iso

1.2 修改文件夹权限

sudo chmod -R 777 /mnt/iso

二、挂载 iso 镜像

这里使用 ubuntu-24.04-desktop-amd64.iso 镜像, 需要替换成自己的镜像。

sudo mount -t iso9660 -o loop /home/ubuntu-24.04-desktop-amd64.iso /mnt/iso

执行完后进入 /mnt/iso 文件夹即可看到 iso 镜像中的内容。

三、修改 apt 源

apt 源通过 /etc/apt/sources.list 进行配置,修改前建议先备份。

3.1 备份默认配置文件

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

3.2 修改 apt 配置

3.2.1 方法一

可在首行加入下列语句,其中 jammyUbuntu 系统代号, 可查看文章底部代号进行修改。

deb file:///mnt jammy main restricted

3.2.2 方法二

可通过下列语句自动写入

sudo apt-cdrom add

执行完后再查看 etc/apt/sources.list 可以发现在末尾已经自动写入。

四、更新源

4.1 清理缓存

sudo apt-get clean all

4.2 更新

sudo apt-get update

4.3 安装测试

尝试安装 gcc

sudo apt-get install gcc

附录

常用 Ubuntu 代号

  • Ubuntu 22.04:jammy
  • Ubuntu 20.04:focal
  • Ubuntu 18.04:bionic
  • Ubuntu 16.04:xenial

常用国内镜像源

注意:使用前记得根据自己系统版本修改代号!!!

阿里云

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse  
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse  
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse  
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse  
  
# deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse  
# deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse  
# deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse  
# deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse  
  
## Pre-released source, not recommended.  
# deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse  
# deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

清华

deb 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 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 main restricted universe multiverse  
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse  
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse  
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse  
  
## Pre-released source, not recommended.  
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse  
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

中科大

deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse  
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse  
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse  
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse  
  
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse  
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse  
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse  
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse  
  
## Pre-released source, not recommended.  
# deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse  
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

网易163

deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse  
deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse  
deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse  
deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse  
  
# deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse  
# deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse  
# deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse  
# deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse  
  
## Pre-released source, not recommended.  
# deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse  
# deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse