在 Ubuntu 中修改 ulimit(用户资源限制)有多种方式,取决于你是临时生效还是永久生效,以及作用范围是当前 shell / 用户 / 系统。
ulimit -a
查看某一项,比如最大打开文件数:
ulimit -n
ulimit -n 65535
ulimit -u 4096
常用参数:
-n:最大打开文件数(file descriptors)-u:最大用户进程数-s:栈大小-c:core 文件大小⚠️ 只对当前终端有效,重启或新开 shell 失效。
/etc/security/limits.confsudo vim /etc/security/limits.conf
示例(用户 ubuntu):
ubuntu soft nofile 65535
ubuntu hard nofile 65535
ubuntu soft nproc 4096
ubuntu hard nproc 4096
说明:
soft:软限制(可临时超过,但会被限制)hard:硬限制(不能超过)nofile:打开文件数nproc:进程数⚠️ 需要 重新登录 才生效。
检查:
grep pam_limits /etc/pam.d/common-session
应包含:
session required pam_limits.so
如果没有,可添加。
如果你修改了 limits.conf,但服务仍不生效,多半是因为使用了 systemd。
编辑服务:
sudo systemctl edit xxx.service
添加:
[Service]
LimitNOFILE=65535
LimitNPROC=4096
然后:
sudo systemctl daemon-reload
sudo systemctl restart xxx
查看是否生效:
cat /proc/<pid>/limits
sudo prlimit --pid $$ --nofile=65535:65535
limits.conf 对 root 一般不限制,需显式写:
root soft nofile 65535
root hard nofile 65535
需要在宿主机或 docker run 中加:
--ulimit nofile=65535:65535
如果你能告诉我:
我可以给你精确配置示例。