I’m new to Jenkins and I’m trying to connect a node through ssh. I’m getting the error below which after searching around seems to be common. I’m unsure on how to resolve it. This directory does not exist so it seems accurate. Do I need to create this directory?
[SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it.
Most of the time I add a jenkins user on the machine I want to use as a node, and then copy an SSH key so that I can easily connect to it through the controller later on.
I also create a link (bad, bad habit) from /home/jenkins to /var/lib/jenkins so /var/lib/jenkins/.ssh/known_hosts does exist.
Your error makes me wonder if there’s a better (and official way) to configure a machine in order to make it a node.
Anyway… If you don’t want to change your process and use your existing folder, you could follow the steps below to create the know_hosts file:
Log in to the remote node machine as a user who has sudo access.
Create the directory /var/lib/jenkins/.ssh if it does not exist already: sudo mkdir -p /var/lib/jenkins/.ssh
Set the correct permissions for the directory: sudo chown -R jenkins:jenkins /var/lib/jenkins/.ssh
Create an empty known_hosts file: sudo touch /var/lib/jenkins/.ssh/known_hosts
Set the correct permissions for the known_hosts file: sudo chmod 600 /var/lib/jenkins/.ssh/known_hosts
Add the SSH host keys for your remote hosts to the known_hosts file. You can do this manually by using the ssh-keyscan command:
Thanks Bruno. That helped. I didn’t create the link but I added directories and ran the commands and it worked. I think I also had a credential error when using the SSH and private key creds. Much appreciated. I’m sure I’ll have more issues.