How To Configure Jenkins Linux slave nodes by Launching Agents via SSH

How To Configure Jenkins Linux slave nodes by Launching Agents via SSH


This is about configuring Linux slaves to Jenkins.


Configure a Jenkins linux slave node and connect it to the master machine, you can follow the steps below

Steps Details

Step#1 Ensure that both the master and slave machines have the same version of Java installed. check with below command

java --version        


Step#2 Assuming ssh is installed on slave agent or install it if not already installed

Installing SSH on Linux involves setting up the OpenSSH server package, which allows remote connections to your system. The specific commands will differ slightly depending on your Linux distribution's package manager


Debian-based systems (Ubuntu, Mint, etc.)

sudo apt update
sudo apt install openssh-server        

RedHat-based systems (CentOS, Fedora, RHEL, etc.)

sudo yum install openssh-server        


Verifying Installation

ssh        

These commands install the server package (openssh-server) to enable remote logins. You might also consider installing the openssh-client package for initiating SSH connections from your system to other servers.


Step#3 Generate SSH Keys and configure with Jenkins slave and master

These all steps are executed on the agent machine


  • Generate SSH Key Pair:

.ssh will be there but you may create it under your user profile if it doesn't exist: i.e. /root/.ssh

mkdir /root/.ssh        


If you don't have an SSH key pair, you can generate one using the following command:

ssh-keygen -t ecdsa-sha2-nistp256        
I used ecdsa algorithm, you may use any other as well like rsa


  • Copy the Public Key to the authorized_keys file placed inside .ssh folder

cat id_ecdsa.pub >> ~/.ssh/authorized_keys        


  • Add the Public Key to the known_hosts file on the master inside /var/lib/jenkins/.ssh/known_hosts in following format

Agent_IP Public_Key



Step#4 Configure Jenkins Agent with SSH Credentials

These all steps are executed on the master machine

Configure Jenkins Agent in Jenkins master:


  1. Go to Manage Jenkins -> Manage Nodes -> New Node
  2. Enter the Node Name there and select Permanent Agent then click OK ,
  3. Edit the Description (Optional),
  4. Name: A name for your agent. i.e. linux-slave-x99,
  5. Enter the # of Executor (One or more, In our case we are going to set up 2 Executors),
  6. Remote Directory: The directory where Jenkins will perform builds on the agent machine. i.e. /opt/jenkins1-slave
  7. Launch method: Choose "Launch agent via SSH"Availability: Keep it online as much as possible.
  8. Select the launch Method - Launch Agent via SSH)Enter the Host address (In our case, 172.28.128.20)
  9. Click the Add button next to Credentials and select the Jenkins scope.Alternate option is that Select username with password or SSH Username with the private key then save If username with password option is chosen, Simply, Username and password of “Jenkins” are enough.
  10. For the credential, set Kind to SSH username with Password [For Method 1]
  11. For the credential, set Kind to SSH username with private key [For Method 2] and provide the username and password
  12. Enter 'your username' for the username
  13. For the private key, select Enter directly. Then, copy the contents of your private key file (~/.ssh/id_ecdsa) and paste it into the Key box (Copy the private key from “id_ecdsa” file which was created in step#4)
  14. If you used a passphrase when generating the key, enter that for Passphrase, otherwise, leave it blank.
  15. Enter a descriptive id and description, then click Add.
  16. Select the Credentials created in the previous steps
  17. Select “Known hosts file Verification Strategy” for Host key Verification Strategy
  18. Others Keep default. (Click help icon and know everything from the page itself)
  19. Click Save to save the new node


Now just open the jenkins agent and click on Launch agent button and this should spin up your agent.



要查看或添加评论,请登录

Muhammad Raheel的更多文章

社区洞察

其他会员也浏览了