Saturday, October 20, 2018

Jenkins - Install Publish Over SSH plugin and execute command on Remote Linux System




Publish Over SSH: Publish Over SSH is one of the plugins used to execute a command over Remote Linux Systems. It's easy to configure and manage, gives us the easier way to run a command on a remote machine.

In this my blog - how to install this Plug-in and setup initial configuration.



Prerequisites:
  • Jenkins instance admin access
  • another Linux system with sudo access.
Step 1- Install Plug-in - To install Publish over SSH plugin follow the step below to finish the installation.
Log in to Jenkins System > Manage Jenkins > Manage Plugins 
  • Select available plug-in tab > type "Publish over SSH" in the search bar on your right upside.

  • Over Next page, check to restart Jenkins when installation is complete.



After successful installation of plug-in login to Jenkins again.

Step 2- Generate the Private and Public key: In order to set up communication between Jenkins server and Linux server. We need to Private Key (Place at Jenkins server) and Public key (Place on Remote Linux server). 

Let's generate both the keys, using ssh-keygen command to generate the keys. 


  • SSH your Linux Server - Login user have sudo access
  • Create a new user (Jenkins_srv) : To create new user run the command below.

$ sudo adduser jenkins_srv

Adding user `jenkins_srv' ...
Adding new group `jenkins_srv' (1004) ...
Adding new user `jenkins_srv' (1004) with group `jenkins_srv' ...
Creating home directory `/home/jenkins_srv' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:  ******
Retype new UNIX password: ******
passwd: password updated successfully
Changing the user information for jenkins_srv
Enter the new value, or press ENTER for the default
        Full Name []: Jenkins_srv
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y
Once a user created successfully. Generate keys using the following command 
jenkins_srv@docker:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jenkins_srv/.ssh/id_rsa):
Created directory '/home/jenkins_srv/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jenkins_srv/.ssh/id_rsa.
Your public key has been saved in /home/jenkins_srv/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:/TQdg4K4mI4IV2WMVagTJCN3TyxJ+MXvtWi0SkowyRE jenkins_srv@docker
The key's randomart image is:
+---[RSA 2048]----+
|. E=+B=o.        |
| oo+==*. .   .   |
| . +.=o.. . . o  |
|  =.+o .o... . o |
|. .oo..oS+..o .  |
|.o o. . = .o .   |
|. ...o o    .    |
|    . .          |
|                 |
+----[SHA256]-----+
jenkins_srv@docker:~$


  • Key created successfully, let check keys using the command below.

jenkins_srv@docker:~$ ls -lah .ssh/

-rw------- 1 jenkins_srv jenkins_srv 1.7K Oct 18 10:56 id_rsa
-rw-r--r-- 1 jenkins_srv jenkins_srv  400 Oct 18 10:56 id_rsa.pub
  • Now copy your Public keys to Authorized_key. run below command.
$ cp id_rsa.pub authorized_key
  • Now check .ssh folder to varified authorized_keys available  on Linux system:
jenkins_srv@docker:~$ ls -alh .ssh/

-rw-r--r-- 1 jenkins_srv jenkins_srv  400 Oct 18 11:31 authorized_keys
-rw------- 1 jenkins_srv jenkins_srv 1.7K Oct 18 11:31 id_rsa
-rw-r--r-- 1 jenkins_srv jenkins_srv  400 Oct 18 11:31 id_rsa.pub
 Step 4- Setup Private Key on Jenkins Server:
Now, Login to Jenkins server and follow the below steps to complete setup private key configuration.
  • Login to Jenkins server
  • Manage Jenkins > Configure System



  • Now go to option Publish Over SSH - and paste Private keys here.
You have successfully added Linux host to Jenkins Server. Now, you can execute commands using Publis over SSH plugin. 

 Step 5-  Grant Jenkins_svc user sudo access:
Sometimes, we need to execute some command as sudo user, let grant our user sudo accessing using by the command below.

  • Login Linux System with user have sudo access and edit file /etc/sudoers

# vi /etc/sudoers
and add this line: jenkins_srv - ALL=(ALL) NOPASSWD:ALL

# User privilege specification
root    ALL=(ALL:ALL) ALL
jenkins_srv     ALL=(ALL) NOPASSWD:ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL


Your sudoers file should be like above screenshot. Save and exit from the file.
 Step 6- Test setting by executing a command.
  • In order to test plugin settings, let's set up a freestyle project. 
  • Now, Execute the job and check Job's output for details.


  • Let's check Job's Output:

Now, you are able to execute the command to a remote Linux machine. All set go setup more job with other configuration.

Thank you! please leave your comment to make the blog better!

2 comments: