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

N0r*_*ert 6 updates update-manager package-management command-line apt

假设我们使用的是 Ubuntu 18.04 LTS (Bionic Beaver)。

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

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

采摘镜

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

注释/更新:

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

mat*_*ure 6

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

安装:

pip install apt-select
Run Code Online (Sandbox Code Playgroud)

或者:

pip3 install apt-select
Run Code Online (Sandbox Code Playgroud)

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

export PATH=$PATH:~/.local/bin/apt-select
Run Code Online (Sandbox Code Playgroud)

用法示例:

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

apt-select --country US
Run Code Online (Sandbox Code Playgroud)

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

apt-select -c -t 3 -m one-week-behind
Run Code Online (Sandbox Code Playgroud)

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

$ apt-select --country US -t 5 --choose
Run Code Online (Sandbox Code Playgroud)


N0r*_*ert 6

镜像服务器列表由 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
Run Code Online (Sandbox Code Playgroud)

功能:

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

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

支持的选项:

-r, --remote-host=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.
Run Code Online (Sandbox Code Playgroud)

-f, --find-current-mirror

Determine the main mirror that is currently configured in
/etc/apt/sources.list and report its URL on standard output.
Run Code Online (Sandbox Code Playgroud)

-b, --find-best-mirror

Discover available mirrors, rank them, select the best one and report its
URL on standard output.
Run Code Online (Sandbox Code Playgroud)

-l, --list-mirrors

List available (ranked) mirrors on the terminal in a human readable format.
Run Code Online (Sandbox Code Playgroud)

-c, --change-mirror=MIRROR_URL

Update /etc/apt/sources.list to use the given MIRROR_URL.
Run Code Online (Sandbox Code Playgroud)

-a, --auto-change-mirror

Discover available mirrors, rank the mirrors by connection speed and update
status and update /etc/apt/sources.list to use the best available mirror.
Run Code Online (Sandbox Code Playgroud)

-u, --update, --update-package-lists

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.
Run Code Online (Sandbox Code Playgroud)

-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.
Run Code Online (Sandbox Code Playgroud)

-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).
Run Code Online (Sandbox Code Playgroud)

-v, --verbose

Increase logging verbosity (can be repeated).
Run Code Online (Sandbox Code Playgroud)

-q, --安静

Decrease logging verbosity (can be repeated).
Run Code Online (Sandbox Code Playgroud)

-h, --help

Show this message and exit.
Run Code Online (Sandbox Code Playgroud)

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

sudo apt-mirror-updater --auto-change-mirror
Run Code Online (Sandbox Code Playgroud)

它还允许通过 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      |
...
Run Code Online (Sandbox Code Playgroud)

然后手动选择镜像:

sudo apt-mirror-updater -c "http://mirror.timeweb.ru/ubuntu"
Run Code Online (Sandbox Code Playgroud)