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

Linux 安裝後 Shell 指令失效?修復 PATH 環境變數恢復 /sbin 路徑

編輯:關於 Linux

全新安裝 Linux 後,若發現 ifconfig、shutdown、reboot 等系統管理指令提示「command not found」,通常是因為 /sbin、/usr/sbin 不在一般使用者的 PATH 環境變數中。

Shell 指令失效示意圖

根本原因

預設僅 root 使用者的 PATH 包含 /sbin、/usr/sbin,一般使用者需手動添加或使用絕對路徑執行。

永久解決方法:修改 /etc/profile

  1. 以 root 編輯 /etc/profile:
vim /etc/profile
  1. 在檔案末尾加入:
export PATH=$PATH:/sbin:/usr/sbin
  1. 儲存離開,立即生效:
source /etc/profile

驗證

echo $PATH
# 應包含 /sbin:/usr/sbin

ifconfig
# 應正常顯示網路介面資訊

替代方案

  • 單次使用:直接輸入絕對路徑 /sbin/ifconfig。
  • 個人化設定:將 export PATH=$PATH:/sbin:/usr/sbin 加入 ~/.bashrc 或 ~/.bash_profile,僅影響當前使用者。
  • 建立別名:alias ifconfig='/sbin/ifconfig' 加入 ~/.bashrc。

修改 /etc/profile 為全域生效,適合多使用者環境;個人偏好可選用 ~/.bashrc。重新登入後即可直接使用系統管理指令,無需每次輸入完整路徑。

Copyright © Windows教程網 All Rights Reserved