114

I was installing elasticsearch following this guide, but elasticsearch is not really the part of this question.

In the first step, I need to add the key:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

and got the following message:

Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).

The installation process was fine, but since it's deprecated, I'm looking for the new usage that replaces apt-key. (I have no problem installing the package.) From man apt-key I saw

apt-key(8) will last be available in Debian 11 and Ubuntu 22.04.

...

Binary keyring files intended to be used with any apt version should therefore always be created with gpg --export.

but it didn't say the alternative to apt-key add. I tried

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --export

but didn't work. So what do I use after the pipe of wget when apt-key is removed?

Braiam
  • 1
  • 11
  • 47
  • 78
dibery
  • 2,760
  • 4
  • 16
  • 25
  • 1
    See [this](https://askubuntu.com/questions/1286545/what-commands-exactly-should-replace-the-deprecated-apt-key) – Raptor Aug 31 '21 at 04:23

11 Answers11

115

Adding a key to /etc/apt/trusted.gpg.d is insecure because it adds the key for all repositories. This is exactly why apt-key had to be deprecated.

Short version

Do similar to what Signal does. If you want to use the key at https://example.com/EXAMPLE.gpg for a repository listed in /etc/apt/sources.list.d/EXAMPLE.list, use:

sudo mkdir -p /etc/apt/keyrings/
wget -O- https://example.com/EXAMPLE.gpg |
    gpg --dearmor |
    sudo tee /etc/apt/keyrings/EXAMPLE.gpg > /dev/null

echo "deb [signed-by=/etc/apt/keyrings/EXAMPLE.gpg] https://example.com/apt stable main" |
    sudo tee /etc/apt/sources.list.d/EXAMPLE.list

# Optional (you can find the email address / ID using `apt-key list`)
sudo apt-key del support@example.com

Long version

While the deprecation notice recommends adding the key to /etc/apt/trusted.gpg.d, this is an insecure solution. To quote this article from Linux Uprising:

The reason for this change is that when adding an OpenPGP key that's used to sign an APT repository to /etc/apt/trusted.gpg or /etc/apt/trusted.gpg.d, the key is unconditionally trusted by APT on all other repositories configured on the system that don't have a signed-by (see below) option, even the official Debian / Ubuntu repositories. As a result, any unofficial APT repository which has its signing key added to /etc/apt/trusted.gpg or /etc/apt/trusted.gpg.d can replace any package on the system. So this change was made for security reasons (your security).

The proper solution is explained in that Linux Uprising article and on the Debian Wiki: Store the key in /etc/apt/keyrings/ (or /usr/share/keyrings/ if you're the package maintainer), and then reference the key in the apt source list.

Therefore, the appropriate method is as follows:

  1. Download the key from https://example.com/EXAMPLE.gpg and store it in /etc/apt/keyrings/EXAMPLE.gpg. The Debian wiki explains that you should dearmor the key (i.e. convert it from base64 to binary) for compatibility with older software. The > /dev/null simply stops the binary key from being displayed in your terminal.
    wget -O- https://example.com/EXAMPLE.gpg |
        gpg --dearmor |
        sudo tee /etc/apt/keyrings/EXAMPLE.gpg > /dev/null
    
    Optionally, you can verify that the file you downloaded is indeed a PGP key by running file /etc/apt/keyrings/EXAMPLE.gpg and inspecting the output.
  2. Add the key to the source file that is used by the repository. Find the appropriate file in /etc/apt/sources.list.d/ and edit it so that it links to the keyring you just added. If the file doesn't exist, you can make one. In the end, it should look something like this:
    deb [signed-by=/etc/apt/keyrings/EXAMPLE.gpg] https://example.com/apt stable main
    
  3. Remove the key from apt-key, if it was added before. Run sudo apt-key list to list all the keys, and find the one that was previously added. Using the key's email address or fingerprint, run sudo apt-key del support@example.com.

Using the newer DEB822 format

In step 2, instead of using the one-line format for sources in sources.list.d, you can also use the newer multi-line format called DEB822. This format is easier to read for humans and computers, and has been available in apt since 2015. Debian and Ubuntu plan to use DEB822 as the default format starting late 2023. Repolib's documentation has a nice comparison and covers the motivation behind the new format.. Note that some external tools that parse the source files themselves instead of wrapping around apt do not fully support this format yet.

To switch to this format, let's say you have the following one-line format source file /etc/apt/sources.list.d/example.list:

deb [signed-by=/etc/apt/keyrings/EXAMPLE.gpg] https://example.com/apt stable main

Comment out this line, and create a new file, /etc/apt/sources.list.d/example.sources, containing:

Types: deb
URIs: https://example.com/apt
Suites: stable
Components: main
Signed-By: /etc/apt/keyrings/EXAMPLE.gpg

Run sudo apt update, and if you see example.com/apt correctly being updated, you can remove the old /etc/apt/sources.list.d/example.list.

Additional resources

FWDekker
  • 1,823
  • 2
  • 20
  • 26
  • 33
    The Debian developer who deprecated `apt-key` WITHOUT replacing it with an equally user-friendly and quick command for this task, made NSA & FSB a HUGE favor...! – ankostis Jun 01 '22 at 21:56
  • 1
    NP, since I answered here I've come up with a complete automated process for myself, I shared it [here](https://askubuntu.com/a/1412554/720005) – Lockszmith Jun 15 '22 at 14:13
  • 1
    `/usr/share/keyrings` is for package maintainers, use [`/etc/apt/keyrings`](https://manpages.ubuntu.com/manpages/jammy/en/man5/sources.list.5.html). You can also check out [this answer](https://askubuntu.com/a/1417382/186999). – x-yuri Jul 23 '22 at 00:17
  • 2
    @ankostis What could be more user-friendly and quick than to just copy a file? – x-yuri Jul 23 '22 at 00:18
  • 4
    @x-yuri it's not just a file copy, it additionally needs a file-edit to reference that new file, many opportunities for error, leading people to simply ignore warnings - unacceptable stance for such a sensitive issue – ankostis Jul 24 '22 at 10:09
  • @ankostis What was really deprecated is [not `apt-key`](https://askubuntu.com/a/1307181/186999), but adding keys unconditionally trusted by `apt`. You could alternatively put the file into `/etc/apt/trusted.gpg.d`, and that was better than using `apt-key add`, because that way it was easier to see which keys were added, and generally a simpler operation. (FYI `signed-by` was [supported](https://askubuntu.com/a/1417382/186999) (long?) before Debian Stretch.) So the difference really is the `[signed-by=...]` part. Which `apt-key` has nothing to do with... – x-yuri Jul 24 '22 at 11:36
  • 1
    ...For people who prefer to understand what they're doing, that's not a problem. For those who blindly follow the manuals... it's a matter of fixing the manual ([example](https://docs.docker.com/engine/install/debian/)). Now, what exactly do you suggest? Make `apt-key add` add files to `/etc/apt/keyrings` (which is a breaking and rather unexpected change), provide a wrapper script that adds lines to `sources.list`, and fix the manuals? – x-yuri Jul 24 '22 at 11:37
  • 6
    I follow the history of how things came to be, but from a user POV, the succinct `apt-key` CRUD CLI has been replaced by a loose set of recommendations, and just that. – ankostis Jul 25 '22 at 12:56
  • 2
    @ankostis MX Linux has a "MX Fix GPG keys" that fixes it automatically. And MX Linux is the most popular Debian distro (maybe for reasons such as this) – UTF_or_Death Nov 27 '22 at 14:03
  • 1
    This is great explanation, and it even fixes the Google Cloud SDK key, which is badly documented in the very own Google docs: https://cloud.google.com/sdk/docs/install The `gpg --dearmor` step was critical to make it work in Ubuntu 22.04. Thank you! – José L. Patiño Jan 27 '23 at 18:25
18

!!Deprecated & insecure!!

answer found here : https://suay.site/?p=526

in short :

retrieve the key and add the key :

curl -s URL | sudo gpg --no-default-keyring --keyring gnupg-ring:/etc/apt/trusted.gpg.d/NAME.gpg --import

authorize the user _apt :

sudo chown _apt /etc/apt/trusted.gpg.d/NAME.gpg
ankostis
  • 8,579
  • 3
  • 47
  • 61
936940
  • 341
  • 2
  • 7
  • 6
    alternatively, instead of `chown`-ing, the referenced article suggests to update the file permissions from 600 to 644, just making the file readable for everyone, as it's a public key anyway: `sudo chmod 644 /etc/apt/trusted.gpg.d/NAME.gpg` – Peter Sep 13 '21 at 08:00
  • 1
    You can avoid saving an additional file by: `curl -s URL | sudo gpg ...`. Also, you can make the script copy-paste-able by parameterizing `NAME`. – dibery Sep 29 '21 at 12:20
  • 3
    This is insecure: The key will be trusted for all repositories, even your distro's own repositories. This is exactly why apt-key had to be deprecated. [I have explained the method recommended by Debian and used by Signal in my answer.](https://stackoverflow.com/a/71384057/) – FWDekker Mar 07 '22 at 16:21
  • 3
    @IvanHanák Please be aware that while this answer works, it is recommended against by the Debian manual (amongst others), because it will trust that key for ALL repositories, not just the repository you're adding here. – FWDekker Oct 10 '22 at 09:52
  • if curl fails can try ***wget -qO - URL*** | gpg ...... – bortunac Jan 31 '23 at 11:50
6

As mentioned in current accepted answer, adding a key to /etc/apt/trusted.gpg.d is insecure because it adds the key for all repositories. This is why apt-key is giving this warning.

You can use a simpler solution like following:

curl -fsSL https://example.com/EXAMPLE.gpg | sudo gpg --dearmor -o /usr/share/keyrings/EXAMPLE.gpg

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/EXAMPLE.gpg] \
 https://example.com/apt stable main" \
| sudo tee -a /etc/apt/sources.list.d/EXAMPLE.list > /dev/null

sudo apt update
sudo apt install <package-name>
Vamsi Nerella
  • 770
  • 6
  • 8
  • 1
    Instead of `sudo tee ... >/dev/null` you can do `sudo gpg --dearmor -o /usr/share/keyrings/EXAMPLE.gpg` (with or without `--yes` for auto-overwrite). See my other [response](https://stackoverflow.com/questions/68992799/warning-apt-key-is-deprecated-manage-keyring-files-in-trusted-gpg-d-instead/71384057?noredirect=1#comment128300822_71384057) to a different answer. – Lockszmith Jun 15 '22 at 14:17
  • 1
    @Lockszmith I have modified the solution as per your suggestion after testing it. – Vamsi Nerella Jun 18 '22 at 14:28
3

From man apt-key (Ubuntu 22.04)

If your existing use of apt-key add looks like this:

wget -qO- https://myrepo.example/myrepo.asc | sudo apt-key add -

Then you can directly replace this with (though note the recommendation below):

wget -qO- https://myrepo.example/myrepo.asc | sudo tee /etc/apt/trusted.gpg.d/myrepo.asc

guettli
  • 25,042
  • 81
  • 346
  • 663
2

MX Linux has a utility script called "MX Fix GPG keys" that takes care of this. Since it's just a bash script it most likely works fine with any other Debian based distro.

It's here https://github.com/MX-Linux/checkaptgpg

UTF_or_Death
  • 864
  • 11
  • 19
0

I got his warning when trying to install nodejs and npm in Ubuntu 20.04

To be more precise:

Instead of this:

curl -sSL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \

Use this:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarn.gpg >/dev/null \
echo "deb [signed-by=/usr/share/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \

So the full installtion script looked like this:

apt-get install -y nodejs \
apt-get install -y npm gnupg2 \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarn.gpg >/dev/null \
echo "deb [signed-by=/usr/share/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
0

Experienced this error recently while trying to install Jenkins on my EC2 instance. However, I was able to resolve it by following the steps below:

  1. Add a repository key to your system by running:

"wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key |sudo gpg --dearmor -o /usr/share/keyrings/jenkins.gpg"

You may have to replace jenkins with the package/software you want to install.

  1. Attach the Debian package repo address to the server's sources.list by running:

"sudo sh -c 'echo deb [signed-by=/usr/share/keyrings/jenkins.gpg] http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'"

  1. Run:

"apt update"

So apt will use the newly created repo.

  1. Go on to attempt/reattempt your installation.

Hope this helps :). Source: https://www.digitalocean.com/community/tutorials/how-to-install-jenkins-on-ubuntu-22-04

folaRin
  • 109
  • 8
  • Thank you for your sharing! It'd be greater to format the commands in two ` for a nicer format. – dibery Feb 06 '23 at 02:46
0

Another sample snippet, resolving the issue using updated deb822 format:

{ echo 'Types: deb'
  echo 'URIs: https://dl.k6.io/deb'
  echo 'Suites: stable'
  echo 'Components: main'
  echo 'Signed-By:'
  set -eo pipefail
  KEY=C5AD17C747E3415A3642D57D77C6C491D6AC1D69
  curl -fsSL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x$KEY" \
    | sed -e 's/^$/./g;s/^/ /g' 
} | sudo tee /etc/apt/sources.list.d/k6.sources

sudo apt-get update && sudo apt-get install k6

In this case, I'm installing k6.io CLI on Ubuntu 22.04 LTS. Adapt as you see fit.

Notice the .sources — not .list!

The benefit of deb822 is that the package-signing pubkey gets put inline in the sources-file (and validates only this repo's packages — which is more secure than trusting it with all other repos).
Being inline in the file saves another | sudo tee hoop:

Types: deb
URIs: https://dl.k6.io/deb
Suites: stable
Components: main
Signed-By:
 -----BEGIN PGP PUBLIC KEY BLOCK-----
 Version: Hockeypuck 2.1.0-189-g15ebf24
 Comment: Hostname: 
 .
 xsFNBGBLRGQBEADCqEcl4YKYLAW8p/rEzBrDLi8fewyqPTLFWosWeu1a4fKzPcW8
 ggl/pjRcXxAxgCt1EhX9bjOrzavdnfnKLYuNkwR0vLWZtNEhAsOovsDzFF6n+WsN
 jtxL9nBZZ/7tgImxMUds8EXotx3R0Le5kbW0QWaWK8NDNayUChGF4ijM1dcacefA
 1ObrQvEKMybdFMxQM+oQjLeIe8TARaoATeLXh/LprNHqDWSAqE3KogChAMykp10i
...

Had to whip up the above, because their official instructions got broken yet again.

ulidtko
  • 14,740
  • 10
  • 56
  • 88
0

Fast way to fix this for Linux users with a UI:

  1. Search for the PPA and do sudo add-apt-repository ppa:[MY_PPA] (the new PPA with keyring is added automatically and up to date)

  2. sudo apt update

  3. Navigate to "Software Sources -> PPA" and delete the old PPA (make sure the new one(s) has/have been added correctly with keyring)

Software Sources

Blackbam
  • 17,496
  • 26
  • 97
  • 150
0

I ran into the apt-key deprecation problem and warnings while adding the kubernetes-xenial repo using a gpg key available online. I was working in an Ubuntu 22.04.1 environment on Apple Silicon (M1/aarch64/arm64). The solution provided below is very similar to the one proposed by Vamsi Nerella elsewhere in this thread, but it includes an explicit example--along with the creation of a new keyring directory ('/etc/apt/keyrings').

This solution also avoids any use of the '/etc/apt/trusted.gpg.d' keyring directory, which some sources have said does nothing to remedy the problem that led to the deprecation of apt-key in the first place.

Although the packages added at the end of this example were from Kubernetes, this approach should work equally well with other packages, as Vamsi discussed.

This solution is also quite similar to the one proposed by Promise Preston (see thread), but it includes a reference to the system architecture, which I needed for my application. Perhaps others will benefit from this example.

Instead of this:

Create a 'kubernetes.list' file using a text editor such as vim or nano:

sudo vim /etc/apt/sources.list.d/kubernetes.list

Add the following text to the new file:

deb http://apt.kubernetes.io/ kubernetes-xenial main

Download the gpg key and use the deprecated apt-key to enable the use of the new repo:

sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
sudo apt-get update
sudo apt-get install kubeadm kubelet kubectl

Use this:

sudo mkdir -p /etc/apt/keyrings

sudo curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg \
| sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes.gpg
     
sudo echo "deb [arch=$(dpkg --print-architecture) \
  signed-by=/etc/apt/keyrings/kubernetes.gpg] \
  http://apt.kubernetes.io/ kubernetes-xenial main" \
  | sudo tee /etc/apt/sources.list.d/kubernetes.list > /dev/null

sudo apt-get update
sudo apt-get install kubeadm kubelet kubectl

If you need a particular version of the Kubernetes packages, such as 1.26 (rather than the latest), you could do this in the last step:

sudo apt-get install -y kubelet=1.26.0-00 kubeadm=1.26.0-00 kubectl=1.26.0-00

As previously mentioned, you could substitute a different key for the one used in this example ('https://packages.cloud.google.com/apt/doc/apt-key.gpg'). The choice of 'kubernetes.gpg' in the dearmored key was arbitrary.

References:

hackr
  • 41
  • 7
-1

This also happens for a poor connection, merely a connection impossible through the port used for download, and to be specific: port 80.

sudo ufw allow port 80

then retry, this can, for some, help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
seletsky
  • 1
  • 1