Jenkins is one of the most popular tools for CI/CD & automation purposes. Jenkins is an open-source automation server that is used to reliably build, test, and deploy software by developers around the world.

Here we are only going to discuss how to add ssh credentials in Jenkins server to make connections to other servers for the purpose of executing various scripts or commands on other servers. We can also make connections to other servers where some files are kept to execute them.

Recommended Read: Simple way to install ADB & FastBoot on Ubuntu

Also Read: Install & learn to use TCPDUMP with examples

In this tutorial, we will learn to add ssh credentials on the Jenkins server and how we can make an ssh connection with a remote server.


Pre-Requisites

We need to install a plugin named PUBLISH OVER SSH on the Jenkins server to add the ssh credentials. Once we have downloaded the plugin by visiting ‘Manage Jenkins’, then ‘Manage Plugins’ section. Here we need to click on the ‘Advanced’ tab & then click on ‘Upload plugin’ to upload the downloaded plugin.

Add ssh credentials on Jenkins server

Now to make an SSH connection, we would require ssh keys. Login to the REMOTE server & create the ssh key pair using the following command

$ cd ~/.ssh

$ ssh-keygen -t rsa -f jenkins_key

The first thing now we have to do is to add the public ssh key to the authorized_keys file, located in the same directory i.e. ~/.ssh. If authorized_keys file is not there, then it will be created with the following command,

$ cat jenkins_key.pub >> authorized_keys

Next, we need a private key that is created. Copy the contents of the private key named ‘jenkins_key’ & head back to the Jenkins page. Now go to ‘Manage Jenkins’ & then to ‘Configure System’. Upon scrolling down a bit, you will see a section named “SSH Server”. See screenshot below,

add ssh credentials in jenkins

Enter the mentioned values in the section,

Name = Name for the SSH connection,

Hostname = IP address or hostname of the remote server,

Username = username on the remote server,

add ssh credentials in jenkins

Now click on ‘Advanced’ & then check the box that says ‘Use password authentication, or use a different key’. Here you need to enter the passphrase for the ssh key (if entered during ssh key created, or else leave it empty) & then copy the contents of the private ssh key in the section named ‘Key’,

add ssh credentials in jenkins

Next click on ‘Test Configuration’ & if the provided information is correct then we will get a success message. To complete the setup, click on the ‘Save’ button located on the bottom of the page. SSH connection to the remote server has been created.

Now when the connection is required to the remote server, all we have to do is to use commands like ‘ssh user@remote-server command’ or ‘ssh user@remote-user sh script.sh’ or ‘scp /home/jenkins/test-file.txt user@remote-server:/home/user-directory ’. You will not be asked to enter any password to complete the operation.

This concluded the tutorial to add ssh credentials in the Jenkins server. You can contact us using the comment box below for any questions or queries.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.