前面系列文章中,咱们对vi编辑器和46个基本命令进行了介绍,本文将演示在centos7下使用RPM和YUM安装和管理软件。php
RPM(RedHat Package Manager),相似于windows下的控制面板,而RPM软件包相似于setup.exe安装文件。RPM会创建统一的数据库文件详细记录软件信息并能自动分析依赖关系, 本来是Red Hat Linux发行版专门用来管理Linux各项套件的程序,因为它遵循GPL规则且功能强大方便,逐渐受到其余发行版的采用,使用范围已不限于红帽系统中了,如CentOS、fedora、suse。html
linux不一样发行版的软件包格式不同,好比ubuntu、debian采用后缀deb格式的软件包;而redhat、centos、fedora、suse等则采用rpm格式软件包。两者是不通用的。好比前文提到的SecureCRT远程工具,官方就提供了适用于不一样linux发行版的软件包。适用ubuntu18的包为:scrt-8.5.4-1942.ubuntu18-64.x86_64.deb,而适用redhat(centos)的包为:scrt-8.5.4-1942.rhel7-64.x86_64.rpm。前端
查看官网: https://www.vandyke.com/cgi-b...node
在RPM公布以前,要想在Linux系统中安装软件只能采起源码包安装的方式。早期在linux安装程序是很是困难的事情,运维人员须要自行编译代码并解决许多依赖关系,升级和卸载服务程序时也要考虑其余程序、库之间的依赖关系,难度很是大。linux
所以为了解决这些问题,RPM机制诞生了使得Linux易于安装和升级。web
仍是以上边的SecureCRT包为例进行说明。shell
scrt-8.5.4-1942.rhel7-64.x86_64.rpm数据库
语法:rpm -q 软件包名apache
案例:编程
[root@heimatengyun ~]# rpm -q firefox firefox-24.5.0-1.el7.centos.x86_64
因为咱们第一篇文章中安装gui已默认安装好firefox,因此此处查出来为已安装。
语法:rpm -ivh 全包名
选项:
-i:install安装
-v:verbose显示详细信息
-h:hash进度条
--nodeps:不检测依赖
说明:
系统镜像中包含了不少rpm包,须要时能够直接挂载光盘进行安装。若是光盘中未包含,能够去网上下载相应rpm包并切换到rpm包所在的目录,执行安装命令便可。
语法:rpm -e 软件包
ps:通常不建议经过此方法卸载软件,容易致使不少问题。
尽管RPM能帮助查询软件的依赖关系,但仍是须要运维人员本身安装解决依赖问题,大型软件依赖很是多,安装起来仍是很是麻烦的。为进一步下降软件安装的难度和复杂度,设计了YUM软件仓库。
YUM(Yellow dog Updater,Modified)是一个在RedHat、Centos和Fedora中的shell前端软件包管理器。能够认为YUM仓库用于管理RPM软件包,仓库中的包能够是红帽官方发布的,也能够是本身编写的。YUM软件仓库能够根据用户需求分许所需软件包以及相关的依赖关系,并自动从服务器下载软件包并安装到系统中。
所以平常工做中,软件一般是采用yum的方式进行安装。
命令 | 功能描述 |
---|---|
yum install 包名 | 安装服务,能够加-y选项避免每次询问 |
yum list | 列出全部可用的package和package组 |
yum clean all | 清除全部缓存数据 |
yum makecache | 生成缓存 |
yum deplist 包名 | 列出一个包全部的依赖包 |
yum remove 包名 | 删除服务 |
yum repolist | 列出可用的yum源 |
案例:
[root@heimatengyun ~]# yum install httpd -y Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.aliyun.com * updates: mirrors.cn99.com Resolving Dependencies --> Running transaction check ...省略中间部份内容 Installed: httpd.x86_64 0:2.4.6-90.el7.centos Dependency Installed: apr.x86_64 0:1.4.8-5.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-90.el7.centos mailcap.noarch 0:2.1.41-2.el7 Complete! [root@heimatengyun ~]# systemctl start httpd
提示安装成功,默认是没启动的,手动启动httpd服务,此时直接经过虚拟机登陆进系统,经过浏览器访问127.0.0.1,发现已经成功安装appache服务。
然而在物理主机上经过虚拟机ip,却没法访问。很大的缘由多是由于防火墙,前期学习能够直接关闭防火墙,避免学习干扰。
若是遇到不能访问的状况,请按如下顺序依次排查便可:
检查进程是否启动、查看进程启动端口、查看防火墙状态、若是防火墙开启则关闭。操做完理论上就能够在宿主机进行访问了。
[root@heimatengyun ~]# ps -ef |grep httpd root 44948 1 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 44949 44948 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 44950 44948 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 44951 44948 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 44952 44948 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 44953 44948 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND root 44963 42126 0 15:07 pts/0 00:00:00 grep --color=auto httpd [root@heimatengyun ~]# netstat -lntup |grep httpd tcp6 0 0 :::80 :::* LISTEN 44948/httpd [root@heimatengyun ~]# firewall-cmd --state running [root@heimatengyun ~]# systemctl stop firewalld.service [root@heimatengyun ~]# firewall-cmd --state not running
此时在物理机浏览器输入虚拟机ip地址,发现能够正常访问apache服务了。
也能够经过如下命令查看httpd服务是否正在运行
[root@heimatengyun ~]# systemctl status httpd.service httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: active (running) since Wed 2019-11-27 15:06:56 CST; 30min ago ...省略部份内容
注意:使用yum源安装时,httpd的版本通常不会是最新的,最新的版本只能去Apache官网下载源码包安装
[root@heimatengyun ~]# yum remove httpd Loaded plugins: fastestmirror, langpacks Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be erased --> Finished Dependency Resolution ...省略部份内容 Is this ok [y/N]: y ...省略部份内容 Removed: httpd.x86_64 0:2.4.6-90.el7.centos Complete!
此时再去查看状态,已经提示找不到服务了
[root@heimatengyun ~]# systemctl status httpd.service httpd.service Loaded: not-found (Reason: No such file or directory) Active: inactive (dead) ...省略部份内容
CentOS默认的yum源不必定是国内镜像,致使yum在线安装及更新速度不是很理想。这时候须要将yum源设置为国内镜像站点,国内主要开源的开源镜像站点有网易和阿里云等。
前提条件:centos要能链接外网。
主要思路就是在阿里云镜像站点下载阿里云的YUM配置文件,而后覆盖系统默认的YUM配置文件便可。配置文件能够本地下载上传覆盖,也能够直接在centos上下载。在覆盖时记得先备份一个默认YUM,以防不测。
按如下命令一条条执行便可
[root@heimatengyun yum.repos.d]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup [root@heimatengyun yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo --2019-11-27 16:13:24-- http://mirrors.aliyun.com/repo/Centos-7.repo Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 118.112.15.1, 182.140.218.238, 182.140.218.243, ... ...省略部份内容 [root@heimatengyun yum.repos.d]# ls CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Vault.repo CentOS-Base.repo.backup CentOS-Sources.repo [root@heimatengyun yum.repos.d]# yum clean all Loaded plugins: fastestmirror, langpacks Cleaning repos: base extras updates Cleaning up everything Cleaning up list of fastest mirrors [root@heimatengyun yum.repos.d]# yum makecache Loaded plugins: fastestmirror, langpacks ...省略部份内容 Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Metadata Cache Created
执行以上命令后替换成功,若是再次安装apache服务的话,能够看到yum已经替换为阿里云的源了。
[root@heimatengyun yum.repos.d]# yum install httpd Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com ...省略部份内容 此处只是为了验证如下源是否替换成功,按Ctrl+C中断安装
YUM源能够简化软件在Linux上的安装过程,可是由于有些特殊的状况下生产环境是没法上外网,不能链接外网的YUM源,所以就没法直接使用YUM命令安装软件。为了在内网中也可使用YUM安装软件,就须要配置本地YUM源。
YUM源本质是一个保存了多个RPM包的web服务器,能够经过http的方式来检索、下载并安装相关的RPM包。
(1)准备另一台虚拟机
按系列文章1从新再装一台centos虚拟机或克隆虚拟机,用于yum源服务器。
ps:系统初次安装完后作一个快照,之后克隆虚拟机就用这个快照,避免重复安装费时间
克隆的虚拟机给原系统如出一辙,所以没法同时启动,须要修改ip地址和主机名称。
修改ip地址:登陆界面,按前面文章的方法修改ip地址为192.168.78.101
修改主机名称:终端执行hostnamectl set-hostname yum-local-server命令便可
(2)将iso镜像挂载到目录
依次执行下列命令,建立目录并挂载,挂载以后经过df命令查看是否挂载成功
[root@yum-local-server /]# mkdir /mnt/cdrom [root@yum-local-server /]# mount /dev/cdrom /mnt/cdrom/ mount: /dev/sr0 is write-protected, mounting read-only [root@yum-local-server /]# df -HT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 19G 4.8G 15G 26% / devtmpfs devtmpfs 949M 0 949M 0% /dev tmpfs tmpfs 958M 87k 958M 1% /dev/shm tmpfs tmpfs 958M 9.4M 949M 1% /run tmpfs tmpfs 958M 0 958M 0% /sys/fs/cgroup /dev/sda1 xfs 521M 125M 397M 24% /boot /dev/sr0 iso9660 4.2G 4.2G 0 100% /mnt/cdrom
(3)修改本机YUM源配置文件
备份原来YUM源配置文件,将源指向本身。
[root@yum-local-server /]# cd /etc/yum.repos.d/ [root@yum-local-server yum.repos.d]# ls CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Sources.repo CentOS-Vault.repo [root@yum-local-server yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak [root@yum-local-server yum.repos.d]# ls CentOS-Base.repo.bak CentOS-Sources.repo CentOS-Debuginfo.repo CentOS-Vault.repo [root@yum-local-server yum.repos.d]# vi CentOS-Base.repo [base] name=CentOS-Local baseurl=file:///mnt/cdrom/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 保存并退出
ps:写法是固定格式,name是自定义仓库名称,baseurl是基础地址指向的是挂在的镜像目录,gpgcheck是一个秘钥功能(能够不启用,若是不启用就不指定gpgkey),enabled是启动的意思若是数值为1的话表明开启若是是0的话表明关闭
(4)清除缓存
[root@yum-local-server yum.repos.d]# yum clean all Loaded plugins: fastestmirror, langpacks Cleaning repos: base Cleaning up everything Cleaning up list of fastest mirrors
(5)列出可用源
[root@yum-local-server yum.repos.d]# yum repolist Loaded plugins: fastestmirror, langpacks base | 3.6 kB 00:00 (1/2): base/group_gz | 157 kB 00:00 (2/2): base/primary_db | 2.7 MB 00:00 Determining fastest mirrors repo id repo name status base CentOS-Local 3,538 repolist: 3,538
(6)安装apache服务
步骤:安装apache、打开httpd服务(默认是关闭的)、关闭防火墙
方法与前边一致,此处再也不贴代码。
(7)将YUM源配置到httpd
将yum源拷贝到httpd的web目录,这样内网其余服务器就能够访问这个内网中的yum源了。
[root@yum-local-server yum.repos.d]# cp -r /mnt/cdrom/ /var/www/html/CentOS
(8)浏览器访问测试
在宿主机直接经过访问 http://192.168.78.101/CentOS/
能够访问说明yum服务器配置成功
每台centos服务器均可以作成本地源,无须链接其余的yum,但若是有多台服务器的话,能够单独作成一台yum源服务器,而后每台服务器修改yum配置文件,将地址都指向它便可。下边咱们新准备一台虚拟机192.168.78.102(若是原来100那台机器没有执行yum remove httpd操做,也能够直接用)来演示本地yum的使用。
重要提示:本处强烈建议新装备一台虚拟机的缘由是,若是您是按照我上边的步骤依次执行,极可能已经执行2.2中的卸载httpd服务。如已执行卸载操做,再次用那台虚拟机执行下边的命令换为本地yum再次安装httpd,会一直装不成功,颇有多是卸载是相关的依赖有残余致使安装冲突。我就遇到这样的问题非常困惑,最后经过新的一台虚拟机上才安装成功。
[root@heimatengyun yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak [root@heimatengyun yum.repos.d]# vi CentOS-Base.repo [base] name=CentOS-101 baseurl=http://192.168.78.101/CentOS/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
保存退出后,清除缓存,列出可用的yum源
[root@heimatengyun yum.repos.d]# yum clean all Loaded plugins: fastestmirror, langpacks Cleaning repos: base Cleaning up everything Cleaning up list of fastest mirrors [root@heimatengyun yum.repos.d]# yum repolist Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile repo id repo name status base CentOS-101 3,538 repolist: 3,538
[root@heimatengyun yum.repos.d]# yum install httpd Loaded plugins: fastestmirror, langpacks ...省略部份内容 Installed: httpd.x86_64 0:2.4.6-17.el7.centos.1 Dependency Installed: apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-17.el7.centos.1 mailcap.noarch 0:2.1.41-2.el7 Complete!
安装成功。
本文讲完centos7下软件的安装和卸载、yum源的配置以后,下一节开始讲解管道符、重定向、环境变量等知识,为后边shell编程作好准备。