搭建 ubuntu 源?ubuntu开发环境搭建
大家好,今天小编来为大家解答搭建 ubuntu 源这个问题,ubuntu开发环境搭建很多人还不知道,现在让我们一起来看看吧!
如何制作完整的Ubuntu本地源镜像
本地部分源镜像的优点是占用空间比较小,一个部分软件包源根据系统需要所占的空间大概也就在1G~2G之间。能满足系统备份还原的基本需求,凡是备份下来的软件包都能安装。但是它的缺点和局限性也是很明显的,它只能使用系统安装过的的软件包,也就是/var/cache/apt/archives/下的所有软件包。如果你的系统是32位的,那么这个源也就是32位的源,而不能被64位的系统所使用。或者如果要安装没有在本机上安装过的软件,那么就不行。因此制作完整的本地软镜像就是个不错的选择。
制作部分软件包源镜像可以作为个人备份系统,还原之用;如果是公司企业的话,推荐还是使用完整的本地源镜像。
下面介绍下制作完整的本地源镜像的方法:
安装前一定要注意:
首先,镜像目录为/var/spool/下的 apt-mirror。
其次,要尽量用官方源或者Ubuntu系统源列表中列出来的镜像站进行镜像。
/var下要有40G以上的空间,建议在40G的基础上再保留30%以上的余量。
/var下如果没有空间,可以把镜像目录改到其他分区中,可以参考如下方法:
[plain] view plain copy
sudo ln-s/media/g/Ubuntu_local/apt-mirror//var/spool/apt-mirror
1.安装apt-mirror工具[plain] view plain copy
sudo apt-get install apt-mirror sudo apt-get install apache2#如果不打算对局域网或互联网开放,这个不必装
2.配置apt-mirror工具
gksugedit/etc/apt/mirrors.list
清除原有内容,没关系,输入以下内容:
[plain] view plain copy
#############config################## # # set base_path /var/spool/apt-mirror # # setmirror_path$base_path/mirror # setskel_path $base_path/skel # setvar_path$base_path/var # setcleanscript$var_path/clean.sh # setdefaultarchrunning hostarchitecture # setpostmirror_script$var_path/postmirror.sh # setrun_postmirror 0 #默认为20线程下载,推荐10线程,20可能会造成服备器或网络loading太重 setnthreads 20 set _tilde 0 # #############end config############## #网易源(速度很快) deb precise main universe restricted multiverse deb precise-security universe main multiverserestricted deb precise-updates universe main multiverserestricted deb precise-proposed universe main multiverse restricted deb precise-backports universe main multiverserestricted #ubuntu12.04 src源不进行同步,因为大多数人用不上,如果同步,要多增加一倍空间 #deb-src precise main universe restricted multiverse #deb-src precise-security universe main multiverserestricted #deb-src precise-proposed universe main multiverserestricted #deb-src precise-backports universe main multiverse restricted #deb-src precise-updates universe main multiverserestricted #下面的代码是为了镜像成功后节省空间的,实际上下面的这一句不写也是可以的 clean
这里的地址规则和sources.list中是一样的,用的是163网速不同,时间不同,完成后有提示,
3.开始制作镜像sudo apt-mirror
制作完成后会有提示,制作过程可以中断,如果时间太长,可以关闭终端或ctrl+c,如果电脑关机,什么时候要继续,再重新执行代码即可,这时原来下载过的是不会重新下载的。直到完成,完成后有如何节约空间的提示,可以不去理会;镜像内容很全的,与官方同步的.
之后再次运行apt-mirror会继续以前的工作.每次都可以这样更新.
此过程根据网速不同,会有不同的时间.网速快的话,1个晚上就能完成.
4.作为本地源使用更新
这一部分和部分软件包源镜像源的使用一样,可参考
制作完成后,在本地存储的地址为:
/var/spool/apt-mirror/mirror/mirrors.163.com/Ubuntu
编辑sources.list
sudo gedit/ets/apt/sources.list
删除原有内容,写入:
deb precise mainuniverse restricted multiverse
deb precise-securityuniverse main multiverse restricted
deb precise-updatesuniverse main multiverse restricted
deb precise-proposeduniverse main multiverse restricted
deb precise-backportsuniverse main multiverse restricted
5.本地安装软件包sudo apt-get update
sudo apt-get install xxxx
6.制作成局域网源
安装apaches
/var/spool/apt-mirror/mirror/mirrors.163.com/ubuntu/作为webroot
地址为局域网ip地址即可.
说明:完整的本地源做好后,通过新立得或apt-get,可以完全免除ubuntu官方源或ubuntu-cn源的负担,你想装什么,就装什么吧
怎么搭建本地Ubuntu镜像服务器
一.需求分析
最近公司软件Team有个需求是这样的:能不能在局域网搭建一个Ubuntu镜像服务器,
这样作的好处是可以节省Ubuntu某些常用工具的安装时间。
二.部署过程
2.1测试环境
目前在公司内有一台能连接到外网的Ubuntu机器, IP: 192.168.8.173,
Ubuntu版本为Ubuntu 9.04 i686。
2.2搭建步骤
为了创建Ubuntu mirror服务器,最少需要60G硬盘空间,Ubuntu每个版本都有32 bit
和64 bit两个版本,有两组deb包。当然可以创建Ubuntu一个版本的32 bit和64 bit的
镜像服务器,也可以创建全部版本的镜像。
这里使用sohu公司的mirror地址作为本地Ubuntu Source服务器,使用sohu的原因是:
1. sohu的Ubuntu Source服务器速度相当快
2. sohu的ubuntu Source服务器每天会和Ubuntu官方服务器同步一次,这样可以保
证我们使用的是最新的软件包。
修改方式如下:
root@ms~: cat/etc/apt/sources.list
deb jaunty main restricted universe multiverse
deb jaunty-security main restricted universe multiverse
deb jaunty-updates main restricted universe multiverse
deb jaunty-proposed main restricted universe multiverse
deb jaunty-backports main restricted universe multiverse
deb-src jaunty main restricted universe multiverse
deb-src jaunty-security main restricted universe multiverse
deb-src jaunty-updates main restricted universe multiverse
deb-src jaunty-proposed main restricted universe multiverse
deb-src jaunty-backports main restricted universe multiverse
安装下面工具:
root@ms~: apt-get install apt-mirror
root@ms~: apt-get install apache2
下面就构建Ubuntu 9.04 32bit的本地Ubuntu mirror服务器为例子,来说明如何配置:
修改配置文件:
root@ms~: cat/etc/apt/mirror.list
############# config##################
#
# set base_path/var/spool/apt-mirror
#
# set mirror_path$base_path/mirror
# set skel_path$base_path/skel
# set var_path$base_path/var
# set cleanscript$var_path/clean.sh
# set defaultarch
# set postmirror_script$var_path/postmirror.sh
set run_postmirror 0
set nthreads 20
set _tilde 0
#
############# end config##############
deb-i386 jaunty main restricted universe multiverse
deb-i386 jaunty-security main restricted universe multiverse
deb-i386 jaunty-updates main restricted universe multiverse
#deb jaunty-proposed main restricted universe multiverse
#deb jaunty-backports main restricted universe multiverse
deb-src jaunty main restricted universe multiverse
deb-src jaunty-security main restricted universe multiverse
deb-src jaunty-updates main restricted universe multiverse
#deb-src jaunty-proposed main restricted universe multiverse
#deb-src jaunty-backports main restricted universe multiverse
#clean
备注:
deb-i386 http://表示下载32 bit版本的deb软件
deb http://表示下载64bit版本的deb软件
jaunty表示Ubuntu 9.04的代号
设置完成后,输入下面命令:
root@ms~: apt-mirror
这时系统会启动20个线程运行wget到 下面下载相应的
deb包。时间比较的长,该命令执行完成后,/var/spool/apt-mirror目录下就有了所有需
要的deb包和相应的配置文件。
由于我是从sohu下载的,所以实际的文件在:
/var/spool/apt-mirror/mirror/mirrors.sohu.com/ubuntu目录下。
这样,本地就有了所有Ubuntu Source服务器所需要的文件,然后开启相应的服务:
这里使用apache2作为Web服务器,默认根目录在/var/www/,所以可以在该目录下做
个链接,如下所示:
root@ms~: cd/var/www
root@ms~: ln/var/spool/apt-mirror/mirror/mirrors.sohu.com/ubuntu-s
开启web服务:
root@ms~: apache2ctl start
打开浏览器输入下面地址即可看到和sohu mirror一样的页面:
同时也可以使用crontab进行每天的更新,修改配置文件:
root@ms~: cat/etc/cron.d/apt-mirror
#
# Regular cron jobs for the apt-mirror package
#
#0 4*** apt-mirror/usr/bin/apt-mirror>/var/spool/apt-mirror/var/cron.log
把命令前面的“#”去掉即可,这样系统会自动使用apt-mirror去和sohu服务器更新。
最后,我们所要做得就是在客户端的机器上修改更新源配置文件,将地址改成本地
mirror服务器的地址即可,如下所示:
root@ms~: cat/etc/apt/sources.list
deb jaunty main restricted universe multiverse
deb jaunty-security main restricted universe multiverse
deb jaunty-updates main restricted universe multiverse
deb jaunty-proposed main restricted universe multiverse
deb jaunty-backports main restricted universe multiverse
deb-src jaunty main restricted universe multiverse
deb-src jaunty-security main restricted universe multiverse
deb-src jaunty-updates main restricted universe multiverse
deb-src jaunty-proposed main restricted universe multiverse
deb-src jaunty-backports main restricted universe multiverse
执行“apt-get update”来重建本地索引,这样以后就会使用本地的Mirror服务器进行更新
使用apt-mirror搭建Ubuntu软件源的实例教程
搭建自有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 trusty main restricted universe multiverse
deb trusty-security main restricted universe multiverse
deb trusty-updates main restricted universe multiverse
deb trusty-proposed main restricted universe multiverse
deb trusty-backports main restricted universe multiverse
deb-src trusty main restricted universe multiverse
deb-src trusty-security main restricted universe multiverse
deb-src trusty-updates main restricted universe multiverse
deb-src trusty-proposed main restricted universe multiverse
deb-src trusty-backports main restricted universe multiverse
# clean
#配置MongoDB源
#官方地址
#
deb dist 10gen
# clean
因为我自定义了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。
这里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 trusty/main i386 Packages
404 Not Found
Err trusty/restricted i386 Packages
404 Not Found
Err trusty/universe i386 Packages
404 Not Found
...
类似错误,因为我的测试环境是x64,只同步了64位架构源,所以必须指定架构为64,现在客户端也无需i386的软件。
添加过source.list后,运行
复制代码
代码如下:
# sudo apt-get update
ubuntu部分正常更新,但是发现会出现以下错误提示
W: GPG error: 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