Setting up SSH Keys on Mac for GitHub

Setting up SSH Keys on Mac for GitHub

ยท

4 min read

This tutorial assumes that you have not set up SSH keys in your machine(mac) before. Right now this tutorial only focuses on mac since that's what I am using.

  1. The first thing to do is to create ssh keys in your machine. In mac, make sure you are on the home directory i.e: /users/pratikthapa. When you run this, it creates a new key you can use.
ssh-keygen -t ed25519 -C your.email.address@mail.com

Here: -t ed25519 is the type of encryption we are using. If you machine is not compatible with this encryption, you can also use rsa. Read full documentation here -C is the contact email associated with the key we are creating.

  1. Location: After running the command above, it will ask where do you want to save this. Just hit enter.

  2. Passphrase:

    • Hit enter for no passphrase.

    • If you enter a passphrase in this step, it provides extra security but GitHub will frequently ask you to enter this exact passphrase during the pull/push of the repo. You can also save this passphrase in the keychain, so you don't have to enter it every time.

  3. Now that you've created ssh keys, you need to make sure your system's ssh-agent knows about it. It's like a wallet that holds multiple identity cards. To do that, in the same terminal, run

eval "$(ssh-agent -s)"

This should print out somethig like this in the terminal.

Agent pid 12345

If you see this, it means that the machine's ssh-agent was able to read/evaluate the ssh key.

  1. Now, we need to put the key into the system's ssh-agent. Think of it as putting the id in the wallet so that we can present it whenever it is necessary.

First, make sure the ssh-agent exists on your mac already. To see it, run

~.ssh/config

Here, ~ represent the home directory.

if the ssh-agent is not present you should see, zsh: no such file or directory: /Users/pratikthapa/.ssh/config

To make sure that the file does not exists visually, run

ls -a

When you are sure that the .ssh/config is not present, run the following commad to create it.

touch ~/.ssh/config
  1. Now the file is created, edit the ssh-agent. Create the wallet to put the id ๐Ÿ˜‰
    vim ~/.ssh/config

To add your private key to the ssh agent type

HOST *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519

Here: /.ssh/ is the folder name id_ed25519 is the file that was created in step 1.

If you used the passphrase in step 3 above do the following to add your passphrase to the keychain.

HOST *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed2
  UseKeychain yes

Then, save and quit vim

  • press esc on your keyboard.

  • type: :wq to write and quit out of vim.

Instead of vim you can also use other tools like visual studio code to edit the file. Open the file using code ~/.ssh/config and follow the same steps above. Or you can simply navigate to the folder, press command + shift + . to see the hideen files and open the config file manually with vs code.

  1. Finally, add the id to the wallet. In other words, Add the ssh key to the file we created.
ssh-add ~/.ssh/id_ed25519
  1. That's it. Now, you need to navigate to GitHub and add your ssh keys there. Basically, presenting your id to GitHub.

    • Open github.com and login

    • Navigate to settings

    • Click on SSH and GPG keys

    • Click on New SSH key button.

    • On the title, write the machine name or whatever you want to identify where the ssh key is coming from and what kind it is.

    • On the key section paste the ssh key from the machine.

      • First, you need to read the file. Run, cat ~/.ssh/id_ed25519.pub. โš ๏ธ Here, .pub means public key.

        • copy the key and paste it.
    • Click Add SSH Key.

    • GitHub will likely ask for your account password.

      • Enter your password and you are done.
  2. To test if the ssh key is working, go to the terminal and type

ssh -T git@github.com

You should see something like this in the terminal

The authenticity of host 'github.com ***'......
You have successfully authenticated, but GitHub does not provide shell access.

That's it!

Did you find this article valuable?

Support Pratik Thapa by becoming a sponsor. Any amount is appreciated!

ย