kubectl "The connection to the server localhost:8080 was refused": your kubeconfig is missing
Fix kubectl "The connection to the server localhost:8080 was refused": kubectl has no kubeconfig. Set it up for kubeadm, k3s, minikube, EKS, GKE or AKS, and under sudo.
by OhSudo ··
Checked on real software: 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
The connection to the server localhost:8080 was refused - did you specify the right host or port?Newer versions print a few couldn't get current server API group list lines before it. The clue is the address. Your cluster doesn’t run on localhost:8080: the API server normally listens on port 6443. localhost:8080 is kubectl’s fallback when it has no configuration at all.
So the cluster may be perfectly healthy. kubectl just doesn’t know where it is.
- kubectllooks for a kubeconfigfinds none(fails)
- No configuration found~/.kube/config, KUBECONFIG, sudo(breaks here)falls back to
- localhost:8080nothing there
TL;DR
kubectl config view # empty = no kubeconfig foundecho $KUBECONFIGls -l ~/.kube/configThen get a config for your kind of cluster (section 2).
1. Confirm it
apiVersion: v1clusters: nullcontexts: nullcurrent-context: ""kind: Configpreferences: {}users: nullclusters: null is the proof. kubectl reads the files listed in $KUBECONFIG, or ~/.kube/config if that variable is empty. ~ depends on who you are, and that’s behind section 3.
2. Get a kubeconfig for your cluster
kubeadm, on the control plane:
mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/configk3s keeps it elsewhere, and readable only by root:
mkdir -p ~/.kubesudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/configsudo chown $(id -u):$(id -g) ~/.kube/configFrom another machine, also change server: https://127.0.0.1:6443 in that file to the server’s address.
Local clusters write it when they start, so start them:
minikube startkind create clusterCloud clusters have a command that adds it:
aws eks update-kubeconfig --region eu-central-1 --name my-clustergcloud container clusters get-credentials my-cluster --region europe-west1az aks get-credentials --resource-group my-group --name my-clusterThen check:
kubectl config current-contextkubectl get nodes3. It works without sudo, but not with it (or the reverse)
sudo kubectl runs as root, whose home is /root, so it reads /root/.kube/config, and sudo normally drops your KUBECONFIG too. You rarely need sudo for kubectl: permissions come from the config file, not your Linux user. Run it without sudo. If you must, pass the file:
sudo kubectl --kubeconfig=/home/me/.kube/config get podsThe same catches scripts, cron jobs and CI: they run with a different user or environment. Set KUBECONFIG explicitly there. In a pipeline, store it as a secret: the CI/CD Pipeline Generator shows one way.
4. Several files, or the wrong context
KUBECONFIG can list several files, separated by : on Linux and macOS:
export KUBECONFIG=~/.kube/config:~/.kube/staging.yamlkubectl config get-contextskubectl config use-context stagingPut the export in your shell’s profile, or it’s gone in the next terminal.
Warning
Keep kubeconfig files private: chmod 600 ~/.kube/config. The admin.conf from kubeadm gives full control of the cluster, so don’t copy it around or commit it to git. Give people and pipelines their own limited credentials.
5. When the address is a real one
If the error names your real server, kubectl has its config, and the problem is the connection:
The connection to the server 10.0.0.10:6443 was refused - did you specify the right host or port?That’s a different job: the API server is down, or something’s in the way. On the control plane:
sudo ss -tlnp | grep 6443sudo systemctl status kubelet --no-pagersudo crictl ps -a | grep kube-apiserverIf it times out instead of being refused, look at firewalls, as “Connection timed out” vs “Connection refused” explains. The Port Lookup has an entry for 6443, and why it shouldn’t be open to everyone.
Shell, we have a problem!
Similar, but different: Unable to connect to the server: x509: certificate signed by unknown authority means kubectl reached a server, but the config has the wrong certificate: see that article. error: You must be logged in to the server (Unauthorized) means your credentials are wrong or expired. And Forbidden means you’re logged in, without the rights. For pods that won’t start once you’re connected, see Pod Pending or CrashLoopBackOff. To write the manifests themselves, try the Kubernetes Manifest Generator. For the commands around this, see the kubectl Cheat Sheet.
$ ./practice.sh
Practice this in the terminal
Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.