linux修改limits.conf不生效

正常情况下, /etc/security/limits.conf 的改动,重新登录就可以生效, 我遇到的问题最后的解决方案是重启虚拟机解决了,也参考了很多网上的文章,整理记录一下

一、修改方法

1.临时方法

为了优化linux性能,可能需要修改这个最大值。临时修改的话ulimit -n 204800就可以了,重启后失效。

2.永久生效方法

(1)、修改/etc/security/limits.conf文件 在文件末尾添加如下内容,然后重启服务器或重新登录即可生效。重启后用查看命令进行查看,如果显示数字等于204800即证明修改成功。如果不成功请继续往下看。
  • 查看命令:(-n是可以打开最大文件描述符的数量。 -u是用户最大可用的进程数。)
ulimit -n
ulimit -u
  • 修改配置命令:vim /etc/security/limits.conf 如下
* soft nofile 204800
* hard nofile 204800
* soft nproc 204800
* hard nproc 204800

我在解决这个问题时候,发现网上都是说修改这一个文件就行,但是我修改后重启服务器发现没更改过来。
后来发现,还需要修改两个文件。如果你重启后也没生效,请继续跟着往下修改。

在修改下面(2)、(3)所示的这些文件时候,有些文件可能并不存在,或者90-nproc.conf没有,但是有20-nproc.conf,建议从根路径下,自己亲自cd进去。然后一步步找到目标文件。如果90-nproc.conf不存在,修改类似的文件即可,即xx-nproc.conf

(2)、/etc/security/limits.d/90-nproc.conf文件尾添加
* soft nproc 204800
* hard nproc 204800
(3)、/etc/security/limits.d/def.conf文件尾添加
* soft nofile 204800
* hard nofile 204800

二、不生效原因及解决方案

  1. 确保 pam 生效
    在 /etc/pam.d/login 中,存在:
session required pam_limits.so
  1. 确保 ssh 使用 pam
    在 /etc/pam.d/sshd 中,存在:
session required pam_limits.so

在 /etc/ssh/sshd_config 中, 存在:

UsePAM yes
  1. limits.conf 建议不要使用星号
    官方 manual 以及网上的教程有很多都用了 * 符号,然而不是所有系统都认的,比如我在解决这个问题时候,发现ubuntu系统就不支持。最前面一列代表用户名即root和evan。

不兼容方式:

* soft nofile 204800
* hard nofild 204800

兼容方式

root soft nofile 204800
root hard nofile 204800
es soft nofile 204800
es hard nofile 204800

参考:

https://evanwang.blog.csdn.net/article/details/103706237?utm_term=/etc/security/limits修改不生效&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allsobaiduweb~default-0-103706237&spm=3001.4430
https://blog.csdn.net/xinqiku/article/details/103312021
https://support.huaweicloud.com/intl/zh-cn/trouble-ecs/ecs_trouble_0354.html

posted @ 2022-10-21 09:30  xiao智  阅读(1562)  评论(0编辑  收藏  举报