CentOS 7 搭建本地YUM倉庫,並定期同步阿里雲源


 

目錄導航:

  1. 系統環境

  2. 修改yum 源為阿里雲源

  3. 安裝yum相關的軟件

  4. 根據源標識同步源到本地目錄

  5. 安裝nginx開啟目錄權限保證本地機器可以直接本地yum源

  6. 客戶端創建repo文件

  

 

1. 系統環境 

# cat /etc/centos-release CentOS Linux release 7.6.1810 (Core) # uname -r 3.10.0-957.el7.x86_64 # ip a |awk 'NR==9{print $2}'|awk -F '/' '{print $1}'
10.0.0.100

 

2. 修改yum 源為阿里雲源

2.1 備份系統自帶的yum源

# tar -zcvf CentOS-bk.tar.gz /etc/yum.repos.d/CentOS-*

 

2.2 修改yum源 

# wget -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

或者

# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo  # curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

 

2.3 檢驗阿里雲源是否正常

# yum repolist

Loaded plugins: fastestmirror

Loading mirror speeds from cached hostfile

 * base: mirrors.aliyun.com

 * extras: mirrors.aliyun.com

 * updates: mirrors.aliyun.com

#倉庫標識倉庫名稱狀態

repo id                                                                     repo name                                                                                                status

!base/7/x86_64                                                              CentOS-7 - Base - mirrors.aliyun.com                                                                     10,019

!epel/x86_64                                                                Extra Packages for Enterprise Linux 7 - x86_64                                                           12,902

!extras/7/x86_64                                                            CentOS-7 - Extras - mirrors.aliyun.com                                                                      371

!updates/7/x86_64                                                           CentOS-7 - Updates - mirrors.aliyun.com                                                                   1,103

repolist: 24,395

 

3. 安裝yum相關的軟件

# yum install -y wget make cmake gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel createrepo yum-utils

yum-utils:reposync同步工具

createrepo:編輯yum庫工具

plugin-priorities:控制yum源更新優先級工具,這個工具可以用來控制進行yum源檢索的先后順序,建議可以用在client端。

 

注:由於很多人喜歡最小化安裝,上邊軟件是一些常用環境。

 

4. 根據源標識同步源到本地目錄

4.1 創建本地目錄

# mkdir /mirror

 

4.2 同步到本地目錄

# reposync -p / mirror

注:不用擔心沒有創建相關目錄,系統自動創建相關目錄,並下載,時間較長請耐心等待。

可以用  repo -r --repoid=repoid 指定要查詢的repo id,可以指定多個(# reposync -r base -p /mirror   #這里同步base目錄到本地)

更新新的rpm包

# reposync -np /mirror

注:時間同樣較長,請耐心等待。

 

4.3 創建索引

# createrepo -po /mirror/base/ /mirror/base/ # createrepo -po /mirror/extras/ /mirror/extras/ # createrepo -po /mirror/updates/ /mirror/updates/ # createrepo -po /mirror/epel/ /mirror/epel/

 

4.4 更新源數據

# createrepo --update /mirror/base # createrepo --update /mirror/extras # createrepo --update /mirror/updates # createrepo --update /mirror/epel

 

4.5 創建定時任務腳本

# vim /mirror/script/centos_yum_update.sh #!/bin/bash echo 'Updating Aliyum Source' DATETIME=`date +%F_%T` exec > /var/log/aliyumrepo_$DATETIME.log reposync -np /mirror if [ $? -eq 0 ];then createrepo --update /mirror/base createrepo --update /mirror/extras createrepo --update /mirror/updates createrepo --update /mirror/epel echo "SUCESS: $DATETIME aliyum_yum update successful"
  else
    echo "ERROR: $DATETIME aliyum_yum update failed"
fi

 

4.6 將腳本加入到定時任務中

# crontab -e # Updating Aliyum Source 00 13 * * 6 [ $(date +%d) -eq $(cal | awk 'NR==3{print $NF}') ] && /bin/bash /mirror/script/centos_yum_update.sh

每月第一個周六的13點更新阿里雲yum源

 

5. 安裝nginx開啟目錄權限保證本地機器可以直接本地yum源

5.1 創建運行賬戶

# groupadd nginx # useradd -r -g nginx -s /bin/false -M nginx # yum install nginx -y

 

5.2  修改nginx 配置文件

# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root /mirror ; #這里是yum源存放目錄 location / { autoindex on; #打開目錄瀏覽功能 autoindex_exact_size off; # off:以可讀的方式顯示文件大小 autoindex_localtime on; # on/off:是否以服務器的文件時間作為顯示的時間 charset utf-8,gbk; #展示中文文件名 index index.html; } error_page 500 502 503 504  /50x.html; location = /50x.html { root html; } } } 

 

6. 客戶端創建repo文件

注:搭建好后yum安裝速度並沒有想象中的那么快,安裝時解決依賴速度也很慢。 

# vim CentOS7.x-Base.repo [base] name=CentOS-$releasever - Base - mirror.template.com baseurl=http://10.0.0.100/base/
path=/ enabled=1 gpgcheck=0 [updates] name=CentOS-$releasever - Updates - mirror.template.com baseurl=http://10.0.0.100/updates/
path=/ enabled=1 gpgcheck=0 [extras] name=CentOS-$releasever - Extras - mirrors.template.com baseurl=http://10.0.0.100/extras/
path=/ enabled=1 gpgcheck=0 [epel] name=CentOS-$releasever - epel - mirrors.template.com baseurl=http://10.0.0.100/epel/
failovermethod=priority enabled=1 gpgcheck=0

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM