148

I have a Lucid Server (10.04) set up and I would like to change the mirror from US (or any other country) to the Main Ubuntu Mirror.

For example my two first entries in sources.list are:

deb http://us.archive.ubuntu.com/ubuntu/ lucid main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ lucid main restricted

In a Desktop environment I would select the main mirror like this:

Software Sources

But how do I do that from the terminal as I don't have a graphical environment installed!

Bruno Pereira
  • 73,643

4 Answers4

190

Open your sources.list file using your favorite text editor, e.g.

sudo nano /etc/apt/sources.list

Locate the text http://us.archive.ubuntu.com/ubuntu and replace it with http://archive.ubuntu.com/ubuntu.

Jakob
  • 10,579
  • I've figured this would do the trick but when I did as you suggested but it gave me a 404 error for the repository. I've checked again now and it seems to be okay. It seemed to be a problem with my network connection. Thank you very much for the answer. – Sorin-Mihai Oprea Feb 16 '12 at 11:10
  • is there a secure way like with the GUI. I mean, not replacing it manually and avoiding typos or whatever. – logoff Jan 10 '14 at 11:24
  • Maybe something like this: http://askubuntu.com/questions/20414/find-and-replace-text-within-a-file-using-commands @logoff – Luke Stanley Feb 28 '14 at 00:39
  • 2
    Also run apt-get update subsequently so that APT can update its packages. Initially I got an Package X has no installation candidate message because APT hasn't yet scanned the new repository for packages. – dutoitns Nov 22 '16 at 09:00
  • To use it in a script the following sed command can be used sed -E -i 's#http://[^\s]*archive\.ubuntu\.com/ubuntu#http://be.archive.ubuntu.com/ubuntu#g' /etc/apt/sources.list' /etc/apt/sources.list replace nl with your country code. – Mandy Schoep Sep 06 '19 at 09:55
  • Excellent. Worked like a charm! – srivenky Nov 20 '19 at 15:50
  • @MandyS for me that one didn't work... I had to use sed -E -i 's#http://[a-z]*archive\.ubuntu\.com/ubuntu#http://be.archive.ubuntu.com/ubuntu#g' /etc/apt/sources.list' (or without the be.-prefix/ a custom one, the problem was the prefix-regex) – polynomial_donut Feb 05 '20 at 12:53
  • Gosh thank you, finally found this. +1 – Leon Bohmann Jan 19 '21 at 20:56
89

This command should do the trick:

sudo sed -i 's|http://us.|http://|g' /etc/apt/sources.list

It will remove the 'us.' prefix in each of the addresses to convert them to addresses of the main server.

Of course replace 'us' by any other mirror you are using.

In depth explanation of command:

sed - stream editor for filtering and transforming text.

  • The -i argument is to edit a file in place.

  • Then 's|regexp|replacement|g', s specifying the search and replace command.

  • The g at the end being the argument to "globally" search.

  • Conclusion: replaces all occurrences of http://us. with http:// in the file /etc/apt/sources.list.

  • 1
    Try explaining the command a little more, what does it do? – Evandro Silva Dec 10 '14 at 13:41
  • 3
    To switch from us to de mirror do the following: sudo sed -i 's/http:\/\/us./http:\/\/de./g' /etc/apt/sources.list' – Christian Feb 17 '17 at 14:21
  • 1
    The method posted here is right, however the code posted in the top is wrong [code] "sudo sed -i 's/http://us./http:///g' /etc/apt/sources.list"[/code] where are the sed cmd posted at the bottom makes sense. – SAGAR Nair Mar 08 '18 at 23:56
  • 1
    Thanks a lot for the manual-text-editing-free one-liner. I went from a few kB/s download speed to 10 MB/s by using sudo sed -i 's|http://us.|http://ch.|g' /etc/apt/sources.list (I'm in Switzerland). I was working with an osboxes image and they are apparently quite misconfigured in that regard... – masterxilo Nov 14 '18 at 09:13
  • ok, this works good. I tried it on my ubuntu linode server. – Silver Moon Nov 16 '22 at 09:47
7

Correct sed usage to remove/change country code "us" from source.list to something else like "au", the command will be as follows:

sed -i 's/http:\/\/us./http:\/\/au./g' /etc/apt/sources.list

or just to remove "us" alone instead of changing it to something, use code below:

sed -i 's/http:\/\/in./http:\/\//g' /etc/apt/sources.list
SAGAR Nair
  • 1,385
4

With vim:

mv /etc/apt/sources.list{,.bak}   # you may want to make a backup
vim /etc/apt/source.list

Type : (you need to hold Shift) to enter command-ine mode, and then type:

%s/http:\/\/us\./http:\/\//g

Hit Enter

Hit Esc to be sure you've exited the command-line mode and entered normal mode. Now you can scroll around the file with keyboard move keys (if you want) to make sure all occurrences of us mirrors have been edited.

Finally, exit with saving by entering command-line mode (type :) and enter wq! to save and exit vim.

Fangxing
  • 271