SSH.NET failing to connect, giving permission denied

421 Views Asked by At

I have a remote Ubuntu 22.04 server, set to a default ssh configuration so it accepts public keys including ssh-rsa, among the usual others. I have a private RSA key that I use to connect to it, and it works fine using Putty, WinSCP, and ssh -i [private_key_file] [username@host].

However, I cannot connect to it using the SSH.NET library. It excepts with a Permission denied (publickey) exception. Here's my code:

string host = "hostname";
string user = "username";
string keyFile = @"path\to\keyfile.pem";

var keyFiles = new[] { new PrivateKeyFile( keyFile ) };
PrivateKeyAuthenticationMethod pkAuth = new PrivateKeyAuthenticationMethod( user, keyFiles );
var methods = new AuthenticationMethod[] { pkAuth };
ConnectionInfo connectionInfo = new( host, user, methods );

using SftpClient client = new( connectionInfo );

try
{
    client.Connect();
    if( client.IsConnected )
        Console.WriteLine( "Connection succeeded!" );
    else
        Console.WriteLine( "Connection failed" );
}
catch( Exception ex )
{
    Console.WriteLine( ex.Message );
}
finally
{
    client.Disconnect();
}

I've tried using SshClient instead of SftpClient, constructing the client by passing the host, username, and keyfile directly instead of ConnectionInfo, even pasting the key into the code and constructing the PrivateKeyFile with a MemoryStream, all to no avail. When I attempt the connection, the auth.log on the server shows this:

Jul 13 15:39:45 ip-xxx-xx-xx-xx1 sshd[125987]: userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms [preauth]
Jul 13 15:39:45 ip-ip-xxx-xx-xx-xx1 sshd[125987]: Connection closed by authenticating user username xx.xxx.xxx.xxx port xxxx [preauth]

I'm mystified, and welcome suggestions.

0

There are 0 best solutions below