<p><code>kubectl</code> will look for the default kubeconfig file <code>$HOME/.kube/config</code>.</p>
<p>Remember that <code>$HOME</code> is the home directory of the user running the command. If you are running <code>kubectl</code> as root, your root user needs to have that file at <code>/root/.kube/config</code> correctly defined.</p>
<p>Overall, you do not need to create any files in your root's home directory. You can simply force <code>kubectl</code> to use a fixed kubeconfig file:</p>
<pre><code>sudo kubectl --kubeconfig=/home/myuser/.kube/config ...
</code></pre>
<p>If you already have an environment variable <code>KUBECONFIG</code> that points to your kubeconfig file, you can also use <code>sudo -E</code> to inherit the current user's environment while using sudo.</p>
<pre><code>sudo -E kubectl ...
</code></pre>

CC BY-SA 4.0
2024-12-29T23:00:05.883Z
2024-12-29T23:00:05.883Z
Julio Henrique Morimoto
<p>Above is correct but you want to add this kube config to for root use below command to add and after that you will be able to use kubect command directly with root user. add it in linux env by export command or in windows by set-</p>
<pre><code>export KUBECONFIG=path/to/kubeconfig.yaml
</code></pre>

CC BY-SA 4.0
2024-12-30T09:28:36.997Z
2024-12-30T09:28:36.997Z
Sandip Singh
<p>When running kubectl as root I get this error:
Couldn't get current server API group list: Get &quot;https://127.0.0.1:38805/api? timeout=32s&quot;: dial tcp 127.0.0.1:38805: connect: connection refused</p>
<p>But when I run it from user it runs okay</p>
<p>When I run kubectl config view as root and as user I get 2 different outputs.</p>

<kubernetes><kubectl>
Running kubectl as root
Timm
# Running kubectl as root

(Asked by: username_0 on 2024-12-26)

When running kubectl as root I get this error:
Couldn't get current server API group list: Get "https://127.0.0.1:38805/api? timeout=32s": dial tcp 127.0.0.1:38805: connect: connection refused

But when I run it from user it runs okay

When I run kubectl config view as root and as user I get 2 different outputs.

**Tags:** kubernetes, kubectl


---

## 2 answers

### Answer by username_1 on 2024-12-29. Score: 0

kubectl will look for the default kubeconfig file $HOME/.kube/config.
Remember that $HOME is the home directory of the user running the command. If you are running kubectl as root, your root user needs to have that file at /root/.kube/config correctly defined.
Overall, you do not need to create any files in your root's home directory. You can simply force kubectl to use a fixed kubeconfig file:
sudo kubectl --kubeconfig=/home/myuser/.kube/config ...

If you already have an environment variable KUBECONFIG that points to your kubeconfig file, you can also use sudo -E to inherit the current user's environment while using sudo.
sudo -E kubectl ...


---

### Answer by username_2 on 2024-12-30. Score: 0

Above is correct but you want to add this kube config to for root use below command to add and after that you will be able to use kubect command directly with root user. add it in linux env by export command or in windows by set-
export KUBECONFIG=path/to/kubeconfig.yaml


---

