使用apt-mirror搭建內(nèi)網(wǎng)Ubuntu APT源同步阿里APT源

背景:
因從內(nèi)網(wǎng)環(huán)境無(wú)法從外網(wǎng)獲取安裝包,所以需要搭建內(nèi)網(wǎng)Apt源同步阿里云Apt源

環(huán)境:
內(nèi)網(wǎng)Apt源服務(wù)器:192.168.33.150
Ubuntu版本:22.04
apt-mirror版本:0.5.4-1
nginx版本:1.20.1

apt-mirror不僅可以處理Ubuntu的軟件源,幾乎debian系的都可以使用apt-mirror來(lái)同步。

內(nèi)網(wǎng)Apt源服務(wù)器配置

安裝apt-mirror同步工具

首先要確認(rèn)內(nèi)網(wǎng)Apt源服務(wù)器(192.168.33.150)是否可正常與其配置的源地址相通

# 安裝apt-mirror同步工具
sudo apt-get install -y apt-mirror

獲取阿里云Ubuntu源配置

我們通過(guò)阿里云鏡像源來(lái)獲取相關(guān)的鏡像源的配置,如下所示(Ubuntu20.04):

deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

其中deb-src為源碼包,如果不需要可以只使用deb。
注意:Ubuntu各個(gè)版本使用的源配置是不一致的,主要是deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse中的focal版本區(qū)別,其對(duì)應(yīng)關(guān)系如下所示(只是一部分)

22.04 - Jammy
21.10 - Impish 
21.04 - Hirsute 
20.10 - Groovy 
20.04 LTS - Focal 
19.10 - Eoan 
19.04 - Disco 
18.10 - Cosmic 
18.04 LTS - Bionic 
17.04 - Zesty 
16.10 - Yakkety 
16.04 LTS - Xenial 

配置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  <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads     20
set _tilde 0
#
############# end config ##############
# 阿里云Ubuntu的APT源
deb [arch=amd64] http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb [arch=amd64] http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb [arch=amd64] http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb [arch=amd64] http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb [arch=amd64] http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

# 下載完成后清除部分空間
clean https://mirrors.aliyun.com/ubuntu

同步阿里云Ubuntu源

運(yùn)行apt-mirror命令即可開(kāi)始同步,運(yùn)行時(shí)會(huì)顯示源的大小,基本每個(gè)ubuntu的源都在200G左右,下載時(shí)間主要看下載速率。
apt-mirror命令支持?jǐn)帱c(diǎn)續(xù)傳,不過(guò)如果是意外中斷,需要?jiǎng)h除/var/spool/apt-mirror/var目錄下的apt-mirror.lock文件再次執(zhí)行。

# 開(kāi)始同步
apt-mirror

建議使用tmux創(chuàng)建一個(gè)終端來(lái)同步,同步完成后,同步的軟件源會(huì)存放到/var/spool/apt-mirror目錄下。

Nginx暴露Apt源

安裝Nginx

apt-get install -y nginx

配置鏡像路徑(隨自己習(xí)慣,創(chuàng)不創(chuàng)建都可以,nginx配置文件中的路徑需要指定到源路徑)

# 創(chuàng)建路徑
mkdir -p /data/public
# 創(chuàng)建軟鏈
ln -s /var/spool/apt-mirror/mirror/mirrors.aliyun.com/ubuntu /data/pubilc/ubuntu

配置Nginx配置文件

# /etc/nginx/nginx.conf
http {
    ...
    sendfile on; # 開(kāi)啟高效文件傳輸模式
    server {
        location / {
            autoindex on; # 開(kāi)啟目錄瀏覽模式
            root /data/public;
            index index.html index.htm;
        }
    ....
    }
}

/data/public中創(chuàng)建index.html文件,用于訪問(wèn)(此處不贅述,可以網(wǎng)上找或者不配置)

啟動(dòng)Nginx,從瀏覽器訪問(wèn)192.168.33.150/ubuntu

客戶端配置

備份原有源配置文件,將地址換成Apt內(nèi)網(wǎng)源服務(wù)器地址

# 備份
cp -a /etc/apt/source.list /etc/apt/source.list.bak
# 修改,將/etc/apt/source.list中的內(nèi)容替換為以下
deb [arch=amd64] http://192.168.33.150/ubuntu/ jammy main restricted universe multiverse
deb [arch=amd64] http://192.168.33.150/ubuntu/ jammy-security main restricted universe multiverse
deb [arch=amd64] http://192.168.33.150/ubuntu/ jammy-updates main restricted universe multiverse
deb [arch=amd64] http://192.168.33.150/ubuntu/ jammy-proposed main restricted universe multiverse
deb [arch=amd64] http://192.168.33.150/ubuntu/ jammy-backports main restricted universe multiverse

配置完成后不要忘記更新apt-get update

mirror.list配置文件詳解

############# config ##################
#
# 配置數(shù)據(jù)基目錄(可以修改為自己需要保存的位置)
# set base_path /var/spool/apt-mirror
# 配置鏡像存儲(chǔ)位置
# set mirror_path $base_path/mirror
# 配置臨時(shí)下載索引位置
# set skel_path $base_path/skel
# 配置日子,URLs和MD5校驗(yàn)信息存儲(chǔ)位置
# set var_path $base_path/var
# 配置刪除過(guò)期源腳本位置(默認(rèn)不刪除,方便安裝舊版本軟件)
# set cleanscript $var_path/clean.sh
# 設(shè)置默認(rèn)架構(gòu), 可填: amd64 或 i386,默認(rèn)是和本機(jī)一個(gè)架構(gòu)
# set defaultarch <running host architecture>
#
# 設(shè)定下載后運(yùn)行的腳本位置
# set postmirror_script $var_path/postmirror.sh
# 設(shè)置是否執(zhí)行 下載后的腳本操作,默認(rèn)是1(但是默認(rèn)沒(méi)有postmirror.sh腳本)
# set run_postmirror 0
# 設(shè)置下載線程數(shù)
set nthreads 20
# 是否替換URL中的波浪線,替換成%7E(HTML代碼),否則會(huì)跳過(guò)不進(jìn)行下載
set _tilde 0
#
############# end config ##############
# 配置Ubuntu jammy 源
deb [arch=amd64] http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb [arch=amd64] http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb [arch=amd64] http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb [arch=amd64] http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb [arch=amd64] http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
# 下載完成后清除部分空間
clean http://mirrors.aliyun.com/ubuntu/
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容