Enable Auto-completion for kubectl for Redhat 8

kubectl is a versitle tool and has abundant commands and options. You would appreciate when it can do auto-completion. With this feature it will greatly simplify your daily kubenetes management tasks.

To enable it, here are what you need to do:

  1. make sure you have bash auto completion package installed.
  2. If not, install it.
  3. configure user specific profile to enable kubectl auto-completion or put it at system-wide level
  4. optional, you can even create an alias for kubectl like most lazy people would do and enable auto-completion for the alias.

1. check if bash auto completion package is installed or not with the following command:

 dnf info bash-completion

If you see the output saying “Installed Packages”, you are good. If it is listed under “Available Packages”. You need to install it.

2. install the bash-completion package. It might has a noarch sufix (no Architecture — architecture independent).

root@joetest:~# dnf search bash-completion
Last metadata expiration check: 0:45:37 ago on Tue 21 Mar 2023 09:10:34 AM CDT.
============= Name Exactly Matched: bash-completion =================
bash-completion.noarch : Programmable completion for Bash
bash-completion.src : Programmable completion for Bash
root@joetest:~# dnf install bash-completion.noarch

With bash built-in auto-completion, you already get command auto-completion for commands if they are under your execution path. But with this bash-completion package, for some commands, you can even get auto-completion for sub-commands & options. For example, systemctl will be able to auto-complete options. And when you give “grep -” and tab twice, it will give you options for the command grep. The package will put the file bash_completion.sh under /etc/profile.d/

3. put kubectl auto-completion into a specific user’s profile. Login as this user first so that the newly installed bash auto-completion package will work for this user. You can test it by just sourceing it in your current shell (the first command below). Then put it into your bash profile (the second command below).

source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc

If you want auto-completion working for all users, you can put the configuration under the /etc/bash_completion.d. Make sure the file kubectl just created has the read permission for others.

kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null
sudo chmod 644 /etc/bash_completion.d/kubectl

4. add an alias for kubectl for your user:

echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -o default -F __start_kubectl k' >>~/.bashrc

Now re-login your bash shell, you can sit back and relax.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s