使用 apt-mirror 搭建自有 Ubuntu 源
时间:2015-05-18 00:58 来源:linux.it.net.cn 作者:IT
搭建自有Ubuntu源的原因有很多
-
节省外网带宽
-
提前下载,缓解国外源下载缓慢
-
私有定制软件包发布
本文测试环境
-
ubuntu 14.04(LTS) 64位
-
apt-mirror 版本 0.5.1-1
-
nginx 版本 1.4.6-1ubuntu3.1
安装apt-mirror
# sudo apt-get update
# sudo apt-get install apt-mirror=0.5.1-1
配置apt-mirror
apt-mirror的配置文件位置为 /etc/apt/mirror.list
根据注释修改相应内容,一般只需要修改 base_path和更改、添加软件源。以下配置文件我只添加了Ubuntu 14.04和MongoDB的软件源,你可以根据你的需要添加Ubuntu其他版本软件源。
############# config ##################
#
# 配置数据基目录
set base_path /data/apt-mirror
# 配置镜像存储位置
# set mirror_path $base_path/mirror
# 配置临时下载索引位置
# set skel_path $base_path/skel
# 配置日子,URLs和MD5校验信息存储位置
# set var_path $base_path/var
# 配置删除过期源脚本位置(默认不删除,方便安装旧版本软件)
# set cleanscript $var_path/clean.sh
# 设置默认架构, 可填: amd64 或 i386,默认是和本机一个架构
# set defaultarch <running host architecture>
#
# 设定下载后运行的脚本位置
# set postmirror_script $var_path/postmirror.sh
# 设置是否执行 下载后的脚本操作,默认是1(但是默认没有postmirror.sh脚本)
set run_postmirror 0
# 设置下载线程数
set nthreads 20
# 是否替换URL中的波浪线,替换成%7E(HTML代码),否则会跳过不进行下载
set _tilde 0
#
############# end config ##############
# 配置Ubuntu trusty 源
deb http://archive.ubuntu.com/ubuntu trusty main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse
# clean http://archive.ubuntu.com/ubuntu
# 配置MongoDB源
# 官方地址 http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
#
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
# clean http://localhost/downloads-distro.mongodb.org
因为我自定义了apt-mirror的数据目录,所以需要赋予 apt-mirror用户权限,否则同步不会正确运行:
# sudo chown -R apt-mirror:apt-mirror /data/apt-mirror
运行第一次同步
确保你的基目录下磁盘空间足够,如上所配置,则至少保证150G的磁盘空间,建议200G以上,以后版本更新,添加软件源等会需要很多磁盘空间。
因为要下载135G的内容,时间比较漫长,建议切换到apt-mirror用户用以下命令放在后台运行
# sudo su apt-mirror
# nohup /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron.log &
安装Nginx,配置HTTP访问
通过以上配置,已经可以在 /data/apt-mirror 目录下看到生成的几个数据文件。/data/apt-mirror/mirror下存放的就是软件镜像。但是如何向其他计算机发布这些数据呢?很明显通过 HTTP服务可以做到这一点。我们通过使用Nginx将mirror目录下的内容通过HTTP协议发布。
安装Nginx
# sudo apt-get install nginx=1.4.6-1ubuntu3.1
nginx配置文件位置: /etc/nginx/sites-enabled/default,将其替换为以下文件:
server {
listen 80;
# 这里填写你的域名,填写localhost就直接通过IP地址访问
server_name localhost;
# 显示目录
autoindex on;
location / {
index index.html index.htm;
# 这里填写镜像保存位置
root /data/apt-mirror/mirror;
}
access_log /var/log/nginx/localhost.log;
}
更改完配置,先测试配置,如果测试通过,则重启nginx
# sudo nginx -t
# sudo service nginx restart
此时访问 http://服务器IP 即可访问到你发布的镜像
配置定时同步
我们当然不能,每天自己手动同步镜像啦。我们需要配置apt-mirror每天定时同步,其实就是配置cron,cron具体讲解请参看:crontab的用法
这里apt-mirror提供了cron模板文件,在/etc/cron.d/apt-mirror 中,取消最后一行的#注释即可生效:
0 4 * * * apt-mirror /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron.log
以上设置每日4点同步一次
客户端配置
现在我们已经配置好了服务端,找个Ubuntu 14.04 64位的来测试一下
备份source.list
# sudo mv /etc/apt/source.list /etc/apt/source.list.bak
新建/etc/apt/source.list, 写入以下内容(上述服务器DNS或IP 替换”自建源IP”)
deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty main restricted universe multiverse
deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse
deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty main restricted universe multiverse
deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse
# 配置MongoDB源
deb [arch=amd64] http://自建源IP/downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
通过比较apt-mirror和source.list文件,其实就是把原有域名变成了一个目录。注意:以上客户端配置也多添加了[arch=amd64],用于指定架构,否则会出现
Err http://192.168.1.71 trusty/main i386 Packages
404 Not Found
Err http://192.168.1.71 trusty/restricted i386 Packages
404 Not Found
Err http://192.168.1.71 trusty/universe i386 Packages
404 Not Found
...
类似错误,因为我的测试环境是x64,只同步了64位架构源,所以必须指定架构为64,现在客户端也无需i386的软件。
添加过source.list后,运行
# sudo apt-get update
ubuntu部分正常更新,但是发现会出现以下错误提示
W: GPG error: http://192.168.1.111 dist Release:
The following signatures couldn't be verified
because the public key is not available: NO_PUBKEY 9ECBEC467F0CEB10
需要添加GPG key,添加某些package时,基本都需要添加GPG key,官方网站一般都会给出
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
此时你就可以使用自建源安装软件啦~~
以下安装了MongoDB和htop来进行测试
sudo apt-get install -y mongodb-org=2.6.5 htop
Ansible自动化部署
如果你熟悉ansible,我写了apt-mirror 的roles 放在了Github上,可以进行一键批量部署。
地址: https://github.com/William-Sang/ansible-roles
-
在 apt-mirror分组下添加主机地址
-
自定义 group_vars/all 中的远程主机用户名和key
-
自定义 group_vars/apt-mirror 中的变量
-
一键批量部署:
# ansible-play -i hosts --limit apt-mirror site.yml
参考链接
-
apt-mirror Github 配置文件通过查阅源代码了解
-
apt-mirror官网
-
_tidle 选项设置原因
*** 虽然查阅了很多资料,做了大量测试。但是难免有所遗漏和错误,欢迎留言指正~
(责任编辑:IT)
搭建自有Ubuntu源的原因有很多
本文测试环境
安装apt-mirror # sudo apt-get update # sudo apt-get install apt-mirror=0.5.1-1 配置apt-mirror
apt-mirror的配置文件位置为 /etc/apt/mirror.list ############# config ################## # # 配置数据基目录 set base_path /data/apt-mirror # 配置镜像存储位置 # set mirror_path $base_path/mirror # 配置临时下载索引位置 # set skel_path $base_path/skel # 配置日子,URLs和MD5校验信息存储位置 # set var_path $base_path/var # 配置删除过期源脚本位置(默认不删除,方便安装旧版本软件) # set cleanscript $var_path/clean.sh # 设置默认架构, 可填: amd64 或 i386,默认是和本机一个架构 # set defaultarch <running host architecture> # # 设定下载后运行的脚本位置 # set postmirror_script $var_path/postmirror.sh # 设置是否执行 下载后的脚本操作,默认是1(但是默认没有postmirror.sh脚本) set run_postmirror 0 # 设置下载线程数 set nthreads 20 # 是否替换URL中的波浪线,替换成%7E(HTML代码),否则会跳过不进行下载 set _tilde 0 # ############# end config ############## # 配置Ubuntu trusty 源 deb http://archive.ubuntu.com/ubuntu trusty main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu trusty main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse # clean http://archive.ubuntu.com/ubuntu # 配置MongoDB源 # 官方地址 http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ # deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen # clean http://localhost/downloads-distro.mongodb.org 因为我自定义了apt-mirror的数据目录,所以需要赋予 apt-mirror用户权限,否则同步不会正确运行: # sudo chown -R apt-mirror:apt-mirror /data/apt-mirror 运行第一次同步
确保你的基目录下磁盘空间足够,如上所配置,则至少保证150G的磁盘空间,建议200G以上,以后版本更新,添加软件源等会需要很多磁盘空间。 # sudo su apt-mirror # nohup /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron.log & 安装Nginx,配置HTTP访问 通过以上配置,已经可以在 /data/apt-mirror 目录下看到生成的几个数据文件。/data/apt-mirror/mirror下存放的就是软件镜像。但是如何向其他计算机发布这些数据呢?很明显通过 HTTP服务可以做到这一点。我们通过使用Nginx将mirror目录下的内容通过HTTP协议发布。 安装Nginx # sudo apt-get install nginx=1.4.6-1ubuntu3.1 nginx配置文件位置: /etc/nginx/sites-enabled/default,将其替换为以下文件: server { listen 80; # 这里填写你的域名,填写localhost就直接通过IP地址访问 server_name localhost; # 显示目录 autoindex on; location / { index index.html index.htm; # 这里填写镜像保存位置 root /data/apt-mirror/mirror; } access_log /var/log/nginx/localhost.log; } 更改完配置,先测试配置,如果测试通过,则重启nginx # sudo nginx -t # sudo service nginx restart 此时访问 http://服务器IP 即可访问到你发布的镜像 配置定时同步 我们当然不能,每天自己手动同步镜像啦。我们需要配置apt-mirror每天定时同步,其实就是配置cron,cron具体讲解请参看:crontab的用法 这里apt-mirror提供了cron模板文件,在/etc/cron.d/apt-mirror 中,取消最后一行的#注释即可生效: 0 4 * * * apt-mirror /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron.log 以上设置每日4点同步一次 客户端配置 现在我们已经配置好了服务端,找个Ubuntu 14.04 64位的来测试一下 备份source.list # sudo mv /etc/apt/source.list /etc/apt/source.list.bak 新建/etc/apt/source.list, 写入以下内容(上述服务器DNS或IP 替换”自建源IP”) deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty main restricted universe multiverse deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty main restricted universe multiverse deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse # 配置MongoDB源 deb [arch=amd64] http://自建源IP/downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen 通过比较apt-mirror和source.list文件,其实就是把原有域名变成了一个目录。注意:以上客户端配置也多添加了[arch=amd64],用于指定架构,否则会出现 Err http://192.168.1.71 trusty/main i386 Packages 404 Not Found Err http://192.168.1.71 trusty/restricted i386 Packages 404 Not Found Err http://192.168.1.71 trusty/universe i386 Packages 404 Not Found ...
类似错误,因为我的测试环境是x64,只同步了64位架构源,所以必须指定架构为64,现在客户端也无需i386的软件。 # sudo apt-get update ubuntu部分正常更新,但是发现会出现以下错误提示 W: GPG error: http://192.168.1.111 dist Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 9ECBEC467F0CEB10 需要添加GPG key,添加某些package时,基本都需要添加GPG key,官方网站一般都会给出 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
此时你就可以使用自建源安装软件啦~~ sudo apt-get install -y mongodb-org=2.6.5 htop
Ansible自动化部署
# ansible-play -i hosts --limit apt-mirror site.yml 参考链接
*** 虽然查阅了很多资料,做了大量测试。但是难免有所遗漏和错误,欢迎留言指正~ (责任编辑:IT) |