Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH.NET Authenticate via private key only (public key authentication)

Attempting to authenticate via username and privatekey only using the current SSH.NET library. I cannot get the password from the user so this is out of the question.

here is what i am doing now.

Renci.SshNet.ConnectionInfo conn = 
    new ConnectionInfo(hostName, port, username, new AuthenticationMethod[]
        {
            new PasswordAuthenticationMethod(username, ""), 
            new PrivateKeyAuthenticationMethod(username, new PrivateKeyFile[] 
                   { new PrivateKeyFile(privateKeyLocation, "") }),
        });

using (var sshClient = new SshClient(conn))
{
    sshClient.Connect();
} 

Now, if I remove the PasswordAuthenticationMethod from the AuthenticationMethod[] array I get an exception for for no suitable authentication method found. If i attempt to pass in the (hostname, port, username, keyfile2) as such

var keyFile = new PrivateKeyFile(privateKeyLocation);
var keyFile2 = new[] {keyFile};

again, no suitable method found.

It seems I have to use the ConnectionInfo object as I outlined above, but it seems that it evaluates the PasswordAuthenticationMethod and cant log in (since i don't provide a password) and never evaluates the PrivateKeyAuthMethod... is this the case? Is there some other way to authenticate with only a username or hostname and private key using SSH.NET lib?

like image 655
petey m Avatar asked Jun 05 '15 12:06

petey m


People also ask

How do I authenticate to private key using SSH?

2.3 Login using your private keyFind your id_rsa. ppk file, open it, then click Open to start the connection. You should be prompted for your username as before (sysadm), but then instead of being prompted for a password, you are asked for the passphrase for your private key. Enter it, and you should be logged in.

How do I authenticate with private key?

Private key passphrase If during key pair generation, a password was entered by the user, then the private key will be stored in an encrypted file. The user will be asked to authenticate himself, before the private key is released and ready to use for establishing a connection.

How does public key authentication work SSH?

Anyone with a copy of the public key can encrypt data which can then only be read by the person who holds the corresponding private key. Once an SSH server receives a public key from a user and considers the key trustworthy, the server marks the key as authorized in its authorized_keys file.

What are the authentication methods used in SSH?

The two widely used methods of SSH authentication for secure remote access are: Password authentication (using user name and passwords) Public key-based authentication (using public and private key pairs)


1 Answers

Your problem here is that you're still using a password, even though it is blank. Remove this line:

new PasswordAuthenticationMethod(username, ""), 

This works perfectly for me:

var pk = new PrivateKeyFile(yourkey);
var keyFiles = new[] { pk };

var methods = new List<AuthenticationMethod>();
methods.Add(new PrivateKeyAuthenticationMethod(UserName, keyFiles));

var con = new ConnectionInfo(HostName, Port, UserName, methods.ToArray());
like image 105
scimino Avatar answered Oct 02 '22 03:10

scimino