jcmd on a distroless pod: attaching to a JVM with kubectl debug

jcmd ships with the JDK. In a container that contains the JDK, a kubectl exec is enough to use it. A distroless image contains the JVM, but no shell and no jcmd. The same command fails:

$ kubectl -n prod exec orders -- jcmd 1 Thread.print
error: Internal error occurred: Internal error occurred: error executing command in container: failed to exec in container: failed to start exec "40220ca7057ce13c71a2aa29b730ec14011e93ce6363111d9b291047bc7e8f94": OCI runtime exec failed: exec failed: unable to start container process: exec: "jcmd": executable file not found in $PATH

An eclipse-temurin:25-jre image has a shell, but no jcmd either. So we have to bring jcmd into the pod, with kubectl debug. The command that works depends on the container’s user, on its filesystem and on the namespace.

The test pod is named orders. Its container does not run as root, and its filesystem is read-only, except for an emptyDir volume on /tmp. The program blocks a thread, order-42, on a lock held by the stock-refresh thread. We tested with Temurin 25.0.5.1+2 in the gcr.io/distroless/java25-debian13:nonroot image. In this image, the JVM runs as user 65532. The jcmd comes from the eclipse-temurin:25-jdk image. The cluster is k3s v1.34.1 with containerd 2.1.4 and a Linux 6.8 kernel. kubectl is version 1.34.1. The prod namespace enforces the restricted level of the Pod Security Standards. Each debug container is started with sleep, then we run the commands in it with kubectl exec.

How jcmd attaches to a JVM

To understand the errors, you need to know the four steps of the attach. They come from the JDK 25 source code, on Linux. Here the JVM is process 1.

  1. jcmd looks for a socket named .java_pid1 in the JVM’s /tmp. It reaches this directory through /proc/1/root/tmp, because the JVM is in another container. If it cannot write to it, it looks in its own /tmp.
  2. If the socket does not exist, jcmd creates an empty file .attach_pid1. It first tries the JVM’s working directory, then the directory from step 1.
  3. jcmd sends the SIGQUIT signal to the JVM. The JVM looks for the .attach_pid1 file in its working directory, then in its /tmp. If the JVM finds the file, and the file belongs to the JVM’s user or to root, the JVM creates the socket in /tmp. Otherwise, it writes a thread dump to its standard output.
  4. jcmd connects to the socket. The JVM accepts a connection from its own user and group, or from root.

jcmd resends SIGQUIT several times, then gives up after 10.5 seconds. The /tmp path of the socket is fixed: the java.io.tmpdir property does not change it.

The debug container is refused, then does not start

kubectl debug adds an ephemeral container to the pod, with the image we choose. With --target=app, this container sees the processes of the app container.

$ kubectl -n prod debug orders --image=eclipse-temurin:25-jdk --target=app --container=debug -- sleep 86400
Targeting container "app". If you don't see processes from this container it may be because the container runtime doesn't support this feature.
--profile=legacy is deprecated and will be removed in the future. It is recommended to explicitly specify a profile, for example "--profile=general".
Error from server (Forbidden): pods "orders" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "debug" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "debug" must set securityContext.capabilities.drop=["ALL"])

The default profile of kubectl 1.34 is legacy. It applies no security settings, and the restricted namespace refuses the container. With --profile=general, the refusal is the same. The message adds that the container must not request the SYS_PTRACE capability.

With --profile=restricted, the command is accepted and kubectl prints no error. But the container does not start:

$ kubectl -n prod get pod orders -o jsonpath={.status.ephemeralContainerStatuses[0].state.waiting}
{"message":"container has runAsNonRoot and image will run as root (pod: \"orders_prod(502e2731-143e-4ec7-9043-35315cde2771)\", container: debug)","reason":"CreateContainerConfigError"}

The restricted profile requires runAsNonRoot, and the eclipse-temurin:25-jdk image runs as root by default. The same failure happens in a namespace with no security level, when the pod declares runAsNonRoot.

This stuck container stays in the pod. The API refuses to remove it, with existing ephemeral containers "debug" may not be removed. It also refuses to change it: on the same pod, the next command must use another name, such as --container=debug2. Here, we start again from a new pod.

The debug container with the JVM’s user

The --custom option extends the profile with a file. We put the JVM’s user and group in it. From a debug container started as another user, stat -c %u:%g /proc/1 prints 65532:65532.

