在使用 Linux/UNIX 時,會經常遇到 “command not found” 的錯誤,就如提示的信息,Linux /UNIX 沒有找到該命令。原因無外乎你命令拼寫錯誤或 Linux/UNIX 系統就沒有安裝該命令。
分析過程
確認命令沒有拼寫錯誤
Linux/UNIX 中的所有命令都是大小寫敏感的。
搜索路徑中檢查
查找命令路徑
$ which xxxx
/usr/bin/which: no xxxx in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
顯示當前的搜索路徑
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
檢查執行命令的目錄是否存在。目錄存在但不正確,則修正即可;目錄不存在,則通過如下命令添加。
把目錄添加到 $PATH 下面
$ export PATH=$PATH:/xxxx/bin
注意:永久生效,需要添加到全局環境變量文件(/etc/profile)或用戶環境變量文件(~/.bash_profile)中。
# 添加到系統環境變量文件,並實時生效
$ echo "export PATH=$PATH:/xxxx/bin" >> /etc/profile && source /etc/profile
# 添加到用戶環境變量文件,並實時生效
$ echo "export PATH=$PATH:/xxxx/bin" >> ~/.bash_profile && source ~/.bash_profile
驗證命令路徑
$ which cd
/usr/bin/cd
常見場景
Centos 最小化安裝,導致 ifconfig,netstat 命令找不到
# 查找 ifconfig 命令路徑
$ which ifconfig
/usr/bin/which: no ifconfig in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
# yum 搜索 ifconfig 命令
$ yum search all ifconfig
============================================================================= 匹配:ifconfig ==============================================================================
python36-ifcfg.noarch : Python cross-platform network interface discovery (ifconfig/ipconfig/ip)
moreutils.x86_64 : Additional unix utilities
net-tools.x86_64 : Basic networking tools
python2-psutil.x86_64 : A process and system utilities module for Python
python34-psutil.x86_64 : A process and system utilities module for Python
python36-psutil.x86_64 : A process and system utilities module for Python
# yum 安裝 net-tools.x86_64 包
$ yum install -y net-tools
# 驗證命令路徑
$ which ifconfig
/usr/sbin/ifconfig