ZBLOG

Linux如何设置本地源

好的,在Linux中设置本地软件源(例如使用DVD/CD或ISO镜像)非常实用,尤其是在没有互联网连接的环境、需要快速部署相同软件到多台机器,或者需要严格管控软件版本的场景下。

以下是针对不同主流发行版的详细设置方法。


核心思路

无论哪种发行版,设置本地源的核心步骤都类似:

  1. 挂载源介质:将包含软件包的DVD或ISO镜像文件挂载到某个目录(如 /mnt/media)。
  2. 配置包管理器:告诉系统的包管理器(apt, yum, dnf, zypper)去你挂载的目录里寻找软件包。
  3. 刷新缓存:让包管理器扫描新配置的源并建立本地缓存。
  4. 优先使用本地源:通过配置优先级,确保系统优先从本地源安装,找不到时再fallback到网络源(可选)。

1. Ubuntu / Debian (使用 apt)

这种方法适用于Ubuntu/Debian的安装光盘或ISO镜像。

步骤 1: 挂载本地介质

假设你的ISO文件是 ubuntu-22.04.3-live-server-amd64.iso,将其挂载到 /mnt 目录。

# 创建挂载点
sudo mkdir -p /mnt/local-repo

# 挂载ISO文件 (将路径替换为你的实际ISO路径)
sudo mount -o loop /path/to/your/ubuntu-22.04.3-live-server-amd64.iso /mnt/local-repo
# 或者挂载物理光驱
# sudo mount /dev/sr0 /mnt/local-repo

# 检查是否挂载成功
ls /mnt/local-repo

步骤 2: 配置APT源

备份现有源列表并创建一个新的源文件。

# 备份所有现有源文件
sudo cp -r /etc/apt/sources.list.d /etc/apt/sources.list.d.bak
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 创建一个新的source list文件来指向本地源
sudo nano /etc/apt/sources.list.d/local.list

在该文件中添加以下内容(Debian用户请将 jammy 替换为自己的版本代号,如 bookworm):

# Local Repository (CD-ROM)
deb [allow-insecure=yes, trusted=yes] file:/mnt/local-repo jammy main restricted
  • allow-insecure=yestrusted=yes:因为本地文件系统没有HTTPS证书,这些选项是必需的。
  • file:/mnt/local-repo:使用 file: URI指定本地路径。
  • jammy main restricted:发布版本和组件名称,请根据你的ISO内容修改。

(可选):注释掉或移除以 httphttps 开头的网络源,以确保只从本地安装。可以在 /etc/apt/sources.list 中每行前加 #

步骤 3: 更新APT缓存

# 更新软件包列表,现在它会扫描本地目录
sudo apt update

# 现在尝试从本地源安装软件吧!
sudo apt install <package-name>

2. CentOS / RHEL / Rocky Linux (使用 yum/dnf)

这种方法适用于CentOS/RHEL系列的DVD镜像。

强烈推荐的方法:创建可用的Repo数据

大多数官方DVD中的 .repo 文件指向的是在线地址,直接使用会报错。我们需要自己创建一个。

为了以后方便管理,建议将整个ISO内容复制到硬盘的一个目录(如 /opt/repos/local),而不是一直挂着ISO。

# Create a directory to hold the repo contents permanently.
sudo mkdir -p /opt/repos/local

# Mount the ISO temporarily to copy files.
sudo mount -o loop /path/to/CentOS-7-x86_64-Everything-2009.iso /mnt

# Copy ALL contents from the ISO to the permanent directory.
sudo cp -rv /mnt/* /opt/repos/local/

# Unmount the ISO since we no longer need it mounted.
sudo umount /mnt

# Ensure the directory structure is correct.
ls -l /opt/repos/local/
步骤 B: Create the Local Repo File

现在为这个本地目录创建一个 .repo配置文件。这是最关键的一步!

sudo nano /etc/yum.repos.d/local.repo

添加以下内容(以CentOS为例):

[Local-BaseOS]
name=Local CentOS BaseOS Repository
baseurl=file:///opt/repos/local/BaseOS/
enabled=1     # Enable this repository 
gpgcheck=0    # Disable GPG check for simplicity (set to 1 and add gpgkey= if you have the key) 
priority=1    # Lower number means higher priority. This ensures local repo is used first.

[Local-AppStream]
name=Local CentOS AppStream Repository 
baseurl=file:///opt/repos/local/AppStream/
enabled=1 
gpgcheck=0 
priority=1 
  • baseurl: 必须指向包含 repodata/文件夹的那个目录!
    • 对于CentOS/RHEL/Fedora DVD镜像来说通常有两个主要仓库:
      • <Your_Mount_Point>/BaseOS/
      • <Your_Mount_Point>/AppStream
步骤 C: Clean and Build Cache

清理旧缓存并让DNF/Yum为新创建的本地仓库建立元数据缓存:

# Clean all cached metadata from disabled or removed repos.
sudo dnf clean all      # For systems using dnf (CentOS8+/Rocky/AlmaLinux)
## OR ## 
sudo yum clean all      # For older systems using yum (e.g., CentOS7)

# Make cache for the new local repository.
sudo dnf makecache      # Or 'yum makecache'

现在就可以使用了:

sudo dnf install httpd 

验证和使用

配置完成后,可以使用以下命令验证:

  • Ubuntu/Debian: apt policy <package-name> ,查看候选版本是否来自你的本地源。
  • CentOS/RHEL: dnf repolist all ,确认你的 local.repo是启用的(enabled)。使用 dnf --disablerepo="*" --enablerepo="local*" list available可以只列出本地源的包。

卸载不再需要的本地仓库:

  1. 卸载挂载点:sudo umount /mnt/cdrom
  2. 删除或重命名你创建的配置文件(如 /etc/yum.repos.d/mylocal.repo)
  3. 再次运行清理和生成缓存命令。


    内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=24303

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?