Skip to content

SSH key authentication — generation, distribution, and lifecycle

Key-based SSH is the floor for any production host, but key generation choice, distribution discipline, and rotation cadence are what determine whether your access actually stays controlled.

Every production server should reject password-based SSH and accept only keys. That much is uncontroversial. The interesting questions are: which key type, where the private key lives, how it gets to the server, how it gets revoked, and what to do when the private key was on a laptop that just walked out the door. This article walks the full lifecycle as we run it.

How to verify

Inventory what is in place on a host before you change anything.

sshd -T 2>/dev/null | grep -iE 'pubkeyauth|passwordauth|kbdinteract' | sort -u
ls -la ~/.ssh/
sudo ls -la /home/*/.ssh/authorized_keys 2>/dev/null
sudo awk '/^[a-z]/{ split($1,a," "); print $NF " " a[1] " " $3 }' /home/*/.ssh/authorized_keys 2>/dev/null
ssh-keygen -lf ~/.ssh/id_ed25519.pub 2>/dev/null

The last command prints the fingerprint and key type. ssh-rsa keys under 3072 bits should be replaced; ssh-ed25519 is the current default for new keys.

What’s happening

SSH key authentication is a challenge-response: the client proves to the server that it holds the private key matching a public key on the server, without ever sending the private key over the wire. The server stores acceptable public keys in ~/.ssh/authorized_keys per user. The client offers each key in its agent (or specified by config) until one is accepted.

Modern best practice is Ed25519 keys (ssh-ed25519): small, fast, immune to the bias and side-channel issues that plagued NIST curves, and supported by every modern OpenSSH. RSA is still acceptable at 3072+ bits, but offers no advantage. ECDSA on NIST curves is best avoided. The private key should be encrypted with a passphrase if it lives on a workstation; for a service account on a CI runner, the key file’s filesystem permissions and a non-interactive setup are the controls.

The lifecycle problem is what gets neglected. A fleet that started with three operator keys five years ago typically has eighteen now, including two that belong to people who left, one from a contractor’s laptop that was wiped, and a CI service key whose private half is in a config repo with git log history showing it. Rotation is what closes those gaps.

The procedure

  1. Generate a fresh Ed25519 keypair on your workstation. Use a meaningful comment so you can identify the key in someone’s authorized_keys later.

    ssh-keygen -t ed25519 -C "alice@workstation-2026-06" -f ~/.ssh/id_ed25519_ops

    The -C comment is what appears in the public key. The passphrase prompt that follows should be answered with a non-empty passphrase; the file is encrypted with it. Empty passphrases are acceptable only for unattended automation keys (CI, deploy bots) where the file’s filesystem perms are the access control.

  2. Confirm the fingerprint matches what you expect.

    ssh-keygen -lf ~/.ssh/id_ed25519_ops.pub
    # SHA256:abc... alice@workstation-2026-06 (ED25519)
  3. Copy the public key to the server with ssh-copy-id. This is safer than manual cat-and-paste because it preserves perms and avoids fingerprint copy mistakes.

    ssh-copy-id -i ~/.ssh/id_ed25519_ops.pub ops@web-01.prod.example.net

    If ssh-copy-id is not available (often on macOS without Homebrew), the equivalent shell pipeline:

    cat ~/.ssh/id_ed25519_ops.pub | ssh ops@web-01 \
      'install -d -m 0700 ~/.ssh && \
       cat >> ~/.ssh/authorized_keys && \
       chmod 0600 ~/.ssh/authorized_keys'
  4. Test the new key in a fresh session before disabling anything else.

    ssh -i ~/.ssh/id_ed25519_ops ops@web-01 'whoami; date'

    If it succeeds, now you can disable password auth per ssh-disable-password-auth.

  5. Use ssh-agent so you do not type the passphrase per command. Add the key to the agent once per workstation session.

    eval "$(ssh-agent -s)"
    ssh-add --apple-use-keychain ~/.ssh/id_ed25519_ops    # macOS
    ssh-add ~/.ssh/id_ed25519_ops                          # Linux
    ssh-add -l    # list loaded keys

    For automation, ssh-add -t 8h adds a key that expires after 8 hours — better than no expiration. See ssh-agent-forwarding-safe before forwarding the agent over a session.

  6. Distribute keys through code, not manual copy. Once you have more than three keys on more than three hosts, the manual workflow loses. Ansible’s authorized_key module is the typical pattern:

    - name: Set operator keys on all hosts
      ansible.builtin.authorized_key:
        user: ops
        state: present
        exclusive: yes   # remove unlisted keys
        key: "{{ lookup('file', 'keys/' + item) }}"
      loop:
        - alice.ed25519.pub
        - bob.ed25519.pub
        - carol.ed25519.pub

    exclusive: yes is the lifecycle hook — removing alice.ed25519.pub from the source list removes Alice’s key everywhere on the next apply. That is how rotation actually works.

  7. Audit periodically. Every quarter (or after every personnel change), enumerate every key on every host and compare against the source of truth.

    ansible all -m shell -a "awk '{print \$3}' /home/*/.ssh/authorized_keys 2>/dev/null" | sort -u

    Any comment that does not match a current operator gets removed; any key without a comment (ssh-ed25519 AAAA... with no trailing identifier) is suspicious and should be traced or revoked.

  8. Rotate on a cadence. Ed25519 has no known cryptographic weakness on a yearly cadence, but operator turnover does. Stack-rotate operator keys yearly (so not all keys change on the same day), service keys every six months, and any key that touched a compromised endpoint immediately.

Common pitfalls

  • Permissions: ~/.ssh must be 0700, authorized_keys must be 0600, the home directory itself must not be group-writable. sshd silently refuses keys with loose perms.
  • Mixing key types in a single authorized_keys: it works, but you cannot tell at a glance which keys are still in their cryptographic prime. Standardize on Ed25519 across the fleet.
  • Leaving the original ubuntu or ec2-user cloud user with its initial key intact after creating named operators — that key has been everywhere and is the first target.
  • Storing private keys in repositories or in chat — even a “private” repo gets cloned and is hard to revoke universally.
  • Treating service-account keys as “set and forget” — they age the worst, because they are rarely audited.

In our managed engagements, the source of truth for who can SSH where is a single Git repository, and the keys propagate to every host through configuration management on a schedule. That separation — humans in one list, hosts in another, glued by code — is what makes the Managed Operations access story auditable.