Skip to main content
This guide walks through inspecting kubeconfig files, switching contexts, making a non-default kubeconfig the default, and troubleshooting a client-certificate path mismatch that prevents kubectl from authenticating. Key concepts covered:
  • Default kubeconfig location and format
  • Viewing and counting clusters/users/contexts
  • Using an alternate kubeconfig without replacing the default
  • Making another kubeconfig the default
  • Troubleshooting certificate path mismatches and fixing credentials

Where is the default kubeconfig located?

By default, kubectl reads the kubeconfig from $HOME/.kube/config. Confirm your home and the file:
A minimal kubeconfig excerpt looks like:
From this file you can determine:
  • Number of clusters defined (here: 1)
  • Number of users defined (here: 1)
  • Number of contexts defined (here: 1)
  • Current context name (here: kubernetes-admin@kubernetes)
  • Cluster name (here: kubernetes)
  • User referenced by the current context (here: kubernetes-admin)
Note: the user field in a context references a user entry in the users: section of the file. Context names are metadata (often user@cluster by convention).

Inspecting another kubeconfig: /root/my-kube-config

There is a separate kubeconfig at /root/my-kube-config that contains multiple clusters, contexts, and users. Relevant excerpts:
Quick observations:
  • Number of clusters: 4
  • Number of contexts: 4
  • Initial current-context: test-user@development
  • The research context maps test-cluster-1 to user dev-user.
  • The dev-user credential block references developer-user.crt and dev-user.key — this will be important for troubleshooting since the actual files on disk may differ.
  • The aws-user entry references aws-user.crt for the client certificate.

Use the research context without replacing the default kubeconfig

You can test the research context from this alternate kubeconfig without touching your default config by using --kubeconfig or setting KUBECONFIG. Example:
Or temporarily set the environment variable for a shell session:

Make /root/my-kube-config the default kubeconfig

If you’d like /root/my-kube-config to become the default for this user so you no longer need --kubeconfig, back up the existing default and move the file into place:
Verify:
Alternatively, set KUBECONFIG=/root/my-kube-config in your environment instead of moving the file.
Always back up your original kubeconfig before replacing it (for example cp -a ~/.kube/config ~/.kube/config.backup), so you can restore it if needed.

Troubleshooting: access fails when using dev-user

With current-context: research (which uses dev-user) you may see authentication errors when running kubectl:
If you get a client certificate error, inspect both the users: block in the kubeconfig and the certificate/key files on disk. Truncated users section from the kubeconfig:
List the actual certificate files:
Common issue: the kubeconfig references developer-user.crt while the directory contains dev-user.crt. That path mismatch prevents kubectl from locating the client certificate and authenticating.

Fix the client-certificate path for dev-user

Use kubectl config set-credentials to update the user entry in the kubeconfig to point to the correct certificate and key. If /root/my-kube-config has already been moved to ~/.kube/config (the default), run:
If instead you want to modify the original /root/my-kube-config without replacing your default, pass --kubeconfig (place it immediately after kubectl):
After updating, verify the user entry points to the correct files:

Verify access

Retry the previously failing command:
If the certificate paths are correct and the certificate is valid for the cluster, kubectl should now succeed.

Useful commands quick reference

Summary

  • Default kubeconfig: ~/.kube/config.
  • Use --kubeconfig or KUBECONFIG to point kubectl at a non-default kubeconfig file.
  • To make another file the default, back up and replace ~/.kube/config.
  • If authentication fails, check the users: entries and ensure client-certificate and client-key paths in the kubeconfig match the files on disk.
  • Use kubectl config set-credentials to correct credentials entries in the kubeconfig.

Watch Video