dnf(Dandified YUM)和 rpm 工具完成上述任务,帮助您高效管理软件包全生命周期。dnf, TencentOS Server 3 yum 和 dnf 均可用。[BaseOS] 和 [AppStream],提供系统核心组件和应用程序包,默认已配置。[BaseOS] 和 [AppStream] 两项。如需使用其他源(如 debuginfo、source),可在对应配置段中将 enabled 设置为 1。cat /etc/yum.repos.d/TencentOS.repo
[BaseOS]name=BaseOS $releasever - $basearchbaseurl=https://mirrors.tencent.com/tlinux/$releasever/BaseOS/$basearch/os/Packages/gpgcheck=1enabled=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TencentOS[AppStream]name=AppStream $releasever - $basearchbaseurl=https://mirrors.tencent.com/tlinux/$releasever/AppStream/$basearch/os/Packages/gpgcheck=1enabled=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TencentOS[BaseOS-debuginfo]name=BaseOS-debuginfo $releasever - $basearchbaseurl=https://mirrors.tencent.com/tlinux/$releasever/BaseOS/$basearch/debug/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TencentOS[AppStream-debuginfo]name=AppStream-debuginfo $releasever - $basearchbaseurl=https://mirrors.tencent.com/tlinux/$releasever/AppStream/$basearch/debug/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TencentOS[BaseOS-source]name=BaseOS-source $releaseverbaseurl=https://mirrors.tencent.com/tlinux/$releasever/BaseOS/source/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TencentOS[AppStream-source]name=AppStream-source $releaseverbaseurl=https://mirrors.tencent.com/tlinux/$releasever/AppStream/source/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TencentOS
dnf install -y epol-release
mkdir -p /mnt/myiso # 创建放置 iso 的目录,自定义目录,请确保存储空间充足mkdir -p /mnt/my_yum # 创建挂载 iso 的目录,自定义目录,请确保存储空间充足
cd /mnt/myisowget https://mirrors.tencent.com/tlinux/4.0/isos/x86_64/TencentOS-Server-4.0-everything-x86_64.iso
mount -o loop /mnt/myiso/TencentOS-Server-4.0-everything-x86_64.iso /mnt/my_yum/
vim /etc/fstab ,并添加以下内容echo "/mnt/myiso/TencentOS-Server-4.0-everything-x86_64.iso /mnt/my_yum iso9660 loop,ro 0 0" >> /etc/fstab
/mnt/my_yum 目录访问 ISO 中的 rpm 包:cd /mnt/my_yum/tree -L 2 .
.├── AppStream│ ├── Packages│ └── repodata├── BaseOS│ ├── Packages│ └── repodata├── boot│ └── grub2├── EFI│ └── BOOT└── images├── efiboot.img├── eltorito.img├── install.img└── pxeboot12 directories, 3 files
# 查看 repo 文件,cat /etc/yum.repos.d/local.repo[AppStream]name=AppStreambaseurl=file:///mnt/my_yum/AppStreampriority=1enabled=1[BaseOS]name=BaseOSbaseurl=file:///mnt/my_yum/BaseOSpriority=1enabled=1
mkdir -p /mnt/local_repo/x86_64/Packages # 自定义目录位置,请确保存储空间充足
dnf install -y createrepo
cp *.rpm /mnt/local_repo/x86_64/Packages
createrepo /mnt/local_repo/x86_64
/mnt/local_repo/x86_64 目录下创建 repodata。# 查看 repo 文件,cat /etc/yum.repos.d/local.repo[local]name=localbaseurl=file:///mnt/local_repo/x86_64priority=1enabled=1
cat /etc/dnf/dnf.conf[main]gpgcheck=1installonly_limit=3clean_requirements_on_remove=Truebest=Trueskip_if_unavailable=Falsezchunk=False
dnf 配置。完整的参数说明可执行 man dnf.conf 查看。参数 | 值类型 | 默认值 | 说明 |
name | string | 无 | repo 源的名称 |
baseurl | list | 无 | repo 源的 URL 列表 |
priority | integer | 无 | repo 源的优先级,取值范围 0 - 99,值越小优先级越高 |
gpgcheck | boolean | 1 | 是否进行 GPG 校验 |
gpgkey | string | 无 | GPG 公钥的 URL 或本地路径,配合 gpgcheck 使用 |
installonly_limit | integer | 3 | 允许被并行安装的软件包数量 |
clean_requirements_on_remove | boolean | True | 是否在卸载软件包时同时卸载没有被其他软件包使用的依赖 |
best | boolean | True | 是否在安装软件包时安装最高版本,如果无法安装则失败 |
skip_if_unavailable | boolean | False | 设为 True 时跳过不可用的源,设为 False(默认)时遇到不可用的源会报错 |
zchunk | boolean | False | 是否通过 zchunk 格式压缩 repo 的元数据 |
# 列出所有已启用的软件源dnf repolist# 列出所有软件源(包含禁用的)dnf repolist all# 查看某个软件源的详细信息dnf repoinfo <repo-name>
dnf repolist all 输出示例:repo id repo name statusAppStream TencentOS-4 - AppStream enabledBaseOS TencentOS-4 - BaseOS enabledextras TencentOS-4 - Extras enabled
# 临时禁用某个源(仅本次命令生效)dnf install <包名> --disablerepo=<repo-id># 永久启用某个源dnf config-manager --enable <repo-id># 永久禁用某个源dnf config-manager --disable <repo-id>
# 通过 URL 添加软件源dnf config-manager --add-repo <repo-url># 手动创建 .repo 文件vi /etc/yum.repos.d/<name>.repo
[myrepo]name=My Custom Repositorybaseurl=https://example.com/repo/enabled=1gpgcheck=1gpgkey=https://example.com/RPM-GPG-KEY
# 列出当前环境所有已安装软件包dnf list installed# 列出所有已安装的软件包(rpm 方式)rpm -qa
rpm -qa 会列出系统全量安装的包列表,一般配合 grep 命令使用。# 搜索包含关键词的软件包dnf search <包名># 在包名和摘要中搜索(更精确)dnf search --all <包名>
# 查看软件包的详细信息dnf info <包名># 查询软件包信息(rpm 方式)rpm -qi <包名># 查看软件包的文件列表rpm -ql <包名># 查询文件属于哪个软件包dnf provides /etc/<包名>/<包名>.confdnf provides "*.so.1"# 查询文件属于哪个软件包(rpm 方式)rpm -qf /etc/<包名>/<包名>.conf
# 列出所有可安装的软件包(仅显示未安装的)dnf list available# 列出特定软件包的所有可用版本dnf list <包名> --showduplicates
# 查询哪些包依赖指定软件包dnf repoquery --whatrequires <包名># 查询指定软件包的依赖dnf repoquery --requires <包名>
# 安装源中的软件包dnf install -y <包名># 安装多个软件包dnf install -y <包名1> <包名2> <包名3># 安装指定版本dnf install <包名-1.2.1># 安装用户自己编译的 rpm 包文件dnf localinstall -y <包名>.rpmdnf install /path/to/<包名>.rpm# 安装软件包组dnf groupinstall "包组名"# 查看可用软件包组dnf grouplist
# 检查有哪些可用更新(不执行更新)dnf check-update# 更新所有软件包(自动确认)dnf update -y# 更新指定软件包dnf update <包名># 将已安装的 rpm 包升级dnf upgrade <包名># 更新软件包组dnf groupupdate "包组名"
dnf update --downloadonly 预下载,或使用 dnf history 记录便于回滚。# 将已安装的 rpm 包降级dnf downgrade <包名>
# 卸载软件包(保留依赖)dnf remove <包名># 卸载软件包及其未被其他包使用的依赖dnf autoremove <包名># 清理不再需要的依赖包dnf autoremove
dnf repoquery --whatrequires <包名> 检查影响范围。# 下载源中的 rpm 包(不安装)dnf download <包名>
# 查看 dnf 操作历史dnf history# 查看某次操作的详情dnf history info <id># 回滚某次操作dnf history undo <id>
rpm 命令可以直接操作 RPM 包,无需连接软件源:# 查询软件包是否已安装rpm -q <包名># 查询所有已安装的软件包rpm -qa# 查询软件包的安装文件rpm -ql <包名># 查询文件属于哪个软件包rpm -qf /etc/<包名>/<包名>.conf# 查询软件包的详细信息rpm -qi <包名># 查询软件包的版本rpm -q --qf "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\\n" <包名># 安装 RPM 包(不解决依赖,适用于离线场景)rpm -ivh <包名>.rpm# 强制安装(忽略依赖检查)rpm -ivh --force --nodeps <包名>.rpm# 卸载rpm -e <包名>
# 查看可用模块dnf module list# 查看特定模块信息dnf module info python3# 启用特定版本流dnf module enable python3:3.8# 安装模块dnf module install python3:3.8/default# 重置模块(恢复默认流)dnf module reset python3
# 清理软件包缓存dnf clean packages# 清理所有缓存dnf clean all# 重新生成元数据缓存dnf makecache
# 验证软件包已安装dnf list installed | grep <包名># 验证软件包完整性rpm -V <包名># 查看 dnf 日志cat /var/log/dnf.log | tail -50
You have enabled checking of packages via GPG keys. This is a good thing.However, you do not have any GPG public keys installed. You need to downloadthe keys for packages you wish to install and install them.You can do that by running the command:rpm --import public.gpg.key
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-TencentOS-*
error: cannot open Packages database in /var/lib/rpm
rm -f /var/lib/rpm/__db.*
rpm --rebuilddbdnf clean all
No match for argument 错误,通常由缓存过期或包名不正确导致。# 清理缓存后重试dnf clean alldnf makecachednf install <包名># 确认软件源已启用dnf repolist# 尝试搜索正确的包名dnf search <关键词>
# 方法1:导入 GPG keyrpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-TencentOS-*# 方法2:临时跳过 GPG 检查(不推荐在生产环境使用)dnf install <包名> --nogpgcheck
dnf update 时下载速度缓慢。# 确认使用了镜像源dnf repolist -v | grep Repo-baseurl# 更换软件源(编辑 /etc/yum.repos.d/*.repo 修改 baseurl)
dnf 和 rpm 查询结果不一致。# 重建 RPM 数据库rpm --rebuilddbdnf clean all
文档反馈