The KUBECONFIG environment variable tells kubectl which kubeconfig files to read. Set it to one path, such as export KUBECONFIG=$HOME/.kube/my-cluster.yaml, and kubectl uses that file. Set it to several paths, and kubectl merges them. Leave it unset, and kubectl falls back to $HOME/.kube/config. This guide covers the exact syntax for Linux, macOS and Windows, the merge rules that surprise people, how it interacts with the --kubeconfig flag, and how to confirm what kubectl is actually using.
TL;DR
- If KUBECONFIG is unset, kubectl reads
$HOME/.kube/config. If it is set, kubectl reads only the files you list, merging them in order. - Separate paths with a colon on Linux and macOS and a semicolon on Windows. When files conflict, the first file wins for map keys.
- The
--kubeconfigflag beats the variable: only that one file is loaded and nothing is merged. - Verify with
kubectl config current-contextandkubectl config viewbefore you run anything destructive. - Treat every kubeconfig like a credential, and never use one from a source you do not trust.
Table of Contents
- What the KUBECONFIG variable does
- How to set the kubeconfig path
- Using several kubeconfig files at once
- KUBECONFIG versus the --kubeconfig flag
- Check which config kubectl is using
- Troubleshooting common problems
- Getting a kubeconfig in the first place
- Protect the file the variable points to
- Our perspective
- Manage every cluster without juggling paths
- FAQ
- Sources
What the KUBECONFIG variable does
A kubeconfig is a YAML file that defines which cluster kubectl targets and which user credentials it authenticates with. It ties together clusters, users and contexts, and a current-context field says which combination is active. A context is simply a named pairing of a cluster, a user and, optionally, a namespace.
KUBECONFIG is the switch that decides which of those files kubectl loads. According to the Kubernetes documentation, the variable holds a list of files that kubectl merges into one effective configuration, and when it is not set kubectl looks for a file named config in $HOME/.kube, as described in the kubeconfig organization guide.
That default is why most people never touch the variable at first. It becomes useful the moment you have a second cluster, a file downloaded from a managed Kubernetes provider, or a CI job that needs its own credentials without touching your personal config.
If you are not sure where the default file lives on your operating system, we cover that in our kubeconfig location guide for Windows, macOS and Linux.
How to set the kubeconfig path
The syntax depends on your shell. The goal is the same each time: put an absolute path in the variable. Absolute paths avoid the confusion that appears when you change directories, because a relative path is resolved against wherever you happen to be standing.
Linux and macOS (Bash, Zsh and other POSIX shells)
For the current terminal session:
export KUBECONFIG=/Users/myuser/.kube/my-test-cluster.yml
That is the form OVHcloud documents for macOS and Linux. The change lasts until you close the terminal.
To make it permanent, add the export line to your shell startup file. E2E Cloud's setup guide shows adding export KUBECONFIG="/path/to/your/kube-config-file" to ~/.bashrc and then running source ~/.bashrc. On macOS with Zsh, the equivalent file is usually ~/.zshrc.
Quote the path if it contains spaces. Run echo $KUBECONFIG afterwards to confirm the value is what you typed.
Windows Command Prompt
For the current console only:
set KUBECONFIG=my-test-cluster.yml
OVHcloud's documentation lists this as the per-process option, and also describes adding the variable through System Properties, Advanced, Environment Variables for a persistent setting. Use a full path such as C:\Users\you\.kube\my-test-cluster.yml rather than a bare filename, so the variable keeps working from any directory.
Windows PowerShell
To store the variable for your user account permanently, E2E Cloud shows this .NET call:
[System.Environment]::SetEnvironmentVariable("KUBECONFIG", "C:\Users\UserName\Downloads\kubeconfig.yaml", [System.EnvironmentVariableTarget]::User)
Swap User for Machine to make it system-wide, which needs an elevated shell. A value stored this way is read by new terminals, so open a fresh window before testing.
| Shell | Command form | Scope |
|---|---|---|
| Bash, Zsh | export KUBECONFIG=/path/to/file | Current session |
Bash with ~/.bashrc | Same line added to the file, then source ~/.bashrc | Every new session |
| Windows cmd | set KUBECONFIG=C:\path\to\file | Current console |
| PowerShell | [System.Environment]::SetEnvironmentVariable(...) with the User target | Your account, new terminals |
| Windows settings | System Properties, Environment Variables | Persistent |
Using several kubeconfig files at once
KUBECONFIG accepts a list. The Kubernetes documentation states that the separator is a colon on Linux and macOS and a semicolon on Windows, per the official kubeconfig guide.

