A kubeconfig file is a credential, not a settings file. Anyone who copies it can act against your cluster with your permissions. Protecting it comes down to four habits: keep the file readable only by you, prefer short-lived credentials over static ones, never run exec plugins from files you did not write, and keep every kubeconfig out of git, logs and CI artifacts. Kubezilla Desktop is built around the same idea: it stores your kubeconfigs securely on your own machine, with no cloud sync and no telemetry.
TL;DR
- A kubeconfig holds cluster endpoints, users and credentials, so a leaked copy can give an attacker working access to the cluster.
- Restrict
~/.kube/configto its owner (mode0600on Linux and macOS) and know every other place a kubeconfig may live. - Replace static tokens and client certificates with short-lived, identity-provider-backed credentials such as OIDC wherever you can.
- Treat kubeconfigs from other people as code: the
execfield can launch programs, and Kubernetes v1.35 added a way to allowlist them. - Scan repositories, pipelines and logs for kubeconfig content, and keep the files local rather than synced to the cloud.
Table of Contents
- What a kubeconfig holds, and why leaking it hurts
- Know where your kubeconfigs live
- File permissions that actually matter
- Prefer short-lived credentials to static ones
- Treat kubeconfig files from others as code
- Keep kubeconfigs out of git, logs and pipelines
- Handle multiple clusters without mixing them up
- A quick audit you can run today
- Why the tool you manage clusters with matters
- Manage your clusters with local-first credentials
- FAQ
- Sources
What a kubeconfig holds, and why leaking it hurts
The Kubernetes documentation describes a kubeconfig as a file that organizes information about clusters, users, namespaces and authentication mechanisms. In practice that means three things sit side by side in one YAML document:
- Clusters: the API server address and the certificate authority used to verify it.
- Users: the proof of who you are. This may be a client certificate and key, a bearer token, or a command that fetches a token on demand.
- Contexts: named pairings of a cluster, a user and usually a namespace.
The file does not just say where the cluster is. It usually carries what is needed to log in. The Threat Matrix for Kubernetes lists kubeconfig theft as a technique for this reason: attackers who obtain the file can use it to access the clusters it names.
That is why a kubeconfig deserves the same care as an SSH private key or a cloud access key. A few details make the exposure worse than it first looks:
- A single file often lists several clusters, so one leak can reach staging and production together.
- Admin-level files grant the same power to whoever holds them, wherever they are.
- Embedded certificates and static tokens keep working until someone actively invalidates them, which may be long after you have forgotten the leak.
Know where your kubeconfigs live
You cannot protect files you cannot find. By default, kubectl reads $HOME/.kube/config. The same Kubernetes page says you can override that with the KUBECONFIG environment variable, which can hold a list of paths, or with the --kubeconfig flag on a single command.
So a real inventory of kubeconfigs on your machine includes:
~/.kube/config, the default.- Every path named in
KUBECONFIG, which shell profiles often set. - Files downloaded from a cloud console or handed over by a colleague, often sitting in Downloads or on the desktop.
- Copies in project folders, backups, dotfile repositories and shared drives.
That fourth group causes most accidents. A file you exported once for a quick test becomes a long-lived copy with no owner. Spend ten minutes searching your home directory for files named config, kubeconfig or *.kubeconfig, delete what you do not use, and move the rest under one protected folder. Our guide to the kubeconfig location on Windows, macOS and Linux lists the usual places by platform, and the KUBECONFIG environment variable guide covers merging and debugging multiple files.
File permissions that actually matter
The rule is simple: only the account that uses the file should be able to read it, and only that account should be able to change it. On Linux and macOS that is mode 0600, meaning read and write for the owner and nothing for the group or others. A Helm issue thread about the warning Helm prints for loose kubeconfig permissions recommends that ~/.kube/config be readable only by its owner, and shows 0600.
To check and fix it:
ls -l ~/.kube/config
chmod 600 ~/.kube/config
chmod 700 ~/.kube
The first command shows the current mode. -rw------- is what you want. The directory command stops other users from listing the folder where the file lives. Run the same check on every other kubeconfig you found in the previous step.
Two more points on Unix-like systems:
- Ownership matters as much as mode. A
0600file owned by another user, or by root after a straysudo, causes confusing failures and tempts people to loosen the mode. Fix the owner withchowninstead. - Shared machines and jump hosts need extra care. If several engineers log in to one host, each needs a personal kubeconfig in their own home directory, never one shared admin file.
On Windows, files inherit permissions from the parent folder. Your user profile folder is normally limited to your account, so the default location is reasonable. Files you save to a shared folder, a synced folder or a public drive are not. Open the file's Security tab, or use icacls, to confirm that only your account and the system accounts have access, and remove broad groups such as Everyone or Users if they appear.
Permissions protect against other local accounts and careless sharing. They do not stop malware running as you. That is why the next sections matter: reduce what a stolen file is worth.
Prefer short-lived credentials to static ones
What is inside the file determines the damage if it escapes. Long-lived credentials are the problem:
- Static service-account tokens and client certificates work until someone deletes the account or the certificate expires, and neither is tied to a person's current employment or device.
- Short-lived tokens issued after a login with your identity provider expire on their own, usually within hours, so a stolen file goes stale quickly.
The standard way to get the second kind is OpenID Connect (OIDC), where kubectl obtains a token through a login flow with your company's identity provider and caches it briefly. The kubeconfig then holds no permanent secret. Our kubeconfig OIDC authentication guide walks through the setup, the kubectl login step and the common fixes.
Why revocation is harder than it sounds
If a client certificate leaks, you may not be able to cancel it. Kubernetes does not support revoking client certificates, according to a knowledge base entry on OpenShift 4 admin kubeconfigs. For certificates from a self-managed certificate authority, the way to invalidate them is to rotate the CA itself, which is disruptive for every other user of that CA. Tokens tied to a service account stop working only when the account is removed.

