jenkinssshdevopsjenkins-agent

Why Jenkins says "Server rejected the 1 private key(s)" while launching the agent?


I am successfully able to connect to remote machine using SSH but when I am launching the agent from Jenkins it throws the following error:

ERROR: Server rejected the 1 private key(s) for user1 (credentialId:xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/method:publickey)
[01/19/17 05:35:15] [SSH] Authentication failed.
hudson.AbortException: Authentication failed.
    at hudson.plugins.sshslaves.SSHLauncher.openConnection(SSHLauncher.java:1219)
    at hudson.plugins.sshslaves.SSHLauncher$2.call(SSHLauncher.java:714)
    at hudson.plugins.sshslaves.SSHLauncher$2.call(SSHLauncher.java:709)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
[01/19/17 05:35:15] Launch failed - cleaning up connection
[01/19/17 05:35:15] [SSH] Connection closed.

I can establish SSH connection from master machine to the node machine using user1, however when I am trying to launch the agent using user1 from jenkins it is rejecting the private key. Is there any solution to overcome this issue?


Solution

  • I solve this issue following below steps:

    From the target slave node's console

    1. Switch to the root user:
    sudo su
    
    1. Add a jenkins user with the home /var/lib/jenkins (Note: I am keeping my home directory in /var/lib/jenkins):
    useradd -d /var/lib/jenkins jenkins
    

    From the Jenkins Master

    Copy the /var/lib/jenkins/.ssh/id_rsa.pub key from the Jenkins user on the master

    From the target slave node's console

    1. Create an authorized_keys file for the Jenkins user
    mkdir /var/lib/jenkins/.ssh
    touch /var/lib/jenkins/.ssh/authorized_keys
    
    1. Paste the key from the Jenkins master into the file vim. Save with :wq!

    2. Make sure the files have correct owner and permission.

    chown -R jenkins /var/lib/jenkins/.ssh
    chmod 600 /var/lib/jenkins/.ssh/authorized_keys
    chmod 700 /var/lib/jenkins/.ssh