Well, as user1686 pointed out, some of what you write doesn't make sense, in that your method and logic doesn't make sense, and they identified that and had some good solutions, i'm going to write on two other things.. the logging on the client side, and also, some examples of useful output from server logs.
Regarding this error message
"Permission denied (publickey)"
It can be very misleading.
For example, you think the public key wasn't accepted, well that isn't necessarily the case! It can even be that the public key was accepted!!
One can check client side logs and server side logs.
I just noticed this re client side logs..
suppose i can't log in, I get that error!
$ ssh user@ip
user@ip: Permission denied (publickey).
To check the client side (maybe not technically called logs or logging, but the client side debugging messages shown with -v), I can run an SSH command that just runs a simple command like pwd, to basically do nothing and exit. And I also redirect the messages that ssh's verbose option gives, into a file so I can read it more easily.
$ ssh -v user@ip pwd 2>a.a
There is also a -vvv for more verbose, but -v is enough lines for me.. According to a test I just did, the -v produces 80 lines when logging in is fine.. about 66 when it didn't. The -vvv produces just over 200 lines when logging in fine.
Look at this from the end of a -v output, this is a case where logging in failed (because i'd renamed the private key from id_rsa to id_rsaa)
debug1: Offering public key: /home/user/.ssh/id_rsa RSA SHA256:Ltp8.....KQ/18J3I
debug1: Server accepts key: /home/user/.ssh/id_rsa RSA SHA256:Ltp8....KQ/18J3I
debug1: Trying private key: /home/user/.ssh/id_ecdsa
debug1: Trying private key: /home/user/.ssh/id_ecdsa_sk
debug1: Trying private key: /home/user/.ssh/id_ed25519
debug1: Trying private key: /home/user/.ssh/id_ed25519_sk
debug1: Trying private key: /home/user/.ssh/id_xmss
debug1: Trying private key: /home/user/.ssh/id_dsa
debug1: No more authentication methods to try.
user@ip: Permission denied (publickey).
Notice that it says the "Server accepts key".. Yet it still says "Permission denied (publickey).". So what I did in putting the public key to authorized_keys, worked.
And i've also had the ""Permission denied (publickey)." message when the username had been wrong.
Here's an example of a working log in , showing -v
debug1: Offering public key: /home/user/.ssh/id_rsa RSA SHA256:Ltp8.....KQ/18J3I
debug1: Server accepts key: /home/user/.ssh/id_rsa RSA SHA256:L....18J3I
Authenticated to ..... using "publickey".
debug1: Remote connections from LOCALH...
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
When the username is incorrect, it never gets to accepting the public key, it only offers it
debug1: Offering public key: /home/user/.ssh/id_rsa RSA HrKQ/18J3I
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/user/.ssh/id_ecdsa
debug1: Trying private key: /home/user/.ssh/id_ecdsa_sk
debug1: Trying private key: /home/user/.ssh/id_ed25519
debug1: Trying private key: /home/user/.ssh/id_ed25519_sk
debug1: Trying private key: /home/user/.ssh/id_xmss
debug1: Trying private key: /home/user/.ssh/id_dsa
debug1: No more authentication methods to try.
userr@ip: Permission denied (publickey).
Looking on the server side log. (user's answer has details on where that can be), mine is /var/log/auth.log
And user mentions to have LogLevel DEBUG, in sshd_config. If you change sshd_config then you can reload it without restarting the server, with the method here https://askubuntu.com/questions/462968/take-changes-in-file-sshd-config-file-without-server-reboot sudo kill -SIGHUP $(pgrep -f "sshd -D")
The server log can show a message for if it's the user, and if the user is ok, then if it's the public key.
If I get the user wrong
/var/log# tail -n 10 auth.log | grep -i 'invalid'
Jun 6 02:24:08 comp sshd[82436]: Connection closed by invalid user roott ip port 52976 [preauth]
if I also get the public key wrong in addition to the user, then I still only get the invalid user message, no message about the public key.
But if I get the user correct and public key wrong.
/var/log# tail -n 10 auth.log | grep -i 'failed'
Jun 6 02:25:09 comp sshd[82456]: Failed publickey for user from ip port 52977 ssh2: RSA SHA256:Lt.../18J3I
:/var/log#
can do grep like this
comp:/var/log# tail -n 10 auth.log | grep -Pi 'invalid|failed'
Jun 6 02:30:58 comp sshd[82506]: Failed publickey for user from ip port 52985 ssh2: RSA SHA256:L.....Q/18J3I
--
root@comp:/var/log# vi ~/.ssh/authorized_keys
(I fixed the public key)
--
comp:/var/log# tail -n 10 auth.log | grep -Pi 'invalid|failed'
(no output there. no error, I logged on)
now for the one below, I did the user wrong.
comp:/var/log# tail -n 10 auth.log | grep -Pi 'invalid|failed'
Jun 6 02:31:59 comp sshd[82693]: Connection closed by invalid user roott ip port 52988 [preauth]
/var/log#
So you can do tail -n 10 auth.log | grep -Pi 'invalid|failed' and if you got the user wrong it'll say. And if you fix that but get the public key wrong, then it will tell you that you got the public key wrong.