linux最大檔案控制代碼數量總結,linux控制代碼

來源:互聯網
上載者:User

linux最大檔案控制代碼數量總結(轉載),linux控制代碼

  最近部署上線的一個引擎,啟動之後記憶體、日誌顯示一切正常,但是外部無法進行引擎訪問。幾經周折,在同事的協助下,找出了問題:root使用者的open files為1024,引擎啟動時,1024個檔案控制代碼已經用盡。在晚上看到一篇不錯的文章,就轉下來了:http://jameswxx.iteye.com/blog/2096461。以下是文章內容:

  寫這個文章是為了以正視聽,網上的文章人云亦云到簡直令人髮指。到底最大檔案數被什麼限制了?too many open files錯誤到底可以通過什麼參數控制?網上的很多文章說的大致步驟是沒有錯的,大致如下:

shell級限制
通過ulimit -n修改,如執行命令ulimit -n 1000,則表示將當前shell的目前使用者所有進程能開啟的最大檔案數量設定為1000.

使用者級限制 
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。

系統級限制
修改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.
 
人家從來就沒說過是限制使用者的單個進程的最大檔案開啟數量,看看紅色部分,是限制當前shell以及該shell啟動的進程開啟的檔案數量。為什麼會給人限制單個線程的最大檔案數量的錯覺,因為很多情況下,在一個shell環境裡,雖然可能會有多個進程,但是非常耗費檔案控制代碼的進程不會很多,只是其中某個進程非常耗費檔案控制代碼,比如伺服器上運行著一個tomcat,那麼就是java進程要佔用大多數檔案控制代碼。此時ulimit設定的最大檔案數和java進程耗費的最大檔案數基本是對應的,所以會給人這樣的一個錯覺。   
還有,很多文章稱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是不受限制的。   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決定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.