Local Ubuntu APT repo mirror on CentOS 7

     

Note: make sure to check out the revised APT repo mirror guide!

So you might want to have a local mirror for your Ubuntu workstations, but you don’t want to run Ubuntu on the mirror server itself, because Ubuntu Server sucks ass. In this case, it’s really not that hard to find a way.

Set up a chroot environment with 18.04 (substitute the mirror URL with whatever works best for ya):

yum install epel-release
yum install debootstrap
mkdir /opt/ubuntu-18.04
ln -s /opt/ubuntu-18.04 /opt/ubuntu
debootstrap --arch=amd64 bionic /opt/ubuntu http://de.archive.ubuntu.com/ubuntu/

Enter chroot:

chroot /opt/ubuntu

Set up apt-mirror:

apt install software-properties-common
add-apt-repository universe
apt install apt-mirror
# use 18.04
sed -i 's@artful@bionic@' /etc/apt/mirror.list
# enable backports
sed -Ei 's@#deb (.*)bionic-backports(.*)@deb \1bionic-backports\2@' /etc/apt/mirror.list
# disable security as you normally fetch that from security.ubuntu.com
sed -Ei 's@deb (.*)bionic-security(.*)@#deb \1bionic-security\2@' /etc/apt/mirror.list
# don't mirror source packages
sed -Ei 's@^deb-src(.*)@#deb-src\1@' /etc/apt/mirror.list
# enable both 32 and 64 bit, some 64-bit packages rely on this, e.g. VirtualBox
sed -Ei 's@^deb (.*)@deb-i386  \1\ndeb-amd64 \1@' /etc/apt/mirror.list
# perform cleanups automatically
echo '/var/spool/apt-mirror/var/clean.sh' >> /var/spool/apt-mirror/var/postmirror.sh

Optionally, if you want to use netinstall, add these to /etc/apt/mirror.list as well:

deb-amd64  http://archive.ubuntu.com/ubuntu bionic main/debian-installer main
deb-amd64  http://archive.ubuntu.com/ubuntu bionic-updates main/debian-installer main
deb-amd64  http://archive.ubuntu.com/ubuntu bionic-backports main/debian-installer main

Run the initial sync, should be around 200 gigs so be patient:

sudo -u apt-mirror apt-mirror

Exit the chroot:

exit

Set up a cronjob for automatic updates, and set up the /var/log/apt-mirror symlink for easy access to the log file on the CentOS host:

echo '0 * * * * root /sbin/chroot /opt/ubuntu /bin/bash -c "sudo -iu apt-mirror /usr/bin/apt-mirror &>> /var/spool/apt-mirror/var/cron.log"' > /etc/cron.d/apt-mirror
systemctl restart crond.service
ln -s /opt/ubuntu/var/spool/apt-mirror/var/cron.log  /var/log/apt-mirror

Then you can install Nginx to host your packages. A minimal host config file that should work:

server
{
    listen 80;
    listen [::]:80;
    server_name apt.foobar.com;

    location /
    {
        root   /opt/ubuntu/var/spool/apt-mirror/mirror/archive.ubuntu.com;
        index  index.html index.htm;
        autoindex on;
    }
}

Then sources.list on the client machines should look something like this:

deb http://apt.foobar.com/ubuntu bionic main restricted
deb http://apt.foobar.com/ubuntu bionic-updates main restricted
deb http://apt.foobar.com/ubuntu bionic universe
deb http://apt.foobar.com/ubuntu bionic-updates universe
deb http://apt.foobar.com/ubuntu bionic multiverse
deb http://apt.foobar.com/ubuntu bionic-updates multiverse
deb http://apt.foobar.com/ubuntu bionic-backports main restricted universe multiverse

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

Aaaand that’s it! EZ.