Jenkins Agents and Nodes: Distributed Builds, Labels, SSH and Inbound Agents, Executors
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
| Type | How it connects | Good for |
|---|---|---|
| SSH agent | The controller opens an SSH connection to the machine and starts the agent | Linux 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 agent | Installed as a service using WinSW or the inbound method | Windows builds |
| Docker / Kubernetes agents | Created on demand per build and removed afterwards | Clean, 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
- Create an SSH key pair on the controller (or your PC):
ssh-keygen -t ed25519 -f jenkins-agent-key -C jenkins-agent - Add the public key to
~/.ssh/authorized_keysofjenkins-agenton the machine - In Jenkins add a SSH Username with private key credential with the private key
- *Manage Jenkins, Nodes, New Node*, name
linux-1, type Permanent Agent - Fill in: Remote root directory
/opt/jenkins-agent, Labelslinux docker maven, Usage *Only build jobs with label expressions matching this node*, Launch method *Launch agents via SSH*, host and credential - Set Host Key Verification Strategy (Known hosts file or manually trusted key). Avoid *Non verifying* in production
- Save. The log shows
Agent successfully connected and online
Add an Inbound Agent (WebSocket)
- New Node
win-1orlinux-2, Permanent Agent, Launch method: *Launch agent by connecting it to the controller*; tick Use WebSocket - Save. The node page shows the command with a secret
- 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
| Setting | Meaning |
|---|---|
| Number of executors | How many builds can run at once on this node. Start with the number of CPU cores or fewer |
| Remote root directory | Where workspaces are stored on the agent |
| Labels | Words 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 Properties | Environment variables and tool locations for that node |
| Availability | Keep 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 expression | Meaning |
|---|---|
linux | Has the label linux |
linux && docker | Both |
mac || windows | Either |
!windows | Not Windows |
(linux || mac) && !gpu | Grouping |
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.jaris 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
| Symptom | Check |
|---|---|
Host key verification failed | Add the host key to known hosts or change the verification strategy |
java: command not found on the node | Install Java, or set the Java path under node advanced settings |
| Inbound agent keeps disconnecting | Network 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 executor | No online node with the requested label, or all executors busy |
| Disk full on agent | Clean workspaces; add a cleanWs() step; increase disk |
Practice Tasks
- Set the controller executors to 0 and add one SSH agent
- Add a second agent using WebSocket and run it as a systemd service
- Give each agent different labels and write a pipeline that uses
label 'a && b' - Mark an agent offline during a build and watch what happens to the queue
- 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.

