본문 바로가기

리눅스

우분투에서 로컬 APT 리포지토리 서버(apt-mirror)를 구축하는 방법

728x90

우분투에서 로컬 APT 리포지토리 서버(apt-mirror)를 구축하는 방법

Ubuntu에서 APT Mirror 서버를 구축하는 것은 패키지 다운로드를 로컬로 관리하고 대역폭을 절약하는 좋은 방법입니다. apt-mirror를 사용하면 Ubuntu의 패키지 미러를 복제하고 로컬 미러 서버를 설정할 수 있습니다.

 

중요: 미러 서버를 구축하려면 서버용 하드웨어 및 충분한 대역폭이 필요합니다.

 

출처-https://blog.ichasco.com/wp-content/uploads/2017/03/Selecci%C3%B3n_053.png(수정)

테스트 환경

서버 아이피 역할
APT-Mirror 10.0.2.15(nat), 192.168.56.101  
APT-Client 192.168.56.201  

1. 서버 준비

APT 미러 서버를 운영하기 위한 서버를 구성합니다. 우분투 미러를 호스팅할 서버를 운영체제로 설정합니다.

2. 웹 서버 설치(미러 서버 설치)

미러 서버를 운영하기 위한 웹 서버(Nginx)를 설치합니다.

sudo apt-get update
sudo apt-get install nginx

웹 서버를 활성화합니다.

sudo systemctl --now enable nginx

3. apt-mirror 패키지 설치

apt-mirror 패키지를 설치합니다.

sudo apt-get update
sudo apt-get install apt-mirror

apt-mirror 실행 파일 경로 및 실행 파일 권한 확인

$ which apt-mirror
/usr/bin/apt-mirror
$ ls -l /usr/bin/apt-mirror
-rwxr-xr-x 1 root root 32351 May 29  2017 /usr/bin/apt-mirror

APT 패키지 매니저로 설치한 apt-mirror 파일 백업

sudo mv /usr/bin/apt-mirror /usr/bin/apt-mirror.original

