5
0

Linux系统资源参数优化

2026-09-04
Linux系统资源参数优化

ssh 登录、su、本地终端登录的用户会读取下面这个文件

[root@centos-1 ~]# cat /etc/security/limits.conf 
# /etc/security/limits.conf
#
#This file sets the resource limits for the users logged in via 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.
#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.
#
#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
#
#<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 file descriptors
#        - 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
#
#<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
#@student        -       maxlogins       4

# End of file
[root@centos-1 ~]# 

该文件用于给登录进来的 Linux 用户设置资源上限,防止某个用户把服务器资源耗尽。这个文件的配置内容对systemd管理的后台服务是不生效的。

[root@centos-1 ~]# ll /etc/security/limits.d/
total 4
-rw-r--r--. 1 root root 191 Apr 10  2018 20-nproc.conf
[root@centos-1 ~]# 

/etc/security/limits.d/*.conf 目录下的文件优先级更高,如果 domain 相同会覆盖 limits.conf 的设置

使用ulimit -a查看:

[root@centos-1 ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 28948
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 28948
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@centos-1 ~]# 

其中大部分设置是默认状态的,有几个常用的设置:

open files                      (-n) 1024      # 单进程最大打开文件数,默认1024(高并发容易爆)
max user processes              (-u) 28948    # 用户最大进程数
max locked memory       (kbytes, -l) 64        # 可锁定内存,es需要调大
core file size          (blocks, -c) 0         # core转储关闭
stack size              (kbytes, -s) 8192      # 栈大小


#open files 和 max user processes使用更加多一些,其余的按需更改

例如更改单进程最大打开文件数:

[root@centos-1 ~]# ulimit -n 65535
[root@centos-1 ~]# 
#这是临时修改,只是针对当前登录的终端进行的,不是永久性修改配置,当退出当前终端后,再次登录,依旧是默认值

永久更改方式:

#打开/etc/security/limits.conf 配置文件,在后面添加配置信息,以更改单进程最大打开文件数为例子:
* soft nofile 65535 #软限制
* hard nofile 65535 #硬限制

#ps: 一般软限制和硬限制保持一致,不让用户有调整空间,直接锁死上限

* - nofile 65535
# 等价于
* soft nofile 65535
* hard nofile 65535

hard:内核设置的最高天花板,不能突破;soft:日常生效值,可以在天花板以内调整

硬限制 hard:

内核强制的最大上限,是资源的天花板。

普通用户不能调高硬限制,只能往小改;只有 root 可以提高 hard 值。

程序无论如何都不能超过 hard,超过直接报错。

软限制 soft:

系统当前实际生效的限制,程序日常运行受这个约束。

普通用户可以自己把 soft 调高,但最高不能超过 hard 的值。

当程序触及 soft 限制,会触发警告 / 报错。

配置文件更改完成后,exit退出当前终端重新登录,命令检查发现配置生效,永久生效。

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或者给予支持!