在 Ubuntu 上想用 sudo 載入 /etc/profile,卻得到 sudo: source: command not found。原因不是 PATH 有問題,而是 source 是 shell 內建指令,sudo 預設不會用 shell 去執行它。
先看目前的開檔數上限:
$ ulimit -n 1024
編輯 /etc/profile,在最後補上一行提高限制:
ulimit -SHn 65535
接著想讓設定生效:
$ sudo source /etc/profile sudo: source: command not found
$ ulimit -SHn 65535 -bash: ulimit: open files: cannot modify limit: Operation not permitted $ sudo ulimit -SHn 65535 sudo: ulimit: command not found
兩個錯誤其實指向同一件事:ulimit 是 bash 內建指令,而 sudo 會去找外部可執行檔,自然找不到;即使找到也改不到 shell 本身的限制。
$ sudo -s # source /etc/profile
再次確認:
# ulimit -n 65535
限制已成功提高。關鍵在於「用 root 身分取得一個互動式 shell,再在同一個 shell 裡執行內建指令」,而不是用 sudo 去呼叫它。
這個觀念不只適用 ulimit,cd、export、alias 等內建指令都一樣——需要它們生效時,切到 root shell 才是正解。