Retrieve Latest Mirror List Using Reflector In Arch Linux

Install Pacaur
Image credit: https://www.flickr.com/photos/66346231@N06/

Keeping most up-to-date mirrorlist in your Arch Linux is one of the recommended thing. This is because, If you have added updated mirrorlist, you could easily avoid slow download rate, and timed-out error messages while installing, and updating packages. As you see in the /etc/pacman.d/mirrorlist file in Arch Linux, you might have noticed that there are only one mirror server per country. However, there should be more than just one server per country, but the mirror list file will display only one. So, how do you add or retrieve the latest and updated mirror list of your choice in Arch Linux? No worries. This is where Reflector comes in help.

Reflector is a simple, yet useful handy utility that checks and retrieves the most up-to-date mirrorlist sorted by speed from Mirror Status page, and saves them in /etc/pacman.d/mirrorlist file in your Arch Linux system. In this brief tutorial, let us see how to retrieve latest mirrorlist using Reflector in Arch Linux. This tool might work on Arch Linux derivatives such as Antergos and Manjaro Linux.

Retrieve latest mirror list using Reflector in Arch Linux

Install Reflector

To install Reflector in Arch Linux, run the following command from your Terminal:

sudo pacman -S reflector rsync curl

The above command will install Reflector along with its dependencies rsync and curl packages.

How to use Reflector ?

Before running Reflector, you must backup your default mirrorlist file. Because, Reflector will overwrite it.

To backup the current mirrorlist, run:

sudo cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak

Now, let us start to retrieve top five mirrors of India according to the download rate, and save them to the mirrorlist file.

sudo reflector --verbose --country 'India' -l 5 --sort rate --save /etc/pacman.d/mirrorlist

The following is a brief summary of what these flags in the above command do.

  • –verbose : prints more information than normal
  • –country India : my prefered mirror location
  • –l 5 : retrieves top five mirrors of India
  • –sort rate – sorts by download rate
  • –save – overwrites my previous mirrorlist file

If want to use only http instead of ftp mirrors, use ‘-p http’ flag in the above command.

Sample output:

rating http://ftp.iitm.ac.in/archlinux/
rating http://mirror.cse.iitk.ac.in/archlinux/
Server Rate Time
http://mirror.cse.iitk.ac.in/archlinux/ 84.09 KiB/s 1.46 s
http://ftp.iitm.ac.in/archlinux/ 37.23 KiB/s 3.29 s

The above command will filter the first five mirrors, sort them by download rate and overwrite the file /etc/pacman.d/mirrorlist. Unfortunately, there are only two mirrors available for India.

Let us verify the updated mirrorlist using command:

cat /etc/pacman.d/mirrorlist

Sample output:

################################################################################
################# Arch Linux mirrorlist generated by Reflector #################
################################################################################

# With: reflector --verbose --country India -l 5 --sort rate --save /etc/pacman.d/mirrorlist
# When: 2016-03-19 11:27:55 UTC
# From: https://www.archlinux.org/mirrors/status/json/
# Retrieved: 2016-03-19 11:27:51 UTC
# Last Check: 2016-03-19 11:25:34 UTC

Server = http://mirror.cse.iitk.ac.in/archlinux/$repo/os/$arch
Server = http://ftp.iitm.ac.in/archlinux/$repo/os/$arch

Now, you can install, update, and upgrade packages from the most updated mirrors.

sudo pacman -Syu
sudo pacman -S <package_name>

Also, to filter 50 most recently synchronized HTTP servers sorted by download rate, run the following command:

sudo reflector --verbose -l 50 -p http --sort rate --save /etc/pacman.d/mirrorlist

To get all country-sorted list run the following command:

sudo curl -o /etc/pacman.d/mirrorlist https://www.archlinux.org/mirrorlist/all/

After retrieving the country-sorted mirrors, edit /etc/pacman.d/mirrorlist file,

sudo nano /etc/pacman.d/mirrorlist

Uncomment any mirror URL of your choice to enable it.

For example, I am going to enable the mirrors from India. So, I removed the (#) symbol in-front of each URL.

[...]
India
Server = http://mirror.cse.iitk.ac.in/archlinux/$repo/os/$arch
Server = http://ftp.iitm.ac.in/archlinux/$repo/os/$arch
[...]

Save and close the file. Update repository lists using command:

sudo pacman -Syu

That’s it. You now got the latest up-to-date mirrorlist for your Arch Linux system using Reflector tool. Start using the updated mirrors.

Update mirrorlist automatically on boot

Create a new file called reflector.service file in /etc/systemd/system/ directory:

sudo nano /etc/systemd/system/reflector.service

Add the following lines:

[Unit]
Description=Pacman mirrorlist update
Requires=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/bin/reflector --protocol http --latest 30 --number 20 --sort rate --save /etc/pacman.d/mirrorlist

[Install]
RequiredBy=multi-user.target

Finally, enable Reflector service on boot and start it using commands:

sudo systemctl enable reflector
sudo systemctl start reflector

You can also create a pacman hook that will run reflector and remove the .pacnew file created every time pacman-mirrorlist gets an upgrade.

To do so, create file called mirrorupgrade.hook:

sudo mkdir /etc/pacman.d/hooks/

sudo nano /etc/pacman.d/hooks/mirrorupgrade.hook

Add the following lines:

[Trigger]
Operation = Upgrade
Type = Package
Target = pacman-mirrorlist

[Action]
Description = Updating pacman-mirrorlist with reflector and removing pacnew...
When = PostTransaction
Depends = reflector
Exec = /usr/bin/env sh -c "reflector --country 'India' --latest 200 --age 24 --sort rate --save /etc/pacman.d/mirrorlist; if [[ -f /etc/pacman.d/mirrorlist.pacnew ]]; then rm /etc/pacman.d/mirrorlist.pacnew; fi"

Replace the country and other arguments that matches with your own values.

That’s all for now. I hope this helps you sometime. If you find this guide useful, please share it on your social networks, and support OSTechNix.

Thanks for reading. Cheers!

Reference:

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...

  • Александр Костик

    thank you very much:)
    I appreciated your work

  • Jane Doe

    Brilliant tip!
    Especially the hook!
    Thank you very much!

  • Vegan Garo

    Thanks!