DEV Community

Cover image for SSH key on github - Windows and Linux
Ryan Souza
Ryan Souza

Posted on • Updated on • Originally published at ryan.dev.br

SSH key on github - Windows and Linux

What is a SSH Key?

An SSH key is a pair of keys used to authenticate a user on a server. The public key is used to encrypt data, and the private key is used to decrypt it. The public key is shared with the server, while the private key is kept secret by the user.

There are various encryption algorithms to generate SSH keys, but in this tutorial, I will teach you how to generate an SSH key using the ED25519 algorithm. ED25519 is an elliptic curve digital signature algorithm considered secure and fast.

To learn more about this topic, I recommend this article from Teleport: Article (in English)

Generating a SSH Key

From now on, I will divide this tutorial into two parts, one for Linux and another for Windows.

Before you start, you need to have Git installed on your machine. If you don't, I recommend the official Git documentation to install Git on your machine: Documentation

Linux

To generate an SSH key on Linux, you need to open the terminal and type the following command:

ssh-keygen -t ed25519 -C <your-email>
Enter fullscreen mode Exit fullscreen mode

ssh-keygen on linux

After typing the command, you will be prompted to fill in some information, such as the key name and password. You can type any name as long as you don't forget it; in my case, I just pressed enter so that Git would automatically generate the key name and have no password.

With the key generated, you should copy the public key to the clipboard. For this, navigate to the folder where the key was generated (by default it is in the ~/.ssh folder, as shown in the previous image) and type the following command:

cat id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

and copy the key that appears on the screen.

Now you need to go to the GitHub settings page and add the key you just copied.

here a shortcut for you to go directly to the GitHub SSH keys settings page: SSH Keys Settings Page

here you should go to New SSH key and paste the key you copied earlier.

paste key on github - linux

By clicking on Add SSH key, you will add the SSH key to GitHub.

Now, just test if the key was added correctly and clone any repository you have access to, always using the SSH address. To test if the key was added correctly, you can type the following command:

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

if everything goes well, you will see a success message, and you can clone any repository you have access to; in my case, I cloned my own repository.

Testing ssh key - linux

Windows

The same process as Linux except that here I used PowerShell to create the SSH key and Git Bash to test the SSH key.

First, you need to open PowerShell and type the following command:

ssh-keygen -t ed25519 -C <your-email>
Enter fullscreen mode Exit fullscreen mode

replace <your-email> with your GitHub email.

ssh-keygen on windows

After typing the command, you will be prompted to fill in some information, such as the key name and password. You can type any name as long as you don't forget it; in my case, I just pressed enter so that Git would automatically generate the key name and have no password.

With the key generated, you should copy the public key to the clipboard. For this, navigate to the folder where the key was generated (by default it is in the C:\Users\ryans\.ssh folder, as shown in the previous image) and type the following command:

type id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

and copy the key that appears on the screen.

getting ssh key in windows

Finally, now you need to go to the GitHub settings page and add the key you just copied. here a shortcut for you to go directly to the GitHub SSH keys settings page: SSH Keys Settings Page

here you should go to New SSH key and paste the key you copied earlier.

paste key on github - windows

By clicking on Add SSH key, you will add the SSH key to GitHub.

Now, just test if the key was added correctly and clone any repository you have access to, always using the SSH address. To test if the key was added correctly, you can type the following command:

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

if everything goes well, you will see a success message, and you can clone any repository you have access to; in my case, I cloned my own repository.

Testing ssh key - linux

References

I hope this tutorial has helped you set up your SSH key on github. If you have any questions or suggestions, leave a comment below or contact me through LinkedIn or the contact tab on my website.

Top comments (9)

Collapse
 
proscatinatingpro profile image
Trung Ly

Nice article! If I may add:

I'd like to add getting to know the ssh_config file.

# github account
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_myaccount_github

# gitlab account
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_myaccount_gitlab

# gitlab company account
Host gitlab.my_company.com
HostName gitlab.my_company.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_mycompanyaccount_gitlab
Enter fullscreen mode Exit fullscreen mode

Reference: gist.github.com/alejandro-martin/a...

Lastly, make sure to use the right permissions to avoid this error:

$ ssh localhost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0777 for '/root/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/root/.ssh/id_rsa": bad permissions
[...]
Enter fullscreen mode Exit fullscreen mode
  • .ssh directory: 700 (drwx------)
  • public key (.pub files): 644 (-rw-r--r--)
  • private key (id_rsa): 600 (-rw-------)
Directory or File Man Page Recommended Permissions Mandatory Permissions
~/.ssh/ There is no general requirement to keep the entire contents of this directory secret, but the recommended permissions are read/write/execute for the user, and not accessible by others. 700
~/.ssh/authorized_keys This file is not highly sensitive, but the recommended permissions are read/write for the user, and not accessible by others 600
~/.ssh/config Because of the potential for abuse, this file must have strict permissions: read/write for the user, and not accessible by others. It may be group-writable provided that the group in question contains only the user. 600
~/.ssh/identity
~/.ssh/id_dsa
~/.ssh/id_rsa
These files contain sensitive data and should be readable by the user but not accessible by others (read/write/execute) 600

Reference: frankindev.com/2020/11/26/permissi...

Collapse
 
ryrden profile image
Ryan Souza

Pretty cool, Having multiple SSH keys looks like something that I'll need in the future. Also, feel free to open a pull request to add this as an Extra Or bonus in the link: github.com/ryrden/ryan.dev/blob/ma...

the post was originally posted at ryan.dev.br/2023-04-17-github-ssh-en/

Collapse
 
disane profile image
Marco

Thank you, always forget this!

Collapse
 
ryrden profile image
Ryan Souza

Me too, I wrote this article to use as a note when I forget

Collapse
 
thomasbnt profile image
Thomas Bnt ☕

Great post 🚀

Collapse
 
ryrden profile image
Ryan Souza

Thanks!!

Collapse
 
chrisngoran profile image
chrisngoran

great job . Thanks

Collapse
 
manchicken profile image
Mike Stemle

(I’m pretty sure you already know this, but you should probably delete and do not use the key that you used as an example)

Collapse
 
ryrden profile image
Ryan Souza

I know man, thanks for the heads up!