{"securityContext":{"runAsUser":65532,"runAsGroup":65532}}

With this file, the container starts and jcmd attaches:

$ kubectl -n prod debug orders --image=eclipse-temurin:25-jdk --target=app --container=debug --profile=restricted --custom=same-uid.json -- sleep 86400
Targeting container "app". If you don't see processes from this container it may be because the container runtime doesn't support this feature.

$ kubectl -n prod exec orders -c debug -- jcmd -l
1 Orders

$ kubectl -n prod exec orders -c debug -- jcmd 1 Thread.print
1:
2026-11-23 16:31:52
Full thread dump OpenJDK 64-Bit Server VM (25.0.5.1+2-LTS mixed mode, sharing)
[...]
"stock-refresh" #18 [21] prio=5 os_prio=0 cpu=1.72ms elapsed=9.17s tid=0x0000e2e2000d1de0 nid=21 waiting on condition  [0x0000e2e2050f1000]
   java.lang.Thread.State: TIMED_WAITING (parking)
	at jdk.internal.misc.Unsafe.park(java.base@25.0.5.1/Native Method)
	at java.util.concurrent.locks.LockSupport.parkNanos(java.base@25.0.5.1/Unknown Source)
	at Orders.lambda$main$0(Orders.java:10)
	- locked <0x00000000facb28f0> (a java.lang.Object)
[...]
"order-42" #19 [22] prio=5 os_prio=0 cpu=0.87ms elapsed=8.95s tid=0x0000e2e2000d3ae0 nid=22 waiting for monitor entry  [0x0000e2e204ef3000]
   java.lang.Thread.State: BLOCKED (on object monitor)
	at Orders.lambda$main$1(Orders.java:15)
	- waiting to lock <0x00000000facb28f0> (a java.lang.Object)
[...]

The debug container runs as user 65532, with no Linux capabilities. The four steps succeed, because jcmd has the same user and the same group as the JVM. jcmd -l finds the JVM in the hsperfdata files of the JVM’s /tmp. The thread dump shows that order-42 is waiting for the lock that stock-refresh holds. No thread dump is written to the application log.

The group is as important as the user. With runAsUser alone, the debug container has group 0, and it cannot read /proc/1/root/tmp. jcmd then fails after 10.5 seconds, and 6 thread dumps are written to the application log.

If the cluster accepts a root container

In a namespace with no security level, a root debug container starts if runAsNonRoot is not declared for the whole pod. The test pod still has its volume on /tmp. We test four cases:

debug containerresult of jcmd 1 Thread.print
root, without --targetstate is not ready to participate in attach handshake!
root, --target=app, legacy profileUnable to open socket file /tmp/.java_pid1: target process 1 doesn't respond within 10500ms or HotSpot VM not loaded
root, --target=app, general profilethe thread dump
root, --target=app, legacy profile, then setpriv to 65532the thread dump

Without --target, the debug container does not see the application’s processes. Its process 1 is its own sleep. jcmd checks that this process handles SIGQUIT, and refuses to attach without sending a signal.

With the legacy profile, root does not have the SYS_PTRACE capability. Without it, the kernel opens /proc/1/root only to the JVM’s user and group:

$ kubectl exec orders -c debug -- ls /proc/1/root/tmp
ls: cannot open file '/proc/1/root/tmp': Permission denied

So jcmd can neither see the socket, nor create the .attach_pid1 file in the JVM’s directories. It still sends SIGQUIT, and the JVM answers with a thread dump on its standard output. After this failure, the application log contains 6 thread dumps:

$ kubectl logs orders -c app | grep -c 'Full thread dump'
6

The general profile adds SYS_PTRACE to the debug container. Root then reads /proc/1/root/tmp, creates the file there, and the JVM accepts its connection. No thread dump is written to the application log.

Without SYS_PTRACE, we can also switch to the JVM’s user with setpriv, which is in the eclipse-temurin:25-jdk image:

$ kubectl exec orders -c debug -- setpriv --reuid=65532 --regid=65532 --clear-groups jcmd 1 Thread.print
1:
2026-11-23 16:32:43
Full thread dump OpenJDK 64-Bit Server VM (25.0.5.1+2-LTS mixed mode, sharing)
[...]

With a user other than root or 65532, jcmd fails as soon as it sends the signal, with java.io.IOException: Operation not permitted.

