linux 设置 limits nofile 和 nproc参数

3,270 阅读3分钟

一、临时配置

重新登录或重启后失效

ulimit -HSn 165536
ulimit -HSu 102400

二、永久配置

配置到配置文件/etc/security/limits.conf或者 /etc/security/limits.d/ 中。
然后重启服务器或重新登录即可生效,如果不成功请看:Linxu修改/etc/security/limits.conf不生效办法

etc/security/limits.d/(20|90)-nproc.conf 与 /etc/security/limits.conf 都是系统用户资源配置文件,两者有什么区别?

CentOS7 中 /etc/security/limits.d/20-nproc.conf(CentOS6是90-nproc.conf)会覆盖 /etc/security/limits.conf 中的 nproc 配置,前提条件是:(20|90)-nproc.conf 和limits.conf 的 domain 相同或者 (20|90)-nproc.conf 比 limits.conf 的 domain 更具体,优先级从高到低分别为:

/etc/security/limits.d/(20|90)-nproc.conf中domain为具体用户的配置
/etc/security/limits.conf中domain为具体用户的配置
/etc/security/limits.d/(20|90)-nproc.conf中domain为通配符(*)的配置
/etc/security/limits.conf中domain为通配符(*)的配置

注意

1. 配置,只能被特定覆盖。
2. domain 中具体用户比通配符(*)优先级高。
3. soft 和 hard 需要都进行设置,才能生效。
4. /etc/security/limits.d/ 下文件的相同配置可以覆盖 /etc/security/limits.conf
5. soft 设置的值 一定要小于或等于 hard 的值,如果配置文件中软限制设置的比硬限制高,则软限制会使用硬限制的值。
6. nofile 不能设置 unlimited,可以设置的最大值为 1048576(2**20),设置的值大于该数,就会进行登录不了。
7. 通过 ulimit 命令设置软限制不能超过硬限制。
8. root 用户可以通过 ulimit 命令降低和提高硬限制(nofile 的值不能超过 /proc/sys/fs/nr_open 的值)。
9. 非root用户可以通过ulimit命令降低硬限制,但不可以通过ulimit命令提高硬限制。

配置命令:

echo -e "* hard nproc 102400\n* soft nproc 102400\n* hard nofile 165536\n* soft nofile 165536" >> /etc/security/limits.d/90-nproc.conf 

三、ulimit 常用命令

      -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

查看所有的配置

ulimit  -a

查看配置的最大打开文件数

ulimit  -n

参考文档:

/etc/security/limits.conf 详解与配置
linux设置nofile和nproc参数