這個文件主要是用來限制用戶對系統資源的使用,具體的使用方法 man 5 limits.conf,裡面便給出了詳細的用法
- user@db-2:~$ cat /etc/security/limits.conf
- # /etc/security/limits.conf
- #
- #Each line describes a limit for a user in the form:
- #
- #<domain> <type> <item> <value>
- #
- #Where:
- #<domain> can be:
- # - an user name
- # - a group name, with @group syntax
- # - the wildcard *, for default entry
- # - the wildcard %, can be also used with %group syntax,
- # for maxlogin limit
- #
- #<type> can have the two values:
- # - "soft" for enforcing the soft limits # 軟限制,必須必硬限制的值要小
- # - "hard" for enforcing hard limits # 硬限制
- #
- #<item> can be one of the following:
- # - core - limits the core file size (KB)
- # - data - max data size (KB)
- # - fsize - maximum filesize (KB)
- # - memlock - max locked-in-memory address space (KB)
- # - nofile - max number of open files # 最大打開的文件數(以文件描敘符,file descripter計數)
- # - rss - max resident set size (KB)
- # - stack - max stack size (KB)
- # - cpu - max CPU time (MIN)
- # - nproc - max number of processes
- # - as - address space limit
- # - maxlogins - max number of logins for this user
- # - maxsyslogins - max number of logins on the system
- # - priority - the priority to run user process with
- # - locks - max number of file locks the user can hold
- # - sigpending - max number of pending signals
- # - msgqueue - max memory used by POSIX message queues (bytes)
- # - nice - max nice priority allowed to raise to
- # - rtprio - max realtime priority
- #
- #<domain> <type> <item> <value>
- #
- root soft nofile 200000
- root hard nofile 200000
- admin hard nofile 65536
- admin soft nofile 65536
- # End of file
每行的格式:
用戶名/用戶組 類型(硬限制、軟限制) 選項 值
比如很多朋友可能在使用mysql的時候遇到two many open files的錯誤,此時便可以通過將運行mysqld的用戶的 nofile(最大打開文件數)這個值增大一點,例如
db_running_user - nofile 設定一個合理的值
注意上面的第二列 ‘-’表示hard和soft,可以顯示的為soft和hard寫兩行
我這只是舉一個例子,這個文件還可以配置很多資源的使用,但是一般的情況下用默認的就行,當你有特殊需求或者自己對這些參數的來龍去脈又相當了解,你可以去改這些默認的參數,查看當前用戶的這些資源使用限制情況可以使用 ulimit -a(查看ulimit的使用方法,用man ulimit無效,可以用help ulimit)
那麼對它的修改什麼時候生效呢?每次重新login的時候生效,至於原因則是我參考的那篇文章裡面說了。
自己的的疑問:
1. 為什麼要同時有soft和hard這兩個限制?這個soft和hard應該是代表兩種類型的閥值吧,一定不能超過hard限制,但是可以超過soft限制,有一個hard不就夠了麼?
知道設置兩個參數的意義在於:hard是一個上限在運行期間不可以調大但可以調小,而soft只能比hard小,在運行期間可以調大。 我也用mysqld測試過,它讀取的是soft值,但是我又不解的是,既然修改/etc/security/limits.conf要重新login才生效,那麼這些修改的意義不就沒有了嗎?因為要重新登錄,意味著服務要重啟
應該是這個解釋比較合理,soft是一個警告值,而hard則是一個真正意義的閥值,超過就會報錯。