Linux资源使用配置文件 /etc/security/limits.conf


这个文件主要是用来限制用户对系统资源的使用,具体的使用方法 man 5 limits.conf,里面便给出了详细的用法

  1. user@db-2:~$ cat /etc/security/limits.conf   
  2. # /etc/security/limits.conf  
  3. #  
  4. #Each line describes a limit for a user in the form:  
  5. #  
  6. #<domain>        <type>  <item>  <value>  
  7. #  
  8. #Where:  
  9. #<domain> can be:  
  10. #        - an user name  
  11. #        - a group namewith @group syntax  
  12. #        - the wildcard *, for default entry  
  13. #        - the wildcard %, can be also used with %group syntax,  
  14. #                 for maxlogin limit  
  15. #  
  16. #<type> can have the two values:  
  17. #        - "soft" for enforcing the soft limits # 软限制,必须必硬限制的值要小  
  18. #        - "hard" for enforcing hard limits     # 硬限制  
  19. #  
  20. #<item> can be one of the following:  
  21. #        - core - limits the core file size (KB)  
  22. #        - data - max data size (KB)  
  23. #        - fsize - maximum filesize (KB)  
  24. #        - memlock - max locked-in-memory address space (KB)  
  25. #        - nofile - max number of open files    # 最大打开的文件数(以文件描叙符,file descripter计数)  
  26. #        - rss - max resident set size (KB)  
  27. #        - stack - max stack size (KB)  
  28. #        - cpu - max CPU time (MIN)  
  29. #        - nproc - max number of processes  
  30. #        - as - address space limit  
  31. #        - maxlogins - max number of logins for this user  
  32. #        - maxsyslogins - max number of logins on the system  
  33. #        - priority - the priority to run user process with  
  34. #        - locks - max number of file locks the user can hold  
  35. #        - sigpending - max number of pending signals  
  36. #        - msgqueue - max memory used by POSIX message queues (bytes)  
  37. #        - nice - max nice priority allowed to raise to  
  38. #        - rtprio - max realtime priority  
  39. #  
  40. #<domain>      <type>  <item>         <value>  
  41. #  
  42. root            soft    nofile          200000  
  43. root            hard    nofile          200000  
  44. admin           hard    nofile          65536  
  45. admin           soft    nofile          65536  
  46. 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则是一个真正意义的阀值,超过就会报错。

相关内容