export KUBECONFIG=$HOME/.kube/config:$HOME/.kube/staging.yaml:$HOME/.kube/prod.yaml
On Windows the same idea reads C:\Users\you\.kube\config;C:\Users\you\.kube\staging.yaml.
This solves a real annoyance. Keeping one file per cluster means, as Spacelift points out, you have to remember each file's path and specify it every time you run kubectl. A merged list lets you keep the files separate on disk while kubectl sees one combined configuration, so you switch between them with kubectl config use-context.
Keep the default file in the list
Setting the variable does not add to the default location. As the GoLinuxCloud walkthrough explains, kubectl is limited to the paths you list, so if you still want the contexts from $HOME/.kube/config, include it in the colon-separated list. This is the most common reason people say their old clusters "disappeared" after exporting the variable: the file is fine, it is just no longer being read.
How merging resolves conflicts
The rules from the Kubernetes documentation are worth knowing before you merge files from different teams:
- Empty filenames in the list are ignored.
- Files kubectl cannot deserialize produce errors.
- When two files set the same value or map key, the first file's value is kept.
The subtlety is that not every setting merges the same way. In the official Kubernetes blog, Stephen Kitt notes that map-based settings use first-definition-wins while other settings use last-definition-wins, which is why the merge logic can feel inconsistent. The practical lesson: give your clusters, users and contexts unique names across files. If two files both define a context called dev, only one of them is going to win, and it will not be obvious which.
Where changes get written
When you edit a merged configuration, for example with kubectl config set-context, kubectl does not write to a single combined file. The kubectl reference says a modified value is written to the file that defines that stanza. A newly created value goes into the first file in the chain that exists, and if none of the listed files exist, kubectl creates the last file in the list. Knowing this saves you from hunting for a context you just created in a file you did not expect.
For day-to-day switching, naming and cleanup of merged setups, see our guide to kubeconfig context management.
KUBECONFIG versus the --kubeconfig flag
There are three places kubectl can find its configuration, and the order matters. The kubectl config reference lays it out:
- If the
--kubeconfigflag is set, only that file is loaded and no merging takes place. - Otherwise, if the KUBECONFIG variable is set, it is used as a list of paths that get merged.
- Otherwise,
${HOME}/.kube/configis used, with no merging.
This follows the general kubectl rule that command-line flags override defaults and matching environment variables. The same applies to context: the --context flag selects a context by name, and the --kubeconfig flag points at a file, as documented in the kubectl cluster-info reference.
Use the variable when you want a persistent working set for a terminal session. Use the flag for a one-off command, or in scripts where you want the file to be explicit and impossible to override by whatever happens to be exported in the environment.
kubectl --kubeconfig=$HOME/.kube/staging.yaml get pods
Check which config kubectl is using
When something behaves oddly, resist guessing. Three commands answer most questions.
echo $KUBECONFIG(orecho %KUBECONFIG%in cmd) shows whether the variable is set and to what.kubectl config viewshows the effective configuration, meaning the merged result, and every context in it. Google Cloud's documentation lists kubectl config view and kubectl config current-context as the way to see your config and the context in use.kubectl config get-contextslists every context and, according to Spacelift's guide to kubectl contexts, marks the active one with an asterisk.
Make a habit of running kubectl config current-context before any command that changes or deletes things. A shell where KUBECONFIG was exported hours ago, plus a context you forgot about, is exactly how a command meant for staging lands on production.
Troubleshooting common problems
The variable seems to be ignored. Check whether a --kubeconfig flag appears in a shell alias, script or wrapper. The flag always wins. Also confirm you exported the variable in the same terminal where you run kubectl. A variable set in one window is invisible to another.
Old clusters vanished. You set KUBECONFIG to a new file and left out the default one. Add $HOME/.kube/config to the list.
Connection errors after the path is correct. A valid file does not guarantee a reachable cluster. Troubleshooting guides commonly suggest checking whether a VPN is required to reach the API server, and whether the file you point at is the one the cluster operator gave you.
Windows paths fail in a list. Use semicolons, not colons, because the drive letter colon in C:\ would otherwise be read as a separator.
Variables inside the YAML. A kubeconfig is a data file, not a shell script, so you should not expect $NODE_IP placeholders inside it to be expanded. Generate or edit the file with kubectl config set-cluster and friends, or render it with a template step in your pipeline before kubectl reads it.
Getting a kubeconfig in the first place
On a cluster you built yourself, the GoLinuxCloud guide describes copying /etc/kubernetes/admin.conf from a control-plane node to $HOME/.kube/config, or pointing KUBECONFIG or --kubeconfig at a copy. You can also build entries with the kubectl config subcommands, which add or modify cluster, user and context definitions. Managed providers usually give you a download or a CLI command that writes the file for you.
One caution applies here. The admin.conf from kubeadm grants cluster-admin privileges, so keep it for controlled administration and give day-to-day users narrower credentials.
Protect the file the variable points to
A kubeconfig is not just configuration. Anyone who holds it can connect to your cluster, so avoid sharing it or committing it to a repository, and if one leaks, invalidate the credentials inside it immediately.
The risk runs in the other direction too. The Kubernetes documentation warns to use only kubeconfig files from trusted sources, because a specially crafted file can lead to malicious code execution or file exposure. Inspect a file you receive the way you would a shell script. GoLinuxCloud adds that a hostile file can aim kubectl at a malicious API server or run helper commands during exec-based authentication.

