Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 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.

1 comment
5
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

Installing an Open Source KMS Server (Vlmcsd) on...

March 16, 2022

Installing PowerShell Core on Linux Distros

February 28, 2022

How to Disable Microsoft Teams Auto Startup?

December 23, 2021

Configuring NFS Server and Client on Linux CentOS/RHEL

November 11, 2021

How to Install and Use ClamAV Antivirus on...

October 13, 2021

1 comment

anon September 21, 2021 - 3:10 pm

Not really sure what ulimit is running on OpenSUSE Leap 15.2, but ulimit-nS fails with -bash: ulimit: S: invalid number (same for -nH)
Correct, for my ulimit implementation, is ulimit -Sn (and -Hn) as it requires the ‘soft’/’hard’ param to be 1st.

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows 7
  • Windows Server 2019
  • Windows Server 2016
  • Windows Server 2012 R2
  • PowerShell
  • VMWare
  • Hyper-V
  • MS Office

Recent Posts

  • Create Organizational Units (OU) Structure in Active Directory with PowerShell

    May 17, 2022
  • Windows Security Won’t Open or Shows a Blank Screen on Windows 10/ 11

    May 17, 2022
  • How to Manually Install Windows Updates from CAB and MSU Files?

    May 16, 2022
  • RDS and RemoteApp Performance Issues on Windows Server 2019/2016

    May 16, 2022
  • Deploying Software (MSI Packages) Using Group Policy

    May 12, 2022
  • Updating VMware ESXi Host from the Command Line

    May 11, 2022
  • Enable or Disable MFA for Users in Azure/Microsoft 365

    April 27, 2022
  • Fix: You’ll Need a New App to Open This Windows Defender Link

    April 27, 2022
  • How to Reset an Active Directory User Password with PowerShell and ADUC?

    April 27, 2022
  • How to Completely Uninstall Previous Versions of Office with Removal Scripts?

    April 26, 2022

Follow us

woshub.com

ad

  • 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?
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • Configuring High Performance NGINX and PHP-FPM Web Server
  • Install and Configure SNMP on RHEL/CentOS/Fedor
  • Adding VLAN Interface in CentOS/Fedora/RHEL
Footer Logo

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


Back To Top