My question is essentially the same as this one: How to force GPG to use a Keycard when it is available which asks how to force GPG to use a keycard when one is available. That question was asked in 2018 however (3 years ago) with less details, and no answer was given. Also see Create backup Yubikey with identical PGP keys in which no solution was found (OP ends up creating separate subkeys which does not answer their own question).

The problem I have is this: I want to use a keycard (specifically two YubiKeys) as a replacement for the local GPG keychain that I've been sharing around the computers I use (office, home, laptop, etc). Despite the simple goal, GPG/OpenPGP seems to make this a unreasonably difficult task.

First, I do not want to use separate subkeys for the two YubiKeys because when I distribute the public keys, gpg will by default encrypt for the last subkey generated-- i.e. only one of the two YubiKeys. I want to be able to use any of my local keys, YubiKey #1 or YubiKey #2 in order to encrypt or authenticate with any server. It seems that the only way I can do this is by sharing Encryption [E], Authentication [A] and Signing [S] subkeys between my local machine(s) and the YubiKeys.

GPG seems to not want you to do this, as keytocard moves the key to the keycard (YubiKey) and leaves behind a stub on the local machine indicating that the key is present on a keycard. Eventually I worked around this though, and copied the same triplet of E,A,S subkeys to both my YubiKeys and to my local keyring.

Now I have an issue that gpg will only use the local keyring, even when the YubiKey is available. I want gpg to prefer a keycard when it is available. I learned from this answer that I can tell gpg-agent to refresh its knowledge on the available keys on keycards with this command: gpg-connect-agent "scd serialno" "learn --force" /bye. Now the YubiKey works, but the local keyring now lists the subkeys with ssb>, suggesting the keys are stored on an external card, and gpg-agent can no longer sign with those subkeys with the local keyring when I remove the YubiKey! The private keys may have been deleted from the local machine. (I have a backup).

It's quite a simple and obvious thing I'm trying to do: allow either the YubiKeys or the local keyring to be used for signature, authentication or signing, and prefer the YubiKeys when available. Yet I can't seem to find a way to make this work. In fact gpg it seems will stubbornly not acknowledge the existence of a private key on both a keycard and in the local keyring. I can only have one working at a given time.

1 Answer 1


The intended purpose of hardware tokens like the yubikey when used with PGP keys is to avoid having private keys lying around on disk which is why the scenario you envisage isn't supported.

If it was easy to set things up this way then people might think they had secured their keys by moving them into they card when they had really just made a copy.

I think the only way to do what you are trying to do would be to have two copies of your keyring on each machine. One with the full key and one with the stubs and have something external to gpg switch which one is active when the yubikey is plugged in/unplugged. How you do that depends on your OS.

I've tried to avoid speculating as to why you might want to do this but the only valid reason I can think of is to avoid typing a long password when you have a yubikey available. If you have something else in mind you might want to ask a question focussed on that end goal rather than the mechanics.

  • Thanks, I feared that might be the only way. 'avoid typing a long password when you have a yubikey available" - yes, that is exactly why. Is there any way to have GPG acknowledge the same private key on two different YubiKeys?
    – Myridium
    Jun 1, 2021 at 12:05
  • I don't think so. What gpg does is insert a stub in the key where the standard would place a key which says, in essence, it is on this card in this slot. This stub can only point at a single location AFAIK. Jun 1, 2021 at 13:46
  • Is it perhaps possible to have the same key listed multiple times in the keyring then? Maybe under different identities?
    – Myridium
    Jun 1, 2021 at 13:49
  • In theory you should be able to do this with older versions of Gnupg which use the RFC4880 OpenPGP message format for keyrings. However there are no guarantees about how GnuPG searches for the key or subkey in the keyring. AIUI newer versions of GnuPG use an indexed format which makes them quicker to search but I suspect make it less likely that you can store multiple versions of the private key for identical public keys in the same keyring. Jun 1, 2021 at 14:09
  • I'm taken aback that there's no way to do this. Does nobody else want to avoid typing long passwords? It's also a shame that I can't even keep a copy on the YubiKey for external use without risking deleting my local keys!
    – Myridium
    Jun 1, 2021 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .