Skip to content

Kubectl & Kubeconfig on a Cluster Node

Add this line to your ~/.bashrc:

echo 'export PATH=$PATH:/var/lib/rancher/rke2/bin' >> ~/.bashrc

Then reload it:

source ~/.bashrc

Verify:

which kubectl

Expected:

/var/lib/rancher/rke2/bin/kubectl

Then you can simply run:

kubectl version

Option 2: Create an alias for kubectl + kubeconfig

Since you always need both:

  • the RKE2 kubectl binary
  • the RKE2 kubeconfig

create an alias in ~/.bashrc:

alias k='sudo /var/lib/rancher/rke2/bin/kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml'

Reload:

source ~/.bashrc

Now your support commands become:

k get nodes
k auth can-i patch clusters/status \
  --as=system:serviceaccount:cattle-capi-system:capi-manager \
  --subresource=status \
  -n fleet-default

Option 3: Set KUBECONFIG permanently

Add to ~/.bashrc:

export KUBECONFIG=/etc/rancher/rke2/rke2.yaml
export PATH=$PATH:/var/lib/rancher/rke2/bin

Reload:

source ~/.bashrc

Then:

kubectl get nodes

:::info This only works if your user account can read the kubeconfig. On your system:

-rw------- root root /etc/rancher/rke2/rke2.yaml

Since it has root-only permissions, you'd still likely need sudo or a copied kubeconfig. :::


Option 4: Create a personal kubeconfig (common admin practice)

Copy the kubeconfig into your home directory:

mkdir -p ~/.kube
sudo cp /etc/rancher/rke2/rke2.yaml ~/.kube/config
sudo chown firstsolar:firstsolar ~/.kube/config
chmod 600 ~/.kube/config

Then add only the PATH change:

export PATH=$PATH:/var/lib/rancher/rke2/bin

After that, kubectl get nodes works without sudo.

Security note

Option 4 gives your Linux account cluster-admin access because the RKE2-generated kubeconfig contains administrative credentials. That's normal for cluster administrators, but I wouldn't do it unless your team's security practices allow it.

For day-to-day Rancher/RKE2 administration, I'd personally use Option 2 (the alias). It's quick, leaves the kubeconfig protected under /etc/rancher/rke2, and makes support troubleshooting commands much less painful.