How to easily add ssh credentials on Jenkins Server
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: Complete guide for Apache TOMCAT installation on Linux
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.]