Generate SSH key on Linux for GitHub, GitLab Bitbucket or other services

Posted by Harald Nezbeda on Tue 24 September 2019

This isn’t complicated but in the perspective of a software developer it is done only once after the OS setup or latter when required for another service.

The command for this is simple:

    ssh-keygen -t rsa

Following the instructions this will generate the RSA file and a Public Key. Done … almost! Keep also in mind to set the permissions for the folder and for the generated file. Here is an example for the default files that the command above is generating:

    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/id_rsa
    chmod 644 ~/.ssh/id_rsa.pub

After being done with this you can go on and copy the content of the id_ras.pub file intro the required service and use this for authenticating over SSH.

GitHub

Add the public key by going to the Settings and then to SSH and GPG keys: https://github.com/settings/keys

SSH Key on GitHub

GitLab

Add the public key by going to the Settings and then to SSH keys: https://gitlab.com/profile/keys

SSH Key on GitLab

Bitbucket

Add the public key by going to the Bitbucket Settings and then to SSH keys: https://bitbucket.org/account/user//ssh-keys/ (For Bitbukcet the link to the settings requires your user id)

SSH Key on Bitbucket

SSH Agent on Servers

If you run the above commands on a server you might end up with an authentication error. This can happen either because the ssh-agent is not running or because the key that was generated has not been added to the ssh-agent. To fix this you need to start the ssh-agent:

eval "$(ssh-agent -s)"

and add the generated key to the ssh-agent (make sure to replace id_rsa with the name you choose for the key):

ssh-add ~/.ssh/id_rsa