-
Notifications
You must be signed in to change notification settings - Fork 73
How‐Tos‐Setting‐Up‐Secure‐SSH‐Keys
Michael Collins edited this page Feb 7, 2024
·
6 revisions
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.
- Open a terminal.
- Run the following command, replacing
[email protected]
with your Github email address &your_key_name
with an appropriate name.
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/your_key_name
- At the prompt, "Enter passphrase (empty for no passphrase)," enter a secure passphrase for the key.
- 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
- Navigate to your Github Key Settings
- Click
New SSH key
. - In the "Title" field, add a descriptive label for the new key.
- Paste your key into the "Key" field.
- Click
Add SSH key
.
- Create a new SSH config file for your key, replacing
name-of-your-config
with an appropriate name.
touch ~/.ssh/name-of-your-config
- 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.
- Navigate to your local Git repository in the terminal.
- 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.