<p>Prefix you command with sudo.</p>
<p>Run:</p>
<p><code>sudo k3s kubectl version</code></p>

CC BY-SA 4.0
2024-05-22T21:43:39.047Z
2024-05-22T21:43:39.047Z
user10339671
<h1>No, do not change permissions of <code>/etc/rancher/k3s/k3s.yaml</code></h1>
<p>First set up your an environmental variable for <code>KUBECONFIG=~/.kube/config</code>.</p>
<pre><code>export KUBECONFIG=~/.kube/config
</code></pre>
<p>Then let's generate the file at that location. <a href="https://github.com/k3s-io/k3s/issues/389" rel="nofollow noreferrer">Your <code>k3s.yaml</code> file should <strong>NOT</strong> be world readable.</a>. This is by-design. It should be owned by root and set to <code>0600</code>. Instead copy the config locally as <a href="https://devops.stackexchange.com/a/16014/18965">described here</a>,</p>
<pre class="lang-shell prettyprint-override"><code>mkdir ~/.kube 2&gt; /dev/null
sudo k3s kubectl config view --raw &gt; &quot;$KUBECONFIG&quot;
chmod 600 &quot;$KUBECONFIG&quot;
</code></pre>
<p>You can add <code>export KUBECONFIG=~/.kube/config</code> to your <code>~/.profile</code> or <code>~/.bashrc</code> to make it persist on reboot.</p>

CC BY-SA 4.0
2022-08-01T07:30:19.310Z
If the "sudo k3s kubectl ..." line is giving an error stating "sudo: k3s: command not found", then you can try by adding ```/usr/local/bin/``` in front of "k3s". Which would make that line as this: ```sudo /usr/local/bin/k3s kubectl config view --raw > "$KUBECONFIG"```
UNOPARATOR
CC BY-SA 4.0
2022-08-01T15:34:00.390Z
@UNOPARATOR the only way that could make a difference is if `/usr/local/bin` is not in your `$PATH` which would be pretty awkward anyway.
Evan Carroll
CC BY-SA 4.0
2022-08-02T05:06:25.540Z
In my case with a CentOS 8 Stream server, after the k3s installation that line wasn't working. There was no mention of adding that to ```$PATH``` but it might be considered common practice and not mentioned because of that. I'm no linux expert of any kind, just commented in case someone like me encounters the same issue.
UNOPARATOR
CC BY-SA 4.0
2022-08-02T14:47:27.220Z
When you run `echo $PATH` you should see `/usr/local/bin` in your path. Almost certainly CentOS does that. (also, CentOS is dead, everyone went to Rocky).
Evan Carroll
CC BY-SA 4.0
2022-08-03T04:49:04.783Z
It wasn't there, so added ```export PATH=$PATH:/usr/local/bin``` line to ```$HOME/.bashrc```. (Just checked out Rocky Linux from wiki, might try that next time - thanks for the heads up)
UNOPARATOR
CC BY-SA 4.0
2023-12-28T20:55:06.103Z
@UNOPARATOR, the problem is that on CentOS by default `sudo` will use a PATH value different from your own PATH. This other value does not include `/usr/local/bin`, which is why it couldn't find the `k3s` command. One way to resolve this is to add `/usr/local/bin` to the `secure_path` option in `/etc/sudoers`. See https://superuser.com/questions/927512/how-to-set-path-for-sudo-commands for details. Ubuntu and Debian do not have this issue (`secure_path` includes `/usr/local/bin`).
Stanley Yu
CC BY-SA 4.0
2024-06-07T02:23:26.217Z
After doing this I have the config file in .kube and KUBECONFIG in my environment variables confirmed by echo $KUBECONFIG but when I execute "kubectl version --client" it is still looking in /etc/rancher/k3s/k3s.yaml
Cord
CC BY-SA 4.0
2024-08-18T01:32:10.827Z
at least in my case adding "export KUBECONFIG=~/.kube/config" to .bashrc and sourcing it on each node in the cluster was sufficient for kubectl to work. that was on debian 12 running k3s v1.30 arm64.
majorgear
CC BY-SA 4.0
2022-05-31T19:53:26.253Z
2024-11-29T18:45:41.247Z
2024-11-29T18:45:41.247Z
Evan Carroll
<p>When I run commands under <code>k3s kubectl</code>, I get</p>
<pre class="lang-shell prettyprint-override"><code>$ k3s kubectl version
WARN[0000] Unable to read /etc/rancher/k3s/k3s.yaml, please start server with --write-kubeconfig-mode to modify kube config permissions 
error: error loading config file &quot;/etc/rancher/k3s/k3s.yaml&quot; : open /etc/rancher/k3s/k3s.yaml: permission denied
</code></pre>
<p>How should I resolve this? Should I change the permissions of <code>/etc/rancher/k3s/k3s.yaml</code></p>