Without a writable /tmp

The next pod no longer has a volume on /tmp: its whole filesystem is read-only. The debug container has the right user, but jcmd fails:

$ kubectl -n prod exec orders -c debug -- jcmd 1 Thread.print
1:
com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file /tmp/.java_pid1: target process 1 doesn't respond within 10500ms or HotSpot VM not loaded
[...]

The message is the same as with root without SYS_PTRACE, but the cause is different. jcmd cannot create the .attach_pid1 file in the JVM’s directories. It creates it in its own /tmp, where the JVM does not look for it. Here too, 6 thread dumps are written to the application log.

Changing java.io.tmpdir does not fix anything. With -Djava.io.tmpdir=/dumps and a writable working directory, the attach fails with the same message. The JVM always creates its socket in /tmp.

Without a socket, we can still send the signal itself. kill -3 sends SIGQUIT, and the thread dump arrives in the application log:

$ kubectl -n prod exec orders -c debug -- sh -c 'kill -3 1'

$ kubectl -n prod logs orders -c app --tail=60
[...]
"order-42" #19 [22] prio=5 os_prio=0 cpu=0.08ms elapsed=17.43s tid=0x0000f3ff040cbb60 nid=22 waiting for monitor entry  [0x0000f3feedcfd000]
   java.lang.Thread.State: BLOCKED (on object monitor)
	at Orders.lambda$main$1(Orders.java:15)
	- waiting to lock <0x00000000facb2848> (a java.lang.Object)
[...]

kill -3 gives a thread dump, but no heap dump and no JFR. To be able to use jcmd, mount an emptyDir volume on /tmp in the pod, before you need it:

    volumeMounts:
      - name: tmp
        mountPath: /tmp
volumes:
  - name: tmp
    emptyDir: {}

Where a heap dump is written

The JVM writes the file, not jcmd. So the path points to a file in the application container. We go back to the pod with a volume on /tmp:

$ kubectl -n prod exec orders -c debug -- jcmd 1 GC.heap_dump heap.hprof
1:
Dumping heap to heap.hprof ...
Unable to create heap.hprof: Read-only file system

$ kubectl -n prod exec orders -c debug -- jcmd 1 GC.heap_dump /tmp/heap.hprof
1:
Dumping heap to /tmp/heap.hprof ...
Heap dump file created [71050400 bytes in 0.201 secs]

