How to Set Up SSH Keys Linux

Scenario: Set up ssh key and copy to linux server with ip address 192.168.99.17.

Steps:
1) Example we will create ssh keys for user bachem,first create the RSA Key Pair.
$ ssh-keygen

[bachem@horse ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/bachem/.ssh/id_rsa): [Enter]
Created directory '/home/bachem/.ssh'.
Enter passphrase (empty for no passphrase): [Enter if no passphrase]
Enter same passphrase again: [Enter if no passphrase]
Your identification has been saved in /home/bachem/.ssh/id_rsa.
Your public key has been saved in /home/bachem/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:H71MXF9aYIVVKLhbrx0cKTyXllYWitG7rhWM+Ie/68M bachem@horse
The key's randomart image is:
+---[RSA 2048]----+
| o. o**|
| . +o+.o|
| + oo*o|
| ooBoO+.|
| S..++@o..|
| .o+.o= |
| .o== . |
| =E. |
| .o=+ |
+----[SHA256]-----+
[bachem@horse ~]$

2) Copy the Public Key to destination server that we will access for ssh.
$ ssh-copy-id bachem@192.168.99.17

[bachem@horse ~]$ ssh-copy-id bachem@192.168.99.17
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/bachem/.ssh/id_rsa.pub"
The authenticity of host '192.168.99.17 (192.168.99.17)' can't be established.
RSA key fingerprint is SHA256:EAlBfp5tZcBsrpqgn4VHG//aJ+17UDX/STD1SBFB2QU.
RSA key fingerprint is MD5:c6:8a:e5:94:05:79:a0:9b:b5:aa:ac:7a:b7:5f:d4:0c.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
bachem@192.168.99.17's password: [enter password continue press Enter]

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'bachem@192.168.99.17'"
and check to make sure that only the key(s) you wanted were added.

[bachem@horse ~]$

3) Test ssh to server 192.168.99.17 it will request no password,like below..
$ ssh bachem@192.168.99.17

[bachem@horse ~]$ ssh bachem@192.168.99.17
Last login: Sun Jul 8 20:58:19 2018 from 192.168.99.99
[bachem@centos6 ~]$ exit
logout
Connection to 192.168.99.17 closed.
[bachem@horse ~]$

To remove ssh keys just delete directory .ssh in home directory.
$ rm -rf /home/bachem/.ssh/

[bachem@horse ~]$ rm -rf /home/bachem/.ssh/
[bachem@horse ~]$ ssh bachem@192.168.99.17
The authenticity of host '192.168.99.17 (192.168.99.17)' can't be established.
RSA key fingerprint is SHA256:EAlBfp5tZcBsrpqgn4VHG//aJ+17UDX/STD1SBFB2QU.
RSA key fingerprint is MD5:c6:8a:e5:94:05:79:a0:9b:b5:aa:ac:7a:b7:5f:d4:0c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.99.17' (RSA) to the list of known hosts.
bachem@192.168.99.17's password:
Last login: Wed Jul 11 11:55:39 2018 from 192.168.99.11
[bachem@centos6 ~]$ exit
logout
Connection to 192.168.99.17 closed.
[bachem@horse ~]$

Leave a Reply

Your email address will not be published. Required fields are marked *