163

I want a list of repositories in sources.list, plus those in sources.list.d/.

Can I get this list in a form suitable for setting up another host so it watches the same repositories?

Additionally, how do I determine which repository is the source of a package, either installed or available?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ktenney
  • 3,945
  • 3
  • 19
  • 12

5 Answers5

209

It seems the closest is:

apt-cache policy
Ry-
  • 218,210
  • 55
  • 464
  • 476
ktenney
  • 3,945
  • 3
  • 19
  • 12
60

As far as I know, you can't ask apt for what their current sources are. However, you can do what you want using shell tools.

Getting a list of repositories:

grep -h ^deb /etc/apt/sources.list /etc/apt/sources.list.d/* >> current.repos.list

Applying the list:

apt-add-repository << current.repos.list

Regarding getting the repository from a package (installed or available), this will do the trick:

apt-cache policy package_name | grep -m1 http | awk '{ print $2 " " $3 }'

However, that will show you the repository of the latest version available of that package, and you may have more repositories for the same package with older versions. Remove all the grep/awk stuff if you want to see the full list.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Valor
  • 1,305
  • 8
  • 13
  • 14
    simple full list `apt-cache policy |grep http |awk '{print $2 $3}' |sort -u` – shadowbq Apr 02 '15 at 13:04
  • 3
    missing a space `apt-cache policy |grep http |awk '{print $2 " " $3}' |sort -u` – shadowbq Sep 05 '19 at 14:15
  • 2
    In the command line with grep: `/etc/apt/sources.list.d/*` should be changed to `/etc/apt/sources.list.d/*.list`. As to the reason why, see https://askubuntu.com/questions/82825/do-files-at-etc-apt-sources-list-d-need-to-have-an-extension-list – Erik Sjölund May 09 '20 at 05:54
  • The OP has left the building, so it is up to us to make the corrections. – Peter Mortensen Jan 29 '21 at 21:24
  • These are all great. Thanks... One thing, though... why pipe grep to awk? `... | grep anything | awk '{print}'` can be done thusly ```... | awk '/anything/ {print}'``` -- it's like catting a file to pipe to grep. – JayRugMan Jul 01 '22 at 15:43
20

Try this:

cat /etc/apt/sources.list
Ry-
  • 218,210
  • 55
  • 464
  • 476
radri
  • 537
  • 3
  • 7
14

It's not a format suitable for blindly copying to another machine, but users who wish to work out whether they've added a repository yet or not (like I did), you can just do:

sudo apt update

When apt is updating, it outputs a list of repositories it fetches. It seems obvious, but I've just realised what the GET URLs are that it spits out.

The following awk-based expression could be used to generate a sources.list file:

 cat /tmp/apt-update.txt | awk '/http/ { gsub("/", " ", $3); gsub("^\s\*$", "main", $3); printf("deb "); if($4 ~ "^[a-z0-9]$") printf("[arch=" $4 "] "); print($2 " " $3) }' | sort | uniq

Alternatively, as other answers suggest, you could just cat all the pre-existing sources like this:

cat /etc/apt/sources.list /etc/apt/sources.list.d/*

Since the disabled repositories are commented out with hash, this should work as intended.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
starbeamrainbowlabs
  • 5,692
  • 8
  • 42
  • 73
-7

All I needed was:

cd /etc/apt
nano source.list
deb http://http.kali.org/kali kali-rolling main non-free contrib
deb-src http://http.kali.org/kali kali-rolling main non-free contrib
apt upgrade && update

Source: Kali Network Repositories (/etc/apt/sources.list)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Crys
  • 7