DEV Community

Pratik Thapa
Pratik Thapa

Posted on

Setting up SSH Keys on Mac for Github

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.

  • 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 that you can now use.
ssh-keygen -t ed25519 -C your.email.address@mail.com
Enter fullscreen mode Exit fullscreen mode

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.

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

  • Passphrase:

    • Hit enter for no passphrase.
    • If you enter a passphrase in this step, it provides extra securtiy but github will frequently ask you to enter this exact passphrase during pull/push of repo. You can also save this passphrase in the keychain, so you don't have to enter it every single time.
  • 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)"
Enter fullscreen mode Exit fullscreen mode

This should print out something like this in the terminal.

Agent pid 12345
Enter fullscreen mode Exit fullscreen mode

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

  • 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 if the ssh-agent exists in your mac already. To see it, run

~.ssh/config
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

touch ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode
  • Now the file is created, edit the ssh-agent. Create the wallet to put the id 😉
    vim ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

To add your private key to the ssh agent type

HOST *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

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

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

HOST *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed2
  UseKeychain yes
Enter fullscreen mode Exit fullscreen mode

Then, save and quit vim

  • press esc in your keyboard.
  • type: :wq to write/save 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.

  • Finally add the id into the wallet. Or Add the ssh key into the file we created.
ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode
  • 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.
  • To test if the ssh key is working, go to the terminal and type

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

That's it!

I am new to this process, feel free to critique me in the comments if what I am saying is wrong.

Top comments (0)