Linux yum-config-manager命令教程:轻松管理YUM软件源(附实例详解和注意事项)

Linux yum-config-manager命令介绍

yum-config-manager 是一个用来管理 YUM 软件源的工具,它允许用户启用、禁用、添加或删除软件源。这个命令对于 Linux 系统管理员来说非常重要,因为它可以帮助他们控制软件包的来源,确保系统安装的软件是最新的,或者从特定的软件源获取特定版本的软件包。

Linux yum-config-manager命令适用的Linux版本

yum-config-manager 命令适用于使用 YUM 包管理器的 Linux 发行版,如 RedHat/CentOS、Fedora。对于其他发行版,可能需要使用不同的命令或工具来管理软件源。

安装 YUM 工具

在 CentOS 7 上,你可以使用以下命令安装 YUM 工具:

bash 复制代码
sudo yum install yum-utils

在 CentOS 8 上,你可以使用以下命令安装 YUM 工具:

bash 复制代码
sudo dnf install yum-utils

Linux yum-config-manager 命令的基本语法

bash 复制代码
yum-config-manager [options] [repository]

Linux yum-config-manager命令的基本语法

bash 复制代码
yum-config-manager [options] [enable|disable|list] [repo-id]

Linux yum-config-manager命令的常用选项或参数说明

选项 说明
--enable 启用指定的软件源
--disable 禁用指定的软件源
--list 列出所有已配置的软件源
--add 添加一个新的软件源配置文件
--remove 删除指定的软件源配置
--setopt 设置软件源的选项
--save 将当前软件源配置保存为默认配置

Linux yum-config-manager命令实例详解

实例1:启用软件源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --enable repo-id

启用指定的软件源 repo-id

实例2:禁用软件源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --disable repo-id

禁用指定的软件源 repo-id

实例3:列出所有软件源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --list

列出系统中所有已配置的软件源。

实例4:添加新的软件源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --add /path/to/new.repo

添加一个新的软件源配置文件 new.repo

实例5:删除软件源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --remove repo-id

删除指定的软件源配置。

实例6:设置软件源选项

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --setopt=repo-id:enabled=1

设置软件源 repo-id 为启用状态。

实例7:保存当前配置为默认

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --save

将当前的软件源配置保存为系统的默认配置。

实例8:启用CentOS 7的额外库源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ sudo yum-config-manager --enable base

在 CentOS 7 中启用基本库源。

实例9:禁用CentOS 7的更新库源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ sudo yum-config-manager --disable updates

在 CentOS 7 中禁用更新库源,以防止自动更新。

实例10:列出所有可用的软件源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --list all

列出系统中所有可用的软件源,包括已启用和已禁用的。

实例11:添加自定义软件源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ echo -e "[custom-repo]\nname=Custom Repository\nbaseurl=http://example.com/custom-repo/\ngpgcheck=0\nenabled=1" | sudo tee /etc/yum.repos.d/custom-repo.repo

添加一个自定义的软件源,并启用它。

实例12:设置软件源的优先级

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --setopt="base.priority=10"

设置基本软件源的优先级为 10,以确保在安装软件包时优先从该源获取。

实例13:禁用所有第三方软件源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --disable \* --enable base --enable updates

禁用所有第三方软件源,只保留基本库源和更新库源。

实例14:启用特定的第三方软件源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --enable epel

启用 EPEL(Extra Packages for Enterprise Linux)软件源,以获取额外的软件包。

实例15:检查特定软件源的状态

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --list epel | grep enabled

检查 EPEL 软件源是否已启用。

实例16:删除特定的软件源配置文件

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ sudo rm /etc/yum.repos.d/epel.repo

如果 yum-config-manager --remove 命令不起作用,可以直接删除软件源的配置文件。

实例17:清除YUM缓存并刷新软件源

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ sudo yum clean all
[linux@bashcommandnotfound.cn ~]$ sudo yum makecache

清除 YUM 缓存并重新生成缓存,确保软件源列表是最新的。

实例18:检查软件源的更新

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ sudo yum check-update

检查系统中的软件包是否有可用的更新。

实例19:配置YUM在离线模式下工作

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ echo "yum clean all" > /etc/yum/yum-offline.conf

创建一个配置文件,使 YUM 在离线模式下工作,不从网络获取软件包信息。

实例20:使用YUM的代理设置

bash 复制代码
[linux@bashcommandnotfound.cn ~]$ yum-config-manager --setopt="proxy=http://proxy.example.com:8080"

为 YUM 设置代理服务器,以便在网络受限的环境中使用。

通过这些实例,用户可以学会如何使用 yum-config-manager 命令来管理他们的软件源,从而更好地控制他们的 Linux 系统。

Linux yum-config-manager命令的注意事项

  • 使用 yum-config-manager 命令时,请确保你有足够的权限(可能需要使用 sudo)。
  • 在添加或删除软件源时,请确保你了解该操作的后果,错误的配置可能会导致系统无法更新。
  • 如果遇到 bash: yum-config-manager: command not found 的错误,可能需要安装或更新 YUM 工具。

Linux yum-config-manager相关命令

在 Linux 系统中,除了 yum-config-manager 命令外,还有许多其他命令与软件包管理相关。以下是一些额外的相关命令,它们各自具有独特的功能和用途:

相关推荐
大树886 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠6 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质6 小时前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush46 小时前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5207 小时前
Linux 11 动态监控指令top
linux
小宇宙Zz7 小时前
Maven依赖冲突
java·服务器·maven
Inhand陈工7 小时前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智8 小时前
ARP代理--工作原理
运维·网络·arp·arp代理
不会C语言的男孩8 小时前
Linux 系统编程 · 第 8 章:进程基础
linux·c语言
shushangyun_8 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化