Ubuntu – Ssh with pubkey

ssh

I have a fresh ubuntu 20.04 cloud server and fail to access the machine with ssh using public/private key authentication

here is my configuration

  • the /etc/ssh/sshd_config on the remote has the following options
Include /etc/ssh/sshd_config.d/*.conf
PermitRootLogin no
PubkeyAuthentication yes
AllowUsers [username]
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM yes
x11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
  • the /home/[username]./ssh/autorized_keys file on the remote has a copy of my public key

  • the ~/.ssh/known_hosts on my local machine has an entry for the remote server (created when PasswordAuthentication was still allowd

still I am getting a Permission denied (publickey) when I try to ssh -i ~/.ssh/id_rsa [username]@135.181.xx.yy

Can anyone point me to the error I am obviously making? I seem unable to find the similar examples. Actually the setting are identical to another 18.04 server where they are working fine.

Best Answer

Looks good sofar. I've got a similar setup:

On my server there is the authorized_keysfile, the known_hosts file is locally.

I created the file: ~/.ssh/configwith the following content:

Host mycloud
  IdentityFile ~/.ssh/id_rsa

With this combination I can login like this:

ssh username@mycloud

If you don't have that config file you need to login like this:

ssh -i .ssh/id_rsa username@mycloud

That will not work out of the box if your server is in the cloud. I had to change the following on my server in /etc/ssh/sshd_config:

PasswordAuthentication no

Match User anotherUser
    PasswordAuthentication yes
Match all

That means that there is "anotherUser" that still may login with a password. For all other users the public/private file authentication is activated