Create and use SSH key

This tutorial will explain how to create ssh key which you can use to access your Linux /Unix server without entering password, or just because this is securest and more professional way to access any server.

I presume that you have certain knowledge with SSH and unix /linux commands, so I am going directly to the point were we are going to generate ssh key.

Access your server, login as your preferred user and start ‘ssh-keygen’.
There are a lot of options with which you can play, as encryption type, how many bits it will be ‘¦ etc.
In our case we will use the default one just to show you the method. Once you manage to create key pair and it works, you can always go back and create another one.

So, type ‘ssh-keygen’ in the command line and fallow the instructions.

In my case I have created key pair with the default settings with passphrase.
If you want you can leave it blank and this will skip the part where you are entering a password at all.

By default ssk-keygen will create two files:

root/.ssh/id_rsa_root
root/.ssh/id_rsa_root.pub

The first one is the SSH private key (obviously the one you are to connect with), the second one the SSH public key (the one which has to mach the private key and stays on the server)

In order the public key to be used it must be included in a file called authorized_keys.
This file usually stays inside an invisible folder (.ssh) inside the user home folder.
In my case I created the key as ‘root’ so it will be: ‘/root/.ssh/’

If there is no such file you must create it and the easiest way is to parse the public key directly to the authorized_keys one.
This can be done with this command:

Maybe you noticed that my file is called ‘authorized_keys2’ instead of ‘authorized_keys’ .
I am not going to fall in particulars, just take it that you may have both variants.

To see on your server which one you have to use, you have to check that inside the ‘sshd’ configuration file (sshd_config). ‘sshd’ is the ssh deamon and it basically runs the ssh connection service.

We will just search the sshd_config file with the next command:

Ok, till now we spoke about the SSH public key, now we have to collect the private key.
In most of the cases and ssh key tutorials is shown something like:

What I did with this command was just to transfer the SSH public key on the server from which I am going to connect with it to the current one.

But this can be really easily passed as just go and do ‘cat’ on the SSH private key and copy and paste it inside a file on the other server.

$ cat id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEoQIBAAKCAQEArgTgbwlQ2axj9zRF4nPa4kA55+/OHoyWd7JOlSEJuMn8H2mu
j1mQw8epy1Ki3prTQ4m+q4jvSRPWidOheIBipQ/GkQLBCOEPKvUA5F5Mm7Lm8uR4

Filed Under: SSH

Anthony Gee About the Author: Anthony G. is an IT specialist with more than 9 years of solid working experience in the Web Hosting industry. Currently works as server support administrator, involved in consultative discussions about Web Hosting and server administration. One of the first writers in the Onlinehowto.net website, now writing for Free Tutorials community - he is publishing tutorials and articles for the wide public, as well as specific technical solutions.

Leave a Reply