Files
Helper-Scripts/yubikey-setup
hendrik-hog 0868ce16aa Add YubiKey new-machine setup script
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.
2026-07-24 19:28:15 +02:00
..
2026-07-24 19:28:15 +02:00
2026-07-24 19:28:15 +02:00

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 Good not unknown).
  • SSH: enables the YubiKey's auth key as an SSH key via gpg-agent (enable-ssh-support), so ssh and git push authenticate with the YubiKey.
  • Git: sets user.name/user.email, user.signingkey, gpg.format openpgp, and commit.gpgsign/tag.gpgsign true.
  • Verification: a local allowed_signers file 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 (primary 63C6571F1095CC5C + 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, and GIT_EMAIL near the top of setup.sh pin this to a specific key/identity. Change them if you adopt this for a different key.