# Clone over SSH

Add your public key to your account and clone, fetch and push over SSH.

An account SSH key identifies you: it reaches the repositories of the organization you added it in,
as far as your IAM policies allow. Passwords are not accepted over SSH. Replace laptop with
a label for the machine the key is on.

## Prerequisites

- An SSH key pair, for example `~/.ssh/id_ed25519.pub`.
- Permission for `git:RegisterSshKey`. Without it the **Add a key** section is not shown.
- `git:PullRepository` on the repository to clone and fetch, and `git:PushRepository` to push.

## Add your key

**Console**
1. Open **Account → SSH keys**.
2. Under **Add a key**, for **Label**, enter laptop.
3. For **Public key**, paste the contents of your `.pub` file.
4. Choose **Add key**. The key appears under **Your keys** with its fingerprint.

**CLI**
```sh
dcloud git ssh-keys create --name laptop --public-key "$(cat ~/.ssh/id_ed25519.pub)"
```

**API**
```http
POST /git/api/ssh-keys
{ "name": "laptop", "publicKey": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEXAMPLE jane.doe@example.com" }
```

**Terraform**
```hcl
resource "dcloud_git_ssh_key" "laptop" {
  name       = "laptop"
  public_key = file("~/.ssh/id_ed25519.pub")
}
```

A key that is already registered, as anyone's account key, is refused with 409
`That key is already registered`. A key that cannot be parsed is refused with 400.

## Clone

Copy the URL from the repository's **Clone** button, on its **SSH** tab, or build it:

```sh
git clone ssh://git@git.<platform domain>/my-org/my-repo.git
```

my-org is your organization's slug. The port appears in the URL only when it is not 22.

**Important:**
Check the server's host key fingerprint before you accept it on the first connection. It is shown
on **Account → SSH keys**, under **Setting up**, as **Server fingerprint**.

## Result

The repository is cloned; push and fetch use the same key. When the server refuses, git prints one
of these:

| Message                                                                                    | Cause                                                                             |
| ------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------- |
| `Repository not found`                                                                     | The repository does not exist, or your policies do not allow `git:PullRepository` |
| `Permission denied: you can read this repository but not push to it (git:PushRepository).` | A push without `git:PushRepository`                                               |
| `This SSH key is registered to a different organization. …`                                | The key was added in another organization; add it in this one                     |

## Related

- [Clone over HTTPS](clone-over-https.md)
- [Add a deploy key](add-a-deploy-key.md) for a machine rather than a person.
- `git.ssh-keys.create` · `dcloud git ssh-keys create` · `dcloud_git_ssh_key`
