0

I am trying to connect to SFTP server, running on another laptop at my home (running on win 10) I am using SSHJ library. When I use authentication with password, it works fine:

client.authPassword("myUserName", "myPassword") 

But when I am trying to authenticate using key, I am getting exception "Exhausted available authentication methods" I wasn't sure if I need to use the private or public key, so I tried both (taken from the SFTP server), both return the same error. Here is my code:

public static void sftpUsingKey() throws IOException {
    final SSHClient client = new SSHClient();
    client.loadKnownHosts();
    try {
        client.addHostKeyVerifier(new PromiscuousVerifier());
        client.connect(remoteHost);
        PKCS8KeyFile keyfile = new PKCS8KeyFile();
        File privateKeyFile = new File("path/to/key");
        keyfile.init(privateKeyFile);
        client.authPublickey("myUserName", keyfile);

        final SFTPClient sftp = client.newSFTPClient();
        try {
            sftp.put(new FileSystemFile(src+fileName), remoteDst);
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }
        finally {
            sftp.close();
        }
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    } finally {
        client.disconnect();
    }
}

Any tip what am I doing wrong?

Gilo
  • 640
  • 3
  • 23
  • 1
    Did you copy your public key to the server and place it in the authorized_keys file? You can use `ssh-copy-id` for that on Linux (not sure if there is a Windows version of that). Can you connect with a regular ssh client? Try adding `-vv` for verbose output. If that works, the problem is with your code, if not, it's with your key setup. – Robert May 14 '20 at 03:57
  • @Robert thanks, that is a good point, for some reason adding the public key to windows SFTP server is not that trivial. I am now trying it – Gilo May 14 '20 at 20:58
  • 1
    You use the private key in your client and "install" the public key for the SFTP-server (it's entirely up to the used SFTP-server on how to do the public key install). The actual error indicates that there is no more authentication methods left for the client to try, so also make sure your SFTP server supports (and is offering) the public key authentication. – Jokkeri May 20 '20 at 04:57
  • @Jokkeri the generation of the private and public keys must be done by the client? Or can it work also if the server is generating it? or a third party? – Gilo May 20 '20 at 14:59
  • 1
    @Gilo The general "good practice" is that the client party will generate the keypair(public & private) and then delivers the public for server admin who installs it. This is because it is not good practice to send out the private key anywhere, as it really is "private". But of course, the keypair can be done anywhere, it is not tied to the machine on what it was created. See this for more info: https://www.ssh.com/ssh/public-key-authentication – Jokkeri May 22 '20 at 12:46

0 Answers0