linux最大文件句柄數量總結

原創文章,轉載請註明出處:http://jameswxx.iteye.com/blog/2096461java

寫這個文章是爲了以正視聽,網上的文章人云亦云到簡直使人髮指。到底最大文件數被什麼限制了?too many open files錯誤到底能夠經過什麼參數控制?網上的不少文章說的大體步驟是沒有錯的,大體以下:shell

shell級限制
經過ulimit -n修改,如執行命令ulimit -n 1000,則表示將當前shell的當前用戶全部進程能打開的最大文件數量設置爲1000.tomcat

用戶級限制
ulimit -n是設置當前shell的當前用戶全部進程能打開的最大文件數量,可是一個用戶可能會同時經過多個shell鏈接到系統,因此還有一個針對用戶的限制,經過修改 /etc/security/limits.conf實現,例如,往limits.conf輸入如下內容:
root soft nofile 1000
root hard nofile 1200
soft nofile表示軟限制,hard nofile表示硬限制,軟限制要小於等於硬限制。上面兩行語句表示,root用戶的軟限制爲1000,硬限制爲1200,即表示root用戶能打開的最大文件數量爲1000,無論它開啓多少個shell。bash

系統級限制
修改cat /proc/sys/fs/file-max服務器

可是呢,有不少很重要的細節,有不少錯誤的描述,一塌糊塗,所以特的在這裏作一個說明。
一 ulimit -n
網上不少人說,ulimit -n限制用戶單個進程的問價打開最大數量。嚴格來講,這個說法實際上是錯誤的。看看ulimit官方描述:
Provides control over the resources available to the shell and to processes started by it, on systems that allow such control. The -H and -S options specify that the hard or soft limit is set for the given resource. A hard limit cannot be increased once it is set; a soft limit may be increased up to the value of the hard limit. If neither -H nor -S is specified, both the soft and hard limits are set. The value of limit can be a number in the unit specified for the resource or one of the special values hard, soft, or unlimited, which stand for the current hard limit, the current soft limit, and no limit, respectively.
If limit is omitted, the current value of the soft limit of the resource is printed, unless the -H option is given. When more than one resource is specified, the limit name and unit are printed before the value.less

人家歷來就沒說過是限制用戶的單個進程的最大文件打開數量,看看紅色部分,是限制當前shell以及該shell啓動的進程打開的文件數量。爲何會給人限制單個線程的最大文件數量的錯覺,由於不少狀況下,在一個shell環境裏,雖然可能會有多個進程,可是很是耗費文件句柄的進程不會不少,只是其中某個進程很是耗費文件句柄,好比服務器上運行着一個tomcat,那麼就是java進程要佔用大多數文件句柄。此時ulimit設置的最大文件數和java進程耗費的最大文件數基本是對應的,因此會給人這樣的一個錯覺。ide

還有,不少文章稱ulimit -n 只容許設置得愈來愈小,好比先執行了ulimit -n 1000,在執行ulimit -n 1001,就會報"cannot modify limit: Operation not permitted"錯誤。這個其實也是不許確的說法。首先要搞清楚,任何用戶均可以執行ulimit,但root用戶和非root用戶是很是不同的。
非root用戶只能越設置越小,不能越設置越大
我在機器上以非root先執行:
[wxx@br162 etc]$ ulimit -n 900
[wxx@br162 etc]$
執行成功,再增大:
[wxx@br162 etc]$ ulimit -n 901
-bash: ulimit: open files: cannot modify limit: Operation not permitted
[wxx@br162 etc]$
增長失敗,若是減小則是OK的:
[wxx@br162 etc]$ ulimit -n 899
[wxx@br162 etc]$
若是再增長到900是不行的:
[wxx@br162 etc]$ ulimit -n 900
-bash: ulimit: open files: cannot modify limit: Operation not permitted
[wxx@br162 etc]$線程

root用戶不受限制
首先切換到root:
[wxx@br162 etc]$ sudo su -
[root@br162 ~]#
查看下當前限制:
[root@br162 ~]# ulimit -n
1000000
[root@br162 ~]#
增大:
[root@br162 ~]# ulimit -n 1000001
[root@br162 ~]#
能夠成功增大,再減少:
[root@br162 ~]# ulimit -n 999999
[root@br162 ~]#
減少也是成功的,再增大:
[root@br162 ~]# ulimit -n 1000002
[root@br162 ~]#
也是ok的,可見root是不受限制的。blog

ulimit裏的最大文件打開數量的默認值
若是在limits.conf裏沒有設置,則默認值是1024,若是limits.con有設置,則默認值以limits.conf爲準。例如我換了一臺機器,登陸進去,ulimit -n顯示以下:
[root@zk203 ~]# ulimit -n
2000
這是由於個人limits.conf裏的文件打開數是2000,以下:
[root@zk203 ~]# cat /etc/security/limits.conf
root soft nofile 2000
root hard nofile 2001
若是limits.conf裏不作任何限制,則從新登陸進來後,ulimit -n顯示爲1024。
[root@zk203 ~]# ulimit -n
1024進程

ulimit修改後生效週期
修改後當即生效,從新登陸進來後失效,由於被重置爲limits.conf裏的設定值

二 /etc/security/limits.conf
網上還有繆傳,ulimit -n設定的值不能超過limits.conf裏設定的文件打開數(即soft nofile)
好吧,其實這要分兩種狀況,root用戶是能夠超過的,好比當前limits.conf設定以下:
root soft nofile 2000
root hard nofile 2001
可是我用root將最大文件數設定到5000是成功的:
[root@zk203 ~]# ulimit -n 5000
[root@zk203 ~]# ulimit -n
5000
[root@zk203 ~]#
可是非root用戶是不能超出limits.conf的設定,我切換到wxx,執行命令以下:
[wxx@zk203 ~]# ulimit -n 5000
-bash: ulimit: open files: cannot modify limit: Operation not permitted
[wxx@zk203 etc]$
因此網上的說法是錯誤的,即便非root用戶的最大文件數設置不能超過limits.conf的設置,這也只是一個表象,其實是由於,每一個用戶登陸進來,ulimit -n的默認值是limits.conf的 soft nofile指定的,可是對於非root用戶,ulimit -n只能愈來愈小,不能愈來愈大,其實這個纔是真正的緣由,可是結果是同樣的。

修改了limits.conf須要重啓系統?
這個說法很是搞笑,修改了limits.conf,從新登陸進來就生效了。在機器上試試就知道了,好多人真的很懶,寧願處處問也不肯意花一分鐘時間實際操做一下。

三 /proc/sys/fs/file-max
網上說,ulimit -n 和limits.conf裏最大文件數設定不能超過/proc/sys/fs/file-max的值,這也是搞笑了,/proc/sys/fs/file-max是系統給出的建議值,系統會計算資源給出一個和合理值,通常跟內存有關係,內存越大,改值越大,可是僅僅是一個建議值,limits.conf的設定徹底能夠超過/proc/sys/fs/file-max。
[root@zk203 ~]# cat /proc/sys/fs/file-max
1610495
我將limits.conf裏文件最大數量設定爲1610496,保存後,打印出來:
[root@zk203 ~]# cat /etc/security/limits.conf
root soft nofile1610496
root hard nofile1610496

四 總結一下 /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決定

相關文章
相關標籤/搜索