內網的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地址