Restore Default Apt Repositories in sources.list From Command Line in Ubuntu?
Tags: ubuntu
Published: 2021-12-14 05:05:21

---
title: Restore Default Apt Repositories in sources.list From Command Line in Ubuntu?
published: true
date: 2021-11-28 11:33:11 UTC
tags: Ubuntu
canonical_url: https://stackallflow.com/ubuntu/restore-default-apt-repositories-in-sources-list-from-command-line-in-ubuntu/
---

<header>
<p><span style="font-size: 1.2em; color: initial;">apt</span><span style="font-size: 1.2em; color: initial;">command line</span><span style="font-size: 1.2em; color: initial;">repository</span></p>
</header>

I am looking for the best method to restore `/etc/apt/sources.list` to default from the command line.

Is there no way to reference the source code of the package which generates this file or something like that? I want a trusted and version-independent way of restoring this file.

### Solutions Ruled Out

Before you mark this as a duplicate, note that I have reviewed [this question](https://askubuntu.com/questions/124017/how-do-i-restore-the-default-repositories) already. It is only applicable if you have the Ubuntu GUI available. This question is specific to the command line.

I have also reviewed [this question](https://askubuntu.com/questions/191669/how-to-restore-the-default-repositories-in-12-04) where the accepted solution is someone pasted the contents of their `sources.list` file. This is not an appropriate way to restore the file as the intentions of the person providing the file contents cannot be verified and the file is subject to change with new releases.

I checked out the [generator at simplelinux.ch](http://repogen.simplylinux.ch/), but this is also not from Ubuntu so I do not plan to use it.

#### Accepted Answer

I’m not sure what you want, but:

- The parent repository is _always_ [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu) – everything else is a mirror of this. The other primary mirrors all have a domain of the form `<cctld>.archive.ubuntu.com`, where the two character short code is the [Country Code Top Level Domain](http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains#Country_code_top-level_domains). You can find additional mirrors with their status at [Launchpad](https://launchpad.net/ubuntu/+archivemirrors).
- The distribution codename is part of the channel (the third term). You can use [`lsb_release -sc`](http://manpages.ubuntu.com/lsb_release.1) to find that out, and it’s the first word of the release pretty name in lowercase (`trusty` for Trusty Tahr, for example).
- There are five channels: `<codename>`, `<codename>-security`, `<codename>-updates`, `<codename>-backports` and `<codename>-proposed`. The first is necessary as it is the base, the second is highly recommended as it contains security fixes, the fourth only if you need some package backported from a newer release and the fifth only if a developer asks you to enable it for testing a possible fix.
- There are four repository sections: `main`, `multiverse`, `universe` and `restricted` ([What’s the difference between multiverse, universe, restricted and main?](https://askubuntu.com/q/58364/158442))

So you can always create a safe `sources.list` which contains just:

```
deb http://archive.ubuntu.com/ubuntu <codename> main multiverse universe restricted
deb http://archive.ubuntu.com/ubuntu <codename>-security main multiverse universe restricted

```

If you want a command to do this:

```
printf 'deb http://archive.ubuntu.com/ubuntu %s main multiverse universe restrictedn' "$(lsb_release -sc)"{,-security} > /etc/apt/sources.list

```

Or, `lsb_release` isn’t available, use `/etc/os-release` from the `base-files` package:

```
printf 'deb http://archive.ubuntu.com/ubuntu %s main multiverse universe restrictedn' "$(. /etc/os-release; printf "%s" "$UBUNTU_CODENAME")"{,-security} > /etc/apt/sources.list

```

* * *

In addition to the Launchpad list, the list provided by the Software Sources program is  
from `/usr/share/python-apt/templates/Ubuntu.mirrors`, which is from the [`python-apt-common`](http://packages.ubuntu.com/trusty/python-apt-common) package. This package is only an indirect _Suggests_ dependency of [`apt`](http://packages.ubuntu.com/trusty/apt), so it may not be installed by default on a server.

The post [Restore Default Apt Repositories in sources.list From Command Line in Ubuntu?](https://stackallflow.com/ubuntu/restore-default-apt-repositories-in-sources-list-from-command-line-in-ubuntu/) appeared first on [Stack All Flow](https://stackallflow.com).