Step-By-Step Guide to Setting Up SSH Keys for Jenkins

Jenkins, the popular open-source automation server that's been making waves in the tech industry.

Today, we'll go through an important aspect of Jenkins - setting up Secure Shell (SSH) keys for securely publishing and transferring files. It may sound daunting, but trust me, I'll guide you every step of the way. Let's get started!

Step 1. Creating an SSH Key Pair on Your Jenkins Server

We're going to kick things off by generating a new SSH key pair right on your Jenkins server. These keys, one public and one private, are the secret handshake that lets your Jenkins server chat with other servers securely.

Open up a terminal on your Jenkins server and punch in the following command:

1# Generate SSH key pair
2
3ssh-keygen -t rsa -b 4096 -C "jenkins@example.com" 

Step 2: Installing the SSH Keys

Now that you've got your SSH key pair, you need to install your public key (id_rsa.pub) on the target server - the server you wish Jenkins to connect to.

To copy the public key to the server, use the following command:

1ssh-copy-id -i ~/.ssh/id_rsa.pub user@hostname 

Don't forget to replace "user@hostname" with your username and the hostname (or IP address) of your target server.


Step 3: Using the Publish Over SSH Plugin

Next, head over to the Jenkins UI. Navigate to Manage Jenkins > Manage Plugins > Available and search for the "Publish Over SSH" plugin. Install this plugin and then reboot Jenkins for the changes to take effect.


Step 4: Configuring the Publish Over SSH Plugin

With the plugin(Publish Over SSH) installed, it's time to configure it. In the Jenkins UI, go to Jenkins > Manage Jenkins > Configure System > Publish over SSH, and scroll down to the "Publish Over SSH" section.

Here's how to complete the configuration:

  1. Click on "Add" to add a new SSH server.
  2. Provide a name for the server.
  3. Fill in the hostname or IP address of the server.
  4. Enter the username you used when you copied the public key to the server.
  5. In the "Key" field, enter the contents of your private key file (id_rsa). You can get this information by running cat ~/.ssh/id_rsa in the terminal and copying the output.

Step 5: Utilizing the SSH Server in a Jenkins Job

With the SSH server set up and ready, it's time to use it in a Jenkins job. In the job configuration, you'll notice a "Send files or execute commands over SSH" option in the "Add post-build action" dropdown.

Here's how to proceed:

  1. Choose the server you configured earlier from the "SSH Server" dropdown.
  2. If you need to transfer files, specify the source files and the remote directory.
  3. If you need to run commands, enter them in the "Exec command" field.

Make sure to save your job settings. The job will now connect to the SSH server using the configured key and carry out the specified actions securely.


Conclusion

Jenkins, when set up with SSH keys, ensures secure communication and file transfer between servers. By following the steps in this guide, you can enhance the security of your Jenkins server and maintain the integrity of your files. Don't forget to ensure your Jenkins server's outgoing connections to the other server are allowed in your network's firewall rules. Stay secure and happy building with Jenkins!

Posts in this Series