Ubuntu errors

How to resolve the GPG NO_PUBKEY Error

How to resolve the GPG NO_PUBKEY Error on Ubuntu

Sometimes your sudo apt update or sudo apt upgrade command just does not want to behave and give you more lip than an angst ridden teenager who thinks the world owes them, responding with the following:

alt sudo apt update failure with NO_PUBKEY

What this means is that the system is missing one, or more, public keys which verify the integrity of the packages.

Using my case as an example, I’ll show you how to resolve these types of issues:

Firstly:

alt Keep calm and dont panic

That’s the usual advice I give to people (including myself) when things go wrong.

Get the Key ID

Run the command again, and this time pay attention to the random string at the end of the message, this is the UID/Key ID of the package.

alt Get the Key ID of the package

In the above example, the Key ID is C85668DF69375001, copy this into a text editor, you will be needing this.

Obtain the Key

Starting with Ubuntu 20.04 and Debian 11 (and I will assume you are on one of these or even later for security and stability sake), the next thing you need to do is retrieve the key.

Using the gpg command we will be pulling down the missing key from Ubuntu’s keyserver.

Issue the following:

sudo gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys [KEY_ID]

replacing [KEY_ID] with the ID you obtained from above like so.

sudo gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C85668DF69375001

You shold see a response similar to:

alt Import the missing key from Ubuntu Key Server

Export the key

Next you need to export the key to the /etc/apt/trusted.d directory under a sensible name.

Issue the following:

sudo gpg --export [KEY_ID] | sudo tee /etc/apt/trusted.gpg.d/[SENSIBLE_NAME].gpg

In my case the offending package was Spotify, so I used the command:

sudo gpg --export C85668DF69375001 | sudo tee /etc/apt/trusted.gpg.d/spotify.gpg

Update your system

Finally you should just need to run sudo apt update or sudo apt upgrade (unless you want to be really trendy and do a sudo apt update && sudo apt upgrade).

And as if by magic, the error has vanished.

alt Issue resolved

Rod Laycock
Principle Engineer

Related