SSH (Secure Shell) keys play a crucial role in ensuring that your data remains protected during transit. In this tutorial, we will walk you through the steps to generate a new SSH key and quickly add it to your Mac.
Open Terminal
You can find Terminal in your Applications folder under Utilities or by searching for it using Spotlight (press Command + Space
and type "Terminal").
Check for existing SSH keys
Run the following command to see if you already have SSH keys:
ls -al ~/.ssh
If you see files named id_rsa
and id_rsa.pub
, you already have SSH keys. If not, continue below.
Generate a new SSH key
Use the command below to generate a new SSH key. Replace your email with the one you use for Git:
ssh-keygen -t ed25519 -C "your_email@example.com"
When prompted, you can press Enter to accept the default file location (usually ~/.ssh/id_ed25519
). You can also set a passphrase for added security.
You can now verify that the keys have been created, by running the following command:
ls -l ~/.ssh
You will normally see that the two keys have been created.
Start the SSH agent
Run the following command to start the SSH agent:
eval "$(ssh-agent -s)"
It will print out something like this:
Agent pid 18608
Add a config file for your keys
If you're using macOS Sierra 10.12.2 or later, you will need to modify your ~/.ssh/config
file to automatically load keys into the ssh-agent and store passphrases in your keychain.
If the file doesn't exist, create the file.
touch ~/.ssh/config
Open your ~/.ssh/config
file, then modify the file to contain the following lines.
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
If your SSH key file has a different name or path than the example code, modify the filename or path to match your current setup. And, If you chose not to add a passphrase to your key, you should omit the
UseKeychain
line.Add your SSH key to the SSH agent
Use the following command to add your SSH key:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
Copy the SSH key to your clipboard
You can copy your SSH public key to your clipboard with this command:
pbcopy < ~/.ssh/id_id_ed25519.pub
Add the SSH key to your Git account
- Go to your Git hosting service (like GitHub, GitLab, or Bitbucket).
- Navigate to SSH key settings (typically found in account settings).
- Paste your SSH key into the provided field and save it.
Test your SSH connection
Run the following command to test your SSH connection:
ssh -T git@github.com
You will see a prompt that says, "The authenticity of host 'github.com (ip_address)' can't be established." You will see the key fingerprint in the format SHA256:...
.
The first time, it's usual to see a prompt that says, "The authenticity of host 'github.com (140.82.121.3)' can't be established." You will see the key fingerprint in the format
SHA256:...
.Add the fingerprint to the known_hosts file
When you see the prompt about the authenticity of the host, verify the fingerprint against the official GitHub SSH key fingerprints found here. If it matches, type yes
and press Enter to add the host to your known_hosts
file.
Conclusion
You have successfully installed and configured your Git SSH key on your Mac and added the fingerprint to your known_hosts
file! You can now use Git with SSH authentication.
π. Similar posts
Why Are My React Components Re-rendering Too Much?
26 Jul 2025
How to Use Custom Hooks in React
01 Jun 2025
Understanding Stale Values in React
17 May 2025