Adding a Linux Slave node to Jenkins

This is a step-by-step instruction to add a Linux slave node to Jenkins.

Setup

  • Get a new server that runs Linux
  • Set up a Jenkins user (you don’t necessarily need Jenkins itself installed, but you need a dedicated user), with a home folder and proper permissions. Ideally, the user is called “jenkins” on both machines.
  • Put the master’s Jenkins SSH public key on the slave’s Jenkins user authorized_keys file (e.g. ~/.ssh/authorized_keys)

Adding the node to Jenkins

  1. Log into Jenkins with an administrator account
  2. On the Home page, go to “Manage Jenkins”, then “Manage Nodes”
  3. On the left, press “New Node”
  4. Enter a node name, then press “Dumb Slave”, then OK
  5. Enter “1” as the number of executors (you probably don’t want to build two devices at the same time)
  6. Enter your path to your jenkins user home in “Remote root directory” (e.g. /home/jenkins/workspace).
  7. Make sure Usage is set to “Only build jobs with label restrictions matching this node”, and Launch method is set to “Launch slave agents on Unix machines via SSH”
  8. Below the Launch method drop-down, enter the slave’s IP address in “Host”
  9. Choose the credential of jenkins user on master, otherwise create a new one as below
    1. Press “Add” to create a new credential
    2. Select “SSH Username with private key” kind, enter the remote username (in our case, “jenkins”)
    3. Input the private key directly or select option “From the Jenkins master ~/.ssh”
    4. If your key has a passphrase, press “Advanced” and type in the passphrase
    5. Press Save to close the dialog, and choose your new credential in the list
  10. Press Save again to save your node.

Your node is now setup! If connection can be made properly, you should see the slave’s statistics appearing in the “Manage Nodes” page.

FAQ

How to generate key-pare for SSH connection

Open PuTTYgen and click “Generate” button with key type “SSH-2 RSA“, download the public and private key files to local disk.

The public key must be in one line and starts with “ssh-rsa“, e.g.

ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAjHnf5dRJPCIADtGBSUR5kM5xCC+3vttHjsMekIzmKt0pdOf2RrI5zLP00OLcRyis2uYX/bqR4pwJH13M/5zPPyKFHWoxhH3pLDlP6IkYvT3U4H+00tN5Y6aSt8H7ae1q0tB6VhqWPvv+SS1L9eY7as9if3dJw0ZkrWYVBX5IlGtWZH0Uu3qsp1p3k+l600CghOVvGlYMAgzdewMPJaRiiiC05+TUZYEPj1QnFLiIZdnQUqx8rwUj8dMUAPjzFZZHFhEiPJjX5AC885n/WBRW/dIhlurzZvsUUoKWB5KYgi1JxNOz4qOD8IriXOwOwiYAedsv8rghfbb1VqPIC3PICQ== rsa-key-20150917

The private key is stored in Jenkins system as a credential object with below,

  • Kind – SSH Username with private key
  • Scope – System (Jenkins and nodes only)
  • Username – jenkins
  • Description – <be specific>
  • Private key – Enter directly or From a file or From ~/.ssh

How to set up Jenkins user

Create a new user named as “jenkins” if it’s not existed on Linux node

Use command “useradd” or “adduser” in Linux and other Unix-like operating systems, and set a non-empty password to unlock the user

useradd jenkins
passwd jenkins
Changing password for user jenkins.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Set proper permissions to jenkins user home directory and .ssh directory

$mkdir ~/.ssh
$chmod 700 ~/.ssh
$touch ~/.ssh/authorized_keys
$chmod 644 ~/.ssh/authorized_keys
$chown jenkins:jenkins ~/.ssh -R

How to trouble shoot when it still goes wrong

Enable the debug level log in file /etc/ssh/sshd_config

LogLevel DEBUG3

Check the log from /var/log/auth.log or /var/log/secure

How To Fix “Server refused our key” Error in CentOS 6

Refer to link, try below command

restorecon -R -v /home/jenkins/.ssh
Advertisements

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s