Windows OS Hub
  • Windows Server
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Group Policies
  • Windows Clients
    • Windows 10
    • Windows 8
    • Windows 7
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
  • PowerShell
  • Exchange
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Group Policies
  • Windows Clients
    • Windows 10
    • Windows 8
    • Windows 7
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
  • PowerShell
  • Exchange

 Windows OS Hub / Linux / How to Fix the ‘Too Many Open Files’ Error in Linux?

September 1, 2020 Linux

How to Fix the ‘Too Many Open Files’ Error in Linux?

Very often ‘too many open files’ errors occur on high-load Linux servers. It means that a process has opened too many files (file descriptors) and cannot open new ones. In Linux, the maximum open file limits are set by default for each process or user and the values are rather small.

In this article we will learn how to check the current limit on the maximum number of open files in Linux and how to change it for the entire host, individual service or a current session.

Contents:
  • ‘Too Many Open Files’ Error & Open File Limits in Linux
  • How to Increase the Max Open File Limit in Linux?
  • Increasing the Open File Descriptor Limit per Service
  • How to Set Max Open Files Limit for Nginx & Apache?
  • Change the Open File Limit for the Current Session

‘Too Many Open Files’ Error & Open File Limits in Linux

First of all, let’s see where the ‘too many open files’ error appears. The most often it occurs on the servers with an installed NGINX/httpd web server or a database server (MySQL/MariaDB/PostgreSQL) when reading a large number of logs. For example, when an Nginx web server exceeds the open file limit, you will see an error:

socket () failed (29: Too many open files) while connecting to upstream

Too many open files error in Linux log files

Using this command, you can get the maximum number of file descriptors your system can open:

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

The open file limit for a current user is 1024. You can check it as follows:

# ulimit -n

get default open file limit in LInux

There are two limit types: Hard and Soft. A user can change a soft limit (however, the soft value cannot exceed the hard one). Only a privileged or root user can modify a hard limit value.

To display the soft limit, run this command:

# ulimit –nS

To display the hard limit value:

# ulimit -nH

How to Increase the Max Open File Limit in Linux?

To allow all services open a large number of files, you can change the limits in your Linux OS. To make new settings permanent and prevent their reset after a server or session restart, you must make changes to /etc/security/limits.conf. Add these lines:

* hard nofile 97816
* soft nofile 97816

If you are using Ubuntu, add this line as well:

session required pam_limits.so

This parameter allows to set open files limits after user authentication.

After making any changes, reload the terminal and check the max_open_files value:

# ulimit -n

97816

Increasing the Open File Descriptor Limit per Service

You can change the limit of open file descriptors for a specific service, rather than for the entire operating system. Let’s take apache as an example. Open the service settings using systemctl:

# systemctl edit httpd.service

Add the limits you want, e.g.:

[Service]
LimitNOFILE=16000
LimitNOFILESoft=16000

set LimitNOFILE for systemd service

After making the changes, update the service configuration and restart it:

# systemctl daemon-reload
# systemctl restart httpd.service

To make sure if the values have changed, get the service PID:

# systemctl status httpd.service

For example, the service PID is 3724:

# cat /proc/3724/limits | grep "Max open files”

get Max open files per process by PID

Thus, you have changed the values for the maximum number of open files for a specific service.

How to Set Max Open Files Limit for Nginx & Apache?

When changing the limit on the number of open files for a web server, you also have to change the service configuration file. For example, specify/change the following directive value in the Nginx configuration file /etc/nginx/nginx.conf:

worker_rlimit_nofile 16000
When configuring Nginx on a highly loaded 8-core server with worker_connections 8192, you need to specify 8192*2*8 (vCPU) = 131072 in worker_rlimit_nofile.

Then restart Nginx.

For apache, create a directory:

# mkdir /lib/systemd/system/httpd.service.d/

Then create the limit_nofile.conf file:

# nano /lib/systemd/system/httpd.service.d/limit_nofile.conf

LimitNOFILE set ulimiys for apache and nginx

Add to it:

[Service]
LimitNOFILE=16000

Don’t forget to restart httpd.

Change the Open File Limit for the Current Session

To change maximum open file limits for your terminal session, run this command:

# ulimit -n 3000

After closing the terminal and creating a new session, the limits will get back to the original values specified in /etc/security/limits.conf.

To change the general value for the system /proc/sys/fs/file-max, change the fs.file-max value in /etc/sysctl.conf:

fs.file-max = 100000

And apply it:

# sysctl -p

fs.file-max

In this article we have learned how to solve the issue when the value of open file descriptor limit in Linux is too small, and discussed some options of changing these limits on the server.

0 comment
2
Facebook Twitter Google + Pinterest
previous post
Configuring Port Forwarding on Windows
next post
How to Block USB Drives in Windows using Group Policy?

Related Reading

Configuring Network Settings on RHEL/CentOS

February 15, 2021

Zabbix – Simple ICMP Ping Checks

February 3, 2021

Configuring Software RAID on Linux Using MDADM

January 11, 2021

Install and Configure OpenVPN Server on Linux CentOS/RHEL

December 31, 2020

Zabbix: Single Sign-On (SSO) Authentication in Active Directory

December 17, 2020

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange
  • Windows 10
  • Windows 8
  • Windows 7
  • Windows Server 2016
  • Windows Server 2012 R2
  • Windows Server 2008 R2
  • PowerShell
  • VMWare
  • MS Office

Recent Posts

  • How to Sign a PowerShell Script (PS1) with a Code Signing Certificate?

    February 25, 2021
  • Change the Default Port Number (TCP/1433) for a MS SQL Server Instance

    February 24, 2021
  • How to Shadow (Remote Control) a User’s RDP session on RDS Windows Server 2016/2019?

    February 22, 2021
  • Configuring PowerShell Script Execution Policy

    February 18, 2021
  • Configuring Proxy Settings on Windows Using Group Policy Preferences

    February 17, 2021
  • Updating Group Policy Settings on Windows Domain Computers

    February 16, 2021
  • Managing Administrative Shares (Admin$, IPC$, C$, D$) in Windows 10

    February 11, 2021
  • Packet Monitor (PktMon) – Built-in Packet Sniffer in Windows 10

    February 10, 2021
  • Fixing “Winload.efi is Missing or Contains Errors” in Windows 10

    February 5, 2021
  • How to Move (Clone) Windows to a New Hard Drive (HDD/SSD)?

    February 4, 2021

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • How to Configure MariaDB Master-Master/Slave Replication?
  • Using iPerf to Test Network Speed and Bandwidth (Throughput)
  • How to Mount Google Drive or OneDrive in Linux?
  • Hyper-V Boot Error: The Image’s Hash and Certificate Are not Allowed
  • Adding VLAN Interface in CentOS/Fedora/RHEL
  • Ubuntu/Mint/Kali Boots to Initramfs Prompt in BusyBox
  • Using YUM Package Manager on CentOS and RHEL
Footer Logo

@2014 - 2018 - Windows OS Hub. All about operating systems for sysadmins


Back To Top