{"id":900,"date":"2023-11-07T02:28:00","date_gmt":"2023-11-07T02:28:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=900"},"modified":"2023-11-07T02:28:00","modified_gmt":"2023-11-07T02:28:00","slug":"setup-slaves-on-jenkins-2","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=900","title":{"rendered":"How to Setup Jenkins Agent\/Slave Using SSH"},"content":{"rendered":"<p>In this beginner&#8217;s guide, we will walk you through the steps to set up Jenkins agent nodes using SSH (Password and SSH key-based authentication).<\/p>\n<p>One of the best features of Jenkins is its distributed nature. You can configure multiple build slaves for better segregation and scalability.<\/p>\n<p>For example, you might want to test a cross-platform code base with a different operating system. In this case, you can set up different Jenkins agents or slaves and run the job against them.<\/p>\n<p>Also, a <a href=\"https:\/\/devopscube.com\/jenkins-architecture-explained\/\" rel=\"noreferrer noopener\">Jenkins distributed architecture<\/a> will reduce the load on the master server.<\/p>\n<h2 id=\"setup-jenkins-agentsslaves-on-jenkins\">Setup Jenkins Agents\/Slaves on Jenkins<\/h2>\n<p>In this guide, I will walk you through the steps for setting up the Jenkins agent node using password and SSH keys.<\/p>\n<p>There are two ways of authentication for setting up the Linux Jenkins slave agents.<\/p>\n<ol>\n<li>Using username and password<\/li>\n<li>Using ssh keys.<\/li>\n<\/ol>\n<p>Also, do check out my <a href=\"https:\/\/devopscube.com\/docker-containers-as-build-slaves-jenkins\/\">Jenkins Docker Based Agent<\/a>. You can scale and manage your Jenkins agent using <a href=\"https:\/\/devopscube.com\/what-is-docker\/\" rel=\"noreferrer noopener\">Docker containers<\/a>.<\/p>\n<h2 id=\"jenkins-agent-prerequisites\">Jenkins Agent Prerequisites<\/h2>\n<p>For Jenkins agent configuration, you need to have the following in the slave machines before adding it to the master.<\/p>\n<ol>\n<li>Java should be installed on your agent server.<\/li>\n<li>A valid Linux user account that can perform the required tasks on the agent server. (preferably a sudo user if your job requires elevated privileges)<\/li>\n<li>Git should be installed as most build job requires git-specific actions.<\/li>\n<\/ol>\n<p>Let&#8217;s get started with the Jenkins agent node configuration.<\/p>\n<h2 id=\"create-a-jenkins-user\">Create a Jenkins User<\/h2>\n<p>It is recommended to execute all Jenkins jobs as a Jenkins user on the Jenkins agent nodes.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note:<\/strong><\/b> I am using Ubuntu machine as a slave. The steps will be same for other Linux flavours as well<\/div>\n<\/div>\n<p><strong>Step 1:<\/strong> Create a Jenkins user and a password using the following command.<\/p>\n<pre><code>sudo adduser jenkins --gecos \"\"<\/code><\/pre>\n<p>Type a password when prompted. The above commands should create a user and a home directory named Jenkins under &#8220;\/home&#8221;.<\/p>\n<p><strong>Step 2:<\/strong> Now, log in as a Jenkins user.<\/p>\n<pre><code>su jenkins<\/code><\/pre>\n<p><strong>Step 3: <\/strong>Create a &#8220;<strong><code>jenkins-agent<\/code><\/strong>&#8221; directory under \/home\/jenkins.<\/p>\n<pre><code>mkdir \/home\/jenkins\/jenkins-agent<\/code><\/pre>\n<h2 id=\"setting-up-jenkins-agentsslaves-using-username-and-password\">Setting up Jenkins Agents\/Slaves using username and password<\/h2>\n<p><strong>Step 1: <\/strong>Head over to Jenkins dashboard &#8211;&gt; Manage Jenkins &#8211;&gt; Nodes<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-39-14.png\" class=\"kg-image\" alt=\"Jenkins Nodes Option\" loading=\"lazy\" width=\"1045\" height=\"585\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-39-14.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-39-14.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-39-14.png 1045w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p><strong>Step 2: <\/strong>Select the new node option.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-40-15.png\" class=\"kg-image\" alt=\"Jenkins new node option\" loading=\"lazy\" width=\"939\" height=\"476\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-40-15.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-40-15.png 939w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p><strong>Step 3:<\/strong> Give it a name, select the &#8220;<code>permanent agent<\/code>&#8221; option and click <strong>Create<\/strong>.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-41-14.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"732\" height=\"625\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-41-14.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-41-14.png 732w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p><strong>Step 4: <\/strong>Enter the details as shown in the image below and save them.<\/p>\n<p>For the credential box, click the add button and enter the agent server username and password. In our case, the username is <strong><code>jenkins<\/code><\/strong> and password is the one you created when you created the Jenkins user on the agent server. This credential will be used by the Jenkins controller to connect to the agent.<\/p>\n<p>To know more about each option, click the question mark on the right side of each text box.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/jenkins-agent-config-1.png\" class=\"kg-image\" alt=\"Jnekins Agent Node Configuration\" loading=\"lazy\" width=\"1141\" height=\"1692\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/jenkins-agent-config-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/jenkins-agent-config-1.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/jenkins-agent-config-1.png 1141w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p><strong>Step 4: <\/strong>Once you click save, the controller will automatically connect to the agent server and configure it as an agent.<\/p>\n<p>If the agent is not connected automatically,<\/p>\n<p>By following the same steps, you can add multiple servers as Jenkins agent nodes.<\/p>\n<h2 id=\"setting-up-jenkins-agent-using-ssh-keys\">Setting up Jenkins Agent Using SSH keys<\/h2>\n<p><strong>Step 1:<\/strong> Log in to the slave server as a jenkins user.<\/p>\n<p><strong>Step 2:<\/strong> Create a .ssh directory and cd into the directory.<\/p>\n<pre><code>mkdir ~\/.ssh &amp;&amp; cd ~\/.ssh<\/code><\/pre>\n<p><strong>Step 3:<\/strong> Create an ssh key pair using the following command. Press enter for all the defaults when prompted.<\/p>\n<pre><code>ssh-keygen -t rsa -C \"The access key for Jenkins slaves\"<\/code><\/pre>\n<p><strong>Step 4: <\/strong>Add the public to <code>authorized_keys<\/code> file using the following command.<\/p>\n<pre><code>cat id_rsa.pub &gt;&gt; ~\/.ssh\/authorized_keys<\/code><\/pre>\n<p><strong>Step 5:<\/strong> Now, copy the contents of the private key to the clipboard.<\/p>\n<pre><code>cat id_rsa<\/code><\/pre>\n<h3 id=\"add-the-ssh-private-key-to-jenkins-credentials\">Add the SSH Private Key to Jenkins Credentials<\/h3>\n<p><strong>Step 1:<\/strong> Go to Jenkins dashboard &#8211;&gt; credentials &#8211;&gt; Global credentials &#8211;&gt; add credentials, select and enter all the credentials as shown below and click ok.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/6-4.png\" class=\"kg-image\" alt=\"Jenkins agent node ssh credentials\" loading=\"lazy\" width=\"888\" height=\"534\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/6-4.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/6-4.png 888w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h2 id=\"connect-ssh-jenkins-agent-to-master-node\">Connect SSH Jenkins Agent to Master Node<\/h2>\n<p><strong>Step 1: <\/strong>Follow the first 3 steps we did for slave configuration using username and password.<\/p>\n<p><strong>Step 2: <\/strong>Follow all the configurations in the 4th step as well. But this time, for the launch method, select the credential you created with the SSH key.<\/p>\n<h2 id=\"test-jenkins-agentslave-nodes\">Test Jenkins Agent\/Slave Nodes<\/h2>\n<p>To test the Jenkins agent node or slave, create a sample project and select the option as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/4-4.png\" class=\"kg-image\" alt=\"Jenkins job with agent node label.\" loading=\"lazy\" width=\"1046\" height=\"610\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/4-4.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/4-4.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/4-4.png 1046w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>You need to select the node using the label option. If you start to type the letter the node list will show up.<\/p>\n<p>You can then run a simple, shell script operation to check if the Jenkins agent is executing the job.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this tutorial, we learned how to set up slaves for Jenkins. There is no complex step involved in this configuration.<\/p>\n<p>When it comes to automating the process, you will need scripts and Jenkins CLI to do this. I will cover that in future posts.<\/p>\n<p>Share if you like it and leave a comment if you have queries!!<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-slaves-on-jenkins-2\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Setup Jenkins Agent\/Slave Using SSH \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-slaves-on-jenkins-2\/<\/p>\n","protected":false},"author":1,"featured_media":901,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-900","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=900"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/901"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}