How to Configure SSH with Jenkins

Jenkins is a Free & open-source devops tools & used for CI/CD automation application. It is written in Java language. Jenkins provides SSH plugins such as ssh, ssh agent & publish over ssh. Using ssh plugins we can configure ssh with jenkins. We needs a private key to configure ssh with jenkins.

Install SSH plugins on Jenkins.

Create a user for access the jenkins jobs.

adduser user-name
or
adduser jenkins

Here is the command output.

Adding user `jenkins' ...
Adding new group `jenkins' (1002) ...
Adding new user `jenkins' (1002) with group `jenkins' ...
Creating home directory `/home/jenkins' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for jenkins
Enter the new value, or press ENTER for the default
	Full Name []: 
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] y

Add new user-name in sudoers file.

user-name ALL=(ALL:ALL) ALL
or
jenkins ALL=(ALL:ALL) ALL

Login to new user-name.

sudo su - user-name

Here is the command output.

[email protected]
or
[email protected]

Create a .ssh forlder.

mkdir ~/.ssh

Create a authorized_keys.

vim ~/.ssh/authorized_keys

Paste a private/public key.

How to get public key/Private key.

  • Run ssh-keygen command on Root user to create a key.
ssh-keygen

Here is the command output.

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:rsAb4BudQmMvizzaXBfjJ59sI0Hsdrlczv4PcV3ghDE [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|            Eoo  |
|            .+ . |
|     .        . .|
|      o        ..|
|  =  oo S.  . . .|
| + * o++o .  o   |
|  = O.++o=  .    |
|.= B =.*=.o  .   |
|+.* . .o+o.....  |
+----[SHA256]-----+

Open the public key file.

cat .ssh/id_rsa.pub ##public key
or
cat .ssh/id_rsa  ##private key

 

Configure SSH with jenkins.

  • Login to Jenkins Server.
  • Provide user-name & password.

 

Fig 1

 

  • Click on Manage configure.

 

Fig 1

 

  • Click on Manage Credentials.

 

Fig. 3

 

  • Click on Jenkins.

 

Fig. 4

 

  • Click on Global Credentials(unrestricted) option.

 

Fig. 5

 

  • Click on adding some credentials.

 

Fig. 6

 

  • Select SSH username with private key.

 

Fig. 7

 

  • Provide Unique ID for key.
  • Provide username.

 

Fig. 8

 

  • At private Key.
  • Click on Add or paste the private key from .ssh/id_rsa
  • Click on OK.

 

Fig 9

 

  • SSH credentials is added successfully.

 

Fig 10

 

Leave a Reply