RedHat/CentOS 系統中,非 root 用戶或最小化安裝環境下執行 ifconfig 提示 command not found,主因是 /sbin、/usr/sbin 不在 PATH 中。本文提供臨時、用戶級、系統級三種修改 PATH 的方法,並介紹直接使用絕對路徑、修改 /etc/profile 取消 root 專屬路徑限制等替代方案。
ifconfig 位於 /sbin/ifconfig(屬 net-tools 套件)。/etc/profile 中有判斷:if [ "$EUID" = "0" ]; then pathmunge /sbin; ...,僅 root 自動獲得 /sbin 路徑。PATH 缺少 /sbin、/usr/sbin。export PATH=$PATH:/sbin:/usr/sbin
echo 'export PATH=$PATH:/sbin:/usr/sbin' >> ~/.bash_profile
source ~/.bash_profile
echo 'export PATH=$PATH:/sbin:/usr/sbin' | sudo tee -a /etc/profile
source /etc/profile
/sbin/ifconfig
# 或建立別名
alias ifconfig='/sbin/ifconfig'
echo "alias ifconfig='/sbin/ifconfig'" >> ~/.bashrc
編輯 /etc/profile,將:
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi
改為(註釋判斷,保留 pathmunge):
#if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
#fi
存檔後 source /etc/profile 或重新登入。所有用戶皆可直接使用 ifconfig、route、netstat 等網管指令。
net-tools(含 ifconfig、netstat、route)已停止維護,建議改用 iproute2 套件:
| 舊指令 | 新指令 |
|---|---|
ifconfig | ip addr show / ip link |
netstat -tuln | ss -tuln |
route -n | ip route show |
arp -n | ip neigh show |
CentOS 7+/RHEL 7+ 預設可能未安裝 net-tools,可 yum install net-tools 補裝,但建議養成 ip/ss 使用習慣。