Linux最大文件打开数

Linux最大文件打开数

 

介绍

在Linux下有时会遇到Socket/File : Can't open so many files的问题。其实Linux是有文件句柄限制的,而且Linux默认一般都是1024(阿里云主机默认是65535)。在生产环境中很容易到达这个值,因此这里就会成为系统的瓶颈。

1.查看方法

使用ulimit -a 或者 ulimit -n

open files (-n) 1024 是linux操作系统对一个进程打开的文件句柄数量的限制(也包含打开的套接字数量)

这里只是对用户级别的限制,其实还有个是对系统的总限制,查看系统总线制:

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

man proc,可得到file-max的描述:

/proc/sys/fs/file-max
              This  file defines a system-wide limit on the number of open files for all processes.  (See
              also 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 messages
              about running out of file handles, try increasing this value:

即file-max是设置系统所有进程一共可以打开的文件数量 。同时一些程序可以通过setrlimit调用,设置每个进程的限制。如果得到大量使用完文件句柄的错误信息,是应该增加这个值。

 

也就是说,这项参数是系统级别的。

2.修改方法

 临时生效:

# ulimit -SHn 10000
其实ulimit 命令身是分软限制和硬限制,加-H就是硬限制,加-S就是软限制。默认显示的是软限制,如果运行ulimit 命令修改时没有加上-H或-S,就是两个参数一起改变。

软限制和硬限制的区别?

硬限制就是实际的限制,而软限制是警告限制,它只会给出警告。

永久生效

要想ulimits 的数值永久生效,必须修改配置文件/etc/security/limits.conf 
在该配置文件中添加

* soft nofile 65535   

* hard nofile 65535  

echo "* soft nofile 65535"  >> /etc/security/limits.conf

echo "* hard nofile 65535"  >> /etc/security/limits.conf

* 表示所用的用户

修改系统总限制

其实上的修改都是对一个进程打开的文件句柄数量的限制,我们还需要设置系统的总限制才可以。

假如,我们设置进程打开的文件句柄数是1024 ,但是系统总线制才500,所以所有进程最多能打开文件句柄数量500。从这里我们可以看出只设置进程的打开文件句柄的数量是不行的。所以需要修改系统的总限制才可以。

echo  6553560 > /proc/sys/fs/file-max

上面是临时生效方法,重启机器后会失效;

永久生效方法:

修改 /etc/sysctl.conf, 加入

fs.file-max = 6553560 重启生效

 

Nginx 进程最大可打开文件数

worker_processes:操作系统启动多少个工作进程运行Nginx。注意是工作进程,不是有多少个nginx工程。在Nginx运行的时候,会启动两种进程,一种是主进程master process;一种是工作进程worker process。例如我在配置文件中将worker_processes设置为4,启动Nginx后,使用进程查看命令观察名字叫做nginx的进程信息,我会看到如下结果:

  1.  
    [root@localhost nginx] # ps -elf | grep nginx
  2.  
    4 S root 2203 2031 0 80 0 - 46881 wait 22:18 pts/0 00:00:00 su nginx
  3.  
    4 S nginx 2204 2203 0 80 0 - 28877 wait 22:18 pts/0 00:00:00 bash
  4.  
    5 S root 2252 1 0 80 0 - 11390 sigsus 22:20 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  5.  
    5 S nobody 2291 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
  6.  
    5 S nobody 2292 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
  7.  
    5 S nobody 2293 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
  8.  
    5 S nobody 2294 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
  9.  
    0 R root 2312 2299 0 80 0 - 28166 - 22:24 pts/0 00:00:00 grep --color=auto nginx

1个nginx主进程,master process;还有四个工作进程,worker process。主进程负责监控端口,协调工作进程的工作状态,分配工作任务,工作进程负责进行任务处理。一般这个参数要和操作系统的CPU内核数成倍数。

worker_connections:这个属性是指单个工作进程可以允许同时建立外部连接的数量。无论这个连接是外部主动建立的,还是内部建立的。这里需要注意的是,一个工作进程建立一个连接后,进程将打开一个文件副本。所以这个数量还受操作系统设定的,进程最大可打开的文件数有关。

设置Nginx进程最大可打开文件数

1、更改操作系统级别的“进程最大可打开文件数”的设置

2、更改Nginx软件级别的“进程最大可打开文件数”的设置

刚才更改的只是操作系统级别的“进程最大可打开文件”的限制,作为Nginx来说,我们还要对这个软件进行更改。打开nginx.conf主配置文件。您需要配合worker_rlimit_nofile属性。如下:

user root root; 
worker_processes 4; 
worker_rlimit_nofile 65535;

#error_log logs/error.log; 
#error_log logs/error.log notice; 
#error_log logs/error.log info;

#pid logs/nginx.pid; 
events { 
        use epoll; 
        worker_connections 65535; 
}

请注意代码行中加粗的两个配置项,请一定两个属性全部配置。

验证Nginx的“进程最大可打开文件数”是否起作用

那么我们如何来验证配置是否起作用了呢?在linux系统中,所有的进程都会有一个临时的核心配置文件描述,存放路径在 /pro/进程号/limit。

首先我们来看一下,没有进行参数优化前的进程配置信息:

  1.  
    [root@localhost nginx] # ps -elf | grep nginx
  2.  
    4 S root 2203 2031 0 80 0 - 46881 wait 22:18 pts/0 00:00:00 su nginx
  3.  
    4 S nginx 2204 2203 0 80 0 - 28877 wait 22:18 pts/0 00:00:00 bash
  4.  
    5 S root 2252 1 0 80 0 - 11390 sigsus 22:20 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  5.  
    5 S nobody 2291 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
  6.  
    5 S nobody 2292 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
  7.  
    5 S nobody 2293 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
  8.  
    5 S nobody 2294 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
  9.  
    0 R root 2318 2299 0 80 0 - 28166 - 22:42 pts/0 00:00:00 grep --color=auto nginx

可以看到,nginx工作进程的进程号是:2291 2292 2293 2294。我们选择一个进程,查看其核心配置信息:

  1.  
    [root@localhost nginx]# cat /proc/ 2291/limits
  2.  
    Limit Soft Limit Hard Limit Units
  3.  
    Max cpu time unlimited unlimited seconds
  4.  
    Max file size unlimited unlimited bytes
  5.  
    Max data size unlimited unlimited bytes
  6.  
    Max stack size 8388608 unlimited bytes
  7.  
    Max core file size 0 unlimited bytes
  8.  
    Max resident set unlimited unlimited bytes
  9.  
    Max processes 3829 3829 processes
  10.  
    Max open files 1024 4096 files
  11.  
    Max locked memory 65536 65536 bytes
  12.  
    Max address space unlimited unlimited bytes
  13.  
    Max file locks unlimited unlimited locks
  14.  
    Max pending signals 3829 3829 signals
  15.  
    Max msgqueue size 819200 819200 bytes
  16.  
    Max nice priority 0 0
  17.  
    Max realtime priority 0 0
  18.  
    Max realtime timeout unlimited unlimited us

请注意其中的Max open files ,分别是1024和4096。那么更改配置信息,并重启Nginx后,配置信息就是下图所示了:

  1.  
    [root@localhost conf]# cat /proc/ 2351/limits
  2.  
    Limit Soft Limit Hard Limit Units
  3.  
    Max cpu time unlimited unlimited seconds
  4.  
    Max file size unlimited unlimited bytes
  5.  
    Max data size unlimited unlimited bytes
  6.  
    Max stack size 8388608 unlimited bytes
  7.  
    Max core file size 0 unlimited bytes
  8.  
    Max resident set unlimited unlimited bytes
  9.  
    Max processes 3829 3829 processes
  10.  
    Max open files 65535 65535 files
  11.  
    Max locked memory 65536 65536 bytes
  12.  
    Max address space unlimited unlimited bytes
  13.  
    Max file locks unlimited unlimited locks
  14.  
    Max pending signals 3829 3829 signals
  15.  
    Max msgqueue size 819200 819200 bytes
  16.  
    Max nice priority 0 0
  17.  
    Max realtime priority 0 0
  18.  
    Max realtime timeout unlimited unlimited us

 

转载于:https://www.cnblogs.com/xy51/p/10218489.html

### 回答1: Linux最大文件打开是由系统内核参ulimit控制的,默认值为1024。可以通过修改ulimit参来增加最大文件打开,以满足特定应用程序的需求。但是需要注意,过高的最大文件打开可能会导致系统资源耗尽,影响系统稳定性。 ### 回答2: Linux下的最大文件打开,指的是一个进程可以同时打开最大文件目,通常用于限制一个进程能够同时处理的文件目。这个限制在Linux系统中是通过一个系统参进行控制的,也就是 ulimit 命令。 在Linux系统中,最大文件打开是可以通过 ulimit 命令进行设置和查询的。ulimit 命令是一个系统工具,用于限制用户的各种资源的使用,包括文件打开(-n 参)。可以用以下命令查询当前系统的最大文件打开: $ ulimit -n Linux系统中默认的最大文件打开是 1024,这意味着,如果一个进程需要同时打开超过 1024 个文件,将会出现错误。用户可以使用 ulimit 命令,将最大文件打开调大。 为了保持系统的稳定性和安全性,需要根据实际情况合理设置最大文件打开。如果设置的过小,可能会导致一些应用程序无法正常运行;而设置的过大,可能会耗尽系统的资源,让系统变得不稳定。 总之,Linux系统的最大文件打开对于系统的运行是非常重要的,用户需要根据实际需要和系统资源情况进行合理的设置。对于一些特殊的应用场景,可能需要更大的文件打开,用户可以通过修改系统内核参来得到更大的文件打开。 ### 回答3: Linux最大文件打开是指系统允许一个进程同时打开最大文件。在Linux中,每次系统调用open、socket等函时,就会为该进程引用计器加1。当该进程关闭该文件描述符时,该引用计器会减1。当该引用计器为0时,该文件描述符就会被释放。 在默认情况下,每个进程可以同时打开1024个文件。这个值可以通过修改系统参来改变。具体地说,可以通过修改/proc/sys/fs/file-max的值来增加系统文件的上限。同时,可以通过ulimit命令调整单个进程最大文件限制。 要注意,增加系统文件上限并不是一件好事情。如果太多进程同时打开大量的文件,可能会导致系统资源不足,从而影响系统的稳定性和性能。因此,在调整系统参之前,建议先进行风险评估和测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值