| Type: |
Code 
|
| Level: |
Intermediate
|
| Date: |
2010-Feb-09
|
| Visited: |
473 times
|
| Rating: |

|
| Published: |
Tony Potter |
|
|
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.
[user@server home]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa_root
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa_root.
Your public key has been saved in /root/.ssh/id_rsa_root.pub.
The key fingerprint is:
fb:a3:c3:b5:92:21:44:a0:cd:41:1c:fa:86:a6:a4:09 user@server
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:
[user@server home]$ cat id_rsa_root.pub > authorized_keys2
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:
$ cat /etc/ssh/sshd_config |grep authorized
AuthorizedKeysFile .ssh/authorized_keys2
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:
$ scp id_rsa user@server_number2:.ssh
Password:
id_rsa 100% 4572 4.5KB/s 00:00
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
Bp//vePOjVuf+yjC2ZeqkFpiUMoeFh16Pvp1At4+/wjYMpgEtSAtyrtot+/qF+68
pvZKBjWEGzXZbIUU/6jL8vdLEmmwDL09chQBCN0sxtMz4NX2xQ==
-----END RSA PRIVATE KEY-----
Do not forget to copy everything from “-----BEGIN…” till “…KEY-----“, otherwise the key wont work.
Now on the other to connect using the new key just use this command:
$ ssh user@server -i .ssh/id_rsa
If you plan to use it on Windows machine with
Putty SSH client for example, please fallow the next tutorial, which is about how to
convert ssh private key with putty keygen