ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

linux 系统如何正确的设置文件句柄限制

linux 系统如何正确的设置文件句柄限制 一Too many open files 报错有时应用程序会报Too many open files的错误这是因为设置的文件句柄不够用。需要修改ulimit nofile和file-max。二修改文件句柄的坑#先说坑绝对要注意否则错误的设置会导致你的服务器无法登录如果不在 /etc/security/limits.conf 配置某个用户或者全局 nofile 参数那么用户open files 限制为默认值1024如果在 /etc/security/limits.conf 中配置了全局或者某个用户的 nofile 参数那么用户open files限制继承该参数指定值。但是要注意的是/etc/security/limits.conf 中配置 nofile 参数是有限制的在2.6.25内核之前有个宏定义定义了这个值的最大值为1024*1024正好是100万(也有说是102万)而在2.6.25内核及其之后这个值是可以通过cat /proc/sys/fs/nr_open来设置。”/proc/sys/fs/nr_open 值通过/ect/sysct.conf 文件中 fs.nr_open 值指定修改后 sysctl -p 生效。如果/etc/security/limits.conf中配置的 nofile参数超出限制会导致服务器无法登录三设置open files 限制1.系统层面限制参数设置(file-max 设置)即file-max是设置 系统所有进程一共可以打开的文件数量 。1.1 查看当前系统file-max限制cat /proc/sys/fs/file-max65536或者sysctl -a | grep file-max1.2 设置 file-max1) 修改 /proc/sys/fs/file-maxecho 655360 /proc/sys/fs/file-max#修改立即生效系统重启后会失效或者2) 修改 /etc/sysctl.conf 添加如下cat /proc/sys/fs/file-maxcat /proc/sys/fs/nr_openecho fs.nr_open 65535600 /etc/sysctl.confecho fs.file-max 55535600 /etc/sysctl.confsysctl -p#最好fs.nr_open 跟着一起修改 sysctl -p 后修改生效重启后不会失效1.3 修改 nofile 限制修改 /etc/security/limits.confvim /etc/security/limits.conf* soft nofile 655360* hard nofile 655360teledb soft nofile 655360teledb hard nofile 655360#上面两行对所有用户生效下面两行对 teledb 用户生效。如果没有配置用户和全局级别的 nofile那么shell默认的nofile限制为 1024#注意soft 限制的数值不要大于 hard 值超过了实际还是以hard指定值为准四ulimit -n 和 file-max 区别1. file-maxfile-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调用设置每个进程的限制。如果得到大量使用完文件句柄的错误信息是应该增加这个值。也就是说这项参数是系统级别的。2.ulimitProvides control over the resources available to the shell and to processes started by it, on systems that allow such control.即设置当前shell以及由它启动的进程的资源限制。显然对服务器来说file-max, ulimit都需要设置否则就可能出现文件描述符用尽的问题五/etc/security/limits.conf 文件中配置生效优先级1.目前测试发现配置文件中用户级别设置优先级高于全局级别2.但是如果我们对同样的域指定了多次限制比如我们对vmuser 用户指定了两次 nofile 设置那么以最后一次出现的有效限制为准六修改文件句柄限制对已有进程是否生效如果某项服务已经启动再动态调整ulimit对其是无效的我们可以通过。cat /proc/程序pid/limits 来查看某个已运行程序limit情况
返回列表