11

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

1 Answer 1

17

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 KUBECONFIG=~/.kube/config to your ~/.profile or ~/.bashrc to make it persist on reboot.

5
  • 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
    Aug 1, 2022 at 7:30
  • @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. Aug 1, 2022 at 15:34
  • 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
    Aug 2, 2022 at 5:06
  • 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). Aug 2, 2022 at 14:47
  • 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
    Aug 3, 2022 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.