github(https://github.com/Stifler6996/apt-mirror)에 apt-mirror 파일로 교체

cd /usr/local/src
git clone https://github.com/Stifler6996/apt-mirror.git
cd apt-mirror
sudo mv apt-mirror /usr/bin/apt-mirror
sudo chown root:root /usr/bin/apt-mirror && sudo chmod 755 /usr/bin/apt-mirror

4. mirror.list 설정 파일 편집(미러 서버 구성)

apt-mirror의 설정 파일은 /etc/apt/mirror.list에 있습니다.

더보기

---

mirror.list 설정 파일

cat /etc/apt/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 ##############

deb http://archive.ubuntu.com/ubuntu artful main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu artful-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu artful-updates main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu artful-proposed main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu artful-backports main restricted universe multiverse

deb-src http://archive.ubuntu.com/ubuntu artful main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu artful-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu artful-updates main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu artful-proposed main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu artful-backports main restricted universe multiverse

clean http://archive.ubuntu.com/ubuntu

---

mirror.list 파일 백업(복사)

sudo cp /etc/apt/mirror.list /etc/apt/mirror.list.origin

텍스트 에디터로 편집하기

sudo vim /etc/apt/mirror.list

다음과 같이 설정을 변경하거나 추가할 수 있습니다.

  • set base_path /var/spool/apt-mirror/mirror: 미러 서버의 기본 경로를 설정합니다. 이 디렉토리에 미러 서버의 모든 데이터가 저장됩니다.
  • deb http://archive.ubuntu.com/ubuntu jammy main: 원본 미러 URL을 설정합니다. jammy을 원하는 Ubuntu 버전으로 변경할 수 있습니다.

sed 명령어로 편집하기

sudo sed -i 's/# set base_path    \/var\/spool\/apt-mirror/set base_path    \/var\/spool\/apt-mirror/' /etc/apt/mirror.list
sudo sed -i 's/# set mirror_path  $base_path\/mirror/set mirror_path  $base_path\/mirror/' /etc/apt/mirror.list
sudo sed -i 's/artful/jammy/g' /etc/apt/mirror.list

원하는 설정을 저장하고 설정 파일을 닫습니다.

5. 미러링 실행(미러 서버 초기화)

미러 서버를 초기화하여 설정한 패키지 미러를 복제합니다. 미러링은 몇 시간 또는 몇 일 동안 소요될 수 있습니다.

sudo apt-mirror
$ sudo apt-mirror
Downloading 18 index files using 18 threads...
Begin time: Tue Oct 17 13:52:30 2023
[18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Tue Oct 17 13:52:36 2023

Processing translation indexes: [T]

Downloading 150 translation files using 20 threads...
Begin time: Tue Oct 17 13:52:36 2023
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Tue Oct 17 13:52:41 2023

Processing DEP-11 indexes: [D]

Downloading 8 dep11 files using 8 threads...
Begin time: Tue Oct 17 13:52:41 2023
[8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Tue Oct 17 13:52:43 2023

Processing indexes: [SP]

16.8 GiB will be downloaded into archive.
Downloading 12838 archive files using 20 threads...
Begin time: Tue Oct 17 13:52:43 2023
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Tue Oct 17 14:03:30 2023

0 bytes in 0 files and 0 directories can be freed.
Run /var/spool/apt-mirror/var/clean.sh for this purpose.

Running the Post Mirror script ...
(/var/spool/apt-mirror/var/postmirror.sh)


Post Mirror script has completed. See above output for any possible errors.

미러 서버가 모든 패키지를 다운로드하고 복제하는 데 시간이 걸릴 수 있습니다.

 

  • apt-mirror 디렉토리 구조
$ tree -d -L 4 /var/spool/apt-mirror
/var/spool/apt-mirror
├── mirror
│   └── archive.ubuntu.com
│       └── ubuntu
│           ├── dists
│           └── pool
├── skel
│   └── archive.ubuntu.com
│       └── ubuntu
│           └── dists
└── var

6. 웹 서버 구성(미러 서버 구성)

웹 서버(Nginx)를 설정하여 미러 데이터 디렉토리를 설정합니다.

예를 들어, Nginx의 경우 /etc/nginx/sites-available/default 파일을 편집하여 미러 데이터 디렉토리를 설정합니다.

sudo vim /etc/nginx/sites-available/default
# Default server configuration
#
server {
        listen 80 default_server;

        server_name _;

        location / {
                root /var/spool/apt-mirror/mirror/archive.ubuntu.com;
                index index.html index.htm;
                autoindex on;
        }
}
  • 또는 mirrors.conf 설정 파일 생성
sudo vim /etc/nginx/conf.d/mirrors.conf
server {
    listen 80;
    
    server_name mirror.domain_com;
    
    root /var/spool/apt-mirror/mirror/archive.ubuntu.com;

    location / {
        autoindex on;
    }
}

server_name 지시자에 사용하려는 도메인으로 변경합니다.

  • sed 명령어로 편집하기
sed -i 's/server_name mirror.domain_com;/server_name mirror.scbyun.com;/' /etc/nginx/conf.d/mirrors.conf
728x90

7. 미러 서버 재시작

nginx -t

웹 서버를 다시 시작하여 미러 서버를 활성화합니다.

sudo systemctl restart nginx

8. 클라이언트 설정

클라이언트 시스템의 APT 소스 파일을 미러 서버를 가리키도록 설정합니다. APT 미러 서버 URL을 APT 소스 파일에 추가하고 apt-get update를 실행하여 미러 서버에서 패키지를 가져옵니다.

더보기

---

sources.list 설정 파일(OFFICIAL Ubuntu Repository)

cat /etc/apt/sources.list
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://kr.archive.ubuntu.com/ubuntu jammy main restricted
# deb-src http://kr.archive.ubuntu.com/ubuntu jammy main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://kr.archive.ubuntu.com/ubuntu jammy-updates main restricted
# deb-src http://kr.archive.ubuntu.com/ubuntu jammy-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://kr.archive.ubuntu.com/ubuntu jammy universe
# deb-src http://kr.archive.ubuntu.com/ubuntu jammy universe
deb http://kr.archive.ubuntu.com/ubuntu jammy-updates universe
# deb-src http://kr.archive.ubuntu.com/ubuntu jammy-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://kr.archive.ubuntu.com/ubuntu jammy multiverse
# deb-src http://kr.archive.ubuntu.com/ubuntu jammy multiverse
deb http://kr.archive.ubuntu.com/ubuntu jammy-updates multiverse
# deb-src http://kr.archive.ubuntu.com/ubuntu jammy-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://kr.archive.ubuntu.com/ubuntu jammy-backports main restricted universe multiverse
# deb-src http://kr.archive.ubuntu.com/ubuntu jammy-backports main restricted universe multiverse

deb http://kr.archive.ubuntu.com/ubuntu jammy-security main restricted
# deb-src http://kr.archive.ubuntu.com/ubuntu jammy-security main restricted
deb http://kr.archive.ubuntu.com/ubuntu jammy-security universe
# deb-src http://kr.archive.ubuntu.com/ubuntu jammy-security universe
deb http://kr.archive.ubuntu.com/ubuntu jammy-security multiverse
# deb-src http://kr.archive.ubuntu.com/ubuntu jammy-security multiverse

---

sources.list 파일 백업

sudo cp /etc/apt/sources.list /etc/apt/sources.list.origin
  • 클라이언트 시스템에서 /etc/apt/sources.list 파일을 편집하여 로컬 미러 서버를 사용하도록 설정
vim /etc/apt/sources.list
deb http://<미러-서버-IP>/mirror/archive.ubuntu.com/ubuntu jammy main restricted
deb-src http://<미러-서버-IP>/mirror/archive.ubuntu.com/ubuntu jammy main restricted

<미러-서버-IP>를 실제 미러 서버의 IP 주소로 변경합니다.

deb http://192.168.56.101/ubuntu/ jammy main restricted
deb-src http://192.168.56.101/ubuntu/ jammy main restricted

sed 명령어로 편집하기

  • 도메인으로 변경하기
sed -i 's/kr.archive.ubuntu.com/mirror.scbyun.com/' /etc/apt/sources.list
  • 아이피로 변경하기
sed -i 's/kr.archive.ubuntu.com/192.168.56.101/' /etc/apt/sources.list
  • APT 캐시 삭제
sudo apt-get clean
  • 패키지 업데이트
apt-get update
$ sudo apt update
Get:1 http://192.168.56.101/ubuntu jammy InRelease [270 kB]
Get:2 http://192.168.56.101/ubuntu jammy/main amd64 Packages [1,395 kB]
Get:3 http://192.168.56.101/ubuntu jammy/main Translation-en [510 kB]
Get:4 http://192.168.56.101/ubuntu jammy/main amd64 c-n-f Metadata [30.3 kB]
Get:5 http://192.168.56.101/ubuntu jammy/restricted amd64 Packages [129 kB]
Get:6 http://192.168.56.101/ubuntu jammy/restricted Translation-en [18.6 kB]
Get:7 http://192.168.56.101/ubuntu jammy/restricted amd64 c-n-f Metadata [488 B]
Fetched 2,354 kB in 0s (4,986 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

모든 설정을 마친 후 클라이언트 시스템에서 패키지 업데이트를 실행합니다. 로컬 미러 서버를 통해 패키지를 다운로드하게 됩니다.

 

테스트로 lftp 패키지를 설치합니다.

$ lftp
Command 'lftp' not found, but can be installed with:
apt install lftp
apt-get install lftp
$ apt-get install lftp
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  lftp
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 720 kB of archives.
After this operation, 1,958 kB of additional disk space will be used.
Get:1 http://192.168.56.101/ubuntu jammy/main amd64 lftp amd64 4.9.2-1build1 [720 kB]
Fetched 720 kB in 0s (3,724 kB/s)
Selecting previously unselected package lftp.
(Reading database ... 101473 files and directories currently installed.)
Preparing to unpack .../lftp_4.9.2-1build1_amd64.deb ...
Unpacking lftp (4.9.2-1build1) ...
Setting up lftp (4.9.2-1build1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning linux images...

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
$ lftp
lftp :~>

 

이러한 단계를 따르면 Ubuntu에서 APT Mirror 서버를 구축하고 로컬 미러를 통해 패키지를 관리할 수 있습니다. 이를 통해 패키지 다운로드 속도가 향상되고 대역폭을 절약할 수 있습니다.

 

참고URL

- LinuxTechi : How to Setup Local APT Repository Server on Ubuntu 22.04

- github : https://github.com/Stifler6996/apt-mirror

 

728x90