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.
- Updated Nov 27, 2026
- Markdown
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.
- 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:
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:
cat ~/.ssh/app1_deploy.pubIt 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#
ssh -T -i ~/.ssh/app1_deploy git@github.comThe 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#
GIT_SSH_COMMAND="ssh -i ~/.ssh/app1_deploy -o IdentitiesOnly=yes" \
git clone git@github.com:team/app1.git /srv/app1IdentitiesOnly=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:
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 yesThen use the alias as the host name in the remote URL:
git clone git@github-app1:team/app1.git /srv/app1
git clone git@github-app2:team/app2.git /srv/app2SSH 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#
cd /srv/app1
git pull
git remote -vgit 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). Usechmod 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/.sshonly work when Git runs as root. Run deploys as a normal user and keep keys in that user’s~/.ssh.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
git@github.com: Permission denied (publickey). | Key not added, or SSH offered a different key | Add the .pub to the host, use IdentityFile and IdentitiesOnly yes |
ERROR: Repository not found. | Deploy key attached to another repository, or wrong URL | Check the repo name and the key’s repository |
ERROR: Key is already in use | The key is a deploy key elsewhere | Generate a new key for this repository |
WARNING: UNPROTECTED PRIVATE KEY FILE! and Permissions 0644 for ... are too open. | The key file is too readable | chmod 600 ~/.ssh/app1_deploy |
Host key verification failed. | known_hosts mismatch | Verify the host fingerprint, then ssh-keygen -R <host> and reconnect |
ssh: Could not resolve hostname github-app1 | Alias missing, or config not read | Check ~/.ssh/config spelling, run as the right user |
remote: Write access to repository not granted. | Read-only deploy key used to push | Expected. Push from a machine with write access |
Bad owner or permissions on /home/deploy/.ssh/config | Config file permissions | chmod 600 ~/.ssh/config |
Quick reference#
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 keyRelated lessons#
- Git remotes, push, pull and fetch: what you run once the key works.
- Log in with SSH keys: the same key pairs for server logins.
- SSH config file shortcuts: more on Host blocks.
- Fix SSH permission denied (publickey): debugging key failures.
- Gitignore and keeping secrets out of Git: never commit keys.
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 learned
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
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.