GPG import public key from smartcard

Date

Tags
#short #nuggets #openpgp #gpg #smartcard

TLDR

On a new computer, insert your USB OpenPGP smartcard and run:

gpg --card-edit
fetch
quit

Explanation

I have a YubiKey 5 (still waiting on my Solo v2) on which I store my OpenPGP secret key.

However, if I boot into a new system, insert my USB OpenPGP smartcard, import my public key from a keyserver:

gpg --keyserver hkps://keys.openpgp.org --recv-keys ABCD1234

configure git:

git config --global user.signingkey ABCD1234

and attempt to sign a commit, I'll get an error message:

git commit -S -m "Signed commit"
# error: gpg failed to sign the data
# fatal: failed to write commit object

GPG doesn't know yet it can interact with the private key stored on the USB OpenPGP smartcard!

So, instead of importing the public key from a keyserver, fetch it from the smartcard with the following commands:

gpg --card-edit
fetch
quit