Skip to content

Instantly share code, notes, and snippets.

@pacmac
Last active July 15, 2023 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pacmac/1395f3e8a32137c4bfdf5faeb8164f9b to your computer and use it in GitHub Desktop.
Save pacmac/1395f3e8a32137c4bfdf5faeb8164f9b to your computer and use it in GitHub Desktop.
Fix Missing Keys when running apt update.
#!/bin/bash
## bash <(curl -Ls https://gist.githubusercontent.com/pacmac/1395f3e8a32137c4bfdf5faeb8164f9b/raw/apt-keyfix.sh)
# Run apt update and capture the error output
error_output=$(apt update 2>&1)
# Check if the error output contains the key verification error message
if [[ $error_output =~ "The following signatures couldn't be verified because the public key is not available" ]]; then
# Retrieve the missing keys and run the apt-key command for each one
keys=$(grep -oE "NO_PUBKEY [0-9A-Fa-f]+" <<< "$error_output" | awk '{print $NF}')
for key in $keys; do
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys "$key"
done
# Run apt update again to refresh the package lists
apt update
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment