Skip to content

How‐Tos‐Setting‐Up‐Secure‐SSH‐Keys

Michael Collins edited this page Feb 7, 2024 · 6 revisions

Configure Secure SSH Keys

This setup allows you to use individual password protected SSH keys for individual repositories.

This guide will walk you through the process of creating password-protected SSH keys, adding them to your GitHub profile, creating a dedicated SSH config file for a specific key, and linking it to a local Git repository using a custom sshCommand.

Generate a New SSH Key

  1. Open a terminal.
  2. Run the following command, replacing your_email@example.com with your Github email address & your_key_name with an appropriate name.
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/your_key_name
  1. At the prompt, "Enter passphrase (empty for no passphrase)," enter a secure passphrase for the key.

Add the SSH Key to Your GitHub Account

  1. Copy the SSH key to your clipboard. (If pbcopy isn't available, install xclip or just open the file and copy its contents manually.)
pbcopy < ~/.ssh/your_key_name.pub
  1. Navigate to your Github Key Settings
  2. Click New SSH key.
  3. In the "Title" field, add a descriptive label for the new key.
  4. Paste your key into the "Key" field.
  5. Click Add SSH key.

Create a Dedicated SSH Config File

  1. Create a new SSH config file for your key, replacing name-of-your-config with an appropriate name.
touch ~/.ssh/name-of-your-config
  1. Open the file in a text editor and add the following configuration, adjusting as necessary for your setup:
echo "IdentityFile ~/.ssh/your_key_name" > filename.txt

This configuration tells SSH to use the your_key_name key for connections to github.com.

Link the SSH Config to a Git Repository

  1. Navigate to your local Git repository in the terminal.
  2. Use the following command to set the custom SSH command for the repository. This tells Git to use the specified SSH config file for operations related to this repository.
git config core.sshCommand "ssh -F ~/.ssh/your-key-name"

Repeat Steps 3 and 4 for each repository or SSH key you wish to configure, creating separate config files as needed.