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.
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 podserror: 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.
- kubectlsends the kubeconfig credentialconnects
- API serverreached over TLS(works)checks who you are(fails)
- Credential refusedUnauthorized(breaks here)
TL;DR
kubectl config current-contextkubectl config view --minify # which user, which cluster
# kubeadm: has the admin certificate expired?sudo kubeadm certs check-expiration
# Cloud clusters: fetch fresh credentialsaws eks update-kubeconfig --name prod --region eu-west-1gcloud container clusters get-credentials prod --region europe-west1az aks get-credentials --resource-group rg-prod --name prod1. Are you talking to the right cluster?
kubectl config current-contextkubectl config get-contextsA 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:
sudo kubeadm certs check-expirationTo read the date from your own kubeconfig:
kubectl config view --raw --minify -o jsonpath='{.users[0].user.client-certificate-data}' | base64 -d | openssl x509 -noout -enddateRenew on a control-plane node, then copy the new admin config:
sudo kubeadm certs renew allsudo cp /etc/kubernetes/admin.conf ~/.kube/configsudo chown $(id -u):$(id -g) ~/.kube/configRestart 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 tokenlast 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 withaws sts get-caller-identity. That identity must have access to the cluster, through an access entry or the olderaws-authConfigMap: a role that isn’t mapped gets this exact error, even with valid AWS credentials. - GKE:
gcloud container clusters get-credentials CLUSTER, with thegke-gcloud-auth-plugincomponent installed. - AKS:
az aks get-credentials, and with Microsoft Entra ID,kubelogin. An expired login needsaz loginagain.
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.