A kubeconfig file tells kubectl which cluster endpoint, user credentials, namespace, and context name to use for Kubernetes API requests. Keeping a received kubeconfig in its own file fits temporary or team-specific access from a cluster administrator or managed service without overwriting $HOME/.kube/config.
kubectl reads $HOME/.kube/config by default, but the KUBECONFIG environment variable or the --kubeconfig flag can point it at another file. A session-scoped KUBECONFIG value keeps the change limited to the active shell until the file and context have been checked.
A kubeconfig can contain credential material and can also reference external authentication commands. Use files only from a trusted cluster owner, restrict local permissions, select the intended context, and run a harmless read command before using the context for changes.
Related: How to install kubectl on Ubuntu or Debian
Related: How to switch Kubernetes context
Related: How to check Kubernetes cluster access
Steps to configure kubectl with a kubeconfig file:
- Create the kubectl config directory if it is missing.
$ mkdir -p ~/.kube
- Copy the received kubeconfig into a named file.
$ cp ~/Downloads/team-a.kubeconfig ~/.kube/team-a.kubeconfig
Use a descriptive filename for each cluster or team so shell history and error messages show which access file is active.
- Restrict the kubeconfig file permissions.
$ chmod 600 ~/.kube/team-a.kubeconfig
A kubeconfig can include bearer tokens, client certificates, or exec-plugin settings. Do not use a file from an untrusted source.
- Point the current shell at the saved kubeconfig file.
$ export KUBECONFIG=$HOME/.kube/team-a.kubeconfig
The export applies only to the current shell and child processes. Use --kubeconfig for one command when a shell-wide override is not needed.
- List the contexts in the selected kubeconfig.
$ kubectl config get-contexts -o name team-a-admin@team-a
- Select the intended context.
$ kubectl config use-context team-a-admin@team-a Switched to context "team-a-admin@team-a".
Use the exact context name listed by kubectl config get-contexts. This writes the current-context field in the selected kubeconfig file, not in $HOME/.kube/config.
- Confirm the active context.
$ kubectl config current-context team-a-admin@team-a
- Verify read-only access through the configured file.
$ kubectl get namespace default NAME STATUS AGE default Active 71s
If the cluster grants only namespace-scoped access, replace default with the namespace named by the cluster owner.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.