LinuxCommandLibrary

kubectl-config

Configure access to Kubernetes clusters

TLDR

Get all contexts in the default kubeconfig file

$ kubectl config get-contexts
copy

Get all clusters/contexts/users in a custom kubeconfig file
$ kubectl config [get-clusters|get-contexts|get-users] --kubeconfig [path/to/kubeconfig.yaml]
copy

Get the current context
$ kubectl config current-context
copy

Set the default namespace of the current context
$ kubectl config set-context --current --namespace [namespace]
copy

Switch to another context
$ kubectl config [[use|use-context]] [context_name]
copy

Delete clusters/contexts/users
$ kubectl config [delete-cluster|delete-context|delete-user] [cluster|context|user]
copy

Permanently add custom kubeconfig files
$ export KUBECONFIG="[path/to/kubeconfig1.yaml]:[path/to/kubeconfig2.yaml]"
copy

SYNOPSIS

kubectl config <SUBCOMMAND> [flags]

PARAMETERS

current-context
    Displays the current context

delete-cluster NAME
    Delete named cluster from kubeconfig

delete-context NAME
    Delete named context from kubeconfig

delete-user NAME
    Delete named user (auth-info alias) from kubeconfig

get-clusters
    Display list of defined clusters

get-contexts
    Describe one or all contexts

rename-context OLD NEW
    Renames a context

set
    Sets an individual value in kubeconfig (alias for set-cluster etc.)

set-cluster NAME [--server=url] [--certificate-authority=path]
    Sets a cluster entry in kubeconfig

set-context NAME [--cluster=NAME] [--user=NAME] [--namespace=NS]
    Sets a context entry in kubeconfig

set-credentials NAME [--client-certificate=path]
    Sets a user entry in kubeconfig

unset PROPERTY
    Unsets a property in kubeconfig (e.g., current-context)

use-context NAME
    Sets the current context

view [--minify] [--flatten]
    Display merged kubeconfig settings or specified file

--kubeconfig string
    Use a particular kubeconfig file

--help -h
    Help for config

DESCRIPTION

kubectl config manages the kubeconfig file used by kubectl to store cluster, user, and context configurations for accessing Kubernetes clusters.

A kubeconfig file defines clusters (with server URLs and certificates), users (credentials like tokens or certs), and contexts (combinations of cluster, user, and namespace). This command enables viewing, editing, and switching these settings without manually editing YAML files.

Key operations include listing contexts, switching the active context, adding new clusters or credentials, and unsetting values. It supports multiple kubeconfig files via the KUBECONFIG environment variable, merging them hierarchically (last file wins conflicts).

Default file is $HOME/.kube/config. Ideal for multi-cluster setups, CI/CD pipelines, and troubleshooting access issues. Always verify changes with kubectl config view to avoid disrupting access.

CAVEATS

Modifies kubeconfig in place; backup before changes. Multiple files merged via KUBECONFIG (last overrides). Some subcommands fail if values missing.

DEFAULT KUBECONFIG

$HOME/.kube/config; created on first kubectl use.

KUBECONFIG ENV

Colon-separated list of files to merge for requests.

HISTORY

Introduced in early Kubernetes releases (~v1.2, 2015) as part of kubectl; evolved with multi-cluster support in v1.14+ and OpenID Connect integration.

SEE ALSO

kubectl(1)

Copied to clipboard