UPDATE: 2023-Nov-15, Back in August, a PR I submitted to the apt-manage project was accepted, it includes a quick-install script which will install apt-manage on Debian and Ubuntu and their derivitives.
Once again, I've modified my answer below, replacing the installation with the quick-install script.
UPDATE: 2023-May-16, I can confirm apt-manage is the safest most consistent way I found to manage apt sources with keys properly.
I've rewritten the answer, removing my personal script (you can view it in the revisions history if you'd like) to focus on the command and not my personal code.
All of the answers here are great, and I've learned a lot from them.
Per @mesterlion comments, as of this time there is a single tool that does all of this, and does it correctly, properly maintained by the Pop!_OS team.
That tool is apt-manage
which is part of RepoLib module which introduced the key
sub-command in version 2.
So in Pop!_OS 22.04, installation will look like this:
sudo apt install --yes apt-manage
The upstream projects (Ubuntu and Debian) are missing this package, for that: the apt-manage
project has a quick-install script (full disclosure, I submitted the PR for it).
Installing will look like:
curl https://raw.githubusercontent.com/pop-os/repolib/HEAD/quick-install.sh | bash
More details can be found in the Installation section of the git-repo's README.md
.
Example 1
After installing apt-manage
, below is an example of a set of calls from my system:
# Adding PPA
sudo apt-manage add --terse --format=sources ppa:ppa:alessandro-strada/ppa
Adding APT source and assinging a key
sudo apt-manage add --terse --format=sources --name Microsoft --identifier packages-microsoft-com 'deb [arch=amd64] https://packages.microsoft.com/ubuntu/22.04/prod jammy main'
Assigning a key via URL
sudo apt-manage key packages-microsoft-com --url=https://packages.microsoft.com/keys/microsoft.asc
R-Project needed some additional love - because they do something 'different':
sudo apt-manage add --terse --format=sources --name R-Project --identifier cloud-r-project-org 'deb [arch=amd64] https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ ""_""'
Specific to R-Project, as one of the values are empty and the command line can' pass it effectively.
sudo sed -i /etc/apt/sources.list.d/cloud-r-project-org.sources -Ee 's/""_""//g'
Assigning a key via fingerprint
sudo apt-manage key cloud-r-project-org --fingerprint=E298A3A825C0D65DFD57CBB651716619E084DAB9
After all were added, run update.
sudo apt update
Example 2: Initialize docker on a fresh Ubuntu compute instance
set -e
export DEBIAN_FRONTEND=noninteractive
apt-get update
&& apt-get install --yes --no-install-recommends
apt-transport-https ca-certificates
wget gnupg software-properties-common lsb-release
curl jq bash-completion
has() { command -v "$1" > /dev/null; }
Try differnt ways of installing apt-manage
First let's hope there is package ready for installation
has apt-manage
|| curl https://raw.githubusercontent.com/pop-os/repolib/HEAD/quick-install.sh | bash
If the repo isn't part of existing apt sources, add it
REPO_URL="https://download.docker.com/linux/ubuntu"
APT_NAME="$(
apt-manage list -a
| grep -B4 "${REPO_URL}" | head -1
| sed -Ee 's/^\W*([^:]+):$/\1/'
)"
if [[ -n "${APT_NAME}" ]]; then
>&2 printf '%s\n'
''
"$(apt-manage list "${APT_NAME}")"
''
"Docker repository found [${APT_NAME}]. Skipping."
else
apt-manage add --identifier download-docker-com --name Docker
--terse --format=sources
"$( printf "deb [arch=%s] %s %s %s"
"$(dpkg --print-architecture)"
"${REPO_URL}"
"$(lsb_release --codename --short)"
"stable"
)"
apt-manage key download-docker-com \
--url=https://download.docker.com/linux/ubuntu/gpg
fi
wget -O - https://host.domain.tld/path/to/<keyfile>.<ext> | gpg --dearmor > /usr/local/share/keyrings/<your-keyfile-name>.gpg
– Hrobky Jan 27 '21 at 14:10gpg --dearmor
works, and one for which it doesn't? Eg am I correct that this mopidy key would work with--dearmor
, but this google key wouldn't? Note passing google key directly togpg --dearmor
still exits with error code 0, which suggests... it works? – laur Jan 28 '21 at 17:55gpg --dearmor
– laur Feb 02 '21 at 12:54--dearmor
, which sent me looking for the conversion method in my answer… but I didn't make note of which ones. As a general rule, I'd say there's no harm in piping through--dearmor
by default (if the key's not ascii-armored, gpg will exit successfully without changing anything). If apt chokes on the resulting key, then try the binary→binary conversion workaround. – Askeli Feb 20 '21 at 20:40.asc
extension for such files, and.gpg
for binary key files. Mopidy apparently isn't following that convention. It seems thatfile
will always report "PGP public key block Public-Key (old)" for an armored file, and something more detailed for an unarmored file. – Jivan Pal Jul 22 '21 at 00:23less my-file.ext
, press Q to exit), and if it's armored, it will have human-readable headers like "BEGIN PGP PUBLIC KEY BLOCK". If it's a binary file,less
will tell you this before opening it, as trying to display its content will render gibberish/mojibake. – Jivan Pal Jul 22 '21 at 00:24apt
command to replace the deprecated behavior? By the description it seems it can easily be scripted. – Snackoverflow Oct 11 '21 at 06:43/etc/apt/trusted.gpg.d/
directory, likeubuntu-keyring-2012-cdimage.gpg
andubuntu-keyring-2018-archive.gpg
. And these do not have corresponding.list
files. So I guess not everything in that directory should be moved blindly. – Snackoverflow Oct 11 '21 at 07:06add-apt-repository ppa:user/ppa-name
will add a file to/etc/apt/sources.list.d
as well as the corresponding key to/etc/apt/trusted.gpg.d
. Should PPA's get the same treatment? – Dodgyrabbit Jan 26 '22 at 02:53/etc/apt/sources.list.d
while some will be in/etc/apt/trusted.gpg
. You could also mention that thesigned-by
can come afterarch=amd64
following a space. It might seem obvious but some people (guilty) might want that confirmation - just for completeness of your answer! Great answer otherwise! – Rik Apr 13 '22 at 18:30add-apt-repository
will be updated or some other tool will be put together to automate this process... – Compholio May 18 '22 at 23:27apt-add-repository
still usesapt-key add
! Which is the origin on the entire problem in the first placce! And you did not even address it with even one word. – Dec 08 '22 at 11:41[arch=amd64]
you must add it inside the brackets instead, likedeb [arch=amd64 signed-by=/etc/apt/keyrings/vivaldi-release.gpg] http://repo.vivaldi.com/stable/deb/ stable main
otherwise you get syntax error from apt:Malformed entry 3 in list file
. – Ingo Steinke Aug 17 '23 at 07:31