Troubleshoot ssh – Permission denied (publickey) error

ssh my_dev_box_host_name

But I got this error

Permission denied (publickey).

then I turned on debuging

ssh -vvv my_dev_box_host_name

See these lines

...
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/wyehearsay/.ssh/id_rsa RSA SHA256:FzMfrbORgYEtcIaWTg2iZOBctxYeNZ9bz/vFxLLtefw agent
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
...

So the public key has been attempted. But server rejected it for some reason.
Note: the same public/private key pair works on another machine to ssh into the same dev box.
Then I logged in to the dev box (via the working machine). And tailing the auth.log.

less +F /var/log/auth.log

At the same time, trying again to ssh from the machine that has issue. Then I noticed this log.

Invalid user xyz from 10.11.50.126 port 50310

Problem solved. On the machine that has issue, I am using a different username (xyz) on the MacOS. While the user defined on chef for the dev box is abc. To fix it, just need to

ssh abc@my_dev_box_host_name

Alternatively, put the username to ssh.config

my_dev_box_host_name
    User abc

Done.

Leave a comment