<rancher><k3s>
error: error loading config file "/etc/rancher/k3s/k3s.yaml": open /etc/rancher/k3s/k3s.yaml: permission denied
Evan Carroll
# error: error loading config file "/etc/rancher/k3s/k3s.yaml": open /etc/rancher/k3s/k3s.yaml: permission denied

(Asked by: username_0 on 2022-05-31)

When I run commands under k3s kubectl, I get
$ k3s kubectl version
WARN[0000] Unable to read /etc/rancher/k3s/k3s.yaml, please start server with --write-kubeconfig-mode to modify kube config permissions 
error: error loading config file "/etc/rancher/k3s/k3s.yaml" : open /etc/rancher/k3s/k3s.yaml: permission denied

How should I resolve this? Should I change the permissions of /etc/rancher/k3s/k3s.yaml

**Tags:** rancher, k3s


---

## 2 answers

### Answer by username_0 on 2022-05-31. Score: 32 | Accepted answer

No, do not change permissions of /etc/rancher/k3s/k3s.yaml
First set up your an environmental variable for KUBECONFIG=~/.kube/config.
export KUBECONFIG=~/.kube/config

Then let's generate the file at that location. Your k3s.yaml file should NOT be world readable.. This is by-design. It should be owned by root and set to 0600. Instead copy the config locally as described here,
mkdir ~/.kube 2> /dev/null
sudo k3s kubectl config view --raw > "$KUBECONFIG"
chmod 600 "$KUBECONFIG"

You can add export KUBECONFIG=~/.kube/config to your ~/.profile or ~/.bashrc to make it persist on reboot.

> **Comments:**
> **username_2**: If the "sudo k3s kubectl ..." line is giving an error stating "sudo: k3s: command not found", then you can try by adding ```/usr/local/bin/``` in front of "k3s". Which would make that line as this: ```sudo /usr/local/bin/k3s kubectl config view --raw > "$KUBECONFIG"```  
> **username_0**: @username_2 the only way that could make a difference is if `/usr/local/bin` is not in your `$PATH` which would be pretty awkward anyway.  
> **username_2**: In my case with a CentOS 8 Stream server, after the k3s installation that line wasn't working. There was no mention of adding that to ```$PATH``` but it might be considered common practice and not mentioned because of that. I'm no linux expert of any kind, just commented in case someone like me encounters the same issue.  
> **username_0**: When you run `echo $PATH` you should see `/usr/local/bin` in your path. Almost certainly CentOS does that. (also, CentOS is dead, everyone went to Rocky).  
> **username_2**: It wasn't there, so added ```export PATH=$PATH:/usr/local/bin``` line to ```$HOME/.bashrc```. (Just checked out Rocky Linux from wiki, might try that next time - thanks for the heads up)  
> **username_3**: @username_2, the problem is that on CentOS by default `sudo` will use a PATH value different from your own PATH. This other value does not include `/usr/local/bin`, which is why it couldn't find the `k3s` command. One way to resolve this is to add `/usr/local/bin` to the `secure_path` option in `/etc/sudoers`. See https://superuser.com/questions/927512/how-to-set-path-for-sudo-commands for details. Ubuntu and Debian do not have this issue (`secure_path` includes `/usr/local/bin`).  
> **username_4**: After doing this I have the config file in .kube and KUBECONFIG in my environment variables confirmed by echo $KUBECONFIG but when I execute "kubectl version --client" it is still looking in /etc/rancher/k3s/k3s.yaml  
> **username_5**: at least in my case adding "export KUBECONFIG=~/.kube/config" to .bashrc and sourcing it on each node in the cluster was sufficient for kubectl to work. that was on debian 12 running k3s v1.30 arm64.  

---

### Answer by username_1 on 2024-05-22. Score: 0

Prefix you command with sudo.

Run:

`sudo k3s kubectl version`


---

