Arch

How To Install and Use Reflector on Arch Linux

How to install and use Reflector on Arch Linux

On Arch Linux applications are installed in packages through package installers that are compatible with it. Mirrors are used to organize and manage these packages on Arch and other Linux distributions. Mirrors are primarily the servers distributed across different countries that host similar copies of all the packages for a specific Linux distribution. Further, to automate the process of fetching mirror lists, a Python-based utility named reflector.

Outline:

How To Install Reflector on Arch Linux

On Arch Linux, when a package installation command is executed via Pacman it fetches packages from the mirrors (servers that host software repositories). The reflector tool helps in finding the fastest and up-to-date mirrors for Arch Linux package repositories, to install the reflector tool on Arch execute:

sudo pacman -S reflector rsync curl

Here in the above command, I have also installed two utilities as well one is rsync which provides additional flexibility for system maintenance. The other one is curl which assists the reflector in fetching mirror data.

Once the installation is completed, you can verify it by executing:

sudo pacman -Qi reflector

How To Use Reflector on Arch Linux

A reflector in Arch serves various purposes which include fetching the mirror list, filtering and sorting mirrors based on given preferences, and overwriting the mirror list. Using a reflector makes it easy to maintain mirror lists and here are the tasks that can be performed by using a reflector on Arch:

1: List of Mirrors For Arch Linux

The mirror list is placed in the Arch default package installer directory, so instead of navigating to that directory simply execute the reflector command and it will list all the mirrors:

reflector

Since the mirror list is quite long, so multiple filters can be used to list the mirrors, for instance, if you want to list the mirrors that are new use the age option:

reflector --sort age

Similarly, to list down mirrors based on the download rate use the rate option, here verbose is used to make the output more explicit:

reflector --sort rate --verbose

Furthermore, to list the mirrors based on the protocols use the p flag and give your preferred protocol like https, http or rsync:

reflector -p <protocol>

Lastly, you can narrow down the list to only a set of the latest mirrors or servers using the l flag along with the number of mirrors you need:

reflector -l <number-of-mirrors>

2: Setting up a Desired Server on Arch

As mentioned above mirrors are distributed across the globe, so there are servers for different countries which can be set based on the user preference. However, before changing the server location it is necessary to first create a backup file for the mirror list, so for that navigate to pacman.d directory:

cd /etc/pacman.d/

Now create a copy for the exiting mirror list and name it with .bak extension:

sudo cp mirrorlist mirrorlist.bak

Now use the c flag for country, p for preferred protocol, sort for filtering the list and save flag for saving the new list in the mirror list:

sudo reflector -c Australia -c Austria -a 12 -p https -p http --sort rate --save /etc/pacman.d/mirrorlist

Here I have used the countries Australia and Austria just for demonstration, you can choose any other as well. Moreover, here I have sorted the list based on the download rate and limited the number of mirrors to 12:

After modifying the mirror list you can verify the changes by reading the mirror list file:

cat /etc/pacman.d/mirrorlist

Here, as in the image above, you can see that the mirror list is updated by executing the same command that was executed earlier.

Note: You might see warnings for downloads while setting up the mirror list according to your preferences, but there is nothing to worry about. This is because of the timeout for the server links, to avoid that you can increase the number of mirrors that is the higher the number the better the result.

3: Saving the Latest Mirrors Based on User Parameters

Mirror lists other than the country or location can also be updated, for instance, you can list a certain number of mirrors based on the rate and their recent update. To demonstrate, I have listed only the three latest mirrors and saved them in the mirror list file:

sudo reflector --verbose --sort rate -l 3 --save /etc/pacman.d/mirrorlist

Here, to get a better result by increasing the number of mirrors, I have listed only 3 for demonstration purposes

Once you have saved the desired mirror list on Arch then verify it by reading the mirror list file:

cat /etc/pacman.d/mirrorlist

As in the image above, it can be seen that the three mirrors are in the list updated by the command executed above.

4: Setting up Reflector Service

To keep the mirror list updated automatically you need to create a service for the reflector, which is already present but needs to be activated so navigate to the system directory in systemd:

cd /etc/systemd/system

Now create a reflector service file using any file editor on Arch Linux:

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

Now copy the code lines given below in the reflector service file:

[Unit]

Description=Pacman mirrorlist update

Wants=network-online.target

After=network-online.target

[Service]

Type=oneshot

ExecStart=/usr/bin/reflector -c Taiwan -c Thailand -a 12 -p https -p http --sort rate --save /etc/pacman.d/mirrorlist

[Install]

RequiredBy=multi-user.target

Here, the code is devised in three sections one is the unit which contains the information about what the unit does and specifies that the unit depends on network availability.

The service section comes with filtering options for mirrors, I have downloaded the 12 mirrors for Taiwan and Thailand sorted based on the download rate. You can write the command for saving the mirror list by sporting it out based on your preferences.

Now simply start the reflector service so that the mirror list is updated according to the given instructions:

sudo systemctl start reflector.service

Now verify the mirror list if it is updated according to the given filters:

cat /etc/pacman.d/mirrorlist

It is to be noted that whenever you make any changes in the reflector service file reload the daemon service so that these changes are applied successfully:

sudo systemctl daemon-reload

5: Setting a Reflector Timer on Arch

Using the reflector timer service on Arch the mirror list can be updated based on any specific time so for that create a time file:

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

Now paste the set of instructions given below in the timer file, Here I have set the time to 7 AM on every Monday, you can set the day and time as desired. Next, I set the random delay of 15 hours before the timer actually starts and afterward set it to be persistent across the system restart. Further, the last code line ensures that the timer will be started once the system is started:

[Unit]

Description=Reflector weekly timer

[Timer]

OnCalendar=Mon ⋆-⋆-⋆ 7:00:00

RandomizeDelaySec=15 h

Persistent=true

[Install]

WantedBy=timers.target

Now save the timer service file and then to make it active start the timer service:

sudo systemctl start reflector.timer

Further, if you need help regarding the use of a reflector on Arch consult its command help by executing:

reflector --help

Conclusion

Reflectors in Arch Linux are primarily used to update and organize the mirrors that are responsible for fetching the packages when installing any application via Pacman. Reflector on Arch Linux can be installed through its default package installer. It provides various options for sorting the mirrors which include age, download rate, protocols, country, and the newly updated.

Similar Posts