I want to update my sources.list
file with the fastest server from the command line in a fresh Ubuntu Server install. I know this is trivially easy with the GUI, but there doesn't seem to be a simple way to do it from from the command line?
I want to update my sources.list
file with the fastest server from the command line in a fresh Ubuntu Server install. I know this is trivially easy with the GUI, but there doesn't seem to be a simple way to do it from from the command line?
You don't have to do any searching anymore - as ajmitch has explained, you can use
deb mirror
to have the best mirror picked for you automatically.Here's one way that will always work, using good old
netselect
and somegrep
magic:The terminal-addict's "find best server" hack!
Download and
dpkg -i
netselect
for your architecture from the Debian website. (it's about 125 KB, no dependencies)Find the fastest Ubuntu mirrors from your location, either up-to-date or at most six hours behind with this (I'll explain it below, sorry it doesn't split up nicely in Markdown)
netselect
:-v
makes it a little verbose -- you want to see progress dots and messages telling you different mirrors mapping to the same IP were merged :)-sN
controls how many mirrors you want at the end (e.g. top 10 mirrors)-tN
is how long each mirror is speed-tested (default is 10; the higher the number, the longer it takes but the more reliable the results.)This is the backquotes stuff (don't paste, just for explanation)
wget
pulls the latest mirror status from https://launchpad.net/ubuntu/+archivemirrors.grep
extracts mirrors that are up-to-date or six-hours behind, along with 8 lines of previous context which includes the actual ftp/http URLsgrep
extracts these ftp/http URLsHere's a sample output from California, USA:
The "ranks" are an arbitrary metric; lower is usually better.
If you're wondering why the kernel.org Sweden-EU mirror and an NZ mirror are in the top three from California, well, so am I ;-) The truth is that
netselect
doesn't always choose the most appropriate URL to display when multiple mirrors map to a single IP; number 3 is also known asnz.archive.ubuntu.com
!Oneliner that selects the best mirror (by download speed) based on mirrors.ubuntu.com for your ip:
Not included in newer Ubuntu due to secturity issues it seems: see: Bug report
But .. I normally just use ping to find out the speed of a connection to some location. Amount of hops and latency.
Here is a Python script I wrote that finds mirrors with the lowest TCP latency.
The script also provides bandwidth and status data taken from launchpad, and will generate a new
sources.list
file automatically or using a mirror chosen from a list.A usage example that lets you choose from 5 US mirrors with the lowest latency to your machine:
For the command line, you can use a Python tool called apt-smart
A usage example that lets you list ranked mirrors within your country (automatically detect):
With
-l
, or--list-mirrors
, you will get ( example output from Travis CI U.S. server ):Of course,
apt-smart
can also change your sources.list if you want to:With
-a
, or--auto-change-mirror
to discover available mirrors, rank the mirrors by connection speed and update status and update /etc/apt/sources.list to use the best available mirror.With
-c
, or--change-mirror MIRROR_URL
to update /etc/apt/sources.list to use the given MIRROR_URL.Compared with other tools:
apt-smart
automatically finds where you are so you don't need to specify the country when you travel abroad.apt-smart
does real HTTP download from each mirror to get more accurate results ( bandwidth & status ) and supports HTTP proxy, rather than usingping
and relying on launchpad 's inaccurate data.apt-smart
is being maintained, whereas most other tools leave issues unfix for a long time.You can easily install
apt-smart
viapip
, for detailed copy'n'paste install commands and usages please see Project Readme.Disclosure: I am the author of
apt-smart
, which is an improved fork ofapt-mirror-updater
.I developed a simple ping-based nodejs script that tests the servers listed on mirrors.ubuntu.com/mirrors.txt and returns the fastest one:
Please let me know if you find it useful or have any suggestions (=
I know this doesn't directly answer the OP's question, but there's a button in the desktop/GUI version of Ubuntu that finds the best mirror for you. It seemed to work pretty well, so I looked into it briefly, but didn't have time to follow up.
The reason I bring it up is because I think it would be pretty straight forward and usable to make it into a command line utility.
If anyone is interested, the test seems to be located in:
Again, that's about as far as I got, but I figured I'd leave this here in case anyone wanted it. I'll probably pick back up on it when I have a little more time.
Command That Finds Fast Mirrors
On Ubuntu 18.04 I got good results by running
That prints a list of mirrors organized by "time" (not explained), and then I used one of the mirrors it ranked highest.
More Details
For me, it was useful to test a few of the top results output by that command by setting them as my mirror in
/etc/apt/sources.list
and then doingto see how long it took to download the package list from that mirror. I tested the top three suggestions and they were all fast, but one of them was twice as fast as the other two in the
time sudo apt update
test.Here's an example output from
python /usr/lib/python3/dist-packages/softwareproperties/MirrorTest.py
:The easiest and efficient way to get the fastest mirror is to use the apt
mirror://
source, seehttps://mvogt.wordpress.com/2011/03/21/the-apt-mirror-method/