CentOS7 同步远程镜像 搭建本地yum服务器
同步CentOS镜像站点的数据到本地服务器,使用nginx实现http服务向局域网内的其他机器提供yum服务,解决内网yum安装软件的问题。
一、前提条件:
1、本机连接互联网,能正常访问CentOS镜像站点,本例使用中科大的源:mirrors.ustc.edu.cn。
2、CentOS镜像站点需要支持 rsync 协议。
二、搭建过程:
1、本机安装所需工具:
yum -y install rsync createrepo
2、创建目录(位置随意):
(1)、centos仓库目录,centosplus可以不同步,一般用不到:
mkdir -p /storage/repos/centos/7/{os,updates,extras,centosplus}/x86_64
mkdir -p /storage/repos/centos/6/{os,updates,extras,centosplus}/x86_64
(2)epel仓库目录:
mkdir -p /storage/repos/epel/7/x86_64
mkdir -p /storage/repos/epel/6/x86_64
#如果需要EPEL软件的源码,请同时创建以下目录
mkdir -p /storage/repos/epel/7/SRPMS/
mkdir -p /storage/repos/epel/6/SRPMS/
3、同步远程镜像(该过程需要很长时间,与你的外网带宽有关):
rsync -avz --delete --exclude='repodata' rsync://mirrors.ustc.edu.cn/centos/7/os/x86_64/ /storage/repos/centos/7/os/x86_64/
rsync -avz --delete --exclude='repodata' rsync://mirrors.ustc.edu.cn/centos/7/updates/x86_64/ /storage/repos/centos/7/updates/x86_64/
rsync -avz --delete --exclude='repodata' rsync://mirrors.ustc.edu.cn/centos/7/extras/x86_64/ /storage/repos/centos/7/extras/x86_64/
rsync -avz --delete --exclude='repodata' rsync://mirrors.ustc.edu.cn/centos/7/centosplus/x86_64/ /storage/repos/centos/7/centosplus/x86_64/
rsync -avz --delete --exclude='repodata' rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/ /storage/repos/centos/6/os/x86_64/
rsync -avz --delete --exclude='repodata' rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/ /storage/repos/centos/6/updates/x86_64/
rsync -avz --delete --exclude='repodata' rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/ /storage/repos/centos/6/extras/x86_64/
rsync -avz --delete --exclude='repodata' rsync://mirrors.ustc.edu.cn/centos/6/centosplus/x86_64/ /storage/repos/centos/6/centosplus/x86_64/
#同步gpgkey
rsync -avz --delete --exclude='repodata' rsync://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-7 /storage/repos/centos/
rsync -avz --delete --exclude='repodata' rsync://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6 /storage/repos/centos/
4、生成本地仓库元数据及索引
createrepo /storage/repos/centos/7/os/x86_64/
createrepo /storage/repos/centos/7/updates/x86_64/
createrepo /storage/repos/centos/7/extras/x86_64/
createrepo /storage/repos/centos/7/centosplus/x86_64/
createrepo /storage/repos/centos/6/os/x86_64/
createrepo /storage/repos/centos/6/updates/x86_64/
createrepo /storage/repos/centos/6/extras/x86_64/
createrepo /storage/repos/centos/6/centosplus/x86_64/
5、同步脚本,如果你的服务器一直连接外网可以配置在定时任务里,定期与远程镜像保持同步:
[root@yum ~]# cat /etc/cron.daily/update-repos_7.sh
#!/bin/bash
#export RSYNC_PROXY="10.60.34.191:3128"
export RSYNC_PROXY="10.61.98.19:3128"
VER='7'
ARCH='x86_64'
CENTOS_REPOS=(os updates extras centosplus)
#同步centos镜像
for REPO in ${CENTOS_REPOS[@]}
do
rsync -avz --delete --exclude='repodata' rsync://rsync.mirrors.ustc.edu.cn/centos/${VER}/${REPO}/${ARCH}/ /storage/repos/centos/${VER}/${REPO}/${ARCH}/
createrepo --update /storage/repos/centos/${VER}/${REPO}/${ARCH}/
done
#同步gpgkey
rsync -avz --delete --exclude='repodata' rsync://rsync.mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-${VER} /storage/repos/centos/
#同步epel镜像
rsync -avz --delete --exclude='repodata' rsync://rsync.mirrors.ustc.edu.cn/epel/${VER}/x86_64/ /storage/repos/epel/${VER}/x86_64/
createrepo --update /storage/repos/epel/${VER}/x86_64/
#如果需要epel软件的源码,同步epel软件源码仓库
#rsync -avz --delete --exclude='repodata' rsync://rsync.mirrors.ustc.edu.cn/epel/${VER}/SRPMS/ /storage/repos/epel/${VER}/SRPMS/
#createrepo /storage/repos/epel/${VER}/SRPMS/
#同步gpgkey
rsync -avz --delete --exclude='repodata' rsync://rsync.mirrors.ustc.edu.cn/epel/RPM-GPG-KEY-EPEL-${VER} /storage/repos/epel/
# chmod 755 /etc/cron.daily/update-repos_6.sh
[root@yum ~]# cat /etc/cron.daily/update-repos_6.sh
#!/bin/bash
#export RSYNC_PROXY="10.60.34.191:3128"
export RSYNC_PROXY="10.61.98.19:3128"
VER='6'
ARCH='x86_64'
CENTOS_REPOS=(os updates extras centosplus)
#同步centos镜像
for REPO in ${CENTOS_REPOS[@]}
do
rsync -avz --delete --exclude='repodata' rsync://rsync.mirrors.ustc.edu.cn/centos/${VER}/${REPO}/${ARCH}/ /storage/repos/centos/${VER}/${REPO}/${ARCH}/
createrepo --update /storage/repos/centos/${VER}/${REPO}/${ARCH}/
done
#同步gpgkey
rsync -avz --delete --exclude='repodata' rsync://rsync.mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-${VER} /storage/repos/centos/
#同步epel镜像
rsync -avz --delete --exclude='repodata' rsync://rsync.mirrors.ustc.edu.cn/epel/${VER}/x86_64/ /storage/repos/epel/${VER}/x86_64/
createrepo --update /storage/repos/epel/${VER}/x86_64/
#如果需要epel软件的源码,同步epel软件源码仓库
#rsync -avz --delete --exclude='repodata' rsync://rsync.mirrors.ustc.edu.cn/epel/${VER}/SRPMS/ /storage/repos/epel/${VER}/SRPMS/
#createrepo /storage/repos/epel/${VER}/SRPMS/
#同步gpgkey
rsync -avz --delete --exclude='repodata' rsync://rsync.mirrors.ustc.edu.cn/epel/RPM-GPG-KEY-EPEL-${VER} /storage/repos/epel/
# chmod 755 /etc/cron.daily/update-repos_6.sh
6、关闭selinux:
# 1、永久关闭
vi /etc/selinux/config
#将其中的 SELINUX=enforcing 配置项 修改为: SELINUX=disabled
# 2、临时关闭
setenforce 0
7、nginx的安装及配置(cenos官方源中没有包含nginx, 通过epel源安装nginx):
(1)、安装epel源:
yum install epel-release
(2)、安装nginx:
yum install -y nginx
(3)、启动nginx:
systemctl start nginx.service
(4)、开机自动启动nginx服务:
systemctl enable nginx.service
(5)、防火墙允许nginx服务:
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
[root@yum ~]# cat /opt/openresty/nginx/conf/nginx.conf|grep -v "#"|grep -v "^$"
user www;
worker_processes 4;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main ;
error_log logs/error.log info;
sendfile on;
keepalive_timeout 120s 120s;
keepalive_requests 10000;
server {
listen 80;
server_name ifconfig.kjtyun.com;
location /cmdb_update/ {
alias /opt/openresty/nginx/html/;
autoindex on;
}
location /yum/ {
alias /opt/openresty/nginx/html/;
autoindex on;
}
location / {
default_type text/html;
add_header Content-Type 'text/html; charset=utf-8';
return 200 "$remote_addr";
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name yum.kjtyun.com;
root /storage/repos/ ;
location = / {
autoindex on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
(8)、重启nginx服务或让nginx重新加载配置:
systemctl restart nginx.service
#或
systemctl reload nginx.service
现在应该可能通过 http://{ipaddress} 能查看到内容了,如果报403之类的错误,请查找nginx相关错误的解决办法。
三、yum客户端(机)配置:
1、修改 /etc/yum.repos.d/CentOS-Base.repo 文件中各仓库的baseurl 和 gpgkey 配置项,模板中的{ipaddress}替换为你的实际IP地址。
[hadoop@node1 yum.repos.d]$ cat CentOS-Base.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
baseurl=http://yum.kjtyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://yum.kjtyun.com/centos/RPM-GPG-KEY-CentOS-$releasever
#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
baseurl=http://yum.kjtyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://yum.kjtyun.com/centos/RPM-GPG-KEY-CentOS-$releasever
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
baseurl=http://yum.kjtyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://yum.kjtyun.com/centos/RPM-GPG-KEY-CentOS-$releasever
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
baseurl=http://yum.kjtyun.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://yum.kjtyun.com/centos/RPM-GPG-KEY-CentOS-$releasever
[hadoop@node1 yum.repos.d]$ cat epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
#metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
baseurl=http://yum.kjtyun.com/epel/$releasever/$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=http://yum.kjtyun.com/epel/RPM-GPG-KEY-EPEL-$releasever
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch/debug
#metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
baseurl=http://yum.kjtyun.com/epel/$releasever/$basearch/debug
failovermethod=priority
enabled=0
gpgcheck=1
gpgkey=http://yum.kjtyun.com/epel/RPM-GPG-KEY-EPEL-$releasever
#[epel-source]
#name=Extra Packages for Enterprise Linux 7 - $basearch - Source
##baseurl=http://download.fedoraproject.org/pub/epel/7/SRPMS
#metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
#failovermethod=priority
#enabled=0
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-$releasever
#gpgkey=http://yum.kjtyun.com/epel/RPM-GPG-KEY-EPEL-$releasever
#gpgcheck=1
3、清除yum缓存:
yum clean all
4、删除yum缓存目录:
rm -rf /var/cache/yum/*
5、创建yum缓存:
yum makecache
---------------------
作者:chenjia6605
来源:CSDN
原文:https://blog.csdn.net/chenjia6605/article/details/82734945
版权声明:本文为博主原创文章,转载请附上博文链接!
相关文章
- 11-24unity particle 粒子系统 制作闪电放电效果
- 11-24告别传统excel,教你高效制作各种报表
- 11-24制作Station主机的KylinOS启动卡
- 11-24制作特斯拉汽车自有监控优盘
- 11-24使用web报表工具制作占百分比报表
- 11-24The program 'yum' is currently not installed. You can install it by typing:
- 11-24Hadoop生态圈-Flume的主流Channel源配置
- 11-24Caffe 源碼閱讀(四) Layer.hpp Layer.cpp
- 11-24yum命令
- 11-24收藏分享:Easy_U USDOS 杏雨梨云 USM等多种启动盘制作工具下载!