This question already has an answer here:
-
SSH.NET Authenticate via private key only (public key authentication)
3 answers
I know that private key authentication works, however I'm looking for public key authentication.
I'm trying to establish a connection using SSH.NET and a public key.
In the Git Bash terminal I can connect and run commands fine using my public key, but when I try connecting to the same host using SSH.NET I get an exception.
Here is my how I'm trying to connect:
using (var client = new SshClient("host.name.com", port, "username"))
{
client.Connect(); // exception thrown here
}
I've also tried this route:
var authMethod = new PrivateKeyAuthenticationMethod("username");
var info = new ConnectionInfo("host.name.com", port, "username", authMethod);
using (var client = new SshClient(info))
Both give the same exception: Permission denied (publickey).
It seems as though I need to specify where my public key is, or I need to put my public key in a certain spot, but I can't find any documentation telling me where to put it.
There's no "private key authentication". It's actually called "public key authentication".
Though you need both private and public key to authenticate using "public key authentication". It's called "public key authentication", because a client (SSH.NET in this case) sends only the public key to the server - So the server authenticates you using the public key only. The private key is used locally only.
Though file formats that are commonly referred to as a private key (like PEM or .ppk) - as opposite to public key (.pub) formats - actually contain whole key pair (both public and private key).
So the question that you link to - SSH.NET Authenticate via private key only (public key authentication) - actually does what you want. It shows how to authenticate using "public key authentication" - using private key file format (both public and private key really). I've edited the title of that question to make this more clear.
And to correct you, it's not true that "In the Git Bash terminal I can connect and run commands fine using my public key" - That's not possible. You must have a private key too.