Tuesday, June 21, 2016

How to select the fastest apt mirror on Ubuntu Linux

https://linuxconfig.org/how-to-select-the-fastest-apt-mirror-on-ubuntu-linux

The following guide will provide you with some information on how to improve Ubuntu's repository download speed by selecting the closest, that is, possibly fastest mirror relative to your geographical location.

1. Country Code

The simplest approach is to make sure that your Ubuntu mirror defined within /etc/apt/sources.list includes a relevant country code appropriate to your location. For example, below you can find a official United States Ubuntu mirror as found in /etc/apt/sources.list:
deb http://us.archive.ubuntu.com/ubuntu/ xenial main restricted
If you are not located in United States simply overwrite the us country code with appropriate code of your country. That is, if your are located for example in Australia update your /etc/apt/sources.list file for all entries as:
deb http://au.archive.ubuntu.com/ubuntu/ xenial main restricted

2. Use mirror protocol

Using mirror protocol as part of your /etc/apt/sources.list entry will instruct apt command to fetch mirrors located within your country only. In order to use mirror protocol update all lines within /etc/apt/sources.list file from the usual eg.:
deb http://us.archive.ubuntu.com/ubuntu/ xenial main restricted
to:
deb mirror://mirrors.ubuntu.com/mirrors.txt xenial main restricted
Repeat the above for all relevant lines where appropriate. Alternatively, use sed command to automatically edit your /etc/apt/sources.list file. Update the below sed command where appropriate to fit your environment:
$ sudo sed -i -e 's/http:\/\/us.archive/mirror:\/\/mirrors/' -e 's/\/ubuntu\//\/mirrors.txt/' /etc/apt/sources.list

3. Manual apt mirror selection

The above solutions look easy and they might just work for you. However, the mirror selected by apt may not be the fastest as it can be burdened by high latency. In this case you may try to choose your mirror manually from the list of mirrors located within your country. Use wget command to retrieve the list. The below wget command will retrieve apt ubuntu mirrors related to your country. Example:
$ wget -qO - mirrors.ubuntu.com/mirrors.txt
http://mirror.netspace.net.au/pub/ubuntu/
http://mirror.internode.on.net/pub/ubuntu/ubuntu/
http://mirror.overthewire.com.au/ubuntu/
http://mirror.aarnet.edu.au/pub/ubuntu/archive/
http://mirror.tcc.wa.edu.au/ubuntu/
http://ubuntu.mirror.serversaustralia.com.au/ubuntu/
http://ftp.iinet.net.au/pub/ubuntu/
http://ubuntu.mirror.digitalpacific.com.au/archive/
http://mirror.waia.asn.au/ubuntu/
http://ubuntu.uberglobalmirror.com/archive/
http://mirror.as24220.net/pub/ubuntu/
http://mirror.as24220.net/pub/ubuntu-archive/
Based on your experience select the best mirror and alter your /etc/apt/sources.list apt configuration file appropriately.

4. Choosing the fastest mirror with netselect

This solution is preferred, as it guarantees the fastest mirror selection. For this we are going to use netselect command. The netselect package is not available within Ubuntu's standard repository by default, so we will need to borrow it from Debian stable repository:
$ sudo apt-get install wget
$ wget http://ftp.au.debian.org/debian/pool/main/n/netselect/netselect_0.3.ds1-26_amd64.deb
$ sudo dpkg -i netselect_0.3.ds1-26_amd64.deb
Once you have the netselect command available on your Ubuntu system use it to locate the fastest mirror based on the lowest icmp latency. The netselect output will be relative to your location. The below example output will show top 20 apt Ubuntu mirrors ( if available ):
$ sudo netselect -s 20 -t 40 $(wget -qO - mirrors.ubuntu.com/mirrors.txt)
   12 http://ubuntu.uberglobalmirror.com/archive/
   20 http://ubuntu.mirror.serversaustralia.com.au/ubuntu/
   21 http://ubuntu.mirror.digitalpacific.com.au/archive/
   38 http://mirror.aarnet.edu.au/pub/ubuntu/archive/
   39 http://mirror.overthewire.com.au/ubuntu/
   45 http://mirror.internode.on.net/pub/ubuntu/ubuntu/
  121 http://mirror.netspace.net.au/pub/ubuntu/
  148 http://mirror.waia.asn.au/ubuntu/
  152 http://mirror.as24220.net/pub/ubuntu-archive/
  162 http://mirror.tcc.wa.edu.au/ubuntu/
  664 http://archive.ubuntu.com/ubuntu/
  664 http://archive.ubuntu.com/ubuntu/
 3825 http://archive.ubuntu.com/ubuntu/
Only found 13 hosts out of 20 requested.
Alter manually your /etc/apt/sources.list file to reflect the above netselect results or use sed command, where the lower score number on the left represents a higher mirror transfer rate. Example:
$ sudo sed -i 's/http:\/\/us.archive.ubuntu.com\/ubuntu\//http:\/\/ubuntu.uberglobalmirror.com\/archive\//' /etc/apt/sources.list

5. Comparing results

The following are my apt-get update command results, while located within Australia:
US MIRROR ( http://us.archive.ubuntu.com/ubuntu ):
Fetched 23.1 MB in 20s (1148 kB/s) 

MIRROR protocol( mirror://mirrors.ubuntu.com/mirrors.txt):
Fetched 23.1 MB in 4min 45s (81.0 kB/s)

AU MIRROR ( http://au.archive.ubuntu.com/ubuntu ):
Fetched 23.1 MB in 12s (1788 kB/s)

NETSTAT Auto-Selected ( http://ubuntu.uberglobalmirror.com/archive ):
Fetched 23.1 MB in 6s (3544 kB/s)

No comments:

Post a Comment