Yuriy Markiv blog

TECH

linux/ubuntu: key-based ssh auth (+ tricky case)

20 October, 2018 | tech

The first case describes ssh auth using key if normal password-based way is not blocked (yet). 

Generate ssh key locally:

ssh-keygen -t rsa

copy your generated key to the remote host:

ssh-copy-id user1@host

(you will be asked for your user1 password this time)

now just log into remote host, it should not ask for password:

ssh user1@host

 

Now let's consider tricky case. Imagine that you need to log into remote host, but password-based auth is closed and remote host does not know your public ssh key yet. 

As you already have your local ssh key generated as described in previous case, let's view it: 

less ~/.ssh/id_rsa.pub

you should see something like: ssh-rsa AAAAB3NzaC... localuser@localpc

(the string is quite long, select it and copy as is)

now log into remote host (you should be able to log into it from some other trusted pc or from hoster console) and open file with stored public ssh keys using your favourite text editor: 

nano ~/.ssh/authorized_keys

paste value copied in the previous step in the end of this file, log out and try to log from your local pc: 

ssh user1@host

Enjoy!