ulimit修改以及永久修改(limits.conf)
ulimit命令
命令说明
ulimit [-SHacdefilmnpqrstuvx]
参数S:表示软限制,当超过限制值会报警
参数H:表示硬限制,必定不能超过限制值
参数a:将列出所有资源限制
ulimit -a 查看所有可以设置的项目
root@A9300:/etc/security# 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) 15094
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) 15094
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
临时修改
ulimit -HSn 65536
ulimit -c unlimited
永久修改(limits.conf)
/etc/security/limits.conf
# open files (-n)
* soft nofile 65536
* hard nofile 65536
root soft nofile 65536
root hard nofile 65536
# max user processes (-u)
* soft nproc 65565
* hard nproc 65565
root soft nproc 65565
root hard nproc 65565
配置格式(limits.conf)
配置格式如下:
<domain> <type> <item> <value>
<domain>
指定的用户或者组,可以使用通配符 * % 等
<type>
有soft,hard和-,soft指的是当前系统生效的设置值,软限制也可以理解为警告值。
hard表名系统中所能设定的最大值。soft的限制不能比hard限制高,用-表名同时设置了soft和hard的值。
<item>
设置项的名称
<value>
设置项的值
所有配置项
# - 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
script
#!/bin/bash
# 系统
echo 'fs.file-max = 65535' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# 用户 cat /etc/security/limits.conf
sudo tee -a /etc/security/limits.conf << EOF
* hard nofile 65535
* soft nofile 65535
root hard nofile 65535
root soft nofile 65535
* soft nproc 65535
* hard nproc 65535
root soft nproc 65535
root hard nproc 65535
* soft core unlimited
* hard core unlimited
root soft core unlimited
root hard core unlimited
EOF
# Systemd
# cd /etc/systemd/
# grep -rn -F "DefaultLimitNOFILE"
sudo sed -i '/DefaultLimitNOFILE/c DefaultLimitNOFILE=65535' /etc/systemd/*.conf
sudo systemctl daemon-reexec