什么是镜像源?

pacman自不必说,这是Arch Linux的默认包管理器。而镜像源则是为加速下载而在各地搭建的服务器,由于众所周知的原因,很多时候我们下载软件包不能从国外的源网站直接下载,而需要从国内的镜像网站下载。国内常见的镜像网站主要有科研机构维护镜像站与企业维护镜像站,比如: - 科研机构维护镜像站 - 南京大学镜像站 - 清华大学tuna镜像站 - 上海交通大学镜像站 - 企业维护镜像站 - 阿里巴巴开源镜像站 - 腾讯软件源

什么是reflector.service

大家可能在Arch安装教程中看到过禁用reflector.service的步骤。简而言之,这是一项定期向pacman的镜像源列表配置里自动写入配置的服务。Arch中文维基上的详细描述是这样的: > Reflector 附带一个 reflector.service。这个服务会用 /etc/xdg/reflector/reflector.conf 中指定的参数运行 Reflector。此文件中的默认选项是一个很好的起点和示例。 > > 例如,从法国和德国的镜像中筛选出 5 个最新的并且支持 HTTPS 的镜像,然后将结果覆写到 /etc/pacman.d/mirrorlist 文件内;使用: > >

1
2
3
4
5
6
/etc/xdg/reflector/reflector.conf
------------------------------------------------------------
--save /etc/pacman.d/mirrorlist
--country France,Germany
--protocol https
--latest 5
> 启用 reflector.service 服务可以在引导时运行 Reflector 脚本。要立即运行,启动该服务。

依葫芦画瓢

既然在中国,我们很简单地修改一下国家,就可以自动地添加中国的镜像源了。注意编辑mirrorlist需要root权限。

1
$ sudo vim /etc/xdg/reflector/reflector.conf
1
2
3
4
5
6
/etc/xdg/reflector/reflector.conf
----------------------------------------------------------
--save /etc/pacman.d/mirrorlist
--country China
--protocol https
--latest 5

照猫画虎

有了这些基本的功能,我们能否再做出一些小改进呢?

查看reflector的help message:

1
$ reflector help
这里的help message比较长,就不全放出来了,其中有几个我觉得比较有意思的:

1
2
3
4
5
6
--sort {age,rate,country,score,delay}
Sort the mirrorlist. "age": last server
synchronization; "rate": download rate; "country":
country name, either alphabetically or in the order
given by the --country option; "score": MirrorStatus
score; "delay": MirrorStatus delay.
1
2
3
4
5
6
-i, --include <regex>
Include servers that match <regex>, where <regex> is a
Python regular express.
-x, --exclude <regex>
Exclude servers that match <regex>, where <regex> is a
Python regular express.

由于我是校园网环境,用科研机构维护镜像站相对比较快,于是我选择了如下配置:

1
$ sudo vim /etc/xdg/reflector/reflector.conf
1
2
3
4
5
6
7
8
/etc/xdg/reflector/reflector.conf
----------------------------------------------------------
--save /etc/pacman.d/mirrorlist
--country China
--protocol https
--latest 5
--sort score
--include edu.cn

更进一步?

至此,虽然已经能自动更新教育网镜像站,但是我还想固定一个我自己学校的镜像站,以表达对yaoge123老师的支持。但是如果用--include进行指定的话,就会只剩下这一个镜像站,稳定性又会收到影响。我希望能在固定一个镜像源的同时,又不损失reflector自动获取的镜像源。于是我编写了一个小脚本,向mirrorlist中写入一个固定的Server。

1
$ vim ~/scripts/manual-reflector-update.sh
1
2
3
4
5
6
7
~/scripts/manual-reflector-update.sh
------------------------------------
sudo systemctl start reflector.service

echo -e '################################################################################\n############## Arch Linux mirrorlist generated manually by Harry ###############\n################################################################################\n\nServer=https://mirrors.nju.edu.cn/archlinux/$repo/os/$arch\n\n' | \
cat - /etc/pacman.d/mirrorlist | \
sudo tee /etc/pacman.d/mirrorlist > /home/Harry/logs/manual-reflector-update.log

首先用systemctl调用reflector以更新mirrorlist,然后再手动向mirrorlist的最前端写入一个指定的Server。 然后还要配置一下reflector.conf

1
$ sudo vim /etc/xdg/reflector/reflector.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Reflector configuration file for the systemd service.
#
# Empty lines and lines beginning with "#" are ignored. All other lines should
# contain valid reflector command-line arguments. The lines are parsed with
# Python's shlex modules so standard shell syntax should work. All arguments are
# collected into a single argument list.
#
# See "reflector --help" for details.

# Recommended Options

# Set the output path where the mirrorlist will be saved (--save).
--save /etc/pacman.d/mirrorlist

# Select the transfer protocol (--protocol).
--protocol https

# Select the country (--country).
# Consult the list of available countries with "reflector --list-countries" and
# select the countries nearest to you or the ones that you trust. For example:
--country China

# Use only the most recently synchronized mirrors (--latest).
--latest 5

# Sort the mirrors by synchronization time (--sort).
--sort score

# Include a custom re
# --include edu.cn
# Exclude a custom re
--exclude nju.edu.cn
这里保留了一点系统原本自动生成的注释部分,主要的修改在最后几行。

最后生成的mirrorlist如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
################################################################################
############## Arch Linux mirrorlist generated manually by Harry ###############
################################################################################

Server=https://mirrors.nju.edu.cn/archlinux/$repo/os/$arch


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

# With: reflector @/etc/xdg/reflector/reflector.conf
# When: 2025-09-24 10:40:33 UTC
# From: https://archlinux.org/mirrors/status/json/
# Retrieved: 2025-09-24 10:38:21 UTC
# Last Check: 2025-09-24 10:16:40 UTC

Server = https://mirrors.ustc.edu.cn/archlinux/$repo/os/$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch
Server = https://mirrors.jlu.edu.cn/archlinux/$repo/os/$arch
Server = https://mirrors.xjtu.edu.cn/archlinux/$repo/os/$arch
Server = https://mirrors.neusoft.edu.cn/archlinux/$repo/os/$arch