I am running both Jenkins master and slave as Docker containers by using jenkins/jenkins:lts and jenkins/ssh-slave image on Ubuntu. Following are the steps:
- Ran
ssh-keygen
inside the jenkins-master container (docker exec -it container_id bash
) to generate the ssh keys - Added the generated public key to authorized_keys file inside ssh-slave container using dockerfile
- Added private key inside Jenkins credentials as per this link
I have looked at many questions related to this issue on Stack Overflow but I am stuck with following error:
[02/08/19 20:31:06] [SSH] Opening SSH connection to ###.##.#.#:22. [02/08/19 20:31:06] [SSH] SSH host key matches key in Known Hosts file. Connection will be allowed. ERROR: Server rejected the 1 private key(s) for jenkins (credentialId:worker-ssh/method:publickey) [02/08/19 20:31:06] [SSH] Authentication failed. Authentication failed. [02/08/19 20:31:06] Launch failed - cleaning up connection [02/08/19 20:31:06] [SSH] Connection closed.
Slave Template in Jenkins:
Name: jenkins-worker
Usage: Use this node as much as possible
Launch method: Launch agent via SSH
Hostname: my ip extracted from ifconfig
Host key verification startegy: known hosts file verification strategy (.ssh/known_hosts
contains entry for host ip provided)
Dockerfile for ssh-slave
#Docker version 18.09.1
FROM jenkins/ssh-slave
COPY /.ssh/id_rsa.pub /.ssh/authorized_keys
RUN chmod 744 /.ssh/authorized_keys
Best Answer
chmod 744 on .ssh/authorized_keys
I think would be bad. ssh will not use keys from files if permissions are too open.Change permissions to 600. rw-------