Generate an SSH key

Move to the ssh directory where the keys and configuration will be stored. The standard location for *nix-like systems is /etc/ssh.

cd /ect/ssh
ssh-keygen -t rsa -b 4096 -C ""

-t - key type, in this case RSA
-b - key length, in this case 4096
-C - comment (any string can be used)

Next, the following prompts appear:

Enter file in which to save the key - the file name for the keys (required field). In this case, enter “UserUser”.

Enter passphrase - the passphrase. If you want to enter a password every time you use the key, enter it. If not, leave it empty.

Enter same passphrase again - enter the same value as in the previous step.

Done, the keys have been created. One is private, the other is public. The one ending in .pub is public.

SSH configuration setup

In the ssh directory /etc/ssh, edit the SSH client file with any convenient editor, for example nano.

  • sshd_config - server side

  • ssh_config - client side

nano ./ssh_config

Add the following lines to the configuration:

Host UserUser_github
HostName github.com
User git
IdentityFile /etc/ssh/UserUser

HostName - the host name where we will go and where the key will be used. In this case, it is GitHub.com
User - the user under which we will log in. In this case, the git program will connect to GitHub
IdentityFile - path to the private key file, without .pub
Host - alias

One or multiple accounts

If only one GitHub account is used on the machine, using Host is not required. If there are several accounts, it is necessary, because lookup will be performed by alias.

For example, a repository link looks like this:

[email protected]:UserUser/repository.git

Without an alias - only one account

git looks at the field containing github.com (the part after @). Then it finds a matching HostName in the ssh_config file, takes the IdentityFile key for that HostName, and then executes the command.

With an alias - multiple accounts

The field that previously contained github.com must be replaced with the alias and take this form:

git@UserUser_github:UserUser/repository.git

Now git takes the UserUser_github alias, finds it among multiple configurations in ssh_config, substitutes HostName in its place, takes the IdentityFile key for this HostName, and executes the command.

In other words, the alias is primary when searching for the required configuration.

GitHub-side setup

In the /etc/ssh directory, print the contents of the public key file. In this case, it is UserUser.pub.

Then go to GitHub and follow this path:

GitHub home page -> Settings -> SSH and GPG keys -> New ssh key

Give the key a name in the Title field and add the contents of the UserUser.pub public key to the Key field.

Click Save.

Now you can commit, push, clone, and so on.