Generating SSH Key for Authentication
Overview
- Generate a new SSH Key
- Start SSH Agent
- Create Config file
- Modify Config file
- Add SSH key to SSH Agent
- Add SSH key to Github Account
Generating a new SSH key
- To create a new ssh key, using the provided email as a label, execute one of the commands below substituting your Github email address
-
For modern unix operating systems
ssh-keygen -t ed25519 -C "your_email@example.com"
-
For legacy unix operating using without Ed25519 support:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-
-
An output comparable to the below text display
Generating public/private ed25519 key pair.
-
Press enter upon being prompted to
Enter a file in which to save the key
. This accepts the default file location.Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
-
Press enter upon being prompted to
Enter passphrase (empty for no passphrase): [Type a passphrase]
.Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again]
Start SSH Agent
-
Execute the command below to Start the ssh-agent in the background.
eval "$(ssh-agent -s)"
Create SSH Config File
-
Execute the command below to create a new
config
file.touch ~/.ssh/config
-
Execute the command below to begin editing the newly created
config
file innano
nano ~/.ssh/config
-
Paste the code below into the
config
file. Modify the value of theIdentityFile
respectively.- If you chose to add a passphrase to your key,
UseKeychain
should be set toyes
.
Host * AddKeysToAgent yes UseKeychain no IdentityFile ~/.ssh/id_rsa
- If you chose to add a passphrase to your key,
Add SSH Key to SSH Agent
-
Execute the command below to add your SSH private key to the ssh-agent and store your passphrase in the keychain. Ensure
id_ed25519
in the command is substituted with the name of your private key file.$ ssh-add -K ~/.ssh/id_rsa
Adding SSH Key to Github
-
Execute the command below to copy the SSH public key to your clipboard.
$ pbcopy < ~/.ssh/id_rsa.pub
-
In the upper-right corner of any page, click your profile photo, then click Settings.
-
In the user settings sidebar, click SSH and GPG keys.
-
Click New SSH key or Add SSH key.
-
In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key “Personal MacBook Air”.
-
Paste your key into the “Key” field.
-
Click Add SSH key.
-
If prompted, confirm your GitHub password.
Use Keychain Helper
- Execute the command below to ensure that once you are authenticated successfully, your credentials are stored in the macOS keychain and will be used each time you
push
orpull
from an HTTPS URL.git config --global credential.helper osxkeychain