In practice: keep kubeconfigs out of shared folders, set restrictive file permissions, and read any file from a colleague or vendor before you add it to KUBECONFIG. For the wider picture, see our practical guide to Kubernetes API access security.
Our perspective
The KUBECONFIG variable is a clever piece of plumbing, but it asks you to be your own file manager. You track paths, remember which terminal exported what, and hope the merge order does what you meant.
We built Kubezilla Desktop around a different idea: the engineer should see every cluster in one interface, not rebuild a colon-separated list each morning. Kubeconfigs are stored securely on your own machine, with no cloud sync and no telemetry, because a file that grants cluster access should not travel anywhere you did not send it.
The app is a native, lightweight Rust application designed for low memory use and fast startup. It connects to multiple clusters, supports SSH tunnels so you can reach remote clusters without juggling VPNs and bastion hosts, and puts logs, metrics, pod terminals, YAML editing, Helm and ArgoCD in the same window. The environment variable remains a good tool for scripts. For interactive work across many clusters, we think a purpose-built client is simply less error-prone.
Margus
Manage every cluster without juggling paths
If you spend your day switching between clusters, Kubezilla Desktop gives you one native app for all of them: multi-cluster access, SSH tunneling, live log streaming, metrics, pod terminals and YAML editing, available on Linux, macOS and Windows. It costs 5 EUR per month.

You can see what it does and get started on the Kubezilla Desktop site, or read more about it as a Kubernetes desktop app.
FAQ
What is the default kubeconfig location if KUBECONFIG is not set?
kubectl looks for a file named config in the .kube directory under your home directory, so $HOME/.kube/config. If the variable is set, that default is skipped unless you include it in the list. The Kubernetes documentation describes this behavior.
Does the --kubeconfig flag override KUBECONFIG?
Yes. When you pass --kubeconfig, kubectl loads only that file and does no merging. The variable is consulted only when the flag is absent, and the home directory default is used when neither is set.
How do I use more than one kubeconfig file?
List the paths in KUBECONFIG, separated by colons on Linux and macOS or semicolons on Windows. kubectl merges them, and the first file wins when values conflict. Include $HOME/.kube/config in the list if you still want its contexts.
How do I know which cluster kubectl will talk to?
Run kubectl config current-context for the active context, and kubectl config get-contexts to list all of them with the current one marked. Run kubectl config view to see the merged configuration. Check before any destructive command.
Sources
- Organizing Cluster Access Using kubeconfig Files, Kubernetes documentation
- kubectl config, Kubernetes reference
- Command line tool (kubectl), Kubernetes reference
- kubectl cluster-info, Kubernetes reference
- Uniform API server access using clientcmd, Kubernetes blog
- Configuring kubectl on an OVHcloud Managed Kubernetes cluster
- Kubeconfig file as env variable, E2E Cloud
- Configure cluster access for kubectl, Google Cloud
- What is a Kubeconfig File, Spacelift
- Kubectl Get Context, Spacelift
- Install kubectl and Set Up kubeconfig, GoLinuxCloud
