Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

CentOS 5/6/7 host 命令找不到解決:安裝 bind-utils 與 su 環境差異避坑

編輯:關於伺服器

現象:執行 host example.com 提示 command not found。

直接解決:安裝 bind-utils

yum install -y bind-utils

host、dig、nslookup 均在該套件中。

進階避坑:su vs su - 環境變數差異導致「命令找不到」

現象複現

[joe@server ~]$ su
Password:
[root@server joe]# ifconfig
bash: ifconfig: command not found

根因

  • su(不帶 -):保留原用戶環境,PATH 仍為普通用戶 PATH(不含 /sbin:/usr/sbin)
  • su - 或 su -l:切換到目標用戶完整登入環境,PATH 包含系統管理目錄

環境對比

# su (保留 joe 環境)
[root@server joe]# env | grep PATH
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/joe/bin

# su - (切換 root 環境)
[root@server ~]# env | grep PATH
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

最佳實踐

  • 管理操作始終使用 su -
  • 腳本中以 su - root -c "command" 執行
  • 或直接 sudo command(需配置 sudoers)
  • 檢查命令完整路徑:which ifconfig → /sbin/ifconfig,可用絕對路徑執行

CentOS 7/8/9 現代替代工具

Copyright © Windows教程網 All Rights Reserved
傳統工具替代(iproute2/ss)套件
ifconfigip addr / ip linkiproute2(預設安裝)
netstatss -tlnpiproute2
routeip routeiproute2
arpip neighiproute2