Friday, August 26, 2016

Jenkins Slave – Remote Host With SSH

In the previous article we have seen how we can configure a Jenkins slave on a remote machine. In this article we will see how we can configure Slave on the remote machine using SSH configuration.

1. On the Remote machine (Ex- create a SSH Key. Use the Same Jenkins User as we configured in the last article. Login as Jenkins and run the ssh key generation commands as,

[jenkins@devm ~]$ ssh-keygen -t rsa -C "this is access to Jenkins Slave"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jenkins/.ssh/id_rsa):
Created directory '/home/jenkins/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jenkins/.ssh/id_rsa.
Your public key has been saved in /home/jenkins/.ssh/
The key fingerprint is:
af:e3:74:ae:24:96:53:0a:c6:fd:43:29:e7:e6:47:f3 this is access to Jenkins Slave
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|   . .   .       |
|    + o S        |
|   . . X .o      |
|      * B.oo     |
|     . B.=. E    |
|       .=+.      |

Save the Keys in the default location. Once keys are created we can see the public and private keys in the /home/Jenkins/.ssh location. Copy the Public key ( ) to authorized_keys file.  

[jenkins@devm ~]$ cd /home/jenkins/
[jenkins@devm ~]$ cd .ssh/
[jenkins@devm .ssh]$ ll
total 8
-rw-------. 1 jenkins jenkins 1675 Aug 25 09:50 id_rsa
-rw-r--r--. 1 jenkins jenkins  413 Aug 25 09:50
[jenkins@devm .ssh]$ cat >> authorized_keys
Make Sure the file permissions on the authorized_keys file is 0644.

2.  Once the key generation is done, we need to copy the private key to the Jenkins so that Jenkins can communicate with the host.

On the Jenkins Side -> Credentials -> System -> Global Credentials -> Add Credentials
ON the Right Hand ,
Select Kind -> “SSH Username and Private Key”
Scope -> Global
User name -> Jenkins
Private Key -> Enter Directly ( This will give you a text box at bottom , enter the Contents that are copied from id_rsa file in the remote machine that we created the keys for)

By this we have configured the SSH Private Key to the Credentials.

3. Create a Slave but Choose different options in the Launch method
Launch method -> Launch Slave agents on Unix machine via SSH. This gives you a Host and Credentials elements under this

Enter the Host Name (Ex-
Select the newly Created user name Jenkins that we created above from the Credentials List Box.

Once saved, the salve will be automatically stared. Make sure JAVA is configured and is available for the Jenkins to start or else we will see Java Not found exceptions

More To Come, Happy learning J

No comments :

Post a Comment