默认的 ulimit 值在哪里设置?(Linux,centos)

nom*_*sir 38 linux centos ulimit

我有两台配置几乎相同的 CentOS 5 服务器。当我登录并执行时ulimit -u,在一台机器上我得到unlimited,而在另一台机器上我得到77824

当我运行一个 cron 时:

* * * * * ulimit -u > ulimit.txt
Run Code Online (Sandbox Code Playgroud)

我得到相同的结果 ( unlimited, 77824)。

我正在尝试确定这些设置的位置,以便我可以更改它们。他们不是在我的任何配置文件(设置.bashrc/etc/profile等等)。这些无论如何都不会影响 cron )或 in /etc/security/limits.conf(这是空的)。

我已经搜索了谷歌,甚至做了这么多grep -Ir 77824 /,但到目前为止没有任何结果。我不明白这些机器怎么会预设有不同的限制。

我实际上想知道的不是这些机器,而是另一台(CentOS 6)机器,它的限制1024太小了。我需要以更高的限制运行 cron 作业,我知道如何设置它的唯一方法是在 cron 作业本身中。没关系,但我宁愿将它设置为系统范围,这样它就不会那么笨拙了。

谢谢你的帮助。这似乎应该很容易(不是)。


编辑 - 已解决

好的,我想通了。这似乎是 CentOS 6 或我的机器配置的问题。在 CentOS 5 配置上,我可以设置/etc/security/limits.conf

* - nproc unlimited
Run Code Online (Sandbox Code Playgroud)

这将有效地更新帐户和 cron 限制。但是,这在我的 CentOS 6 机器中不起作用。相反,我必须这样做:

myname1 - nproc unlimited
myname2 - nproc unlimited
...
Run Code Online (Sandbox Code Playgroud)

事情按预期进行。也许 UID 规范适用,但通配符 (*) 绝对不在这里。奇怪的是,通配符确实适用于nofile限制。

我仍然很想知道默认值实际上来自哪里,因为默认情况下,这个文件是空的,我不明白为什么我对两个 CentOS 机器有不同的默认值,它们具有相同的硬件并且来自同一个提供商.

Tot*_*tor 52

这些“默认”限制适用于:

  • Linux内核启动时(到initsystemd过程)
  • 继承,从父进程的限制(在fork(2)时间),
  • 用户会话打开时的PAM(可以替换内核/继承值),
  • systemd,尤其是它管理的流程,
  • 过程本身(可以取代PAM&内核/继承值,参见setrlimit(2))。

普通用户的进程不能上升硬限制。

Linux内核

在启动时,Linux 为init(或systemd)进程设置默认限制,然后所有其他(子)进程继承这些限制。要查看这些限制:cat /proc/1/limits.

例如,内核默认的最大文件描述符数( ulimit -n) 是 1024/1024(软、硬),并且已经提高在 Linux 2.6.39 中到 1024/4096。

您所谈论的默认最大进程数限制为大约:

Total RAM in kB / 128
Run Code Online (Sandbox Code Playgroud)

对于 x86 架构(至少),但发行版有时会更改默认内核值,因此请检查内核源代码中的kernel/fork.c, fork_init(). “进程数”限制在那里称为 RLIMIT_NPROC。

聚丙烯酰胺

通常,为了确保登录时的用户身份验证,PAM 与一些模块一起使用(请参阅 参考资料/etc/pam.d/login)。

在 Debian 上,负责设置限制的 PAM 模块在这里:/lib/security/pam_limits.so

