CentOS7 本地私有YUM源配置简录

# 依据《CentOS7实验机模板搭建部署》克隆实验主机,进行部署
HOSTNAME=LocalRepository
hostnamectl set-hostname "$HOSTNAME"
echo "$HOSTNAME">/etc/hostname
echo "$(grep -E '127|::1' /etc/hosts)">/etc/hosts
echo "$(ip a|grep "inet "|grep -v 127|awk -F'[ /]' '{print $6}') $HOSTNAME">>/etc/hosts

# 安装软件
yum -y install createrepo

# 上传需要共享的rpm包到共享目录
mkdir /YumRepository
cd /YumRepository
# scp USER@IP:/PATH /YumRepository

# 创建yum仓库的repodata目录和repomd.xml文件,repomd.xml是核心的配置文件
# 该文件指明了仓库中拥有的rpm包,当该文件不存在时,目录不能被识别成yum仓库
createrepo -pdo /YumRepository /YumRepository
# 当仓库中的rpm包有更新时,升级该文件
createrepo --update /YumRepository

# 使用nginx做局域网共享
yum -y install epel-release
yum -y install nginx
sed -i 's|/usr/share/nginx/html|/YumRepository|g' /etc/nginx/nginx.conf
systemctl enable nginx
systemctl restart nginx

# 测试
cd /tmp/
wget http://$(hostname -i)/kubectl-1.12.1-2.x86_64.rpm

# 客户端配置,在想要从该yum仓库中下载安装rpm包的机器上配置
echo '192.168.77.90 LocalRepository'>>/etc/hosts
cd /etc/yum.repos.d
cat >LocalRepository.repo<<EOF
[LocalRepository]
name=LocalRepository
baseurl=http://LocalRepository
enable=1
gpgcheck=0
EOF
yum clean all
yum makecache faster
# 安装yumdownloader命令
yum -y install yum-utils
cd /tmp/
yumdownloader kubectl
ls -l kubectl*.rpm

# 根据网络yum源持续更新rpm包
cat >/root/checkOS/yumRepoRefresh.sh<<EOF
cd /YumRepository
rm -rf index.html
wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/
for rpm in \$(cat index.html |awk -F'"' '{if(\$2~/.*.rpm/) print \$2}')
do
  wget -nc https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/\${rpm}
  # 使用 -nc 参数,忽略已经存在的包
done
# 更新仓库,否则新下载的rpm包不会被识别
createrepo --update /YumRepository
EOF
chmod +x /root/checkOS/yumRepoRefresh.sh
# 添加自动任务
crontab -l>/tmp/crontab.tmp
echo -e '\n# Yum Repo Daily Refresh '>>/tmp/crontab.tmp
echo '0 0 * * * /bin/bash /root/checkOS/yumRepoRefresh.sh'>>/tmp/crontab.tmp
cat /tmp/crontab.tmp |crontab
rm -rf /tmp/crontab.tmp

[TOC]

你可能感兴趣的:(Linux)