limits.conf文件限制着用戶可以使用的最大文件數,最大線程,最大內存等資源使用量。
最近在測試環境部署hadoop集群和elastic集群,因此需要對limits.conf文件進行修改。
vim /etc/security/limits.conf * soft nofile 1048576 * hard nofile 1048576 * soft memlock -1 * hard memlock -1 * soft nproc -1 * hard nproc -1
出於好奇,發現內存和線程都可以設置為不限制。想偷懶把nofile也設置成不限制。錯誤配置如下:
vim /etc/security/limits.conf * soft nofile -1 * hard nofile -1 * soft memlock -1 * hard memlock -1 * soft nproc -1 * hard nproc -1
過了不久,開發的同事找我,有個測試集群不能遠程了,發現就是我修改的這台設備。我自己試了下切換用戶,報錯如下:
# su - root could not open session
將配置文件修改為默認值后,可以遠程登錄。
所有,修改limits.conf這類限制資源使用的文件,一定要備份配置文件,並進行測試,最好能多開一個終端,在當前終端不可使用的時候,用來執行補救措施。
那么如何配置nofile,確定nofile的最大值呢。
個人經驗是使用ulimt -n命令進行測試,如果小於系統允許的最大值,設置成功,大於最大值,系統會報錯提示。
# ulimit -n 1100000 -bash: ulimit: open files: cannot modify limit: Operation not permitted # ulimit -n 1048576 # ulimit -n 1048577 -bash: ulimit: open files: cannot modify limit: Operation not permitted # ulimit -n 1048575 # ulimit -n 1048576
另外nofile的配置可以參考
http://jameswxx.iteye.com/blog/2096461
- /proc/sys/fs/file-max限制不了/etc/security/limits.conf
- 只有root用戶才有權限修改/etc/security/limits.conf
- 對於非root用戶, /etc/security/limits.conf會限制ulimit -n,但是限制不了root用戶
- 對於非root用戶,ulimit -n只能越設置越小,root用戶則無限制
- 任何用戶對ulimit -n的修改只在當前環境有效,退出后失效,重新登錄新來后,ulimit -n由limits.conf決定
- 如果limits.conf沒有做設定,則默認值是1024
- 當前環境的用戶所有進程能打開的最大問價數量由ulimit -n決定