'key_load_public: No such file or directory even though file exists

I am trying to connect to a Bitbucket server. My machine has Windows, with Git Bash.

On /h/.ssh/id_rsa and /h/.ssh/id_rsa.pub, I have a key for the repository. Also in /h/.ssh/config, I have the configuration for the repository:

Host my-repo-name
        User my-user
        Hostname my-repo.com
        Port 7999
        IdentityFile id_rsa

When I'm trying to connect to the repository like this:

ssh -Tv [email protected]

I'm getting this message:

OpenSSH_7.1p2, OpenSSL 1.0.2h  3 May 2016
debug1: Reading configuration data /h//.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to my-repo.com port 22.
debug1: Connection established.
debug1: identity file /h/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory

There are two things I don't understand here:

1.If I have /h/.ssh/id_rsa and /h/.ssh/id_rsa.pub, why am I getting key_load_public: No such file or directory?

2.If in the configuration I specify Port 7999, why does it connect to port 22 Connecting to my-repo.com port 22?



Solution 1:[1]

  1. If I have /h/.ssh/id_rsa and /h/.ssh/id_rsa.pub, why am I getting key_load_public: No such file or directory?

It is just bogus warning. More information can be found if you run with more verbose log level -vvv.

  1. If in the configuration I specify Port 7999, why does it connect to port 22 Connecting to my-repo.com port 22?

Because the Port 7999 is sepcified only for host my-repo-name, not for my-repo.com where you are trying to connect. It would get used if you would run

ssh -Tv git@my-repo-name

Solution 2:[2]

In my case, I tried to clone a gitlab project, and the solution should work for any git service. The error was

#15 0.589 debug1: identity file /root/.ssh/id_rsa type -1, 
#15 0.589 debug1: key_load_public: No such file or directory

It disappeared after using

echo "Host *\n  User git\n  HostName gitlab.com\n  AddKeysToAgent yes\n  IdentityFile /root/.ssh/id_rsa" >> /etc/ssh/ssh_config

This is also what is being asked for in your message block:

debug1: Reading configuration data /etc/ssh/ssh_config
...
debug1: key_load_public: No such file or directory

The solution proves that ssh needs to check the server to be a known host. If you have not assigned the needed known host, the gitlab server is not being trusted, which is meant by the key_load_public cannot be found. Yes, the error message is confusing.

You can also manually fill that ssh_config without the echo command, of course.

I have just seen that this was also the solution on Server Fault, see "Permission denied (publickey)" with "key_load_public: No such file or directory" upon SSH attempt.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jakuje
Solution 2