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 16.04 and Ubuntu 14.04. The setup will just be similar for Ubuntu 12.04 and 17.04.

Install apt-mirror

apt-get install apt-mirror

Configuring apt-mirror

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

Open the file  /etc/apt/mirror.list

vim /etc/apt/mirror.list

Modify the file like below to sync official Ubuntu mirrors for 16.04 and 14.04 to your network.

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

deb-amd64 http://archive.ubuntu.com/ubuntu xenial main main/debian-installer main/installer-amd64 main/i18n main/source restricted restricted/debian-installer universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse

deb-i386 http://archive.ubuntu.com/ubuntu xenial main main/debian-installer main/installer-i386 main/i18n main/source restricted restricted/debian-installer universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse

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

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

deb-amd64 http://archive.ubuntu.com/ubuntu trusty main main/debian-installer main/installer-amd64 main/i18n main/source restricted restricted/debian-installer universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse

deb-i386 http://archive.ubuntu.com/ubuntu trusty main main/debian-installer main/installer-i386 main/i18n main/source restricted restricted/debian-installer universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse
#
deb-src http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse

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

Write cron job that runs daily to automatically do sync:

50 3  * * * /usr/bin/apt-mirror

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

/usr/bin/apt-mirror

Serving Repositories with nginx:

apt-get install nginx
systemctl enable nginx
systemctl start nginx
vim /etc/nginx/conf.d/mirrors.conf

The content should look similar to below:

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

location / {
 autoindex on;
 }
}

Restart nginx

systemctl restart nginx

Configuring Clients

Ubuntu 16.04

vim  /etc/apt/sources.list

Add:

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

###### Ubuntu Main Repos

deb http://domain.com/ubuntu/ xenial main restricted universe multiverse

###### Ubuntu Security Repos

deb http://security.ubuntu.com/ubuntu xenial-security main restricted universe multiverse

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

Ubuntu 14.04

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

###### Ubuntu Main Repos

deb http://domain.com/ubuntu/ trusty main restricted universe multiverse

###### Ubuntu Security Repos

deb http://security.ubuntu.com/ubuntu trusty-security main restricted universe multiverse

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

Do apt update:

apt-get update