Kubernetes kubeconfig files - contexts, the KUBECONFIG variable and merge rules
Answer: By default kubectl uses $HOME/.kube/config; specify other kubeconfig files with the KUBECONFIG environment variable or the --kubeconfig flag. Define contexts that each group a cluster, namespace and user, and switch with kubectl config use-context <name>.
Commands
kubectl config view
kubectl config get-contexts
kubectl config use-context staging
kubectl config set-context --current --namespace=team-a
KUBECONFIG=$HOME/.kube/config:$HOME/.kube/staging kubectl config view --flatten
kubectl --kubeconfig ./ci-config get pods
Example file
apiVersion: v1
kind: Config
clusters:
- name: development
cluster:
server: https://k8s.example.org/k8s/clusters/c-xxyyzz
proxy-url: http://proxy.example.org:3128
users:
- name: developer
contexts:
- name: development
context:
cluster: development
user: developer
namespace: default
current-context: development
Merge rules
- If
--kubeconfigis set, kubectl uses only that file; there is no merge. - Otherwise, if
KUBECONFIGis set, its files are merged. The list is colon-delimited on Linux and Mac and semicolon-delimited on Windows. - When merging, the first file to set a particular value or map key wins, including
current-contextand whole user entries. - Empty filenames are ignored, and files that cannot be deserialized produce errors.
- The
--context,--user,--clusterand--serverflags override what the merged file says. - Only one authentication technique per user is allowed; two conflicting techniques fail.
Paths and safety
- File references inside a kubeconfig are relative to the location of the kubeconfig file; on the command line they are relative to the current working directory.
- Only use kubeconfig files from trusted sources. A specially-crafted file could lead to malicious code execution or file exposure, so inspect untrusted ones like a shell script.
See also
Claims
- id: C1 claim: kubectl defaults to $HOME/.kube/config. source: S2 evidence: "
kubectllooks for a file namedconfigin the$HOME/.kubedirectory."
- id: C2 claim: The first file to set a value wins during merges. source: S2 evidence: "The first file to set a particular value or map key wins."
- id: C3 claim: Only use kubeconfig files from trusted sources. source: S1 evidence: "Only use kubeconfig files from trusted sources."
Sources
- [S1] Organizing Cluster Access Using kubeconfig Files. Kubernetes docs. https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/ . License: CC-BY-4.0. Accessed 2026-11-18.
- [S2] organize-cluster-access-kubeconfig.md (source page). kubernetes/website (GitHub). https://github.com/kubernetes/website/blob/main/content/en/docs/concepts/configuration/organize-cluster-access-kubeconfig.md . License: CC-BY-4.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Kubernetes docs, Organizing Cluster Access Using kubeconfig Files; kubernetes/website (GitHub), organize-cluster-access-kubeconfig.md (source page). CC-BY-4.0 (https://raw.githubusercontent.com/kubernetes/website/main/LICENSE). Copyright The Kubernetes Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Kubernetes docs, Organizing Cluster Access Using kubeconfig Files; kubernetes/website (GitHub), organize-cluster-access-kubeconfig.md (source page). CC-BY-4.0 (https://raw.githubusercontent.com/kubernetes/website/main/LICENSE). Copyright The Kubernetes Authors.
Cite this
iinventedlight. 2026. "Kubernetes kubeconfig files - contexts, the KUBECONFIG variable and merge rules." iinventedlight Library, iil:note:0eacjwqe, version of 2026-11-18. https://iinventedlight.com/library/notes/kubernetes-kubeconfig/. License: CC-BY-4.0.
@misc{iil_note_0eacjwqe,
author = {{iinventedlight}},
title = {{Kubernetes kubeconfig files - contexts, the KUBECONFIG variable and merge rules}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/kubernetes-kubeconfig/},
note = {Permanent ID iil:note:0eacjwqe. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/kubernetes-kubeconfig.md.