Skip to content
ohsudo, Home
Kubernetes

kubectl "error: You must be logged in to the server (Unauthorized)": renew your credentials

Fix kubectl "You must be logged in to the server (Unauthorized)": check the context, renew expired certificates and tokens, and refresh EKS, GKE and AKS credentials.

Intermediate3 min readApplies to Kubernetes 1.28+ · kubectl · kubeadm · Amazon EKS · Google GKE · Azure AKS

by OhSudo ··

Checked on real software: Kubernetes (kind), kubectl · November 2026How we check

Not sure this is your cause?Find it step by step: Pod not running →

Part of a learning path:Kubernetes: debugging pods

$ kubectl get pods
error: You must be logged in to the server (Unauthorized)

kubectl reached the API server, and the server didn’t accept who you said you were. The connection and TLS worked. What failed is the credential in your kubeconfig: a client certificate, a token, or a login plugin. It’s expired, revoked, or belongs to another cluster.

  1. kubectlsends the kubeconfig credential
  2. API serverreached over TLS(works)
  3. Credential refusedUnauthorized(breaks here)
The connection and TLS worked. The API server didn't accept the credential in your kubeconfig: expired, revoked, or for another cluster.

TL;DR

Terminal window
kubectl config current-context
kubectl config view --minify # which user, which cluster
# kubeadm: has the admin certificate expired?
sudo kubeadm certs check-expiration
# Cloud clusters: fetch fresh credentials
aws eks update-kubeconfig --name prod --region eu-west-1
gcloud container clusters get-credentials prod --region europe-west1
az aks get-credentials --resource-group rg-prod --name prod

1. Are you talking to the right cluster?

Terminal window
kubectl config current-context
kubectl config get-contexts

A kubeconfig often has several clusters. Credentials from one don’t work on another, and a cluster that was recreated with the same name and address has new keys, so the old kubeconfig is rejected. Switch with kubectl config use-context NAME, or get a new kubeconfig for the new cluster.

2. An expired client certificate (kubeadm)

kubeadm signs the admin’s certificate, in /etc/kubernetes/admin.conf, for one year. After that, every kubectl command using it fails like this. Check:

Terminal window
sudo kubeadm certs check-expiration

To read the date from your own kubeconfig:

Terminal window
kubectl config view --raw --minify -o jsonpath='{.users[0].user.client-certificate-data}' | base64 -d | openssl x509 -noout -enddate

Renew on a control-plane node, then copy the new admin config:

Terminal window
sudo kubeadm certs renew all
sudo cp /etc/kubernetes/admin.conf ~/.kube/config
sudo chown $(id -u):$(id -g) ~/.kube/config

Restart the control-plane components after renewing, so they use the new certificates: kubeadm’s documentation does it by moving the static pod manifests out of /etc/kubernetes/manifests/ and back. Upgrading the cluster with kubeadm upgrade also renews them, which is why clusters that are upgraded yearly rarely see this.

3. A token that no longer works

Tokens in a kubeconfig, such as one for a CI service account, stop working when:

  • they expire. Tokens from kubectl create token last one hour by default. Ask for longer with --duration=24h, within what the cluster allows.
  • the service account, or its token Secret, was deleted. Recreating an account with the same name doesn’t bring an old token back.

A service account token is a JWT. Paste it into the JWT Decoder to read its exp (expiry) and sub (system:serviceaccount:NAMESPACE:NAME) without sending it anywhere.

4. Cloud clusters

Managed clusters use a login plugin that fetches a short-lived token on each command. Fetch the config again, and update the tools:

  • EKS: aws eks update-kubeconfig --name CLUSTER --region REGION. Check which identity the AWS CLI uses with aws sts get-caller-identity. That identity must have access to the cluster, through an access entry or the older aws-auth ConfigMap: a role that isn’t mapped gets this exact error, even with valid AWS credentials.
  • GKE: gcloud container clusters get-credentials CLUSTER, with the gke-gcloud-auth-plugin component installed.
  • AKS: az aks get-credentials, and with Microsoft Entra ID, kubelogin. An expired login needs az login again.

If the cloud CLI itself has no credentials, fix that first: for AWS, see Unable to locate credentials.

5. On the server side

If many users fail at once, look at the API server. Its log, kubectl logs -n kube-system kube-apiserver-NODE from a working account or crictl logs on the node, says Unable to authenticate the request with the reason. A clock that’s far off on the client or the server also makes valid certificates and tokens look expired or not yet valid: check timedatectl on both.

Warning

Don’t share /etc/kubernetes/admin.conf with people or pipelines. It’s a cluster-admin identity that can’t be revoked short of replacing the cluster’s CA. Give each one its own account with only the rights it needs, as in Forbidden.

Shell, we have a problem!

Similar, but different: Forbidden: User … cannot list resource means you’re logged in, without the rights. x509: certificate signed by unknown authority means kubectl doesn’t trust the server, before any login. The connection to the server localhost:8080 was refused means there’s no kubeconfig at all. For everyday commands, see the kubectl cheat sheet.

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles