• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
孙龙 程序员
少时总觉为人易,华年方知立业难
博客园    首页    新随笔    联系   管理    订阅  订阅
linux软件包管理和使用 yum

1,什么是YUM

yum是RedHat以及CentOS中的软件包管理器,能够荣国互联网下载.rpm包并安装,并可以自动处理依赖性关系,无须繁琐地一次次下载,安装

 2,yum源是什么

要成功使用YUM工具安装跟新软件或者系统,就需要有一个包含各种rpm软件包的repository(软件仓库),这个软件仓库我们习惯称为yum源

 

[root@host-234 liexin521]# ll /etc/yum.repos.d/
总用量 92
-rw-r--r--. 1 root root 1664 11月 23 2020 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 11月 23 2020 CentOS-CR.repo
-rw-r--r--. 1 root root  649 11月 23 2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 11月 23 2020 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 11月 23 2020 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 11月 23 2020 CentOS-Sources.repo
-rw-r--r--. 1 root root 8515 11月 23 2020 CentOS-Vault.repo
-rw-r--r--. 1 root root  616 11月 23 2020 CentOS-x86_64-kernel.repo
-rw-r--r--. 1 root root 1919 3月   9 2021 docker-ce.repo
-rw-r--r--. 1 root root 1358 9月   5 2021 epel.repo
-rw-r--r--. 1 root root 1457 9月   5 2021 epel-testing.repo
-rw-r--r--. 1 root root  773 5月  17 16:48 gitlab_gitlab-ce.repo
-rw-r--r--. 1 root root  173 10月 12 2020 google-chrome.repo
-rw-r--r--. 1 root root  669 5月   2 2019 ius-archive.repo
-rw-r--r--. 1 root root  591 5月   2 2019 ius.repo
-rw-r--r--. 1 root root  669 5月   2 2019 ius-testing.repo
-rw-r--r--. 1 root root   85 11月 30 2016 jenkins.repo
-rw-r--r--. 1 root root  756 4月  19 2021 mariadb.repo
-rw-r--r--. 1 root root  474 4月  25 2018 nodesource-el7.repo
-rw-r--r--. 1 root root  472 9月   9 2015 nodesource-el.repo
-rw-r--r--. 1 root root  130 4月  19 2021 yarn.repo

 

 

vim /etc/yum.repos.d/CentOS-Base.repo

