Nov 4, 2017

The default Linux limits are usually too low

Some application require higher limits on open files and sockets (like a proxy web server, database). .

Linux

Per shell/script

The shell limits are governed by ulimit. The status is checked with ulimit -a. For example to change the open files limit from 1024 to 10240 do:
# ulimit -n 10240                    # This is only valid within the shell
The ulimit command can be used in a script to change the limits for the script only.

Per user/process

Login users and applications can be configured in /etc/security/limits.conf. For example:
# cat /etc/security/limits.conf
*   hard    nproc   250              # Limit user processes
asterisk hard nofile 409600          # Limit application open files

System wide

Kernel limits are set with sysctl. Permanent limits are set in /etc/sysctl.conf.
# sysctl -a                          # View all system limits
# sysctl fs.file-max                 # View max open files limit
# sysctl fs.file-max=102400          # Change max open files limit
# echo "1024 50000" > /proc/sys/net/ipv4/ip_local_port_range  # port range
# cat /etc/sysctl.conf
fs.file-max=102400                   # Permanent entry in sysctl.conf
# cat /proc/sys/fs/file-nr           # How many file descriptors are in use

FreeBSD

Per shell/script

Use the command limits in csh or tcsh or as in Linux, use ulimit in an sh or bash shell.

Per user/process

The default limits on login are set in /etc/login.conf. An unlimited value is still limited by the system maximal value.

System wide

Kernel limits are also set with sysctl. Permanent limits are set in /etc/sysctl.conf or/boot/loader.conf. The syntax is the same as Linux but the keys are different.
# sysctl -a                          # View all system limits
# sysctl kern.maxfiles=XXXX          # maximum number of file descriptors
kern.ipc.nmbclusters=32768           # Permanent entry in /etc/sysctl.conf
kern.maxfiles=65536                  # Typical values for Squid
kern.maxfilesperproc=32768
kern.ipc.somaxconn=8192              # TCP queue. Better for apache/sendmail
# sysctl kern.openfiles              # How many file descriptors are in use
# sysctl kern.ipc.numopensockets     # How many open sockets are in use
# sysctl net.inet.ip.portrange.last=50000 # Default is 1024-5000
# netstat -m                         # network memory buffers statistics
See The FreeBSD handbook Chapter 11http://www.freebsd.org/handbook/configtuning-kernel-limits.html for details. And also FreeBSD performance tuninghttp://serverfault.com/questions/64356/freebsd-performance-tuning-sysctls-loader-conf-kernel

Solaris

The following values in /etc/system will increase the maximum file descriptors per proc:
set rlim_fd_max = 4096               # Hard limit on file descriptors for a single proc
set rlim_fd_cur = 1024               # Soft limit on file descriptors for a single proc