在 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。
sudo -s
# 或 sudo -i (登入式 Shell,會載入 root 環境)
source /etc/profile
ulimit -SHn 65535
# 驗證
ulimit -n
exit
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、變數引用、命令替換。