萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Ubuntu 解決 sudo: source: command not found:正確重載環境變數與提權姿勢

在 Ubuntu 執行 sudo source /etc/profile 報 sudo: source: command not found,因 source 是 Shell 內建指令,非外部可執行檔,sudo 無法直接呼叫。本文說明正確的環境變數重載與提權操作方式。

錯誤原因

  • source (或 .) 是 bash 內建命令,不在 $PATH 中。
  • sudo 僅能執行外部程式(execve 系統呼叫),無法直接執行 Shell 內建指令。
  • ulimit 同理為內建指令,sudo ulimit 同樣會報 command not found。

正確解決方案

方案一:取得 root Shell 再執行內建指令(推薦)


sudo -s
# 或 sudo -i (登入式 Shell,會載入 root 環境)
source /etc/profile
ulimit -SHn 65535
# 驗證
ulimit -n
exit

方案二:使用 bash -c 明確指定解釋器


sudo bash -c 'source /etc/profile'
sudo bash -c 'ulimit -SHn 65535'

方案三:直接修改系統級限制配置(永久、無需重載)

編輯 /etc/security/limits.conf:


* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535

編輯 /etc/pam.d/common-session 確認含:


session required pam_limits.so

重新登入 即生效,無需手動 ulimit。

環境變數永久生效的標準做法

範圍配置檔生效方式
當前用戶~/.profile / ~/.bash_profile重新登入或 source ~/.profile
所有用戶/etc/profile / /etc/environment重新登入
當前 Shell—export VAR=value

注意:/etc/environment 非腳本,僅支援 KEY=VALUE 格式,不支援 export、變數引用、命令替換。

copyright © 萬盛學電腦網 all rights reserved