Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dungfly98/cbd2a9839bac30046972f57e0e5a47c2 to your computer and use it in GitHub Desktop.

Select an option

Save dungfly98/cbd2a9839bac30046972f57e0e5a47c2 to your computer and use it in GitHub Desktop.

Revisions

  1. @bonnopc bonnopc created this gist Nov 23, 2021.
    61 changes: 61 additions & 0 deletions multipleSSHkeysForUnix.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    # Enable Multiple SSH Keys for UNIX Based OS

    Follow these steps below to enable multiple SSH keys on your device with UNIX Based OS (MacOS/ Ubuntu/ Debian etc.). Suppose, you have two different users/ accounts, one is `personalAccount` and another is `companyAccount`. And you have already a default key configured with `personalAccount`. (If you haven't set up your default ssh-key yet, please [follow this article](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account) before going ahead with these steps described below.)

    ### 1. Generate another ssh-key
    Generate a new ssh-key for your `companyAccount`.
    ```cmd
    cd ~/.ssh
    ssh-keygen -t rsa -C "[email protected]"
    ```

    ### 2. Clear previous cached keys
    ```cmd
    ssh-add -D
    ```

    ### 3. Add Keys
    Then add your keys as following -
    ```cmd
    ssh-add ~/.ssh/id_rsa
    ssh-add ~/.ssh/id_rsa_company
    ```

    ### 4. Check added Keys
    You can always check your keys by entering following command
    ```cmd
    ssh-add -l
    ```

    ### 5. Create or Update config
    You may have no `config` file right now. to create a config enter
    ```cmd
    touch config
    ```

    Or maybe, you already have a `config` file in your `~/.ssh` directory. Now you have to edit & save this file and include these lines as described below. (You can use `nano config` or open and edit with your favorite editor.)
    ```md
    Host personalAccount.github.com
    HostName github.com
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_rsa

    Host companyAccount.github.com
    HostName github.com
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_rsa_company
    ```

    As you can see here, you'll be using two hosts as `personalAccount.github.com` & `companyAccount.github.com`.

    ### 6. Cloning a (Private) Repository
    You can always use `git clone [email protected]:someUserName/example-repo.git` by using your default or `personalAccount` credentials. But you have to change the url while cloning from the `companyAccount` repositories.

    Suppose we have a repo url of something like `[email protected]:companyAccount/some-private-repo.git`, then you have to change this url and clone this repo as following -

    ```cmd
    git clone [email protected]:companyAccount/some-private-repo.git
    ```
    Note: the host name (`companyAccount.github.com`) should match with the Host described as in the `config` file.