CentOS内网yum源构建&使用最佳实践

一、yum源构建(base源、updates源、epel源)

1. 找一台能连互联网的centos服务器,虚拟机也行。

2. 备份已有yum repo文件

cd /etc/yum.repo.d
mkdir bak
mv *.repo bak

3. 导入华为云yum repo

#导入华为yum源配置
cat > /etc/yum.repos.d/CentOS-Base.repo <<EOF
# 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 - repo.huaweicloud.com
baseurl=https://repo.huaweicloud.com/centos/\$releasever/os/\$basearch/
#mirrorlist=https://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=os
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7
 
#released updates 
[updates]
name=CentOS-\$releasever - Updates - repo.huaweicloud.com
baseurl=https://repo.huaweicloud.com/centos/\$releasever/updates/\$basearch/
#mirrorlist=https://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=updates
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7
 
#additional packages that may be useful
[extras]
name=CentOS-\$releasever - Extras - repo.huaweicloud.com
baseurl=https://repo.huaweicloud.com/centos/\$releasever/extras/\$basearch/
#mirrorlist=https://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=extras
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-\$releasever - Plus - repo.huaweicloud.com
baseurl=https://repo.huaweicloud.com/centos/\$releasever/centosplus/\$basearch/
#mirrorlist=https://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=https://repo.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7

EOF

cat > /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 7 - \$basearch
baseurl=https://repo.huaweicloud.com/epel/7/\$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
 
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - \$basearch - Debug
baseurl=https://repo.huaweicloud.com/epel/7/\$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
 
[epel-source]
name=Extra Packages for Enterprise Linux 7 - \$basearch - Source
baseurl=https://repo.huaweicloud.com/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0

EOF

#清理yum缓存
yum clean all
#重新构建yum缓存
yum makecache

4. 安装yum源同步工具

yum -y install yum-utils createrepo

5. 同步yum源到本地

#yum源同步 ${downloadPath} 下载路径
reposync -r base -r updates -r epel -d -n -m -g -p ${downloadPath}

6. 创建repodata索引

createrepo -g  ${downloadPath}/epel/comps.xml ${downloadPath}/epel/
createrepo -g  ${downloadPath}/base/comps.xml ${downloadPath}/base/
createrepo ${downloadPath}/updates/

7. 打包离线源

tar -zcvf base.tar.gz ${downloadPath}/base/
tar -zcvf updates.tar.gz ${downloadPath}/updates/
tar -zcvf epel.tar.gz ${downloadPath}/epel/

二、yum源使用

1. 上传tar包到内网服务器

2. 解压文件

#创建解压路径
mkdir -p /var/www/html/centos7/
#解压 ${uploadPath}上传路径
tar -zxvf ${uploadPath}/base.tar.gz -C /var/www/html/centos7/
tar -zxvf ${uploadPath}/updates.tar.gz -C /var/www/html/centos7/
tar -zxvf ${uploadPath}/epel.tar.gz -C /var/www/html/centos7/

3. 备份已有repo

cd /etc/yum.repo.d
mkdir bak
mv *.repo bak

4. 配置本地源

#配置本地源
cat > /etc/yum.repo.d/local.repo <<EOF
[base]
name=CentOS-7-base
baseurl=file:///var/www/html/centos7/base
gpgcheck=0
enabled=1
priority=1

EOF

#本地源生效
yum clean all
yum makecache

5. 安装httpd服务

#从本地yum源安装
yum -y httpd
​#启动httpd服务
systemctl start httpd
#开机自启httpd服务
systemctl enable httpd

6. 配置内网源

#删除本地源配置
rm -rf /etc/yum.repos.d/local.repo

#配置内网源
#${localIP} 本地ip地址
cat > /etc/yum.repos.d/intranet_os.repo <<EOF
[base]
name=CentOS-7-base
baseurl=http://${localIP}/centos7/base/
gpgcheck=0
enabled=1
priority=1

[updates]
name=CentOS-7-updates
baseurl=http://${localIP}/centos7/updates/
gpgcheck=0
enabled=1
priority=1

[epel]
name=CentOS-7-epel
baseurl=http://${localIP}/centos7/epel/
gpgcheck=0
enabled=1
priority=1

EOF

#内网源生效
yum clean all
yum makecache

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用Apache或Nginx来在CentOS构建一个虚拟主机来提供yum源。下面是一些基本步骤: 1. 安装和置Web服务器:首先,确保您已经安装了Apache或Nginx。您可以使用以下命令之一安装所需的软件包: - 对于Apache:`sudo yum install httpd` - 对于Nginx:`sudo yum install nginx` 2. 创建虚拟主机置文件:在Web服务器置目录中创建一个新的虚拟主机置文件。对于Apache,可以使用以下命令创建一个名为`yum.conf`的文件: `sudo vi /etc/httpd/conf.d/yum.conf` 对于Nginx,可以使用以下命令创建一个名为`yum.conf`的文件: `sudo vi /etc/nginx/conf.d/yum.conf` 3. 编辑虚拟主机置文件:在虚拟主机置文件中,您需要指定您要提供的yum源的位置。以下是一个示例置文件的内容: ``` server { listen 80; server_name yum.example.com; location / { root /path/to/your/yum/repo; autoindex on; index index.html; } } ``` 确保将`yum.example.com`替换为您自己的域名,并将`/path/to/your/yum/repo`替换为实际的yum源路径。 4. 保存并退出置文件后,重新启动Web服务器以使更改生效。对于Apache,使用以下命令重新启动: `sudo systemctl restart httpd` 对于Nginx,使用以下命令重新启动: `sudo systemctl restart nginx` 5. 置DNS解析:为了使yum源可用,您需要在DNS服务器或本地hosts文件中添加一个记录,将您指定的域名解析为虚拟主机所在的IP地址。 6. 测试yum源:现在,您可以尝试使用置的yum源。在其他CentOS系统上,编辑`/etc/yum.repos.d/`目录中的`.repo`文件,并将`baseurl`设置为您新创建的虚拟主机的URL。 ``` [custom-yum] name=Custom Yum Repository baseurl=http://yum.example.com/ enabled=1 gpgcheck=0 ``` 然后,运行`sudo yum update`或其他yum命令来测试您的yum源是否正常工作。 希望这些步骤对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值