如何从命令行切换 APT 镜像?

如何从命令行切换 APT 镜像?

我们假设我们正在使用 Ubuntu 18.04 LTS(Bionic Beaver)。

我知道通过 GUI 方式切换当前使用的 APT 镜像。

用户应该打开软件与更新software-properties-gtksoftware-properties-kde)并导航至Ubuntu 软件Ubuntu 软件)选项卡,然后选择镜像下载自列表:

挑选镜子

但是如何从命令行切换 APT 镜像?

说明/更新:

  1. 我需要一个无需直接编辑的解决方案/etc/apt/sources.list来防止拼写错误并自动选择正确的镜像。
  2. 我需要用一个简单的命令切换镜像,相当于从中选择一个下载自列表中software-properties-gtk(镜像地址在系统中的保存位置也很有趣)。
    3. 我创建了community.ubuntu.com 上的讨论和民意调查“Ubuntu 是否需要 software-properties-gtk / software-properties-kde 的控制台替代方案?”

答案1

镜像服务器列表由 Python 库检索(该get_server_list过程在中定义/usr/lib/python3/dist-packages/aptsources/distro.py并从中调用/usr/lib/python3/dist-packages/softwareproperties/gtk/SoftwarePropertiesGtk.py)。

解决方案是使用名为apt-mirror-updater. 可以从pip/安装pip3

sudo pip3 install apt-mirror-updater

功能:

用法:apt-mirror-updater [选项]

apt-mirror-updater 程序通过发现可用镜像、对可用镜像进行排名、在镜像之间自动切换以及强大的软件包列表更新,自动化了 Debian 和 Ubuntu 的 apt-get 镜像选择。

支持的选项:

-r,--远程主机 = SSH_ALIAS

Operate on a remote system instead of the local system. The SSH_ALIAS
argument gives the SSH alias of the remote host. It is assumed that the
remote account has root privileges or password-less sudo access.

-f, --查找当前镜像

Determine the main mirror that is currently configured in
/etc/apt/sources.list and report its URL on standard output.

-b, --find-best-mirror

Discover available mirrors, rank them, select the best one and report its
URL on standard output.

-l, --list-镜像

List available (ranked) mirrors on the terminal in a human readable format.

-c, --change-mirror=镜像网址

Update /etc/apt/sources.list to use the given MIRROR_URL.

-a,--自动更改镜像

Discover available mirrors, rank the mirrors by connection speed and update
status and update /etc/apt/sources.list to use the best available mirror.

-u、--update、--update 软件包列表

Update the package lists using `apt-get update', retrying on failure and
automatically switch to a different mirror when it looks like the current
mirror is being updated.

-x, --exclude=模式

Add a pattern to the mirror selection blacklist. PATTERN is expected to be
a shell pattern (containing wild cards like `?' and `*') that is matched
against the full URL of each mirror.

-m, --max=COUNT

Don't query more than COUNT mirrors for their connection status
(defaults to 50). If you give the number 0 no limit will be applied.

Because Ubuntu mirror discovery can report more than 300 mirrors it's
useful to limit the number of mirrors that are queried, otherwise the
ranking of mirrors will take a long time (because over 300 connections
need to be established).

-v,--详细

Increase logging verbosity (can be repeated).

-q,--安静

Decrease logging verbosity (can be repeated).

-h,--帮助

Show this message and exit.

因此它可以找到最佳镜像并将其应用于/etc/apt/sources.list

sudo apt-mirror-updater --auto-change-mirror

它还允许通过 URL 选择镜像并将其应用于/etc/apt/sources.list

$ apt-mirror-updater --list-mirrors
-----------------------------------------------------------------------------------------------------------------------
| Rank | Mirror URL                                        | Available? | Updating? | Last updated   | Bandwidth      |
-----------------------------------------------------------------------------------------------------------------------
|    1 | http://mirror.timeweb.ru/ubuntu                   | Yes        | No        | Up to date     | 6.49 KB/s      |
|    2 | http://no.archive.ubuntu.com/ubuntu               | Yes        | No        | Up to date     | 6.38 KB/s      |
|    3 | http://ftp.aso.ee/ubuntu                          | Yes        | No        | Up to date     | 5.62 KB/s      |
|    4 | http://mirror.plusserver.com/ubuntu/ubuntu        | Yes        | No        | Up to date     | 4.77 KB/s      |
|    5 | http://nl.archive.ubuntu.com/ubuntu               | Yes        | No        | Up to date     | 4.68 KB/s      |
...

然后手动选择镜像:

sudo apt-mirror-updater -c "http://mirror.timeweb.ru/ubuntu"

答案2

一些解决方案(在我的 Ubuntu 18.04.1 LTS 上测试):https://github.com/jblakeman/apt-select.git

安装:

pip install apt-select

或者:

pip3 install apt-select

将脚本添加到 PATH 以便从任何地方运行它(使其永久化):

export PATH=$PATH:~/.local/bin/apt-select

用法示例:

获取来自美国的顶级镜像,生成新的sources.list::

apt-select --country US

从前 3 个镜像中选择,包括上周最新更新的镜像::

apt-select -c -t 3 -m one-week-behind

从 5 个到您的机器延迟最低的美国镜像中进行选择:

$ apt-select --country US -t 5 --choose

答案3

不需要安装任何其他工具的一行程序......

APT_MIRROR=au
sed --in-place --regexp-extended "s/(\/\/)(archive\.ubuntu)/\1${APT_MIRROR}.\2/" /etc/apt/sources.list

相关内容