Cannot launch Jenkin agent ssh

Hi all,
I’m Jenkins newbie
I have jenkins and ssh-agent running with docker compose as below

version: "3"

services:

  jenkins:
    image: jenkins/jenkins:2.430-jdk21
    container_name: jenkins
    environment:
      - TZ=Asia/Ho_Chi_Minh
    ports:
      - "8080:8080"
    volumes:
      - ./jenkins_home:/var/jenkins_home
      - ./kubectl:/usr/local/bin/kubectl
    networks:
      - jenkins-net

  jenkins-agent-docker:
    image: jenkins/ssh-agent:jdk21
    container_name: jenkins-agent-docker
    #user: 1000:1000
    environment:
      - TZ=Asia/Ho_Chi_Minh
      #- JENKINS_AGENT_SSH_PUBKEY=
      - JENKINS_AGENT_SSH_PUBKEY=ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCz/9DTzHY/uSF58WpHpZQ2wTgphA2FWGFX/g9xYSFPhctqO67/sU9Uq8li1wIOWJ9f89JERq8p7+CaSVoyl7/PC3LG9T63IspKsPm4XKviZ9RiuvIILfcbJ6f9QO7HS4qoEmKGIZaZtahuxMdqvorJAK+h4uxGuk7BMMabktk3Kf6Qf5KaH/B/HTZpBnTMucwYWx2eHFl2uH78pFqffZDq4ZDKBcBBRWgSzMSPrCqOOwuFOxjeAGezDsBIxNDVqd56t8LLl/mtuqZeD1P9Wwm3KhrdgwtMGN0MwujYP2oD/sbeGa4iuqMkGEX9pY4cFh+IoD/fu6w1cpgv6xXaXMGD
    ports:
      - "22:22"
    networks:
      - jenkins-net

networks:
  jenkins-net:
    driver: bridge
    name: jenkins-net

My jenkins server’s ip is 192.168.7.112
From my computer I can ssh to jenkins ssh agent container as below

ssh -i private_key jenkins@192.168.7.112
Linux ae2efc3d9fc4 5.19.0-45-generic #46-Ubuntu SMP PREEMPT_DYNAMIC Wed Jun 7 09:08:58 UTC 2023 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
jenkins@ae2efc3d9fc4:~$

From Jenkins UI , I added credential “docker-agent1” (SSH Username with private key) , as below

Then from Manage Jenkins → Nodes , I created “docker-agent1”

When I launch agent , I get error as below

SSHLauncher{host='192.168.7.112', port=22, credentialsId='docker-agent1', jvmOptions='', javaPath='', prefixStartSlaveCmd='', suffixStartSlaveCmd='', launchTimeoutSeconds=60, maxNumRetries=10, retryWaitTime=15, sshHostKeyVerificationStrategy=hudson.plugins.sshslaves.verifiers.ManuallyTrustedKeyVerificationStrategy, tcpNoDelay=true, trackCredentials=true}
[11/22/23 13:48:02] [SSH] Opening SSH connection to 192.168.7.112:22.
[11/22/23 13:48:02] [SSH] SSH host key matches key seen previously for this host. Connection will be allowed.
ERROR: Server rejected the 1 private key(s) for jenkins (credentialId:docker-agent1/method:publickey)
ERROR: Failed to authenticate as jenkins with credential=docker-agent1
java.io.IOException: Publickey authentication failed.
	at com.trilead.ssh2.auth.AuthenticationManager.authenticatePublicKey(AuthenticationManager.java:349)
	at com.trilead.ssh2.Connection.authenticateWithPublicKey(Connection.java:472)
	at com.cloudbees.jenkins.plugins.sshcredentials.impl.TrileadSSHPublicKeyAuthenticator.doAuthenticate(TrileadSSHPublicKeyAuthenticator.java:110)
	at com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator.authenticate(SSHAuthenticator.java:431)
	at com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator.authenticate(SSHAuthenticator.java:468)
	at hudson.plugins.sshslaves.SSHLauncher.openConnection(SSHLauncher.java:878)
	at hudson.plugins.sshslaves.SSHLauncher.lambda$launch$0(SSHLauncher.java:434)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.io.IOException: Could not generate signature
	at com.trilead.ssh2.signature.KeyAlgorithm.generateSignature(KeyAlgorithm.java:43)
	at com.trilead.ssh2.auth.AuthenticationManager.authenticatePublicKey(AuthenticationManager.java:316)
	... 10 more
Caused by: java.security.SignatureException: Could not sign data
	at java.base/sun.security.rsa.RSASignature.engineSign(RSASignature.java:196)
	at java.base/java.security.Signature$Delegate.engineSign(Signature.java:1410)
	at java.base/java.security.Signature.sign(Signature.java:713)
	at com.trilead.ssh2.signature.KeyAlgorithm.generateSignature(KeyAlgorithm.java:41)
	... 11 more
Caused by: javax.crypto.BadPaddingException: RSA private key operation failed
	at java.base/sun.security.rsa.RSACore.crtCrypt(RSACore.java:209)
	at java.base/sun.security.rsa.RSACore.rsa(RSACore.java:130)
	at java.base/sun.security.rsa.RSASignature.engineSign(RSASignature.java:193)
	... 14 more
[11/22/23 13:48:02] [SSH] Authentication failed.
Authentication failed.
[11/22/23 13:48:02] Launch failed - cleaning up connection
[11/22/23 13:48:02] [SSH] Connection closed.

How can I fixed it, please give me some advice, thank you very much.

This is “docker-agent1” node that I created ( sorry , I can only upload 1 media each post)

This is “docker-agent1” node that I created ( sorry , I can only upload 1 media each post)

BadPaddingException means that the Key does not have the correct padding in the private key you pasted in the SSH agent configuration, it should be a valid armored SSH private Key, check Using Jenkins agents

Caused by: javax.crypto.BadPaddingException: RSA private key operation failed
at java.base/sun.security.rsa.RSACore.crtCrypt(RSACore.java:209)
at java.base/sun.security.rsa.RSACore.rsa(RSACore.java:130)
at java.base/sun.security.rsa.RSASignature.engineSign(RSASignature.java:193)
… 14 more

Hi,

If you want to give a try at Jenkins on top of Docker, please have a look at this docker compose file.