京东6.18大促主会场领京享红包更优惠

 找回密码
 立即注册

QQ登录

只需一步,快速开始

/etc/security/limits.conf详解与设置过程

2024-11-3 00:55| 发布者: c2688| 查看: 57| 评论: 0

摘要: 目录一、 /etc/security/limits.conf详解/etc/security/limits.conf 设置剖析/etc/security/limits.d/目录二、 ulimit 如何设置设置注意事项注意覆盖点的问题示例一示例二三、ulimit 设置后见效临时设置永世设置设置
目录

[code]/etc/security/limits.conf[/code] 文件现实是 [code]Linux PAM[/code](插入式认证模块,Pluggable Authentication Modules)中 [code]pam_limits.so[/code] 的设置文件,而且只针对于单个会话。

该设置不会影响体系服务的资源限制。

还要注意 [code]/etc/security/limits.d/[/code] 的这个目录.

一、 /etc/security/limits.conf详解

/etc/security/limits.conf 设置剖析

[code]# /etc/security/limits.conf # #This file sets the resource limits for the users logged in via PAM. 该文件为通过PAM登录的用户设置资源限制。 #It does not affect resource limits of the system services. #它不影响体系服务的资源限制。 #Also note that configuration files in /etc/security/limits.d directory, #which are read in alphabetical order, override the settings in this #file in case the domain is the same or more specific. 请注意/etc/security/limits.d下按照字母次序分列的设置文件会覆盖 /etc/security/limits.conf中的 domain相同的的设置 #That means for example that setting a limit for wildcard domain here #can be overriden with a wildcard setting in a config file in the #subdirectory, but a user specific setting here can be overriden only #with a user specific setting in the subdirectory. 这意味着,例如使用通配符的domain会被子目录中相同的通配符设置所覆盖,但是某一用户的特定设置 只能被字母路中用户的设置所覆盖。实在就是某一用户A如果在/etc/security/limits.conf有设置,当 /etc/security/limits.d子目录下设置文件也有效户A的设置时,那么A中某些设置会被覆盖。最终取的值是 /etc/security/limits.d 下的设置文件的设置。 # #Each line describes a limit for a user in the form: #每一行形貌一个用户设置 #<domain> <type> <item> <value> #Where: #<domain> can be: # - a user name 一个用户名 # - a group name, with @group syntax 用户组格式为@GROUP_NAME # - 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 有soft,hard和-,soft指的是当前体系见效的设置值,软限制也可以理解为告诫值。 hard表名体系中所能设定的最大值。soft的限制不能比hard限制高,用-表名同时设置了soft和hard的值。 #<item> can be one of the following: <item>可以使以下选项中的一个 # - 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 file descriptors 最大打开的文件数(以文件描叙符,file descripter计数) # - rss - max resident set size (KB) 最大长期设置大小 # - stack - max stack size (KB) 最大栈大小 # - cpu - max CPU time (MIN) 最多CPU占用时间,单位为MIN分钟 # - nproc - max number of processes 进程的最大数目 # - as - address space limit (KB) 地址空间限制 # - 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 values: [-20, 19] max nice优先级允许提升到值 # - rtprio - max realtime pr iority # #<domain> <type> <item> <value> # #* soft core 0 #* hard rss 10000 #@student hard nproc 20 #@faculty soft nproc 20 #@faculty hard nproc 50 #ftp hard nproc 0 #@st[/code]

/etc/security/limits.d/目录

[code]/etc/security/limits.d/ [/code]目录

该目录下默认有 [code]*-nproc.conf[/code] 文件,该文件是用于限制用户的线程限制。我们也可以在该目录创建设置文件在 [code]/etc/security/limits.d/[/code] 下,以 [code].conf[/code] 末端。

centos 7

在CentOS 7版本中为/etc/security/limits.d/20-nproc.conf

[code]# Default limit for number of user's processes to prevent # accidental fork bombs. # See rhbz #432903 for reasoning. * soft nproc 4096 # 全部的用户默承认以打开最大的进程数为 4096 root soft nproc unlimited # root 用户默承认以打开最大的进程数 无穷制的。[/code]
  • CentOS 6

在[code]CentOS 6[/code]版本中为[code]/etc/security/limits.d/90-nproc.conf[/code]

二、 ulimit 如何设置

设置注意事项

注意不能设置 [code]nofile[/code]不能设置 [code]unlimited[/code],[code]noproc[/code]可以.

当我们设置了 [code]nofile[/code]不能设置 [code]unlimited[/code] 后,我们进行 [code]ssh[/code] 登录,是登录不了的,而且报错下面的内容。

