Skip to content

Instantly share code, notes, and snippets.

@David-Araripe
Created April 2, 2024 13:44
Show Gist options
  • Select an option

  • Save David-Araripe/d1adf167bf0c8c1229cede9073718877 to your computer and use it in GitHub Desktop.

Select an option

Save David-Araripe/d1adf167bf0c8c1229cede9073718877 to your computer and use it in GitHub Desktop.

Setup ssh keys for git authentication

Creating your ssh key

For creating an ssh key, you can use the command ssh-keygen. If you'd like to use a custom filename for your key, you can include that by adding the -f argumet, such as in the example:

ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/my_custom_key_rsa

Additionally, you can opt for other key configurations, such as ed25519. For this, just change the -t parameter. If you're looking for more information, you can check here this security.stackexchange answer

Editing .ssh/config

Now that you've created your key, you can open (or create if it doesn't exist) the ~/.ssh/config file. We'll work with github on this example, so if you're working on gitlab or on something else, change Host and HostName accordingly. For github, you can add the lines:

Host github
    HostName github.com
    User git
    IdentityFile ~/.ssh/my_custom_key_rsa
    IdentitiesOnly yes
Troubleshooting SSH File Permissions

If you encounter permission errors with SSH, ensure your `.ssh` directory and the files within it have the correct permissions set:

  • .ssh directory should be 700 (drwx------).
  • Private keys (my_custom_key_rsa) should be 600 (-rw-------).
  • Public keys (my_custom_key_rsa.pub) can be 644 (-rw-r--r--).
  • The config file should also be 600 (-rw-------).

You can set these permissions with the following commands:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/my_custom_key_rsa
chmod 644 ~/.ssh/my_custom_key_rsa.pub
chmod 600 ~/.ssh/config

After adjusting these permissions, try your SSH connection again.

Please make sure that you add the generated ssh key to your github account. If you're not familiar with this step, please follow this tutorial.

Test your ssh connection

To ensure everything is set up correctly, you can test your SSH connection with the following command:

ssh -T git@github

clone using ssh

Now, when you clone a repository from github, use the SSH URL, which looks something like this:

git clone git@github:username/repository.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment