本地yum源制作

配置yum源:
安装好centos7后,系统自带的有yum源,只不过yum指向的centos官方的yum源
如果想使用国内的yum源,比如:阿里源,163源,华为源等,需要额外配置
以配置阿里源举例:
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
注意:如果wget无法使用时,可是使用curl代替
 
本地源制作:
创建yum仓库需要用到createrepo,用于创建yum源(软件仓库),即为存放于本地特定位置的众多rpm包建立索引,描述各包所需依赖信息,并形成元数据。
我这里单独使用一个数据盘(可选)
fdisk快速分区,新建40G
echo -e 'n\np\n\n\n\nw' | fdisk /dev/sdb
mkfs.ext4 /dev/sdb1
e2label /dev/sdb1 /www
mkdir /www
mount -t ext4 /dev/sdb1 /www
df -h|grep /dev/sdb1
#开机挂载磁盘
echo "mount -t ext4 /dev/sdb1 /www" >>/etc/rc.d/rc.local
tail -1 /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
 
yum安装配置
设置yum下载目录
mkdir -p /www/share/yum
cp /etc/yum.conf{,.bak}
sed -i 's#^keepcache=0#keepcache=1#' /etc/yum.conf
sed -i 's/^cachedir/#cachedir/' /etc/yum.conf
sed -ir '3 icachedir=/www/share/yum/$basearch/$releasever \n' /etc/yum.conf
head /etc/yum.conf
 
#安装createrepo,http服务
yum install createrepo httpd -y
 
配置http目录共享
echo '#http share
Alias /share /www/share
<Directory "/www/share">
    Options Indexes FollowSymLinks
    IndexOptions NameWidth=* DescriptionWidth=* FoldersFirst
    IndexOptions SuppressIcon HTMLTable Charset=UTF-8 SuppressHTMLPreamble
    Order allow,deny
    Allow from all
    Require all granted
</Directory>
'>/etc/httpd/conf.d/share.conf
cp /etc/httpd/conf/httpd.conf{,.bak}
echo "
ServerName localhost
#关闭版本号显示
ServerSignature Off
ServerTokens Prod
">>/etc/httpd/conf/httpd.conf
systemctl enable httpd.service
systemctl restart httpd.service
浏览器访问 10.0.0.240/share ,能访问即正常
 
创建用来存放yum软件的目录
mkdir -p /www/share/centos7_rpm
createrepo -p /www/share/centos7_rpm/
 
创建源文件(可选,主要是给客户端使用)
echo "
[My_share]
name=My_Souce
baseurl=http://10.0.0.240/share/centos7_rpm/
gpgcheck=0
enabled=1
cost=88
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
">/www/share/Lan7.repo
 
RPM更新
yum缓存的rpm包拷贝到/www/share/centos7_rpm/
find /www/share/yum -name *.rpm |sed -r 's#.*#mv & /www/share/centos7_rpm/\n#'|bash
 
以下是createrepo的常用操作
下载没有安装过的包
yum install --downloadonly --downloaddir=/www/share/centos7_rpm/ -y 包名称
 
下载已经安装过的包
yum reinstall --downloadonly --downloaddir=/www/share/centos7_rpm/ -y 包名称
 
更新源,构建元数据文件
createrepo --update -p /www/share/centos7_rpm/
 
还可以编写脚本
#rpm下载、yum更新 shell
echo '#!/bin/sh
[ $# = 0 ] && {
echo "更新源"
/usr/bin/createrepo --update -p /www/share/centos7_rpm/
} || {
echo "yum下载"
yum install --downloadonly --downloaddir=/www/share/centos7_rpm/ -y $*
echo "更新源"
/usr/bin/createrepo --update -p /www/share/centos7_rpm/
}
'>yumd.sh
ln -s $(pwd)/yumd.sh /usr/bin/yumd
chmod +x yumd.sh
 
# 执行 yumd 更新源
# 执行 yumd 软件1 软件2 ,就会下载相关软件并更新源
posted @ 2020-08-03 17:31  李占勋  阅读(265)  评论(0编辑  收藏  举报