5

I set SSH login which connect from Windows Agent to Linux but TeamCity gives following error.

[New build problem] com.jcraft.jsch.JSchException: invalid privatekey: [B@5543cd

The keypair which generated by SSH Secure Shell has no problem to connect to server.

Similar question is here but it's not my programmatic problem but teamcity inner exception.

Note that my problem is not a case of GitHub VCS connectivity issue. just a problem between my Windows TeamCity Agent to CentOS Linux server.

Full stacktrace is as below.

[Step 6/8] com.jcraft.jsch.JSchException: invalid privatekey: [B@5543cd at com.jcraft.jsch.KeyPair.load(KeyPair.java:702) at com.jcraft.jsch.KeyPair.load(KeyPair.java:542) at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40) at com.jcraft.jsch.JSch.addIdentity(JSch.java:389) at com.jcraft.jsch.JSch.addIdentity(JSch.java:349) at jetbrains.buildServer.deployer.agent.ssh.SSHSessionProvider.initSessionKeyFile(SSHSessionProvider.java:110) at jetbrains.buildServer.deployer.agent.ssh.SSHSessionProvider.(SSHSessionProvider.java:80) at jetbrains.buildServer.deployer.agent.ssh.SSHExecRunner.createBuildProcess(SSHExecRunner.java:26) at jetbrains.buildServer.agent.impl.runner.CallRunnerService.doCreateBuildProcess(CallRunnerService.java:71) at jetbrains.buildServer.agent.impl.runner.CallRunnerService.createBuildProcess(CallRunnerService.java:47) at jetbrains.buildServer.agent.impl.buildStages.runnerStages.start.CallRunnerStage.doBuildStage(CallRunnerStage.java:47) at jetbrains.buildServer.agent.impl.buildStages.RunnerStagesExecutor$1.callStage(RunnerStagesExecutor.java:25) at jetbrains.buildServer.agent.impl.buildStages.RunnerStagesExecutor$1.callStage(RunnerStagesExecutor.java:18) at jetbrains.buildServer.agent.impl.buildStages.StagesExecutor.callRunStage(StagesExecutor.java:78) at jetbrains.buildServer.agent.impl.buildStages.StagesExecutor.doStages(StagesExecutor.java:37) at jetbrains.buildServer.agent.impl.buildStages.RunnerStagesExecutor.doStages(RunnerStagesExecutor.java:18) at jetbrains.buildServer.agent.impl.buildStages.startStages.steps.RunnerContextExecutor.callRunnerStages(RunnerContextExecutor.java:43) at jetbrains.buildServer.agent.impl.buildStages.startStages.steps.StepExecutor.processNextStep(StepExecutor.java:25) at jetbrains.buildServer.agent.impl.buildStages.startStages.steps.ForEachBuildRunnerStage.executeRunnerStep(ForEachBuildRunnerStage.java:138) at jetbrains.buildServer.agent.impl.buildStages.startStages.steps.ForEachBuildRunnerStage.runStep(ForEachBuildRunnerStage.java:123) at jetbrains.buildServer.agent.impl.buildStages.startStages.steps.ForEachBuildRunnerStage.executeBuildRunners(ForEachBuildRunnerStage.java:83) at jetbrains.buildServer.agent.impl.buildStages.startStages.steps.ForEachBuildRunnerStage.doBuildStage(ForEachBuildRunnerStage.java:44) at jetbrains.buildServer.agent.impl.buildStages.BuildStagesExecutor$1.callStage(BuildStagesExecutor.java:31) at jetbrains.buildServer.agent.impl.buildStages.BuildStagesExecutor$1.callStage(BuildStagesExecutor.java:24) at jetbrains.buildServer.agent.impl.buildStages.StagesExecutor.callRunStage(StagesExecutor.java:78) at jetbrains.buildServer.agent.impl.buildStages.StagesExecutor.doStages(StagesExecutor.java:37) at jetbrains.buildServer.agent.impl.buildStages.BuildStagesExecutor.doStages(BuildStagesExecutor.java:24) at jetbrains.buildServer.agent.impl.BuildRunAction.doStages(BuildRunAction.java:70) at jetbrains.buildServer.agent.impl.BuildRunAction.runBuild(BuildRunAction.java:50) at jetbrains.buildServer.agent.impl.BuildAgentImpl.doActualBuild(BuildAgentImpl.java:263) at jetbrains.buildServer.agent.impl.BuildAgentImpl.access$100(BuildAgentImpl.java:50) at jetbrains.buildServer.agent.impl.BuildAgentImpl$1.run(BuildAgentImpl.java:236) at java.lang.Thread.run(Thread.java:744)

Community
  • 1
  • 1
Youngjae
  • 24,352
  • 18
  • 113
  • 198
  • Could you edit your question to describe specifically how you generated this key, and what specific file you set Teamcity to read for the key? – Kenster Feb 27 '15 at 17:52

3 Answers3

12

For everyone coming here searching for a solution to the "Invalid private key" on Teamcity, the solution is to generate the key with this command:

ssh-keygen -t rsa -m PEM

This is pointed out in this page of the documentation

Joan Picornell
  • 405
  • 3
  • 6
1

Most probably, the format of Private Key you are providing is not the one JSCH expects (i.e. OpenSSH).

Similar question and answer is provided here: JSCH - Invalid private key

Community
  • 1
  • 1
Ben Goldin
  • 589
  • 2
  • 7
0

After a considerable amount of time spent on this and running the correct commands and getting the same error of invalid privatekey, I looked up what was JSch looking for? Eventually I found this code that helped me create the key that it wanted and I was able to connect:

try {
        com.jcraft.jsch.KeyPair pair = com.jcraft.jsch.KeyPair.genKeyPair(new 
        JSch(), com.jcraft.jsch.KeyPair.RSA);
        ByteArrayOutputStream publicKeyOut = new ByteArrayOutputStream();
        ByteArrayOutputStream privateKeyOut = new ByteArrayOutputStream();
        pair.writePublicKey(publicKeyOut, "kamranbhatti");
        pair.writePrivateKey(privateKeyOut);
        String publicKey = new String(publicKeyOut.toByteArray());
        String privateKey = new String(privateKeyOut.toByteArray());
        System.out.println(publicKey);
        System.out.println(privateKey);

    } catch (Exception e) { 

          } 

I copied the private key to the server and I was able to connect.

kobey
  • 21
  • 2