$ kubectl -n prod cp orders:/proc/1/root/tmp/heap.hprof heap.hprof -c debug
tar: Removing leading `/' from member names

A relative path starts from the JVM’s working directory, which is read-only here. The dump succeeds in /tmp, the only writable directory. The file is not in the /tmp of the debug container. We read it through /proc/1/root/tmp, and kubectl cp copies it through this path. The copied file is 71,050,400 bytes, like the dump. A JFR file follows the same rule: JFR.start filename=/tmp/rec.jfr writes it to the JVM’s /tmp.

We then mount a second emptyDir with sizeLimit: 64Mi, and we write the same 71 MB dump to it. The command succeeds. In both tests, Kubernetes evicts the pod about two minutes later:

$ kubectl -n prod get pod orders-dump -o jsonpath={.status.phase}{" | "}{.status.reason}{" | "}{.status.message}{"\n"}
Failed | Evicted | Usage of EmptyDir volume "dumps" exceeds the limit "64Mi". 

After the eviction, kubectl cp answers cannot exec into a container in a completed pod, and the volume is deleted from the node. Give the volume a size larger than the heap, and copy the dump as soon as it is written.

What the debug container costs the pod

The ephemeral container has no request and no limit: its resources field is {}. Its cgroup has no memory limit, but it is inside the pod’s cgroup. In the test pod, the app container has a limit of 512Mi, and the pod’s cgroup has the same limit. So the debug container shares this limit with the application.

jcmd is itself a JVM. After a single jcmd 1 VM.version, the memory peak of the debug container is 67 MB. With -J-Xmx16m -J-XX:+UseSerialGC -J-XX:TieredStopAtLevel=1, it is 53 MB. kill -3 does not raise it.

A heap dump uses memory in the application container. Before the 71 MB dump, this container uses 98 MB. During the dump, its peak is 245 MB. Afterwards, it uses 174 MB, including 71 MB of cache for the written file.

When the pod is almost full, jcmd can get the application killed. We start the JVM with -Xms430m -Xmx430m -XX:+AlwaysPreTouch, so that the whole heap is in memory from startup. The pod then uses 482 MB, for a limit of 512Mi, which is 537 MB:

$ kubectl -n prod exec orders -c debug -- jcmd 1 VM.version
1:
command terminated with exit code 137

The kernel kills the debug container and the application. Their oom_score_adj values are close: 936 for the JVM, 999 for the debug container. Kubernetes marks the app container as OOMKilled and restarts it. The state we wanted to observe is lost.

With a 100 MB application, the result is different. A JVM started with -Xms480m in the debug container is killed alone, and the application keeps running without a restart. Before a jcmd, check that at least 70 MB remain between the pod’s memory and its limit. A heap dump needs more: here, the application container peaks 147 MB above its level before the dump. Otherwise, use kill -3.

The other cases tested

With shareProcessNamespace: true in the pod, all the containers share their processes. --target is no longer needed, and the JVM is no longer process 1. jcmd -l prints 7 Orders, and jcmd 7 VM.version works.

With -XX:+DisableAttachMechanism, jcmd fails with The VM does not support the attach mechanism. With -XX:-UsePerfData, jcmd -l prints nothing, but jcmd 1 VM.version works.

The jcmd of an eclipse-temurin:21-jdk image also attaches to this JDK 25 JVM, for VM.version and Thread.print.

The Kubernetes account needs one more permission than kubectl exec needs. Without this permission, kubectl debug fails with cannot patch resource "pods/ephemeralcontainers". With the patch verb on the pods/ephemeralcontainers resource, the command works.

Reading the error

what we observecause
Forbidden: violates PodSecuritythe kubectl debug profile does not meet the namespace level
container in CreateContainerConfigErrorroot image in a pod or a profile with runAsNonRoot
state is not ready to participate in attach handshake!no --target: jcmd targets a process that is not the JVM
Operation not permitted, immediatelya user that is neither the JVM’s nor root: the signal is refused
failure after 10.5 seconds, 6 thread dumps in the logthe JVM does not see the .attach_pid1 file: wrong group, root without SYS_PTRACE, or read-only filesystem
failure after 10.5 seconds, no thread dump in the logthe JVM sees the file but cannot create the socket: read-only /tmp, writable working directory
The VM does not support the attach mechanismJVM started with -XX:+DisableAttachMechanism
command terminated with exit code 137the pod exceeded its memory limit

Which command for which pod

pod situationwhat works
writable /tmp, with or without a restricted namespace--target=app --profile=restricted --custom=same-uid.json, then jcmd 1
root container and SYS_PTRACE capability accepted, writable /tmp--target=app --profile=general, then jcmd 1
root container accepted without SYS_PTRACE, writable /tmp--target=app, setpriv to the JVM’s user and group, then jcmd 1
read-only /tmpsame debug container, kill -3 1, then kubectl logs
shareProcessNamespace: truesame command without --target, jcmd -l then jcmd with the printed PID
pod close to its memory limitkill -3 1 rather than jcmd

In summary

jcmd attaches in four steps: a socket in the JVM’s /tmp, an .attach_pid file, the SIGQUIT signal, then the connection. The JVM only accepts its own user and its own group, or root.

In a restricted namespace, the legacy and general profiles of kubectl debug are refused. The restricted profile is accepted, but a root image does not start. The command that works adds --custom with the JVM’s user and group. It also works outside a restricted namespace.

As root, you need --target, then the SYS_PTRACE capability of the general profile or setpriv to the JVM’s user and group. Without one of the two, the attach fails after 10.5 seconds, and the JVM writes 6 thread dumps to the application log.

Without a writable /tmp, jcmd cannot attach, and java.io.tmpdir does not change that. kill -3 still works, and the JVM then writes a thread dump to the log.

A heap dump is written by the JVM, in its container. We copy it through /proc/1/root. In the tests, Kubernetes evicts the pod about two minutes after the dump when the volume is too small.

The debug container counts in the pod’s memory limit. In this test, one jcmd adds 67 MB to it. On an almost full pod, it got the application killed.

What next?

For the jcmd commands that are useful once jcmd is attached, see jcmd: the commands to know in production. To read the thread dump you get, see Reading a thread dump with jstack.