How to access your server using key pair

From your remote host generate key pair using ssh-keygen.

:~$ ssh-keygen -b 4096 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/helloiamnewuser/.ssh/id_rsa):
Created directory ‘/home/helloiamnewuser/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/helloiamnewuser/.ssh/id_rsa.
Your public key has been saved in /home/helloiamnewuser/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:WhlZ7/YpQlaZyqlnmhjrFZdHPkE7B8GhXzX/a1J0fk0 helloiamnewuser@f674
The key’s randomart image is:
+—[RSA 4096]—-+
| . o=+.|
| o . ++o+|
| o *+ooE|
| +.=o.=*+|
| S Boo..o=|
| + O.. …o|
| o +.. ..oo |
| .. . .o |
| .o. |
+—-[SHA256]—–+

:~$ cd /home/helloiamnewuser/.ssh

:~/.ssh$ echo “$(cat id_rsa.pub)” > authorized_keys

:~/.ssh$ ls -ahl

total 20K
drwx—— 2 helloiamnewuser helloiamnewuser 4.0K May 19 07:09 .
drwxr-xr-x 4 helloiamnewuser helloiamnewuser 4.0K May 19 06:41 ..
-rw-rw-r– 1 helloiamnewuser helloiamnewuser 746 May 19 07:09 authorized_keys
-rw——- 1 helloiamnewuser helloiamnewuser 3.2K May 19 06:41 id_rsa
-rw-r–r– 1 helloiamnewuser helloiamnewuser 746 May 19 06:41 id_rsa.pub

To view the content of authorized_keys type cat authorized_keys

:~/.ssh$ cat authorized_keys

Next , download private key to localhost computer.

From your localhost computer type this command :

:~$ scp helloiamnewuser@yourremoteIP:/home/helloiamnewuser/.ssh/id_rsa /home/yourpathdirectory/Downloads
helloiamnewuser@yourremoteIP’s password:
id_rsa 100% 3247 7.7KB/s 00:00

Now you have a private key id_rsa , and try to connect your remote host

 

:~$ ssh -i /home/yourpathdirectory/Downloads/id_rsa helloiamnewuser@youremoteIP 
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 3.13.0-147-generic x86_64)