- How to Change Open File Limit in Linux
- Check Open File Limits in Linux
- Change Open File Limits in Linux
- Permanently Set Open File Limits in Linux
- 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
- How do I change the number of open files limit in Linux? [closed]
- 4 Answers 4
- «Max open files» for working process
- 8 Answers 8
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
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.
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.
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 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
# cat /proc/sys/fs/file-max 818354
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
# ulimit -Hn 4096
Check Soft Limits in Linux
# ulimit -Sn 1024
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.
# su marin $ ulimit -Sn 1024
$ ulimit -Hn 4096
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:
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:
## Example hard limit for max opened files marin hard nofile 4096 ## Example soft limit for max opened files marin soft nofile 1024
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.
How do I change the number of open files limit in Linux? [closed]
When running my application I sometimes get an error about too many files open . Running ulimit -a reports that the limit is 1024. How do I increase the limit above 1024? Edit ulimit -n 2048 results in a permission error.
I just went through this on Centos 7 (same on RHEL) and made a blog post covering it because I had so much trouble even with all these posts: coding-stream-of-consciousness.com/2018/12/21/…. Often along with open files, you need to increase nproc which actually resides in multiple settings files. and if you use systemd/systemctl that has its own separate settings. It’s kind of nuts.
4 Answers 4
You could always try doing a ulimit -n 2048 . This will only reset the limit for your current shell and the number you specify must not exceed the hard limit
Each operating system has a different hard limit setup in a configuration file. For instance, the hard open file limit on Solaris can be set on boot from /etc/system.
set rlim_fd_max = 166384 set rlim_fd_cur = 8192
On OS X, this same data must be set in /etc/sysctl.conf.
kern.maxfilesperproc=166384 kern.maxfiles=8192
Under Linux, these settings are often in /etc/security/limits.conf.
There are two kinds of limits:
- soft limits are simply the currently enforced limits
- hard limits mark the maximum value which cannot be exceeded by setting a soft limit
Soft limits could be set by any user while hard limits are changeable only by root. Limits are a property of a process. They are inherited when a child process is created so system-wide limits should be set during the system initialization in init scripts and user limits should be set during user login for example by using pam_limits.
There are often defaults set when the machine boots. So, even though you may reset your ulimit in an individual shell, you may find that it resets back to the previous value on reboot. You may want to grep your boot scripts for the existence ulimit commands if you want to change the default.
«Max open files» for working process
Additional info: my process is ‘java’. I need to increase “Max open files” without stopping the process.
8 Answers 8
Another option is to use the prlimit command (from the util-linux package). For example if you want to set the maximum number of open files for a running process to 4096:
I’m using linux mint (Ubuntu based) and I can’t find it in the default installation. Normally it’s part of the package util-linux but eve this package on Mint doesn’t bring this command.
➜ ~ lsb_release -d Description: Ubuntu 16.04.1 LTS ➜ ~ whereis prlimit prlimit: /usr/bin/prlimit /usr/share/man/man2/prlimit.2.gz /usr/share/man/man1/prlimit.1.g
As a system administrator: The /etc/security/limits.conf file controls this on most Linux installations; it allows you to set per-user limits. You’ll want a line like myuser — nofile 1000 .
Within a process: The getrlimit and setrlimit calls control most per-process resource allocation limits. RLIMIT_NOFILE controls the maximum number of file descriptors. You will need appropriate permissions to call it.
I don’t know of any. If there is, I suspect you’ll find it buried deep in some Linux-specific programming guide, because I can’t fathom where else standard POSIX would put it.
Sometimes limits.conf is not taken into account and you have to set DefaultLimitNOFILE in /etc/systemd/system.conf and user.conf .
You could use gdb, break into the process, call the aforementioned syscalls to raise the limit you’re interested in, then continue the job and exit gdb. I’ve edited things on the fly this way a few times.
Your app wouldn’t be down, but just frozen for the moment while you performed the call. if you’re quick (or you script it!), it’ll likely not be noticeable.
Yeah, this is somewhat like tweaking the Matrix to make things work. stop the world, edit a variable, continue the world. oh! DeJaVu! I am the One! (well, when I have gdb!)
echo -n "Max open files=20:20" > /proc/$pid/limits
. works in RHEL5.5 and RHEL6.7.
Note that the «-n» is mandatory; a trailing newline will generate a complaint about invalid arguments.
This link details how to change this system wide or per user.
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 as follows (login as the root):
$ sysctl -w fs.file-max=100000
You need to edit /etc/sysctl.conf file and put following line so that after reboot the setting will remain as it is
Thanks for a link. But I’ve found it before asking 🙂 But the problem is to change parameter without stopping process (on runtime) and for process-wide.
@wako: It can’t be done from outside the process (unless you’re running one of the very latest development kernels, which is unlikely)
Yes, it is possible to increase the limits in /proc//limits at run time. Just find the pid and execute below command:
echo -n "Max open files=20:20" > /proc/$pid/limits
The following commands give the max # of open files per process permissible by default limits (soft and hard respectively):
You can use a program or a command to change these limits. Look at ulimit (man ulimit).
On Ubuntu 16.04, with a rethinkdb process running, none of these solutions worked.
I kept getting error: accept() failed: Too many open files.
What ultimately worked was this in my /etc/security/limits.conf file. Note the nproc in addition to the nofile. As I understand it, root needs to specified separately.
* soft nofile 200000 * hard nofile 1048576 root soft nofile 200000 root hard nofile 1048576 * soft nproc 200000 * hard nproc 1048576 root soft nproc 200000 root hard nproc 1048576
You can see the system max files by running cat /proc/sys/fs/file-max . I just set mine to a high maximum well within reason for the size of the server.
You can verify the max open files your process is allowed by running cat /proc//limits .