20100522 open files - plembo/onemoretech GitHub Wiki

title: Open files link: https://onemoretech.wordpress.com/2010/05/22/open-files/ author: lembobro description: post_id: 156 created: 2010/05/22 22:03:44 created_gmt: 2010/05/22 22:03:44 comment_status: open post_name: open-files status: publish post_type: post

Open files

Just some quick notes on setting how many files can be open system-wide and by a particular user.

On UNIX, and UNIX-like systems like Linux, every process is represented by a file handle. Limits are set by default on the number that can be opened simultaneously in order to preserve system performance. Unfortunately if you’re running and Internet web server and it runs out of file handles customers will not be able to connect and the daemon might even crash. I’ve had this happen more than once on LDAP servers that were severely resource-constrained, and one some that weren’t.

RHEL (Red Hat Enterprise Linux) and most other Linux distros allow you to set the system-wide limit in /etc/sysctl.conf and then run sysctl -p to make it effective without restarting the machine. To give myself maximum flexibility, I usually set it to a bigger number than I think I’ll need, e.g. 65536. The sysctl.conf entry would look like:

fs.file-max = 65536

To effect this change on a running system, run /sbin/sysctl -p as root.

This alone won’t make any more than 1024 (the default) open files available to most users.

To specify the file handle limit for users, you need to edit /etc/security/limits.conf.

dsadmin - nofile 8196

Sets the file handle limit for the dsadmin user to 8196, which is adequate for most programs.

* -  nofile 8196

Sets the same limit for all system users (the ‘*’).

Finally, setting

ulimit -n 8196

in the user’s .bashrc is the last step, to make sure their environment is set correctly over time. The ulimit command lists the number of file handles available for the running user. To get list all the kernel parameters set for a user, you would run ulimit -a as the user.

[dsadmin@example ~]$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 26612
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 8196
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 26612
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Postscript:

Taking my cue from various 3rd party application vendors, I’ve begun to bump up my file handles to 65536 for everyone.

That means going into /etc/security/limits.conf and making it look like:

* - nofile 65536

Keep in mind this won’t be effective for a user until they log in (or in the case of an application, it gets recycled: /etc/init.d/httpd restart).

Copyright 2004-2019 Phil Lembo