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 apt-get update
sudo apt-get install openjdk-8-jre
sudo apt-get 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.

15 thoughts on “Add linux slave node in the Jenkins

  1. I have done this a couple of times already, but this is the first time I come across some notes using the ssh-copy-id command. Thanks so much!


  2. Thanks, Mohit Goyal

    It worked for me but in the logs, it gave the below error. I have SFTP installed in my slave Ubuntu 16.04.3 LTS

    could you please let me know how to fix this issue?

    [01/11/18 18:35:08] [SSH] Starting sftp client.
    ERROR: [01/11/18 18:35:08] [SSH] SFTP failed. Copying via SCP. The subsystem request failed.
    at com.trilead.ssh2.Session.startSubSystem(
    at com.trilead.ssh2.SFTPv3Client.(
    at com.trilead.ssh2.SFTPv3Client.(
    at hudson.plugins.sshslaves.SFTPClient.(
    at hudson.plugins.sshslaves.SSHLauncher.copySlaveJar(
    at hudson.plugins.sshslaves.SSHLauncher.access$400(
    at hudson.plugins.sshslaves.SSHLauncher$
    at hudson.plugins.sshslaves.SSHLauncher$
    at java.util.concurrent.ThreadPoolExecutor.runWorker(
    at java.util.concurrent.ThreadPoolExecutor$
    Caused by: The server denied the request.
    … 12 more
    [01/11/18 18:35:08] [SSH] Copying latest slave.jar…
    Expanded the channel window size to 4MB
    [01/11/18 18:35:08] [SSH] Starting slave process: cd “/home/tushar” && exec java -jar slave.jar
    channel started
    Remoting version: 3.14
    This is a Unix agent
    Evacuated stdout
    Agent successfully connected and online


  3. Hello Mohit
    Can you share how to configure Jenkins CI is hosted on Linux and we have different Windows VM to run our automation test suite.


  4. Hi, I did the same and It’s not working yet. I have the next error:

    ERROR: Server rejected the 1 private key(s) for git

    Can you help me please?


      1. Thanks Mohit.

        I was using the same ssh key that I’m using for connect with GitLab, that was my error. Now create a new ssh key for my slave but when try to connect and run, this is my new error

        Authentication failed.

        On Host Key Verification Strategy with Know host say:

        SSH host key matches key in Known Hosts file. Connection will be allowed.

        But is rejected and I don’t know why. The result is the same with Non verifying verification.


  5. Thanks Mohit,
    This got me thru the SSH Configuration part of the linux Node/Slave setup. I will add a couple of things I had problems with here just in case someone else encounters them:

    1. JAVA_HOME was not set to the correct value, not sure why this happens. The solution for me was to add and environment variable to the node Name: JAVA_HOME Value:
    2. X11 there was an X problem which causes drivers to fail to start, solution
    sudo apt-get install Xvfb on the slave and add another environment variable to the node configuration in Jenkins Name: DISPLAY , Value: 0: – this got me a bit further
    3. I was using driver.manage().window().maximize() to max the window size, apparently this does not work on a node with Chrome and Xvfb. Switch to options.addArguments(“–start-maximized”);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s