# 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.

URL: https://levelrail.com/learn/git/ssh-keys-and-deploy-keys-for-git/
Updated: 2026-11-27

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](https://levelrail.com/learn/server-basics/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`:

```text
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](https://levelrail.com/learn/server-basics/ssh-config-file-shortcuts/)) for more options.

Remember the background in [what is SSH](https://levelrail.com/learn/server-basics/what-is-ssh-and-how-does-it-work/)) and [what Git is](https://levelrail.com/learn/git/what-is-git-and-why-servers-use-it/)) 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

| 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

```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
```

## Related lessons

- [Git remotes, push, pull and fetch](https://levelrail.com/learn/git/git-remotes-push-pull-fetch/)): what you run once the key works.
- [Log in with SSH keys](https://levelrail.com/learn/server-basics/log-in-with-ssh-keys/)): the same key pairs for server logins.
- [SSH config file shortcuts](https://levelrail.com/learn/server-basics/ssh-config-file-shortcuts/)): more on Host blocks.
- [Fix SSH permission denied (publickey)](https://levelrail.com/learn/troubleshooting/fix-ssh-permission-denied-publickey/)): debugging key failures.
- [Gitignore and keeping secrets out of Git](https://levelrail.com/learn/git/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](https://levelrail.com/blog/manage-secrets-self-hosted-docker/)). The terms [SSH key](https://levelrail.com/learn/glossary/#ssh-key)), [public key](https://levelrail.com/learn/glossary/#public-key)) and [private key](https://levelrail.com/learn/glossary/#private-key)) are in the glossary.

## Next

Learn what must never reach the repository in [gitignore and keeping secrets out of Git](https://levelrail.com/learn/git/gitignore-and-keeping-secrets-out-of-git/)).

## 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

- [OpenSSH ssh-keygen manual](https://manpages.ubuntu.com/manpages/noble/man1/ssh-keygen.1.html)
- [Ubuntu manpages](https://manpages.ubuntu.com/)
