---
source: https://iinventedlight.com/library/notes/kubernetes-kubeconfig.md
title: "Kubernetes kubeconfig files - contexts, the KUBECONFIG variable and merge rules"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC-BY-4.0 (Kubernetes docs); CC-BY-4.0 (kubernetes/website (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-devops: kubernetes kubeconfig"
tags: [kubernetes, kubectl, kubeconfig, contexts, clusters]
client: Librarian
sources:
  - id: S1
    title: "Organizing Cluster Access Using kubeconfig Files"
    publisher: Kubernetes docs
    url: https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/
    accessed: 2026-11-18
    license: "CC-BY-4.0"
  - id: S2
    title: "organize-cluster-access-kubeconfig.md (source page)"
    publisher: kubernetes/website (GitHub)
    url: https://github.com/kubernetes/website/blob/main/content/en/docs/concepts/configuration/organize-cluster-access-kubeconfig.md
    accessed: 2026-11-18
    license: "CC-BY-4.0"
---
# 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

```shell
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

```yaml
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 `--kubeconfig` is set, kubectl uses only that file; there is no merge.
- Otherwise, if `KUBECONFIG` is 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-context` and whole user entries.
- Empty filenames are ignored, and files that cannot be deserialized produce errors.
- The `--context`, `--user`, `--cluster` and `--server` flags 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

- [kubernetes-kubectl-basics](https://iinventedlight.com/library/notes/kubernetes-kubectl-basics/)
- [kubernetes-namespaces](https://iinventedlight.com/library/notes/kubernetes-namespaces/)
- [kubernetes-rbac](https://iinventedlight.com/library/notes/kubernetes-rbac/)

## Claims

- id: C1
  claim: kubectl defaults to $HOME/.kube/config.
  source: S2
  evidence: "`kubectl` looks for a file named `config` in the `$HOME/.kube` directory."

- 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
