共计 7077 个字符,预计需要花费 18 分钟才能阅读完成。
1 背景
由于开发环境只有局域网,没法使用网上的各种 YUM 源,来回拷贝 rpm 包安装麻烦,还得解决依赖问题。想着在 CentOS7.2 搭建个本地 / 局域网 YUM 源,方便自己跟同事安装软件。
2 环境
[root@linuxidc.com ~]# cat /etc/RedHat-release
CentOS Linux release 7.2.1511 (Core)
并且是 Minimal Install。
3 安装
3.1 本地 YUM 源
3.1.1 ISO 源
准备 rpm 包
挂载 CentOS-7-x86_64-Everything-1511.iso,把里面所有文件都拷贝到本地目录 /yum/yum-iso
[root@linuxidc.com ~]# mkdir /mnt/yum-iso
[root@linuxidc.com ~]# mount /dev/cdrom /mnt/yum-iso/
mount: /dev/sr0 is write-protected, mounting read-only
[root@linuxidc.com ~]# mkdir /yum/
[root@linuxidc.com ~]# cp -a /mnt/yum-iso/ /yum/
[root@linuxidc.com ~]# ll /yum/
total 4
dr-xr-xr-x. 8 root root 4096 Dec 10 2015 yum-iso
[root@linuxidc.com ~]# ll /yum/yum-iso/
total 640
-r--r--r--. 1 root root 14 Dec 10 2015 CentOS_BuildTag
dr-xr-xr-x. 3 root root 33 Dec 10 2015 EFI
-r--r--r--. 1 root root 215 Dec 10 2015 EULA
-r--r--r--. 1 root root 18009 Dec 10 2015 GPL
dr-xr-xr-x. 3 root root 69 Dec 10 2015 images
dr-xr-xr-x. 2 root root 4096 Dec 10 2015 isolinux
dr-xr-xr-x. 2 root root 41 Dec 10 2015 LiveOS
dr-xr-xr-x. 2 root root 483328 Dec 10 2015 Packages
dr-xr-xr-x. 2 root root 4096 Dec 10 2015 repodata
-r--r--r--. 1 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-7
-r--r--r--. 1 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--. 1 root root 2883 Dec 10 2015 TRANS.TBL
[root@linuxidc.com ~]#
备份、移除其他 repo
[root@linuxidc.com ~]# cd /etc/yum.repos.d/
[root@linuxidc.com yum.repos.d]# ll
-rw-r--r--. 1 root root 1664 Dec 9 2015 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Dec 9 2015 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Dec 9 2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 290 Dec 9 2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Dec 9 2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Dec 9 2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 Dec 9 2015 CentOS-Vault.repo
[root@linuxidc.com yum.repos.d]# tar zcvf repo-bk.tar.gz CentOS-*
CentOS-Base.repo
CentOS-CR.repo
CentOS-Debuginfo.repo
CentOS-fasttrack.repo
CentOS-Media.repo
CentOS-Sources.repo
CentOS-Vault.repo
[root@linuxidc.com yum.repos.d]# rm -f CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Sources.repo CentOS-Vault.repo
配置本地 repo
[root@linuxidc.com yum.repos.d]# vi CentOS-Media.repo
填入如下内容
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///yum/yum-iso/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
注释:配置 repo 路径、关闭 gbp 校验、启用这个 repo
制作 cache
[root@linuxidc.com yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: c7-media
Cleaning up everything
Cleaning up list of fastest mirrors
[root@linuxidc.com yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
c7-media | 3.6 kB 00:00:00
(1/4): c7-media/group_gz | 155 kB 00:00:00
(2/4): c7-media/primary_db | 5.3 MB 00:00:00
(3/4): c7-media/filelists_db | 6.2 MB 00:00:00
(4/4): c7-media/other_db | 2.3 MB 00:00:00
Determining fastest mirrors
Metadata Cache Created
[root@linuxidc.com yum.repos.d]#
这样就可以 使用yum 安装软件啦
[root@linuxidc.com yum.repos.d]# yum groupinstall "Development tools"
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package autoconf.noarch 0:2.69-11.el7 will be installed
--> Processing Dependency: perl >= 5.006 for package: autoconf-2.69-11.el7.noarch
--> Processing Dependency: m4 >= 1.4.14 for package: autoconf-2.69-11.el7.noarch
--> Processing Dependency: perl(warnings) for package: autoconf-2.69-11.el7.noarch
或者
[root@linuxidc.com yum.repos.d]# yum install tree
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution
3.1.2 自定义 YUM 源
[root@linuxidc.com yum.repos.d]# yum -y install createrepo
准备 rpm 包
(需要自己确定依赖包)、目录。这里使用 tree 做演示。
[root@linuxidc.com ~]# mkdir -p /yum/yum-custom/packages
[root@linuxidc.com ~]# cp tree-1.6.0-10.el7.x86_64.rpm /yum/yum-custom/packages/
创建 repo
[root@linuxidc.com ~]# createrepo -u -d /yum/yum-custom/
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@linuxidc.com ~]# ll /yum/yum-custom/
total 4
drwxr-xr-x. 2 root root 41 Dec 20 07:03 packages
drwxr-xr-x. 2 root root 4096 Dec 20 07:08 repodata
[root@linuxidc.com ~]#
配置自定义 repo
[root@linuxidc.com ~]# vi /etc/yum.repos.d/CentOS-Media.repo
填入如下内容
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///yum/yum-custom/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
制作 cache
[root@linuxidc.com ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: c7-media
Cleaning up everything
Cleaning up list of fastest mirrors
[root@linuxidc.com ~]#
[root@linuxidc.com ~]# yum makecache
Loaded plugins: fastestmirror
c7-media | 3.0 kB 00:00:00
(1/3): c7-media/filelists_db | 880 B 00:00:00
(2/3): c7-media/primary_db | 1.8 kB 00:00:00
(3/3): c7-media/other_db | 1.3 kB 00:00:00
Determining fastest mirrors
Metadata Cache Created
[root@linuxidc.com ~]#
使用自定义 repo
[root@linuxidc.com ~]# yum install tree
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution
3.2 局域网 YUM 源
局域网 YUM 源可以配成:本地 YUM 源 + FTP 服务器
这里使用自定义源 yum-custom(见上面)+ VSFTP 演示。
下载 vsftpd
[root@linuxidc.com ~]# yum -y install vsftpd
配置 vsftpd
编辑 vsftp.conf
[root@linuxidc.com ~]# vi /etc/vsftpd/vsftpd.conf
并增加匿名用户 root 目录(默认已经启用匿名访问)
anon_root=/yum/
注意目录权限如下
[root@linuxidc.com ~]# ll -a /yum/
total 8
drwxr-xr-x. 4 root root 37 Dec 20 07:37 .
dr-xr-xr-x. 18 root root 4096 Dec 20 06:32 ..
drwxr-xr-x. 4 root root 36 Dec 20 07:08 yum-custom
dr-xr-xr-x. 8 root root 4096 Dec 10 2015 yum-iso
关闭 selinux
临时
[root@linuxidc.com ~]# setenforce 0
或者,永久:
编辑 config
[root@linuxidc.com ~]# vi /etc/selinux/config
设置
SELINUX=disabled
重启
[root@linuxidc.com ~]# reboot
启用 vsftp
[root@linuxidc.com ~]# systemctl start vsftpd
[root@linuxidc.com ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
[root@linuxidc.com ~]#
局域网使用
局域网另外一台机器 min-client,
配置 repo
编辑 repo
[root@min-client ~]# vim /etc/yum.repos.d/CentOS-Media.repo
内容如下
[c7-media]
name=CentOS-$releasever - Media
baseurl=ftp://192.168.118.133/yum-custom
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
其中 192.168.118.133 为上面 vsftp 服务器地址
启用这个局域网的 repo
[root@min-client ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: c7-media
Cleaning up everything
[root@min-client ~]# yum makecache
Loaded plugins: fastestmirror
c7-media | 3.0 kB 00:00
(1/3): c7-media/filelists_db | 880 B 00:00
(2/3): c7-media/other_db | 1.3 kB 00:00
(3/3): c7-media/primary_db | 1.8 kB 00:00
Determining fastest mirrors
Metadata Cache Created
[root@min-client ~]#
使用
[root@linuxidc.com ~]# yum -y install tree
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution
目前差不多这些就够用了。以后有时间把 163 的 YUM 源爬下来,当本地源用。
更多 YUM 相关教程见以下内容:
RHEL7 本地 yum 源配置 http://www.linuxidc.com/Linux/2017-01/139140.htm
CentOS 7 使用阿里云的 yum 源、PIP 源 http://www.linuxidc.com/Linux/2017-01/13966.htm
CentOS 及 Red Hat Linux 安装 yum 源 http://www.linuxidc.com/Linux/2017-02/140205.htm
CentOS 7 更改 yum 源与更新系统 http://www.linuxidc.com/Linux/2017-01/140067.htm
redhat7.0 配置本地 yum 源 http://www.linuxidc.com/Linux/2017-01/139148.htm
软件包管理之前端管理工具 yum http://www.linuxidc.com/Linux/2017-02/140270.htm
CentOS 7 使用阿里云的 yum 源、PIP 源 http://www.linuxidc.com/Linux/2017-01/13966.htm
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141391.htm