[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates 
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

 

CentOS 镜像使用帮助

 https://mirrors.tuna.tsinghua.edu.cn/help/centos/

请注意,CentOS 8 (非 Stream 版)已提前进入 EOL 停止服务阶段,因此镜像已被官方移动。如果您正在寻找关于这些系统的镜像,请参考 centos-vault 的帮助。

 

该文件夹只提供 CentOS 7 与 8,架构仅为 x86_64 ,如果需要较早版本的 CentOS,请参考 centos-vault 的帮助,若需要其他架构,请参考 centos-altarch 的帮助。

 

建议先备份 /etc/yum.repos.d/ 内的文件。

 

然后编辑 /etc/yum.repos.d/ 中的相应文件,在 mirrorlist= 开头行前面加 # 注释掉;并将 baseurl= 开头行取消注释(如果被注释的话)。 对于 CentOS 7 ,请把该行内的域名(例如mirror.centos.org)替换为 mirrors.tuna.tsinghua.edu.cn。 对于 CentOS 8 ,请把 mirror.centos.org/$contentdir 替换为 mirrors.tuna.tsinghua.edu.cn/centos。

 

以上步骤可以被下方的命令一步完成

 

# 对于 CentOS 7
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo

# 对于 CentOS 8
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo

 

 

注意其中的*通配符,如果只需要替换一些文件中的源,请自行增删。

 

注意,如果需要启用其中一些 repo,需要将其中的 enabled=0 改为 enabled=1。

 

最后,更新软件包缓存

 

sudo yum makecache

 

vim epel.repo

[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=http://download.example/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=http://download.example/pub/epel/7/$basearch/debug
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place it's address here.
#baseurl=http://download.example/pub/epel/7/source/tree/
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

 

 配置:epel源:

yum install -y epel-release 

 

EPEL 镜像使用帮助

https://mirrors.tuna.tsinghua.edu.cn/help/epel/

EPEL(Extra Packages for Enterprise Linux)是由Fedora Special Interest Group维护的Enterprise Linux(RHEL、CentOS)中经 常用到的包。

下面以CentOS 7为例讲解如何使用本镜像站的epel镜像。CentOS 8同样可用该方法。

首先从CentOS Extras这个源(本镜像站也有镜像)里安装epel-release:

yum install epel-release

 

修改/etc/yum.repos.d/epel.repo,将mirrorlist和metalink开头的行注释掉。

接下来,取消注释这个文件里baseurl开头的行,并将其中的http://download.fedoraproject.org/pub替换成https://mirrors.tuna.tsinghua.edu.cn。

可以用如下命令自动替换:(来自 https://github.com/tuna/issues/issues/687)

sed -e 's!^metalink=!#metalink=!g' \
    -e 's!^#baseurl=!baseurl=!g' \
    -e 's!//download\.fedoraproject\.org/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
    -e 's!//download\.example/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
    -e 's!http://mirrors!https://mirrors!g' \
    -i /etc/yum.repos.d/epel*.repo

 

修改结果如下:(仅供参考,不同版本可能不同)

[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch/debug
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/SRPMS
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1

 

运行 yum update 测试一下吧。

 

 

 

 更换阿里云源

1.进入到yum的源目录下
 
 cd /etc/yum.repos.d/
 
2.将原来的CentOS-Base.repo进行备份
 
 mv CentOS-Base.repo CentOS-Base.repo_back
 
3.下载替换阿里源
 
 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
5.生成缓存
yum clean all  清除缓存
yum makecache  生成缓存

 

安装常用工具

需要配置好epel源

必须安装的工具

tree   vim wget  bash-completion  bash-completion-extras  lrzsz  net-tools sysstat  iotop iftop htop unzip nc nmap telnet bc  psmisc httpd-tools  bind-utils nethogs expect

 

命令作用
tree 以树形显示目录结构
psmisc pstree 等命令
vim vi升级版
wget 下载工具
bash-completion(默认源) 自动补全增强工具,可以补全命令参数
bash-completion-extras(epel) 自动补全超级增强工具,可以补全命令参数
lrzsz 上传下载工具 rz和sz
net-tools ifconfig route命令属于的软件包
sysstat sar iostat 属于的软件包
iotop 查询系统每个进程的io读写情况 swap写入情况
iftop 查询网络流量情况
nethogs 显示每个进程的流量情况
htop top升级版
unzip 解压
nc ncat 类似telnet
nmap 网络扫描工具
telnet 检查端口是否开启
bc 基础计算器
lsof 显示所有被打开的文件
dig DNS解析 bind-utils软件包
nslookup DNS解析 bind-utils软件包
host DNS解析 bind-utils软件包
htpasswd 创建密码文件 存放用户名和加密的密码 一般用于 nginx 简单认证中
mkpasswd 生成随机密码 属于expect软件包

 

 

yum常用命令

  • 1. 列出所有可更新的软件清单命令:yum check-update

  • 2. 更新所有软件命令:yum update

  • 3. 仅安装指定的软件命令:yum install <package_name>

  • yum reinstall 重新安装
  • 4. 仅更新指定的软件命令:yum update <package_name>

  • 5. 列出所有可安裝的软件清单命令:yum list

  • 6. 删除软件包命令:yum remove <package_name>

  • 7. 查找软件包命令:yum search <keyword>

  • 查找:yum provides 根据文件。命令。库文件,搜索对应的软件包
  • 缓存yum源软件仓库,xml元数据文件 yum makecache
  • 8. 清除缓存命令:

    • yum clean packages: 清除缓存目录下的软件包
    • yum clean headers: 清除缓存目录下的 headers
    • yum clean oldheaders: 清除缓存目录下旧的 headers
    • yum clean, yum clean all (= yum clean packages; yum clean oldheaders) :清除缓存目录下的软件包及旧的 headers

 

yum install -y bash-completion*   命令不全工具
yum clean packages 只清除缓存的软件包

 

包组相关指令:

一次性安装常用软件开发工具
yum -y group install Development tools或者  yum  group install Development tools

 

yum provides 查询某个程序所在安装包

已知apache 的一个压测工具名为:ab,但是不知道应该安装那个安装包的情况,使用yum provides指令参考如下:

 

 

查询到安装包为:httpd-tools

直接安装:

 

 

 

 

 

 

全局配置文件:

/etc/yum.conf

[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

 

 

YUM 签名检查机制

 

YUM更新软件包

 

 删除软件包

 

yum将软件安装到指定的目录

有时候可能希望将软件安装到自己指定的某个目录,这里记录一下实现办法。

【注意】个人并不推荐使用yum安装软件时将软件安装到自定义目录,因为实际操作过程中发现如果安装到自定义目录,会额外安装很多依赖的软件包,即使这些依赖包已经安装过,也会在你自定义的目录中重新安装!

yum -c /etc/yum.conf  --installroot=/usr/local --relearever=/ install love
yum install  percona-xtrabackup-24-2.4.23-1.el6.x86_64.rpm --installroot=/home/xxx/percona-xtrabackup/

 

该命令简单解释如下:

-c /etc/yum.conf                  表示指定yum配置文件地址

--installroot=/usr/local        表示指定自定义的安装目录

 

本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/16518594.html

posted on 2022-07-25 19:47  孙龙-程序员  阅读(118)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3