由於ISO鏡像中的軟件包太少、太老,所以我的實驗使用的yum源是阿里雲鏡像源;但是阿里雲鏡像源必須要連接外網才可以使用,做實驗時倒無所謂;但是在公司部署大量服務器時會占用出口帶寬。另外有些特殊的鏡像源架設在國外,使用起來很不穩定。所以本地yum源倉庫時必要的
下面是我在部署公司本地yum倉庫時的具體部署步驟(以epel-7源為例):
1、部署環境:
- 系統:Centos7 x86_64
- 應用服務:httpd、createrepo、reposync
- 鏡像源:https://mirrors.aliyun.com/repo/epel-7.repo
2、現在阿里雲鏡像源repo文件到本地:
[root@localhost ~] wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
3、安裝httpd服務,用於提供存放yum源:
[root@localhost ~] yum install httpd -y
使用默認配置即可,如果有特殊需求可以修改配置文件/etc/httpd/conf/httpd.conf
http默認家目錄/var/www/html
4、安裝yum-utils提供reporsync服務:
[root@localhost ~] yum install yum-utils -y
5、選擇指定倉庫標識作為本地yum源:
執行yum repolist命令查看yum倉庫標識
[root@localhost ~]# yum repolist Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * epel: mirrors.aliyun.com * extras: mirrors.163.com * updates: mirrors.163.com repo id repo name status base/7/x86_64 CentOS-7 - Base 9,363 epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 11,681 extras/7/x86_64 CentOS-7 - Extras 380 updates/7/x86_64 CentOS-7 - Updates 1,838 repolist: 23,262
我使用epel作為本地yum源,用/var/www/html作為yum倉庫根目錄
[root@localhost ~]# reposync -r epel -p /var/www/html/
命令執行完畢后,會將阿里雲中的epel源同步到本地/var/www/html中;在/var/www/html中自動創建epel目錄用於存放rpm包;第一次同步是時間可能較長,大概1W多個rpm包。
| 19 kB 00:00:00 (11680/11681): zvbi-fonts-0.2.35-1.el7.noarch.rpm | 116 kB 00:00:00 (11681/11681): xonotic-data-0.8.2-1.el7.noarch.rpm | 873 MB 00:14:38 [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# ll /var/www/html/ total 4 drwxr-xr-x. 31 root root 4096 May 27 23:50 epel
6、createrepo 命令創對/var/www/html/epel下的 rpm 包 創建為本地的 YUM 倉庫,目的是為生成repodata目錄並自動創建索引信息
[root@localhost ~]# createrepo -pdo /var/www/html/epel/ /var/www/html/epel/ #第一個目錄是repodata存放目錄,第二個目錄是需要生成索引信息yum源倉庫目錄 Spawning worker 0 with 5841 pkgs Spawning worker 1 with 5840 pkgs Workers Finished Saving Primary metadata Saving file lists metadata Saving other metadata Generating sqlite DBs Sqlite DBs complete [root@localhost ~]# ll /var/www/html/epel/repodata/ total 34824 -rw-r--r--. 1 root root 2950038 May 28 02:09 458c248cc2fc1f9be754a7fd20f576c4acd231e32a49dfe26ba441d9ef1e8196-other.sqlite.bz2 -rw-r--r--. 1 root root 9980489 May 28 02:09 691ac5899b99b6a7d6de2c03f8706ab40a4104ffa95169837cd53860be9bd08c-filelists.sqlite.bz2 -rw-r--r--. 1 root root 9029887 May 28 02:09 9efa5c655cbaa93cfe74fa88f1521a00cdd7575a1ee537b3285c2f739470cd78-filelists.xml.gz -rw-r--r--. 1 root root 6186568 May 28 02:09 b833e13dd3f617ae5c2f228991fcaecaf5b2602844da466fae2b6eef2578e64e-primary.sqlite.bz2 -rw-r--r--. 1 root root 3369194 May 28 02:09 f5dc4532b7ee4bc249b397e441842f684dc9df3b6b37cfb709080802c4fe9fce-primary.xml.gz -rw-r--r--. 1 root root 2101964 May 28 02:09 f9f0522ce0356e3e95b0849e59ebfe35870c4359a8d1c6447b1d017a23ececbe-other.xml.gz -rw-r--r--. 1 root root 3012 May 28 02:09 repomd.xml
7、驗證本地yum源是否能正常使用:
登錄其他內網其他服務器,要能與yum源服務器通信;編寫repo文件:
vim /etc/yum.repos.d/epel-7.repo內容如下 [epel] name=local epel baseurl=http://yum源服務器IP/epel enabled=1 gpgcheck=0
備份其他repo文件至/etc/yum.repos.d/repobak目錄下,以防影響測試:
[root@localhost yum.repos.d]# ll /etc/yum.repos.d/ total 8 -rw-r--r--. 1 root root 79 May 28 17:26 epel.repo drwxr-xr-x. 2 root root 4096 May 28 17:25 repobak
執行yum clean all、yum makecache:
[root@localhost yum.repos.d]# yum clean all Loaded plugins: fastestmirror Cleaning repos: epel Cleaning up everything [root@localhost yum.repos.d]# yum makecache Loaded plugins: fastestmirror epel | 2.9 kB 00:00:00 (1/3): epel/primary_db | 5.9 MB 00:00:00 (2/3): epel/filelists_db | 9.5 MB 00:00:00 (3/3): epel/other_db | 2.8 MB 00:00:00 Determining fastest mirrors Metadata Cache Created
#上面信息表明本地epel源被正常緩存;表明配置成功。
7、為保證本地yum源能和阿里雲鏡像源同步,可以通過腳本定時任務實現:
vim /root/yum-update.sh #!/bin/bash datetime=`date +"%Y-%m-%d"` exec > /var/log/epel.log #同步日志輸出 reposync -d -r epel -p /var/www/html/ #同步鏡像源 if [ $? -eq 0 ];then createrepo --update /var/www/html/epel #每次添加新的rpm時,必須更新epel索引信息 echo "SUCESS: $datetime epel update successful"else echo "ERROR: $datetime epel update failed"fi #定時任務:每周六凌晨三點同步yum源 crontab -e 0 3 * * 6 /bin/bash /root/yum-update.sh