环境信息:

  • Ubuntu2204
  • k8s: v1.27.5

问题现象:想要实现kubectl tab补全效果。报错:bash: _get_comp_words_by_ref: command not found

解决步骤:

  1. 安装 bash-completion
    安装apt install bash-completion假如出现以下提示,需要先执行'apt --fix-broken install这个命令处理依赖性
root@k8s-node2:~# apt-get install bash-completion
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
 libexpat1-dev : Depends: libexpat1 (= 2.4.7-1ubuntu0.3) but 2.4.7-1ubuntu0.2 is to be installed
 libpython3.10 : Depends: libpython3.10-stdlib (= 3.10.12-1~22.04.3) but 3.10.6-1~22.04.2ubuntu1 is to be installed
 libpython3.10-dev : Depends: libpython3.10-stdlib (= 3.10.12-1~22.04.3) but 3.10.6-1~22.04.2ubuntu1 is to be installed
 python3.10-dev : Depends: python3.10 (= 3.10.12-1~22.04.3) but 3.10.6-1~22.04.2ubuntu1 is to be installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).
  1. 执行bash_completion并重新加载kubectl completion,执行完成后就可以tab补全kubectl命令了
root@k8s-node2:~#source /usr/share/bash-completion/bash_completion;source <(kubectl completion bash)
  1. 添加命令到Bash配置文件,以便每次打开新的Bash shell时都能够启用kubectl命令的自动补全功能
echo "source /usr/share/bash-completion/bash_completion;source <(kubectl completion bash)" >> ~/.bashrc # 在你的 bash shell 中永久地添加自动补全
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