最近因为要开好几台虚拟机,要是联网更新的话会很慢,而且很麻烦,因此就想着在本地搭建一个更新源,这样能快一点,下面就是我搭建的过程。

apt-get实际使用wget命令,从/etc/apt/sources.list配置文件指定的源上下载对应的.deb软件包,然后使用dpkg -i packageName.deb安装软件。如果Ubuntu电脑所在的环境网络性能较差,或者不能连接互联网,那么就很难安装Ubuntu Source服务器上的海量软件包。

需求

  为了创建Ubuntu mirror服务器,你需要至少60G硬盘剩余空间。Ubuntu每一个版本分为32bit和64bit两个版本,有两组deb包。最新的ubuntu版本两组deb包需要大约90GB的空间。

  你可以创建Ubuntu一个版本的32或者64bit服务器,也可以创建同时服务于32和64bit的服务器。也可以创建服务于所有ubuntu版本的source服务器。根据你的硬盘剩余空间和你的需要而定。

使用中国科技大学的Ubuntu Source服务器作为源服务器

中国科学技术大学更新服务器(位于合肥,千兆教育网接入,百兆电信/联通线路智能路由),由中科大 Linux 用户协会和中科大学网络信息中心维护,包含其他开源镜像,Deepin 官方服务器 实际亦指向此处:

使用apt-mirror创建Ubuntu Source服务器的本地镜像服务器


1
2
3
sudo apt-get install apt-mirror
sudo apt-get install apache2

我们需要上面这两个软件来构建我们的本地Ubuntu Source服务器。

  我现在以构建ubuntu12.04的32和64位版本的本地ubunt source服务器为例进行介绍。

如果你需要服务于其他版本,请修改对应的配置。

  /etc/apt/mirror.list配置文件修改为如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
############# 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  <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads     20
set _tilde 0
#
############# end config ##############
deb http: //debian .ustc.edu.cn /ubuntu/ precise main multiverse restricted universe
deb http: //debian .ustc.edu.cn /ubuntu/ precise-backports main multiverse restricted universe
deb http: //debian .ustc.edu.cn /ubuntu/ precise-proposed main multiverse restricted universe
deb http: //debian .ustc.edu.cn /ubuntu/ precise-security main multiverse restricted universe
deb http: //debian .ustc.edu.cn /ubuntu/ precise-updates main multiverse restricted universe
deb-src http: //debian .ustc.edu.cn /ubuntu/ precise main multiverse restricted universe
deb-src http: //debian .ustc.edu.cn /ubuntu/ precise-backports main multiverse restricted universe
deb-src http: //debian .ustc.edu.cn /ubuntu/ precise-proposed main multiverse restricted universe
deb-src http: //debian .ustc.edu.cn /ubuntu/ precise-security main multiverse restricted universe
deb-src http: //debian .ustc.edu.cn /ubuntu/ precise-updates main multiverse restricted universe
deb-i386 http: //debian .ustc.edu.cn /ubuntu/ precise main multiverse restricted universe
deb-i386 http: //debian .ustc.edu.cn /ubuntu/ precise-backports main multiverse restricted universe
deb-i386 http: //debian .ustc.edu.cn /ubuntu/ precise-proposed main multiverse restricted universe
deb-i386 http: //debian .ustc.edu.cn /ubuntu/ precise-security main multiverse restricted universe
deb-i386 http: //debian .ustc.edu.cn /ubuntu/ precise-updates main multiverse restricted universe

deb http://* 表示下载64位版本的deb软件。

  deb-i386 http://* 表示下载32位版本的deb软件。

  然后执行: sudo apt-mirror

  这会启动20个线程【上面文件中配置】运行wget到 http://mirrors.sohu.com/Ubuntu 下面下载相应的deb包。

  sudo apt-mirror执行完成后,/var/spool/apt-mirror目录下就有了所有需要的deb包和相应的配置文件。因为我用的是中国科技大学的更新源所以下载好的路径是/var/spool/apt-mirror/mirror/debian.ustc.edu.cn/ubuntu

