实战:linux修改open files-2022.1.15
目录
实验环境
这个测试实验当时是在centos7.x上测试的;
实验软件
无
0、基础知识
🍀 soft nofile (告警阈值) 、 hard nofile (最大阈值 )
linux系统默认的open files 为1024 ,如果程序报错 too many open files错误,就是因为open files 数目不够,需要修改ulimit 和file-max。
🍀 /proc/sys/fs/file-max(系统所有进程一共可以打开的文件数量)
man proc,可得到file-max的描述,/proc/sys/fs/file-maxThis file defines a system-wide limit on the number of open files for all processes. (Seealso setrlimit(2), which can be used by a process to set the per-process limit,RLIMIT_NOFILE, on the number of files it may open.) If you get lots of error messagesabout running out of file handles, try increasing this value:即file-max是设置 系统所有进程一共可以打开的文件数量 。同时一些程序可以通过setrlimit调用,设置每个进程的限制。如果得到大量使用完文件句柄的错误信息,是应该增加这个值。也就是说,这项参数是系统级别的。
🍀 /proc/sys/fs/nr_open ( 单个进程可分配的最大文件数 )
🍀 ulimit
Provides control over the resources available to the shell and to processes started by it, on systems that allow such control.即设置当前shell以及由它启动的进程的资源限制。显然,对服务器来说,file-max, ulimit都需要设置,否则就可能出现文件描述符用尽的问题修改。
🍀 查看openfile数量
[root@pxe-hg ~]#ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7183
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024 #openfile数量
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7183
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
#或者也可以用ulimit -n直接查看open file的值
[root@pxe-hg ~]#ulimit -n
65535
🍀 在测试之前,我们先来看下当前机器参数相关参数的默认值
#openfile值为默认1024
[root@pxe-hg ~]#ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7183
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7183
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@pxe-hg ~]#
#/etc/security/limits.conf文件内容已被全部注释掉;
#file-max值默认为180935
[root@pxe-hg ~]#cat /proc/sys/fs/file-max
180935
[root@pxe-hg ~]#sysctl -a|grep file-max
fs.file-max = 180935
1、修改file-max
(1)临时生效
# echo 6553560 > /proc/sys/fs/file-max
# sysctl -w "fs.file-max=6553560"
前面2种重启机器后会恢复为默认值!
(2)永久生效
# vim /etc/sysctl.conf 加入以下内容,重启生效(虽然用syctl -p可以生效,但建议尽量重启使其生效!)
echo "fs.file-max = 6553560" >> /etc/sysctl.conf
#说明:
#/proc/sys/fs/nr_open ( 单个进程可分配的最大文件数 )
#/proc/sys/fs/file-max ( 所有进程可使用的最大文件数 )
😘 以上2种方法均实际测试有效果。
2、修改open files
方法1:临时生效
# ulimit -HSn 102400 //这只是在当前终端有效,退出之后,open files又变为默认值。
方法2:永久生效
(1)
当然也可以写到/etc/profile中,因为每次登录终端时,都会自动执行/etc/profile
(2)#推荐这个方法
# vim /etc/security/limits.conf //加入以下配置,重启即可生效 #注意,这个方法经实际测试,对all用户均生效!!!
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
#说明:
# soft nofile (告警阈值) 、 hard nofile (最大阈值 )
😘 以上2种方法均实际测试有效果。
注意事项
📍 ulimit -HSn 102400命令
注意:为了让一个程序的open files数目扩大,可以在启动脚本前面加上ulimit -HSn 102400命令。但当程序是一个daemon时,可能这种方法无效,因为没有终端。
📍 修改某个程序的ulimit一定要在其启动之前
注意:如果某项服务已经启动,再动态调整ulimit是无效的,特别是涉及到线上业务就更麻烦了。这时,可以考虑通过修改/proc/’程序pid’/limits来实现动态修改。
📍 一般建议同时修改file-max和ulimit的值,并且修改后建议重启下机器
📍 要想让open file值有效果,PAM功能必须开启
经实际测试,要想让open file值有效果,PAM功能必须开启,也就是说/etc/ssh/sshd_config
里的UsePAM yes
必须是开启的才行(如果PAM功能别关闭,那么这个open file值将不会生效,但file-max的值是生效的);
本来默认ssh这个UsePAM功能就是开启的,如果升级ssh版本后,也是要保持这个功能是可用的才行;(特别注意这个事项)
如果配置了open file值后,还是不生效的话,可能就是PAM功能有问题(ssh升级有问题或者安全加固有问题),可以考虑查看如下3个文件。
/etc/securoty/limits.conf
/etc/pam.d/login
/etc/pam.d/su
📍 注意:这个open file的值是不断可以进行调优的!!!
soft nofile< hard nofile< nr_open< file-max
📍 推荐配置1:k8s节点(目前就先使用这个配置吧!!!😘)
1、配置file-max参数
echo "fs.file-max = 6553560" >> /etc/sysctl.conf
2、配置ulimit内容
# vim /etc/security/limits.conf
* soft nofile 1048576
* hard nofile 1048576
* soft nproc 1024000
* hard nproc 1024000
* soft memlock unlimited
* hard memlock unlimited
* soft core unlimited
* hard core unlimited
* soft stack 1024000
* hard stack 1024000
#说明:
/proc/sys/fs/file-max(系统所有进程一共可以打开的文件数量)
/proc/sys/fs/nr_open (单个进程可分配的最大文件数),这个参数一般建议是1024000
注意:是如下2行配置决定了ulimit -a中opne file的值!!!
* soft nofile 1048576
* hard nofile 1048576
3、重启机器
reboot
4、测试效果
cat /proc/sys/fs/file-max
ulimit -a
🍀 注意:/proc/sys/fs/nr_open (单个进程可分配的最大文件数),这个参数一般建议是1024000
📍 推荐配置2:k8s节点
📍 linux中ulimit最大值是多少:2^20=1048576
# vim /etc/security/limits.conf #也就是如下可以配置的最大数量
* soft nofile 1048576
* hard nofile 1048576
📍 方法:如果ulimit配置不生效,可以考虑再加个账户
📍 TS:open file配置不生效(已解决)-2022.1.15
- 问题现象
业务侧配置了open file,但不生效,请求协助排查。
- 解决过程
发现主机的UsePAM功能是关闭的,但配置了ulimit相关的内容后root用户是生效的,但普通用户登录是没有效果的,ulimit -a显示open file依然是默认值1024,重启也没有效果。
最终经百度,发现配置了如下配置,重启就生效了:
方式一:修改 systemctl 配置
$ vi /etc/systemd/system.conf #在最后追加如下内容
# 分别设置软硬限制:<Soft Limit>:<Hard Limit>
DefaultLimitNOFILE=65535:65535
# 同时设置软硬限制
DefaultLimitNOFILE=65535
修改完成后重启服务器才会生效。
注意,这种方式对所有服务生效,如果想要对某个服务单独设置请看方式二。
这个文章完整链接如下:
🍀 Linux 系统修改 open files 无效
也许你通过各种方式,知道通过以下方式可以修改 open files:
$ vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
1
2
3
但是,这种方式只对当前登录的用户生效,其他系统服务则不生效。
这是因为通过 systemctl 启动的服务,会从他它自己的配置文件中读取配置进行设置,所以修改 /etc/security/limits.conf 对一些系统服务是无效的。
当然,这一步也需要进行的。
如何修改
方式一:修改 systemctl 配置
$ vi /etc/systemd/system.conf
\# 分别设置软硬限制:<Soft Limit>:<Hard Limit>
DefaultLimitNOFILE=65535:65535
\# 同时设置软硬限制
DefaultLimitNOFILE=65535
1
2
3
4
5
修改完成后重启服务器才会生效。
注意,这种方式对所有服务生效,如果想要对某个服务单独设置请看方式二。
方式二:修改单个服务的限制
$ vi /lib/systemd/system/<服务名>.service
\# 在 [Service] 后添加(只能同时设置,不支持分别设置)
LimitNOFILE=65535
1
2
3
修改完成后重启该服务即可,不需要重启整个服务器。
其他问题
如何查看是否生效?
$ cat /proc/<PID>/limits
1
还是不生效?
一些系统服务是通过 /etc/init.d/ 下的脚本启动的,启动脚本可能进行了单独设置,可以在脚本中搜索关键字 ulimit 进行确认。
参考链接
https://www.cnblogs.com/cncaptain/p/8629502.html
https://www.cnblogs.com/chinasoft/p/15341071.html
关于我
我的博客主旨:我希望每一个人拿着我的博客都可以做出实验现象,先把实验做出来,然后再结合理论知识更深层次去理解技术点,这样学习起来才有乐趣和动力。并且,我的博客内容步骤是很完整的,也分享源码和实验用到的软件,希望能和大家一起共同进步!
各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人免费帮您解决问题:
-
个人微信二维码:x2675263825 (舍得), qq:2675263825。
-
个人博客地址:www.onlyonexl.cn
-
个人微信公众号:云原生架构师实战
-
个人csdn
https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421
最后
好了,关于linux修改open files实验就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!