Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

There are two different working answers to this question below:

  1. Use apt-get's mirror: method
    This method asks the Ubuntu server for a list of mirrors near you based on your IP, and selects one of them. The easiest alternative, with the minor downside that sometimes the closest mirror may not be the fastest.
  2. Command-line foo using netselect
    Shows you how to use the netselect tool to find the fastest recently updated servers from you -- network-wise, not geographically. Use sed to replace mirrors in sources.list.

The other answers, including the accepted answer, are no longer valid (for Ubuntu 11.04 and newer) because they recommended Debian packages such as netselect-apt and apt-spy which do not work with Ubuntu.

Use sed to replace mirrors in sources.list

Since some sources use addition folders as part of their path it might be better to use the alternate separator syntax.

sudo sed -i '' /etc/apt/sources.list
share|improve this question
In regular expressions, the . character means any character. If you want it to match a ., you need to escape it with \, so us.archive[..] should be us\.archive[..] – Egil May 4 '11 at 7:13
Related:… – Jorge Castro Apr 6 '12 at 19:13
In my case I had to replace the # signs with slashes (/). Otherwise I got sed: -e expression #1, char 53: unterminated s' command`. – Ethan Leroy Oct 18 '13 at 21:18
@EthanLeroy same here with Ubuntu 12.04.3 – logoff Jan 10 '14 at 11:44
Should be slash not hash. – Matt H May 19 '14 at 22:20
up vote 9 down vote accepted
Pakket netselect-apt

    dapper (net): Choose the fastest Debian mirror with netselect 
    0.3.ds1-5: all
    hardy (net): Choose the fastest Debian mirror with netselect 
    0.3.ds1-11: all
Pakket apt-spy

    dapper (admin): writes a sources.list file based on bandwidth tests 
    3.1-14: amd64 i386 powerpc

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.

share|improve this answer
netselect-apt doesn't seem to be available in Ubuntu 12.04 – offby1 Nov 6 '13 at 23:22
correct: see here – Rinzwind Nov 7 '13 at 7:41
This is not the most upvoted, or the best answer any more, check next one – ntg Jun 10 '15 at 7:54

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.

apt-get now supports a 'mirror' method that will automatically select a good mirror based on your location. Putting:

deb mirror:// precise main restricted universe multiverse
deb mirror:// precise-updates main restricted universe multiverse
deb mirror:// precise-backports main restricted universe multiverse
deb mirror:// precise-security main restricted universe multiverse

on the top in your /etc/apt/sources.list file should be all that is needed to make it automatically pick a mirror for you based on your geographical location.

Lucid (10.04), Maverick (10.10), Natty (11.04), and Oneiric (11.10) users can replace precise with the appropriate name.

share|improve this answer
Great tip. Just note that after making the change you need to run sudo apt-get update before doing any apt-get install for it to use your closest mirror. – Simon East Jun 29 '13 at 17:03
Related: – Till Jul 18 '13 at 15:45
Nice tip, but unhelpful in my case. It works on geolocation, giving me the local server, which is waaaayy slower where I am. The network temporal distance is the important factor here, not spatial distance. – jarondl Jul 31 '13 at 8:24
in fact, for example here in Italy this way put in use a mirror that has slow downloading... – Pisu Jul 15 '15 at 7:11

Here's one way that will always work, using good old netselect and some grep 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)

    sudo netselect -v -s10 -t20 `wget -q -O- | grep -P -B8 "statusUP|statusSIX" | grep -o -P "(f|ht)tp.*\"" | tr '"\n' '  '`
  • netselect:

    1. -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 :)
    2. -sN controls how many mirrors you want at the end (e.g. top 10 mirrors)
    3. -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 -q -O-
    | grep -P -B8 "status(UP|SIX)" 
    | grep -o -P "(f|ht)tp.*\"" 
    | | tr '"\n' '  '
    1. wget pulls the latest mirror status from
    2. The first 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 URLs
    3. The second grep extracts these ftp/http URLs
    4. tr just converts linebreaks to spaces, since netselect wants its list of servers to test that way.
  • Here's a sample output from California, USA:

    • The "ranks" are an arbitrary metric; lower is usually better.
    • If you're wondering why the 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 as!
share|improve this answer
netselect picks mirrors that have low udp or icmp latency. It doesn't necessarily pick mirrors that can give more bandwidth. – Tobu Oct 13 '13 at 19:40

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.

share|improve this answer
That's brilliant, thanks! – Wouter Van Vliet Oct 28 '14 at 10:32
I want to let you know that I've made a debian package with your script that is ready to be used in a very easy and straightforward way: – Gabriel Mazetto Nov 3 '15 at 4:25

I developed a simple ping-based nodejs script that tests the servers listed on and returns the fastest one:

sudo npm install -g ffum

Please let me know if you find it useful or have any suggestions (=

share|improve this answer
ffum does not work: Connection error. – James Fu Jul 10 '13 at 8:48
It doesn't work: Empty output. – Juan Simón Aug 27 '13 at 1:06
git clone the repo and run node ffum – Michael Aug 7 '14 at 3:58
Awesome, works for me! I had a bug where it was looking for node instead of nodejs... also would be cool to have some verbose of each tested archive speed. – tweak2 Aug 27 '14 at 16:57

Oneliner that select best (by download speed) mirror based on for yours ip.

curl -s | xargs -n1 -I {} sh -c 'echo `curl -r 0-102400 -s -w %{speed_download} -o /dev/null {}/ls-lR.gz` {}' |sort -g -r |head -1| awk '{ print $2  }'
share|improve this answer

The easiest and efficient way to get the fastest mirror is to use the apt mirror:// source, see

share|improve this answer

I use the following to auto select mirrors (and disable deb-src)

sudo sed -i -e 's%' -e 's/^deb-src/#deb-src/' /etc/apt/sources.list
share|improve this answer

nice terminal program here:

# apt-get install netselect-apt

Available Options

stable|testing|unstable|experimental|woody|sarge|etch|sid Specify which distribution of Debian to use. By default stable is used.

-s, --sources
While generating OUTFILE include also deb-src lines to use with ‘‘apt-get source’’ to obtain Debian source packages.

-i, --infile INFILE
Use INFILE instead of mirrors_full for reading mirror list. The file must be in the same format as mirrors_full.

-o, --outfile OUTFILE
Use OUTFILE instead of sources.list.

-n, --nonfree
Include also non-free section while generating OUTFILE.

-f, --ftp
Use FTP mirrors instead of HTTP and generate OUTFILE accordingly.


If you want non-free repos use the following command

# netselect-apt -n
share|improve this answer
that program is for Debian, not Ubuntu. – Juan Simón Aug 27 '13 at 1:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.