Skip to content

Instantly share code, notes, and snippets.

@edoves
Created October 30, 2024 14:37
Show Gist options
  • Save edoves/6f1401568a8e6ac42029f1ca0c73ff8c to your computer and use it in GitHub Desktop.
Save edoves/6f1401568a8e6ac42029f1ca0c73ff8c to your computer and use it in GitHub Desktop.
Generate a New SSH Key and Add it to your GitHub

Generate a New SSH Key and Add it to your GitHub

After following this video in setting up your ssh-key with a custom algo like id_custom you might end up having error. the next step is add a configuration

To create or update an SSH config file (~/.ssh/config), follow these steps:

1. Open the Terminal

2. Open the Config File

Run this command to open the ~/.ssh/config file using a text editor (e.g., nano). If the file doesn’t exist, this command will create it:

nano ~/.ssh/config

3. Add the Configuration

Paste the following configuration into the file:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa
  • Host: This is a nickname for the configuration. github.com is standard when configuring GitHub.
  • HostName: The actual hostname of the remote server (in this case, github.com).
  • User: The username for connecting to the host (GitHub uses git).
  • IdentityFile: The path to your private SSH key (e.g., ~/.ssh/id_rsa). Make sure this matches your actual key file.

If you’re using a different SSH key file (e.g., ~/.ssh/github_rsa), update the IdentityFile path accordingly.

4. Save the File

  • If you’re using nano, save the file by pressing CTRL + O, then Enter.
  • Exit the editor by pressing CTRL + X.

5. Set the Correct Permissions

Make sure the permissions for your ~/.ssh/config file are secure:

chmod 600 ~/.ssh/config

6. Test the Configuration

Try connecting to GitHub again to make sure everything works:

If it succeeds, you should see a message like:

Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

This confirms that the configuration is working correctly. If you encounter any errors, let me know!

How to Use Multiple GitHub Accounts on the Same Machine?Video

To use multiple GitHub accounts on the same machine, follow these steps:

1. Set Up SSH Keys for Each GitHub Account

Each GitHub account should have its own SSH key pair.

  1. Generate a new SSH key for each account:

    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    • When prompted, save the key under a specific name (e.g., ~/.ssh/id_rsa_personal for your personal account and ~/.ssh/id_rsa_work for your work account).
  2. Add the SSH keys to the SSH agent:

    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa_personal
    ssh-add ~/.ssh/id_rsa_work
  3. Copy the SSH public key and add it to the corresponding GitHub account:

    cat ~/.ssh/id_rsa_personal.pub
    cat ~/.ssh/id_rsa_work.pub
    • Copy the output and add it as an SSH key in the settings of each GitHub account.

2. Configure SSH Config File

Edit your SSH config file (~/.ssh/config) to map each SSH key to a specific GitHub account:

# Personal GitHub Account
Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal

# Work GitHub Account
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_work

3. Clone Repositories Using the Configured Hosts

When cloning or adding a remote repository, use the configured host instead of the default github.com. For example:

  • For your personal account:
    git clone git@github-personal:username/repo.git
  • For your work account:
    git clone git@github-work:username/repo.git

4. Set Up Git User Configurations for Each Repository

You can set up specific user credentials for each repository so commits are linked to the right GitHub account.

  1. Navigate to the repository folder:

    cd path/to/repo
  2. Set up the local user email and name:

    git config user.email "[email protected]"
    git config user.name "Your Name"

    or for your work repo:

    git config user.email "[email protected]"
    git config user.name "Your Name"

5. Switch Accounts for Existing Repositories

If you need to switch the SSH key for an existing repository:

  1. Open the repository's .git/config file.

  2. Update the url under [remote "origin"]:

    • Change github.com to either github-personal or github-work based on the account.

    For example:

    [remote "origin"]
    url = git@github-personal:username/repo.git

This setup allows you to work seamlessly with multiple GitHub accounts on the same machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment