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_rsaAdditionally, 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
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 yesTroubleshooting SSH File Permissions
If you encounter permission errors with SSH, ensure your `.ssh` directory and the files within it have the correct permissions set:
.sshdirectory should be700(drwx------).- Private keys (
my_custom_key_rsa) should be600(-rw-------). - Public keys (
my_custom_key_rsa.pub) can be644(-rw-r--r--). - The
configfile should also be600(-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/configAfter 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.
To ensure everything is set up correctly, you can test your SSH connection with the following command:
ssh -T git@githubNow, when you clone a repository from github, use the SSH URL, which looks something like this:
git clone git@github:username/repository.git