kubectl command permission
The .kube directory does not exist in your home directory. You can create this directory and then copy the k3s.yaml file to create your config file.
Here's how you can do it:
shCopy
mkdir -p $HOME/.kube
sudo cp /etc/rancher/k3s/k3s.yaml $HOME/.kube/config
sudo chown $(whoami):$(whoami) $HOME/.kube/config
sudo chmod 600 $HOME/.kube/config
Explanation of commands:
mkdir -p $HOME/.kube: This command creates the.kubedirectory in your home directory. The-pflag ensures thatmkdirdoes not throw an error if the directory already exists and makes any necessary parent directories.sudo cp /etc/rancher/k3s/k3s.yaml $HOME/.kube/config: This command copies thek3s.yamlfile from/etc/rancher/k3s/to your.kubedirectory and names the copied fileconfig.sudo chown $(whoami):$(whoami) $HOME/.kube/config: This command changes the ownership of theconfigfile to your user. This is important because the file needs to be owned by the user who will be runningkubectlcommands.sudo chmod 600 $HOME/.kube/config: This command changes the permissions of theconfigfile so that only the owner can read and write to it, which is a good security practice for kubeconfig files.
Once you have completed these steps, try running kubectl get nodes again. You should no longer receive the permission issue if these steps are followed correctly.
These changes will persist across reboots, with the exception of the export command, which sets an environment variable for the current session. If you want to make the KUBECONFIG environment variable setting permanent, you can add it to your shell's profile script.
For example, if you're using Bash, you could add the export line to your .bashrc or .bash_profile:
bashCopy
echo 'export KUBECONFIG=$HOME/.kube/config' >> $HOME/.bashrc
Or if you're using Zsh, you could add it to your .zshrc:
bashCopy
echo 'export KUBECONFIG=$HOME/.kube/config' >> $HOME/.zshrc
After adding this line to the appropriate file, you'll need to either restart your terminal session or source the file (e.g., source $HOME/.bashrc or source $HOME/.zshrc) to load the changes. Once this is done, the KUBECONFIG variable will be set automatically every time you start a new shell session.
Make sure to replace $HOME with the actual path if your shell does not expand variables when running echo commands.
This way, you won't have to manually export the KUBECONFIG environment variable every time you open a new terminal session. It will be set permanently across reboots.
Apply the changes:
For the change to take effect in your current session, source the profile script:
For Bash:
bashCopy
source ~/.bashrc
For Bash on macOS Catalina and later:
bashCopy
source ~/.bash_profile