如何轻松创建和使用本地CentOS 7 YUM存储库
作为Linux系统管理员,虽然有义务在服务器上安装软件包,但是并非总是必须通过Internet从官方CentOS存储库中安装软件包。
一个很好的例子是当Internet连接速度慢并且想要保存时通过在LAN上创建存储库来增加带宽。
本指南将准确地做到这一点。
我们提供了使用CentOS DVD ISO镜像创建可正常工作的本地yum存储库所需的所有步骤。
以下是使用的步骤:
步骤1:下载CentOS-7-x86_64-Everything ISO或者CentOS 7 DVD ISO镜像。
我将使用CentOS-7-x86_64-Everything ISO镜像,该镜像可从CentOS 7官方的下载页面下载。
我们也可以使用CentOS 7 DVD ISO。
步骤2:创建挂载点和挂载ISO镜像。
下载后,我们必须挂载它。
我们可以使用两个挂载选项:将ISO文件挂载到Linux系统中的本地目录中。
a)创建挂载目录
[root@controller]# mkdir /mnt/centos7
b) 安装CentOS Everything或CentOS 7 DVD ISO的ISO映像
[root@controller]# mount -t iso9660 -o loop CentOS-7-x86_64-Everything-1511.iso /mnt/centos7
c)使用du或者ls命令确认已安装。
[root@controller ~]# du -sch /mnt/centos7/* 512 /mnt/centos7/CentOS_BuildTag 5.9M /mnt/centos7/EFI 512 /mnt/centos7/EULA 18K /mnt/centos7/GPL 466M /mnt/centos7/images 84M /mnt/centos7/isolinux 280M /mnt/centos7/LiveOS 6.5G /mnt/centos7/Packages 25M /mnt/centos7/repodata 2.0K /mnt/centos7/RPM-GPG-KEY-CentOS-7 2.0K /mnt/centos7/RPM-GPG-KEY-CentOS-Testing-7 3.0K /mnt/centos7/TRANS.TBL 7.4G total [root@controller ~]#
"将ISO镜像文件安装到Web服务器目录。
"我正在使用在另一台CentOS服务器上运行的Apache服务器。
如果要在其他Linux服务器上进行此操作,请用此处提供的目录替换Web服务器的根目录。
a)创建安装目录
[root@controller]# mkdir /var/www/html/centos/ [root@controller]# mount -t iso9660 -o loop CentOS-7-x86_64-Everything-1511.iso /var/www/html/centos/
我们从上一个命令获得的输出是:mount:/dev/loop0被写保护,只读安装/var/www/html/centos /目录的内容应如下所示:
[root@controller]# ls -lh /var/www/html/centos/ total 1.5M -r–r–r– 1 root root 14 Dec 10 01:35 CentOS_BuildTag dr-xr-xr-x3 root root 2.0K Dec 10 01:33 EFI -r–r–r– 1 root root 215 Dec 10 01:35 EULA -r–r–r– 1 root root 18K Dec 10 01:35 GPL dr-xr-xr-x 3 root root 2.0K Dec 10 01:33 images dr-xr-xr-x 2 root root 2.0K Dec 10 01:33 isolinux dr-xr-xr-x 2 root root 2.0K Dec 10 01:33 LiveOS dr-xr-xr-x 2 root root 1.5M Dec 10 01:34 Packages dr-xr-xr-x 2 root root 4.0K Dec 10 01:35 repodata -r–r–r– 1 root root 1.7K Dec 10 01:35 RPM-GPG-KEY-CentOS-7 -r–r–r– 1 root root 1.7K Dec 10 01:35 RPM-GPG-KEY-CentOS-Testing-7 -r–r–r– 1 root root 2.9K Dec 10 01:36 TRANS.TBL [root@controller ~]#
如果CD/DVD驱动器上有CentOS 7 DVD ISO镜像。
使用以下命令创建安装点并安装cdrom:
[root@controller]# mkdir -p /mnt/cent/cdrom [root@controller]# mount /dev/cdrom /mnt/cent/cdrom
我们可以看到/mnt/cent/cdrom /的内容
[root@controller]# ls -lh /mnt/cent/cdrom/ total 1.5M -r–r–r– 1 root root 14 Dec 10 01:35 CentOS_BuildTag dr-xr-xr-x 3 root root 2.0K Dec 10 01:33 EFI -r–r–r– 1 root root 215 Dec 10 01:35 EULA -r–r–r– 1 root root 18K Dec 10 01:35 GPL dr-xr-xr-x 3 root root 2.0K Dec 10 01:33 images dr-xr-xr-x 2 root root 2.0K Dec 10 01:33 isolinux dr-xr-xr-x 2 root root 2.0K Dec 10 01:33 LiveOS dr-xr-xr-x 2 root root 1.5M Dec 10 01:34 Packages dr-xr-xr-x 2 root root 4.0K Dec 10 01:35 repodata -r–r–r– 1 root root 1.7K Dec 10 01:35 RPM-GPG-KEY-CentOS-7 -r–r–r– 1 root root 1.7K Dec 10 01:35 RPM-GPG-KEY-CentOS-Testing-7 -r–r–r– 1 root root 2.9K Dec 10 01:36 TRANS.TBL [root@controller ~]#
步骤3:创建一个仓库,并将其放入/etc/yum/repos.d/目录中。
在安装了CentOS最低版本的服务器上,或者从刚添加的本地存储库中提取软件包时需要安装软件包的服务器上,即可完成此操作。
a)对于第2步第1部分:`
[root@controller ~]# cat >> /etc/yum.repos.d/centos7-local.repo<< EOF [centos7-local] name=centos7-local baseurl=file:///mnt/centos7/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 EOF
b)"对于第2步第2部分:":我正在使用的服务器的IP地址为" 192.168.1.60"。
以下是存储库配置。
[root@controller ~]# cat > /etc/yum.repos.d/centos7-local.repo<< EOF [centos7-local] name=centos7-local baseurl=http://192.168.1.60/centos enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 EOF
c)对于"第2步第3部分":我们使用的挂载点是/mnt/centos/cdrom /。
[root@controller ~]# cat > /etc/yum.repos.d/centos7-local.repo<< EOF [centos7-local] name=centos7-local baseurl=file:///mnt/cent/cdrom/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 EOF
检查添加的Repo:
[root@controller ~]# cat /etc/yum.repos.d/centos7-local.repo [centos7-local] name=centos7-local baseurl=http://192.168.1.60/centos enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
然后执行yum clean all删除缓存的日期并更新Repo列表
[root@controller ~]# yum clean all Loaded plugins: fastestmirror, langpacks, priorities Cleaning repos: base epel extras remi-safe updates Cleaning up everything Cleaning up list of fastest mirrors
注意:单独启用本地添加的存储库不足以让我们从中安装软件包,这是因为默认情况下,任何要安装的软件包都将使用CentOS Base存储库。
我们将在短期内讨论一个可行的解决方案。
CentOS 7存储库中可用的软件包组是:
[root@controller~]# yum group list Loaded plugins: fastestmirror There is no installed groups file. Maybe run: yum groups mark convert (see man yum) Loading mirror speeds from cached hostfile * base: mirror.bitco.co.za * epel: epel.mirrors.ovh.net * extras: mirror.bitco.co.za * updates: mirror.bitco.co.za Available Environment Groups: Minimal Install Compute Node Infrastructure Server File and Print Server MATE Desktop Basic Web Server Virtualization Host Server with GUI GNOME Desktop KDE Plasma Workspaces Development and Creative Workstation Available Groups: CIFS file server Compatibility Libraries Console Internet Tools Desktop Desktop Platform Desktop Platform Development Development Tools Eclipse Educational Software Electronic Lab FCoE Storage Client Fedora Packager General Purpose Desktop Graphical Administration Tools Haskell Legacy UNIX Compatibility Messaging Client Support Messaging Server Support Milkymist MySQL Database client MySQL Database server NFS file server Network Storage Server SNMP Support Scientific Support Security Tools Server Platform Server Platform Development Smart Card Support Storage Availability Tools System Administration Tools System Management TeX support TurboGears application framework Virtualization Web-Based Enterprise Management Xfce iSCSI Storage Client Done [root@asterisk-server1 ~]#
注意:当启用其他CentOS仓库并指定centos-local仓库时,yum总是尝试从仓库中下载带有最新软件包的最新软件包。
默认仓库是CentOS Base仓库。
如果要强制yum使用本地存储库,则必须禁用所有其他存储库并在完成后将其启用。
我们可以使用两个选项来实现此目的。
选项1:最简单的解决方案如下所示:
[root@controller ~]# mkdir ~/repos [root@controller ~]# cp -r /etc/yum.repos.d/* ~/repos/ [root@controller ~]# ls -l ~/repos/ total 56 -rw-r–r– 1 root root 143 Mar 17 02:33 centos7-local.repo -rw-r–r– 1 root root 1664 Mar 17 02:33 CentOS-Base.repo -rw-r–r– 1 root root 1309 Mar 17 02:33 CentOS-CR.repo -rw-r–r– 1 root root 649 Mar 17 02:33 CentOS-Debuginfo.repo -rw-r–r– 1 root root 290 Mar 17 02:33 CentOS-fasttrack.repo -rw-r–r– 1 root root 630 Mar 17 02:33 CentOS-Media.repo -rw-r–r– 1 root root 516 Mar 17 02:33 CentOS-OpenStack-liberty.repo -rw-r–r– 1 root root 1331 Mar 17 02:33 CentOS-Sources.repo -rw-r–r– 1 root root 1952 Mar 17 02:33 CentOS-Vault.repo -rw-r–r– 1 root root 957 Mar 17 02:33 epel.repo -rw-r–r– 1 root root 1056 Mar 17 02:33 epel-testing.repo -rw-r–r– 1 root root 1177 Mar 17 02:33 remi-php70.repo -rw-r–r– 1 root root 2340 Mar 17 02:33 remi.repo -rw-r–r– 1 root root 449 Mar 17 02:33 remi-safe.repo [root@controller ~]# [root@controller ~]# rm -rf /etc/yum.repos.d/* [root@controller ~]# ls -l /etc/yum.repos.d/ total 0
然后添加添加本地仓库。
[root@controller ~]#cat > /etc/yum.repos.d/centos7-local.repo<< EOF [centos7-local] name=centos7-local baseurl=http://192.168.1.60/centos enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 EOF
检查是否添加成功:
[root@controller ~]# cat /etc/yum.repos.d/centos7-local.repo [centos7-local] name=centos7-local baseurl=http://192.168.1.60/centos enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
我们可以进行任何安装。
yum将使用的唯一存储库是centos7-local。
示例:
要安装LDAP服务以配置OpenLDAP服务器,请执行以下操作:
[root@controller ~]# yum install openldap-servers openldap-clients Loaded plugins: fastestmirror, langpacks, priorities centos7-local | 3.6 kB 00:00:00 Loading mirror speeds from cached hostfile Resolving Dependencies –> Running transaction check —> Package openldap-clients.x86_64 0:2.4.40-8.el7 will be installed —> Package openldap-servers.x86_64 0:2.4.40-8.el7 will be installed –> Finished Dependency Resolution Dependencies Resolved ======================================================================================= Package Arch Version Repository Size ======================================================================================= Installing: openldap-clients x86_64 2.4.40-8.el7 centos7-local 186 k openldap-servers x86_64 2.4.40-8.el7 centos7-local 2.1 M Transaction Summary ======================================================================================== Install 2 Packages Total download size: 2.3 M Installed size: 5.2 M Is this ok [y/d/N]: y Downloading packages: (1/2): openldap-clients-2.4.40-8.el7.x86_64.rpm | 186 kB 00:00:00 (2/2): openldap-servers-2.4.40-8.el7.x86_64.rpm | 2.1 MB 00:00:00 —————————————————————————————————————————————————– Total 21 MB/s | 2.3 MB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : openldap-servers-2.4.40-8.el7.x86_64 1/2 Installing : openldap-clients-2.4.40-8.el7.x86_64 2/2 Verifying : openldap-clients-2.4.40-8.el7.x86_64 1/2 Verifying : openldap-servers-2.4.40-8.el7.x86_64 2/2 Installed: openldap-clients.x86_64 0:2.4.40-8.el7 openldap-servers.x86_64 0:2.4.40-8.el7 Complete! [root@controller ~]#
正如我们从下面的屏幕快照中看到的那样,所使用的存储库为centos7-local
。
要安装属于" GNOME桌面"环境组的所有软件包,我们将执行以下操作:
[root@controller ~]# yum groupinstall "GNOME Desktop"
要安装mariadb,mariadb-server之类的单个软件包,我们将执行以下操作:
[root@controller ~]# yum install mariadb mariadb-server
"选项2:使用–enablerepo = centos7-local命令选项和–disablerepo = *命令选项"。
要安装属于"虚拟化主机"环境组的所有软件包,我们将执行以下操作:
[root@controller ~]# yum –disablerepo=* –enablerepo=centos7-local groupinstall "Virtualization Host"
要安装openldap
#yum –disablerepo = * –enablerepo = centos7-local
安装openldap-servers openldap-客户端如果我们具有CentOS 7最低版本并安装了GNOME桌面环境组软件包,请不要忘记告诉systemd
自动启动到GNOME桌面。
[root@controller ~]# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
稍后,我们可以将存储库返回到原始目录:
[root@controller ~]# cp -r ~/repos/* /etc/yum.repos.d/
切记将存储库从~/repos /复制回/etc/yum.repos.d/目录。