How to Enable Kubectl Auto-Completion on Linux

Speed up your kubectl usage on Linux by setting up auto-completion for faster Kubernetes tasks. Here's how to do it

In container orchestration, Kubernetes stands tall as the go-to platform, with its command-line tool, kubectl, acting as the powerful wand in the hands of its wizards. While undeniably potent, its vast array of commands and options can sometimes make even seasoned professionals fumble.

After all, it is hard to remember all the countless options and combinations between them that the kubectl tool offers.

But even if you are among the most seasoned professionals who know them by heart, there will always be that one moment with their tedious input, sometimes bordering on writing entire sentences.

What if there was a way to make this tool more intuitive and efficient with the ability to automatically complete (hint) the available options, similar to the auto-complete you are used to with other Linux commands?

We’ve got good news – you can get this functionality quickly and easily, and we’ll show you how immediately. So, are you ready to accelerate your Kubernetes tasks with kubectl auto-completion? Let’s dive in!

Enable Kubernetes’ Kubectl Auto-Completion

First, make sure your Linux system has the “bash-completion” package installed. Most distributions come with it installed by default, but if not, you can easily add it depending on the package manager you use, APT, DNF, Pacman, etc.

And now to the fun part. The kubectl command has built-in functionality to оutput shell completion code for specified shell – Bash or Zsh. Since Bash is the most widely used and almost all Linux distributions come with it as a default, we will use it.

So, open the terminal, navigate to your home directory (if you are not in it), and generate the kubectl auto-completion script for Bash with the following command:

kubectl completion bash > .kubectl_completion_bashCode language: CSS (css)

The command will create a “.kubectl_completion_bash” file in your home directory containing everything you need for the auto-completion functionality of the kubectl command.

The next step is to tell our system to use it. To do this, first open the “.bashrc” file with the terminal text editor you are using.

nano ~/.bashrc

Then go to the very end of it and add the following line:

source ~/.kubectl_completion_bash
Enable the kubectl command auto-completion.
Enable the kubectl command auto-completion.

Save the file and exit. Finally,  to enable kubectl auto-completion immediately in the current shell session, source the file:

source ~/.bashrc

Now try typing a kubectl command, using the familiar repeated press of “Tab” to autocomplete the command or display the available options. The magic is done – everything should now work as expected.

kubectl's auto-completion in action.
kubectl’s auto-completion in action.

Conclusion

As we’ve explored, enabling kubectl auto-completion on Linux isn’t just a matter of convenience; it is an enhancement that can dramatically streamline your Kubernetes operations.

By integrating this feature, we not only minimize the risk of command errors but also foster a more efficient and enjoyable Kubernetes command-line experience.

For detailed information about the kubectl command line tool and all the options it offers, refer to its official documentation.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%