一次修改limits.conf 引发的血案

本文详细解析在Linux环境下调整Nginx openfile限制时遇到的问题及解决方案,探讨limits.conf与内核参数之间的关系,避免sudo权限错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

起因:
在重启nginx 过程中,发现有报 open file 限制的警告,于是没考虑太多,直接去修改/etc/security/limits.conf

修改前

*                soft    nofile          65536
*                hard    nofile          65536

修改后

*                soft    nofile          6553600
*                hard    nofile          6553600

保存退出,发现无论是复制窗口还是重新登录都会失败,因为是使用的普通账号,在执行sudo 和su 的时候也会报错如下

sudo: pam_open_session: Permission denied
sudo: policy plugin failed session initialization

原因:

/etc/security/limits.conf 文件中nofile 的软硬限制的值不能超过内核参数 /proc/sys/fs/nr_open ,否则就有问题

解决方案

  1. 如果已经重启启动,那没有没法了,只能重启,进入单用户模式,然后将limits.conf 修改为正常的
  2. 如果只有一个普通账号还登录着,也没有办法了,因为sudo也用不了,只能重启,单用户
  3. 如果还有会话使用root 用户登录着,可以直接用root修改回来,或者root把 /proc/sys/fs/nr_open 内核参数调大
关于linux上默认资源限制的一些参考知识
ulimit

Provides control over the resources available to the shell and to processes started by
it, on systems that allow such control. The -H and -S options specify that the hard or
soft limit is set for the given resource. A hard limit cannot be increased by a non-root
user once it is set; a soft limit may be increased up to the value of the hard limit. If
neither -H nor -S is specified, both the soft and hard limits are set. The value of
limit can be a number in the unit specified for the resource or one of the special values
hard, soft, or unlimited, which stand for the current hard limit, the current soft limit,
and no limit, respectively. If limit is omitted, the current value of the soft limit of
the resource is printed, unless the -H option is given. When more than one resource is
specified, the limit name and unit are printed before the value

ulimit 是用来限制用户在当前会话的资源限制的,若新开一个会话,则用户的会话级资源限制又会回复到默认值。如果要想让用户的资源限制的修改同步到用户的每个限制,有两种做法

  • 写入用户家目录的 profile or bashrc 中
  • 修改limits.conf
limits.conf

通过 man limits.conf 查看limits.conf 文件介绍

The pam_limits.so module applies ulimit limits, nice priority and number of simultaneous login
sessions limit to user login sessions. This description of the configuration file syntax applies
to the /etc/security/limits.conf file and *.conf files in the /etc/security/limits.d directory.

由此可见,limits.conf 的修改会作用到pam_limits.so ,继而影响所有 login session ,我们使用sudo , 复制会话都会打开一个login session,因此,limits.conf 中nofile 的修改需要慎重,虽然根据man 手册,可以设置值为 -1,unlimited or infinity indicating no limit,但如果设置为-1,则会出现文章之前出现的问题。我这里还只是将nofile的值修改的太大就出现问题。

内核参数

具体的内核参数的解释可以查看http://man7.org/linux/man-pages/man5/proc.5.html

/proc/sys/fs/file-max

  • 操作系统级别的限制,所有进程打开的文件数之和不能超过这个值

/proc/sys/fs/file-nr

  • 操作系统级别的参数,查看当前已经打开的文件数以及操作系统允许的最大值

/proc/sys/fs/nr_open

  • 进程级别的参数,限制每个进程能打开的最大文件数
修改nofile 的正确姿势
  1. 查看操作系统打开文件数总和的最大限制 /proc/sys/fs/file-max
  2. 查看操作系统单个进程打开最大文件数的限制 /proc/sys/fs/nr_open
  3. 在不超过 nr_open 的情况下修改limits.conf 的 软硬限制
  4. 若limits.conf 配置的值已经非常趋于 nr_open ,可以将内核参数 nr_open 设置得更大一点,同理,file-max 也需要设置的足够大。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值