SSH 登陆 limits 配置不生效解决办法

发布时间 2023-12-18 15:17:03作者: 热气球!

参考文档

SSH 登陆 limits 配置不生效解决办法 - 自由早晚乱余生 - 博客园 (cnblogs.com)

环境信息

公司测试环境(涉及敏感信息补贴出具体IP)

现象描述

配置/etc/security/limits.conf后退出shell 并重新登录,执行ulimit -n但显示时仍然是1024,配置并没有生效。

配置文件如下

/etc/security/limits.d/20-nproc.conf

[root@server203 ~]# cat /etc/security/limits.d/20-nproc.conf 
# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.

*          soft    nproc     4096
root       soft    nproc     unlimited

/etc/security/limits.conf

[root@server203 ~]# cat /etc/security/limits.conf
*               hard    nofile          65536
*               soft    nofile          65536
root            hard    nofile          65536
root            soft    nofile          65536
*               soft    nproc           65536
*               hard    nproc           65536
root            soft    nproc           65536
root            hard    nproc           65536
*               soft    core            65536
*               hard    core            65536
root            soft    core            65536
root            hard    core            65536

解决方法

检查sshd 服务的 PAM 模块是不是没有开启

/etc/security/limits.conf 文件实际是 Linux PAM(插入式认证模块,Pluggable Authentication Modules)中 pam_limits.so 的配置文件,我们没有开启 PAM 模块,最终也就没有读取到 /etc/security/limits.conf 的内容。 引用自SSH 登陆 limits 配置不生效解决办法 - 自由早晚乱余生 - 博客园 (cnblogs.com)

/etc/ssh/sshd_configUsePAM no 更改为 UsePAM yes, 然后重启 sshd 服务。

grep 'UsePAM' /etc/ssh/sshd_config
UsePAM yes

systemctl restart sshd
# exit并重新登录机器
ulimit -n 
65536