一般我们必要设置的 [code]/etc/security/limits.d/20-nofile.conf[/code] 为。

[code]root soft nofile 65535 root hard nofile 65535 * soft nofile 65535 * hard nofile 65535[/code]

[code]/etc/security/limits.d/20-nproc.conf[/code]设置为

[code]* - nproc 65535 root soft nproc unlimited root hard nproc unlimited[/code]

注意覆盖点的问题

示例一

当 [code]/etc/security/limits.conf[/code]设置了:

[code]root soft nofile 65538 root hard nofile 65538 * soft nofile 65539 * hard nofile 65539[/code]

这个[code]root [/code]用户的 默认取值是 [code]65538[/code] ,[code]*[/code] 统配符固然在 [code]root[/code] 设置后面,但是 [code]root[/code] 的设置只能被 [code]root[/code] 进行覆盖。

我们看下这个设置,当如许设置的时间

[code]root soft nofile 65538 root hard nofile 65538 * soft nofile 65539 * hard nofile 65539 root soft nofile 65539[/code]

这个的 [code]root [/code]用户的取值还是 [code]65538[/code],因为固然 [code]root soft nofile 65539[/code] 会覆盖我们之前的设置,但是这个设置是不见效的。

因为 [code]root soft nofile 65539[/code] 设置的值大于[code]root hard nofile 65538[/code] , [code]soft[/code] 设置的值不能大于 [code]hard[/code].

示例二

当我们在 [code]/etc/security/limits.conf[/code] 设置了:

[code]root soft nofile 65538 root hard nofile 65538 * soft nofile 65539 * hard nofile 65539[/code]

然后我们在 [code]/etc/security/limits.d/20-nofile.conf[/code]设置了:

[code]root soft nofile 65536 root hard nofile 65536 * soft nofile 65540 * hard nofile 65540[/code]

最后的取值是会取 [code]/etc/security/limits.d/20-nofile.conf[/code]内里的值。

  • 设置,只能被特定覆盖。
  • /etc/security/limits.d/ 下文件的相同设置可以覆盖 /etc/security/limits.conf
  • soft和hard必要都进行设置,才气见效。
  • nofile不能设置 unlimited
  • nofile可以设置的最大值为 1048576(2**20),设置的值大于该数,就会进行登录不了。
  • soft 设置的值 肯定要小于或即是 hard 的值。
  • 详细详细设置根据应用情况进行设置。

三、ulimit 设置后见效

临时设置

设置可以打开文件的最大数为 [code]65536[/code]

[code]ulimit -SHn 65536[/code]

重启后失效。

永世设置

设置到设置文件[code]/etc/security/limits.conf[/code]或者 [code]/etc/security/limits.d/[/code] 中。

然后退出当前会话,重新登录。 即可见效,重启设置也会保留。

设置不见效的问题

四、ulimit 常用下令

[code] -S use the `soft` resource limit # 设置软限制 -H use the `hard` resource limit # 设置硬限制 -a all current limits are reported# 显示全部的设置。 -b the socket buffer size # 设置socket buffer 的最大值。 -c the maximum size of core files created # 设置core文件的最大值. -d the maximum size of a process's data segment # 设置线程数据段的最大值 -e the maximum scheduling priority (`nice') # 设置最大调度优先级 -f the maximum size of files written by the shell and its children # 创建文件的最大值。 -i the maximum number of pending signals # 设置最大的等待信号 -l the maximum size a process may lock into memory #设置在内存中锁定进程的最大值 -m the maximum resident set size -n the maximum number of open file descriptors # 设置最大可以的打开文件形貌符。 -p the pipe buffer size -q the maximum number of bytes in POSIX message queues -r the maximum real-time scheduling priority -s the maximum stack size -t the maximum amount of cpu time in seconds -u the maximum number of user processes # 设置用户可以创建的最大进程数。 -v the size of virtual memory # 设置假造内存的最大值 -x the maximum number of file locks[/code]

查看设置

  • 查看全部的设置
[code]ulimit -a[/code]
  • 查看设置的最大打开文件数
[code]ulimit -n[/code]
  • 更改设置
[code]ulimit -SHn 65536[/code]

总结

以上为个人经验,希望能给各人一个参考,也希望各人多多支持脚本之家。


来源:https://www.jb51.net/server/3284329l9.htm
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
关闭

站长推荐上一条 /6 下一条

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2025-7-1 19:06 , Processed in 0.030088 second(s), 18 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2025 Discuz! Team.

返回顶部