SSH key-based authentication

First, generate a key without password:

root@example:~# ssh-keygen -f ~/.ssh/id_rsa -q -P ""

You will see id_rsa and id_rsa.pub

root@example:~# ls -la .ssh
total 20
drwx------ 2 root root 4096 Nov 18 15:19 .
drwx------ 7 root root 4096 Apr 14  2018 ..
-rw------- 1 root root 1679 Nov 18 15:19 id_rsa
-rw-r--r-- 1 root root  401 Nov 18 15:19 id_rsa.pub
-rw-r--r-- 1 root root  444 Sep 22  2017 known_hosts

Checking the public key contents

root@example:~# cat ~/.ssh/id_rsa.pub
ssh-rsa AAPIUIOFDUYNUFYDuiyDuYDIUFYuiHDOFihoudHFJDMFSQLDFhpuhgdFhgpsuDHFSDSDCSDCSZMCKJHISDOIVSDFVSDVCSCMJQOMLCSDVCSDCDC6sdfvlkqjhkMKJRE654LJHLJHLHLKGg65F6D4f646465465htgj4u6ktk4r64y64f4vbqd/fvqsd54f3aGRSGSFLJHJKHGGIH65645gfhgfmkjoijUHUKGkHGKB65DhkJDF786DFlhjDLjhHFQ547DQfDQ5jjlihEDKhgFYGtftjGYGPKUHfmKDNFLKGSFMljQDMOIuhMDLQHmLHoùŶHm%HDMOUGMQL?Df56595654MEKILDHfkSbdlHDLjf,DF5464645FMkhKQBHKfGHKUhMFhmlnhMOhF653 root@example.com

Everything seems good. id_rsa.pub needs to be added into de ~/.ssh/authorized_keys of the remote server. Instead of doing it manually, let’s use the utility ssh-copy-id.

root@example:~# ssh-copy-id username@178.47.124.147
/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
username@178.47.124.147's password: 

Number of key(s) added: 1

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

Ok, now let’s test the ssh connection.

root@example:~# ssh username@178.47.124.147
Linux 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Nov 18 15:15:12 2018 from 81.247.143.9
username@178.47.124.147:~$ exit
logout
Connection to 178.47.124.147 closed.
root@example:~#

Done.

Leave a Reply

Your email address will not be published.

Loading Facebook Comments ...
Loading Disqus Comments ...