Self-contained setup.sh that imports the bundled public key, binds the YubiKey card stubs, enables SSH via the auth key, and configures git signing — so a fresh machine is provisioned without hunting for the public key. Idempotent and verified end to end.
yubikey-setup
Set up the YubiKey on a fresh machine — GPG commit signing, SSH auth, and git identity — without hunting for the public key.
The YubiKey's OpenPGP app holds the private keys (signature, auth, encryption).
gpg needs the matching public key in its keyring to sign, but a fresh
machine doesn't have it and the card alone doesn't hand it over in a usable
form. This subfolder bundles pubkey.asc (the public key block) so setup.sh
can import it directly — clone the repo on a new machine, insert the YubiKey,
run the script, done.
What it configures
- GPG: imports the bundled public key, binds the YubiKey's secret stubs,
sets ultimate trust (so signatures verify as
Goodnotunknown). - SSH: enables the YubiKey's auth key as an SSH key via gpg-agent
(
enable-ssh-support), sosshandgit pushauthenticate with the YubiKey. - Git: sets
user.name/user.email,user.signingkey,gpg.format openpgp, andcommit.gpgsign/tag.gpgsign true. - Verification: a local
allowed_signersfile plus a test GPG signature with the YubiKey to confirm everything works end to end.
Requirements
gpg(gnupg),ssh-keygen(openssh),git- A YubiKey with an OpenPGP app inserted
- Run as your normal user
Usage
./setup.sh
Tap the YubiKey when it blinks (it signs a test blob and, for SSH, exposes the auth key through the agent). It's idempotent — re-running on a machine that's already set up just re-confirms each step.
After it runs, for the current shell, enable the YubiKey for SSH:
export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
New logins pick this up automatically via gpg-agent. Add the YubiKey's SSH
public key (ssh-add -L) to Gitea/GitHub under Settings → SSH/GPG Keys so
pushes authenticate and commits verify.
Files
setup.sh— the installer (run this on a fresh machine).pubkey.asc— the public key block (primary63C6571F1095CC5C+ S/A/E subkeys). Safe to ship in a repo: it's a public key, no secret material.gpg-switch-yubikey— re-learn the currently inserted YubiKey after physically swapping between two YubiKeys that hold identical key material.
Switching between two YubiKeys
GPG binds secret key stubs to a specific card serial number. When you swap to a
different YubiKey holding identical key material, GPG prompts you to insert the
other one. gpg-switch-yubikey deletes the stubs and re-learns whichever card
is currently inserted:
# one-time: put the script on your PATH
cp gpg-switch-yubikey ~/.local/bin/
chmod +x ~/.local/bin/gpg-switch-yubikey
# after physically swapping YubiKeys:
gpg-switch-yubikey
It contains a hardcoded GPG key fingerprint — edit the FINGERPRINT variable to
match your own key.
Notes
- No private keys are stored or generated by this setup. The YubiKey remains the sole holder of the private material; only the public key is bundled here.
- The script resolves machine-specific values (UID, the gpg-agent SSH socket path) at runtime, so it's portable across UIDs.
FINGERPRINT,KEYID,GIT_NAME, andGIT_EMAILnear the top ofsetup.shpin this to a specific key/identity. Change them if you adopt this for a different key.