云网牛站
所在位置:首页 > Linux教程 > 创建本地CentOS 6/CentOS 7镜像和Epel镜像的方法

创建本地CentOS 6/CentOS 7镜像和Epel镜像的方法

2019-03-04 10:34:45作者:连一续稿源:云网牛站

本文介绍如何创建本地CentOS 6/CentOS 7镜像的技巧。当你拥有大量系统并且希望节省带宽,紧固软件包安装,网络安装和系统更新时,这将非常有用。本文将使用rsync同步来自上游的镜像,用于保存同步内容的路径是/var/mirrors/,确保调整具有足够空间的文件系统的路径,或者安装新的磁盘/分区。先参考使用rsync命令同步本地目录和远程主机目录

 

Bash脚本同步CentOS 6/7镜像

下面的脚本将同步CentOS 6/CentOS 7镜像,用你想要的替换目录变量,将脚本保存到文件中,make:

#!/bin/bash

if [ -f /var/lock/subsys/rsync_updates ]; then

echo "Updates via rsync already running."

exit 0

fi

base_dir="/var/mirrors/centos/"

centos_7_mirror_dir="/var/mirrors/centos/7/"

centos_6_mirror_dir="/var/mirrors/centos/6/"

touch /var/lock/subsys/rsync_updates

if [[ -d "$centos_7_mirror_dir"  && -d "$centos_6_mirror_dir" ]] ; then

    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/7/  "$centos_7_mirror_dir" && \

    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/6/  "$centos_6_mirror_dir" && \

    # sync keys

    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/RPM-GPG-KEY-CentOS-7  "$base_dir" && \

    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/RPM-GPG-KEY-CentOS-6  "$base_dir" && \

    rm -rf /var/lock/subsys/rsync_updates

else

echo "Directories doesn't exist"

fi

 

用于同步Epel镜像的Bash脚本

#!/bin/bash

if [ -f /var/lock/subsys/rsync_updates ]; then

echo "Updates via rsync already running."

exit 0

fi

epel6_mirror_dir="/var/mirrors/epel/6/x86_64"

epel7_mirror_dir="/var/mirrors/epel/7/x86_64"

base_dir="/var/mirrors/epel/"

touch /var/lock/subsys/rsync_updates

if [[ -d "$epel6_mirror_dir"  && -d "$epel7_mirror_dir" ]] ; then

    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/6/x86_64/ "$epel6_mirror_dir" && \

    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/7/x86_64/ "$epel7_mirror_dir" && \

    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/RPM-GPG-KEY-EPEL-7 "$base_dir" && \

    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/RPM-GPG-KEY-EPEL-6 "$base_dir" && \

    rm -rf /var/lock/subsys/rsync_updates

else

echo "Directories doesn't exist"

fi

if [[ $? -eq '0' ]]; then

echo ""

echo "Sync successful.."

else

echo " Syncing failed"

exit 1

fi

这些脚本可以在cron中运行,以保持镜像最新。

 

用Nginx服务镜像

同步完成后,你可以通过使用nginx开始使用它们。

安装nginx:

yum -y install epel-release

yum -y install nginx

然后添加配置文件:

cat /etc/nginx/conf.d/centos.conf

server {

listen 80;

server_name centos.mirrors.domain.com;

root /var/mirrors/centos/;

location / {

autoindex on;

}

}

对于epel:

cat /etc/nginx/conf.d/epel.conf

server {

listen 80;

server_name epel.mirrors.domain.com;

root /var/mirrors/epel;

location / {

autoindex on;

}

}

启动并启用nginx:

systemctl start nginx

systemctl enable nginx

 

配置CentOS Server以使用镜像

CentOS Base镜像:

cd /etc/yum.repos.d/

mkdir old

mv CentOS* old

vim centos.repo

添加以下内容:

[base]

name=CentOS-$releasever - Base

baseurl=http://centos.mirrors.domain.com/$releasever/os/$basearch/

enabled=1

gpgcheck=1

gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[updates]

name=CentOS-$releasever - Updates

baseurl=http://centos.mirrors.domain.com/$releasever/updates/$basearch/

enabled=1

gpgcheck=1

gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[extras]

name=CentOS-$releasever - Extras

baseurl=http://centos.mirrors.domain.com/$releasever/extras/$basearch/

enabled=0

gpgcheck=1

gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[centosplus]

name= CentOS-$releasever - Plus

baseurl=http://centos.mirrors.domain.com/$releasever/centosplus/$basearch/

enabled=0

gpgcheck=1

gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

通过更新repo缓存来测试repos是否正常工作:

yum clean all

yum makecache fast

yum -v repolist

Epel Mirrors:

# cat /etc/yum.repos.d/epel.repo

[epel]

name=Extra Packages for Enterprise Linux 7 - $basearch

baseurl=http://epel.mirrors.domain.com/$releasever/$basearch/

enabled=1

gpgcheck=1

gpgkey=http://epel.mirrors.domain.com/RPM-GPG-KEY-EPEL-$releasever

 

相关主题

在CentOS 7系统中采用清华源IPv6来安装epel与ius源

精选文章
热门文章