Skip to main content

LIMIT

  • linux-pam 提供
    • /etc/pam.d/su
    • session required pam_limits.so
  • /etc/security/limits.conf - 全局配置
  • /etc/security/limits.d/ - 通常用于配置单个进程
  • 参考
tip
  • limits 针对每个进程生效 - 进程可修改 soft 值,但小于 hard 值
  • open files 默认 1024/4096 - 对于服务端应用来说太小了
# 查看其他进程的 limits
cat /proc/1/limits

# 修改已运行进程
# util-linux-2.21
prlimit --pid $(pidof prometheus) --nofile=65535:65535

cat /proc/sys/fs/file-max # 最大 fd 19778411
cat /proc/sys/fs/nr_open # 1048576 - 1024*1024 - nofile hard limit 最大值
cat /proc/sys/fs/file-nr # 已分配 0 最大

# 19778411
# 26367207
sysctl fs.file-max
# sysctl -w fs.file-max=19778411

ulimit -n # soft limit
ulimit -Hn # hard limit

ulimit -n 4096 # 临时修改
conf/itemtypeunitflagvalueflavor
real-time non-blocking timemicroseconds-Runlimited
corecore file sizeblocks-c0
datadata seg sizekbytes-dunlimited
nicescheduling priority-e0
fsizefile sizeblocks-funlimited
sigpendingpending signals-i63510linux
memlockmax locked memorykbytes-l64
max memory sizekbytes-munlimited
nofileopen files-n1024
pipe size512 bytes-p8
msgqueuePOSIX message queuesbytes-q819200linux
rtprioreal-time priority-r0linux
stackstack sizekbytes-s8192
cpu timeseconds-tunlimited
nprocmax user processes-u63510
virtual memorykbytes-vunlimited
locksfile locks-xunlimitedlinux
maxloginslogins for user
maxsysloginsall logins on system
priority
rsslinux < 2.4.30
asaddress space limitkbytes

/proc/1/limits

Limit                     Soft Limit           Hard Limit           Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 30464 30464 processes
Max open files 1024 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 30464 30464 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us

limits.conf

  • /etc/security/limits.conf
<domain><type><item><value>
limits.conf
*               soft    core            0
* hard nofile 512
@student hard nproc 20
@faculty soft nproc 20
@faculty hard nproc 50
ftp hard nproc 0
@student - maxlogins 4
:123 hard cpu 5000
@500: soft cpu 10000
600:700 hard locks 10
  • domain
    • username
    • @group
    • * - 默认
    • % - maxlogins
    • <min_uid>:<max_uid>
    • @<min_gid>:<max_gid>
    • %:<gid>
  • type
    • hard
    • soft
    • - - soft+hard
  • value
    • -1, unlimited, infinity - 无限制

建议

/etc/security/limits.d/default.conf
* hard nofile 65536
* soft nofile 4096
kubernetes-nobody.conf
nobody soft nofile 40960

统计

# nproc
ps h -Led -o user | sort | uniq -c | sort -n

SSH limits 不生效

sshd_config

UsePrivilegeSeparation no

UsePAM yes

Unsupported option UsePAM

apk add openssh-server-pam
sudo service sshd restart

open files

  • 1073741816
  • 1048576

debug

strace -o loglimit su - user01