basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Git and SSH keys: deploy keys, two accounts, signed commits

    Three jobs handled with SSH keys and aliases in ~/.ssh/config: a server gets access to one repository and nothing else, two GitHub accounts live on one computer, and commits are signed with the same key you log in with — so GitHub marks them “Verified”.

    How GitHub knows who’s there

    Everyone goes to one address — [email protected], and the user is always git. GitHub decides who arrived by the key: an account key is the account with all its repositories, a deploy key is one repository. So different access on one computer means different keys, and ~/.ssh/config picks the key through aliases:

    git@github-infra:example-user/infra-web1.git   → Host github-infra → key infra_deploy → one repository
    [email protected]:example-user/notes.git              → Host github.com   → id_ed25519        → personal account
    git@github-work:work-org/app.git                 → Host github-work  → id_work           → work account

    A deploy key: the server sees one repository

    A server running git pull in /opt (Server configs in git) needs to read its own repository — and only that. A personal account key would open every repository, and if the server were compromised, all of them at once (Each key and token grants one access).

    1. Create a key on the server

      sudo ssh-keygen -t ed25519 -f /root/.ssh/infra_deploy -C "deploy web1 infra-web1" -N ""

      No passphrase (-N ""): git pull runs without a person. That’s acceptable because the key opens one repository, and it doesn’t need write access. The key belongs to root because root owns /opt.

    2. Add it to the repository

      sudo cat /root/.ssh/infra_deploy.pub
      # ssh-ed25519 AAAAC3Nza… deploy web1 infra-web1

      On GitHub: the repository, Settings, Deploy keys, Add deploy key. Enable “Allow write access” only if the server will push; under the rule “only git pull on the server”, leave it off.

    3. An alias in root’s ~/.ssh/config

      # /root/.ssh/config
      Host github-infra
          HostName github.com
          User git
          IdentityFile /root/.ssh/infra_deploy
          IdentitiesOnly yes

      Check the login: for a deploy key GitHub greets you with the repository name:

      sudo ssh -T github-infra
      # Hi example-user/infra-web1! You've successfully authenticated, but GitHub does not provide shell access.
    4. The repository URL — through the alias

      cd /opt
      sudo git remote set-url origin git@github-infra:example-user/infra-web1.git
      sudo git pull

    Two accounts on one computer

    1. A second key — for the work account

      ssh-keygen -t ed25519 -f ~/.ssh/id_work -C "work laptop-2026"

      Add the .pub to the work GitHub account (Settings, SSH and GPG keys).

    2. An alias

      # ~/.ssh/config — above the Host * block
      Host github-work
          HostName github.com
          User git
          IdentityFile ~/.ssh/id_work
          IdentitiesOnly yes
      
      Host github.com
          IdentityFile ~/.ssh/id_ed25519
          IdentitiesOnly yes

      IdentitiesOnly yes on both: otherwise the agent offers the first matching key, and GitHub lets you in as the wrong account.

    3. Work repositories — through the alias and with their own email

      git clone git@github-work:work-org/app.git ~/work/app

      The email for every repository under ~/work/ comes through includeIf in ~/.gitconfig (~/.gitconfig block by block):

      # ~/.gitconfig-work
      [user]
          email = [email protected]

    Signing commits with an SSH key

    Since git 2.34, commits can be signed with an SSH key; no GPG needed. GitHub marks such commits “Verified”: the commit was made by the key’s owner, not by someone who typed another person’s name into user.name.

    1. Turn on signing

      git config --global gpg.format ssh
      git config --global user.signingKey ~/.ssh/id_ed25519.pub
      git config --global commit.gpgSign true
      git config --global tag.gpgSign true

      user.signingKey is the path to the public half: the agent signs with the private one (ssh-agent), so the passphrase isn’t asked a second time.

    2. The signing key — into GitHub

      Settings, SSH and GPG keys, New SSH key, type Signing Key. The same .pub you use to log in, but as a separate entry: a login key doesn’t verify signatures.

    3. Verifying signatures locally

      Without a list of trusted keys, git log --show-signature prints error: gpg.ssh.allowedSignersFile needs to be configured and exist for ssh signature verification and No signature, even though there is one. The list lives in a file:

      mkdir -p ~/.config/git
      echo "[email protected] namespaces=\"git\" $(cat ~/.ssh/id_ed25519.pub)" \
        >> ~/.config/git/allowed_signers
      git config --global gpg.ssh.allowedSignersFile ~/.config/git/allowed_signers

    If it doesn’t work

    ERROR: The key you are authenticating with has been marked as read only.

    Cause
    The deploy key was added without “Allow write access”, and the server runs git push.
    Fix
    That’s by design if the server only reads. If it must push, enable write access for this key in the repository settings.

    GitHub won’t accept a key: Key is already in use

    Cause
    One key is one deploy key for one repository, or one account’s key. GitHub won’t take it twice.
    Fix
    For a second repository, a separate key and a separate alias in ~/.ssh/config.

    The push goes as the wrong account: Permission to work-org/app.git denied to example-user

    Cause
    ssh offered the first matching key — the personal one. GitHub identifies the account by the key, not the URL.
    Fix
    A github-work alias with IdentityFile and IdentitiesOnly yes, and the repository URL through it: git remote set-url origin git@github-work:work-org/app.git.

    Commits on GitHub show someone else’s name or no avatar

    Cause
    The commit has an email that isn’t on the account: the work repository still has the personal one, or the other way round.
    Fix
    includeIf with a different email for ~/work/ (~/.gitconfig block by block); check with git config user.email inside the repository.

    error: gpg failed to sign the data

    Cause
    Signing is on (commit.gpgSign), but the format is still openpgp: git looks for gpg and a GPG key.
    Fix
    git config --global gpg.format ssh, and user.signingKey set to the path of the .pub.

    Signed commits show “Unverified” on GitHub

    Cause
    The key was added to GitHub as a login key (Authentication), and signing needs a separate Signing Key entry. Or the commit email isn’t verified on the account.
    Fix
    Settings, SSH and GPG keys, New SSH key, type Signing Key — the same .pub.

    How to check

    Each alias leads to its own account:

    ssh -T [email protected]
    # Hi example-user! You've successfully authenticated, but GitHub does not provide shell access.
    ssh -T github-work
    # Hi work-user! You've successfully authenticated, but GitHub does not provide shell access.

    The last commit is signed:

    git log --show-signature -1 --format='%h %s'
    # Good "git" signature for [email protected] with ED25519 key SHA256:u7WT…
    # 3e2d1c traefik: readTimeout 300s