The practical conclusion is to avoid needing revocation. Give people identity-based access that expires, keep admin certificates for break-glass use only, and store those in a vault instead of on laptops.
Give each file the least privilege it needs
- Create separate users or service accounts for separate jobs rather than one all-powerful identity.
- Bind them to roles limited to the namespaces and verbs the job needs.
- Use read-only contexts for day-to-day inspection and keep write access for the moments you need it.
Our Kubernetes API access security guide goes deeper on role design and hardening the API endpoint itself.
Treat kubeconfig files from others as code
A kubeconfig can do more than describe a connection. A user entry may include an exec section that tells kubectl to run a command to fetch credentials. That is how cloud and OIDC login helpers work, and it is legitimate. It also means that a kubeconfig you were sent, or downloaded from a site, can ask your machine to run whatever program it names when you use it.
The habit that follows: open any kubeconfig you did not write in a text editor before using it, read the exec command and its arguments, and be suspicious of anything you do not recognize. Do not load files from untrusted sources at all.
Kubernetes now has a built-in control for this. As of Kubernetes v1.35, administrators can restrict which executables a kubeconfig may run, using the credentialPluginPolicy and credentialPluginAllowlist fields in the kuberc configuration file. If your team standardizes on a small number of login helpers, allowlisting just those turns a whole class of malicious-file attacks into a refused command. Check your kubectl version first, since the setting only helps clients new enough to honor it.

