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/

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


Add the public key by going to the Settings and then to SSH and GPG keys:

SSH Key on GitHub


Add the public key by going to the Settings and then to SSH keys:

SSH Key on GitLab


Add the public key by going to the Bitbucket Settings and then to 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