Jenkins can make ssh connection to a remote server for the purpose of executing a command or running a script or we can also copy file from jenkins or some other server to another remote server. For this purpose, we need to create a ssh connection between Jenkins server & remote server.
This tutorials details the process to add ssh credentials on Jenkins server & easily make a ssh connection with a remote server.
Recommended Read: 3 methods to Install Plugins on Jenkins server
Also Read: Is there a way to change Jenkins Home Directory ?
Pre-Requisites
Toadd ssh credentials on Jenkins server, we need to have ‘SSH Credentials’ plugin installed on jenkins server . It can be installed through ‘Manage Plugins’ section under ‘Manage Jenkins’ or we can also install it by downloading it from the following link, PUBLISH OVER SSH.
Once we have installed it, we can now add ssh credentials on jenkins servers.
For detailed instructions on how to install plugins on Jenkins server, please refer to out article “3 methods to Install Plugins on Jenkins server”
Add ssh credentials on Jenkins server
Next step in adding SSH credentials on Jenkins requires us to have the private ssh key for the server we want to connect with Jenkins server. So let’s take a scenario, we want to connect to a server from jenkins with user ‘Dan’ .
So to produce the private, login to the server as dan & goto folder ‘/home/dan/.ssh’,
$ ssh dan@remote-server
$ cd ~/.ssh
Next step will be to produce the public/private key for the user dan on the remote server,
$ ssh-keygen -t rsa -f for_jenkins_key
here, for_jenkins_key is the name of the keys that will be generated. There will be two files ‘for_jenkins_key’ & ‘for_jenkins_key.pub’. We need to use the content of ‘for_jenkins_key’ in the jenkins server & the content of ‘for_jenkins_key.pub’ file goes into another file named ‘authorised_keys’ located in the same folder i.e. .ssh folder,
$ cat for_jenkins_key.pub > authorized_keys
Note:- authorized_keys file might not be present by default. If that’s the case, than create one.
Next copy all the content of ‘for_jenkins_key’ file & now login to your jenkins server. Once logged in, goto ‘Manage Jenkins’ & than to 'Configure System' or alternatively you can also visit the url ‘http://localhost:8080/configure’ to go there directly (here localhost:8080 is the jenkins location on my server, replace it with yours).
Now in ‘Configure Jenkins’ look for section that says ‘SSH Server’. Remember that this section will only appear if you have ‘SSH Credential Plugin’ installed on jenkins server.
As already mentioned, enter the details related to your remote machine,
Next click on ‘Advanced’ & check the box that says ‘Use password authentication, or use a different key’. Than enter the passphrase, if used any during the creation of ssh keys on remote machine & than paste the contents of ‘for_jenkins_key’ in the section ‘key’,
After making the changes, click on ‘Test Configuration’ & you should get a success message if all the settings are correct.
Lastly, click on ‘Save’ button at the bottom of the page & we have successfully created a ssh connection to a remote server. Now whenever we need to execute a command or need to copy files to remote server from jenkins, all we have to do is just write the command, for example ‘ssh dan@remote-server date’ & command will be executed on the remote machine & output will show on the jenkins console output.
I hope this tutorial on how to add ssh credentials on Jenkins server was helpful to you. Please do let us know if you are facing any issues or have any suggestions, you can use the comment box below.]
If you think we have helped you or just want to support us, please consider these :-
Connect to us: Facebook | Twitter | Google Plus
Donate us some of your hard earned money: [paypal-donation]
Linux TechLab is thankful for your continued support.
Hey, I tried to follow your step-by-step tutorial, it started really nice, but when I reach /configure, I don’t have any “SSh server” section. I have “SSH Credentials Plugin” installed by default, and I tried to install a bunch of SSH plugins, but nothing appears like in your screenshots. I’m using Jenkins v. 2.130, is there any way you could help me?
Thank you in advance
Hi Joseph,
I have also used more than one plugin to store ssh credentials on my jenkins server & all the plugins are configured using ‘Manage Jenkins’ sections only.
Look for something like ‘SSH Server’ in ‘Manage Jenkins’ or if that still is not working for you than you can try another plugin called ‘Publish over ssh’, if you need jenkins for deployment.
If still you face any issue, you can write to us @ admin@linuxtechlab.com. We will certainly help you out.
Hi Shusain,
These is a very nice and detailed tutorial here and I found it very useful. I was trying my hands on “Publish over SSH ” plugin and was not able to get a success result of Test Configuration even though I generated the SSH Key and used it in authorized key file but after reading your article I was able to configure and test the SSH Server credential configuration successfully.
However, when I a trying to connect to the server and execute a command from my pipeline it is giving me an “Host key verification failed.” error.
This the pipeline that I am testing.
node {
stage(‘Test’) {
sh ‘hostname’
}
stage(‘Run DTC PRICE data ppatch’) {
sh ‘echo “Success!”; exit 0’
}
stage(‘Execute test command over SSH remote server’){
sh ‘ssh user_name_remotehost@remote_host ls -lrt’
}
}
Could you please help in identifying what I am doing wrong here in using the SSH?
Hi Abhinav, did you resolve this issue, I am facing the same issue.
I am grateful for providing step by step tutorial. However, I would like to make a few suggestions.
1. If we are using a shared server then the command ***cat for_jenkins_key.pub > authorized_keys*** this will overwrite the authorized_keys file if it exists. It would be good to use “>>” to ensure we are appending key at the end of the file.
2. We tested the success message on Jenkins which is awesome but to extend this tutorial you could show us an example of executing a small shell script on a remote server using Jenkins. I know it’s not the scope of this tutorial but it would be helpful for beginners to see end to end workings of Jenkins.
Best,
Suprit
thank you for these helpful suggestions, will surely update.
If that can help, i ‘ve just done this shell script on a publish over ssh build step :
echo "index des fichiers html[generated at $(date '+%Y-%m-%d %H:%M:%S')]" >index.html && for f in *.htm; do echo $f | sed "s/^/\\/" >>index.html && grep "name=\"nom\"" $f | sed "s/.*//" | sed "s/>index.html && echo " [$f]" >>index.html; done
creates an index of html files,
Searches for title in
Think can by handy
Thank you, your instructions were very helpful.
Excellent…!!!!!!!Perfect Solution without fail
jenkins.plugins.publish_over.BapPublisherException: Failed to connect and initialize SSH connection. Message: [Failed to connect session for config [Ansible]. Message [java.net.ConnectException: Connection timed out: connect]] this error coming
It can be either of 2 things, you might be using the wrong ssh key or permisssions are not correct for ssh folder or keys. To address that, use the following command,
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Nice post.
Thank you Shusain, its very useful for the people who use to work on SSH and the way you explained its help to understand the logic very quickly please keep contributing.. and stay safe