Keep kubeconfigs out of git, logs and pipelines
Most real-world leaks are boring: a file committed by accident, a debug step that prints it, a backup that includes it. A few controls cover most of the ground.
Source control
- Add
kubeconfig,*.kubeconfigand.kube/to the global gitignore on every developer machine, not just the project one. - Scan existing repositories and their history with a secret scanner. Deleting the file in a later commit does not remove it from history.
- If a kubeconfig was ever committed, treat its credentials as compromised and replace them. Removing the commit is not enough.
CI/CD and logs
- Inject credentials at run time from the pipeline's secret store and write them to a temporary file that is deleted at the end of the job.
- Avoid commands that dump the environment, such as
envorprintenv, in jobs that hold credentials. - Never
cata kubeconfig for debugging, and check that job artifacts and cached folders do not contain one. - Prefer pipeline identities that obtain short-lived tokens, so a leaked log line expires quickly.
Backups and sync
Home-folder backups and dotfile sync tools will happily copy ~/.kube. Exclude it, or make sure the backup target is encrypted and access-controlled. The same goes for cloud-synced folders: a kubeconfig placed in one is now stored on someone else's servers and on every device that syncs it.
Handle multiple clusters without mixing them up
Security is not only about attackers. A very common incident is running a destructive command against the wrong cluster. A valid kubeconfig can still point at production when you think you are in staging.
Some habits reduce that risk:
- Show the current context and namespace in your shell prompt.
- Check
kubectl config current-contextbefore any delete, apply or scale. - Use separate kubeconfig files per environment rather than one merged file with every cluster, and select them with
KUBECONFIGdeliberately. - Name contexts clearly, with the environment in the name.
The kubeconfig context management guide covers switching and merging without losing track of where you are. For clusters that sit behind a bastion host, an SSH tunnel keeps the API server off the public internet; our Kubernetes SSH tunnel guide shows the commands and the certificate errors that usually follow.
A quick audit you can run today
| Check | What good looks like | Fix if not |
|---|---|---|
| File mode | -rw------- on every kubeconfig | chmod 600 <file> |
| Directory mode | drwx------ on ~/.kube | chmod 700 ~/.kube |
| Stray copies | None in Downloads, shared or synced folders | Delete or move to one protected folder |
| Credential type | OIDC or other expiring tokens for people | Move users off static tokens and certs |
| Admin certificates | Held in a vault, not on laptops | Remove and issue scoped access |
| Exec entries | Only login helpers you recognize | Remove, or allowlist with kuberc on v1.35+ |
| Git history | No kubeconfig content found by a scan | Rotate credentials, then clean history |
| CI logs | No kubeconfig or environment dumps | Remove debug steps, shorten token life |
Work through the table per machine and per pipeline. Most teams find at least two or three failures on the first pass.
Why the tool you manage clusters with matters
Everything above assumes kubeconfigs live as loose YAML files that you hand-edit, merge with environment variables and copy between machines. Each of those steps is a chance for a mode to be wrong, a copy to be forgotten or a context to be mixed up. The management tool you use can remove many of those steps.
That is the reasoning behind how we built Kubezilla Desktop. It is a native app for Linux, macOS and Windows that keeps your kubeconfigs in secure local storage and has no cloud sync and no telemetry, so your cluster credentials stay on your own machine. It also supports multi-cluster access and SSH tunnels, which means you can reach clusters behind a bastion without exporting extra files or opening the API server to the internet.
Our view: the safest kubeconfig is the one you handle least
Every security guide ends up at the same list: tight permissions, short-lived credentials, no stray copies. We agree with all of it, and we also think the list is too long to rely on discipline alone. Each manual step is a place where a tired engineer at 2 a.m. can slip.
That is why Kubezilla Desktop is local-first. Kubeconfigs are stored securely on your machine. We do not sync them to a cloud and we do not collect telemetry, because we do not think a tool that manages your clusters should ever need to see your credentials. It is written in Rust, so it stays light on memory and starts fast, which matters when you open it many times a day.
We also think switching clusters should be a visible, deliberate act, with all your clusters in one interface rather than a dozen terminal tabs and exported variables. Fewer files, fewer copies and fewer environment tricks mean fewer ways to leak or to run the wrong command in the wrong place.
Margus
Manage your clusters with local-first credentials
If you are tightening how your team handles cluster access, Kubezilla Desktop gives you one native interface for your clusters, with kubeconfigs stored securely on your machine. You get live logs, metrics, pod terminals, YAML editing, workload scaling, Helm and ArgoCD operations and PVC file browsing without juggling CLI tools and browser tabs. It costs €5 per month.

To see how a first connection works, follow the tutorial on connecting to a cluster.
FAQ
What permissions should a kubeconfig file have?
On Linux and macOS, make it readable and writable only by its owner, which is mode 0600, and set ~/.kube to 0700. A Helm discussion on kubeconfig permission warnings shows the same recommendation. On Windows, keep the file in your user profile and make sure only your account can access it.
Where does kubectl look for a kubeconfig?
By default it reads $HOME/.kube/config. The KUBECONFIG environment variable can point it at one or more other files, and the --kubeconfig flag overrides it for a single command, as the Kubernetes documentation explains. Check both places when you inventory your files.
What should I do if a kubeconfig leaks?
Assume the credentials inside it are compromised and replace them at once. Delete service accounts whose tokens were exposed and issue new ones. If the file held client certificates, plan for CA rotation, because Kubernetes does not support revoking individual client certificates. Then review your API server audit logs for activity from unfamiliar addresses.
Is it safe to use a kubeconfig someone sends me?
Only after reading it. The exec field can make kubectl run a program on your machine, so inspect it in a text editor first and refuse anything you do not recognize. From Kubernetes v1.35, a kuberc allowlist can limit which executables a kubeconfig may launch.
Sources
- Organizing Cluster Access Using kubeconfig Files (Kubernetes documentation)
- Kubeconfig file (Threat Matrix for Kubernetes)
- WARNING: Kubernetes configuration file is group-readable (Helm issue 9115)
- Admin kubeconfig management (VSHN Knowledge Base)
- Kubernetes v1.35: Restricting executables invoked by kubeconfigs via kuberc
