Wednesday, July 3, 2024
HomeOperating SystemsUbuntuCreating Ubuntu Mirrors using apt-mirror

Creating Ubuntu Mirrors using apt-mirror

Creating Ubuntu Mirrors using apt-mirror: Hello good people. In this short tutorial, I’ll show you how to setup local Ubuntu repository mirrors using apt-mirror. This usually comes in handy when you have a good number of Ubuntu servers in your network.

What is apt-mirror?

This is a small tool that provides ability to mirror any parts (or even all) of Debian and Ubuntu GNU/Linux distributions or any other apt sources which typically provided by open source developers.

This guide will cover setting up repository mirrors for Ubuntu. A similar process applies to any other release of Ubuntu Linux.

Install apt-mirror on Ubuntu

Installation requiremets:

  • Updated OS
  • Adequate space. You can mount secondary disk in /var/spool/apt-mirror (Repository contents storage path)

Update package repository index

sudo apt update

Then install apt-mirror package:

sudo apt install apt-mirror

Configuring apt-mirror on Ubuntu

After installing apt-mirror, we can now proceed to do some configurations. This implies adding repository urls.

Open the file  /etc/apt/mirror.list

sudo vim /etc/apt/mirror.list

Modify the file like below to sync official Ubuntu mirrors for 22.04/20.04/18.04 to your network.

To sync Ubuntu 22.04, add the following lines to the file.

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

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

To sync Ubuntu 20.04, add the following lines to the file.

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

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

To sync Ubuntu 18.04, add the following line to the file.

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

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

This will work both for 32 bit and 64 bit Ubuntu systems.

Initiate sync manually

You can manually invoke apt-mirror command as well to initiate sync.

sudo /usr/bin/apt-mirror

Sample execution output:

Downloading 340 index files using 20 threads...
Begin time: Tue Jan 18 18:44:40 2022
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Tue Jan 18 18:44:41 2022

Processing translation indexes: [TTTTTTTT]

Downloading 567 translation files using 20 threads...
Begin time: Tue Jan 18 18:44:41 2022
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Tue Jan 18 18:44:42 2022

Processing DEP-11 indexes: [DDDDDDDD]

Downloading 104 dep11 files using 20 threads...
Begin time: Tue Jan 18 18:44:42 2022
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Tue Jan 18 18:44:42 2022

Processing indexes: [SSSSPPPPPPPP]
........

Automate packages sync using cron job

Write cron job that runs daily to automatically do sync:

$ sudo crontab -e
50 3  * * * /usr/bin/apt-mirror

Serving Repositories with nginx:

Install Nginx web server:

sudo apt update
sudo apt install nginx

Start and enable the service:

sudo systemctl enable nginx
sudo systemctl start nginx

Configure Nginx to serve Ubuntu repositories:

sudo vim /etc/nginx/conf.d/mirrors.conf

The content should look similar to below:

server {
 listen 80;
 server_name mirror.domain.com; 
 root /var/spool/apt-mirror/mirror/archive.ubuntu.com/;

 location / {
   autoindex on;
 }
}

Restart nginx

sudo systemctl restart nginx

Configuring Clients to use configured mirro

We’ll use Ubuntu 20.04 as an example in client machine configuration.

Edit the default sources configuration file:

sudo vim /etc/apt/sources.list

Set repositories to configured one.

#------------------------------------------------------------------------------#
# OFFICIAL UBUNTU REPOS #
#------------------------------------------------------------------------------#

###### Ubuntu Main Repos
deb http://mirror.domain.com/ubuntu/ focal main restricted universe multiverse

###### Ubuntu Security Repos
deb http://mirror.domain.com/ubuntu focal-security main restricted universe multiverse

###### Ubuntu Update Repos
deb http://mirror.domain.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirror.domain.com/ubuntu/ focal-backports main restricted universe multiverse

Test if the repos configured are working:

sudo apt update

See more articles below:

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

Most Popular

Recent Comments