How do I use SSH in a Jenkins pipeline?
I had a similar issue. I did not use the label 'master', and I identified that the file transfer works across slaves when I do it like this:
Step 1 - create SSH keys in a remote host server, include the key to authorized_keys
Step 2 - Create credential using SSH keys in Jenkins, use the private key from the remote host
Use the SSH agent plugin:
stage ('Deploy') { steps{ sshagent(credentials : ['use-the-id-from-credential-generated-by-jenkins']) { sh 'ssh -o StrictHostKeyChecking=no user@hostname.com uptime' sh 'ssh -v user@hostname.com' sh 'scp ./source/filename user@hostname.com:/remotehost/target' } }}
Use the SSH agent plugin:
When using this plugin you can use the global credentials.
To add a remote host to known hosts and hopefully cope with your error try to manually ssh from the Jenkins host to the target host as the Jenkins user.
Get on the host where Jenkins is installed. Type
sudo su jenkins
Now use ssh or scp like
ssh username@server
You should be prompted like this:
The authenticity of host 'server (ip)' can't be established. ECDSA key fingerprint is SHA256:some-weird-string. Are you sure you want to continue connecting (yes/no)?
Type yes. The server will be permanently added as a known host. Don't even bother passing a password, just Ctrl + C and try running a Jenkins job.