内网的linux服务器愈来愈多,有必要搭建一个内网debian源镜像服务器。linux
debian官方提供了建设源镜像的脚本ftpsync,并且有详细的源镜像设置说明apache
ftpsync使用rsync程序进行源镜像安全
# apt-get install rsync
从官方下载ftpsync脚本bash
# wget http://ftp-master.debian.org/ftpsync.tar.gz
ftpsync使用环境变量BASEDIR来定位程序文件、配置文件和日志文件等的存放路径,BASEDIR默认取值${HOME},能够将ftpsync放置到用户主目录下服务器
在用户主目录下新建bin,etc,log目录,解压ftpsync,架构
$ tar zxvf ftpsync.tar.gz $ cp distrib/bin/ftpsync ~/bin $ cp distrib/etc/ftpsync.conf.sample ~/etc/ftpsync.conf $ cp distrib/etc/common ~/etc/common
创建存放镜像文件的单独目录,各类架构须要的磁盘空间,当前镜像all,amd64和source总共约须要190G硬盘空间。ssh
最后打开~/etc/ftpsync.conf,修改如下内容:ide
TO=”/srv/mirrors/debian/” ##镜像源存放位置 RSYNC_HOST=”ftp.cn.debian.org” ##镜像自哪一个外部源,debian中国官方源镜像是最佳选择,固然ftp.tw.debian.org,ftp.kr.debian.org和ftp.jp.debian.org速度也很快,中国官方源镜像由中国科技大学维护 ARCH_EXCLUDE=”alpha arm armel armhf hppa hurd-i386 i386 ia64 kfreebsd-amd64 kfreebsd-i386 m68k mipsel mips powerpc s390 s390x sh sparc source” ##排除的架构,此处只保留amd64源,source源也排除,只镜像必要的,尽可能节省硬盘空间。
注:当前脚本排除source镜像会提示错误:
Unexpected remote arg: ftp.cd.debian.org::debian
rsync error: syntax or usage error (code 1) at main.c(1232) [sender=3.0.9]
不要排除source可解决此错误。网站
当档案库有变化时,上游源镜像服务器会主动向下游镜像服务器推送同步通知,而后下游源镜像服务器就能够及时的更新本身的档案库,这就是推模式镜像。debian的主服务器与下游镜像服务器之间即采用此种模式。spa
此种模式须要下游镜像服务器配置ssh服务,上游镜像服务器使用ssh来通知下游服务器。可使用一个普通用户来接受通知,将上游镜像服务器的公钥保存在~/.ssh/authorized_keys文件中,而且在此文件中添加以下语句以限制上游镜像服务器的权限
no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,command=”~/bin/ftpsync”,from=”ip_address”
此处ip_address即上游镜像服务器的IP地址。
并且上游upstream镜像服务器还能够经过用户名/密码来受权谁能够向某些下游镜像服务器进行推送,而且这些用户名/密码是与系统隔离的,并非使用/etc/passwd,进一步加强安全性。
使用推送模式同步须要将下游downstream服务器地址,ssh端口和使用的用户告知上游源镜像服务器维护者
对于企业内部的源镜像服务器,没有必要使用推模式,只要在空闲时段定时与上游源镜像服务器同步便可。
使用cron让ftpsync定时自动运行,/etc/cron.d/目录下添加文件ftpsync,内容以下:
SHELL=/bin/bash
PATH=/home/username/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#minute hour day_of_month month day_of_week user command
0 1 * * * username ftpsync
username为运行ftpsync脚本的用户,天天凌晨1点自动运行ftpsync脚本与上游源镜像服务器同步
内网其余用户须要经过http或者ftp协议从本地源镜像服务器更新系统,此处使用apache来提供http方式的源镜像服务
# apt-get install apache2
/etc/apache2/sites-available/目录下添加文件debian_mirror,内容以下:
1 <VirtualHost *:80> 2 3 DocumentRoot /srv/mirrors/debian 4 #<Directory /> 5 # Options FollowSymLinks 6 # AllowOverride None 7 #</Directory> 8 <Directory /srv/mirrors/debian> 9 Options Indexes SymlinksIfOwnerMatch FollowSymLinks MultiViews 10 IndexOptions NameWidth=* SuppressDescription 11 AllowOverride None 12 Order allow,deny 13 allow from all 14 </Directory> 15 16 ErrorLog ${APACHE_LOG_DIR}/debian_mirror_error.log 17 18 # Possible values include: debug, info, notice, warn, error, crit, 19 # alert, emerg. 20 LogLevel warn 21 22 CustomLog ${APACHE_LOG_DIR}/debian_mirror_access.log combined 23 </VirtualHost>
而后/etc/apache2/sites-enabled目录下新建符号连接,此处将其设置为默认网站,也可使用虚拟主机
# rm 000-default # ln -sf /etc/apache2/sites-available/debian_mirror 000-default
最后
# rm 000-default # ln -sf /etc/apache2/sites-available/debian_mirror 000-default
使用本地源镜像服务
编辑/etc/apt/source.list,添加
deb http://mirror_ip wheezy main contrib non-free deb http://mirror_ip wheezy-updates main contrib non-free deb http://mirror_ip wheezy-proposed-updates main contrib non-free
mirror_ip便是新建的源镜像服务器的IP地址