Unable to ssh localhost: Permission denied (publickey) / Connection closed by ::1 [preauth]

After reading some good manuals I realized that the public key of ubuntu@ (e.g., /home/ubuntu/.ssh/id_dsa.pub) must be added to the user's /home/ubuntu/.ssh/authorized_keys file, which contains public keys for public key authentication)

ubuntu@<localhost>:~$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

If you're running Ubuntu on Windows Subsystem for Linux, there will not be a preinstalled public key or authorized keys list, so you'll need to generate your own.

If you don't already have openssh-server installed:

  1. sudo apt-get upgrade
  2. sudo apt-get update
  3. sudo apt-get install openssh-server
  4. sudo service ssh start

Then take the following steps to enable sshing to localhost:

  1. cd ~/.ssh
  2. ssh-keygen to generate a public/private rsa key pair; use the default options
  3. cat id_rsa.pub >> authorized_keys to append the key to the authorized_keys file
  4. chmod 640 authorized_keys to set restricted permissions
  5. sudo service ssh restart to pickup recent changes
  6. ssh localhost

Tags:

Ubuntu

Ssh