Command Functions:
The apt-key command provides several subcommands for managing GPG keys:
list: List all keys in the keyring
add: Add a key file to the keyring
del: Remove a key from the keyring
export: Export a key from the keyring
update: Update keys using the keyring package
net-update: Update keys using the network
adv: Pass advanced options to gpg
finger: Show fingerprints of keys
Key Locations:
APT uses several keyring files to store trusted keys:
/etc/apt/trusted.gpg: Main keyring file
/etc/apt/trusted.gpg.d/: Directory containing additional keyring files
~/.gnupg/: User's GPG directory (when operating in user mode)
Working with Repositories:
Keys are essential for adding trusted repositories:
- First add the repository key:
sudo apt-key add repository-key.gpg
- Then add the repository:
sudo add-apt-repository 'deb http://repo.example.com/ubuntu stable main'
- Update package lists:
sudo apt update
Key Formats:
- Binary format: Default GPG key format (.gpg extension)
- ASCII-armored: Text-based format that can be easily included in emails or web pages (.asc extension)
- Use
-a or --ascii-armor option to export keys in ASCII format
Key Identification:
Keys can be referred to by:
- Key ID: Last 8 characters of the fingerprint (e.g., 3F1EA0C7)
- Long Key ID: Last 16 characters of the fingerprint
- Fingerprint: Full 40-character fingerprint
- Email address: If the key has an associated email
Keyserver Operations:
The adv subcommand allows advanced operations with keyservers:
- Receiving keys:
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys KEY_ID
- Refreshing keys:
apt-key adv --refresh-keys
- Searching for keys:
apt-key adv --keyserver keyserver.ubuntu.com --search-keys search_term
Security Considerations:
- Always verify key fingerprints from trusted sources before adding them
- Avoid adding keys from untrusted sources, as they could compromise your system
- Regularly update keys with
apt-key update to get revocation certificates
- Consider using HTTPS to download keys when possible
- Keys added to the system are trusted for all APT operations by all users
Deprecation Notice:
Note that apt-key is deprecated and will eventually be removed:
- The recommended approach is to place repository keys in
/etc/apt/trusted.gpg.d/ with a .gpg extension
- Or place keys in
/usr/share/keyrings/ and reference them in the sources list using the signed-by option
- Example:
deb [signed-by=/usr/share/keyrings/example-archive-keyring.gpg] http://example.com/debian stable main
Common Patterns:
- Add a key from a website:
wget -qO - https://example.com/key.gpg | sudo apt-key add -
- Add a key for a PPA:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys PPA_KEY_ID
- Remove an expired key:
sudo apt-key del EXPIRED_KEY_ID
- View key details:
apt-key list | grep -A 2 KEY_ID
Troubleshooting:
- If
NO_PUBKEY errors appear during apt update, you need to add the missing key
- Use
apt-key list to check if a key is already installed
- If a key can't be fetched from the default keyserver, try an alternative one
- For network issues, try using the
hkp protocol: --keyserver hkp://keyserver.ubuntu.com:80
- For corporate environments with proxies, set HTTP_PROXY environment variable before running apt-key