Sub topic 14
Deploy keys for single repositories
This chapter assumes you can generate an SSH key, add a public key to GitHub, and test a connection with ssh -T. A deploy key reuses those steps, but attaches the key to one repository instead of to your account.
What a deploy key is
A deploy key is an SSH public key that you add to a single repository. Whoever holds the matching private key can reach that repository and nothing else. The key does not belong to a user, so it keeps working if the person who created it leaves the team.
This differs from a key on your account. An account key acts as you and can reach every repository you can reach. A deploy key can only reach the one repository it was added to.
When to use one
Deploy keys fit automated systems that need access to one repository, such as:
- A production server that pulls a release.
- A build machine that clones a single project.
- A backup job that reads one repository.
For these cases, an account key gives the machine far more access than it needs. A deploy key limits the damage if the machine is compromised.
Setting one up
Run these steps on the machine that needs access. Use a dedicated file name, and leave the passphrase empty only if the machine runs unattended and the private key is protected by file permissions.
ssh-keygen -t ed25519 -C "deploy key for OWNER/REPO" -f ~/.ssh/repo_deploy_keyThen add the public key in GitHub:
- Open the repository and choose Settings.
- Choose Deploy keys, then Add deploy key.
- Enter a title, paste the contents of
~/.ssh/repo_deploy_key.pub, and save.
By default a deploy key is read-only. Tick Allow write access only if the machine must push. Leave it off for servers that only pull.
Using the key
Because the key is not your default, tell SSH to offer it for this repository. A host alias in ~/.ssh/config does this.
Host github-repo
HostName github.com
User git
IdentityFile ~/.ssh/repo_deploy_key
IdentitiesOnly yesClone through the alias, replacing OWNER/REPO with your repository:
git clone git@github-repo:OWNER/REPO.gitIdentitiesOnly yes matters here. Without it, SSH may offer other keys from your agent first, and GitHub could authenticate you as a different identity.
Limits and common errors
- Not for people. A deploy key has no user identity, so commits and audit entries cannot be traced to a person.
- One repository only. Each repository needs its own key pair.
- Organization policy. An organization can restrict deploy keys, and you need admin access to the repository to add one.
- Wrong key offered. If you see
Permission denied (publickey)or a push is refused, check that the alias andIdentityFilepoint to the deploy key, and that write access is enabled if you are pushing.
Exercise
A server must pull releases from one repository and must never push. Which kind of key should you use, and which setting should you leave off?
Show answer
Use a deploy key generated for that repository, and leave "Allow write access" unchecked so the key stays read-only.
Exercise
Why do you add IdentitiesOnly yes to the SSH config entry for a deploy key?
Show answer
It makes SSH offer only the named key. Without it, SSH may offer other keys from the agent first, and GitHub could authenticate as a different identity with broader access.