Polarisbancage

Navigation

81days since
最爱的banban生日

172days since
Me

Linux‎ > ‎

系统打开文件限制

http://www.faqs.org/docs/securing/chap6sec72.html

The file-max file /proc/sys/fs/file-max sets the maximum number of file-handles that the Linux kernel will allocate. We generally tune this file to improve the number of open files by increasing the value of /proc/sys/fs/file-max to something reasonable like 256 for every 4M of RAM we have: i.e. for a machine with 128 MB of RAM, set it to 8192 - 128/4=32 32*256=8192.

The default setup for the file-max parameter under Red Hat Linux is: "4096" To adjust the value of file-max to 128 MB of RAM, type the following on your terminal:

Version 6.1 only

          [root@deep] /# echo "8192" >/proc/sys/fs/file-max
Add the above commands to the /etc/rc.d/rc.local script file and you'll not have to type it again the next time your server reboots.

Version 6.2 only

Edit the /etc/sysctl.conf file and add the following line:
          # Improve the number of open files
fs.file-max = 8192
You must restart your network for the change to take effect. The command to manually restart the network is the following:
          [root@deep] /# /etc/rc.d/init.d/network restart

Setting network parameters [ OK ] Bringing up interface lo [ OK ] Bringing up interface eth0 [ OK ] Bringing up interface eth1 [ OK ]

Comments (1)

君钧柴 - Oct 9, 2009 8:53 PM

Too many open files经常在使用linux的时候出现,大多数情况是您的程序没有正常关闭一些资源引起的,所以出现这种情况,请检查io读写,socket通讯等是否正常关闭。

如果检查程序没有问题,那就有可能是linux默认的open files值太小,不能满足当前程序默认值的要求,比如数据库连接池的个数,tomcat请求连接的个数等。。。

查看当前系统open files的默认值,可执行:

Java代码

1. [root@pororo script]# ulimit -a
2. core file size (blocks, -c) 0
3. data seg size (kbytes, -d) unlimited
4. scheduling priority (-e) 0
5. file size (blocks, -f) unlimited
6. pending signals (-i) 128161
7. max locked memory (kbytes, -l) 32
8. max memory size (kbytes, -m) unlimited
9. open files (-n) 800000
10. pipe size (512 bytes, -p) 8
11. POSIX message queues (bytes, -q) 819200
12. real-time priority (-r) 0
13. stack size (kbytes, -s) 10240
14. cpu time (seconds, -t) unlimited
15. max user processes (-u) 128161
16. virtual memory (kbytes, -v) unlimited
17. file locks (-x) unlimited

[root@pororo script]# 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) 128161
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 800000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 128161
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited



如果发现open files项比较小,可以按如下方式更改:

1. 检查/proc/sys/fs/file-max文件来确认最大打开文件数已经被正确设置。

Java代码

1. # cat /proc/sys/fs/file-max

# cat /proc/sys/fs/file-max



如果设置值太小,修改文件/etc/sysctl.conf的变量到合适的值。这样会在每次重启之后生效。 如果设置值够大,跳过这一步。

Java代码

1. # echo 2048 > /proc/sys/fs/file-max

# echo 2048 > /proc/sys/fs/file-max



编辑文件/etc/sysctl.conf,插入下行:

Java代码

1. fs.file-max = 8192

fs.file-max = 8192



2. 在/etc/security/limits.conf文件中设置最大打开文件数, 下面是一行提示:

Java代码

1. #<domain> <type> <item> <value>

#<domain> <type> <item> <value>



添加如下这行:

Java代码

1. * - nofile 8192

* - nofile 8192



这行设置了每个用户的默认打开文件数为2048。注意"nofile"项有两个可能的限制措施。就是<type>项下的hard和soft。要使修改过得最大打开文件数生效,必须对这两种限制进行设定。 如果使用"-"字符设定<type>, 则hard和soft设定会同时被设定。

硬限制表明soft限制中所能设定的最大值。 soft限制指的是当前系统生效的设置值。 hard限制值可以被普通用户降低。但是不能增加。 soft限制不能设置的比hard限制更高。 只有root用户才能够增加hard限制值。

当增加文件限制描述,可以简单的把当前值双倍。 例子如下, 如果你要提高默认值1024, 最好提高到2048, 如果还要继续增加, 就需要设置成4096。

最后用ulimit -a再次查看,open files的值,没什么问题的话,就已经改过来了。