本人家里有好幾臺(tái)運(yùn)行Debian GNU/Linux testing的計(jì)算機(jī),共處于一個(gè)小局域網(wǎng)內(nèi)。因?yàn)镈ebian testing是滾動(dòng)更新的,所以我每隔幾天就要把它們?nèi)扛乱槐?。但是一?lái)中國(guó)家庭的網(wǎng)速還是說(shuō)不上很快,每臺(tái)機(jī)器都要從上游軟件源的服務(wù)器上下載成百上千兆字節(jié)的軟件包要花不少時(shí)間;二來(lái)這樣操作也給軟件源的服務(wù)器造成不小的負(fù)擔(dān)。
apt-p2p,顧名思義,就是用戶與用戶之間直接進(jìn)行 deb 軟件包的傳遞。
后來(lái)我想起Debian曾開(kāi)發(fā)過(guò)一個(gè)叫apt-p2p的軟件,它的設(shè)計(jì)初衷是緩存已下載的軟件包提供給其他同樣運(yùn)行著apt-p2p的peers,作為從軟件源下載的補(bǔ)充。那么是否可以用這個(gè)軟件在局域網(wǎng)內(nèi)搭建一個(gè)Debian軟件包緩存呢?
apt-p2p擁有著 P2P 技術(shù)的優(yōu)勢(shì):
# * 即能從用戶節(jié)點(diǎn)中獲取軟件包,同時(shí)又能從官方源中下載
# * 使用 apt-p2p 的人越多,軟件包的傳遞效率就越高、速度越快
# * 低 CPU 和內(nèi)存資源消耗
# *先進(jìn)行哈希校驗(yàn),之后才進(jìn)行軟件包的傳遞,最大限度地確保安全、避免下載到損壞的包
但同樣,它也存在著 P2P 的弊端:
# * 如果使用 apt-p2p 的人不多,速度可能還不及原裝的 apt-get。
# * 盡管機(jī)率非常小,但仍可能遭遇到潛在的哈希欺騙,進(jìn)而引發(fā)系統(tǒng)安全隱患
目前,在天朝境內(nèi)使用 apt-p2p 的人的確不多,這一情況可能是制約 apt-p2p 在天朝推廣的最主要原因。
但如果人人都不去支持 apt-p2p,那 apt-p2p 又如何發(fā)展得起來(lái)呢?我們又如何才能獲得 apt-p2p 的便利呢?
所以,鄙人在此號(hào)召大家一起來(lái)使用 apt-p2p!
安裝步驟方法
1、準(zhǔn)備依賴(lài)環(huán)境
sudo apt-get install python python-apt python-twisted python-twisted-web2
2、安裝 apt-p2p
sudo apt-get install apt-p2p
3、修改軟件源列表(注:第三方源不用也不必修改)
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
sudo sed -i 's%http://%http://localhost:9977/%g' /etc/apt/sources.list
4、根據(jù)需要配置apt-p2p
sudo gedit /etc/apt-p2p/apt-p2p.conf
其中
“PORT = ”指定 apt-p2p 使用的端口,默認(rèn)為“9977”。當(dāng)然,你也可以指定它為“9527”,只是要確保與《/etc/apt/sources.list》中的修改值一致
“UPLOAD_LIMIT = ”指定最大上傳速度,默認(rèn)為“0”即不限速,建議設(shè)置為“10~50”(KB/秒)
“MIN_DOWNLOAD_PEERS = ”是指定當(dāng)可用的 prees 節(jié)點(diǎn)少于多少時(shí),才開(kāi)始從官方源下載,建議使用默認(rèn)值“3”
“CACHE_DIR = ”是指定 apt-p2p 的軟件包緩存目錄,建議使用默認(rèn)值
“OTHER_DIRS = ”是指定額外的軟件包目錄。注意:一旦指定,在該目錄下的所有文件都會(huì)被上傳
“UPLOAD_LIMIT = ”指定是否進(jìn)行軟件包下載數(shù)據(jù)列表統(tǒng)計(jì)并公布。默認(rèn)為“yes”即進(jìn)行公布,若你的電腦對(duì)保密等級(jí)要求特別高,那請(qǐng)?jiān)O(shè)置為“no”
“DHT-ONLY = ”是否僅只使用分布式散列表提供的資源,建議使用默認(rèn)值“no”
5、若是裝有防火墻,須將“9967”、“9977”端口開(kāi)放;若是使用路由器,還需要進(jìn)行映射設(shè)置
6、重新啟動(dòng) apt-p2p,以應(yīng)用新配置
sudo rm -rf /var/cache/apt-p2p/cache/*
sudo /etc/init.d/apt-p2p restart
7、開(kāi)始更新、升級(jí)系統(tǒng)或安裝軟件包
sudo apt-get update
sudo apt-get upgrade
說(shuō)明:有的教程說(shuō)要禁用第三方源,但就鄙人實(shí)際使用下來(lái)的情況,就算不禁用第三方源也沒(méi)有什么影響
檢視 apt-p2p 運(yùn)行情況的方法:
在瀏覽器地址欄中輸入 http://localhost:9977/,即可察看相關(guān)信息
本地軟件包緩存搭建方法:
apt-p2p由Python寫(xiě)成,安裝好后默認(rèn)監(jiān)聽(tīng)于TCP和UDP的9977端口,可通過(guò)/etc/apt-p2p/apt-p2p.conf改變其行為。TCP端口同時(shí)用于本機(jī)apt下載軟件包,而UDP端口用于組建在p2p文件共享工具中常見(jiàn)的DHT網(wǎng)絡(luò)。只需要在/etc/apt/sources.list文件中軟件源的url,例如http://mirror.server/debian/的服務(wù)器域名前面插入localhost:9977,即成為http://localhost:9977/mirror.server/debian/,具體從互聯(lián)網(wǎng)上獲取軟件包的工作便交給apt-p2p負(fù)責(zé)了:
當(dāng)收到來(lái)自本機(jī)apt的文件下載請(qǐng)求時(shí),apt-p2p會(huì)先檢測(cè)文件是否已經(jīng)存在于緩存的目錄樹(shù)中
如果已存在,則還要檢查是否為最新(通過(guò)向軟件源服務(wù)器發(fā)出HEAD請(qǐng)求來(lái)實(shí)現(xiàn))
如果是,則直接返回緩存的文件
否則從互聯(lián)網(wǎng)上下載文件供給apt,同時(shí)存入緩存并記錄其散列值
下載文件會(huì)首先嘗試從peer處下載,不成再到軟件源服務(wù)器處下載
緩存下來(lái)的文件和散列值可供其他peer使用??梢酝ㄟ^(guò)apt-p2p的日志文件/var/log/apt-p2p.log深入觀測(cè)這一過(guò)程。
以下是我安裝了apt-p2p的主機(jī)mycastle的sources.list:
deb http://localhost:9977/mirrors.ustc.edu.cn/debian/ testing main
deb-src http://localhost:9977/mirrors.ustc.edu.cn/debian/ testing main
deb http://localhost:9977/mirrors.ustc.edu.cn/debian-security/ testing/updates main
deb-src http://localhost:9977/mirrors.ustc.edu.cn/debian-security testing/updates main
deb http://localhost:9977/mirrors.ustc.edu.cn/debian/ testing-proposed-updates main
deb-src http://localhost:9977/mirrors.ustc.edu.cn/debian/ testing-proposed-updates main
deb http://localhost:9977/mirrors.ustc.edu.cn/debian/ unstable main
deb-src http://localhost:9977/mirrors.ustc.edu.cn/debian/ unstable main
然而apt-p2p的緩存只能給本機(jī)使用,其他主機(jī)若試圖直接訪問(wèn)9977端口只會(huì)得到404錯(cuò)誤。因此還需要在安裝apt-p2p的主機(jī)上架設(shè)一個(gè)http反向代理,使得在apt-p2p看來(lái),所有的請(qǐng)求均來(lái)自本機(jī)。
我使用的反向代理是Pound,它在安裝好后默認(rèn)是禁用的。在/etc/pound/pound.cfg中配置好服務(wù)器后再到/etc/default/pound中啟用它,然后使用service啟動(dòng)服務(wù)器即可。
我使用的配置如下:
...(keep default)
ListenHTTP
Address 0.0.0.0
Port 9978
## allow PUT and DELETE also (by default only GET, POST and HEAD)?:
xHTTP 0
Service
BackEnd
Address 127.0.0.1
Port 9977
End
End
End
監(jiān)聽(tīng)于9978端口,后臺(tái)服務(wù)器在本機(jī)的9977端口。
這樣一來(lái),其他主機(jī)便也可以通過(guò)9978端口使用apt-p2p的緩存了。我的做法是在打算使用緩存的其他主機(jī)上將/etc/apt/sources.list移動(dòng)到/etc/apt/sources.list.d/50_main.list,然后仿照緩存主機(jī)的軟件源列表建立/etc/apt/sources.list.d/10_apt-p2p-home.list,只是要把url中的localhost:9977換成hostname-of-cache-server>.local:9978即可,當(dāng)然最好保持軟件源服務(wù)器相同。這樣apt會(huì)優(yōu)先通過(guò)反向代理使用apt-p2p的緩存,當(dāng)緩存不可用時(shí)仍可直接連接軟件源服務(wù)器獲取軟件包:
10_apt-p2p-home.list:/p>
p>deb http://mycastle.local:9978/mirrors.ustc.edu.cn/debian/ testing main
deb-src http://mycastle.local:9978/mirrors.ustc.edu.cn/debian/ testing main
deb http://mycastle.local:9978/mirrors.ustc.edu.cn/debian-security/ testing/updates main
deb-src http://mycastle.local:9978/mirrors.ustc.edu.cn/debian-security testing/updates main
deb http://mycastle.local:9978/mirrors.ustc.edu.cn/debian/ testing-proposed-updates main
deb-src http://mycastle.local:9978/mirrors.ustc.edu.cn/debian/ testing-proposed-updates main
deb http://mycastle.local:9978/mirrors.ustc.edu.cn/debian/ unstable main
deb-src http://mycastle.local:9978/mirrors.ustc.edu.cn/debian/ unstable main
50_main.list:/p>
p>deb http://mirrors.ustc.edu.cn/debian/ testing main
deb-src http://mirrors.ustc.edu.cn/debian/ testing main
deb http://mirrors.ustc.edu.cn/debian-security/ testing/updates main
deb-src http://mirrors.ustc.edu.cn/debian-security testing/updates main
deb http://mirrors.ustc.edu.cn/debian/ testing-proposed-updates main
deb-src http://mirrors.ustc.edu.cn/debian/ testing-proposed-updates main
deb http://mirrors.ustc.edu.cn/debian/ unstable main
deb-src http://mirrors.ustc.edu.cn/debian/ unstable main
如安裝了apt-transport-https,可以將50_main.list中的http換成https。
這種以主機(jī)名加“.local”的域名格式是mDNS/DNS-SD協(xié)議提供的一種在廣播域內(nèi)定位主機(jī)和發(fā)布服務(wù)的方法,主要實(shí)現(xiàn)有Avahi和Mac OS X的Bonjour。
這樣一來(lái),在局域網(wǎng)內(nèi),任何一臺(tái)主機(jī)都優(yōu)先通過(guò)apt-p2p獲取軟件包,一臺(tái)更新可以惠及全家;即使短時(shí)間內(nèi)更新多臺(tái)主機(jī),也只需要向軟件源索取一次;而且大規(guī)模的數(shù)據(jù)流動(dòng)發(fā)生在帶寬很高的局域網(wǎng)內(nèi),更縮短了更新時(shí)間,提高了效率。
只是這樣一來(lái)相當(dāng)于在非緩存主機(jī)上配置了兩個(gè)軟件源,而當(dāng)這兩個(gè)軟件源重復(fù)部分的信息不同步時(shí)(如筆記本計(jì)算機(jī)在局域網(wǎng)外更新了直連軟件源服務(wù)器的軟件包列表),apt-get source是無(wú)法使用的,因?yàn)閍pt對(duì)源碼包的檢查更嚴(yán)格。