kubectl config current-context

Display the name of the current context from the kubeconfig. A quick check to confirm which cluster you are targeting.

kubectl config current-context [flags]

Common Flags

FlagShortDescription
--kubeconfigPath to the kubeconfig file to read from

Examples

Show the current context name

kubectl config current-context

Use in a shell prompt

export PS1="\$(kubectl config current-context) \w $ "

Conditional logic based on context

if [ "$(kubectl config current-context)" = "production" ]; then echo 'PRODUCTION'; fi

Check context from a specific kubeconfig

kubectl config current-context --kubeconfig=~/.kube/prod-config

Use in a script guard

[[ $(kubectl config current-context) == *prod* ]] && echo 'WARNING: Production context!'

When to Use kubectl config current-context

kubectl config current-context prints the name of the currently active context. It is a simple but essential command for confirming which cluster, user, and namespace your kubectl commands will target.

Basic Usage

kubectl config current-context
# Output: production

That single word tells you which cluster kubectl will communicate with. Before running any command — especially destructive ones — verify you are in the right context.

Safety Pattern: Always Verify

Make it a habit to check your context before important operations:

# Before any destructive operation
kubectl config current-context
# production

# STOP and think: is this the right cluster?
kubectl delete deployment my-app -n staging

Shell Prompt Integration

The most effective safety measure is displaying the current context in your shell prompt:

Bash

# Add to ~/.bashrc
kube_context() {
  kubectl config current-context 2>/dev/null
}
export PS1='[\$(kube_context)] \w $ '
# Result: [production] ~/projects $

Zsh with Oh My Zsh

# The kube-ps1 plugin does this automatically
plugins=(... kube-ps1)
PROMPT='$(kube_ps1) '$PROMPT
# Result: (production|default) ~/projects $

Starship Prompt

# In ~/.config/starship.toml
[kubernetes]
disabled = false
format = '[$context(\($namespace\))]($style) '

Scripting Guards

Use current-context as a safety gate in deployment scripts:

#!/bin/bash
CURRENT=$(kubectl config current-context)
TARGET_CONTEXT="staging"

if [ "$CURRENT" != "$TARGET_CONTEXT" ]; then
  echo "ERROR: Expected context '$TARGET_CONTEXT' but got '$CURRENT'"
  echo "Run: kubectl config use-context $TARGET_CONTEXT"
  exit 1
fi

echo "Context verified: $CURRENT"
kubectl apply -f manifests/

Production Safety Script

A wrapper function that blocks destructive commands in production:

# Add to ~/.bashrc or ~/.zshrc
kubectl() {
  local ctx
  ctx=$(command kubectl config current-context 2>/dev/null)

  # Warn on production context for destructive commands
  if [[ "$ctx" == *"prod"* ]]; then
    case "$1" in
      delete|scale|drain|cordon|taint)
        echo "WARNING: You are in production context '$ctx'"
        read -p "Are you sure? (yes/no): " confirm
        if [ "$confirm" != "yes" ]; then
          echo "Aborted."
          return 1
        fi
        ;;
    esac
  fi

  command kubectl "$@"
}

Using with CI/CD

In automated pipelines, verify the context matches expectations:

#!/bin/bash
# Verify we're targeting the right cluster
EXPECTED_CONTEXT="ci-deploy-staging"
ACTUAL_CONTEXT=$(kubectl config current-context)

if [ "$ACTUAL_CONTEXT" != "$EXPECTED_CONTEXT" ]; then
  echo "FATAL: Context mismatch. Expected: $EXPECTED_CONTEXT, Got: $ACTUAL_CONTEXT"
  exit 1
fi

# Proceed with deployment
kubectl apply -f deployment.yaml

Current Context Details

If you need more than just the name, combine with other commands:

# Just the name
kubectl config current-context
# production

# Full details of the current context
kubectl config view --minify
# Shows cluster, user, and namespace for the current context

# Specific details
kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}'
# https://10.0.0.1:6443

kubectl config view --minify -o jsonpath='{.contexts[0].context.namespace}'
# default

When No Context Is Set

If no current context is configured:

kubectl config current-context
# error: current-context is not set
echo $?
# 1

Handle this gracefully in scripts:

CONTEXT=$(kubectl config current-context 2>/dev/null)
if [ -z "$CONTEXT" ]; then
  echo "No active context. Available contexts:"
  kubectl config get-contexts -o name
  exit 1
fi

Multi-Terminal Awareness

Remember that current-context is read from the kubeconfig file. If you switch contexts in one terminal, all terminals using the same kubeconfig file are affected:

# Terminal 1
kubectl config use-context production
# Now ALL terminals using ~/.kube/config point to production

# To isolate contexts per terminal, copy the kubeconfig
export KUBECONFIG=$(mktemp)
cp ~/.kube/config "$KUBECONFIG"
kubectl config use-context staging
# Only this terminal is affected

This isolation technique is critical when working with multiple clusters simultaneously and is a common topic in Kubernetes operational interviews.

Interview Questions About This Command

How do you check which Kubernetes context is currently active?
kubectl config current-context prints the name of the current context. This tells you which cluster, user, and namespace kubectl is targeting.
How is current-context different from get-contexts?
current-context returns just the active context name (a single string). get-contexts lists all contexts with details like cluster, user, and namespace.
Why should you check the current context before running destructive commands?
To prevent accidentally running commands against the wrong cluster. It is a critical safety habit, especially with production access.

Common Mistakes

  • Not checking current-context before running destructive operations like delete or scale-down.
  • Assuming the context name tells you everything — the same context name could point to different clusters in different kubeconfig files.
  • Not incorporating context awareness into shell prompts, making it easy to lose track of the target cluster.

Related Commands