number of open file descriptors

June 05, 2025 / Administrator / 5阅读 / 0评论/ 分类: Linux-Gun

number of open file descriptors

refer to : https://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/

What are File Descriptors (FDs) in Linux?

In Linux systems, a File Descriptor (FD) is a handle used by processes (such as HTTPD/Nginx, MySQL, and others) to interact with open files, network sockets, pipes, and other I/O resources.

Are the FDs limits important?

Yes. Resource limits prevent processes from monopolizing system resources, ensuring smooth functioning. Hence, you need to set the FDs limit for each service including system-wide limits. If the correct limit is not set, you will get an errors such as “24: Too many open files” or “WARNING! Your cache is running out of file descriptors“.

Soft vs. Hard limits

There are two types of limits:

  1. Soft limits are the currently practical limits. They cannot exceed Hard limits.
  2. A regular user can increase their Soft limit up to the Hard limit.
  3. Root or privileged processes can modify both Soft and Hard limits.

System-wide vs. Service-specific FDs limits

You need to set limit at the service/user level or system-wide:

  1. System-wide: If you want the change across all relevant users and services.
  2. User or Service-specific: When you need to adjust the limit for a particular systemd or init service only. For example, nginx user is used to run nginx service. So it makes more sense to set hard and soft limit for the nginx user.

Command To List Number Of Open File Descriptors

Use the following command command to display maximum number of open file descriptors. Try “bat,” “cat,” “less,” or “more” commands:

cat /proc/sys/fs/file-max

Output:

75000

75000 files normal user can have open in single login session. To see the hard and soft values, issue the command as follows:

ulimit -Hn
ulimit -Sn

To see the hard and soft values for httpd or oracle user, issue the command as follows:

su - username

In this example, su to oracle user, enter:

su - oracle
ulimit -Hn
ulimit -Sn

System-wide File Descriptors (FD) Limits

The number of concurrently open file descriptors throughout the system can be changed via /etc/sysctl.conf file under Linux operating systems.

The Number Of Maximum Files Was Reached, How Do I Fix This Problem?

Many application such as Oracle database or Apache web server needs this range quite higher. So you can increase the maximum number of open files by setting a new value in kernel variable /proc/sys/fs/file-max

You need to edit the /etc/sysctl.conf or (create a new file named /etc/sysctl.d/1000-custom.conf) file and put following line so that after reboot the setting will remain as it is:

vi /etc/sysctl.conf

or

vi /etc/sysctl.d/1000-custom.conf

Append a config directive as follows:

fs.file-max = 100000

Save and close the file by pressing Esc+:wq!. Users need to log out and log back in again to changes take effect

cat /proc/sys/fs/file-max

User or Service Level FD Limits

The above procedure sets system-wide file descriptors (FD) limits. However, you can limit httpd (or any other users) user to specific limits by editing /etc/security/limits.conf file, enter:

vi /etc/security/limits.conf

or

vi /etc/security/limits.d/httpd.conf

Set httpd user soft and hard limits as follows:

httpd soft nofile 4096
httpd hard nofile 10240

Save and close the file. To see limits, enter:

su - httpd

ulimit -Hn

ulimit -Sn

ALL User level FD

如果是针对所有用户

编辑 /etc/security/limits.conf 文件,添加以下内容:

vim /etc/security/limits.conf
# 针对所有用户(或替换 * 为具体用户名),* wildcard doesn't work for root,所以root用户需要单独配置
root               soft     nofile             65535
root                hard     nofile             65535
*               soft     nofile             65535
*               hard     nofile             65535
  • 说明
    • soft:应用可临时超过,但会收到警告。
    • hard:绝对上限,普通用户无法修改
  • 生效方式
    • 重启系统重新登录用户会话(退出终端再登录)。
    • 验证:ulimit -n 应显示新值(如 65535)。

文章作者:Administrator

文章链接:http://localhost:8090//archives/number-of-open-file-descriptors

版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议,转载请注明出处!


评论