Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. gubatron revised this gist Mar 8, 2021. 1 changed file with 3 additions and 3 deletions.
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ So you went ahead and created your `(alice_github.id_rsa, alice_github.id_rsa.pu

    ```
    [remote "origin"]
    url = "ssh://git@github.com/alice/repo1.git"
    url = "ssh://git@github.com:alice/repo1.git"
    ```

    And finally you went to the repository `Settings > Deploy keys` section and added the contents of `alice_github.id_rsa.pub`
    @@ -53,13 +53,13 @@ So first thing is editing the `.git/config` files on your repo clones, so they l
    For repo1
    ```
    [remote "origin"]
    url = "ssh://git@repo1.github.com/alice/repo1.git"
    url = "ssh://git@repo1.github.com:alice/repo1.git"
    ```

    For repo2
    ```
    [remote "origin"]
    url = "ssh://git@repo2.github.com/alice/repo2.git"
    url = "ssh://git@repo2.github.com:alice/repo2.git"
    ```

    And then, on your `.ssh/config` file, now you will be able to enter a configuration for each subdomain :)
  2. gubatron revised this gist May 13, 2020. 1 changed file with 2 additions and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # How to configure multiple deploy keys for different private github repositories on the same computer without using ssh-agent

    Let's say `alice` is a github.com user, with 2 or more private repositories `repoN`.
    For this example we'll work with just two repositories named `repo1` and `repo2`

  3. gubatron revised this gist Oct 22, 2014. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions multiple-deploy-keys-multiple-private-repos-github-ssh-config.md
    Original file line number Diff line number Diff line change
    @@ -31,14 +31,16 @@ At this point you could do your `git pull origin master` without entering a pass

    ### but what about the second repository?

    So your instinct will be to grab that key and add it to `repo2`'s Deploy keys, but github will tell you that the key is already being used.
    So your instinct will be to grab that key and add it to `repo2`'s Deploy keys, but github.com will error out and tell you that the key is already being used.

    Now you go and generate another key (using `ssh-keygen -t rsa -C "alice@alice.com"` without passwords of course), and so that this doesn't become a mess, you will now name your keys like this:

    - Private: `repo1.alice_github.id_rsa`, Public: `repo1.alice_github.id_rsa.pub`
    - Private: `repo2.alice_github.id_rsa`, Public: `repo2.alice_github.id_rsa.pub`
    - `repo1` keypair: `(repo1.alice_github.id_rsa, repo1.alice_github.id_rsa.pub)`
    - `repo2` keypair: `(repo2.alice_github.id_rsa, repo2.alice_github.id_rsa.pub)`

    You will now put the new public key on `repo2`'s, github is happy, but now you have an ssh problem to deal with.
    You will now put the new public key on `repo2`'s Deploy keys configuration at github.com, but now you have an ssh problem to deal with.

    ### How can ssh tell which key to use if the repositories are hosted on the same `github.com` domain?

    Your `.ssh/config` file points to `github.com` and it doesn't know which key to use when it's time to do the pull.

  4. gubatron revised this gist Oct 22, 2014. 1 changed file with 1 addition and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ And finally you went to the repository `Settings > Deploy keys` section and adde

    At this point you could do your `git pull origin master` without entering a password without issue.

    but what about the second repository?
    ### but what about the second repository?

    So your instinct will be to grab that key and add it to `repo2`'s Deploy keys, but github will tell you that the key is already being used.

  5. gubatron revised this gist Oct 22, 2014. 1 changed file with 5 additions and 4 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    Let's say `alice` is a github.com user, with 2 or more private repositories `repoN`.
    For this example we'll work with just two repositories named `repo1` and `repo2`

    https://github.com/alice/repo1
    https://github.com/alice/repo2
    `https://github.com/alice/repo1`

    `https://github.com/alice/repo2`

    You need to be to pull from these repositories without entering a passwords probably on a server, or on multiple servers.
    You want to perform `git pull origin master` for example, and you want this to happen without asking for a password.
    @@ -34,8 +35,8 @@ So your instinct will be to grab that key and add it to `repo2`'s Deploy keys, b

    Now you go and generate another key (using `ssh-keygen -t rsa -C "alice@alice.com"` without passwords of course), and so that this doesn't become a mess, you will now name your keys like this:

    Private: `repo1.alice_github.id_rsa`, Public: `repo1.alice_github.id_rsa.pub`
    Private: `repo2.alice_github.id_rsa`, Public: `repo2.alice_github.id_rsa.pub`
    - Private: `repo1.alice_github.id_rsa`, Public: `repo1.alice_github.id_rsa.pub`
    - Private: `repo2.alice_github.id_rsa`, Public: `repo2.alice_github.id_rsa.pub`

    You will now put the new public key on `repo2`'s, github is happy, but now you have an ssh problem to deal with.

  6. gubatron created this gist Oct 22, 2014.
    78 changes: 78 additions & 0 deletions multiple-deploy-keys-multiple-private-repos-github-ssh-config.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    Let's say `alice` is a github.com user, with 2 or more private repositories `repoN`.
    For this example we'll work with just two repositories named `repo1` and `repo2`

    https://github.com/alice/repo1
    https://github.com/alice/repo2

    You need to be to pull from these repositories without entering a passwords probably on a server, or on multiple servers.
    You want to perform `git pull origin master` for example, and you want this to happen without asking for a password.

    You don't like dealing with ssh-agent, you have discovered (or you're discovering now) about `~/.ssh/config` a file that let's your ssh client know what private key to use depending on Hostname and username, with a simple configuration entry that looks like this:

    ```
    Host github.com
    HostName github.com
    User git
    IdentityFile /home/alice/.ssh/alice_github.id_rsa
    IdentitiesOnly yes
    ```

    So you went ahead and created your `(alice_github.id_rsa, alice_github.id_rsa.pub)` keypair, you then also went to your repository's `.git/config` file and you modified the url of your remote `origin` to be something like this:

    ```
    [remote "origin"]
    url = "ssh://git@github.com/alice/repo1.git"
    ```

    And finally you went to the repository `Settings > Deploy keys` section and added the contents of `alice_github.id_rsa.pub`

    At this point you could do your `git pull origin master` without entering a password without issue.

    but what about the second repository?

    So your instinct will be to grab that key and add it to `repo2`'s Deploy keys, but github will tell you that the key is already being used.

    Now you go and generate another key (using `ssh-keygen -t rsa -C "alice@alice.com"` without passwords of course), and so that this doesn't become a mess, you will now name your keys like this:

    Private: `repo1.alice_github.id_rsa`, Public: `repo1.alice_github.id_rsa.pub`
    Private: `repo2.alice_github.id_rsa`, Public: `repo2.alice_github.id_rsa.pub`

    You will now put the new public key on `repo2`'s, github is happy, but now you have an ssh problem to deal with.

    Your `.ssh/config` file points to `github.com` and it doesn't know which key to use when it's time to do the pull.

    So I found a trick with github.com. You can tell your ssh client that each repository lives in a different github.com subdomain, in these cases, they will be `repo1.github.com` and `repo2.github.com`

    So first thing is editing the `.git/config` files on your repo clones, so they look like this instead:

    For repo1
    ```
    [remote "origin"]
    url = "ssh://git@repo1.github.com/alice/repo1.git"
    ```

    For repo2
    ```
    [remote "origin"]
    url = "ssh://git@repo2.github.com/alice/repo2.git"
    ```

    And then, on your `.ssh/config` file, now you will be able to enter a configuration for each subdomain :)

    ```
    Host repo1.github.com
    HostName github.com
    User git
    IdentityFile /home/alice/.ssh/repo1.alice_github.id_rsa
    IdentitiesOnly yes
    Host repo2.github.com
    HostName github.com
    User git
    IdentityFile /home/alice/.ssh/repo2.alice_github.id_rsa
    IdentitiesOnly yes
    ```

    Now you are able to `git pull origin master` without entering any passwords from both repositories.

    If you have multiple machines, you could copy the keys to each of the machines and reuse them, but I'd advise doing the leg work to generate 1 key per machine and repo. You will have a lot more keys to handle, but you will be less vulnerable if one gets compromised.