To update MongoDB GPG keys on Debian 11 Bullseye, remove the deprecated apt-key method and use the modern signed-by approach. Download the latest key via curl, save it to /etc/apt/trusted.gpg.d/, and verify the package signature during installation to ensure secure repository access.
Why the Old Method Fails on Bullseye
Debian 11 Bullseye enforces stricter security policies regarding package signing. The traditional method of adding keys globally usingapt-key adv --keyserver is deprecated and often blocked by default configurations in newer versions of APT. Attempting to use this legacy command may result in warnings or errors that prevent package managers from verifying repository integrity. This shift is part of a broader industry move toward per-repository signing keys, which isolates trust boundaries and reduces the risk of compromising all installed packages if a single key is exposed.
For administrators managing infrastructure, understanding this change is critical. While some older guides might suggest linking to unrelated security topics like VPN client access interfaces or blocking MAC addresses with iptables, these are not relevant to the specific task of updating package management keys. The focus must remain strictly on the GnuPG integration within the APT system. Similarly, while securing an Ubuntu server shares similarities with Debian, the file paths and service names can differ slightly, so relying on generic advice without checking the specific distribution version can lead to configuration drift.
Downloading the Latest Key
The first step is to retrieve the current public key for the MongoDB repository. MongoDB provides their signing keys through official channels, and you should always download them directly from their secure servers rather than copying keys from third-party sources. Use thecurl utility to fetch the key and pipe it into a file located in the trusted GPG directory.
Run the following command to download the key:
curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | sudo tee /usr/share/keyrings/mongodb-server-6.0.gpg > /dev/null
This command uses several flags to ensure reliability. The -f flag fails silently on HTTP errors, -s suppresses progress meters, and -S shows errors on stderr. The -L flag follows redirects, which is essential since MongoDB may redirect requests to different CDN nodes. The output is redirected to /dev/null to keep the terminal clean, while the key data is saved to /usr/share/keyrings/. Note that you should adjust the URL to match your specific MongoDB version, such as 5.0 or 7.0, by changing the filename accordingly.
Configuring the Repository List
Once the key is stored locally, you must configure the APT sources list to reference this specific key file. This replaces the global keyring usage with a local, repository-specific binding. Create or edit the source list file for MongoDB. Use a text editor to add the following line to/etc/apt/sources.list.d/mongodb-org-6.0.list:
deb [ signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 main
The crucial element here is the [ signed-by=... ] option inside the square brackets. This tells APT exactly which key to use for validating packages from this specific repository. If you omit this or point it to a non-existent file, APT will reject the packages due to unverified signatures. Ensure that the path matches the location where you saved the key in the previous step. Also, verify that the codename bullseye and the version number align with your actual setup.
Verifying the Installation
After updating the configuration, refresh the package index to apply the changes. This step forces APT to re-download the package lists, now using the new signed-by configuration. Execute the update command:sudo apt-get update
If the configuration is correct, this command should complete without any GPG errors. You may see standard informational messages about fetching packages, but there should be no warnings about "NO_PUBKEY" or similar signing issues. Once the update succeeds, you can proceed to install or upgrade MongoDB packages safely. Always check the output carefully; if errors persist, double-check the key file permissions and the path specified in the sources list.
Removing Deprecated Keys
It is good practice to remove any previously added global keys to avoid confusion. Although APT prioritizes the signed-by directive, having redundant keys in the global keyring can clutter your system's trust store. You can list existing keys associated with MongoDB using:apt-key list | grep mongodb
If you find old keys, remove them using apt-key del followed by the key ID. However, note that apt-key itself is deprecated, so this step is primarily for cleanup rather than functional necessity. The primary security improvement comes from ensuring all repositories use the local keyring method exclusively.
Frequently Asked Questions
What happens if I forget the signed-by parameter?
APT will fail to verify the packages because it does not know which key to trust for that specific repository. You will receive GPG errors during the update process, preventing installation until the configuration is corrected.Can I use the same key file for multiple MongoDB versions?
Yes, you can reuse the same key file if the signing key has not changed between versions. However, it is safer to create separate key files for each major version to maintain clear separation of trust and simplify troubleshooting.How do I check if my current APT version supports signed-by?
Most modern versions of Debian 11 include APT versions that fully support the signed-by directive. You can check your APT version by runningapt --version and comparing it against the documentation for your release.
Is it safe to skip the key verification entirely?
No, skipping verification exposes your system to potential man-in-the-middle attacks where malicious packages could be injected into your repository stream. Always ensure that the key fingerprint matches the official MongoDB documentation before trusting the repository.Why did my previous apt-key commands stop working?
Debian 11 deprecated the globalapt-key mechanism to improve security isolation. Newer APT releases enforce strict validation, requiring explicit key bindings per repository to prevent accidental trust expansion across unrelated software sources.