Add linux slave node in the Jenkins

As per best practices, the master node should be only used for storing configuration and backup purposes. Only slaves should be used for build. In this blog post, we’ll discover steps required for adding slave node in the Jenkins farm. Most of these steps will cover how to prepare linux slave server for Jenkins usage. The below commands are for CentOS 7 server but these can be easily translated to other linux distros.

Install Java on the Slave server
Run below command on the server:

sudo yum update
sudo yum install openjdk-8-jre
sudo yum install openjdk-8-jdk
You can check if jvm is installed properly using java -version.

In order to help Java-based applications locate the Java virtual machine properly, you need to set two environment variables: “JAVA_HOME” and “JRE_HOME”:

export JAVA_HOME=’/usr/lib/jvm/jre-1.8.0-openjdk’
export JRE_HOME=’/usr/lib/jvm/java-8-openjdk-amd64/jre’
Edit profile script and add these two export commands to it so that these variables are always available whenever the system restarts.

Add administrative service user to the Slave server
This is important from administrative and auditing point of view. In our case, let’s say that the service account name is Jenkins. We’ll also create a user group named jenkins. For this, run below command

sudo useradd jenkins -U -s /bin/bash
Verify that user and group are created by checking /etc/passwd and /etc/group files. Now change the password associated with this account using:

sudo passwd jenkins
and enter new password when asked. Now, configure sudo privileges for this user by modifying /etc/sudoers:

Modifying sudoers to assign sudo privileges.JPG
Modifying sudoers to assign sudo privileges
Configure SSH Key authentication for Jenkins
First we need to create the key pair on the master machine:

ssh-keygen -t rsa
Once you have entered the Gen Key command, you will get a few more questions about file location to save keypair and passphrase. It’s up to you whether you want to use a passphrase. Entering a passphrase does have its benefits: the security of a key, no matter how encrypted, still depends on the fact that it is not visible to anyone else. The only downside, of course, to having a passphrase, is then having to type it in each time you use the Key Pair. for our purposes, we’ll leave the passphrase as empty.

Below is a sample run from my lab machine:

Generate ssh keypair on the slave jenkins server.JPG
Generate ssh keypair on the master jenkins server
The public key is now located in /home/jenkins/.ssh/ The private key (identification) is now located in /home/jenkins/.ssh/id_rsa.

Once the key pair is generated, it’s time to place the public key into the slave machine’s authorized_keys file with the ssh-copy-id command:

ssh-copy-id jenkins@
You need to replace the username and password in the above command as per your environment. Also note that if you are doing this on a cloud virtual machine, do the same for the internal as well as public ip of the machine.

You should see something like below output:

[jenkins@centos2 ~]$ ssh-copy-id jenkins@
The authenticity of host ‘ (’ can’t be established.
ECDSA key fingerprint is 53:c2:32:63:12:a2:8f:29:25:40:fa:0a:b1:d4:8c:f4.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys
jenkins@’s password:

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘jenkins@′”
and check to make sure that only the key(s) you wanted were added.
Now you can go ahead and login into the machine with your username and you will not be prompted for password.

Setup relationship between slave and master
Login into Jenkins master machine with administrative credentials. First go to Manage Jenkins -> Manage plugins and install ‘SSH Slaves Plugin’. Now Go to Manage Jenkins -> Manage node:

Manage node section.JPG
Manage node section
Select new node from left pane. Then enter slave machine’s IP address and select ‘Permanent Agent’ and click okay. This will ask for further details.

In the ‘# of executors’, select maximum number of concurrent builds that Jenkins may perform on this agent. Generally, this is set as per no of processor cores available on the remote machine. For our purposes, we’ll set it to 10.

In the ‘remote root directory’, add the path for a directory dedicated to be used by agent which should be /home/jenkins. In the launch method, select ‘launch slave agents via ssh’ and add the slave machine’s ip address and credentials.

These are going to be details for our case:

Add slave machine’s details.JPG
Add slave machine’s details
In the last, click save and then okay. It’ll take few minutes to connect and bring the slave node online. For checking logs, click on the slave machine name and then click logs:

Click on the log tab associated to dropdown for Slave machine.png
Click on the log tab associated to dropdown for Slave machine.png
Once you click logs, you should be able to see output like below:

[02/14/17 07:39:01] [SSH] Opening SSH connection to
[02/14/17 07:39:02] [SSH] Authentication successful.
[02/14/17 07:39:03] [SSH] The remote users environment is:
BASH_VERSINFO=([0]=”4″ [1]=”2″ [2]=”46″ [3]=”1″ [4]=”release” [5]=”x86_64-redhat-linux-gnu”)
IFS=$’ \t\n’
LESSOPEN=’||/usr/bin/ %s’
PS4=’+ ‘
SSH_CLIENT=’ 44392 22′
SSH_CONNECTION=’ 44392 22’
command_not_found_handle ()
local runcnf=1;
local retval=127;
[[ $- =~ i ]] || runcnf=0;
[ ! -S /var/run/dbus/system_bus_socket ] && runcnf=0;
[ ! -x /usr/libexec/packagekitd ] && runcnf=0;
[ ${COMP_CWORD-} ] && runcnf=0;
if [ $runcnf -eq 1 ]; then
/usr/libexec/pk-command-not-found “$@”;
local shell=`basename “$SHELL”`;
echo “$shell: $1: command not found”;
return $retval
[02/14/17 07:39:03] [SSH] Checking java version of java
[02/14/17 07:39:03] [SSH] java -version returned 1.8.0_121.
[02/14/17 07:39:03] [SSH] Starting sftp client.
[02/14/17 07:39:03] [SSH] Copying latest slave.jar…
[02/14/17 07:39:04] [SSH] Copied 715,860 bytes.
Expanded the channel window size to 4MB
[02/14/17 07:39:04] [SSH] Starting slave process: cd “/home/jenkins” && java -jar slave.jar
channel started
Slave.jar version: 3.2
This is a Unix agent
Evacuated stdout
Agent successfully connected and online
If everything mentioned above is configured correctly, it should be able to successfully connect to the slave machine