如果你选择其他Ubuntu Source服务区,那么会下载到其他对应的目录下。

现在我们本地已经有了所有Ubuntu Source服务器需要的文件,但是现在我们还不能使用本地的Ubuntu Source服务器。因为它还没有启动。

  Ubuntu Source服务器实际上是一个http服务器。我们访问sohu的Ubuntu Source服务器就是用浏览器访问的。

  所以,我们也需要启动一个web服务器来启动本地的Ubuntu Source服务器。

  我选择使用apache2这个web服务器来为Ubuntu Source服务器服务。Ubuntu Source服务器实际上就是一系列静态文件。

  Apt-cache ,apt-get 程序就是通过wget下载对应的静态文件实现软件下载和安装的。

  众所周知,Apache2 Web服务器服务于静态文件时非常高效、稳定。因此我选择apache2服务器。

  apache2服务器安装后,默认使用/var/www/目录作为Web的根目录。

  我这里取个巧,直接

1
2
3
cd /var/www/
ln /var/spool/apt-mirror/mirror/debian .ustc.edu.cn /ubuntu/ -s

在/var/www目录下创建一个Ubuntu超链接,链接到/var/spool/apt-mirror/mirror/debian.ustc.edu.cn/ubuntu/上

 sudo /etc/init.d/apache2 start/restart/stop/status 命令可以管理apache2服务器。使用sudo apache2ctl start启动。

  打开浏览器,输入http://localhost/Ubuntu

  就可以看到和http://mirrors.sohu.com/Ubuntu/ 相同的页面。

  至此,我们的本地Ubuntu Source服务器已经创建成功了。

  每天定时同步本地ubunt source服务器和远程ubunt source服务器

我们现在创建的ubunt source服务器的内容和中国科技大学版本的服务器相同。但是,每天Ubuntu团队都在向Ubuntu Source服务器中添加新的deb包。

  中国科技大学每天也都会和Ubuntu官方source服务器同步。

  我们必须经常使用sudo apt-mirror命令才能同步最新的deb包。

  我们可以使用一个定时执行的脚本,实现每天和中国科技大学的Ubuntu Source服务器同步来解决这个问题。


1
sudovim /etc/cron .d /apt-mirror

  把命令签名的#去掉。命令如下:


1
0 4 * * * apt-mirror /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron .log

  这样,计算机每天就会自动启动apt-mirror和Ubuntu Source服务器同步。

使用本地Ubuntu Source镜像服务器

  现在,我们可以修改/etc/atp/source.list文件以使用本地Ubuntu Source服务器作为apt源。

  如,我的ubunt服务器的地址是222.24.24.175,那么加入:

1
2
3
4
5
6
7
8
9
10
deb http: //222 .24.24.175 /ubuntu/ precise main multiverse restricted universe
deb http: //222 .24.24.175 /ubuntu/ precise-backports main multiverse restricted universe
deb http: //222 .24.24.175 /ubuntu/ precise-proposed main multiverse restricted universe
deb http: //222 .24.24.175 /ubuntu/ precise-security main multiverse restricted universe
deb http: //222 .24.24.175 /ubuntu/ precise-updates main multiverse restricted universe
deb-src http: //222 .24.24.175 /ubuntu/ precise main multiverse restricted universe
deb-src http: //222 .24.24.175 /ubuntu/ precise-backports main multiverse restricted universe
deb-src http: //222 .24.24.175 /ubuntu/ precise-proposed main multiverse restricted universe
deb-src http: //222 .24.24.175 /ubuntu/ precise-security main multiverse restricted universe
deb-src http: //222 .24.24.175 /ubuntu/ precise-updates main multiverse restricted universe

  即可。如果你还想同时使用其他的ubunt source服务器,那么把上述配置放在配置文件的上面,以先匹配。

  然后执行sudo apt-get update更新,以重建本地索引。以后就会使用本地的ubunt source服务器了。速度嗖嗖的:)