如何在Linux中修改打开文件描述符的数量

  • 原创
  • Madman
  • /
  • /
  • 0
  • 9533 次阅读

如何在Linux中增加打开文件数限制-min.png

Synopsis: 在Linux中,如果您拥有对系统的root访问权限,那么可以更改可以打开文件描述符的最大数量。Linux系统资源限制又分为整个系统层面的总限制,此时要修改内核参数,比如/proc/sys/fs/file-max。还可以对进程和用户分别做资源限制,将ulimit命令写入应用程序的启动脚本中,此时要注意子进程允许打开的最大文件数不能超过父进程的阈值限制。还可以在当前shell会话中直接执行ulimit命令,此时限制的是当前 shell 进程以及其派生的子进程

1. 全局级的文件描述符限制

查看当前的Linux系统上可打开的文件描述符的最大数量:

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

此数值表示每个登录会话中用户可以打开的文件数量,结果可能会因各系统而异。

如果Linux系统用作Apache/MySQL之类的服务器,可能需要打开更多的文件描述符,可以使用sysctl命令增大内核参数fs.file-max的数值:

1. 增加到500000
# sysctl -w fs.file-max=500000

2. 查看效果
# cat /proc/sys/fs/file-max

上述设置只在当前会话有效,如果需要新建会话或重启系统也生效,那么要修改/etc/sysctl.conf配置文件,在末尾添加fs.file-max=500000,然后你想立即看到效果的话,请运行sysctl -p命令

2. 用户级的文件描述符限制

除了可以通过修改内核参数,来设置Linux系统全局的文件描述符限制外,还可以精细化到每个用户或用户组。

2.1 内置命令ulimit

使用ulimit内置命令可以设置shell的资源限制,包括最大可打开的文件数:

1. 查看当前用户所有的资源限制信息
# 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) 31204
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     
                                
                            
  • July
  • Instant cash can be yours today co093912.tw1.ru ol
  • Your bonus offer closes at 12 o'clock tonight co093912.tw1.ru Ra
  • This Very Moment Claim Your Extra Cash ci421269.tw1.ru 7m
  • Victory and cash in under a minute co093912.tw1.ru Tu
  • Your cash payout is prepared yugfwegyfg.temp.swtest.ru kG
  • The first to arrive get the monetary rewards co093912.tw1.ru HA
  • A time limited seasonal reward is now active co093912.tw1.ru n2
  • Your sign up comes with cash ci421269.tw1.ru Ky
  • This is your specific individual payout co093912.tw1.ru Pa
  • Receive Payment for Your Time yugfwegyfg.temp.swtest.ru 0z
  • Profit from Your Own Learning Initiatives co093912.tw1.ru bY
  • This cash offer expires in a single day ci421269.tw1.ru Ud
  • Keep updated earn free cash yugfwegyfg.temp.swtest.ru YK
  • This cash payout is for you as an individual co093912.tw1.ru mR
  • A cash fortune is standing by ci421269.tw1.ru lE
  • Enjoy This Monetary Gift Immediately ci421269.tw1.ru UN
  • Earn an Introductory Bonus for Investing ci421269.tw1.ru ju
  • In a few seconds you could be richer co093912.tw1.ru 3j
  • Quick cash victories through play yugfwegyfg.temp.swtest.ru 5G
  • Go ahead and claim your USD75 yugfwegyfg.temp.swtest.ru 5u
  • A happy accident has led to monetary gain ci421269.tw1.ru Jb
  • Get Cash Back for Your Trades yugfwegyfg.temp.swtest.ru af
  • Reveal a cash prize by scratching here co093912.tw1.ru sR
未经允许不得转载: LIFE & SHARE - 王颜公子 » 如何在Linux中修改打开文件描述符的数量

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列