Jenkins Agents and Nodes: Distributed Builds, Labels, SSH and Inbound Agents, Executors

CI CD Pipelines with Jenkins 40 min min read Updated: Nov 17, 2026 Intermediate
Jenkins Agents and Nodes: Distributed Builds, Labels, SSH and Inbound Agents, Executors
Intermediate Topic 1 of 3

Why Agents?

  • Capacity: run many builds at once on many machines
  • Different environments: Linux, Windows, macOS (for iOS), GPUs, special networks
  • Safety: build code can be untrusted. Keep it off the controller that holds all your secrets
  • Isolation: one team or project per agent group
  • Elasticity: create agents on demand in Docker or Kubernetes and delete them afterwards
Rule: Set the controller's *Number of executors* to 0 (*Manage Jenkins, Nodes, Built-In Node, Configure*). All builds then run on agents.

Agent Types

TypeHow it connectsGood for
SSH agentThe controller opens an SSH connection to the machine and starts the agentLinux servers with SSH access. Simple
Inbound agent (WebSocket)The agent starts on the machine and connects out to the controller over HTTP(S)Machines behind firewalls or NAT; works through a normal reverse proxy
Inbound agent (TCP)Agent connects to the controller's TCP port (50000)Older setups. WebSocket is easier
Windows service agentInstalled as a service using WinSW or the inbound methodWindows builds
Docker / Kubernetes agentsCreated on demand per build and removed afterwardsClean, repeatable builds (next lessons)

Prepare the Agent Machine

bash # On the agent (Linux) sudo apt-get update && sudo apt-get install -y openjdk-21-jre git sudo useradd -m -s /bin/bash jenkins-agent sudo mkdir -p /opt/jenkins-agent && sudo chown jenkins-agent: /opt/jenkins-agent java -version # the agent needs a Java version supported by your controller

Add an SSH Agent

  1. Create an SSH key pair on the controller (or your PC): ssh-keygen -t ed25519 -f jenkins-agent-key -C jenkins-agent
  2. Add the public key to ~/.ssh/authorized_keys of jenkins-agent on the machine
  3. In Jenkins add a SSH Username with private key credential with the private key
  4. *Manage Jenkins, Nodes, New Node*, name linux-1, type Permanent Agent
  5. Fill in: Remote root directory /opt/jenkins-agent, Labels linux docker maven, Usage *Only build jobs with label expressions matching this node*, Launch method *Launch agents via SSH*, host and credential
  6. Set Host Key Verification Strategy (Known hosts file or manually trusted key). Avoid *Non verifying* in production
  7. Save. The log shows Agent successfully connected and online

Add an Inbound Agent (WebSocket)

  1. New Node win-1 or linux-2, Permanent Agent, Launch method: *Launch agent by connecting it to the controller*; tick Use WebSocket
  2. Save. The node page shows the command with a secret
  3. Run it on the agent machine (as a service for production)
bash curl -sO https://jenkins.example.com/jnlpJars/agent.jar java -jar agent.jar -url https://jenkins.example.com/ -name linux-2 -secret @secret-file -workDir /opt/jenkins-agent -webSocket
text # /etc/systemd/system/jenkins-agent.service [Unit] Description=Jenkins inbound agent After=network-online.target [Service] User=jenkins-agent WorkingDirectory=/opt/jenkins-agent ExecStart=/usr/bin/java -jar /opt/jenkins-agent/agent.jar -url https://jenkins.example.com/ -name linux-2 -secret @/opt/jenkins-agent/secret -workDir /opt/jenkins-agent -webSocket Restart=always RestartSec=10 [Install] WantedBy=multi-user.target

Node Settings Explained

SettingMeaning
Number of executorsHow many builds can run at once on this node. Start with the number of CPU cores or fewer
Remote root directoryWhere workspaces are stored on the agent
LabelsWords used to select nodes: linux, docker, gpu, java21
Usage*Use this node as much as possible* or *Only build jobs with label expressions matching*
Node PropertiesEnvironment variables and tool locations for that node
AvailabilityKeep online always, or bring online on demand

Choose Where a Job Runs

groovy pipeline { agent { label 'linux && docker' } // both labels required stages { stage('Build') { steps { sh 'docker version' } } stage('Windows installer') { agent { label 'windows' } // this stage runs on another node steps { bat 'build-installer.cmd' } } stage('Any of two') { agent { label 'mac || arm64' } // either label steps { sh 'uname -m' } } stage('Specific machine') { agent { node { label 'linux-1'; customWorkspace '/data/ws/app' } } steps { sh 'pwd' } } } }
Label expressionMeaning
linuxHas the label linux
linux && dockerBoth
mac || windowsEither
!windowsNot Windows
(linux || mac) && !gpuGrouping

Monitor and Maintain Agents

  • The Nodes page shows status, free disk, free swap, response time and clock difference. Jenkins takes a node offline automatically when disk or temp space is too low
  • Click a node, then Log to see connection problems; Script Console (admin only) for debugging
  • Use ws-cleanup (cleanWs()) and a cron job to remove old workspaces, otherwise disks fill
  • Keep Java and OS patched. Update the agent via the controller: the agent.jar is downloaded from it, so versions match
  • Take a node offline for maintenance with *Mark this node temporarily offline*. Running builds finish
bash # Disk check on agents (also set up monitoring) df -h /opt/jenkins-agent du -sh /opt/jenkins-agent/workspace/* | sort -h | tail find /opt/jenkins-agent/workspace -maxdepth 1 -type d -mtime +14 -exec rm -rf {} + # careful: test first without -exec

Security for Agents

  • Agents run code from your jobs. Do not let anyone with *Job Configure* rights reach agents that have sensitive access unless it is intended
  • Give agents the minimum network access and cloud permissions
  • Controller to agent security (agent-to-controller access control) is on by default. Do not disable it
  • Use separate agents or labels for production deployments and for untrusted builds such as pull requests from forks
  • Do not store long-lived secrets on agents. Use Jenkins credentials injected at build time
  • Use TLS (HTTPS) between agents and the controller when using WebSocket

Troubleshooting Agents

SymptomCheck
Host key verification failedAdd the host key to known hosts or change the verification strategy
java: command not found on the nodeInstall Java, or set the Java path under node advanced settings
Inbound agent keeps disconnectingNetwork or proxy idle timeouts; run as a service with Restart=always; use WebSocket behind HTTPS
Permission denied (publickey)Public key not in authorized_keys, wrong user, wrong credential
Builds wait forever: Waiting for next available executorNo online node with the requested label, or all executors busy
Disk full on agentClean workspaces; add a cleanWs() step; increase disk

Practice Tasks

  1. Set the controller executors to 0 and add one SSH agent
  2. Add a second agent using WebSocket and run it as a systemd service
  3. Give each agent different labels and write a pipeline that uses label 'a && b'
  4. Mark an agent offline during a build and watch what happens to the queue
  5. Write down which jobs should never run on your production-deploy agent

Conclusion

Use agents to run builds: SSH agents are simple, WebSocket inbound agents work behind firewalls. Select them with labels, keep the controller at zero executors, clean workspaces, monitor disk and secure agents because they run job code.

Quick Summary: controller executors 0; SSH vs inbound; node settings; labels expressions; agent per stage; monitoring + cleanup; security; troubleshooting.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators