Chapter 6 · Lesson 8 of 10
Git for deployers

SSH keys and deploy keys for Git on a server

A deploy key is an SSH key pair whose public half is added to one repository as read-only, so a server can clone and pull that repository without your personal account.

By the end you will

Generate an SSH key, register it as a deploy key, and clone over SSH from a server, including one key per repository with a Host alias.

Time
20 min
Level
Intermediate
Check
4 questions
You need
  • A server with Git installed
  • A repository on GitHub, GitLab or Gitea that you administer

Use SSH keys so Git can pull and push without a password. For your own machine, create a personal key and add it to your account. For a server, create a separate key and add it to one repository as a read-only deploy key. If one server deploys several repositories, give each repository its own key and select it with a Host alias in ~/.ssh/config.

How it works#

SSH keys come in pairs. The private key stays on the machine and proves who you are. The public key is copied to the other side, here your Git host. When Git connects over SSH, the host checks that the client holds the matching private key. The general ideas are in log in with SSH keys, and this lesson applies them to Git.

Two kinds of access exist on most hosts:

  • Account SSH key. Attached to your user. It reaches everything you can reach. Use it on your own laptop.
  • Deploy key. Attached to one repository. It is read-only unless you tick a write option. Use it on servers and CI runners.

A server that only runs git pull should never carry a key that can push, or read your other repositories.

Step 1: Generate a key#

Run this as the user that will run Git (for example deploy), not root:

bash
ssh-keygen -t ed25519 -C "deploy@app1.example.com" -f ~/.ssh/app1_deploy

-t ed25519 selects a modern key type, -C adds a label, and -f sets the file name so keys do not overwrite each other. You are asked for a passphrase. For an unattended server you often leave it empty, so protect the file instead: the private key must be readable only by its owner (chmod 600 ~/.ssh/app1_deploy). Two files are created: app1_deploy (private) and app1_deploy.pub (public).

Step 2: Add the public key to the host#

Print the public key:

bash
cat ~/.ssh/app1_deploy.pub

It is a single line beginning with ssh-ed25519. Copy the whole line.

  • Deploy key: in the repository settings on your host, open the deploy keys section, add a title (such as app1 server), paste the line, and leave write access off.
  • Personal key: add it under your account’s SSH keys settings.

Never paste the private key anywhere. If you did, delete the key pair and make a new one.

Step 3: Trust the host and test the connection#

bash
ssh -T -i ~/.ssh/app1_deploy git@github.com

The first time, SSH shows the host’s fingerprint and asks Are you sure you want to continue connecting (yes/no/[fingerprint])?. Compare the fingerprint with the one your host publishes in its documentation, then type yes. SSH saves it in ~/.ssh/known_hosts and does not ask again. A good result is a short greeting from the host saying you authenticated, often with a note that shell access is not provided. The wording differs per host, and -T simply skips opening a terminal. Replace github.com with your own host name when you use GitLab or a self-hosted Git server.

Step 4: Clone with the key#

bash
GIT_SSH_COMMAND="ssh -i ~/.ssh/app1_deploy -o IdentitiesOnly=yes" \
  git clone git@github.com:team/app1.git /srv/app1

IdentitiesOnly=yes makes SSH use only the key you named, instead of offering every key it finds. That avoids the host rejecting you after too many attempts.

Step 5: Use a Host alias for several deploy keys#

Typing that every time is tedious, and one host cannot map two keys by hostname alone. Fix this in ~/.ssh/config:

output
Host github-app1
    HostName github.com
    User git
    IdentityFile ~/.ssh/app1_deploy
    IdentitiesOnly yes

Host github-app2
    HostName github.com
    User git
    IdentityFile ~/.ssh/app2_deploy
    IdentitiesOnly yes

Then use the alias as the host name in the remote URL:

bash
git clone git@github-app1:team/app1.git /srv/app1
git clone git@github-app2:team/app2.git /srv/app2

