Fix Jenkins Agent Disconnecting Randomly
The Error: Agent Keeps Disconnecting
Builds fail with:
ERROR: Connection was broken: java.io.IOException:
Unexpected termination of the channel
Or in the agent status page:
Agent went offline because Jenkins could not connect to it.
java.net.SocketTimeoutException: connect timed out
The agent appears online briefly, runs part of a build, then drops. Builds get stuck in the queue or fail mid-step.
Root Cause
Jenkins agent disconnections usually stem from:
- JVM heap exhaustion on the agent — the agent process runs out of memory and crashes
- Network timeout between controller and agent — firewalls, NAT, or load balancers drop idle TCP connections
- SSH keepalive not configured — for SSH-connected agents, idle connections are killed
- Agent machine running out of disk space — the remoting JAR cannot write temporary files
- Mismatched Java versions — controller and agent running incompatible JVM versions
Step-by-Step Fix
1. Check agent logs for the root cause
# Agent log location (varies by launch method)
# JNLP agent
cat /var/jenkins/agent/logs/remoting.log
# SSH agent — check from Jenkins UI
# Manage Jenkins > Nodes > <agent> > Log
Look for OutOfMemoryError, SocketTimeoutException, or Connection reset.
2. Increase agent JVM heap
The default -Xmx for the agent remoting process is often too low:
# For JNLP/inbound agents, update the launch command
java -Xmx512m -jar agent.jar \
-url https://jenkins.example.com \
-secret <secret> \
-name <agent-name> \
-workDir /var/jenkins/agent
For SSH agents, set JVM options in Jenkins UI:
- Go to Manage Jenkins > Nodes > [agent] > Configure
- Under Launch method > Advanced, set JVM Options:
-Xmx512m
3. Configure TCP keepalives
Prevent firewalls from dropping idle connections. On the Jenkins controller, go to Manage Jenkins > Configure Global Security and set:
- TCP port for inbound agents: Fixed port (e.g., 50000)
On the agent machine, tune kernel keepalives:
# Keep TCP connections alive through firewalls/NAT
sudo sysctl -w net.ipv4.tcp_keepalive_time=60
sudo sysctl -w net.ipv4.tcp_keepalive_intvl=10
sudo sysctl -w net.ipv4.tcp_keepalive_probes=6
# Make permanent
cat <<EOF | sudo tee -a /etc/sysctl.d/99-keepalive.conf
net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_keepalive_intvl = 10
net.ipv4.tcp_keepalive_probes = 6
EOF
4. Fix SSH agent keepalives
For SSH-based agents, configure the SSH client on the controller:
# /var/lib/jenkins/.ssh/config (or equivalent)
Host *
ServerAliveInterval 30
ServerAliveCountMax 5
TCPKeepAlive yes
And on the agent's SSH server:
# /etc/ssh/sshd_config
ClientAliveInterval 30
ClientAliveCountMax 5
sudo systemctl reload sshd
5. Check disk space on the agent
df -h /var/jenkins/agent
# Clean up old workspaces
find /var/jenkins/agent/workspace -maxdepth 1 -mtime +7 -exec rm -rf {} +
Jenkins stores build artifacts, workspaces, and remoting cache on the agent. If the disk fills, the agent crashes.
6. Match Java versions
# On the controller
java -version
# On the agent
java -version
Jenkins requires compatible Java versions. If the controller runs Java 17, agents should also run Java 17. Mismatches cause serialization errors and disconnects.
7. Use the availability strategy
In the agent configuration (Manage Jenkins > Nodes > [agent] > Configure):
- Set Availability to "Keep this agent online as much as possible"
- Set # of Retries for reconnection to 5+
- Set Seconds Between Retries to 15
Prevention Tips
- Monitor agent health with the Jenkins Monitoring plugin — track heap usage, thread count, and response time.
- Use ephemeral agents (Kubernetes pod agents, EC2 plugin, Docker agents) to avoid long-lived connection issues entirely.
- Set workspace cleanup policies — use the Workspace Cleanup plugin to delete workspaces after builds.
- Pin Java versions in your agent provisioning automation so controller/agent versions always match.
- Alert on agent offline events — use the Jenkins Notification plugin or a Prometheus exporter to catch disconnections before users report stuck builds.
Was this article helpful?
Related Articles
Fix Jenkins Pipeline 'Scripts Not Permitted to Use' / 'Script Not Yet Approved'
Resolve Jenkins Groovy sandbox errors by approving script signatures, configuring the script security plugin, and using safe alternatives.
Jenkins Declarative Pipelines: From Zero to Production CI/CD
How to write Jenkins declarative pipelines from scratch — stages, agents, environment variables, credentials, parallel execution, post conditions, and shared libraries. Practical Jenkinsfile patterns for real projects.
Jenkins Kubernetes Agents: Dynamic Build Pods for Scalable CI/CD
Configure Jenkins to dynamically spin up Kubernetes pods as build agents. Covers the Kubernetes plugin, pod templates, JNLP agents, multi-container pods, resource limits, and RBAC — so you never manage static build nodes again.
Fix SSH 'Connection Refused' After Server Reboot
Diagnose and fix SSH connection refused errors after a Linux server reboot, covering sshd service, firewall rules, port configuration, and host key issues.
Jenkins Declarative Pipelines: The Complete Jenkinsfile Guide
Master Jenkins declarative pipelines — stages, steps, post actions, environment variables, credentials, parallel execution, and when conditions.
Docker Agents in Jenkins: Reproducible Builds Every Time
Run Jenkins pipeline stages inside Docker containers for clean, reproducible builds — agent configuration, custom images, Docker-in-Docker, and Kaniko alternatives.
More in Jenkins
View all →Jenkins Installation & Configuration: From Zero to First Pipeline
Install Jenkins on Ubuntu and Docker, configure security settings, manage plugins, and create your first freestyle and pipeline jobs step by step.
Jenkins Shared Libraries: Reusable Pipeline Code at Scale
Build and maintain Jenkins shared libraries — directory structure, global vars, custom steps, class-based libraries, testing, and versioning strategies.