So you just got a new MacBook, you already installed your IDE and setup your dev environment, the last bit of the puzzle is for you to pull down your remote repo from GitHub, write some code and then push up that bad boi. Well not so fast, because you haven’t connected your new computer to your github account, github won’t let you run a ‘git push’ command. So to fix that, we need to setup your SSH keys and install it on your GitHub account. There are 5 things we need to do to make sure everything goes smoothly:
- We need to Generate a Pair of Private and Public SSH Key on Your Mac
- Add the SSH Key to Your SSH Agent
- Copy the Public Key to Your Clipboard
- Add the newly generated SSH Key to Your GitHub Account
- Then Test Your SSH Connection to make sure everything is good
Here’s everything in details
Generate a Pair of Private and Public SSH Key on Your Mac
Open the Terminal on your Mac and execute the command below, make sure you replace this "[email protected]"
with the actual email address you use for your github account.
1
ssh-keygen -t rsa -b 4096 -C "[email protected]"
When prompted, press Enter to save the SSH key pair in the default location (~/.ssh/id_rsa)
and optionally set a passphrase for added security. If you don’t want to add a passphrase just click enter twice.
The ssh-keygen
command helps us generate a new SSH key pair. And Here’s what each option does:
-t rsa
: Specifies the type of key to create (RSA).-b 4096
: Specifies the number of bits in the key (4096 bits for stronger security).-C "[email protected]"
: Provides a comment, usually your email address, to identify the key. Please make sure this email address is the same as the one you’re using in your github.com account.
Add the SSH Key to Your SSH Agent:
We need to Start the SSH agent by running this command:
1
eval "$(ssh-agent -s)"
Now let’s add our SSH private key to the SSH agent:
1
ssh-add ~/.ssh/id_rsa
-
The
ssh-agent
is a program that manages SSH keys. We start it with eval"$(ssh-agent -s)"
command. -
While the
ssh-add
command adds the SSH private key to the SSH agent. This allows us to use the private key for authenticating our Github account without having to enter the passphrase every time.
Copy the Public Key to Your Clipboard:
We are almost done, let’s use the following command to copy the SSH public key to our clipboard:
1
pbcopy < ~/.ssh/id_rsa.pub
The pbcopy
command is specific to macOS and allows us to copy the contents of the file in our (~/.ssh/id_rsa.pub
path, which is our SSH public key) to the clipboard.
This step prepares the public key to be added to our GitHub account on github.com.
Add SSH Key to Your GitHub Account:
With the keys generated and the public key copied to the clipboard let us go right ahead and log in to our GitHub account and navigate to the settings where we can manage SSH keys.
- Go to the GitHub website and log in to your account.
- Navigate to “Settings” > “SSH and GPG keys”.
- Click on “New SSH key” Button
- Right click and Paste your public key into the provided field and give it a relevant title.
- Click on “Add SSH key”.
It should look something like this:
PSA The value in the image is not a real SSH key, that’s just a stubbed data for demo.
Our GitHub account will now recognize the new Mac as a trusted device for authentication. GitHub uses this SSH key to authenticate us when we interact with repositories via SSH.
Test SSH Connection:
This part is optional but we can use it to test and make sure everything was successful. Back in the terminal, let’s run the following command to test our SSH connection to GitHub:
1
ssh -T [email protected]
You may be prompted to confirm the connection, type “yes” and press Enter. If successful, you will see a message confirming your authentication.
Voila! We have established a secure SSH auth between our mac and github. We can now git push and pull as usual.