- How to Increase Number of Open Files Limit in Linux
- Find Linux Open File Limit
- Check Hard Limit in Linux
- Check Soft Limits in Linux
- How to Check System wide File Descriptors Limits in Linux
- Set User Level Open File limits in Linux
- Final thoughts
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- linux-notes.org
- Увеличить Max Open File Limit в Unix/Linux
- Увеличить Max Open File Limit в Linux
- Увеличить Max Open File Limit в Mac OS X
- Увеличюем nginx worker_rlimit_nofile в nginx ( на уровне Nginx)
- Включение ограничений на основе PAM в Unix/Lixux
- 3 thoughts on “ Увеличить Max Open File Limit в Unix/Linux ”
- Добавить комментарий Отменить ответ
- 3 Methods to Change the Number of Open File Limit in Linux
- Why limiting the number of opened files
- 1) ulimit command
- 2) Pluggable Authentication Modules (PAM) module
- 3) System wide limit
- How to set ulimit and file descriptors limit on Linux Servers
- To see what is the present open file limit in any Linux System
- ulimit command :
- How to fix the problem when limit on number of Maximum Files was reached ?
- Set User level resource limit via limit.conf file
How to Increase Number of Open Files Limit in Linux
In Linux, you can change the maximum amount of open files. You may modify this number by using the ulimit command. It grants you the ability to control the resources available for the shell or process started by it.
In this short tutorial we will show you how to check your current limit of open files and files descriptions, but to do so, you will need to have root access to your system.
First, Lets see how we can find out the maximum number of opened file descriptors on your Linux system.
Find Linux Open File Limit
The value is stored in:
The number you will see, shows the number of files that a user can have opened per login session. The result might be different depending on your system.
For example on a CentOS server of mine, the limit was set to 818354, while on Ubuntu server that I run at home the default limit was set to 176772.
If you want to see the hard and soft limits, you can use the following commands:
Check Hard Limit in Linux
Check Soft Limits in Linux
To see the hard and soft values for different users, you can simply switch user with “su” to the user which limits you want to check.
How to Check System wide File Descriptors Limits in Linux
If you are running a server, some of your applications may require higher limits for opened file descriptors. A good example for such are MySQL/MariaDB services or Apache web server.
You can increase the limit of opened files in Linux by editing the kernel directive fs.file-max . For that purpose, you can use the sysctl utility.
For example, to increase open file limit to 500000, you can use the following command as root:
You can check the current value for opened files with the following command:
With the above command the changes you have made will only remain active until the next reboot. If you wish to apply them permanently, you will have to edit the following file:
Add the following line:
Of course, you can change the number per your needs. To verify the changes again use:
Users will need to logout and login again for the changes to take effect. If you want to apply the limit immediately, you can use the following command:
Set User Level Open File limits in Linux
The above examples, showed how to set global limits, but you may want to apply limits per user basis. For that purpose, as user root, you will need to edit the following file:
If you are a Linux administrator, I suggest you that you become very familiar with that file and what you can do to it. Read all of the comments in it as it provides great flexibility in terms of managing system resources by limiting users/groups on different levels.
The lines that you should add take the following parameters:
Here is an example of setting a soft and hard limits for user marin:
Final thoughts
This brief article showed you a basic example of how you can check and configure global and user level limits for maximum number of opened files.
While we just scratched the surface, I highly encourage you to have a more detailed look and read regarding /etc/sysctl.conf and /etc/security/limits.conf and learn how to use them. They will be of great help for you one day.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
linux-notes.org
Несколько раз я сталкивался с ошибкой «Too many open files»(Слишком много открытых файлов) на сервере с высокой нагрузкой. Это означает, что сервер исчерпывает ресурс на максимальный предел открытых файлов (max open file limit). Теперь вопрос в том, как я могу увеличить лимиты открытых файлов на Linux? Да все очень просто, в своей статье «Увеличить Max Open File Limit в Unix/Linux» покажу как это выполняется.
Увеличить Max Open File Limit в Unix/Linux
Приведу команды на различные Unix/Linux ОС
Увеличить Max Open File Limit в Linux
Для начала проверим какой предел установлен в ОС:
Увеличиваем данный предел в Linux
Мы можем увеличить лимиты для открытых файлов:
-===ВРЕМЕННО===-
Если есть необходимость увеличить лимит временно (для тестирования, например), то можно это сделать так:
Вот еще один пример:
-===ПОСТОЯННО===-
Если есть необходимость увеличить лимит навсегда, то можно это сделать так:
Эти настройки будут сохраняться даже после перезагрузки системы. После добавления конфигурации в файл, выполните следующую команду, чтобы изменения вступили в силу:
Настройка лимитов для каждого пользователя
Проверка установленных лимитов
Используйте следующую команду, чтобы увидеть максимальное чисто для открытых файлов:
Подключаемся от пользователя (у меня это nginx):
Проверяем параметры Hard лимитов :
В консоле, можно ввести данную команду (очень удобно отображает):
Проверяем параметры лимитов Soft :
Увеличить Max Open File Limit в Mac OS X
Выполним проверку лимитов с помощью:
- Первый аргумент — soft limit.
- Второй аргумент — hard limit.
Можно прописать в файл:
Увеличюем nginx worker_rlimit_nofile в nginx ( на уровне Nginx)
В nginx также можно увеличить лимиты с директивой worker_rlimit_nofile, которая позволяет увеличить этот лимит, если это не хватает данного ресурса на лету на уровне процесса:
И прописываем (редактируем):
После чего, проверяем конфигурацию nginx и перезапускаем его:
Save and close the file. Reload nginx web server, enter:
В комментариях писали что нельзя установить данные лимиты для Kali Linux. Вот, решил показать наглядный пример:
ulimit в Kali Linux
Включение ограничений на основе PAM в Unix/Lixux
Для Debian/Ubuntu
Редактируем файл (Debian/Ubuntu):
Открываем еще один файл:
И, приводим к виду:
И выполняем рестарт:
Для CentOS/RedHat/Fedora
Редактируем файл (Debian/Ubuntu):
Открываем еще один файл:
И, приводим к виду:
И выполняем рестарт:
У меня все! Статья «Увеличить Max Open File Limit в Unix/Linux», завершено.
3 thoughts on “ Увеличить Max Open File Limit в Unix/Linux ”
в kali linux не работает смена хард и софт лимитов. всё равно пишет 4096 и 1024 соответственно.
Можно! Что-то делаете не так. Я выложил скриншот в статье и показал что можно все сделать.
Мужикам на картинке к статье явно не повезло.
Свет приглушён, явно видно зелёный оттенок табличек аварийного освещения — либо дело за полночь, либо у них блекаут и работают от дизеля.
Сидят за консолью. По сети, оно, похоже, уже не отвечает. Плохо дело.
Стул притащили. Значит, давно не отвечает. Надо было прописать MAX OPEN FILE LIMIT заранее, а не когда уже навернулось.
Добавить комментарий Отменить ответ
Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.
Источник
3 Methods to Change the Number of Open File Limit in Linux
The operating system assigns temporarily a number called file handle to a file when it is opened for access. A special area of main memory is reserved for file handles, and the size of this area determines how many files can be opened at once. The processes on Linux are restricted by a number of constraints that also prevent them from executing properly and each process has several limits associated with it. The shell restricts the number of files handles that programs can open simultaneously. In this article, I will show you different methods that you can use to change the number of Open File Limit in Linux.
Why limiting the number of opened files
Because the operating system needs memory to manage each file, you can face a limitation of the number of files which can be opened. As a program can also close file handlers, it could create many files as big as it wants, until all the available disk space is full. In this case, one aspect of security is preventing the resource exhaustion by imposing limitations.
Under Linux, there are two kinds of limits:
- softlimit is the value which can be changed by the process at any time.
- hardlimit marks the maximum value which cannot be exceeded by setting a soft limit
You can see the maximum number of opened file descriptors on your Linux system as below:
The value shows the number of files that a user can have opened per login session but you should notice that the result might be different depending on your system. For some reasons, you can need to increase the value of the limitation set. This is why your Linux system offers the possibility (increasing or decreasing) to modify these limitations by changing the maximum of the open files count per process and per system.
1) ulimit command
The ulimit command can be used to increase the number of files which can be opened in a shell. This command is a bash built-in command, so it affects only bash and programs launched from it. The ulimit syntax is as follows:
The options define what is being limited. You can see some options as below
- -a (Current Settings Passing): causes ulimit to report its current settings.
- -f (File Limits) limits the size of files that may be created by the shell
- -n limits the number of open file descriptors.
- -H and -S (Hard and Soft Limits) options modify other options, causing them to be set as hard or soft limits, respectively. Hard limits may not be subsequently increased, but soft limits may be. If neither option is provided, ulimit sets both the hard and soft limits for the feature specified.
So, to see the current limitation, you can do as below:
You can check the hard limit as below:
and the soft limit as below:
You can edit the limit as below
and you can check below:
The problem now is, if you log out and log in or restart your computer, the value will be reset. Remember that making these limits more permanent requires editing one of the user’s configuration files ( .bashrc or .profile ) or the systemwide configuration files ( /etc/bashrc or /etc/profile ) by adding the ulimit command line to the end of the file as below
Now even if you restart, the limit set by the user will be permanent. You can choose to increase only the hard limit ( -Hn ) or the soft limit ( -Sn ) but you should notice that soft limits could be set by any user while hard limits are changeable only by root once it is set.
2) Pluggable Authentication Modules (PAM) module
Imposing such limits is best done through a Pluggable Authentication Modules (PAM) module called pam_limits. Most major Linux distributions use this module as part of their standard PAM configuration so it is already present on some Linux systems but you will need to configure it by editing the /etc/security/limits.conf file. This file contains consist of four essential fields:
- domain: it describes the entity to which the limit applies. It can be a username, a group name (with the form @groupname) or an asterisk ( * ) wildcard which matches everybody. However, you should notice that wildcard does not apply to root user
- type: this field specifies the limit as hard or soft. A hard limit is imposed by the system administrator and cannot be exceeded under any circumstances, whereas a user may temporarily exceed a soft limit. You can also use a dash ( — ) to signify that a limit is both hard and soft. Notice that a soft limit may be increased up to the value of the hard limit
- item: it specifies what type of item is being limited. This could be nofile (the number of open data files), fsize (the size of files created by the user), core (the size of core files), etc
- value: it specifies the value that’s to be applied to the limit
You can see for example the content of this file as below:
To edit the maximum number of opened files for all users, you can add, for example, add to the end of the file the lines below:
After this, you need to edit the file /etc/pam.d/login
Then save the file. You can check the result as below:
3) System wide limit
On Linux system we have file-max which is the maximum File Descriptors (FD) and the default settings for ulimit and file-max assume that several users would share the system. This is why these settings limit the number of resources used by each user. You can increase the limit of opened files in Linux by editing the /etc/sysctl.conf or by editing the directive fs.file-max
You can see the current value for opened files with the command
And you can edit the default value as below:
You can check the result as below
By using the sysctl command, the modifications are applied until the next reboot. To make the configuration persistent, you can directly edit the /etc/sysctl.conf file as below:
If you list the content of /proc/sys/fs/file-max , you will see that the change didn’t directly take effect. Now you need to directly apply the changes with the -p option of the sysctl command as below:
Источник
How to set ulimit and file descriptors limit on Linux Servers
Introduction: Challenges like number of open files in any of the production environment has become common now a day. Since many applications which are Java based and Apache based, are getting installed and configured, which may lead to too many open files, file descriptors etc. If this exceeds the default limit that is set, then one may face access control problems and file opening challenges. Many production environments come to standstill kind of situations because of this.
Luckily, we have “ulimit” command in any of the Linux based server, by which one can see/set/get number of files open status/configuration details. This command is equipped with many options and with this combination one can set number of open files. Following are step-by-step commands with examples explained in detail.
To see what is the present open file limit in any Linux System
To get open file limit on any Linux server, execute the following command,
The above number shows that user can open ‘146013’ file per user login session.
This clearly indicates that individual Linux operating systems have different number of open files. This is based on dependencies and applications which are running in respective systems.
ulimit command :
As the name suggests, ulimit (user limit) is used to display and set resources limit for logged in user.When we run ulimit command with -a option then it will print all resources’ limit for the logged in user. Now let’s run “ulimit -a” on Ubuntu / Debian and CentOS systems,
Ubuntu / Debian System,
CentOS System
As we can be seen here different OS have different limits set. All these limits can be configured/changed using “ulimit” command.
To display the individual resource limit then pass the individual parameter in ulimit command, some of parameters are listed below:
- ulimit -n –> It will display number of open files limit
- ulimit -c –> It display the size of core file
- umilit -u –> It will display the maximum user process limit for the logged in user.
- ulimit -f –> It will display the maximum file size that the user can have.
- umilit -m –> It will display the maximum memory size for logged in user.
- ulimit -v –> It will display the maximum memory size limit
Use below commands check hard and soft limits for number of open file for the logged in user
How to fix the problem when limit on number of Maximum Files was reached ?
Let’s assume our Linux server has reached the limit of maximum number of open files and want to extend that limit system wide, for example we want to set 100000 as limit of number of open files.
Use sysctl command to pass fs.file-max parameter to kernel on the fly, execute beneath command as root user,
Above changes will be active until the next reboot, so to make these changes persistent across the reboot, edit the file /etc/sysctl.conf and add same parameter,
save and exit file,
Run the beneath command to make above changes into effect immediately without logout and reboot.
Now verify whether new changes are in effect or not.
Use below command to find out how many file descriptors are currently being utilized:
Note:- Command “sysctl -p” is used to commit the changes without reboot and logout.
Set User level resource limit via limit.conf file
“/etc/sysctl.conf” file is used to set resource limit system wide but if you want to set resource limit for specific user like Oracle, MariaDB and Apache then this can be achieved via “/etc/security/limits.conf” file.
Sample Limit.conf is shown below,
Let’s assume we want to set hard and soft limit on number of open files for linuxtechi user and for oracle user set hard and soft limit on number of open process, edit the file “/etc/security/limits.conf” and add the following lines
Save & exit the file.
Note: In case you want to put resource limit on a group instead of users, then it can also be possible via limit.conf file, in place of user name , type @ and rest of the items will be same, example is shown below,
Verify whether new changes are in effect or not,
Note: Other majorly used command is “ lsof ” which is used for finding out “how many files are opened currently”. This command is very helpful for admins.
Conclusion:
As mentioned in the introduction section “ulimit” command is very powerful and helps one to configure and make sure application installations are smoother without any bottlenecks. This command helps in fixing many of the number of file limitations in Linux based servers.
Источник