Expanding on this answer: It looks like the private keys stored on a smart card are shadowed (explanation of the format, which contains the card number) in the ~/.gnupg/private-keys-v1.d
subfolder.
The files contained in ~/.gnupg/private-keys-v1.d
have the name of the keygrip of the subkeys. I deleted them and at the next execution of gpg --card-status
they got recreated, and now GPG expects the new smart card.
To summarize:
Identify the keygrip of the keys you need to move, e.g. with
gpg --with-keygrip -k
pub rsa4096 2020-03-21 [SC]
B7FF63C625A1B25AFE61C54B2E81C65179077A0A
[...]
sub rsa4096 2020-03-21 [E] [expires: 2025-03-20]
Keygrip = A69CB750D1E4B7F3CE063031AED8C13AA2E8E7CB
Make a backup of the ~/.gnupg
folder, just for safety.
cp -r ~/.gnupg ~/.gnupg.bak
remove the corresponding .key
files:
rm ~/.gnupg/private-keys-v1.d/A69CB750D1E4B7F3CE063031AED8C13AA2E8E7CB.key
regenerate the shadowed keys with:
gpg --card-status
Then test it by encrypting and decrypting something. For instance:
echo Success | gpg --encrypt --recipient <yourself> | gpg --decrypt
Tested on gpg 2.2.19 (Mint)