Linux set file limits

On Linux — set maximum open files to unlimited. Possible?

Is it possible to set the maximum number of open files to some «infinite» value or must it be a number? I had a requirement to set the descriptor limit for a daemon user to be «unlimited» and I’m trying to determine if that’s possible or how to do it. I’ve seen some mailing lists refer to a «max» value that can be used (as in: «myuser hard nofile max», but so far the man pages and references I’ve consulted don’t back that up. If I can’t use ‘max’ or similar, I’d like to know how to determine what the max number of files is (theoretically) so I have some basis for whatever number I pick. I don’t want to use 100000000 or something if there’s a more reasonable way to get an upper bound. I’m using RHEL 5 if it’s important. Update: I’m an idiot when it comes to writing questions. Ideally I’d like to do this in the limits.conf file (which is where «max» would come from). Does that change any answers? Thanks for the comments. This is for a JBOSS instance and not a daemon I’m writing so I don’t know if setrlimit() is useful to me. However, Jefromi — I do like the definition of Infinity 🙂 I saw a post that suggests a file descriptor is «two shorts and a pointer» so I should be able to calculate the approximate upper bound.

2 Answers 2

POSIX allows you to set the RLIMIT_NOFILE resource limit to RLIM_INFINITY using setrlimit() . What this means is that the system will not enforce this resource limit. Of course, you will still be limited by the implementation (e.g. MAXINT ) and any other resource limitations (e.g. available memory).

Update: RHEL 5 has a maximum value of 1048576 (2 20 ) for this limit ( NR_OPEN in /usr/include/linux/fs.h ), and will not accept any larger value including infinity, even for root. So on RHEL 5 you can use this value in /etc/security/limits.conf and that is as close as you are going to get to infinity.

Not long ago a Linux kernel patch was applied to allow this limit to be set to infinity, however it has since been reverted as a result of unintended consequences.

Potentially useful: Ubuntu 14.04 also has this 2^20 limits in /etc/security/limits.conf . Anything past that will reset the limit to the default 2^10

Читайте также:  Linux изменить домашнюю папку пользователя

Источник

How to Change Open File Limit in Linux

In Linux, there are limits defined by the system for anything that consumes resources. For example, there are limits on how many arguments can be passed to a certain command, how many threads can run at the same time, etc.

Similarly, there is a limit on the number of open files. As you might know, an open file is actively being used in some of the other programs and hence consumes memory.
You can view and modify the open file limit with the command ‘ulimit‘.

Now, there are two types of limits defined: Hard limit and Soft limit.

  • The hard limit for open files is a statically set value, and can only be altered by the ‘root‘ user of Linux.
  • The soft limit is one that can be altered by processes dynamically, i.e., in the runtime, if the process is in need of more open files than the number allowed by the soft limit.

Needless to say, a soft limit is something that could lead to security issues.

Check Open File Limits in Linux

To view or list the hard limit of files, run:

Similarly, for viewing soft limit, run:

Here the ‘S’ and ‘H’ stand for soft and hard limits respectively, whereas the ‘n’ denotes the number of open files.

Change Open File Limits in Linux

Let’s now try to modify these limits by simply adding a third argument for the new value of the limit after ‘Sn’ or ‘Hn’ .

$ ulimit -Sn 5000 $ ulimit -Hn 5000

Change Open File Limit in Linux

However, the limits altered in this way remain modified temporarily till the session (i.e. the terminal) is open. Follow the steps below to permanently modify the limits.

Permanently Set Open File Limits in Linux

Open the file ‘/etc/security/limits.conf‘ in ‘vim‘ or any editor of your choice. Note that this is a write-protected file and should be opened with ‘sudo‘ or as an administrator.

$ sudo vim /etc/security/limits.conf

Note that the limits can be set for all users, or for individual users. The syntax to set a hard limit is:

The ‘*’ is signifying that the limit is for all users. The second string on the line is a type of limit, which is ‘hard’ in this case.

The third string is the limit that we want to modify. In our case, it is ‘nofile’, i.e. number of files. Like this other limits can also be set here, Eg. ‘noproc’ for a number of processes. Finally, the fourth string is the new limit to be set for the number of files.

If you want to set the limit only for a particular user, you can do so like the following:

Here ‘abhi’ is the name of the user.

Now, we will add these lines to the file and then see if the limit has been altered.

Set Open File Limit in Linux

These limits will require the user to logout and log back in, as the older limits are still being considered by running programs. Run ulimit again to verify if the limits are indeed changed.

Читайте также:  Linux show files by date
Conclusion

In this article, we have seen how to change the limit on the number of open files in Linux. Run ‘ulimit —help’ if you want to know the command in more detail.

Thanks for reading, and let us know your thoughts or questions in the comments below!

Источник

How to increase the open file limits on Ubuntu

On a Linux-based system such as Ubuntu, we occasionally face problems such as “too many open files,” especially when you install and work in platforms like Apache web server and Oracle database. The encountered error signifies that our server has opened the files up to the default maximum number. This issue occurs when the system imposes resource constraints on any user or session. For instance, the maximum size that can be locked into memory, the maximum size of created file, the maximum CPU time that can be utilized, the maximum number of processes that are allowed to execute, and the maximum virtual memory size that can be used; these are all examples of resource limitations in an Ubuntu system.

What are the limits types in Ubuntu

Limits in an Ubuntu system are of two types:

  • Soft limit: This type of limit indicates the current value of the session or user.. An Ubuntu user has the option to increase its value to the hard limit.
  • Hard Limit: The superuser/root sets the maximum allowable limit for a user or session, known as the hard limit.

Now, we will explain the procedure to increase the open file limits on Ubuntu. So let’s start!

How to increase the open file limits on Ubuntu

First of all, open your Ubuntu terminal by pressing “CTRL+ALT+T”. Now, we will check out the current limits of the resources. For this, we will execute the “unlimit” command.

What is unlimit command in Ubuntu

The “unlimit” is a Linux shell command utilized to set, view, or limit the current user’s resources. It also extracts information about how many open file descriptors each process has. Many Ubuntu users execute this command to limit the resources which are in the use of a process.

How to check current limits in Ubuntu

To view the current limits set, add the “-a” option in the “unlimit” command:

Execution of the above-given command will show you the following output:

How to check the soft and hard open file limits in Ubuntu

To view the current soft limit, execute the unlimit command with the “-Sn” option:

From the below-given output, you can see that “1024” is the soft limits of the open files in our Ubuntu system:

The “-Hn” option is added to the “unlimit” command for retrieving the hard limit of the open files:

How to increase the open file limits for the current session in Ubuntu

As mentioned earlier, you can utilize the “unlimit” command for setting the current session limits. If you want to increase the open file limits for your current Ubuntu session, choose a value between the soft and hard limits and add that specific numerical value with the “-n” option. Adding the “-n” option to the “unlimit” command lets you increase the open file limits:

Читайте также:  Linux системное программирование pdf

How to increase per-user open file limits in Ubuntu

You can also increase the open file limits for a specific user. To do so, open the limits configuration file “/etc/security/limits.conf” in the “nano” editor:

In the “nano” editor, the limit configuration file will look like this:

Now, add the following content in the “/etc/security/limits.conf” file:

linuxhint soft nproc 100000

linuxhint hard nproc 100000

linuxhint soft nofile 100000

linuxhint hard nofile 100000

Press “CTRL+O” and save the changes we made in the “/etc/security/limits.conf”:

In the next step, we will restrict the number of system resources a user can get in a single session by enabling the pam_limits. For this, you have to edit the “/etc/pam.d/common-session” file in your “nano” editor:

Now, add this line to enable the pam_limits for the current session:

Press “CTRL+O” to save the added content of “ /etc/pam.d/common-session” file:

How to increase system-wide open file limits in Ubuntu

Ubuntu also provides you the option to increase system-wide open file limits. For this purpose, we will edit the “/etc/sysctl.conf” file:

You can set the maximum number of file handles that the Linux kernel will allocate by using the “fs.file-max” parameter. Now, add the below-given line in the “sysctl.conf” file:

Setting the value “2097152” for “fs.file-max” parameter will set this value as maximum number of file handles:

Again, press “CTRL+O” to save the “sysctl.conf” file:

The “sysctl” command with the “-p” option will load the kernel settings from the “sysctl.conf” file. For applying the changes we have made, execute the below-given “sysctl” command in your terminal:

The execution of the above-given command will increase the maximum number of open files across the entire system:

Conclusion

In Ubuntu, many applications, such as the Apache web server or the Oracle databases, require a higher open file limit, resulting in an excessive number of open files, file descriptors, etc. If the number of open files exceeds the default limit, file opening difficulties and access control issues may arise. This article showed you how you can increase the open file limits in your Ubuntu system. Moreover, you have also seen how to check and increase the system-wide and per-user limits of opened files.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.

Источник

Оцените статью
Adblock
detector