该库将读取配置/etc/security/limits.conf/etc/security/limits.d/*.conf,但即使这些文件是空的,pam_limits.so可能会使用硬编码的值,你可以在源代码中检查。

例如,在 Debian 上,库已打补丁,因此默认情况下,最大进程数( nproc) 是无限的,最大文件数( nofile) 为 1024/1024:

  case RLIMIT_NOFILE:
      pl->limits[i].limit.rlim_cur = 1024;
      pl->limits[i].limit.rlim_max = 1024;
Run Code Online (Sandbox Code Playgroud)

因此,请检查您的 CentOS 的 PAM 模块源代码(查找 RLIMIT_NPROC)。

但是,请注意,许多进程不会通过 PAM(通常,如果它们不是由登录用户启动的,例如守护进程和 cron 作业)。

系统

如今,它systemd被广泛使用,它可以替代init并配置特定的限制值,尤其是对其管理和创建的进程/守护程序。

它默认使用的一些限制可以在/etc/systemd/system.conf. 文档中提供了更多信息。


小智 15

在 RHEL6 (CentOS6) 上, “最大用户进程”默认设置为 1024。
您可以在文件中更改此值:

/etc/security/limits.d/90-nproc.conf
Run Code Online (Sandbox Code Playgroud)

如果您想投诉,请参阅https://bugzilla.redhat.com/show_bug.cgi?id=432903 :)


小智 6

互联网上关于这方面的信息很糟糕,这是我为 debian linux 制作的limits.conf 文件,显示了所有可能的选项及其最大“安全”限制,相应地进行调整。

这些是您可以设置的最高值,有些东西被散列了,激活这些会导致您出错并且无法登录到您的控制台,修改注释掉的选项需要您自担风险,但您不应该需要(默认为无限制)大多数)

我希望这对某人有用,因为我在任何地方都找不到此信息,对此文件进行了 4 小时的研究。

==== FILE START =====
# /etc/security/limits.conf
# 
#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
#- the wildcard     *, for default entry
#- the wildcard %, can be also used with %group syntax,
#         for maxlogin limit
#- NOTE: group and wildcard limits are not applied to     root.
#  To apply a limit to the     root user, <domain> must be
#  the literal username     root.
#
#<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
#- 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 (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]
#- rtprio - max realtime priority
#- chroot - change     root to directory (Debian-specific)
#
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#root            hard    core            100000
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#ftp             -       chroot          /ftp
#@student        -       maxlogins       4

# -- Defaults:
#(core) core file size                (blocks, -c) 0 (ulimit -Hc or -Sc)
#(data) data seg size                  (bytes, -d) unlimited
#(priority) scheduling priority               (-e) 0
#(fsize) file size                    (blocks, -f) unlimited
#(sigpending) pending signals                 (-i) 378197
#(memlock) max locked memory          (kbytes, -l) 64
# max memory size                     (kbytes, -m) unlimited
#(nofile) open files                          (-n) 65536
# pipe size                        (512 bytes, -p) 8
#(msgqueue) POSIX message queues       (bytes, -q) 819200
#(rtprio) real-time priority                  (-r) 0
#(stack) stack size                   (kbytes, -s) 8192
#(cpu) cpu time                      (seconds, -t) unlimited
#(nproc) max user processes                   (-u) 378197
# virtual memory                      (kbytes, -v) unlimited
#(locks) file locks                           (-x) unlimited

# --     root Limits:
root               -    core            -1
root               -    data            -1
root               -    fsize           -1
root               -    memlock         -1
root               -    nofile          999999
root               -    stack           -1
root               -    cpu             -1
root               -    nproc           -1
root               -    priority        0
root               -    locks           -1
root               -    sigpending      -1
root               -    msgqueue        -1
root               -    rtprio          -1
root               -    maxlogins       -1
root               -    maxsyslogins    -1
#root               -    rss             -1
#root               -    as              -1
#root               -    nice            0
#root               -    chroot          -1

#All Users:
# -- Hard Limits
*               hard    core            -1
*               hard    data            -1
*               hard    fsize           -1
*               hard    memlock         -1
*               hard    nofile          999999
*               hard    stack           -1
*               hard    cpu             -1
*               hard    nproc           -1
*               hard    priority        0
*               hard    locks           -1
*               hard    sigpending      -1
*               hard    msgqueue        -1
*               hard    rtprio          -1
*               hard    maxlogins       -1
*               hard    maxsyslogins    -1
#*               hard    rss             -1
#*               hard    as              -1
#*               hard    nice            0
#*               hard    chroot          -1

# -- Soft Limits
*               soft    core            -1
*               soft    data            -1
*               soft    fsize           -1
*               soft    memlock         -1
*               soft    nofile          999999
*               soft    stack           -1
*               soft    cpu             -1
*               soft    nproc           -1
*               soft    priority        0
*               soft    locks           -1
*               soft    sigpending      -1
*               soft    msgqueue        -1
*               soft    maxlogins       -1
*               soft    maxsyslogins    -1
*               soft    rtprio          -1
#*               soft    rss             -1
#*               soft    as              -1
#*               soft    nice            0
#*               soft    chroot          -1

#randomuser:
# -- Soft Limits
randomuser           soft    core            -1
randomuser           soft    data            -1
randomuser           soft    fsize           -1
randomuser           soft    memlock         -1
randomuser           soft    nofile          999999
randomuser           soft    stack           -1
randomuser           soft    cpu             -1
randomuser           soft    nproc           -1
randomuser           soft    priority        0
randomuser           soft    locks           -1
randomuser           soft    sigpending      -1
randomuser           soft    msgqueue        -1
randomuser           soft    maxlogins       -1
randomuser           soft    maxsyslogins    -1
randomuser           soft    rtprio          -1
#randomuser           soft    rss             -1
#randomuser           soft    as              -1
#randomuser           soft    nice            0
#randomuser           soft    chroot          -1

# End of file
Run Code Online (Sandbox Code Playgroud)


jam*_*ate 1

它似乎是/etc/security/limits.conf

http://ss64.com/bash/limits.conf.html