SSH sees github-app1, picks the matching key, and connects to the real host. The file needs the right permissions, chmod 600 ~/.ssh/config. See SSH config file shortcuts for more options.

Remember the background in what is SSH and what Git is if any of this is new.

Check it worked#

bash
cd /srv/app1
git pull
git remote -v

git pull should report Already up to date. or fetch new commits without asking for a password. git remote -v shows the alias in the URL. To check that the key is read-only, try a push from a throwaway commit: it should fail with a permission message. Do not leave that commit behind.

Edge cases and gotchas#

  • One deploy key, one repository. Hosts reject a deploy key already used on another repository. That is why aliases exist.
  • Do not copy your laptop’s private key to a server. Generate a fresh key on the server so the private key never travels.
  • Permissions are enforced. SSH ignores a private key that other users can read (UNPROTECTED PRIVATE KEY FILE). Use chmod 600.
  • known_hosts hygiene. If a host key legitimately changes, remove the old line with ssh-keygen -R github.com, and verify the new fingerprint before accepting it.
  • Rotate keys. When a person leaves or a server is retired, delete its deploy key on the host. It is a few clicks and closes a door.
  • Not root. Keys in /root/.ssh only work when Git runs as root. Run deploys as a normal user and keep keys in that user’s ~/.ssh.

Troubleshooting#

SymptomLikely causeFix
git@github.com: Permission denied (publickey).Key not added, or SSH offered a different keyAdd the .pub to the host, use IdentityFile and IdentitiesOnly yes
ERROR: Repository not found.Deploy key attached to another repository, or wrong URLCheck the repo name and the key’s repository
ERROR: Key is already in useThe key is a deploy key elsewhereGenerate a new key for this repository
WARNING: UNPROTECTED PRIVATE KEY FILE! and Permissions 0644 for ... are too open.The key file is too readablechmod 600 ~/.ssh/app1_deploy
Host key verification failed.known_hosts mismatchVerify the host fingerprint, then ssh-keygen -R <host> and reconnect
ssh: Could not resolve hostname github-app1Alias missing, or config not readCheck ~/.ssh/config spelling, run as the right user
remote: Write access to repository not granted.Read-only deploy key used to pushExpected. Push from a machine with write access
Bad owner or permissions on /home/deploy/.ssh/configConfig file permissionschmod 600 ~/.ssh/config

Quick reference#

bash
ssh-keygen -t ed25519 -C "deploy@app1.example.com" -f ~/.ssh/app1_deploy
chmod 600 ~/.ssh/app1_deploy
cat ~/.ssh/app1_deploy.pub            # paste into the host's deploy keys
ssh -T git@github-app1                # test via the alias
git clone git@github-app1:team/app1.git /srv/app1
ssh-keygen -R github.com              # forget a changed host key

For the bigger picture of where keys fit in a self-hosted setup, see the blog post on managing secrets for self-hosted Docker. The terms SSH key, public key and private key are in the glossary.

Next#

Learn what must never reach the repository in gitignore and keeping secrets out of Git.

Check what you learned4 questions
1Which command creates a modern SSH key pair for Git?
2Which file do you paste into the Git host as the deploy key?
3Why should a production server's deploy key be read-only?
4You have two repositories and two deploy keys on one server. How does SSH pick the right key?

Frequently asked questions

What is a deploy key?

A deploy key is an SSH public key attached to a single repository on your Git host. It gives read-only access by default, which is all a server needs to clone and pull.

Should I use my personal SSH key on a server?

No. A personal key can reach every repository you can, and if the server is compromised, all of them are exposed. A read-only deploy key limits the damage to one repository.

Can I use the same deploy key for two repositories?

Most hosts refuse it, because a deploy key can be attached to only one repository. Create one key per repository and choose between them with a Host alias in ~/.ssh/config.

Why does Git ask Are you sure you want to continue connecting?

It is the first connection to that host, and SSH asks you to trust its fingerprint. After you answer yes, the host key is saved in ~/.ssh/known_hosts and you are not asked again.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios