limits.conf设置了nofile,但是ulimit中的openfiles仍然是1024

根据您提供的信息,虽然已在 /etc/security/limits.conf 中设置了 nofile 限制,但 root 用户执行 ulimit -a 显示的 open files 值仍为 1024。以下是原因和解决步骤:

原因分析:

  1. root 用户不受通配符设置影响​​:

    • limits.conf 中,* 代表所有​​非 root 用户​​,对 root 用户无效(官方文档明确指出:“group and wildcard limits are not applied to root”)。
    • 您当前的设置:
      * soft nofile 65535     # 仅作用于非 root 用户
      * hard nofile 1048576   # 仅作用于非 root 用户
  2. ​需要为 root 用户单独配置​​:

    • 必须显式指定 root 用户才能生效。

解决方案:

步骤 1:编辑 /etc/security/limits.conf

# 添加 root 用户的配置(在文件末尾):
root    soft    nofile    65535
root    hard    nofile    1048576

步骤 2:确保 PAM 模块生效

  • 检查 /etc/pam.d/common-session/etc/pam.d/sshd(取决于登录方式)是否包含:
    session required pam_limits.so
    若没有,添加该行并重启服务(如 sudo systemctl restart sshd)。

步骤 3:验证方式

  • ​方式 1:重新登录 root 会话​
    直接退出当前 SSH 会话并重新登录,执行 ulimit -n 检查。

  • ​方式 2:临时修改(不持久)​
    当前会话可临时调整:

    ulimit -n 65535   # 将当前会话的 soft nofile 改为 65535

补充说明:

  • ​systemd 系统的额外配置​​(适用于服务进程):
    若进程由 systemd 管理(如服务),需修改 /etc/systemd/system.conf

    DefaultLimitNOFILE=65535     # soft
    DefaultLimitNOFILESOFT=65535 # hard (可选)

    然后执行:

    sudo systemctl daemon-reload
    sudo systemctl restart service-name
  • ​验证配置来源​​:
    执行以下命令检查当前进程的实际限制:

    cat /proc/$$/limits | grep "Max open files"

结果验证:

完成上述步骤后,重新登录 root 用户并执行:

ulimit -a | grep "open files"
# 或
ulimit -n

应显示新值 65535(soft)或更高。

​关键点​​:* 不覆盖 root 用户,必须显式配置 root!  

 

 

 

* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535

posted @ 2025-07-14 10:46  mofy  阅读(42)  评论(0)    收藏  举报