因爲後面工做中可能會遇到服務器在無公網的狀況下使用,那麼這個時候就可能須要在本地搭建私有yum倉庫
了。正好趁着週末學習一下如何搭建,如下是我學習自建yum倉庫的筆記。html
這裏我默認你們都會編譯安裝Nginx,因此使用yum的方式安裝以減小文章篇幅。python
# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
複製代碼
# yum -y install nginx
複製代碼
# systemctl start nginx
# systemctl enable nginx
複製代碼
mirrors.conf
# vim /etc/nginx/conf.d/mirrors.conf
複製代碼
內容以下:nginx
server {
listen 80 default_server;
server_name _;
location / {
# 倉庫文件路徑
root /opt/aliyun-mirrors/;
# 開啓目錄顯示功能
autoindex on;
# 關閉詳細文件大小統計,默認爲b,以kb、mb、gb爲單位顯示
autoindex_exact_size off;
# 開啓以服務器時區顯示文件修改日期
autoindex_localtime on;
# 以html風格將目錄展現在瀏覽器中
autoindex_format html;
charset utf-8,gbk;
}
}
複製代碼
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# nginx -s reload
複製代碼
Everything
版本的ISO鏡像文件相對rpm包要豐富一些,且使用ISO鏡像來搭建倉庫是比較方便快捷的。shell
iso鏡像文件
至服務器# mkdir /mnt/centos7-iso
複製代碼
# mount -o loop -t iso9660 /tmp/CentOS-7-x86_64-Everything-1908.iso /mnt/centos7-iso/
mount: /dev/loop0 is write-protected, mounting read-only
# echo "/tmp/CentOS-7-x86_64-Everything-1908.iso /mnt/centos7-iso/ iso9660 defaults 0 0" >> /etc/fstab
複製代碼
掛載完成,使用方法見下方vim
安裝必備工具centos
createrepo
命令用於建立yum源(軟件倉庫),即爲存放於本地特定位置的衆多rpm包創建索引,描述各包所需依賴信息,並造成元數據reposync
命令是一個python腳本
。包含在yum-utils
包中。所以,咱們若是要使用reposync命令的時候,須要安裝yum-utils包。# yum -y install createrepo yum-utils
複製代碼
獲取阿里雲鏡像站配置文件瀏覽器
# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
複製代碼
建立存儲倉庫路徑bash
# mkdir -p /opt/aliyun-mirrors/centos/7/os/{base,extras,updates}
複製代碼
使用reposync
同步鏡像到本地,命令格式:reposync -r 倉庫名(通常爲base) -p 目標目錄
服務器
# reposync -r base -p /opt/aliyun-mirrors/centos/7/os/
# reposync -r extras -p /opt/aliyun-mirrors/centos/7/os/
# reposync -r updates -p /opt/aliyun-mirrors/centos/7/os/
複製代碼
建立yum倉庫dom
# createrepo /opt/aliyun-mirrors/centos/7/os/base/
# createrepo /opt/aliyun-mirrors/centos/7/os/extras/
# createrepo /opt/aliyun-mirrors/centos/7/os/updates/
複製代碼
至此鏡像同步完成,如今配置nginx提供外部訪問便可,配置文件如上述nginx部署中配置文件,修改本地倉庫地址爲你所建立地址便可,訪問瀏覽器以下界面
慎用!由於文件總量很大,會佔用服務器較多資源,而且因爲流量過大可能會封IP,如非必要請儘可能避免使用該方式
同步腳本以下:
#!/bin/bash
kill -9 $(ps -aux | grep rsync | grep -v grep | awk '{print $2}')
# 待同步倉庫
repos=("centos" "epel" "ceph")
# repo存儲路徑
mirrorsdir="/opt/mirrors/"
# 中科大鏡像站rsync地址
# URL="rsync://rsync.mirrors.ustc.edu.cn/repo/"
# 清華大學鏡像站rsync地址
URL="rsync://mirrors.tuna.tsinghua.edu.cn/"
if [ ! -d "${mirrorsdir}" ]; then
echo "建立鏡像倉庫路徑:${mirrorsdir}"
mkdir -p ${mirrorsdir}
fi
# 同步倉庫
for repo in ${repos[@]}; do
if [ ! -d "${mirrorsdir}/${repo}" ]; then
echo "建立${repo}鏡像倉庫路徑"
mkdir -p ${mirrorsdir}/${repo}
fi
echo "開始同步${repo}相關鏡像文件"
rsync -avzPH --delete ${URL}${repo}/ ${mirrorsdir}${repo}/
done
複製代碼
保存腳本後直接執行便可,同步完成後能夠在瀏覽器看到相應的文件列表
若是單機使用,則在/etc/yum.repos.d/CentOS-Base.repo
中baseurl
的值爲file:///倉庫文件路徑
,提供給局域網其餘服務器的話須要使用Nginx或者Apache,而且baseurl
的值爲倉庫服務器的IP
或者域名
。
如下爲客戶端修改repo的腳本,使用時修改倉庫地址及IP部分便可
#!/bin/bash
# 自建倉庫IP
repoIP="10.100.5.80"
echo "添加域名解析到hosts文件"
cat /etc/hosts | grep ${repoIP}
if [ $? == 1 ]; then
echo "${repoIP} mirrors.akiya.org" >>/etc/hosts
echo "備份CentOS-Base.repo"
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
echo "寫入自建源repo配置文件"
cat >/etc/yum.repos.d/CentOS-Base.repo <<EOF
[akiya-mirror]
name=CentOS-$releasever - Base
# 若是是本地倉庫則填寫本地文件路徑便可
# 注意:file:///是三斜線,必定要記住路徑不能寫錯,寫錯你就走遠了,老弟
baseurl=file:///mnt/centos7-iso/
# 遠程倉庫則填寫對應的遠程倉庫地址
# baseurl=http://mirrors.akiya.org/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF
yum clean all
yum repolist
fi
複製代碼
保存並執行腳本後能夠看到已有咱們的自建源了。
複製代碼
# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id repo name status
akiya-mirror CentOS- - Base 10,097
repolist: 10,097
複製代碼
能夠看到咱們安裝軟件的時候Repository
顯示爲咱們本身設置的akiya-mirror
使用下述腳本,修改domain
值爲倉庫所在服務器的IP
或域名
便可。
#!/bin/bash
# 自建倉庫IP
repoIP="10.100.5.80"
domain="mirrors.akiya.org"
echo "添加域名解析到hosts文件"
cat /etc/hosts | grep ${repoIP}
if [ $? == 1 ]; then
echo "${repoIP} ${domain}" >>/etc/hosts
echo "備份CentOS-Base.repo"
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
echo "寫入自建源repo配置文件"
cat >/etc/yum.repos.d/CentOS-Base.repo <<EOF
[base]
name=CentOS-\$releasever - Base - ${domain}
failovermethod=priority
baseurl=http://${domain}/centos/\$releasever/os/base/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-\$releasever - Updates - ${domain}
failovermethod=priority
baseurl=http://${domain}/centos/\$releasever/os/updates/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-\$releasever - Extras - ${domain}
failovermethod=priority
baseurl=http://${domain}/centos/\$releasever/os/extras/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF
yum clean all
yum repolist
fi
複製代碼
使用此方法自建源後/etc/yum.repos.d/CentOS-Base.repo
修改內容請參考中科大CentOS 源使用幫助,把baseurl
的域名替換爲你自建倉庫服務器的IP
或者域名
便可。
使用iso鏡像:
11GB
iso文件
,既能夠安裝操做系統又能掛載用做本地yum倉庫使用reposync同步阿里雲等鏡像倉庫對應發行版本:
9.8GB
;extras(305):408MB
;updates(680):5.5GB
;合計:16GB
使用rsync同步完整倉庫:
150GB
,估計CentOS相關倉庫文件用500G磁盤應該夠了